ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/plot_emu_fakes.cc
Revision: 1.11
Committed: Mon Jul 16 10:21:13 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.10: +7 -4 lines
Log Message:
Add denominator def with Dz and D0 but no IP, move some things from Various to PlotHeaders, clean up plotters

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