-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootstrap.C
208 lines (174 loc) · 6.46 KB
/
Bootstrap.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* File : Bootstrap.C
* Author : Anton Riedel <[email protected]>
* Date : 27.10.2021
* Last Modified Date: 21.03.2022
* Last Modified By : Anton Riedel <[email protected]>
*/
#include "GridHelperMacros.H"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
#include <vector>
Int_t Bootstrap(const char *FileNameMean, const char *ListOfFiles) {
// load config file
std::fstream ConfigFile("config.json");
nlohmann::json Jconfig = nlohmann::json::parse(ConfigFile);
std::vector<std::string> skipSysCheck =
Jconfig["task"]["SkipSysCheck"].get<std::vector<std::string>>();
// open all files
std::vector<TFile *> Files;
std::fstream FileList(ListOfFiles);
std::string Line;
while (std::getline(FileList, Line)) {
Files.push_back(TFile::Open(Line.c_str(), "READ"));
}
TFile *Mean = new TFile(FileNameMean, "READ");
// get list of all tasks
std::vector<std::string> defaultTasks;
std::vector<std::string> tmp;
std::vector<std::vector<std::string>> syscheckTasks;
TDirectoryFile *tdirFile = dynamic_cast<TDirectoryFile *>(Mean->Get(
Jconfig["task"]["OutputTDirectory"].get<std::string>().c_str()));
std::vector<Double_t> CentralityBinEdges =
Jconfig["task"]["CentralityBinEdges"].get<std::vector<Double_t>>();
Double_t numberOfChecks =
tdirFile->GetListOfKeys()->GetEntries() / (CentralityBinEdges.size() - 1);
for (std::size_t i = 0; i < CentralityBinEdges.size() - 1; i++) {
defaultTasks.push_back(std::string(
tdirFile->GetListOfKeys()->At(i * numberOfChecks)->GetName()));
tmp.clear();
for (int j = i * numberOfChecks + 1; j < (i + 1) * numberOfChecks; j++) {
tmp.push_back(std::string(tdirFile->GetListOfKeys()->At(j)->GetName()));
}
syscheckTasks.push_back(tmp);
}
// get list of all observables
std::vector<std::string> Observables;
TList *FinalResultsList = dynamic_cast<TList *>(
dynamic_cast<TList *>(
tdirFile->Get(tdirFile->GetListOfKeys()->First()->GetName()))
->FindObject("FinalResults"));
for (auto cor : *dynamic_cast<TList *>(
FinalResultsList->FindObject("FinalResultCorrelator"))) {
for (auto dep : *dynamic_cast<TList *>(cor)) {
Observables.push_back(std::string(dep->GetName()));
}
}
for (auto sc : *dynamic_cast<TList *>(
FinalResultsList->FindObject("FinalResultSymmetricCumulant"))) {
for (auto dep : *dynamic_cast<TList *>(sc)) {
Observables.push_back(std::string(dep->GetName()));
}
}
TFile *Output = new TFile("Bootstrap.root", "RECREATE");
TGraphErrors *gstat = nullptr;
TGraphErrors *gsys = nullptr;
TH1 *hist, *tmpHist;
std::vector<TH1 *> hists;
std::vector<Double_t> x = {};
std::vector<Double_t> ex = {};
std::vector<Double_t> y = {};
std::vector<Double_t> ey = {};
Double_t sigma = 0.;
for (std::size_t i = 0; i < defaultTasks.size(); i++) {
for (auto Observable : Observables) {
std::cout << "Working on " << Observable << " in task "
<< defaultTasks.at(i) << std::endl;
// compute statistical error
x.clear();
ex.clear();
y.clear();
ey.clear();
hists.clear();
// load histogram holding all sample mean
hist = dynamic_cast<TH1 *>(
GetObjectFromOutputFile(Mean, defaultTasks.at(i), Observable));
// load histograms holding subsample means
for (auto file : Files) {
hists.push_back(dynamic_cast<TH1 *>(
GetObjectFromOutputFile(file, defaultTasks.at(i), Observable)));
}
// loop over all bins
for (Int_t bin = 1; bin <= hist->GetNbinsX(); bin++) {
x.push_back(hist->GetBinCenter(bin));
ex.push_back(hist->GetBinWidth(bin) / 2.);
if (!std::isnan(hist->GetBinContent(bin))) {
y.push_back(hist->GetBinContent(bin));
sigma = 0.;
for (auto h : hists) {
sigma += (hist->GetBinContent(bin) - h->GetBinContent(bin)) *
((hist->GetBinContent(bin) - h->GetBinContent(bin)));
}
sigma = TMath::Sqrt(sigma / (Files.size() * (Files.size() - 1.)));
ey.push_back(sigma);
} else {
std::cout << "NaN encountered. Keep going..." << std::endl;
y.push_back(0.);
ey.push_back(0.);
}
}
gstat =
new TGraphErrors(x.size(), x.data(), y.data(), ex.data(), ey.data());
gstat->Write((std::string("STAT_") + defaultTasks.at(i) +
std::string("_") + Observable)
.c_str());
delete gstat;
// compute systematical error
x.clear();
ex.clear();
y.clear();
ey.clear();
hists.clear();
// mean already loaded
// load histograms holding systemaical variations
for (std::size_t j = 0; j < syscheckTasks.at(i).size(); j++) {
Bool_t flag = kTRUE;
for (auto skip : skipSysCheck) {
if (boost::contains(syscheckTasks.at(i).at(j), skip)) {
flag = kFALSE;
std::cout << "Skipping systematic check: "
<< syscheckTasks.at(i).at(j) << std::endl;
}
}
if (flag) {
hists.push_back(dynamic_cast<TH1 *>(GetObjectFromOutputFile(
Mean, syscheckTasks.at(i).at(j), Observable)));
}
}
// loop over all bins
for (Int_t bin = 1; bin <= hist->GetNbinsX(); bin++) {
x.push_back(hist->GetBinCenter(bin));
ex.push_back(hist->GetBinWidth(bin) / 2.);
if (!std::isnan(hist->GetBinContent(bin))) {
y.push_back(hist->GetBinContent(bin));
sigma = 0.;
for (auto h : hists) {
sigma += (hist->GetBinContent(bin) - h->GetBinContent(bin)) *
((hist->GetBinContent(bin) - h->GetBinContent(bin)));
}
sigma = TMath::Sqrt(sigma);
ey.push_back(sigma);
} else {
std::cout << "NaN encountered. Keep going..." << std::endl;
y.push_back(0.);
ey.push_back(0.);
}
}
gsys =
new TGraphErrors(x.size(), x.data(), y.data(), ex.data(), ey.data());
gsys->Write((std::string("SYS_") + defaultTasks.at(i) + std::string("_") +
Observable)
.c_str());
delete gsys;
}
}
Output->Close();
return 0;
}