5 #pragma GCC diagnostic ignored "-Wfloat-conversion"
6 #pragma GCC diagnostic ignored "-Wconversion"
20 constexpr Color_t
Colours[
NVars] = {kRed, kGreen+1, kBlack, kBlue+1, kOrange+1};
21 constexpr ELineStyle
Style[
NVars] = {kDotted, kDashed, kSolid, kDashDotted, kDashDotted};
24 void ScanInput(std::vector<std::string>& DialNameVecr,
25 std::vector<std::string>& SampleNameVec,
26 std::vector<double>& SigmaValues,
27 const std::string& filename)
31 TDirectoryFile *SigmaDir = infile->Get<TDirectoryFile>(
"SigmaVar");
34 TIter next(SigmaDir->GetListOfKeys());
38 while ((key =
static_cast<TKey*
>(next()))) {
40 auto classname = std::string(key->GetClassName());
41 auto dirname = std::string(key->GetName());
43 if (classname !=
"TDirectoryFile")
continue;
44 dirname = std::string(key->GetName());
46 SigmaDir->cd(dirname.c_str());
47 TIter nextsub(gDirectory->GetListOfKeys());
48 TKey *subkey =
nullptr;
50 DialNameVecr.push_back(dirname);
53 if(SampleNameVec.size() != 0)
continue;
55 while ((subkey =
static_cast<TKey*
>(nextsub())))
57 auto subdirname = std::string(subkey->GetName());
58 SampleNameVec.push_back(subdirname);
61 if(SigmaValues.size() != 0)
continue;
62 SigmaDir->cd((dirname +
"/" + subdirname).c_str());
64 TKey *subsubkey =
nullptr;
65 TIter nextsubsub(gDirectory->GetListOfKeys());
66 while ((subsubkey =
static_cast<TKey*
>(nextsubsub())))
68 auto subsubdirname = std::string(subsubkey->GetTitle());
70 std::string histname = subsubdirname;
72 auto StripPrefix = [](std::string& str,
const std::string& prefix) {
73 if (str.find(prefix +
"_") == 0) {
74 str.erase(0, prefix.length() + 1);
75 if (str.find(prefix) == 0) {
76 MACH3LOG_ERROR(
"Failed to strip prefix '{}' from string '{}'", prefix, str);
80 MACH3LOG_ERROR(
"String '{}' does not start with expected prefix '{}'", str, prefix);
85 StripPrefix(histname, subdirname);
86 StripPrefix(histname, dirname);
92 size_t sig_pos = histname.find(
"_sig_");
94 std::string sigma_part = histname.substr(sig_pos + 5);
97 if (histname.find(
"nom_") != std::string::npos) {
99 PriorKnot =
static_cast<int>(SigmaValues.size());
101 }
else if (sigma_part.size() > 0 && sigma_part[0] ==
'n') {
102 sigma = -std::stod(sigma_part.substr(1));
103 }
else if (sigma_part.size() > 0 && sigma_part[0] ==
'p') {
104 sigma = std::stod(sigma_part.substr(1));
111 SigmaValues.push_back(sigma);
117 MACH3LOG_ERROR(
"Didn't find prior knot, something is not right...");
131 bool SkipDirectory(
const std::vector<std::string>& ExcludeString,
const std::vector<std::string>& IncludeString,
const std::string& dirname)
134 for(
unsigned int i = 0; i < ExcludeString.size(); i++)
136 if (dirname.find(ExcludeString[i]) != std::string::npos){ Skip =
true;
break; }
138 for(
unsigned int i = 0; i < IncludeString.size(); i++)
140 if (!(dirname.find(IncludeString[i]) != std::string::npos)){ Skip =
true;
break; }
145 std::vector<double>
GetDialValues(
const std::vector<std::unique_ptr<TH1D>>& Poly) {
146 std::vector<double> values;
147 for (
const auto& hist : Poly) {
148 std::string title = hist->GetTitle();
149 auto pos = title.rfind(
"_val_");
150 if (pos != std::string::npos) {
151 std::string val_str = title.substr(pos + 5);
152 double val = std::stod(val_str);
153 values.push_back(val);
154 MACH3LOG_DEBUG(
"Extracted dial value {} from title '{}'", val, title);
156 MACH3LOG_DEBUG(
"Failed to extract dial value from title '{}'", title);
163 void PlotRatio(
const std::vector<std::unique_ptr<TH1D>>& Poly,
164 const std::unique_ptr<TCanvas>&
canv,
165 const std::string& Title,
166 const std::string& outfilename)
170 TPad* pad1 =
new TPad(
"pad1",
"pad1",0.,0.25,1.,1.0);
172 TPad* pad2 =
new TPad(
"pad2",
"pad2",0.,0.,1.,0.25);
175 pad1->SetLeftMargin(
canv->GetLeftMargin());
176 pad1->SetRightMargin(
canv->GetRightMargin());
177 pad1->SetTopMargin(
canv->GetTopMargin());
178 pad1->SetBottomMargin(0);
180 pad2->SetLeftMargin(
canv->GetLeftMargin());
181 pad2->SetRightMargin(
canv->GetRightMargin());
182 pad2->SetTopMargin(0);
183 pad2->SetBottomMargin(0.30);
192 for(
int ik = 0; ik < static_cast<int>(
sigmaArray.size()); ++ik)
194 Poly[ik]->SetLineWidth(2.);
195 Poly[ik]->SetLineColor(
Colours[ik]);
196 Poly[ik]->SetLineStyle(
Style[ik]);
198 max = std::max(max, Poly[ik]->GetMaximum());
200 Poly[0]->SetTitle(Title.c_str());
201 Poly[0]->SetMaximum(max*1.2);
202 Poly[0]->Draw(
"HIST");
203 for(
int ik = 1; ik < static_cast<int>(
sigmaArray.size()); ++ik)
205 Poly[ik]->Draw(
"HIST SAME");
208 std::vector<double> Integral(
sigmaArray.size());
209 for(
int ik = 0; ik < static_cast<int>(
sigmaArray.size()); ++ik)
210 Integral[ik] = Poly[ik]->Integral();
212 auto leg = std::make_unique<TLegend>(0.55, 0.55, 0.8, 0.88);
213 leg->SetTextSize(0.04);
214 for (
int j = 0; j < static_cast<int>(
sigmaArray.size()); j++)
217 leg->AddEntry(Poly[j].get(), Form(
"Prior (%.2f), #int=%.2f", DialValues[j], Integral[j]),
"l");
219 leg->AddEntry(Poly[j].get(), Form(
"%.0f#sigma (%.2f), #int=%.2f",
sigmaArray[j], DialValues[j], Integral[j]),
"l");
222 leg->SetLineColor(0);
223 leg->SetLineStyle(0);
224 leg->SetFillColor(0);
225 leg->SetFillStyle(0);
226 leg->SetBorderSize(0);
231 auto line = std::make_unique<TLine>(Poly[0]->GetXaxis()->GetBinLowEdge(Poly[0]->GetXaxis()->GetFirst()), 1.0, Poly[0]->GetXaxis()->GetBinUpEdge(Poly[0]->GetXaxis()->GetLast()), 1.0);
232 std::vector<std::unique_ptr<TH1D>> Ratio(
sigmaArray.size()-1);
234 size_t ratio_index = 0;
235 for (
int i = 0; i < static_cast<int>(Poly.size()); ++i) {
237 Ratio[ratio_index] =
M3::Clone(Poly[i].get());
238 Ratio[ratio_index]->Divide(Poly[
PriorKnot].get());
242 Ratio[0]->GetYaxis()->SetTitle(
"Ratio to Prior");
243 Ratio[0]->SetBit(TH1D::kNoTitle);
244 Ratio[0]->GetXaxis()->SetTitleSize(0.12);
245 Ratio[0]->GetYaxis()->SetTitleOffset(0.4);
246 Ratio[0]->GetYaxis()->SetTitleSize(0.10);
248 Ratio[0]->GetXaxis()->SetLabelSize(0.10);
249 Ratio[0]->GetYaxis()->SetLabelSize(0.10);
251 Ratio[0]->SetBit(TH1D::kNoTitle);
255 for (
int j = 0; j < static_cast<int>(
sigmaArray.size())-1; j++)
257 for (
int i = 1; i < Ratio[0]->GetXaxis()->GetNbins(); i++)
259 maxz = std::max(maxz, Ratio[j]->GetBinContent(i));
260 minz = std::min(minz, Ratio[j]->GetBinContent(i));
266 if (std::fabs(1 - maxz) > std::fabs(1-minz))
267 Ratio[0]->GetYaxis()->SetRangeUser(1-std::fabs(1-maxz),1+std::fabs(1-maxz));
269 Ratio[0]->GetYaxis()->SetRangeUser(1-std::fabs(1-minz),1+std::fabs(1-minz));
271 Ratio[0]->Draw(
"HIST");
273 for(
int ik = 1; ik < static_cast<int>(
sigmaArray.size())-1; ++ik)
275 Ratio[ik]->Draw(
"HIST SAME");
278 line->SetLineWidth(2);
279 line->SetLineColor(kBlack);
282 canv->Print((outfilename).c_str());
291 auto canvas = std::make_unique<TCanvas>(
"canv",
"canv", 1080, 1080);
293 TDirectoryFile *SigmaDir = infile->Get<TDirectoryFile>(
"SigmaVar");
295 std::string outfilename = filename.substr(0, filename.find(
".root"));
296 outfilename = outfilename +
"_RatioPlots1d.pdf";
297 gErrorIgnoreLevel = kWarning;
298 gStyle->SetDrawBorder(0);
299 gStyle->SetTitleBorderSize(2);
300 gStyle->SetOptStat(0);
302 canvas->SetTopMargin(0.10);
303 canvas->SetBottomMargin(0.08);
304 canvas->SetRightMargin(0.05);
305 canvas->SetLeftMargin(0.12);
306 canvas->Print((outfilename+
"[").c_str());
308 auto IncludeString = GetFromManager<std::vector<std::string>>(Settings[
"IncludeString"], {});
309 auto ExcludeString = GetFromManager<std::vector<std::string>>(Settings[
"ExcludeString"], {});
310 TDirectory *dir =
nullptr;
324 std::vector<std::unique_ptr<TH1D>> Projection;
325 TIter nextsub(dir->GetListOfKeys());
326 TKey *subsubkey =
nullptr;
329 while ((subsubkey =
static_cast<TKey*
>(nextsub())))
331 auto name = std::string(subsubkey->GetName());
332 auto classname = std::string(subsubkey->GetClassName());
334 if (classname ==
"TH1D")
337 Projection.emplace_back(
M3::Clone(SigmaDir->Get<TH1D>(name.c_str())));
341 PlotRatio(Projection, canvas, Title, outfilename);
342 gDirectory->cd(
"..");
346 canvas->Print((outfilename+
"]").c_str());
351 int main(
int argc,
char **argv)
356 MACH3LOG_ERROR(
"Need two inputs: output of sigma var and config");
359 std::string filename = argv[1];
360 std::string ConfigName = argv[2];
361 MACH3LOG_INFO(
"Running {} with {} {}", argv[0], filename, ConfigName);
366 YAML::Node settings = Config[
"PlotSigmaVariation"];
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
void PlotRatio(const std::vector< std::unique_ptr< TH1D >> &Poly, const std::unique_ptr< TCanvas > &canv, const std::string &Title, const std::string &outfilename)
constexpr ELineStyle Style[NVars]
int main(int argc, char **argv)
constexpr Color_t Colours[NVars]
std::vector< double > GetDialValues(const std::vector< std::unique_ptr< TH1D >> &Poly)
void CompareSigVar1D(const std::string &filename, const YAML::Node &Settings)
std::vector< std::string > DialNameVector
std::vector< std::string > SampleNameVector
std::vector< double > sigmaArray
constexpr const int NVars
void ScanInput(std::vector< std::string > &DialNameVecr, std::vector< std::string > &SampleNameVec, std::vector< double > &SigmaValues, const std::string &filename)
Scan inputs to figure out dial name and used sample names.
bool SkipDirectory(const std::vector< std::string > &ExcludeString, const std::vector< std::string > &IncludeString, const std::string &dirname)
Check whether to skip directory or not based on defined strings.
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Custom exception class for MaCh3 errors.
std::unique_ptr< ObjectType > Clone(const ObjectType *obj, const std::string &name="")
KS: Creates a copy of a ROOT-like object and wraps it in a smart pointer.
TFile * Open(const std::string &Name, const std::string &Type, const std::string &File, const int Line)
Opens a ROOT file with the given name and mode.
constexpr static const int _BAD_INT_
Default value used for int initialisation.