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