ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/plot_emu_fakes.cc
Revision: 1.5
Committed: Tue Jun 19 17:11:28 2012 UTC (12 years, 11 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.4: +41 -20 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.1
32     #ifndef CMSSW_BASE
33     #define CMSSW_BASE "../../"
34     #endif
35    
36     using namespace std;
37     using namespace RooFit;
38    
39     TCanvas *can;
40    
41 dkralph 1.5 double get_fake_weight(TString uncert, SimpleLepton fake_lep, FR_struct fr);
42 dkralph 1.1 void makeHTML(FOFlags &ctrl);
43     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str="");
44     TVector3 get_corrected_met(double met, double metphi, TLorentzVector lepton);
45     TString entries_str(TH1D *hist);
46     TString integral_str(TH1D *hist);
47 dkralph 1.2 double integrateHist(TH1D *hist) { return hist->Integral(0,hist->GetNbinsX()+1); }
48 dkralph 1.1
49 dkralph 1.2 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hSSmet, double &total_mc_scale);
50 dkralph 1.1
51     //----------------------------------------------------------------------------------------
52     int main(int argc, char** argv)
53     {
54     double lumi = 1600;
55    
56     FOFlags ctrl;
57     parse_foargs( argc, argv, ctrl );
58 dkralph 1.4 ctrl.dump();
59 dkralph 1.1
60     can = new TCanvas("can","can");
61    
62     // FR_struct fr = initFRs(ctrl.mufakedir+"/fr.root",ctrl.elefakedir+"/fr.root");
63     FR_struct fr = initFRs(ctrl.mufakedir,ctrl.elefakedir);
64    
65     TString cmsswpath(CMSSW_BASE + TString("/src"));
66     string xspath = (string(cmsswpath)+"/MitPhysics/data/xs.dat");
67     SimpleTable xstab(xspath.c_str());
68    
69     vector<CSample*> samplev;
70    
71     ifstream instream;
72     instream.open(ctrl.config); assert(instream.is_open());
73     TString ntupledir;
74     string line;
75     while(getline(instream,line)) {
76     if(line[0]=='#') continue;
77    
78     stringstream ss(line);
79     if(line[0]=='^') {
80     TString dummy;
81     if(TString(line).Contains("^ntupdir")) ss >> dummy >> ntupledir;
82     // if(TString(line).Contains("^skim")) ss >> dummy >> ntupledir;
83     continue;
84     }
85    
86     if(line[0]=='$') {
87     samplev.push_back(new CSample());
88     stringstream ss(line);
89     string chr;
90     TString sname;
91     Int_t color;
92     ss >> chr >> sname >> color;
93     string legend = line.substr(line.find('@')+1);
94     samplev.back()->name = sname;
95     samplev.back()->legend = legend;
96     samplev.back()->color = color;
97     samplev.back()->hists = init_hists(ctrl,sname);
98     continue;
99     }
100    
101     TString dset;
102     bool isdata;
103     double xsec;
104     ss >> dset >> isdata >> xsec;
105 dkralph 1.4 assert(ctrl.muSele==ctrl.eleSele);
106     TString fname = ntupledir+"/"+ctrl.muSele+"/"+dset+"/merged.root";
107 dkralph 1.1 (samplev.back()->fsv).push_back(new filestuff(dset,fname,dset,isdata));
108     }
109     instream.close();
110    
111    
112     for(unsigned ics=0; ics<samplev.size(); ics++) {
113     CSample *cs = samplev[ics];
114    
115     for(unsigned ifs=0; ifs<(cs->fsv).size(); ifs++) {
116    
117     filestuff *fs = (cs->fsv)[ifs];
118     for(unsigned ientry=0; ientry<fs->getentries(); ientry++) {
119     fs->getentry(ientry,"info");
120    
121     // veto extra leptons
122     unsigned npass = fs->passingL->size();
123     unsigned nfail = fs->failingL->size();
124     if(npass+nfail != 2) continue;
125    
126     for(unsigned iw=0; iw<fs->passingL->size(); iw++) {
127     SimpleLepton w_lep = (*fs->passingL)[iw];
128    
129     double wgt = fs->isdata_ ? 1 : lumi*xstab.Get(fs->dataset_)/fs->total_entries_;
130    
131     // selection on the W lepton
132     // if(w_lep.isoPF04/w_lep.vec.Pt() > 0.025) continue;
133 dkralph 1.2 if(!(w_lep.tightCutsApplied)) continue;
134 dkralph 1.1 if((abs(w_lep.type)==11) && (ctrl.faketype=="mu")) {
135     if(w_lep.vec.Pt() < 20) continue;
136     } else if((abs(w_lep.type)==13) && (ctrl.faketype=="ele")) {
137     if(w_lep.vec.Pt() < 25) continue;
138     } else continue;
139    
140     // require an extra OF SS lepton
141     bool has_ssof=false;
142     vector<SimpleLepton> ssof_leps;
143     vector<TString> types;
144     for(unsigned iextra=0; iextra<fs->passingL->size(); iextra++) {
145     SimpleLepton ex_lep = (*fs->passingL)[iextra];
146    
147     if(iextra == iw) continue; // unnecessary
148     if(abs(ex_lep.type) == abs(w_lep.type)) continue;
149     if(ex_lep.charge != w_lep.charge) continue;
150     has_ssof = true;
151     ssof_leps.push_back(ex_lep);
152     types.push_back("pass");
153     }
154     for(unsigned iextra=0; iextra<fs->failingL->size(); iextra++) {
155     SimpleLepton ex_lep = (*fs->failingL)[iextra];
156    
157     if(abs(ex_lep.type) == abs(w_lep.type)) continue;
158     if(ex_lep.charge != w_lep.charge) continue;
159     has_ssof = true;
160     ssof_leps.push_back(ex_lep);
161     types.push_back("fail");
162     }
163     if(!has_ssof) continue;
164    
165     // fill hists with W + (fake or pass)
166     double mt = get_mt(w_lep.vec.Phi(),fs->info->metphi,w_lep.vec.Pt(),fs->info->met);
167 dkralph 1.2 if(fs->info->met < 25) continue;
168 dkralph 1.1 (*(cs->hists)["both"])["w_pt"]->Fill( w_lep.vec.Pt(), wgt);
169     (*(cs->hists)["both"])["w_eta"]->Fill( w_lep.vec.Eta(), wgt);
170     (*(cs->hists)["both"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), wgt);
171     (*(cs->hists)["both"])["met"]->Fill( fs->info->met, wgt);
172     (*(cs->hists)["both"])["mt"]->Fill( mt, wgt);
173    
174     for(unsigned iextra=0; iextra<ssof_leps.size(); iextra++) {
175     SimpleLepton ex_lep = ssof_leps[iextra];
176     double mass = (w_lep.vec + ex_lep.vec).M();
177 dkralph 1.3 // if(dr(w_lep,ex_lep) < 0.05) continue;
178     //????????????????????????????????????????????????????????????????????????????????????????
179 dkralph 1.2
180 dkralph 1.3 if(mass < 1) {
181     cout << "low mass: " << mass << endl
182     << "\tW lepton: " << w_lep.type << setw(12) << w_lep.vec.Eta() << setw(12) << w_lep.vec.Phi()
183     << "\tf'ble lepton: " << ex_lep.type << setw(12) << ex_lep.vec.Eta() << setw(12) << ex_lep.vec.Phi()
184     << endl;
185     }
186 dkralph 1.1
187     if(types[iextra] == "fail") {
188    
189     // fill fake rate prediction
190 dkralph 1.5
191     double fake_wgt = get_fake_weight("",ex_lep,fr);
192     double fake_wgt_lo = get_fake_weight("lo",ex_lep,fr);
193     double fake_wgt_hi = get_fake_weight("hi",ex_lep,fr);
194    
195 dkralph 1.3 (*(cs->hists)["pred"])["w_pt"]->Fill( w_lep.vec.Pt(), fake_wgt*wgt);
196     (*(cs->hists)["pred"])["l2_pt"]->Fill( ex_lep.vec.Pt(), fake_wgt*wgt);
197 dkralph 1.5 (*(cs->hists)["pred_lo"])["l2_pt"]->Fill( ex_lep.vec.Pt(), fake_wgt_lo*wgt);
198     (*(cs->hists)["pred_hi"])["l2_pt"]->Fill( ex_lep.vec.Pt(), fake_wgt_hi*wgt);
199 dkralph 1.3 (*(cs->hists)["pred"])["w_eta"]->Fill( w_lep.vec.Eta(), fake_wgt*wgt);
200     (*(cs->hists)["pred"])["l2_eta"]->Fill( ex_lep.vec.Eta(), fake_wgt*wgt);
201 dkralph 1.5 (*(cs->hists)["pred_lo"])["l2_eta"]->Fill( ex_lep.vec.Eta(), fake_wgt_lo*wgt);
202     (*(cs->hists)["pred_hi"])["l2_eta"]->Fill( ex_lep.vec.Eta(), fake_wgt_hi*wgt);
203 dkralph 1.3 (*(cs->hists)["pred"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), fake_wgt*wgt);
204     (*(cs->hists)["pred"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), fake_wgt*wgt);
205     (*(cs->hists)["pred"])["mass"]->Fill( mass, fake_wgt*wgt);
206 dkralph 1.5 (*(cs->hists)["pred_lo"])["mass"]->Fill( mass, fake_wgt_lo*wgt);
207     (*(cs->hists)["pred_hi"])["mass"]->Fill( mass, fake_wgt_hi*wgt);
208 dkralph 1.3 (*(cs->hists)["pred"])["met"]->Fill( fs->info->met, fake_wgt*wgt);
209     (*(cs->hists)["pred"])["mt"]->Fill( mt, fake_wgt*wgt);
210     (*(cs->hists)["pred"])["fr"]->Fill( fake_wgt );
211 dkralph 1.1 } else if(types[iextra] == "pass") {
212    
213     // fill the observation
214    
215 dkralph 1.3 (*(cs->hists)["obs"])["w_pt"]->Fill( w_lep.vec.Pt(), wgt);
216     (*(cs->hists)["obs"])["l2_pt"]->Fill( ex_lep.vec.Pt(), wgt);
217     (*(cs->hists)["obs"])["w_eta"]->Fill( w_lep.vec.Eta(), wgt);
218     (*(cs->hists)["obs"])["l2_eta"]->Fill( ex_lep.vec.Eta(), wgt);
219     (*(cs->hists)["obs"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), wgt);
220     (*(cs->hists)["obs"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), wgt);
221     (*(cs->hists)["obs"])["mass"]->Fill( mass, wgt);
222     (*(cs->hists)["obs"])["met"]->Fill( fs->info->met, wgt);
223     (*(cs->hists)["obs"])["mt"]->Fill( mt, wgt);
224 dkralph 1.1 } else { cout << "ERROR: types: " << types[iextra] << endl; assert(0); }
225     }
226     }
227     }
228     }
229     }
230    
231     assert(samplev.size()==3);
232     CSample *cs = samplev[0];
233     CSample *cs_mc = samplev[1];
234     CSample *cs_zmc = samplev[2];
235    
236     double total_mc_scale=-1;
237 dkralph 1.2 RooFitResult *fitres = fitMt(ctrl,(*(cs_zmc->hists)["both"])["mt"],(*(cs_mc->hists)["both"])["mt"],(*(cs->hists)["both"])["mt"],total_mc_scale);
238 dkralph 1.1
239     // RooArgList float_pars = fitres->floatParsFinal();
240     // RooRealVar *nWandZ = (RooRealVar*)float_pars.at(float_pars.index("nWandZ"));
241     // cout << nWandZ->getVal() << endl;
242    
243     map<TString,TH1D*>::iterator it_v;
244     for(it_v=(*(cs->hists)["pred"]).begin(); it_v!=(*(cs->hists)["pred"]).end(); it_v++) {
245     TString var((*it_v).first);
246    
247     // plot passing and failing fakes
248 dkralph 1.2 if(var!="mt") {
249 dkralph 1.1 (*(cs_mc->hists)["both"])[var]->Scale(total_mc_scale);
250     (*(cs_zmc->hists)["both"])[var]->Scale(total_mc_scale);
251     }
252     CPlot cplot_both("both_"+var,"",(*(cs->hists)["both"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
253     cplot_both.AddHist1D((*(cs->hists)["both"])[var],"tight+fk'ble: "+integral_str((*(cs->hists)["both"])[var]),"E");
254     cplot_both.AddToStack((*(cs_zmc->hists)["both"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["both"])[var]),920);
255     cplot_both.AddToStack((*(cs_mc->hists)["both"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["both"])[var]),426);
256    
257     if(var.Contains("eta")) cplot_both.SetYRange(0,1.4*(*(cs->hists)["both"])[var]->GetMaximum());
258     if(var.Contains("isoPF")) cplot_both.SetLogy();
259 dkralph 1.2 if(var=="met") cplot_both.AddLine(25,0,25,18050,843,kDashed);
260 dkralph 1.1 cplot_both.Draw(can,true,"png");
261    
262     // plot observation vs. prediction
263     (*(cs_mc->hists)["obs"])[var]->Scale(total_mc_scale);
264     (*(cs_zmc->hists)["obs"])[var]->Scale(total_mc_scale);
265     CPlot cplot(var,"",(*(cs->hists)["pred"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
266     cplot.AddHist1D((*(cs->hists)["obs"])[var],"Obs: "+integral_str((*(cs->hists)["obs"])[var]),"E");
267     cplot.AddHist1D((*(cs->hists)["pred"])[var],"FR predic: "+integral_str((*(cs->hists)["pred"])[var]),"hist",kRed);
268 dkralph 1.5 if(var=="mass" || var=="l2_pt" || var=="l2_eta") {
269     cplot.AddHist1D((*(cs->hists)["pred_lo"])[var],"FR lo: "+integral_str((*(cs->hists)["pred_lo"])[var]),"hist",kRed,kDashed);
270     cplot.AddHist1D((*(cs->hists)["pred_hi"])[var],"FR hi: "+integral_str((*(cs->hists)["pred_hi"])[var]),"hist",kRed,kDashed);
271     }
272    
273     // 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     if(var.Contains("isoPF")) cplot.SetLogy();
278     cplot.Draw(can,true,"png");
279     }
280    
281     makeHTML(ctrl);
282     }
283     //----------------------------------------------------------------------------------------
284 dkralph 1.5 double get_fake_weight(TString uncert, SimpleLepton fake_lep, FR_struct fr)
285 dkralph 1.1 {
286     double rate=0;
287 dkralph 1.5 assert(uncert=="" || uncert=="lo" || uncert=="hi");
288 dkralph 1.1 if(abs(fake_lep.type) == 13) { // fake muons:
289 dkralph 1.5 double x = fake_lep.vec.Eta();
290     if(fr.absEtaMu) x = fabs(x);
291     double y = fake_lep.vec.Pt();
292     rate = fr.mufr.getEff(x,y,true);
293     if(uncert == "lo") rate -= fr.mufr.getErrLow(x,y,true);
294     if(uncert == "hi") rate += fr.mufr.getErrHigh(x,y,true);
295 dkralph 1.1 } else if(abs(fake_lep.type) == 11) {
296 dkralph 1.5 double x = fake_lep.vec.Eta();
297     if(fr.absEtaEle) x = fabs(x);
298     double y = fake_lep.vec.Pt();
299     rate = fr.elefr.getEff(x,y,true);
300     if(uncert == "lo") rate -= fr.elefr.getErrLow(x,y,true);
301     if(uncert == "hi") rate += fr.elefr.getErrHigh(x,y,true);
302 dkralph 1.1 } else assert(0);
303    
304     return rate/(1-rate);
305     }
306     //----------------------------------------------------------------------------------------
307     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str)
308     {
309 dkralph 1.5 map<TString,map<TString,TH1D*>* > hists;
310     map<TString,TH1D*> *h_both = new map<TString,TH1D*>; hists["both"] = h_both;
311     map<TString,TH1D*> *h_pred = new map<TString,TH1D*>; hists["pred"] = h_pred;
312     map<TString,TH1D*> *h_pred_lo = new map<TString,TH1D*>; hists["pred_lo"] = h_pred_lo;
313     map<TString,TH1D*> *h_pred_hi = new map<TString,TH1D*>; hists["pred_hi"] = h_pred_hi;
314     map<TString,TH1D*> *h_obs = new map<TString,TH1D*>; hists["obs"] = h_obs;
315 dkralph 1.1 map<TString,map<TString,TH1D*>* >::iterator it_h;
316     double w_pt_max,l2_pt_max,mass_max;
317     if(ctrl.faketype=="mu") {
318     w_pt_max = 80;
319 dkralph 1.5 l2_pt_max = 35;
320 dkralph 1.1 mass_max = 120;
321     } else if(ctrl.faketype=="ele"){
322     w_pt_max = 90;
323     l2_pt_max = 60;
324     mass_max = 210;
325     } else assert(0);
326    
327     for(it_h=hists.begin(); it_h!=hists.end(); it_h++) {
328 dkralph 1.3 (*((*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();
329     (*((*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();
330     (*((*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();
331     (*((*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();
332 dkralph 1.1 (*((*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();
333     (*((*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();
334 dkralph 1.3 (*((*it_h).second))["mass"] = new TH1D(TString("mass") +"_"+(*it_h).first+str,";#bf{m_{ll}};", 25,0,mass_max); (*((*it_h).second))["mass"]->Sumw2();
335     (*((*it_h).second))["met"] = new TH1D(TString("met") +"_"+(*it_h).first+str,";#bf{MET};", 25,0,100); (*((*it_h).second))["met"]->Sumw2();
336     (*((*it_h).second))["mt"] = new TH1D(TString("mt") +"_"+(*it_h).first+str,";#bf{m_{T}};", 25,2,150); (*((*it_h).second))["mt"]->Sumw2();
337     (*((*it_h).second))["fr"] = new TH1D(TString("fr") +"_"+(*it_h).first+str,";#bf{FR};", 125,-.01,.1); (*((*it_h).second))["fr"]->Sumw2();
338 dkralph 1.1 }
339    
340     return hists;
341     }
342     //--------------------------------------------------------------------------------------------------
343     void makeHTML(FOFlags &ctrl)
344     {
345     // TString title(ctrl.inputdir);
346     // title = title(title.Last('/')+1,title.Length());
347     TString title("EMU closure test: Fake "+ctrl.faketype);
348     ofstream htmlfile;
349     char htmlfname[100];
350     sprintf(htmlfname,"%s/"+ctrl.faketype+".html",ctrl.outdir.Data());
351     htmlfile.open(htmlfname);
352    
353     htmlfile << "<!DOCTYPE html" << endl;
354     htmlfile << " PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" << endl;
355     htmlfile << "<html>" << endl;
356    
357     htmlfile << "<head><title>"+title+"</title></head>" << endl;
358     htmlfile << "<body bgcolor=\"EEEEEE\">" << endl;
359     htmlfile << "<h3 style=\"text-align:left; color:DD6600;\">"+title+"</h3>" << endl;
360    
361     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
362     htmlfile << "<tr>" << endl;
363 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;
364     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;
365 dkralph 1.1 htmlfile << "</tr>" << endl;
366     htmlfile << "</table>" << endl;
367    
368     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
369     htmlfile << "<tr>" << endl;
370 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;
371     htmlfile << "<td width=\"33.3%\"><a></a></td>" << endl;
372     htmlfile << "<td width=\"33.3%\"><a></a></td>" << endl;
373     htmlfile << "</tr>" << endl;
374     htmlfile << "</table>" << endl;
375    
376     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
377     htmlfile << "<tr>" << endl;
378 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;
379     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;
380     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;
381     htmlfile << "</tr>" << endl;
382     htmlfile << "</table>" << endl;
383    
384     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
385     htmlfile << "<tr>" << endl;
386     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/w_pt.png\"><img src=\"plots/w_pt.png\" alt=\"plots/w_pt.png\" width=\"100%\"></a></td>" << endl;
387     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/w_eta.png\"><img src=\"plots/w_eta.png\" alt=\"plots/w_eta.png\" width=\"100%\"></a></td>" << endl;
388     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/w_isoPF04.png\"><img src=\"plots/w_isoPF04.png\" alt=\"plots/w_isoPF04.png\" width=\"100%\"></a></td>" << endl;
389     htmlfile << "</tr>" << endl;
390     htmlfile << "</table>" << endl;
391    
392     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
393     htmlfile << "<tr>" << endl;
394     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/l2_pt.png\"><img src=\"plots/l2_pt.png\" alt=\"plots/l2_pt.png\" width=\"100%\"></a></td>" << endl;
395     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/l2_eta.png\"><img src=\"plots/l2_eta.png\" alt=\"plots/l2_eta.png\" width=\"100%\"></a></td>" << endl;
396     htmlfile << "<td width=\"33.3%\"><a target=\"_blank\" href=\"plots/l2_isoPF04.png\"><img src=\"plots/l2_isoPF04.png\" alt=\"plots/l2_isoPF04.png\" width=\"100%\"></a></td>" << endl;
397     htmlfile << "</tr>" << endl;
398     htmlfile << "</table>" << endl;
399     htmlfile << "<hr />" << endl;
400    
401     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
402     htmlfile << "<tr>" << endl;
403     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;
404     htmlfile << "</tr>" << endl;
405     htmlfile << "</table>" << endl;
406    
407     htmlfile << "<hr />" << endl;
408    
409     htmlfile << "<hr />" << endl;
410    
411     htmlfile << "</body>" << endl;
412     htmlfile << "</html>" << endl;
413     htmlfile.close();
414     }
415     //----------------------------------------------------------------------------------------
416     TVector3 get_corrected_met(double met, double metphi, TLorentzVector lepton)
417     {
418     TLorentzVector corr_met;
419     corr_met.SetPtEtaPhiM(met,0,metphi,0);
420     TVector3 w_lep_trans(lepton.Px(),lepton.Py(),0);
421     return corr_met.Vect() + w_lep_trans;
422     }
423     //----------------------------------------------------------------------------------------
424     TString entries_str(TH1D *hist)
425     {
426     int entries = hist->GetEntries();
427     stringstream ss;
428     ss << entries;
429     TString str;
430     ss >> str;
431     return str;
432     }
433     //----------------------------------------------------------------------------------------
434     TString integral_str(TH1D *hist)
435     {
436     stringstream ss;
437 dkralph 1.2 ss << fixed << setprecision(1) << integrateHist(hist);
438 dkralph 1.1 TString str;
439     ss >> str;
440     return str;
441     }
442     //----------------------------------------------------------------------------------------
443 dkralph 1.2 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hdata, double &total_mc_scale)
444 dkralph 1.1 {
445 dkralph 1.2 double original_w_integral = integrateHist(hmcW);
446 dkralph 1.1
447 dkralph 1.2 RooRealVar mt("mt","MT [GeV]",hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
448     RooBinning mtbins(hdata->GetNbinsX(),hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
449     mt.setBinning(mtbins);
450 dkralph 1.1
451     // data
452 dkralph 1.2 RooDataHist obsMt("obsMt","obsMt",RooArgList(mt),hdata);
453 dkralph 1.1 // W mc pdf
454     hmcW->Smooth(2);
455 dkralph 1.2 RooDataHist hW("hW","hW",RooArgList(mt),hmcW);
456     RooHistPdf pW("pW","pW",RooArgSet(mt),hW,3);
457     // Z mc pdf
458     RooDataHist hZ("hZ","hZ",RooArgList(mt),hmcZ);
459     RooHistPdf pZ("pZ","pZ",RooArgSet(mt),hZ,3);
460 dkralph 1.1 // rayleigh pdf
461 dkralph 1.2 RooRealVar sig("sig","sig",400,15,585);
462     RooRealVar a1("a1","a1",-3,-10,10);
463     // RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(2*sig*sig))",RooArgList(mt,sig));
464     RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(sig*sig +2*sig*a1*(mt) + a1*a1*(mt)*(mt)))",RooArgList(mt,sig,a1));
465    
466     RooRealVar nObs("nObs","nObs",integrateHist(hdata));
467     RooRealVar nWandZ("nWandZ","nWandZ",0.98*nObs.getVal(),.95*nObs.getVal(),1.1*nObs.getVal());
468     RooRealVar zFrac("zFrac","zFrac",integrateHist(hmcZ) / (integrateHist(hmcZ) + integrateHist(hmcW)));
469     RooAddPdf modelWandZ("modelWandZ","modelWandZ",RooArgList(pZ,pW),zFrac);
470     RooRealVar nRayl("nRayl","nRayl",0.05*nObs.getVal(),0.0*nObs.getVal(),.05*nObs.getVal());
471    
472     RooExtendPdf extWandZ("extWandZ","extWandZ",modelWandZ,nWandZ);
473     RooExtendPdf extRayl("extRayl","extRayl",rayl,nRayl);
474    
475     RooAddPdf *model = new RooAddPdf("model","model",RooArgList(extWandZ,extRayl));
476    
477     RooFitResult *fitres = model->fitTo(obsMt,Extended(kTRUE),Save(kTRUE));
478     fitres->Print();
479    
480     TH1D *rayl_hist = (TH1D*)rayl.createHistogram("rayl_mt",mt,Binning(mtbins));
481     rayl_hist->Scale(nRayl.getVal()/integrateHist(rayl_hist));
482     TH1D *totalHist = (TH1D*)model->createHistogram("total_mt",mt,Binning(mtbins));
483     totalHist->Scale( (nWandZ.getVal() + nRayl.getVal()) / integrateHist(totalHist) );
484    
485     // // why the fuck do I need this?
486     // double tot = nWandZ.getVal() + nRayl.getVal();
487     // if(use_exp) tot += nExp.getVal();
488     // double hack_fac = integrateHist(hdata)/tot;
489     // rayl_hist->Scale(hack_fac);
490     // exp_hist->Scale(hack_fac);
491     // hmcZ->Scale(hack_fac);
492     // hmcW->Scale(hack_fac);
493    
494     // RooPlot *frame = mt.frame();
495     // obsMt.plotOn(frame);
496     // model->plotOn(frame);
497     // model->plotOn(frame,Components("pW"),LineStyle(kDashed),LineColor(kRed));
498     // model->plotOn(frame,Components("pZ"),LineColor(kRed));
499     // model->plotOn(frame,Components("rayl"),LineColor(kGreen));
500     // frame->Draw();
501     // can->SaveAs("~/public_html/foo.png");
502    
503     hmcW->Scale( (nWandZ.getVal()*(1-zFrac.getVal())) / integrateHist(hmcW) );
504     hmcZ->Scale( (nWandZ.getVal()*zFrac.getVal()) / integrateHist(hmcZ) );
505 dkralph 1.1
506 dkralph 1.2 CPlot plotMtFit("mtfitW","","MT [GeV]","Events",ctrl.outdir+"/plots");
507     plotMtFit.AddHist1D(hdata,"tight+fk'ble: " + integral_str(hdata),"E");
508     plotMtFit.AddHist1D(totalHist,"total MC: " + integral_str(totalHist),"hist",kRed);
509     plotMtFit.AddToStack(rayl_hist,"jet (Rayleigh): " + integral_str(rayl_hist),kBlue);
510     plotMtFit.AddToStack(hmcZ,"Z+j MC: " + integral_str(hmcZ),kGray);
511     plotMtFit.AddToStack(hmcW,"W+j MC: " + integral_str(hmcW),kCyan-6);
512     plotMtFit.Draw(can,kTRUE,"png");
513 dkralph 1.1
514 dkralph 1.2 total_mc_scale = integrateHist(hmcW) / original_w_integral;
515 dkralph 1.1
516     return fitres;
517     }