ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/macros/plot_fake_closure.C
Revision: 1.1
Committed: Wed Dec 7 19:48:16 2011 UTC (13 years, 5 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 dkralph 1.1 #if !defined(__CINT__) || defined(__MAKECINT__)
2     #include <TROOT.h> // access to gROOT, entry point to ROOT system
3     #include <TFile.h> // file handle class
4     #include <TTree.h> // class to access ntuples
5     #include <TChain.h>
6     #include <TCanvas.h> // class for drawing
7     #include <TH1F.h> // 1D histograms
8     #include <TH2F.h> // 2D histograms
9     #include <TBenchmark.h> // class to track macro running statistics
10     #include <TVector3.h> // 3D vector class
11     #include <TRegexp.h> // 3D vector class
12     #include <vector> // STL vector class
13     #include <iostream> // standard I/O
14     #include <iomanip> // functions to format standard I/O
15     #include <fstream> // functions for file I/O
16     #include <string> // C++ string class
17     #include <sstream> // class for parsing strings
18     #include "RooHistPdf.h"
19     #include "RooGlobalFunc.h"
20     #include "RooPlot.h"
21     #include "RooRealVar.h"
22     #include "RooDataHist.h"
23     #include "RooGenericPdf.h"
24     #include "RooDataSet.h"
25     #include "RooAddPdf.h"
26     #include "RooFormulaVar.h"
27     #include "RooCurve.h"
28     #include "RooBinning.h"
29     #include "Common/CPlot.hh" // helper class for plots
30     #include "Common/MitStyleRemix.hh" // style settings for drawing
31     #include "Common/MyTools.hh" // miscellaneous helper functions
32     #include "Common/CSample.hh" // helper class for organizing input ntuple files
33    
34     #endif
35     using namespace RooFit;
36    
37     TString scheme = "bdt-medium";
38     TString lumi = "2100";
39     TCanvas *c = new TCanvas("c","c");
40     vector<TString> fnamev,labelv;
41    
42     void fitMet(TH1F *hmcZ, TH1F *hmcW, TH1F *hSSmet);
43     TH1F* get_FR_pred(TTree *tree, TString var, TString extra, int nbins, double xmin, double xmax ) {
44     TH1F* hFR = new TH1F("hFR","",nbins,xmin,xmax);
45     hFR->Sumw2();
46     tree->Draw(var+">>hFR","w*(w!=1)"+extra);
47     return hFR;
48     }
49    
50     TH1F* get_SS(TTree *tree, TString var, TString extra, int nbins, double xmin, double xmax) {
51     TH1F* hSS = new TH1F("hSS","",nbins,xmin,xmax);
52     hSS->Sumw2();
53     tree->Draw(var+">>hSS","(w==1)"+extra);
54     return hSS;
55     }
56    
57     TH1F* get_MC(TTree *tree, TString var, TString extra, int nbins, double xmin, double xmax) {
58     TH1F *hMC = new TH1F("hMC","",nbins,xmin,xmax);
59     hMC->Sumw2();
60     tree->Draw(var+">>hMC",lumi+"*npuw*xswgt*(w==1)"+extra);
61     return hMC;
62     }
63    
64     void plot_fake_closure()
65     {
66     CPlot::sOutDir = "~/public_html/4lplots";
67    
68     map<TString,TTree*> treemap;
69     map<TString,TString> labelmap;
70    
71     TChain *wdata_chain = new TChain("wdata_chain","wdata_chain");
72     // wdata_chain->AddFile("root/ntuples/W-bdt-medium-iso07-straightrate.root",-1,"passtuple");
73     // wdata_chain->AddFile("root/ntuples/W-bdt-medium-iso07-2011B.root",-1,"passtuple");
74     wdata_chain->AddFile("root/ntuples/W-bdt-medium-wip3dcut.root",-1,"passtuple");
75     treemap["wdata"] = wdata_chain;
76    
77     TChain *zmc_chain = new TChain("zmc_chain","zmc_chain");
78     zmc_chain->AddFile("root/ntuples/W-bdt-medium-zjets-iso07-zjets.root",-1,"passtuple");
79     treemap["zmc"] = zmc_chain;
80    
81     TChain *wmc_chain = new TChain("wmc_chain","wmc_chain");
82     wmc_chain->AddFile("root/ntuples/W-bdt-medium-wjets-iso07-zjets.root",-1,"passtuple");
83     treemap["wmc"] = wmc_chain;
84    
85     double xmin,xmax;
86     int nbins;
87     TString var,cut,extra,ylabel;
88    
89     //----------------------------------------------------------------------------------------
90     // EMu W selection
91     cut = "*(pttight>25 && pfisotight/pttight<.025)";
92     ylabel = "events";
93    
94     var = "mZ1";
95     xmin = 0; xmax = 300; nbins = 20;
96     extra = cut + "*(met>30)";
97     CPlot plotmZ1W(var+"W","","#bf{m_{ll} [GeV]}",ylabel);
98     TH1F *hSSmZ1 = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
99     plotmZ1W.AddHist1D(hSSmZ1,"Obs.","E");
100     TH1F *hFRmZ1 = get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax);
101     plotmZ1W.AddToStack(hFRmZ1,"FR predic",kCyan-6);
102     plotmZ1W.Draw(c,kTRUE,"png");
103    
104     extra = cut + "*(met>30 && pt2<20)";
105     CPlot plotmZ1W_lopt(var+"W_lopt","","#bf{m_{ll} [GeV]}",ylabel);
106     TH1F *hSSmZ1_lopt = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
107     plotmZ1W_lopt.AddHist1D(hSSmZ1_lopt,"Obs.","E");
108     TH1F *hFRmZ1_lopt = get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax);
109     plotmZ1W_lopt.AddToStack(hFRmZ1_lopt,"FR predic",kCyan-6);
110     plotmZ1W_lopt.Draw(c,kTRUE,"png");
111    
112     extra = cut + "*(met>30 && pt2>20)";
113     CPlot plotmZ1W_hipt(var+"W_hipt","","#bf{m_{ll} [GeV]}",ylabel);
114     TH1F *hSSmZ1_hipt = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
115     plotmZ1W_hipt.AddHist1D(hSSmZ1_hipt,"Obs.","E");
116     TH1F *hFRmZ1_hipt = get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax);
117     plotmZ1W_hipt.AddToStack(hFRmZ1_hipt,"FR predic",kCyan-6);
118     plotmZ1W_hipt.Draw(c,kTRUE,"png");
119    
120     var = "pt2";
121     xmin = 0; xmax = 80; nbins = 30;
122     extra = cut;
123     CPlot plotpt2W(var+"W","","#bf{loose p_{T} [GeV]}",ylabel);
124     TH1F *pt2_SS = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
125     plotpt2W.AddHist1D(pt2_SS,"Obs.","E");
126     TH1F *pt2_FR = get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax);
127     plotpt2W.AddToStack(pt2_FR,"FR predic",kCyan-6);
128     plotpt2W.Draw(c,kTRUE,"png");
129     for(int i=0; i<pt2_SS->GetNbinsX()+2; i++) {
130     cout << pt2_SS->GetBinContent(i) << "\t" << pt2_FR->GetBinContent(i) << "\t" << (pt2_SS->GetBinContent(i) - pt2_FR->GetBinContent(i))/(0.5*(pt2_SS->GetBinContent(i) + pt2_FR->GetBinContent(i))) << endl;
131     }
132    
133     var = "abs(eta2)";
134     xmin = 0; xmax = 2.6; nbins = 30;
135     extra = cut;
136     CPlot ploteta2W("eta2W","","#bf{loose p_{T} [GeV]}",ylabel);
137     TH1F *htmp = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
138     ploteta2W.AddHist1D(htmp,"Obs.","E");
139     ploteta2W.AddToStack(get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax),"FR predic",kCyan-6);
140     ploteta2W.SetYRange(0,1.4*htmp->GetMaximum());
141     ploteta2W.Draw(c,kTRUE,"png");
142    
143     var = "mt";
144     xmin = 0; xmax = 120; nbins = 30;
145     extra = cut + "*(met>30)";
146     CPlot plotMtW(var+"W","","#bf{m_{T} [GeV]}",ylabel);
147     plotMtW.AddHist1D(get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax),"Obs.","E");
148     plotMtW.AddToStack(get_FR_pred(treemap["wdata"],var,extra,nbins,xmin,xmax),"FR predic",kCyan-6);
149     plotMtW.Draw(c,kTRUE,"png");
150    
151     //----------------------------------------------------------------------------------------
152     // met fit
153     var = "met";
154     xmin = 0; xmax = 100; nbins = 25;
155     extra = cut;
156     TH1F *hSSmet = get_SS(treemap["wdata"],var,extra,nbins,xmin,xmax);
157     TH1F *hmcZ = get_MC(treemap["zmc"],var,extra,nbins,xmin,xmax); hmcZ->Scale(1./6.5);
158     TH1F *hmcW = get_MC(treemap["wmc"],var,extra,nbins,xmin,xmax); hmcW->Scale(1./6.5);
159    
160     fitMet(hmcZ,hmcW,hSSmet);
161    
162     double integral=0,err=0;
163     vector<double> integrals,errs;
164     // inclusive
165     integral = hSSmZ1->IntegralAndError(1,hSSmZ1->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
166     integral = hFRmZ1->IntegralAndError(1,hFRmZ1->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
167     // for(int i=0; i<hSSmZ1->GetNbinsX()+2; i++) {
168     // cout << hSSmZ1->GetBinContent(i) << "\t" << hFRmZ1->GetBinContent(i) << "\t" << (hSSmZ1->GetBinContent(i) - hFRmZ1->GetBinContent(i))/(0.5*(hSSmZ1->GetBinContent(i) + hFRmZ1->GetBinContent(i))) << endl;
169     // }
170     // low pt
171     integral = hSSmZ1_lopt->IntegralAndError(1,hSSmZ1_lopt->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
172     integral = hFRmZ1_lopt->IntegralAndError(1,hFRmZ1_lopt->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
173     // high pt
174     integral = hSSmZ1_hipt->IntegralAndError(1,hSSmZ1_hipt->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
175     integral = hFRmZ1_hipt->IntegralAndError(1,hFRmZ1_hipt->GetNbinsX(),err); integrals.push_back(integral); errs.push_back(err);
176    
177     cout << "Inclusive: " << endl;
178     cout << "\tSS: " << fixed << setprecision(1) << integrals[0] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[0] << "\t (events: " << hSSmZ1->GetEntries() << ")" << endl;
179     cout << "\tFR: " << fixed << setprecision(1) << integrals[1] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[1] << "\t (events: " << hFRmZ1->GetEntries() << ")\t";
180     cout << setprecision(3) << 2*(integrals[1]-integrals[0])/(integrals[0]+integrals[1]) << endl;
181     cout << "Pt < 20: " << endl;
182     cout << "\tSS: " << fixed << setprecision(1) << integrals[2] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[2] << "\t (events: " << hSSmZ1_lopt->GetEntries() << ")" << endl;
183     cout << "\tFR: " << fixed << setprecision(1) << integrals[3] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[3] << "\t (events: " << hFRmZ1_lopt->GetEntries() << ")\t";
184     cout << setprecision(3) << 2*(integrals[3]-integrals[2])/(integrals[3]+integrals[2]) << endl;
185     cout << "Pt > 20: " << endl;
186     cout << "\tSS: " << fixed << setprecision(1) << integrals[4] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[4] << "\t (events: " << hSSmZ1_hipt->GetEntries() << ")" << endl;
187     cout << "\tFR: " << fixed << setprecision(1) << integrals[5] << "\t+/- " << fixed << setprecision(1) << setw(5) << errs[5] << "\t (events: " << hFRmZ1_hipt->GetEntries() << ")\t";
188     cout << setprecision(3) << 2*(integrals[5]-integrals[4])/(integrals[5]+integrals[4]) << endl;
189     }
190     //----------------------------------------------------------------------------------------
191     void fitMet(TH1F *hmcZ, TH1F *hmcW, TH1F *hSSmet)
192     {
193     RooRealVar met("met","MET [GeV]",0,100);
194     RooBinning metbins(25,0,100);
195     met.setBinning(metbins);
196     // Z mc pdf
197     RooDataHist hZ("hZ","hZ",RooArgList(met),hmcZ);
198     RooHistPdf pZ("pZ","pZ",RooArgSet(met),hZ);
199     // W mc pdf
200     hmcW->Smooth(2);
201     RooDataHist hW("hW","hW",RooArgList(met),hmcW);
202     RooHistPdf pW("pW","pW",RooArgSet(met),hW,3);
203     // rayleigh pdf
204     RooRealVar sig("sig","sig",3,1,15);
205     RooRealVar a1("a1","a1",0.1,0,1);
206     RooGenericPdf rayl("rayl","rayl","met*exp(-(met)*(met)/(sig*sig +2*sig*a1*(met) + a1*a1*(met)*(met)))",RooArgList(met,sig,a1));
207    
208     RooRealVar nWandZ("nWandZ","nWandZ",10000,0,11000);//81+611,1000);
209     RooRealVar fZ("fZ","fZ",84./628);
210     RooRealVar nRayl("nRayl","nRayl",300,0,10000);
211     RooAddPdf mMC("mMC","mMC",RooArgList(pZ,pW),RooArgList(fZ));
212     RooAddPdf model("model","model",RooArgList(mMC,rayl),RooArgList(nWandZ,nRayl));
213    
214     RooDataHist obsMet("obsMet","obsMet",RooArgList(met),hSSmet);
215     model.fitTo(obsMet);
216    
217     CPlot plotMetFit("metfitW","","MET [GeV]","Events");
218     TH1F *rayl_hist = (TH1F*)rayl.createHistogram("met",met,Binning(metbins));
219     rayl_hist->Scale(nRayl.getVal()/rayl_hist->Integral());
220     plotMetFit.AddToStack(rayl_hist,"jet (Rayleigh)",kBlue);
221     TH1F *z_hist = (TH1F*)pZ.createHistogram("met",met,Binning(metbins));
222     z_hist->Scale(fZ.getVal()*nWandZ.getVal()/z_hist->Integral());
223     plotMetFit.AddToStack(z_hist,"Z+jets",kGray);
224     TH1F *w_hist = (TH1F*)pW.createHistogram("met",met,Binning(metbins));
225     w_hist->Scale( (nWandZ.getVal()*(1-fZ.getVal()))/w_hist->Integral());
226     plotMetFit.AddToStack(w_hist,"W+jets",kCyan-6);
227     TH1F *h_obs = (TH1F*)obsMet.createHistogram("met",met,Binning(metbins));
228     plotMetFit.AddHist1D(h_obs,"Obs.");
229     plotMetFit.AddLine(30,0,30,850,kRed,kDashed);
230     plotMetFit.Draw(c,kTRUE,"png");
231    
232    
233     cout << "\nnW in mc: " << hmcW->Integral() << endl;
234     cout << "nZ in mc: " << hmcZ->Integral() << endl << endl;
235     }
236