11#include <nlohmann/json.hpp>
13using json = nlohmann::json;
18 std::cout <<
"Analysis::CDIReader::CDIReader: " <<
m_cdipath <<
" could not be opened."<<std::endl;
22 m_CDIFile->GetObject(
"VersionInfo/BuildNumber", s);
24 std::cout <<
" CDI file build number: " << s->GetName() << std::endl;
26 TList* taggerkeys =
m_CDIFile->GetListOfKeys();
27 for (
const auto tagger : *taggerkeys){
29 const std::string taggername{tagger->GetName()};
30 if(taggername !=
"VersionInfo"){
32 auto* taggerDir =
static_cast<TDirectoryFile*
>(
m_CDIFile->Get(taggername.c_str()));
42 TList* keys = parentDir->GetListOfKeys();
47 TList* labelkeys = parentDir->GetListOfKeys();
50 std::set<std::string> DSID_set;
51 std::set<std::string> systematics_set;
54 std::string taggername = path.at(0);
55 std::string jetcollname = path.at(1);
56 std::string workingpointname = path.at(2);
61 for(
const auto label : *labelkeys){
62 std::string labelname =
label->GetName();
63 if(labelname ==
"cutvalue" || labelname.find(
"fraction") != std::string::npos)
continue;
65 theseLabels.push_back(labelname);
68 TDirectoryFile* flavourDir = (TDirectoryFile*)parentDir->Get(labelname.c_str());
71 TList* DSIDkeys = flavourDir->GetListOfKeys();
72 for(
const auto CDHistCont : *DSIDkeys){
73 std::string DSIDname = CDHistCont->GetName();
74 DSID_set.insert(DSIDname);
76 if(DSIDname ==
"default_SF"){
78 std::string dir = taggername +
"/" + jetcollname +
"/" + workingpointname +
"/" + labelname +
"/default_SF";
82 std::cout <<
"No default_SF CalibrationDataHistogramContainer?" << std::endl;
85 for(
const std::string &s : uncertainties){
86 systematics_set.insert(s);
90 std::string flav_spec_unc_name = labelname +
"_syst";
91 theseData[flav_spec_unc_name] = uncertainties;
95 std::cout <<
"No flavour directory?" << std::endl;
99 std::sort(theseLabels.begin(), theseLabels.end());
100 theseData[
"labels"] = std::move(theseLabels);
102 Labels theseDSIDs(DSID_set.size());
103 std::copy(DSID_set.begin(), DSID_set.end(), theseDSIDs.begin());
104 theseData[
"DSIDs"] = std::move(theseDSIDs);
106 Labels theseSystematics(systematics_set.size());
107 std::copy(systematics_set.begin(), systematics_set.end(), theseSystematics.begin());
108 theseData[
"systematics"] = std::move(theseSystematics);
113 for(
const auto coll: *keys){
114 std::string collname = coll->GetName();
115 if ( collname.find(
"cutvalue") != std::string::npos || collname.find(
"fraction") != std::string::npos)
continue;
118 TDirectoryFile* collDir = (TDirectoryFile*)parentDir->Get(collname.c_str());
119 if(collDir && collname !=
"VersionInfo"){
120 std::string nextmap = metamap +
";" + collname;
123 std::cout <<
"No collection directory?" << std::endl;
151 int current_tagger = (tagger < 0) ? -1 : 0;
152 int current_jetcoll = (jetcoll < 0) ? -1 : 0;
153 int current_wpoint = (wpoint < 0) ? -1 : 0;
154 int current_label = (
label < 0) ? -1 : 0;
157 if(current_tagger != -1) current_tagger += 1;
158 if(tagger != current_tagger || tagger == 0)
continue;
160 std::cout <<
"| " << tag << std::endl;
162 for (
const auto& [
jet, wps] : jets){
163 if(current_jetcoll != -1) current_jetcoll += 1;
164 if(jetcoll != current_jetcoll || jetcoll == 0)
continue;
166 std::cout <<
"|\\__ " <<
jet << std::endl;
168 int num_wps = wps.size();
170 for(
const auto& [wp, labels] : wps){
171 if(current_wpoint != -1) current_wpoint += 1;
172 if(wpoint != current_wpoint || wpoint == 0)
continue;
174 if(num_wp_seen != num_wps){
175 std::cout <<
"| |\\__" << wp << std::endl;
177 std::cout <<
"| \\__" << wp << std::endl;
182 for(
const std::string& l : d[
"labels"]){
183 if(current_label != -1) current_label += 1;
184 if(
label != current_label ||
label == 0)
continue;
185 if(num_wp_seen != num_wps && label_index == 0){
186 std::cout <<
"| | \\___" <<
" (" << label_index <<
") " << l << std::endl;
187 }
else if(label_index != 0 && num_wp_seen != num_wps) {
188 std::cout <<
"| | \\___" <<
" (" << label_index <<
") " << l << std::endl;
190 std::cout <<
"| \\___" <<
" (" << label_index <<
") " << l << std::endl;
203 bool configured =
false;
215 if(tag.first == tagger)
break;
221 if(
jet.first == jetcoll)
break;
225 for(
const auto& wpoint :
m_metadata[tagger][jetcoll]){
227 if(wpoint.first == wp)
break;
229 if (
verbose) std::cout <<
" Your configuration looks good! Available labels are : " << std::endl;
232 for(
const std::string& flavour_label :
m_metadata[tagger][jetcoll][wp][
"labels"]){
239 if (
verbose) std::cout <<
"Couldn't find \"" << wp <<
"\" for " << tagger <<
" / " << jetcoll <<
" in this CDI file!" << std::endl;
240 if (
verbose) std::cout <<
"Here are your options :" << std::endl;
244 if (
verbose) std::cout <<
"Couldn't find \"" << jetcoll <<
"\" under " << tagger <<
" in this CDI file!" << std::endl;
245 if (
verbose) std::cout <<
"Here are your options :" << std::endl;
249 if (
verbose) std::cout <<
"Couldn't find \"" << tagger <<
"\" in this CDI file" << std::endl;
250 if (
verbose) std::cout <<
"Here are your options :" << std::endl;
258 std::filesystem::path
cwd = std::filesystem::current_path();
259 std::filesystem::path filepath =
cwd /
"CDI.json";
260 std::ofstream output(filepath);
261 output << std::setw(4) << json_metadata << std::endl;
271 std::cout <<
" CDIReader :: You need to validate your configuration before working with (flavour) labels!" << std::endl;
274 if (tagger.empty() || jetcollection.empty() || workingpoint.empty()){
278 DSIDs = std::move(DSID_vec);
281 std::cout <<
" The tagger [" << tagger <<
"] doesn't exist in this CDI file!" << std::endl;
284 std::cout <<
" The jet collection [" << jetcollection <<
"] doesn't exist in " << tagger <<
" this CDI file!" << std::endl;
287 std::cout <<
" The working point [" << workingpoint <<
"] doesn't exist in " << tagger <<
"/" << jetcollection <<
" this CDI file!" << std::endl;
289 for (
const std::string& DSID :
m_metadata[tagger][jetcollection][workingpoint][
"DSIDs"]){
290 DSIDs.push_back(DSID);
298 std::cout <<
" CDIReader :: You need to validate your configuration before working with (flavour) labels!" << std::endl;
301 if (tagger.empty() || jetcollection.empty() || workingpoint.empty()){
305 std::cout <<
" The tagger [" << tagger <<
"] doesn't exist in this CDI file!" << std::endl;
307 std::cout <<
" The jet collection [" << jetcollection <<
"] doesn't exist in " << tagger <<
" this CDI file!" << std::endl;
309 std::cout <<
" The working point [" << workingpoint <<
"] doesn't exist in " << tagger <<
"/" << jetcollection <<
" this CDI file!" << std::endl;
311 for (
const std::string&
label :
m_metadata[tagger][jetcollection][workingpoint][
"labels"]){
312 labels.push_back(
label);
324 std::cout <<
" The tagger [" << tagger <<
"] doesn't exist in this CDI file!" << std::endl;
328 jetcolls.push_back(
jet.first);
337 if (tagger.empty() || jetcollection.empty()){
341 std::cout <<
" The tagger [" << tagger <<
"] doesn't exist in this CDI file!" << std::endl;
343 std::cout <<
" The jet collection [" << jetcollection <<
"] doesn't exist in " << tagger <<
" this CDI file!" << std::endl;
345 for(
const auto& wp :
m_metadata[tagger][jetcollection]){
346 wps.push_back(wp.first);
void record_metadata(const std::string &datum, int depth=0)
Labels getDSIDs(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
Labels getLabels(const std::string &tagger="", const std::string &jetcollection="", const std::string &workingpoint="")
std::unique_ptr< TFile > m_CDIFile
std::set< std::string > m_taggers
std::set< std::string > m_DSIDs
void record_metadata_map(const Data &data, const std::string &path)
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)
std::set< std::string > m_workingpoints
Labels getJetCollections(const std::string &tagger)
std::set< std::string > m_jetcollections
CDIReader(const std::string &cdipath, bool verbose=false)
normal constructor.
std::set< std::string > m_labels
std::map< std::string, Labels > Data
void printMetadata(int tagger=-1, int jetcoll=-1, int wpoint=-1, int label=-1)
std::vector< std::string > Labels
bool m_initialized
flag whether the initialization has been carried out
void crawlCDI(TDirectoryFile *parentDir, int depth=0, const std::string &metamap="")
std::vector< std::string > listUncertainties() const
retrieve the list of "uncertainties" accessible to this object.
This is the class holding information for histogram-based calibration results.
std::string depth
tag string for intendation
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
std::vector< std::string > split(const std::string &s, const std::string &t=":")
std::string label(const std::string &format, int i)
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.