ATLAS Offline Software
Loading...
Searching...
No Matches
ElectronPhotonVariableCorrectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 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 for( unsigned int confFile_itr = 0; confFile_itr < confFiles.size(); confFile_itr++ )
125 {
126 // name: supertool name + type name + variable name
127 std::string variable = ""; //get the name of the variable to be corrected
128 std::string confFileWithFullPath = PathResolverFindCalibFile(confFiles.at(confFile_itr));
129 ANA_CHECK(getCorrectionVariableName(variable, confFileWithFullPath));
130 TString toolname = TString::Format("%s_%s_%s", this->name().c_str(), name.c_str(), variable.c_str());
131 ANA_MSG_DEBUG("Subtool name: " << toolname.Data());
132 toolHolder.at(confFile_itr) = std::make_unique<ElectronPhotonVariableCorrectionBase>(toolname.Data());
133 ANA_CHECK(toolHolder.at(confFile_itr)->setProperty("ConfigFile", confFiles.at(confFile_itr)));
134 ANA_CHECK(toolHolder.at(confFile_itr)->setProperty("OutputLevel", this->msg().level()));
135 ANA_CHECK(toolHolder.at(confFile_itr)->initialize());
136 }
137 //everything worked out, so
138 return StatusCode::SUCCESS;
139}
140
141const StatusCode ElectronPhotonVariableCorrectionTool::applyToFlagMatchesToolHolder( const std::vector<std::string>& confFiles, const std::vector<std::unique_ptr<ElectronPhotonVariableCorrectionBase>>& toolHolder, ElectronPhotonVariableCorrectionBase::EGammaObjects toolHolderType )
142{
143 // loop over conf file holder
144 for (unsigned int tool_itr = 0; tool_itr < toolHolder.size(); tool_itr++)
145 {
146 // get ApplyTo flag
147 ElectronPhotonVariableCorrectionBase::EGammaObjects confFileType = toolHolder.at(tool_itr)->isAppliedTo();
148 // skip all further tests if should be applied to all objects
150 // continue if ApplyTo flag matches toolholder
151 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::convertedPhotons && toolHolder.at(tool_itr)->applyToConvertedPhotons()) continue;
152 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::unconvertedPhotons && toolHolder.at(tool_itr)->applyToUnconvertedPhotons()) continue;
153 if (toolHolderType == ElectronPhotonVariableCorrectionBase::EGammaObjects::allElectrons && toolHolder.at(tool_itr)->applyToElectrons()) continue;
154 // if this point is reached, something is wrong, so
155 ATH_MSG_ERROR("In conf " << confFiles.at(tool_itr) << ": The ApplyTo flag does not match with the container type from the conf file.");
156 return StatusCode::FAILURE;
157 }
158 //everything worked out, so
159 return StatusCode::SUCCESS;
160}
161
162/* ========================================
163 * Apply correction
164 * ======================================== */
165
167{
168 bool isConvertedPhoton = xAOD::EgammaHelpers::isConvertedPhoton(&photon);
169
170 // check if need to run over converted or unconverted photons
171 if (isConvertedPhoton)
172 {
173 // correct variables on the converted photon
174 for (unsigned int convertedPhotonTool_itr = 0; convertedPhotonTool_itr < m_convertedPhotonTools.size(); convertedPhotonTool_itr++)
175 {
176 ATH_MSG_VERBOSE("Running tool " << m_convertedPhotonTools.at(convertedPhotonTool_itr)->name());
177 if ((m_convertedPhotonTools.at(convertedPhotonTool_itr)->applyCorrection(photon)) != CP::CorrectionCode::Ok)
178 {
179 ATH_MSG_ERROR("Could not apply correction to converted photon object.");
181 }
182 }
183 }
184 else
185 {
186 // correct variables on the converted photon
187 for (unsigned int unconvertedPhotonTool_itr = 0; unconvertedPhotonTool_itr < m_unconvertedPhotonTools.size(); unconvertedPhotonTool_itr++)
188 {
189 ATH_MSG_VERBOSE("Running tool " << m_unconvertedPhotonTools.at(unconvertedPhotonTool_itr)->name());
190 if ((m_unconvertedPhotonTools.at(unconvertedPhotonTool_itr)->applyCorrection(photon)) != CP::CorrectionCode::Ok)
191 {
192 ATH_MSG_ERROR("Could not apply correction to unconverted photon object.");
194 }
195 }
196 }
197
198 // everything worked out, so
200}
202{
203 // correct variables on the electron
204 for (unsigned int electronTool_itr = 0; electronTool_itr < m_electronTools.size(); electronTool_itr++)
205 {
206 ATH_MSG_VERBOSE("Running tool " << m_electronTools.at(electronTool_itr)->name());
207 if ((m_electronTools.at(electronTool_itr)->applyCorrection(electron)) != CP::CorrectionCode::Ok)
208 {
209 ATH_MSG_ERROR("Could not apply correction to electron object.");
211 }
212 }
213 // everything worked out, so
215}
216
217/* ========================================
218 * Corrected Copy
219 * ======================================== */
220
222{
223 ATH_MSG_VERBOSE("Will correct photon " << &in_photon << " of pT, eta = " << in_photon.pt() << " " << in_photon.eta());
224 out_photon = new xAOD::Photon(in_photon);
225 return applyCorrection(*out_photon);
226}
227
229{
230 ATH_MSG_VERBOSE("Will correct electron " << &in_electron << " of pT, eta = " << in_electron.pt() << " " << in_electron.eta());
231 out_electron = new xAOD::Electron(in_electron);
232 return applyCorrection(*out_electron);
233}
234
235/* ========================================
236 * Helper functions
237 * ======================================== */
238
239const StatusCode ElectronPhotonVariableCorrectionTool::getCorrectionVariableName( std::string &variableName, const std::string& confFile ) const
240{
241 // Retrieve properties from configuration file, using TEnv class
242 TEnv env;
243 env.ReadFile(confFile.c_str(), kEnvLocal);
244 // Send warning if duplicates found in conf file
245 env.IgnoreDuplicates(false);
246
247 // retrieve variable name
248 if (env.Lookup("Variable"))
249 {
250 variableName = env.GetValue("Variable", "");
251 }
252 else
253 {
254 ATH_MSG_ERROR("In conf file " << confFile << ": Correction variable is empty or not in configuration file.");
255 return StatusCode::FAILURE;
256 }
257 //everything worked out, so
258 return StatusCode::SUCCESS;
259}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug 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".
Electron_v1 Electron
Definition of the current "egamma version".