ATLAS Offline Software
testElectronPhotonVariableCorrectionBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
11 // ROOT includes
12 #include "TFile.h"
13 #include "TString.h"
14 
15 //EDM includes
19 #include "xAODEgamma/Electron.h"
20 #include "xAODEgamma/Photon.h"
21 
23 //
25 
27 #ifdef ROOTCORE
28 #include "xAODRootAccess/TEvent.h"
29 //#include "xAODRootAccess/TStore.h"
30 #endif //ROOTCORE
31 
32 //main test code
33 
34 int main (int argc, char* argv[])
35 {
36  using namespace asg::msgUserCode;
37  // The application's name:
38  const char* APP_NAME = argv[0];
39 
40  // Check if we received a file name:
41  if (argc < 2)
42  {
43  ANA_MSG_ERROR("No file name received!" );
44  ANA_MSG_ERROR( " Usage: %s [xAOD file name] %d Num of events to process %d (0 photons , 1 electrons)");
45  return EXIT_FAILURE;
46  }
47 
48  // Check if we want to process Electron/Photon shifts
49  bool isPhoton = false;
50  bool isElectron = false;
51  if (argc < 4)
52  {
53  ANA_MSG_INFO(APP_NAME << " By default looking at Photon" );
54  isPhoton=true;
55  }
56  else
57  {
58  int argv1 = atoi(argv[3]);
59  if (argv1 == 0)
60  {
61  isPhoton = true;
62  ANA_MSG_INFO (APP_NAME << " We are looking at photon shifts" );
63  }
64  else if (argv1 == 1)
65  {
66  isElectron = true;
67  ANA_MSG_INFO(APP_NAME << " We are looking at electron shifts");
68  }
69  else
70  {
71  ANA_MSG_ERROR ("Usage: %s [xAOD file name] %d Num of events to process %d (0 photons , 1 electrons)");
72  return EXIT_FAILURE;
73  }
74  }
75 
76  //open input file
77  const TString fileName = argv[1];
78  ANA_MSG_INFO(APP_NAME << " Opening file: " << fileName.Data());
79  std::unique_ptr<TFile> inputFile(TFile::Open(fileName, "READ")); //probably rather "READ"?
80  ANA_CHECK(inputFile.get());
81 
82  // Create a TEvent object (persistent store)
84  // Create a TStore object (transient store)
85  xAOD::TStore trans;
86  ANA_CHECK(pers.readFrom(inputFile.get()));
87  //
88  ANA_MSG_INFO("Number of events in the file: " << pers.getEntries());
89 
90  // Decide how many events to run over:
91  Long64_t entries = pers.getEntries();
92  if (argc > 2)
93  {
94  const Long64_t userInputEntries = atoll(argv[2]);
95  if (userInputEntries < entries)
96  {
97  entries = userInputEntries;
98  ANA_MSG_INFO("Running over " << userInputEntries << " events.");
99  }
100  }
101 
102  // ===============================================
103  // Photon test
104  // ===============================================
105  if (isPhoton)
106  {
107  //initialise the tool
108  //converted photons
109  std::string configFilePathConverted = "EGammaVariableCorrection/ElectronPhotonVariableCorrectionBase_ExampleConvertedPhotonConf_Eratio.conf";
110  ElectronPhotonVariableCorrectionBase CorrectConvertedPhotonTool("CorrectConvertedPhotonTool");
111  ANA_CHECK(CorrectConvertedPhotonTool.setProperty("ConfigFile",configFilePathConverted));
112  ANA_CHECK(CorrectConvertedPhotonTool.initialize());
113  //unconverted photons
114  std::string configFilePathUnconverted = "EGammaVariableCorrection/ElectronPhotonVariableCorrectionBase_ExampleUnconvertedPhotonConf_Eratio.conf";
115  ElectronPhotonVariableCorrectionBase CorrectUnconvertedPhotonTool("CorrectUnconvertedPhotonTool");
116  ANA_CHECK(CorrectUnconvertedPhotonTool.setProperty("ConfigFile",configFilePathUnconverted));
117  ANA_CHECK(CorrectUnconvertedPhotonTool.initialize());
118 
119  std::string correctionVariable = CorrectConvertedPhotonTool.getCorrectionVariable();
120  ANA_MSG_INFO("Correcting Variable: " << correctionVariable);
121 
122  //loop over the events
123  for (Long64_t entry = 0; entry < entries; entry++)
124  {
125  //get entry
126  pers.getEntry(entry);
127  ANA_MSG_INFO("============================");
128  ANA_MSG_INFO("Event: " << entry);
129 
130  //get photon container
131  const xAOD::PhotonContainer* photons;
132  ANA_CHECK(pers.retrieve(photons, "Photons"));
133 
134  // Make a deep copy of the photon container
135  // Create the new container and its auxiliary store.
136  auto photons_copy = std::make_unique<xAOD::PhotonContainer>();
137  auto photons_copy_aux = std::make_unique<xAOD::AuxContainerBase>();
138  photons_copy->setStore (photons_copy_aux.get()); //< Connect the two
139 
140  //copy photons over
141  for (auto photon : *photons) {
142  // Copy this photon to the output container:
143  xAOD::Photon* photon_copy = new xAOD::Photon();
144  photons_copy->push_back (photon_copy); // photon acquires the photon_copy auxstore
145  *photon_copy = *photon; // copies auxdata from one auxstore to the other
146  }
147  // end make deep copy
148 
149  //loop over deep copy of photon container
150  for (unsigned int photon_itr = 0; photon_itr < photons_copy->size(); photon_itr++)
151  {
152  ANA_MSG_INFO("---------------------------");
153  ANA_MSG_INFO("Photon: " << photon_itr);
154  xAOD::Photon* photon = photons_copy->at(photon_itr);
155 
156  //apply correction
158  {
159  ANA_MSG_INFO("Converted Photon.");
160  ANA_CHECK(CorrectConvertedPhotonTool.applyCorrection(*photon));
161  }
162  else
163  {
164  ANA_MSG_INFO("Unconverted Photon.");
165  ANA_CHECK(CorrectUnconvertedPhotonTool.applyCorrection(*photon));
166  }
167 
168  //get original and corrected value
169  SG::AuxElement::Accessor<float> VariableToCorrect(correctionVariable + "_original");
170  SG::AuxElement::Accessor<float> CorrectedVariable(correctionVariable);
171 
172  //print results
173  ANA_MSG_INFO("Original value: " << VariableToCorrect(*photon));
174  ANA_MSG_INFO("Corrected value: " << CorrectedVariable(*photon));
175 
176  } // loop over deep copy of photon container
177  } // loop over events
178  } // if isPhoton
179 
180  // ===============================================
181  // Electron test
182  // ===============================================
183  if (isElectron)
184  {
185  std::string configFilePath = "EGammaVariableCorrection/ElectronPhotonVariableCorrectionBase_ExampleElectronConf_Eratio.conf";
186  ElectronPhotonVariableCorrectionBase CorrectElectronTool("CorrectElectronTool");
187  ANA_CHECK(CorrectElectronTool.setProperty("ConfigFile",configFilePath));
188  ANA_CHECK(CorrectElectronTool.initialize());
189 
190  std::string correctionVariable = CorrectElectronTool.getCorrectionVariable();
191  ANA_MSG_INFO("Correcting Variable: " << correctionVariable);
192 
193  //loop over the events
194  for (Long64_t entry = 0; entry < entries; entry++)
195  {
196  //get entry
197  pers.getEntry(entry);
198  ANA_MSG_INFO("============================");
199  ANA_MSG_INFO("Event: " << entry);
200 
201  //get electron container
203  ANA_CHECK(pers.retrieve(electrons, "Electrons"));
204 
205  // Make a deep copy of the electron container
206  // Create the new container and its auxiliary store.
207  auto electrons_copy = std::make_unique<xAOD::ElectronContainer>();
208  auto electrons_copy_aux = std::make_unique<xAOD::AuxContainerBase>();
209  electrons_copy->setStore (electrons_copy_aux.get()); //< Connect the two
210 
211  //copy electrons over
212  for (auto electron : *electrons) {
213  // Copy this electron to the output container:
214  xAOD::Electron* electron_copy = new xAOD::Electron();
215  electrons_copy->push_back (electron_copy); // electron acquires the electron_copy auxstore
216  *electron_copy = *electron; // copies auxdata from one auxstore to the other
217  }
218  // end make deep copy
219 
220  //loop over deep copy of electron container
221  for (unsigned int electron_itr = 0; electron_itr < electrons_copy->size(); electron_itr++)
222  {
223  ANA_MSG_INFO("---------------------------");
224  ANA_MSG_INFO("Electron: " << electron_itr);
225  xAOD::Electron* electron = electrons_copy->at(electron_itr);
226 
227  //apply correction
228  ANA_CHECK(CorrectElectronTool.applyCorrection(*electron));
229 
230  //get original and corrected value
231  SG::AuxElement::Accessor<float> VariableToCorrect(correctionVariable + "_original");
232  SG::AuxElement::Accessor<float> CorrectedVariable(correctionVariable);
233 
234  //print results
235  ANA_MSG_INFO("Original value: " << VariableToCorrect(*electron));
236  ANA_MSG_INFO("Corrected value: " << CorrectedVariable(*electron));
237 
238  } // loop over deep copy of electron container
239  } // loop over events
240  } // if isElectron
241 
242  return 0;
243 }
ElectronPhotonVariableCorrectionBase::initialize
virtual StatusCode initialize() override
Initialize the class instance.
Definition: ElectronPhotonVariableCorrectionBase.cxx:47
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
ElectronPhotonVariableCorrectionBase::applyCorrection
const CP::CorrectionCode applyCorrection(xAOD::Photon &photon) const
Apply the correction given in the conf file to the passed photon.
Definition: ElectronPhotonVariableCorrectionBase.cxx:202
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
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
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:100
AsgMessaging.h
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
xAOD::EgammaHelpers::isConvertedPhoton
bool isConvertedPhoton(const xAOD::Egamma *eg, bool excludeTRT=false)
is the object a converted photon
Definition: EgammaxAODHelpers.cxx:25
ElectronContainer.h
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
Photon.h
xAOD::TEvent::getEntries
::Long64_t getEntries() const
Get how many entries are available from the current input file(s)
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1294
xAOD::TEvent::getEntry
::Int_t getEntry(::Long64_t entry, ::Int_t getall=0)
Function loading a given entry of the input TTree.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1325
xAOD::EgammaHelpers::isElectron
bool isElectron(const xAOD::Egamma *eg)
is the object an electron (not Fwd)
Definition: EgammaxAODHelpers.cxx:12
ANA_MSG_INFO
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:290
APP_NAME
#define APP_NAME
Definition: BoostedXbbTag.cxx:23
TEvent.h
ElectronPhotonVariableCorrectionBase
Class to correct electron and photon MC variables.
Definition: ElectronPhotonVariableCorrectionBase.h:42
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
xAOD::Electron_v1
Definition: Electron_v1.h:34
xAOD::Photon
Photon_v1 Photon
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Photon.h:17
main
int main(int argc, char *argv[])
Test if ElectronPhotonVariableCorrectionBase runs fine on electrons and (un-)converted photons.
Definition: testElectronPhotonVariableCorrectionBase.cxx:34
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
PhotonAuxContainer.h
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:199
ElectronPhotonVariableCorrectionBase::getCorrectionVariable
const std::string & getCorrectionVariable()
Returns the variable which should be corrected according to the passed configuration file.
Definition: ElectronPhotonVariableCorrectionBase.h:95
xAOD::Photon_v1
Definition: Photon_v1.h:37
xAOD::EgammaHelpers::isPhoton
bool isPhoton(const xAOD::Egamma *eg)
is the object a photon
Definition: EgammaxAODHelpers.cxx:21
xAOD::TEvent::retrieve
StatusCode retrieve(const T *&obj, const std::string &key)
Retrieve either an input or an output object from the event.
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
entries
double entries
Definition: listroot.cxx:49
xAOD::TEvent::readFrom
StatusCode readFrom(::TFile *file, Bool_t useTreeCache=kTRUE, const char *treeName=EVENT_TREE_NAME)
Connect the object to a new input file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:365
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
Electron.h
ElectronPhotonVariableCorrectionBase.h
PhotonContainer.h
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84