CMS 3D CMS Logo

TkAlStyle.cc
Go to the documentation of this file.
1 // Common style file for TkAl public plots
2 // --------------------------------------------------------------
3 //
4 // Defines a set of functions to define the plotting style and
5 // provide commonly used style objects such as histogram title
6 // and legends. Can be used in compiled code and uncompiled scripts.
7 //
8 // Always call TkAlStyle::set(PublicationStatus) in the beginning
9 // of your plotting script to adjust the gStyle options. Also,
10 // the behaviour of some other methods of this class depend on
11 // this, e.g. the histogram title displays "CMS preliminary" etc.
12 // depending on the PublicationStatus. Call TkAlStyle::set before
13 // declaring any histograms (or call TH1::UseCurrentStyle()) to
14 // make sure the style settings are used.
15 // --------------------------------------------------------------
16 
17 #ifndef TKAL_STYLE_CC
18 #define TKAL_STYLE_CC
19 
20 #include "TColor.h"
21 #include "TError.h"
22 #include "TLegend.h"
23 #include "TPaveText.h"
24 #include "TString.h"
25 #include "TStyle.h"
26 #include <iostream>
27 
28 
29 // Publication status: determines what is plotted in title
32  TString str = "";
33  if( status == NO_STATUS ) str = "Status not set yet!";
34  else if( status == INTERNAL ) str = "internal";
35  else if( status == INTERNAL_SIMULATION ) str = "simulation (internal)";
36  else if( status == PRELIMINARY ) str = "preliminary";
37  else if( status == PUBLIC ) str = "public";
38  else if( status == SIMULATION ) str = "simulation (public)";
39  else if( status == UNPUBLISHED ) str = "unpublished";
40  else if( status == CUSTOM ) str = "custom title set";
41 
42  return str;
43 }
44 
45 
46 // Data era: determines labels of data-taking periods, e.g. CRUZET
48 static TString toTString(const Era era) {
49  TString str = "";
50  if( era == CRUZET15 ) str = "0T cosmic ray data 2015";
51  else if( era == CRAFT15 ) str = "3.8T cosmic ray data 2015";
52  else if( era == COLL0T15 ) str = "0T collision data 2015";
53 
54  return str;
55 }
56 
57 
58 class TkAlStyle {
59 public:
60  // Adjusts the gStyle settings and store the PublicationStatus
61  static void set(const PublicationStatus status, const Era era = NONE, const TString customTitle = "", const TString customRightTitle = "");
62  static void set(const TString customTitle);
64 
65  // Draws a title "<CMS label> 2015" on the current pad
66  // dependending on the PublicationStatus
67  // INTERNAL : no extra label (intended for AN-only plots with data)
68  // INTERNAL : show "Simulation" label (intended for AN-only plots, no "CMS")
69  // PRELIMINARY : show "CMS preliminary 2015" label
70  // PUBLIC : show "CMS 2015" label
71  // SIMULATION : show "CMS Simulation" label
72  // UNPUBLISHED : show "CMS (unpublished)" label (intended for additional material on TWiki)
73  // Note that this method does not allow for easy memory
74  // handling. For that, use standardTitle().
75  static void drawStandardTitle() { standardTitle()->Draw("same"); standardRightTitle()->Draw("same"); }
76  static void drawStandardTitle(const Era era) { standardTitle()->Draw("same"); standardRightTitle(era)->Draw("same"); }
77 
78  // Returns a TPaveText object that fits as a histogram title
79  // with the current pad dimensions.
80  // It has the same text as described in drawStandardTitle().
81  // The idea of this method is that one has control over the
82  // TPaveText object and can do proper memory handling.
83  static TPaveText* standardTitle(PublicationStatus status) {
84  return title(header(status));
85  }
86  static TPaveText* standardTitle() {
88  }
89 
90  static TPaveText* standardRightTitle(const Era era) {
91  return righttitle(rightheader(era));
92  }
93  static TPaveText* standardRightTitle() {
94  return standardRightTitle(era_);
95  }
96 
97  // Returns a TPaveText object that fits as a histogram title
98  // with the current pad dimensions and displays the specified text txt.
99  static TPaveText* customTitle(const TString& txt) { return title(txt); }
100  static TPaveText* customRightTitle(const TString& txt) { return righttitle(txt); }
101 
102  static TString legendheader;
103  static TString legendoptions;
104  static double textSize;
105  // Returns a TLegend object that fits into the top-right corner
106  // of the current pad. Its width, relative to the pad size (without
107  // margins), can be specified. Its height is optimized for nEntries
108  // entries.
109  static TLegend* legend(const int nEntries, const double relWidth=0.5) {
110  return legendTR(nEntries,relWidth);
111  }
112  static TLegend* legend(TString position, const int nEntries, const double relWidth=0.5) {
113  position.ToLower();
114  if( !( position.Contains("top") || position.Contains("bottom") ) )
115  position += "top";
116  if( !( position.Contains("left") || position.Contains("right") ) )
117  position += "right";
118  TLegend* leg = nullptr;
119  if( position.Contains("top") && position.Contains("right") ) {
120  leg = legendTR(nEntries,relWidth);
121  } else if( position.Contains("top") && position.Contains("left") ) {
122  leg = legendTL(nEntries,relWidth);
123  } else if( position.Contains("bottom") && position.Contains("right") ) {
124  leg = legendBR(nEntries,relWidth);
125  } else if( position.Contains("bottom") && position.Contains("left") ) {
126  leg = legendBL(nEntries,relWidth);
127  } else {
128  leg = legendTR(nEntries,relWidth);
129  }
130 
131  return leg;
132  }
133  // Same but explicitly state position on pad
134  static TLegend* legendTL(const int nEntries, const double relWidth=0.5) {
135  return legend(nEntries,relWidth,true,true);
136  }
137  static TLegend* legendTR(const int nEntries, const double relWidth=0.5) {
138  return legend(nEntries,relWidth,false,true);
139  }
140  static TLegend* legendBL(const int nEntries, const double relWidth=0.5) {
141  return legend(nEntries,relWidth,true,false);
142  }
143  static TLegend* legendBR(const int nEntries, const double relWidth=0.5) {
144  return legend(nEntries,relWidth,false,false);
145  }
146 
147 
148  // Returns a TPaveText object that fits into the top-right corner
149  // of the current pad and that can be used for additional labels.
150  // Its width, relative to the pad size (without margins), can be
151  // specified. Its height is optimized for nEntries entries.
152  static TPaveText* label(const int nEntries, const double relWidth=0.5) {
153  return labelTR(nEntries,relWidth);
154  }
155 
156  static TPaveText* label(TString position, const int nEntries, const double relWidth=0.5) {
157  position.ToLower();
158  if( !( position.Contains("top") || position.Contains("bottom") ) )
159  position += "top";
160  if( !( position.Contains("left") || position.Contains("right") ) )
161  position += "right";
162  TPaveText* label = nullptr;
163  if( position.Contains("top") && position.Contains("right") ) {
164  label = labelTR(nEntries,relWidth);
165  } else if( position.Contains("top") && position.Contains("left") ) {
166  label = labelTL(nEntries,relWidth);
167  } else if( position.Contains("bottom") && position.Contains("right") ) {
168  label = labelBR(nEntries,relWidth);
169  } else if( position.Contains("bottom") && position.Contains("left") ) {
170  label = labelBL(nEntries,relWidth);
171  } else {
172  label = labelTR(nEntries,relWidth);
173  }
174 
175  return label;
176  }
177 
178  // Same but explicitly state position on pad
179  static TPaveText* labelTL(const int nEntries, const double relWidth=0.5) {
180  return label(nEntries,relWidth,true,true);
181  }
182  static TPaveText* labelTR(const int nEntries, const double relWidth=0.5) {
183  return label(nEntries,relWidth,false,true);
184  }
185  static TPaveText* labelBL(const int nEntries, const double relWidth=0.5) {
186  return label(nEntries,relWidth,true,false);
187  }
188  static TPaveText* labelBR(const int nEntries, const double relWidth=0.5) {
189  return label(nEntries,relWidth,false,false);
190  }
191 
192 
193  static double lineHeight() { return lineHeight_; }
194 
195 
196 private:
198  static Era era_;
199  static TString customTitle_;
200  static TString customRightTitle_;
201  static double lineHeight_;
202  static double margin_;
203 
204  // creates a title
205  static TString applyCMS(const TString& txt);
206  static TPaveText* title(const TString& txt);
207  static TPaveText* righttitle(const TString& txt);
208 
209  // returns the standard-title (CMS label 2015) depending
210  // on the PublicationStatus
211  static TString header(const PublicationStatus status);
212  static TString rightheader(const Era era);
213 
214  // NDC coordinates for TPave, TLegend,...
215  static void setXCoordinatesL(const double relWidth, double& x0, double& x1);
216  static void setXCoordinatesR(const double relWidth, double& x0, double& x1);
217  static void setYCoordinatesT(const int nEntries, double& y0, double& y1);
218  static void setYCoordinatesB(const int nEntries, double& y0, double& y1);
219 
220  static TLegend* legend(const int nEntries, const double relWidth, const bool left, const bool top);
221  static TPaveText* label(const int nEntries, const double relWidth, const bool leftt, const bool top);
222 };
223 
226 TString TkAlStyle::legendheader = "";
227 TString TkAlStyle::legendoptions = "all";
228 TString TkAlStyle::customTitle_ = "";
229 TString TkAlStyle::customRightTitle_ = "";
230 double TkAlStyle::lineHeight_ = 0.042;
231 double TkAlStyle::margin_ = 0.04;
232 double TkAlStyle::textSize = 0.035;
233 
234 
235 // --------------------------------------------------------------
236 void TkAlStyle::setXCoordinatesL(const double relWidth, double& x0, double& x1) {
237  x0 = gStyle->GetPadLeftMargin()+margin_;
238  x1 = x0 + relWidth*(1.-gStyle->GetPadLeftMargin()-gStyle->GetPadRightMargin()-2.*margin_);
239 }
240 
241 
242 // --------------------------------------------------------------
243 void TkAlStyle::setXCoordinatesR(const double relWidth, double& x0, double& x1) {
244  x0 = 1.-gStyle->GetPadRightMargin()-margin_-relWidth*(1.-gStyle->GetPadLeftMargin()-gStyle->GetPadRightMargin()-2.*margin_);
245  x1 = 1.-gStyle->GetPadRightMargin()-margin_;
246 }
247 
248 
249 // --------------------------------------------------------------
250 void TkAlStyle::setYCoordinatesT(const int nEntries, double& y0, double& y1) {
251  y1 = 1.-gStyle->GetPadTopMargin()-margin_;
252  y0 = y1-nEntries*lineHeight_;
253 }
254 
255 
256 // --------------------------------------------------------------
257 void TkAlStyle::setYCoordinatesB(const int nEntries, double& y0, double& y1) {
258  y1 = gStyle->GetPadBottomMargin()+margin_;
259  y0 = y1+nEntries*lineHeight_;
260 }
261 
262 
263 // --------------------------------------------------------------
264 TLegend* TkAlStyle::legend(const int nEntries, const double relWidth, const bool left, const bool top) {
265  double x0 = 0.;
266  double x1 = 0.;
267  double y0 = 0.;
268  double y1 = 0.;
269  bool hasheader = (TkAlStyle::legendheader != "");
270  if( left ) setXCoordinatesL(relWidth, x0,x1);
271  else setXCoordinatesR(relWidth, x0,x1);
272  if( top ) setYCoordinatesT(nEntries+hasheader,y0,y1);
273  else setYCoordinatesB(nEntries+hasheader,y0,y1);
274 
275  TLegend* leg = new TLegend(x0,y0,x1,y1);
276  leg->SetBorderSize(0);
277  leg->SetFillColor(0);
278  leg->SetFillStyle(0);
279  leg->SetTextFont(42);
280  leg->SetTextSize(textSize);
281  if (hasheader) leg->SetHeader(TkAlStyle::legendheader);
282 
283  return leg;
284 }
285 
286 
287 // --------------------------------------------------------------
288 TPaveText* TkAlStyle::label(const int nEntries, const double relWidth, const bool left, const bool top) {
289  double x0 = 0.;
290  double x1 = 0.;
291  double y0 = 0.;
292  double y1 = 0.;
293  if( left ) setXCoordinatesL(relWidth,x0,x1);
294  else setXCoordinatesR(relWidth,x0,x1);
295  if( top ) setYCoordinatesT(nEntries,y0,y1);
296  else setYCoordinatesB(nEntries,y0,y1);
297 
298  TPaveText* label = new TPaveText(x0,y0,x1,y1,"NDC");
299  label->SetBorderSize(0);
300  label->SetFillColor(0);
301  label->SetFillStyle(0);
302  label->SetTextFont(42);
303  label->SetTextAlign(12); // left adjusted and vertically centered
304  label->SetTextSize(textSize);
305  label->SetMargin(0.);
306 
307  return label;
308 }
309 
310 // --------------------------------------------------------------
311 //unfortunately no #definecommand in TLatex...
312 //#CMS{text} gives CMS in big bold, text in italics
313 //#CMS with no "argument" just gives CMS in big bold
314 //#noCMS{text} gives text in italics
315 TString TkAlStyle::applyCMS(const TString& txt) {
316  TString newtxt = txt;
317  newtxt.ReplaceAll("#CMS{","#scale[1.4]{#font[61]{CMS}} #font[52]{");
318  newtxt.ReplaceAll("#noCMS{","#font[52]{");
319  newtxt.ReplaceAll("#CMS","#scale[1.4]{#font[61]{CMS}}");
320  return newtxt;
321 }
322 
323 
324 // --------------------------------------------------------------
325 TPaveText* TkAlStyle::title(const TString& txt) {
326  double x0 = gStyle->GetPadLeftMargin();
327  double x1 = 1.-gStyle->GetPadRightMargin();
328  double y0 = 1.-gStyle->GetPadTopMargin();
329  double y1 = 1.;
330  if (txt.Contains("#CMS")) y0 += .02;
331  TPaveText* theTitle = new TPaveText(x0,y0,x1,y1,"NDC");
332  theTitle->SetBorderSize(0);
333  theTitle->SetFillColor(10);
334  theTitle->SetFillStyle(0);
335  theTitle->SetTextFont(42);
336  theTitle->SetTextAlign(13); // left bottom adjusted
337  theTitle->SetTextSize(0.038);
338  theTitle->SetMargin(0.);
339  theTitle->AddText(applyCMS(txt));
340 
341  return theTitle;
342 }
343 
344 
345 // --------------------------------------------------------------
346 TPaveText* TkAlStyle::righttitle(const TString& txt) {
347  TString newtxt = applyCMS(txt);
348  double x0 = gStyle->GetPadLeftMargin();
349  double x1 = 1.-gStyle->GetPadRightMargin();
350  double y0 = 1.-gStyle->GetPadTopMargin();
351  double y1 = 1.;
352  TPaveText* theTitle = new TPaveText(x0,y0,x1,y1,"NDC");
353  theTitle->SetBorderSize(0);
354  theTitle->SetFillColor(10);
355  theTitle->SetFillStyle(0);
356  theTitle->SetTextFont(42);
357  theTitle->SetTextAlign(33); // right bottom adjusted
358  theTitle->SetTextSize(0.038);
359  theTitle->SetMargin(0.);
360  theTitle->AddText(newtxt);
361 
362  return theTitle;
363 }
364 
365 
366 // --------------------------------------------------------------
368  TString txt;
369  if( status == NO_STATUS ) {
370  std::cout << "Status not set yet! Can't draw the title!" << std::endl;
371  } else if( status == INTERNAL_SIMULATION ) {
372  txt = "#noCMS{Simulation}";
373  } else if( status == PRELIMINARY ) {
374  txt = "#CMS{Preliminary}";
375  } else if( status == PUBLIC ) {
376  txt = "#CMS";
377  } else if( status == SIMULATION ) {
378  txt = "#CMS{Simulation}";
379  } else if( status == UNPUBLISHED ) {
380  txt = "#CMS{(unpublished)}";
381  } else if( status == CUSTOM ) {
382  txt = customTitle_;
383  }
384 
385  return txt;
386 }
387 
388 TString TkAlStyle::rightheader(const Era era)
389 {
390  TString txt = "";
391  if( era != NONE ) {
392  txt = toTString(era);
393  } else {
394  txt = customRightTitle_;
395  }
396  return txt;
397 }
398 
399 
400 // --------------------------------------------------------------
401 void TkAlStyle::set(const PublicationStatus status, const Era era, const TString customTitle, const TString customRightTitle) {
402  // Store the PublicationStatus for later usage, e.g. in the title
406  era_ = era;
407  if (publicationStatus_ == CUSTOM && customTitle_ == "")
408  std::cout << "Error: you are trying to use a custom title, but you don't provide it" << std::endl;
409  if (publicationStatus_ != CUSTOM && customTitle_ != "")
410  std::cout << "Error: you provide a custom title, but you don't indicate CUSTOM status. Your title will not be used." << std::endl;
411 
412  // Suppress message when canvas has been saved
413  gErrorIgnoreLevel = 1001;
414 
415  // Zero horizontal error bars
416  gStyle->SetErrorX(0);
417 
418  // For the canvas
419  gStyle->SetCanvasBorderMode(0);
420  gStyle->SetCanvasColor(kWhite);
421  gStyle->SetCanvasDefH(800); //Height of canvas
422  gStyle->SetCanvasDefW(800); //Width of canvas
423  gStyle->SetCanvasDefX(0); //Position on screen
424  gStyle->SetCanvasDefY(0);
425 
426  // For the frame
427  gStyle->SetFrameBorderMode(0);
428  gStyle->SetFrameBorderSize(10);
429  gStyle->SetFrameFillColor(kBlack);
430  gStyle->SetFrameFillStyle(0);
431  gStyle->SetFrameLineColor(kBlack);
432  gStyle->SetFrameLineStyle(0);
433  gStyle->SetFrameLineWidth(2);
434  gStyle->SetLineWidth(3);
435 
436  // For the Pad
437  gStyle->SetPadBorderMode(0);
438  gStyle->SetPadColor(kWhite);
439  gStyle->SetPadGridX(false);
440  gStyle->SetPadGridY(false);
441  gStyle->SetGridColor(0);
442  gStyle->SetGridStyle(3);
443  gStyle->SetGridWidth(1);
444 
445  // Margins
446  gStyle->SetPadTopMargin(0.08);
447  gStyle->SetPadBottomMargin(0.13);
448  gStyle->SetPadLeftMargin(0.16);
449  gStyle->SetPadRightMargin(0.05);
450 
451  // For the histo:
452  gStyle->SetHistLineColor(kBlack);
453  gStyle->SetHistLineStyle(0);
454  gStyle->SetHistLineWidth(3);
455  gStyle->SetMarkerSize(0.8);
456  gStyle->SetEndErrorSize(4);
457  gStyle->SetHatchesLineWidth(1);
458 
459  // For the statistics box:
460  gStyle->SetOptStat(0);
461 
462  // For the axis
463  gStyle->SetAxisColor(1,"XYZ");
464  gStyle->SetTickLength(0.03,"XYZ");
465  gStyle->SetNdivisions(510,"XYZ");
466  gStyle->SetPadTickX(1);
467  gStyle->SetPadTickY(1);
468  gStyle->SetStripDecimals(kFALSE);
469 
470  // For the axis labels and titles
471  gStyle->SetTitleColor(1,"XYZ");
472  gStyle->SetLabelColor(1,"XYZ");
473  gStyle->SetLabelFont(42,"XYZ");
474  gStyle->SetLabelOffset(0.007,"XYZ");
475  gStyle->SetLabelSize(0.04,"XYZ");
476  gStyle->SetTitleFont(42,"XYZ");
477  gStyle->SetTitleSize(0.047,"XYZ");
478  gStyle->SetTitleXOffset(1.2);
479  gStyle->SetTitleYOffset(1.5);
480 
481  // For the legend
482  gStyle->SetLegendBorderSize(0);
483 }
484 
485 void TkAlStyle::set(const TString customTitle)
486 {
487  set(CUSTOM, NONE, customTitle);
488 }
489 
490 #endif
static TString customTitle_
Definition: TkAlStyle.cc:199
static void setYCoordinatesB(const int nEntries, double &y0, double &y1)
Definition: TkAlStyle.cc:257
static TString applyCMS(const TString &txt)
Definition: TkAlStyle.cc:315
static TPaveText * customTitle(const TString &txt)
Definition: TkAlStyle.cc:99
static TPaveText * label(TString position, const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:156
static PublicationStatus status()
Definition: TkAlStyle.cc:63
static Era era_
Definition: TkAlStyle.cc:198
static double textSize
Definition: TkAlStyle.cc:104
static TString customRightTitle_
Definition: TkAlStyle.cc:200
static TPaveText * standardRightTitle()
Definition: TkAlStyle.cc:93
static double lineHeight_
Definition: TkAlStyle.cc:201
static void drawStandardTitle(const Era era)
Definition: TkAlStyle.cc:76
static void set(const PublicationStatus status, const Era era=NONE, const TString customTitle="", const TString customRightTitle="")
Definition: TkAlStyle.cc:401
static TPaveText * title(const TString &txt)
Definition: TkAlStyle.cc:325
static void setYCoordinatesT(const int nEntries, double &y0, double &y1)
Definition: TkAlStyle.cc:250
static PublicationStatus publicationStatus_
Definition: TkAlStyle.cc:197
PublicationStatus
Definition: TkAlStyle.cc:30
static void drawStandardTitle()
Definition: TkAlStyle.cc:75
TString toTString(const PublicationStatus status)
Definition: TkAlStyle.cc:31
static TPaveText * standardTitle()
Definition: TkAlStyle.cc:86
static TString legendheader
Definition: TkAlStyle.cc:102
static TPaveText * standardRightTitle(const Era era)
Definition: TkAlStyle.cc:90
static TPaveText * customRightTitle(const TString &txt)
Definition: TkAlStyle.cc:100
static void setXCoordinatesL(const double relWidth, double &x0, double &x1)
Definition: TkAlStyle.cc:236
static TPaveText * labelBR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:188
static TLegend * legendBR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:143
static double margin_
Definition: TkAlStyle.cc:202
Era
Definition: TkAlStyle.cc:47
static double lineHeight()
Definition: TkAlStyle.cc:193
static TPaveText * righttitle(const TString &txt)
Definition: TkAlStyle.cc:346
static TPaveText * labelBL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:185
static TPaveText * labelTR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:182
static TString legendoptions
Definition: TkAlStyle.cc:103
static TLegend * legendBL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:140
gErrorIgnoreLevel
Definition: utils.py:27
static TString rightheader(const Era era)
Definition: TkAlStyle.cc:388
static TLegend * legendTR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:137
static TPaveText * label(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:152
static int position[264][3]
Definition: ReadPGInfo.cc:289
static TLegend * legend(TString position, const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:112
#define str(s)
static void setXCoordinatesR(const double relWidth, double &x0, double &x1)
Definition: TkAlStyle.cc:243
static TPaveText * labelTL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:179
static TLegend * legend(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:109
static TLegend * legendTL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.cc:134
static TPaveText * standardTitle(PublicationStatus status)
Definition: TkAlStyle.cc:83
static TString header(const PublicationStatus status)
Definition: TkAlStyle.cc:367