ThinSliceSample.cxx
Go to the documentation of this file.
1 #include "ThinSliceSample.h"
2 
3 std::string protoana::PreciseToString(const double val, const int n) {
4  std::ostringstream out;
5  out.precision(n);
6  out << std::fixed << val;
7  return out.str();
8 }
9 
11  std::string name, int flux_type,
12  const std::vector<fhicl::ParameterSet> & selections,
13  const std::vector<double> & incident_bins,
14  const std::vector<double> & true_incident_bins,
15  size_t beam_energy_bin,
16  bool is_signal, std::pair<double, double> range)
17  : fSampleName(name),
18  fFluxType(flux_type),
19  fIsSignal(is_signal),
20  fRange(range) {
21 
22  std::string inc_name = "";
23  std::string title = name +
24  (is_signal ?
25  ("(" + protoana::PreciseToString(range.first) + " "
26  + protoana::PreciseToString(range.second) + ")") :
27  "") +
28  ";Reconstructed KE (MeV)";
29  if (is_signal) {
30  inc_name = "sample_" + name + "_" +
31  protoana::PreciseToString(range.first) + "_" +
32  protoana::PreciseToString(range.second) + "_" +
33  "_incident_hist_" +
34  std::to_string(beam_energy_bin);
35  }
36  else {
37  inc_name = "sample_" + name + "_incident_hist_" + std::to_string(beam_energy_bin);
38  }
39  inc_name += ("_" + std::to_string(beam_energy_bin));
40  //fIncidentHist = TH1D(inc_name.c_str(), title.c_str(), incident_bins.size() - 1,
41  // &incident_bins[0]);
42 
43  inc_name += "_true";
44  fTrueIncidentHist = TH1D(inc_name.c_str(), title.c_str(),
45  true_incident_bins.size() - 1,
46  &true_incident_bins[0]);
47 
48  for (auto it = selections.begin(); it != selections.end(); ++it) {
49  std::string sel_name = "";
50  if (is_signal) {
51  sel_name = "sample_" + name + "_" +
52  protoana::PreciseToString(range.first) + "_" +
53  protoana::PreciseToString(range.second) + "_selected_" +
54  it->get<std::string>("Name") + "_hist";
55  }
56  else {
57  sel_name = "sample_" + name + "_selected_" +
58  it->get<std::string>("Name") + "_hist";
59  }
60  sel_name += "_" + std::to_string(beam_energy_bin);
61 
62  std::vector<std::vector<double>> selected_bins
63  = it->get<std::vector<std::vector<double>>>("RecoBins");
64  if (selected_bins.size() == 1) {
65  fSelectionHists[it->get<int>("ID")] = new TH1D(
66  sel_name.c_str(), title.c_str(), selected_bins[0].size() - 1,
67  &selected_bins[0][0]);
68  }
69  else if (selected_bins.size() == 2) {
70  fSelectionHists[it->get<int>("ID")] = new TH2D(
71  sel_name.c_str(), title.c_str(),
72  selected_bins[0].size() - 1, &selected_bins[0][0],
73  selected_bins[1].size() - 1, &selected_bins[1][0]);
74  }
75  else if (selected_bins.size() == 3) {
76  fSelectionHists[it->get<int>("ID")] = new TH3D(
77  sel_name.c_str(), title.c_str(),
78  selected_bins[0].size() - 1, &selected_bins[0][0],
79  selected_bins[1].size() - 1, &selected_bins[1][0],
80  selected_bins[2].size() - 1, &selected_bins[2][0]);
81  }
82  /*else {
83  throw
84  }*/
85  }
87 }
88 
90  if (!fMadeRebinned) {
91  //std::string inc_name = fIncidentHist.GetName();
92  //inc_name += "Rebinned";
93  //fIncidentHistRebinned = TH1D(inc_name.c_str(), fIncidentHist.GetTitle(),
94  // fIncidentHist.GetNbinsX(), 0, fIncidentHist.GetNbinsX());
95  //for (int i = 1; i <= fIncidentHist.GetNbinsX(); ++i) {
96  // fIncidentHistRebinned.SetBinContent(i, fIncidentHist.GetBinContent(i));
97 
98  // double low_edge = fIncidentHist.GetXaxis()->GetBinLowEdge(i);
99  // double up_edge = fIncidentHist.GetXaxis()->GetBinUpEdge(i);
100  // std::string bin_label = (low_edge < 0. ? "< 0." :
101  // (protoana::PreciseToString(low_edge, 0) + " - " +
102  // protoana::PreciseToString(up_edge, 0)));
103  // fIncidentHistRebinned.GetXaxis()->SetBinLabel(i, bin_label.c_str());
104  //}
105 
106  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
107  TH1 * sel_hist = (TH1 *)it->second;
108  std::string name = sel_hist->GetName();
109  name += "Rebinned";
110 
111  size_t nAxes = 1;
112  if (sel_hist->GetNbinsY() > 1) ++nAxes;
113  if (sel_hist->GetNbinsZ() > 1) ++nAxes;
114 
115  if (nAxes == 1) {
116  fSelectionHistsRebinned[it->first] = new TH1D(
117  name.c_str(), sel_hist->GetTitle(),
118  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX());
119  Rebin1D(sel_hist, fSelectionHistsRebinned[it->first]);
120  }
121  else if (nAxes == 2) {
122  fSelectionHistsRebinned[it->first] = new TH2D(
123  name.c_str(), sel_hist->GetTitle(),
124  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX(),
125  sel_hist->GetNbinsY(), 0, sel_hist->GetNbinsY());
126  Rebin2D(sel_hist, fSelectionHistsRebinned[it->first]);
127  }
128  else if (nAxes == 3) {
129  fSelectionHistsRebinned[it->first] = new TH3D(
130  name.c_str(), sel_hist->GetTitle(),
131  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX(),
132  sel_hist->GetNbinsY(), 0, sel_hist->GetNbinsY(),
133  sel_hist->GetNbinsZ(), 0, sel_hist->GetNbinsZ());
134  Rebin3D(sel_hist, fSelectionHistsRebinned[it->first]);
135  }
136  }
137 
138  fMadeRebinned = true;
139  }
140 }
141 
142 void protoana::ThinSliceSample::Rebin1D(TH1 * sel_hist, TH1 * rebinned) {
143  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
144  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
145  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
146  std::string bin_label = (low_x < 0. ? "< 0." :
147  (protoana::PreciseToString(low_x, 0) + " - " +
148  protoana::PreciseToString(up_x, 0)));
149  rebinned->GetXaxis()->SetBinLabel(i, bin_label.c_str());
150 
151  rebinned->SetBinContent(i, sel_hist->GetBinContent(i));
152  }
153 }
154 
155 void protoana::ThinSliceSample::Rebin2D(TH1 * sel_hist, TH1 * rebinned) {
156  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
157  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
158  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
159  std::string bin_label = (low_x < 0. ? "< 0." :
160  (protoana::PreciseToString(low_x, 0) + " - " +
161  protoana::PreciseToString(up_x, 0)));
162  rebinned->GetXaxis()->SetBinLabel(
163  i, bin_label.c_str());
164  for (int j = 1; j <= sel_hist->GetNbinsY(); ++j) {
165  double low_y = sel_hist->GetYaxis()->GetBinLowEdge(j);
166  double up_y = sel_hist->GetYaxis()->GetBinUpEdge(j);
167  std::string y_label = (low_y < 0. ? "< 0." :
168  (protoana::PreciseToString(low_y, 0) + " - " +
169  protoana::PreciseToString(up_y, 0)));
170  rebinned->GetYaxis()->SetBinLabel(j, bin_label.c_str());
171  rebinned->SetBinContent(i, j, sel_hist->GetBinContent(i, j));
172  }
173  }
174 }
175 
176 void protoana::ThinSliceSample::Rebin3D(TH1 * sel_hist, TH1 * rebinned) {
177  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
178  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
179  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
180  std::string bin_label = (low_x < 0. ? "< 0." :
181  (protoana::PreciseToString(low_x, 0) + " - " +
182  protoana::PreciseToString(up_x, 0)));
183  rebinned->GetXaxis()->SetBinLabel(i, bin_label.c_str());
184  for (int j = 1; j <= sel_hist->GetNbinsY(); ++j) {
185  double low_y = sel_hist->GetYaxis()->GetBinLowEdge(j);
186  double up_y = sel_hist->GetYaxis()->GetBinUpEdge(j);
187  std::string y_label = (low_y < 0. ? "< 0." :
188  (protoana::PreciseToString(low_y, 0) + " - " +
189  protoana::PreciseToString(up_y, 0)));
190  rebinned->GetYaxis()->SetBinLabel(j, bin_label.c_str());
191 
192  for (int k = 1; k <= sel_hist->GetNbinsY(); ++k) {
193  double low_z = sel_hist->GetYaxis()->GetBinLowEdge(k);
194  double up_z = sel_hist->GetYaxis()->GetBinUpEdge(k);
195  std::string y_label = (low_z < 0. ? "< 0." :
196  (protoana::PreciseToString(low_z, 0) + " - " +
197  protoana::PreciseToString(up_z, 0)));
198  rebinned->GetZaxis()->SetBinLabel(k, bin_label.c_str());
199 
200  rebinned->SetBinContent(i, j, k, sel_hist->GetBinContent(i, j, k));
201  }
202  }
203  }
204 }
205 
207  //for (int i = 1; i <= fIncidentHist.GetNbinsX(); ++i) {
208  // fIncidentHistRebinned.SetBinContent(i, fIncidentHist.GetBinContent(i));
209  //}
210  for (auto it = fSelectionHistsRebinned.begin();
211  it != fSelectionHistsRebinned.end(); ++it) {
212  for (int i = 1; i <= it->second->GetNbinsX(); ++i) {
213  it->second->SetBinContent(i, fSelectionHists[it->first]->GetBinContent(i));
214  }
215  }
216 }
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
std::void_t< T > n
void Rebin1D(TH1 *sel_hist, TH1 *rebinned)
ThinSliceSample(std::string name, int flux_type, const std::vector< fhicl::ParameterSet > &selections, const std::vector< double > &incident_bins, const std::vector< double > &true_incident_bins, size_t beam_energy_bin, bool is_signal=false, std::pair< double, double > range={0., 0.})
std::map< int, TH1 * > fSelectionHistsRebinned
std::map< int, TH1 * > fSelectionHists
void Rebin3D(TH1 *sel_hist, TH1 *rebinned)
std::string PreciseToString(const double val, const int n=2)
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void Rebin2D(TH1 *sel_hist, TH1 *rebinned)