ATLAS Offline Software
MuonTriggerSFFilesTest.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 // Script for checking whether muon trigger scale factors file contains correct histograms
6 
7 #include <string>
8 #include <map>
9 #include <fstream>
10 #include <set>
11 #include <sstream>
12 #include <algorithm>
13 
14 #include <TFile.h>
15 #include <TError.h>
16 #include <TH2.h>
17 #include <TKey.h>
18 
19 static const std::vector<std::string> qualities { "HighPt", "Tight", "Medium", "Loose" };
20 static const std::vector<std::string> types { "data", "mc" };
21 static const std::vector<std::string> regions { "barrel", "endcap" };
22 static const std::vector<std::string> bins { "coarse", "fine" };
23 static const std::vector<std::string> systematics { "nominal", "stat_up", "stat_down", "syst_up", "syst_down" };
24 
25 int main(int argc, char* argv[]) {
26 
27  const char* APP_NAME = argv[0];
28 
29  if (argc < 3) {
30  Error(APP_NAME, "No scale factors or configuration file name received!");
31  Error(APP_NAME, " Usage: %s [configuration file name] [scale factors file name]", APP_NAME);
32  return 1;
33  }
34 
35  std::ifstream confFileStream(argv[1], std::ifstream::in);
36  if (!confFileStream.good()) {
37  Error(APP_NAME, " Unable to load configuration file");
38  return 1;
39  }
40 
41  std::map<std::string, std::set<std::string>> triggersByPeriods;
42  std::string periodInfo;
43  while (std::getline(confFileStream, periodInfo, ';')) {
44  // Ignore spaces and line changes
45  periodInfo.erase(std::remove(periodInfo.begin(), periodInfo.end(), ' '), periodInfo.end());
46  periodInfo.erase(std::remove(periodInfo.begin(), periodInfo.end(), '\n'), periodInfo.end());
47  if (periodInfo.empty()) continue;
48 
49  std::stringstream ss;
50  ss.str(periodInfo);
51  std::string period;
52  if (!std::getline(ss, period, ':')) {
53  Error(APP_NAME, " Configuration file contains invalid period description: %s", periodInfo.c_str());
54  return 1;
55  }
56  if (triggersByPeriods.find(period) != triggersByPeriods.end()) {
57  Error(APP_NAME, " Period %s is defined multiple times in configuration file", period.c_str());
58  }
59 
60  std::set<std::string> triggers;
61  std::string trigger;
62  while (std::getline(ss, trigger, ',')) {
63  triggers.insert(trigger);
64  }
65  triggersByPeriods[period] = triggers;
66  }
67  confFileStream.close();
68 
69 
70  TFile* file = TFile::Open(argv[2]);
71  if (!file) {
72  Error(APP_NAME, " Unable to load scale factors file");
73  return 1;
74  }
75 
76  for (size_t i = 0; i < qualities.size(); i++) {
77  TDirectory* qualityDirectory = file->GetDirectory(qualities[i].c_str());
78  if (!qualityDirectory) {
79  Error(APP_NAME, " Directory for quality %s is missing", qualities[i].c_str());
80  continue;
81  }
82  TKey* periodKey;
83  TIter nextPeriod(qualityDirectory->GetListOfKeys());
84  while ((periodKey = (TKey*)nextPeriod())) {
85  if (triggersByPeriods.find(periodKey->GetName()) == triggersByPeriods.end()) {
86  Error(APP_NAME, " Quality %s contains period %s, which is not defined in configuration file", qualities[i].c_str(), periodKey->GetName());
87  }
88  }
89 
90  for (auto const& x : triggersByPeriods)
91  {
92  std::string period = x.first;
93  std::set<std::string> triggers = x.second;
94  TDirectory* periodDirectory = qualityDirectory->GetDirectory(period.c_str());
95  if (!periodDirectory) {
96  Error(APP_NAME, " Configuration file contains period %s, but this is missing in scale factors file under quality %s", period.c_str(), qualities[i].c_str());
97  continue;
98  }
99 
100  TKey* triggerKey;
101  TIter nextTrigger(periodDirectory->GetListOfKeys());
102  while ((triggerKey = (TKey*)nextTrigger())) {
103  if (triggers.find(triggerKey->GetName()) == triggers.end()) {
104  Error(APP_NAME, " Period %s under quality %s contains trigger %s, which is not defined in configuration file", period.c_str(), qualities[i].c_str(), triggerKey->GetName());
105  }
106  }
107 
108  for (auto const& trigger : triggers)
109  {
110  TDirectory* triggerDirectory = periodDirectory->GetDirectory(trigger.c_str());
111  if (!triggerDirectory) {
112  Error(APP_NAME, " Configuration file contains trigger %s under period %s, but this is missing in scale factors file under quality %s", trigger.c_str(), period.c_str(), qualities[i].c_str());
113  continue;
114  }
115 
116  for (size_t ibins = 0; ibins < bins.size(); ++ibins) {
117  for (size_t iregion = 0; iregion < regions.size(); ++iregion) {
118  for (size_t itype = 0; itype < types.size(); ++itype) {
119  for (size_t isys = 0; isys < systematics.size(); ++isys) {
120  if (types[itype].find("data") != std::string::npos && systematics[isys].find("syst") != std::string::npos) continue;
121  std::string path = "eff_etaphi_" + bins[ibins] + "_" + regions[iregion] + "_" + types[itype] + "_" + systematics[isys];
122  TH2* hist = dynamic_cast<TH2*>(triggerDirectory->Get(path.c_str()));
123  if (!hist) {
124  Error(APP_NAME, " Histogram %s not found under trigger %s, period %s and quality %s", path.c_str(), trigger.c_str(), period.c_str(), qualities[i].c_str());
125  continue;
126  }
127  if (hist->GetEntries() == 0) {
128  Error(APP_NAME, " Histogram %s under trigger %s, period %s and quality %s is empty", path.c_str(), trigger.c_str(), period.c_str(), qualities[i].c_str());
129  }
130  }
131  }
132  }
133  }
134  }
135  }
136  }
137  file->Close();
138  delete file;
139 
140  return 0;
141 }
python.AtlRunQueryAMI.period
period
Definition: AtlRunQueryAMI.py:225
periodInfo
Definition: periodInfo.py:1
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
plotmaker.hist
hist
Definition: plotmaker.py:148
python.TrigTLAMonitorAlgorithm.triggers
triggers
Definition: TrigTLAMonitorAlgorithm.py:196
x
#define x
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
lumiFormat.i
int i
Definition: lumiFormat.py:85
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
APP_NAME
#define APP_NAME
Definition: BoostedXbbTag.cxx:23
file
TFile * file
Definition: tile_monitor.h:29
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
CheckAppliedSFs.systematics
def systematics
Definition: CheckAppliedSFs.py:231
main
int main(int argc, char *argv[])
Definition: MuonTriggerSFFilesTest.cxx:25
L1Topo::Error
Error
The different types of error that can be flagged in the L1TopoRDO.
Definition: Error.h:16