HighLAND
HistoStack.hxx
1 #ifndef HistoStack_h
2 #define HistoStack_h
3 
4 #include <map>
5 #include <vector>
6 #include <stdio.h>
7 #include <iostream>
8 #include <TH1F.h>
9 #include <TH2F.h>
10 #include <THStack.h>
11 #include <TLegend.h>
12 #include "DrawingUtils.hxx"
13 
14 /// This class is used by the DrawingTools and DrawingToolsBase classes to aid
15 /// the creation of stacked histograms.
16 ///
17 /// The stacked histograms are created based on track categories (for example,
18 /// "particle") and track types within those categories (for example, "muon"
19 /// and "electron"). The track categories are from the TrackCategoryDefinition
20 /// class, and the types from the TrackTypeDefinition class.
21 ///
22 /// The underlying implementation does not use a THStack, but overlaid
23 /// histograms. This means we the same logic can be used to stack both TH1
24 /// and TH2 histograms.
25 class HistoStack {
26 
27  public:
28 
29  /// Instantiate the object, setting the title of the plot, the X axis, and
30  /// the Y axis.
31  HistoStack(const std::string& title, const std::string& titleX, const std::string& titleY);
32 
33  /// Destructor, which cleans up the cumulative histograms used internally
34  /// by the class. The user should delete the histograms that were added to
35  /// the stack.
36  virtual ~HistoStack();
37 
38  /// Tells whether this is a 1D histo
39  bool Is1D(){return _is1D;}
40 
41  /// Tells whether this is a 2D histo
42  bool Is2D(){return _is2D;}
43 
44  /// Add a new 1D histogram to the stack, with fill colour "fc", line colour "lc".
45  void Add(TH1_h* h1, int lc, int lw, int fc, int fs,const std::string& leg);
46 
47  /// Add a new 2D histogram to the stack, with fill colour "fc", line colour "lc".
48  void Add(TH2_h* h2, int fc, int lc, const std::string& leg="");
49 
50  /// Draw the stack of histograms that have been added.
51  /// If "root_opt" contains "same", then the current active pad is re-used.
52  /// If "opt" contains "ETOT", then error bars are drawn on the total stack.
53  void Draw(int lc, int lw, int fc, int fs, const std::string& root_opt="", const std::string& opt="",const std::string& leg_opt="",int mode=0);
54 
55  /// Same as above but with a predefined style
56  void Draw(const std::string& root_opt = "", const std::string& opt = "");
57 
58  /// The total histogram is normally the sum of all the histograms that were
59  /// added to the stack. This can be overriden (before calling Draw()).
60  void SetTotal(TH1_h* h1) { _hall1D = h1; }
61 
62  /// The total histogram is normally the sum of all the histograms that were
63  /// added to the stack. This can be overriden (before calling Draw()).
64  void SetTotal(TH2_h* h2) { _hall2D = h2; }
65 
66  /// Return the total 1D histo
67  TH1_h* GetTotal1D() { return _hall1D; }
68 
69  /// Return the total 1D histo with only stat errors
70  TH1_h* GetTotalStat1D() { return _hall1D_stat; }
71 
72  /// Return the total 1D histo with only systematic errors
73  TH1_h* GetTotalSyst1D() { return _hall1D_syst; }
74 
75  /// Return the total 2D histo
76  TH2_h* GetTotal2D() { return _hall2D; }
77 
78  /// Sets the total 1D histo if it does not exists or adds to the previous one when it exists
79  void AddTotal(TH1_h* h1, TH1_h* hsyst=NULL);
80 
81  /// Add histos for updating systematics when using several files
82  void AddSystHistos(TH2_h* h1, TH2_h* h2, TH2_h* h1w);
83 
84  /// Get Histos for updating systematics when using several files
85  void GetSystHistos(const std::string& group, TH2_h*& h1, TH2_h*& h2, TH2_h*& h1w);
86 
87  /// Add a new group of systemtic histos
88  void AddSystHistosGroup(const std::string& group){_systHistosGroups.push_back(group);}
89 
90  /// Set the current Systematics group
91  void SetCurrentSystGroup(const std::string& group){_currentSystGroup = group;}
92 
93 
94  const std::string& GetCurrentSystGroup(){return _currentSystGroup;}
95 
96  /// get the vector of groups of systemtic histos
97  std::vector<std::string> GetSystHistosGroups() const {return _systHistosGroups;}
98 
99  /// Sets the total 2D histo if it does not exists or adds to the previous one when it exists
100  void AddTotal(TH2_h* h2);
101 
102  /// Fill the legend with info in the HistoStack
103  void FillLegend(TLegend* leg);
104 
105  /// Returns one of the histos in the 1D stack. The title is used for comparison
106  TH1_h* GetHisto1D(const std::string& title);
107 
108  /// Returns the 1D histo with a given index
109  TH1_h* GetHisto1D(Int_t index);
110 
111  /// Returns one of the histos in the 2D stack. The title is used for comparison
112  TH2_h* GetHisto2D(const std::string& title);
113 
114  /// Returns the 2D histo with a given index
115  TH2_h* GetHisto2D(Int_t index);
116 
117  /// Dump the stack contents
118  void Print() const;
119 
120  /// normalize all histos in the stack by area
121  void NormalizeByArea(const std::string& uopt, double area=1);
122 
123  /// delete all temporary histos used for drawing the stak (with variable binning normalization)
124  void ResetDrawHistos();
125 
126  /// Get the maximum for the HistoStack
127  double GetMaximum(const std::string& opt="");
128 
129  /// Get the maximum for the HistoStack taking into account the upper error
130  double GetMaximumWithError(const std::string& opt="");
131 
132  /// Set the maximum for the HistoStack
133  void SetMaximum(double max);
134 
135  /// Set the minimum for the HistoStack
136  void SetMinimum(double min);
137 
138  protected:
139 
140  /// The TH1_hs that were added to the stack.
141  std::vector<TH1_h*> _histos1D;
142 
143  /// The TH1_hs that were added to the stack. Temporary histos used for drawing the stack (with variable binning normalization)
144  std::vector<TH1_h*> _histos1D_draw;
145 
146  /// The TH2_hs that were added to the stack
147  std::vector<TH2_h*> _histos2D;
148 
149  /// The root stack
150  THStack* _stack;
151 
152  /// The "total" 1D histogram. Nominally the sum of all the stacked
153  /// histograms, but can be set by the user.
154  TH1_h* _hall1D;
155 
156  /// The "total" 1D histogram with only stat errors
157  TH1_h* _hall1D_stat;
158 
159  /// The "total" 1D histogram with only systematic errors
160  TH1_h* _hall1D_syst;
161 
162  /// temporary histo used for drawing the total (with variable binning normalization)
163  TH1_h* _hall1D_draw;
164 
165  /// The "total" 2D histogram. Nominally the sum of all the stacked
166  /// histograms, but can be set by the user.
167  TH2_h* _hall2D;
168 
169  /// The title of the plot.
170  std::string _title;
171 
172  /// The title of the X axis.
173  std::string _titleX;
174 
175  /// The title of the Y axis.
176  std::string _titleY;
177 
178  /// is it 1D ?
179  bool _is1D;
180 
181  /// is it 2D ?
182  bool _is2D;
183 
184  /// Histos for updating systematics when using several files
185  std::vector<std::string> _systHistosGroups;
186 
187  std::string _currentSystGroup;
188 
189  std::map<std::string,TH2_h*> _h1;
190  std::map<std::string,TH2_h*> _h1w;
191  std::map<std::string,TH2_h*> _h2;
192 
193 };
194 
195 #endif
196 
197 
void Draw(int lc, int lw, int fc, int fs, const std::string &root_opt="", const std::string &opt="", const std::string &leg_opt="", int mode=0)
Definition: HistoStack.cxx:148
bool _is2D
is it 2D ?
Definition: HistoStack.hxx:182
bool _is1D
is it 1D ?
Definition: HistoStack.hxx:179
TH2_h * GetHisto2D(const std::string &title)
Returns one of the histos in the 2D stack. The title is used for comparison.
Definition: HistoStack.cxx:114
void ResetDrawHistos()
delete all temporary histos used for drawing the stak (with variable binning normalization) ...
Definition: HistoStack.cxx:73
void AddTotal(TH1_h *h1, TH1_h *hsyst=NULL)
Sets the total 1D histo if it does not exists or adds to the previous one when it exists...
Definition: HistoStack.cxx:328
TH1_h * GetHisto1D(const std::string &title)
Returns one of the histos in the 1D stack. The title is used for comparison.
Definition: HistoStack.cxx:87
THStack * _stack
The root stack.
Definition: HistoStack.hxx:150
TH1_h * _hall1D_stat
The "total" 1D histogram with only stat errors.
Definition: HistoStack.hxx:157
TH1_h * _hall1D
Definition: HistoStack.hxx:154
void FillLegend(TLegend *leg)
Fill the legend with info in the HistoStack.
Definition: HistoStack.cxx:306
void AddSystHistosGroup(const std::string &group)
Add a new group of systemtic histos.
Definition: HistoStack.hxx:88
TH1_h * GetTotalStat1D()
Return the total 1D histo with only stat errors.
Definition: HistoStack.hxx:70
std::string _title
The title of the plot.
Definition: HistoStack.hxx:170
double GetMaximum(const std::string &opt="")
Get the maximum for the HistoStack.
Definition: HistoStack.cxx:538
TH1_h * GetTotalSyst1D()
Return the total 1D histo with only systematic errors.
Definition: HistoStack.hxx:73
void SetMinimum(double min)
Set the minimum for the HistoStack.
Definition: HistoStack.cxx:581
virtual ~HistoStack()
Definition: HistoStack.cxx:34
void SetCurrentSystGroup(const std::string &group)
Set the current Systematics group.
Definition: HistoStack.hxx:91
void NormalizeByArea(const std::string &uopt, double area=1)
normalize all histos in the stack by area
Definition: HistoStack.cxx:499
std::vector< std::string > _systHistosGroups
Histos for updating systematics when using several files.
Definition: HistoStack.hxx:185
HistoStack(const std::string &title, const std::string &titleX, const std::string &titleY)
Definition: HistoStack.cxx:6
std::vector< TH1_h * > _histos1D_draw
The TH1_hs that were added to the stack. Temporary histos used for drawing the stack (with variable b...
Definition: HistoStack.hxx:144
void SetMaximum(double max)
Set the maximum for the HistoStack.
Definition: HistoStack.cxx:571
TH1_h * _hall1D_syst
The "total" 1D histogram with only systematic errors.
Definition: HistoStack.hxx:160
void Add(TH1_h *h1, int lc, int lw, int fc, int fs, const std::string &leg)
Add a new 1D histogram to the stack, with fill colour "fc", line colour "lc".
Definition: HistoStack.cxx:431
bool Is1D()
Tells whether this is a 1D histo.
Definition: HistoStack.hxx:39
void Print() const
Dump the stack contents.
Definition: HistoStack.cxx:590
std::vector< std::string > GetSystHistosGroups() const
get the vector of groups of systemtic histos
Definition: HistoStack.hxx:97
TH1_h * _hall1D_draw
temporary histo used for drawing the total (with variable binning normalization)
Definition: HistoStack.hxx:163
std::vector< TH2_h * > _histos2D
The TH2_hs that were added to the stack.
Definition: HistoStack.hxx:147
void AddSystHistos(TH2_h *h1, TH2_h *h2, TH2_h *h1w)
Add histos for updating systematics when using several files.
Definition: HistoStack.cxx:391
double GetMaximumWithError(const std::string &opt="")
Get the maximum for the HistoStack taking into account the upper error.
Definition: HistoStack.cxx:552
void GetSystHistos(const std::string &group, TH2_h *&h1, TH2_h *&h2, TH2_h *&h1w)
Get Histos for updating systematics when using several files.
Definition: HistoStack.cxx:421
void SetTotal(TH1_h *h1)
Definition: HistoStack.hxx:60
TH2_h * GetTotal2D()
Return the total 2D histo.
Definition: HistoStack.hxx:76
std::string _titleY
The title of the Y axis.
Definition: HistoStack.hxx:176
std::string _titleX
The title of the X axis.
Definition: HistoStack.hxx:173
std::vector< TH1_h * > _histos1D
The TH1_hs that were added to the stack.
Definition: HistoStack.hxx:141
TH1_h * GetTotal1D()
Return the total 1D histo.
Definition: HistoStack.hxx:67
void SetTotal(TH2_h *h2)
Definition: HistoStack.hxx:64
TH2_h * _hall2D
Definition: HistoStack.hxx:167
bool Is2D()
Tells whether this is a 2D histo.
Definition: HistoStack.hxx:42