ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/plot_emu_fakes.cc
Revision: 1.10
Committed: Fri Jul 13 07:44:52 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 dkralph 1.1 #include <iostream>
2     #include <algorithm>
3     #include <iomanip>
4    
5     #include "RooHistPdf.h"
6     #include "RooGlobalFunc.h"
7     #include "RooPlot.h"
8     #include "RooRealVar.h"
9     #include "RooDataHist.h"
10     #include "RooGenericPdf.h"
11     #include "RooDataSet.h"
12     #include "RooAddPdf.h"
13     #include "RooFormulaVar.h"
14     #include "RooFitResult.h"
15     #include "RooCurve.h"
16     #include "RooBinning.h"
17     #include "RooExponential.h"
18 dkralph 1.2 #include "RooExtendPdf.h"
19 dkralph 1.1
20     #include "TCanvas.h"
21     #include "TChain.h"
22     #include "TString.h"
23     #include "TStyle.h"
24     #include "TH1D.h"
25    
26     #include "CPlot.h"
27     #include "FOArgs.h"
28    
29     #include "fake_defs.h"
30 dkralph 1.5 #include "Various.h"
31 dkralph 1.9 #include "PlotHeaders.h"
32 dkralph 1.1
33     #ifndef CMSSW_BASE
34     #define CMSSW_BASE "../../"
35     #endif
36    
37     using namespace std;
38     using namespace RooFit;
39    
40     TCanvas *can;
41    
42 dkralph 1.9 void makeHTML(FOFlags &ctrl, TString plotLabel);
43 dkralph 1.1 map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str="");
44 dkralph 1.6 void fillHist(CSample *cs, TString type, TString var, double val, double wgt, double wgt_lo, double wgt_hi);
45 dkralph 1.1
46 dkralph 1.9 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hSSmet, double &total_mc_scale, TString plotLabel);
47 dkralph 1.1
48     //----------------------------------------------------------------------------------------
49     int main(int argc, char** argv)
50     {
51     double lumi = 1600;
52 dkralph 1.6
53 dkralph 1.1 FOFlags ctrl;
54     parse_foargs( argc, argv, ctrl );
55 dkralph 1.4 ctrl.dump();
56 dkralph 1.1
57     can = new TCanvas("can","can");
58    
59 dkralph 1.9 FR_struct fr2011 = initFRs(ctrl.mufakefile2011,ctrl.elefakefile2011);
60     FR_struct fr2012 = initFRs(ctrl.mufakefile2012,ctrl.elefakefile2012);
61 dkralph 1.1
62     TString cmsswpath(CMSSW_BASE + TString("/src"));
63     string xspath = (string(cmsswpath)+"/MitPhysics/data/xs.dat");
64     SimpleTable xstab(xspath.c_str());
65    
66     vector<CSample*> samplev;
67 dkralph 1.9 TString ntupledir(""),label(""),plotLabel("");
68     readConfigFile(ctrl.config, ntupledir, label, plotLabel, samplev, &ctrl, init_hists);
69 dkralph 1.1
70 dkralph 1.8 vector<pair<unsigned,unsigned> > runEvtv; // vector to veto duplicate events
71 dkralph 1.9 UInt_t minRun=999999999,maxRun=0;
72 dkralph 1.1 for(unsigned ics=0; ics<samplev.size(); ics++) {
73     CSample *cs = samplev[ics];
74 dkralph 1.8 cout << cs->name << ": " << endl;
75 dkralph 1.1 for(unsigned ifs=0; ifs<(cs->fsv).size(); ifs++) {
76     filestuff *fs = (cs->fsv)[ifs];
77 dkralph 1.8 cout << "\t" << fs->fname_ << endl;
78 dkralph 1.9 FR_struct *fr = (fs->era_==2011) ? &fr2011 : &fr2012;
79     unsigned nDuplSkipped=0;
80 dkralph 1.8 for(unsigned ientry=0; ientry<fs->getentries("FOtree"); ientry++) {
81 dkralph 1.1 fs->getentry(ientry,"info");
82 dkralph 1.9
83     if(fs->isdata_) {
84     setMinMaxRun(fs->info->run, minRun, maxRun);
85     // bool dupl = takeCareOfDuplicateEvents(fs->info->run, fs->info->evt, runEvtv, nDuplSkipped);
86     // if(dupl) continue;
87     }
88 dkralph 1.1
89     unsigned npass = fs->passingL->size();
90     unsigned nfail = fs->failingL->size();
91    
92 dkralph 1.6 // z veto
93     bool zVeto=false;
94     for(unsigned ilep1=0; ilep1<npass; ilep1++) {
95     SimpleLepton lep1 = (*fs->passingL)[ilep1];
96     for(unsigned ilep2=ilep1+1; ilep2<npass; ilep2++) {
97     SimpleLepton lep2 = (*fs->passingL)[ilep2];
98 dkralph 1.7 // if(abs(lep1.type) == abs(lep2.type)) zVeto = true;
99     // if(lep1.charge != lep2.charge) zVeto = true;
100     if(abs(lep1.type) == abs(lep2.type) &&
101     lep1.charge != lep2.charge) zVeto = true;
102 dkralph 1.6 }
103     }
104     if(zVeto) continue;
105    
106     for(unsigned iw=0; iw<npass; iw++) {
107 dkralph 1.1 SimpleLepton w_lep = (*fs->passingL)[iw];
108    
109 dkralph 1.7 double wgt=1;
110     if(!fs->isdata_) {
111     double xsWgt = lumi*xstab.Get(fs->dataset_)/fs->total_entries_;
112     double puWgt = weightTrue2012(fs->info->npu);
113     wgt = xsWgt*puWgt;
114     }
115 dkralph 1.1
116 dkralph 1.6 // make fake ele and fake mu plots separately
117 dkralph 1.8 assert(ctrl.faketype=="mu" || ctrl.faketype=="ele");
118     assert(abs(w_lep.type)==11 || abs(w_lep.type)==13);
119     if( abs(w_lep.type)==11 && ctrl.faketype=="ele") continue;
120     if( abs(w_lep.type)==13 && ctrl.faketype=="mu" ) continue;
121 dkralph 1.7
122 dkralph 1.1 // selection on the W lepton
123     // if(w_lep.isoPF04/w_lep.vec.Pt() > 0.025) continue;
124 dkralph 1.6 if(fs->info->met < 25) continue;
125 dkralph 1.2 if(!(w_lep.tightCutsApplied)) continue;
126 dkralph 1.6 if(w_lep.vec.Pt() < 25) continue;
127 dkralph 1.8 if(fabs(w_lep.ip3dSig) > 4) continue;
128     // if( fs->isdata_)
129     // if( !(w_lep.bdtfail & 2) ) continue;
130 dkralph 1.7 // if(!w_lep.isTight) continue; // this bit stores whether the w lepton is trigger matched
131 dkralph 1.1
132     // require an extra OF SS lepton
133     bool has_ssof=false;
134     vector<SimpleLepton> ssof_leps;
135     vector<TString> types;
136 dkralph 1.6 for(unsigned iextra=iw+1; iextra<npass; iextra++) {
137 dkralph 1.1 SimpleLepton ex_lep = (*fs->passingL)[iextra];
138     if(abs(ex_lep.type) == abs(w_lep.type)) continue;
139     if(ex_lep.charge != w_lep.charge) continue;
140     has_ssof = true;
141     ssof_leps.push_back(ex_lep);
142     types.push_back("pass");
143     }
144 dkralph 1.6 for(unsigned iextra=0; iextra<nfail; iextra++) {
145 dkralph 1.1 SimpleLepton ex_lep = (*fs->failingL)[iextra];
146     if(abs(ex_lep.type) == abs(w_lep.type)) continue;
147     if(ex_lep.charge != w_lep.charge) continue;
148     has_ssof = true;
149     ssof_leps.push_back(ex_lep);
150     types.push_back("fail");
151     }
152     if(!has_ssof) continue;
153    
154     // fill hists with W + (fake or pass)
155     double mt = get_mt(w_lep.vec.Phi(),fs->info->metphi,w_lep.vec.Pt(),fs->info->met);
156 dkralph 1.7 (*(cs->hists)["both"])["npv"]->Fill( fs->info->npv, wgt);
157 dkralph 1.1 (*(cs->hists)["both"])["w_pt"]->Fill( w_lep.vec.Pt(), wgt);
158     (*(cs->hists)["both"])["w_eta"]->Fill( w_lep.vec.Eta(), wgt);
159     (*(cs->hists)["both"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), wgt);
160     (*(cs->hists)["both"])["met"]->Fill( fs->info->met, wgt);
161     (*(cs->hists)["both"])["mt"]->Fill( mt, wgt);
162 dkralph 1.6 (*(cs->hists)["both"])["npass"]->Fill( npass, wgt);
163     (*(cs->hists)["both"])["nfail"]->Fill( nfail, wgt);
164 dkralph 1.8 (*(cs->hists)["both"])["run"]->Fill( fs->info->run, wgt);
165 dkralph 1.1
166     for(unsigned iextra=0; iextra<ssof_leps.size(); iextra++) {
167     SimpleLepton ex_lep = ssof_leps[iextra];
168     double mass = (w_lep.vec + ex_lep.vec).M();
169 dkralph 1.2
170 dkralph 1.8 if(fabs(ex_lep.ip3dSig) > 4) continue;
171 dkralph 1.1 if(types[iextra] == "fail") {
172    
173     // fill fake rate prediction
174 dkralph 1.5
175 dkralph 1.9 double fake_wgt = get_fake_weight("",ex_lep,*fr);
176     double fake_wgt_lo = get_fake_weight("lo",ex_lep,*fr);
177     double fake_wgt_hi = get_fake_weight("hi",ex_lep,*fr);
178 dkralph 1.5
179 dkralph 1.6 double centr=fake_wgt*wgt,lo=fake_wgt_lo*wgt,hi=fake_wgt_hi*wgt;
180     fillHist( cs, "pred", "w_pt", w_lep.vec.Pt(), centr, lo, hi);
181     fillHist( cs, "pred", "l2_pt", ex_lep.vec.Pt(), centr, lo, hi);
182     fillHist( cs, "pred", "w_eta", w_lep.vec.Eta(), centr, lo, hi);
183     fillHist( cs, "pred", "l2_eta", ex_lep.vec.Eta(), centr, lo, hi);
184     fillHist( cs, "pred", "w_isoPF04", w_lep.isoPF04/w_lep.vec.Pt(), centr, lo, hi);
185     fillHist( cs, "pred", "l2_isoPF04", ex_lep.isoPF04/ex_lep.vec.Pt(), centr, lo, hi);
186 dkralph 1.8 fillHist( cs, "pred", "w_ip3ds", w_lep.ip3dSig, centr, lo, hi);
187     fillHist( cs, "pred", "l2_ip3ds", ex_lep.ip3dSig, centr, lo, hi);
188 dkralph 1.6 fillHist( cs, "pred", "mass", mass, centr, lo, hi);
189     fillHist( cs, "pred", "met", fs->info->met, centr, lo, hi);
190     fillHist( cs, "pred", "mt", mt, centr, lo, hi);
191 dkralph 1.8 fillHist( cs, "pred", "run", fs->info->run, centr, lo, hi);
192 dkralph 1.6
193 dkralph 1.3 (*(cs->hists)["pred"])["fr"]->Fill( fake_wgt );
194 dkralph 1.6
195 dkralph 1.1 } else if(types[iextra] == "pass") {
196    
197     // fill the observation
198    
199 dkralph 1.6 fillHist( cs, "obs", "w_pt", w_lep.vec.Pt(), wgt, 0, 0);
200     fillHist( cs, "obs", "l2_pt", ex_lep.vec.Pt(), wgt, 0, 0);
201     fillHist( cs, "obs", "w_eta", w_lep.vec.Eta(), wgt, 0, 0);
202     fillHist( cs, "obs", "l2_eta", ex_lep.vec.Eta(), wgt, 0, 0);
203     fillHist( cs, "obs", "w_isoPF04", w_lep.isoPF04/w_lep.vec.Pt(), wgt, 0, 0);
204     fillHist( cs, "obs", "l2_isoPF04", ex_lep.isoPF04/ex_lep.vec.Pt(), wgt, 0, 0);
205 dkralph 1.8 fillHist( cs, "obs", "w_ip3ds", w_lep.ip3dSig, wgt, 0, 0);
206     fillHist( cs, "obs", "l2_ip3ds", ex_lep.ip3dSig, wgt, 0, 0);
207 dkralph 1.6 fillHist( cs, "obs", "mass", mass, wgt, 0, 0);
208     fillHist( cs, "obs", "met", fs->info->met, wgt, 0, 0);
209     fillHist( cs, "obs", "mt", mt, wgt, 0, 0);
210 dkralph 1.8 fillHist( cs, "obs", "run", fs->info->run, wgt, 0, 0);
211 dkralph 1.1 } else { cout << "ERROR: types: " << types[iextra] << endl; assert(0); }
212     }
213     }
214     }
215 dkralph 1.9 cout << "\t\tWARNING: not checking for duplicate events" << endl; //skipped " << nDuplSkipped << " duplicate events" << endl;
216 dkralph 1.1 }
217     }
218 dkralph 1.9 cout << "run range: " << setw(12) << minRun << setw(12) << maxRun << endl;
219 dkralph 1.1
220     assert(samplev.size()==3);
221     CSample *cs = samplev[0];
222     CSample *cs_mc = samplev[1];
223     CSample *cs_zmc = samplev[2];
224    
225     double total_mc_scale=-1;
226 dkralph 1.9 RooFitResult *fitres = fitMt(ctrl,(*(cs_zmc->hists)["both"])["mt"],(*(cs_mc->hists)["both"])["mt"],(*(cs->hists)["both"])["mt"],total_mc_scale,plotLabel);
227 dkralph 1.1
228 dkralph 1.6 cout << "\n\n\ntotal mc scale: " << total_mc_scale << endl << endl << endl;
229    
230 dkralph 1.1 // RooArgList float_pars = fitres->floatParsFinal();
231     // RooRealVar *nWandZ = (RooRealVar*)float_pars.at(float_pars.index("nWandZ"));
232     // cout << nWandZ->getVal() << endl;
233    
234 dkralph 1.9 gSystem->mkdir(ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype,true);
235     TFile runHistFile(ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype+"/runs.root","recreate");
236 dkralph 1.1 map<TString,TH1D*>::iterator it_v;
237     for(it_v=(*(cs->hists)["pred"]).begin(); it_v!=(*(cs->hists)["pred"]).end(); it_v++) {
238     TString var((*it_v).first);
239    
240 dkralph 1.6 // plot passing and failing fakes together
241 dkralph 1.2 if(var!="mt") {
242 dkralph 1.1 (*(cs_mc->hists)["both"])[var]->Scale(total_mc_scale);
243     (*(cs_zmc->hists)["both"])[var]->Scale(total_mc_scale);
244     }
245 dkralph 1.8 TH1D *hBoth = (*(cs->hists)["both"])[var];
246 dkralph 1.9 CPlot cplot_both("both_"+var,"",hBoth->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype+"/plots");
247 dkralph 1.8 cplot_both.AddHist1D(hBoth,"tight+fk'ble: "+integral_str(hBoth),"E");
248 dkralph 1.1 cplot_both.AddToStack((*(cs_zmc->hists)["both"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["both"])[var]),920);
249     cplot_both.AddToStack((*(cs_mc->hists)["both"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["both"])[var]),426);
250    
251 dkralph 1.8 if(var.Contains("eta")) cplot_both.SetYRange(0,1.4*hBoth->GetMaximum());
252 dkralph 1.1 if(var.Contains("isoPF")) cplot_both.SetLogy();
253 dkralph 1.2 if(var=="met") cplot_both.AddLine(25,0,25,18050,843,kDashed);
254 dkralph 1.1 cplot_both.Draw(can,true,"png");
255 dkralph 1.8 if(cs->isdata && var=="run") {
256     assert(hBoth->GetBinContent(0)==0 && hBoth->GetBinContent(hBoth->GetXaxis()->GetNbins()+1)==0);
257     hBoth->Write("runs");
258     }
259 dkralph 1.1
260     // plot observation vs. prediction
261     (*(cs_mc->hists)["obs"])[var]->Scale(total_mc_scale);
262     (*(cs_zmc->hists)["obs"])[var]->Scale(total_mc_scale);
263 dkralph 1.9 CPlot cplot(var,"",(*(cs->hists)["pred"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype+"/plots");
264 dkralph 1.10 cplot.AddHist1D((*(cs->hists)["obs"])[var],cs->legend+": "+integral_str((*(cs->hists)["obs"])[var]),"E");
265 dkralph 1.1 cplot.AddHist1D((*(cs->hists)["pred"])[var],"FR predic: "+integral_str((*(cs->hists)["pred"])[var]),"hist",kRed);
266 dkralph 1.6 cplot.AddHist1D((*(cs->hists)["pred_lo"])[var],"stat lo: "+integral_str((*(cs->hists)["pred_lo"])[var]),"hist",kRed,kDashed);
267     cplot.AddHist1D((*(cs->hists)["pred_hi"])[var],"stat hi: "+integral_str((*(cs->hists)["pred_hi"])[var]),"hist",kRed,kDashed);
268    
269     // stack on the mc
270 dkralph 1.5 // cplot.AddToStack((*(cs_zmc->hists)["obs"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["obs"])[var]),920);
271     // cplot.AddToStack((*(cs_mc->hists)["obs"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["obs"])[var]),426);
272 dkralph 1.1
273     if(var.Contains("eta")) cplot.SetYRange(0,1.4*max((*(cs->hists)["obs"])[var]->GetMaximum(),(*(cs->hists)["pred"])[var]->GetMaximum()));
274 dkralph 1.8 if(var.Contains("isoPF") || var.Contains("ip3ds")) {
275 dkralph 1.6 cplot.SetLogy();
276     TH1D *hobs = (*(cs->hists)["obs"])[var];
277     TH1D *hpred = (*(cs->hists)["pred"])[var];
278     double isoMin = 0.008*min(hobs->GetBinContent(1), hpred->GetBinContent(1));
279     double isoMax = 1.8*max(hobs->GetBinContent(1), hpred->GetBinContent(1));
280    
281     cplot.SetYRange(isoMin,isoMax);
282     // cplot.SetYRange(0,0.8*min((*(cs->hists)["obs"])[var]->GetMinimum(),(*(cs->hists)["pred"])[var]->GetMinimum()));
283     }
284 dkralph 1.1 cplot.Draw(can,true,"png");
285     }
286 dkralph 1.8 runHistFile.Close();
287 dkralph 1.1
288 dkralph 1.9 makeHTML(ctrl,plotLabel);
289 dkralph 1.1 }
290     //----------------------------------------------------------------------------------------
291     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str)
292     {
293 dkralph 1.5 map<TString,map<TString,TH1D*>* > hists;
294     map<TString,TH1D*> *h_both = new map<TString,TH1D*>; hists["both"] = h_both;
295     map<TString,TH1D*> *h_pred = new map<TString,TH1D*>; hists["pred"] = h_pred;
296     map<TString,TH1D*> *h_pred_lo = new map<TString,TH1D*>; hists["pred_lo"] = h_pred_lo;
297     map<TString,TH1D*> *h_pred_hi = new map<TString,TH1D*>; hists["pred_hi"] = h_pred_hi;
298     map<TString,TH1D*> *h_obs = new map<TString,TH1D*>; hists["obs"] = h_obs;
299 dkralph 1.1 map<TString,map<TString,TH1D*>* >::iterator it_h;
300     double w_pt_max,l2_pt_max,mass_max;
301     if(ctrl.faketype=="mu") {
302     w_pt_max = 80;
303 dkralph 1.5 l2_pt_max = 35;
304 dkralph 1.1 mass_max = 120;
305     } else if(ctrl.faketype=="ele"){
306     w_pt_max = 90;
307     l2_pt_max = 60;
308     mass_max = 210;
309     } else assert(0);
310    
311     for(it_h=hists.begin(); it_h!=hists.end(); it_h++) {
312 dkralph 1.8 (*((*it_h).second))["npv"] = new TH1D(TString("npv") +"_"+(*it_h).first+str,";#bf{n PV};", 50,-.5,49.5); (*((*it_h).second))["npv"]->Sumw2();
313     (*((*it_h).second))["w_pt"] = new TH1D(TString("w_pt") +"_"+(*it_h).first+str,";#bf{real p_{T} [GeV]};", 37,15,w_pt_max); (*((*it_h).second))["w_pt"]->Sumw2();
314     (*((*it_h).second))["l2_pt"] = new TH1D(TString("l2_pt") +"_"+(*it_h).first+str,";#bf{fake p_{T} [GeV]};", 25,0,l2_pt_max); (*((*it_h).second))["l2_pt"]->Sumw2();
315     (*((*it_h).second))["w_eta"] = new TH1D(TString("w_eta")+"_"+(*it_h).first+str,";#bf{real #eta};", 25,-2.5,2.5); (*((*it_h).second))["w_eta"]->Sumw2();
316     (*((*it_h).second))["l2_eta"] = new TH1D(TString("l2_eta")+"_"+(*it_h).first+str,";#bf{fake #eta};", 25,-2.5,2.5); (*((*it_h).second))["l2_eta"]->Sumw2();
317     (*((*it_h).second))["w_isoPF04"] = new TH1D(TString("w_isoPF04")+"_"+(*it_h).first+str,";#bf{real PF Iso};", 25,0,1); (*((*it_h).second))["w_isoPF04"]->Sumw2();
318     (*((*it_h).second))["l2_isoPF04"] = new TH1D(TString("l2_isoPF04")+"_"+(*it_h).first+str,";#bf{fake PF Iso};", 25,0,3); (*((*it_h).second))["l2_isoPF04"]->Sumw2();
319     (*((*it_h).second))["w_ip3ds"] = new TH1D(TString("w_ip3ds")+"_"+(*it_h).first+str,";#bf{real ip3dSig};", 25,0,10); (*((*it_h).second))["w_ip3ds"]->Sumw2();
320     (*((*it_h).second))["l2_ip3ds"] = new TH1D(TString("l2_ip3ds")+"_"+(*it_h).first+str,";#bf{fake ip3dSig};", 25,0,10); (*((*it_h).second))["l2_ip3ds"]->Sumw2();
321     (*((*it_h).second))["mass"] = new TH1D(TString("mass") +"_"+(*it_h).first+str,";#bf{m_{ll}};", 25,0,mass_max); (*((*it_h).second))["mass"]->Sumw2();
322     (*((*it_h).second))["met"] = new TH1D(TString("met") +"_"+(*it_h).first+str,";#bf{MET};", 25,0,100); (*((*it_h).second))["met"]->Sumw2();
323     (*((*it_h).second))["mt"] = new TH1D(TString("mt") +"_"+(*it_h).first+str,";#bf{m_{T}};", 25,2,150); (*((*it_h).second))["mt"]->Sumw2();
324     (*((*it_h).second))["fr"] = new TH1D(TString("fr") +"_"+(*it_h).first+str,";#bf{FR};", 125,-.01,.1); (*((*it_h).second))["fr"]->Sumw2();
325     (*((*it_h).second))["npass"] = new TH1D(TString("npass") +"_"+(*it_h).first+str,";#bf{N Pass};", 125,-.01,4); (*((*it_h).second))["npass"]->Sumw2();
326     (*((*it_h).second))["nfail"] = new TH1D(TString("nfail") +"_"+(*it_h).first+str,";#bf{N Fail};", 125,-.01,4); (*((*it_h).second))["nfail"]->Sumw2();
327     (*((*it_h).second))["run"] = new TH1D(TString("run") +"_"+(*it_h).first+str,";#bf{Run};", 50,160405,196550); (*((*it_h).second))["run"]->Sumw2();
328 dkralph 1.1 }
329    
330     return hists;
331     }
332     //--------------------------------------------------------------------------------------------------
333 dkralph 1.9 void makeHTML(FOFlags &ctrl, TString plotLabel)
334 dkralph 1.1 {
335     // TString title(ctrl.inputdir);
336     // title = title(title.Last('/')+1,title.Length());
337     TString title("EMU closure test: Fake "+ctrl.faketype);
338 dkralph 1.9 TString htmlfname(ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype+"/plots.html");
339     ofstream htmlfile(htmlfname);;
340 dkralph 1.1
341     htmlfile << "<!DOCTYPE html" << endl;
342     htmlfile << " PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" << endl;
343     htmlfile << "<html>" << endl;
344    
345     htmlfile << "<head><title>"+title+"</title></head>" << endl;
346     htmlfile << "<body bgcolor=\"EEEEEE\">" << endl;
347     htmlfile << "<h3 style=\"text-align:left; color:DD6600;\">"+title+"</h3>" << endl;
348    
349     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
350     htmlfile << "<tr>" << endl;
351 dkralph 1.2 htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/mtfitW.png\"><img src=\"plots/mtfitW.png\" alt=\"plots/mtfitW.png\" width=\"100%\"></a></td>" << endl;
352     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/both_met.png\"><img src=\"plots/both_met.png\" alt=\"plots/both_met.png\" width=\"100%\"></a></td>" << endl;
353 dkralph 1.1 htmlfile << "</tr>" << endl;
354     htmlfile << "</table>" << endl;
355    
356     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
357     htmlfile << "<tr>" << endl;
358 dkralph 1.3 htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/fr.png\"><img src=\"plots/fr.png\" alt=\"plots/fr.png\" width=\"100%\"></a></td>" << endl;
359 dkralph 1.7 htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/both_npv.png\"><img src=\"plots/both_npv.png\" alt=\"plots/both_npv.png\" width=\"100%\"></a></td>" << endl;
360 dkralph 1.8 htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/run.png\"><img src=\"plots/run.png\" alt=\"plots/run.png\" width=\"100%\"></a></td>" << endl;
361 dkralph 1.3 htmlfile << "</tr>" << endl;
362     htmlfile << "</table>" << endl;
363    
364     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
365     htmlfile << "<tr>" << endl;
366 dkralph 1.1 htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/mass.png\"><img src=\"plots/mass.png\" alt=\"plots/mass.png\" width=\"100%\"></a></td>" << endl;
367     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/mt.png\"><img src=\"plots/mt.png\" alt=\"plots/mt.png\" width=\"100%\"></a></td>" << endl;
368     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/met.png\"><img src=\"plots/met.png\" alt=\"plots/met.png\" width=\"100%\"></a></td>" << endl;
369     htmlfile << "</tr>" << endl;
370     htmlfile << "</table>" << endl;
371    
372     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
373     htmlfile << "<tr>" << endl;
374 dkralph 1.8 htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/w_pt.png\"><img src=\"plots/w_pt.png\" alt=\"plots/w_pt.png\" width=\"100%\"></a></td>" << endl;
375     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/w_eta.png\"><img src=\"plots/w_eta.png\" alt=\"plots/w_eta.png\" width=\"100%\"></a></td>" << endl;
376     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/w_isoPF04.png\"><img src=\"plots/w_isoPF04.png\" alt=\"plots/w_isoPF04.png\" width=\"100%\"></a></td>" << endl;
377     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/w_ip3ds.png\"><img src=\"plots/w_ip3ds.png\" alt=\"plots/w_ip3ds.png\" width=\"100%\"></a></td>" << endl;
378 dkralph 1.1 htmlfile << "</tr>" << endl;
379     htmlfile << "</table>" << endl;
380    
381     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
382     htmlfile << "<tr>" << endl;
383 dkralph 1.8 htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/l2_pt.png\"><img src=\"plots/l2_pt.png\" alt=\"plots/l2_pt.png\" width=\"100%\"></a></td>" << endl;
384     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/l2_eta.png\"><img src=\"plots/l2_eta.png\" alt=\"plots/l2_eta.png\" width=\"100%\"></a></td>" << endl;
385     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/l2_isoPF04.png\"><img src=\"plots/l2_isoPF04.png\" alt=\"plots/l2_isoPF04.png\" width=\"100%\"></a></td>" << endl;
386     htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/l2_ip3ds.png\"><img src=\"plots/l2_ip3ds.png\" alt=\"plots/l2_ip3ds.png\" width=\"100%\"></a></td>" << endl;
387 dkralph 1.1 htmlfile << "</tr>" << endl;
388     htmlfile << "</table>" << endl;
389     htmlfile << "<hr />" << endl;
390    
391 dkralph 1.6 // htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
392     // htmlfile << "<tr>" << endl;
393     // htmlfile << "<td width=\"25%\"><a target=\"_blank\" href=\"plots/both_w_isoPF04.png\"><img src=\"plots/both_w_isoPF04.png\" alt=\"plots/both_w_isoPF04.png\" width=\"100%\"></a></td>" << endl;
394     // htmlfile << "</tr>" << endl;
395     // htmlfile << "</table>" << endl;
396 dkralph 1.1
397     htmlfile << "<hr />" << endl;
398    
399     htmlfile << "<hr />" << endl;
400    
401     htmlfile << "</body>" << endl;
402     htmlfile << "</html>" << endl;
403     htmlfile.close();
404     }
405     //----------------------------------------------------------------------------------------
406 dkralph 1.9 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hdata, double &total_mc_scale, TString plotLabel)
407 dkralph 1.1 {
408 dkralph 1.2 double original_w_integral = integrateHist(hmcW);
409 dkralph 1.1
410 dkralph 1.2 RooRealVar mt("mt","MT [GeV]",hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
411     RooBinning mtbins(hdata->GetNbinsX(),hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
412     mt.setBinning(mtbins);
413 dkralph 1.1
414     // data
415 dkralph 1.2 RooDataHist obsMt("obsMt","obsMt",RooArgList(mt),hdata);
416 dkralph 1.1 // W mc pdf
417     hmcW->Smooth(2);
418 dkralph 1.2 RooDataHist hW("hW","hW",RooArgList(mt),hmcW);
419     RooHistPdf pW("pW","pW",RooArgSet(mt),hW,3);
420     // Z mc pdf
421     RooDataHist hZ("hZ","hZ",RooArgList(mt),hmcZ);
422     RooHistPdf pZ("pZ","pZ",RooArgSet(mt),hZ,3);
423 dkralph 1.1 // rayleigh pdf
424 dkralph 1.2 RooRealVar sig("sig","sig",400,15,585);
425     RooRealVar a1("a1","a1",-3,-10,10);
426     // RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(2*sig*sig))",RooArgList(mt,sig));
427     RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(sig*sig +2*sig*a1*(mt) + a1*a1*(mt)*(mt)))",RooArgList(mt,sig,a1));
428    
429     RooRealVar nObs("nObs","nObs",integrateHist(hdata));
430     RooRealVar nWandZ("nWandZ","nWandZ",0.98*nObs.getVal(),.95*nObs.getVal(),1.1*nObs.getVal());
431     RooRealVar zFrac("zFrac","zFrac",integrateHist(hmcZ) / (integrateHist(hmcZ) + integrateHist(hmcW)));
432     RooAddPdf modelWandZ("modelWandZ","modelWandZ",RooArgList(pZ,pW),zFrac);
433     RooRealVar nRayl("nRayl","nRayl",0.05*nObs.getVal(),0.0*nObs.getVal(),.05*nObs.getVal());
434    
435     RooExtendPdf extWandZ("extWandZ","extWandZ",modelWandZ,nWandZ);
436     RooExtendPdf extRayl("extRayl","extRayl",rayl,nRayl);
437    
438     RooAddPdf *model = new RooAddPdf("model","model",RooArgList(extWandZ,extRayl));
439    
440     RooFitResult *fitres = model->fitTo(obsMt,Extended(kTRUE),Save(kTRUE));
441     fitres->Print();
442    
443     TH1D *rayl_hist = (TH1D*)rayl.createHistogram("rayl_mt",mt,Binning(mtbins));
444     rayl_hist->Scale(nRayl.getVal()/integrateHist(rayl_hist));
445     TH1D *totalHist = (TH1D*)model->createHistogram("total_mt",mt,Binning(mtbins));
446     totalHist->Scale( (nWandZ.getVal() + nRayl.getVal()) / integrateHist(totalHist) );
447    
448     // // why the fuck do I need this?
449     // double tot = nWandZ.getVal() + nRayl.getVal();
450     // if(use_exp) tot += nExp.getVal();
451     // double hack_fac = integrateHist(hdata)/tot;
452     // rayl_hist->Scale(hack_fac);
453     // exp_hist->Scale(hack_fac);
454     // hmcZ->Scale(hack_fac);
455     // hmcW->Scale(hack_fac);
456    
457     // RooPlot *frame = mt.frame();
458     // obsMt.plotOn(frame);
459     // model->plotOn(frame);
460     // model->plotOn(frame,Components("pW"),LineStyle(kDashed),LineColor(kRed));
461     // model->plotOn(frame,Components("pZ"),LineColor(kRed));
462     // model->plotOn(frame,Components("rayl"),LineColor(kGreen));
463     // frame->Draw();
464     // can->SaveAs("~/public_html/foo.png");
465    
466     hmcW->Scale( (nWandZ.getVal()*(1-zFrac.getVal())) / integrateHist(hmcW) );
467     hmcZ->Scale( (nWandZ.getVal()*zFrac.getVal()) / integrateHist(hmcZ) );
468 dkralph 1.1
469 dkralph 1.9 CPlot plotMtFit("mtfitW","","MT [GeV]","Events",ctrl.outdir+"/"+plotLabel+"/"+ctrl.faketype+"/plots");
470 dkralph 1.2 plotMtFit.AddHist1D(hdata,"tight+fk'ble: " + integral_str(hdata),"E");
471     plotMtFit.AddHist1D(totalHist,"total MC: " + integral_str(totalHist),"hist",kRed);
472     plotMtFit.AddToStack(rayl_hist,"jet (Rayleigh): " + integral_str(rayl_hist),kBlue);
473     plotMtFit.AddToStack(hmcZ,"Z+j MC: " + integral_str(hmcZ),kGray);
474     plotMtFit.AddToStack(hmcW,"W+j MC: " + integral_str(hmcW),kCyan-6);
475     plotMtFit.Draw(can,kTRUE,"png");
476 dkralph 1.1
477 dkralph 1.2 total_mc_scale = integrateHist(hmcW) / original_w_integral;
478 dkralph 1.1
479     return fitres;
480     }
481 dkralph 1.6 //----------------------------------------------------------------------------------------
482     void fillHist(CSample *cs, TString type, TString var, double val, double wgt, double wgt_lo, double wgt_hi)
483     {
484     (*(cs->hists)[type])[var]->Fill( val, wgt);
485     if(type=="pred") {
486     (*(cs->hists)[type+"_lo"])[var]->Fill( val, wgt_lo);
487     (*(cs->hists)[type+"_hi"])[var]->Fill( val, wgt_hi);
488     }
489     }