ATLAS Offline Software
TrigGlobEffCorrExample5b.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
8 /*
9  * Another example showing how to configure the tool for photon triggers.
10  * This time for a more complex configuration (asymmetric diphoton trigger);
11  * see also the explanations in the example 1 as the configuration for photon triggers
12  * is very similar to that for electron triggers.
13  */
14 
15 // ROOT include(s):
16 #include <TFile.h>
17 #include <TError.h>
18 
19 // Infrastructure include(s):
20 #ifdef XAOD_STANDALONE
21  #include "xAODRootAccess/Init.h"
22  #include "xAODRootAccess/TEvent.h"
23  #include "xAODRootAccess/TStore.h"
24 #else
26  #include "POOLRootAccess/TEvent.h"
27 #endif
28 
30 // EDM include(s):
31 #include "AsgTools/AnaToolHandle.h"
36 #include "PATCore/PATCoreEnums.h"
39 
40 // stdlib include(s):
41 #include <random>
42 #include <vector>
43 #include <array>
44 using std::vector;
45 using std::string;
46 
47 #define MSGSOURCE "Example 5b"
48 
49 ANA_MSG_HEADER(Test)
51 using namespace Test;
52 int main(int argc, char* argv[])
53 {
54  ANA_CHECK_SET_TYPE(bool)
55  const char* filename = nullptr;
56  bool debug = false, cmdline_error = false, toys = false;
57  for(int i=1;i<argc;++i)
58  {
59  if(string(argv[i]) == "--debug") debug = true;
60  else if(string(argv[i]) == "--toys") toys = true;
61  else if(!filename && *argv[i]!='-') filename = argv[i];
62  else cmdline_error = true;
63  }
64  if(!filename || cmdline_error)
65  {
66  Error(MSGSOURCE, "No file name received!");
67  Error(MSGSOURCE, " Usage: %s [--debug] [--toys] [DxAOD file name]", argv[0]);
68  return 1;
69  }
70  #ifdef XAOD_STANDALONE
71  xAOD::Init(MSGSOURCE).ignore();
72  TFile* file = TFile::Open(filename, "READ");
73  if(!file)
74  {
75  Error(MSGSOURCE, "Unable to open file!");
76  return 2;
77  }
80  StatusCode::enableFailure();
81  #else
82  IAppMgrUI* app = POOL::Init();
84  TString file(filename);
85  #endif
86  event.readFrom(file).ignore();
87  Long64_t entries = event.getEntries();
88  Info(MSGSOURCE, "Number of events in the file: %lli", entries);
89 
90  /* ********************************************************************** */
91 
92  Info(MSGSOURCE, "Configuring the photon CP tools");
94  ToolHandleArray<IAsgPhotonEfficiencyCorrectionTool> photonEffTools;
96  ToolHandleArray<IAsgPhotonEfficiencyCorrectionTool> photonSFTools;
98  std::map<string,string> legsPerTool;
99 
101  vector<asg::AnaToolHandle<IAsgPhotonEfficiencyCorrectionTool>> factory;
102  enum{ cLEGS, cKEY };
103  vector<std::array<string,2>> toolConfigs = {
105  {"g35_loose, g35_medium_L1EM20VH", "DI_PH_2015_g35_loose_2016_g35_loose_2017_g35_medium_L1EM20VH_2018_g35_medium_L1EM20VH"},
106  {"g25_loose, g25_medium_L1EM20VH", "DI_PH_2015_g25_loose_2016_g25_loose_2017_g25_medium_L1EM20VH_2018_g25_medium_L1EM20VH"}
107  };
108 
109  const char* mapPath = "PhotonEfficiencyCorrection/2015_2018/"
110  "rel21.2/Summer2018_Rec_v1/map1.txt";
111  for(auto& cfg : toolConfigs)
112  for(int j=0;j<2;++j)
113  {
114  string name = "AsgPhotonEfficiencyCorrectionTool/"
115  + ((j? "PhTrigEff_" : "PhTrigSF_")
116  + std::to_string(factory.size()/2));
117  auto t = factory.emplace(factory.end(), name);
118  t->setProperty("MapFilePath", mapPath).ignore();
119  t->setProperty("TriggerKey", string(j?"":"Eff_") + cfg[cKEY]).ignore();
120  t->setProperty("IsoKey", "Loose").ignore();
121  t->setProperty("ForceDataType", (int)PATCore::ParticleDataType::Full).ignore();
122  if(t->initialize() != StatusCode::SUCCESS)
123  {
124  Error(MSGSOURCE, "Unable to initialize the photon CP tool <%s>!",
125  t->name().c_str());
126  return 3;
127  }
128  auto& handles = (j? photonSFTools : photonEffTools);
129  handles.push_back(t->getHandle());
132  name = handles[handles.size()-1].name();
133  legsPerTool[name] = cfg[cLEGS];
134  }
135 
136  /* ********************************************************************** */
137 
138  Info(MSGSOURCE, "Configuring the global trigger SF tool");
139  asg::AnaToolHandle<ITrigGlobalEfficiencyCorrectionTool> myTool("TrigGlobalEfficiencyCorrectionTool/TrigGlobal");
140  myTool.setProperty("PhotonEfficiencyTools", photonEffTools).ignore();
141  myTool.setProperty("PhotonScaleFactorTools", photonSFTools).ignore();
142  myTool.setProperty("TriggerCombination2015", "g35_loose_g25_loose").ignore();
143  myTool.setProperty("TriggerCombination2016", "g35_loose_g25_loose").ignore();
144  myTool.setProperty("TriggerCombination2017", "g35_medium_g25_medium_L12EM20VH").ignore();
145  myTool.setProperty("TriggerCombination2018", "g35_medium_g25_medium_L12EM20VH").ignore();
146  myTool.setProperty("ListOfLegsPerTool", legsPerTool).ignore();
147 
148  if(debug) myTool.setProperty("OutputLevel", MSG::DEBUG).ignore();
149  if(toys) myTool.setProperty("NumberOfToys", 1000).ignore();
150  if(myTool.initialize() != StatusCode::SUCCESS)
151  {
152  Error(MSGSOURCE, "Unable to initialize the TrigGlob tool!");
153  return 3;
154  }
155 
158  const unsigned periodRuns[] = {
160  276262, 278727, 279932, 280423, 281130, 282625,
162  297730, 300345, 301912, 302737, 303638, 303943, 305291, 307124,
163  305359, 309311, 310015,
165  325713, 329385, 330857, 332720, 334842, 336497, 336832, 338183,
167  348885, 349534, 350310, 352274, 354107, 354826, 355261, 355331,
168  355529, 357050, 359191
169  };
170  std::uniform_int_distribution<unsigned> uniformPdf(0,
171  sizeof(periodRuns)/sizeof(*periodRuns) - 1);
172  std::default_random_engine randomEngine;
173 
174  /* ********************************************************************** */
175 
176  Info(MSGSOURCE, "Starting the event loop");
177  unsigned errors = 0;
178  double nSuitableEvents = 0., sumW = 0.;
179  static const SG::Decorator<unsigned> RandomRunNumberDec("RandomRunNumber");
180  static const SG::ConstAccessor<int> truthTypeAcc("truthType");
181  for(Long64_t entry = 0; entry < entries; ++entry)
182  {
183  event.getEntry(entry);
184 
186  const xAOD::EventInfo* eventInfo = nullptr;
187  event.retrieve(eventInfo,"EventInfo").ignore();
188  unsigned runNumber = periodRuns[uniformPdf(randomEngine)];
189  RandomRunNumberDec(*eventInfo) = runNumber;
190 
191  vector<const xAOD::Photon*> myTriggeringPhotons;
192  const xAOD::PhotonContainer* photons = nullptr;
193  event.retrieve(photons,"Photons").ignore();
194  unsigned n36 = 0;
195  for(auto photon : *photons)
196  {
197  if(!photon->caloCluster()) continue;
198  float eta = fabs(photon->caloCluster()->etaBE(2));
199  float pt = photon->pt();
200  if(pt<10e3f || eta>=2.37 || (eta>1.37 && eta<1.52)) continue;
201  int t = truthTypeAcc(*photon);
202  if(t!=14) continue;
204  if(pt < 26e3f) continue;
205  myTriggeringPhotons.push_back(photon);
206  if(pt > 36e3f) ++n36;
207  }
208 
210  if(myTriggeringPhotons.size()<2 || n36<1) continue;
211 
213  double sf = 1.;
214  auto cc = myTool->getEfficiencyScaleFactor(myTriggeringPhotons, sf);
216  {
217  nSuitableEvents += 1;
218  sumW += sf;
219  }
220  else
221  {
222  Warning(MSGSOURCE, "Scale factor evaluation failed");
223  ++errors;
224  }
225  if(errors>10)
226  {
227  Error(MSGSOURCE, "Too many errors reported!");
228  break;
229  }
230  }
231  Info(MSGSOURCE, "Average scale factor: %f (over %ld events)",
232  sumW / nSuitableEvents, long(nSuitableEvents));
233  #ifndef XAOD_STANDALONE
234  ANA_CHECK(app->finalize())
235  #endif
236  return errors? 4 : 0;
237 }
asg::AnaToolHandle::initialize
StatusCode initialize()
initialize the tool
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:69
asg::AnaToolHandle< ITrigGlobalEfficiencyCorrectionTool >
ITrigGlobalEfficiencyCorrectionTool.h
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
test_pyathena.pt
pt
Definition: test_pyathena.py:11
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
SG::ConstAccessor< int >
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
IAsgPhotonEfficiencyCorrectionTool.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:97
asg::AnaToolHandle::setProperty
StatusCode setProperty(const std::string &property, const T2 &value)
set the given property of the tool.
POOL::Init
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:29
POOL::TEvent::kClassAccess
@ kClassAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:45
ANA_MSG_HEADER
#define ANA_MSG_HEADER(NAME)
for standalone code this creates a new message category
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:113
PATCore::ParticleDataType::Full
@ Full
Definition: PATCoreEnums.h:22
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:54
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
lumiFormat.i
int i
Definition: lumiFormat.py:92
MessageCheck.h
macros for messaging and checking status codes
PATCoreEnums.h
TEvent.h
MSGSOURCE
#define MSGSOURCE
Definition: TrigGlobEffCorrExample5b.cxx:47
file
TFile * file
Definition: tile_monitor.h:29
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AnaToolHandle.h
mergePhysValFiles.errors
list errors
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:43
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
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:39
TEvent.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
WriteCaloSwCorrections.cfg
cfg
Definition: WriteCaloSwCorrections.py:23
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
AthAnalysisHelper.h
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
ITrigGlobalEfficiencyCorrectionTool::getEfficiencyScaleFactor
virtual CP::CorrectionCode getEfficiencyScaleFactor(const std::vector< const xAOD::IParticle * > &particles, double &efficiencyScaleFactor)=0
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:199
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
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
DEBUG
#define DEBUG
Definition: page_access.h:11
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
entries
double entries
Definition: listroot.cxx:49
main
int main(int argc, char *argv[])
Definition: TrigGlobEffCorrExample5b.cxx:52
L1Topo::Error
Error
The different types of error that can be flagged in the L1TopoRDO.
Definition: Error.h:16
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
Decorator.h
Helper class to provide type-safe access to aux data.
PhotonContainer.h
TStore.h
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:81
python.handimod.cc
int cc
Definition: handimod.py:523
xAOD::Init
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition: Init.cxx:31