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
48 {
49
51
52
53 if (argc < 2) {
56 return 1;
57 }
58
59
60#ifdef XAOD_STANDALONE
63 return 1;
64 }
66#else
68#endif
69
70
73 std::unique_ptr<TFile>
ifile(TFile::Open(fileName,
"READ"));
76 return 1;
77 }
78
79
80 if (
event.readFrom(
ifile.get() ).isFailure() ) {
82 return 1;
83 }
84 Info(
APP_NAME,
"Number of events in the file: %i",
static_cast<int>(
event.getEntries()));
85
86
87 Long64_t
entries =
event.getEntries();
88 if (argc > 2) {
89 const Long64_t
e = atoll(argv[2]);
91 }
92
93
94 const int Nwp = 7;
95 std::vector<std::string> WPnames = {"Tight", "Medium", "Loose", "VeryLoose", "HighPt", "LowPt", "LowPtMVA"};
96
97
98
99
100
101
102 std::vector<std::string>
padding;
104
105 unsigned int maxNameLength = 0;
106 for (
int wp = 0;
wp < Nwp;
wp++)
107 if (WPnames[wp].size() > maxNameLength) maxNameLength = WPnames[
wp].size();
108
109 for (
int wp = 0;
wp < Nwp;
wp++) {
110 std::string pad = "";
111 for (
unsigned int i = 0;
i < maxNameLength - WPnames[
wp].size();
i++) pad +=
" ";
112
114 }
115
116
117 std::string namesString = " ";
118 for (
int wp = 0;
wp < Nwp;
wp++) namesString += WPnames[wp] +
" ";
119
120
121 int allMuons = 0;
122 int nPositive = 0;
123 int nNegative = 0;
124
125
126 int selectedMuons[Nwp];
127 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuons[wp] = 0;
128
129 int selectedMuonsNotBad[Nwp];
130 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsNotBad[wp] = 0;
131
132
133 const int Ntype = 5;
134
137 if (
type == xAOD::Muon::Combined)
139 else if (
type == xAOD::Muon::MuonStandAlone)
141 else if (
type == xAOD::Muon::SegmentTagged)
143 else if (
type == xAOD::Muon::CaloTagged)
145 else if (
type == xAOD::Muon::SiliconAssociatedForwardMuon)
147 else
149 }
150
151
152 int allMuonsType[Ntype];
154
155
156 int selectedMuonsType[Ntype][Nwp];
158 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsType[
type][wp] = 0;
159
160 int selectedMuonsTypeNotBad[Ntype][Nwp];
162 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTypeNotBad[
type][wp] = 0;
163
164
165
166 const int Nauthor = xAOD::Muon::NumberOfMuonAuthors;
167
168 std::string authorNames[Nauthor];
170 if (author == xAOD::Muon::MuidCo)
171 authorNames[
author] =
"MuidCo";
172 else if (author == xAOD::Muon::STACO)
173 authorNames[
author] =
"STACO";
174 else if (author == xAOD::Muon::MuTag)
175 authorNames[
author] =
"MuTag";
176 else if (author == xAOD::Muon::MuTagIMO)
177 authorNames[
author] =
"MuTagIMO";
178 else if (author == xAOD::Muon::MuidSA)
179 authorNames[
author] =
"MuidSA";
180 else if (author == xAOD::Muon::MuGirl)
181 authorNames[
author] =
"MuGirl";
182 else if (author == xAOD::Muon::MuGirlLowBeta)
183 authorNames[
author] =
"MuGirlLowBeta";
184 else if (author == xAOD::Muon::CaloTag)
185 authorNames[
author] =
"CaloTag";
186 else if (author == xAOD::Muon::CaloLikelihood)
187 authorNames[
author] =
"CaloLikelihood";
188 else if (author == xAOD::Muon::CaloScore)
189 authorNames[
author] =
"CaloScore";
190 else if (author == xAOD::Muon::ExtrapolateMuonToIP)
191 authorNames[
author] =
"ExtrapolateMuonToIP";
192 else
193 authorNames[
author] =
"unknown";
194 }
195
196
197 int allMuonsAuthor[Nauthor];
198 for (
int author = 0;
author < Nauthor;
author++) allMuonsAuthor[author] = 0;
199
200
201 int selectedMuonsAuthor[Nauthor][Nwp];
203 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsAuthor[author][wp] = 0;
204
205 int selectedMuonsAuthorNotBad[Nauthor][Nwp];
207 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsAuthorNotBad[author][wp] = 0;
208
209
210
211 const int Neta = 4;
212 double etaCuts[Neta - 1] = {1.0, 2.0, 2.5};
213
214 std::string etaRegions = "|eta| < 1.0 1.0 < |eta| < 2.0 2.0 < |eta| < 2.5 |eta| > 2.5";
215
216
217 int allMuonsEta[Neta];
218 for (
int eta = 0;
eta < Neta;
eta++) allMuonsEta[
eta] = 0;
219
220
221 int selectedMuonsEta[Neta][Nwp];
223 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEta[
eta][wp] = 0;
224
225 int selectedMuonsEtaNotBad[Neta][Nwp];
227 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEtaNotBad[
eta][wp] = 0;
228
229
230 const int NtruthType = 5;
231
232 std::string truthTypeNames[NtruthType] = {"Prompt", "Non-isolated", "Hadron", "Background", "Other"};
233
234
235 int allMuonsTruthType[NtruthType];
236 for (
int truthType = 0;
truthType < NtruthType;
truthType++) allMuonsTruthType[truthType] = 0;
237
238
239 int selectedMuonsTruthType[NtruthType][Nwp];
241 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTruthType[truthType][wp] = 0;
242
243 int selectedMuonsTruthTypeNotBad[NtruthType][Nwp];
245 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsTruthTypeNotBad[truthType][wp] = 0;
246
247
249
250 std::vector<std::unique_ptr<CP::MuonSelectionTool> > selectorTools;
251 selectorTools.clear();
252
253
255
256 event.getEntry(entry);
257
258
259 int selectedMuonsEvent[Nwp];
260 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEvent[wp] = 0;
261
262 int selectedMuonsEventNotBad[Nwp];
263 for (
int wp = 0;
wp < Nwp;
wp++) selectedMuonsEventNotBad[wp] = 0;
264
265
267 if (
event.retrieve( ei,
"EventInfo" ).isFailure() ) {
269 return 1;
270 }
271
272 if(entry==0)
273 {
274 bool isRun3=false;
277
278 if(isRun3)
Info(
APP_NAME,
"setting up muon selection tools for Run3 geometry");
279 else Info(
APP_NAME,
"setting up muon selection tools for Run2 geometry");
280
281 for (
int wp = 0;
wp < Nwp;
wp++) {
282 Info(
APP_NAME,
"Creating selector tool for working point: %s ...", WPnames[wp].c_str());
283
285 muonSelection->
msg().setLevel(MSG::INFO);
286
287 bool failed = false;
288 failed = failed || muonSelection->setProperty("IsRun3Geo",isRun3).isFailure();
289 failed = failed || muonSelection->setProperty("MaxEta", 2.7).isFailure();
290 if (WPnames[wp] == "LowPtMVA") {
291 failed = failed || muonSelection->setProperty( "MuQuality", 5).isFailure();
292 failed = failed || muonSelection->setProperty( "UseMVALowPt", true).isFailure();
293 }
294 else
295 failed = failed || muonSelection->setProperty("MuQuality", wp).isFailure();
296
297 failed = failed || muonSelection->setProperty("TurnOffMomCorr", true).isFailure();
298 failed = failed || muonSelection->
initialize().isFailure();
299
300 if (failed) {
301 Error(
APP_NAME,
"Failed to set up MuonSelectorTool for working point: %s !", WPnames[wp].c_str() );
302 return 1;
303 }
304
305 selectorTools.emplace_back(muonSelection);
306 }
307 }
308
310 "===>>> start processing event #%i, "
311 "run #%i %i events processed so far <<<===",
312 static_cast<int>(ei->
eventNumber()),
static_cast<int>(ei->
runNumber()),
static_cast<int>(entry));
313
314
316 if (
event.retrieve( muons,
"Muons" ).isFailure() ) {
318 return 1;
319 }
321
322 xAOD::Muon::Quality my_quality;
323 bool passesIDRequirements;
324 bool passesPreselectionCuts;
325
326 int muCounter = 0;
327
328
331 for (; mu_itr != mu_end; ++mu_itr) {
332 int etaIndex = Neta - 1;
334 if (std::abs((*mu_itr)->eta()) < etaCuts[
eta]) {
336 break;
337 }
338
339 allMuons++;
340 allMuonsType[(*mu_itr)->muonType()]++;
341 allMuonsAuthor[(*mu_itr)->author()]++;
342 allMuonsEta[etaIndex]++;
343 muCounter++;
344
345 Info(
APP_NAME,
"===== Muon number: %i",
static_cast<int>(muCounter));
346
347
350 int truthClass =
isMC ? truthTypeAcc (**mu_itr) : -999;
351
361 else
363
365
366 if ((*mu_itr)->charge() > 0)
367 nPositive++;
368 else
369 nNegative++;
370
371 passesIDRequirements = selectorTools[0]->passedIDCuts(**mu_itr);
372 passesPreselectionCuts = selectorTools[0]->passedMuonCuts(**mu_itr);
373 my_quality = selectorTools[0]->getQuality(**mu_itr);
374
375
376 if (isMC)
Info(
APP_NAME,
"Muon truthType: %d (%s)", truthClass, truthTypeNames[truthType].c_str());
377 Info(
APP_NAME,
"Muon pT [GeV]: %g ", std::abs((*mu_itr)->pt()) / 1000.);
378 Info(
APP_NAME,
"Muon eta, phi: %g, %g ", (*mu_itr)->eta(), (*mu_itr)->phi());
379 Info(
APP_NAME,
"Muon muonType: %d (%s)", (*mu_itr)->muonType(), typeNames[(*mu_itr)->muonType()].c_str());
380 Info(
APP_NAME,
"Muon primary author: %d (%s)", (*mu_itr)->author(), authorNames[(*mu_itr)->author()].c_str());
381
382 Info(
APP_NAME,
"Muon quality (from tool, from xAOD): %d, %d", my_quality, (*mu_itr)->quality());
383 Info(
APP_NAME,
"Muon passes cuts (ID hits, preselection): %d, %d", passesIDRequirements, passesPreselectionCuts);
384
385
386
387
388 std::string selectionResults = "Muon selection acceptance: ";
389 std::string badMuonResults = "Bad muon flag: ";
390
391 for (
int wp = 0;
wp < Nwp;
wp++) {
392 if (selectorTools[wp]->
accept(*mu_itr)) {
394 selectedMuonsEvent[
wp]++;
395 selectedMuonsType[(*mu_itr)->muonType()][
wp]++;
396 selectedMuonsAuthor[(*mu_itr)->author()][
wp]++;
398 selectedMuonsEta[etaIndex][
wp]++;
399 selectionResults += "pass ";
400
401 if (!selectorTools[wp]->isBadMuon(**mu_itr)) {
402 selectedMuonsNotBad[
wp]++;
403 selectedMuonsEventNotBad[
wp]++;
404 selectedMuonsTypeNotBad[(*mu_itr)->muonType()][
wp]++;
405 selectedMuonsAuthorNotBad[(*mu_itr)->author()][
wp]++;
407 selectedMuonsEtaNotBad[etaIndex][
wp]++;
408 }
409 } else
410 selectionResults += "fail ";
411
412 if (!selectorTools[wp]->isBadMuon(**mu_itr))
413 badMuonResults += "good ";
414 else
415 badMuonResults += "bad ";
416 }
417
418
422
423 }
424
425
426 std::string NselectedString = "Number of selected muons: ";
427 std::string NselectedStringNotBad = "Including bad muon veto: ";
428 for (
int wp = 0;
wp < Nwp;
wp++) {
429 NselectedString += std::to_string(selectedMuonsEvent[wp]) + " ";
430 NselectedStringNotBad += std::to_string(selectedMuonsEventNotBad[wp]) + " ";
431 }
432
437
438
440 "===>>> done processing event #%i, "
441 "run #%i %i events processed so far <<<===",
442 static_cast<int>(ei->
eventNumber()),
static_cast<int>(ei->
runNumber()),
static_cast<int>(entry + 1));
443
444 }
445
446
447
448 Info(
APP_NAME,
"======================================");
449 Info(
APP_NAME,
"========= Full run summary ===========");
450 Info(
APP_NAME,
"======================================");
451
453
454 Info(
APP_NAME,
"%i positive and %i negative muons", nPositive, nNegative);
455
456 Info(
APP_NAME,
"Selected muons by working point (numbers in parenthesis include bad muon veto):");
458 for (
int wp = 0;
wp < Nwp;
wp++)
459 Info(
APP_NAME,
"%s: %s %i (%i)", WPnames[wp].c_str(), padding[wp].c_str(), selectedMuons[wp], selectedMuonsNotBad[wp]);
461
462
463 Info(
APP_NAME,
"Selected muons by type and working point (numbers in parenthesis include bad muon veto):");
464 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
465 for (
int l = 0;
l < Nwp + 2;
l++) {
466 std::string
line =
"";
467 if (l == 0) {
470 } else if (l == 1) {
471 line +=
"All muons: ";
473 std::stringstream
ss;
474 ss << std::left << std::setw(16) << std::to_string(allMuonsType[
type]);
476 }
477 } else {
481 std::stringstream
ss;
482 ss << std::left << std::setw(16)
483 << (std::to_string(selectedMuonsType[
type][wp]) +
" (" + std::to_string(selectedMuonsTypeNotBad[
type][wp]) +
")");
485 }
486 }
487
489 }
490 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
491
492
493
494 Info(
APP_NAME,
"Selected muons by primary author and working point (numbers in parenthesis include bad muon veto):");
495 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
496 for (
int l = 0;
l < Nwp + 2;
l++) {
497 std::string
line =
"";
498 if (l == 0) {
501
502
503 if (allMuonsAuthor[author] == 0) continue;
504
505 std::stringstream
ss;
506 ss << std::left << std::setw(16);
509 }
510 } else if (l == 1) {
511 line +=
"All muons: ";
513
514 if (allMuonsAuthor[author] == 0) continue;
515
516 std::stringstream
ss;
517 ss << std::left << std::setw(16);
518 ss << std::to_string(allMuonsAuthor[author]);
520 }
521 } else {
525
526 if (allMuonsAuthor[author] == 0) continue;
527
528 std::stringstream
ss;
529 ss << std::left << std::setw(16);
530 ss << (std::to_string(selectedMuonsAuthor[author][wp]) +
" (" + std::to_string(selectedMuonsAuthorNotBad[author][wp]) +
")");
532 }
533 }
534
536 }
537 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
538
539
540
541 Info(
APP_NAME,
"Selected muons by |eta| and working point (numbers in parenthesis include bad muon veto):");
542 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
543 for (
int l = 0;
l < Nwp + 2;
l++) {
544 std::string
line =
"";
545 if (l == 0) {
548 } else if (l == 1) {
549 line +=
"All muons: ";
551 std::stringstream
ss;
552 ss << std::left << std::setw(20) << std::to_string(allMuonsEta[
eta]);
554 }
555 } else {
559 std::stringstream
ss;
560 ss << std::left << std::setw(20)
561 << (std::to_string(selectedMuonsEta[
eta][wp]) +
" (" + std::to_string(selectedMuonsEtaNotBad[
eta][wp]) +
")");
563 }
564 }
565
567 }
568 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
569
570
571 if (isMC) {
572 Info(
APP_NAME,
"Selected muons by truth classification and working point (numbers in parenthesis include bad muon veto):");
573 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
574 for (
int l = 0;
l < Nwp + 2;
l++) {
575 std::string
line =
"";
576 if (l == 0) {
578 for (
int truthType = 0;
truthType < NtruthType;
truthType++) line += truthTypeNames[truthType] +
" ";
579 } else if (l == 1) {
580 line +=
"All muons: ";
582 std::stringstream
ss;
583 ss << std::left << std::setw(16) << std::to_string(allMuonsTruthType[truthType]);
585 }
586 } else {
590 std::stringstream
ss;
591 ss << std::left << std::setw(16)
592 << (std::to_string(selectedMuonsTruthType[truthType][wp]) + " (" +
593 std::to_string(selectedMuonsTruthTypeNotBad[truthType][wp]) + ")");
595 }
596 }
597
599 }
600 Info(
APP_NAME,
"---------------------------------------------------------------------------------------");
601 }
602
603
605
606
607 return 0;
608}
Scalar eta() const
pseudorapidity method
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".