ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
hist1d.hpp
Go to the documentation of this file.
1 #ifndef H_HIST1D
2 #define H_HIST1D
3 
4 #include <vector>
5 #include <utility>
6 #include <memory>
7 #include <set>
8 #include <limits>
9 
10 #include "TH1D.h"
11 #include "TLegend.h"
12 #include "TCanvas.h"
13 #include "TPad.h"
14 #include "TLatex.h"
15 #include "TLine.h"
16 #include "TGraphAsymmErrors.h"
17 
18 #include "core/figure.hpp"
19 #include "core/process.hpp"
20 #include "core/axis.hpp"
21 #include "core/plot_opt.hpp"
22 
23 class Hist1D final: public Figure{
24 public:
26  public:
27  SingleHist1D(const Hist1D &stack,
28  const std::shared_ptr<Process> &process,
29  const TH1D &hist);
30  ~SingleHist1D() = default;
31 
32  TH1D raw_hist_;
33  mutable TH1D scaled_hist_;
34 
35  void RecordEvent(const Baby &baby) final;
36 
37  double GetMax(double max_bound = std::numeric_limits<double>::infinity(),
38  bool include_error_bar = false,
39  bool include_overflow = false) const;
40  double GetMin(double max_bound = 0.,
41  bool include_error_bar = false,
42  bool include_overflow = false) const;
43 
44  private:
45  SingleHist1D() = delete;
46  SingleHist1D(const SingleHist1D &) = delete;
47  SingleHist1D& operator=(const SingleHist1D &) = delete;
48  SingleHist1D(SingleHist1D &&) = delete;
49  SingleHist1D& operator=(SingleHist1D &&) = delete;
50 
53  };
54 
55  Hist1D(const Axis &xaxis, const NamedFunc &cut,
56  const std::vector<std::shared_ptr<Process> > &processes,
57  const std::vector<PlotOpt> &plot_options = {PlotOpt()});
58  Hist1D(Hist1D &&) = default;
59  Hist1D& operator=(Hist1D &&) = default;
60  ~Hist1D() = default;
61 
62  void Print(double luminosity,
63  const std::string &subdir) final;
64 
65  std::set<const Process*> GetProcesses() const final;
66 
67  FigureComponent * GetComponent(const Process *process) final;
68 
69  std::string Name() const;
70  std::string Title() const;
71 
72  Hist1D & Weight(const NamedFunc &weight);
73  Hist1D & Tag(const std::string &tag);
74  Hist1D & RatioTitle(const std::string &numerator,
75  const std::string &denominator);
76 
80  std::string tag_;
81  std::string ratio_numerator_;
82  std::string ratio_denominator_;
83  std::vector<PlotOpt> plot_options_;
84 
85 private:
86  std::vector<std::unique_ptr<SingleHist1D> > backgrounds_;
87  std::vector<std::unique_ptr<SingleHist1D> > signals_;
88  std::vector<std::unique_ptr<SingleHist1D> > datas_;
89 
90  mutable PlotOpt this_opt_;
91  mutable double luminosity_;
92  mutable double mc_scale_;
93  mutable double mc_scale_error_;
94  static TH1D blank_;//<!Blank histogram for creating dummy legend entries
95 
96  Hist1D(const Hist1D &) = delete;
97  Hist1D& operator=(const Hist1D &) = delete;
98  Hist1D() = delete;
99 
100  void RefreshScaledHistos();
101  void InitializeHistos() const;
102  void MergeOverflow() const;
103  void ScaleHistos() const;
104  void StackHistos() const;
105  void NormalizeHistos() const;
106 
107  void SetRanges() const;
108 
109  void ApplyStyles() const;
110  void StyleHisto(TH1D &h) const;
111  void AdjustFillStyles() const;
112 
113  void GetPads(std::unique_ptr<TCanvas> &c,
114  std::unique_ptr<TPad> &top,
115  std::unique_ptr<TPad> &bottom) const;
116 
117  void FixYAxis(std::vector<TH1D> &bottom_plots) const;
118 
119  std::vector<std::shared_ptr<TLatex> > GetTitleTexts() const;
120  TGraphAsymmErrors GetBackgroundError() const;
121  std::vector<TLine> GetCutLines(double y_min, double y_max, bool adjust_bottom) const;
122  std::vector<TH1D> GetBottomPlots(double &the_min, double &the_max) const;
123  TLine GetBottomHorizontal() const;
124 
125  void StripTopPlotLabels() const;
126 
127  double GetMaxDraw(double max_bound = std::numeric_limits<double>::infinity()) const;
128  double GetMinDraw(double min_bound = 0.) const;
129 
130  std::vector<std::shared_ptr<TLegend> > GetLegends();
131  void AddEntries(std::vector<std::shared_ptr<TLegend> > &legends,
132  const std::vector<std::unique_ptr<SingleHist1D> > &hists,
133  const std::string &style,
134  std::size_t n_entries,
135  std::size_t &entries_added) const;
136  double GetLegendRatio() const;
137 
138  double GetYield(std::vector<std::unique_ptr<SingleHist1D> >::const_iterator h) const;
139  double GetMean(std::vector<std::unique_ptr<SingleHist1D> >::const_iterator h) const;
140 
141  void GetTitleSize(double &width, double &height, bool in_pixels) const;
142 
143  const std::vector<std::unique_ptr<SingleHist1D> >& GetComponentList(const Process *process);
144 };
145 
146 #endif
double GetMean(std::vector< std::unique_ptr< SingleHist1D > >::const_iterator h) const
Get mean of histogram.
Definition: hist1d.cpp:1376
std::vector< ScalarType > VectorType
Definition: named_func.hpp:16
TLine GetBottomHorizontal() const
Get horizontal line drawn at "agreement" value for bottom plots.
Definition: hist1d.cpp:1105
FigureComponent * GetComponent(const Process *process) final
Definition: hist1d.cpp:468
void SetRanges() const
Set y-axis plotting range.
Definition: hist1d.cpp:663
NamedFunc::VectorType cut_vector_
Definition: hist1d.hpp:52
void ScaleHistos() const
Scales histograms to required luminosity.
Definition: hist1d.cpp:594
std::string Name() const
Definition: hist1d.cpp:479
NamedFunc cut_
Event selection.
Definition: hist1d.hpp:78
Axis xaxis_
Specification of content: plotted variable, binning, etc.
Definition: hist1d.hpp:77
Abstract base class for access to ntuple variables.
Definition: baby.hpp:16
std::string tag_
Filename tag to identify plot.
Definition: hist1d.hpp:80
double GetYield(std::vector< std::unique_ptr< SingleHist1D > >::const_iterator h) const
Get integrated number of weighted entries in histogram.
Definition: hist1d.cpp:1349
Hist1D & Weight(const NamedFunc &weight)
Definition: hist1d.cpp:507
const std::vector< std::unique_ptr< SingleHist1D > > & GetComponentList(const Process *process)
Definition: hist1d.cpp:1407
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
void RecordEvent(const Baby &baby) final
Definition: hist1d.cpp:152
std::vector< std::unique_ptr< SingleHist1D > > backgrounds_
Background components of the figure.
Definition: hist1d.hpp:86
NamedFunc proc_and_hist_cut_
Definition: hist1d.hpp:51
PlotOpt this_opt_
Plot style currently being drawn.
Definition: hist1d.hpp:90
void NormalizeHistos() const
Normalize histograms to data or 100%*(bin width) if needed for current style.
Definition: hist1d.cpp:628
void Print(double luminosity, const std::string &subdir) final
Produce and save formatted plots at given luminosity.
Definition: hist1d.cpp:337
std::string ratio_numerator_
Label for numerator in ratio plot.
Definition: hist1d.hpp:81
double GetMin(double max_bound=0., bool include_error_bar=false, bool include_overflow=false) const
Definition: hist1d.cpp:246
double mc_scale_
data/MC normalization
Definition: hist1d.hpp:92
double GetMaxDraw(double max_bound=std::numeric_limits< double >::infinity()) const
Get highest drawn point below max_bound across all component histograms.
Definition: hist1d.cpp:1150
std::vector< PlotOpt > plot_options_
Styles with which to draw plot.
Definition: hist1d.hpp:83
std::vector< std::unique_ptr< SingleHist1D > > datas_
Data components of the figure.
Definition: hist1d.hpp:88
Hist1D & RatioTitle(const std::string &numerator, const std::string &denominator)
Definition: hist1d.cpp:517
NamedFunc::VectorType val_vector_
Definition: hist1d.hpp:52
void RefreshScaledHistos()
Generates stacked and scaled histograms from unstacked and unscaled ones.
Definition: hist1d.cpp:531
Definition: axis.hpp:12
std::vector< std::unique_ptr< SingleHist1D > > signals_
Signal components of the figure.
Definition: hist1d.hpp:87
void StyleHisto(TH1D &h) const
Set label styles and title for a histogram.
Definition: hist1d.cpp:714
std::vector< TH1D > GetBottomPlots(double &the_min, double &the_max) const
Get ratio or other plots drawn on the lower pad.
Definition: hist1d.cpp:973
Container for a TH1D associated with a single Process.
Definition: hist1d.hpp:25
NamedFunc::VectorType wgt_vector_
Definition: hist1d.hpp:52
void ApplyStyles() const
Set label styles and title for all histograms.
Definition: hist1d.cpp:698
void AddEntries(std::vector< std::shared_ptr< TLegend > > &legends, const std::vector< std::unique_ptr< SingleHist1D > > &hists, const std::string &style, std::size_t n_entries, std::size_t &entries_added) const
Distribute processes from list of histograms across legends.
Definition: hist1d.cpp:1270
std::vector< TLine > GetCutLines(double y_min, double y_max, bool adjust_bottom) const
Get vertical lines at cut values.
Definition: hist1d.cpp:942
void GetTitleSize(double &width, double &height, bool in_pixels) const
Get width and height of title region.
Definition: hist1d.cpp:1398
std::set< const Process * > GetProcesses() const final
Definition: hist1d.cpp:454
double mc_scale_error_
data/MC normalization uncertainty
Definition: hist1d.hpp:93
void StackHistos() const
Stacks histograms if necessary for current plot style.
Definition: hist1d.cpp:610
void FixYAxis(std::vector< TH1D > &bottom_plots) const
Adjust y-axis title offset based on y-axis range.
Definition: hist1d.cpp:823
Hist1D()=delete
void AdjustFillStyles() const
Make histograms a hollow line for unstacked styles.
Definition: hist1d.cpp:761
std::vector< std::shared_ptr< TLegend > > GetLegends()
Get list of legends emulating single legend with multiple columns.
Definition: hist1d.cpp:1213
void MergeOverflow() const
Moves overflow (underflow) contents into last (first) visible bin.
Definition: hist1d.cpp:559
std::vector< std::shared_ptr< TLatex > > GetTitleTexts() const
Get text to print at top of plot.
Definition: hist1d.cpp:857
double GetMax(double max_bound=std::numeric_limits< double >::infinity(), bool include_error_bar=false, bool include_overflow=false) const
Definition: hist1d.cpp:215
TH1D scaled_hist_
Kludge. Mutable storage of scaled and stacked histogram.
Definition: hist1d.hpp:33
A full 1D plot with stacked/overlayed histograms.
Definition: hist1d.hpp:23
void GetPads(std::unique_ptr< TCanvas > &c, std::unique_ptr< TPad > &top, std::unique_ptr< TPad > &bottom) const
Generated canvas and pads for top and bottom plots.
Definition: hist1d.cpp:784
double luminosity_
Luminosity currently being drawn.
Definition: hist1d.hpp:91
std::string ratio_denominator_
Label for denominator in ratio plot.
Definition: hist1d.hpp:82
TH1D raw_hist_
Histogram storing distribution before stacking and luminosity weighting.
Definition: hist1d.hpp:32
std::string Title() const
Definition: hist1d.cpp:493
SingleHist1D & operator=(const SingleHist1D &)=delete
~Hist1D()=default
Hist1D & Tag(const std::string &tag)
Definition: hist1d.cpp:512
void StripTopPlotLabels() const
Definition: hist1d.cpp:1128
static TH1D blank_
Definition: hist1d.hpp:94
NamedFunc weight_
Event weight.
Definition: hist1d.hpp:79
void InitializeHistos() const
Sets all Hist1D::SingleHist1D::scaled_hist_ to corresponding Hist1D::SingleHist1D::raw_hist_.
Definition: hist1d.cpp:542
TGraphAsymmErrors GetBackgroundError() const
Get uncertainty on total background.
Definition: hist1d.cpp:917
double GetMinDraw(double min_bound=0.) const
Get lowest drawn point above min_bound across all component histograms.
Definition: hist1d.cpp:1181
double GetLegendRatio() const
Get factor by which to expand y-axis range to fit legend.
Definition: hist1d.cpp:1328