ThinSliceDataSet.cxx
Go to the documentation of this file.
1 #include "ThinSliceDataSet.h"
2 #include "ThinSliceSample.h"
3 
5  const std::vector<double> & incident_bins,
6  const std::vector<fhicl::ParameterSet> & selections) {
7  fIncidentHist = TH1D("Data_incident_hist",
8  "Data;Reconstructed KE (MeV)",
9  incident_bins.size() - 1,
10  &incident_bins[0]);
11  for (auto it = selections.begin(); it != selections.end(); ++it) {
12  fSelectionNames[it->get<int>("ID")] = it->get<std::string>("Name");
13  std::string sel_name = "Data_selected_" + it->get<std::string>("Name") +
14  "_hist";
15  std::vector<std::vector<double>> selected_bins =
16  it->get<std::vector<std::vector<double>>>("RecoBins");
17 
18  std::vector<std::string> titles =
19  it->get<std::vector<std::string>>("AxisTitles");
20  TString title = "Data";
21  for (auto & t : titles) {
22  title += ";" + t;
23  }
24 
25  if (selected_bins.size() == 1) {
26  fSelectionHists[it->get<int>("ID")] = new TH1D(
27  sel_name.c_str(), title/*.c_str()"Data;Reconstructed KE (MeV)"*/,
28  selected_bins[0].size() - 1, &selected_bins[0][0]);
29  }
30  else if (selected_bins.size() == 2) {
31  fSelectionHists[it->get<int>("ID")] = new TH2D(
32  sel_name.c_str(), title/*.c_str()"Data;Reconstructed KE (MeV)"*/,
33  selected_bins[0].size() - 1, &selected_bins[0][0],
34  selected_bins[1].size() - 1, &selected_bins[1][0]);
35  }
36  else if (selected_bins.size() == 3) {
37  fSelectionHists[it->get<int>("ID")] = new TH3D(
38  sel_name.c_str(), title/*.c_str()"Data;Reconstructed KE (MeV)"*/,
39  selected_bins[0].size() - 1, &selected_bins[0][0],
40  selected_bins[1].size() - 1, &selected_bins[1][0],
41  selected_bins[2].size() - 1, &selected_bins[2][0]);
42  }
43  /*else {
44  * throw
45  * }*/
46  }
47 }
48 
50  if (!fMadeRebinned) {
51  std::string inc_name = fIncidentHist.GetName();
52  inc_name += "Rebinned";
53  fIncidentHistRebinned = TH1D(inc_name.c_str(), fIncidentHist.GetTitle(),
54  fIncidentHist.GetNbinsX(), 0, fIncidentHist.GetNbinsX());
55  for (int i = 1; i <= fIncidentHist.GetNbinsX(); ++i) {
56  fIncidentHistRebinned.SetBinContent(i, fIncidentHist.GetBinContent(i));
57 
58  double low_edge = fIncidentHist.GetXaxis()->GetBinLowEdge(i);
59  double up_edge = fIncidentHist.GetXaxis()->GetBinUpEdge(i);
60  std::string bin_label = (low_edge < 0. ? "< 0." :
61  (protoana::PreciseToString(low_edge, 0) + " - " +
62  protoana::PreciseToString(up_edge, 0)));
63  fIncidentHistRebinned.GetXaxis()->SetBinLabel(i, bin_label.c_str());
64  }
65 
66  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
67  TH1 * sel_hist = (TH1 *)it->second;
68  std::string name = sel_hist->GetName();
69  name += "Rebinned";
70 
71  size_t nAxes = 1;
72  if (sel_hist->GetNbinsY() > 1) ++nAxes;
73  if (sel_hist->GetNbinsZ() > 1) ++nAxes;
74 
75  if (nAxes == 1) {
76  TString title = sel_hist->GetTitle();
77  title += ";";
78  title += sel_hist->GetXaxis()->GetTitle();
79  fSelectionHistsRebinned[it->first] = new TH1D(
80  name.c_str(), title/*.c_str()sel_hist->GetTitle()*/,
81  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX());
82  Rebin1D(sel_hist, fSelectionHistsRebinned[it->first]);
83  }
84  else if (nAxes == 2) {
85  std::string title = sel_hist->GetTitle();
86  title += ";";
87  title += sel_hist->GetXaxis()->GetTitle();
88  title += ";";
89  title += sel_hist->GetYaxis()->GetTitle();
90 
91  fSelectionHistsRebinned[it->first] = new TH2D(
92  name.c_str(), title.c_str()/*sel_hist->GetTitle()*/,
93  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX(),
94  sel_hist->GetNbinsY(), 0, sel_hist->GetNbinsY());
95  Rebin2D(sel_hist, fSelectionHistsRebinned[it->first]);
96  }
97  else if (nAxes == 3) {
98  std::string title = sel_hist->GetTitle();
99  title += ";";
100  title += sel_hist->GetXaxis()->GetTitle();
101  title += ";";
102  title += sel_hist->GetYaxis()->GetTitle();
103  title += ";";
104  title += sel_hist->GetZaxis()->GetTitle();
105 
106  fSelectionHistsRebinned[it->first] = new TH3D(
107  name.c_str(), title.c_str()/*sel_hist->GetTitle()*/,
108  sel_hist->GetNbinsX(), 0, sel_hist->GetNbinsX(),
109  sel_hist->GetNbinsY(), 0, sel_hist->GetNbinsY(),
110  sel_hist->GetNbinsZ(), 0, sel_hist->GetNbinsZ());
111  Rebin3D(sel_hist, fSelectionHistsRebinned[it->first]);
112  }
113  }
114 
115  fMadeRebinned = true;
116  }
117 }
118 
120  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
121  for (int i = 1; i <= it->second->GetNbinsX(); ++i) {
122  fSelectionHistsRebinned[it->first]->SetBinContent(
123  i, it->second->GetBinContent(i));
124  }
125  }
126 }
127 
128 void protoana::ThinSliceDataSet::Rebin1D(TH1 * sel_hist, TH1 * rebinned) {
129  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
130  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
131  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
132  std::string bin_label = (low_x < 0. ? "< 0." :
133  (protoana::PreciseToString(low_x, 0) + " - " +
134  protoana::PreciseToString(up_x, 0)));
135  rebinned->GetXaxis()->SetBinLabel(i, bin_label.c_str());
136 
137  rebinned->SetBinContent(i, sel_hist->GetBinContent(i));
138  }
139 }
140 
141 void protoana::ThinSliceDataSet::Rebin2D(TH1 * sel_hist, TH1 * rebinned) {
142  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
143  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
144  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
145  std::string bin_label = (low_x < 0. ? "< 0." :
146  (protoana::PreciseToString(low_x, 0) + " - " +
147  protoana::PreciseToString(up_x, 0)));
148  rebinned->GetXaxis()->SetBinLabel(
149  i, bin_label.c_str());
150  for (int j = 1; j <= sel_hist->GetNbinsY(); ++j) {
151  double low_y = sel_hist->GetYaxis()->GetBinLowEdge(j);
152  double up_y = sel_hist->GetYaxis()->GetBinUpEdge(j);
153  std::string y_label = (low_y < 0. ? "< 0." :
154  (protoana::PreciseToString(low_y, 0) + " - " +
155  protoana::PreciseToString(up_y, 0)));
156  rebinned->GetYaxis()->SetBinLabel(j, bin_label.c_str());
157  rebinned->SetBinContent(i, j, sel_hist->GetBinContent(i, j));
158  }
159  }
160 }
161 
162 void protoana::ThinSliceDataSet::Rebin3D(TH1 * sel_hist, TH1 * rebinned) {
163  for (int i = 1; i <= sel_hist->GetNbinsX(); ++i) {
164  double low_x = sel_hist->GetXaxis()->GetBinLowEdge(i);
165  double up_x = sel_hist->GetXaxis()->GetBinUpEdge(i);
166  std::string bin_label = (low_x < 0. ? "< 0." :
167  (protoana::PreciseToString(low_x, 0) + " - " +
168  protoana::PreciseToString(up_x, 0)));
169  rebinned->GetXaxis()->SetBinLabel(i, bin_label.c_str());
170  for (int j = 1; j <= sel_hist->GetNbinsY(); ++j) {
171  double low_y = sel_hist->GetYaxis()->GetBinLowEdge(j);
172  double up_y = sel_hist->GetYaxis()->GetBinUpEdge(j);
173  std::string y_label = (low_y < 0. ? "< 0." :
174  (protoana::PreciseToString(low_y, 0) + " - " +
175  protoana::PreciseToString(up_y, 0)));
176  rebinned->GetYaxis()->SetBinLabel(j, bin_label.c_str());
177 
178  for (int k = 1; k <= sel_hist->GetNbinsY(); ++k) {
179  double low_z = sel_hist->GetYaxis()->GetBinLowEdge(k);
180  double up_z = sel_hist->GetYaxis()->GetBinUpEdge(k);
181  std::string y_label = (low_z < 0. ? "< 0." :
182  (protoana::PreciseToString(low_z, 0) + " - " +
183  protoana::PreciseToString(up_z, 0)));
184  rebinned->GetZaxis()->SetBinLabel(k, bin_label.c_str());
185 
186  rebinned->SetBinContent(i, j, k, sel_hist->GetBinContent(i, j, k));
187  }
188  }
189  }
190 }
191 
193 
194  //bool retry = true;
195  //while (retry) {
196  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
197  it->second->Reset();
198  //fSelectionHistsRebinned[it->first]->Reset();
199  }
200 
201  for (int i = 0; i < fTotal; ++i) {
202  double r = fRNG.Uniform();
203  std::pair<int, int> bin;
204  for (size_t j = 0; j < fCumulatives.size(); ++j) {
205  //std::cout << fCumulatives[j].second << " " << r <<
206  // " " << fCumulatives[j].second - r << std::endl;
207  if ((fCumulatives[j].second - r) > 0.) {
208  bin = fCumulatives[j].first;
209  }
210  else {
211  break;
212  }
213  }
214  //std::cout << "Found bin: " << bin.first << " " << bin.second << std::endl;
215  fSelectionHists[bin.first]->AddBinContent(bin.second);
216  //fSelectionHistsRebinned[bin.first]->AddBinContent(bin.second);
217  }
218 
219  //bool good = true;
220  /*
221  double new_total = 0.;
222  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
223  for (int i = 1; i <= it->second->GetNbinsX(); ++i) {
224  if (it->second->GetBinContent(i) < 1.) {
225  std::cout << "DataSet fluctuation: Found bin with 0 content. Adding" << std::endl;
226  it->second->AddBinContent(i);
227  //fSelectionHistsRebinned[it->first]->AddBinContent(i);
228  //good = false;
229  }
230  new_total += it->second->GetBinContent(i);
231  }
232  }
233 
234  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
235  it->second->Scale(new_total/fTotal);
236  //fSelectionHistsRebinned[it->first]->Scale(new_total/fTotal);
237  }
238  */
239 
241 
242  //retry = !good;
243  //}
244 
245  // std::cout << "Bin vals: ";
246  // std::cout << bin.second << " ";
247  // std::cout << std::endl;
248 }
249 
251  const std::map<int, std::vector<std::vector<ThinSliceSample>>> & samples,
252  double & flux) {
253 
254  flux = 0.;
255  for (auto it = fSelectionHists.begin(); it != fSelectionHists.end(); ++it) {
256  it->second->Reset();
257  }
258 
259  for (auto it = samples.begin(); it != samples.end(); ++it) {
260  for (size_t i = 0; i < it->second.size(); ++i) {
261  for (size_t j = 0; j < it->second[i].size(); ++j) {
262  const auto & hists = it->second[i][j].GetSelectionHists();
263  for (auto it2 = hists.begin(); it2 != hists.end(); ++it2) {
264  fSelectionHists[it2->first]->Add(it2->second);
265  flux += it2->second->Integral();
266  }
267  }
268  }
269  }
270 }
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
struct vector vector
void Rebin3D(TH1 *sel_hist, TH1 *rebinned)
std::vector< std::pair< std::pair< int, int >, double > > fCumulatives
void Rebin2D(TH1 *sel_hist, TH1 *rebinned)
std::map< int, TH1 * > fSelectionHistsRebinned
void FillHistsFromSamples(const std::map< int, std::vector< std::vector< ThinSliceSample >>> &samples, double &flux)
void Rebin1D(TH1 *sel_hist, TH1 *rebinned)
std::map< int, std::string > fSelectionNames
QTextStream & bin(QTextStream &s)
std::string PreciseToString(const double val, const int n=2)
std::map< int, TH1 * > fSelectionHists
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85