CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DQMOffline/JetMET/interface/SusyDQM/Overlayer.h

Go to the documentation of this file.
00001 #ifndef OVERLAYER_H
00002 #define OVERLAYER_H
00003 
00004 #include "TFile.h"
00005 #include "TH1.h"
00006 #include "TCanvas.h"
00007 #include "TLegend.h"
00008 #include <iostream>
00009 #include <sstream>
00010 #include "round_string.h"
00011 
00012 class Overlayer {
00013 public:
00014  Overlayer(std::string filename, std::string datadir, std::string simdir, Color_t color)
00015   : file_(new TFile(filename.c_str(),"READ")),
00016     dataLocation_(datadir),
00017     simLocation_(simdir),
00018     color_(color)
00019 { clear(); }
00020 
00021   Overlayer& clear();
00022   Overlayer& find(std::string);
00023   Overlayer& xlabel(std::string s) { xlabel_=s; return *this;}
00024   Overlayer& ylabel(std::string s) { ylabel_=s; return *this;}
00025   Overlayer& logy(bool state=true) { logy_=state; return *this;}
00026   Overlayer& withLegend(bool state=true);
00027   Overlayer& legendLeft(bool state=true) { left_=state; return *this;}
00028   void print(std::string);
00029 private:
00030   std::string getStats(const TH1* const) const;
00031 
00032   TFile* const file_;
00033   const std::string dataLocation_,simLocation_;
00034   const Color_t color_;
00035   std::string histName_,xlabel_,ylabel_,title_;
00036   TH1* data_;
00037   TH1* sim_;
00038   bool logy_,left_;
00039   TLegend* legend_;
00040 };
00041 
00042 Overlayer& Overlayer::
00043 clear() {
00044   std::cout << "clear" << std::endl;
00045   data_=sim_=0;
00046   xlabel_=ylabel_=histName_=title_="";
00047   logy_=false;
00048   legend_=0;
00049   left_=false;
00050   return *this;
00051 }
00052 
00053 Overlayer& Overlayer::
00054 find(std::string name) {
00055   std::cout << "find " << name << "\t" << std::flush;
00056   histName_=name; 
00057   data_ = (TH1*) file_->GetDirectory(dataLocation_.c_str())->FindObjectAny(name.c_str());
00058   sim_ = (TH1*) file_->GetDirectory(simLocation_.c_str())->FindObjectAny(name.c_str());
00059   return *this;
00060 }
00061 
00062 Overlayer& Overlayer::withLegend(bool state) {
00063   std::cout << "legend: " << (state?"true\t":"false\t") << std::flush;
00064   if(!state) {legend_=0; return *this;}
00065   
00066   std::string data_stats = getStats(data_);
00067   std::string sim_stats = getStats(sim_);
00068   unsigned maxlength = std::max( std::max( dataLocation_.size(), simLocation_.size()), 
00069                                  std::max( data_stats.size(), sim_stats.size()) );
00070 
00071   if(left_) legend_ = new TLegend(0,0.75,maxlength*0.015,1.0);
00072   else legend_ = new TLegend(1.0-maxlength*0.015,0.75,1.0,1.0); 
00073 
00074   legend_->SetFillColor(kWhite);
00075   legend_->AddEntry(data_,("#splitline{"+dataLocation_+"}{"+ data_stats + "}").c_str() );
00076   legend_->AddEntry(sim_,("#splitline{"+simLocation_+"}{"+ sim_stats + "}").c_str() );
00077   return *this;
00078 }
00079 
00080 std::string Overlayer::
00081 getStats(const TH1* const hist) const {
00082   stringstream ss;
00083   ss << "N: " << hist->GetEntries() << ", "
00084      << "#mu: " << round_string()(std::make_pair(hist->GetMean(),hist->GetMeanError())) << ", "
00085      << "#sigma: " << round_string()(std::make_pair(hist->GetRMS(),hist->GetRMSError()));
00086   return ss.str().c_str();
00087 }
00088 
00089 void Overlayer::
00090 print(std::string suffix) {
00091   std::cout << "print\t" << std::flush;
00092 
00093   sim_->GetXaxis()->SetLabelSize(0.05);  sim_->GetXaxis()->SetTitleSize(0.05);
00094   sim_->GetYaxis()->SetLabelSize(0.05);  sim_->GetYaxis()->SetTitleSize(0.05);
00095   sim_->SetTitle((title_+";"+xlabel_+";"+ylabel_).c_str());
00096   sim_->Scale(data_->Integral()/sim_->Integral());  
00097   sim_->SetMinimum( (logy_?0.1:0) );
00098   sim_->SetMaximum( (logy_?2:1.1) * std::max( sim_->GetBinContent(sim_->GetMaximumBin()) + sim_->GetBinError(sim_->GetMaximumBin()) ,
00099                                               data_->GetBinContent(data_->GetMaximumBin()) + data_->GetBinError(data_->GetMaximumBin()) ) );
00100 
00101   sim_->SetFillColor(color_);
00102   TH1* sim_errors = (TH1*) sim_->Clone((histName_+"_clone").c_str());
00103   sim_->SetLineColor(kRed);
00104   sim_errors->SetFillColor(kRed);
00105   sim_errors->SetFillStyle(3244);
00106   data_->SetMarkerStyle(20);
00107   
00108   TCanvas c("c","",800,600);
00109   if(logy_) c.SetLogy();
00110 
00111   sim_->Draw("hist");
00112   sim_errors->Draw("e2same");
00113   data_->Draw("same");
00114   if(legend_) legend_->Draw();
00115 
00116   c.Print((histName_+suffix).c_str());  
00117 }
00118 
00119 #endif