ATLAS Offline Software
CDIReader.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // CDIReader.h, (c) ATLAS Detector software
8 
9 #ifndef ANALYSISCDIREADER_H
10 #define ANALYSISCDIREADER_H
11 
12 #include <string>
13 #include <vector>
14 #include <map>
15 #include <set>
16 #include <iostream>
17 #include <unordered_map>
18 #include <algorithm>
19 
20 #include "TFile.h"
21 #include "TDirectoryFile.h"
22 #include "TMath.h"
23 #include "TEnv.h"
24 #include "TObjString.h"
25 
29 
30 #include <filesystem>
31 #include <fstream>
32 
33 class TH1;
34 class TFile;
35 class TMap;
36 
37 namespace Analysis
38 {
39  class CDIReader {
40 
41  typedef std::vector<std::string> Labels;
42  typedef std::map<std::string, Labels> Data; // {"labels": ["B", "C", ...], "systematics" : ["syst1", "syst2", ...], "B_syst" : ["syst1", "syst3", ...], "C_syst" : ["syst2", "syst3", ...], "DSID" : ["1", "2", "3", ...]}
43  typedef std::map<std::string, Data> WPoint;
44  typedef std::map<std::string, WPoint> JetColl;
45  typedef std::map<std::string, JetColl> Meta;
46 
47  public:
48 
50  CDIReader(const std::string& cdipath, bool verbose = false);
51  ~CDIReader() = default;
52 
53  void printTaggers(){
54  std::cout << "number of taggers: " << m_taggers.size() << std::endl;
55  for(const std::string& el : m_taggers){
56  std::cout << el << ", ";
57  } std::cout << std::endl;
58  }
59 
61  std::cout << "number of jet collections: " << m_jetcollections.size() << std::endl;
62  for(const std::string& el : m_jetcollections){
63  std::cout << el << ", ";
64  } std::cout << std::endl;
65  }
66 
68  std::cout << "number of working points: " << m_workingpoints.size() << std::endl;
69  for(const std::string& el : m_workingpoints){
70  std::cout << el << ", ";
71  } std::cout << std::endl;
72  }
73 
74  void printLabels(){
75  std::cout << "number of labels: " << m_labels.size() << std::endl;
76  for(const std::string& el : m_labels){
77  std::cout << el << ", ";
78  } std::cout << std::endl;
79  }
80 
81  void printDSIDs(){
82  Labels DSIDs = getDSIDs(); // without arguments, will return the vector of DSIDs
83  std::cout << "number of DSIDs: " << DSIDs.size() << std::endl;
84  for(const std::string& el : DSIDs){
85  std::cout << el << ", ";
86  } std::cout << std::endl;
87  };
88 
89  bool checkConfig(const std::string& tagger, const std::string& jetcoll, const std::string& wp, bool verbose = false);
90  Labels getDSIDs(const std::string& tagger = "", const std::string& jetcollection = "", const std::string& workingpoint = "");
91  Labels getLabels(const std::string& tagger = "", const std::string& jetcollection = "", const std::string& workingpoint = "");
92  Labels getWorkingPoints(const std::string& tagger, const std::string& jetcollection);
93  Labels getJetCollections(const std::string& tagger);
95 
96  private:
97  // utility function for printing out the configuration options
98  void printMetadata(int tagger = -1, int jetcoll = -1, int wpoint = -1, int label = -1);
100  bool m_initialized = false;
101  bool m_use_json = false;
102 
103  std::string m_cdipath = "";
104  std::unique_ptr<TFile> m_CDIFile;
105  std::set<std::string> m_taggers {};
106  std::set<std::string> m_jetcollections {};
107  std::set<std::string> m_workingpoints {};
108  std::set<std::string> m_labels {};
109  std::set<std::string> m_DSIDs {};
110 
111  void crawlCDI(TDirectoryFile* parentDir, int depth = 0, const std::string& metamap = "");
112 
113  void record_metadata(const std::string& datum, int depth = 0){
114  switch(depth){
115  case 0:
116  m_taggers.insert(datum);
117  break;
118  case 1:
119  m_jetcollections.insert(datum);
120  break;
121  case 2:
122  m_workingpoints.insert(datum);
123  break;
124  case 3:
125  m_labels.insert(datum);
126  break;
127  default:
128  std::cout << " record_metadata :: depth is " << depth << std::endl;
129  }
130  }
131 
132  void record_metadata_map(const Data& data, const std::string& path){
133  Labels metamap_path = split(path); // <"tagger", "jetcoll", "wp", "label">
134  int size = metamap_path.size();
135  if(size == 3){
136  m_metadata[metamap_path.at(0)][metamap_path.at(1)][metamap_path.at(2)] = data;
137  } else {
138  std::cout << " record_metadata_map :: the directory structure doesn't match what we expect!" << std::endl;
139  }
140  }
141 
142  // this function defines how we identify the working point directory
143  bool isWPdirectory(TList* list){
144  // check the directory (TList) contents
145  for(const auto label : *list){
146  const char* labelname = label->GetName();
147  if(strcmp(labelname, "B") == 0 || strcmp(labelname, "C") == 0
148  || strcmp(labelname, "Light") == 0 || strcmp(labelname, "T") == 0
149  || strcmp(labelname, "Z_BB") == 0 || strcmp(labelname, "QCD_BB") == 0
150  || strcmp(labelname, "Top_BX") == 0) return true;
151  }
152  return false;
153  }
154 
155  // local utility function: trim leading and trailing whitespace in the property strings
156  std::string trim(const std::string& str, const std::string& whitespace = " \t") {
157  const auto strBegin = str.find_first_not_of(whitespace);
158  if (strBegin == std::string::npos){
159  return ""; // no content
160  }
161  const auto strEnd = str.find_last_not_of(whitespace);
162  const auto strRange = strEnd - strBegin + 1;
163  return str.substr(strBegin, strRange);
164  }
165 
166  // local utility function: split string into a vector of substrings separated by a specified separator
167  std::vector<std::string> split(const std::string& str, char token = ';') {
168  std::vector<std::string> result;
169  if (str.size() > 0) {
170  std::string::size_type end;
171  std::string tmp(str);
172  do {
173  end = tmp.find(token);
174  std::string entry = trim(tmp.substr(0,end));
175  if (entry.size() > 0) result.push_back(entry);
176  if (end != std::string::npos) tmp = tmp.substr(end+1);
177  } while (end != std::string::npos);
178  }
179  return result;
180  }
181 
184 
185  };
186 
187 
188 }
189 
190 #endif
Analysis::CDIReader::m_use_json
bool m_use_json
Definition: CDIReader.h:101
TrigJetMonitorAlgorithm.jetcoll
jetcoll
Definition: TrigJetMonitorAlgorithm.py:1306
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Analysis::CDIReader::m_taggers
std::set< std::string > m_taggers
Definition: CDIReader.h:105
Analysis::CDIReader::getLabels
Labels getLabels(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
Definition: CDIReader.cxx:291
Analysis::CDIReader::m_metadata
Meta m_metadata
Definition: CDIReader.h:183
egammaParameters::depth
@ depth
pointing depth of the shower as calculated in egammaqgcld
Definition: egammaParamDefs.h:276
get_generator_info.result
result
Definition: get_generator_info.py:21
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
Analysis::CDIReader::m_jetcollections
std::set< std::string > m_jetcollections
Definition: CDIReader.h:106
Analysis::CDIReader::~CDIReader
~CDIReader()=default
Analysis::CDIReader::checkConfig
bool checkConfig(const std::string &tagger, const std::string &jetcoll, const std::string &wp, bool verbose=false)
Definition: CDIReader.cxx:194
Analysis::CDIReader::getWorkingPoints
Labels getWorkingPoints(const std::string &tagger, const std::string &jetcollection)
Definition: CDIReader.cxx:330
CalibrationDataContainer.h
Analysis::CDIReader::printWorkingPoints
void printWorkingPoints()
Definition: CDIReader.h:67
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
Analysis::CDIReader::printLabels
void printLabels()
Definition: CDIReader.h:74
Analysis::CDIReader::isWPdirectory
bool isWPdirectory(TList *list)
Definition: CDIReader.h:143
Analysis::CDIReader::Labels
std::vector< std::string > Labels
Definition: CDIReader.h:41
Analysis::CDIReader::getDSIDs
Labels getDSIDs(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
Definition: CDIReader.cxx:264
Analysis::CDIReader::m_DSIDs
std::set< std::string > m_DSIDs
Definition: CDIReader.h:109
Analysis::CDIReader::Meta
std::map< std::string, JetColl > Meta
Definition: CDIReader.h:45
CalibrationDataVariables.h
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
Analysis::CDIReader::split
std::vector< std::string > split(const std::string &str, char token=';')
Definition: CDIReader.h:167
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Analysis::CDIReader::printTaggers
void printTaggers()
Definition: CDIReader.h:53
Analysis::CDIReader::crawlCDI
void crawlCDI(TDirectoryFile *parentDir, int depth=0, const std::string &metamap="")
Definition: CDIReader.cxx:35
Analysis::CDIReader::m_cdipath
std::string m_cdipath
Definition: CDIReader.h:103
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
PlotSFuncertainty.wp
wp
Definition: PlotSFuncertainty.py:112
Analysis::CDIReader::m_CDIFile
std::unique_ptr< TFile > m_CDIFile
Definition: CDIReader.h:104
Analysis::CDIReader::WPoint
std::map< std::string, Data > WPoint
Definition: CDIReader.h:43
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
Analysis::CDIReader::m_workingpoints
std::set< std::string > m_workingpoints
Definition: CDIReader.h:107
Analysis
The namespace of all packages in PhysicsAnalysis/JetTagging.
Definition: BTaggingCnvAlg.h:20
Analysis::CDIReader::getJetCollections
Labels getJetCollections(const std::string &tagger)
Definition: CDIReader.cxx:313
Analysis::CDIReader::printJetCollections
void printJetCollections()
Definition: CDIReader.h:60
Analysis::CDIReader::trim
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Definition: CDIReader.h:156
Analysis::CDIReader::m_label_vec
Labels m_label_vec
Definition: CDIReader.h:182
Analysis::CDIReader::printMetadata
void printMetadata(int tagger=-1, int jetcoll=-1, int wpoint=-1, int label=-1)
Definition: CDIReader.cxx:127
Analysis::CDIReader::m_initialized
bool m_initialized
flag whether the initialization has been carried out
Definition: CDIReader.h:100
Analysis::CDIReader::record_metadata
void record_metadata(const std::string &datum, int depth=0)
Definition: CDIReader.h:113
Analysis::CDIReader::Data
std::map< std::string, Labels > Data
Definition: CDIReader.h:42
Analysis::CDIReader::JetColl
std::map< std::string, WPoint > JetColl
Definition: CDIReader.h:44
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
Analysis::CDIReader::record_metadata_map
void record_metadata_map(const Data &data, const std::string &path)
Definition: CDIReader.h:132
Analysis::CDIReader::m_labels
std::set< std::string > m_labels
Definition: CDIReader.h:108
str
Definition: BTagTrackIpAccessor.cxx:11
Analysis::CDIReader::getTaggers
Labels getTaggers()
Definition: CDIReader.cxx:347
Analysis::CDIReader
Definition: CDIReader.h:39
CalibrationDataInterfaceBase.h
Analysis::CDIReader::CDIReader
CDIReader(const std::string &cdipath, bool verbose=false)
normal constructor.
Definition: CDIReader.cxx:15
Analysis::CDIReader::printDSIDs
void printDSIDs()
Definition: CDIReader.h:81