ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/makePlots.C
Revision: 1.3
Committed: Tue Jun 22 14:55:49 2010 UTC (14 years, 10 months ago) by jengbou
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +6 -3 lines
Log Message:
Minor change of style

File Contents

# Content
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 #include "TPaveText.h"
10 #include "TLegend.h"
11 #include "TStyle.h"
12 #include "TLatex.h"
13 #include <map>
14
15 using namespace std;
16 //// 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.07);
182
183 latex.SetTextAlign(31); // align right
184 latex.DrawLatex(0.98,0.94,"#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.94,"#font[22]{CMS preliminary 2010}");
191 return;
192 }
193
194 /// Main code
195 struct rang {
196 double min;
197 double max;
198 rang(const double & x1 = 999999, const double & x2 = -999999) {min = x1; max = x2;}
199 };
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 const int intLS_[2] = {1,5};
210 const int nvar_ = 6;
211 const int mergbin_[2] = {25,600};
212 // variables to plot
213 string coord[nvar_] = {"X","Y","Z","beamWidthX","beamWidthY","SigmaZ"};
214 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 // beammonitor module names
223 string dqmapp[2] = {"trk","pix"};
224 string label[2] = {"Track Based","Pix Vtx Based"};
225
226 //gROOT->SetStyle("CMS");
227 setTDRStyle();
228 gStyle->SetErrorX(0);
229
230 fs[0] = TFile::Open("fill_1089_trk4.root");
231 fs[1] = TFile::Open("fill_1089_pix4.root");
232 string appendix = "1089";
233
234 int nbins_ = 100;
235 int xmax_ = -99999;
236 int xmin_ = 99999;
237
238 // Getting/merging X-axis abels
239 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 nbins_ = 3500;// = xmax_;
264
265 // Making plots
266 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 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
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 nthBin = valxbin;
295 if (
296 nthBin > 0
297 && nthBin <= nbins_
298 ) {
299 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 }
313 // find good y-axis range
314 double yval = htmp->GetBinContent(i+1);
315 double yerr = htmp->GetBinError(i+1);
316 if (
317 (n0<3 && nthBin%intLS_[0] == 0)
318 || (n0 >=3
319 && nthBin%intLS_[1] == 0
320 )
321 ) {
322 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 }
327 }
328
329 // Labeling
330 if (n1 ==0) {// execute once for each module
331 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 char buffer[5];
340 sprintf(buffer,"%i",i);
341 hs[histName]->GetXaxis()->SetBinLabel(i/intLS_[1],buffer);
342 hs[histName]->LabelsOption("h","x");
343 }
344 }
345 }
346
347 if (n1 == 1)
348 hs[histName]->SetMarkerStyle(22);
349
350 // hs[histName]->SetMarkerStyle(8);
351 // if (n0<3) {
352 // hs[histName]->SetMarkerSize(0.8);
353 // hs[histName]->SetLineWidth(0.4);
354 // }
355 // else {
356 // hs[histName]->SetMarkerSize(0.4);
357 // hs[histName]->SetLineWidth(0.2);
358 // }
359 hs[histName]->GetXaxis()->SetTitle(htmp->GetXaxis()->GetTitle());
360 //hs[histName]->GetXaxis()->SetTitleSize(0.08);
361 //hs[histName]->GetXaxis()->SetTitleOffset(0.72);
362 //hs[histName]->GetXaxis()->SetLabelSize(0.07);
363 //hs[histName]->GetXaxis()->SetLabelOffset(0.01);
364
365 // if (coord[n0] == "Z" || coord[n0] == "SigmaZ")
366 // hs[histName]->GetYaxis()->SetTitle(htmp->GetYaxis()->GetTitle());
367 // else {
368 // TString tmp = htmp->GetYaxis()->GetTitle();
369 // TString tmpTitle;
370 // if (n0 <3 ) tmpTitle = tmp.Replace(tmp.Length()-4,tmp.Length(),"[ #mum]");
371 // else tmpTitle = tmp.Replace(tmp.Length()-4,tmp.Length(),"[#mum]");
372 // hs[histName]->GetYaxis()->SetTitle(tmpTitle);
373 // }
374 hs[histName]->GetYaxis()->SetTitle(TString(ytitle[n0]));
375 //hs[histName]->GetYaxis()->SetTitleSize(0.08);
376 //hs[histName]->GetYaxis()->SetTitleOffset(0.7);
377 //hs[histName]->GetYaxis()->SetLabelSize(0.05);
378
379 }
380 //cout << "Ran max = " << ranges[TString(coord[n0])].max << "; ";
381 //cout << "Ran min = " << ranges[TString(coord[n0])].min << endl;;
382 }
383
384
385 // gStyle->SetPadTickX(0);
386 // gStyle->SetPadTickY(0);
387 // tdrGrid(true);
388
389 TString txtcom = "CMS Preliminary 2010, #sqrt{s} = 7 TeV Data";
390 //TString txtcom1 = "Run 135149; LHC Fill: 1089";
391
392 // Position plots
393 ts["pos"] = new TCanvas("pos","positions",800,900);
394 ts["pos"]->Divide(1,3);
395
396 TPaveText tmptxt(0.2,0.7,0.45,0.88,"NDC");
397 tmptxt.AddText(txtcom);
398 //tmptxt.AddText(txtcom1);
399 tmptxt.SetBorderSize(0);
400 tmptxt.SetFillColor(0);
401 tmptxt.SetFillStyle(0);
402
403 for (int n0=0;n0<3;++n0) {//X,Y,Z
404 TString cvName = "pos_"+coord[n0];
405 ts[cvName] = new TCanvas(cvName,"position",800,600);
406
407 TLegend *tmpleg = new TLegend(0.65,0.7,0.85,0.88);
408 tmpleg->SetFillColor(0);
409 tmpleg->SetFillStyle(0);
410
411 for (int n1=0;n1<2;++n1) {//TRK,PIX
412 TString histName = TString(coord[n0]+"_"+dqmapp[n1]);
413 histName += "_new";
414 hs[histName]->SetLineColor(n1*2+2);
415 hs[histName]->SetMarkerColor(n1*2+2);
416 tmpleg->AddEntry(hs[histName],TString(label[n1]),"p");
417 if (n1==0) {
418 hs[histName]->GetXaxis()->SetRangeUser(560,700);
419 //hs[histName]->GetXaxis()->SetRangeUser(290,3380);
420 hs[histName]->GetYaxis()->SetRangeUser(ranges[TString(coord[n0])].min,ranges[TString(coord[n0])].max);
421 hs[histName]->Draw();
422 }
423 else
424 hs[histName]->Draw("same");
425 }
426 //if (n0 == 0)
427 tmpleg->Draw();
428 //tmptxt.Draw();
429 cmsPrel(-1);
430 ts[cvName]->Print(TString(cvName+"_"+appendix+".png"));
431 ts["pos"]->cd(n0+1);
432 ts[cvName]->DrawClonePad();
433 }
434 ts["pos"]->Print(TString("positions_"+ appendix +".png"));
435
436 // Width plots
437 ts["width"] = new TCanvas("width","widths",800,900);
438 ts["width"]->Divide(1,3);
439
440 TPaveText tmptxt1(0.35,0.7,0.6,0.88,"NDC");
441 tmptxt1.AddText(txtcom);
442 //tmptxt1.AddText(txtcom1);
443 tmptxt1.SetBorderSize(0);
444 tmptxt1.SetFillColor(0);
445 tmptxt1.SetFillStyle(0);
446
447 for (int n0=3;n0<nvar_;++n0) {//X,Y,Z
448 TString cvName = "width_"+coord[n0-3];
449 ts[cvName] = new TCanvas(cvName,"width",800,600);
450
451 TLegend *tmpleg = new TLegend(0.35,0.7,0.55,0.88);
452 tmpleg->SetFillColor(0);
453 tmpleg->SetFillStyle(0);
454
455 for (int n1=0;n1<2;++n1) {//TRK,PIX
456 TString histName = TString(coord[n0]+"_"+dqmapp[n1]);
457 histName += "_new";
458 hs[histName]->SetLineColor(n1*2+2);
459 hs[histName]->SetMarkerColor(n1*2+2);
460 tmpleg->AddEntry(hs[histName],TString(label[n1]),"p");
461 if (n1==0) {
462 //hs[histName]->GetXaxis()->SetRangeUser(1,int(hs[histName]->GetNbinsX()-40));
463 hs[histName]->GetXaxis()->SetRangeUser(290,3380);
464 hs[histName]->GetYaxis()->SetRangeUser(ranges[TString(coord[n0])].min,ranges[TString(coord[n0])].max);
465 hs[histName]->Draw();
466 }
467 else
468 hs[histName]->Draw("same");
469 }
470 //if (n0 == 3)
471 tmpleg->Draw();
472 //tmptxt1.Draw();
473 cmsPrel(-1);
474 ts[cvName]->Print(TString(cvName+"_"+appendix+".png"));
475 ts["width"]->cd(n0-2);
476 ts[cvName]->DrawClonePad();
477 }
478 ts["width"]->Print(TString("widths_"+ appendix +".png"));
479 }
480
481