ATLAS Offline Software
TrigGlobEffCorrExample0.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  * Simple example: single electron or single muon trigger, using different
10  * triggers for 2015 and 2016-2018.
11  *
12  */
13 
14 // ROOT include(s):
15 #include <TFile.h>
16 #include <TError.h>
17 
18 // Infrastructure include(s):
19 #ifdef XAOD_STANDALONE
20  #include "xAODRootAccess/Init.h"
21  #include "xAODRootAccess/TEvent.h"
22  #include "xAODRootAccess/TStore.h"
23 #else
25  #include "POOLRootAccess/TEvent.h"
26 #endif
28 // EDM include(s):
29 #include "AsgTools/AnaToolHandle.h"
35 #include "xAODMuon/MuonContainer.h"
36 #include "PATCore/PATCoreEnums.h"
40 
41 // stdlib include(s):
42 #include <random>
43 #include <vector>
44 #include <array>
45 using std::vector;
46 using std::string;
47 
48 #define MSGSOURCE "Example 0"
49 
50 ANA_MSG_HEADER(Test)
52 using namespace Test;
53 int main(int argc, char* argv[])
54 {
55  ANA_CHECK_SET_TYPE(bool)
56  const char* filename = nullptr;
57  bool debug = false, cmdline_error = false, toys = false;
58  for(int i=1;i<argc;++i)
59  {
60  if(string(argv[i]) == "--debug") debug = true;
61  else if(string(argv[i]) == "--toys") toys = true;
62  else if(!filename && *argv[i]!='-') filename = argv[i];
63  else cmdline_error = true;
64  }
65  if(!filename || cmdline_error)
66  {
67  Error(MSGSOURCE, "No file name received!");
68  Error(MSGSOURCE, " Usage: %s [--debug] [--toys] [DxAOD file name]", argv[0]);
69  return 1;
70  }
71  #ifdef XAOD_STANDALONE
72  xAOD::Init(MSGSOURCE).ignore();
73  TFile* file = TFile::Open(filename, "READ");
74  if(!file)
75  {
76  Error(MSGSOURCE, "Unable to open file!");
77  return 2;
78  }
81  StatusCode::enableFailure();
82  #else
83  IAppMgrUI* app = POOL::Init();
85  TString file(filename);
86  #endif
87  event.readFrom(file).ignore();
88  Long64_t entries = event.getEntries();
89  Info(MSGSOURCE, "Number of events in the file: %lli", entries);
90 
91  /* ********************************************************************** */
92 
93  Info(MSGSOURCE, "Configuring the electron CP tools");
95  ToolHandleArray<IAsgElectronEfficiencyCorrectionTool> electronEffTools;
97  ToolHandleArray<IAsgElectronEfficiencyCorrectionTool> electronSFTools;
99  vector<asg::AnaToolHandle<IAsgElectronEfficiencyCorrectionTool>> factory;
100  const char* mapPath = "ElectronEfficiencyCorrection/2015_2017/"
101  "rel21.2/Consolidation_September2018_v1/map1.txt";
102  for(int j=0;j<2;++j)
103  {
104  string name = "AsgElectronEfficiencyCorrectionTool/"
105  + ((j? "ElTrigEff_" : "ElTrigSF_")
106  + std::to_string(factory.size()/2));
107  auto t = factory.emplace(factory.end(), name);
108  t->setProperty("MapFilePath", mapPath).ignore();
109  t->setProperty("TriggerKey", string(j?"":"Eff_")
110  + "SINGLE_E_2015_e24_lhmedium_L1EM20VH_OR_e60_lhmedium_OR_e120_lhloose_2016_2018_e26_lhtight_nod0_ivarloose_OR_e60_lhmedium_nod0_OR_e140_lhloose_nod0").ignore();
111  t->setProperty("IdKey", "Tight").ignore();
112  t->setProperty("IsoKey", "FCTight").ignore();
113  t->setProperty("CorrelationModel", "TOTAL").ignore();
114  t->setProperty("ForceDataType", (int)PATCore::ParticleDataType::Full).ignore();
115  if(t->initialize() != StatusCode::SUCCESS)
116  {
117  Error(MSGSOURCE, "Unable to initialize the electron CP tool <%s>!",
118  t->name().c_str());
119  return 3;
120  }
121  auto& handles = (j? electronSFTools : electronEffTools);
122  handles.push_back(t->getHandle());
123  }
124 
125  /* ********************************************************************** */
126 
127  Info(MSGSOURCE, "Configuring the muon CP tools");
129  ToolHandleArray<CP::IMuonTriggerScaleFactors> muonTools;
130  asg::AnaToolHandle<CP::IMuonTriggerScaleFactors> muonTool("CP::MuonTriggerScaleFactors/MuonTrigEff");
131  muonTool.setProperty("MuonQuality", "Tight").ignore();
132  muonTool.setProperty("useRel207", false).ignore();
133  if(muonTool.initialize() != StatusCode::SUCCESS)
134  {
135  Error(MSGSOURCE, "Unable to initialize the muon CP tool!");
136  return 3;
137  }
138  muonTools.push_back(muonTool.getHandle());
139 
140  /* ********************************************************************** */
141 
142  Info(MSGSOURCE, "Configuring the global trigger SF tool");
143  asg::AnaToolHandle<ITrigGlobalEfficiencyCorrectionTool> myTool("TrigGlobalEfficiencyCorrectionTool/TrigGlobal");
144  myTool.setProperty("ElectronEfficiencyTools", electronEffTools).ignore();
145  myTool.setProperty("ElectronScaleFactorTools", electronSFTools).ignore();
146  myTool.setProperty("MuonTools", muonTools).ignore();
147  const char* triggers2015 =
148  "mu20_iloose_L1MU15_OR_mu50"
149  "|| e24_lhmedium_L1EM20VH_OR_e60_lhmedium_OR_e120_lhloose";
150  myTool.setProperty("TriggerCombination2015", triggers2015).ignore();
151  const char* triggers2016to2018 =
152  "mu26_ivarmedium_OR_mu50"
153  "|| e26_lhtight_nod0_ivarloose_OR_e60_lhmedium_nod0_OR_e140_lhloose_nod0";
154  myTool.setProperty("TriggerCombination2016", triggers2016to2018).ignore();
155  myTool.setProperty("TriggerCombination2017", triggers2016to2018).ignore();
156  myTool.setProperty("TriggerCombination2018", triggers2016to2018).ignore();
157 
158  if(debug) myTool.setProperty("OutputLevel", MSG::DEBUG).ignore();
159  if(toys) myTool.setProperty("NumberOfToys", 1000).ignore();
160  if(myTool.initialize() != StatusCode::SUCCESS)
161  {
162  Error(MSGSOURCE, "Unable to initialize the TrigGlob tool!");
163  return 3;
164  }
165 
168  const unsigned periodRuns[] = {
170  276073, 278727, 279932, 280423, 281130, 282625,
172  296939, 300345, 301912, 302737, 303638, 303943, 305291, 307124,
173  305359, 309311, 310015,
175  325713, 329385, 330857, 332720, 334842, 336497, 336832, 338183,
177  348885, 349534, 350310, 352274, 354107, 354826, 355261, 355331,
178  355529, 357050, 359191, 361635, 361738, 363664
179  };
180  std::uniform_int_distribution<unsigned> uniformPdf(0,
181  sizeof(periodRuns)/sizeof(*periodRuns) - 1);
182  std::default_random_engine randomEngine;
183 
184  static const SG::ConstAccessor<int> truthType("truthType");
185  static const SG::ConstAccessor<int> truthOrigin("truthOrigin");
186 
187  /* ********************************************************************** */
188 
189  Info(MSGSOURCE, "Starting the event loop");
190  unsigned errors = 0;
191  double nSuitableEvents = 0., sumW = 0.;
192  static const SG::Decorator<unsigned> RandomRunNumberDec("RandomRunNumber");
193  for(Long64_t entry = 0; entry < entries; ++entry)
194  {
195  event.getEntry(entry);
196 
198  const xAOD::EventInfo* eventInfo = nullptr;
199  event.retrieve(eventInfo,"EventInfo").ignore();
200  unsigned runNumber = periodRuns[uniformPdf(randomEngine)];
201  RandomRunNumberDec(*eventInfo) = runNumber;
202  vector<const xAOD::Electron*> myTriggeringElectrons;
203  const xAOD::ElectronContainer* electrons = nullptr;
204  event.retrieve(electrons,"Electrons").ignore();
205  for(auto electron : *electrons)
206  {
207  if(!electron->caloCluster()) continue;
208  float eta = fabs(electron->caloCluster()->etaBE(2));
209  float pt = electron->pt();
210  if(pt<10e3f || eta>=2.47) continue;
211  if(!truthType.isAvailable(*electron)) continue;
212  if(!truthOrigin.isAvailable(*electron)) continue;
213  int t = truthType(*electron), o = truthOrigin(*electron);
214  if(t!=2 || !(o==10 || (o>=12 && o<=22) || o==43)) continue;
216  if(pt < (runNumber>290000? 27e3f : 25e3f)) continue;
217 
218  myTriggeringElectrons.push_back(electron);
219  }
220 
221  vector<const xAOD::Muon*> myTriggeringMuons;
222  const xAOD::MuonContainer* muons = nullptr;
223  event.retrieve(muons,"Muons").ignore();
224  for(auto muon : *muons)
225  {
226  float pt = muon->pt();
227  if(pt<10e3f || fabs(muon->eta())>=2.5) continue;
228  auto mt = muon->muonType();
229  if(mt!=xAOD::Muon::Combined && mt!=xAOD::Muon::MuonStandAlone) continue;
230  auto& mtp = *(muon->primaryTrackParticle());
231  if(!truthType.isAvailable(mtp)) continue;
232  if(!truthOrigin.isAvailable(mtp)) continue;
233  int t = truthType(mtp), o = truthOrigin(mtp);
234  if(t!=6 || !(o==10 || (o>=12 && o<=22) || o==43)) continue;
236  if(pt < (runNumber>290000? 27.3e3f : 21e3f)) continue;
237 
238  myTriggeringMuons.push_back(muon);
239  }
240 
242  if(myTriggeringElectrons.size()+myTriggeringMuons.size() < 1) continue;
243 
244 
246  double sf = 1.;
247  auto cc = myTool->getEfficiencyScaleFactor(myTriggeringElectrons,
248  myTriggeringMuons, sf);
250  {
251  nSuitableEvents += 1;
252  sumW += sf;
253  }
254  else
255  {
256  Warning(MSGSOURCE, "Scale factor evaluation failed");
257  ++errors;
258  }
259  if(errors>10)
260  {
261  Error(MSGSOURCE, "Too many errors reported!");
262  break;
263  }
264  }
265  Info(MSGSOURCE, "Average scale factor: %f (over %ld events)",
266  sumW / nSuitableEvents, long(nSuitableEvents));
267  #ifndef XAOD_STANDALONE
268  ANA_CHECK(app->finalize())
269  #endif
270  return errors? 4 : 0;
271 }
272 
asg::AnaToolHandle::initialize
StatusCode initialize()
initialize the tool
SGTest::store
TestStore store
Definition: TestStore.cxx:23
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
asg::AnaToolHandle< CP::IMuonTriggerScaleFactors >
IAsgElectronEfficiencyCorrectionTool.h
ITrigGlobalEfficiencyCorrectionTool.h
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
MSGSOURCE
#define MSGSOURCE
Definition: TrigGlobEffCorrExample0.cxx:48
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 >
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:100
asg::AnaToolHandle::setProperty
StatusCode setProperty(const std::string &property, const T2 &value)
set the given property of the tool.
IDTPM::truthType
int truthType(const U &p)
Definition: TrackParametersHelper.h:274
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:46
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
IDTPM::truthOrigin
int truthOrigin(const U &p)
Definition: TrackParametersHelper.h:283
PATCore::ParticleDataType::Full
@ Full
Definition: PATCoreEnums.h:22
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:58
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
ElectronContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
MessageCheck.h
macros for messaging and checking status codes
PATCoreEnums.h
TEvent.h
IMuonTriggerScaleFactors.h
file
TFile * file
Definition: tile_monitor.h:29
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
AnaToolHandle.h
Trk::Combined
@ Combined
Definition: TrackSummaryTool.h:32
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:40
TEvent.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
MuonContainer.h
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
asg::AnaToolHandle::getHandle
const ToolHandle< T > & getHandle() const noexcept
the tool handle we wrap
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
main
int main(int argc, char *argv[])
Definition: TrigGlobEffCorrExample0.cxx:53
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
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
entries
double entries
Definition: listroot.cxx:49
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.
TStore.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
AuxElement.h
Base class for elements of a container that can have aux data.
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