simple-tof-analysis
 All Classes Namespaces Functions Variables Groups Pages
style.h
1 #include <TStyle.h>
2 #include <TColor.h>
3 #include <TH2D.h>
4 #include <TH1D.h>
5 #include <TCanvas.h>
6 #include <TRandom.h>
7 #include <TLatex.h>
8 #include <iostream>
9 #include <TROOT.h>
10 
11 const Style_t font = 63; //fonts: 132 = serif, 62 = default
12  //133,63 = fonts with constant size, independent of the pad size
13 const double LEGEND_SIZE = 25;
14 
15 const Int_t COLOR_NA61 = kAzure;
16 const Int_t COLOR_NA61_SYSTEMATIC = 10006;
17 const Int_t MARKER_NA61 = kFullCircle;
18 const Int_t COLOR_NA49 = kRed;
19 const Int_t COLOR_NA49_SYSTEMATIC = 10007;
20 const Int_t MARKER_NA49 = kFullSquare;
21 
22 const Int_t COLOR_AU = kRed-7;
23 
24 const double MARKER_SIZE = 0.8;
25 const double MARKER_BIGGER = 1.5*MARKER_SIZE;
26 // const double LEGEND_SIZE = 25;
27 
28 const Int_t REACTION_MARKER[5] = {kFullTriangleDown, 34, kFullCircle, kFullTriangleUp, kFullSquare}; //34 == full cross
29 const Int_t REACTION_MARKER_OPEN[5] = {kOpenTriangleDown, 28, kOpenCircle, kOpenTriangleUp, kOpenSquare}; //28 == open cross
30 const Int_t REACTION_COLOR[7] = {kAzure-2, kCyan+2, kSpring-6, kOrange+7, kRed+2, COLOR_NA61, COLOR_NA49};
31 const Int_t REACTION_SYSTEMATIC_COLOR[5] = {10001, 10002, 10003, 10004, 10005};
32 
33 
34 const Double_t MINIMUM_ACCEPTANCE = 0.9; //minimum matched to MC ratio required in order to accept the bin
35 
36 void set_style(int iPalette = 1) {
37  gStyle->Reset();
38 
39  gStyle->SetCanvasDefH(550);
40  gStyle->SetCanvasDefW(650);
41 
42  gStyle->SetTitleFont(font, "xyz");
43  gStyle->SetTitleSize(LEGEND_SIZE, "xyz");
44  gStyle->SetTitleOffset(0.7, "z");
45  gStyle->SetLabelFont(font, "xyz");
46  gStyle->SetLabelSize(LEGEND_SIZE, "xyz");
47 
48 
49  //gStyle->SetTextFont(font);
50  gStyle->SetTextSize(LEGEND_SIZE);
51  gStyle->SetLegendFont(font);
52 
53 
54  //dimensions
55  gStyle->SetPadTopMargin(0.1);
56  gStyle->SetPadBottomMargin(0.15);
57  gStyle->SetPadLeftMargin(0.2);
58  gStyle->SetPadRightMargin(0.2);
59 
60  //make all white:
61  gStyle->SetFrameBorderMode(0);
62  gStyle->SetFrameFillColor(0);
63  gStyle->SetFrameFillStyle(0); //don't fill histogram interior
64  gStyle->SetPadColor(0);
65  gStyle->SetCanvasColor(0);
66  gStyle->SetStatColor(0);
67  gStyle->SetTitleFillColor(0);
68 
69  //statbox
70  gStyle->SetOptStat(0);
71  gStyle->SetOptFit(0);
72  gStyle->SetStatX(0.90);
73  gStyle->SetStatY(0.90);
74  gStyle->SetStatW(0.12);
75  gStyle->SetStatH(0.12);
76 
77  //number format for "text" drawing option
78  gStyle->SetPaintTextFormat(".2f"); //number of decimal digits printed with "text" draw option
79 
80  //title
81  gStyle->SetTitleBorderSize(0);
82  gStyle->SetTitleX(0.2);
83  gStyle->SetTitleY(0.975f);
84  gStyle->SetTitleH(0.05f);
85 
86  //label
87  gStyle->SetLegendBorderSize(1);
88  gStyle->SetLegendFillColor(0); //does nothing?!
89  gStyle->SetFillStyle(0);
90 
91  gStyle->SetNdivisions(505);
92 
93 
94  //I don't know what is this
95  // gStyle->SetCanvasBorderMode(0);
96  // gStyle->SetPadBorderMode(0);
97 
98  // gStyle->SetTitleW(0.8f);
99  //
100  //
101  // gStyle->SetStatBorderSize(1);
102  // // gStyle->SetOptTitle(0);
103 
104  // gStyle->SetTitleXOffset(1);
105  // gStyle->SetTitleYOffset(1.5);
106  // gStyle->SetMarkerStyle(20);
107  // gStyle->SetMarkerSize(0.5);
108  // gStyle->SetFuncWidth(1.);
109 
110 
111  // gStyle->SetErrorX(0.); //no horizontal errorbars. I don't want this by default because I can miss empty bins this way
112 
113 
114  // gROOT->ForceStyle();
115 
116 
117  static const Int_t nPalettes = 3;
118  if(iPalette >= nPalettes) {
119  cerr<<"Invalid palette number "<<iPalette<<endl;
120  return;
121  }
122  static Int_t NCont[nPalettes];
123  static Int_t NRGBs[nPalettes];
124  static Int_t pal[nPalettes][1000];
125 
126 
127  static Bool_t ArePalettesDefined = false;
128 
129  if(!ArePalettesDefined) {
130  //Palette defining is time consuming and it occupies memory permanently
131  //so it shouldn't be done more than once
132  //https://sft.its.cern.ch/jira/browse/ROOT-177
133 
134  //palette with grey in the middle
135  // NCont[0] = 999;
136  // NRGBs[0] = 11;
137  // Double_t stops0[] = { 0.00, 0.02, 0.125,0.25, 0.375,0.50,0.625, 0.75, 0.875,0.98, 1.00 };
138  // Double_t red0[] = { 0.90, 0.80, 0.40, 0.00, 0.25, 0.80, 0.87, 0.90, 1.00, 0.60, 0.31 };
139  // Double_t green0[] = { 0.90, 0.80, 0.50, 0.85, 1.00, 0.80, 1.00, 0.50, 0.20, 0.00, 0.00 };
140  // Double_t blue0[] = { 1.00, 1.00, 1.00, 0.85, 0.25, 0.80, 0.12, 0.05, 0.00, 0.00, 0.00 };
141 
142 // NCont[0] = 999;
143 // NRGBs[0] = 9;
144 //
145 // Double_t stops0[] = { 0.00, 0.02, 0.125,0.30, 0.50, 0.70, 0.875, 0.98, 1.00 };
146 // Double_t red0[] = { 0.90, 0.80, 0.40, 0.00, 0.80, 1.00, 1.00, 0.60, 0.31 };
147 // Double_t green0[] = { 0.90, 0.80, 0.50, 0.85, 0.80, 0.85, 0.30, 0.00, 0.00 };
148 // Double_t blue0[] = { 1.00, 1.00, 1.00, 0.85, 0.80, 0.00, 0.00, 0.00, 0.00 };
149 
150  NCont[0] = 999;
151  NRGBs[0] = 7;
152 
153  Double_t stops0[] = { 0.00, 0.20, 0.40, 0.50, 0.60, 0.80, 1.00 };
154  Double_t red0[] = { 0.80, 0.15, 0.70, 0.95, 0.80, 0.35, 0.10 };
155  Double_t green0[] = { 0.20, 0.60, 0.70, 0.95, 0.90, 0.85, 0.20 };
156  Double_t blue0[] = { 0.80, 1.00, 0.90, 0.95, 0.80, 0.45, 0.00 };
157 
158 // Double_t red0[] = { 0.05, 0.35, 0.75, 0.95, 0.70, 0.15, 0.80 };
159 // Double_t green0[] = { 0.25, 0.95, 0.90, 0.95, 0.70, 0.60, 0.20 };
160 // Double_t blue0[] = { 0.01, 0.20, 0.80, 0.95, 0.90, 1.00, 0.80 };
161 
162 
163  Int_t ind;
164 
165  ind = TColor::CreateGradientColorTable(NRGBs[0], stops0, red0, green0, blue0, NCont[0]);
166  for (int i = 0; i < NCont[0]; i++)
167  pal[0][i] = ind+i;
168 
169 
170  //standard palette
171  NCont[1] = 999;
172  NRGBs[1] = 10;
173  Double_t stops1[] = { 0.00, 0.02, 0.15, 0.30, 0.44, 0.58, 0.72, 0.86, 0.98, 1.00 };
174  Double_t red1[] = { 0.90, 0.80, 0.40, 0.00, 0.25, 0.87, 0.90, 1.00, 0.60, 0.31 };
175  Double_t green1[] = { 0.90, 0.80, 0.50, 0.85, 1.00, 1.00, 0.50, 0.20, 0.00, 0.00 };
176  Double_t blue1[] = { 1.00, 1.00, 1.00, 0.85, 0.25, 0.12, 0.05, 0.00, 0.00, 0.00 };
177 
178  ind = TColor::CreateGradientColorTable(NRGBs[1], stops1, red1, green1, blue1, NCont[1]);
179  for (int i = 0; i < NCont[1]; i++)
180  pal[1][i] = ind+i;
181 
182  //acceptance palette
183  NCont[2] = 999;
184  NRGBs[2] = 10;
185  Double_t stops2[] = { 0.00, 0.02, 0.20, 0.40, 0.60, 0.80, 0.99999*MINIMUM_ACCEPTANCE, MINIMUM_ACCEPTANCE, 0.98, 1.00 };
186  Double_t red2[] = { 0.90, 0.80, 0.40, 0.00, 0.25, 0.85, 0.95, 1.00, 0.60, 0.31 };
187  Double_t green2[] = { 0.90, 0.80, 0.50, 0.85, 1.00, 0.95, 0.95, 0.20, 0.00, 0.00 };
188  Double_t blue2[] = { 1.00, 1.00, 1.00, 0.85, 0.25, 0.12, 0.05, 0.00, 0.00, 0.00 };
189 
190  ind = TColor::CreateGradientColorTable(NRGBs[2], stops2, red2, green2, blue2, NCont[2]);
191  for (int i = 0; i < NCont[2]; i++)
192  pal[2][i] = ind+i;
193 
194  ArePalettesDefined = true;
195 
196  //BEGIN prepare nice bright color versions for systematic error
197  // TColor * cl[7];
198  for(Int_t i = 0; i < 7; i++) { //5 colors for reaction + NA61 + NA49
199  TColor * ctmp = gROOT->GetColor(REACTION_COLOR[i]);
200  Float_t r, g, b;
201  ctmp->GetRGB(r, g, b);
202 
203  const Double_t intensity = 0.8; //0-1
204  const Double_t brightness = 0.9; //0-1
205  const Double_t mean = (r + g + b)/3;
206 
207 // cout<<"Color check before:"<<i<<" "<<mean<<" "<<r<<" "<<g<<" "<<b<<endl;
208 
209  r = ((1-intensity) * mean + intensity * r) * (1-brightness) + brightness;
210  g = ((1-intensity) * mean + intensity * g) * (1-brightness) + brightness;
211  b = ((1-intensity) * mean + intensity * b) * (1-brightness) + brightness;
212 
213  // cl[i] =
214 new TColor(10001+i, r, g, b, TString::Format("my_color_%d", i));
215 // cout<<"Color check after:"<<cl[i]<<" "<<i<<" "<<mean<<" "<<r<<" "<<g<<" "<<b<<endl;
216  }
217  //END prepare nice bright color versions for systematic error
218  }
219 
220  gStyle->SetNumberContours(NCont[iPalette]);
221  gStyle->SetPalette(NCont[iPalette], pal[iPalette]);
222 }
223 
224 void set_style1D() {
225  gStyle->SetTitleOffset(1.1, "xz");
226  gStyle->SetTitleOffset(1.8, "y");
227  // gStyle->SetLabelOffset(0.005, "xyz");
228 
229  gStyle->SetPadTopMargin(0.05);
230  gStyle->SetPadBottomMargin(0.1);
231  gStyle->SetPadRightMargin(0.05);
232 }
233 
234 void set_style2D() {
235  gStyle->SetTitleOffset(1.1, "x");
236  gStyle->SetTitleOffset(1.4, "y");
237  gStyle->SetTitleOffset(1.2, "z");
238  // gStyle->SetLabelOffset(0.005, "xyz");
239 
240  gStyle->SetPadTopMargin(0.05);
241  gStyle->SetPadBottomMargin(0.15);
242  gStyle->SetPadLeftMargin(0.14);
243  gStyle->SetPadRightMargin(0.2);
244 }
245 
246 void LocaldrawTH2Hacked(TH2 * h1, Double_t min = -0.10, Double_t max = 0.10, float text = false, Bool_t draw_palette = true) {
258  if(text) {
259  gStyle->SetPaintTextFormat(TString::Format(".%df", (int)(text)));
260  }
261 
262 
263  TH2D * h = (TH2D*)(h1->Clone("drawTH2HackedTempH"));
264  TH2D * h2 = (TH2D*)(h1->Clone("drawTH2HackedTempH2"));
265  h2->Reset(); // we will use it only to draw the colour palette
266 
267  const Int_t nLevels = 999;
268  Double_t levels[nLevels];
269  for(int i = 1; i < nLevels; i++) {
270  levels[i] = min + (max - min) / (nLevels - 1) * (i);
271  }
272  levels[0] = -1E6;
273 
274  for(int i = 1; i <= h->GetNbinsX(); ++i) { //push empty bins really low
275  for(int j = 1; j <= h->GetNbinsY(); ++j) {
276  if(h->GetBinContent(i, j) == 0
277  && h->GetBinError(i, j) == 0
278  )
279  h->SetBinContent(i, j, levels[0]-1);
280 
281  h2->SetBinContent(i, j, levels[0]-1); //this is to prevent any possibility of the scatter plot being visible
282  }
283  }
284 
285  h->SetContour((sizeof(levels)/sizeof(Double_t)), levels);
286 
287 
288  //draw colour palette
289 
290  if(draw_palette) {
291  h2->SetContour((sizeof(levels)/sizeof(Double_t)), levels);
292  h2->GetZaxis()->SetRangeUser(min, max); // ... set the range for the palette
293  h2->DrawClone("z"); // draw the "color palette"
294  }
295  h->SetMarkerColor(kWhite); //white scatter plot invisible!
296  h->GetZaxis()->SetRangeUser(levels[0]+1, -levels[0]-1); // ... set the range ...
297 
298  h->DrawClone("col same");
299 
300  if(text) {
301  h->SetMarkerSize(LEGEND_SIZE*15);
302  h->DrawCopy("text same");
303  }
304 
305  h->Draw("sameaxis"); //prevents color bleeding into the axes (doesn't actually)
306 }
307 
308 void style() {/*
312  set_style();
313  set_style2D();
314  TCanvas * c = new TCanvas();
315  TH2D h("h", "", 20, -1, 1, 20, 0, 20);
316  TRandom r(0);
317  for(int i = 0; i < 2000; i++) h.Fill(r.Gaus(0.1, 0.2), r.Poisson(5));
318  h.GetXaxis()->SetNdivisions(505);
319  h.SetXTitle("Gaussian distribution #mu=0.1 #sigma=0.2");
320  h.SetYTitle("Poisson distribution #lambda=5");
321  h.SetZTitle("entries");
322  h.Draw("colz");
323  TLatex tl(0.2, 0.9, "Style test");
324  tl.SetNDC();
325  tl.Draw();
326  c->SaveAs("style.pdf");
327  delete c;
328 
329  set_style(0);
330  set_style2D();
331 
332  c = new TCanvas();
333  for(int i = 0; i < 2000; i++) {
334  h.Fill(r.Gaus(0.5, 0.2), 5+r.Poisson(5), -1);
335  }
336  for(int i = 0; i < 100000; i++) {
337  h.Fill(r.Gaus(0.0, 5), r.Gaus(10, 50), r.Gaus(0, 0.5));
338  }
339  h.SetXTitle("abc");
340  h.SetYTitle("def");
341  h.SetZTitle("entries");
342  LocaldrawTH2Hacked(&h, -60, 60, 0);
343  tl.Draw();
344  c->SaveAs("style2.pdf");
345  delete c;*/
346 }
void set_style1D()
Definition: style.h:474
void set_style2D()
Definition: style.h:461