CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FlavourHistorgrams.h
Go to the documentation of this file.
1 
2 #ifndef FlavourHistograms_H
3 #define FlavourHistograms_H
4 
8 
9 // #include "BTagPlotPrintC.h"
10 
11 #include "TH1F.h"
12 #include "TCanvas.h"
13 #include "TROOT.h"
14 #include "TSystem.h"
15 #include "TStyle.h"
16 
19 #include <iostream>
20 #include <string>
21 
22 //class DQMStore;
23 
24 //
25 // class to describe Histo
26 //
27 template <class T>
29 
30 public:
31 
32  FlavourHistograms (const std::string& baseNameTitle_ , const std::string& baseNameDescription_ ,
33  const int& nBins_ , const double& lowerBound_ , const double& upperBound_ ,
34  const bool& statistics_ , const bool& plotLog_ , const bool& plotNormalized_ ,
35  const std::string& plotFirst_ , const bool& update, const std::string& folder, const bool& mc) ;
36 
37  virtual ~FlavourHistograms () ;
38 
39 
40  // define arrays (if needed)
41 // void defineArray ( int * dimension , int max , int indexToPlot ) ;
42 
43  // fill entry
44  // For single variables and arrays (for arrays only a single index can be filled)
45  void fill ( const int & flavour, const T & variable) const;
46 
47  // For single variables and arrays
48  void fill ( const int & flavour, const T * variable) const;
49 
50 
51  void settitle(const char* title) ;
52 
53  void plot (TPad * theCanvas = 0) ;
54 
55  void epsPlot(const std::string& name);
56 
57  // needed for efficiency computations -> this / b
58  // (void : alternative would be not to overwrite the histos but to return a cloned HistoDescription)
59  void divide ( const FlavourHistograms<T> & bHD ) const ;
60 
61  inline void SetMaximum(const double& max) { theMax = max;}
62  inline void SetMinimum(const double& min) { theMin = min;}
63 
64 
65  // trivial access functions
66  inline std::string baseNameTitle () const { return theBaseNameTitle ; }
67  inline std::string baseNameDescription () const { return theBaseNameDescription ; }
68  inline int nBins () const { return theNBins ; }
69  inline double lowerBound () const { return theLowerBound ; }
70  inline double upperBound () const { return theUpperBound ; }
71  inline bool statistics () const { return theStatistics ; }
72  inline bool plotLog () const { return thePlotLog ; }
73  inline bool plotNormalized () const { return thePlotNormalized ; }
74  inline std::string plotFirst () const { return thePlotFirst ; }
75  inline int* arrayDimension () const { return theArrayDimension; }
76  inline int maxDimension () const {return theMaxDimension; }
77  inline int indexToPlot () const {return theIndexToPlot; }
78 
79  // access to the histos
80  inline TH1F * histo_all () const { return theHisto_all->getTH1F() ; }
81  inline TH1F * histo_d () const { return theHisto_d ->getTH1F() ; }
82  inline TH1F * histo_u () const { return theHisto_u->getTH1F() ; }
83  inline TH1F * histo_s () const { return theHisto_s->getTH1F() ; }
84  inline TH1F * histo_c () const { return theHisto_c->getTH1F() ; }
85  inline TH1F * histo_b () const { return theHisto_b->getTH1F() ; }
86  inline TH1F * histo_g () const { return theHisto_g->getTH1F() ; }
87  inline TH1F * histo_ni () const { return theHisto_ni->getTH1F() ; }
88  inline TH1F * histo_dus () const { return theHisto_dus->getTH1F() ; }
89  inline TH1F * histo_dusg () const { return theHisto_dusg->getTH1F() ; }
90 
91  std::vector<TH1F*> getHistoVector() const;
92 
93 
94 protected:
95 
96  void fillVariable ( const int & flavour , const T & var ) const;
97 
98  //
99  // the data members
100  //
101 
102 // T * theVariable ;
103 
104  // for arrays
107  int theIndexToPlot ; // in case that not the complete array has to be plotted
108 
109  std::string theBaseNameTitle ;
111  int theNBins ;
112  double theLowerBound ;
113  double theUpperBound ;
115  bool thePlotLog ;
117  std::string thePlotFirst ; // one character; gives flavour to plot first: l (light) , c , b
118  double theMin, theMax;
119 
120  // the histos
131 
132  // DQMStore * dqmStore_;
133 
134 
135  // the canvas to plot
136  TCanvas* theCanvas ;
137  private:
139 
140  bool mcPlots_;
141 
142 } ;
143 
144 template <class T>
145 FlavourHistograms<T>::FlavourHistograms (const std::string& baseNameTitle_ , const std::string& baseNameDescription_ ,
146  const int& nBins_ , const double& lowerBound_ , const double& upperBound_ ,
147  const bool& statistics_ , const bool& plotLog_ , const bool& plotNormalized_ ,
148  const std::string& plotFirst_, const bool& update, const std::string& folder, const bool& mc) :
149  // BaseFlavourHistograms () ,
150  // theVariable ( variable_ ) ,
151  theMaxDimension(-1), theIndexToPlot(-1), theBaseNameTitle ( baseNameTitle_ ) , theBaseNameDescription ( baseNameDescription_ ) ,
152  theNBins ( nBins_ ) , theLowerBound ( lowerBound_ ) , theUpperBound ( upperBound_ ) ,
153  theStatistics ( statistics_ ) , thePlotLog ( plotLog_ ) , thePlotNormalized ( plotNormalized_ ) ,
154  thePlotFirst ( plotFirst_ ), theMin(-1.), theMax(-1.), mcPlots_(mc)
155 {
156  // defaults for array dimensions
157  theArrayDimension = 0 ;
158 
159  // check plot order string
160  if ( thePlotFirst == "l" || thePlotFirst == "c" || thePlotFirst == "b" ) {
161  // OK
162  }
163  else {
164  // not correct: print warning and set default (l)
165  std::cout << "FlavourHistograms::FlavourHistograms : thePlotFirst was not correct : " << thePlotFirst << std::endl ;
166  std::cout << "FlavourHistograms::FlavourHistograms : Set it to default value (l)! " << std::endl ;
167  thePlotFirst = "l" ;
168  }
169 
170  if (!update) {
171  // book histos
172  HistoProviderDQM prov("Btag",folder);
174  if (mcPlots_) {
184  }else{
185  theHisto_d = 0;
186  theHisto_u = 0;
187  theHisto_s = 0;
188  theHisto_c = 0;
189  theHisto_b = 0;
190  theHisto_g = 0;
191  theHisto_ni = 0;
192  theHisto_dus = 0;
193  theHisto_dusg = 0;
194  }
195  // statistics if requested
196  if ( theStatistics ) {
197  theHisto_all ->getTH1F()->Sumw2() ;
198  if (mcPlots_ ) {
199 
200  theHisto_d ->getTH1F()->Sumw2() ;
201  theHisto_u ->getTH1F()->Sumw2() ;
202  theHisto_s ->getTH1F()->Sumw2() ;
203  theHisto_c ->getTH1F()->Sumw2() ;
204  theHisto_b ->getTH1F()->Sumw2() ;
205  theHisto_g ->getTH1F()->Sumw2() ;
206  theHisto_ni ->getTH1F()->Sumw2() ;
207  theHisto_dus ->getTH1F()->Sumw2() ;
208  theHisto_dusg->getTH1F()->Sumw2() ;
209  }
210  }
211  } else {
212  HistoProviderDQM prov("Btag",folder);
213  theHisto_all = prov.access(theBaseNameTitle + "ALL" ) ;
214  if (mcPlots_) {
215 
216  theHisto_d = prov.access(theBaseNameTitle + "D" ) ;
217  theHisto_u = prov.access(theBaseNameTitle + "U" ) ;
218  theHisto_s = prov.access(theBaseNameTitle + "S" ) ;
219  theHisto_c = prov.access(theBaseNameTitle + "C" ) ;
220  theHisto_b =prov.access(theBaseNameTitle + "B" ) ;
221  theHisto_g =prov.access(theBaseNameTitle + "G" ) ;
222  theHisto_ni =prov.access(theBaseNameTitle + "NI" ) ;
223  theHisto_dus =prov.access(theBaseNameTitle + "DUS" ) ;
224  theHisto_dusg =prov.access(theBaseNameTitle + "DUSG") ;
225  }
226  }
227 
228  // defaults for other data members
229  theCanvas = 0 ;
230 }
231 
232 
233 template <class T>
235  // delete the canvas*/
236  delete theCanvas ;
237 }
238 
239 
240 // define arrays (if needed)
241 // template <class T>
242 // void FlavourHistograms<T>::defineArray ( int * dimension , int max , int indexToPlot ) {
243 // // indexToPlot < 0 if all to be plotted
244 // theArrayDimension = dimension ;
245 // theMaxDimension = max ;
246 // theIndexToPlot = indexToPlot ;
247 // }
248 
249 // fill entry
250 template <class T> void
251 FlavourHistograms<T>::fill ( const int & flavour, const T & variable) const
252 {
253  // For single variables and arrays (for arrays only a single index can be filled)
254  fillVariable ( flavour , variable ) ;
255 }
256 
257 template <class T> void
258 FlavourHistograms<T>::fill ( const int & flavour, const T * variable) const
259 {
260  if ( theArrayDimension == 0 ) {
261  // single variable
262  fillVariable ( flavour , *variable ) ;
263  } else {
264  // array
265  int iMax = (*theArrayDimension > theMaxDimension) ? theMaxDimension : *theArrayDimension ;
266  //
267  for ( int i = 0 ; i != iMax ; ++i ) {
268  // check if only one index to be plotted (<0: switched off -> plot all)
269  if ( ( theIndexToPlot < 0 ) || ( i == theIndexToPlot ) ) {
270  fillVariable ( flavour , *(variable+i) ) ;
271  }
272  }
273 
274  // if single index to be filled but not enough entries: fill 0.0 (convention!)
275  if ( theIndexToPlot >= iMax ) {
276  // cout << "==>> The index to be filled is too big -> fill 0.0 : " << theBaseNameTitle << " : " << theIndexToPlot << " >= " << iMax << endl ;
277  const T& theZero = static_cast<T> ( 0.0 ) ;
278  fillVariable ( flavour , theZero ) ;
279  }
280  }
281 }
282 
283 
284 template <class T>
286  if(theHisto_all) theHisto_all ->setAxisTitle(title) ;
287  if (mcPlots_) {
288 
289  theHisto_d ->setAxisTitle(title) ;
290  theHisto_u ->setAxisTitle(title) ;
291  theHisto_s ->setAxisTitle(title) ;
292  theHisto_c ->setAxisTitle(title) ;
293  theHisto_b ->setAxisTitle(title) ;
294  theHisto_g ->setAxisTitle(title) ;
295  theHisto_ni ->setAxisTitle(title) ;
296  theHisto_dus ->setAxisTitle(title) ;
297  theHisto_dusg->setAxisTitle(title) ;
298  }
299 }
300 
301 
302 template <class T>
303 void FlavourHistograms<T>::plot (TPad * theCanvas /* = 0 */) {
304 
305 //fixme:
306  bool btppNI = false;
307  bool btppColour = true;
308 
309  if (theCanvas)
310  theCanvas->cd();
311 
312  RecoBTag::setTDRStyle()->cd();
313  gPad->UseCurrentStyle();
314 // if ( !btppTitle ) gStyle->SetOptTitle ( 0 ) ;
315 //
316 // // here: plot histograms in a canvas
317 // theCanvas = new TCanvas ( "C" + theBaseNameTitle , "C" + theBaseNameDescription ,
318 // btppXCanvas , btppYCanvas ) ;
319 // theCanvas->SetFillColor ( 0 ) ;
320 // theCanvas->cd ( 1 ) ;
321  gPad->SetLogy ( 0 ) ;
322  if ( thePlotLog ) gPad->SetLogy ( 1 ) ;
323  gPad->SetGridx ( 0 ) ;
324  gPad->SetGridy ( 0 ) ;
325  gPad->SetTitle ( 0 ) ;
326 
327  MonitorElement * histo[4];
328  int col[4], lineStyle[4], markerStyle[4];
329  int lineWidth = 1 ;
330 
331  const double markerSize = gPad->GetWh() * gPad->GetHNDC() / 500.;
332 
333  // default (l)
334  histo[0] = theHisto_dusg ;
335  //CW histo_1 = theHisto_dus ;
336  histo[1] = theHisto_b ;
337  histo[2] = theHisto_c ;
338  histo[3]= 0 ;
339 
340  double max = theMax;
341  if (theMax<=0.) {
342  max = theHisto_dusg->getTH1F()->GetMaximum();
343  if (theHisto_b->getTH1F()->GetMaximum() > max) max = theHisto_b->getTH1F()->GetMaximum();
344  if (theHisto_c->getTH1F()->GetMaximum() > max) max = theHisto_c->getTH1F()->GetMaximum();
345  }
346 
347  if (btppNI) {
348  histo[3] = theHisto_ni ;
349  if (theHisto_ni->getTH1F()->GetMaximum() > max) max = theHisto_ni->getTH1F()->GetMaximum();
350  }
351 
352  if ( btppColour ) { // print colours
353  col[0] = 4 ;
354  col[1] = 2 ;
355  col[2] = 6 ;
356  col[3] = 3 ;
357  lineStyle[0] = 1 ;
358  lineStyle[1] = 1 ;
359  lineStyle[2] = 1 ;
360  lineStyle[3] = 1 ;
361  markerStyle[0] = 20 ;
362  markerStyle[1] = 21 ;
363  markerStyle[2] = 22 ;
364  markerStyle[3] = 23 ;
365  lineWidth = 1 ;
366  }
367  else { // different marker/line styles
368  col[1] = 1 ;
369  col[2] = 1 ;
370  col[3] = 1 ;
371  col[0] = 1 ;
372  lineStyle[0] = 2 ;
373  lineStyle[1] = 1 ;
374  lineStyle[2] = 3 ;
375  lineStyle[3] = 4 ;
376  markerStyle[0] = 20 ;
377  markerStyle[1] = 21 ;
378  markerStyle[2] = 22 ;
379  markerStyle[3] = 23 ;
380  }
381 
382  // if changing order (NI stays always last)
383 
384  // c to plot first
385  if ( thePlotFirst == "c" ) {
386  histo[0] = theHisto_c ;
387  if ( btppColour ) col[0] = 6 ;
388  if ( !btppColour ) lineStyle[0] = 3 ;
389  histo[2] = theHisto_dusg ;
390  if ( btppColour ) col[2] = 4 ;
391  if ( !btppColour ) lineStyle[2] = 2 ;
392  }
393 
394  // b to plot first
395  if ( thePlotFirst == "b" ) {
396  histo[0] = theHisto_b ;
397  if ( btppColour ) col[0] = 2 ;
398  if ( !btppColour ) lineStyle[0] = 1 ;
399  histo[1] = theHisto_dusg ;
400  if ( btppColour ) col[1] = 4 ;
401  if ( !btppColour ) lineStyle[1] = 2 ;
402  }
403 
404 
405  histo[0] ->getTH1F()->GetXaxis()->SetTitle ( theBaseNameDescription.c_str() ) ;
406  histo[0] ->getTH1F()->GetYaxis()->SetTitle ( "Arbitrary Units" ) ;
407  histo[0] ->getTH1F()->GetYaxis()->SetTitleOffset(1.25) ;
408 
409  for (int i=0; i != 4; ++i) {
410  if (histo[i]== 0 ) continue;
411  histo[i] ->getTH1F()->SetStats ( false ) ;
412  histo[i] ->getTH1F()->SetLineStyle ( lineStyle[i] ) ;
413  histo[i] ->getTH1F()->SetLineWidth ( lineWidth ) ;
414  histo[i] ->getTH1F()->SetLineColor ( col[i] ) ;
415  histo[i] ->getTH1F()->SetMarkerStyle ( markerStyle[i] ) ;
416  histo[i] ->getTH1F()->SetMarkerColor ( col[i] ) ;
417  histo[i] ->getTH1F()->SetMarkerSize ( markerSize ) ;
418  }
419 
420  if ( thePlotNormalized ) {
421 // cout <<histo[0]->GetEntries() <<" "<< histo[1]->GetEntries() <<" "<< histo[2]->GetEntries()<<" "<<histo[3]->GetEntries()<<endl;
422  if (histo[0]->getTH1F()->GetEntries() != 0) {histo[0]->getTH1F() ->DrawNormalized() ;} else { histo[0]->getTH1F() ->SetMaximum(1.0);
423 histo[0] ->getTH1F()->Draw() ;}
424  if (histo[1]->getTH1F()->GetEntries() != 0) histo[1] ->getTH1F()->DrawNormalized("Same") ;
425  if (histo[2]->getTH1F()->GetEntries() != 0) histo[2]->getTH1F() ->DrawNormalized("Same") ;
426  if ((histo[3] != 0) && (histo[3]->getTH1F()->GetEntries() != 0)) histo[3] ->getTH1F()->DrawNormalized("Same") ;
427  }
428  else {
429  histo[0]->getTH1F()->SetMaximum(max*1.05);
430  if (theMin!=-1.) histo[0]->getTH1F()->SetMinimum(theMin);
431  histo[0]->getTH1F()->Draw() ;
432  histo[1]->getTH1F()->Draw("Same") ;
433  histo[2]->getTH1F()->Draw("Same") ;
434  if ( histo[3] != 0 ) histo[3]->getTH1F()->Draw("Same") ;
435  }
436 
437 }
438 
439 template <class T>
440 void FlavourHistograms<T>::epsPlot(const std::string& name)
441 {
442  TCanvas tc(theBaseNameTitle.c_str() , theBaseNameDescription.c_str());
443 
444  plot(&tc);
445  tc.Print((name + theBaseNameTitle + ".eps").c_str());
446 }
447 
448 
449 // needed for efficiency computations -> this / b
450 // (void : alternative would be not to overwrite the histos but to return a cloned HistoDescription)
451 template <class T>
453  // divide histos using binomial errors
454  //
455  // ATTENTION: It's the responsability of the user to make sure that the HistoDescriptions
456  // involved in this operation have been constructed with the statistics option switched on!!
457  //
458  theHisto_all ->getTH1F()-> Divide ( theHisto_all->getTH1F() , bHD.histo_all () , 1.0 , 1.0 , "b" ) ;
459  if (mcPlots_) {
460  theHisto_d ->getTH1F()-> Divide ( theHisto_d ->getTH1F() , bHD.histo_d () , 1.0 , 1.0 , "b" ) ;
461  theHisto_u ->getTH1F()-> Divide ( theHisto_u ->getTH1F() , bHD.histo_u () , 1.0 , 1.0 , "b" ) ;
462  theHisto_s ->getTH1F()-> Divide ( theHisto_s ->getTH1F() , bHD.histo_s () , 1.0 , 1.0 , "b" ) ;
463  theHisto_c ->getTH1F()-> Divide ( theHisto_c ->getTH1F() , bHD.histo_c () , 1.0 , 1.0 , "b" ) ;
464  theHisto_b ->getTH1F()-> Divide ( theHisto_b ->getTH1F() , bHD.histo_b () , 1.0 , 1.0 , "b" ) ;
465  theHisto_g ->getTH1F()-> Divide ( theHisto_g ->getTH1F() , bHD.histo_g () , 1.0 , 1.0 , "b" ) ;
466  theHisto_ni ->getTH1F()-> Divide ( theHisto_ni->getTH1F() , bHD.histo_ni () , 1.0 , 1.0 , "b" ) ;
467  theHisto_dus ->getTH1F()-> Divide ( theHisto_dus->getTH1F() , bHD.histo_dus () , 1.0 , 1.0 , "b" ) ;
468  theHisto_dusg ->getTH1F()-> Divide ( theHisto_dusg->getTH1F() , bHD.histo_dusg() , 1.0 , 1.0 , "b" ) ;
469  }
470 }
471 
472 
473 template <class T>
474 void FlavourHistograms<T>::fillVariable ( const int & flavour , const T & var ) const {
475  // all
476  theHisto_all ->Fill ( var ) ;
477  // flavour specific
478  if (!mcPlots_) return;
479 
480  switch(flavour) {
481  case 1:
482  theHisto_d->Fill( var );
483  theHisto_dus->Fill( var );
484  theHisto_dusg->Fill( var );
485  return;
486  case 2:
487  theHisto_u->Fill( var );
488  theHisto_dus->Fill( var );
489  theHisto_dusg->Fill( var );
490  return;
491  case 3:
492  theHisto_s->Fill( var );
493  theHisto_dus->Fill( var );
494  theHisto_dusg->Fill( var );
495  return;
496  case 4:
497  theHisto_c->Fill( var );
498  return;
499  case 5:
500  theHisto_b->Fill( var );
501  return;
502  case 21:
503  theHisto_g->Fill( var );
504  theHisto_dusg->Fill( var );
505  return;
506  default:
507  theHisto_ni->Fill( var );
508  return;
509  }
510 }
511 
512 template <class T>
513 std::vector<TH1F*> FlavourHistograms<T>::getHistoVector() const
514 {
515  std::vector<TH1F*> histoVector;
516  histoVector.push_back ( theHisto_all->getTH1F() );
517  if (mcPlots_) {
518  histoVector.push_back ( theHisto_d->getTH1F() );
519  histoVector.push_back ( theHisto_u->getTH1F() );
520  histoVector.push_back ( theHisto_s->getTH1F() );
521  histoVector.push_back ( theHisto_c->getTH1F() );
522  histoVector.push_back ( theHisto_b->getTH1F() );
523  histoVector.push_back ( theHisto_g ->getTH1F() );
524  histoVector.push_back ( theHisto_ni->getTH1F() );
525  histoVector.push_back ( theHisto_dus->getTH1F() );
526  histoVector.push_back ( theHisto_dusg->getTH1F());
527  }
528  return histoVector;
529 }
530 #endif
double lowerBound() const
int i
Definition: DBlmapReader.cc:9
MonitorElement * theHisto_g
void plot(TPad *theCanvas=0)
MonitorElement * theHisto_c
void SetMinimum(const double &min)
TH1F * histo_all() const
TStyle * setTDRStyle()
Definition: Tools.cc:287
MonitorElement * theHisto_u
int maxDimension() const
TH1F * histo_c() const
void fill(const int &flavour, const T &variable) const
MonitorElement * theHisto_all
std::string baseNameDescription() const
#define min(a, b)
Definition: mlp_lapack.h:161
TH1F * histo_dus() const
std::vector< TH1F * > getHistoVector() const
TH1F * histo_b() const
TH1F * histo_g() const
void SetMaximum(const double &max)
void epsPlot(const std::string &name)
virtual MonitorElement * book1D(const std::string &name, const std::string &title, const int &nchX, const double &lowX, const double &highX)
const T & max(const T &a, const T &b)
void fillVariable(const int &flavour, const T &var) const
std::string baseNameTitle() const
void settitle(const char *title)
TH1F * histo_d() const
TH1F * getTH1F(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
int * arrayDimension() const
MonitorElement * theHisto_d
MonitorElement * theHisto_dusg
std::string theBaseNameDescription
TH1F * getTH1F(void) const
MonitorElement * theHisto_b
TH1F * histo_s() const
int indexToPlot() const
TH1F * histo_ni() const
void divide(const FlavourHistograms< T > &bHD) const
std::string plotFirst() const
bool plotNormalized() const
bool plotLog() const
std::string theBaseNameTitle
#define update(a, b)
TH1F * histo_u() const
tuple cout
Definition: gather_cfg.py:121
virtual MonitorElement * access(const std::string &name)
double upperBound() const
MonitorElement * theHisto_s
bool statistics() const
long double T
int flavour(const Candidate &part)
Definition: pdgIdUtils.h:31
TH1F * histo_dusg() const
MonitorElement * theHisto_dus
MonitorElement * theHisto_ni