ATLAS Offline Software
Loading...
Searching...
No Matches
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"
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");
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
ToolHandle< IegammaMVACalibTool > m_mvaElectron
MVA tool for electron.
virtual StatusCode initialize() override
StatusCode execute(xAOD::CaloCluster &cluster, const xAOD::Egamma &eg, const egammaMVACalib::GlobalEventInfo &gei=egammaMVACalib::GlobalEventInfo()) const override final
Main execute.
ToolHandle< IegammaMVACalibTool > m_mvaUnconvertedPhoton
MVA tool for unconverted photon.
Gaudi::Property< float > m_maxConvR
StatusCode resolve_flags()
Gaudi::Property< int > m_removeTRTConvBarrel
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.
ToolHandle< IegammaMVACalibTool > m_mvaConvertedPhoton
MVA tool for converted photon.
ToolHandle< IegammaMVACalibTool > m_mvaFwdElectron
MVA tool for forward electron.
Gaudi::Property< std::string > m_folder
bool isConvCalib(const xAOD::Photon &ph) const
Decide if the photon is converted or not.
virtual double e() const
The total energy of the particle.
void setCalE(flt_t)
Set Energy for signal state CALIBRATED.
bool isFwdElectron(const xAOD::Egamma *eg)
is the object a Fwd electron
bool isConvertedPhoton(const xAOD::Egamma *eg, bool excludeTRT=false)
is the object a converted photon
float conversionRadius(const xAOD::Vertex *vx)
return the conversion radius or 9999.
bool isElectron(const xAOD::Egamma *eg)
is the object an electron (not Fwd)
bool isPhoton(const xAOD::Egamma *eg)
is the object a photon
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
Egamma_v1 Egamma
Definition of the current "egamma version".
Definition Egamma.h:17
Photon_v1 Photon
Definition of the current "egamma version".
A structure holding some global event information.