ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
axis.cpp
Go to the documentation of this file.
1 #include "core/axis.hpp"
2 
3 #include <algorithm>
4 
5 using namespace std;
6 
7 Axis::Axis(const vector<double> &bins,
8  const NamedFunc &var,
9  const string &title,
10  const set<double> &cut_vals):
11  var_(var),
12  title_(title),
13  units_(""),
14  cut_vals_(cut_vals),
15  bins_(bins){
16  sort(bins_.begin(), bins_.end());
17  ParseUnits();
18  }
19 
20 Axis::Axis(size_t nbins,
21  double xmin, double xmax,
22  const NamedFunc &var,
23  const string &title,
24  const set<double> &cut_vals):
25  var_(var),
26  title_(title),
27  units_(""),
28  cut_vals_(cut_vals),
29  bins_(GetEdges(nbins, xmin, xmax)){
30  ParseUnits();
31  }
32 
33 size_t Axis::Nbins() const{
34  return bins_.size()-1;
35 }
36 
37 
38 Axis & Axis::Bins(const vector<double> &bins){
39  bins_ = bins;
40  sort(bins_.begin(), bins_.end());
41  return *this;
42 }
43 
44 Axis & Axis::Bins(size_t nbins, double xmin, double xmax){
45  bins_ = GetEdges(nbins, xmin, xmax);
46  return *this;
47 }
48 
49 const vector<double> & Axis::Bins() const{
50  return bins_;
51 }
52 
53 double Axis::AvgBinWidth() const{
54  if(bins_.size() < 2) return 0.;
55  else return (bins_.back()-bins_.front())/(bins_.size()-1);
56 }
57 
58 string Axis::Title() const{
59  if(units_ == ""){
60  return title_;
61  }else{
62  return title_+" ["+units_+"]";
63  }
64 }
65 
66 vector<double> Axis::GetEdges(size_t nbins, double xmin, double xmax){
67  vector<double> edges(nbins+1);
68  if(nbins != 0){
69  double delta = (xmax-xmin)/nbins;
70  for(size_t i = 0; i < nbins+1; ++i){
71  edges.at(i) = i*delta + xmin;
72  }
73  }
74 
75  //Not necessary, but make sure that first and last edge are correct to available precision
76  edges.front() = xmin;
77  edges.back() = xmax;
78 
79  return edges;
80 }
81 
83  auto p1 = title_.rfind('[');
84  auto p2 = title_.rfind(']');
85  if(p1 >= p2 || p2 == string::npos) return;
86  units_ = title_.substr(p1+1, p2-p1-1);
87  title_ = title_.substr(0, p1);
88  while(title_.back() == ' '){
89  title_.pop_back();
90  }
91 }
Axis(const std::vector< double > &bins, const NamedFunc &var, const std::string &title="", const std::set< double > &cut_vals={})
Definition: axis.cpp:7
std::string Title() const
Definition: axis.cpp:58
std::string units_
Units of Axis::var_.
Definition: axis.hpp:40
double AvgBinWidth() const
Definition: axis.cpp:53
STL namespace.
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
static std::vector< double > GetEdges(std::size_t nbins, double xmin, double xmax)
Definition: axis.cpp:66
std::string title_
Axis title without units.
Definition: axis.hpp:39
Definition: axis.hpp:12
NamedFunc var_
Variable to be plotted.
Definition: axis.hpp:38
void ParseUnits()
Definition: axis.cpp:82
std::size_t Nbins() const
Definition: axis.cpp:33
std::vector< double > bins_
List of bin edges.
Definition: axis.hpp:44
std::set< double > cut_vals_
Values of HistoDef::var_ for which to plot a line.
Definition: axis.hpp:41
const std::vector< double > & Bins() const
Definition: axis.cpp:49