susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
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(TextSize); // Set global text size
41  gStyle->SetTitleFontSize(TitleSize);
42  gStyle->SetTitleSize(TitleSize,"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(0); // No ticks at the right
76  gStyle->SetPadTickY(0); // 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[] = {"TextSize", "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::fixYAxis(TH1 *h, TPad *pad){
97  float maxi = h->GetMaximum()*1.15;
98  int digits = static_cast<int>((log(maxi)/log(10.)+0.001)+1);
99  if(digits<2) digits = 2;
100  TString Section = Group; Section += "_Digits_"; Section += digits;
101  TString fnames[] = {"PadLeftMargin", "yTitleOffset"};
102  float *fvalues[] = {&PadLeftMargin, &yTitleOffset};
103  parseStyleFile(Section, fnames, fvalues, 2, 0, 0, 0);
104 
105  h->SetTitleOffset(yTitleOffset,"y");
106  pad->SetLeftMargin(PadLeftMargin);
107 }
108 
109 // Test the global style settings for a generic histogram.
110 void styles::testGlobalStyle(bool fixY, float scale) {
111 
113 
114  TH1* h = new TH1F("h", "h", 50, 0, 50);
115  TH1* hc[6];
116  for (int i=1; i<=50; i++) {
117  double value = scale*exp(-0.5*pow(((i-25.)/5.),2)); // Gaussian shape
118  h->SetBinContent(i, value);
119  }
120 
121  TCanvas c;
122  if(nPads == 2) c.Divide(2);
123  if(nPads == 3) c.Divide(3);
124  if(nPads == 4) c.Divide(2,2);
125  if(nPads == 6) c.Divide(3,2);
126  TPad *cPad = static_cast<TPad *>(c.cd(1)); h->Draw();
127  if(fixY) fixYAxis(h,cPad);
128  setTitles(h, "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(10 MeV^{2})");
129  float scales[] = {0.1, 10, 0.01};
130  for(int pads = 2; pads<=4; pads++){
131  if(nPads>=pads){
132  cPad = static_cast<TPad*>(c.cd(pads));
133  hc[pads-2] = static_cast<TH1F*>(h->Clone());
134  hc[pads-2]->Scale(scales[pads-2]);
135  if(fixY) fixYAxis(hc[pads-2],cPad);
136  hc[pads-2]->Draw();
137  setTitles(hc[pads-2], "D^{(*)0/+} channels", "xlabel^{2}_{miss} (GeV^{2})", "Events/(1000 MeV^{2})");
138  }
139  }
140  TString epsName = "babar_code/styles/Plot_"; epsName += nPads; epsName += "Pads.eps";
141  c.Print(epsName);
142 
143 }
144 
146  cout<<"nFont = " << nFont << endl;
147  cout<<"nPads = " << nPads << endl;
148  cout<<"nDivisions = " << nDivisions << endl;
149  cout<<"CanvasW = " << CanvasW << endl;
150  cout<<"CanvasH = " << CanvasH << endl;
151  cout<<"TextSize = " << TextSize << endl;
152  cout<<"TitleSize = " << TitleSize << endl;
153  cout<<"LabelSize = " << LabelSize << endl;
154  cout<<"PadRightMargin = " << PadRightMargin << endl;
155  cout<<"PadTopMargin = " << PadTopMargin << endl;
156  cout<<"PadBottomMargin = " << PadBottomMargin << endl;
157  cout<<"xTitleOffset = " << xTitleOffset << endl;
158  cout<<"PadLeftMargin = " << PadLeftMargin << endl;
159  cout<<"yTitleOffset = " << yTitleOffset << endl;
160  cout<<"zTitleOffset = " << zTitleOffset << endl;
161 
162 }
163 
164 // ----------------------------------------------------------------------
165 void styles::setMarkers(TH1 *h, float Msize, int Mstyle) {
166  h->SetMarkerStyle(Mstyle);
167  h->SetMarkerSize(Msize);
168 }
169 
170 // ----------------------------------------------------------------------
171 void styles::setTitles(TH1 *h, TString xTitle, TString yTitle, TString Left, TString Right) {
172  if (0==h) {
173  cout << " Histogram not defined" << endl;
174  } else {
175  h->SetTitle(""); h->SetXTitle(xTitle); h->SetYTitle(yTitle);
176  TLatex label; label.SetNDC(kTRUE);
177  label.SetTextAlign(13);
178  label.DrawLatex(PadLeftMargin+0.04,1-PadTopMargin-0.03,Left);
179  label.SetTextAlign(33);
180  label.DrawLatex(1-PadRightMargin-0.02,1-PadTopMargin-0.03,Right);
181  }
182 }
183 
184 // ----------------------------------------------------------------------
185 void styles::styleHist(TH1 *h, Int_t color, Int_t fillstyle,
186  Int_t symbol, Double_t size, Int_t width) {
187  h->SetLineColor(color);
188  h->SetLineWidth(width);
189  h->SetMarkerColor(color);
190  h->SetMarkerStyle(symbol);
191  h->SetMarkerSize(size);
192  h->SetStats(kFALSE);
193  h->SetFillStyle(fillstyle);
194  h->SetFillColor(color);
195 }
196 
197 // ----------------------------------------------------------------------
198 void styles::setTitleSizes(TH1 *h, float size, float lsize, int font,
199  float xoff, float yoff, int divisions) {
200  if (0==h) {
201  cout << " Histogram not defined" << endl;
202  } else {
203  h->SetTitleOffset(xoff, "x"); h->SetTitleOffset(yoff, "y");
204  h->SetTitleSize(size, "x"); h->SetTitleSize(size, "y");
205  h->SetLabelSize(lsize, "x"); h->SetLabelSize(lsize, "y");
206  h->SetLabelFont(font, "x"); h->SetLabelFont(font, "y");
207  h->GetXaxis()->SetTitleFont(font); h->GetYaxis()->SetTitleFont(font);
208  h->SetNdivisions(divisions,"X"); h->SetNdivisions(divisions, "Y");
209  }
210 }
211 
212 void styles::parseStyleFile(TString group, TString fnames[], float *fvalues[], int nFloat,
213  TString inames[], int *ivalues[], int nInt){
214  std::ifstream file(confFile);
215  TString word, s_value;
216  while(file >> word){
217  if(word.Contains("[")) {
218  word.ReplaceAll("[",""); word.ReplaceAll("]","");
219  if(word == group){
220  while(file >> word){
221  if(word.Contains("[")) break;
222  file >> s_value; file >> s_value;
223  for(int var(0); var < nFloat; var++){
224  if(word == fnames[var]){
225  *fvalues[var] = s_value.Atof();
226  break;
227  }
228  } // Loop finding requested float variables
229  for(int var(0); var < nInt; var++){
230  if(word == inames[var]){
231  *ivalues[var] = s_value.Atoi();
232  break;
233  }
234  } // Loop finding requested int variables
235  } // Loop over group variables
236  break;
237  }
238  }
239  } // Loop over all words
240 }
241 
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:185
void parseStyleFile(TString group, TString fnames[], float *fvalues[], int nFloat, TString inames[], int *ivalues[], int nInt)
Definition: styles.cpp:212
void setDefaultStyle()
Definition: styles.cpp:36
float TextSize
Definition: styles.hpp:35
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 setTitleSizes(TH1 *h, float size, float lsize, int font=62, float xoff=1., float yoff=1., int divisions=405)
Definition: styles.cpp:198
TString Group
Definition: styles.hpp:32
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:145
void fixYAxis(TH1 *h, TPad *pad)
Definition: styles.cpp:96
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:165
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:171
int nFont
Definition: styles.hpp:33
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:110
float PadBottomMargin
Definition: styles.hpp:36
styles(TString group="Standard")
Definition: styles.cpp:25