CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Overlayer.h
Go to the documentation of this file.
1 #ifndef OVERLAYER_H
2 #define OVERLAYER_H
3 
4 #include "TFile.h"
5 #include "TH1.h"
6 #include "TCanvas.h"
7 #include "TLegend.h"
8 #include <iostream>
9 #include <sstream>
10 #include "round_string.h"
11 
12 class Overlayer {
13 public:
15  : file_(new TFile(filename.c_str(),"READ")),
16  dataLocation_(datadir),
17  simLocation_(simdir),
18  color_(color)
19 { clear(); }
20 
21  Overlayer& clear();
23  Overlayer& xlabel(std::string s) { xlabel_=s; return *this;}
24  Overlayer& ylabel(std::string s) { ylabel_=s; return *this;}
25  Overlayer& logy(bool state=true) { logy_=state; return *this;}
26  Overlayer& withLegend(bool state=true);
27  Overlayer& legendLeft(bool state=true) { left_=state; return *this;}
28  void print(std::string);
29 private:
30  std::string getStats(const TH1* const) const;
31 
32  TFile* const file_;
34  const Color_t color_;
36  TH1* data_;
37  TH1* sim_;
38  bool logy_,left_;
39  TLegend* legend_;
40 };
41 
43 clear() {
44  std::cout << "clear" << std::endl;
45  data_=sim_=0;
47  logy_=false;
48  legend_=0;
49  left_=false;
50  return *this;
51 }
52 
55  std::cout << "find " << name << "\t" << std::flush;
56  histName_=name;
57  data_ = (TH1*) file_->GetDirectory(dataLocation_.c_str())->FindObjectAny(name.c_str());
58  sim_ = (TH1*) file_->GetDirectory(simLocation_.c_str())->FindObjectAny(name.c_str());
59  return *this;
60 }
61 
63  std::cout << "legend: " << (state?"true\t":"false\t") << std::flush;
64  if(!state) {legend_=0; return *this;}
65 
66  std::string data_stats = getStats(data_);
67  std::string sim_stats = getStats(sim_);
68  unsigned maxlength = std::max( std::max( dataLocation_.size(), simLocation_.size()),
69  std::max( data_stats.size(), sim_stats.size()) );
70 
71  if(left_) legend_ = new TLegend(0,0.75,maxlength*0.015,1.0);
72  else legend_ = new TLegend(1.0-maxlength*0.015,0.75,1.0,1.0);
73 
74  legend_->SetFillColor(kWhite);
75  legend_->AddEntry(data_,("#splitline{"+dataLocation_+"}{"+ data_stats + "}").c_str() );
76  legend_->AddEntry(sim_,("#splitline{"+simLocation_+"}{"+ sim_stats + "}").c_str() );
77  return *this;
78 }
79 
81 getStats(const TH1* const hist) const {
82  stringstream ss;
83  ss << "N: " << hist->GetEntries() << ", "
84  << "#mu: " << round_string()(std::make_pair(hist->GetMean(),hist->GetMeanError())) << ", "
85  << "#sigma: " << round_string()(std::make_pair(hist->GetRMS(),hist->GetRMSError()));
86  return ss.str().c_str();
87 }
88 
89 void Overlayer::
91  std::cout << "print\t" << std::flush;
92 
93  sim_->GetXaxis()->SetLabelSize(0.05); sim_->GetXaxis()->SetTitleSize(0.05);
94  sim_->GetYaxis()->SetLabelSize(0.05); sim_->GetYaxis()->SetTitleSize(0.05);
95  sim_->SetTitle((title_+";"+xlabel_+";"+ylabel_).c_str());
96  sim_->Scale(data_->Integral()/sim_->Integral());
97  sim_->SetMinimum( (logy_?0.1:0) );
98  sim_->SetMaximum( (logy_?2:1.1) * std::max( sim_->GetBinContent(sim_->GetMaximumBin()) + sim_->GetBinError(sim_->GetMaximumBin()) ,
99  data_->GetBinContent(data_->GetMaximumBin()) + data_->GetBinError(data_->GetMaximumBin()) ) );
100 
101  sim_->SetFillColor(color_);
102  TH1* sim_errors = (TH1*) sim_->Clone((histName_+"_clone").c_str());
103  sim_->SetLineColor(kRed);
104  sim_errors->SetFillColor(kRed);
105  sim_errors->SetFillStyle(3244);
106  data_->SetMarkerStyle(20);
107 
108  TCanvas c("c","",800,600);
109  if(logy_) c.SetLogy();
110 
111  sim_->Draw("hist");
112  sim_errors->Draw("e2same");
113  data_->Draw("same");
114  if(legend_) legend_->Draw();
115 
116  c.Print((histName_+suffix).c_str());
117 }
118 
119 #endif
TH1 * sim_
Definition: Overlayer.h:37
const std::string simLocation_
Definition: Overlayer.h:33
const Color_t color_
Definition: Overlayer.h:34
bool logy_
Definition: Overlayer.h:38
Overlayer & logy(bool state=true)
Definition: Overlayer.h:25
Overlayer & find(std::string)
Definition: Overlayer.h:54
TH1 * data_
Definition: Overlayer.h:36
std::string ylabel_
Definition: Overlayer.h:35
TLegend * legend_
Definition: Overlayer.h:39
const TString datadir("/src/Fireworks/Core/")
const std::string dataLocation_
Definition: Overlayer.h:33
Overlayer & withLegend(bool state=true)
Definition: Overlayer.h:62
TFile *const file_
Definition: Overlayer.h:32
Overlayer & clear()
Definition: Overlayer.h:43
Overlayer & xlabel(std::string s)
Definition: Overlayer.h:23
void print(std::string)
Definition: Overlayer.h:90
Overlayer(std::string filename, std::string datadir, std::string simdir, Color_t color)
Definition: Overlayer.h:14
std::string title_
Definition: Overlayer.h:35
bool left_
Definition: Overlayer.h:38
std::string getStats(const TH1 *const) const
Definition: Overlayer.h:81
Overlayer & legendLeft(bool state=true)
Definition: Overlayer.h:27
Overlayer & ylabel(std::string s)
Definition: Overlayer.h:24
tuple filename
Definition: lut2db_cfg.py:20
std::string histName_
Definition: Overlayer.h:35
std::string xlabel_
Definition: Overlayer.h:35
tuple cout
Definition: gather_cfg.py:145