HighLAND
Public Member Functions | Protected Attributes | List of all members
HistoStack Class Reference

#include <HistoStack.hxx>

Public Member Functions

 HistoStack (const std::string &title, const std::string &titleX, const std::string &titleY)
 
virtual ~HistoStack ()
 
bool Is1D ()
 Tells whether this is a 1D histo.
 
bool Is2D ()
 Tells whether this is a 2D histo.
 
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".
 
void Add (TH2_h *h2, int fc, int lc, const std::string &leg="")
 Add a new 2D histogram to the stack, with fill colour "fc", line colour "lc".
 
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)
 
void Draw (const std::string &root_opt="", const std::string &opt="")
 Same as above but with a predefined style.
 
void SetTotal (TH1_h *h1)
 
void SetTotal (TH2_h *h2)
 
TH1_h * GetTotal1D ()
 Return the total 1D histo.
 
TH1_h * GetTotalStat1D ()
 Return the total 1D histo with only stat errors.
 
TH1_h * GetTotalSyst1D ()
 Return the total 1D histo with only systematic errors.
 
TH2_h * GetTotal2D ()
 Return the total 2D histo.
 
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.
 
void AddSystHistos (TH2_h *h1, TH2_h *h2, TH2_h *h1w)
 Add histos for updating systematics when using several files.
 
void GetSystHistos (const std::string &group, TH2_h *&h1, TH2_h *&h2, TH2_h *&h1w)
 Get Histos for updating systematics when using several files.
 
void AddSystHistosGroup (const std::string &group)
 Add a new group of systemtic histos.
 
void SetCurrentSystGroup (const std::string &group)
 Set the current Systematics group.
 
const std::string & GetCurrentSystGroup ()
 
std::vector< std::string > GetSystHistosGroups () const
 get the vector of groups of systemtic histos
 
void AddTotal (TH2_h *h2)
 Sets the total 2D histo if it does not exists or adds to the previous one when it exists.
 
void FillLegend (TLegend *leg)
 Fill the legend with info in the HistoStack.
 
TH1_h * GetHisto1D (const std::string &title)
 Returns one of the histos in the 1D stack. The title is used for comparison.
 
TH1_h * GetHisto1D (Int_t index)
 Returns the 1D histo with a given index.
 
TH2_h * GetHisto2D (const std::string &title)
 Returns one of the histos in the 2D stack. The title is used for comparison.
 
TH2_h * GetHisto2D (Int_t index)
 Returns the 2D histo with a given index.
 
void Print () const
 Dump the stack contents.
 
void NormalizeByArea (const std::string &uopt, double area=1)
 normalize all histos in the stack by area
 
void ResetDrawHistos ()
 delete all temporary histos used for drawing the stak (with variable binning normalization)
 
double GetMaximum (const std::string &opt="")
 Get the maximum for the HistoStack.
 
double GetMaximumWithError (const std::string &opt="")
 Get the maximum for the HistoStack taking into account the upper error.
 
void SetMaximum (double max)
 Set the maximum for the HistoStack.
 
void SetMinimum (double min)
 Set the minimum for the HistoStack.
 

Protected Attributes

std::vector< TH1_h * > _histos1D
 The TH1_hs that were added to the stack.
 
std::vector< TH1_h * > _histos1D_draw
 The TH1_hs that were added to the stack. Temporary histos used for drawing the stack (with variable binning normalization)
 
std::vector< TH2_h * > _histos2D
 The TH2_hs that were added to the stack.
 
THStack * _stack
 The root stack.
 
TH1_h * _hall1D
 
TH1_h * _hall1D_stat
 The "total" 1D histogram with only stat errors.
 
TH1_h * _hall1D_syst
 The "total" 1D histogram with only systematic errors.
 
TH1_h * _hall1D_draw
 temporary histo used for drawing the total (with variable binning normalization)
 
TH2_h * _hall2D
 
std::string _title
 The title of the plot.
 
std::string _titleX
 The title of the X axis.
 
std::string _titleY
 The title of the Y axis.
 
bool _is1D
 is it 1D ?
 
bool _is2D
 is it 2D ?
 
std::vector< std::string > _systHistosGroups
 Histos for updating systematics when using several files.
 
std::string _currentSystGroup
 
std::map< std::string, TH2_h * > _h1
 
std::map< std::string, TH2_h * > _h1w
 
std::map< std::string, TH2_h * > _h2
 

Detailed Description

This class is used by the DrawingTools and DrawingToolsBase classes to aid the creation of stacked histograms.

The stacked histograms are created based on track categories (for example, "particle") and track types within those categories (for example, "muon" and "electron"). The track categories are from the TrackCategoryDefinition class, and the types from the TrackTypeDefinition class.

The underlying implementation does not use a THStack, but overlaid histograms. This means we the same logic can be used to stack both TH1 and TH2 histograms.

Definition at line 25 of file HistoStack.hxx.

Constructor & Destructor Documentation

§ HistoStack()

HistoStack::HistoStack ( const std::string &  title,
const std::string &  titleX,
const std::string &  titleY 
)

Instantiate the object, setting the title of the plot, the X axis, and the Y axis.

Definition at line 6 of file HistoStack.cxx.

6  {
7 //*******************************************************
8 
9  _title = title;
10  _titleX = titleX;
11  _titleY = titleY;
12 
13  _hall1D = NULL;
14  _hall2D = NULL;
15 
16  _hall1D_stat = NULL;
17  _hall1D_syst = NULL;
18 
19  _is1D = false;
20  _is2D = false;
21 
22  _stack = NULL;
23  _hall1D_draw = NULL;
24 
25  // _h1 = _h2 = _h1w = NULL;
26 
27  _systHistosGroups.clear();
28 
29  _currentSystGroup ="default";
30 
31 }
bool _is2D
is it 2D ?
Definition: HistoStack.hxx:182
bool _is1D
is it 1D ?
Definition: HistoStack.hxx:179
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
std::string _title
The title of the plot.
Definition: HistoStack.hxx:170
std::vector< std::string > _systHistosGroups
Histos for updating systematics when using several files.
Definition: HistoStack.hxx:185
TH1_h * _hall1D_syst
The "total" 1D histogram with only systematic errors.
Definition: HistoStack.hxx:160
TH1_h * _hall1D_draw
temporary histo used for drawing the total (with variable binning normalization)
Definition: HistoStack.hxx:163
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
TH2_h * _hall2D
Definition: HistoStack.hxx:167

§ ~HistoStack()

HistoStack::~HistoStack ( )
virtual

Destructor, which cleans up the cumulative histograms used internally by the class. The user should delete the histograms that were added to the stack.

Definition at line 34 of file HistoStack.cxx.

34  {
35 //*******************************************************
36 
37  if (_hall1D) delete _hall1D;
38  if (_hall2D) delete _hall2D;
39 
40  if (_hall1D_syst) delete _hall1D_syst;
41  if (_hall1D_stat) delete _hall1D_stat;
42 
43  std::map<std::string, TH1_h*>::iterator itm1;
44  std::map<std::string, TH2_h*>::iterator itm2;
45  for (itm2 = _h1.begin(); itm2 != _h1.end(); itm2++)
46  if (itm2->second) delete itm2->second;
47  _h1.clear();
48 
49  for (itm2 = _h2.begin(); itm2 != _h2.end(); itm2++)
50  if (itm2->second) delete itm2->second;
51  _h2.clear();
52 
53  for (itm2 = _h1w.begin(); itm2 != _h1w.end(); itm2++)
54  if (itm2->second) delete itm2->second;
55  _h1w.clear();
56 
57  _systHistosGroups.clear();
58 
59  std::vector<TH1_h*>::iterator it1;
60  for (it1 = _histos1D.begin(); it1 != _histos1D.end(); it1++) delete *it1;
61  _histos1D.clear();
62 
63  std::vector<TH2_h*>::iterator it2;
64  for (it2 = _histos2D.begin(); it2 != _histos2D.end(); it2++) delete *it2;
65  _histos2D.clear();
66 
67  // delete also the histograms created for drawing
69 }
void ResetDrawHistos()
delete all temporary histos used for drawing the stak (with variable binning normalization) ...
Definition: HistoStack.cxx:73
TH1_h * _hall1D_stat
The "total" 1D histogram with only stat errors.
Definition: HistoStack.hxx:157
TH1_h * _hall1D
Definition: HistoStack.hxx:154
std::vector< std::string > _systHistosGroups
Histos for updating systematics when using several files.
Definition: HistoStack.hxx:185
TH1_h * _hall1D_syst
The "total" 1D histogram with only systematic errors.
Definition: HistoStack.hxx:160
std::vector< TH2_h * > _histos2D
The TH2_hs that were added to the stack.
Definition: HistoStack.hxx:147
std::vector< TH1_h * > _histos1D
The TH1_hs that were added to the stack.
Definition: HistoStack.hxx:141
TH2_h * _hall2D
Definition: HistoStack.hxx:167

Member Function Documentation

§ Draw()

void HistoStack::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 
)

Draw the stack of histograms that have been added. If "root_opt" contains "same", then the current active pad is re-used. If "opt" contains "ETOT", then error bars are drawn on the total stack.

Definition at line 148 of file HistoStack.cxx.

148  {
149 //*******************************************************
150 
151  (void)leg_opt;
152  (void)mode;
153 
154  // Delete all histos used for drawing the stack, just in case this stack was drawn before
155  ResetDrawHistos();
156 
157  if (_is1D) {
158  bool makeTotal = (!_hall1D);
159 
160  std::vector<TH1_h*>::reverse_iterator rit1;
161  int i = 0;
162  for (rit1 = _histos1D.rbegin(); rit1 != _histos1D.rend(); rit1++, i++) {
163  TH1_h* ht = *rit1;
164 
165  if (makeTotal) {
166  if (i == 0) {
167  _hall1D = new TH1_h(*ht);
168  _hall1D->SetName((std::string(ht->GetName()) + "_cum").c_str());
169  _hall1D->SetTitle(ht->GetTitle());
170  } else {
171  _hall1D->Add(_hall1D, ht);
172  }
173  }
174  if (i == 0) {
175  if (_stack) delete _stack;
176  _stack = new THStack((std::string(ht->GetName()) + "_stack").c_str(), _title.c_str());
177  }
178  }
179 
180  // Copy the histogram before normalizing
181  _hall1D_draw = new TH1_h(*_hall1D);
182  Double_t min_width;
183  drawUtils::NormalizeVariableBinning(_hall1D_draw,2,opt, min_width);
184 
185  for (rit1 = _histos1D.rbegin(); rit1 != _histos1D.rend(); rit1++) {
186  TH1_h* ht = *rit1;
187  // Clone the histograms before normalizing
188  TH1_h* htp = new TH1_h(*ht);
189  _histos1D_draw.push_back(htp);
190  drawUtils::NormalizeVariableBinning(htp,2,opt+" USEMINWIDTH",min_width);
191  // Add the normalized histo to the stack
192  _stack->Add(htp);
193  }
194 
195 
196  // Set the maximum and minimum Y
197  // double maxy = _hall1D_draw->GetBinContent(_hall1D_draw->GetMaximumBin()) * 1.1;
198  // if (_stack && opt.find("ETOT")) {
199  // maxy += _hall1D_draw->GetBinError(_hall1D_draw->GetMaximumBin()) * 1.1;
200  // }
201 
202  // if (fabs(usermaxY) > 1e-6 && usermaxY > userminY) {
203  // maxy = usermaxY;
204  // }
205  // double miny = userminY;
206 
207  // If the stack is pressent (colore drawing) just draw it
208  if (_stack){
209  if (root_opt.find("same") == std::string::npos) {
210  _stack->Draw(("HIST"+root_opt).c_str());
211  _stack->GetXaxis()->SetTitle(_titleX.c_str());
212  _stack->GetYaxis()->SetTitle(_titleY.c_str());
213  _stack->Draw(("HIST"+root_opt).c_str());
214  } else {
215  _stack->Draw("HIST same");
216  }
217  }
218 
219  // This is just to draw the stats box before doing the variable bin normalization
220  _hall1D->GetXaxis()->SetTitle(_titleX.c_str());
221  _hall1D->GetYaxis()->SetTitle(_titleY.c_str());
222  _hall1D->SetTitle(_title.c_str());
223 
224 
225  if (_stack)
226  _hall1D->Draw((root_opt+"sames AXIS").c_str());
227  else
228  _hall1D->Draw((root_opt+"AXIS").c_str());
229 
230 
231  // Style for the total histo in the case it has to be drawn
232  _hall1D_draw->SetLineColor(lc);
233  _hall1D_draw->SetMarkerColor(lc);
234  _hall1D_draw->SetLineWidth(lw);
235  _hall1D_draw->SetFillColor(fc);
236  _hall1D_draw->SetFillStyle(fs);
237 
238  // Draw error bars for the total stack when requested
239  if (_stack && drawUtils::CheckOption(opt, "ETOT")) {
240  _hall1D_draw->Draw("same e2");
241  }
242  // Draw the total when there is no stack
243  if (!_stack){
244  _hall1D_draw->SetMarkerStyle(20);
245  _hall1D_draw->SetMarkerSize(1);
246  _hall1D_draw->Draw((root_opt+"same").c_str());
247 
248  if (_hall1D_stat){
249  _hall1D_stat->SetLineColor(4);
250  _hall1D_stat->SetFillColor(4);
251  _hall1D_stat->Draw((root_opt+"same").c_str());
252  }
253  }
254 
255  } else if (_is2D) {
256  bool makeTotal = (!_hall2D);
257 
258  std::vector<TH2_h*>::reverse_iterator rit1;
259  int i = 0;
260  for (rit1 = _histos2D.rbegin(); rit1 != _histos2D.rend(); rit1++, i++) {
261  TH2_h* ht = *rit1;
262 
263  if (makeTotal) {
264  if (i == 0) {
265  _hall2D = new TH2_h(*ht);
266  _hall2D->SetName((std::string(ht->GetName()) + "_cum").c_str());
267  _hall2D->SetTitle(ht->GetTitle());
268  } else {
269  _hall2D->Add(_hall2D, ht);
270  }
271  }
272  if (i == 0) {
273  if (_stack) delete _stack;
274  _stack = new THStack((std::string(ht->GetName()) + "_stack").c_str(), _title.c_str());
275  }
276  _stack->Add(ht);
277 
278  }
279 
280 
281  // Style for the total histo in the case it has to be drawn
282  _hall2D->SetLineColor(lc);
283  _hall2D->SetMarkerColor(lc);
284  _hall2D->SetLineWidth(lw);
285  _hall2D->SetFillColor(fc);
286  _hall2D->SetFillStyle(fs);
287 
288  if (_stack){
289  if (root_opt.find("same") == std::string::npos) {
290  _stack->Draw(("HIST "+root_opt).c_str());
291  _stack->GetXaxis()->SetTitle(_titleX.c_str());
292  _stack->GetYaxis()->SetTitle(_titleY.c_str());
293  _stack->Draw(("HIST "+root_opt).c_str());
294  } else {
295  _stack->Draw("HIST same");
296  }
297  }
298  else{
299  _hall2D->Draw(root_opt.c_str());
300  }
301  }
302 
303 }
bool _is2D
is it 2D ?
Definition: HistoStack.hxx:182
bool _is1D
is it 1D ?
Definition: HistoStack.hxx:179
void ResetDrawHistos()
delete all temporary histos used for drawing the stak (with variable binning normalization) ...
Definition: HistoStack.cxx:73
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
void NormalizeVariableBinning(TH1 *h, int mode, const std::string &opt, Double_t &minwidth)
Normalize bin contents by bin width. return the with of the bin with minimum width.
TH1_h * _hall1D
Definition: HistoStack.hxx:154
std::string _title
The title of the plot.
Definition: HistoStack.hxx:170
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
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
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
TH2_h * _hall2D
Definition: HistoStack.hxx:167
bool CheckOption(const std::string &uopt, const std::string &this_opt)
Check if specific option exists, and if so if it appears in option field.

§ SetTotal() [1/2]

void HistoStack::SetTotal ( TH1_h *  h1)
inline

The total histogram is normally the sum of all the histograms that were added to the stack. This can be overriden (before calling Draw()).

Definition at line 60 of file HistoStack.hxx.

60 { _hall1D = h1; }
TH1_h * _hall1D
Definition: HistoStack.hxx:154

§ SetTotal() [2/2]

void HistoStack::SetTotal ( TH2_h *  h2)
inline

The total histogram is normally the sum of all the histograms that were added to the stack. This can be overriden (before calling Draw()).

Definition at line 64 of file HistoStack.hxx.

64 { _hall2D = h2; }
TH2_h * _hall2D
Definition: HistoStack.hxx:167

Member Data Documentation

§ _hall1D

TH1_h* HistoStack::_hall1D
protected

The "total" 1D histogram. Nominally the sum of all the stacked histograms, but can be set by the user.

Definition at line 154 of file HistoStack.hxx.

§ _hall2D

TH2_h* HistoStack::_hall2D
protected

The "total" 2D histogram. Nominally the sum of all the stacked histograms, but can be set by the user.

Definition at line 167 of file HistoStack.hxx.


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