ra4_macros  bede988c286599a3a84b77a4d788ac0a971e89f9
styles.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 // styles - Class to set default plotting styles, read from a text file
3 //----------------------------------------------------------------------------
4 
5 #ifndef INT_ROOT
6 #include "styles.hpp"
7 #endif
8 
9 #include "TCanvas.h"
10 #include "TFile.h"
11 #include "TH1.h"
12 #include "TPad.h"
13 #include "TString.h"
14 #include "TStyle.h"
15 #include "TLatex.h"
16 #include "TROOT.h"
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 #include <cmath>
21 
22 using std::cout;
23 using std::endl;
24 
25 styles::styles(TString group) {
26  confFile = "txt/plot_styles.txt";
27  TString inames[] = {"nFont", "nDivisions"};
28  int *ivalues[] = {&nFont, &nDivisions};
29  parseStyleFile("General", 0, 0, 0, inames, ivalues, 2);
30  Group = group;
31  nPads = 1;
32 
34 }
35 
38  gStyle->SetCanvasDefW(CanvasW);
39  gStyle->SetCanvasDefH(CanvasH);
40  gStyle->SetTextSize(LegendSize); // Set global text size
41  gStyle->SetTitleFontSize(TitleSize); // Set top title size
42  gStyle->SetTitleSize(LabelSize,"xyz"); // Set the 2 axes title size
43  gStyle->SetLabelSize(LabelSize,"xyz"); // Set the 2 axes label size
44 
45  gStyle->SetTitleOffset(xTitleOffset,"x");
46  gStyle->SetTitleOffset(yTitleOffset,"y");
47  gStyle->SetTitleOffset(zTitleOffset,"z");
48  gStyle->SetPadRightMargin (PadRightMargin);
49  gStyle->SetPadBottomMargin(PadBottomMargin);
50  gStyle->SetPadTopMargin(PadTopMargin);
51  gStyle->SetPadLeftMargin (PadLeftMargin);
52  gStyle->SetNdivisions(nDivisions, "xyz"); // 5 primary ticks and 4 secondary ticks
53 
54  gStyle->SetTitleFont(nFont,"xyz"); // Set the all 2 axes title font
55  gStyle->SetLabelFont(nFont,"xyz"); // Set the all 2 axes label font
56  gStyle->SetTextFont(nFont); // Set global text font
57 }
58 
59 void styles::setHistoStyle(TH1 *h) {
60  h->SetTitleSize(TitleSize,"xyz"); // Set the 2 axes title size
61  h->SetLabelSize(LabelSize,"xyz"); // Set the 2 axes label size
62 
63  h->SetTitleOffset(xTitleOffset,"x");
64  h->SetTitleOffset(yTitleOffset,"y");
65  h->SetTitleOffset(zTitleOffset,"z");
66  h->SetNdivisions(nDivisions, "xyz"); // 5 primary ticks and 4 secondary ticks
67  h->SetTitleFont(nFont,"xyz"); // Set the all 2 axes title font
68  h->SetLabelFont(nFont,"xyz"); // Set the all 2 axes label font
69 }
70 
71 // Set default styles globally.
73  gStyle->SetPalette(1); // Decent colors for 2D plots
74  gStyle->SetOptStat(0); // No Stats box
75  gStyle->SetPadTickX(1); // No ticks at the right
76  gStyle->SetPadTickY(1); // No ticks at the top
77 }
78 
79 void styles::setGroup(TString group){
80  Group = group;
82 }
83 
84 // Set default style for the specific group
86  TString inames[] = {"CanvasW", "CanvasH"};
87  TString fnames[] = {"LegendSize", "TitleSize", "LabelSize", "PadRightMargin", "PadTopMargin", "PadBottomMargin",
88  "xTitleOffset", "PadLeftMargin", "yTitleOffset", "zTitleOffset"};
89  int *ivalues[] = {&CanvasW, &CanvasH};
92  parseStyleFile(Group, fnames, fvalues, 10, inames, ivalues, 2);
93 }
94 
95 // Fix for y axes that have too much/little space for the label due to number of digits
96 void styles::moveYAxisLabel(TH1 *h, float maxi, bool isLog){
97  int digits = static_cast<int>((log(maxi)/log(10.)+0.001)+1);
98  if(digits<2) digits = 2;
99  TString Section = Group;
100  if(isLog)Section += "_Log";
101  else {Section += "_Digits_"; Section += digits; }
102  TString fnames[] = {"PadLeftMargin", "yTitleOffset"};
103  float *fvalues[] = {&PadLeftMargin, &yTitleOffset};
104 
105  parseStyleFile(Section, fnames, fvalues, 2, 0, 0, 0);
106  h->SetTitleOffset(yTitleOffset,"y");
107  //pad->SetLeftMargin(PadLeftMargin);
108 }
109 
110 // Test the global style settings for a generic histogram.
111 void styles::testGlobalStyle(bool fixY, float scale) {
112 
114 
115  TH1* h = new TH1F("h", "h", 50, 0, 50);
116  TH1* hc[6];
117  for (int i=1; i<=50; i++) {
118  double value = scale*exp(-0.5*pow(((i-25.)/5.),2)); // Gaussian shape
119  h->SetBinContent(i, value);
120  }
121 
122  TCanvas c;
123  if(nPads == 2) c.Divide(2);
124  if(nPads == 3) c.Divide(3);
125  if(nPads == 4) c.Divide(2,2);
126  if(nPads == 6) c.Divide(3,2);
127  c.cd(1);
128  h->Draw();
129  if(fixY) moveYAxisLabel(h,100);
130  setTitles(h, "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(10 MeV^{2})");
131  float scales[] = {0.1, 10, 0.01};
132  for(int pads = 2; pads<=4; pads++){
133  if(nPads>=pads){
134  c.cd(pads);
135  hc[pads-2] = static_cast<TH1F*>(h->Clone());
136  hc[pads-2]->Scale(scales[pads-2]);
137  if(fixY) moveYAxisLabel(hc[pads-2],hc[pads-2]->GetMaximum());
138  hc[pads-2]->Draw();
139  setTitles(hc[pads-2], "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(1000 MeV^{2})");
140  }
141  }
142  TString epsName = "babar_code/styles/Plot_"; epsName += nPads; epsName += "Pads.eps";
143  c.Print(epsName);
144 
145 }
146 
148  cout<<"nFont = " << nFont << endl;
149  cout<<"nPads = " << nPads << endl;
150  cout<<"nDivisions = " << nDivisions << endl;
151  cout<<"CanvasW = " << CanvasW << endl;
152  cout<<"CanvasH = " << CanvasH << endl;
153  cout<<"LegendSize = " << LegendSize << endl;
154  cout<<"TitleSize = " << TitleSize << endl;
155  cout<<"LabelSize = " << LabelSize << endl;
156  cout<<"PadRightMargin = " << PadRightMargin << endl;
157  cout<<"PadTopMargin = " << PadTopMargin << endl;
158  cout<<"PadBottomMargin = " << PadBottomMargin << endl;
159  cout<<"xTitleOffset = " << xTitleOffset << endl;
160  cout<<"PadLeftMargin = " << PadLeftMargin << endl;
161  cout<<"yTitleOffset = " << yTitleOffset << endl;
162  cout<<"zTitleOffset = " << zTitleOffset << endl;
163 
164 }
165 
166 // ----------------------------------------------------------------------
167 void styles::setMarkers(TH1 *h, float Msize, int Mstyle) {
168  h->SetMarkerStyle(Mstyle);
169  h->SetMarkerSize(Msize);
170 }
171 
172 // ----------------------------------------------------------------------
173 void styles::setTitles(TH1 *h, TString xTitle, TString yTitle, TString Left, TString Right) {
174  if (0==h) {
175  cout << " Histogram not defined" << endl;
176  } else {
177  h->SetXTitle(xTitle); h->SetYTitle(yTitle);
178  TLatex label; label.SetNDC(kTRUE);
179  label.SetTextSize(0.06);
180  label.SetTextAlign(11);
181  label.DrawLatex(PadLeftMargin,1-PadTopMargin+0.02,Left);
182  label.SetTextAlign(31);
183  label.DrawLatex(1-PadRightMargin,1-PadTopMargin+0.02,Right);
184  }
185 }
186 
187 // ----------------------------------------------------------------------
188 void styles::styleHist(TH1 *h, Int_t color, Int_t fillstyle,
189  Int_t symbol, Double_t size, Int_t width) {
190  h->SetLineColor(color);
191  h->SetLineWidth(width);
192  h->SetMarkerColor(color);
193  h->SetMarkerStyle(symbol);
194  h->SetMarkerSize(size);
195  h->SetStats(kFALSE);
196  h->SetFillStyle(fillstyle);
197  h->SetFillColor(color);
198 }
199 
200 // ----------------------------------------------------------------------
201 void styles::setTitleSizes(TH1 *h, float size, float lsize, int font,
202  float xoff, float yoff, int divisions) {
203  if (0==h) {
204  cout << " Histogram not defined" << endl;
205  } else {
206  h->SetTitleOffset(xoff, "x"); h->SetTitleOffset(yoff, "y");
207  h->SetTitleSize(size, "x"); h->SetTitleSize(size, "y");
208  h->SetLabelSize(lsize, "x"); h->SetLabelSize(lsize, "y");
209  h->SetLabelFont(font, "x"); h->SetLabelFont(font, "y");
210  h->GetXaxis()->SetTitleFont(font); h->GetYaxis()->SetTitleFont(font);
211  h->SetNdivisions(divisions,"X"); h->SetNdivisions(divisions, "Y");
212  }
213 }
214 
215 void styles::parseStyleFile(TString group, TString fnames[], float *fvalues[], int nFloat,
216  TString inames[], int *ivalues[], int nInt){
217  std::ifstream file(confFile);
218  TString word, s_value;
219  while(file >> word){
220  if(word.Contains("[")) {
221  word.ReplaceAll("[",""); word.ReplaceAll("]","");
222  if(word == group){
223  while(file >> word){
224  if(word.Contains("[")) break;
225  file >> s_value; file >> s_value;
226  for(int var(0); var < nFloat; var++){
227  if(word == fnames[var]){
228  *fvalues[var] = s_value.Atof();
229  break;
230  }
231  } // Loop finding requested float variables
232  for(int var(0); var < nInt; var++){
233  if(word == inames[var]){
234  *ivalues[var] = s_value.Atoi();
235  break;
236  }
237  } // Loop finding requested int variables
238  } // Loop over group variables
239  break;
240  }
241  }
242  } // Loop over all words
243 }
244 
int nPads
Definition: styles.hpp:33
void styleHist(TH1 *h, Int_t color=1, Int_t fillstyle=0, Int_t symbol=8, Double_t size=0.7, Int_t width=1)
Definition: styles.cpp:188
void parseStyleFile(TString group, TString fnames[], float *fvalues[], int nFloat, TString inames[], int *ivalues[], int nInt)
Definition: styles.cpp:215
void setDefaultStyle()
Definition: styles.cpp:36
float LabelSize
Definition: styles.hpp:35
float zTitleOffset
Definition: styles.hpp:35
int CanvasW
Definition: styles.hpp:34
float PadTopMargin
Definition: styles.hpp:36
void moveYAxisLabel(TH1 *h, float maxi, bool isLog=false)
Definition: styles.cpp:96
void setTitleSizes(TH1 *h, float size, float lsize, int font=62, float xoff=1., float yoff=1., int divisions=405)
Definition: styles.cpp:201
TString Group
Definition: styles.hpp:32
float LegendSize
Definition: styles.hpp:35
void setGlobalStyle()
Definition: styles.cpp:72
TString confFile
Definition: styles.hpp:32
void setGroup(TString group)
Definition: styles.cpp:79
void readGroupStyle()
Definition: styles.cpp:85
void printValues()
Definition: styles.cpp:147
float TitleSize
Definition: styles.hpp:35
float xTitleOffset
Definition: styles.hpp:35
void setHistoStyle(TH1 *h)
Definition: styles.cpp:59
void setMarkers(TH1 *h, float Msize=0.6, int Mstyle=20)
Definition: styles.cpp:167
int nDivisions
Definition: styles.hpp:33
float yTitleOffset
Definition: styles.hpp:35
void setTitles(TH1 *h, TString xTitle="", TString yTitle="", TString Left="", TString Right="")
Definition: styles.cpp:173
int nFont
Definition: styles.hpp:33
tuple file
Definition: parse_card.py:238
float PadRightMargin
Definition: styles.hpp:36
float PadLeftMargin
Definition: styles.hpp:36
int CanvasH
Definition: styles.hpp:34
void testGlobalStyle(bool fixY=true, float scale=1000.)
Definition: styles.cpp:111
float PadBottomMargin
Definition: styles.hpp:36
styles(TString group="Standard")
Definition: styles.cpp:25