ATLAS Offline Software
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"
17 # include "xAODRootAccess/TEvent.h"
18 # include "xAODRootAccess/TStore.h"
19 #endif // ROOTCORE
20 
21 // EDM include(s):
23 #include "xAODEgamma/Photon.h"
25 #include "xAODCore/ShallowCopy.h"
27 
29 #include "AsgMessaging/MsgStream.h"
30 // To disable sending data
32 
33 #include <iostream>
34 #include <string>
35 
36 namespace asg {
37 ANA_MSG_HEADER(TestxAODPhotonAlg)
38 ANA_MSG_SOURCE(TestxAODPhotonAlg, "")
39 }
40 
41 int main( int argc, char* argv[] ) {
42 
44  // The application's name:
45  const char* APP_NAME = argv[ 0 ];
46 
47  using namespace asg::TestxAODPhotonAlg;
48  ANA_CHECK_SET_TYPE(int);
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 */
ShallowCopy.h
python.Dso.registry
registry
Definition: Control/AthenaServices/python/Dso.py:159
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
AsgPhotonEfficiencyCorrectionTool::getEfficiencyScaleFactorError
virtual CP::CorrectionCode getEfficiencyScaleFactorError(const xAOD::Egamma &inputObject, double &efficiencyScaleFactorError) const override
Get the "photon scale factor error" as a return value.
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:325
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
AsgPhotonEfficiencyCorrectionTool.h
xAOD::TFileAccessTracer::enableDataSubmission
static void enableDataSubmission(::Bool_t value)
Function for turning data submission on/off.
Definition: TFileAccessTracer.cxx:281
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::SystematicSet::name
std::string name() const
returns: the systematics joined into a single string.
Definition: SystematicSet.cxx:278
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
SG::ConstAccessor< float >
AsgPhotonEfficiencyCorrectionTool::applyEfficiencyScaleFactor
virtual CP::CorrectionCode applyEfficiencyScaleFactor(xAOD::Egamma &inputObject) const override
Decorate the photon with its scale factor.
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:338
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:100
AsgPhotonEfficiencyCorrectionTool::initialize
virtual StatusCode initialize() override
Gaudi Service Interface method implementations.
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:99
xAOD::CaloCluster_v1::etaBE
float etaBE(const unsigned layer) const
Get the eta in one layer of the EM Calo.
Definition: CaloCluster_v1.cxx:644
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
TFileAccessTracer.h
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
AsgPhotonEfficiencyCorrectionTool::getEfficiencyScaleFactor
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.
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:304
lumiFormat.i
int i
Definition: lumiFormat.py:85
AsgPhotonEfficiencyCorrectionTool::recommendedSystematics
virtual CP::SystematicSet recommendedSystematics() const override
returns: the list of all systematics this tool recommends to use
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:381
POOL::TEvent::getEntries
long getEntries()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:124
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
Photon.h
MessageCheck.h
macros for messaging and checking status codes
xAOD::Egamma_v1::caloCluster
const xAOD::CaloCluster * caloCluster(size_t index=0) const
Pointer to the xAOD::CaloCluster/s that define the electron candidate.
Definition: Egamma_v1.cxx:388
APP_NAME
#define APP_NAME
Definition: BoostedXbbTag.cxx:23
TEvent.h
CP::SystematicSet::end
const_iterator end() const
description: const iterator to the end of the set
Definition: SystematicSet.h:59
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
AsgPhotonEfficiencyCorrectionTool
Definition: AsgPhotonEfficiencyCorrectionTool.h:38
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
asg::ANA_MSG_HEADER
ANA_MSG_HEADER(msgSTT) ANA_MSG_SOURCE(msgSTT
AsgPhotonEfficiencyCorrectionTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const CP::SystematicSet &systConfig) override
Configure this tool for the given systematics.
Definition: AsgPhotonEfficiencyCorrectionTool.cxx:391
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
ANA_MSG_SOURCE
#define ANA_MSG_SOURCE(NAME, TITLE)
the source code part of ANA_MSG_SOURCE
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:133
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
AsgPhotonEfficiencyCorrectionTool::appliedSystematics
const CP::SystematicSet & appliedSystematics() const
returns: the currently applied systematics
Definition: AsgPhotonEfficiencyCorrectionTool.h:70
xAOD::Photon_v1
Definition: Photon_v1.h:37
ANA_CHECK_SET_TYPE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:314
CP::SystematicRegistry
This module implements the central registry for handling systematic uncertainties with CP tools.
Definition: SystematicRegistry.h:25
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:74
CP::SystematicSet::begin
const_iterator begin() const
description: const iterator to the beginning of the set
Definition: SystematicSet.h:55
AthCommonMsg::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
main
int main(int argc, char *argv[])
Definition: TestxAODPhotonAlg.cxx:41
entries
double entries
Definition: listroot.cxx:49
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
LArCellNtuple.ifile
string ifile
Definition: LArCellNtuple.py:133
PhotonContainer.h
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
TStore.h
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
CP::SystematicRegistry::getInstance
static SystematicRegistry & getInstance()
Get the singleton instance of the registry for the curren thread.
Definition: SystematicRegistry.cxx:25
xAOD::Init
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition: Init.cxx:31
MsgStream.h
SystematicsUtil.h