a simple testing macro for the MuonSelectorTools_xAOD package shamelessly stolen from CPToolTests.cxx
Example of how to run the MuonSelectorTools package to obtain information from muons
47 {
48
50
51
52 if (argc < 2) {
55 return 1;
56 }
57
58
59#ifdef XAOD_STANDALONE
62 return 1;
63 }
65#else
67#endif
68
69
72 std::unique_ptr<TFile>
ifile(TFile::Open(fileName,
"READ"));
75 return 1;
76 }
77
78
79 if (
event.readFrom(
ifile.get() ).isFailure() ) {
81 return 1;
82 }
83 Info(
APP_NAME,
"Number of events in the file: %i",
static_cast<int>(
event.getEntries()));
84
85
86 Long64_t
entries =
event.getEntries();
87 if (argc > 2) {
88 const Long64_t
e = atoll(argv[2]);
90 }
91
92
93 const int Nwp = 7;
94 std::vector<std::string> WPnames = {"Tight", "Medium", "Loose", "VeryLoose", "HighPt", "LowPt", "LowPtMVA"};
95
96
97
98
99
100
101 std::vector<std::string>
padding;
103
104 unsigned int maxNameLength = 0;
105 for (
int wp = 0;
wp < Nwp;
wp++)
106 if (WPnames[wp].
size() > maxNameLength) maxNameLength = WPnames[
wp].size();
107
108 for (
int wp = 0;
wp < Nwp;
wp++) {
109 std::string pad = "";
110 for (
unsigned int i = 0;
i < maxNameLength - WPnames[
wp].size();
i++) pad +=
" ";
111
113 }
114
115
116 std::string namesString = " ";
117 for (
int wp = 0;
wp < Nwp;
wp++) namesString += WPnames[wp] +
" ";
118
119
120 int allMuons = 0;
121 int nPositive = 0;
122 int nNegative = 0;
123
124
125 int selectedMuons[Nwp];
126 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuons[wp] = 0;
127
128 int selectedMuonsNotBad[Nwp];
129 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsNotBad[wp] = 0;
130
131
132 const int Ntype = 5;
133
134
135
136 int allMuonsType[Ntype];
138
139
140 int selectedMuonsType[Ntype][Nwp];
142 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsType[
type][wp] = 0;
143
144 int selectedMuonsTypeNotBad[Ntype][Nwp];
146 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTypeNotBad[
type][wp] = 0;
147
148
149
150 constexpr std::size_t nAuthor = static_cast<int>(xAOD::Muon::Author::NumberOfMuonAuthors);
151 std::array<int, nAuthor> allMuonsAuthor{};
152
153
154 std::array<std::array<int, Nwp>, nAuthor> selectedMuonsAuthor{}, selectedMuonsAuthorNotBad{};
155
156
157
158 const int Neta = 4;
159 double etaCuts[Neta - 1] = {1.0, 2.0, 2.5};
160
161 std::string etaRegions = "|eta| < 1.0 1.0 < |eta| < 2.0 2.0 < |eta| < 2.5 |eta| > 2.5";
162
163
164 int allMuonsEta[Neta];
165 for (
int eta = 0;
eta < Neta;
eta++) allMuonsEta[
eta] = 0;
166
167
168 int selectedMuonsEta[Neta][Nwp];
170 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEta[
eta][wp] = 0;
171
172 int selectedMuonsEtaNotBad[Neta][Nwp];
174 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEtaNotBad[
eta][wp] = 0;
175
176
177 const int NtruthType = 5;
178
179 std::string truthTypeNames[NtruthType] = {"Prompt", "Non-isolated", "Hadron", "Background", "Other"};
180
181
182 int allMuonsTruthType[NtruthType];
183 for (
int truthType = 0;
truthType < NtruthType;
truthType++) allMuonsTruthType[truthType] = 0;
184
185
186 int selectedMuonsTruthType[NtruthType][Nwp];
188 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTruthType[truthType][wp] = 0;
189
190 int selectedMuonsTruthTypeNotBad[NtruthType][Nwp];
192 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTruthTypeNotBad[truthType][wp] = 0;
193
194
196
197 std::vector<std::unique_ptr<CP::MuonSelectionTool> > selectorTools;
198 selectorTools.clear();
199
200
202
203 event.getEntry(entry);
204
205
206 int selectedMuonsEvent[Nwp];
207 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEvent[wp] = 0;
208
209 int selectedMuonsEventNotBad[Nwp];
210 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEventNotBad[wp] = 0;
211
212
214 if (
event.retrieve( ei,
"EventInfo" ).isFailure() ) {
216 return 1;
217 }
218
219 if(entry==0)
220 {
221 bool isRun3=false;
224
225 if(isRun3)
Info(
APP_NAME,
"setting up muon selection tools for Run3 geometry");
226 else Info(
APP_NAME,
"setting up muon selection tools for Run2 geometry");
227
228 for (
int wp = 0;
wp < Nwp;
wp++) {
229 Info(
APP_NAME,
"Creating selector tool for working point: %s ...", WPnames[wp].c_str());
230
232 muonSelection->
msg().setLevel(MSG::INFO);
233
234 bool failed = false;
235 failed = failed || muonSelection->setProperty("IsRun3Geo",isRun3).isFailure();
236 failed = failed || muonSelection->setProperty("MaxEta", 2.7).isFailure();
237 if (WPnames[wp] == "LowPtMVA") {
238 failed = failed || muonSelection->setProperty( "MuQuality", 5).isFailure();
239 failed = failed || muonSelection->setProperty( "UseMVALowPt", true).isFailure();
240 }
241 else
242 failed = failed || muonSelection->setProperty("MuQuality", wp).isFailure();
243
244 failed = failed || muonSelection->setProperty("TurnOffMomCorr", true).isFailure();
245 failed = failed || muonSelection->
initialize().isFailure();
246
247 if (failed) {
248 Error(
APP_NAME,
"Failed to set up MuonSelectorTool for working point: %s !", WPnames[wp].c_str() );
249 return 1;
250 }
251
252 selectorTools.emplace_back(muonSelection);
253 }
254 }
255
257 "===>>> start processing event #%i, "
258 "run #%i %i events processed so far <<<===",
259 static_cast<int>(ei->
eventNumber()),
static_cast<int>(ei->
runNumber()),
static_cast<int>(entry));
260
261
263 if (
event.retrieve( muons,
"Muons" ).isFailure() ) {
265 return 1;
266 }
268
269 xAOD::Muon::Quality my_quality;
270 bool passesIDRequirements;
271 bool passesPreselectionCuts;
272
273 int muCounter = 0;
274
275
278 for (; mu_itr != mu_end; ++mu_itr) {
279 int etaIndex = Neta - 1;
281 if (std::abs((*mu_itr)->eta()) < etaCuts[
eta]) {
283 break;
284 }
285
286 allMuons++;
287 allMuonsType[static_cast<int>((*mu_itr)->muonType())]++;
288 allMuonsAuthor[static_cast<int>((*mu_itr)->author())]++;
289 allMuonsEta[etaIndex]++;
290 muCounter++;
291
292 Info(
APP_NAME,
"===== Muon number: %i",
static_cast<int>(muCounter));
293
294
297 int truthClass =
isMC ? truthTypeAcc (**mu_itr) : -999;
298
308 else
310
312
313 if ((*mu_itr)->charge() > 0)
314 nPositive++;
315 else
316 nNegative++;
317
318 passesIDRequirements = selectorTools[0]->passedIDCuts(**mu_itr);
319 passesPreselectionCuts = selectorTools[0]->passedMuonCuts(**mu_itr);
320 my_quality = selectorTools[0]->getQuality(**mu_itr);
321
322
323 if (isMC)
Info(
APP_NAME,
"Muon truthType: %d (%s)", truthClass, truthTypeNames[truthType].c_str());
324 Info(
APP_NAME,
"Muon pT [GeV]: %g ", std::abs((*mu_itr)->pt()) / 1000.);
325 Info(
APP_NAME,
"Muon eta, phi: %g, %g ", (*mu_itr)->eta(), (*mu_itr)->phi());
326 {
327 std::stringstream sstr{};
328 sstr<<"Muon muonType: "<<(*mu_itr)->muonType();
330 }
331 {
332 std::stringstream sstr{};
333 sstr<<"Muon primary author: "<<(*mu_itr)->author();
335 }{
336 std::stringstream sstr{};
337 sstr<<"Muon quality (from tool, from xAOD): "<<my_quality<<", "<<(*mu_itr)->quality();
339 }
340 Info(
APP_NAME,
"Muon passes cuts (ID hits, preselection): %d, %d", passesIDRequirements, passesPreselectionCuts);
341
342
343
344
345 std::string selectionResults = "Muon selection acceptance: ";
346 std::string badMuonResults = "Bad muon flag: ";
347
348 for (
int wp = 0;
wp < Nwp;
wp++) {
349 if (selectorTools[wp]->
accept(*mu_itr)) {
351 selectedMuonsEvent[
wp]++;
352 selectedMuonsType[(*mu_itr)->muonType()][
wp]++;
353 selectedMuonsAuthor[(*mu_itr)->author()][
wp]++;
355 selectedMuonsEta[etaIndex][
wp]++;
356 selectionResults += "pass ";
357
358 if (!selectorTools[wp]->isBadMuon(**mu_itr)) {
359 selectedMuonsNotBad[
wp]++;
360 selectedMuonsEventNotBad[
wp]++;
361 selectedMuonsTypeNotBad[(*mu_itr)->muonType()][
wp]++;
362 selectedMuonsAuthorNotBad[(*mu_itr)->author()][
wp]++;
364 selectedMuonsEtaNotBad[etaIndex][
wp]++;
365 }
366 } else
367 selectionResults += "fail ";
368
369 if (!selectorTools[wp]->isBadMuon(**mu_itr))
370 badMuonResults += "good ";
371 else
372 badMuonResults += "bad ";
373 }
374
375
379
380 }
381
382
383 std::string NselectedString = "Number of selected muons: ";
384 std::string NselectedStringNotBad = "Including bad muon veto: ";
385 for (
int wp = 0;
wp < Nwp;
wp++) {
386 NselectedString += std::to_string(selectedMuonsEvent[wp]) + " ";
387 NselectedStringNotBad += std::to_string(selectedMuonsEventNotBad[wp]) + " ";
388 }
389
394
395
397 "===>>> done processing event #%i, "
398 "run #%i %i events processed so far <<<===",
399 static_cast<int>(ei->
eventNumber()),
static_cast<int>(ei->
runNumber()),
static_cast<int>(entry + 1));
400
401 }
402
403
404
405 Info(
APP_NAME,
"======================================");
406 Info(
APP_NAME,
"========= Full run summary ===========");
407 Info(
APP_NAME,
"======================================");
408
410
411 Info(
APP_NAME,
"%i positive and %i negative muons", nPositive, nNegative);
412
413 Info(
APP_NAME,
"Selected muons by working point (numbers in parenthesis include bad muon veto):");
415 for (
int wp = 0;
wp < Nwp;
wp++)
416 Info(
APP_NAME,
"%s: %s %i (%i)", WPnames[wp].c_str(), padding[wp].c_str(), selectedMuons[wp], selectedMuonsNotBad[wp]);
418
419
420 Info(
APP_NAME,
"Selected muons by type and working point (numbers in parenthesis include bad muon veto):");
421 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
422 for (
int l = 0;
l < Nwp + 2;
l++) {
423 std::stringstream
line{};
424 if (l == 0) {
426 for (
int type = 0; type < Ntype; type++) line <<static_cast<xAOD::Muon::MuonType>(
type) <<
" ";
427 } else if (l == 1) {
428 line <<
"All muons: ";
430 line << std::left << std::setw(16) << allMuonsType[
type];
431 }
432 } else {
436 line << std::left << std::setw(16)
437 << selectedMuonsType[
type][
wp]<<
" ("<<selectedMuonsTypeNotBad[
type][
wp] <<
")";
438 }
439 }
440
442 }
443 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
444
445
446
447 Info(
APP_NAME,
"Selected muons by primary author and working point (numbers in parenthesis include bad muon veto):");
448 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
449 for (
int l = 0;
l < Nwp + 2;
l++) {
450 std::stringstream
line{};
451 if (l == 0) {
454
455
456 if (allMuonsAuthor[author] == 0) continue;
457
458 line << static_cast<xAOD::Muon::Author>(author);
459 }
460 } else if (l == 1) {
461 line <<
"All muons: ";
463
464 if (allMuonsAuthor[author] == 0) continue;
465
466 line << std::left << std::setw(16)<<allMuonsAuthor[
author];
467 }
468 } else {
472
473 if (allMuonsAuthor[author] == 0) continue;
474
475 line << std::left << std::setw(16);
477 << selectedMuonsAuthorNotBad[
author][
wp]<<
")";
478 }
479 }
480
482 }
483 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
484
485
486
487 Info(
APP_NAME,
"Selected muons by |eta| and working point (numbers in parenthesis include bad muon veto):");
488 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
489 for (
int l = 0;
l < Nwp + 2;
l++) {
490 std::string
line =
"";
491 if (l == 0) {
494 } else if (l == 1) {
495 line +=
"All muons: ";
497 std::stringstream
ss;
498 ss << std::left << std::setw(20) << std::to_string(allMuonsEta[
eta]);
500 }
501 } else {
505 std::stringstream
ss;
506 ss << std::left << std::setw(20)
507 << (std::to_string(selectedMuonsEta[
eta][wp]) +
" (" + std::to_string(selectedMuonsEtaNotBad[
eta][wp]) +
")");
509 }
510 }
511
513 }
514 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
515
516
517 if (isMC) {
518 Info(
APP_NAME,
"Selected muons by truth classification and working point (numbers in parenthesis include bad muon veto):");
519 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
520 for (
int l = 0;
l < Nwp + 2;
l++) {
521 std::string
line =
"";
522 if (l == 0) {
524 for (
int truthType = 0;
truthType < NtruthType;
truthType++) line += truthTypeNames[truthType] +
" ";
525 } else if (l == 1) {
526 line +=
"All muons: ";
528 std::stringstream
ss;
529 ss << std::left << std::setw(16) << std::to_string(allMuonsTruthType[truthType]);
531 }
532 } else {
536 std::stringstream
ss;
537 ss << std::left << std::setw(16)
538 << (std::to_string(selectedMuonsTruthType[truthType][wp]) + " (" +
539 std::to_string(selectedMuonsTruthTypeNotBad[truthType][wp]) + ")");
541 }
542 }
543
545 }
546 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
547 }
548
549
551
552
553 return 0;
554}
Scalar eta() const
pseudorapidity method
size_t size() const
Number of registered mappings.
DataModel_detail::const_iterator< DataVector > const_iterator
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.
size_type size() const noexcept
Returns the number of elements in the collection.
Helper class to provide constant type-safe access to aux data.
bool eventType(EventType type) const
Check for one particular bitmask value.
@ IS_SIMULATION
true: simulation, false: data
uint32_t runNumber() const
The current event's run number.
uint64_t eventNumber() const
The current event's event number.
ReadStats & stats()
Access the object belonging to the current thread.
static IOStats & instance()
Singleton object accessor.
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
StatusCode accept(const xAOD::Muon *mu)
int truthType(const U &p)
Error
The different types of error that can be flagged in the L1TopoRDO.
l
Printing final latex table to .tex output file.
std::atomic< int > padding
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
EventInfo_v1 EventInfo
Definition of the latest event info version.
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".