ATLAS Offline Software
egammaMVASvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "xAODEgamma/Egamma.h"
9 #include "xAODEgamma/Electron.h"
10 #include "xAODEgamma/Photon.h"
13 
14 #include <TFile.h>
15 #include <TObjString.h>
16 
17 
19 {
20  ATH_MSG_DEBUG("In initialize of " << name() << "..." );
21 
22  if (!m_mvaElectron.empty()) {
23  ATH_MSG_DEBUG("Retrieving mvaElectron");
24  ATH_CHECK(m_mvaElectron.retrieve());
25  } else {
26  ATH_MSG_DEBUG("Disabling mvaElectron");
27  m_mvaElectron.disable();
28  }
29 
30  if (!m_mvaFwdElectron.empty()) {
31  ATH_MSG_DEBUG("Retrieving mvaFwdElectron");
32  ATH_CHECK(m_mvaFwdElectron.retrieve());
33  } else {
34  ATH_MSG_DEBUG("Disabling mvaFwdElectron");
35  m_mvaFwdElectron.disable();
36  }
37 
38  if (!m_mvaUnconvertedPhoton.empty()) {
39  ATH_MSG_DEBUG("Retrieving mvaUnconvertedPhoton");
41  } else {
42  ATH_MSG_DEBUG("Disabling mvaUnconvertedPhoton");
43  m_mvaUnconvertedPhoton.disable();
44  }
45 
46  if (!m_mvaConvertedPhoton.empty()) {
47  ATH_MSG_DEBUG("Retrieving mvaConvertedPhoton");
48  ATH_CHECK(m_mvaConvertedPhoton.retrieve());
49  } else {
50  ATH_MSG_DEBUG("Disabling mvaConvertedPhoton");
51  m_mvaConvertedPhoton.disable();
52  }
53 
55 
58 
59  return StatusCode::SUCCESS;
60 }
61 
63 {
64  if (m_removeTRTConvBarrel == -1) {
65  const bool removeTRTConvBarrelDefault = false;
66  if (!m_mvaConvertedPhoton.empty() and !m_folder.empty()) {
67  const std::string mva_filename = PathResolverFindCalibFile(m_folder + "/MVACalib_convertedPhoton.weights.root");
68  std::unique_ptr<TFile> f(TFile::Open(mva_filename.c_str()));
69  if (!f || f->IsZombie()) {
70  ATH_MSG_ERROR("Could not open file: " << mva_filename);
71  m_removeTRTConvBarrel = removeTRTConvBarrelDefault;
72  }
73  else { // we have the MVA for converted photons
74  TObjString* conversionDefinitionObj = nullptr;
75  f->GetObject("conversionDefinition", conversionDefinitionObj);
76  if (conversionDefinitionObj) {
77  m_removeTRTConvBarrel = (conversionDefinitionObj->GetString() == "removeTRTConvBarrel");
78  } else {
79  // the conversion definition is not encoded in the file, this is true for the old ones
80  m_removeTRTConvBarrel = removeTRTConvBarrelDefault;
81  }
82  }
83  } else { // we are running without converted
84  m_removeTRTConvBarrel = removeTRTConvBarrelDefault;
85  }
86  }
87  if (m_removeTRTConvBarrel == -1) {
88  ATH_MSG_ERROR("Could not determine if TRT converted photons should be removed in the barrel");
89  return StatusCode::FAILURE;
90  }
91  return StatusCode::SUCCESS;
92 }
93 
95 {
99  // special case in Run3 to avoid TRT converted photons in the barrel
101  }
102  return isConvCalib;
103 }
104 
106  const xAOD::Egamma& eg,
107  double& mvaE,
108  const egammaMVACalib::GlobalEventInfo& gei) const
109 {
110 
111  ATH_MSG_DEBUG("calling egammaMVASvc::getEnergy with cluster and eg");
112 
113  mvaE = 0.;
114 
116  if (!m_mvaElectron.empty()) {
117  mvaE = m_mvaElectron->getEnergy(cluster, &eg, gei);
118  } else {
119  ATH_MSG_FATAL("Trying to calibrate an electron, but disabled");
120  return StatusCode::FAILURE;
121  }
122  } else if (xAOD::EgammaHelpers::isFwdElectron(&eg)) {
123  if (!m_mvaFwdElectron.empty()) {
124  mvaE = m_mvaFwdElectron->getEnergy(cluster, &eg, gei);
125  } else {
126  ATH_MSG_FATAL("Trying to calibrate a forward electron, but disabled");
127  return StatusCode::FAILURE;
128  }
129  } else if (xAOD::EgammaHelpers::isPhoton(&eg)) {
130  const xAOD::Photon* ph = static_cast<const xAOD::Photon*>(&eg);
131  const bool is_conv_calib = isConvCalib(*ph);
132  if (is_conv_calib) {
133  if (!m_mvaConvertedPhoton.empty()) {
134  mvaE = m_mvaConvertedPhoton->getEnergy(cluster, &eg, gei);
135  } else {
136  ATH_MSG_FATAL("Trying to calibrate a converted photon, but disabled");
137  return StatusCode::FAILURE;
138  }
139  } else {
140  if (!m_mvaUnconvertedPhoton.empty()) {
141  mvaE = m_mvaUnconvertedPhoton->getEnergy(cluster, &eg, gei);
142  } else {
143  ATH_MSG_FATAL("Trying to calibrate an unconverted photon, but disabled");
144  return StatusCode::FAILURE;
145  }
146  }
147  } else {
148  ATH_MSG_FATAL("Egamma object is of unsupported type");
149  return StatusCode::FAILURE;
150  }
151 
152  ATH_MSG_DEBUG( "Calculated MVA calibrated energy = " << mvaE );
153  return StatusCode::SUCCESS;
154 }
155 
158  double& mvaE,
159  const egammaMVACalib::GlobalEventInfo& gei) const
160 {
161 
162  ATH_MSG_DEBUG("calling egammaMVASvc::getEnergy with cluster and egType (" << egType <<")");
163 
164  mvaE = 0.0;
165  switch (egType) {
167  if (!m_mvaElectron.empty()) {
168  mvaE = m_mvaElectron->getEnergy(cluster,nullptr, gei);
169  } else {
170  ATH_MSG_FATAL("Trying to calibrate an electron, but disabled");
171  return StatusCode::FAILURE;
172  }
173  break;
175  if (!m_mvaFwdElectron.empty()) {
176  mvaE = m_mvaFwdElectron->getEnergy(cluster,nullptr, gei);
177  } else {
178  ATH_MSG_FATAL("Trying to calibrate a forward electron, but disabled");
179  return StatusCode::FAILURE;
180  }
181  break;
184  // treat converted photons like unconverted photons since don't have access to vertex
185  if (!m_mvaUnconvertedPhoton.empty()) {
186  mvaE = m_mvaUnconvertedPhoton->getEnergy(cluster,nullptr, gei);
187  } else {
188  ATH_MSG_FATAL("Trying to calibrate an unconverted photon, but disabled");
189  return StatusCode::FAILURE;
190  }
191  break;
192  default:
193  ATH_MSG_FATAL("Egamma object " << egType << " is of unsupported type");
194  return StatusCode::FAILURE;
195  }
196 
197  ATH_MSG_DEBUG( "Calculated MVA calibrated energy = " << mvaE );
198  return StatusCode::SUCCESS;
199 }
200 
201 
203  const xAOD::Egamma& eg,
204  const egammaMVACalib::GlobalEventInfo& gei) const
205 {
206  double mvaE = 0.;
207 
208  ATH_CHECK(getEnergy(cluster, eg, mvaE, gei));
209 
210  if (mvaE > eg.m()) {
211  cluster.setCalE(mvaE);
212  }
213  else {
214  ATH_MSG_DEBUG("MVA energy (" << mvaE << ") < particle mass ("
215  << eg.m() << "), setting e = cluster energy (" << cluster.e() << ")");
216  cluster.setCalE(cluster.e());
217  }
218  return StatusCode::SUCCESS;
219 }
220 
223  const egammaMVACalib::GlobalEventInfo& gei) const
224 {
225 
226  double mvaE = 0.;
227 
228  ATH_CHECK(getEnergy(cluster, egType, mvaE, gei));
229 
230  if (mvaE > 0) {
231  cluster.setCalE(mvaE);
232  }
233  else {
234  ATH_MSG_DEBUG("MVA energy (" << mvaE << ") < 0, setting e = cluster energy ("
235  << cluster.e() << ")");
236  cluster.setCalE(cluster.e());
237  }
238  return StatusCode::SUCCESS;
239 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
egammaMVASvc::initialize
virtual StatusCode initialize() override
Definition: egammaMVASvc.cxx:18
egammaMVASvc::m_folder
Gaudi::Property< std::string > m_folder
Definition: egammaMVASvc.h:88
egammaMVASvc::m_removeTRTConvBarrel
Gaudi::Property< int > m_removeTRTConvBarrel
Definition: egammaMVASvc.h:84
xAOD::EgammaParameters::convertedPhoton
@ convertedPhoton
Definition: EgammaEnums.h:20
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ParticleTest.eg
eg
Definition: ParticleTest.py:29
egammaMVASvc::getEnergy
StatusCode getEnergy(const xAOD::CaloCluster &cluster, const xAOD::Egamma &eg, double &mvaE, const egammaMVACalib::GlobalEventInfo &gei=egammaMVACalib::GlobalEventInfo()) const override final
Compute the calibrated energy.
Definition: egammaMVASvc.cxx:105
egammaMVASvc::m_mvaFwdElectron
ToolHandle< IegammaMVACalibTool > m_mvaFwdElectron
MVA tool for forward electron.
Definition: egammaMVASvc.h:69
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
egammaMVASvc::resolve_flags
StatusCode resolve_flags()
Definition: egammaMVASvc.cxx:62
xAOD::EgammaParameters::EgammaType
EgammaType
Definition: EgammaEnums.h:17
Egamma.h
egammaMVASvc::m_maxConvR
Gaudi::Property< float > m_maxConvR
Definition: egammaMVASvc.h:80
egammaMVASvc::m_mvaElectron
ToolHandle< IegammaMVACalibTool > m_mvaElectron
MVA tool for electron.
Definition: egammaMVASvc.h:65
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:62
EgammaxAODHelpers.h
xAOD::EgammaHelpers::isConvertedPhoton
bool isConvertedPhoton(const xAOD::Egamma *eg, bool excludeTRT=false)
is the object a converted photon
Definition: EgammaxAODHelpers.cxx:25
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CaloCluster.h
Photon.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
xAOD::EgammaHelpers::isElectron
bool isElectron(const xAOD::Egamma *eg)
is the object an electron (not Fwd)
Definition: EgammaxAODHelpers.cxx:12
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:140
xAOD::EgammaHelpers::isFwdElectron
bool isFwdElectron(const xAOD::Egamma *eg)
is the object a Fwd electron
Definition: EgammaxAODHelpers.cxx:17
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
xAOD::EgammaHelpers::conversionRadius
float conversionRadius(const xAOD::Vertex *vx)
return the conversion radius or 9999.
Definition: PhotonxAODHelpers.cxx:58
xAOD::CaloCluster_v1::setCalE
void setCalE(flt_t)
Set Energy for signal state CALIBRATED.
Definition: CaloCluster_v1.cxx:306
egammaMVASvc::isConvCalib
bool isConvCalib(const xAOD::Photon &ph) const
Decide if the photon is converted or not.
Definition: egammaMVASvc.cxx:94
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:321
egammaMVASvc.h
xAOD::Photon_v1
Definition: Photon_v1.h:37
egammaMVACalib::GlobalEventInfo
A structure holding some global event information.
Definition: GlobalEventInfo.h:9
xAOD::EgammaHelpers::isPhoton
bool isPhoton(const xAOD::Egamma *eg)
is the object a photon
Definition: EgammaxAODHelpers.cxx:21
xAOD::EgammaParameters::forwardelectron
@ forwardelectron
Definition: EgammaEnums.h:21
EgammaDefs.h
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
Electron.h
egammaMVASvc::execute
StatusCode execute(xAOD::CaloCluster &cluster, const xAOD::Egamma &eg, const egammaMVACalib::GlobalEventInfo &gei=egammaMVACalib::GlobalEventInfo()) const override final
Main execute.
Definition: egammaMVASvc.cxx:202
egammaMVASvc::m_mvaUnconvertedPhoton
ToolHandle< IegammaMVACalibTool > m_mvaUnconvertedPhoton
MVA tool for unconverted photon.
Definition: egammaMVASvc.h:73
xAOD::CaloCluster_v1::e
virtual double e() const
The total energy of the particle.
Definition: CaloCluster_v1.cxx:265
egammaMVASvc::m_mvaConvertedPhoton
ToolHandle< IegammaMVACalibTool > m_mvaConvertedPhoton
MVA tool for converted photon.
Definition: egammaMVASvc.h:77
xAOD::EgammaParameters::unconvertedPhoton
@ unconvertedPhoton
Definition: EgammaEnums.h:19