ATLAS Offline Software
Loading...
Searching...
No Matches
TestxAODPhotonAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// System include(s):
6#include <memory>
7#include <cstdlib>
8
9// ROOT include(s):
10#include <TFile.h>
11#include <TError.h>
12#include <TString.h>
13
14// Infrastructure include(s):
15#ifdef ROOTCORE
16# include "xAODRootAccess/Init.h"
19#endif // ROOTCORE
20
21// EDM include(s):
23#include "xAODEgamma/Photon.h"
27
30// To disable sending data
32
33#include <iostream>
34#include <string>
35
36namespace asg {
37ANA_MSG_HEADER(TestxAODPhotonAlg)
38ANA_MSG_SOURCE(TestxAODPhotonAlg, "")
39}
40
41int main( int argc, char* argv[] ) {
42
44 // The application's name:
45 const char* APP_NAME = argv[ 0 ];
46
47 using namespace asg::TestxAODPhotonAlg;
49 MSG::Level mylevel = MSG::INFO;
50 setMsgLevel(mylevel);
51 msg().setName(APP_NAME);
52
53 // Check if we received a file name:
54 if( argc < 2 ) {
55 Error( APP_NAME, "No file name received!" );
56 Error( APP_NAME, " Usage: %s [xAOD file name]", APP_NAME );
57 return 1;
58 }
59
60 // Initialise the application:
62
63 // Open the input file:
64 const TString fileName = argv[ 1 ];
65 Info( APP_NAME, "Opening file: %s", fileName.Data() );
66 std::unique_ptr< TFile > ifile( TFile::Open( fileName, "READ" ) );
67 ANA_CHECK( ifile.get() );
68
69 // Create a TEvent object:
70// xAOD::TEvent event( xAOD::TEvent::kBranchAccess ); //will work for a sample produced in devval
72 ANA_CHECK( event.readFrom( ifile.get() ) );
73 Info( APP_NAME, "Number of events in the file: %i",
74 static_cast< int >( event.getEntries() ) );
75
76
77 // Decide how many events to run over:
78 Long64_t entries = event.getEntries();
79 if( argc > 2 ) {
80 const Long64_t e = atoll( argv[ 2 ] );
81 if( e < entries ) {
82 entries = e;
83 }
84 }
85
86 // Initialize photonFS tool
87 AsgPhotonEfficiencyCorrectionTool photonSF_ID("AsgPhotonEfficiencyCorrectionTool_idSF");
88 AsgPhotonEfficiencyCorrectionTool photonSF_Iso("AsgPhotonEfficiencyCorrectionTool_isoSF");
89 AsgPhotonEfficiencyCorrectionTool photonSF_Trig("AsgPhotonEfficiencyCorrectionTool_TrigSF");
90
91 photonSF_ID.msg().setLevel( mylevel );
92 photonSF_Iso.msg().setLevel( mylevel );
93 photonSF_Trig.msg().setLevel( mylevel );
94
95
96 //Set Properties for photonID_SF tool
97 ANA_CHECK(photonSF_ID.setProperty("ForceDataType",1));
98
99 //Set Properties for photonISO_SF tool
100 ANA_CHECK(photonSF_Iso.setProperty("IsoKey","Loose")); // Set isolation WP: Loose,Tight,TightCaloOnly
101 ANA_CHECK(photonSF_Iso.setProperty("ForceDataType",1)); //set data type: 1 for FULLSIM, 3 for AF2
102
103 //Set Properties for PhotonTrig_SF tool
104 ANA_CHECK(photonSF_Trig.setProperty("IsoKey","Loose")); // Set isolation WP: Loose,Tight,TightCaloOnly
105 ANA_CHECK(photonSF_Trig.setProperty("TriggerKey","DI_PH_2015_2016_g25_loose_2017_2018_g50_loose_L1EM20VH")); // Set photon trigger
106 ANA_CHECK(photonSF_Trig.setProperty("ForceDataType",1)); //set data type: 1 for FULLSIM, 3 for AF2
107
108 // If the Pileup reweighting tool is not initialized, one can use next properties:
109 ANA_CHECK(photonSF_ID.setProperty("UseRandomRunNumber",false));
110 ANA_CHECK(photonSF_ID.setProperty("DefaultRandomRunNumber",428648)); // first runnumber of physics in run-3
111 ANA_CHECK(photonSF_Iso.setProperty("UseRandomRunNumber",false));
112 ANA_CHECK(photonSF_Iso.setProperty("DefaultRandomRunNumber",428648)); // first runnumber of physics in run-3
113 ANA_CHECK(photonSF_Trig.setProperty("UseRandomRunNumber",false));
114 ANA_CHECK(photonSF_Trig.setProperty("DefaultRandomRunNumber",349534)); // first runnumber of 2018 data taking
115
116 if(!photonSF_ID.initialize()){
117 std::cout <<"Failed to initialize the tool, check for errors"<<std::endl;
118 return 1;
119 }
120 if(!photonSF_Iso.initialize()){
121 std::cout <<"Failed to initialize the tool, check for errors"<<std::endl;
122 return 1;
123 }
124 if(!photonSF_Trig.initialize()){
125 std::cout <<"Failed to initialize the tool, check for errors"<<std::endl;
126 return 1;
127 }
128
129 // Test that recommended systematics properly bieng registered:
130 std::vector<CP::SystematicSet> sysList;
132 const CP::SystematicSet& recommendedSystematics = registry.recommendedSystematics();
133 sysList = CP::make_systematics_vector(recommendedSystematics); // replaces continuous systematics with the +/-1 sigma variation
134 std::cout << "List of recommended systematics from the registry:"<<std::endl;
135 for (auto sysListItr = recommendedSystematics.begin(); sysListItr != recommendedSystematics.end(); ++sysListItr){
136 std::cout <<(*sysListItr).name()<<std::endl;
137 }
138
139 // restructure all recommended systematic variations for the SF tool
140 // for +/- nsigma variation see
141 // https://twiki.cern.ch/twiki/bin/view/AtlasProtected/PhotonEfficiencyRun2#Systematic_variations
142
143 std::cout << "restructure all recommended systematic variations for the SF tool"<<std::endl;
144 std::vector<CP::SystematicSet> syst_PhotonID, syst_PhotonIso, syst_PhotonTrig;
145 for (const auto& SystematicsVariation : CP::make_systematics_vector(photonSF_ID.recommendedSystematics()))
146 {
147 syst_PhotonID.emplace_back();
148 syst_PhotonID.back().insert(SystematicsVariation);
149 }
150 for (const auto& SystematicsVariation : CP::make_systematics_vector(photonSF_Iso.recommendedSystematics()))
151 {
152 syst_PhotonIso.emplace_back();
153 syst_PhotonIso.back().insert(SystematicsVariation);
154 }
155 for (const auto& SystematicsVariation : CP::make_systematics_vector(photonSF_Trig.recommendedSystematics()))
156 {
157 syst_PhotonTrig.emplace_back();
158 syst_PhotonTrig.back().insert(SystematicsVariation);
159 }
160 //Print all recomended systemtaics
161 for (const auto& sSystematicSet: syst_PhotonID){
162 Info(APP_NAME,"PhotonEfficiencyCorrectionTool ID instance has next systematic variation %s ",sSystematicSet.name().c_str());
163 }
164 for (const auto& sSystematicSet: syst_PhotonIso){
165 Info(APP_NAME,"PhotonEfficiencyCorrectionTool Iso instance has next systematic variation %s ",sSystematicSet.name().c_str());
166 }
167 for (const auto& sSystematicSet: syst_PhotonTrig){
168 Info(APP_NAME,"PhotonEfficiencyCorrectionTool Iso instance has next systematic variation %s ",sSystematicSet.name().c_str());
169 }
170
171 double efficiencyScaleFactor=0, efficiencyScaleFactorError=0;
172 // Loop over the events:
173 std::cout << "loop on " << entries << " entries"<<std::endl;
174 for( int entry = 0; entry < entries; ++entry ) {
175
176 // Tell the object which entry to look at:
177 event.getEntry( entry );
178
179 // Get the Photon container from the event:
180 const xAOD::PhotonContainer *photons = nullptr;
181 ANA_CHECK( event.retrieve( photons, "Photons" ) );
182
183 //Clone
184 std::pair< xAOD::PhotonContainer*, xAOD::ShallowAuxContainer* > photons_shallowCopy = xAOD::shallowCopyContainer( *photons );
185
186 //Iterate over the shallow copy
187 xAOD::PhotonContainer* phsCorr = photons_shallowCopy.first;
188 xAOD::PhotonContainer::iterator ph_itr = phsCorr->begin();
189 xAOD::PhotonContainer::iterator ph_end = phsCorr->end();
190
191 static const SG::ConstAccessor<float> sfAcc("SF");
192
193 unsigned int i = 0;
194 for( ; ph_itr != ph_end; ++ph_itr, ++i ) {
195 xAOD::Photon* ph = *ph_itr;
196
197 // skip photons with pt outsize the acceptance
198 if(ph->pt()<10000.0) continue;
199 const xAOD::CaloCluster* cluster = ph->caloCluster();
200 if (!cluster){
201 ATH_MSG_ERROR("No cluster associated to the Photon \n");
202 return 1;
203 }
204 if( std::abs(cluster->etaBE(2))>2.37) continue;
205 Info (APP_NAME,"Event #%d, Photon #%d", entry, i);
206 Info (APP_NAME,"xAOD/raw pt = %f, eta = %f ", ph->pt(), ph->eta() );
207
208 // will set the systematic variation to nominal - no need to do it if applySystematicVariation("UP/DOWN") is not called
209 ANA_CHECK(photonSF_ID.applySystematicVariation(syst_PhotonID.at(0)));
210 ANA_CHECK(photonSF_Iso.applySystematicVariation(syst_PhotonIso.at(0)));
211
212 // Get photon ID SF and the uncertainty
213 ANA_CHECK(photonSF_ID.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
214 ANA_CHECK(photonSF_ID.getEfficiencyScaleFactorError(*ph,efficiencyScaleFactorError));
215 ANA_CHECK(photonSF_Trig.applySystematicVariation(syst_PhotonTrig.at(0)));
216
217 Info( APP_NAME,"===>>> Result ID: ScaleFactor %f, TotalUncertainty %f ",efficiencyScaleFactor,efficiencyScaleFactorError);
218
219 // Get photon SF and the uncertainty
220 ANA_CHECK(photonSF_Iso.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
221 ANA_CHECK(photonSF_Iso.getEfficiencyScaleFactorError(*ph,efficiencyScaleFactorError));
222
223 Info( APP_NAME,"===>>> Result Iso: ScaleFactor %f, TotalUncertainty %f ",efficiencyScaleFactor,efficiencyScaleFactorError);
224
225 // Get photon trigger SF and the uncertainty
226 ANA_CHECK(photonSF_Trig.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
227 ANA_CHECK(photonSF_Trig.getEfficiencyScaleFactorError(*ph,efficiencyScaleFactorError));
228
229 Info( APP_NAME,"===>>> Result Trigger: ScaleFactor %f, TotalUncertainty %f ",efficiencyScaleFactor,efficiencyScaleFactorError);
230
231 // decorate photon (for different name use photonSF_ID.setProperty("ResultName","ID_"); or photonSF_Iso.setProperty("ResultName","Iso_");)
232 ANA_CHECK(photonSF_ID.applyEfficiencyScaleFactor(*ph));
233 Info( "applyEfficiencyScaleFactor()","===>>> new decoration: \"SF\"=%f", sfAcc(*ph));
234 ANA_CHECK(photonSF_Iso.applyEfficiencyScaleFactor(*ph));
235 Info( "applyEfficiencyScaleFactor()","===>>> new decoration: \"SF\"=%f", sfAcc(*ph));
236 ANA_CHECK(photonSF_Trig.applyEfficiencyScaleFactor(*ph));
237 Info( "applyEfficiencyScaleFactor()","===>>> new decoration: \"SF\"=%f", sfAcc(*ph));
238
239 // get SF for all recommended systematic variations (nominal is also included):
240 for (const auto& sSystematicSet: syst_PhotonID){
241 ANA_CHECK(photonSF_ID.applySystematicVariation(sSystematicSet));
242 ANA_CHECK(photonSF_ID.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
243 Info( APP_NAME,"===>>> apply %s: ScaleFactor = %f",photonSF_ID.appliedSystematics().name().c_str(),efficiencyScaleFactor);
244 }
245 for (const auto& sSystematicSet: syst_PhotonIso){
246 ANA_CHECK(photonSF_Iso.applySystematicVariation(sSystematicSet));
247 ANA_CHECK(photonSF_Iso.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
248 Info( APP_NAME,"===>>> apply %s: ScaleFactor = %f",photonSF_Iso.appliedSystematics().name().c_str(),efficiencyScaleFactor);
249 }
250 for (const auto& sSystematicSet: syst_PhotonTrig){
251 ANA_CHECK(photonSF_Trig.applySystematicVariation(sSystematicSet));
252 ANA_CHECK(photonSF_Trig.getEfficiencyScaleFactor(*ph,efficiencyScaleFactor));
253 Info( APP_NAME,"===>>> apply %s: ScaleFactor = %f",photonSF_Trig.appliedSystematics().name().c_str(),efficiencyScaleFactor);
254 }
255
256 } // END LOOP ON PHOTONS
257
258 } // END LOOP ON EVENTS
259
260 // Return gracefully:
261 return 0;
262
263} // END PROGRAM
264
265/*
266 if(argc!=2){
267 printf("input parameters:\nTestxAODPhotonTool [path]\n");
268 printf("example: TestxAODPhotonTool /afs/cern.ch/work/k/krasznaa/public/xAOD/19.0.X_rel_4/mc12_8TeV.105200.McAtNloJimmy_CT10_ttbar_LeptonFilter.AOD.19.0.X_rel_4.pool.root\n");
269 return 0;
270 }
271*/
#define ATH_MSG_ERROR(x)
#define APP_NAME
macros for messaging and checking status codes
#define ANA_CHECK(EXP)
check whether the given expression was successful
#define ANA_MSG_HEADER(NAME)
for standalone code this creates a new message category
#define ANA_MSG_SOURCE(NAME, TITLE)
the source code part of ANA_MSG_SOURCE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
virtual CP::CorrectionCode applyEfficiencyScaleFactor(xAOD::Egamma &inputObject) const override
Decorate the photon with its scale factor.
virtual StatusCode applySystematicVariation(const CP::SystematicSet &systConfig) override
Configure this tool for the given systematics.
virtual StatusCode initialize() override
Gaudi Service Interface method implementations.
virtual CP::SystematicSet recommendedSystematics() const override
returns: the list of all systematics this tool recommends to use
virtual CP::CorrectionCode getEfficiencyScaleFactor(const xAOD::Egamma &inputObject, double &efficiencyScaleFactor) const override
Add some method for now as a first step to move the tool to then new interface.
const CP::SystematicSet & appliedSystematics() const
returns: the currently applied systematics
virtual CP::CorrectionCode getEfficiencyScaleFactorError(const xAOD::Egamma &inputObject, double &efficiencyScaleFactorError) const override
Get the "photon scale factor error" as a return value.
MsgStream & msg() const
This module implements the central registry for handling systematic uncertainties with CP tools.
const SystematicSet & recommendedSystematics() const
returns: the recommended set of systematics
static SystematicRegistry & getInstance()
Get the singleton instance of the registry for the curren thread.
Class to wrap a set of SystematicVariations.
std::string name() const
returns: the systematics joined into a single string.
const_iterator end() const
description: const iterator to the end of the set
const_iterator begin() const
description: const iterator to the beginning of the set
DataModel_detail::iterator< DataVector > iterator
Definition DataVector.h:842
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
Helper class to provide constant type-safe access to aux data.
float etaBE(const unsigned layer) const
Get the eta in one layer of the EM Calo.
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
const xAOD::CaloCluster * caloCluster(size_t index=0) const
Pointer to the xAOD::CaloCluster/s that define the electron candidate.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
static void enableDataSubmission(::Bool_t value)
Function for turning data submission on/off.
int main()
Definition hello.cxx:18
double entries
Definition listroot.cxx:49
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition Init.cxx:31
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, const EventContext &ctx)
Function making a shallow copy of a constant container.
Photon_v1 Photon
Definition of the current "egamma version".
MsgStream & msg
Definition testRead.cxx:32