Public Member Functions | Private Member Functions | Private Attributes | List of all members
protoana::ThinSliceDataSet Class Reference

#include <ThinSliceDataSet.h>

Public Member Functions

 ThinSliceDataSet ()
 
 ThinSliceDataSet (const std::vector< double > &incident_bins, const std::vector< fhicl::ParameterSet > &selections)
 
 ~ThinSliceDataSet ()
 
const std::map< int, std::string > & GetSelectionNames () const
 
std::stringGetSelectionName (int id)
 
std::map< int, TH1 * > & GetSelectionHists ()
 
std::map< int, TH1 * > & GetRebinnedSelectionHists ()
 
TH1 * GetSelectionHist (int id)
 
TH1D & GetIncidentHist ()
 
TH1D & GetRebinnedIncidentHist ()
 
TH1 * GetRebinnedSelectionHist (int id)
 
void FillIncidentHist (const std::vector< double > &vals)
 
void FillSelectionHist (int id, double val)
 
template<size_t N>
void FillSelectionHist (int id, const double(&vals)[N])
 
void MakeRebinnedHists ()
 
void Refill1DRebinned ()
 
void GetCumulatives ()
 
void GenerateStatFluctuation ()
 
void FillHistsFromSamples (const std::map< int, std::vector< std::vector< ThinSliceSample >>> &samples, double &flux)
 

Private Member Functions

void Rebin1D (TH1 *sel_hist, TH1 *rebinned)
 
void Rebin2D (TH1 *sel_hist, TH1 *rebinned)
 
void Rebin3D (TH1 *sel_hist, TH1 *rebinned)
 

Private Attributes

std::map< int, TH1 * > fSelectionHists
 
TH1D fIncidentHist
 
std::map< int, TH1 * > fSelectionHistsRebinned
 
TH1D fIncidentHistRebinned
 
bool fMadeRebinned = false
 
std::map< int, std::stringfSelectionNames
 
std::vector< std::pair< std::pair< int, int >, double > > fCumulatives
 
TRandom3 fRNG = TRandom3(0)
 
double fTotal
 

Detailed Description

Definition at line 16 of file ThinSliceDataSet.h.

Constructor & Destructor Documentation

protoana::ThinSliceDataSet::ThinSliceDataSet ( )
inline

Definition at line 18 of file ThinSliceDataSet.h.

18 {};
protoana::ThinSliceDataSet::ThinSliceDataSet ( const std::vector< double > &  incident_bins,
const std::vector< fhicl::ParameterSet > &  selections 
)

Definition at line 4 of file ThinSliceDataSet.cxx.

6  {
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 }
std::string string
Definition: nybbler.cc:12
std::map< int, std::string > fSelectionNames
std::map< int, TH1 * > fSelectionHists
protoana::ThinSliceDataSet::~ThinSliceDataSet ( )
inline

Definition at line 21 of file ThinSliceDataSet.h.

21 {};

Member Function Documentation

void protoana::ThinSliceDataSet::FillHistsFromSamples ( const std::map< int, std::vector< std::vector< ThinSliceSample >>> &  samples,
double &  flux 
)

Definition at line 250 of file ThinSliceDataSet.cxx.

252  {
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 }
std::map< int, TH1 * > fSelectionHists
void protoana::ThinSliceDataSet::FillIncidentHist ( const std::vector< double > &  vals)
inline

Definition at line 55 of file ThinSliceDataSet.h.

55  {
56  for (size_t i = 0; i < vals.size(); ++i) {
57  fIncidentHist.Fill(vals.at(i));
58  }
59  };
void protoana::ThinSliceDataSet::FillSelectionHist ( int  id,
double  val 
)
inline

Definition at line 61 of file ThinSliceDataSet.h.

61  {
62  if (fSelectionHists.find(id) != fSelectionHists.end()) {
63  fSelectionHists.at(id)->Fill(val);
64  }
65  };
std::map< int, TH1 * > fSelectionHists
template<size_t N>
void protoana::ThinSliceDataSet::FillSelectionHist ( int  id,
const double(&)  vals[N] 
)
inline

Definition at line 67 of file ThinSliceDataSet.h.

67  {
68  if (N < 1 || N > 3) {
69  std::string message = "Error: trying to fill hists with too many values";
70  message += std::to_string(N);
71  throw std::runtime_error(message);
72  }
73 
74  if (fSelectionHists.find(id) != fSelectionHists.end()) {
75  if (N == 1) {
76  fSelectionHists.at(id)->Fill(vals[0]);
77  }
78  else if (N == 2) {
79  ((TH2D*)fSelectionHists.at(id))->Fill(vals[0], vals[1]);
80  }
81  else if (N == 3) {
82  ((TH3D*)fSelectionHists.at(id))->Fill(vals[0], vals[1], vals[2]);
83  }
84  }
85  }
std::string string
Definition: nybbler.cc:12
std::map< int, TH1 * > fSelectionHists
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void protoana::ThinSliceDataSet::GenerateStatFluctuation ( )

Definition at line 192 of file ThinSliceDataSet.cxx.

192  {
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 }
std::vector< std::pair< std::pair< int, int >, double > > fCumulatives
QTextStream & bin(QTextStream &s)
std::map< int, TH1 * > fSelectionHists
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
void protoana::ThinSliceDataSet::GetCumulatives ( )
inline

Definition at line 90 of file ThinSliceDataSet.h.

90  {
91  fTotal = 0;
92  std::cout << "Making cumulatives" << std::endl;
93  for (auto it = fSelectionHists.begin();
94  it != fSelectionHists.end(); ++it) {
95  for (int i = 1; i <= it->second->GetNbinsX(); ++i) {
96  double val = it->second->GetBinContent(i);
97  if (fCumulatives.size()) {
98  val += fCumulatives.back().second;
99  }
100  fCumulatives.push_back({{it->first, i}, val});
101  fTotal += it->second->GetBinContent(i);
102  std::cout << fCumulatives.back().second << " " << fTotal << std::endl;
103  }
104  }
105 
106  for (auto it = fCumulatives.begin(); it != fCumulatives.end(); ++it) {
107  it->second /= fTotal;
108  }
109 
110  std::sort(fCumulatives.begin(), fCumulatives.end(),
111  [](auto a, auto b){return (a.second > b.second);});
112  std::cout << "N Cumulatives: " << fCumulatives.size() << std::endl;
113  for (auto c : fCumulatives) {
114  std::cout << c.second << std::endl;
115  }
116  };
std::vector< std::pair< std::pair< int, int >, double > > fCumulatives
const double a
static bool * b
Definition: config.cpp:1043
std::map< int, TH1 * > fSelectionHists
QTextStream & endl(QTextStream &s)
TH1D& protoana::ThinSliceDataSet::GetIncidentHist ( )
inline

Definition at line 43 of file ThinSliceDataSet.h.

43  {
44  return fIncidentHist;
45  };
TH1D& protoana::ThinSliceDataSet::GetRebinnedIncidentHist ( )
inline

Definition at line 47 of file ThinSliceDataSet.h.

47  {
48  return fIncidentHistRebinned;
49  };
TH1* protoana::ThinSliceDataSet::GetRebinnedSelectionHist ( int  id)
inline

Definition at line 51 of file ThinSliceDataSet.h.

51  {
52  return fSelectionHistsRebinned.at(id);
53  };
std::map< int, TH1 * > fSelectionHistsRebinned
std::map<int, TH1 *>& protoana::ThinSliceDataSet::GetRebinnedSelectionHists ( )
inline

Definition at line 35 of file ThinSliceDataSet.h.

35  {
37  };
std::map< int, TH1 * > fSelectionHistsRebinned
TH1* protoana::ThinSliceDataSet::GetSelectionHist ( int  id)
inline

Definition at line 39 of file ThinSliceDataSet.h.

39  {
40  return fSelectionHists.at(id);
41  };
std::map< int, TH1 * > fSelectionHists
std::map<int, TH1 *>& protoana::ThinSliceDataSet::GetSelectionHists ( )
inline

Definition at line 31 of file ThinSliceDataSet.h.

31  {
32  return fSelectionHists;
33  };
std::map< int, TH1 * > fSelectionHists
std::string& protoana::ThinSliceDataSet::GetSelectionName ( int  id)
inline

Definition at line 27 of file ThinSliceDataSet.h.

27  {
28  return fSelectionNames.at(id);
29  }
std::map< int, std::string > fSelectionNames
const std::map<int, std::string>& protoana::ThinSliceDataSet::GetSelectionNames ( ) const
inline

Definition at line 23 of file ThinSliceDataSet.h.

23  {
24  return fSelectionNames;
25  };
std::map< int, std::string > fSelectionNames
void protoana::ThinSliceDataSet::MakeRebinnedHists ( )

Definition at line 49 of file ThinSliceDataSet.cxx.

49  {
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 }
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
void Rebin3D(TH1 *sel_hist, TH1 *rebinned)
void Rebin2D(TH1 *sel_hist, TH1 *rebinned)
std::map< int, TH1 * > fSelectionHistsRebinned
void Rebin1D(TH1 *sel_hist, TH1 *rebinned)
std::string PreciseToString(const double val, const int n=2)
std::map< int, TH1 * > fSelectionHists
void protoana::ThinSliceDataSet::Rebin1D ( TH1 *  sel_hist,
TH1 *  rebinned 
)
private

Definition at line 128 of file ThinSliceDataSet.cxx.

128  {
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 }
std::string string
Definition: nybbler.cc:12
std::string PreciseToString(const double val, const int n=2)
void protoana::ThinSliceDataSet::Rebin2D ( TH1 *  sel_hist,
TH1 *  rebinned 
)
private

Definition at line 141 of file ThinSliceDataSet.cxx.

141  {
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 }
std::string string
Definition: nybbler.cc:12
std::string PreciseToString(const double val, const int n=2)
void protoana::ThinSliceDataSet::Rebin3D ( TH1 *  sel_hist,
TH1 *  rebinned 
)
private

Definition at line 162 of file ThinSliceDataSet.cxx.

162  {
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 }
std::string string
Definition: nybbler.cc:12
std::string PreciseToString(const double val, const int n=2)
void protoana::ThinSliceDataSet::Refill1DRebinned ( )

Definition at line 119 of file ThinSliceDataSet.cxx.

119  {
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 }
std::map< int, TH1 * > fSelectionHistsRebinned
std::map< int, TH1 * > fSelectionHists

Member Data Documentation

std::vector<std::pair<std::pair<int, int>, double> > protoana::ThinSliceDataSet::fCumulatives
private

Definition at line 134 of file ThinSliceDataSet.h.

TH1D protoana::ThinSliceDataSet::fIncidentHist
private

Definition at line 129 of file ThinSliceDataSet.h.

TH1D protoana::ThinSliceDataSet::fIncidentHistRebinned
private

Definition at line 131 of file ThinSliceDataSet.h.

bool protoana::ThinSliceDataSet::fMadeRebinned = false
private

Definition at line 132 of file ThinSliceDataSet.h.

TRandom3 protoana::ThinSliceDataSet::fRNG = TRandom3(0)
private

Definition at line 135 of file ThinSliceDataSet.h.

std::map<int, TH1 *> protoana::ThinSliceDataSet::fSelectionHists
private

Definition at line 128 of file ThinSliceDataSet.h.

std::map<int, TH1 *> protoana::ThinSliceDataSet::fSelectionHistsRebinned
private

Definition at line 130 of file ThinSliceDataSet.h.

std::map<int, std::string> protoana::ThinSliceDataSet::fSelectionNames
private

Definition at line 133 of file ThinSliceDataSet.h.

double protoana::ThinSliceDataSet::fTotal
private

Definition at line 136 of file ThinSliceDataSet.h.


The documentation for this class was generated from the following files: