ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/JetAnalysis/macros/plotFigure.C
Revision: 1.1
Committed: Tue Jan 29 00:37:01 2013 UTC (12 years, 3 months ago) by yilmaz
Content type: text/plain
Branch: MAIN
CVS Tags: HiForest_V02_75, HiForest_V02_74, HiForest_V02_71, HiForest_V02_70, HiForest_V02_69, HiForest_V02_68, HiForest_V02_67, HiForest_V02_66, HiForest_V02_65, HiForest_V02_64
Log Message:
plotting macros

File Contents

# User Rev Content
1 yilmaz 1.1 #if !defined(__CINT__) || defined(__MAKECINT__)
2    
3     #include <iostream>
4     #include "TCanvas.h"
5     #include "TError.h"
6     #include "TPad.h"
7     #include "TString.h"
8     #include "TRandom.h"
9     #include "TH1F.h"
10    
11     #include "TFile.h"
12     #include "TTree.h"
13     #include "TH1D.h"
14     #include "TH2D.h"
15     #include "TCanvas.h"
16     #include "TLegend.h"
17     #include "TLatex.h"
18     #include "TString.h"
19    
20     #endif
21    
22     #include "weightMix.C"
23    
24     static int iPlot = -99;
25    
26    
27     //---------------------------------------------------------------------
28     void makeMultiPanelCanvas(TCanvas*& canv, const Int_t columns,
29     const Int_t rows, const Float_t leftOffset=0.,
30     const Float_t bottomOffset=0.,
31     const Float_t leftMargin=0.2,
32     const Float_t bottomMargin=0.2,
33     const Float_t edge=0.05);
34    
35     void plotBalance(int cbin = 0,
36     TString infname = "file1.root",
37     TString refname = "file2.root",
38     TString mixname = "file3.root",
39     bool useWeight = true,
40     bool drawXLabel = false,
41     bool drawLeg = false);
42    
43    
44     void drawText(const char *text, float xp, float yp);
45    
46     //--------------------------------------------------------------
47     // drawPatch() is a crazy way of removing 0 in the second and third
48     // pad which is partially shown due to no margin between the pads
49     // if anybody has a better way of doing it let me know! - Andre
50     //--------------------------------------------------------------
51     void drawPatch(float x1, float y1, float x2, float y2);
52     //---------------------------------------------------------------------
53    
54     void plotFigure(int iplot = 3){
55     TString infname = "/d101/yetkin/analysis/d0128/data_pPb.root";
56     TString refname = "/d101/yetkin/analysis/d0128/data_PbPb.root";
57     TString mixname = "/d101/yetkin/analysis/d0128/mix_hydjet.root";
58    
59     iPlot = iplot;
60    
61     TH1::SetDefaultSumw2();
62    
63     TCanvas *c1 = new TCanvas("c1","",1050,700);
64     makeMultiPanelCanvas(c1,3,2,0.0,0.0,0.2,0.2,0.02);
65     TLatex *jetf_PbPb;
66    
67     if(iplot == 3){
68     for(int i = 0; i < 6; ++i){
69     c1->cd(i+1)->SetLogy();
70     }
71     }
72    
73    
74     c1->cd(1);
75    
76     jetf_PbPb = new TLatex(0.477,0.54,"Anti-k_{T} (PFlow), R=0.3");
77     jetf_PbPb->SetTextFont(63);
78     jetf_PbPb->SetTextSize(15);
79     jetf_PbPb->Draw();
80    
81     double y1 = 0.07;
82     plotBalance(5,infname,refname,mixname,true,false,false);
83     drawText("N_{trk}^{offline} < 60",0.6,y1);
84    
85     c1->cd(2);
86    
87     plotBalance(4,infname,refname,mixname,0,false,false);
88     drawText("60 #leq N_{trk}^{offline} < 90",0.4,y1);
89    
90    
91     c1->cd(3);
92    
93     plotBalance(3,infname,refname,mixname,0,false,false);
94     drawText("90 #leq N_{trk}^{offline} < 110",0.4,y1);
95     // drawText("(c)",0.05,0.885);
96    
97    
98     TLatex tsel;
99     tsel.SetNDC();
100     tsel.SetTextFont(63);
101     tsel.SetTextSize(15);
102     tsel.DrawLatex(0.15,0.75,Form("p_{T,1} > %d GeV/c",leadCut));
103     tsel.DrawLatex(0.15,0.65,Form("p_{T,2} > %d GeV/c",subleadCut));
104     tsel.DrawLatex(0.15,0.55,"#Delta#phi_{12} > #frac{2}{3}#pi");
105    
106    
107     double y2 = 0.23;
108    
109     c1->cd(4);
110     plotBalance(2,infname,refname,mixname,0,false,false);
111     drawText("110 #leq N_{trk}^{offline} < 150",0.45,y2);
112     // drawText("(d)",0.25,0.92);
113    
114    
115    
116     c1->cd(5);
117     plotBalance(1,infname,refname,mixname,0,true,false);
118     drawText("150 #leq N_{trk}^{offline} < 180",0.42,y2);
119     // drawText("(e)",0.05,0.92);
120    
121     if(sideCorrect == 0){
122     jetf_PbPb = new TLatex(0.05,0.21,"Mismatched background not subtracted");
123     // jetf_PbPb->Draw();
124     }
125    
126     c1->cd(6);
127     plotBalance(0,infname,refname,mixname,0,false,1);
128     drawText("180 #leq N_{trk}^{offline}",0.45,y2);
129     // drawText("(f)",0.05,0.92);
130    
131     c1->cd(1);
132    
133     TLatex *cms = new TLatex(0.03,0.28,"CMS Preliminary");
134     if(iPlot == 3) cms = new TLatex(0.04,1.,"CMS Preliminary");
135     cms->SetTextFont(63);
136     cms->SetTextSize(17);
137     cms->Draw();
138    
139     c1->cd(1);
140     TLatex *lumi = new TLatex(0.1,0.26,"pPb L=10.6 nb^{-1}");
141     if(iPlot == 3) lumi = new TLatex(0.1,0.46,"pPb L=10.6 nb^{-1}");
142    
143     lumi->SetTextFont(63);
144     lumi->SetTextSize(15);
145     lumi->Draw();
146    
147     lumi = new TLatex(0.1,0.24,"PbPb L=150 #mub^{-1}");
148     if(iPlot == 3) lumi = new TLatex(0.1,0.14,"PbPb L=150 #mub^{-1}");
149    
150     lumi->SetTextFont(63);
151     lumi->SetTextSize(15);
152     lumi->Draw();
153    
154    
155    
156     c1->cd(2);
157     TLatex *jetf_pp;
158    
159     jetf_pp = new TLatex(0.12,0.24,"anti-k_{T}, (R=0.3) PF jets PU");
160    
161     jetf_pp->SetTextFont(63);
162     jetf_pp->SetTextSize(15);
163     jetf_pp->Draw();
164    
165     const char* date = "20130127";
166     string figures[] = {"imbalance","","","dphi"};
167     string formats[] = {"gif","pdf","eps","C"};
168    
169     for(int it = 0; it < 4; ++it){
170     c1->Print(Form("./fig/dijet_%s_lead%d_sub%d_all_cent_%s%s.%s",figures[iPlot].data(),leadCut,subleadCut,date,subtract?"_subt":"",formats[it].data()));
171     }
172    
173     }
174    
175     void plotBalance(int cbin,
176     TString infname,
177     TString pythia,
178     TString mix,
179     bool useWeight,
180     bool drawXLabel,
181     bool drawLeg)
182     {
183    
184     useWeight = 1;
185    
186     bool refOldNtuple = 0;
187     TCut lead(Form("pt1>%d && abs(eta1) < 2",leadCut));
188     TCut dijet(Form("pt1>%d && pt2>%d && abs(eta1) < 2 && abs(eta2) < 2",leadCut,subleadCut));
189    
190     TCut deltaPhi("abs(dphi)>2.0944");
191    
192     if(iPlot != 3) dijet = dijet&&deltaPhi;
193    
194     TCut side(Form("pt1>%d && pt2>%d && abs(dphi)>%f && abs(dphi)<%f && abs(eta1) < 2 && abs(eta2) < 2",leadCut,subleadCut, sideMin, sideMax));
195    
196     TCut jetID("trkMax1 > 4 || trkMax2 > 4");
197     // jetID = "trkMax1 > -99999";
198     TCut noise("noise < 0");
199     TCut weight("weight");
200    
201    
202     double sideScale = sideCorrect*(3.1415926536-2.0944)/(sideMax-sideMin);
203    
204     TString cstring = "";
205    
206     TCut centHF("");
207     TCut centNtrk("");
208    
209     if(cbin==0) centHF = "bin>=20 && bin<26";
210     if(cbin==1) centHF = "bin>=26 && bin<27";
211     if(cbin==2) centHF = "bin>=27 && bin<28";
212     if(cbin==3) centHF = "bin>=28 && bin<29";
213     if(cbin==4) centHF = "bin>=29 && bin<31";
214     if(cbin==5) centHF = "bin>=31";
215    
216     if(cbin==0) centNtrk = "ntrk >= 180";
217     if(cbin==1) centNtrk = "150 <= ntrk && ntrk < 180";
218     if(cbin==2) centNtrk = "110 <= ntrk && ntrk < 150";
219     if(cbin==3) centNtrk = "90 <= ntrk && ntrk < 150";
220     if(cbin==4) centNtrk = "60 <= ntrk && ntrk < 90";
221     if(cbin==5) centNtrk = "ntrk < 60";
222    
223    
224     cout<<"plotting ntrk bin : "<<cbin<<endl;
225    
226     // open the data file
227     TFile *inf = new TFile(infname.Data());
228     TTree *nt =(TTree*)inf->FindObjectAny("ntdijet");
229     TTree *ntevt =(TTree*)inf->FindObjectAny("ntevt");
230     nt->AddFriend(ntevt);
231    
232     // open the pythia (MC) file
233     TFile *infPythia = new TFile(pythia.Data());
234     TTree *ntPythia;
235     TTree *ntevtPythia;
236     if(!refOldNtuple){
237     ntPythia = (TTree*) infPythia->FindObjectAny("ntdijet");
238     ntevtPythia = (TTree*) infPythia->FindObjectAny("ntevt");
239     ntPythia->AddFriend(ntevtPythia);
240     }else{
241     ntPythia = (TTree*) infPythia->FindObjectAny("nt");
242     }
243    
244     // open the datamix file
245     TFile *infMix = new TFile(mix.Data());
246     TTree *ntMix =(TTree*)infMix->FindObjectAny("ntdijet");
247     TTree *ntevtMix =(TTree*)infMix->FindObjectAny("ntevt");
248     // TFile *infW = new TFile("weights_hydjet.root");
249     // TTree *ntw =(TTree*)infW->FindObjectAny("ntw");
250    
251     TTree *ntw =(TTree*)infMix->FindObjectAny("ntw");
252     ntMix->AddFriend(ntw);
253     ntMix->AddFriend(ntevtMix);
254    
255     int Nbin = 10;
256     double max = 1.;
257    
258     if(iPlot == 3){
259     Nbin = 30;
260     max = pi;
261     };
262    
263    
264     // projection histogram
265     TH1D *h = new TH1D(Form("h",cbin),"",Nbin,0,max);
266     TH1D *hPythia = new TH1D(Form("hPythia",cbin),"",Nbin,0,max);
267     TH1D *hDataMix = new TH1D(Form("hDataMix",cbin),"",Nbin,0,max);
268    
269     TH1D *hB = new TH1D(Form("hB",cbin),"",Nbin,0,max);
270     TH1D *hPythiaB = new TH1D(Form("hPythiaB",cbin),"",Nbin,0,max);
271     TH1D *hDataMixB = new TH1D(Form("hDataMixB",cbin),"",Nbin,0,max);
272    
273     TH1D *hFull = new TH1D("hFull","",Nbin,0,max);
274     TH1D *hPythiaFull = new TH1D("hPythiaFull","",Nbin,0,max);
275     TH1D *hDataMixFull = new TH1D("hDataMixFull","",Nbin,0,max);
276    
277     TH1D* hNorm = new TH1D("hNorm","",1000,0,1000);
278     TH1D* hNormPythia = new TH1D("hNormPythia","",1000,0,1000);
279     TH1D* hNormDataMix = new TH1D("hNormDataMix","",1000,0,1000);
280    
281     hB->SetLineStyle(2);
282     hPythiaB->SetLineStyle(2);
283     hDataMixB->SetLineStyle(2);
284    
285     // ntPythia->SetAlias("pt1","et1");
286     // ntPythia->SetAlias("pt2","et2");
287    
288     nt->SetAlias("pt1","jtpt1");
289     nt->SetAlias("pt2","jtpt2");
290     nt->SetAlias("eta1","jteta1");
291     nt->SetAlias("eta2","jteta2");
292     nt->SetAlias("phi1","jtphi1");
293     nt->SetAlias("phi2","jtphi2");
294    
295     ntMix->SetAlias("pt1","jtpt1");
296     ntMix->SetAlias("pt2","jtpt2");
297     ntMix->SetAlias("eta1","jteta1");
298     ntMix->SetAlias("eta2","jteta2");
299     ntMix->SetAlias("phi1","jtphi1");
300     ntMix->SetAlias("phi2","jtphi2");
301    
302    
303     if(!refOldNtuple){
304     ntPythia->SetAlias("pt1","jtpt1");
305     ntPythia->SetAlias("pt2","jtpt2");
306     ntPythia->SetAlias("eta1","jteta1");
307     ntPythia->SetAlias("eta2","jteta2");
308     ntPythia->SetAlias("phi1","jtphi1");
309     ntPythia->SetAlias("phi2","jtphi2");
310     }
311    
312     nt->SetAlias("dphi","acos(cos(phi1-phi2))");
313     ntMix->SetAlias("dphi","acos(cos(phi1-phi2))");
314     ntPythia->SetAlias("dphi","acos(cos(phi1-phi2))");
315    
316     if(iPlot == 0){
317     nt->SetAlias("var","pt2/pt1");
318     ntPythia->SetAlias("var","pt2/pt1");
319     ntMix->SetAlias("var","pt2/pt1");
320     }
321    
322     if(iPlot == 3){
323     nt->SetAlias("var","acos(cos(phi1-phi2))");
324     ntPythia->SetAlias("var","acos(cos(phi1-phi2))");
325     ntMix->SetAlias("var","acos(cos(phi1-phi2))");
326     }
327    
328     if(iPlot == 8){
329     nt->SetAlias("var","pu2/pu1");
330     ntPythia->SetAlias("var","pu2/pu1");
331     ntMix->SetAlias("var","pu2/pu1");
332     }
333    
334     nt->Draw("var>>hFull",dijet&&noise&&jetID&&centNtrk);
335     nt->Draw("var>>hB",side&&noise&&jetID&&centNtrk);
336     nt->Draw("pt1>>hNorm",lead&&noise&&jetID&&centNtrk);
337    
338     ntMix->Draw("var>>hDataMixFull",weight*(dijet&&jetID&&centHF));
339     ntMix->Draw("var>>hDataMixB",weight*(side&&jetID&&centHF));
340     ntMix->Draw("pt1>>hNormDataMix",weight*(lead&&jetID&&centHF));
341    
342     ntPythia->Draw("var>>hPythiaFull",dijet&&noise&&jetID&&centHF);
343     ntPythia->Draw("var>>hPythiaB",side&&noise&&jetID&&centHF);
344     ntPythia->Draw("pt1>>hNormPythia",lead&&noise&&jetID&&centHF);
345    
346     hDataMixB->Scale(sideScale);
347     hB->Scale(sideScale);
348     hPythiaB->Scale(sideScale);
349    
350     hDataMix->Add(hDataMixFull);
351     h->Add(hFull);
352     hPythia->Add(hPythiaFull);
353    
354     if(subtract){
355     hDataMix->Add(hDataMixB,-1);
356     h->Add(hB,-1);
357     hPythia->Add(hPythiaB,-1);
358     }
359    
360     hB->SetFillStyle(3005);
361     hB->SetFillColor(15);
362    
363     // calculate the statistical error and normalize
364     h->SetLineColor(dataColor);
365     h->SetMarkerColor(dataColor);
366     h->Sumw2();
367     if(normLead){
368     h->Scale(1./hNorm->Integral());
369     hB->Scale(1./hNorm->Integral());
370     }else{
371     hB->Scale(1./h->Integral());
372     h->Scale(1./h->Integral());
373     }
374     h->SetMarkerStyle(20);
375    
376     if(hPythia->Integral() > 0){
377     hPythia->Scale(1./hNormPythia->Integral());
378     }
379    
380     hPythia->SetLineColor(kBlue);
381     hPythia->SetFillColor(kAzure-8);
382     hPythia->SetFillStyle(3005);
383    
384     if(normLead){
385     hDataMixB->Scale(1./hNormDataMix->Integral());
386     hDataMix->Scale(1./hNormDataMix->Integral());
387     }else{
388     hDataMixB->Scale(1./hDataMix->Integral());
389     hDataMix->Scale(1./hDataMix->Integral());
390     }
391     hDataMix->SetLineColor(mixColor);
392     hDataMix->SetFillColor(mixColor);
393     hDataMix->SetFillStyle(3004);
394    
395     hDataMix->SetMarkerSize(0);
396     hDataMix->SetStats(0);
397    
398     hDataMix->GetXaxis()->SetLabelSize(22);
399     hDataMix->GetXaxis()->SetLabelFont(43);
400     hDataMix->GetXaxis()->SetTitleSize(28);
401     hDataMix->GetXaxis()->SetTitleFont(43);
402     hDataMix->GetXaxis()->SetTitleOffset(2.2);
403     hDataMix->GetXaxis()->CenterTitle();
404    
405    
406     hDataMix->GetYaxis()->SetLabelSize(22);
407     hDataMix->GetYaxis()->SetLabelFont(43);
408     hDataMix->GetYaxis()->SetTitleSize(28);
409     hDataMix->GetYaxis()->SetTitleFont(43);
410     hDataMix->GetYaxis()->SetTitleOffset(2.2);
411     hDataMix->GetYaxis()->CenterTitle();
412    
413     if(drawXLabel) hDataMix->SetXTitle("A_{J} = (p_{T,1}-p_{T,2})/(p_{T,1}+p_{T,2})");
414     hDataMix->SetYTitle("Event Fraction");
415    
416     if(iPlot==9)hDataMix->SetXTitle("p_{T,2}^{background}/p_{T,1}^{background}");
417    
418     if(drawXLabel){
419     if(iPlot==0) hDataMix->SetXTitle("p_{T,2}/p_{T,1}");
420     if(iPlot==3) hDataMix->SetXTitle("#Delta #phi_{1,2}");
421     }
422    
423     if(iPlot==0) hDataMix->SetMaximum(0.32);
424     if(iPlot==3){
425     hDataMix->SetMaximum(2.52);
426     hDataMix->SetMinimum(0.00004);
427     }
428    
429     //hDataMix->GetXaxis()->SetNdivisions(905,true);
430     hDataMix->GetYaxis()->SetNdivisions(505,true);
431    
432     hPythia->SetMarkerColor(ppColor);
433     hPythia->SetLineColor(ppColor);
434     hPythia->SetMarkerStyle(24);
435    
436     hDataMix->Draw();//"hist");
437     hDataMix->Draw("hist same");
438     hPythia->Draw("same");
439    
440     cout<<"PbPb ENTRIES : "<<endl;
441     cout<<hPythia->GetEntries()<<endl;
442    
443     cout<<"PbPb integral : "<<endl;
444     cout<<hPythia->Integral()<<endl;
445    
446     cout<<"pPb integral : "<<endl;
447     cout<<h->Integral()<<endl;
448    
449     cout<<"Mix integral : "<<endl;
450     cout<<hDataMix->Integral()<<endl;
451    
452     h->SetLineWidth(1);
453     h->Draw("same");
454     h->SetLineWidth(2);
455     h->Draw("same");
456    
457     // hDataMixB->Draw("same hist");
458     // hB->Draw("same hist");
459    
460     cout<<" mean value of data "<<h->GetMean()<<endl;
461    
462     if(drawLeg){
463     TLegend *t3=new TLegend(0.06,0.6,0.53,0.95);
464    
465     // t3->AddEntry(h,Form("%s #mub^{-1}",LUM),"");
466     t3->AddEntry(h,"pPb #sqrt{s}=5.02 TeV","p");
467     t3->AddEntry(hPythia,"PbPb #sqrt{s}=2.76 TeV","p");
468     t3->AddEntry(hDataMix,"PYTHIA+HYDJET 1.8","lf");
469    
470     t3->SetFillColor(0);
471     t3->SetBorderSize(0);
472     t3->SetFillStyle(0);
473     t3->SetTextFont(63);
474     t3->SetTextSize(15);
475     t3->Draw();
476     }
477    
478     }
479    
480     void drawPatch(float x1, float y1, float x2, float y2){
481     TLegend *t1=new TLegend(x1,y1,x2,y2);
482     t1->SetFillColor(kWhite);
483     t1->SetBorderSize(0);
484     t1->SetFillStyle(1001);
485     t1->Draw("");
486     }
487    
488     void drawText(const char *text, float xp, float yp){
489     TLatex *tex = new TLatex(xp,yp,text);
490     tex->SetTextFont(63);
491     tex->SetTextSize(22);
492     //tex->SetTextSize(0.05);
493     tex->SetTextColor(kBlack);
494     tex->SetLineWidth(1);
495     tex->SetNDC();
496     tex->Draw();
497     }
498    
499    
500     void makeMultiPanelCanvas(TCanvas*& canv,
501     const Int_t columns,
502     const Int_t rows,
503     const Float_t leftOffset,
504     const Float_t bottomOffset,
505     const Float_t leftMargin,
506     const Float_t bottomMargin,
507     const Float_t edge) {
508     if (canv==0) {
509     Error("makeMultiPanelCanvas","Got null canvas.");
510     return;
511     }
512     canv->Clear();
513    
514     TPad* pad[columns][rows];
515    
516     Float_t Xlow[columns];
517     Float_t Xup[columns];
518     Float_t Ylow[rows];
519     Float_t Yup[rows];
520     Float_t PadWidth =
521     (1.0-leftOffset)/((1.0/(1.0-leftMargin)) +
522     (1.0/(1.0-edge))+(Float_t)columns-2.0);
523     Float_t PadHeight =
524     (1.0-bottomOffset)/((1.0/(1.0-bottomMargin)) +
525     (1.0/(1.0-edge))+(Float_t)rows-2.0);
526     Xlow[0] = leftOffset;
527     Xup[0] = leftOffset + PadWidth/(1.0-leftMargin);
528     Xup[columns-1] = 1;
529     Xlow[columns-1] = 1.0-PadWidth/(1.0-edge);
530    
531     Yup[0] = 1;
532     Ylow[0] = 1.0-PadHeight/(1.0-edge);
533     Ylow[rows-1] = bottomOffset;
534     Yup[rows-1] = bottomOffset + PadHeight/(1.0-bottomMargin);
535    
536     for(Int_t i=1;i<columns-1;i++) {
537     Xlow[i] = Xup[0] + (i-1)*PadWidth;
538     Xup[i] = Xup[0] + (i)*PadWidth;
539     }
540     Int_t ct = 0;
541     for(Int_t i=rows-2;i>0;i--) {
542     Ylow[i] = Yup[rows-1] + ct*PadHeight;
543     Yup[i] = Yup[rows-1] + (ct+1)*PadHeight;
544     ct++;
545     }
546    
547     TString padName;
548     for(Int_t i=0;i<columns;i++) {
549     for(Int_t j=0;j<rows;j++) {
550     canv->cd();
551     padName = Form("p_%d_%d",i,j);
552     pad[i][j] = new TPad(padName.Data(),padName.Data(),
553     Xlow[i],Ylow[j],Xup[i],Yup[j]);
554     if(i==0) pad[i][j]->SetLeftMargin(leftMargin);
555     else pad[i][j]->SetLeftMargin(0);
556    
557     if(i==(columns-1)) pad[i][j]->SetRightMargin(edge);
558     else pad[i][j]->SetRightMargin(0);
559    
560     if(j==0) pad[i][j]->SetTopMargin(edge);
561     else pad[i][j]->SetTopMargin(0);
562    
563     if(j==(rows-1)) pad[i][j]->SetBottomMargin(bottomMargin);
564     else pad[i][j]->SetBottomMargin(0);
565    
566     pad[i][j]->Draw();
567     pad[i][j]->cd();
568     pad[i][j]->SetNumber(columns*j+i+1);
569     }
570     }
571     }
572    
573