ATLAS Offline Software
DecoratePromptLeptonImproved.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Local
8 
9 // ROOT
10 #include "TMVA/Config.h"
11 #include "TH1.h"
12 
13 // C/C++
14 #include <cmath>
15 #include <iostream>
16 #include <sstream>
17 #include <sys/stat.h>
18 
19 //======================================================================================================
20 Prompt::DecoratePromptLeptonImproved::DecoratePromptLeptonImproved(const std::string& name, ISvcLocator* pSvcLocator):
21  AthAlgorithm(name, pSvcLocator),
22  m_BDTVarKey(Def::NONE)
23 {}
24 
25 //=============================================================================
27 {
28  ATH_MSG_DEBUG("Initializing DecoratePromptLeptonImproved...");
29  ATH_MSG_DEBUG("m_leptonsName = " << m_leptonsName);
30  if (m_printTime)
31  {
32  //
33  // Reset timers
34  //
35  m_timerAll .Reset();
36  m_timerExec.Reset();
37  m_timerMuon.Reset();
38  m_timerElec.Reset();
39 
40  //
41  // Start full timer
42  //
43  m_timerAll.Start();
44  }
45 
46  ATH_CHECK(m_trackJetsKey.initialize());
47  ATH_CHECK(m_primaryVertexKey.initialize());
48  ATH_CHECK(m_clusterContainerKey.initialize());
49 
50  ATH_MSG_DEBUG("Initializing " << m_electronsKey);
51 
52  ATH_CHECK(m_electronsKey.initialize());
53  ATH_CHECK(m_muonsKey.initialize());
54 
55  ATH_MSG_DEBUG("Number of int vars to read: " << m_stringIntVars.size());
56  ATH_MSG_DEBUG("Number of float vars to read: " << m_stringFloatVars.size());
57 
58  // Setup variable holder
59  m_vars = std::make_unique<Prompt::VarHolder>();
60 
61  //
62  // Read vector<string> vars into vector<Var> and append
63  //
64  m_intVars = m_vars->readVectorVars(m_stringIntVars);
65  m_floatVars = m_vars->readVectorVars(m_stringFloatVars);
66 
67  m_allVars.insert(m_allVars.end(), m_intVars .begin(), m_intVars .end());
68  m_allVars.insert(m_allVars.end(), m_floatVars.begin(), m_floatVars.end());
69 
70  m_varTMVA.resize(m_allVars.size());
71 
72 
73  //
74  // Get key for recording BDT output
75  //
76  m_BDTVarKey = m_vars->registerDynamicVar(m_BDTName);
77 
78  if(m_BDTVarKey == Def::NONE) {
79  ATH_MSG_ERROR("Failed to create key for BDT name=" << m_BDTName);
80  return StatusCode::FAILURE;
81  }
82 
83  //
84  // Fill decorator maps
85  //
86  ATH_CHECK(initializeDecorators());
87 
88  //
89  // Initialize const accessors
90  //
91  initializeConstAccessors();
92 
93  ATH_MSG_DEBUG("Initialized DecoratePromptLeptonImproved.");
94 
95  //
96  // Initialize TMVA Reader
97  //
98  ATH_CHECK(initializeTMVAReader());
99 
100  ATH_MSG_DEBUG("Initialized TMVA Reader.");
101 
102  return StatusCode::SUCCESS;
103 }
104 
105 //=============================================================================
107 {
108  if(m_printTime) {
109  //
110  // Print full time stopwatch
111  //
112  m_timerAll.Stop();
113 
114  ATH_MSG_INFO("Real time: " << m_timerAll.RealTime() << "\t CPU time: " << m_timerAll.CpuTime());
115 
116  ATH_MSG_INFO("Execute time: " << PrintResetStopWatch(m_timerExec));
117  ATH_MSG_INFO("Muon time: " << PrintResetStopWatch(m_timerMuon));
118  ATH_MSG_INFO("Elec time: " << PrintResetStopWatch(m_timerElec));
119  }
120 
121  return StatusCode::SUCCESS;
122 }
123 
124 
125 //=============================================================================
127 {
128 
129  //
130  // Start execute timer
131  //
132  TimerScopeHelper timer(m_timerExec);
133 
134  //
135  // Retrieve containers from evtStore
136  //
137  SG::ReadHandle<xAOD::JetContainer> trackJets(m_trackJetsKey);
138  SG::ReadHandle<xAOD::VertexContainer> vertices(m_primaryVertexKey);
140 
141  ATH_MSG_DEBUG("======================================="
142  << "\n\t\t\t Size of vertex container: " << vertices ->size()
143  << "\n\t\t\t Size of track jet container: " << trackJets->size()
144  << "\n-----------------------------------------------------------------");
145 
146  //
147  // Find default Primary Vertex
148  //
149  const xAOD::Vertex *primaryVertex = nullptr;
150 
151  for(const xAOD::Vertex *vertex: *vertices) {
152  if(vertex->vertexType() == xAOD::VxType::PriVtx) {
153  primaryVertex = vertex;
154  break;
155  }
156  }
157 
158  if(m_leptonsName == "Electrons") {
159  //
160  // Process electrons
161  //
162  ATH_MSG_DEBUG("Reading " << m_electronsKey);
164 
165  for(const xAOD::Electron *elec: *electrons) {
166  decorateElec(*elec, *trackJets, *clusters, primaryVertex);
167  }
168  } else if(m_leptonsName == "Muons") {
169  //
170  // Process muons
171  //
172  ATH_MSG_DEBUG("Reading " << m_muonsKey);
173  SG::ReadHandle<xAOD::MuonContainer> muons(m_muonsKey);
174 
175  for(const xAOD::Muon *muon: *muons) {
176  decorateMuon(*muon, *trackJets, primaryVertex);
177  ATH_MSG_DEBUG("Muon decorated");
178  }
179  } else {
180  ATH_MSG_ERROR("Must specify Electrons or Muons");
181  return StatusCode::FAILURE;
182  }
183 
184  return StatusCode::SUCCESS;
185 }
186 
187 
188 //=============================================================================
190 {
191  //
192  // Make new instance of TMVA Reader and add variables
193  //
194  m_TMVAReader = std::make_unique<TMVA::Reader>();
195 
196  for(unsigned i = 0; i < m_allVars.size(); ++i) {
197  m_TMVAReader->AddVariable(m_vars->asStr(m_allVars.at(i)), &m_varTMVA[i]);
198  }
199 
200  //
201  // Get path to xml training file
202  //
203  std::string fullPathToFile = PathResolverFindCalibFile("JetTagNonPromptLepton/"
204  + m_configFileVersion
205  + "/TMVAClassification_" + m_methodTitleMVA + ".weights.xml");
206 
207  if(!m_configPathOverride.empty()) {
208  ATH_MSG_INFO("Override path resolver result");
209  fullPathToFile = m_configPathOverride;
210  }
211 
212  ATH_MSG_INFO("TMVA configuration file: " + fullPathToFile);
213  ATH_MSG_INFO("TMVA method name: " + m_methodTitleMVA);
214 
215  //
216  // Book an instance of BDT reader if path is available
217  //
218  if(fullPathToFile == "") {
219  ATH_MSG_ERROR("Could not find path to xml training file");
220  return false;
221  }
222  else {
223  m_TMVAReader->BookMVA(m_methodTitleMVA.toString(), fullPathToFile);
224  }
225 
226  return true;
227 }
228 
229 
230 //=============================================================================
232 {
233  //
234  // Fill short variable map
235  //
236  for(Prompt::Def::Var &var: m_intVars) {
237  SG::AuxElement::Decorator<short> shortDecorator(m_inputVarDecoratePrefix + m_vars->asStr(var));
238 
239  if(!m_shortMap.insert(shortDecoratorMap::value_type(var, shortDecorator)).second) {
240  ATH_MSG_ERROR("Instantiation of Decorator class failed for short decorator map for var: " << m_vars->asStr(var));
241  return StatusCode::FAILURE;
242  }
243  }
244 
245  //
246  // Fill float variable map
247  //
248  for(Prompt::Def::Var &var: m_floatVars) {
249  SG::AuxElement::Decorator<float> floatDecorator(m_inputVarDecoratePrefix + m_vars->asStr(var));
250 
251  if(!m_floatMap.insert(floatDecoratorMap::value_type(var, floatDecorator)).second) {
252  ATH_MSG_ERROR("Instantiation of Decorator class failed for float decorator map for var: " << m_vars->asStr(var));
253  return StatusCode::FAILURE;
254  }
255  }
256 
257  //
258  // Fill additional variables
259  //
260  if(!m_floatMap.insert(floatDecoratorMap::value_type(m_BDTVarKey, SG::AuxElement::Decorator<float>(m_BDTName))).second) {
261  ATH_MSG_ERROR("Failed to add variable: " << m_vars->asStr(m_BDTVarKey));
262  return StatusCode::FAILURE;
263  }
264 
265  for(const std::string &evar: m_extraDecoratorFloatVars) {
266  const Def::Var ekey = m_vars->registerDynamicVar(evar);
267 
268  if(ekey == Def::NONE) {
269  ATH_MSG_ERROR("Failed to create key for variable name=" << evar);
270  return StatusCode::FAILURE;
271  }
272 
273  if(m_floatMap.find(ekey) != m_floatMap.end()) {
274  ATH_MSG_DEBUG("Ignore duplicate variable name=" << evar);
275  continue;
276  }
277 
278  if(!m_floatMap.insert(floatDecoratorMap::value_type(ekey, SG::AuxElement::Decorator<float>(m_inputVarDecoratePrefix + evar))).second) {
279  ATH_MSG_ERROR("Failed to add variable: \"" << evar << "\"");
280  return StatusCode::FAILURE;
281  }
282  }
283 
284  for(const std::string &evar: m_extraDecoratorShortVars) {
285  const Def::Var ekey = m_vars->registerDynamicVar(evar);
286 
287  if(ekey == Def::NONE) {
288  ATH_MSG_ERROR("Failed to create key for variable name=" << evar);
289  return StatusCode::FAILURE;
290  }
291 
292  if(m_shortMap.find(ekey) != m_shortMap.end()) {
293  ATH_MSG_DEBUG("Ignore duplicate variable name=" << evar);
294  continue;
295  }
296 
297  if(!m_shortMap.insert(shortDecoratorMap::value_type(ekey, SG::AuxElement::Decorator<short>(m_inputVarDecoratePrefix + evar))).second) {
298  ATH_MSG_ERROR("Failed to add variable: \"" << evar << "\"");
299  return StatusCode::FAILURE;
300  }
301  }
302 
303  //
304  // Veto the decorators of the variables in the veto-list if exist
305  //
306  for(const std::string &vvar: m_vetoDecoratorFloatVars) {
307  const Def::Var vkey = m_vars->registerDynamicVar(vvar);
308 
309  if(vkey == Def::NONE) {
310  ATH_MSG_ERROR("Failed to create key for variable name=" << vvar);
311  return StatusCode::FAILURE;
312  }
313 
314  floatDecoratorMap::iterator iter = m_floatMap.find(vkey);
315 
316  if(iter != m_floatMap.end()) {
317  ATH_MSG_DEBUG("Remove the variable from the veto-list, name=" << vvar);
318  m_floatMap.erase(iter);
319  }
320  }
321 
322  for(const std::string &vvar: m_vetoDecoratorShortVars) {
323  const Def::Var vkey = m_vars->registerDynamicVar(vvar);
324 
325  if(vkey == Def::NONE) {
326  ATH_MSG_ERROR("Failed to create key for variable name=" << vvar);
327  return StatusCode::FAILURE;
328  }
329 
330  shortDecoratorMap::iterator iter = m_shortMap.find(vkey);
331 
332  if(iter != m_shortMap.end()) {
333  ATH_MSG_DEBUG("Remove the variable from the veto-list, name=" << vvar);
334  m_shortMap.erase(iter);
335  }
336  }
337 
338  // Print decorator counts
339  ATH_MSG_DEBUG("Added " << m_shortMap.size() << " short decorators");
340  ATH_MSG_DEBUG("Added " << m_floatMap.size() << " float decorators");
341 
342  // Instantiate MVA X bin
343  if(m_leptonPtBinsVector.size() < 2) {
344  ATH_MSG_ERROR("Invalid PtBins size=" << m_leptonPtBinsVector.size());
345  return StatusCode::FAILURE;
346  }
347 
348  std::unique_ptr<double []> PtBins = std::make_unique<double []>(m_leptonPtBinsVector.size());
349 
350  for(unsigned i = 0; i < m_leptonPtBinsVector.size(); i++) {
351  PtBins[i] = m_leptonPtBinsVector[i];
352  }
353 
354  m_leptonPtBinHist = std::make_unique<TH1D>("PtBin", "PtBin", m_leptonPtBinsVector.size() - 1, PtBins.get());
355 
356  return StatusCode::SUCCESS;
357 }
358 
359 //=============================================================================
361 {
362  //
363  // Instantiate isolation accessors
364  //
365  m_accessCalIsolation30 = std::make_unique<AccessFloat> ("topoetcone30");
366  m_accessTrackIsolation30 = std::make_unique<AccessFloat> ("ptvarcone30");
367  m_accessTrackIsolation30TTVA = std::make_unique<AccessFloat> ("ptvarcone30_Nonprompt_All_MaxWeightTTVA_pt500");
368 
369  m_accessDeepSecondaryVertex = std::make_unique<AccessVertex>(m_vertexLinkName);
370 
371  //
372  // Instantiate accessors for the muon specific variables
373  //
374  m_accessMuonCalE = std::make_unique<AccessFloat> ("calE");
375  m_accessMuonParamEnergyLoss = std::make_unique<AccessFloat> ("ParamEnergyLoss");
376 
377  //
378  // Instantiate accessors for RNN variables
379  //
380  for(const std::string &name: m_accessorRNNVars) {
381  const Def::Var akey = m_vars->registerDynamicVar(name);
382 
383  if(m_accessRNNMap.insert(floatAccessorMap::value_type(akey, AccessFloat(name))).second) {
384  ATH_MSG_DEBUG("Add float RNN accessor: " << name);
385  }
386  else {
387  ATH_MSG_WARNING("Skip duplicate float accessor: " << name);
388  }
389  }
390 }
391 
392 //=============================================================================
394  const xAOD::Electron &electron,
395  const xAOD::JetContainer &trackJets,
397  const xAOD::Vertex *primaryVertex
398 )
399 {
400  //
401  // Find nearest track jet to electron
402  //
403  TimerScopeHelper timer(m_timerElec);
404 
405  Prompt::VarHolder vars;
406 
407  std::pair<double, const xAOD::Jet*> match = findTrackJet(electron, trackJets);
408 
409  if(match.second) {
410  //
411  // Get muon calorimeter energy variable, RNN and secondary vertex variables
412  //
413  getElectronAnpVariables(electron, clusters, vars, primaryVertex);
414 
415  //
416  // Get mutual variables, passing track as argument
417  //
418  getMutualVariables(electron, *match.second, electron.trackParticle(), vars);
419 
420  //
421  // Pass variables to TMVA
422  //
423  addVarsToTMVA(vars);
424  }
425  else {
426  //
427  // Decorate electron with default values
428  //
429  fillVarDefault(vars);
430 
431  ATH_MSG_DEBUG("No track jet found near to electron");
432  }
433 
434  //
435  // Decorate electron with input vars and BDT weight
436  //
437  decorateAuxLepton(electron, vars);
438 }
439 
440 
441 //=============================================================================
443  const xAOD::Muon &muon,
444  const xAOD::JetContainer &trackJets,
445  const xAOD::Vertex *primaryVertex
446 )
447 {
448  //
449  // Find nearest track jet to muon
450  //
451  TimerScopeHelper timer(m_timerMuon);
452 
453  Prompt::VarHolder vars;
454 
455  std::pair<double, const xAOD::Jet*> match = findTrackJet(muon, trackJets);
456 
457  if(match.second) {
458  //
459  // Get muon calorimeter energy variable, RNN and secondary vertex variables
460  //
461  getMuonAnpVariables(muon, vars, primaryVertex);
462 
463  //
464  // Get mutual variables, passing track as argument
465  //
466  getMutualVariables(muon, *match.second, muon.primaryTrackParticle(), vars);
467 
468  //
469  // Add variables to TMVA Reader
470  //
471  addVarsToTMVA(vars);
472  }
473  else {
474  //
475  // Decorate muon with default values
476  //
477  fillVarDefault(vars);
478 
479  ATH_MSG_DEBUG("No track jet found near to muon");
480  }
481 
482  //
483  // Decorate muon with input vars and BDT weight
484  //
485  decorateAuxLepton(muon, vars);
486 }
487 
488 //=============================================================================
490  const xAOD::Electron &elec,
492  Prompt::VarHolder &vars,
493  const xAOD::Vertex *primaryVertex
494 )
495 {
496  //
497  // Get Muon variables - calorimeter
498  //
499  double sumCoreEt_large = 0.0, CaloClusterSumEtRel = 0.0;
500 
501  if(elec.caloCluster()) {
502  const double elec_calEta = elec.caloCluster()->eta();
503  const double elec_calPhi = elec.caloCluster()->phi();
504 
505  for(const xAOD::CaloCluster *cluster: clusters) {
506  const double deta = elec_calEta - cluster->eta();
507  const double dphi = TVector2::Phi_mpi_pi(elec_calPhi - cluster->phi());
508  const double dr = std::sqrt(deta*deta + dphi*dphi);
509 
510  if(dr < m_elecMinCalErelConeSize) {
511  sumCoreEt_large += cluster->pt();
512  }
513  }
514  }
515 
516  if(elec.pt() > 0.0) CaloClusterSumEtRel = sumCoreEt_large/elec.pt();
517 
519 
520  //
521  // Get lepton isolation variables
522  //
523  const double Topoetcone30rel = accessIsolation(*m_accessCalIsolation30, elec);
524  const double Ptvarcone30rel = accessIsolation(*m_accessTrackIsolation30, elec);
525 
528 
529  //
530  // Get secondary vertex variable
531  //
532  std::vector<double> goodVertexNdistLong;
533 
534  if(m_accessDeepSecondaryVertex->isAvailable(elec)) {
535  std::vector<ElementLink<xAOD::VertexContainer> > vtxLinks = (*m_accessDeepSecondaryVertex)(elec);
536 
537  for(ElementLink<xAOD::VertexContainer> &vtxLink: vtxLinks) {
538  if(!vtxLink.isValid()) {
539  ATH_MSG_WARNING("VertexContainer : invalid link");
540  continue;
541  }
542 
543  const xAOD::Vertex *vtx = *vtxLink;
544 
545  const double fitProb = Prompt::getVertexFitProb(vtx);
546 
547  if(fitProb < m_vertexMinChiSquaredProb) {
548  continue;
549  }
550 
551  const double theta = std::acos(getVertexCosThetaWithLepDir(elec, vtx, primaryVertex));
552 
553  if (theta < m_vertexMinThetaBarrElec && std::fabs(elec.eta()) <= m_vertexBarrEcapAbsEtaAt) continue;
554  else if (theta < m_vertexMinThetaEcapElec && std::fabs(elec.eta()) > m_vertexBarrEcapAbsEtaAt) continue;
555 
556  const double vertex_ndist_long = getVertexLongitudinalNormDist(elec, vtx, primaryVertex);
557 
558  goodVertexNdistLong.push_back(vertex_ndist_long);
559  }
560  }
561  else {
562  ATH_MSG_WARNING("VertexContainer : " << m_vertexLinkName << " not found for the electron");
563  }
564 
565  double best_vertex_ndist_long = 0.0;
566 
567  if(goodVertexNdistLong.size() > 0) {
568  std::sort(goodVertexNdistLong.begin(), goodVertexNdistLong.end());
569  best_vertex_ndist_long = goodVertexNdistLong.back();
570  }
571 
573  vars.addVar(Prompt::Def::CandVertex_NPassVtx, goodVertexNdistLong.size());
574 }
575 
576 //=============================================================================
578  const xAOD::Muon &muon,
579  Prompt::VarHolder &vars,
580  const xAOD::Vertex *primaryVertex
581 )
582 {
583  //
584  // Get Muon variables - calorimeter
585  //
586  double calE = -99.0, peloss = -99.0, caloClusterERel = -99.0;
587 
588  if(muon.clusterLink().isValid()) {
589  const xAOD::CaloCluster* cluster = *(muon.clusterLink());
590 
591  if(m_accessMuonCalE->isAvailable(*cluster) && m_accessMuonParamEnergyLoss->isAvailable(muon)) {
592  calE = (*m_accessMuonCalE)(*cluster);
593  peloss = (*m_accessMuonParamEnergyLoss)(muon);
594 
595  caloClusterERel = calE/peloss;
596  }
597  else {
598  ATH_MSG_WARNING("Muon calE or ParamEnergyLoss not found in auxiliary store");
599  }
600  }
601 
602  vars.addVar(Prompt::Def::CaloClusterERel, caloClusterERel);
603 
604  //
605  // Get lepton isolation variables
606  //
607  const double Topoetcone30rel = accessIsolation(*m_accessCalIsolation30, muon);
608  const double ptvarcone30TightTTVAPt500rel = accessIsolation(*m_accessTrackIsolation30TTVA, muon);
609 
611  vars.addVar(Prompt::Def::Ptvarcone30_TightTTVA_pt500rel, ptvarcone30TightTTVAPt500rel);
612 
613  //
614  // Get Muon Secondary Vertex variable
615  //
616  std::vector<double> goodVertexNdistLong;
617 
618  if(m_accessDeepSecondaryVertex->isAvailable(muon)) {
619  std::vector<ElementLink<xAOD::VertexContainer> > vtxLinks = (*m_accessDeepSecondaryVertex)(muon);
620  goodVertexNdistLong.reserve(vtxLinks.size());
621 
622  for(ElementLink<xAOD::VertexContainer> &vtxLink: vtxLinks) {
623  if(!vtxLink.isValid()) {
624  ATH_MSG_WARNING("VertexContainer : invalid link");
625  continue;
626  }
627 
628  const xAOD::Vertex *vtx = *vtxLink;
629 
630  const double fitProb = Prompt::getVertexFitProb(vtx);
631 
632  if(fitProb > m_vertexMinChiSquaredProb) {
633  const double vertex_ndist_long = getVertexLongitudinalNormDist(muon, vtx, primaryVertex);
634 
635  goodVertexNdistLong.push_back(vertex_ndist_long);
636  }
637  }
638  }
639  else {
640  ATH_MSG_WARNING("VertexContainer : " << m_vertexLinkName << " not found for the muon");
641  }
642 
643  double best_vertex_ndist_long = 0.0;
644 
645  if(goodVertexNdistLong.size() > 0) {
646  std::sort(goodVertexNdistLong.begin(), goodVertexNdistLong.end());
647  best_vertex_ndist_long = goodVertexNdistLong.back();
648  }
649 
651  vars.addVar(Prompt::Def::CandVertex_NPassVtx, goodVertexNdistLong.size());
652 }
653 
654 //=============================================================================
656  const xAOD::IParticle &particle,
657  const xAOD::Jet &track_jet,
658  const xAOD::TrackParticle *track,
659  Prompt::VarHolder &vars
660 )
661 {
662  //
663  // Add lepton - jet variables to VarHolder
664  //
665  double PtFrac = -99.;
666  double PtRel = -99.;
667 
668  if(particle.pt() > 0.0 && track_jet.pt() > 0.0) {
669 
670  if(track) {
671  PtFrac = track->pt()/track_jet.pt();
672  }
673 
674  const double angle = particle.p4().Vect().Angle(track_jet.p4().Vect());
675 
676  PtRel = particle.pt() * std::sin(angle);
677  }
678 
679  //
680  // Add vars to VarHolder
681  //
684  vars.addVar(Prompt::Def::DRlj, track_jet.p4().DeltaR(particle.p4()));
686 
687  //
688  // Get RNN variables
689  //
690  for(floatAccessorMap::value_type &acc: m_accessRNNMap) {
691  if(acc.second.isAvailable(particle)) {
692  vars.addVar(acc.first, acc.second(particle));
693  }
694  else {
695  ATH_MSG_WARNING("LeptonTagger RNN not found in auxiliary store for variable=" << vars.asStr(acc.first));
696  }
697  }
698 
699  //
700  // Get lepton variables - pT X bin
701  //
702  const double lepPt = particle.pt();
703 
704  const double xmax = m_leptonPtBinHist->GetXaxis()->GetXmax();
705  const double xmin = m_leptonPtBinHist->GetXaxis()->GetXmin();
706 
707  int curr_bin = 0;
708 
709  if(xmin < lepPt && lepPt < xmax) {
710  curr_bin = m_leptonPtBinHist->FindBin(lepPt);
711  }
712  else if (!(lepPt < xmax)) {
713  curr_bin = m_leptonPtBinHist->GetNbinsX();
714  }
715  else if (!(lepPt > xmin)) {
716  curr_bin = 1;
717  }
718 
719  vars.addVar(Prompt::Def::MVAXBin, curr_bin);
720  vars.addVar(Prompt::Def::RawPt, lepPt);
721 
722  ATH_MSG_DEBUG("getMutualVariables - lepPt = " << lepPt << ", MVAXBin = " << curr_bin);
723 }
724 
725 //=============================================================================
727  AccessFloat &isoAccessor,
729 ) {
730  double isolation = -99., isolationrel = -99.;
731 
732  if(isoAccessor.isAvailable(particle)) {
733  isolation = isoAccessor(particle);
734  }
735  else {
736  ATH_MSG_WARNING("Lepton isolation not found in auxiliary store");
737  }
738 
739  if(particle.pt() > 0.0) {
740  isolationrel = isolation / particle.pt();
741  }
742 
743  return isolationrel;
744 }
745 
746 //=============================================================================
748 {
749  //
750  // Add variables to TMVA reader
751  //
752  for(unsigned i = 0; i < m_allVars.size(); ++i) {
753  m_varTMVA[i] = 0.0;
754 
755  if(!vars.getVar(m_allVars.at(i), m_varTMVA[i])) {
756  ATH_MSG_WARNING("Missing input variable: " << m_vars->asStr(m_allVars.at(i)));
757  }
758  }
759 
760  //
761  // Decorate lepton with classifier response, if goodJet
762  //
763  float bdt_weight = m_TMVAReader->EvaluateMVA(m_methodTitleMVA.toString());
764 
765  if(m_BDTVarKey != Def::NONE) {
766  vars.addVar(m_BDTVarKey, bdt_weight);
767  }
768  else {
769  ATH_MSG_WARNING("addVarsToTMVA - invalid Def::Var key for " << m_BDTName);
770  }
771 }
772 
773 //=============================================================================
775 {
776  //
777  // Add default values to VarHolder
778  //
779  for(const floatDecoratorMap::value_type &dec: m_floatMap) {
780  vars.addVar(dec.first, -99.0);
781  }
782 
783  for(const shortDecoratorMap::value_type &dec: m_shortMap) {
784  vars.addVar(dec.first, -99.0);
785  }
786 }
787 
788 //=============================================================================
790  const xAOD::IParticle &particle,
791  Prompt::VarHolder &vars
792 )
793 {
794  //
795  // Decorate lepton with input short variables
796  //
797  for(shortDecoratorMap::value_type &dec: m_shortMap) {
798  double val = 0.0;
799 
800  if(vars.getVar(dec.first, val)) {
801  dec.second(particle) = static_cast<short>(val);
802 
803  ATH_MSG_DEBUG("Short variable: " << vars.asStr(dec.first) << " = " << val);
804  }
805  else {
806  ATH_MSG_WARNING("Short variable " << vars.asStr(dec.first) << " not decorated to lepton");
807  }
808  }
809 
810  //
811  // Decorate lepton with input float variables
812  //
813  for(floatDecoratorMap::value_type &dec: m_floatMap) {
814  double val = 0.0;
815 
816  if(vars.getVar(dec.first, val)) {
817  dec.second(particle) = val;
818 
819  ATH_MSG_DEBUG("Float variable: " << vars.asStr(dec.first) << " = " << val);
820  }
821  else {
822  ATH_MSG_WARNING("Float variable " << vars.asStr(dec.first) << " not decorated to lepton");
823  }
824  }
825 }
826 
827 //=============================================================================
828 template<class T> std::pair<double, const xAOD::Jet*> Prompt::DecoratePromptLeptonImproved::findTrackJet(const T &part,
829  const xAOD::JetContainer &jets)
830 {
831  //
832  // Find nearest track jet and a return a pair of dR and xAOD::Jet*
833  //
834  const xAOD::Jet *minjet = 0;
835  double mindr = 10.0;
836  std::pair<double, const xAOD::Jet*> match(mindr, minjet);
837 
838  for(const xAOD::Jet* jet: jets) {
839  const double dr = part.p4().DeltaR(jet->p4());
840 
841  if(!minjet || dr < mindr) {
842  mindr = dr;
843  minjet = jet;
844  match = std::make_pair(mindr, minjet);
845  }
846  }
847 
848 
849  if(minjet && mindr < m_maxLepTrackJetDR) {
850  return match;
851  }
852 
853  minjet = 0;
854  return std::make_pair(10., minjet);
855 }
856 
857 //=============================================================================
859  const xAOD::IParticle &lepton,
860  const xAOD::Vertex *secondaryVertex,
861  const xAOD::Vertex *primaryVertex
862 )
863 {
864  //
865  // get the Longitudinal nomalized distance between the secondary vertex and primary vertex
866  //
867  if(!secondaryVertex || !primaryVertex) {
868  ATH_MSG_WARNING("getVertexLongitudinalNormDist - invalid pointer of lepton/secondaryVertex/primaryVertex");
869  return 0.0;
870  }
871 
872 
873  float normDist_SVPV = 0.0;
874 
875  if(!Prompt::GetAuxVar(*secondaryVertex, normDist_SVPV, "normDistToPriVtx")) {
876  ATH_MSG_WARNING("getVertexLongitudinalNormDist - missing \"normDistToPriVtx\"");
877  }
878 
879  double cos_theta = getVertexCosThetaWithLepDir(lepton, secondaryVertex, primaryVertex);
880 
881  return normDist_SVPV*cos_theta;
882 }
883 
884 //=============================================================================
886  const xAOD::Vertex *secondaryVertex,
887  const xAOD::Vertex *primaryVertex)
888 {
889  //
890  // get the Longitudinal nomalized distance between the secondary vertex and primary vertex
891  //
892  if(!secondaryVertex || !primaryVertex) {
893  ATH_MSG_WARNING("GetVertexThetaWithLepDir - invalid pointer of lepton/secondaryVertex/primaryVertex");
894  return 0.0;
895  }
896 
897  const Amg::Vector3D sv_to_pv_v3 = secondaryVertex->position() - primaryVertex->position();
898 
899  const TVector3 sv_to_pv_t3 = TVector3(sv_to_pv_v3.x(), sv_to_pv_v3.y(), sv_to_pv_v3.z());
900  const TVector3 lepton_dirt = lepton.p4().Vect();
901 
902  const double cos_theta = sv_to_pv_t3.Unit()*lepton_dirt.Unit();
903 
904  return cos_theta;
905 }
906 
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
NONE
@ NONE
Definition: sTGCenumeration.h:13
xAOD::CaloCluster_v1::phi
virtual double phi() const
The azimuthal angle ( ) of the particle.
Definition: CaloCluster_v1.cxx:256
Prompt::Def::RawPt
@ RawPt
Definition: VarHolder.h:94
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
PromptUtils.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Prompt::GetAuxVar
bool GetAuxVar(const T1 &obj, T2 &value, const std::string &var_name)
Definition: PromptUtils.h:93
Prompt::getVertexFitProb
double getVertexFitProb(const xAOD::Vertex *vtx)
Definition: PromptUtils.cxx:21
Prompt::Def::TrackJetNTrack
@ TrackJetNTrack
Definition: VarHolder.h:69
Prompt::DecoratePromptLeptonImproved::initializeTMVAReader
bool initializeTMVAReader()
Definition: DecoratePromptLeptonImproved.cxx:189
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
Prompt::VarHolder::addVar
bool addVar(unsigned key, double value)
Adds a variable to the VarHolder.
Definition: VarHolder.h:210
Prompt::VarHolder
Definition: VarHolder.h:112
Prompt::DecoratePromptLeptonImproved::initializeConstAccessors
void initializeConstAccessors()
Definition: DecoratePromptLeptonImproved.cxx:360
xAOD::JetConstituentVector::size
size_t size() const
number of constituents
Definition: JetConstituentVector.cxx:102
Prompt::DecoratePromptLeptonImproved::decorateMuon
void decorateMuon(const xAOD::Muon &muon, const xAOD::JetContainer &trackJets, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:442
Prompt::DecoratePromptLeptonImproved::findTrackJet
std::pair< double, const xAOD::Jet * > findTrackJet(const T &part, const xAOD::JetContainer &jets)
Definition: DecoratePromptLeptonImproved.cxx:828
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
python.TurnDataReader.dr
dr
Definition: TurnDataReader.py:112
xAOD::Jet_v1::getConstituents
JetConstituentVector getConstituents() const
Return a vector of consituents. The object behaves like vector<const IParticle*>. See JetConstituentV...
Definition: Jet_v1.cxx:147
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
Phi_mpi_pi
__HOSTDEV__ double Phi_mpi_pi(double)
Definition: GeoRegion.cxx:7
Prompt::PrintResetStopWatch
std::string PrintResetStopWatch(TStopwatch &watch)
Definition: PromptUtils.cxx:244
Prompt::Def::DRlj
@ DRlj
Definition: VarHolder.h:70
Prompt::DecoratePromptLeptonImproved::getElectronAnpVariables
void getElectronAnpVariables(const xAOD::Electron &elec, const xAOD::CaloClusterContainer &clusters, Prompt::VarHolder &vars, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:489
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
Prompt::DecoratePromptLeptonImproved::finalize
virtual StatusCode finalize() override
Definition: DecoratePromptLeptonImproved.cxx:106
Prompt::Def::PtFrac
@ PtFrac
Definition: VarHolder.h:71
Prompt::Def::NONE
@ NONE
Definition: VarHolder.h:68
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Prompt::VarHolder::getVar
double getVar(const unsigned key) const
Definition: VarHolder.h:275
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
Prompt::Def::Ptvarcone30rel
@ Ptvarcone30rel
Definition: VarHolder.h:91
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Prompt::DecoratePromptLeptonImproved::execute
virtual StatusCode execute() override
Definition: DecoratePromptLeptonImproved.cxx:126
Prompt::Def::MVAXBin
@ MVAXBin
Definition: VarHolder.h:93
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
xAOD::CaloCluster_v1::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
Definition: CaloCluster_v1.cxx:251
lumiFormat.i
int i
Definition: lumiFormat.py:85
Prompt::DecoratePromptLeptonImproved::decorateAuxLepton
void decorateAuxLepton(const xAOD::IParticle &particle, Prompt::VarHolder &vars)
Definition: DecoratePromptLeptonImproved.cxx:789
xmin
double xmin
Definition: listroot.cxx:60
DecoratePromptLeptonImproved.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
angle
double angle(const GeoTrf::Vector2D &a, const GeoTrf::Vector2D &b)
Definition: TRTDetectorFactory_Full.cxx:73
Prompt::DecoratePromptLeptonImproved::getMuonAnpVariables
void getMuonAnpVariables(const xAOD::Muon &muon, Prompt::VarHolder &vars, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:577
xAOD::Egamma_v1::caloCluster
const xAOD::CaloCluster * caloCluster(size_t index=0) const
Pointer to the xAOD::CaloCluster/s that define the electron candidate.
Definition: Egamma_v1.cxx:388
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
Prompt::DecoratePromptLeptonImproved::getMutualVariables
void getMutualVariables(const xAOD::IParticle &particle, const xAOD::Jet &track_jet, const xAOD::TrackParticle *track, Prompt::VarHolder &vars)
Definition: DecoratePromptLeptonImproved.cxx:655
AthAlgorithm
Definition: AthAlgorithm.h:47
Prompt::Def::CandVertex_NPassVtx
@ CandVertex_NPassVtx
Definition: VarHolder.h:100
Prompt::Def::CaloClusterSumEtRel
@ CaloClusterSumEtRel
Definition: VarHolder.h:96
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
Prompt::DecoratePromptLeptonImproved::addVarsToTMVA
void addVarsToTMVA(Prompt::VarHolder &vars)
Definition: DecoratePromptLeptonImproved.cxx:747
xAOD::IParticle::p4
virtual FourMom_t p4() const =0
The full 4-momentum of the particle.
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
xAOD::Electron_v1
Definition: Electron_v1.h:34
Prompt::DecoratePromptLeptonImproved::fillVarDefault
void fillVarDefault(Prompt::VarHolder &vars) const
Definition: DecoratePromptLeptonImproved.cxx:774
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::Jet_v1::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: Jet_v1.cxx:71
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
RunTileMonitoring.clusters
clusters
Definition: RunTileMonitoring.py:133
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
Prompt::Def::Ptvarcone30_TightTTVA_pt500rel
@ Ptvarcone30_TightTTVA_pt500rel
Definition: VarHolder.h:92
xmax
double xmax
Definition: listroot.cxx:61
xAOD::Egamma_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: Egamma_v1.cxx:65
Prompt::DecoratePromptLeptonImproved::accessIsolation
float accessIsolation(SG::AuxElement::ConstAccessor< float > &isoAccessor, const xAOD::IParticle &particle)
Definition: DecoratePromptLeptonImproved.cxx:726
Prompt::Def::CandVertex_normDistToPriVtxLongitudinalBest
@ CandVertex_normDistToPriVtxLongitudinalBest
Definition: VarHolder.h:98
Prompt::Def::PtRel
@ PtRel
Definition: VarHolder.h:72
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAOD::Egamma_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition: Egamma_v1.cxx:70
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
Prompt::DecoratePromptLeptonImproved::initialize
virtual StatusCode initialize() override
Definition: DecoratePromptLeptonImproved.cxx:26
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
Prompt::DecoratePromptLeptonImproved::getVertexCosThetaWithLepDir
double getVertexCosThetaWithLepDir(const xAOD::IParticle &lepton, const xAOD::Vertex *secondaryVertex, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:885
Prompt::Def::Var
Var
Definition: VarHolder.h:57
Prompt::TimerScopeHelper
Definition: PromptUtils.h:112
Prompt::DecoratePromptLeptonImproved::DecoratePromptLeptonImproved
DecoratePromptLeptonImproved(const std::string &name, ISvcLocator *pSvcLocator)
Definition: DecoratePromptLeptonImproved.cxx:20
xAOD::Jet_v1::pt
virtual double pt() const
The transverse momentum ( ) of the particle.
Definition: Jet_v1.cxx:44
Prompt::DecoratePromptLeptonImproved::getVertexLongitudinalNormDist
double getVertexLongitudinalNormDist(const xAOD::IParticle &lepton, const xAOD::Vertex *secondaryVertex, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:858
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Prompt::DecoratePromptLeptonImproved::initializeDecorators
StatusCode initializeDecorators()
Definition: DecoratePromptLeptonImproved.cxx:231
Prompt::Def::CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx
@ CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx
Definition: VarHolder.h:99
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
Prompt::Def::Topoetcone30rel
@ Topoetcone30rel
Definition: VarHolder.h:90
Prompt::Def::CaloClusterERel
@ CaloClusterERel
Definition: VarHolder.h:95
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356
Prompt::VarHolder::asStr
std::string asStr(const uint32_t key, const double val)
Definition: VarHolder.cxx:178
Prompt::DecoratePromptLeptonImproved::decorateElec
void decorateElec(const xAOD::Electron &electron, const xAOD::JetContainer &trackJets, const xAOD::CaloClusterContainer &clusters, const xAOD::Vertex *primaryVertex)
Definition: DecoratePromptLeptonImproved.cxx:393