ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/plot_emu_fakes.cc
Revision: 1.3
Committed: Thu Jun 14 10:27:47 2012 UTC (12 years, 11 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.2: +45 -36 lines
Log Message:
Move the code to keep eta and pt within the bounds of the fake rate histograms into CEffUser2D.

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    
31     #ifndef CMSSW_BASE
32     #define CMSSW_BASE "../../"
33     #endif
34    
35     using namespace std;
36     using namespace RooFit;
37    
38     TCanvas *can;
39    
40     double get_fake_weight(SimpleLepton fake_lep, FR_struct fr);
41     void makeHTML(FOFlags &ctrl);
42     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str="");
43     TVector3 get_corrected_met(double met, double metphi, TLorentzVector lepton);
44     TString entries_str(TH1D *hist);
45     TString integral_str(TH1D *hist);
46 dkralph 1.2 double integrateHist(TH1D *hist) { return hist->Integral(0,hist->GetNbinsX()+1); }
47 dkralph 1.1
48 dkralph 1.2 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hSSmet, double &total_mc_scale);
49 dkralph 1.1
50     //----------------------------------------------------------------------------------------
51     int main(int argc, char** argv)
52     {
53     double lumi = 1600;
54    
55     FOFlags ctrl;
56     parse_foargs( argc, argv, ctrl );
57    
58     can = new TCanvas("can","can");
59    
60     // FR_struct fr = initFRs(ctrl.mufakedir+"/fr.root",ctrl.elefakedir+"/fr.root");
61     FR_struct fr = initFRs(ctrl.mufakedir,ctrl.elefakedir);
62    
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     map<TString,float> npreds,nobs;
69    
70     ifstream instream;
71     instream.open(ctrl.config); assert(instream.is_open());
72     TString ntupledir;
73     string line;
74     while(getline(instream,line)) {
75     if(line[0]=='#') continue;
76    
77     stringstream ss(line);
78     if(line[0]=='^') {
79     TString dummy;
80     if(TString(line).Contains("^ntupdir")) ss >> dummy >> ntupledir;
81     // if(TString(line).Contains("^skim")) ss >> dummy >> ntupledir;
82     continue;
83     }
84    
85     if(line[0]=='$') {
86     samplev.push_back(new CSample());
87     stringstream ss(line);
88     string chr;
89     TString sname;
90     Int_t color;
91     ss >> chr >> sname >> color;
92     string legend = line.substr(line.find('@')+1);
93     samplev.back()->name = sname;
94     samplev.back()->legend = legend;
95     samplev.back()->color = color;
96     samplev.back()->hists = init_hists(ctrl,sname);
97     continue;
98     }
99    
100     TString dset;
101     bool isdata;
102     double xsec;
103     ss >> dset >> isdata >> xsec;
104     TString fname = ntupledir+"/"+dset+"/merged.root";
105     (samplev.back()->fsv).push_back(new filestuff(dset,fname,dset,isdata));
106     }
107     instream.close();
108    
109    
110     for(unsigned ics=0; ics<samplev.size(); ics++) {
111     CSample *cs = samplev[ics];
112     npreds[cs->name] = float(0);
113     nobs[cs->name] = float(0);
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    
191     double fake_wgt = get_fake_weight(ex_lep,fr);
192    
193     npreds[cs->name] += fake_wgt*wgt;
194 dkralph 1.3 (*(cs->hists)["pred"])["w_pt"]->Fill( w_lep.vec.Pt(), fake_wgt*wgt);
195     (*(cs->hists)["pred"])["l2_pt"]->Fill( ex_lep.vec.Pt(), fake_wgt*wgt);
196     (*(cs->hists)["pred"])["w_eta"]->Fill( w_lep.vec.Eta(), fake_wgt*wgt);
197     (*(cs->hists)["pred"])["l2_eta"]->Fill( ex_lep.vec.Eta(), fake_wgt*wgt);
198     (*(cs->hists)["pred"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), fake_wgt*wgt);
199     (*(cs->hists)["pred"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), fake_wgt*wgt);
200     (*(cs->hists)["pred"])["mass"]->Fill( mass, fake_wgt*wgt);
201     (*(cs->hists)["pred"])["met"]->Fill( fs->info->met, fake_wgt*wgt);
202     (*(cs->hists)["pred"])["mt"]->Fill( mt, fake_wgt*wgt);
203     (*(cs->hists)["pred"])["fr"]->Fill( fake_wgt );
204 dkralph 1.1 } else if(types[iextra] == "pass") {
205    
206     // fill the observation
207    
208     nobs[cs->name] += 1;
209 dkralph 1.3 (*(cs->hists)["obs"])["w_pt"]->Fill( w_lep.vec.Pt(), wgt);
210     (*(cs->hists)["obs"])["l2_pt"]->Fill( ex_lep.vec.Pt(), wgt);
211     (*(cs->hists)["obs"])["w_eta"]->Fill( w_lep.vec.Eta(), wgt);
212     (*(cs->hists)["obs"])["l2_eta"]->Fill( ex_lep.vec.Eta(), wgt);
213     (*(cs->hists)["obs"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), wgt);
214     (*(cs->hists)["obs"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), wgt);
215     (*(cs->hists)["obs"])["mass"]->Fill( mass, wgt);
216     (*(cs->hists)["obs"])["met"]->Fill( fs->info->met, wgt);
217     (*(cs->hists)["obs"])["mt"]->Fill( mt, wgt);
218 dkralph 1.1 } else { cout << "ERROR: types: " << types[iextra] << endl; assert(0); }
219     }
220     }
221     }
222     }
223     }
224    
225     assert(samplev.size()==3);
226     CSample *cs = samplev[0];
227     CSample *cs_mc = samplev[1];
228     CSample *cs_zmc = samplev[2];
229    
230     double total_mc_scale=-1;
231 dkralph 1.2 RooFitResult *fitres = fitMt(ctrl,(*(cs_zmc->hists)["both"])["mt"],(*(cs_mc->hists)["both"])["mt"],(*(cs->hists)["both"])["mt"],total_mc_scale);
232 dkralph 1.1
233     // RooArgList float_pars = fitres->floatParsFinal();
234     // RooRealVar *nWandZ = (RooRealVar*)float_pars.at(float_pars.index("nWandZ"));
235     // cout << nWandZ->getVal() << endl;
236    
237     map<TString,TH1D*>::iterator it_v;
238     for(it_v=(*(cs->hists)["pred"]).begin(); it_v!=(*(cs->hists)["pred"]).end(); it_v++) {
239     TString var((*it_v).first);
240    
241     // plot passing and failing fakes
242 dkralph 1.2 if(var!="mt") {
243 dkralph 1.1 (*(cs_mc->hists)["both"])[var]->Scale(total_mc_scale);
244     (*(cs_zmc->hists)["both"])[var]->Scale(total_mc_scale);
245     }
246     CPlot cplot_both("both_"+var,"",(*(cs->hists)["both"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
247     cplot_both.AddHist1D((*(cs->hists)["both"])[var],"tight+fk'ble: "+integral_str((*(cs->hists)["both"])[var]),"E");
248     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     if(var.Contains("eta")) cplot_both.SetYRange(0,1.4*(*(cs->hists)["both"])[var]->GetMaximum());
252     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    
256     // plot observation vs. prediction
257     (*(cs_mc->hists)["obs"])[var]->Scale(total_mc_scale);
258     (*(cs_zmc->hists)["obs"])[var]->Scale(total_mc_scale);
259     CPlot cplot(var,"",(*(cs->hists)["pred"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
260     cplot.AddHist1D((*(cs->hists)["obs"])[var],"Obs: "+integral_str((*(cs->hists)["obs"])[var]),"E");
261     cplot.AddHist1D((*(cs->hists)["pred"])[var],"FR predic: "+integral_str((*(cs->hists)["pred"])[var]),"hist",kRed);
262    
263     cplot.AddToStack((*(cs_zmc->hists)["obs"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["obs"])[var]),920);
264     cplot.AddToStack((*(cs_mc->hists)["obs"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["obs"])[var]),426);
265    
266     if(var.Contains("eta")) cplot.SetYRange(0,1.4*max((*(cs->hists)["obs"])[var]->GetMaximum(),(*(cs->hists)["pred"])[var]->GetMaximum()));
267     if(var.Contains("isoPF")) cplot.SetLogy();
268     cplot.Draw(can,true,"png");
269     }
270    
271     makeHTML(ctrl);
272     }
273     //----------------------------------------------------------------------------------------
274     double get_fake_weight(SimpleLepton fake_lep, FR_struct fr)
275     {
276     double rate=0;
277     if(abs(fake_lep.type) == 13) { // fake muons:
278 dkralph 1.3 rate = fr.mufr.getEff(fake_lep.vec.Eta(),fake_lep.vec.Pt(),true);
279 dkralph 1.1 } else if(abs(fake_lep.type) == 11) {
280 dkralph 1.3 rate = fr.elefr.getEff(fake_lep.vec.Eta(),fake_lep.vec.Pt(),true);
281 dkralph 1.1 } else assert(0);
282    
283     return rate/(1-rate);
284     }
285     //----------------------------------------------------------------------------------------
286     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str)
287     {
288     map<TString,TH1D*> *h_both = new map<TString,TH1D*>;
289     map<TString,TH1D*> *h_pred = new map<TString,TH1D*>;
290     map<TString,TH1D*> *h_obs = new map<TString,TH1D*> ;
291     map<TString,map<TString,TH1D*>* > hists; hists["both"] = h_both; hists["pred"] = h_pred, hists["obs"] = h_obs;
292     map<TString,map<TString,TH1D*>* >::iterator it_h;
293     double w_pt_max,l2_pt_max,mass_max;
294     if(ctrl.faketype=="mu") {
295     w_pt_max = 80;
296     l2_pt_max = 60;
297     mass_max = 120;
298     } else if(ctrl.faketype=="ele"){
299     w_pt_max = 90;
300     l2_pt_max = 60;
301     mass_max = 210;
302     } else assert(0);
303    
304     for(it_h=hists.begin(); it_h!=hists.end(); it_h++) {
305 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();
306     (*((*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();
307     (*((*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();
308     (*((*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();
309 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();
310     (*((*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();
311 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();
312     (*((*it_h).second))["met"] = new TH1D(TString("met") +"_"+(*it_h).first+str,";#bf{MET};", 25,0,100); (*((*it_h).second))["met"]->Sumw2();
313     (*((*it_h).second))["mt"] = new TH1D(TString("mt") +"_"+(*it_h).first+str,";#bf{m_{T}};", 25,2,150); (*((*it_h).second))["mt"]->Sumw2();
314     (*((*it_h).second))["fr"] = new TH1D(TString("fr") +"_"+(*it_h).first+str,";#bf{FR};", 125,-.01,.1); (*((*it_h).second))["fr"]->Sumw2();
315 dkralph 1.1 }
316    
317     return hists;
318     }
319     //--------------------------------------------------------------------------------------------------
320     void makeHTML(FOFlags &ctrl)
321     {
322     // TString title(ctrl.inputdir);
323     // title = title(title.Last('/')+1,title.Length());
324     TString title("EMU closure test: Fake "+ctrl.faketype);
325     ofstream htmlfile;
326     char htmlfname[100];
327     sprintf(htmlfname,"%s/"+ctrl.faketype+".html",ctrl.outdir.Data());
328     htmlfile.open(htmlfname);
329    
330     htmlfile << "<!DOCTYPE html" << endl;
331     htmlfile << " PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" << endl;
332     htmlfile << "<html>" << endl;
333    
334     htmlfile << "<head><title>"+title+"</title></head>" << endl;
335     htmlfile << "<body bgcolor=\"EEEEEE\">" << endl;
336     htmlfile << "<h3 style=\"text-align:left; color:DD6600;\">"+title+"</h3>" << endl;
337    
338     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
339     htmlfile << "<tr>" << endl;
340 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;
341     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;
342 dkralph 1.1 htmlfile << "</tr>" << endl;
343     htmlfile << "</table>" << endl;
344    
345     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
346     htmlfile << "<tr>" << endl;
347 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;
348     htmlfile << "<td width=\"33.3%\"><a></a></td>" << endl;
349     htmlfile << "<td width=\"33.3%\"><a></a></td>" << endl;
350     htmlfile << "</tr>" << endl;
351     htmlfile << "</table>" << endl;
352    
353     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
354     htmlfile << "<tr>" << endl;
355 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;
356     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;
357     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;
358     htmlfile << "</tr>" << endl;
359     htmlfile << "</table>" << endl;
360    
361     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
362     htmlfile << "<tr>" << endl;
363     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;
364     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;
365     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;
366     htmlfile << "</tr>" << endl;
367     htmlfile << "</table>" << endl;
368    
369     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
370     htmlfile << "<tr>" << endl;
371     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;
372     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;
373     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;
374     htmlfile << "</tr>" << endl;
375     htmlfile << "</table>" << endl;
376     htmlfile << "<hr />" << endl;
377    
378     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
379     htmlfile << "<tr>" << endl;
380     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;
381     htmlfile << "</tr>" << endl;
382     htmlfile << "</table>" << endl;
383    
384     htmlfile << "<hr />" << endl;
385    
386     htmlfile << "<hr />" << endl;
387    
388     htmlfile << "</body>" << endl;
389     htmlfile << "</html>" << endl;
390     htmlfile.close();
391     }
392     //----------------------------------------------------------------------------------------
393     TVector3 get_corrected_met(double met, double metphi, TLorentzVector lepton)
394     {
395     TLorentzVector corr_met;
396     corr_met.SetPtEtaPhiM(met,0,metphi,0);
397     TVector3 w_lep_trans(lepton.Px(),lepton.Py(),0);
398     return corr_met.Vect() + w_lep_trans;
399     }
400     //----------------------------------------------------------------------------------------
401     TString entries_str(TH1D *hist)
402     {
403     int entries = hist->GetEntries();
404     stringstream ss;
405     ss << entries;
406     TString str;
407     ss >> str;
408     return str;
409     }
410     //----------------------------------------------------------------------------------------
411     TString integral_str(TH1D *hist)
412     {
413     stringstream ss;
414 dkralph 1.2 ss << fixed << setprecision(1) << integrateHist(hist);
415 dkralph 1.1 TString str;
416     ss >> str;
417     return str;
418     }
419     //----------------------------------------------------------------------------------------
420 dkralph 1.2 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hdata, double &total_mc_scale)
421 dkralph 1.1 {
422 dkralph 1.2 double original_w_integral = integrateHist(hmcW);
423 dkralph 1.1
424 dkralph 1.2 RooRealVar mt("mt","MT [GeV]",hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
425     RooBinning mtbins(hdata->GetNbinsX(),hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
426     mt.setBinning(mtbins);
427 dkralph 1.1
428     // data
429 dkralph 1.2 RooDataHist obsMt("obsMt","obsMt",RooArgList(mt),hdata);
430 dkralph 1.1 // W mc pdf
431     hmcW->Smooth(2);
432 dkralph 1.2 RooDataHist hW("hW","hW",RooArgList(mt),hmcW);
433     RooHistPdf pW("pW","pW",RooArgSet(mt),hW,3);
434     // Z mc pdf
435     RooDataHist hZ("hZ","hZ",RooArgList(mt),hmcZ);
436     RooHistPdf pZ("pZ","pZ",RooArgSet(mt),hZ,3);
437 dkralph 1.1 // rayleigh pdf
438 dkralph 1.2 RooRealVar sig("sig","sig",400,15,585);
439     RooRealVar a1("a1","a1",-3,-10,10);
440     // RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(2*sig*sig))",RooArgList(mt,sig));
441     RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(sig*sig +2*sig*a1*(mt) + a1*a1*(mt)*(mt)))",RooArgList(mt,sig,a1));
442    
443     RooRealVar nObs("nObs","nObs",integrateHist(hdata));
444     RooRealVar nWandZ("nWandZ","nWandZ",0.98*nObs.getVal(),.95*nObs.getVal(),1.1*nObs.getVal());
445     RooRealVar zFrac("zFrac","zFrac",integrateHist(hmcZ) / (integrateHist(hmcZ) + integrateHist(hmcW)));
446     RooAddPdf modelWandZ("modelWandZ","modelWandZ",RooArgList(pZ,pW),zFrac);
447     RooRealVar nRayl("nRayl","nRayl",0.05*nObs.getVal(),0.0*nObs.getVal(),.05*nObs.getVal());
448    
449     RooExtendPdf extWandZ("extWandZ","extWandZ",modelWandZ,nWandZ);
450     RooExtendPdf extRayl("extRayl","extRayl",rayl,nRayl);
451    
452     RooAddPdf *model = new RooAddPdf("model","model",RooArgList(extWandZ,extRayl));
453    
454     RooFitResult *fitres = model->fitTo(obsMt,Extended(kTRUE),Save(kTRUE));
455     fitres->Print();
456    
457     TH1D *rayl_hist = (TH1D*)rayl.createHistogram("rayl_mt",mt,Binning(mtbins));
458     rayl_hist->Scale(nRayl.getVal()/integrateHist(rayl_hist));
459     TH1D *totalHist = (TH1D*)model->createHistogram("total_mt",mt,Binning(mtbins));
460     totalHist->Scale( (nWandZ.getVal() + nRayl.getVal()) / integrateHist(totalHist) );
461    
462     // // why the fuck do I need this?
463     // double tot = nWandZ.getVal() + nRayl.getVal();
464     // if(use_exp) tot += nExp.getVal();
465     // double hack_fac = integrateHist(hdata)/tot;
466     // rayl_hist->Scale(hack_fac);
467     // exp_hist->Scale(hack_fac);
468     // hmcZ->Scale(hack_fac);
469     // hmcW->Scale(hack_fac);
470    
471     // RooPlot *frame = mt.frame();
472     // obsMt.plotOn(frame);
473     // model->plotOn(frame);
474     // model->plotOn(frame,Components("pW"),LineStyle(kDashed),LineColor(kRed));
475     // model->plotOn(frame,Components("pZ"),LineColor(kRed));
476     // model->plotOn(frame,Components("rayl"),LineColor(kGreen));
477     // frame->Draw();
478     // can->SaveAs("~/public_html/foo.png");
479    
480     hmcW->Scale( (nWandZ.getVal()*(1-zFrac.getVal())) / integrateHist(hmcW) );
481     hmcZ->Scale( (nWandZ.getVal()*zFrac.getVal()) / integrateHist(hmcZ) );
482 dkralph 1.1
483 dkralph 1.2 CPlot plotMtFit("mtfitW","","MT [GeV]","Events",ctrl.outdir+"/plots");
484     plotMtFit.AddHist1D(hdata,"tight+fk'ble: " + integral_str(hdata),"E");
485     plotMtFit.AddHist1D(totalHist,"total MC: " + integral_str(totalHist),"hist",kRed);
486     plotMtFit.AddToStack(rayl_hist,"jet (Rayleigh): " + integral_str(rayl_hist),kBlue);
487     plotMtFit.AddToStack(hmcZ,"Z+j MC: " + integral_str(hmcZ),kGray);
488     plotMtFit.AddToStack(hmcW,"W+j MC: " + integral_str(hmcW),kCyan-6);
489     plotMtFit.Draw(can,kTRUE,"png");
490 dkralph 1.1
491 dkralph 1.2 total_mc_scale = integrateHist(hmcW) / original_w_integral;
492 dkralph 1.1
493     return fitres;
494     }