Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:59

0001 #include <algorithm>
0002 #include <iostream>
0003 #include <string>
0004 #include <vector>
0005 
0006 #include "TH1F.h"
0007 #include "TFile.h"
0008 #include "TStyle.h"
0009 #include "TCanvas.h"
0010 #include "TLegend.h"
0011 
0012 /// Fill the cutflow plot from 'yield' histograms
0013 TH1F* cutflow(TFile* sample, const std::vector<std::string>& validSteps);
0014 /// Check whether the given parameter is valid or not
0015 bool isValid(const std::vector<std::string>& validObjects, const std::string& value);
0016 
0017 /// -------------------------------------------------------------------------------
0018 ///
0019 /// Display the selection monitoring plots. The parameters are the histogram name 
0020 /// withing the root file and the selection step that make up the corresponding 
0021 /// directory within the root file. All valid parameter are given in the vectors 
0022 /// validHists_ (for the hostogram names) and validSteps_ (for the selection steps).
0023 /// The use case is:
0024 /// .x PhysicsTools/PatExamples/bin/monitorTopSelection.C+("muonPt", "Step1")
0025 ///
0026 /// -------------------------------------------------------------------------------
0027 void monitorTopSelection(const std::string& histName="yield", std::string selectionStep="Step1")
0028 {
0029   gStyle->SetOptStat(0);
0030 
0031   // list of valid histogram names
0032   std::vector<std::string> validHists_;
0033   validHists_.push_back("yield"   );
0034   validHists_.push_back("elecMult");
0035   validHists_.push_back("elecIso" );
0036   validHists_.push_back("elecPt"  );
0037   validHists_.push_back("muonMult");
0038   validHists_.push_back("muonIso" );
0039   validHists_.push_back("muonPt"  );
0040   validHists_.push_back("jetMult" );
0041   validHists_.push_back("jet1Pt"  );
0042   validHists_.push_back("jet2Pt"  );
0043   validHists_.push_back("jet3Pt"  );
0044   validHists_.push_back("jet4Pt"  );
0045   validHists_.push_back("met"     );
0046   
0047   // list of valid selection steps
0048   std::vector<std::string> validSteps_;
0049   validSteps_.push_back("Step1"   );
0050   validSteps_.push_back("Step2"   );
0051   validSteps_.push_back("Step3a"  );
0052   validSteps_.push_back("Step4"   );
0053   validSteps_.push_back("Step5"   );
0054   validSteps_.push_back("Step6a"  );
0055   validSteps_.push_back("Step6b"  );
0056   validSteps_.push_back("Step6c"  );
0057   validSteps_.push_back("Step7"   );
0058   
0059   // check validity of input
0060   if(!isValid(validHists_, histName) || !isValid(validSteps_, selectionStep)){ 
0061     return;
0062   }
0063   
0064   // list of input samples (ATTENTION obey order)
0065   std::vector<string> samples_;
0066   samples_.push_back("analyzePatTopSelection_ttbar.root");
0067   samples_.push_back("analyzePatTopSelection_wjets.root");
0068   samples_.push_back("analyzePatTopSelection_zjets.root");
0069   samples_.push_back("analyzePatTopSelection_qcd.root"  );
0070   samples_.push_back("analyzePatTopSelection.root");
0071 
0072   //open files
0073   std::vector<TFile*> files;
0074   for(unsigned int idx=0; idx<samples_.size(); ++idx){
0075     files.push_back(new TFile(samples_[idx].c_str()));
0076   }
0077   // load histograms
0078   std::vector<TH1F*> hists;
0079   if(histName=="yield"){
0080     for(unsigned int idx=0; idx<files.size(); ++idx){
0081       hists.push_back(cutflow(files[idx], validSteps_));
0082     }
0083   } 
0084   else{
0085     std::string histPath = selectionStep.append(std::string("/").append(histName));
0086     for(unsigned int idx=0; idx<files.size(); ++idx){
0087       hists.push_back((TH1F*)files[idx]->Get((std::string("mon").append(histPath)).c_str()));
0088     }
0089   }
0090   float lumi = 2.;
0091   // scale and stack histograms for simulated events
0092   // scales for 1pb-1: ttbar,  wjets,  zjets,  qcd 
0093   float scales[] =    {0.165,  0.312,  0.280, 0.287};
0094   for(unsigned int idx=0; idx<samples_.size()-1; ++idx){
0095     hists[idx]->Scale(lumi*scales[idx]);
0096   }
0097   for(unsigned int idx=1; idx<samples_.size()-1; ++idx){
0098     hists[idx]->Add(hists[idx-1]);
0099   }
0100   // setup the canvas and draw the histograms
0101   TCanvas* canv0 = new TCanvas("canv0", "canv0", 600, 600);
0102   canv0->cd(0);
0103   canv0->SetLogy(1);
0104   if(histName=="yield"){
0105     hists[3]->SetTitle("Selection Steps");
0106   }
0107   hists[3]->SetMinimum(1.);
0108   hists[3]->SetMaximum(20.*hists[4]->GetMaximum());
0109   hists[3]->SetFillColor(kYellow);
0110   hists[3]->Draw();
0111   hists[2]->SetFillColor(kAzure-2);
0112   hists[2]->Draw("same");
0113   hists[1]->SetFillColor(kGreen-3);
0114   hists[1]->Draw("same");
0115   hists[0]->SetFillColor(kRed+1);
0116   hists[0]->Draw("same");
0117   // plot data points
0118   hists[4]->SetLineWidth(3.);
0119   hists[4]->SetLineColor(kBlack);
0120   hists[4]->SetMarkerColor(kBlack);
0121   hists[4]->SetMarkerStyle(20.);
0122   hists[4]->Draw("esame");
0123   canv0->RedrawAxis();
0124   
0125   TLegend* leg = new TLegend(0.35,0.6,0.85,0.90);
0126   leg->SetFillStyle ( 0);
0127   leg->SetFillColor ( 0);
0128   leg->SetBorderSize( 0);
0129   leg->AddEntry( hists[4], "CMS Data 2010 (2 pb^{-1})"     , "PL");
0130   leg->AddEntry( hists[3], "QCD"                           , "F");
0131   leg->AddEntry( hists[2], "Z/#gamma#rightarrowl^{+}l^{-}" , "F");
0132   leg->AddEntry( hists[1], "W#rightarrowl#nu"              , "F");
0133   leg->AddEntry( hists[0], "t#bar{t} (incl)"               , "F");
0134   leg->Draw("same");
0135 }
0136 
0137 TH1F* cutflow(TFile* sample, const std::vector<std::string>& validSteps)
0138 {
0139   // book histogram
0140   TH1F* hist = new TH1F(sample->GetName(), sample->GetName(), validSteps.size(), 0., validSteps.size());
0141   // set labels
0142   for(unsigned int idx=0; idx<validSteps.size(); ++idx){
0143     hist->GetXaxis()->SetBinLabel( idx+1 , validSteps[idx].c_str());
0144   }
0145   hist->LabelsOption("h", "X"); //"h", "v", "u", "d"
0146   // fill histogram
0147   for(unsigned int idx=0; idx<validSteps.size(); ++idx){
0148     TH1F* buffer = (TH1F*)sample->Get((std::string("mon").append(validSteps[idx]).append("/yield")).c_str());
0149     hist->SetBinContent(idx+1, buffer->GetBinContent(1));
0150   }
0151   return hist;
0152 }
0153 
0154 bool isValid(const std::vector<std::string>& validObjects, const std::string& value)
0155 {
0156   // check whether value is in the list of valid hostogram names
0157   if(std::find(validObjects.begin(), validObjects.end(), value)==validObjects.end()){
0158     std::cout << " ERROR : " << value << " is not a valid value" << std::endl;
0159     std::cout << " List of valid values:" << std::endl;
0160     for(std::vector<std::string>::const_iterator obj=validObjects.begin(); obj!=validObjects.end(); ++obj){
0161       std::cout << " " << (*obj) << std::endl;
0162     }
0163     std::cout << std::endl;
0164     return false;
0165   }
0166   return true;
0167 }