All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Binning.cxx
Go to the documentation of this file.
2 
3 #include "TDirectory.h"
4 #include "TH1.h"
5 #include "TObjString.h"
6 #include "TVectorD.h"
7 
8 namespace ana
9 {
10  int Binning::fgNextID = 0;
11 
12  //----------------------------------------------------------------------
14  : fID(-1)
15  {
16  }
17 
18  //----------------------------------------------------------------------
19  Binning Binning::SimpleHelper(int n, double lo, double hi,
20  const std::vector<std::string>& labels)
21  {
22  assert(labels.empty() || int(labels.size()) == n);
23 
24  Binning bins;
25  bins.fNBins = n;
26  bins.fMin = lo;
27  bins.fMax = hi;
28  bins.fEdges.resize(n+1);
29  for (int i = 0; i <= n; i++)
30  bins.fEdges[i] = lo + i*(hi-lo)/n;
31  bins.fLabels = labels;
32  bins.fIsSimple = true;
33 
34  return bins;
35  }
36 
37  //----------------------------------------------------------------------
38  Binning Binning::Simple(int n, double lo, double hi,
39  const std::vector<std::string>& labels)
40  {
41  Binning bins = SimpleHelper(n, lo, hi, labels);
42 
43  auto it = IDMap().find(bins);
44  if(it == IDMap().end()){
45  bins.fID = fgNextID++;
46  IDMap().emplace(bins, bins.fID);
47  }
48  else{
49  bins.fID = it->second;
50  }
51 
52  return bins;
53  }
54 
55  //----------------------------------------------------------------------
56  Binning Binning::LogUniform(int n, double lo, double hi)
57  {
58  std::vector<double> edges(n+1);
59  const double logSpacing = exp( (log(hi) - log(lo)) / n );
60  for (int i = 0; i <= n; ++i) {
61  if (i == 0) edges[i] = lo;
62  else edges[i] = logSpacing*edges[i-1];
63  }
64  return Custom(edges);
65  }
66 
67  //----------------------------------------------------------------------
68  Binning Binning::CustomHelper(const std::vector<double>& edges)
69  {
70  assert(edges.size() > 1);
71 
72  Binning bins;
73  bins.fEdges = edges;
74  bins.fNBins = edges.size()-1;
75  bins.fMin = edges.front();
76  bins.fMax = edges.back();
77  bins.fIsSimple = false;
78 
79  return bins;
80  }
81 
82  //----------------------------------------------------------------------
83  Binning Binning::Custom(const std::vector<double>& edges)
84  {
85  Binning bins = CustomHelper(edges);
86 
87  auto it = IDMap().find(bins);
88  if(it == IDMap().end()){
89  bins.fID = fgNextID++;
90  IDMap().emplace(bins, bins.fID);
91  }
92  else{
93  bins.fID = it->second;
94  }
95 
96  return bins;
97  }
98 
99  //----------------------------------------------------------------------
100  int Binning::FindBin(float x) const
101  {
102  // Treat anything outside [fMin, fMax) at Underflow / Overflow
103  if (x < fMin) return 0; // Underflow
104  if (x >= fMax) return fEdges.size(); // Overflow
105 
106  // Follow ROOT convention, first bin of histogram is bin 1
107 
108  if (this->IsSimple()){
109  double binwidth = (fMax - fMin) / fNBins;
110  int bin = (x - fMin) / binwidth + 1;
111  return bin;
112  }
113 
114  int bin =
115  std::lower_bound(fEdges.begin(), fEdges.end(), x) - fEdges.begin();
116  if (x == fEdges[bin]) bin++;
117  assert(bin >= 0 && bin < (int)fEdges.size());
118  return bin;
119  }
120 
121  //----------------------------------------------------------------------
122  Binning Binning::FromTAxis(const TAxis* ax)
123  {
124  Binning bins;
125 
126  // Evenly spaced binning
127  if(!ax->GetXbins()->GetArray()){
128  bins = SimpleHelper(ax->GetNbins(), ax->GetXmin(), ax->GetXmax());
129  }
130  else{
131  std::vector<double> edges(ax->GetNbins()+1);
132  ax->GetLowEdge(&edges.front());
133  edges[ax->GetNbins()] = ax->GetBinUpEdge(ax->GetNbins());
134 
135  bins = Binning::Custom(edges);
136  }
137 
138  auto it = IDMap().find(bins);
139  if(it != IDMap().end()){
140  bins.fID = it->second;
141  }
142  else{
143  bins.fID = fgNextID++;
144  IDMap().emplace(bins, bins.fID);
145  }
146 
147  return bins;
148  }
149 
150  // Can we give trueE bin a special ID?
151  //----------------------------------------------------------------------
153  {
154  // Osc P is ~sin^2(1/E). Difference in prob across a bin is ~dP/dE which
155  // goes as 1/E^2 times a trigonometric term depending on the parameters but
156  // bounded between -1 and +1. So bins should have width ~1/E^2. E_i~1/i
157  // achieves that.
158 
159  // Binning roughly re-optimized for SBN sterile analyses
160 
161  const int kNumTrueEnergyBins = 80;
162 
163  // N+1 bin low edges
164  std::vector<double> edges(kNumTrueEnergyBins+1);
165 
166  const double Emin = .2; // 200 MeV
167 
168  // How many edges to generate. Allow room for 0-Emin bin
169  const double N = kNumTrueEnergyBins-1;
170  const double A = N*Emin;
171 
172  edges[0] = 0;
173 
174  for(int i = 1; i <= N; ++i){
175  edges[kNumTrueEnergyBins-i] = A/i;
176  }
177 
178  edges[kNumTrueEnergyBins] = 20; // Replace the infinity that would be here
179 
180  return Binning::Custom(edges);
181  }
182 
183 
184  //----------------------------------------------------------------------
186  {
187  // constant binnig
188 
189  const int kNumTrueLOverTrueEBins = 2000;
190  const double klow = 0.0;
191  const double khigh = 5.0;
192 
193  return Binning::Simple(kNumTrueLOverTrueEBins, klow, khigh);
194  }
195 
196  //----------------------------------------------------------------------
197  void Binning::SaveTo(TDirectory* dir) const
198  {
199  TDirectory* tmp = gDirectory;
200  dir->cd();
201 
202  TObjString("Binning").Write("type");
203 
204  TVectorD nminmax(3);
205 
206  nminmax[0] = fNBins;
207  nminmax[1] = fMin;
208  nminmax[2] = fMax;
209 
210  nminmax.Write("nminmax");
211 
212  TVectorD issimple(1);
213  issimple[0] = fIsSimple;
214  issimple.Write("issimple");
215 
216  TVectorD edges(fEdges.size());
217  for(unsigned int i = 0; i < fEdges.size(); ++i)
218  edges[i] = fEdges[i];
219 
220  edges.Write("edges");
221 
222  for(unsigned int i = 0; i < fLabels.size(); ++i)
223  TObjString(fLabels[i].c_str()).Write(TString::Format("label%d", i).Data());
224 
225  tmp->cd();
226  }
227 
228  //----------------------------------------------------------------------
229  std::unique_ptr<Binning> Binning::LoadFrom(TDirectory* dir)
230  {
231  TObjString* tag = (TObjString*)dir->Get("type");
232  assert(tag);
233  assert(tag->GetString() == "Binning");
234 
235  TVectorD* vMinMax = (TVectorD*)dir->Get("nminmax");
236  assert(vMinMax);
237 
238  Binning ret;
239 
240  const TVectorD* issimple = (TVectorD*)dir->Get("issimple");
241  if((*issimple)[0]){
242  ret = Binning::Simple((*vMinMax)[0],
243  (*vMinMax)[1],
244  (*vMinMax)[2]);
245  }
246  else{
247  const TVectorD* vEdges = (TVectorD*)dir->Get("edges");
248  std::vector<double> edges(vEdges->GetNrows());
249  for(int i = 0; i < vEdges->GetNrows(); ++i) edges[i] = (*vEdges)[i];
250 
251  ret = Binning::Custom(edges);
252  }
253 
254  for(unsigned int i = 0; ; ++i){
255  TObjString* s = (TObjString*)dir->Get(TString::Format("label%d", i).Data());
256  if(!s) break;
257  ret.fLabels.push_back(s->GetString().Data());
258  }
259 
260  return std::make_unique<Binning>(ret);
261  }
262 
263  //----------------------------------------------------------------------
264  bool Binning::operator==(const Binning& rhs) const
265  {
266  // NB don't look at ID here or in < because we use these in the maps below
267  // that are used to find the IDs in the first place
268  if(fIsSimple != rhs.fIsSimple) return false;
269  if(fIsSimple){
270  return fNBins == rhs.fNBins && fMin == rhs.fMin && fMax == rhs.fMax;
271  }
272  else{
273  return fEdges == rhs.fEdges;
274  }
275  }
276 
277  //----------------------------------------------------------------------
278  bool Binning::operator<(const Binning& rhs) const
279  {
280  if(fIsSimple != rhs.fIsSimple) return fIsSimple < rhs.fIsSimple;
281  if(fIsSimple){
282  return std::make_tuple(fNBins, fMin, fMax) < std::make_tuple(rhs.fNBins, rhs.fMin, rhs.fMax);
283  }
284  else{
285  return fEdges < rhs.fEdges;
286  }
287  }
288 
289  //----------------------------------------------------------------------
290  std::map<Binning, int>& Binning::IDMap()
291  {
292  static std::map<Binning, int> ret;
293  return ret;
294  }
295 
296 }
* labels
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
bool operator<(const Binning &rhs) const
Definition: Binning.cxx:278
process_name opflash particleana ie x
Binning TrueLOverTrueEBins()
Definition: Binning.cxx:185
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static std::map< Binning, int > & IDMap()
Definition: Binning.cxx:290
std::vector< std::string > fLabels
Definition: Binning.h:56
static Binning CustomHelper(const std::vector< double > &edges)
Definition: Binning.cxx:68
process_name opflashCryoW ana
std::vector< double > fEdges
Definition: Binning.h:55
constexpr details::BinObj< T > bin(T value)
Returns a wrapper to print the specified data in binary format.
bool fIsSimple
Definition: Binning.h:59
bool IsSimple() const
Definition: Binning.h:31
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Binning.h:63
bool operator==(const Binning &rhs) const
Definition: Binning.cxx:264
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
void SaveTo(TDirectory *dir) const
Definition: Binning.cxx:197
int FindBin(float x) const
Definition: Binning.cxx:100
static Binning Custom(const std::vector< double > &edges)
Definition: Binning.cxx:83
tuple dir
Definition: dropbox.py:28
double fMax
Definition: Binning.h:58
Binning TrueEnergyBins()
Default true-energy bin edges.
Definition: Binning.cxx:152
static Binning LogUniform(int n, double lo, double hi)
Definition: Binning.cxx:56
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
process_name largeant stream1 can override from command line with o or output physics producers generator N
double fMin
Definition: Binning.h:58
float A
Definition: dedx.py:137
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:38
static std::unique_ptr< Binning > LoadFrom(TDirectory *dir)
Definition: Binning.cxx:229
int fNBins
Definition: Binning.h:57
static Binning SimpleHelper(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:19