ATLAS Offline Software
IsolationLowPtPLVTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3  */
4 
7 #include <xAODBase/ObjectType.h>
8 
9 // Tools includes:
10 #include <cmath>
11 
13 #include "TMVA/MethodBDT.h"
14 #include "TMVA/Reader.h"
15 #include "TMVA/Tools.h"
16 
17 namespace CP {
18  // Accessors for input variables
19  static const ShortAccessor s_acc_TrackJetNTrack("PromptLeptonInput_TrackJetNTrack");
20  static const FloatAccessor s_acc_DRlj("PromptLeptonInput_DRlj");
21  static const FloatAccessor s_acc_PtRel("PromptLeptonInput_PtRel");
22  static const FloatAccessor s_acc_PtFrac("PromptLeptonInput_PtFrac");
23  static const FloatAccessor s_acc_topoetcone20("topoetcone20");
24  static const FloatAccessor s_acc_ptvarcone20("ptvarcone20");
25  static const FloatAccessor s_acc_ptvarcone30("ptvarcone30");
26  static const FloatDecorator s_dec_iso_PLT("LowPtPLV");
27 
28  constexpr int N_VARIABLES = 6;
29 
30  IsolationLowPtPLVTool::IsolationLowPtPLVTool(const std::string& toolName) : asg::AsgTool(toolName) {}
31 
33  TMVA::Tools::Instance();
34 
35  std::string fullPathToFile_Muon = PathResolverFindCalibFile(m_muonCalibFile);
36  std::string fullPathToFile_Elec = PathResolverFindCalibFile(m_elecCalibFile);
37 
38  std::vector<std::string> BDT_vars_Muon{"TrackJetNTrack", "PtRel", "PtFrac", "DRlj", "TopoEtCone20Rel", "PtVarCone30Rel"};
39 
40  std::vector<std::string> BDT_vars_Elec{"TrackJetNTrack", "PtRel", "PtFrac", "DRlj", "TopoEtCone20Rel", "PtVarCone20Rel"};
41  m_TMVAReader_Muon = std::make_unique<TMVA::Reader>(BDT_vars_Muon, "!Silent:Color");
42  m_TMVAReader_Elec = std::make_unique<TMVA::Reader>(BDT_vars_Elec, "!Silent:Color");
43 
44  if (fullPathToFile_Muon.empty()) {
45  ATH_MSG_ERROR("Error! No xml file found for Muon LowPtPLV");
46  return StatusCode::FAILURE;
47  }
48  m_TMVAReader_Muon->BookMVA(m_muonMethodName.value(), fullPathToFile_Muon);
49  TMVA::MethodBDT* method_Muon_bdt = dynamic_cast<TMVA::MethodBDT*>(m_TMVAReader_Muon->FindMVA(m_muonMethodName.value()));
50  if (!method_Muon_bdt) {
51  ATH_MSG_ERROR("Error! No method found for Muon LowPtPLV");
52  return StatusCode::FAILURE;
53  }
54 
55  if (fullPathToFile_Elec.empty()) {
56  ATH_MSG_ERROR("Error! No xml file found for Electron LowPtPLV");
57  return StatusCode::FAILURE;
58  }
59  m_TMVAReader_Elec->BookMVA(m_elecMethodName.value(), fullPathToFile_Elec);
60  TMVA::MethodBDT* method_Elec_bdt = dynamic_cast<TMVA::MethodBDT*>(m_TMVAReader_Elec->FindMVA(m_elecMethodName.value()));
61  if (!method_Elec_bdt) {
62  ATH_MSG_ERROR("Error! No method found for Electron LowPtPLV");
63  return StatusCode::FAILURE;
64  }
65 
66  ATH_MSG_INFO("Initialized IsolationLowPtPLVTool");
67  return StatusCode::SUCCESS;
68  }
69 
71  // Check if input variables exist
72  bool inputvar_missing = false;
73  if (!s_acc_TrackJetNTrack.isAvailable(Particle)) {
74  ATH_MSG_ERROR("TrackJetNTrack not available");
75  inputvar_missing = true;
76  }
77 
78  if (!s_acc_DRlj.isAvailable(Particle)) {
79  ATH_MSG_ERROR("DRlj not available");
80  inputvar_missing = true;
81  }
82 
83  if (!s_acc_PtRel.isAvailable(Particle)) {
84  ATH_MSG_ERROR("PtRel not available");
85  inputvar_missing = true;
86  }
87 
88  if (!s_acc_PtFrac.isAvailable(Particle)) {
89  ATH_MSG_ERROR("PtFrac not available");
90  inputvar_missing = true;
91  }
92 
93  if (!s_acc_topoetcone20.isAvailable(Particle)) {
94  ATH_MSG_ERROR("topoetcone20 not available");
95  inputvar_missing = true;
96  }
97 
98  if (Particle.type() == xAOD::Type::ObjectType::Electron && !s_acc_ptvarcone20.isAvailable(Particle)) {
99  ATH_MSG_ERROR("ptvarcone20 not available");
100  inputvar_missing = true;
101  }
102 
103  if (Particle.type() == xAOD::Type::ObjectType::Muon && !s_acc_ptvarcone30.isAvailable(Particle)) {
104  ATH_MSG_ERROR("ptvarcone30 not available");
105  inputvar_missing = true;
106  }
107 
108  if (inputvar_missing) {
109  ATH_MSG_ERROR("input variable(s) missing, augmenting fixed value 1.1");
110  s_dec_iso_PLT(Particle) = 1.1;
111  return StatusCode::FAILURE;
112  }
113 
114  short TrackJetNTrack = s_acc_TrackJetNTrack(Particle);
115  float DRlj = s_acc_DRlj(Particle);
116  float PtRel = s_acc_PtRel(Particle);
117  float PtFrac = s_acc_PtFrac(Particle);
118  float topoetcone20 = s_acc_topoetcone20(Particle);
119  float ptvarcone30 = 0;
120  float ptvarcone20 = 0;
121 
122  float pt = Particle.pt();
123  float score = 1.1;
124  std::vector<double> var_vector(N_VARIABLES, 0);
126  ptvarcone30 = s_acc_ptvarcone30(Particle);
127  var_vector[0] = TrackJetNTrack;
128  var_vector[1] = PtRel;
129  var_vector[2] = PtFrac;
130  var_vector[3] = DRlj;
131  var_vector[4] = topoetcone20 / pt;
132  var_vector[5] = ptvarcone30 / pt;
133  score = m_TMVAReader_Muon->EvaluateMVA(var_vector, m_muonMethodName.value());
135  ptvarcone20 = s_acc_ptvarcone20(Particle);
136  var_vector[0] = TrackJetNTrack;
137  var_vector[1] = PtRel;
138  var_vector[2] = PtFrac;
139  var_vector[3] = DRlj;
140  var_vector[4] = topoetcone20 / pt;
141  var_vector[5] = ptvarcone20 / pt;
142  score = m_TMVAReader_Elec->EvaluateMVA(var_vector, m_elecMethodName.value());
143  } else {
144  ATH_MSG_ERROR("The function needs either a muon or an electron!");
145  return StatusCode::FAILURE;
146  }
147  s_dec_iso_PLT(Particle) = score;
148 
149  return StatusCode::SUCCESS;
150  }
151 } // namespace CP
xAOD::Iso::ptvarcone30
@ ptvarcone30
Definition: IsolationType.h:56
CP::N_VARIABLES
constexpr int N_VARIABLES
Definition: IsolationLowPtPLVTool.cxx:28
CP::IsolationLowPtPLVTool::IsolationLowPtPLVTool
IsolationLowPtPLVTool(const std::string &name)
Definition: IsolationLowPtPLVTool.cxx:30
xAOD::Iso::topoetcone20
@ topoetcone20
Topo-cluster ET-sum.
Definition: IsolationType.h:48
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
xAOD::Iso::ptvarcone20
@ ptvarcone20
Mini-Isolation http://arxiv.org/abs/1007.2221.
Definition: IsolationType.h:55
CP::IsolationLowPtPLVTool::m_TMVAReader_Elec
std::unique_ptr< TMVA::Reader > m_TMVAReader_Elec
Definition: IsolationLowPtPLVTool.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CP::IsolationLowPtPLVTool::m_elecCalibFile
Gaudi::Property< std::string > m_elecCalibFile
Definition: IsolationLowPtPLVTool.h:28
Prompt::Def::TrackJetNTrack
@ TrackJetNTrack
Definition: VarHolder.h:69
asg
Definition: DataHandleTestTool.h:28
test_pyathena.pt
pt
Definition: test_pyathena.py:11
CP::IsolationLowPtPLVTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: IsolationLowPtPLVTool.cxx:32
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
Prompt::Def::DRlj
@ DRlj
Definition: VarHolder.h:70
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
CP::FloatDecorator
SG::AuxElement::Decorator< float > FloatDecorator
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:22
CP::IsolationLowPtPLVTool::augmentPLV
virtual StatusCode augmentPLV(const xAOD::IParticle &particle) override
This method adds the lowPT PLV score as decoration to the lepton.
Definition: IsolationLowPtPLVTool.cxx:70
Prompt::Def::PtFrac
@ PtFrac
Definition: VarHolder.h:71
CP::IsolationLowPtPLVTool::m_elecMethodName
Gaudi::Property< std::string > m_elecMethodName
Definition: IsolationLowPtPLVTool.h:32
ObjectType.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
IsolationLowPtPLVTool.h
CP::IsolationLowPtPLVTool::m_muonMethodName
Gaudi::Property< std::string > m_muonMethodName
Definition: IsolationLowPtPLVTool.h:31
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::Particle_v1
Description of a generic particle.
Definition: Particle_v1.h:31
PathResolver.h
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
Muon
struct TBPatternUnitContext Muon
CP::IsolationLowPtPLVTool::m_TMVAReader_Muon
std::unique_ptr< TMVA::Reader > m_TMVAReader_Muon
Definition: IsolationLowPtPLVTool.h:33
xAOD::score
@ score
Definition: TrackingPrimitives.h:513
xAOD::Particle_v1::type
virtual Type::ObjectType type() const
The type of the object as a simple enumeration.
Definition: Particle_v1.cxx:56
CP::FloatAccessor
SG::AuxElement::ConstAccessor< float > FloatAccessor
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:21
CP::IsolationLowPtPLVTool::m_muonCalibFile
Gaudi::Property< std::string > m_muonCalibFile
Definition: IsolationLowPtPLVTool.h:25
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.
IParticleHelpers.h
CP::ShortAccessor
SG::AuxElement::ConstAccessor< short > ShortAccessor
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:34
xAOD::Particle_v1::pt
virtual double pt() const
The transverse momentum ( ) of the particle.
Definition: Particle_v1.cxx:26