001
002
003
004
005
006
007
008
009
010
011
012
013 #include "GaudiKernel/ITHistSvc.h"
014
015 #include <TMath.h>
016 #include <TH1.h>
017 #include <TH1F.h>
018 #include <TH2F.h>
019
020 #include "JetTagCalibration/CalibrationBroker.h"
021
022 #include "TrkNeuralNetworkUtils/TTrainedNetwork.h"
023 #include "JetTagTools/JetFitterNNTool.h"
024 #include "TrkNeuralNetworkUtils/NeuralNetworkToHistoTool.h"
025 #include "JetTagInfo/JetFitterTagInfo.h"
026
027 #include "JetTagTools/JetFitterVariablesNormalization.h"
028 #include "JetTagTools/JetFitterPtEtaCategories.h"
029
030 #include <TString.h>
031
032 namespace Analysis {
033
034
035 JetFitterNNTool::JetFitterNNTool(const std::string& name,
036 const std::string& n, const IInterface* p):
037 AthAlgTool(name, n,p),
038 m_calibrationDirectory("JetFitter"),
039 m_calibrationSubDirectory("NeuralNetwork"),
040 m_networkToHistoTool("Trk::NeuralNetworkToHistoTool"),
041 m_useCombinedIPNN(true),
042 m_calibrationTool("BTagCalibrationBroker"),
043 m_maximumRegisteredLayers(4)
044 {
045 declareProperty("CalibrationDirectory",m_calibrationDirectory);
046 declareProperty("CalibrationSubDirectory",m_calibrationSubDirectory);
047
048 declareProperty("NeuralNetworkToHistoTool",m_networkToHistoTool);
049 declareProperty("useCombinedIPNN",m_useCombinedIPNN);
050
051 declareProperty("calibrationTool", m_calibrationTool);
052
053 declareProperty("maximumRegisteredLayers",m_maximumRegisteredLayers);
054
055 declareInterface<JetFitterNNTool>(this);
056
057 }
058
059
060
061
062
063 JetFitterNNTool::~JetFitterNNTool() {
064
065 std::map<std::string,TTrainedNetwork*>::iterator NNbegin=m_NN.begin();
066 std::map<std::string,TTrainedNetwork*>::iterator NNend=m_NN.end();
067
068 for (std::map<std::string,TTrainedNetwork*>::iterator NNiter=NNbegin;
069 NNiter!=NNend;
070 ++NNiter)
071 {
072 delete (*NNiter).second;
073 (*NNiter).second=0;
074 }
075
076 }
077
078 StatusCode JetFitterNNTool::initialize() {
079
080
081 StatusCode sc = m_calibrationTool.retrieve();
082 if (sc.isFailure())
083 {
084 ATH_MSG_FATAL(" Could not retrieve " << m_calibrationTool << ". Aborting...");
085 return sc;
086 } else ATH_MSG_INFO(" Retrieved: " << m_calibrationTool);
087
088 sc = m_networkToHistoTool.retrieve();
089 if (sc.isFailure())
090 {
091 ATH_MSG_FATAL(" Could not retrieve " << m_networkToHistoTool << ". Aborting...");
092 return sc;
093 } else ATH_MSG_INFO(" Retrieved: " << m_networkToHistoTool << ". ");
094
095 ATH_MSG_INFO("Calibration setting: cannot use Neural Network with more than: " << m_maximumRegisteredLayers << ".");
096
097
098 initializeCalibrationFile();
099
100 ATH_MSG_INFO(" Initialization of JetFitterNNTool succesfull");
101 return StatusCode::SUCCESS;
102 }
103
104 void JetFitterNNTool::initializeCalibrationFile()
105 {
106
107 TString directory(m_calibrationSubDirectory);
108 directory+="/";
109 if (m_useCombinedIPNN)
110 {
111 directory+="comb";
112 }
113 else
114 {
115 directory+="standalone";
116 }
117 directory+="/";
118
119 m_calibrationTool->registerHistogram(m_calibrationDirectory,
120 std::string((const char*)(directory+"LayersInfo")));
121
122 Int_t nHidden=m_maximumRegisteredLayers-2;
123
124 for (Int_t i=0;i<nHidden+1;++i)
125 {
126
127 TString weightName("Layer");
128 weightName+=i;
129 weightName+="_weights";
130
131 TString thresholdName("Layer");
132 thresholdName+=i;
133 thresholdName+="_thresholds";
134
135 m_calibrationTool->registerHistogram(m_calibrationDirectory,
136 std::string((const char*)(directory+weightName)));
137
138 m_calibrationTool->registerHistogram(m_calibrationDirectory,
139 std::string((const char*)(directory+thresholdName)));
140
141 }
142
143 ATH_MSG_DEBUG(" Registered NN histograms with directory: " << m_calibrationDirectory << " and subdirectory " << directory);
144
145 m_calibrationTool->printStatus();
146
147 }
148
149
150 StatusCode JetFitterNNTool::finalize() {
151
152
153
154 ATH_MSG_INFO(" Finalization of JetFitterNNTool succesfull");
155 return StatusCode::SUCCESS;
156 }
157
158 void JetFitterNNTool::loadCalibration(const std::string & jetauthor) {
159
160 std::vector<TH1*> retrievedHistos;
161
162
163
164 TString directory(m_calibrationSubDirectory);
165 directory+="/";
166 if (m_useCombinedIPNN)
167 {
168 directory+="comb";
169 }
170 else
171 {
172 directory+="standalone";
173 }
174 directory+="/";
175
176 std::pair<TH1*, bool> histoLayers = m_calibrationTool->retrieveHistogram(m_calibrationDirectory,
177 jetauthor,
178 std::string((const char*)(directory+TString("LayersInfo"))));
179
180 if (histoLayers.second==false && m_NN[jetauthor]!=0)
181 {
182 return;
183 }
184 else if(histoLayers.second==true)
185 {
186 ATH_MSG_DEBUG(" HistoLayers in " << directory << " was updated. Switching updated now to false ... ");
187 m_calibrationTool->updateHistogramStatus(m_calibrationDirectory,
188 jetauthor,
189 std::string((const char*)(directory+TString("LayersInfo"))),
190 false);
191 }
192
193
194
195 TH1F* myHistoLayers=dynamic_cast<TH1F*>(histoLayers.first);
196
197 if (myHistoLayers==0)
198 {
199 ATH_MSG_ERROR(" Cannot retrieve LayersInfo histogram ");
200 return;
201 }
202
203 retrievedHistos.push_back(myHistoLayers);
204
205 Int_t nHidden=myHistoLayers->GetNbinsX()-2;
206
207 ATH_MSG_INFO(" Retrieving calibration for NN with: " << nHidden << " hidden layers.");
208
209 for (Int_t i=0;i<nHidden+1;++i)
210 {
211
212 TString weightName("Layer");
213 weightName+=i;
214 weightName+="_weights";
215
216 TString thresholdName("Layer");
217 thresholdName+=i;
218 thresholdName+="_thresholds";
219
220 std::pair<TH1*, bool> weightHisto = m_calibrationTool->retrieveHistogram(m_calibrationDirectory,
221 jetauthor,
222 std::string((const char*)(directory+weightName)));
223
224 if (weightHisto.second==true)
225 {
226 m_calibrationTool->updateHistogramStatus(m_calibrationDirectory,
227 jetauthor,
228 std::string((const char*)(directory+weightName)),
229 false);
230 }
231
232
233 TH2F* myWeightHisto=dynamic_cast<TH2F*>(weightHisto.first);
234
235 if (myWeightHisto==0)
236 {
237 ATH_MSG_ERROR(" Cannot retrieve histogram: " << weightName);
238 }
239 else
240 {
241 ATH_MSG_VERBOSE(" Retrieved histo: " << weightName << " for channel : " << jetauthor <<
242 " the first bin content of the weight 2d histo is: " << myWeightHisto->GetBinContent(1,1));
243 }
244
245
246 retrievedHistos.push_back(myWeightHisto);
247
248 std::pair<TH1*, bool> thresholdHisto = m_calibrationTool->retrieveHistogram(m_calibrationDirectory,
249 jetauthor,
250 std::string((const char*)(directory+thresholdName)));
251
252 if (thresholdHisto.second==true)
253 {
254 m_calibrationTool->updateHistogramStatus(m_calibrationDirectory,
255 jetauthor,
256 std::string((const char*)(directory+thresholdName)),
257 false);
258 }
259
260 TH1F* myThresholdHisto=dynamic_cast<TH1F*>(thresholdHisto.first);
261
262 if (myThresholdHisto==0)
263 {
264 ATH_MSG_ERROR(" Cannot retrieve histogram: " << thresholdName);
265 }
266
267 retrievedHistos.push_back(myThresholdHisto);
268
269 }
270
271 TTrainedNetwork* NN=m_NN[jetauthor];
272 if (NN!=0)
273 {
274 delete NN;
275 NN=0;
276 ATH_MSG_DEBUG(" Istantiating TTrainedNetwork for jet author: " << jetauthor);
277 }
278
279 m_NN[jetauthor]=m_networkToHistoTool->fromHistoToTrainedNetwork(retrievedHistos);
280
281
282
283 }
284
285 void JetFitterNNTool::fillLikelihoodValues(JetFitterTagInfo & myTagInfo,
286 const std::string & jetauthor,
287 double jetpT,
288 double jeteta,
289 double IP3dlike) {
290
291 if (jetauthor=="") {
292 ATH_MSG_WARNING(" Hypothesis or jetauthor is empty. No likelihood value given back. ");
293 }
294
295
296
297 loadCalibration(jetauthor);
298
299
300 TTrainedNetwork* NN=m_NN[jetauthor];
301 if (NN==0)
302 {
303 ATH_MSG_WARNING(" JetFitter NN instance not found: cannot do any calculation...");
304 return;
305 }
306
307 std::vector<double> weights=myTagInfo.tagLikelihood();
308
309 if (weights.size()!=0)
310 {
311 weights.clear();
312 ATH_MSG_WARNING(" The probability values for the tagging were already filled. Inconsistency found...");
313 }
314
315
316 if (m_useCombinedIPNN)
317 {
318 if (NN->getnInput()!=9)
319 {
320 ATH_MSG_ERROR(" Expect 9 input nodes in NN. Have: " << NN->getnInput());
321 }
322 }
323 else
324 {
325 if (NN->getnInput()!=8)
326 {
327 ATH_MSG_ERROR(" Expect 8 input nodes in NN. Have: " << NN->getnInput());
328 }
329 }
330
331 std::vector<Double_t> inputData;
332
333 inputData.push_back(norm_nVTX(myTagInfo.nVTX()));
334 inputData.push_back(norm_nTracksAtVtx(myTagInfo.nTracksAtVtx()));
335 inputData.push_back(norm_nSingleTracks(myTagInfo.nSingleTracks()));
336 inputData.push_back(norm_energyFraction(myTagInfo.energyFraction()));
337 inputData.push_back(norm_mass(myTagInfo.mass()));
338 inputData.push_back(norm_significance3d(myTagInfo.significance3d()));
339
340 if (m_useCombinedIPNN)
341 {
342 if (IP3dlike<-1e6)
343 {
344 ATH_MSG_WARNING(" Expected a value for the IP3D Tagger... it is: " << IP3dlike << " This is an ERROR not a WARNING... (No Tagging done)");
345 return;
346 }
347 else
348 {
349 inputData.push_back(norm_IP3D(IP3dlike));
350 }
351 }
352
353 inputData.push_back(norm_cat_pT(getPtCategory(jetpT).first));
354 inputData.push_back(norm_cat_eta(getEtaCategory(jeteta).first));
355
356 ATH_MSG_DEBUG(" Pt is: "<< jetpT
357 << " cat " << norm_cat_pT(getPtCategory(jetpT).first)
358 << ". (Check scale...!) ");
359
360 std::vector<double> outputValues=NN->calculateOutputValues(inputData);
361
362 ATH_MSG_DEBUG(" NN Discriminator b: " << outputValues[0] <<
363 " c: " << outputValues[1] << " u: " << outputValues[2]);
364
365 weights.push_back(outputValues[0]);
366 weights.push_back(outputValues[2]);
367 weights.push_back(outputValues[1]);
368
369 myTagInfo.setTagLikelihood(weights);
370
371 }
372
373
374 }
375
| Due to the LXR bug, the updates fail sometimes to remove references to deleted files. The Saturday's full rebuilds fix these problems |
|
This page was automatically generated by the
LXR engine.
|
|