MaCh3  2.8.0
Reference Guide
UmbrellaPlotModule.cpp
Go to the documentation of this file.
2 #include "Manager/Manager.h"
4 
6 #include "TSystem.h"
7 #include "TChain.h"
8 #include "TSystemDirectory.h"
10 
15 
16 //this file has lots of usage of the ROOT plotting interface that only takes floats, turn this warning off for this CU for now
17 #pragma GCC diagnostic ignored "-Wfloat-conversion"
18 #pragma GCC diagnostic ignored "-Wold-style-cast"
19 #pragma GCC diagnostic ignored "-Wunused-parameter"
20 #pragma GCC diagnostic ignored "-Wunused-variable"
21 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
22 #pragma GCC diagnostic ignored "-Wconversion"
23 
24 
25 namespace M3{
27 
29  m_parser = std::make_unique<MaCh3ArgumentParser>("plot", "1.0", argparse::default_arguments::help);
30  m_parser->add_description("Tool for plotting MCMC diagnostic like autocorrelations.");
31  m_parser->add_argument("input-file")
32  .help("Umbrella sampling input ROOT file.")
33  .metavar("INPUT_FILE")
34  .required();
35  m_parser->add_argument("output-dir")
36  .help("Output directory for plots.")
37  .metavar("OUTPUT_DIR")
38  .required();
39  return m_parser.get();
40 }
41 
42 
45  std::string InputFile = m_parser->get<std::string>("input-file");
46  std::string outputDir = m_parser->get<std::string>("output-dir");
47 
48  UmbrellaPlotting(InputFile, outputDir);
49  return 0;
50 }
51 
52 void UmbrellaPlotModule::UmbrellaPlotting(std::string InputFile,
53  std::string outputDir) {
54  // ============================================
55  // Configuration: Number of bins for each variable
56  // ============================================
57  int nBins_dcp = 200; // Number of bins for delta_cp
58  int nBins_sin2th23 = 100; // Number of bins for sin^2(theta_23)
59  int nBins_sin2th13 = 100;
60  int nBins_sin2th12 = 100;
61  int nBins_delm23 = 3000; // Number of bins for delta_m23^2
62  int nBins_delm12 = 100;
63 
64  // Unified plotting ranges requested for all sin2th23 and delm23 views.
65  double sin2th23_min = 0.45;
66  double sin2th23_max = 0.57;
67  double sin2th13_min = 0.015;
68  double sin2th13_max = 0.03;
69  double sin2th12_min = 0.24;
70  double sin2th12_max = 0.36;
71  double delm23_no_min = 0.00245;
72  double delm23_no_max = 0.0026;
73  double delm23_io_min = -delm23_no_max;
74  double delm23_io_max = -delm23_no_min;
75  double delm12_min = 6.5e-5;
76  double delm12_max = 8.5e-5;
77 
78  auto normalizeHist = [&](TH1 *h) {
79  if (!h) return;
80  double integral = h->Integral();
81  if (integral > 0.0) h->Scale(1.0 / integral);
82  };
83 
84  auto printHistStats = [&](const std::string &label, TH1 *h) {
85  if (!h) {
86  std::cout << "[DEBUG] " << label << " : histogram is null" << std::endl;
87  return;
88  }
89  std::cout << std::fixed << std::setprecision(6)
90  << "[DEBUG] " << label
91  << " entries=" << h->GetEntries()
92  << " effEntries=" << h->GetEffectiveEntries()
93  << " integral=" << h->Integral()
94  << " mean=" << h->GetMean()
95  << " rms=" << h->GetRMS()
96  << std::endl;
97  };
98 
99  TFile *f = TFile::Open(InputFile.c_str());
100 
101  // ============================================
102  // Open the ROOT file
103  // ===========================================
104  // T2K regerence posterior input
105  //std::string referenceFile = "/home/driley/projects/def-blairt2k/driley/T2K_outputs/reduce_AsimovA22Fit_070723_FinalChain.root";
106  //std::string referenceTreeName = "osc_posteriors";
107  bool use_reference_chain = false;
108  bool use_reference_LLH_scan = true;
109 
110  double asimovSin2th12 = std::numeric_limits<double>::quiet_NaN();
111  double asimovSin2th23 = std::numeric_limits<double>::quiet_NaN();
112  double asimovSin2th13 = std::numeric_limits<double>::quiet_NaN();
113  double asimovDelm12 = std::numeric_limits<double>::quiet_NaN();
114  double asimovDelm23 = std::numeric_limits<double>::quiet_NaN();
115  double asimovDcp = std::numeric_limits<double>::quiet_NaN();
116  double asimovBaseline = std::numeric_limits<double>::quiet_NaN();
117  double asimovElectronDensity = std::numeric_limits<double>::quiet_NaN();
118 
119  // load in reference chain
120  std::string referenceFile = "";
121  std::string referenceTreeName = "";
122 
123  if(use_reference_chain) {
124  referenceFile = "/home/driley/projects/def-blairt2k/driley/T2K_outputs/umbrella_testing/fixing_params/allfixed/all_oscParams_fixed_hadded.root";
125  referenceTreeName = "posteriors";
126  }
127 
128  // setup reference LLH scan input (works best when fit is only over the required variables)
129  std::string referenceLLHScanFile = "";
130  std::string referenceLLHScanDcpHistName = "";
131  std::string referenceLLHScanDcp_NO_histName = "";
132  std::string referenceLLHScanDcp_IO_histName = "";
133 
134  if(use_reference_LLH_scan) {
135  referenceLLHScanFile = "/home/driley/Projects-T2K-MaCh3/umbrella_t2k/mcm_utils/target_1D_histos.root";
136  referenceLLHScanDcpHistName = "dcp";
137  referenceLLHScanDcp_NO_histName = "dcp_NO";
138  referenceLLHScanDcp_IO_histName = "dcp_IO";
139  }
140 
141  // begin loading in fit file
142  if (!f || f->IsZombie()) {
143  std::cerr << "Error: Cannot open file!" << std::endl;
144  return;
145  }
146 
147  // Get the tree
148  TTree *tree = (TTree*)f->Get("posteriors"); // Update with your tree name
149  if (!tree) {
150  std::cerr << "Error: Cannot find tree!" << std::endl;
151  return;
152  }
153 
154  // Read Asimov metadata from MaCh3_Config YAML in the raw chain file.
155  // this is for plotting of asimov point
156  bool isAsimovChain = false;
157 
158  auto calculateJarlskog = [&](double sin2th12, double sin2th23, double sin2th13, double dcp) {
159  if (!std::isfinite(sin2th12) || !std::isfinite(sin2th23) || !std::isfinite(sin2th13) || !std::isfinite(dcp)) {
160  return std::numeric_limits<double>::quiet_NaN();
161  }
162  return std::sqrt(std::max(0.0, sin2th12) * std::max(0.0, 1.0 - sin2th12) *
163  std::max(0.0, sin2th23) * std::max(0.0, 1.0 - sin2th23)) *
164  std::max(0.0, 1.0 - sin2th13) * std::sqrt(std::max(0.0, sin2th13)) *
165  std::sin(dcp);
166  };
167 
168  const double asimovJarlskog = calculateJarlskog(asimovSin2th12, asimovSin2th23, asimovSin2th13, asimovDcp);
169 
170  auto drawAsimovLineWithLegend = [&](double xValue, TLegend *existingLegend = nullptr) {
171  if (!isAsimovChain || !std::isfinite(xValue) || !gPad) return;
172  gPad->Update();
173  const double yMin = gPad->GetUymin();
174  const double yMax = gPad->GetUymax();
175  TLine *asimovLine = new TLine(xValue, yMin, xValue, yMax);
176  asimovLine->SetLineColor(kBlack);
177  asimovLine->SetLineStyle(3);
178  asimovLine->SetLineWidth(2);
179  asimovLine->Draw("SAME");
180 
181  if (existingLegend) {
182  existingLegend->AddEntry(asimovLine, "Asimov point", "l");
183  existingLegend->Draw();
184  } else {
185  TLegend *asimovLegend = new TLegend(0.14, 0.83, 0.34, 0.91);
186  asimovLegend->SetBorderSize(0);
187  asimovLegend->SetFillStyle(0);
188  asimovLegend->AddEntry(asimovLine, "Asimov point", "l");
189  asimovLegend->Draw();
190  }
191  };
192 
193  auto drawAsimovMarkerWithLegend = [&](double xValue, double yValue, TLegend *existingLegend = nullptr) {
194  if (!isAsimovChain || !std::isfinite(xValue) || !std::isfinite(yValue) || !gPad) return;
195  gPad->Update();
196  // small cross sized relative to the axis ranges
197  const double xMin = gPad->GetUxmin();
198  const double xMax = gPad->GetUxmax();
199  const double yMin = gPad->GetUymin();
200  const double yMax = gPad->GetUymax();
201  const double dx = 0.012 * (xMax - xMin);
202  const double dy = 0.012 * (yMax - yMin);
203 
204  TLine *l1 = new TLine(xValue - dx, yValue - dy, xValue + dx, yValue + dy);
205  l1->SetLineColor(kRed);
206  l1->SetLineWidth(2);
207  l1->Draw("SAME");
208 
209  TLine *l2 = new TLine(xValue - dx, yValue + dy, xValue + dx, yValue - dy);
210  l2->SetLineColor(kRed);
211  l2->SetLineWidth(2);
212  l2->Draw("SAME");
213 
214  if (existingLegend) {
215  existingLegend->AddEntry(l1, "Asimov point", "l");
216  existingLegend->Draw();
217  } else {
218  TLegend *lm = new TLegend(0.14, 0.83, 0.34, 0.91);
219  lm->SetBorderSize(0);
220  lm->SetFillStyle(0);
221  lm->AddEntry(l1, "Asimov point", "l");
222  lm->Draw();
223  }
224  };
225 
226 
227  // Build reference histograms from osc_posteriors for direct overlay.
228  bool hasReferenceDcp = false;
229  bool hasReferenceSin2Th23 = false;
230  bool hasReferenceDelm23 = false;
231  TH1D *hRefDcpShape = nullptr;
232  TH1D *hRefSin2Th23Shape = nullptr;
233  TH1D *hRefDelm23Shape = nullptr;
234  TFile *fRef = TFile::Open(referenceFile.c_str());
235  if (!fRef || fRef->IsZombie()) {
236  std::cerr << "Warning: Cannot open reference file: " << referenceFile << std::endl;
237  } else {
238  TTree *refTree = (TTree*)fRef->Get(referenceTreeName.c_str());
239  if (!refTree) {
240  std::cerr << "Warning: Cannot find reference tree: " << referenceTreeName << std::endl;
241  } else if (!refTree->GetBranch("dcp") && !refTree->GetBranch("delta_cp")) {
242  std::cerr << "Warning: dcp branch not found in reference tree." << std::endl;
243  } else if (refTree->GetBranch("delta_cp")) {
244  hRefDcpShape = new TH1D("hRefDcpShape", "", nBins_dcp, -3.1415, 3.1415);
245  refTree->Draw("delta_cp>>hRefDcpShape", "", "goff");
246  hRefDcpShape->SetDirectory(0);
247  hasReferenceDcp = true;
248  std::cout << "Loaded reference dcp histogram from " << referenceTreeName << std::endl;
249  } else {
250  hRefDcpShape = new TH1D("hRefDcpShape", "", nBins_dcp, -3.1415, 3.1415);
251  refTree->Draw("dcp>>hRefDcpShape", "", "goff");
252  hRefDcpShape->SetDirectory(0);
253  hasReferenceDcp = true;
254  std::cout << "Loaded reference dcp histogram from " << referenceTreeName << std::endl;
255  }
256 
257  if (!refTree->GetBranch("theta23") && !refTree->GetBranch("sin2th_23")) {
258  std::cerr << "Warning: theta23 branch not found in reference tree." << std::endl;
259  } else if (refTree->GetBranch("sin2th_23")) {
260  hRefSin2Th23Shape = new TH1D("hRefSin2Th23Shape", "", nBins_sin2th23, sin2th23_min, sin2th23_max);
261  refTree->Draw("sin2th_23>>hRefSin2Th23Shape", "", "goff");
262  hRefSin2Th23Shape->SetDirectory(0);
263  hasReferenceSin2Th23 = true;
264  std::cout << "Loaded reference sin^{2}(theta23) histogram from " << referenceTreeName << std::endl;
265  } else {
266  hRefSin2Th23Shape = new TH1D("hRefSin2Th23Shape", "", nBins_sin2th23, sin2th23_min, sin2th23_max);
267  refTree->Draw("theta23>>hRefSin2Th23Shape", "", "goff");
268  hRefSin2Th23Shape->SetDirectory(0);
269  hasReferenceSin2Th23 = true;
270  std::cout << "Loaded reference sin^{2}(theta23) histogram from " << referenceTreeName << std::endl;
271  }
272 
273  if (!refTree->GetBranch("dm23") && !refTree->GetBranch("delm2_23")) {
274  std::cerr << "Warning: dm23 branch not found in reference tree." << std::endl;
275  } else if (refTree->GetBranch("delm2_23")) {
276  hRefDelm23Shape = new TH1D("hRefDelm23Shape", "", nBins_delm23, delm23_io_min, delm23_no_max);
277  refTree->Draw("delm2_23>>hRefDelm23Shape", "", "goff");
278  TH1D *hRefDelm23IO_dbg = new TH1D("hRefDelm23IO_dbg", "", nBins_delm23, delm23_io_min, delm23_io_max);
279  TH1D *hRefDelm23NO_dbg = new TH1D("hRefDelm23NO_dbg", "", nBins_delm23, delm23_no_min, delm23_no_max);
280  refTree->Draw("delm2_23>>hRefDelm23IO_dbg", "delm2_23 < 0", "goff");
281  refTree->Draw("delm2_23>>hRefDelm23NO_dbg", "delm2_23 > 0", "goff");
282  printHistStats("Reference delm23 IO (raw)", hRefDelm23IO_dbg);
283  printHistStats("Reference delm23 NO (raw)", hRefDelm23NO_dbg);
284  hRefDelm23Shape->SetDirectory(0);
285  normalizeHist(hRefDelm23Shape);
286  hasReferenceDelm23 = true;
287  std::cout << "Loaded reference delm23 histogram from " << referenceTreeName << std::endl;
288  } else {
289  hRefDelm23Shape = new TH1D("hRefDelm23Shape", "", nBins_delm23, delm23_io_min, delm23_no_max);
290  refTree->Draw("dm23>>hRefDelm23Shape", "", "goff");
291  TH1D *hRefDm23IO_dbg = new TH1D("hRefDm23IO_dbg", "", nBins_delm23, delm23_io_min, delm23_io_max);
292  TH1D *hRefDm23NO_dbg = new TH1D("hRefDm23NO_dbg", "", nBins_delm23, delm23_no_min, delm23_no_max);
293  refTree->Draw("dm23>>hRefDm23IO_dbg", "dm23 < 0", "goff");
294  refTree->Draw("dm23>>hRefDm23NO_dbg", "dm23 > 0", "goff");
295  printHistStats("Reference dm23 IO (raw)", hRefDm23IO_dbg);
296  printHistStats("Reference dm23 NO (raw)", hRefDm23NO_dbg);
297  hRefDelm23Shape->SetDirectory(0);
298  normalizeHist(hRefDelm23Shape);
299  hasReferenceDelm23 = true;
300  std::cout << "Loaded reference dm23 histograms from " << referenceTreeName << std::endl;
301  }
302  fRef->Close();
303  }
304 
305  // --------------------------------------------------
306  // Load LLH-scan reference histograms (linear likelihoods)
307  // --------------------------------------------------
308  bool hasReferenceLLH_Dcp = false;
309  bool hasReferenceLLH_Dcp_NO = false;
310  bool hasReferenceLLH_Dcp_IO = false;
311  TH1D *hRefLLH_Dcp = nullptr;
312  TH1D *hRefLLH_Dcp_NO = nullptr;
313  TH1D *hRefLLH_Dcp_IO = nullptr;
314 
315  if (use_reference_LLH_scan && !referenceLLHScanFile.empty()) {
316  TFile *fLLH = TFile::Open(referenceLLHScanFile.c_str());
317  if (!fLLH || fLLH->IsZombie()) {
318  std::cerr << "Warning: Cannot open reference LLH-scan file: " << referenceLLHScanFile << std::endl;
319  } else {
320  // Generic getter that leaves a cloned, detached, normalized histogram
321  auto loadAndNormalize1D = [&](const std::string &name, TH1D *&outHist, bool &outFlag) {
322  TObject *obj = fLLH->Get(name.c_str());
323  if (!obj) {
324  std::cerr << "Warning: LLH-scan histogram '" << name << "' not found in " << referenceLLHScanFile << std::endl;
325  return;
326  }
327  TH1 *htmp = dynamic_cast<TH1*>(obj);
328  if (!htmp) {
329  std::cerr << "Warning: Object '" << name << "' is not a histogram." << std::endl;
330  return;
331  }
332  outHist = (TH1D*)htmp->Clone((std::string("hRefLLH_") + name + "_clone").c_str());
333  outHist->SetDirectory(0);
334  // LLH scans are likelihoods (not log-likelihood) so area-normalize for overlay
335  double I = outHist->Integral();
336  if (I > 0) outHist->Scale(1.0 / I);
337  outFlag = true;
338  printHistStats(std::string("Reference LLH ") + name, outHist);
339  };
340 
341  if (!referenceLLHScanDcpHistName.empty()) loadAndNormalize1D(referenceLLHScanDcpHistName, hRefLLH_Dcp, hasReferenceLLH_Dcp);
342  if (!referenceLLHScanDcp_NO_histName.empty()) loadAndNormalize1D(referenceLLHScanDcp_NO_histName, hRefLLH_Dcp_NO, hasReferenceLLH_Dcp_NO);
343  if (!referenceLLHScanDcp_IO_histName.empty()) loadAndNormalize1D(referenceLLHScanDcp_IO_histName, hRefLLH_Dcp_IO, hasReferenceLLH_Dcp_IO);
344 
345  fLLH->Close();
346  }
347  }
348 
349  bool hasUmbrellaWeight = false;
350  // check if the umbrella weight branch exists
351  if (!tree->GetBranch("umbrella_weight")) {
352  std::cerr << "Warning: umbrella_weight branch not found, plotting as unweighted umbrella" << std::endl;
353  } else {
354  hasUmbrellaWeight = true;
355  }
356 
357  std::cout << "[DEBUG] Chain entries total=" << tree->GetEntries() << std::endl;
358  TH1D *hChainDelm23IO_dbg = new TH1D("hChainDelm23IO_dbg", "", nBins_delm23, delm23_io_min, delm23_io_max);
359  TH1D *hChainDelm23NO_dbg = new TH1D("hChainDelm23NO_dbg", "", nBins_delm23, delm23_no_min, delm23_no_max);
360  tree->Draw("delm2_23>>hChainDelm23IO_dbg", "delm2_23 < 0", "goff");
361  tree->Draw("delm2_23>>hChainDelm23NO_dbg", "delm2_23 > 0", "goff");
362  printHistStats("Chain delm23 IO (raw)", hChainDelm23IO_dbg);
363  printHistStats("Chain delm23 NO (raw)", hChainDelm23NO_dbg);
364 
365  if (hasUmbrellaWeight) {
366  TH1D *hChainDelm23IO_w_dbg = new TH1D("hChainDelm23IO_w_dbg", "", nBins_delm23, delm23_io_min, delm23_io_max);
367  TH1D *hChainDelm23NO_w_dbg = new TH1D("hChainDelm23NO_w_dbg", "", nBins_delm23, delm23_no_min, delm23_no_max);
368  tree->Draw("delm2_23>>hChainDelm23IO_w_dbg", "(delm2_23 < 0) * umbrella_weight", "goff");
369  tree->Draw("delm2_23>>hChainDelm23NO_w_dbg", "(delm2_23 > 0) * umbrella_weight", "goff");
370  printHistStats("Chain delm23 IO (weighted)", hChainDelm23IO_w_dbg);
371  printHistStats("Chain delm23 NO (weighted)", hChainDelm23NO_w_dbg);
372  }
373 
374  // Create canvas
375  TCanvas *c1 = new TCanvas("c1", "Delta CP vs Delta m23", 900, 700);
376  c1->SetRightMargin(0.15);
377  // Create 2D histogram: delta m23 vs delta CP
378  TH2F *h2d = new TH2F("h2d", "#Delta m_{23}^{2} vs #delta_{CP};#delta_{CP};#Delta m_{23}^{2} (eV^{2})",
379  nBins_dcp, -3.14159, 3.14159,
380  nBins_delm23, delm23_io_min, delm23_no_max);
381  // Draw from tree
382  tree->Draw("delm2_23:delta_cp>>h2d", "", "COLZ");
383  normalizeHist(h2d);
384  h2d->Draw("COLZ");
385  // Styling
386  h2d->SetStats(0); // Remove stats box
387  h2d->GetXaxis()->SetTitle("#delta_{CP}");
388  h2d->GetYaxis()->SetTitle("#Delta m_{23}^{2} (eV^{2})");
389  h2d->GetZaxis()->SetTitle("Normalized Entries");
390  //gPad->SetLogz(); // Optional: log scale on z-axis for better visibility
391  c1->Update();
392  // draw Asimov marker and Save plot
393  drawAsimovMarkerWithLegend(asimovDcp, asimovDelm23, nullptr);
394  c1->SaveAs((outputDir + "delm23_vs_dcp.png").c_str());
395 
396  if (hasUmbrellaWeight){
397  tree->Draw("delm2_23:delta_cp>>h2d", "umbrella_weight", "COLZ");
398  normalizeHist(h2d);
399  h2d->Draw("COLZ");
400  c1->Update();
401  drawAsimovMarkerWithLegend(asimovDcp, asimovDelm23, nullptr);
402  c1->SaveAs((outputDir + "delm23_vs_dcp_weighted.png").c_str());
403  }
404 
405 
406  // Create side-by-side canvas for IO (left) and NO (right)
407  TCanvas *c2 = new TCanvas("c2", "Delta m23 IO and NO", 1800, 700);
408  c2->Divide(2, 1);
409 
410  // Left pad: IO
411  c2->cd(1);
412  gPad->SetRightMargin(0.15);
413  // Create 2D histogram: dcp vs delm23
414  TH2F *h2d_IO = new TH2F("h2d_IO", "Inverted Ordering;#Delta m_{23}^{2} (eV^{2});#delta_{CP}",
415  nBins_delm23, delm23_io_min, delm23_io_max, // X-axis: delm23 (IO range)
416  nBins_dcp, -3.1415, 3.1415); // Y-axis: dcp
417  // Draw from tree
418  tree->Draw("delta_cp:delm2_23>>h2d_IO", "delm2_23 < 0", "COLZ");
419  normalizeHist(h2d_IO);
420  h2d_IO->Draw("COLZ");
421  // Styling
422  h2d_IO->SetStats(0);
423  h2d_IO->GetXaxis()->SetTitle("#Delta m_{23}^{2} (eV^{2})");
424  h2d_IO->GetYaxis()->SetTitle("#delta_{CP} / #pi");
425  h2d_IO->GetZaxis()->SetTitle("Normalized Entries");
426  //gPad->SetLogz();
427 
428  // Right pad: NO
429  c2->cd(2);
430  gPad->SetRightMargin(0.15);
431  // Create 2D histogram: dcp vs delm23
432  TH2F *h2d_NO = new TH2F("h2d_NO", "Normal Ordering;#Delta m_{23}^{2} (eV^{2});#delta_{CP}",
433  nBins_delm23, delm23_no_min, delm23_no_max, // X-axis: delm23 (NO range)
434  nBins_dcp, -3.1415, 3.1415); // Y-axis: dcp
435  // Draw from tree
436  tree->Draw("delta_cp:delm2_23>>h2d_NO", "delm2_23 > 0", "COLZ");
437  normalizeHist(h2d_NO);
438  h2d_NO->Draw("COLZ");
439  // Styling
440  h2d_NO->SetStats(0);
441  h2d_NO->GetXaxis()->SetTitle("#Delta m_{23}^{2} (eV^{2})");
442  h2d_NO->GetYaxis()->SetTitle("#delta_{CP} / #pi");
443  h2d_NO->GetZaxis()->SetTitle("Normalized Entries");
444  //gPad->SetLogz();
445 
446  // Update canvas
447  // draw Asimov markers per pad, update and save
448  c2->cd(1);
449  drawAsimovMarkerWithLegend(asimovDelm23, asimovDcp, nullptr);
450  c2->cd(2);
451  drawAsimovMarkerWithLegend(asimovDelm23, asimovDcp, nullptr);
452  c2->Update();
453  c2->SaveAs((outputDir + "delm23_vs_dcp_IO_NO.png").c_str());
454 
455  if (hasUmbrellaWeight){
456  // Left pad: IO weighted
457  c2->cd(1);
458  tree->Draw("delta_cp:delm2_23>>h2d_IO", "(delm2_23 < 0) * umbrella_weight", "COLZ");
459  normalizeHist(h2d_IO);
460  h2d_IO->Draw("COLZ");
461 
462  // Right pad: NO weighted
463  c2->cd(2);
464  tree->Draw("delta_cp:delm2_23>>h2d_NO", "(delm2_23 > 0) * umbrella_weight", "COLZ");
465  normalizeHist(h2d_NO);
466  h2d_NO->Draw("COLZ");
467 
468  // Update canvas
469  // draw Asimov markers per pad, update and save weighted
470  c2->cd(1);
471  drawAsimovMarkerWithLegend(asimovDelm23, asimovDcp, nullptr);
472  c2->cd(2);
473  drawAsimovMarkerWithLegend(asimovDelm23, asimovDcp, nullptr);
474  c2->Update();
475  c2->SaveAs((outputDir + "delm23_vs_dcp_IO_NO_weighted.png").c_str());
476  }
477 
478  // Lambda to calculate HPD contour level for given confidence level (0-1)
479  auto calculateHPDLevel = [](TH2F *hist, double confidenceLevel) -> double {
480  std::vector<double> binContents;
481  for (int i = 1; i <= hist->GetNbinsX(); ++i) {
482  for (int j = 1; j <= hist->GetNbinsY(); ++j) {
483  double content = hist->GetBinContent(i, j);
484  if (content > 0) {
485  binContents.push_back(content);
486  }
487  }
488  }
489 
490  if (binContents.empty()) return 0.0;
491 
492  std::sort(binContents.rbegin(), binContents.rend()); // Sort in descending order
493 
494  double totalSum = 0;
495  for (double val : binContents) totalSum += val;
496 
497  double targetSum = confidenceLevel * totalSum;
498  double runningSum = 0;
499 
500  for (double val : binContents) {
501  runningSum += val;
502  if (runningSum >= targetSum) {
503  return val;
504  }
505  }
506  return binContents.back();
507  };
508 
509  // Lambda to create wrapped histogram for circular delta_cp coordinate
510  auto createWrappedHistogram = [](TH2F *originalHist, const std::string &name) -> TH2F* {
511  int nBinsX = originalHist->GetNbinsX();
512  int nBinsY = originalHist->GetNbinsY();
513 
514  double xMin = originalHist->GetXaxis()->GetBinLowEdge(1);
515  double xMax = originalHist->GetXaxis()->GetBinUpEdge(nBinsX);
516  double yMin = originalHist->GetYaxis()->GetBinLowEdge(1);
517  double yMax = originalHist->GetYaxis()->GetBinUpEdge(nBinsY);
518  double yRange = yMax - yMin;
519 
520  // Extended histogram spanning 3 periods for proper boundary wrapping
521  TH2F *wrapped = new TH2F(name.c_str(), "",
522  nBinsX, xMin, xMax,
523  nBinsY * 3, yMin - yRange, yMax + yRange);
524  wrapped->SetDirectory(0);
525 
526  // Fill with periodically wrapped data
527  for (int i = 1; i <= nBinsX; ++i) {
528  for (int j = 1; j <= nBinsY; ++j) {
529  double content = originalHist->GetBinContent(i, j);
530  // Center period
531  wrapped->SetBinContent(i, j + nBinsY, content);
532  // Lower wrapped period
533  wrapped->SetBinContent(i, j, content);
534  // Upper wrapped period
535  wrapped->SetBinContent(i, j + 2*nBinsY, content);
536  }
537  }
538 
539  return wrapped;
540  };
541 
542  // Helper to compute one-sided bounds (do not draw) — integrates from side with more posterior
543  auto computeOneSidedBoundsValues = [&](TH1 *hist, double targets[3], double outBounds[3], bool &integrateLeftToRight) {
544  for (int i = 0; i < 3; ++i) outBounds[i] = std::numeric_limits<double>::quiet_NaN();
545  integrateLeftToRight = true;
546  if (!hist) return;
547  double total = hist->Integral();
548  if (total <= 0) return;
549  int nBins = hist->GetNbinsX();
550  double leftSum = 0.0;
551  double rightSum = 0.0;
552  for (int b = 1; b <= nBins; ++b) {
553  double center = hist->GetBinCenter(b);
554  double c = hist->GetBinContent(b);
555  if (center < 0) leftSum += c; else rightSum += c;
556  }
557  integrateLeftToRight = (leftSum >= rightSum);
558 
559  for (int t = 0; t < 3; ++t) {
560  double target = targets[t];
561  double cum = 0.0;
562  if (integrateLeftToRight) {
563  for (int b = 1; b <= nBins; ++b) {
564  cum += hist->GetBinContent(b);
565  if (cum / total >= target) {
566  outBounds[t] = hist->GetBinLowEdge(b+1);
567  break;
568  }
569  }
570  if (!std::isfinite(outBounds[t])) outBounds[t] = hist->GetXaxis()->GetXmax();
571  } else {
572  for (int b = nBins; b >= 1; --b) {
573  cum += hist->GetBinContent(b);
574  if (cum / total >= target) {
575  outBounds[t] = hist->GetBinLowEdge(b);
576  break;
577  }
578  }
579  if (!std::isfinite(outBounds[t])) outBounds[t] = hist->GetXaxis()->GetXmin();
580  }
581  }
582  };
583 
584  auto drawAbsDelm23Overlay = [&](const std::string &suffix,
585  const std::string &ioSelection,
586  const std::string &noSelection) {
587  const int nBinsAbsDelm23 = 80;
588  const int nBinsAbsDcp = 60;
589 
590  // Set reversed greyscale palette (grey to black) for this plot only
591  gStyle->SetPalette(kGreyScale);
592  TColor::InvertPalette();
593 
594  TCanvas *cAbs = new TCanvas(("c_abs_" + suffix).c_str(), "Delta m23 magnitude overlay", 900, 700);
595  cAbs->SetRightMargin(0.15);
596  cAbs->SetLeftMargin(0.12);
597  cAbs->SetBottomMargin(0.12);
598 
599  TH2F *hAbsFrame = new TH2F(("hAbsFrame_" + suffix).c_str(),
600  "|#Delta m_{23}^{2}| vs #delta_{CP};|#Delta m_{23}^{2}| (eV^{2});#delta_{CP} / #pi",
601  nBinsAbsDelm23, delm23_no_min, delm23_no_max,
602  nBinsAbsDcp, -3.1415, 3.1415);
603  hAbsFrame->SetStats(0);
604  hAbsFrame->Draw();
605 
606  TH2F *hAbsIO = new TH2F(("hAbsIO_" + suffix).c_str(), "",
607  nBinsAbsDelm23, delm23_no_min, delm23_no_max,
608  nBinsAbsDcp, -3.1415, 3.1415);
609  TH2F *hAbsNO = new TH2F(("hAbsNO_" + suffix).c_str(), "",
610  nBinsAbsDelm23, delm23_no_min, delm23_no_max,
611  nBinsAbsDcp, -3.1415, 3.1415);
612 
613  std::string ioDrawExpr = std::string("delta_cp:TMath::Abs(delm2_23)>>") + hAbsIO->GetName();
614  std::string noDrawExpr = std::string("delta_cp:TMath::Abs(delm2_23)>>") + hAbsNO->GetName();
615  tree->Draw(ioDrawExpr.c_str(), ioSelection.c_str(), "goff");
616  tree->Draw(noDrawExpr.c_str(), noSelection.c_str(), "goff");
617 
618  hAbsIO->SetDirectory(0);
619  hAbsNO->SetDirectory(0);
620  printHistStats("Abs delm23 IO", hAbsIO);
621  printHistStats("Abs delm23 NO", hAbsNO);
622 
623  // Calculate HPD contour levels for 1-sigma (68.3%)
624  double hpdLevel1SigmaNO = calculateHPDLevel(hAbsNO, 0.683);
625 
626  double hpdLevel1SigmaIO = calculateHPDLevel(hAbsIO, 0.683);
627 
628  // Draw density first
629  hAbsNO->SetLineColor(kAzure + 2);
630  hAbsNO->SetLineWidth(1);
631  hAbsNO->Draw("COLZ");
632 
633  hAbsIO->SetLineColor(kOrange + 7);
634  hAbsIO->SetLineWidth(1);
635  hAbsIO->Draw("COLZ SAME");
636 
637  // Smooth the fine histograms for contours
638  TH2F *hNO_contour = (TH2F *)hAbsNO->Clone("hNO_contour");
639  TH2F *hIO_contour = (TH2F *)hAbsIO->Clone("hIO_contour");
640  hNO_contour->SetDirectory(0);
641  hIO_contour->SetDirectory(0);
642 
643  hNO_contour->Smooth(1, "k5a");
644  hIO_contour->Smooth(1, "k5a");
645 
646  double noLevels[1] = {hpdLevel1SigmaNO};
647  hNO_contour->SetContour(1, noLevels);
648  hNO_contour->SetLineColor(kAzure + 2);
649  hNO_contour->SetLineWidth(3);
650  hNO_contour->Draw("CONT3 SAME");
651 
652  double ioLevels[1] = {hpdLevel1SigmaIO};
653  hIO_contour->SetContour(1, ioLevels);
654  hIO_contour->SetLineColor(kOrange + 7);
655  hIO_contour->SetLineWidth(3);
656  hIO_contour->Draw("CONT3 SAME");
657 
658  TLegend *legend = new TLegend(0.68, 0.73, 0.88, 0.91);
659  legend->SetBorderSize(0);
660  legend->SetFillStyle(0);
661  legend->AddEntry(hAbsNO, "NO 1#sigma", "l");
662  legend->AddEntry(hAbsIO, "IO 1#sigma", "l");
663  legend->Draw();
664  // add Asimov marker (abs of delm for this plot) and save
665  drawAsimovMarkerWithLegend(std::abs(asimovDelm23), asimovDcp, legend);
666  cAbs->Update();
667  cAbs->SaveAs((outputDir + "delm23_vs_dcp_abs_IO_NO" + suffix + ".png").c_str());
668  };
669 
670  drawAbsDelm23Overlay("", "delm2_23 < 0", "delm2_23 > 0");
671 
672  if (hasUmbrellaWeight) {
673  drawAbsDelm23Overlay("_weighted",
674  "(delm2_23 < 0) * umbrella_weight",
675  "(delm2_23 > 0) * umbrella_weight");
676  }
677 
678  // Reset palette to default for all other plots
679  gStyle->SetPalette(kBird);
680 
681  // Create canvas for Delta m23 NO vs sin^2(theta23)
682  TCanvas *c4 = new TCanvas("c4", "Delta m23 NO vs sin^{2}(#theta_{23})", 900, 700);
683  c4->SetRightMargin(0.15);
684  // Create 2D histogram: delta m23 vs delta CP
685  TH2F *h2d_sin = new TH2F("h2d_sin", "#Delta m_{23}^{2} vs sin^{2}(#theta_{23});sin^{2}(#theta_{23});#Delta m_{23}^{2} (eV^{2})",
686  nBins_sin2th23, sin2th23_min, sin2th23_max, nBins_delm23, delm23_no_min, delm23_no_max);
687  // Draw from tree (note: y:x format)
688  tree->Draw("delm2_23:sin2th_23>>h2d_sin", "delm2_23 > 0", "COLZ");
689  normalizeHist(h2d_sin);
690  h2d_sin->Draw("COLZ");
691  // Styling
692  h2d_sin->SetStats(0); // Remove stats box
693  h2d_sin->GetXaxis()->SetTitle("sin^{2}(#theta_{23})");
694  h2d_sin->GetYaxis()->SetTitle("#Delta m_{23}^{2} (eV^{2})");
695  h2d_sin->GetZaxis()->SetTitle("Normalized Entries");
696  //gPad->SetLogz(); // Optional: log scale on z-axis for better visibility
697  // Update canvas
698  // draw Asimov marker and Save plot
699  drawAsimovMarkerWithLegend(asimovSin2th23, asimovDelm23, nullptr);
700  c4->Update();
701  c4->SaveAs((outputDir + "th23_vs_delm23_no.png").c_str());
702 
703  if (hasUmbrellaWeight){
704  tree->Draw("delm2_23:sin2th_23>>h2d_sin", "(delm2_23 > 0) * umbrella_weight", "COLZ");
705  normalizeHist(h2d_sin);
706  h2d_sin->Draw("COLZ");
707  drawAsimovMarkerWithLegend(asimovSin2th23, asimovDelm23, nullptr);
708  c4->Update();
709  c4->SaveAs((outputDir + "th23_vs_delm23_no_weighted.png").c_str());
710  }
711 
712  // Create canvas for Delta m23 NO vs sin^2(theta23)
713  TCanvas *c5 = new TCanvas("c5", "Delta m23 IO vs sin^{2}(#theta_{23})", 900, 700);
714  c5->SetRightMargin(0.15);
715  // Create 2D histogram: delta m23 vs delta CP
716  TH2F *h2d_sin_IO = new TH2F("h2d_sin_IO", "#Delta m_{23}^{2} vs sin^{2}(#theta_{23});sin^{2}(#theta_{23});#Delta m_{23}^{2} (eV^{2})",
717  nBins_sin2th23, sin2th23_min, sin2th23_max,
718  nBins_delm23, delm23_io_min, delm23_io_max);
719  // Draw from tree (note: y:x format)
720  tree->Draw("delm2_23:sin2th_23>>h2d_sin_IO", "delm2_23 < 0", "COLZ");
721  normalizeHist(h2d_sin_IO);
722  h2d_sin_IO->Draw("COLZ");
723  // Styling
724  h2d_sin_IO->SetStats(0); // Remove stats box
725  h2d_sin_IO->GetXaxis()->SetTitle("sin^{2}(#theta_{23})");
726  h2d_sin_IO->GetYaxis()->SetTitle("#Delta m_{23}^{2} (eV^{2})");
727  h2d_sin_IO->GetZaxis()->SetTitle("Normalized Entries");
728  //gPad->SetLogz(); // Optional: log scale on z-axis for better visibility
729  // Update canvas
730  drawAsimovMarkerWithLegend(asimovSin2th23, asimovDelm23, nullptr);
731  c5->Update();
732  c5->SaveAs((outputDir + "th23_vs_delm23_io.png").c_str());
733 
734  if (hasUmbrellaWeight){
735  tree->Draw("delm2_23:sin2th_23>>h2d_sin_IO", "(delm2_23 < 0)*umbrella_weight", "COLZ");
736  normalizeHist(h2d_sin_IO);
737  h2d_sin_IO->Draw("COLZ");
738  drawAsimovMarkerWithLegend(asimovSin2th23, asimovDelm23, nullptr);
739  c5->Update();
740  c5->SaveAs((outputDir + "th23_vs_delm23_io_weighted.png").c_str());
741  }
742 
743  // Create canvas for delta_cp vs sin^2(theta23)
744  TCanvas *c6 = new TCanvas("c6", "Delta_cp vs sin^{2}(#theta_{23})", 900, 700);
745  c6->SetRightMargin(0.15);
746  // Create 2D histogram: delta m23 vs delta CP
747  TH2F *h2d_delta_cp = new TH2F("h2d_delta_cp", "#delta_{CP} vs sin^{2}(#theta_{23});#delta_{CP};sin^{2}(#theta_{23})",
748  nBins_dcp, -3.1415, 3.1415,
749  nBins_sin2th23, sin2th23_min, sin2th23_max);
750  // Draw from tree
751  tree->Draw("sin2th_23:delta_cp>>h2d_delta_cp", "", "COLZ");
752  normalizeHist(h2d_delta_cp);
753  h2d_delta_cp->Draw("COLZ");
754  // Styling
755  h2d_delta_cp->SetStats(0); // Remove stats box
756  h2d_delta_cp->GetYaxis()->SetTitle("sin^{2}(#theta_{23})");
757  h2d_delta_cp->GetXaxis()->SetTitle("#delta_{CP}");
758  h2d_delta_cp->GetZaxis()->SetTitle("Normalized Entries");
759  //gPad->SetLogz(); // Optional: log scale on z-axis for better visibility
760  // Update canvas
761  drawAsimovMarkerWithLegend(asimovDcp, asimovSin2th23, nullptr);
762  c6->Update();
763  c6->SaveAs((outputDir + "th23_vs_delta_cp.png").c_str());
764 
765  if (hasUmbrellaWeight){
766  tree->Draw("sin2th_23:delta_cp>>h2d_delta_cp", "umbrella_weight", "COLZ");
767  normalizeHist(h2d_delta_cp);
768  h2d_delta_cp->Draw("COLZ");
769  drawAsimovMarkerWithLegend(asimovDcp, asimovSin2th23, nullptr);
770  c6->Update();
771  c6->SaveAs((outputDir + "th23_vs_delta_cp_weighted.png").c_str());
772  }
773 
774  // Create canvas for delta_cp vs sin^2(theta13)
775  TCanvas *cth13 = new TCanvas("cth13", "Delta_cp vs sin^{2}(#theta_{13})", 900, 700);
776  cth13->SetRightMargin(0.15);
777  // Create 2D histogram: delta m23 vs delta CP
778  TH2F *h2d_delta_cp_13 = new TH2F("h2d_delta_cp_13", "#delta_{CP} vs sin^{2}(#theta_{13});#delta_{CP};sin^{2}(#theta_{13})",
779  nBins_dcp, -3.1415, 3.1415,
780  nBins_sin2th13, sin2th13_min, sin2th13_max);
781  // Draw from tree
782  tree->Draw("sin2th_13:delta_cp>>h2d_delta_cp_13", "", "COLZ");
783  normalizeHist(h2d_delta_cp_13);
784  h2d_delta_cp_13->Draw("COLZ");
785  // Styling
786  h2d_delta_cp_13->SetStats(0); // Remove stats box
787  h2d_delta_cp_13->GetYaxis()->SetTitle("sin^{2}(#theta_{13})");
788  h2d_delta_cp_13->GetXaxis()->SetTitle("#delta_{CP}");
789  h2d_delta_cp_13->GetZaxis()->SetTitle("Normalized Entries");
790  //gPad->SetLogz(); // Optional: log scale on z-axis for better visibility
791  // Update canvas
792  drawAsimovMarkerWithLegend(asimovDcp, asimovSin2th13, nullptr);
793  cth13->Update();
794  cth13->SaveAs((outputDir + "th13_vs_delta_cp.png").c_str());
795 
796  if (hasUmbrellaWeight){
797  tree->Draw("sin2th_13:delta_cp>>h2d_delta_cp_13", "umbrella_weight", "COLZ");
798  normalizeHist(h2d_delta_cp_13);
799  h2d_delta_cp_13->Draw("COLZ");
800  drawAsimovMarkerWithLegend(asimovDcp, asimovSin2th13, nullptr);
801  cth13->Update();
802  cth13->SaveAs((outputDir + "th13_vs_delta_cp_weighted.png").c_str());
803  }
804 
805  // plot 1D histograms of delta_cp, delm23, theta23, theta13, theta12, delm12
806  TCanvas *c7 = new TCanvas("c7", "1D Histograms", 3200, 1200);
807  c7->Divide(4, 2);
808 
809  // delta_cp unweighted
810  c7->cd(1);
811  TH1F *h1d_delta_cp_unweighted = new TH1F("h1d_delta_cp_unweighted", "#delta_{CP};#delta_{CP};Entries", nBins_dcp, -3.1415, 3.1415);
812  tree->Draw("delta_cp>>h1d_delta_cp_unweighted", "", "");
813  normalizeHist(h1d_delta_cp_unweighted);
814  h1d_delta_cp_unweighted->SetMinimum(0);
815  h1d_delta_cp_unweighted->SetStats(0);
816  h1d_delta_cp_unweighted->Draw("HIST");
817  h1d_delta_cp_unweighted->Draw("E SAME");
818  drawAsimovLineWithLegend(asimovDcp);
819 
820  c7->cd(2);
821  TH1F *h1d_delm23_all_unweighted = new TH1F("h1d_delm23_all_unweighted", "#Delta m_{23}^{2} (full range);#Delta m_{23}^{2} (eV^{2});Normalized Entries", nBins_delm23, delm23_io_min, delm23_no_max);
822  tree->Draw("delm2_23>>h1d_delm23_all_unweighted", "", "");
823  normalizeHist(h1d_delm23_all_unweighted);
824  h1d_delm23_all_unweighted->SetStats(0);
825  h1d_delm23_all_unweighted->Draw("HIST");
826  h1d_delm23_all_unweighted->Draw("E SAME");
827  drawAsimovLineWithLegend(asimovDelm23);
828 
829  c7->cd(3);
830  TH1F *h1d_delm23_unweighted = (TH1F*)h1d_delm23_all_unweighted->Clone("h1d_delm23_IO_unweighted");
831  h1d_delm23_unweighted->SetTitle("#Delta m_{23}^{2} (IO)");
832  h1d_delm23_unweighted->SetStats(0);
833  h1d_delm23_unweighted->GetXaxis()->SetRangeUser(delm23_io_min, delm23_io_max);
834  h1d_delm23_unweighted->Draw("HIST");
835  h1d_delm23_unweighted->Draw("E SAME");
836  drawAsimovLineWithLegend(asimovDelm23);
837 
838  c7->cd(4);
839  TH1F *h1d_delm23_NO_unweighted = (TH1F*)h1d_delm23_all_unweighted->Clone("h1d_delm23_NO_unweighted");
840  h1d_delm23_unweighted->SetTitle("#Delta m_{23}^{2} (NO)");
841  h1d_delm23_NO_unweighted->SetStats(0);
842  h1d_delm23_NO_unweighted->GetXaxis()->SetRangeUser(delm23_no_min, delm23_no_max);
843  h1d_delm23_NO_unweighted->Draw("HIST");
844  h1d_delm23_NO_unweighted->Draw("E SAME");
845  drawAsimovLineWithLegend(asimovDelm23);
846 
847  c7->cd(5);
848  TH1F *h1d_sinth23_unweighted = new TH1F("h1d_sinth23_unweighted", "sin^{2}(#theta_{23});sin^{2}(#theta_{23});Entries", nBins_sin2th23, sin2th23_min, sin2th23_max);
849  tree->Draw("sin2th_23>>h1d_sinth23_unweighted", "", "");
850  normalizeHist(h1d_sinth23_unweighted);
851  h1d_sinth23_unweighted->SetStats(0);
852  h1d_sinth23_unweighted->Draw("HIST");
853  h1d_sinth23_unweighted->Draw("E SAME");
854  drawAsimovLineWithLegend(asimovSin2th23);
855 
856  c7->cd(6);
857  TH1F *h1d_sinth13_unweighted = new TH1F("h1d_sinth13_unweighted", "sin^{2}(#theta_{13});sin^{2}(#theta_{13});Entries", nBins_sin2th13, sin2th13_min, sin2th13_max);
858  tree->Draw("sin2th_13>>h1d_sinth13_unweighted", "", "");
859  normalizeHist(h1d_sinth13_unweighted);
860  h1d_sinth13_unweighted->SetStats(0);
861  h1d_sinth13_unweighted->Draw("HIST");
862  h1d_sinth13_unweighted->Draw("E SAME");
863  drawAsimovLineWithLegend(asimovSin2th13);
864 
865  c7->cd(7);
866  TH1F *h1d_sinth12_unweighted = new TH1F("h1d_sinth12_unweighted", "sin^{2}(#theta_{12});sin^{2}(#theta_{12});Entries", nBins_sin2th12, sin2th12_min, sin2th12_max);
867  tree->Draw("sin2th_12>>h1d_sinth12_unweighted", "", "");
868  normalizeHist(h1d_sinth12_unweighted);
869  h1d_sinth12_unweighted->SetStats(0);
870  h1d_sinth12_unweighted->Draw("HIST");
871  h1d_sinth12_unweighted->Draw("E SAME");
872  drawAsimovLineWithLegend(asimovSin2th12);
873 
874  c7->cd(8);
875  TH1F *h1d_delm12_unweighted = new TH1F("h1d_delm12_unweighted", "#Delta m_{12}^{2};#Delta m_{12}^{2} (eV^{2});Entries", nBins_delm12, delm12_min, delm12_max);
876  tree->Draw("delm2_12>>h1d_delm12_unweighted", "", "");
877  normalizeHist(h1d_delm12_unweighted);
878  h1d_delm12_unweighted->SetStats(0);
879  h1d_delm12_unweighted->Draw("HIST");
880  h1d_delm12_unweighted->Draw("E SAME");
881  drawAsimovLineWithLegend(asimovDelm12);
882 
883  c7->Update();
884  c7->SaveAs((outputDir + "1D_histograms_unweighted.png").c_str());
885 
886  // Now create weighted version with sigma calculations
887  TCanvas *c7_weighted = new TCanvas("c7_weighted", "1D Histograms Weighted", 3200, 1200);
888  c7_weighted->Divide(4, 2);
889 
890  // delta_cp weighted
891  c7_weighted->cd(1);
892  TH1F *h1d_delta_cp = new TH1F("h1d_delta_cp", "#delta_{CP};#delta_{CP};Weighted Entries", nBins_dcp, -3.1415, 3.1415);
893  if (hasUmbrellaWeight) {
894  tree->Draw("delta_cp>>h1d_delta_cp", "umbrella_weight", "");
895  } else {
896  tree->Draw("delta_cp>>h1d_delta_cp", "", "");
897  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted delta_cp histogram." << std::endl;
898  }
899  normalizeHist(h1d_delta_cp);
900  h1d_delta_cp->SetStats(0);
901  h1d_delta_cp->Sumw2(); // Enable proper error calculation for weighted histograms
902  h1d_delta_cp->Draw("HIST");
903  h1d_delta_cp->Draw("E SAME");
904 
905  // add 1,3,5 sigma contour lines
906  double sigma1 = 0.6827;
907  double sigma3 = 0.9973;
908  double sigma5 = 0.99994;
909 
910  double sigma1_upper;
911  double sigma1_lower;
912  double sigma3_upper;
913  double sigma3_lower;
914  double sigma5_upper;
915  double sigma5_lower;
916 
917  // Find the HPD (Highest Posterior Density) bin
918  int hpdBin = 1;
919  double maxEntries = 0;
920  for (int j = 1; j <= h1d_delta_cp->GetNbinsX(); j++) {
921  if (h1d_delta_cp->GetBinContent(j) > maxEntries) {
922  maxEntries = h1d_delta_cp->GetBinContent(j);
923  hpdBin = j;
924  }
925  }
926 
927  std::cout << "HPD bin: " << hpdBin << " with " << maxEntries << " entries" << std::endl;
928 
929  // Create a copy of the histogram to work with
930  TH1F *h1d_copy = (TH1F*)h1d_delta_cp->Clone("h1d_copy");
931 
932  // Track which bins have been included
933  std::vector<bool> included(h1d_delta_cp->GetNbinsX() + 1, false);
934  included[hpdBin] = true;
935 
936  // Helper function to find contiguous regions with a gap tolerance
937  auto findContiguousRegions = [&](int gapTolerance = 2) {
938  std::vector<std::pair<int, int>> regions; // pairs of (lower_bin, upper_bin)
939 
940  int start = -1;
941  int gapCount = 0;
942 
943  for (int k = 1; k <= h1d_delta_cp->GetNbinsX(); k++) {
944  if (included[k]) {
945  if (start == -1) {
946  start = k; // start of a new region
947  }
948  gapCount = 0; // reset gap counter
949  } else if (start != -1) {
950  gapCount++;
951  if (gapCount > gapTolerance) {
952  // end of region
953  regions.push_back({start, k - gapCount});
954  start = -1;
955  gapCount = 0;
956  }
957  }
958  }
959 
960  // Close last region if it exists
961  if (start != -1) {
962  regions.push_back({start, h1d_delta_cp->GetNbinsX()});
963  }
964 
965  return regions;
966  };
967 
968  // loop over the bins from highest to lowest summing the bins until we reach the desired sigma levels
969  double totalEntries = h1d_delta_cp->Integral();
970  double sumEntries = h1d_delta_cp->GetBinContent(hpdBin);
971  double level1 = 0;
972  double level3 = 0;
973  double level5 = 0;
974 
975  std::vector<std::pair<double, double>> sigma1_regions;
976  std::vector<std::pair<double, double>> sigma3_regions;
977  std::vector<std::pair<double, double>> sigma5_regions;
978 
979  for (int i = 0; i < h1d_delta_cp->GetNbinsX(); i++) {
980  // find the bin with the highest entries that hasn't been included yet
981  int maxBin = 0;
982  double maxBinEntries = 0;
983  for (int j = 1; j <= h1d_delta_cp->GetNbinsX(); j++) {
984  if (!included[j] && h1d_copy->GetBinContent(j) > maxBinEntries) {
985  maxBinEntries = h1d_copy->GetBinContent(j);
986  maxBin = j;
987  }
988  }
989 
990  if (maxBin == 0) break; // no more bins to add
991 
992  sumEntries += maxBinEntries;
993  included[maxBin] = true;
994  double frac = sumEntries / totalEntries;
995 
996  std::cout << "Added bin " << maxBin << " with " << maxBinEntries << " entries. Fraction: " << frac << std::endl;
997 
998  if (frac >= sigma1 && level1 == 0) {
999  level1 = maxBinEntries;
1000  std::cout << "1 sigma level: " << level1 << std::endl;
1001  // Find all contiguous regions
1002  auto regions = findContiguousRegions(2);
1003  std::cout << "1 sigma regions (" << regions.size() << " mode(s)):" << std::endl;
1004  for (const auto& region : regions) {
1005  double lower = h1d_delta_cp->GetBinLowEdge(region.first);
1006  double upper = h1d_delta_cp->GetBinLowEdge(region.second + 1);
1007  sigma1_regions.push_back({lower, upper});
1008  std::cout << " [" << lower << ", " << upper << "]" << std::endl;
1009  }
1010  }
1011  if (frac >= sigma3 && level3 == 0) {
1012  level3 = maxBinEntries;
1013  std::cout << "3 sigma level: " << level3 << std::endl;
1014  // Find all contiguous regions
1015  auto regions = findContiguousRegions(2);
1016  std::cout << "3 sigma regions (" << regions.size() << " mode(s)):" << std::endl;
1017  for (const auto& region : regions) {
1018  double lower = h1d_delta_cp->GetBinLowEdge(region.first);
1019  double upper = h1d_delta_cp->GetBinLowEdge(region.second + 1);
1020  sigma3_regions.push_back({lower, upper});
1021  std::cout << " [" << lower << ", " << upper << "]" << std::endl;
1022  }
1023  }
1024  if (frac >= sigma5 && level5 == 0) {
1025  level5 = maxBinEntries;
1026  std::cout << "5 sigma level: " << level5 << std::endl;
1027  // Find all contiguous regions
1028  auto regions = findContiguousRegions(2);
1029  std::cout << "5 sigma regions (" << regions.size() << " mode(s)):" << std::endl;
1030  for (const auto& region : regions) {
1031  double lower = h1d_delta_cp->GetBinLowEdge(region.first);
1032  double upper = h1d_delta_cp->GetBinLowEdge(region.second + 1);
1033  sigma5_regions.push_back({lower, upper});
1034  std::cout << " [" << lower << ", " << upper << "]" << std::endl;
1035  }
1036  }
1037  }
1038 
1039  // Draw vertical lines at the sigma bounds for all regions
1040  double ymax = h1d_delta_cp->GetMaximum();
1041 
1042  // 1 sigma bounds (yellow/orange) - draw all regions
1043  for (const auto& region : sigma1_regions) {
1044  TLine *line_lower = new TLine(region.first, 0, region.first, ymax);
1045  line_lower->SetLineColor(kOrange+1);
1046  line_lower->SetLineStyle(2);
1047  line_lower->SetLineWidth(2);
1048  line_lower->Draw("same");
1049 
1050  TLine *line_upper = new TLine(region.second, 0, region.second, ymax);
1051  line_upper->SetLineColor(kOrange+1);
1052  line_upper->SetLineStyle(2);
1053  line_upper->SetLineWidth(2);
1054  line_upper->Draw("same");
1055  }
1056 
1057  // 3 sigma bounds (blue) - draw all regions
1058  for (const auto& region : sigma3_regions) {
1059  TLine *line_lower = new TLine(region.first, 0, region.first, ymax);
1060  line_lower->SetLineColor(kAzure+2);
1061  line_lower->SetLineStyle(2);
1062  line_lower->SetLineWidth(2);
1063  line_lower->Draw("same");
1064 
1065  TLine *line_upper = new TLine(region.second, 0, region.second, ymax);
1066  line_upper->SetLineColor(kAzure+2);
1067  line_upper->SetLineStyle(2);
1068  line_upper->SetLineWidth(2);
1069  line_upper->Draw("same");
1070  }
1071 
1072  // 5 sigma bounds (magenta) - draw all regions
1073  for (const auto& region : sigma5_regions) {
1074  TLine *line_lower = new TLine(region.first, 0, region.first, ymax);
1075  line_lower->SetLineColor(kMagenta+2);
1076  line_lower->SetLineStyle(2);
1077  line_lower->SetLineWidth(2);
1078  line_lower->Draw("same");
1079 
1080  TLine *line_upper = new TLine(region.second, 0, region.second, ymax);
1081  line_upper->SetLineColor(kMagenta+2);
1082  line_upper->SetLineStyle(2);
1083  line_upper->SetLineWidth(2);
1084  line_upper->Draw("same");
1085  }
1086 
1087  TLegend *legRef = nullptr;
1088  if (hasReferenceDcp && hRefDcpShape) {
1089  std::cout << "Overlaying reference delta_cp shape on weighted histogram" << std::endl;
1090  TH1D *hRefOverlay = (TH1D*)hRefDcpShape->Clone("hRefDcpShape_overlay_linear");
1091  hRefOverlay->SetDirectory(0);
1092  double refIntegral = hRefOverlay->Integral();
1093  if (refIntegral > 0) {
1094  hRefOverlay->Scale(h1d_delta_cp->Integral() / refIntegral);
1095  }
1096  hRefOverlay->SetLineColor(kMagenta + 2);
1097  hRefOverlay->SetLineWidth(3);
1098  hRefOverlay->SetLineStyle(1);
1099  hRefOverlay->SetFillStyle(0);
1100  hRefOverlay->Draw("HIST SAME");
1101 
1102  legRef = new TLegend(0.15, 0.74, 0.48, 0.88);
1103  legRef->SetBorderSize(0);
1104  legRef->SetFillStyle(0);
1105  legRef->AddEntry(h1d_delta_cp, "Umbrella weighted", "l");
1106  legRef->AddEntry(hRefOverlay, "Reference dcp (shape, area-normalized)", "l");
1107  legRef->Draw();
1108  }
1109  drawAsimovLineWithLegend(asimovDcp, hasReferenceDcp ? legRef : nullptr);
1110 
1111  // If LLH-scan reference histograms were provided, overlay them (they were area-normalized above)
1112  if (hasReferenceLLH_Dcp && hRefLLH_Dcp) {
1113  std::cout << "Overlaying LLH-scan reference dcp histogram on weighted histogram (resampled to umbrella bins)" << std::endl;
1114  // Resample LLH histogram to match umbrella binning
1115  TH1D *hLLHResamp = new TH1D("hRefLLH_dcp_resamp", "", h1d_delta_cp->GetNbinsX(), h1d_delta_cp->GetXaxis()->GetXmin(), h1d_delta_cp->GetXaxis()->GetXmax());
1116  hLLHResamp->SetDirectory(0);
1117  for (int i = 1; i <= hLLHResamp->GetNbinsX(); ++i) {
1118  double xlow = hLLHResamp->GetBinLowEdge(i);
1119  double xhigh = hLLHResamp->GetBinLowEdge(i+1);
1120  double val = 0.0;
1121  int nOrig = hRefLLH_Dcp->GetNbinsX();
1122  for (int k = 1; k <= nOrig; ++k) {
1123  double origLow = hRefLLH_Dcp->GetXaxis()->GetBinLowEdge(k);
1124  double origHigh = origLow + hRefLLH_Dcp->GetBinWidth(k);
1125  double overlap = std::max(0.0, std::min(origHigh, xhigh) - std::max(origLow, xlow));
1126  if (overlap > 0.0) {
1127  double origWidth = hRefLLH_Dcp->GetBinWidth(k);
1128  double contrib = hRefLLH_Dcp->GetBinContent(k) * (overlap / origWidth);
1129  val += contrib;
1130  }
1131  }
1132  hLLHResamp->SetBinContent(i, val);
1133  }
1134  // Now scale resampled LLH to umbrella histogram area
1135  double llhInt = hLLHResamp->Integral();
1136  if (llhInt > 0) hLLHResamp->Scale(h1d_delta_cp->Integral() / llhInt);
1137  hLLHResamp->SetLineColor(kBlack);
1138  hLLHResamp->SetLineWidth(3);
1139  hLLHResamp->SetLineStyle(2);
1140  hLLHResamp->SetFillStyle(0);
1141  hLLHResamp->Draw("HIST SAME");
1142 
1143  TLegend *legLLH = new TLegend(0.15, 0.60, 0.50, 0.74);
1144  legLLH->SetBorderSize(0);
1145  legLLH->SetFillStyle(0);
1146  legLLH->AddEntry(h1d_delta_cp, "Umbrella weighted", "l");
1147  legLLH->AddEntry(hLLHResamp, "LLH scan (resampled)", "l");
1148  legLLH->Draw();
1149  }
1150 
1151  c7_weighted->cd(2);
1152  TH1F *h1d_delm23_all = new TH1F("h1d_delm23_all", "#Delta m_{23}^{2} (full range);#Delta m_{23}^{2} (eV^{2});Normalized Entries", nBins_delm23, delm23_io_min, delm23_no_max);
1153  if (hasUmbrellaWeight) {
1154  tree->Draw("delm2_23>>h1d_delm23_all", "umbrella_weight", "");
1155  std::cout << "Applying umbrella weights to delm23 histogram" << std::endl;
1156  } else {
1157  tree->Draw("delm2_23>>h1d_delm23_all", "", "");
1158  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted #Delta m_{23}^{2} histogram." << std::endl;
1159  }
1160  normalizeHist(h1d_delm23_all);
1161  h1d_delm23_all->SetStats(0);
1162  h1d_delm23_all->Draw("HIST");
1163  h1d_delm23_all->Draw("E SAME");
1164 
1165  TLegend *legRefDelm23All = nullptr;
1166  if (hasReferenceDelm23 && hRefDelm23Shape) {
1167  TH1D *hRefDelm23AllOverlay = (TH1D*)hRefDelm23Shape->Clone("hRefDelm23All_overlay_linear");
1168  hRefDelm23AllOverlay->SetDirectory(0);
1169  double refIntegral = hRefDelm23AllOverlay->Integral();
1170  if (refIntegral > 0) {
1171  hRefDelm23AllOverlay->Scale(h1d_delm23_all->Integral() / refIntegral);
1172  }
1173  hRefDelm23AllOverlay->SetLineColor(kMagenta + 2);
1174  hRefDelm23AllOverlay->SetLineWidth(3);
1175  hRefDelm23AllOverlay->SetLineStyle(1);
1176  hRefDelm23AllOverlay->SetFillStyle(0);
1177  hRefDelm23AllOverlay->Draw("HIST SAME");
1178 
1179  legRefDelm23All = new TLegend(0.50, 0.74, 0.88, 0.88);
1180  legRefDelm23All->SetBorderSize(0);
1181  legRefDelm23All->SetFillStyle(0);
1182  legRefDelm23All->AddEntry(h1d_delm23_all, "Umbrella weighted", "l");
1183  legRefDelm23All->AddEntry(hRefDelm23AllOverlay, "Reference #Delta m_{23}^{2} (full range)", "l");
1184  legRefDelm23All->Draw();
1185  }
1186  drawAsimovLineWithLegend(asimovDelm23, hasReferenceDelm23 ? legRefDelm23All : nullptr);
1187 
1188  c7_weighted->cd(3);
1189  TH1F *h1d_delm23 = (TH1F*)h1d_delm23_all->Clone("h1d_delm23_IO");
1190  h1d_delm23->SetStats(0);
1191  h1d_delm23->Sumw2();
1192  h1d_delm23->GetXaxis()->SetRangeUser(delm23_io_min, delm23_io_max);
1193  h1d_delm23->Draw("HIST");
1194  h1d_delm23->Draw("E SAME");
1195 
1196  TLegend *legRefDelm23IO = nullptr;
1197  if (hasReferenceDelm23 && hRefDelm23Shape) {
1198  TH1D *hRefDelm23IOOverlay = (TH1D*)hRefDelm23Shape->Clone("hRefDelm23IO_overlay_linear");
1199  hRefDelm23IOOverlay->SetDirectory(0);
1200  hRefDelm23IOOverlay->GetXaxis()->SetRangeUser(delm23_io_min, delm23_io_max);
1201  hRefDelm23IOOverlay->SetLineColor(kMagenta + 2);
1202  hRefDelm23IOOverlay->SetLineWidth(3);
1203  hRefDelm23IOOverlay->SetLineStyle(1);
1204  hRefDelm23IOOverlay->SetFillStyle(0);
1205  hRefDelm23IOOverlay->Draw("HIST SAME");
1206 
1207  legRefDelm23IO = new TLegend(0.50, 0.74, 0.88, 0.88);
1208  legRefDelm23IO->SetBorderSize(0);
1209  legRefDelm23IO->SetFillStyle(0);
1210  legRefDelm23IO->AddEntry(h1d_delm23, "Umbrella weighted", "l");
1211  legRefDelm23IO->AddEntry(hRefDelm23IOOverlay, "Reference #Delta m_{23}^{2} (IO)", "l");
1212  legRefDelm23IO->Draw();
1213  }
1214  drawAsimovLineWithLegend(asimovDelm23, hasReferenceDelm23 ? legRefDelm23IO : nullptr);
1215 
1216  c7_weighted->cd(4);
1217  TH1F *h1d_delm23_NO = (TH1F*)h1d_delm23_all->Clone("h1d_delm23_NO");
1218  h1d_delm23_NO->SetStats(0);
1219  h1d_delm23_NO->Sumw2();
1220  h1d_delm23_NO->GetXaxis()->SetRangeUser(delm23_no_min, delm23_no_max);
1221  h1d_delm23_NO->Draw("HIST");
1222  h1d_delm23_NO->Draw("E SAME");
1223 
1224  TLegend *legRefDelm23NO = nullptr;
1225  if (hasReferenceDelm23 && hRefDelm23Shape) {
1226  std::cout << "Drawing reference delm23 shape for NO" << std::endl;
1227  TH1D *hRefDelm23NOOverlay = (TH1D*)hRefDelm23Shape->Clone("hRefDelm23NO_overlay_linear");
1228  hRefDelm23NOOverlay->SetDirectory(0);
1229  hRefDelm23NOOverlay->GetXaxis()->SetRangeUser(delm23_no_min, delm23_no_max);
1230  hRefDelm23NOOverlay->SetLineColor(kMagenta + 2);
1231  hRefDelm23NOOverlay->SetLineWidth(3);
1232  hRefDelm23NOOverlay->SetLineStyle(1);
1233  hRefDelm23NOOverlay->SetFillStyle(0);
1234  hRefDelm23NOOverlay->Draw("HIST SAME");
1235 
1236  legRefDelm23NO = new TLegend(0.50, 0.74, 0.88, 0.88);
1237  legRefDelm23NO->SetBorderSize(0);
1238  legRefDelm23NO->SetFillStyle(0);
1239  legRefDelm23NO->AddEntry(h1d_delm23_NO, "Umbrella weighted", "l");
1240  legRefDelm23NO->AddEntry(hRefDelm23NOOverlay, "Reference #Delta m_{23}^{2} (NO)", "l");
1241  legRefDelm23NO->Draw();
1242  }
1243  drawAsimovLineWithLegend(asimovDelm23, hasReferenceDelm23 ? legRefDelm23NO : nullptr);
1244 
1245  // Compute and print mass ordering preference (NO/IO) using the globally-normalized full-range histogram
1246  // Use `h1d_delm23_all` which has been normalized so its integral == 1, then sum bins over IO/NO ranges.
1247  if (h1d_delm23_all) {
1248  int binIO_low = h1d_delm23_all->GetXaxis()->FindBin(delm23_io_min);
1249  int binIO_high = h1d_delm23_all->GetXaxis()->FindBin(delm23_io_max);
1250  int binNO_low = h1d_delm23_all->GetXaxis()->FindBin(delm23_no_min);
1251  int binNO_high = h1d_delm23_all->GetXaxis()->FindBin(delm23_no_max);
1252 
1253  double fracIO = h1d_delm23_all->Integral(binIO_low, binIO_high);
1254  double fracNO = h1d_delm23_all->Integral(binNO_low, binNO_high);
1255 
1256  if (fracIO > 0) {
1257  double ratioNOtoIO = fracNO / fracIO;
1258  std::cout << "Mass ordering preference (NO/IO) = " << ratioNOtoIO
1259  << " (NO frac=" << fracNO << ", IO frac=" << fracIO << ")" << std::endl;
1260  } else {
1261  std::cout << "Mass ordering preference: IO fraction is zero; cannot compute ratio. "
1262  << "NO_frac=" << fracNO << " IO_frac=" << fracIO << std::endl;
1263  }
1264  } else {
1265  std::cout << "Mass ordering preference: h1d_delm23_all is null; cannot compute." << std::endl;
1266  }
1267 
1268  c7_weighted->cd(5);
1269  TH1F *h1d_sinth23 = new TH1F("h1d_sinth23", "sin^{2}(#theta_{23});sin^{2}(#theta_{23});Weighted Entries", nBins_sin2th23, sin2th23_min, sin2th23_max);
1270  if (hasUmbrellaWeight) {
1271  tree->Draw("sin2th_23>>h1d_sinth23", "umbrella_weight", "");
1272  } else {
1273  tree->Draw("sin2th_23>>h1d_sinth23", "", "");
1274  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted sin^2(theta23) histogram." << std::endl;
1275  }
1276  normalizeHist(h1d_sinth23);
1277  h1d_sinth23->SetStats(0);
1278  h1d_sinth23->Sumw2();
1279  h1d_sinth23->Draw("HIST");
1280  h1d_sinth23->Draw("E SAME");
1281 
1282  TLegend *legRefSin = nullptr;
1283  if (hasReferenceSin2Th23 && hRefSin2Th23Shape) {
1284  TH1D *hRefSinOverlay = (TH1D*)hRefSin2Th23Shape->Clone("hRefSin2Th23_overlay_linear");
1285  hRefSinOverlay->SetDirectory(0);
1286  double refIntegral = hRefSinOverlay->Integral();
1287  if (refIntegral > 0) {
1288  hRefSinOverlay->Scale(h1d_sinth23->Integral() / refIntegral);
1289  }
1290  hRefSinOverlay->SetLineColor(kMagenta + 2);
1291  hRefSinOverlay->SetLineWidth(3);
1292  hRefSinOverlay->SetLineStyle(1);
1293  hRefSinOverlay->SetFillStyle(0);
1294  hRefSinOverlay->Draw("HIST SAME");
1295 
1296  legRefSin = new TLegend(0.50, 0.74, 0.88, 0.88);
1297  legRefSin->SetBorderSize(0);
1298  legRefSin->SetFillStyle(0);
1299  legRefSin->AddEntry(h1d_sinth23, "Umbrella weighted", "l");
1300  legRefSin->AddEntry(hRefSinOverlay, "Reference sin^{2}(#theta_{23})", "l");
1301  legRefSin->Draw();
1302  }
1303  drawAsimovLineWithLegend(asimovSin2th23, hasReferenceSin2Th23 ? legRefSin : nullptr);
1304 
1305  c7_weighted->cd(6);
1306  TH1F *h1d_sinth13 = new TH1F("h1d_sinth13", "sin^{2}(#theta_{13});sin^{2}(#theta_{13});Weighted Entries", nBins_sin2th13, sin2th13_min, sin2th13_max);
1307  if (hasUmbrellaWeight) {
1308  tree->Draw("sin2th_13>>h1d_sinth13", "umbrella_weight", "");
1309  } else {
1310  tree->Draw("sin2th_13>>h1d_sinth13", "", "");
1311  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted sin^2(theta13) histogram." << std::endl;
1312  }
1313  normalizeHist(h1d_sinth13);
1314  h1d_sinth13->SetStats(0);
1315  h1d_sinth13->Sumw2();
1316  h1d_sinth13->Draw("HIST");
1317  h1d_sinth13->Draw("E SAME");
1318  drawAsimovLineWithLegend(asimovSin2th13);
1319 
1320  c7_weighted->cd(7);
1321  TH1F *h1d_sinth12 = new TH1F("h1d_sinth12", "sin^{2}(#theta_{12});sin^{2}(#theta_{12});Weighted Entries", nBins_sin2th12, sin2th12_min, sin2th12_max);
1322  if (hasUmbrellaWeight) {
1323  tree->Draw("sin2th_12>>h1d_sinth12", "umbrella_weight", "");
1324  } else {
1325  tree->Draw("sin2th_12>>h1d_sinth12", "", "");
1326  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted sin^2(theta12) histogram." << std::endl;
1327  }
1328  normalizeHist(h1d_sinth12);
1329  h1d_sinth12->SetStats(0);
1330  h1d_sinth12->Sumw2();
1331  h1d_sinth12->Draw("HIST");
1332  h1d_sinth12->Draw("E SAME");
1333  drawAsimovLineWithLegend(asimovSin2th12);
1334 
1335  c7_weighted->cd(8);
1336  TH1F *h1d_delm12 = new TH1F("h1d_delm12", "#Delta m_{12}^{2};#Delta m_{12}^{2} (eV^{2});Weighted Entries", nBins_delm12, delm12_min, delm12_max);
1337  if (hasUmbrellaWeight) {
1338  tree->Draw("delm2_12>>h1d_delm12", "umbrella_weight", "");
1339  } else {
1340  tree->Draw("delm2_12>>h1d_delm12", "", "");
1341  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted #Delta m_{12}^{2} histogram." << std::endl;
1342  }
1343  normalizeHist(h1d_delm12);
1344  h1d_delm12->SetStats(0);
1345  h1d_delm12->Sumw2();
1346  h1d_delm12->Draw("HIST");
1347  h1d_delm12->Draw("E SAME");
1348  drawAsimovLineWithLegend(asimovDelm12);
1349 
1350  c7_weighted->Update();
1351  c7_weighted->SaveAs((outputDir + "1D_histograms_weighted.png").c_str());
1352 
1353  // Create separate canvas for delta_cp with log scale
1354  TCanvas *c8 = new TCanvas("c8", "Delta CP Log Scale", 900, 700);
1355  c8->SetLogy();
1356  TH1F *h1d_delta_cp_log = (TH1F*)h1d_delta_cp->Clone("h1d_delta_cp_log");
1357  h1d_delta_cp_log->SetTitle("#delta_{CP} (Log Scale);#delta_{CP};Weighted Entries");
1358  h1d_delta_cp_log->Draw("HIST");
1359  h1d_delta_cp_log->Draw("E SAME");
1360 
1361  // Draw vertical lines at the sigma bounds for all regions on log scale plot
1362  double ymax_log = h1d_delta_cp_log->GetMaximum();
1363  double ymin_log = h1d_delta_cp_log->GetMinimum();
1364  if (ymin_log <= 0) ymin_log = 0.1; // avoid log(0)
1365 
1366  // 1 sigma bounds (user colors) - draw one-sided lines and HPD regions
1367  for (const auto& region : sigma1_regions) {
1368  TLine *line_lower = new TLine(region.first, ymin_log, region.first, ymax_log);
1369  line_lower->SetLineColor(kRed);
1370  line_lower->SetLineStyle(2);
1371  line_lower->SetLineWidth(2);
1372  line_lower->Draw("same");
1373 
1374  TLine *line_upper = new TLine(region.second, ymin_log, region.second, ymax_log);
1375  line_upper->SetLineColor(kRed);
1376  line_upper->SetLineStyle(2);
1377  line_upper->SetLineWidth(2);
1378  line_upper->Draw("same");
1379  }
1380 
1381  // 3 sigma bounds (turquoise/darker blue) - draw all regions
1382  for (const auto& region : sigma3_regions) {
1383  TLine *line_lower = new TLine(region.first, ymin_log, region.first, ymax_log);
1384  line_lower->SetLineColor(kAzure+2);
1385  line_lower->SetLineStyle(2);
1386  line_lower->SetLineWidth(2);
1387  line_lower->Draw("same");
1388 
1389  TLine *line_upper = new TLine(region.second, ymin_log, region.second, ymax_log);
1390  line_upper->SetLineColor(kAzure+2);
1391  line_upper->SetLineStyle(2);
1392  line_upper->SetLineWidth(2);
1393  line_upper->Draw("same");
1394  }
1395 
1396  // 5 sigma bounds (purple) - draw all regions
1397  for (const auto& region : sigma5_regions) {
1398  TLine *line_lower = new TLine(region.first, ymin_log, region.first, ymax_log);
1399  line_lower->SetLineColor(kMagenta+2);
1400  line_lower->SetLineStyle(2);
1401  line_lower->SetLineWidth(2);
1402  line_lower->Draw("same");
1403 
1404  TLine *line_upper = new TLine(region.second, ymin_log, region.second, ymax_log);
1405  line_upper->SetLineColor(kMagenta+2);
1406  line_upper->SetLineStyle(2);
1407  line_upper->SetLineWidth(2);
1408  line_upper->Draw("same");
1409  }
1410 
1411  TLegend *legRefLog = nullptr;
1412  if (hasReferenceDcp && hRefDcpShape) {
1413  TH1D *hRefOverlayLog = (TH1D*)hRefDcpShape->Clone("hRefDcpShape_overlay_log");
1414  hRefOverlayLog->SetDirectory(0);
1415  double refIntegral = hRefOverlayLog->Integral();
1416  if (refIntegral > 0) {
1417  hRefOverlayLog->Scale(h1d_delta_cp_log->Integral() / refIntegral);
1418  }
1419  hRefOverlayLog->SetLineColor(kMagenta + 2);
1420  hRefOverlayLog->SetLineWidth(3);
1421  hRefOverlayLog->SetLineStyle(1);
1422  hRefOverlayLog->SetFillStyle(0);
1423  hRefOverlayLog->Draw("HIST SAME");
1424 
1425  legRefLog = new TLegend(0.50, 0.74, 0.88, 0.88);
1426  legRefLog->SetBorderSize(0);
1427  legRefLog->SetFillStyle(0);
1428  legRefLog->AddEntry(h1d_delta_cp_log, "Umbrella weighted", "l");
1429  legRefLog->AddEntry(hRefOverlayLog, "Reference dcp (shape, area-normalized)", "l");
1430  legRefLog->Draw();
1431  }
1432  if (hasReferenceLLH_Dcp && hRefLLH_Dcp) {
1433  // Resample LLH histogram to match log-plot binning
1434  TH1D *hLLHResampLog = new TH1D("hRefLLH_dcp_resamp_log", "", h1d_delta_cp_log->GetNbinsX(), h1d_delta_cp_log->GetXaxis()->GetXmin(), h1d_delta_cp_log->GetXaxis()->GetXmax());
1435  hLLHResampLog->SetDirectory(0);
1436  for (int i = 1; i <= hLLHResampLog->GetNbinsX(); ++i) {
1437  double xlow = hLLHResampLog->GetBinLowEdge(i);
1438  double xhigh = hLLHResampLog->GetBinLowEdge(i+1);
1439  double val = 0.0;
1440  int nOrig = hRefLLH_Dcp->GetNbinsX();
1441  for (int k = 1; k <= nOrig; ++k) {
1442  double origLow = hRefLLH_Dcp->GetXaxis()->GetBinLowEdge(k);
1443  double origHigh = origLow + hRefLLH_Dcp->GetBinWidth(k);
1444  double overlap = std::max(0.0, std::min(origHigh, xhigh) - std::max(origLow, xlow));
1445  if (overlap > 0.0) {
1446  double origWidth = hRefLLH_Dcp->GetBinWidth(k);
1447  double contrib = hRefLLH_Dcp->GetBinContent(k) * (overlap / origWidth);
1448  val += contrib;
1449  }
1450  }
1451  hLLHResampLog->SetBinContent(i, val);
1452  }
1453  double llhI = hLLHResampLog->Integral();
1454  if (llhI > 0) hLLHResampLog->Scale(h1d_delta_cp_log->Integral() / llhI);
1455  hLLHResampLog->SetLineColor(kBlue + 2);
1456  hLLHResampLog->SetLineWidth(3);
1457  hLLHResampLog->SetLineStyle(2);
1458  hLLHResampLog->SetFillStyle(0);
1459  hLLHResampLog->Draw("HIST SAME");
1460  TLegend *legLLHLog = new TLegend(0.50, 0.60, 0.88, 0.74);
1461  legLLHLog->SetBorderSize(0);
1462  legLLHLog->SetFillStyle(0);
1463  legLLHLog->AddEntry(h1d_delta_cp_log, "Umbrella weighted", "l");
1464  legLLHLog->AddEntry(hLLHResampLog, "LLH scan (resampled & area-normalized)", "l");
1465  legLLHLog->Draw();
1466  }
1467  drawAsimovLineWithLegend(asimovDcp, hasReferenceDcp ? legRefLog : nullptr);
1468 
1469  c8->Update();
1470  c8->SaveAs((outputDir + "delta_cp_log_scale_weighted.png").c_str());
1471 
1472  // Create separate two-panel log-scale delta_cp plots marginalized over IO and NO.
1473  TCanvas *c9 = new TCanvas("c9", "Delta CP Log Scale Marginalized by Ordering", 1800, 700);
1474  c9->Divide(2, 1);
1475 
1476  std::string ioSelection = hasUmbrellaWeight ? "(delm2_23 < 0) * umbrella_weight" : "(delm2_23 < 0)";
1477  std::string noSelection = hasUmbrellaWeight ? "(delm2_23 > 0) * umbrella_weight" : "(delm2_23 > 0)";
1478 
1479  // IO panel
1480  c9->cd(1);
1481  gPad->SetLogy();
1482  TH1F *h1d_delta_cp_io_log = new TH1F("h1d_delta_cp_io_log", "#delta_{CP} marginalized over IO;#delta_{CP};Normalized Entries", nBins_dcp, -3.1415, 3.1415);
1483  tree->Draw("delta_cp>>h1d_delta_cp_io_log", ioSelection.c_str(), "");
1484  normalizeHist(h1d_delta_cp_io_log);
1485  h1d_delta_cp_io_log->SetStats(0);
1486  double ioMinPositive = 1.0;
1487  bool ioFoundPositive = false;
1488  for (int b = 1; b <= h1d_delta_cp_io_log->GetNbinsX(); ++b) {
1489  double y = h1d_delta_cp_io_log->GetBinContent(b);
1490  if (y > 0.0) {
1491  if (!ioFoundPositive || y < ioMinPositive) {
1492  ioMinPositive = y;
1493  }
1494  ioFoundPositive = true;
1495  }
1496  }
1497  h1d_delta_cp_io_log->SetMinimum(ioFoundPositive ? 0.5 * ioMinPositive : 1e-8);
1498  h1d_delta_cp_io_log->Draw("HIST");
1499  h1d_delta_cp_io_log->Draw("E SAME");
1500  drawAsimovLineWithLegend(asimovDcp);
1501  if (hasReferenceLLH_Dcp_IO && hRefLLH_Dcp_IO) {
1502  TH1D *hLLHIO = new TH1D("hRefLLH_dcp_io_resamp", "", h1d_delta_cp_io_log->GetNbinsX(), h1d_delta_cp_io_log->GetXaxis()->GetXmin(), h1d_delta_cp_io_log->GetXaxis()->GetXmax());
1503  hLLHIO->SetDirectory(0);
1504  for (int i = 1; i <= hLLHIO->GetNbinsX(); ++i) {
1505  double xlow = hLLHIO->GetBinLowEdge(i);
1506  double xhigh = hLLHIO->GetBinLowEdge(i+1);
1507  double val = 0.0;
1508  int nOrig = hRefLLH_Dcp_IO->GetNbinsX();
1509  for (int k = 1; k <= nOrig; ++k) {
1510  double origLow = hRefLLH_Dcp_IO->GetXaxis()->GetBinLowEdge(k);
1511  double origHigh = origLow + hRefLLH_Dcp_IO->GetBinWidth(k);
1512  double overlap = std::max(0.0, std::min(origHigh, xhigh) - std::max(origLow, xlow));
1513  if (overlap > 0.0) {
1514  double origWidth = hRefLLH_Dcp_IO->GetBinWidth(k);
1515  double contrib = hRefLLH_Dcp_IO->GetBinContent(k) * (overlap / origWidth);
1516  val += contrib;
1517  }
1518  }
1519  hLLHIO->SetBinContent(i, val);
1520  }
1521  double li = hLLHIO->Integral();
1522  if (li > 0) hLLHIO->Scale(h1d_delta_cp_io_log->Integral() / li);
1523  hLLHIO->SetLineColor(kBlue + 2);
1524  hLLHIO->SetLineWidth(2);
1525  hLLHIO->SetLineStyle(2);
1526  hLLHIO->Draw("HIST SAME");
1527  TLegend *legLLHIO = new TLegend(0.50, 0.60, 0.88, 0.74);
1528  legLLHIO->SetBorderSize(0);
1529  legLLHIO->SetFillStyle(0);
1530  legLLHIO->AddEntry(h1d_delta_cp_io_log, "Umbrella weighted (IO)", "l");
1531  legLLHIO->AddEntry(hLLHIO, "LLH scan IO (resampled & area-normalized)", "l");
1532  legLLHIO->Draw();
1533  }
1534 
1535  // NO panel
1536  c9->cd(2);
1537  gPad->SetLogy();
1538  TH1F *h1d_delta_cp_no_log = new TH1F("h1d_delta_cp_no_log", "#delta_{CP} marginalized over NO;#delta_{CP};Normalized Entries", nBins_dcp, -3.1415, 3.1415);
1539  tree->Draw("delta_cp>>h1d_delta_cp_no_log", noSelection.c_str(), "");
1540  normalizeHist(h1d_delta_cp_no_log);
1541  h1d_delta_cp_no_log->SetStats(0);
1542  double noMinPositive = 1.0;
1543  bool noFoundPositive = false;
1544  for (int b = 1; b <= h1d_delta_cp_no_log->GetNbinsX(); ++b) {
1545  double y = h1d_delta_cp_no_log->GetBinContent(b);
1546  if (y > 0.0) {
1547  if (!noFoundPositive || y < noMinPositive) {
1548  noMinPositive = y;
1549  }
1550  noFoundPositive = true;
1551  }
1552  }
1553  h1d_delta_cp_no_log->SetMinimum(noFoundPositive ? 0.5 * noMinPositive : 1e-8);
1554  h1d_delta_cp_no_log->Draw("HIST");
1555  h1d_delta_cp_no_log->Draw("E SAME");
1556  drawAsimovLineWithLegend(asimovDcp);
1557  if (hasReferenceLLH_Dcp_NO && hRefLLH_Dcp_NO) {
1558  TH1D *hLLHNO = new TH1D("hRefLLH_dcp_no_resamp", "", h1d_delta_cp_no_log->GetNbinsX(), h1d_delta_cp_no_log->GetXaxis()->GetXmin(), h1d_delta_cp_no_log->GetXaxis()->GetXmax());
1559  hLLHNO->SetDirectory(0);
1560  for (int i = 1; i <= hLLHNO->GetNbinsX(); ++i) {
1561  double xlow = hLLHNO->GetBinLowEdge(i);
1562  double xhigh = hLLHNO->GetBinLowEdge(i+1);
1563  double val = 0.0;
1564  int nOrig = hRefLLH_Dcp_NO->GetNbinsX();
1565  for (int k = 1; k <= nOrig; ++k) {
1566  double origLow = hRefLLH_Dcp_NO->GetXaxis()->GetBinLowEdge(k);
1567  double origHigh = origLow + hRefLLH_Dcp_NO->GetBinWidth(k);
1568  double overlap = std::max(0.0, std::min(origHigh, xhigh) - std::max(origLow, xlow));
1569  if (overlap > 0.0) {
1570  double origWidth = hRefLLH_Dcp_NO->GetBinWidth(k);
1571  double contrib = hRefLLH_Dcp_NO->GetBinContent(k) * (overlap / origWidth);
1572  val += contrib;
1573  }
1574  }
1575  hLLHNO->SetBinContent(i, val);
1576  }
1577  double ln = hLLHNO->Integral();
1578  if (ln > 0) hLLHNO->Scale(h1d_delta_cp_no_log->Integral() / ln);
1579  hLLHNO->SetLineColor(kBlue + 2);
1580  hLLHNO->SetLineWidth(2);
1581  hLLHNO->SetLineStyle(2);
1582  hLLHNO->Draw("HIST SAME");
1583  TLegend *legLLHNO = new TLegend(0.50, 0.60, 0.88, 0.74);
1584  legLLHNO->SetBorderSize(0);
1585  legLLHNO->SetFillStyle(0);
1586  legLLHNO->AddEntry(h1d_delta_cp_no_log, "Umbrella weighted (NO)", "l");
1587  legLLHNO->AddEntry(hLLHNO, "LLH scan NO (resampled & area-normalized)", "l");
1588  legLLHNO->Draw();
1589  }
1590 
1591  c9->Update();
1592  c9->SaveAs((outputDir + "delta_cp_log_scale_marginalized_io_no.png").c_str());
1593 
1594  // Jarlskog invariant plots (unitless)
1595  const double jarlskogMin = -0.06;
1596  const double jarlskogMax = 0.06;
1597  const int nBinsJarlskog = 160;
1598 
1599  TCanvas *c10 = new TCanvas("c10", "Jarlskog Invariant", 900, 700);
1600  TH1F *h1d_jarlskog_unweighted = new TH1F("h1d_jarlskog_unweighted", "Jarlskog invariant;Jarlskog invariant;Entries",
1601  nBinsJarlskog, jarlskogMin, jarlskogMax);
1602  tree->Draw("TMath::Sqrt(TMath::Max(0.,sin2th_12)*TMath::Max(0.,1.-sin2th_12)*TMath::Max(0.,sin2th_23)*TMath::Max(0.,1.-sin2th_23))*TMath::Max(0.,1.-sin2th_13)*TMath::Sqrt(TMath::Max(0.,sin2th_13))*TMath::Sin(delta_cp)>>h1d_jarlskog_unweighted",
1603  "", "");
1604  normalizeHist(h1d_jarlskog_unweighted);
1605  h1d_jarlskog_unweighted->SetStats(0);
1606  h1d_jarlskog_unweighted->Draw("HIST");
1607  h1d_jarlskog_unweighted->Draw("E SAME");
1608  drawAsimovLineWithLegend(asimovJarlskog);
1609  // One-sided confidence intervals for Jarlskog (unweighted)
1610  auto computeOneSidedBounds = [&](TH1 *hist, double targets[3], double outBounds[3]) {
1611  // targets: fractions (e.g., 0.6827, 0.9973, 0.99994)
1612  // outBounds: boundaries in x where cumulative from chosen side reaches target
1613  for (int i = 0; i < 3; ++i) outBounds[i] = std::numeric_limits<double>::quiet_NaN();
1614  if (!hist) return;
1615  double total = hist->Integral();
1616  if (total <= 0) return;
1617  int nBins = hist->GetNbinsX();
1618  // determine which side of zero holds more posterior (simple count of bin content)
1619  double leftSum = 0.0;
1620  double rightSum = 0.0;
1621  for (int b = 1; b <= nBins; ++b) {
1622  double center = hist->GetBinCenter(b);
1623  double c = hist->GetBinContent(b);
1624  if (center < 0) leftSum += c; else rightSum += c;
1625  }
1626  bool integrateLeftToRight = (leftSum >= rightSum);
1627 
1628  for (int t = 0; t < 3; ++t) {
1629  double target = targets[t];
1630  double cum = 0.0;
1631  if (integrateLeftToRight) {
1632  for (int b = 1; b <= nBins; ++b) {
1633  cum += hist->GetBinContent(b);
1634  if (cum / total >= target) {
1635  // upper edge of bin b
1636  outBounds[t] = hist->GetBinLowEdge(b+1);
1637  break;
1638  }
1639  }
1640  // if not reached, set to max
1641  if (!std::isfinite(outBounds[t])) outBounds[t] = hist->GetXaxis()->GetXmax();
1642  } else {
1643  for (int b = nBins; b >= 1; --b) {
1644  cum += hist->GetBinContent(b);
1645  if (cum / total >= target) {
1646  // lower edge of bin b
1647  outBounds[t] = hist->GetBinLowEdge(b);
1648  break;
1649  }
1650  }
1651  if (!std::isfinite(outBounds[t])) outBounds[t] = hist->GetXaxis()->GetXmin();
1652  }
1653  }
1654  // draw lines on current pad
1655  double hist_ymax = hist->GetMaximum();
1656  // colors: 1sigma yellow-orangey, 3sigma turquoise/darker blue, 5sigma purple
1657  int colors[3] = {kOrange+1, kAzure+2, kMagenta+2};
1658  for (int t = 0; t < 3; ++t) {
1659  if (!std::isfinite(outBounds[t])) continue;
1660  TLine *line = nullptr;
1661  if (integrateLeftToRight) {
1662  // region is [xmin, outBounds[t]] -> draw vertical at outBounds[t]
1663  line = new TLine(outBounds[t], 0, outBounds[t], hist_ymax);
1664  } else {
1665  // region is [outBounds[t], xmax] -> draw vertical at outBounds[t]
1666  line = new TLine(outBounds[t], 0, outBounds[t], hist_ymax);
1667  }
1668  line->SetLineColor(colors[t]);
1669  line->SetLineStyle(2);
1670  line->SetLineWidth(2);
1671  line->Draw("same");
1672  }
1673  };
1674  // (no one-sided bounds for unweighted histograms)
1675  c10->Update();
1676  c10->SaveAs((outputDir + "jarlskog_unweighted.png").c_str());
1677 
1678  TCanvas *c10_log = new TCanvas("c10_log", "Jarlskog Invariant Log Scale", 900, 700);
1679  c10_log->SetLogy();
1680  TH1F *h1d_jarlskog_unweighted_log = (TH1F*)h1d_jarlskog_unweighted->Clone("h1d_jarlskog_unweighted_log");
1681  h1d_jarlskog_unweighted_log->SetTitle("Jarlskog invariant (Log Scale);Jarlskog invariant;Entries");
1682  h1d_jarlskog_unweighted_log->Draw("HIST");
1683  h1d_jarlskog_unweighted_log->Draw("E SAME");
1684  drawAsimovLineWithLegend(asimovJarlskog);
1685  // (no one-sided bounds for unweighted histograms)
1686  c10_log->Update();
1687  c10_log->SaveAs((outputDir + "jarlskog_unweighted_log.png").c_str());
1688 
1689  TCanvas *c10_weighted = new TCanvas("c10_weighted", "Jarlskog Invariant Weighted", 900, 700);
1690  TH1F *h1d_jarlskog_weighted = new TH1F("h1d_jarlskog_weighted", "Jarlskog invariant;Jarlskog invariant;Weighted Entries",
1691  nBinsJarlskog, jarlskogMin, jarlskogMax);
1692  if (hasUmbrellaWeight) {
1693  tree->Draw("TMath::Sqrt(TMath::Max(0.,sin2th_12)*TMath::Max(0.,1.-sin2th_12)*TMath::Max(0.,sin2th_23)*TMath::Max(0.,1.-sin2th_23))*TMath::Max(0.,1.-sin2th_13)*TMath::Sqrt(TMath::Max(0.,sin2th_13))*TMath::Sin(delta_cp)>>h1d_jarlskog_weighted",
1694  "umbrella_weight", "");
1695  } else {
1696  tree->Draw("TMath::Sqrt(TMath::Max(0.,sin2th_12)*TMath::Max(0.,1.-sin2th_12)*TMath::Max(0.,sin2th_23)*TMath::Max(0.,1.-sin2th_23))*TMath::Max(0.,1.-sin2th_13)*TMath::Sqrt(TMath::Max(0.,sin2th_13))*TMath::Sin(delta_cp)>>h1d_jarlskog_weighted",
1697  "", "");
1698  std::cout << "WARNING:::::No umbrella weights found, plotting unweighted Jarlskog histogram." << std::endl;
1699  }
1700  normalizeHist(h1d_jarlskog_weighted);
1701  h1d_jarlskog_weighted->SetStats(0);
1702  h1d_jarlskog_weighted->Sumw2();
1703  h1d_jarlskog_weighted->Draw("HIST");
1704  h1d_jarlskog_weighted->Draw("E SAME");
1705  drawAsimovLineWithLegend(asimovJarlskog);
1706  // One-sided confidence intervals for Jarlskog (weighted)
1707  double targets[3] = {sigma1, sigma3, sigma5};
1708  double bounds[3];
1709  computeOneSidedBounds(h1d_jarlskog_weighted, targets, bounds);
1710  // create legend entries for one-sided CI lines (weighted)
1711  double ymax_w = h1d_jarlskog_weighted->GetMaximum();
1712  TLine *legLine1 = nullptr;
1713  TLine *legLine3 = nullptr;
1714  TLine *legLine5 = nullptr;
1715  if (std::isfinite(bounds[0])) legLine1 = new TLine(bounds[0], 0, bounds[0], ymax_w);
1716  if (std::isfinite(bounds[1])) legLine3 = new TLine(bounds[1], 0, bounds[1], ymax_w);
1717  if (std::isfinite(bounds[2])) legLine5 = new TLine(bounds[2], 0, bounds[2], ymax_w);
1718  if (legLine1) { legLine1->SetLineColor(kOrange+1); legLine1->SetLineStyle(2); legLine1->SetLineWidth(2); }
1719  if (legLine3) { legLine3->SetLineColor(kAzure+2); legLine3->SetLineStyle(2); legLine3->SetLineWidth(2); }
1720  if (legLine5) { legLine5->SetLineColor(kMagenta+2); legLine5->SetLineStyle(2); legLine5->SetLineWidth(2); }
1721  TLegend *legCI = new TLegend(0.15, 0.60, 0.50, 0.74);
1722  legCI->SetBorderSize(0);
1723  legCI->SetFillStyle(0);
1724  if (legLine1) legCI->AddEntry(legLine1, "1 #sigma (one-sided)", "l");
1725  if (legLine3) legCI->AddEntry(legLine3, "3 #sigma (one-sided)", "l");
1726  if (legLine5) legCI->AddEntry(legLine5, "5 #sigma (one-sided)", "l");
1727  legCI->Draw();
1728  c10_weighted->Update();
1729  c10_weighted->SaveAs((outputDir + "jarlskog_weighted.png").c_str());
1730 
1731  TCanvas *c10_weighted_log = new TCanvas("c10_weighted_log", "Jarlskog Invariant Weighted Log Scale", 900, 700);
1732  c10_weighted_log->SetLogy();
1733  TH1F *h1d_jarlskog_weighted_log = (TH1F*)h1d_jarlskog_weighted->Clone("h1d_jarlskog_weighted_log");
1734  h1d_jarlskog_weighted_log->SetTitle("Jarlskog invariant (Log Scale);Jarlskog invariant;Weighted Entries");
1735  h1d_jarlskog_weighted_log->Draw("HIST");
1736  h1d_jarlskog_weighted_log->Draw("E SAME");
1737  drawAsimovLineWithLegend(asimovJarlskog);
1738  // Draw previously computed one-sided bounds on the log canvas as well
1739  for (int t = 0; t < 3; ++t) {
1740  if (!std::isfinite(bounds[t])) continue;
1741  double hist_ymax = h1d_jarlskog_weighted_log->GetMaximum();
1742  int colors_log[3] = {kOrange+1, kAzure+2, kMagenta+2};
1743  TLine *line = new TLine(bounds[t], 0, bounds[t], hist_ymax);
1744  line->SetLineColor(colors_log[t]);
1745  line->SetLineStyle(2);
1746  line->SetLineWidth(2);
1747  line->Draw("same");
1748  }
1749  // legend for log canvas
1750  TLegend *legCI_log = new TLegend(0.15, 0.60, 0.50, 0.74);
1751  legCI_log->SetBorderSize(0);
1752  legCI_log->SetFillStyle(0);
1753  if (legLine1) legCI_log->AddEntry(legLine1, "1 #sigma (one-sided)", "l");
1754  if (legLine3) legCI_log->AddEntry(legLine3, "3 #sigma (one-sided)", "l");
1755  if (legLine5) legCI_log->AddEntry(legLine5, "5 #sigma (one-sided)", "l");
1756  legCI_log->Draw();
1757  c10_weighted_log->Update();
1758  c10_weighted_log->SaveAs((outputDir + "jarlskog_weighted_log.png").c_str());
1759 
1760  struct TriangleVar {
1761  std::string name;
1762  std::string title;
1763  std::string branch;
1764  int bins;
1765  double min;
1766  double max;
1767  double asimov;
1768  };
1769 
1770  std::vector<TriangleVar> triangleVars = {
1771  {"delta_cp", "#delta_{CP}", "delta_cp", nBins_dcp, -3.1415, 3.1415, asimovDcp},
1772  {"sin2th_23", "sin^{2}(#theta_{23})", "sin2th_23", nBins_sin2th23, sin2th23_min, sin2th23_max, asimovSin2th23},
1773  {"sin2th_13", "sin^{2}(#theta_{13})", "sin2th_13", nBins_sin2th13, sin2th13_min, sin2th13_max, asimovSin2th13},
1774  {"sin2th_12", "sin^{2}(#theta_{12})", "sin2th_12", nBins_sin2th12, sin2th12_min, sin2th12_max, asimovSin2th12},
1775  {"delm2_23", "#Delta m_{23}^{2} (eV^{2})", "delm2_23", nBins_delm23, delm23_io_min, delm23_no_max, asimovDelm23},
1776  {"delm2_12", "#Delta m_{12}^{2} (eV^{2})", "delm2_12", nBins_delm12, delm12_min, delm12_max, asimovDelm12}
1777  };
1778 
1779  auto drawAsimovLineNoLegend = [&](double xValue) {
1780  if (!isAsimovChain || !std::isfinite(xValue) || !gPad) return;
1781  gPad->Update();
1782  const double yMin = gPad->GetUymin();
1783  const double yMax = gPad->GetUymax();
1784  TLine *asimovLine = new TLine(xValue, yMin, xValue, yMax);
1785  asimovLine->SetLineColor(kBlack);
1786  asimovLine->SetLineStyle(3);
1787  asimovLine->SetLineWidth(2);
1788  asimovLine->Draw("SAME");
1789  };
1790 
1791  auto drawAsimovMarkerNoLegend = [&](double xValue, double yValue) {
1792  if (!isAsimovChain || !std::isfinite(xValue) || !std::isfinite(yValue) || !gPad) return;
1793  gPad->Update();
1794  const double xMin = gPad->GetUxmin();
1795  const double xMax = gPad->GetUxmax();
1796  const double yMin = gPad->GetUymin();
1797  const double yMax = gPad->GetUymax();
1798  const double dx = 0.012 * (xMax - xMin);
1799  const double dy = 0.012 * (yMax - yMin);
1800 
1801  TLine *l1 = new TLine(xValue - dx, yValue - dy, xValue + dx, yValue + dy);
1802  l1->SetLineColor(kRed);
1803  l1->SetLineWidth(2);
1804  l1->Draw("SAME");
1805 
1806  TLine *l2 = new TLine(xValue - dx, yValue + dy, xValue + dx, yValue - dy);
1807  l2->SetLineColor(kRed);
1808  l2->SetLineWidth(2);
1809  l2->Draw("SAME");
1810  };
1811 
1812  auto drawTrianglePlot = [&](const std::string &weightExpr, const std::string &suffix, int mo = 0) {
1813  if (mo == 1) {
1814  std::cout << "Drawing triangle plot for IO" << std::endl;
1815  triangleVars[4].min = delm23_no_min;
1816  triangleVars[4].max = delm23_no_max;
1817  } else if (mo == -1) {
1818  std::cout << "Drawing triangle plot for NO" << std::endl;
1819  triangleVars[4].min = delm23_io_min;
1820  triangleVars[4].max = delm23_io_max;
1821  } else {
1822  std::cout << "Drawing triangle plot for both MO" << std::endl;
1823  }
1824 
1825  const int nVars = static_cast<int>(triangleVars.size());
1826  TCanvas *cTri = new TCanvas(("c_triangle" + suffix).c_str(), "Triangle Plot", 1400, 1400);
1827  cTri->SetFillStyle(0);
1828  cTri->cd();
1829 
1830  // Use MCMCProcessor-style pad math for non-uniform margins
1831  const double TPm[4] = {.07, .07, .05, .05};
1832  const double Pm[2] = {.2, .1};
1833 
1834  const double TPw = 1.0 - TPm[0] - TPm[2];
1835  const double a_x = (Pm[0] * TPw) / (1.0 * nVars + Pm[0] * (1.0 - 1.0 * nVars));
1836  const double b_x = (TPw - a_x) / (1.0 * nVars);
1837 
1838  std::vector<double> X_Min(nVars), X_Max(nVars);
1839  X_Min[0] = TPm[0];
1840  X_Max[0] = X_Min[0] + a_x + b_x;
1841  for (int i = 1; i < nVars; ++i) {
1842  X_Min[i] = X_Max[i - 1];
1843  X_Max[i] = X_Min[i] + b_x;
1844  }
1845 
1846  const double TPh = 1.0 - TPm[1] - TPm[3];
1847  const double a_y = (Pm[1] * TPh) / (1.0 * nVars + Pm[1] * (1.0 - 1.0 * nVars));
1848  const double b_y = (TPh - a_y) / (1.0 * nVars);
1849 
1850  std::vector<double> Y_Min(nVars), Y_Max(nVars);
1851  Y_Min[nVars - 1] = TPm[1];
1852  Y_Max[nVars - 1] = Y_Min[nVars - 1] + a_y + b_y;
1853  for (int i = nVars - 2; i >= 0; --i) {
1854  Y_Min[i] = Y_Max[i + 1];
1855  Y_Max[i] = Y_Min[i] + b_y;
1856  }
1857 
1858  for (int row = 0; row < nVars; ++row) {
1859  for (int col = 0; col <= row; ++col) {
1860  const double x1 = X_Min[col];
1861  const double x2 = X_Max[col];
1862  const double y1 = Y_Min[row];
1863  const double y2 = Y_Max[row];
1864 
1865  TPad *pad = new TPad(Form("tri_pad_%s_%d_%d", suffix.c_str(), row, col), "", x1, y1, x2, y2);
1866  pad->SetFillStyle(0);
1867  pad->SetRightMargin(0.02);
1868  pad->SetTopMargin(0.02);
1869  pad->SetLeftMargin(col == 0 ? 0.26 : 0.06);
1870  pad->SetBottomMargin(row == nVars - 1 ? 0.18 : 0.06);
1871  pad->Draw();
1872  pad->cd();
1873 
1874  const TriangleVar &xVar = triangleVars[col];
1875  const TriangleVar &yVar = triangleVars[row];
1876 
1877  if (row == col) {
1878  TH1F *h1 = new TH1F(Form("h_tri_1d_%s_%s", suffix.c_str(), xVar.name.c_str()),
1879  "", xVar.bins, xVar.min, xVar.max);
1880  std::string drawExpr = xVar.branch + ">>" + h1->GetName();
1881  tree->Draw(drawExpr.c_str(), weightExpr.c_str(), "goff");
1882  normalizeHist(h1);
1883  h1->SetStats(0);
1884  h1->GetXaxis()->SetTitle(row == nVars - 1 ? xVar.title.c_str() : "");
1885  h1->GetYaxis()->SetTitle(col == 0 ? "Entries" : "");
1886  h1->GetXaxis()->SetLabelSize(row == nVars - 1 ? 0.07 : 0.0);
1887  h1->GetYaxis()->SetLabelSize(col == 0 ? 0.07 : 0.0);
1888  h1->GetXaxis()->SetTitleSize(row == nVars - 1 ? 0.08 : 0.0);
1889  h1->GetYaxis()->SetTitleSize(col == 0 ? 0.08 : 0.0);
1890  h1->Draw("HIST");
1891  h1->Draw("E SAME");
1892  drawAsimovLineNoLegend(xVar.asimov);
1893  } else {
1894  TH2F *h2 = new TH2F(Form("h_tri_2d_%s_%s_%s", suffix.c_str(), yVar.name.c_str(), xVar.name.c_str()),
1895  "", xVar.bins, xVar.min, xVar.max, yVar.bins, yVar.min, yVar.max);
1896  std::string drawExpr = yVar.branch + ":" + xVar.branch + ">>" + h2->GetName();
1897  tree->Draw(drawExpr.c_str(), weightExpr.c_str(), "goff");
1898  normalizeHist(h2);
1899  h2->SetStats(0);
1900  h2->GetXaxis()->SetTitle(row == nVars - 1 ? xVar.title.c_str() : "");
1901  h2->GetYaxis()->SetTitle(col == 0 ? yVar.title.c_str() : "");
1902  h2->GetXaxis()->SetLabelSize(row == nVars - 1 ? 0.07 : 0.0);
1903  h2->GetYaxis()->SetLabelSize(col == 0 ? 0.07 : 0.0);
1904  h2->GetXaxis()->SetTitleSize(row == nVars - 1 ? 0.08 : 0.0);
1905  h2->GetYaxis()->SetTitleSize(col == 0 ? 0.08 : 0.0);
1906  h2->Draw("COLZ");
1907  drawAsimovMarkerNoLegend(xVar.asimov, yVar.asimov);
1908  }
1909  // return to canvas for next pad
1910  cTri->cd();
1911  }
1912  }
1913 
1914  cTri->Update();
1915  cTri->SaveAs((outputDir + "triangle" + suffix + ".png").c_str());
1916  };
1917 
1918  drawTrianglePlot("", "_unweighted", 0);
1919  if (hasUmbrellaWeight) {
1920  drawTrianglePlot("umbrella_weight", "_weighted", 0);
1921  } else {
1922  std::cout << "WARNING:::::No umbrella weights found, skipping weighted triangle plot." << std::endl;
1923  }
1924 
1925  drawTrianglePlot("(delm2_23 < 0)","_io_unweighted",-1);
1926  drawTrianglePlot("(delm2_23 > 0)","_no_unweighted",1);
1927  if (hasUmbrellaWeight) {
1928  drawTrianglePlot("(delm2_23 < 0) * umbrella_weight","_io_weighted",-1);
1929  drawTrianglePlot("(delm2_23 > 0) * umbrella_weight","_no_weighted",1);
1930  } else {
1931  std::cout << "WARNING:::::No umbrella weights found, skipping weighted triangle plots marginalized by ordering." << std::endl;
1932  }
1933 }
1934 } // namespace M3
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:60
Module for Umbrella sampling plot analysis.
Extended ArgumentParser with MaCh3-specific functionality.
Definition: m3argparse.hpp:17
std::unique_ptr< MaCh3ArgumentParser > m_parser
Argument parser for this plugin.
Definition: plugin.hpp:47
int Run() override
Execute the Umbrella sampling plot analysis.
MaCh3ArgumentParser * get_parser() override
Get the argument parser for this module.
void UmbrellaPlotting(std::string InputFile, std::string outputDir)
virtual ~UmbrellaPlotModule()
Destructor.
Main namespace for MaCh3 software.