ATLAS Offline Software
Loading...
Searching...
No Matches
CDIReader.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
33class TH1;
34class TFile;
35class TMap;
36
37namespace 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
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
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.empty()) result.push_back(std::move(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
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
void record_metadata(const std::string &datum, int depth=0)
Definition CDIReader.h:113
Labels getDSIDs(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
void printWorkingPoints()
Definition CDIReader.h:67
Labels getLabels(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
std::unique_ptr< TFile > m_CDIFile
Definition CDIReader.h:104
std::set< std::string > m_taggers
Definition CDIReader.h:105
std::set< std::string > m_DSIDs
Definition CDIReader.h:109
void record_metadata_map(const Data &data, const std::string &path)
Definition CDIReader.h:132
Labels getWorkingPoints(const std::string &tagger, const std::string &jetcollection)
bool checkConfig(const std::string &tagger, const std::string &jetcoll, const std::string &wp, bool verbose=false)
bool isWPdirectory(TList *list)
Definition CDIReader.h:143
std::set< std::string > m_workingpoints
Definition CDIReader.h:107
void printJetCollections()
Definition CDIReader.h:60
Labels getJetCollections(const std::string &tagger)
std::map< std::string, JetColl > Meta
Definition CDIReader.h:45
std::set< std::string > m_jetcollections
Definition CDIReader.h:106
std::map< std::string, WPoint > JetColl
Definition CDIReader.h:44
CDIReader(const std::string &cdipath, bool verbose=false)
normal constructor.
Definition CDIReader.cxx:15
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Definition CDIReader.h:156
std::string m_cdipath
Definition CDIReader.h:103
std::set< std::string > m_labels
Definition CDIReader.h:108
std::map< std::string, Labels > Data
Definition CDIReader.h:42
std::vector< std::string > split(const std::string &str, char token=';')
Definition CDIReader.h:167
void printMetadata(int tagger=-1, int jetcoll=-1, int wpoint=-1, int label=-1)
std::map< std::string, Data > WPoint
Definition CDIReader.h:43
std::vector< std::string > Labels
Definition CDIReader.h:41
bool m_initialized
flag whether the initialization has been carried out
Definition CDIReader.h:100
void crawlCDI(TDirectoryFile *parentDir, int depth=0, const std::string &metamap="")
Definition CDIReader.cxx:35
std::string depth
tag string for intendation
Definition fastadd.cxx:46
bool verbose
Definition hcg.cxx:73
std::string label(const std::string &format, int i)
Definition label.h:19
The namespace of all packages in PhysicsAnalysis/JetTagging.