ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/Plotting/Modules/WZStudy.C
Revision: 1.6
Committed: Mon Dec 3 16:06:21 2012 UTC (12 years, 5 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Changes since 1.5: +11 -8 lines
Log Message:
Updating THStack implementation

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4    
5     #include <TCut.h>
6     #include <TROOT.h>
7     #include <TCanvas.h>
8     #include <TMath.h>
9     #include <TColor.h>
10     #include <TPaveText.h>
11     #include <TRandom.h>
12     #include <TH1.h>
13     #include <TH2.h>
14     #include <TF1.h>
15     #include <TSQLResult.h>
16     #include <TProfile.h>
17     #include <TLegendEntry.h>
18    
19     using namespace std;
20    
21 buchmann 1.4 namespace TriLeptons {
22     float Rsfof_CorrectionFactor=-1;
23     float Rsfof_CorrectionFactor_Error=-1;
24     }
25    
26 buchmann 1.3 string IdentifierForUnderlinedEntry(string Entry) {
27     if(Contains(Entry,"t_bar_t__")) return "ttbar";
28     if(Contains(Entry,"Single_top__")) return "SingleTop";
29     if(Contains(Entry,"W_Jets__")) return "W+Jets";
30     if(Contains(Entry,"WZ__")) return "WZ";
31     if(Contains(Entry,"Z_Jets")) return "Z+Jets";
32     if(Contains(Entry,"DATA")) return "Data";
33     if(Contains(Entry,"2l2q_")) return "ZZ->2l2q";
34     if(Contains(Entry,"4l_")) return "ZZ->4l";
35     return Entry;
36     }
37 buchmann 1.2
38 buchmann 1.3 void WZ_kin_plot(string variable, int nbins, float min, float high, string xlabel, TCut cut, string saveas) {
39 buchmann 1.1 TCanvas *can = new TCanvas("can","can");
40     TPad *pad = new TPad("pad","pad",0,0,1,1);
41     pad->cd();
42    
43 buchmann 1.3 THStack stack = allsamples.DrawStack("stack",variable,nbins,min,high,xlabel, "events", cut,mc, luminosity);
44     TH1F *hdata = allsamples.Draw("hdata",variable,nbins,min,high,xlabel, "events", cut,data, luminosity);
45 buchmann 1.4
46     float StackMax=CollapseStack(stack)->GetMaximum();
47     if(StackMax>hdata->GetMaximum()) hdata->SetMaximum(1.2*StackMax);
48 buchmann 1.3 hdata->Draw("e1");
49 buchmann 1.6 stack.Draw("histo,same");
50 buchmann 1.3 hdata->Draw("e1,same");
51 buchmann 1.4
52 buchmann 1.1 TLegend *kinleg = allsamples.allbglegend();
53     kinleg->Draw();
54     DrawPrelim();
55    
56 buchmann 1.3 float total=0;
57     float wz=0;
58     cout << "Numbers with variable " << variable << " and cut " << cut << ": " << endl;
59     TIter nextSF = TIter(stack.GetHists());
60 buchmann 1.1 TH1F *h;
61 buchmann 1.3 stringstream spurity;
62     int bracecounter=0;
63    
64    
65 buchmann 1.1 while ( h = (TH1F*)nextSF() ) {
66 buchmann 1.3 cout << IdentifierForUnderlinedEntry(h->GetName()) << " : " << h->Integral() << endl;
67     if(IdentifierForUnderlinedEntry(h->GetName())=="WZ") {
68     wz+=h->Integral();
69     total+=h->Integral();
70     } else {
71     if(IdentifierForUnderlinedEntry(h->GetName())!="Data") total+=h->Integral();
72     }
73     bracecounter++;
74     spurity << "#splitline{" << IdentifierForUnderlinedEntry(h->GetName()) << " : " << h->Integral() << "}{";
75 buchmann 1.1 }
76 buchmann 1.3
77 buchmann 1.4 spurity << "#splitline{Purity: " << wz/total << "}{N(events): " << total << "}";
78 buchmann 1.3 for(int i=0;i<bracecounter;i++) spurity << "}";
79    
80     TPaveText *PurityPaveText = new TPaveText(0.60, 0.40,0.89, 0.6,"blNDC");
81     PurityPaveText->SetFillStyle(4000);
82     PurityPaveText->SetBorderSize(0);
83     PurityPaveText->SetFillColor(kWhite);
84     PurityPaveText->SetTextFont(42);
85     PurityPaveText->SetTextSize(0.021);
86     PurityPaveText->AddText(spurity.str().c_str());
87     PurityPaveText->Draw();
88 buchmann 1.1
89    
90 buchmann 1.3 // TText *purity = new TText(0.8,0.6,spurity.str().c_str());
91     // purity->Draw();
92     cout << "DATA : " << hdata->Integral() << endl;
93 buchmann 1.1
94 buchmann 1.3 save_with_ratio(hdata,stack,pad->cd(),"WZ_study/"+saveas);
95     delete hdata;
96     delete can;
97     }
98    
99 buchmann 1.4 void SF_vs_OF_plots(string variable, int nbins, float min, float high, string xlabel, TCut cut, TCut sf, TCut of, int mcordata, string saveas) {
100     TCanvas *can = new TCanvas("can","can");
101     TPad *pad = new TPad("pad","pad",0,0,1,1);
102     pad->cd();
103    
104     TLegend *kinleg = make_legend();
105     if(mcordata==data) kinleg->SetHeader("Data");
106     else kinleg->SetHeader("MC");
107    
108     TH1F *sfdata = allsamples.Draw("sfdata",variable,nbins,min,high,xlabel, "events", cut&&sf,mcordata, luminosity);
109     TH1F *ofdata = allsamples.Draw("ofdata",variable,nbins,min,high,xlabel, "events", cut&&of,mcordata, luminosity);
110    
111     float OFmax=ofdata->GetMaximum();
112     if(OFmax>sfdata->GetMaximum()) sfdata->SetMaximum(1.2*OFmax);
113     ofdata->SetLineColor(kRed);
114     sfdata->Draw("e1");
115     ofdata->Draw("histo,same");
116     sfdata->Draw("e1,same");
117    
118     kinleg->AddEntry(sfdata,"Same flavor","p");
119     kinleg->AddEntry(ofdata,"Opposite flavor","l");
120     kinleg->Draw();
121     if(mcordata==data) DrawPrelim();
122     else DrawMCPrelim();
123    
124     save_with_ratio(sfdata,ofdata,pad->cd(),"WZ_study/SF_vs_OF_plots/"+saveas);
125     delete sfdata;
126     delete ofdata;
127     delete can;
128     }
129    
130 buchmann 1.3 void write_report(TCut BasicCut, TCut cut, ofstream &report) {
131     THStack stack = allsamples.DrawStack("stack","tri_mll",10,0,1000,"m_{ll}", "events", BasicCut&&cut,mc, luminosity);
132     float total=0;
133     float wz=0;
134     TIter nextSF = TIter(stack.GetHists());
135     TH1F *h;
136 buchmann 1.1 while ( h = (TH1F*)nextSF() ) {
137 buchmann 1.3 cout << IdentifierForUnderlinedEntry(h->GetName()) << " : " << h->Integral() << endl;
138     if(IdentifierForUnderlinedEntry(h->GetName())=="WZ") {
139     wz+=h->Integral();
140     total+=h->Integral();
141     } else {
142     if(IdentifierForUnderlinedEntry(h->GetName())!="Data") total+=h->Integral();
143     }
144     report << h->Integral() << ";";
145     }
146     report << wz/total << ";\n";
147     report << flush;
148    
149     }
150    
151 buchmann 1.4 vector<string> producecuts(string variablename, float cut1, float cut2, float cut3=-1, float cut4=-1, float cut5=-1, float cut6=-1, float cut7=-1, float cut8=-1, float cut9=-1, float cut10=-1, float cut11=-1 ) {
152     float allcuts[12] = {cut1,cut2,cut3,cut4,cut5,cut6,cut7,cut8,cut9,cut10,cut11};
153 buchmann 1.3 vector<string> cuts;
154 buchmann 1.4 for(int i = 0; i < 11; ++i ) {
155 buchmann 1.3 if(allcuts[i]<0) continue;
156     stringstream nowcut;
157     nowcut << variablename << allcuts[i];
158     cuts.push_back(nowcut.str());
159     }
160     return cuts;
161     }
162    
163    
164     void OptimizeSelection(TCut BasicCut) {
165     ofstream report;
166     report.open ("report3.txt");
167    
168 buchmann 1.4 vector<string> mt = producecuts("tri_mT>",0,10,15,20,25,30,35,40,45,50);
169 buchmann 1.3 vector<string> et = producecuts("met[4]>",0,10,20,30,40,50);
170 buchmann 1.4 vector<string> mll = producecuts("abs(tri_mll-91.2)<",10000,5,10,15,20);
171     vector<string> mlll = producecuts("abs(tri_mlll-91)>",0,5,10,15,20,25,30,40,50);
172 buchmann 1.3
173     for(int imt=0;imt<mt.size();imt++) {
174     for(int iet=0;iet<et.size();iet++) {
175     for(int imll=0;imll<mll.size();imll++) {
176     for(int imlll=0;imlll<mlll.size();imlll++) {
177     stringstream cut;
178     cut << mt[imt] << " && " << et[iet] << " && " << mll[imll] << " && " << mlll[imlll];
179     report << mt[imt] << ";" << et[iet] << ";" << mll[imll] << ";" << mlll[imlll] <<";";
180     // cout << "Cut is : " << cut.str() << endl;
181     write_report(BasicCut, TCut(cut.str().c_str()),report);
182     }
183     }
184     }
185 buchmann 1.1 }
186 buchmann 1.3 report.close();
187     }
188    
189 buchmann 1.4 void SF_OF_comparison(TCut BasicCut, TCut Selection) {
190     int nbins=38;
191     float mll_low=10;
192     float mll_hi=200;
193    
194     // all MC samples
195     TH1F *sfmc = allsamples.Draw("sfmc" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF,mc , luminosity);
196     TH1F *ofmc = allsamples.Draw("ofmc" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF,mc , luminosity);
197    
198     TCanvas *can = new TCanvas("can","can");
199     TPad *pad = new TPad("pad","pad",0,0,1,1);
200     pad->cd();
201     if(ofmc->GetMaximum()>sfmc->GetMaximum()) sfmc->SetMaximum(1.3*ofmc->GetMaximum());
202     ofmc->SetLineColor(kRed);
203     TLegend *leg = make_legend();
204     leg->AddEntry(sfmc,"same flavor","P");
205     leg->AddEntry(ofmc,"opposite flavor","L");
206     leg->SetHeader("MC:");
207     sfmc->Draw("e1");
208     ofmc->Draw("histo,same");
209     sfmc->Draw("e1,same");
210     leg->Draw();
211     DrawMCPrelim();
212    
213     save_with_ratio(sfmc,ofmc,pad->cd(),"WZ_study/OFSFcomparison_MC");
214    
215     // WZ only
216     TH1F *sfwz = allsamples.Draw("sfwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF,mc , luminosity,allsamples.FindSample("WZJetsTo3"));
217     TH1F *ofwz = allsamples.Draw("ofwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF,mc , luminosity,allsamples.FindSample("WZJetsTo3"));
218    
219     TPad *pad2 = new TPad("pad","pad",0,0,1,1);
220     pad2->cd();
221     if(ofwz->GetMaximum()>sfwz->GetMaximum()) sfwz->SetMaximum(1.3*ofwz->GetMaximum());
222     ofwz->SetLineColor(kRed);
223     sfwz->Draw("e1");
224     ofwz->Draw("histo,same");
225     sfwz->Draw("e1,same");
226     leg->SetHeader("WZ MC:");
227     leg->Draw();
228     DrawMCPrelim();
229    
230     save_with_ratio(sfwz,ofwz,pad2->cd(),"WZ_study/OFSFcomparison_WZ");
231    
232     // data
233     TH1F *sfdata = allsamples.Draw("sfdata","tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF,data, luminosity);
234     TH1F *ofdata = allsamples.Draw("ofdata","tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF,data, luminosity);
235    
236     TPad *pad3 = new TPad("pad","pad",0,0,1,1);
237     pad3->cd();
238     if(ofdata->GetMaximum()>sfdata->GetMaximum()) sfdata->SetMaximum(1.3*ofdata->GetMaximum());
239     ofdata->SetLineColor(kRed);
240     sfdata->Draw("e1");
241     ofdata->Draw("histo,same");
242     sfdata->Draw("e1,same");
243     leg->SetHeader("Data:");
244     leg->Draw();
245     DrawPrelim();
246    
247     save_with_ratio(sfdata,ofdata,pad3->cd(),"WZ_study/OFSFcomparison_data");
248    
249     delete can;
250    
251     dout << "SF events: " << sfdata->Integral() << " (mc : " << sfmc->Integral() << " , wz : " << sfwz->Integral() << " ) WZ purity: " << 100*sfwz->Integral()/sfmc->Integral() << " %" << endl;
252     dout << "OF events: " << ofdata->Integral() << " (mc : " << ofmc->Integral() << " , wz : " << ofwz->Integral() << " ) WZ purity: " << 100*ofwz->Integral()/ofmc->Integral() << " %" << endl;
253    
254     dout << "RATIO : SF / OF = " << sfdata->Integral() / ofdata->Integral() << " +/- " << (sfdata->Integral()/ofdata->Integral()) * TMath::Sqrt(1/sfdata->Integral()+1/ofdata->Integral()) << " (stat) " << endl;
255    
256 buchmann 1.5 TriLeptons::Rsfof_CorrectionFactor=ofwz->Integral() / sfwz->Integral();
257 buchmann 1.6 TriLeptons::Rsfof_CorrectionFactor_Error=(ofwz->Integral()/sfwz->Integral()) * TMath::Sqrt(1/sfwz->GetEntries()+1/ofwz->GetEntries());
258 buchmann 1.4
259     }
260    
261     void DrawMatchGenDistribution(string variable, int nbins, float low, float high, bool uselog, string xlabel, string SaveAs, TCut basecut) {
262     TLegend *leg = make_legend();
263     leg->SetY1(0.7);
264     TCanvas *can = new TCanvas("can","can");
265     TPad *pad = new TPad("pad","pad",0,0,1,1);
266     pad->cd();
267     pad->SetLogy(uselog);
268    
269     TCut IsMatch("(gentri_GoodWMatch&&gentri_GoodZMatch)");
270     TCut NoMatch("!(gentri_GoodWMatch&&gentri_GoodZMatch)");
271    
272     TH1F *goodMatch = allsamples.Draw("goodMatch",variable,nbins,low,high,xlabel,"events",basecut&&IsMatch,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
273     TH1F *noMatch = allsamples.Draw("noMatch", variable,nbins,low,high,xlabel,"events",basecut&&NoMatch,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
274    
275     noMatch->SetLineColor(TColor::GetColor("#FF0000"));//an evil red
276     goodMatch->SetLineColor(TColor::GetColor("#04B404")); // a nice green
277    
278     // noMatch->Scale(1.0/noMatch->Integral());
279     // goodMatch->Scale(1.0/goodMatch->Integral());
280    
281     if(noMatch->GetMaximum()>goodMatch->GetMaximum()) goodMatch->SetMaximum(1.3*noMatch->GetMaximum());
282    
283     goodMatch->Draw("histo");
284     noMatch->Draw("histo,same");
285    
286     leg->AddEntry(goodMatch,"correct match","l");
287     leg->AddEntry(noMatch,"incorrect match","l");
288     leg->Draw();
289    
290     save_with_ratio(goodMatch,noMatch,pad->cd(),"WZ_study/GenMatchComparison/"+SaveAs);
291    
292     delete goodMatch;
293     delete noMatch;
294     delete can;
295     }
296    
297     void DrawMatchRecoDistribution(string variable, int nbins, float low, float high, bool uselog, string xlabel, string SaveAs, TCut basecut) {
298     TLegend *leg = make_legend();
299     leg->SetY1(0.7);
300     TCanvas *can = new TCanvas("can","can");
301     TPad *pad = new TPad("pad","pad",0,0,1,1);
302     pad->cd();
303     pad->SetLogy(uselog);
304    
305     TCut IsMatch("(tri_GoodWMatch&&tri_GoodZMatch)");
306     TCut NoMatch("!(tri_GoodWMatch&&tri_GoodZMatch)");
307    
308     TH1F *goodMatch = allsamples.Draw("goodMatch",variable,nbins,low,high,xlabel,"events",basecut&&IsMatch,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
309     TH1F *noMatch = allsamples.Draw("noMatch", variable,nbins,low,high,xlabel,"events",basecut&&NoMatch,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
310    
311     noMatch->SetLineColor(TColor::GetColor("#FF0000"));//an evil red
312     goodMatch->SetLineColor(TColor::GetColor("#04B404")); // a nice green
313    
314     // noMatch->Scale(1.0/noMatch->Integral());
315     // goodMatch->Scale(1.0/goodMatch->Integral());
316    
317     if(noMatch->GetMaximum()>goodMatch->GetMaximum()) goodMatch->SetMaximum(1.3*noMatch->GetMaximum());
318    
319     goodMatch->Draw("histo");
320     noMatch->Draw("histo,same");
321    
322     leg->AddEntry(goodMatch,"correct match","l");
323     leg->AddEntry(noMatch,"incorrect match","l");
324     leg->Draw();
325     DrawPrelim();
326    
327     save_with_ratio(goodMatch,noMatch,pad->cd(),"WZ_study/RecoMatchComparison/"+SaveAs);
328    
329     delete goodMatch;
330     delete noMatch;
331     delete can;
332     }
333    
334     void GenSFOFCalculator(string title, TCut Cut) {
335     TH1F *histoSF = allsamples.Draw("histoSF","gentri_mll",1,0,1000,"processing","events",Cut&&TCut("abs(gentri_id2)==abs(gentri_id3)"),mc,luminosity,allsamples.FindSample("WZJetsTo3"));
336     TH1F *histoOF = allsamples.Draw("histoOF","gentri_mll",1,0,1000,"processing","events",Cut&&TCut("abs(gentri_id2)!=abs(gentri_id3)"),mc,luminosity,allsamples.FindSample("WZJetsTo3"));
337     cout << title << endl;
338     cout << " " << (const char*) Cut << endl;
339     cout << " SF: \t" << histoSF->Integral() << " +/- " << sqrt(histoSF->GetEntries())*(histoSF->Integral()/histoSF->GetEntries()) << endl;
340     cout << " OF: \t" << histoOF->Integral() << " +/- " << sqrt(histoOF->Integral())*(histoOF->Integral()/histoOF->GetEntries()) << endl;
341     cout << " SF/OF: \t" << histoSF->Integral() / histoOF->Integral() << " +/- " << histoSF->GetEntries() / histoOF->GetEntries() * sqrt(1.0/histoSF->GetEntries() + 1.0 / histoOF->GetEntries()) << endl;
342    
343     delete histoSF;
344     delete histoOF;
345     }
346    
347     void reco_SF_OF_comparison(TCut Base, TCut Selection) {
348    
349     bool dolog=true;
350     bool nolog=false;
351    
352     DrawMatchRecoDistribution("tri_mlll",60,0,300,dolog,"m_{lll}","TrileptonMass",Base);
353     DrawMatchRecoDistribution("tri_mll",60,0,300,dolog,"m_{l_{1}l_{2}}","LeadingDileptonMass",Base);
354     DrawMatchRecoDistribution("tri_mT",50,0,100,nolog,"m_{T}","MT",Base);
355     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass",Base);
356     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass_SF",Base&&TCut("abs(tri_id2)==abs(tri_id3)"));
357     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass_OF",Base&&TCut("abs(tri_id2)!=abs(tri_id3)"));
358     DrawMatchRecoDistribution("genMET",60,0,300,dolog,"gen MET","MET",Base);
359     DrawMatchRecoDistribution("abs(tri_id1)",50,-0.5,49.5,nolog,"id(l_{1})","genID1",Base);
360     DrawMatchRecoDistribution("abs(tri_id2)",50,-0.5,49.5,nolog,"id(l_{2})","genID2",Base);
361     DrawMatchRecoDistribution("abs(tri_id3)",50,-0.5,49.5,nolog,"id(l_{3})","genID3",Base);
362     DrawMatchRecoDistribution("abs(tri_genMID1)",50,-0.5,49.5,nolog,"mother id(l_{1})","genMID1",Base);
363     DrawMatchRecoDistribution("abs(tri_genMID2)",50,-0.5,49.5,nolog,"mother id(l_{2})","genMID2",Base);
364     DrawMatchRecoDistribution("abs(tri_genMID3)",50,-0.5,49.5,nolog,"mother id(l_{3})","genMID3",Base);
365    
366     DrawMatchRecoDistribution("tri_mlll",60,0,300,dolog,"m_{lll}","FullSelection/TrileptonMass",Base&&Selection);
367     DrawMatchRecoDistribution("tri_mll",60,0,300,dolog,"m_{l_{1}l_{2}}","FullSelection/LeadingDileptonMass",Base&&Selection);
368     DrawMatchRecoDistribution("tri_mT",50,0,100,nolog,"m_{T}","FullSelection/MT",Base&&Selection);
369     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass",Base&&Selection);
370     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass_SF",Base&&TCut("abs(tri_id2)==abs(tri_id3)")&&Selection);
371     DrawMatchRecoDistribution("tri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass_OF",Base&&TCut("abs(tri_id2)!=abs(tri_id3)")&&Selection);
372     DrawMatchRecoDistribution("genMET",60,0,300,dolog,"gen MET","FullSelection/MET",Base&&Selection);
373     DrawMatchRecoDistribution("abs(tri_id1)",50,-0.5,49.5,nolog,"id(l_{1})","FullSelection/genID1",Base&&Selection);
374     DrawMatchRecoDistribution("abs(tri_id2)",50,-0.5,49.5,nolog,"id(l_{2})","FullSelection/genID2",Base&&Selection);
375     DrawMatchRecoDistribution("abs(tri_id3)",50,-0.5,49.5,nolog,"id(l_{3})","FullSelection/genID3",Base&&Selection);
376     DrawMatchRecoDistribution("abs(tri_genMID1)",50,-0.5,49.5,nolog,"mother id(l_{1})","FullSelection/genMID1",Base&&Selection);
377     DrawMatchRecoDistribution("abs(tri_genMID2)",50,-0.5,49.5,nolog,"mother id(l_{2})","FullSelection/genMID2",Base&&Selection);
378     DrawMatchRecoDistribution("abs(tri_genMID3)",50,-0.5,49.5,nolog,"mother id(l_{3})","FullSelection/genMID3",Base&&Selection);
379     }
380    
381     void generator_SF_OF_comparison() {
382     /*we want to analyze the cuts at generator level (and their impact), plus understand how bad matching can affect the selection.
383     we want to know the distributions for correctly and incorrectly matched W/Z for the following variables:
384     - M_{lll}
385     - M_{ll}
386     - M_{T}
387     - M_{l_{2}l_{3}}
388     - MET
389     */
390     TCut genBase("genNleptons>=3 && gentri_pt1>30 && gentri_pt2>20 && gentri_pt3>20");
391     TCut genParticleBase("genNleptons>=3 && pgentri_pt1>30 && pgentri_pt2>20 && pgentri_pt3>20");
392    
393     TCut Selection("abs(gentri_mlll-91)>30 && genMET>30 && abs(gentri_mll-91)<10 && gentri_mT > 40");
394     TCut genParticleSelection("abs(pgentri_mlll-91)>30 && genMET>30 && abs(pgentri_mll-91)<10 && abs(pgentri_submll-91)>20 && pgentri_mT > 40");
395    
396     bool dolog=true;
397     bool nolog=false;
398    
399     DrawMatchGenDistribution("gentri_mlll",60,0,300,dolog,"m_{lll}","TrileptonMass",genBase);
400     DrawMatchGenDistribution("gentri_mll",60,0,300,dolog,"m_{l_{1}l_{2}}","LeadingDileptonMass",genBase);
401     DrawMatchGenDistribution("gentri_mT",50,0,100,nolog,"m_{T}","MT",genBase);
402     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass",genBase);
403     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass_SF",genBase&&TCut("abs(gentri_id2)==abs(gentri_id3)"));
404     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","SubleadingDileptonMass_OF",genBase&&TCut("abs(gentri_id2)!=abs(gentri_id3)"));
405     DrawMatchGenDistribution("genMET",60,0,300,dolog,"gen MET","MET",genBase);
406     DrawMatchGenDistribution("abs(gentri_id1)",50,-0.5,49.5,nolog,"id(l_{1})","genID1",genBase);
407     DrawMatchGenDistribution("abs(gentri_id2)",50,-0.5,49.5,nolog,"id(l_{2})","genID2",genBase);
408     DrawMatchGenDistribution("abs(gentri_id3)",50,-0.5,49.5,nolog,"id(l_{3})","genID3",genBase);
409     DrawMatchGenDistribution("abs(gentri_genMID1)",50,-0.5,49.5,nolog,"mother id(l_{1})","genMID1",genBase);
410     DrawMatchGenDistribution("abs(gentri_genMID2)",50,-0.5,49.5,nolog,"mother id(l_{2})","genMID2",genBase);
411     DrawMatchGenDistribution("abs(gentri_genMID3)",50,-0.5,49.5,nolog,"mother id(l_{3})","genMID3",genBase);
412    
413     DrawMatchGenDistribution("gentri_mlll",60,0,300,dolog,"m_{lll}","FullSelection/TrileptonMass",genBase&&Selection);
414     DrawMatchGenDistribution("gentri_mll",60,0,300,dolog,"m_{l_{1}l_{2}}","FullSelection/LeadingDileptonMass",genBase&&Selection);
415     DrawMatchGenDistribution("gentri_mT",50,0,100,nolog,"m_{T}","FullSelection/MT",genBase&&Selection);
416     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass",genBase&&Selection);
417     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass_SF",genBase&&TCut("abs(gentri_id2)==abs(gentri_id3)")&&Selection);
418     DrawMatchGenDistribution("gentri_submll",60,0,300,nolog,"m_{l_{2}l_{3}}","FullSelection/SubleadingDileptonMass_OF",genBase&&TCut("abs(gentri_id2)!=abs(gentri_id3)")&&Selection);
419     DrawMatchGenDistribution("genMET",60,0,300,dolog,"gen MET","FullSelection/MET",genBase&&Selection);
420     DrawMatchGenDistribution("abs(gentri_id1)",50,-0.5,49.5,nolog,"id(l_{1})","FullSelection/genID1",genBase&&Selection);
421     DrawMatchGenDistribution("abs(gentri_id2)",50,-0.5,49.5,nolog,"id(l_{2})","FullSelection/genID2",genBase&&Selection);
422     DrawMatchGenDistribution("abs(gentri_id3)",50,-0.5,49.5,nolog,"id(l_{3})","FullSelection/genID3",genBase&&Selection);
423     DrawMatchGenDistribution("abs(gentri_genMID1)",50,-0.5,49.5,nolog,"mother id(l_{1})","FullSelection/genMID1",genBase&&Selection);
424     DrawMatchGenDistribution("abs(gentri_genMID2)",50,-0.5,49.5,nolog,"mother id(l_{2})","FullSelection/genMID2",genBase&&Selection);
425     DrawMatchGenDistribution("abs(gentri_genMID3)",50,-0.5,49.5,nolog,"mother id(l_{3})","FullSelection/genMID3",genBase&&Selection);
426    
427     return;
428    
429    
430     GenSFOFCalculator("Basic selection",genBase);
431     GenSFOFCalculator("Basic selection, correct Z",genBase&&TCut("gentri_GoodZMatch"));
432     GenSFOFCalculator("Basic selection, wrong Z",genBase&&TCut("!gentri_GoodZMatch"));
433     GenSFOFCalculator("Outside |m_{23}-Z|<20",genBase&&TCut("abs(gentri_submll-91)>20"));
434     GenSFOFCalculator("Outside |m_{23}-Z|<20, Correct Z", genBase&&TCut("abs(gentri_submll-91)>20&&gentri_GoodZMatch"));
435     GenSFOFCalculator("Outside |m_{23}-Z|<20, Wrong Z", genBase&&TCut("abs(gentri_submll-91)>20&&!gentri_GoodZMatch"));
436     GenSFOFCalculator("Outside |m_{23}-Z|<20, Correct W", genBase&&TCut("abs(gentri_submll-91)>20&&gentri_GoodWMatch"));
437     GenSFOFCalculator("Outside |m_{23}-Z|<20, Wrong W", genBase&&TCut("abs(gentri_submll-91)>20&&!gentri_GoodWMatch"));
438     GenSFOFCalculator("Outside |m_{23}-Z|<20, Correct Z, correct W", genBase&&TCut("abs(gentri_submll-91)>20&&gentri_GoodZMatch&&gentri_GoodWMatch"));
439     GenSFOFCalculator("Outside |m_{23}-Z|<20, Wrong Z or W", genBase&&TCut("abs(gentri_submll-91)>20&&!(gentri_GoodZMatch&&gentri_GoodWMatch)"));
440     GenSFOFCalculator("Full selection, Correct Z and W", genBase&&Selection&&TCut("abs(gentri_submll-91)>20&&(gentri_GoodZMatch&&gentri_GoodWMatch)"));
441     GenSFOFCalculator("Full selection, Wrong Z or W", genBase&&Selection&&TCut("abs(gentri_submll-91)>20&&!(gentri_GoodZMatch&&gentri_GoodWMatch)"));
442     // GenSFOFCalculator("Full selection", genBase&&Selection);
443     // GenSFOFCalculator("Full selection with genParticles", genBase&&genParticleSelection&&TCut("abs(gentri_submll-91)>20"));
444    
445    
446     }
447    
448    
449     void AlgorithmTest(TCut BasicCut, TCut Selection) {
450     TCut Pure("tri_GoodWMatch && tri_GoodZMatch");
451    
452     int nbins=38;
453     float mll_low=10;
454     float mll_hi=200;
455    
456     TCanvas *can = new TCanvas("can","can");
457     TH1F *sfwz = allsamples.Draw("sfwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
458     TH1F *ofwz = allsamples.Draw("ofwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
459    
460     TH1F *sfwzpure = allsamples.Draw("sfwzpure","tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF&&Pure,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
461     TH1F *ofwzpure = allsamples.Draw("ofwzpure","tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF&&Pure,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
462    
463     delete can;
464    
465 buchmann 1.6 dout << "SF WZ events: " << sfwz->Integral() << " (correctly combined WZ: " << sfwzpure->Integral() << ")" << endl;
466     dout << "OF WZ events: " << ofwz->Integral() << " (correctly combined WZ: " << ofwzpure->Integral() << ")" << endl;
467 buchmann 1.4
468     dout << "RATIO : SF / OF = " << sfwz->Integral() / ofwz->Integral() << " +/- " << sfwz->Integral()/ofwz->Integral() * sqrt(1/sfwz->GetEntries() + 1/ofwz->GetEntries()) << endl;
469     dout << " PURE: SF / OF = " << sfwzpure->Integral() / ofwzpure->Integral() << " +/- " << sfwzpure->Integral()/ofwzpure->Integral() * sqrt(1/sfwzpure->GetEntries() + 1/ofwzpure->GetEntries()) << endl;
470     dout << "PURITY: " << endl;
471     dout << " SF : " << 100.0*sfwzpure->Integral()/sfwz->Integral() << " +/- " << 100.0*sfwzpure->Integral()/sfwz->Integral() * sqrt(1/sfwzpure->GetEntries() + 1/sfwz->GetEntries()) <<endl;
472     dout << " OF : " << 100.0*ofwzpure->Integral()/ofwz->Integral() << " +/- " << 100.0*ofwzpure->Integral()/ofwz->Integral() * sqrt(1/ofwzpure->GetEntries() + 1/ofwz->GetEntries()) <<endl;
473    
474    
475     delete sfwz;
476     delete ofwz;
477     delete sfwzpure;
478     delete ofwzpure;
479     }
480    
481     void GetResult(TCut BasicCut, TCut Selection) {
482     if(TriLeptons::Rsfof_CorrectionFactor<0 || TriLeptons::Rsfof_CorrectionFactor_Error<0) SF_OF_comparison(BasicCut,Selection);
483     int nbins=38;
484     float mll_low=10;
485     float mll_hi=200;
486    
487     TCanvas *can = new TCanvas("can","can");
488     TH1F *sfwz = allsamples.Draw("sfwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepSF,data,luminosity);
489     TH1F *ofwz = allsamples.Draw("ofwz" ,"tri_submll",nbins,mll_low,mll_hi,"m_{l_{2}l_{3}} [GeV]", "events", BasicCut&&Selection&&TriLepOF,data,luminosity);
490    
491     delete can;
492    
493     dout << " ****** FINAL RESULT ****** " << endl;
494     dout << "SF events: " << sfwz->Integral() << " +/- " << sqrt(sfwz->Integral()) << endl;
495     dout << "OF events: " << ofwz->Integral() << " +/- " << sqrt(ofwz->Integral()) << endl;
496    
497 buchmann 1.6 dout << "Correction factor : " << TriLeptons::Rsfof_CorrectionFactor << " +/- " << TriLeptons::Rsfof_CorrectionFactor_Error << endl;
498    
499 buchmann 1.4 float rawstatuncert=sfwz->Integral()/ofwz->Integral() * sqrt(1/sfwz->GetEntries() + 1/ofwz->GetEntries());
500     float rawresult=sfwz->Integral() / ofwz->Integral();
501     dout << "UNCORRECTED RATIO : SF / OF = " << rawresult << " +/- " << rawstatuncert << endl;
502     dout << "CORRECTED RATIO : SF / OF = " << TriLeptons::Rsfof_CorrectionFactor * rawresult << " +/- " << (TriLeptons::Rsfof_CorrectionFactor * rawresult) * sqrt(rawstatuncert*rawstatuncert/(rawresult*rawresult) + ( TriLeptons::Rsfof_CorrectionFactor_Error * TriLeptons::Rsfof_CorrectionFactor_Error )/(TriLeptons::Rsfof_CorrectionFactor * TriLeptons::Rsfof_CorrectionFactor));
503    
504    
505     delete sfwz;
506     delete ofwz;
507     // delete can;
508     }
509    
510    
511     void PredictionFailurePlot(string truthvariable, string predictionvariable, int nbins, float min, float max, TCut PredictionCut, TCut TruthCut, string saveas, string xlabel) {
512     TLegend *leg = make_legend();
513     leg->SetY1(0.7);
514     TCanvas *can = new TCanvas("can","can");
515     TPad *pad = new TPad("pad","pad",0,0,1,1);
516     pad->cd();
517     // pad->SetLogy(1);
518    
519     TH1F *Prediction = allsamples.Draw("Prediction",predictionvariable,nbins,min,max,xlabel,"events",PredictionCut,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
520     TH1F *MCTruth = allsamples.Draw("MCTruth", truthvariable, nbins,min,max,xlabel,"events",TruthCut ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
521    
522     Prediction->SetLineColor(TColor::GetColor("#FF0000"));//an evil red
523     MCTruth->SetLineColor(TColor::GetColor("#04B404")); // a nice green
524    
525     // if(Prediction->GetMaximum()>MCTruth->GetMaximum()) MCTruth->SetMaximum(1.3*Prediction->GetMaximum());
526    
527     if(MCTruth->GetMaximum()>Prediction->GetMaximum()) {
528     MCTruth->SetMaximum(MCTruth->GetMaximum()*1.2);
529     MCTruth->DrawNormalized("histo");
530     Prediction->DrawNormalized("histo,same");
531     } else {
532     Prediction->SetMaximum(Prediction->GetMaximum()*1.2);
533     Prediction->DrawNormalized("histo");
534     MCTruth->DrawNormalized("histo,same");
535     }
536    
537    
538     leg->AddEntry(Prediction,"prediction (norm.)","l");
539     leg->AddEntry(MCTruth,"MC truth (norm.)","l");
540     leg->Draw();
541     DrawMCPrelim();
542     save_with_ratio(MCTruth,Prediction,pad->cd(),"WZ_study/PredictionMethodFailure/"+saveas);
543    
544     delete Prediction;
545     delete MCTruth;
546     delete can;
547    
548    
549    
550     }
551    
552     void WhyDoesThePredictionMethodFail(TCut PredictionCut, TCut TruthCut) {
553     PredictionFailurePlot("tri_submll","tri_badsubmll",50,0,200,PredictionCut,TruthCut,"SubMll","m_{23}");
554     PredictionFailurePlot("tri_mT","tri_badmT",50,0,200,PredictionCut,TruthCut,"MT","m_{T}");
555     PredictionFailurePlot("tri_mll","tri_badmll",25,0,200,PredictionCut,TruthCut,"Mll","m_{12}");
556     }
557    
558    
559     void ComputeNewMiscombinationRate(TCut origSelection, TCut sBasicCut) {
560     TCanvas *can = new TCanvas("can","can");
561    
562     string sSelection= (const char*)origSelection;
563     sSelection= ReplaceAll(sSelection,"tri_","tri_bad");
564     sSelection= ReplaceAll(sSelection,"tri_badmlll","tri_mlll");
565     cout << "modified " << (const char*)origSelection << " to " << sSelection << endl;
566    
567     TCut FlavorBlindSelection((sSelection+(string)"&&(abs(tri_id2)!=abs(tri_id3))").c_str()); // making sure we select OF events
568     TCut CorrectSelection (origSelection && TCut("(abs(tri_id2)!=abs(tri_id3))")); // making sure we select OF events
569     TCut OFZ ("tri_mll!=tri_badmll");
570    
571     TH1F *WZ_OF_OFZ = allsamples.Draw("WZ_OFZ" ,"tri_badsubmll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", FlavorBlindSelection&&OFZ ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
572     TH1F *WZ_OF_any = allsamples.Draw("WZ_OF_any" ,"tri_badsubmll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", FlavorBlindSelection ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
573    
574 buchmann 1.5 TH1F *data_OF_OFZ = allsamples.Draw("data_OFZ" ,"tri_badsubmll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", FlavorBlindSelection&&OFZ ,data,luminosity);
575     TH1F *data_OF_any = allsamples.Draw("data_OF_any" ,"tri_badsubmll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", FlavorBlindSelection ,data,luminosity);
576    
577 buchmann 1.6 dout << "Miscombination rate in WZ sample with selection in place : " << 100 * WZ_OF_OFZ->Integral() / WZ_OF_any->Integral() << " +/- " << 100*(WZ_OF_OFZ->Integral() / WZ_OF_any->Integral())* sqrt( 1.0/WZ_OF_OFZ->GetEntries() + 1.0/WZ_OF_any->GetEntries())<< " % (" << WZ_OF_OFZ->Integral() << " / " << WZ_OF_any->Integral() << ")" << endl;
578     dout << "Miscombination rate in DATA with selection in place : " << 100 * data_OF_OFZ->Integral() / data_OF_any->Integral() << " +/- " << 100*(data_OF_OFZ->Integral() / data_OF_any->Integral()) * sqrt( 1.0/data_OF_OFZ->Integral() + 1.0/data_OF_any->Integral() ) << " % (" << data_OF_OFZ->Integral() << " / " << data_OF_any->Integral() << ")" << endl;
579 buchmann 1.4
580     delete WZ_OF_any;
581     delete WZ_OF_OFZ;
582     delete can;
583     }
584    
585     void ComputeMiscombinationRate(TCut origSelection, TCut sBasicCut) {
586     /* TCanvas *can = new TCanvas("can","can");
587     //idea: use the flavor-blind version of the algorithm to obtain the yields in our signal region.
588     string sSelection= (const char*)origSelection;
589     sSelection= ReplaceAll(sSelection,"tri_","tri_bad");
590     sSelection= ReplaceAll(sSelection,"tri_badmlll","tri_mlll");
591     cout << "modified " << (const char*)origSelection << " to " << sSelection << endl;
592    
593     TCut BadSelection((sSelection+(string)"&&abs(tri_id2)!=abs(tri_id3)").c_str()); // making sure we select OF events
594     TCut GoodSelection(origSelection && TCut("abs(tri_id2)!=abs(tri_id3)")); // making sure we select OF events
595    
596     TCut BasicCut(sBasicCut&&TCut("tri_id2!=tri_id3"));
597    
598     TCut TruthBad("!(tri_GoodZMatch&&tri_GoodZMatch)");
599     TCut MethodBad("!(tri_id2==tri_badid2&&tri_id3==tri_badid3)");
600    
601     for(int icut=0;icut<2;icut++) {
602     TCut Goodused;
603     TCut Badused;
604     if(icut==0) {
605     Goodused=BasicCut;
606     Badused =BasicCut;
607     cout << "Preselection only: " << endl;
608     cout << " used : " << (const char*) Goodused << endl;
609     } else {
610     Goodused=BasicCut&&GoodSelection;
611     Badused =BasicCut&&BadSelection;
612     cout << "Full selection: " << endl;
613     cout << " Good used : " << (const char*) Goodused << endl;
614     cout << " Bad used : " << (const char*) Goodused << endl;
615     }
616    
617     TH1F *Truth_badcomb = allsamples.Draw("Truth_badcomb" ,"tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Goodused ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
618     TH1F *Truth_all = allsamples.Draw("Truth_all" ,"tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Goodused ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
619    
620     TH1F *Events_WZ = allsamples.Draw("Events_WZ" ,"tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Goodused ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
621     TH1F *bad_WZ = allsamples.Draw("bad_WZ" ,"tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Goodused&&TruthBad ,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
622     TH1F *MethodBad_WZ = allsamples.Draw("MethodBad_WZ" ,"tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Badused&&MethodBad,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
623     TH1F *MethodBad_WZg= allsamples.Draw("MethodBad_WZg","tri_submll",1,0.0,1000.0,"m_{l_{2}l_{3}} [GeV]", "events", Badused,mc,luminosity,allsamples.FindSample("WZJetsTo3"));
624    
625     cout << " ********** WZ **********" << endl;
626     cout << " N events : " << Events_WZ->Integral() << "\t (mc events: " << Events_WZ->GetEntries() << ")" << endl;
627     cout << " Bad combinations: " << bad_WZ->Integral() << "\t (mc events: " << bad_WZ->GetEntries() << ")" << endl;
628     cout << " Predicted bad com.: " << MethodBad_WZ->Integral() << " / " << MethodBad_WZg->Integral() << "\t (mc events: " << MethodBad_WZ->GetEntries() << ")" << endl;
629    
630    
631     delete Events_WZ;
632     delete bad_WZ;
633     delete MethodBad_WZ;
634     delete MethodBad_WZg;
635     }
636     delete can;
637    
638     WhyDoesThePredictionMethodFail(BasicCut&&MethodBad,BasicCut&&TruthBad);
639     */
640     }
641    
642    
643    
644    
645 buchmann 1.3 void CarryOutWZStudy() {
646     TCut BasicCut("leptonNum>2&&tri_pt1>30&&tri_pt2>20&&tri_pt3>20");
647 buchmann 1.4 TCut Selection("abs(tri_mlll-91)>30 && tri_mT>40 && met[4] > 30 && abs(tri_mll-91)<10 && tri_submll > 10 && abs(tri_submll-91)>5");
648 buchmann 1.6 /*
649 buchmann 1.4 //OptimizeSelection(BasicCut);
650    
651     //ComputeMiscombinationRate(Selection, BasicCut);
652    
653     ComputeNewMiscombinationRate(Selection, BasicCut);
654 buchmann 1.3
655     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut,"Inclusive/Zmass");
656     WZ_kin_plot("tri_mlll",40,0,400,"m_{ll} [GeV]",BasicCut,"Inclusive/TrileptonMass");
657     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut,"Inclusive/mT");
658     WZ_kin_plot("met[4]", 40,0,400,"raw PFMET [GeV]",BasicCut,"Inclusive/MET");
659     WZ_kin_plot("tri_pt1",40,0,400,"p_{T}^{1} [GeV]",BasicCut,"Inclusive/Pt1");
660     WZ_kin_plot("tri_pt2",40,0,400,"p_{T}^{2} [GeV]",BasicCut,"Inclusive/Pt2");
661     WZ_kin_plot("tri_pt3",40,0,400,"p_{T}^{3} [GeV]",BasicCut,"Inclusive/Pt3");
662    
663     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("abs(tri_mlll-91)>30"),"Mlll_cut/Zmass");
664     WZ_kin_plot("tri_mlll",40,0,400,"m_{ll} [GeV]",BasicCut&&TCut("abs(tri_mlll-91)>30"),"Mlll_cut/TrileptonMass");
665     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("abs(tri_mlll-91)>30"),"Mlll_cut/mT");
666     WZ_kin_plot("met[4]", 40,0,400,"raw PFMET [GeV]",BasicCut&&TCut("abs(tri_mlll-91)>30"),"Mlll_cut/MET");
667    
668     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("abs(tri_mll-91)<10"),"Zwindow/Zmass");
669     WZ_kin_plot("tri_mlll",40,0,400,"m_{ll} [GeV]",BasicCut&&TCut("abs(tri_mll-91)<10"),"Zwindow/TrileptonMass");
670     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("abs(tri_mll-91)<10"),"Zwindow/mT");
671     WZ_kin_plot("met[4]", 40,0,400,"raw PFMET [GeV]",BasicCut&&TCut("abs(tri_mll-91)<10"),"Zwindow/MET");
672    
673     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut,"CutFlow/Zmass__0_CutOnNothing");
674     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("tri_mT>40"),"CutFlow/Zmass__1_CutOn_MT");
675     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30"),"CutFlow/Zmass__2_CutOn_MT_n_Mll");
676     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30"),"CutFlow/Zmass__3_CutOn_MT_n_Mll_n_Met");
677     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30&&abs(tri_mll-91)<10"),"CutFlow/Zmass__4_CutOn_MT_n_Mll_n_Met_MllWindow");
678    
679     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut,"CutFlow/MT__0_CutOnNothing");
680     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("tri_mT>40"),"CutFlow/MT__1_CutOn_MT");
681     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30"),"CutFlow/MT__2_CutOn_MT_n_Mll");
682     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30"),"CutFlow/MT__3_CutOn_MT_n_Mll_n_Met");
683     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30&&abs(tri_mll-91)<10"),"CutFlow/MT__4_CutOn_MT_n_Mll_n_Met_MllWindow");
684    
685     WZ_kin_plot("met[4]",40,0,200,"MET [GeV]",BasicCut,"CutFlow/MET__0_CutOnNothing");
686     WZ_kin_plot("met[4]",40,0,200,"MET [GeV]",BasicCut&&TCut("tri_mT>40"),"CutFlow/MET__1_CutOn_MT");
687     WZ_kin_plot("met[4]",40,0,200,"MET [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30"),"CutFlow/MET__2_CutOn_MT_n_Mll");
688     WZ_kin_plot("met[4]",40,0,200,"MET [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30"),"CutFlow/MET__3_CutOn_MT_n_Mll_n_Met");
689     WZ_kin_plot("met[4]",40,0,200,"MET [GeV]",BasicCut&&TCut("tri_mT>40&&abs(tri_mlll-91)>30&&met[4]>30&&abs(tri_mll-91)<10"),"CutFlow/MET__4_CutOn_MT_n_Mll_n_Met_MllWindow");
690    
691    
692     WZ_kin_plot("tri_mll",40,0,200,"m_{Z} [GeV]",BasicCut&&Selection,"Zmass");
693     WZ_kin_plot("tri_mlll",40,0,400,"m_{lll} [GeV]",BasicCut&&Selection,"TrileptonMass");
694     WZ_kin_plot("tri_mT",40,0,200,"m_{T} [GeV]",BasicCut&&Selection,"mT");
695     WZ_kin_plot("met[4]", 40,0,400,"raw PFMET [GeV]",BasicCut&&Selection,"MET");
696     WZ_kin_plot("tri_pt1",40,0,400,"p_{T}^{1} [GeV]",BasicCut&&Selection,"Pt1");
697     WZ_kin_plot("tri_pt2",40,0,400,"p_{T}^{2} [GeV]",BasicCut&&Selection,"Pt2");
698     WZ_kin_plot("tri_pt3",40,0,400,"p_{T}^{3} [GeV]",BasicCut&&Selection,"Pt3");
699 buchmann 1.4
700 buchmann 1.6 */
701 buchmann 1.4 WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepSF,"mllsub_SF");
702     WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepOF,"mllsub_OF");
703     WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepSF&&TCut("tri_id2==0"),"mllsub_SF_ee");
704     WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepSF&&TCut("tri_id2==1"),"mllsub_SF_mm");
705     WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepOF&&TCut("tri_id2==0"),"mllsub_OF_em");
706     WZ_kin_plot("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection&&TriLepOF&&TCut("tri_id2==1"),"mllsub_OF_me");
707    
708    
709     SF_OF_comparison(BasicCut,Selection);
710     // generator_SF_OF_comparison();
711     // reco_SF_OF_comparison(BasicCut,Selection);
712    
713     SF_vs_OF_plots("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection,TriLepSF,TriLepOF,mc,"mllsub_MC");
714     SF_vs_OF_plots("tri_submll",40,0,200,"m_{l_{2}l_{3}} [GeV]",BasicCut&&Selection,TriLepSF,TriLepOF,data,"mllsub_data");
715     AlgorithmTest(BasicCut,Selection);
716     GetResult(BasicCut,Selection);
717    
718    
719     }
720    
721     void Make_Chi2_Resolution_Minimization(TGraph *gr) {
722     TCanvas *can2 = new TCanvas("can2","can2");
723     can2->cd();
724     gStyle->SetOptFit(0);
725     gr->Draw("AP*");
726     gr->GetXaxis()->SetTitle("MET scale factor");
727     gr->GetYaxis()->SetTitle("#chi^{2}");
728     gr->GetXaxis()->CenterTitle();
729     gr->GetYaxis()->CenterTitle();
730     gr->Draw("AP*");
731     gr->Fit("pol3");
732     TF1 *fit = (TF1*)gr->GetFunction("pol3");
733     fit->SetLineWidth(2);
734     fit->SetLineColor(kBlue);
735     gr->Draw("AP*");
736     DrawPrelim();
737     CompleteSave(can2,"WZ_study/DYResolution/Result_Chi2");
738     delete can2;
739     }
740 buchmann 1.3
741 buchmann 1.4 void ExtractDYMissingResolution() {
742 buchmann 1.3
743 buchmann 1.4 TCanvas *can = new TCanvas("can","can");
744    
745     TH1F *histoSF = allsamples.Draw("histoSF","met[4]",50,0,100,"MET [GeV]","events",TCut("id1==id2&&mll>0&&pfJetGoodNum40>=2"),data,luminosity);
746     TH1F *histoOF = allsamples.Draw("histoOF","met[4]",50,0,100,"MET [GeV]","events",TCut("id1!=id2&&mll>0&&pfJetGoodNum40>=2"),data,luminosity);
747     THStack histoMC = allsamples.DrawStack("histoMC","met[4]",50,0,100,"MET [GeV]","events",TCut("id1==id2&&mll>0&&pfJetGoodNum40>=2"),mc,luminosity);
748     TFile *f = new TFile("file.root","RECREATE");
749 buchmann 1.3
750 buchmann 1.4 TH1F *MC_wo_DY = CollapseStack(histoMC);
751     TIter nextSF = TIter(histoMC.GetHists());
752     TH1F *h;
753     while ( h = (TH1F*)nextSF() ) {
754     if(Contains(h->GetName(),"Z_Jets")) {
755     MC_wo_DY->Add(h,-1);
756     }
757     }
758     MC_wo_DY->Write();
759    
760    
761     int ntests=150;
762     TH1F *DYres[ntests];
763    
764     float min_chi2=-1;
765     int best_index=-1;
766     stringstream allresults;
767     TGraph *gr = new TGraph();
768     for(int i=0;i<ntests;i++) {
769    
770     float smearby=0.15*i/((float)ntests);
771    
772     stringstream drawvariable;
773     drawvariable << "((" << 1+smearby << ")*met[4])";
774     cout << "Going to use the variable " << drawvariable.str() << endl;
775     stringstream name;
776     name << "histo_DY_" << i;
777     DYres[i] = allsamples.Draw(name.str().c_str(),drawvariable.str(),50,0,100,"MET [GeV]","events",TCut("id1==id2&&mll>0&&pfJetGoodNum40>=2"),mc,luminosity, allsamples.FindSample("DYJets"));
778     DYres[i]->Write();
779    
780     TPad *pad = new TPad("pad","pad",0,0,1,1);
781     pad->cd();
782    
783     DrawPrelim();
784     MC_wo_DY->SetFillColor(kWhite);
785     MC_wo_DY->SetLineColor(kBlue);
786     TLegend *leg = make_legend();
787     leg->SetY1(0.8);
788     leg->AddEntry(histoSF,"data","pl");
789     leg->AddEntry(MC_wo_DY,"MC","l");
790    
791     TH1F *sum = (TH1F*)MC_wo_DY->Clone("sum");
792     sum->Add(DYres[i]);
793     sum->Draw("histo");
794     histoSF->Draw("e1,same");
795     stringstream Result;
796    
797     Double_t chi2;
798     Int_t ndf,igood;
799     Double_t res=0.0;
800     Double_t chi2prob = MarcosChi2TestX(histoSF,sum,chi2,ndf,igood,"UW");
801     Result << "#splitline{Smeared by : " << std::setprecision(3) << 100*smearby << " %}{#splitline{#chi^{2} / NDF : " << std::setprecision(3) << chi2 << " / " << ndf << "}{ #chi^{2} prob : " << std::setprecision(3) << chi2prob << "}}";
802    
803     allresults << "\n" << smearby << ";" << chi2;
804     gr->SetPoint(i,smearby,chi2);
805    
806     if(min_chi2<0||chi2<min_chi2) {
807     min_chi2=chi2;
808     best_index=i;
809     }
810    
811     TPaveText *PurityPaveText = new TPaveText(0.60, 0.40,0.89, 0.6,"blNDC");
812     PurityPaveText->SetFillStyle(4000);
813     PurityPaveText->SetBorderSize(0);
814     PurityPaveText->SetFillColor(kWhite);
815     PurityPaveText->SetTextFont(42);
816     PurityPaveText->SetTextSize(0.042);
817     PurityPaveText->AddText(Result.str().c_str());
818     PurityPaveText->Draw();
819     DrawPrelim();
820     leg->Draw();
821    
822    
823     stringstream savethis;
824     savethis << "WZ_study/DYResolution/Response" << 100*smearby << "Percent";
825    
826     save_with_ratio(histoSF,sum,pad->cd(),savethis.str());
827     }
828    
829     gr->SetTitle("Chi2Diagram");
830     gr->SetName("Chi2Diagram");
831    
832    
833     dout << "Result summary: " << endl;
834     dout << allresults.str() << endl;
835 buchmann 1.3
836 buchmann 1.4 nextSF = TIter(histoMC.GetHists());
837     THStack stack;
838     while ( h = (TH1F*)nextSF() ) {
839     if(!Contains(h->GetName(),"Z_Jets")) {
840     stack.Add(h);
841     }
842     }
843 buchmann 1.3
844    
845 buchmann 1.4 TPad *pad = new TPad("pad","pad",0,0,1,1);
846     pad->cd();
847    
848     DYres[best_index]->SetFillColor(kYellow);
849     DYres[best_index]->SetLineColor(kYellow);
850 buchmann 1.1
851 buchmann 1.4 stack.Add(DYres[best_index]);
852     stack.Draw();
853     histoSF->Draw("e1,same");
854     DrawPrelim();
855     save_with_ratio(histoSF,CollapseStack(stack),pad->cd(),"WZ_study/DYResolution/Result");
856 buchmann 1.1
857 buchmann 1.4 TFile *fr = new TFile("rumba.root","RECREATE");
858     gr->Write();
859     fr->Close();
860 buchmann 1.1
861 buchmann 1.4 delete can;
862 buchmann 1.1
863 buchmann 1.4 Make_Chi2_Resolution_Minimization(gr);
864    
865 buchmann 1.1 }
866    
867     void WZstudy() {
868 buchmann 1.4 // ExtractDYMissingResolution();
869 buchmann 1.1
870 buchmann 1.6 cutWeight=TCut("(weight*(weight<1000)*(is_data+(!is_data)))");
871 buchmann 1.1 switch_overunderflow(true);
872     cout << "Essential cut is " << (const char*) essentialcut << endl;
873     cout << "Going to set essential cut to new triggers" << endl;
874     TCut essential_bkp = essentialcut;
875     write_warning(__FUNCTION__,"Need to define trigger requirement for WZ!");
876 buchmann 1.3 essentialcut=TCut("mll>5||mll<6");
877 buchmann 1.1 cout << "Essential cut is now " << (const char*) essentialcut << endl;
878    
879     CarryOutWZStudy();
880    
881    
882     cout << "Essential cut is now " << (const char*) essentialcut << endl;
883     essentialcut = essential_bkp;
884     cout << "Essential cut is now " << (const char*) essentialcut << " (restored)" << endl;
885     switch_overunderflow(false);
886 buchmann 1.4
887 buchmann 1.1 }
888 buchmann 1.3