All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
ana::Binning Class Reference

Represent the binning of a Spectrum's x-axis. More...

#include <Binning.h>

Public Member Functions

int NBins () const
 
double Min () const
 
double Max () const
 
int FindBin (float x) const
 
bool IsSimple () const
 
const std::vector< double > & Edges () const
 
const std::vector< std::string > & Labels () const
 
void SaveTo (TDirectory *dir) const
 
int ID () const
 
bool operator== (const Binning &rhs) const
 
bool operator< (const Binning &rhs) const
 

Static Public Member Functions

static Binning Simple (int n, double lo, double hi, const std::vector< std::string > &labels={})
 
static Binning LogUniform (int n, double lo, double hi)
 
static Binning Custom (const std::vector< double > &edges)
 
static Binning FromTAxis (const TAxis *ax)
 
static std::unique_ptr< BinningLoadFrom (TDirectory *dir)
 
static int MaxID ()
 

Protected Member Functions

 Binning ()
 

Static Protected Member Functions

static Binning SimpleHelper (int n, double lo, double hi, const std::vector< std::string > &labels={})
 
static Binning CustomHelper (const std::vector< double > &edges)
 
static std::map< Binning, int > & IDMap ()
 

Protected Attributes

std::vector< double > fEdges
 
std::vector< std::string > fLabels
 
int fNBins
 
double fMin
 
double fMax
 
bool fIsSimple
 
int fID
 

Static Protected Attributes

static int fgNextID = 0
 The next ID that hasn't yet been assigned. More...
 

Detailed Description

Represent the binning of a Spectrum's x-axis.

May be "Simple" (equally spaced) or "Custom" (arbitrary binning)

Definition at line 18 of file Binning.h.

Constructor & Destructor Documentation

ana::Binning::Binning ( )
protected

Definition at line 13 of file Binning.cxx.

14  : fID(-1)
15  {
16  }

Member Function Documentation

Binning ana::Binning::Custom ( const std::vector< double > &  edges)
static

Definition at line 83 of file Binning.cxx.

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  }
static std::map< Binning, int > & IDMap()
Definition: Binning.cxx:290
static Binning CustomHelper(const std::vector< double > &edges)
Definition: Binning.cxx:68
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Binning.h:63
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
Binning ana::Binning::CustomHelper ( const std::vector< double > &  edges)
staticprotected

Definition at line 68 of file Binning.cxx.

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  }
const std::vector<double>& ana::Binning::Edges ( ) const
inline

Definition at line 32 of file Binning.h.

33  {
34  return fEdges;
35  }
std::vector< double > fEdges
Definition: Binning.h:55
int ana::Binning::FindBin ( float  x) const

Definition at line 100 of file Binning.cxx.

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  }
process_name opflash particleana ie x
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 IsSimple() const
Definition: Binning.h:31
double fMax
Definition: Binning.h:58
double fMin
Definition: Binning.h:58
int fNBins
Definition: Binning.h:57
Binning ana::Binning::FromTAxis ( const TAxis *  ax)
static

Definition at line 122 of file Binning.cxx.

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  }
static std::map< Binning, int > & IDMap()
Definition: Binning.cxx:290
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Binning.h:63
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
static Binning Custom(const std::vector< double > &edges)
Definition: Binning.cxx:83
static Binning SimpleHelper(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:19
int ana::Binning::ID ( ) const
inline

Definition at line 42 of file Binning.h.

42 {return fID;}
std::map< Binning, int > & ana::Binning::IDMap ( )
staticprotected

Definition at line 290 of file Binning.cxx.

291  {
292  static std::map<Binning, int> ret;
293  return ret;
294  }
bool ana::Binning::IsSimple ( ) const
inline

Definition at line 31 of file Binning.h.

31 {return fIsSimple;}
bool fIsSimple
Definition: Binning.h:59
const std::vector<std::string>& ana::Binning::Labels ( ) const
inline

Definition at line 37 of file Binning.h.

37 {return fLabels;}
std::vector< std::string > fLabels
Definition: Binning.h:56
std::unique_ptr< Binning > ana::Binning::LoadFrom ( TDirectory *  dir)
static

Definition at line 229 of file Binning.cxx.

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  }
static Binning Custom(const std::vector< double > &edges)
Definition: Binning.cxx:83
tuple dir
Definition: dropbox.py:28
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:38
Binning ana::Binning::LogUniform ( int  n,
double  lo,
double  hi 
)
static

Definition at line 56 of file Binning.cxx.

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  }
static Binning Custom(const std::vector< double > &edges)
Definition: Binning.cxx:83
double ana::Binning::Max ( ) const
inline

Definition at line 29 of file Binning.h.

29 {return fMax;}
double fMax
Definition: Binning.h:58
static int ana::Binning::MaxID ( )
inlinestatic

Definition at line 43 of file Binning.h.

43 {return fgNextID-1;}
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Binning.h:63
double ana::Binning::Min ( ) const
inline

Definition at line 28 of file Binning.h.

28 {return fMin;}
double fMin
Definition: Binning.h:58
int ana::Binning::NBins ( ) const
inline

Definition at line 27 of file Binning.h.

27 {return fNBins;}
int fNBins
Definition: Binning.h:57
bool ana::Binning::operator< ( const Binning rhs) const

Definition at line 278 of file Binning.cxx.

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  }
std::vector< double > fEdges
Definition: Binning.h:55
bool fIsSimple
Definition: Binning.h:59
double fMax
Definition: Binning.h:58
double fMin
Definition: Binning.h:58
int fNBins
Definition: Binning.h:57
bool ana::Binning::operator== ( const Binning rhs) const

Definition at line 264 of file Binning.cxx.

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  }
std::vector< double > fEdges
Definition: Binning.h:55
bool fIsSimple
Definition: Binning.h:59
double fMax
Definition: Binning.h:58
double fMin
Definition: Binning.h:58
int fNBins
Definition: Binning.h:57
void ana::Binning::SaveTo ( TDirectory *  dir) const

Definition at line 197 of file Binning.cxx.

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  }
std::vector< std::string > fLabels
Definition: Binning.h:56
std::vector< double > fEdges
Definition: Binning.h:55
bool fIsSimple
Definition: Binning.h:59
tuple dir
Definition: dropbox.py:28
double fMax
Definition: Binning.h:58
double fMin
Definition: Binning.h:58
int fNBins
Definition: Binning.h:57
Binning ana::Binning::Simple ( int  n,
double  lo,
double  hi,
const std::vector< std::string > &  labels = {} 
)
static

Definition at line 38 of file Binning.cxx.

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  }
static std::map< Binning, int > & IDMap()
Definition: Binning.cxx:290
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Binning.h:63
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
static Binning SimpleHelper(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:19
Binning ana::Binning::SimpleHelper ( int  n,
double  lo,
double  hi,
const std::vector< std::string > &  labels = {} 
)
staticprotected

Definition at line 19 of file Binning.cxx.

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  }
* labels

Member Data Documentation

std::vector<double> ana::Binning::fEdges
protected

Definition at line 55 of file Binning.h.

int ana::Binning::fgNextID = 0
staticprotected

The next ID that hasn't yet been assigned.

Definition at line 63 of file Binning.h.

int ana::Binning::fID
protected

Definition at line 61 of file Binning.h.

bool ana::Binning::fIsSimple
protected

Definition at line 59 of file Binning.h.

std::vector<std::string> ana::Binning::fLabels
protected

Definition at line 56 of file Binning.h.

double ana::Binning::fMax
protected

Definition at line 58 of file Binning.h.

double ana::Binning::fMin
protected

Definition at line 58 of file Binning.h.

int ana::Binning::fNBins
protected

Definition at line 57 of file Binning.h.


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