ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/plot_emu_fakes.cc
Revision: 1.2
Committed: Wed Jun 13 14:11:22 2012 UTC (12 years, 11 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.1: +79 -82 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    
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.2
178     // if(mass < 1) {
179     // cout << "low mass: " << mass << endl
180     // << "\tW lepton: " << w_lep.type << "\t" << w_lep
181    
182 dkralph 1.1
183     if(types[iextra] == "fail") {
184    
185     // fill fake rate prediction
186    
187     double fake_wgt = get_fake_weight(ex_lep,fr);
188    
189     npreds[cs->name] += fake_wgt*wgt;
190     (*(cs->hists)["pred"])["w_pt"]->Fill( w_lep.vec.Pt(), fake_wgt*wgt);
191     (*(cs->hists)["pred"])["l2_pt"]->Fill( ex_lep.vec.Pt(), fake_wgt*wgt);
192     (*(cs->hists)["pred"])["w_eta"]->Fill( w_lep.vec.Eta(), fake_wgt*wgt);
193     (*(cs->hists)["pred"])["l2_eta"]->Fill( ex_lep.vec.Eta(), fake_wgt*wgt);
194     (*(cs->hists)["pred"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), fake_wgt*wgt);
195     (*(cs->hists)["pred"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), fake_wgt*wgt);
196     (*(cs->hists)["pred"])["mass"]->Fill( mass, fake_wgt*wgt);
197     (*(cs->hists)["pred"])["met"]->Fill( fs->info->met, fake_wgt*wgt);
198     (*(cs->hists)["pred"])["mt"]->Fill( mt, fake_wgt*wgt);
199     } else if(types[iextra] == "pass") {
200    
201     // fill the observation
202    
203     nobs[cs->name] += 1;
204     (*(cs->hists)["obs"])["w_pt"]->Fill( w_lep.vec.Pt(), wgt);
205     (*(cs->hists)["obs"])["l2_pt"]->Fill( ex_lep.vec.Pt(), wgt);
206     (*(cs->hists)["obs"])["w_eta"]->Fill( w_lep.vec.Eta(), wgt);
207     (*(cs->hists)["obs"])["l2_eta"]->Fill( ex_lep.vec.Eta(), wgt);
208     (*(cs->hists)["obs"])["w_isoPF04"]->Fill( w_lep.isoPF04/w_lep.vec.Pt(), wgt);
209     (*(cs->hists)["obs"])["l2_isoPF04"]->Fill( ex_lep.isoPF04/ex_lep.vec.Pt(), wgt);
210     (*(cs->hists)["obs"])["mass"]->Fill( mass, wgt);
211     (*(cs->hists)["obs"])["met"]->Fill( fs->info->met, wgt);
212     (*(cs->hists)["obs"])["mt"]->Fill( mt, wgt);
213     } else { cout << "ERROR: types: " << types[iextra] << endl; assert(0); }
214     }
215     }
216     }
217     }
218     }
219    
220     assert(samplev.size()==3);
221     CSample *cs = samplev[0];
222     CSample *cs_mc = samplev[1];
223     CSample *cs_zmc = samplev[2];
224    
225     double total_mc_scale=-1;
226 dkralph 1.2 RooFitResult *fitres = fitMt(ctrl,(*(cs_zmc->hists)["both"])["mt"],(*(cs_mc->hists)["both"])["mt"],(*(cs->hists)["both"])["mt"],total_mc_scale);
227 dkralph 1.1
228     // RooArgList float_pars = fitres->floatParsFinal();
229     // RooRealVar *nWandZ = (RooRealVar*)float_pars.at(float_pars.index("nWandZ"));
230     // cout << nWandZ->getVal() << endl;
231    
232     map<TString,TH1D*>::iterator it_v;
233     for(it_v=(*(cs->hists)["pred"]).begin(); it_v!=(*(cs->hists)["pred"]).end(); it_v++) {
234     TString var((*it_v).first);
235    
236     // plot passing and failing fakes
237 dkralph 1.2 if(var!="mt") {
238 dkralph 1.1 (*(cs_mc->hists)["both"])[var]->Scale(total_mc_scale);
239     (*(cs_zmc->hists)["both"])[var]->Scale(total_mc_scale);
240     }
241     CPlot cplot_both("both_"+var,"",(*(cs->hists)["both"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
242     cplot_both.AddHist1D((*(cs->hists)["both"])[var],"tight+fk'ble: "+integral_str((*(cs->hists)["both"])[var]),"E");
243     cplot_both.AddToStack((*(cs_zmc->hists)["both"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["both"])[var]),920);
244     cplot_both.AddToStack((*(cs_mc->hists)["both"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["both"])[var]),426);
245    
246     if(var.Contains("eta")) cplot_both.SetYRange(0,1.4*(*(cs->hists)["both"])[var]->GetMaximum());
247     if(var.Contains("isoPF")) cplot_both.SetLogy();
248 dkralph 1.2 if(var=="met") cplot_both.AddLine(25,0,25,18050,843,kDashed);
249 dkralph 1.1 cplot_both.Draw(can,true,"png");
250    
251     // plot observation vs. prediction
252     (*(cs_mc->hists)["obs"])[var]->Scale(total_mc_scale);
253     (*(cs_zmc->hists)["obs"])[var]->Scale(total_mc_scale);
254     CPlot cplot(var,"",(*(cs->hists)["pred"])[var]->GetXaxis()->GetTitle(),"events",ctrl.outdir+"/plots");
255     cplot.AddHist1D((*(cs->hists)["obs"])[var],"Obs: "+integral_str((*(cs->hists)["obs"])[var]),"E");
256     cplot.AddHist1D((*(cs->hists)["pred"])[var],"FR predic: "+integral_str((*(cs->hists)["pred"])[var]),"hist",kRed);
257    
258     cplot.AddToStack((*(cs_zmc->hists)["obs"])[var],"Z+j MC: "+integral_str((*(cs_zmc->hists)["obs"])[var]),920);
259     cplot.AddToStack((*(cs_mc->hists)["obs"])[var],"W+j MC: "+integral_str((*(cs_mc->hists)["obs"])[var]),426);
260    
261     if(var.Contains("eta")) cplot.SetYRange(0,1.4*max((*(cs->hists)["obs"])[var]->GetMaximum(),(*(cs->hists)["pred"])[var]->GetMaximum()));
262     if(var.Contains("isoPF")) cplot.SetLogy();
263     cplot.Draw(can,true,"png");
264     }
265    
266     makeHTML(ctrl);
267     }
268     //----------------------------------------------------------------------------------------
269     double get_fake_weight(SimpleLepton fake_lep, FR_struct fr)
270     {
271     double rate=0;
272     if(abs(fake_lep.type) == 13) { // fake muons:
273     double ptval = min(double(fake_lep.vec.Pt()),FAKE_MU_PT_MAX - 0.1);
274     double etaval = min(double(fabs(fake_lep.vec.Eta())),FAKE_MU_ETA_MAX - 0.1);
275     rate = fr.mufr.getEff(etaval,ptval);
276     } else if(abs(fake_lep.type) == 11) {
277     double ptval = min(double(fake_lep.vec.Pt()),FAKE_ELE_PT_MAX - 0.1);
278     double etaval = min(double(fabs(fake_lep.vec.Eta())),FAKE_ELE_ETA_MAX - 0.1);
279     rate = fr.elefr.getEff(etaval,ptval);
280     } else assert(0);
281    
282     return rate/(1-rate);
283     }
284     //----------------------------------------------------------------------------------------
285     map<TString,map<TString,TH1D*>* > init_hists(FOFlags &ctrl, TString str)
286     {
287     map<TString,TH1D*> *h_both = new map<TString,TH1D*>;
288     map<TString,TH1D*> *h_pred = new map<TString,TH1D*>;
289     map<TString,TH1D*> *h_obs = new map<TString,TH1D*> ;
290     map<TString,map<TString,TH1D*>* > hists; hists["both"] = h_both; hists["pred"] = h_pred, hists["obs"] = h_obs;
291     map<TString,map<TString,TH1D*>* >::iterator it_h;
292     double w_pt_max,l2_pt_max,mass_max;
293     if(ctrl.faketype=="mu") {
294     w_pt_max = 80;
295     l2_pt_max = 60;
296     mass_max = 120;
297     } else if(ctrl.faketype=="ele"){
298     w_pt_max = 90;
299     l2_pt_max = 60;
300     mass_max = 210;
301     } else assert(0);
302    
303     for(it_h=hists.begin(); it_h!=hists.end(); it_h++) {
304     (*((*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();
305     (*((*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();
306     (*((*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();
307     (*((*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();
308     (*((*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();
309     (*((*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();
310     (*((*it_h).second))["mass"] = new TH1D(TString("mass") +"_"+(*it_h).first+str,";#bf{m_{ll}};", 25,0,mass_max); (*((*it_h).second))["mass" ]->Sumw2();
311     (*((*it_h).second))["met"] = new TH1D(TString("met") +"_"+(*it_h).first+str,";#bf{MET};", 25,0,100); (*((*it_h).second))["met" ]->Sumw2();
312     (*((*it_h).second))["mt"] = new TH1D(TString("mt") +"_"+(*it_h).first+str,";#bf{m_{T}};", 25,2,150); (*((*it_h).second))["mt" ]->Sumw2();
313     }
314    
315     return hists;
316     }
317     //--------------------------------------------------------------------------------------------------
318     void makeHTML(FOFlags &ctrl)
319     {
320     // TString title(ctrl.inputdir);
321     // title = title(title.Last('/')+1,title.Length());
322     TString title("EMU closure test: Fake "+ctrl.faketype);
323     ofstream htmlfile;
324     char htmlfname[100];
325     sprintf(htmlfname,"%s/"+ctrl.faketype+".html",ctrl.outdir.Data());
326     htmlfile.open(htmlfname);
327    
328     htmlfile << "<!DOCTYPE html" << endl;
329     htmlfile << " PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" << endl;
330     htmlfile << "<html>" << endl;
331    
332     htmlfile << "<head><title>"+title+"</title></head>" << endl;
333     htmlfile << "<body bgcolor=\"EEEEEE\">" << endl;
334     htmlfile << "<h3 style=\"text-align:left; color:DD6600;\">"+title+"</h3>" << endl;
335    
336     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
337     htmlfile << "<tr>" << endl;
338     // 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;
339 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;
340     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;
341 dkralph 1.1 htmlfile << "</tr>" << endl;
342     htmlfile << "</table>" << endl;
343    
344     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
345     htmlfile << "<tr>" << endl;
346     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;
347     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;
348     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;
349     htmlfile << "</tr>" << endl;
350     htmlfile << "</table>" << endl;
351    
352     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
353     htmlfile << "<tr>" << endl;
354     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;
355     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;
356     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;
357     htmlfile << "</tr>" << endl;
358     htmlfile << "</table>" << endl;
359    
360     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
361     htmlfile << "<tr>" << endl;
362     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;
363     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;
364     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;
365     htmlfile << "</tr>" << endl;
366     htmlfile << "</table>" << endl;
367     htmlfile << "<hr />" << endl;
368    
369     htmlfile << "<table border=\"0\" cellspacing=\"5\" width=\"100%\">" << endl;
370     htmlfile << "<tr>" << endl;
371     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;
372     htmlfile << "</tr>" << endl;
373     htmlfile << "</table>" << endl;
374    
375     htmlfile << "<hr />" << endl;
376    
377     htmlfile << "<hr />" << endl;
378    
379     htmlfile << "</body>" << endl;
380     htmlfile << "</html>" << endl;
381     htmlfile.close();
382     }
383     //----------------------------------------------------------------------------------------
384     TVector3 get_corrected_met(double met, double metphi, TLorentzVector lepton)
385     {
386     TLorentzVector corr_met;
387     corr_met.SetPtEtaPhiM(met,0,metphi,0);
388     TVector3 w_lep_trans(lepton.Px(),lepton.Py(),0);
389     return corr_met.Vect() + w_lep_trans;
390     }
391     //----------------------------------------------------------------------------------------
392     TString entries_str(TH1D *hist)
393     {
394     int entries = hist->GetEntries();
395     stringstream ss;
396     ss << entries;
397     TString str;
398     ss >> str;
399     return str;
400     }
401     //----------------------------------------------------------------------------------------
402     TString integral_str(TH1D *hist)
403     {
404     stringstream ss;
405 dkralph 1.2 ss << fixed << setprecision(1) << integrateHist(hist);
406 dkralph 1.1 TString str;
407     ss >> str;
408     return str;
409     }
410     //----------------------------------------------------------------------------------------
411 dkralph 1.2 RooFitResult *fitMt(FOFlags &ctrl, TH1D *hmcZ, TH1D *hmcW, TH1D *hdata, double &total_mc_scale)
412 dkralph 1.1 {
413 dkralph 1.2 double original_w_integral = integrateHist(hmcW);
414 dkralph 1.1
415 dkralph 1.2 RooRealVar mt("mt","MT [GeV]",hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
416     RooBinning mtbins(hdata->GetNbinsX(),hdata->GetXaxis()->GetXmin(),hdata->GetXaxis()->GetXmax());
417     mt.setBinning(mtbins);
418 dkralph 1.1
419     // data
420 dkralph 1.2 RooDataHist obsMt("obsMt","obsMt",RooArgList(mt),hdata);
421 dkralph 1.1 // W mc pdf
422     hmcW->Smooth(2);
423 dkralph 1.2 RooDataHist hW("hW","hW",RooArgList(mt),hmcW);
424     RooHistPdf pW("pW","pW",RooArgSet(mt),hW,3);
425     // Z mc pdf
426     RooDataHist hZ("hZ","hZ",RooArgList(mt),hmcZ);
427     RooHistPdf pZ("pZ","pZ",RooArgSet(mt),hZ,3);
428 dkralph 1.1 // rayleigh pdf
429 dkralph 1.2 RooRealVar sig("sig","sig",400,15,585);
430     RooRealVar a1("a1","a1",-3,-10,10);
431     // RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(2*sig*sig))",RooArgList(mt,sig));
432     RooGenericPdf rayl("rayl","rayl","mt*exp(-(mt)*(mt)/(sig*sig +2*sig*a1*(mt) + a1*a1*(mt)*(mt)))",RooArgList(mt,sig,a1));
433    
434     RooRealVar nObs("nObs","nObs",integrateHist(hdata));
435     RooRealVar nWandZ("nWandZ","nWandZ",0.98*nObs.getVal(),.95*nObs.getVal(),1.1*nObs.getVal());
436     RooRealVar zFrac("zFrac","zFrac",integrateHist(hmcZ) / (integrateHist(hmcZ) + integrateHist(hmcW)));
437     RooAddPdf modelWandZ("modelWandZ","modelWandZ",RooArgList(pZ,pW),zFrac);
438     RooRealVar nRayl("nRayl","nRayl",0.05*nObs.getVal(),0.0*nObs.getVal(),.05*nObs.getVal());
439    
440     RooExtendPdf extWandZ("extWandZ","extWandZ",modelWandZ,nWandZ);
441     RooExtendPdf extRayl("extRayl","extRayl",rayl,nRayl);
442    
443     RooAddPdf *model = new RooAddPdf("model","model",RooArgList(extWandZ,extRayl));
444    
445     RooFitResult *fitres = model->fitTo(obsMt,Extended(kTRUE),Save(kTRUE));
446     fitres->Print();
447    
448     TH1D *rayl_hist = (TH1D*)rayl.createHistogram("rayl_mt",mt,Binning(mtbins));
449     rayl_hist->Scale(nRayl.getVal()/integrateHist(rayl_hist));
450     TH1D *totalHist = (TH1D*)model->createHistogram("total_mt",mt,Binning(mtbins));
451     totalHist->Scale( (nWandZ.getVal() + nRayl.getVal()) / integrateHist(totalHist) );
452    
453     // // why the fuck do I need this?
454     // double tot = nWandZ.getVal() + nRayl.getVal();
455     // if(use_exp) tot += nExp.getVal();
456     // double hack_fac = integrateHist(hdata)/tot;
457     // rayl_hist->Scale(hack_fac);
458     // exp_hist->Scale(hack_fac);
459     // hmcZ->Scale(hack_fac);
460     // hmcW->Scale(hack_fac);
461    
462     // RooPlot *frame = mt.frame();
463     // obsMt.plotOn(frame);
464     // model->plotOn(frame);
465     // model->plotOn(frame,Components("pW"),LineStyle(kDashed),LineColor(kRed));
466     // model->plotOn(frame,Components("pZ"),LineColor(kRed));
467     // model->plotOn(frame,Components("rayl"),LineColor(kGreen));
468     // frame->Draw();
469     // can->SaveAs("~/public_html/foo.png");
470    
471     hmcW->Scale( (nWandZ.getVal()*(1-zFrac.getVal())) / integrateHist(hmcW) );
472     hmcZ->Scale( (nWandZ.getVal()*zFrac.getVal()) / integrateHist(hmcZ) );
473 dkralph 1.1
474 dkralph 1.2 CPlot plotMtFit("mtfitW","","MT [GeV]","Events",ctrl.outdir+"/plots");
475     plotMtFit.AddHist1D(hdata,"tight+fk'ble: " + integral_str(hdata),"E");
476     plotMtFit.AddHist1D(totalHist,"total MC: " + integral_str(totalHist),"hist",kRed);
477     plotMtFit.AddToStack(rayl_hist,"jet (Rayleigh): " + integral_str(rayl_hist),kBlue);
478     plotMtFit.AddToStack(hmcZ,"Z+j MC: " + integral_str(hmcZ),kGray);
479     plotMtFit.AddToStack(hmcW,"W+j MC: " + integral_str(hmcW),kCyan-6);
480     plotMtFit.Draw(can,kTRUE,"png");
481 dkralph 1.1
482 dkralph 1.2 total_mc_scale = integrateHist(hmcW) / original_w_integral;
483 dkralph 1.1
484     return fitres;
485     }