ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/makePlots.C
Revision: 1.2
Committed: Thu Jun 17 06:38:21 2010 UTC (14 years, 10 months ago) by jengbou
Content type: text/plain
Branch: MAIN
Changes since 1.1: +316 -57 lines
Log Message:
update

File Contents

# User Rev Content
1 jengbou 1.1 #include "TROOT.h"
2     #include "TH1.h"
3     #include "TF1.h"
4     #include "TMath.h"
5     #include "TAxis.h"
6     #include "TString.h"
7     #include "TFile.h"
8     #include "TCanvas.h"
9 jengbou 1.2 #include "TPaveText.h"
10 jengbou 1.1 #include "TLegend.h"
11     #include "TStyle.h"
12 jengbou 1.2 #include "TLatex.h"
13 jengbou 1.1 #include <map>
14    
15     using namespace std;
16 jengbou 1.2 //// TDR Style
17     // fixOverlay: Redraws the axis
18     void fixOverlay() {
19     gPad->RedrawAxis();
20     }
21    
22     void setTDRStyle() {
23     TStyle *tdrStyle = new TStyle("tdrStyle","Style for P-TDR");
24    
25     // For the canvas:
26     tdrStyle->SetCanvasBorderMode(0);
27     tdrStyle->SetCanvasColor(kWhite);
28     tdrStyle->SetCanvasDefH(600); //Height of canvas
29     tdrStyle->SetCanvasDefW(600); //Width of canvas
30     tdrStyle->SetCanvasDefX(0); //POsition on screen
31     tdrStyle->SetCanvasDefY(0);
32    
33     // For the Pad:
34     tdrStyle->SetPadBorderMode(0);
35     // tdrStyle->SetPadBorderSize(Width_t size = 1);
36     tdrStyle->SetPadColor(kWhite);
37     tdrStyle->SetPadGridX(false);
38     tdrStyle->SetPadGridY(false);
39     tdrStyle->SetGridColor(0);
40     tdrStyle->SetGridStyle(3);
41     tdrStyle->SetGridWidth(1);
42    
43     // For the frame:
44     tdrStyle->SetFrameBorderMode(0);
45     tdrStyle->SetFrameBorderSize(1);
46     tdrStyle->SetFrameFillColor(0);
47     tdrStyle->SetFrameFillStyle(0);
48     tdrStyle->SetFrameLineColor(1);
49     tdrStyle->SetFrameLineStyle(1);
50     tdrStyle->SetFrameLineWidth(1);
51    
52     // For the histo:
53     // tdrStyle->SetHistFillColor(1);
54     // tdrStyle->SetHistFillStyle(0);
55     tdrStyle->SetHistLineColor(1);
56     tdrStyle->SetHistLineStyle(0);
57     tdrStyle->SetHistLineWidth(1);
58     // tdrStyle->SetLegoInnerR(Float_t rad = 0.5);
59     // tdrStyle->SetNumberContours(Int_t number = 20);
60    
61     tdrStyle->SetEndErrorSize(2);
62     tdrStyle->SetErrorX(0.);
63    
64     tdrStyle->SetMarkerStyle(20);
65    
66     //For the fit/function:
67     tdrStyle->SetOptFit(1);
68     tdrStyle->SetFitFormat("5.4g");
69     tdrStyle->SetFuncColor(2);
70     tdrStyle->SetFuncStyle(1);
71     tdrStyle->SetFuncWidth(1);
72    
73     //For the date:
74     tdrStyle->SetOptDate(0);
75     // tdrStyle->SetDateX(Float_t x = 0.01);
76     // tdrStyle->SetDateY(Float_t y = 0.01);
77    
78     // For the statistics box:
79     tdrStyle->SetOptFile(0);
80     tdrStyle->SetOptStat(0); // To display the mean and RMS: SetOptStat("mr");
81     // tdrStyle->SetOptStat("mr"); // To display the mean and RMS: SetOptStat("mr");
82     tdrStyle->SetStatColor(kWhite);
83     tdrStyle->SetStatFont(42);
84     tdrStyle->SetStatFontSize(0.025);
85     tdrStyle->SetStatTextColor(1);
86     tdrStyle->SetStatFormat("6.4g");
87     tdrStyle->SetStatBorderSize(1);
88     tdrStyle->SetStatH(0.1);
89     tdrStyle->SetStatW(0.15);
90     // tdrStyle->SetStatStyle(Style_t style = 1001);
91     // tdrStyle->SetStatX(Float_t x = 0);
92     // tdrStyle->SetStatY(Float_t y = 0);
93    
94     // Margins:
95     tdrStyle->SetPadTopMargin(0.1);//0.06
96     tdrStyle->SetPadBottomMargin(0.16);//0.13
97     tdrStyle->SetPadLeftMargin(0.13);//0.16
98     tdrStyle->SetPadRightMargin(0.03);
99    
100     // For the Global title:
101    
102     tdrStyle->SetOptTitle(0);
103     tdrStyle->SetTitleFont(42);
104     tdrStyle->SetTitleColor(1);
105     tdrStyle->SetTitleTextColor(1);
106     tdrStyle->SetTitleFillColor(0);//10
107     tdrStyle->SetTitleFontSize(0.05);
108     // tdrStyle->SetTitleH(0); // Set the height of the title box
109     // tdrStyle->SetTitleW(0); // Set the width of the title box
110     // tdrStyle->SetTitleX(0); // Set the position of the title box
111     // tdrStyle->SetTitleY(0.985); // Set the position of the title box
112     // tdrStyle->SetTitleStyle(Style_t style = 1001);
113     // tdrStyle->SetTitleBorderSize(2);
114    
115     // For the axis titles:
116    
117     tdrStyle->SetTitleBorderSize(0);//not cms
118     tdrStyle->SetTitleFillColor(0);//not cms
119    
120     tdrStyle->SetTitleColor(1, "XYZ");
121     tdrStyle->SetTitleFont(42, "XYZ");
122     tdrStyle->SetTitleSize(0.09, "XYZ");//0.06
123     // tdrStyle->SetTitleXSize(Float_t size = 0.02); // Another way to set the size?
124     // tdrStyle->SetTitleYSize(Float_t size = 0.02);
125     tdrStyle->SetTitleXOffset(0.9);
126     tdrStyle->SetTitleYOffset(0.7);//1.3
127     // tdrStyle->SetTitleOffset(1.1, "Y"); // Another way to set the Offset
128    
129     // For the axis labels:
130    
131     tdrStyle->SetLabelColor(1, "XYZ");
132     tdrStyle->SetLabelFont(42, "XYZ");
133     tdrStyle->SetLabelOffset(0.01, "XYZ");
134     tdrStyle->SetLabelSize(0.08, "YZ");
135     tdrStyle->SetLabelSize(0.1, "X");
136    
137     // For the axis:
138    
139     tdrStyle->SetAxisColor(1, "XYZ");
140     tdrStyle->SetStripDecimals(kTRUE);
141     tdrStyle->SetTickLength(0.03, "XYZ");
142     tdrStyle->SetNdivisions(510, "XYZ");
143     tdrStyle->SetPadTickX(1); // To get tick marks on the opposite side of the frame
144     tdrStyle->SetPadTickY(1);
145    
146     // For the legend
147     tdrStyle->SetLegendBorderSize(0);
148    
149     // Change for log plots:
150     tdrStyle->SetOptLogx(0);
151     tdrStyle->SetOptLogy(0);
152     tdrStyle->SetOptLogz(0);
153    
154     // Postscript options:
155     tdrStyle->SetPaperSize(20.,20.);
156     // tdrStyle->SetLineScalePS(Float_t scale = 3);
157     // tdrStyle->SetLineStyleString(Int_t i, const char* text);
158     // tdrStyle->SetHeaderPS(const char* header);
159     // tdrStyle->SetTitlePS(const char* pstitle);
160    
161     // tdrStyle->SetBarOffset(Float_t baroff = 0.5);
162     // tdrStyle->SetBarWidth(Float_t barwidth = 0.5);
163     // tdrStyle->SetPaintTextFormat(const char* format = "g");
164     // tdrStyle->SetPalette(Int_t ncolors = 0, Int_t* colors = 0);
165     // tdrStyle->SetTimeOffset(Double_t toffset);
166     // tdrStyle->SetHistMinimumZero(kTRUE);
167    
168     tdrStyle->cd();
169    
170     }
171     // tdrGrid: Turns the grid lines on (true) or off (false)
172    
173     void tdrGrid(bool gridOn) {
174     gStyle->SetPadGridX(gridOn);
175     gStyle->SetPadGridY(gridOn);
176     }
177    
178     void cmsPrel(double intLumi){
179     TLatex latex;
180     latex.SetNDC();
181     latex.SetTextSize(0.05);
182    
183     latex.SetTextAlign(31); // align right
184     latex.DrawLatex(0.98,0.945,"#sqrt{s} = 7 TeV");
185     if (intLumi > 0.) {
186     latex.SetTextAlign(31); // align right
187     latex.DrawLatex(0.98,0.68,Form("#int #font[12]{L}dt = %.1fnb^{-1}",intLumi));
188     }
189     latex.SetTextAlign(11); // align left
190     latex.DrawLatex(0.02,0.945,"#font[22]{CMS preliminary 2010}");
191     return;
192     }
193    
194     /// Main code
195 jengbou 1.1 struct rang {
196     double min;
197     double max;
198 jengbou 1.2 rang(const double & x1 = 999999, const double & x2 = -999999) {min = x1; max = x2;}
199 jengbou 1.1 };
200    
201     map<TString,TString> histName;
202     map<TString,TH1*> hs;
203     map<TString,TCanvas*> ts;
204     map<TString,rang> ranges;
205     map<int,TFile*> fs;
206     map<int,TString> binlabel;
207    
208     void makePlots() {
209 jengbou 1.2 const int intLS_[2] = {1,5};
210 jengbou 1.1 const int nvar_ = 6;
211 jengbou 1.2 const int mergbin_[2] = {25,600};
212 jengbou 1.1 // variables to plot
213     string coord[nvar_] = {"X","Y","Z","beamWidthX","beamWidthY","SigmaZ"};
214 jengbou 1.2 string ytitle[6] = {
215     "Beam Spot X_{ } [#mum]",
216     "Beam Spot Y_{ } [#mum]",
217     "Beam Spot Z_{ } [cm]",
218     "Beam Spot #sigma_{X} [#mum]",
219     "Beam Spot #sigma_{Y} [#mum]",
220     "Beam Spot #sigma_{Z} [cm]"
221     };
222 jengbou 1.1 // beammonitor module names
223     string dqmapp[2] = {"trk","pix"};
224     string label[2] = {"Track Based","Pix Vtx Based"};
225    
226 jengbou 1.2 //gROOT->SetStyle("CMS");
227     setTDRStyle();
228 jengbou 1.1 gStyle->SetErrorX(0);
229    
230 jengbou 1.2 fs[0] = TFile::Open("fill_1089_trk4.root");
231     fs[1] = TFile::Open("fill_1089_pix4.root");
232 jengbou 1.1 string appendix = "1089";
233    
234     int nbins_ = 100;
235     int xmax_ = -99999;
236     int xmin_ = 99999;
237    
238 jengbou 1.2 // Getting/merging X-axis abels
239 jengbou 1.1 for (int n1=0;n1<2;++n1) { // loop modules
240     TString histName = TString("X_"+dqmapp[n1]);
241     TH1F *htmp = (TH1F*) fs[n1]->Get("X");
242     TAxis *xtmp = htmp->GetXaxis();
243     TString tmplabel = xtmp->GetBinLabel(xtmp->GetXmax());
244     int widx=0;
245     for (int w=0;w<tmplabel.Length();w++) {
246     if (tmplabel(w,1) == "-" ||
247     tmplabel(w,1) == ":") // for TRK based: 13xxxx:yyy-zzzz;y and z not necessarily have same digits
248     widx = w + 1;
249     }
250     tmplabel.Remove(0,widx);
251     if (tmplabel.Atoi() > xmax_) xmax_ = tmplabel.Atoi();
252    
253     tmplabel = xtmp->GetBinLabel(1);
254     for (int w=0;w<tmplabel.Length();w++) {
255     if (tmplabel(w,1) == "-" ||
256     tmplabel(w,1) == ":")
257     widx = w + 1;
258     }
259     tmplabel.Remove(0,widx);
260     if (tmplabel.Atoi() < xmin_) xmin_ = tmplabel.Atoi();
261     }
262     //cout << "xmin_ = " << xmin_ << " xmax = " << xmax_ << endl;
263 jengbou 1.2 nbins_ = 3500;// = xmax_;
264 jengbou 1.1
265 jengbou 1.2 // Making plots
266 jengbou 1.1 for (int n0=0;n0<nvar_;++n0) { // loop variables
267     for (int n1=0;n1<2;++n1) { // loop modules
268     TString histName = TString(coord[n0]+"_"+dqmapp[n1]);
269    
270     TH1F *htmp = (TH1F*) fs[n1]->Get(TString(coord[n0]));
271     histName += "_new";
272 jengbou 1.2 if ( n0 <3) hs[histName] = new TH1F(histName,histName,nbins_/intLS_[0],intLS_[0],(nbins_/intLS_[0])*intLS_[0]+intLS_[0]);
273     else {
274     hs[histName] = new TH1F(histName,histName,nbins_/intLS_[1],intLS_[1],(nbins_/intLS_[1])*intLS_[1]+intLS_[1]);
275     }
276     // cout << "(nbins_/intLS_[0])*intLS_[0] = " << (nbins_/intLS_[0])*intLS_[0] << endl;
277     // cout << "(nbins_/intLS_[1])*intLS_[1] = " << (nbins_/intLS_[1])*intLS_[1] << endl;
278    
279     if (coord[n0] != "Z" && coord[n0] != "SigmaZ") {
280     htmp->Scale(10000.); // change unit of cm to micron
281     }
282 jengbou 1.1
283     for (int i=0;i<htmp->GetNbinsX();i++) {
284     int nthBin = 0;
285     binlabel[i+1] = htmp->GetXaxis()->GetBinLabel(i+1);
286     int widx = 0;
287     for (int w=0;w<binlabel[i+1].Length();w++) {
288     if (binlabel[i+1](w,1) == "-" ||
289     binlabel[i+1](w,1) == ":") // for TRK based: 13xxxx:yyy-zzzz;y and z not necessarily have same digits
290     widx = w + 1;
291     }
292     binlabel[i+1].Remove(0,widx);
293     int valxbin = binlabel[i+1].Atoi();
294 jengbou 1.2 nthBin = valxbin;
295 jengbou 1.1 if (
296     nthBin > 0
297     && nthBin <= nbins_
298     ) {
299 jengbou 1.2 if (n0 < 3
300     && nthBin%intLS_[0] == 0
301     ) {
302     hs[histName]->SetBinContent(nthBin/intLS_[0],htmp->GetBinContent(i+1));
303     hs[histName]->SetBinError(nthBin/intLS_[0],htmp->GetBinError(i+1));
304     }
305     else
306     if (n0 >=3
307     && nthBin%intLS_[1] == 0
308     ) {
309     hs[histName]->SetBinContent(nthBin/intLS_[1],htmp->GetBinContent(i+1));
310     hs[histName]->SetBinError(nthBin/intLS_[1],htmp->GetBinError(i+1));
311     }
312 jengbou 1.1 }
313     // find good y-axis range
314     double yval = htmp->GetBinContent(i+1);
315     double yerr = htmp->GetBinError(i+1);
316     if (
317 jengbou 1.2 (n0<3 && nthBin%intLS_[0] == 0)
318     || (n0 >=3
319     && nthBin%intLS_[1] == 0
320     )
321 jengbou 1.1 ) {
322 jengbou 1.2 if (yval+2*yerr >= ranges[TString(coord[n0])].max)
323     ranges[TString(coord[n0])].max = yval + 2*yerr;
324     if (yval-2*yerr <= ranges[TString(coord[n0])].min)
325     ranges[TString(coord[n0])].min = yval - 2*yerr;
326 jengbou 1.1 }
327     }
328    
329     // Labeling
330     if (n1 ==0) {// execute once for each module
331 jengbou 1.2 for (int i=1;i<nbins_;++i) {
332     if (n0 < 3 && i%mergbin_[0] == 0) {
333     char buffer[5];
334     sprintf(buffer,"%i",i);
335     hs[histName]->GetXaxis()->SetBinLabel(i/intLS_[0],buffer);
336     hs[histName]->LabelsOption("h","x");
337     }
338     else if (n0 >= 3 && i%mergbin_[1] == 0) {
339 jengbou 1.1 char buffer[5];
340 jengbou 1.2 sprintf(buffer,"%i",i);
341     hs[histName]->GetXaxis()->SetBinLabel(i/intLS_[1],buffer);
342 jengbou 1.1 hs[histName]->LabelsOption("h","x");
343     }
344     }
345     }
346    
347 jengbou 1.2 // hs[histName]->SetMarkerStyle(8);
348     // if (n0<3) {
349     // hs[histName]->SetMarkerSize(0.8);
350     // hs[histName]->SetLineWidth(0.4);
351     // }
352     // else {
353     // hs[histName]->SetMarkerSize(0.4);
354     // hs[histName]->SetLineWidth(0.2);
355     // }
356 jengbou 1.1 hs[histName]->GetXaxis()->SetTitle(htmp->GetXaxis()->GetTitle());
357 jengbou 1.2 //hs[histName]->GetXaxis()->SetTitleSize(0.08);
358     //hs[histName]->GetXaxis()->SetTitleOffset(0.72);
359     //hs[histName]->GetXaxis()->SetLabelSize(0.07);
360     //hs[histName]->GetXaxis()->SetLabelOffset(0.01);
361    
362     // if (coord[n0] == "Z" || coord[n0] == "SigmaZ")
363     // hs[histName]->GetYaxis()->SetTitle(htmp->GetYaxis()->GetTitle());
364     // else {
365     // TString tmp = htmp->GetYaxis()->GetTitle();
366     // TString tmpTitle;
367     // if (n0 <3 ) tmpTitle = tmp.Replace(tmp.Length()-4,tmp.Length(),"[ #mum]");
368     // else tmpTitle = tmp.Replace(tmp.Length()-4,tmp.Length(),"[#mum]");
369     // hs[histName]->GetYaxis()->SetTitle(tmpTitle);
370     // }
371     hs[histName]->GetYaxis()->SetTitle(TString(ytitle[n0]));
372     //hs[histName]->GetYaxis()->SetTitleSize(0.08);
373     //hs[histName]->GetYaxis()->SetTitleOffset(0.7);
374     //hs[histName]->GetYaxis()->SetLabelSize(0.05);
375 jengbou 1.1
376     }
377 jengbou 1.2 //cout << "Ran max = " << ranges[TString(coord[n0])].max << "; ";
378     //cout << "Ran min = " << ranges[TString(coord[n0])].min << endl;;
379 jengbou 1.1 }
380    
381    
382 jengbou 1.2 // gStyle->SetPadTickX(0);
383     // gStyle->SetPadTickY(0);
384     // tdrGrid(true);
385    
386     TString txtcom = "CMS Preliminary 2010, #sqrt{s} = 7 TeV Data";
387     //TString txtcom1 = "Run 135149; LHC Fill: 1089";
388 jengbou 1.1
389     // Position plots
390 jengbou 1.2 ts["pos"] = new TCanvas("pos","positions",800,900);
391 jengbou 1.1 ts["pos"]->Divide(1,3);
392 jengbou 1.2
393     TPaveText tmptxt(0.2,0.7,0.45,0.88,"NDC");
394     tmptxt.AddText(txtcom);
395     //tmptxt.AddText(txtcom1);
396     tmptxt.SetBorderSize(0);
397     tmptxt.SetFillColor(0);
398     tmptxt.SetFillStyle(0);
399    
400 jengbou 1.1 for (int n0=0;n0<3;++n0) {//X,Y,Z
401 jengbou 1.2 TString cvName = "pos_"+coord[n0];
402     ts[cvName] = new TCanvas(cvName,"position",800,600);
403    
404     TLegend *tmpleg = new TLegend(0.65,0.7,0.85,0.88);
405 jengbou 1.1 tmpleg->SetFillColor(0);
406     tmpleg->SetFillStyle(0);
407    
408     for (int n1=0;n1<2;++n1) {//TRK,PIX
409     TString histName = TString(coord[n0]+"_"+dqmapp[n1]);
410     histName += "_new";
411     hs[histName]->SetLineColor(n1*2+2);
412     hs[histName]->SetMarkerColor(n1*2+2);
413     tmpleg->AddEntry(hs[histName],TString(label[n1]),"p");
414     if (n1==0) {
415     hs[histName]->GetXaxis()->SetRangeUser(560,700);
416 jengbou 1.2 //hs[histName]->GetXaxis()->SetRangeUser(290,3380);
417 jengbou 1.1 hs[histName]->GetYaxis()->SetRangeUser(ranges[TString(coord[n0])].min,ranges[TString(coord[n0])].max);
418     hs[histName]->Draw();
419     }
420     else
421     hs[histName]->Draw("same");
422     }
423     //if (n0 == 0)
424     tmpleg->Draw();
425 jengbou 1.2 //tmptxt.Draw();
426     cmsPrel(-1);
427     ts[cvName]->Print(TString(cvName+"_"+appendix+".png"));
428     ts["pos"]->cd(n0+1);
429     ts[cvName]->DrawClonePad();
430 jengbou 1.1 }
431     ts["pos"]->Print(TString("positions_"+ appendix +".png"));
432    
433     // Width plots
434 jengbou 1.2 ts["width"] = new TCanvas("width","widths",800,900);
435 jengbou 1.1 ts["width"]->Divide(1,3);
436 jengbou 1.2
437     TPaveText tmptxt1(0.35,0.7,0.6,0.88,"NDC");
438     tmptxt1.AddText(txtcom);
439     //tmptxt1.AddText(txtcom1);
440     tmptxt1.SetBorderSize(0);
441     tmptxt1.SetFillColor(0);
442     tmptxt1.SetFillStyle(0);
443    
444 jengbou 1.1 for (int n0=3;n0<nvar_;++n0) {//X,Y,Z
445 jengbou 1.2 TString cvName = "width_"+coord[n0-3];
446     ts[cvName] = new TCanvas(cvName,"width",800,600);
447    
448     TLegend *tmpleg = new TLegend(0.35,0.7,0.55,0.88);
449 jengbou 1.1 tmpleg->SetFillColor(0);
450     tmpleg->SetFillStyle(0);
451    
452     for (int n1=0;n1<2;++n1) {//TRK,PIX
453     TString histName = TString(coord[n0]+"_"+dqmapp[n1]);
454     histName += "_new";
455     hs[histName]->SetLineColor(n1*2+2);
456     hs[histName]->SetMarkerColor(n1*2+2);
457     tmpleg->AddEntry(hs[histName],TString(label[n1]),"p");
458     if (n1==0) {
459     //hs[histName]->GetXaxis()->SetRangeUser(1,int(hs[histName]->GetNbinsX()-40));
460 jengbou 1.2 hs[histName]->GetXaxis()->SetRangeUser(290,3380);
461 jengbou 1.1 hs[histName]->GetYaxis()->SetRangeUser(ranges[TString(coord[n0])].min,ranges[TString(coord[n0])].max);
462     hs[histName]->Draw();
463     }
464     else
465     hs[histName]->Draw("same");
466     }
467     //if (n0 == 3)
468     tmpleg->Draw();
469 jengbou 1.2 //tmptxt1.Draw();
470     cmsPrel(-1);
471     ts[cvName]->Print(TString(cvName+"_"+appendix+".png"));
472     ts["width"]->cd(n0-2);
473     ts[cvName]->DrawClonePad();
474 jengbou 1.1 }
475     ts["width"]->Print(TString("widths_"+ appendix +".png"));
476     }
477 jengbou 1.2
478