ATLAS Offline Software
Loading...
Searching...
No Matches
ElectronPhotonVariableCorrectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
12
16#include "TEnv.h"
17
18// ===========================================================================
19// Standard Constructor
20// ===========================================================================
22 AsgTool(myname)
23{
24 //declare the needed properties
25 declareProperty("ConfigFile",m_configFile="", "The configuration file to use");
26}
27
29{
30 // Locate configuration file, abort if not found
31 std::string configFile;
32 if (!m_configFile.empty())
33 {
35 if (configFile.empty())
36 {
37 ATH_MSG_ERROR("Could not locate configuration file " << m_configFile);
38 return StatusCode::FAILURE;
39 }
40 ATH_MSG_DEBUG("Use configuration file " << m_configFile);
41 }
42 else
43 {
44 ATH_MSG_ERROR("Config file string is empty. Please provide a config file to the tool.");
45 return StatusCode::FAILURE;
46 }
47
48 // Retreive properties from configuration file, using TEnv class
49 TEnv env;
50 env.ReadFile(configFile.c_str(), kEnvLocal);
51 // Send warning if duplicates found in conf file
52 env.IgnoreDuplicates(false);
53
54 // retrieve different object type conf files
55 if (env.Lookup("ElectronConfigs"))
56 {
58 }
59 if (env.Lookup("ConvertedPhotonConfigs"))
60 {
61 m_convertedPhotonConfFiles = AsgConfigHelper::HelperString("ConvertedPhotonConfigs",env);
62 }
63 if (env.Lookup("UnconvertedPhotonConfigs"))
64 {
65 m_unconvertedPhotonConfFiles = AsgConfigHelper::HelperString("UnconvertedPhotonConfigs",env);
66 }
67
68 // check if any conf files were received
70 {
71 ANA_MSG_ERROR("You did not provide any config files for the ElectronPhotonVariableCorrectionBase to the ElectronPhotonVariableCorrectionTool.");
72 return StatusCode::FAILURE;
73 }
74
75 ATH_MSG_VERBOSE("number of files for the electron case : " << m_electronConfFiles.size());
76 for (const auto& fN : m_electronConfFiles)
77 ATH_MSG_VERBOSE("file " << fN);
78
79 // initialize the ElectronPhotonVariableCorrectionTools
81
82 ANA_MSG_INFO("Initialized tool " << name());
83 //everything worked out, so
84 return StatusCode::SUCCESS;
85}
86
101
102const StatusCode ElectronPhotonVariableCorrectionTool::findAllConfigFiles( std::vector<std::string>& confFiles )
103{
104 //loop over conf file vector, find conf file using path resolver
105 for( unsigned int confFile_itr = 0; confFile_itr < confFiles.size(); confFile_itr++ )
106 {
107 std::string tmp_confFile = confFiles.at(confFile_itr);
108 confFiles.at(confFile_itr) = PathResolverFindCalibFile(confFiles.at(confFile_itr));
109 if (confFiles.at(confFile_itr).empty())
110 {
111 ATH_MSG_ERROR("Could not locate configuration file " << tmp_confFile);
112 return StatusCode::FAILURE;
113 }
114 }
115 //everything worked out, so
116 return StatusCode::SUCCESS;
117}
118
119const StatusCode ElectronPhotonVariableCorrectionTool::initializeTools( const std::string& name, const std::vector<std::string>& confFiles, std::vector<std::unique_ptr<ElectronPhotonVariableCorrectionBase>>& toolHolder )
120{
121 // adapt size of toolHolder
122 toolHolder.resize(confFiles.size());
123 // for each conf file, initialize one tool
124 const std::string configFilePropStr{"ConfigFile"};
125 const std::string opLvlPropStr{"OutputLevel"};
126 for( unsigned int confFile_itr = 0; confFile_itr < confFiles.size(); confFile_itr++ )
127 {
128 // name: supertool name + type name + variable name
129 std::string variable = ""; //get the name of the variable to be corrected
130 std::string confFileWithFullPath = PathResolverFindCalibFile(confFiles.at(confFile_itr));
131 ANA_CHECK(getCorrectionVariableName(variable, confFileWithFullPath));
132 TString toolname = TString::Format("%s_%s_%s", this->name().c_str(), name.c_str(), variable.c_str());
133 ANA_MSG_DEBUG("Subtool name: " << toolname.Data());
134 toolHolder.at(confFile_itr) = std::make_unique<ElectronPhotonVariableCorrectionBase>(toolname.Data());
135 ANA_CHECK(toolHolder.at(confFile_itr)->setProperty(configFilePropStr, confFiles.at(confFile_itr)));
136 ANA_CHECK(toolHolder.at(confFile_itr)->setProperty(opLvlPropStr, this->msg().level()));
137 ANA_CHECK(toolHolder.at(confFile_itr)->initialize());
138 }
139 //everything worked out, so
140 return StatusCode::SUCCESS;
141}
142
143const StatusCode ElectronPhotonVariableCorrectionTool::applyToFlagMatchesToolHolder( const std::vector<std::string>& confFiles, const std::vector<std::unique_ptr<ElectronPhotonVariableCorrectionBase>>& toolHolder, ElectronPhotonVariableCorrectionBase::EGammaObjects toolHolderType )
144{
145 // loop over conf file holder
146 for (unsigned int tool_itr = 0; tool_itr < toolHolder.size(); tool_itr++)
147 {
148 // get ApplyTo flag
149 ElectronPhotonVariableCorrectionBase::EGammaObjects confFileType = toolHolder.at(tool_itr)->isAppliedTo();
150 // skip all further tests if should be applied to all objects
152 // continue if ApplyTo flag matches toolholder
153 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::convertedPhotons && toolHolder.at(tool_itr)->applyToConvertedPhotons()) continue;
154 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::unconvertedPhotons && toolHolder.at(tool_itr)->applyToUnconvertedPhotons()) continue;
155 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::allElectrons && toolHolder.at(tool_itr)->applyToElectrons()) continue;
156 // if this point is reached, something is wrong, so
157 ATH_MSG_ERROR("In conf " << confFiles.at(tool_itr) << ": The ApplyTo flag does not match with the container type from the conf file.");
158 return StatusCode::FAILURE;
159 }
160 //everything worked out, so
161 return StatusCode::SUCCESS;
162}
163
164/* ========================================
165 * Apply correction
166 * ======================================== */
167
169{
170 bool isConvertedPhoton = xAOD::EgammaHelpers::isConvertedPhoton(&photon);
171
172 // check if need to run over converted or unconverted photons
173 if (isConvertedPhoton)
174 {
175 // correct variables on the converted photon
176 for (unsigned int convertedPhotonTool_itr = 0; convertedPhotonTool_itr < m_convertedPhotonTools.size(); convertedPhotonTool_itr++)
177 {
178 ATH_MSG_VERBOSE("Running tool " << m_convertedPhotonTools.at(convertedPhotonTool_itr)->name());
179 if ((m_convertedPhotonTools.at(convertedPhotonTool_itr)->applyCorrection(photon)) != CP::CorrectionCode::Ok)
180 {
181 ATH_MSG_ERROR("Could not apply correction to converted photon object.");
183 }
184 }
185 }
186 else
187 {
188 // correct variables on the converted photon
189 for (unsigned int unconvertedPhotonTool_itr = 0; unconvertedPhotonTool_itr < m_unconvertedPhotonTools.size(); unconvertedPhotonTool_itr++)
190 {
191 ATH_MSG_VERBOSE("Running tool " << m_unconvertedPhotonTools.at(unconvertedPhotonTool_itr)->name());
192 if ((m_unconvertedPhotonTools.at(unconvertedPhotonTool_itr)->applyCorrection(photon)) != CP::CorrectionCode::Ok)
193 {
194 ATH_MSG_ERROR("Could not apply correction to unconverted photon object.");
196 }
197 }
198 }
199
200 // everything worked out, so
202}
204{
205 // correct variables on the electron
206 for (unsigned int electronTool_itr = 0; electronTool_itr < m_electronTools.size(); electronTool_itr++)
207 {
208 ATH_MSG_VERBOSE("Running tool " << m_electronTools.at(electronTool_itr)->name());
209 if ((m_electronTools.at(electronTool_itr)->applyCorrection(electron)) != CP::CorrectionCode::Ok)
210 {
211 ATH_MSG_ERROR("Could not apply correction to electron object.");
213 }
214 }
215 // everything worked out, so
217}
218
219/* ========================================
220 * Corrected Copy
221 * ======================================== */
222
224{
225 ATH_MSG_VERBOSE("Will correct photon " << &in_photon << " of pT, eta = " << in_photon.pt() << " " << in_photon.eta());
226 out_photon = new xAOD::Photon(in_photon);
227 return applyCorrection(*out_photon);
228}
229
231{
232 ATH_MSG_VERBOSE("Will correct electron " << &in_electron << " of pT, eta = " << in_electron.pt() << " " << in_electron.eta());
233 out_electron = new xAOD::Electron(in_electron);
234 return applyCorrection(*out_electron);
235}
236
237/* ========================================
238 * Helper functions
239 * ======================================== */
240
241const StatusCode ElectronPhotonVariableCorrectionTool::getCorrectionVariableName( std::string &variableName, const std::string& confFile ) const
242{
243 // Retrieve properties from configuration file, using TEnv class
244 TEnv env;
245 env.ReadFile(confFile.c_str(), kEnvLocal);
246 // Send warning if duplicates found in conf file
247 env.IgnoreDuplicates(false);
248
249 // retrieve variable name
250 if (env.Lookup("Variable"))
251 {
252 variableName = env.GetValue("Variable", "");
253 }
254 else
255 {
256 ATH_MSG_ERROR("In conf file " << confFile << ": Correction variable is empty or not in configuration file.");
257 return StatusCode::FAILURE;
258 }
259 //everything worked out, so
260 return StatusCode::SUCCESS;
261}
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ANA_MSG_ERROR(xmsg,...)
Macro printing error messages.
#define ANA_MSG_DEBUG(xmsg,...)
Macro printing debug messages.
#define ANA_MSG_INFO(xmsg,...)
Macro printing info messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Return value from object correction CP tools.
@ Error
Some error happened during the object correction.
@ Ok
The correction was done successfully.
EGammaObjects
Define the categories of EGamma objects tool can be applied to.
std::vector< std::string > m_electronConfFiles
The configuration files for the base class - electrons.
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_electronTools
The base class instances for single variable correction - electrons.
const StatusCode initializeCorrectionTools()
Configure, initialize, and check the base class instances saved in the m_*Tools members.
std::vector< std::string > m_unconvertedPhotonConfFiles
The configuration files for the base class - unconverted photons.
virtual const CP::CorrectionCode correctedCopy(const xAOD::Photon &in_photon, xAOD::Photon *&out_photon) const override
Make a corrected copy of the passed photon according to the given conf file.
const StatusCode findAllConfigFiles(std::vector< std::string > &confFiles)
Locate all config files for the base class instances passed to the tool in m_configFile in the ATLAS ...
const StatusCode initializeTools(const std::string &name, const std::vector< std::string > &confFiles, std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > &toolHolder)
Initialize the base clase instances saved in the m_*Tools members.
virtual StatusCode initialize() override
Initialize the tool instance.
ElectronPhotonVariableCorrectionTool(const std::string &myname)
Declare the interface that the class provides.
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_unconvertedPhotonTools
The base class instances for single variable correction - unconverted photons.
std::string m_configFile
The configuration file for the tool.
const StatusCode applyToFlagMatchesToolHolder(const std::vector< std::string > &confFiles, const std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > &toolHolder, ElectronPhotonVariableCorrectionBase::EGammaObjects toolHolderType)
Check if the ApplyTo flag from the base class conf file does match with the container type spcified i...
std::vector< std::string > m_convertedPhotonConfFiles
The configuration files for the base class - converted photons.
virtual const CP::CorrectionCode applyCorrection(xAOD::Photon &photon) const override
Apply the correction given in the conf file to the passed photon.
const StatusCode getCorrectionVariableName(std::string &variableName, const std::string &confFile) const
Get current name of the variable to be corrected by the current base class instance,...
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_convertedPhotonTools
The base class instances for single variable correction - converted photons.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition Egamma_v1.cxx:66
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition Egamma_v1.cxx:71
std::vector< std::string > HelperString(const std::string &input, TEnv &env)
bool isConvertedPhoton(const xAOD::Egamma *eg, bool excludeTRT=false)
is the object a converted photon
Photon_v1 Photon
Definition of the current "egamma version".
Definition Photon.h:17
Electron_v1 Electron
Definition of the current "egamma version".
Definition Electron.h:17