ATLAS Offline Software
Loading...
Searching...
No Matches
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
10
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
29//#include "xAODRootAccess/TStore.h"
30#endif //ROOTCORE
31
32//main test code
33
34int 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
202 const xAOD::ElectronContainer* electrons;
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}
bool isElectron(const T &p)
Definition AtlasPID.h:202
bool isPhoton(const T &p)
Definition AtlasPID.h:376
#define APP_NAME
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
Class to correct electron and photon MC variables.
const std::string & getCorrectionVariable()
Returns the variable which should be corrected according to the passed configuration file.
const CP::CorrectionCode applyCorrection(xAOD::Photon &photon) const
Apply the correction given in the conf file to the passed photon.
virtual StatusCode initialize() override
Initialize the class instance.
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
StatusCode retrieve(const T *&obj, const std::string &key)
Retrieve either an input or an output object from the event.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
::Long64_t getEntries() const
Get how many entries are available from the current input file(s)
StatusCode readFrom(::TFile *file, bool useTreeCache=true, std::string_view treeName=EVENT_TREE_NAME)
Connect the object to a new input file.
::Int_t getEntry(::Long64_t entry, ::Int_t getall=0)
Function loading a given entry of the input TTree.
A relatively simple transient store for objects created in analysis.
Definition TStore.h:45
int main()
Definition hello.cxx:18
double entries
Definition listroot.cxx:49
bool isConvertedPhoton(const xAOD::Egamma *eg, bool excludeTRT=false)
is the object a converted photon
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
Photon_v1 Photon
Definition of the current "egamma version".
Electron_v1 Electron
Definition of the current "egamma version".