129{
130 auto canvas = std::make_unique<TCanvas>("canvas", "canvas", 0, 0, 1024, 1024);
131 canvas->SetGrid();
132 canvas->SetTickx();
133 canvas->SetTicky();
134
135 canvas->SetBottomMargin(0.1f);
136 canvas->SetTopMargin(0.02f);
137 canvas->SetRightMargin(0.08f);
138 canvas->SetLeftMargin(0.15f);
139
140 gStyle->SetOptTitle(0);
141 gStyle->SetOptStat(0);
142 gStyle->SetPalette(51);
143
145
146
147 TChain* Chain = new TChain("posteriors","");
148 Chain->Add(inputFile.c_str());
149
150
151 TObjArray* brlis = Chain->GetListOfBranches();
152
153
154 int nBranches = brlis->GetEntries();
155 int RelevantBranches = 0;
156 for (int i = 0; i < nBranches; i++)
157 {
158
159 TBranch* br = static_cast<TBranch*>(brlis->At(i));
160 if(!br){
163 }
164 TString bname = br->GetName();
165
166
167 if(bname.BeginsWith("xsec_"))
168 {
170 RelevantBranches++;
171 }
172 }
173
174
175 Chain->SetBranchStatus("*", false);
176
177 std::vector<double> fParProp(RelevantBranches);
178
179 for (int i = 0; i < RelevantBranches; ++i)
180 {
181 Chain->SetBranchStatus(
BranchNames[i].Data(),
true);
182 Chain->SetBranchAddress(
BranchNames[i].Data(), &fParProp[i]);
183 }
184
185 YAML::Node Settings = YAML::LoadFile(configFile.c_str());
186
187 std::vector<std::string> SetsNames;
188 std::vector<std::vector<std::string>> RemoveNames;
189 std::vector<bool> Exclude;
190 std::vector<std::string> FancyTittle;
191
192 std::vector<std::vector<bool>> isRelevantParam;
193 std::vector<std::string> node = Settings["GetPenaltyTerm"]["PenaltySets"].as<std::vector<std::string>>();
194 for (unsigned int i = 0; i < node.size(); i++)
195 {
196 std::string ParName = node[i];
197 SetsNames.push_back(ParName);
198
199 const auto& Set = Settings["GetPenaltyTerm"][ParName];
200
201 RemoveNames.push_back(Set[0].as<std::vector<std::string>>());
202 Exclude.push_back(Set[1].as<bool>());
203 FancyTittle.push_back(Set[2].as<std::string>());
204 }
205
206 const int NSets = int(SetsNames.size());
207
208 isRelevantParam.resize(NSets);
209
210 for(int i = 0; i < NSets; i++)
211 {
212 isRelevantParam[i].resize(
size);
213 int counter = 0;
214
215 for (
int j = 0; j <
size; j++)
216 {
217 isRelevantParam[i][j] = false;
218
219
220 if(Exclude[i])
221 {
222 bool found = false;
223 for (unsigned int k = 0; k < RemoveNames[i].size(); k++)
224 {
225 if (
ParamNames[j].rfind(RemoveNames[i][k], 0) == 0)
226 {
227 found = true;
228 }
229 }
230 if(!found)
231 {
232 isRelevantParam[i][j] = true;
233 counter++;
234 }
235 }
236
237 else
238 {
239 for (unsigned int k = 0; k < RemoveNames[i].size(); k++)
240 {
241 if (
ParamNames[j].rfind(RemoveNames[i][k], 0) == 0)
242 {
243 isRelevantParam[i][j] = true;
244 counter++;
245 break;
246 }
247 }
248 }
249 }
250 MACH3LOG_INFO(
" Found {} params for set {}", counter, SetsNames[i]);
251 }
252
253 int AllEvents = int(Chain->GetEntries());
254 std::vector<std::unique_ptr<TH1D>> hLogL(NSets);
255 for (int i = 0; i < NSets; i++) {
256 std::string NameTemp = "LogL_" + SetsNames[i];
257 hLogL[i] = std::make_unique<TH1D>(NameTemp.c_str(), NameTemp.c_str(), AllEvents, 0, AllEvents);
258 hLogL[i]->SetLineColor(kBlue);
259 }
260 std::vector<double> logL(NSets, 0.0);
261 for(int n = 0; n < AllEvents; ++n)
262 {
264
265 Chain->GetEntry(n);
266
267 for(int k = 0; k < NSets; ++k) logL[k] = 0.;
268#ifdef MULTITHREAD
269
270 double *logL_private = nullptr;
271
272
273
274 #pragma omp parallel private(logL_private)
275 {
276 logL_private = new double[NSets]();
277 for(int k = 0; k < NSets; ++k) logL_private[k] = 0.;
278
279 #pragma omp for
280 for (
int i = 0; i <
size; i++)
281 {
282 for (int j = 0; j <= i; ++j)
283 {
284
286 {
287 for(int k = 0; k < NSets; ++k)
288 {
289
290 if (isRelevantParam[k][i] && isRelevantParam[k][j])
291 {
292
293 int scale = 1;
294 if(i != j) scale = 2;
296 }
297 }
298 }
299 }
300 }
301
302 for(int k = 0; k < NSets; ++k)
303 {
304 #pragma omp atomic
305 logL[k] += logL_private[k];
306 }
307
308 delete[] logL_private;
309 }
310
311#else
312 for (
int i = 0; i <
size; i++)
313 {
314 for (int j = 0; j <= i; ++j)
315 {
316
318 {
319 for(int k = 0; k < NSets; ++k)
320 {
321
322 if (isRelevantParam[k][i] && isRelevantParam[k][j])
323 {
324
325 int scale = 1;
326 if(i != j) scale = 2;
328 }
329 }
330 }
331 }
332 }
333#endif
334 for(int k = 0; k < NSets; ++k)
335 {
336 hLogL[k]->SetBinContent(n, logL[k]);
337 }
338 }
339
340
341 std::string OutputName = inputFile + "_PenaltyTerm" +".root";
342 TFile* OutputFile = new TFile(OutputName.c_str(), "recreate");
343 TDirectory *PenaltyTermDir = OutputFile->mkdir("PenaltyTerm");
344
345 canvas->Print(Form("%s_PenaltyTerm.pdf[",inputFile.c_str()), "pdf");
346 for(int i = 0; i < NSets; i++)
347 {
348 const double Maximum = hLogL[i]->GetMaximum();
349 hLogL[i]->GetYaxis()->SetRangeUser(0., Maximum*1.2);
350 hLogL[i]->SetTitle(FancyTittle[i].c_str());
351 hLogL[i]->GetXaxis()->SetTitle("Step");
352 hLogL[i]->GetYaxis()->SetTitle(FancyTittle[i].c_str());
353 hLogL[i]->GetYaxis()->SetTitleOffset(1.4f);
354
355 hLogL[i]->Draw("");
356
357 PenaltyTermDir->cd();
358 hLogL[i]->Write();
359
360 canvas->Print(Form("%s_PenaltyTerm.pdf",inputFile.c_str()), "pdf");
361 }
362 canvas->Print(Form("%s_PenaltyTerm.pdf]",inputFile.c_str()), "pdf");
363 delete Chain;
364
365 for (
int i = 0; i <
size; i++)
366 {
368 }
370 OutputFile->Close();
371 delete OutputFile;
372}
std::vector< double > nominal
std::vector< std::string > ParamNames
void ReadXSecFile(const std::string &inputFile)
std::vector< TString > BranchNames
std::vector< bool > isFlat
Custom exception class for MaCh3 errors.
void PrintProgressBar(const Long64_t Done, const Long64_t All)
KS: Simply print progress bar.