ATLAS Offline Software
Loading...
Searching...
No Matches
TrigGlobEffCorrExample3a.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 * The set of examples 3a - 3e illustrates the use of lepton selection tags
10 * for various scenarios:
11 *
12 * - Example 3a: trigger = 2e12_lhloose_L12EM10VH, selecting events containing
13 * >=2 loose electrons, the leading-pT electron always satisfying
14 * in addition tight PID+isolation requirements.
15 *
16 * - Example 3b: trigger = e24_lhmedium_L1EM20VH_OR_e60_lhmedium_OR_e120_lhloose
17 * || 2e12_lhloose_L12EM10VH
18 * selecting events with >=2 loose electrons where the leading-pT
19 * electron also satisfies medium PID requirements.
20 * Only the latter is allowed to fire the single-electron trigger.
21 *
22 * - Example 3c: trigger = e24_lhmedium_L1EM20VH_OR_e60_lhmedium_OR_e120_lhloose
23 * || 2e12_lhloose_L12EM10VH
24 * selecting events with >=2 loose electrons. Any electron also
25 * satisfying medium PID requirements is allowed to fire the
26 * single-electron trigger.
27 *
28 * - Example 3d: trigger = 2e17_lhvloose_nod0 || e7_lhmedium_nod0_mu24
29 * || e26_lhtight_nod0_ivarloose_OR_e60_lhmedium_nod0_OR_e140_lhloose_nod0
30 * same type of selection as example 3c but with 3 PID working
31 * points, using two distinct decorations.
32 *
33 * - Example 3e: same scenario as example 3d, but using an alternative
34 * implementation that requires only one decoration.
35 *
36 */
37/*
38 * For this example (3a), one needs to set up two versions of the electron
39 * trigger tools: one configured for offline tight PID+isolation, to be used for
40 * the leading electron, and another configured for offline loose PID, to be
41 * used for subleading electrons.
42 *
43 * The configuration of the TrigGlobalEfficiencyCorrectionTool then involves
44 * filling two additional properties, 'ListOfTagsPerTool' and
45 * 'LeptonTagDecorations'. We also fill a third one, 'ElectronLegsPerTag';
46 * it isn't required but still advised as the tool can then perform further
47 * consistency checks of its configuration.",
48 *
49 * The leading electron will always be tagged with a 'Signal' decoration
50 * (decorated value set to 1), while subleading electrons are never tagged
51 * (-> decorated value set to 0).
52 * Since efficiencies provided by CP tools are inclusive, one should NEVER
53 * tag the subleading leptons as 'Signal', even if they satisfy the tight PID
54 * + isolation requirements; doing so would bias the results.
55 * See example 3c that deals with a more complicate situation requiring
56 * handling multiple PID levels for all leptons.
57 *
58 */
59
60// ROOT include(s):
61#include <TFile.h>
62#include <TError.h>
63
64// Infrastructure include(s):
65#ifdef XAOD_STANDALONE
66 #include "xAODRootAccess/Init.h"
69#else
72#endif
73
75// EDM include(s):
87
88// stdlib include(s):
89#include <sstream>
90#include <random>
91#include <vector>
92#include <array>
93using std::vector;
94using std::string;
95
97namespace { vector<string> split_comma_delimited(const std::string&); }
98
99#define MSGSOURCE "Example 3a"
100
101ANA_MSG_HEADER(Test)
103using namespace Test;
104int main(int argc, char* argv[])
105{
107 const char* filename = nullptr;
108 bool debug = false, cmdline_error = false, toys = false;
109 for(int i=1;i<argc;++i)
110 {
111 if(string(argv[i]) == "--debug") debug = true;
112 else if(string(argv[i]) == "--toys") toys = true;
113 else if(!filename && *argv[i]!='-') filename = argv[i];
114 else cmdline_error = true;
115 }
116 if(!filename || cmdline_error)
117 {
118 Error(MSGSOURCE, "No file name received!");
119 Error(MSGSOURCE, " Usage: %s [--debug] [--toys] [DxAOD file name]", argv[0]);
120 return 1;
121 }
122 #ifdef XAOD_STANDALONE
123 xAOD::Init(MSGSOURCE).ignore();
124 TFile* file = TFile::Open(filename, "READ");
125 if(!file)
126 {
127 Error(MSGSOURCE, "Unable to open file!");
128 return 2;
129 }
131 xAOD::TStore store;
132 StatusCode::enableFailure();
133 #else
134 IAppMgrUI* app = POOL::Init();
136 TString file(filename);
137 #endif
138 event.readFrom(file).ignore();
139 Long64_t entries = event.getEntries();
140 Info(MSGSOURCE, "Number of events in the file: %lli", entries);
141
142 /* ********************************************************************** */
143
144 Info(MSGSOURCE, "Configuring the electron CP tools");
146 ToolHandleArray<IAsgElectronEfficiencyCorrectionTool> electronEffTools;
148 ToolHandleArray<IAsgElectronEfficiencyCorrectionTool> electronSFTools;
150 std::map<std::string,std::string> legsPerTool;
152 std::map<std::string,std::string> tagsPerTool;
154 std::map<std::string,std::string> legsPerTag;
156 static const SG::Decorator<char> dec_signal("Signal");
157
159 vector<asg::AnaToolHandle<IAsgElectronEfficiencyCorrectionTool>> factory;
160 enum{ cLEGS, cTAG, cKEY, cPID, cISO };
161 std::vector<std::array<std::string,5> > toolConfigs = {
164 {"e12_lhloose_L1EM10VH", "Signal", "2015_e12_lhloose_L1EM10VH", "Tight", "FixedCutTightTrackOnly"},
166 {"e12_lhloose_L1EM10VH", "*", "2015_e12_lhloose_L1EM10VH", "LooseBLayer", ""}
167 };
168
169 const char* mapPath = "ElectronEfficiencyCorrection/2015_2017/"
170 "rel21.2/Moriond_February2018_v2/map6.txt";
171 for(auto& cfg : toolConfigs)
172 for(int j=0;j<2;++j)
173 {
174 string name = "AsgElectronEfficiencyCorrectionTool/"
175 + ((j? "ElTrigEff_" : "ElTrigSF_")
176 + std::to_string(factory.size()/2));
177 auto t = factory.emplace(factory.end(), name);
178 t->setProperty("MapFilePath", mapPath).ignore();
179 t->setProperty("TriggerKey", string(j?"":"Eff_") + cfg[cKEY]).ignore();
180 t->setProperty("IdKey", cfg[cPID]).ignore();
181 t->setProperty("IsoKey", cfg[cISO]).ignore();
182
183 t->setProperty("CorrelationModel", "TOTAL").ignore();
184 t->setProperty("ForceDataType", (int)PATCore::ParticleDataType::Full).ignore();
185 if(t->initialize() != StatusCode::SUCCESS)
186 {
187 Error(MSGSOURCE, "Unable to initialize the electron CP tool <%s>!",
188 t->name().c_str());
189 return 3;
190 }
191 auto& handles = (j? electronSFTools : electronEffTools);
192 handles.push_back(t->getHandle());
195 name = handles[handles.size()-1].name();
196 legsPerTool[name] = cfg[cLEGS];
197 tagsPerTool[name] = cfg[cTAG];
198 if(!j)
199 {
200 for(auto& tag : ::split_comma_delimited(cfg[cTAG]))
201 {
202 if(legsPerTag[tag]=="") legsPerTag[tag] = cfg[cLEGS];
203 else legsPerTag[tag] += "," + cfg[cLEGS];
204 }
205 }
206
207 }
208
209 /* ********************************************************************** */
210
211 Info(MSGSOURCE, "Configuring the global trigger SF tool");
212 asg::AnaToolHandle<ITrigGlobalEfficiencyCorrectionTool> myTool("TrigGlobalEfficiencyCorrectionTool/TrigGlobal");
213 myTool.setProperty("ElectronEfficiencyTools", electronEffTools).ignore();
214 myTool.setProperty("ElectronScaleFactorTools", electronSFTools).ignore();
215 const char* triggers2015 = "2e12_lhloose_L12EM10VH";
216 myTool.setProperty("TriggerCombination2015", triggers2015).ignore();
217 myTool.setProperty("LeptonTagDecorations", "Signal").ignore();
218 myTool.setProperty("ListOfLegsPerTool", legsPerTool).ignore();
219 myTool.setProperty("ListOfTagsPerTool", tagsPerTool).ignore();
220 myTool.setProperty("ListOfLegsPerTag", legsPerTag).ignore();
221
222 if(debug) myTool.setProperty("OutputLevel", MSG::DEBUG).ignore();
223 if(toys) myTool.setProperty("NumberOfToys", 1000).ignore();
224 if(myTool.initialize() != StatusCode::SUCCESS)
225 {
226 Error(MSGSOURCE, "Unable to initialize the TrigGlob tool!");
227 return 3;
228 }
229
232 const unsigned periodRuns[] = {
234 276073, 278727, 279932, 280423, 281130, 282625
235 };
236 std::uniform_int_distribution<unsigned> uniformPdf(0,
237 sizeof(periodRuns)/sizeof(*periodRuns) - 1);
238 std::default_random_engine randomEngine;
239
240 static const SG::ConstAccessor<int> truthType("truthType");
241 static const SG::ConstAccessor<int> truthOrigin("truthOrigin");
242
243 /* ********************************************************************** */
244
245 Info(MSGSOURCE, "Starting the event loop");
246 unsigned errors = 0;
247 double nSuitableEvents = 0., sumW = 0.;
248 static const SG::Decorator<unsigned> RandomRunNumberDec("RandomRunNumber");
249 for(Long64_t entry = 0; entry < entries; ++entry)
250 {
251 event.getEntry(entry);
252
254 const xAOD::EventInfo* eventInfo = nullptr;
255 event.retrieve(eventInfo,"EventInfo").ignore();
256 unsigned runNumber = periodRuns[uniformPdf(randomEngine)];
257 RandomRunNumberDec(*eventInfo) = runNumber;
258 vector<const xAOD::Electron*> myTriggeringElectrons;
259 const xAOD::ElectronContainer* electrons = nullptr;
260 event.retrieve(electrons,"Electrons").ignore();
261 for(auto electron : *electrons)
262 {
263 if(!electron->caloCluster()) continue;
264 float eta = fabs(electron->caloCluster()->etaBE(2));
265 float pt = electron->pt();
266 if(pt<10e3f || eta>=2.47) continue;
267 if(!truthType.isAvailable(*electron)) continue;
268 if(!truthOrigin.isAvailable(*electron)) continue;
269 int t = truthType(*electron), o = truthOrigin(*electron);
270 if(t!=2 || !(o==10 || (o>=12 && o<=22) || o==43)) continue;
272 if(pt < 13e3f) continue;
273
274 myTriggeringElectrons.push_back(electron);
275 }
276
277 vector<const xAOD::Muon*> myTriggeringMuons;
278
280 if(myTriggeringElectrons.size() < 2) continue;
281
284 auto compareByPt = [](const xAOD::Electron*e1, const xAOD::Electron*e2)
285 { return e1->pt() < e2->pt(); };
286 auto leadingElectron = *std::max_element(myTriggeringElectrons.begin(),
287 myTriggeringElectrons.end(), compareByPt);
288 for(auto electron : myTriggeringElectrons)
289 {
292 dec_signal(*electron) = (electron==leadingElectron)? 1 : 0;
293 }
294
295
297 double sf = 1.;
298 auto cc = myTool->getEfficiencyScaleFactor(myTriggeringElectrons,
299 myTriggeringMuons, sf);
301 {
302 nSuitableEvents += 1;
303 sumW += sf;
304 }
305 else
306 {
307 Warning(MSGSOURCE, "Scale factor evaluation failed");
308 ++errors;
309 }
310 if(errors>10)
311 {
312 Error(MSGSOURCE, "Too many errors reported!");
313 break;
314 }
315 }
316 Info(MSGSOURCE, "Average scale factor: %f (over %ld events)",
317 sumW / nSuitableEvents, long(nSuitableEvents));
318 #ifndef XAOD_STANDALONE
319 ANA_CHECK(app->finalize());
320 #endif
321 return errors? 4 : 0;
322}
323
325namespace
326{
327 inline vector<string> split_comma_delimited(const string& s)
328 {
329 std::stringstream ss(s);
330 std::vector<std::string> tokens;
331 std::string token;
332 while(std::getline(ss, token, ','))
333 {
334 if(token.length()) tokens.push_back(token);
335 }
336 return tokens;
337 }
338}
Scalar eta() const
pseudorapidity method
Base class for elements of a container that can have aux data.
Helper class to provide constant type-safe access to aux data.
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
Helper class to provide type-safe access to aux data.
static Double_t ss
const bool debug
@ Ok
The correction was done successfully.
Helper class to provide constant type-safe access to aux data.
Helper class to provide type-safe access to aux data.
Definition Decorator.h:59
a modified tool handle that allows its owner to configure new tools from the C++ side
StatusCode setProperty(const std::string &property, const T2 &value)
set the given property of the tool.
StatusCode initialize()
initialize the tool
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
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
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition Init.cxx:31
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
EventInfo_v1 EventInfo
Definition of the latest event info version.
Electron_v1 Electron
Definition of the current "egamma version".
#define MSGSOURCE
Test code to test ElectronPhotonVariableCorrectionTool Dictionaries.
TFile * file