CMS 3D CMS Logo

TkAlStyle.h
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 ALIGNMENT_OFFLINEVALIDATION_TKAL_STYLE_H
18 #define ALIGNMENT_OFFLINEVALIDATION_TKAL_STYLE_H
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 #include <string>
28 #include <algorithm>
29 
30 // Publication status: determines what is plotted in title
40 };
41 
42 // Data era: determines labels of data-taking periods, e.g. CRUZET
44 
45 class TkAlStyle {
46 public:
47  // Adjusts the gStyle settings and store the PublicationStatus
48  static void set(const PublicationStatus status,
49  const Era era = NONE,
50  const TString customTitle = "",
51  const TString customRightTitle = "");
52  static void set(const TString customTitle);
54 
55  // Draws a title "<CMS label> 2015" on the current pad
56  // dependending on the PublicationStatus
57  // INTERNAL : no extra label (intended for AN-only plots with data)
58  // INTERNAL : show "Simulation" label (intended for AN-only plots, no "CMS")
59  // PRELIMINARY : show "CMS preliminary 2015" label
60  // PUBLIC : show "CMS 2015" label
61  // SIMULATION : show "CMS Simulation" label
62  // UNPUBLISHED : show "CMS (unpublished)" label (intended for additional material on TWiki)
63  // Note that this method does not allow for easy memory
64  // handling. For that, use standardTitle().
65  static void drawStandardTitle() {
66  standardTitle()->Draw("same");
67  standardRightTitle()->Draw("same");
68  }
69  static void drawStandardTitle(const Era era) {
70  standardTitle()->Draw("same");
71  standardRightTitle(era)->Draw("same");
72  }
73  static PublicationStatus toStatus(std::string _status);
74 
75  // Returns a TPaveText object that fits as a histogram title
76  // with the current pad dimensions.
77  // It has the same text as described in drawStandardTitle().
78  // The idea of this method is that one has control over the
79  // TPaveText object and can do proper memory handling.
80  static TPaveText* standardTitle(PublicationStatus status) { return title(header(status)); }
81  static TPaveText* standardTitle() { return standardTitle(publicationStatus_); }
82 
83  static TPaveText* standardRightTitle(const Era era) { return righttitle(rightheader(era)); }
84  static TPaveText* standardRightTitle() { return standardRightTitle(era_); }
85 
86  // Returns a TPaveText object that fits as a histogram title
87  // with the current pad dimensions and displays the specified text txt.
88  static TPaveText* customTitle(const TString& txt) { return title(txt); }
89  static TPaveText* customRightTitle(const TString& txt) { return righttitle(txt); }
90 
91  static TString legendheader;
92  static TString legendoptions;
93  static double textSize;
94  // Returns a TLegend object that fits into the top-right corner
95  // of the current pad. Its width, relative to the pad size (without
96  // margins), can be specified. Its height is optimized for nEntries
97  // entries.
98  static TLegend* legend(const int nEntries, const double relWidth = 0.5) { return legendTR(nEntries, relWidth); }
99  static TLegend* legend(TString position, const int nEntries, const double relWidth = 0.5) {
100  position.ToLower();
101  if (!(position.Contains("top") || position.Contains("bottom")))
102  position += "top";
103  if (!(position.Contains("left") || position.Contains("right")))
104  position += "right";
105  TLegend* leg = nullptr;
106  if (position.Contains("top") && position.Contains("right")) {
107  leg = legendTR(nEntries, relWidth);
108  } else if (position.Contains("top") && position.Contains("left")) {
109  leg = legendTL(nEntries, relWidth);
110  } else if (position.Contains("bottom") && position.Contains("right")) {
111  leg = legendBR(nEntries, relWidth);
112  } else if (position.Contains("bottom") && position.Contains("left")) {
113  leg = legendBL(nEntries, relWidth);
114  } else {
115  leg = legendTR(nEntries, relWidth);
116  }
117 
118  return leg;
119  }
120  // Same but explicitly state position on pad
121  static TLegend* legendTL(const int nEntries, const double relWidth = 0.5) {
122  return legend(nEntries, relWidth, true, true);
123  }
124  static TLegend* legendTR(const int nEntries, const double relWidth = 0.5) {
125  return legend(nEntries, relWidth, false, true);
126  }
127  static TLegend* legendBL(const int nEntries, const double relWidth = 0.5) {
128  return legend(nEntries, relWidth, true, false);
129  }
130  static TLegend* legendBR(const int nEntries, const double relWidth = 0.5) {
131  return legend(nEntries, relWidth, false, false);
132  }
133 
134  // Returns a TPaveText object that fits into the top-right corner
135  // of the current pad and that can be used for additional labels.
136  // Its width, relative to the pad size (without margins), can be
137  // specified. Its height is optimized for nEntries entries.
138  static TPaveText* label(const int nEntries, const double relWidth = 0.5) { return labelTR(nEntries, relWidth); }
139 
140  static TPaveText* label(TString position, const int nEntries, const double relWidth = 0.5) {
141  position.ToLower();
142  if (!(position.Contains("top") || position.Contains("bottom")))
143  position += "top";
144  if (!(position.Contains("left") || position.Contains("right")))
145  position += "right";
146  TPaveText* label = nullptr;
147  if (position.Contains("top") && position.Contains("right")) {
148  label = labelTR(nEntries, relWidth);
149  } else if (position.Contains("top") && position.Contains("left")) {
150  label = labelTL(nEntries, relWidth);
151  } else if (position.Contains("bottom") && position.Contains("right")) {
152  label = labelBR(nEntries, relWidth);
153  } else if (position.Contains("bottom") && position.Contains("left")) {
154  label = labelBL(nEntries, relWidth);
155  } else {
156  label = labelTR(nEntries, relWidth);
157  }
158 
159  return label;
160  }
161 
162  // Same but explicitly state position on pad
163  static TPaveText* labelTL(const int nEntries, const double relWidth = 0.5) {
164  return label(nEntries, relWidth, true, true);
165  }
166  static TPaveText* labelTR(const int nEntries, const double relWidth = 0.5) {
167  return label(nEntries, relWidth, false, true);
168  }
169  static TPaveText* labelBL(const int nEntries, const double relWidth = 0.5) {
170  return label(nEntries, relWidth, true, false);
171  }
172  static TPaveText* labelBR(const int nEntries, const double relWidth = 0.5) {
173  return label(nEntries, relWidth, false, false);
174  }
175 
176  static double lineHeight() { return lineHeight_; }
177 
178 private:
180  static Era era_;
181  static TString customTitle_;
182  static TString customRightTitle_;
183  static double lineHeight_;
184  static double margin_;
185 
186  // creates a title
187  static TString applyCMS(const TString& txt);
188  static TPaveText* title(const TString& txt);
189  static TPaveText* righttitle(const TString& txt);
190 
191  // returns the standard-title (CMS label 2015) depending
192  // on the PublicationStatus
193  static TString header(const PublicationStatus status);
194  static TString rightheader(const Era era);
195 
196  // NDC coordinates for TPave, TLegend,...
197  static void setXCoordinatesL(const double relWidth, double& x0, double& x1);
198  static void setXCoordinatesR(const double relWidth, double& x0, double& x1);
199  static void setYCoordinatesT(const int nEntries, double& y0, double& y1);
200  static void setYCoordinatesB(const int nEntries, double& y0, double& y1);
201 
202  static TLegend* legend(const int nEntries, const double relWidth, const bool left, const bool top);
203  static TPaveText* label(const int nEntries, const double relWidth, const bool leftt, const bool top);
204 };
205 
206 #endif
static TString customTitle_
Definition: TkAlStyle.h:181
static void setYCoordinatesB(const int nEntries, double &y0, double &y1)
Definition: TkAlStyle.cc:91
static TString applyCMS(const TString &txt)
Definition: TkAlStyle.cc:156
static TPaveText * customTitle(const TString &txt)
Definition: TkAlStyle.h:88
static TPaveText * label(TString position, const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:140
static PublicationStatus status()
Definition: TkAlStyle.h:53
static Era era_
Definition: TkAlStyle.h:180
Era
Definition: TkAlStyle.h:43
static double textSize
Definition: TkAlStyle.h:93
static TString customRightTitle_
Definition: TkAlStyle.h:182
static TPaveText * standardRightTitle()
Definition: TkAlStyle.h:84
static double lineHeight_
Definition: TkAlStyle.h:183
static void drawStandardTitle(const Era era)
Definition: TkAlStyle.h:69
static TPaveText * title(const TString &txt)
Definition: TkAlStyle.cc:165
static void setYCoordinatesT(const int nEntries, double &y0, double &y1)
Definition: TkAlStyle.cc:85
static PublicationStatus publicationStatus_
Definition: TkAlStyle.h:179
PublicationStatus
Definition: TkAlStyle.h:31
static void drawStandardTitle()
Definition: TkAlStyle.h:65
static TPaveText * standardTitle()
Definition: TkAlStyle.h:81
static TString legendheader
Definition: TkAlStyle.h:91
static TPaveText * standardRightTitle(const Era era)
Definition: TkAlStyle.h:83
static TPaveText * customRightTitle(const TString &txt)
Definition: TkAlStyle.h:89
static void setXCoordinatesL(const double relWidth, double &x0, double &x1)
Definition: TkAlStyle.cc:72
static TPaveText * labelBR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:172
static TLegend * legendBR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:130
static double margin_
Definition: TkAlStyle.h:184
static double lineHeight()
Definition: TkAlStyle.h:176
static TPaveText * righttitle(const TString &txt)
Definition: TkAlStyle.cc:186
static TPaveText * labelBL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:169
static TPaveText * labelTR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:166
static TString legendoptions
Definition: TkAlStyle.h:92
static TLegend * legendBL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:127
static TString rightheader(const Era era)
Definition: TkAlStyle.cc:229
static TLegend * legendTR(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:124
static TPaveText * label(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:138
static int position[264][3]
Definition: ReadPGInfo.cc:289
static TLegend * legend(TString position, const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:99
Definition: TkAlStyle.h:43
static void setXCoordinatesR(const double relWidth, double &x0, double &x1)
Definition: TkAlStyle.cc:78
static PublicationStatus toStatus(std::string _status)
Definition: TkAlStyle.cc:48
static TPaveText * labelTL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:163
static TLegend * legend(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:98
static TLegend * legendTL(const int nEntries, const double relWidth=0.5)
Definition: TkAlStyle.h:121
static TPaveText * standardTitle(PublicationStatus status)
Definition: TkAlStyle.h:80
static TString header(const PublicationStatus status)
Definition: TkAlStyle.cc:206