ATLAS Offline Software
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 
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  {
62  }
63  if (env.Lookup("UnconvertedPhotonConfigs"))
64  {
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 
88 {
89  //initialize all tools
93  // check if ApplyTo Flag matches with the tool holder
97 
98  //everything worked out, so
99  return StatusCode::SUCCESS;
100 }
101 
102 const 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 
119 const 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 
141 const 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 {
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
199  return CP::CorrectionCode::Ok;
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
214  return CP::CorrectionCode::Ok;
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 
239 const 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 }
AsgConfigHelper::HelperString
std::vector< std::string > HelperString(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:122
ElectronPhotonVariableCorrectionTool::m_unconvertedPhotonTools
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_unconvertedPhotonTools
The base class instances for single variable correction - unconverted photons.
Definition: ElectronPhotonVariableCorrectionTool.h:97
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
taskman.configFile
configFile
Definition: taskman.py:311
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
StateLessPT_NewConfig.Format
Format
Definition: StateLessPT_NewConfig.py:146
ElectronPhotonVariableCorrectionTool::getCorrectionVariableName
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,...
Definition: ElectronPhotonVariableCorrectionTool.cxx:239
ElectronPhotonVariableCorrectionTool::correctedCopy
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.
Definition: ElectronPhotonVariableCorrectionTool.cxx:221
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
AsgEGammaConfigHelper.h
ElectronPhotonVariableCorrectionTool::m_electronTools
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_electronTools
The base class instances for single variable correction - electrons.
Definition: ElectronPhotonVariableCorrectionTool.h:99
ElectronPhotonVariableCorrectionBase::EGammaObjects::allElectrons
@ allElectrons
ElectronPhotonVariableCorrectionTool::m_convertedPhotonConfFiles
std::vector< std::string > m_convertedPhotonConfFiles
The configuration files for the base class - converted photons.
Definition: ElectronPhotonVariableCorrectionTool.h:89
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
ElectronPhotonVariableCorrectionTool::m_electronConfFiles
std::vector< std::string > m_electronConfFiles
The configuration files for the base class - electrons.
Definition: ElectronPhotonVariableCorrectionTool.h:93
ElectronPhotonVariableCorrectionTool::applyToFlagMatchesToolHolder
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...
Definition: ElectronPhotonVariableCorrectionTool.cxx:141
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
ElectronPhotonVariableCorrectionTool::findAllConfigFiles
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 ...
Definition: ElectronPhotonVariableCorrectionTool.cxx:102
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
ElectronPhotonVariableCorrectionTool::m_unconvertedPhotonConfFiles
std::vector< std::string > m_unconvertedPhotonConfFiles
The configuration files for the base class - unconverted photons.
Definition: ElectronPhotonVariableCorrectionTool.h:91
ElectronPhotonVariableCorrectionBase::EGammaObjects::convertedPhotons
@ convertedPhotons
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
ANA_MSG_INFO
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:290
ElectronPhotonVariableCorrectionBase::EGammaObjects
EGammaObjects
Define the categories of EGamma objects tool can be applied to.
Definition: ElectronPhotonVariableCorrectionBase.h:99
python.selection.variable
variable
Definition: selection.py:33
ElectronPhotonVariableCorrectionTool::applyCorrection
virtual const CP::CorrectionCode applyCorrection(xAOD::Photon &photon) const override
Apply the correction given in the conf file to the passed photon.
Definition: ElectronPhotonVariableCorrectionTool.cxx:166
ElectronPhotonVariableCorrectionTool::initializeCorrectionTools
const StatusCode initializeCorrectionTools()
Configure, initialize, and check the base class instances saved in the m_*Tools members.
Definition: ElectronPhotonVariableCorrectionTool.cxx:87
ElectronPhotonVariableCorrectionTool::m_configFile
std::string m_configFile
The configuration file for the tool.
Definition: ElectronPhotonVariableCorrectionTool.h:87
PathResolver.h
python.hypoToolDisplay.toolname
def toolname(tool)
Definition: hypoToolDisplay.py:13
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
xAOD::Electron_v1
Definition: Electron_v1.h:34
ElectronPhotonVariableCorrectionTool::ElectronPhotonVariableCorrectionTool
ElectronPhotonVariableCorrectionTool(const std::string &myname)
Declare the interface that the class provides.
Definition: ElectronPhotonVariableCorrectionTool.cxx:21
xAOD::Photon
Photon_v1 Photon
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Photon.h:17
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:199
xAOD::Photon_v1
Definition: Photon_v1.h:37
ElectronPhotonVariableCorrectionTool.h
ElectronPhotonVariableCorrectionTool::m_convertedPhotonTools
std::vector< std::unique_ptr< ElectronPhotonVariableCorrectionBase > > m_convertedPhotonTools
The base class instances for single variable correction - converted photons.
Definition: ElectronPhotonVariableCorrectionTool.h:95
ElectronPhotonVariableCorrectionTool::initialize
virtual StatusCode initialize() override
Initialize the tool instance.
Definition: ElectronPhotonVariableCorrectionTool.cxx:28
CP::CorrectionCode
Return value from object correction CP tools.
Definition: CorrectionCode.h:31
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
ElectronPhotonVariableCorrectionBase::EGammaObjects::allEGammaObjects
@ allEGammaObjects
xAOD::Egamma_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: Egamma_v1.cxx:65
xAOD::Egamma_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition: Egamma_v1.cxx:70
python.DataFormatRates.env
env
Definition: DataFormatRates.py:32
ElectronPhotonVariableCorrectionTool::initializeTools
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.
Definition: ElectronPhotonVariableCorrectionTool.cxx:119
ElectronPhotonVariableCorrectionBase::EGammaObjects::unconvertedPhotons
@ unconvertedPhotons
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288