EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PlotFunctions.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PlotFunctions.h
1 #include "TROOT.h"
2 #include "TChain.h"
3 #include "TEfficiency.h"
4 #include "TH1.h"
5 #include "TGraphErrors.h"
6 #include "TCut.h"
7 #include "TCanvas.h"
8 #include "TStyle.h"
9 #include "TLegend.h"
10 #include "TMath.h"
11 #include "TLine.h"
12 #include "TLatex.h"
13 #include "TRatioPlot.h"
14 #include "TString.h"
15 
16 #include <glob.h>
17 #include <iostream>
18 #include <iomanip>
19 #include <vector>
20 
21 //
22 // Some universal constants for code to use
23 //
24 
25 // Units, Cross-Sections
26 // standard baseline units: fb, GeV, mm, seconds
27 
28 float u_fb = 1.0;
29 float u_mb = 1.0e12 * u_fb; // convert to fb
30 
31 float u_GeV = 1.0;
32 float u_MeV = 1.0e3 * u_GeV; // convert to GeV
33 
34 float u_mm = 1.0;
35 float u_cm = 1e2 * u_mm;
36 
37 
38 struct plot_config {
39  TString xtitle = "";
40  TString ytitle = "";
41  TH1F* htemplate = nullptr;
42  std::vector<float> xlimits;
43  std::vector<float> ylimits;
44  bool logy = kFALSE;
45  bool logx = kFALSE;
46  TString cuts = "";
47 
48  int markercolor = kBlack;
49  int markerstyle = kDot;
50  int linecolor = kBlack;
51  int linestyle = kSolid;
52  int linewidth = 3;
53  int fillcolor = 0;
54  int fillstyle = 0;
55 
56 };
57 
58 
59 
60 
61 static Int_t histUID = 0;
62 
63 inline Int_t getHistUID()
64 {
65  Int_t thisID = histUID;
66  histUID++;
67  return thisID;
68 }
69 
70 
71 inline float smart_legend_x(float x_trial, float x_width)
72 {
73  float excess = 0.92 - (x_trial + x_width);
74  float x_new = x_trial;
75 
76  if (excess < 0.0) {
77  std::cout << "smart_legend_x(): x width overshoots range - shifting by " << excess << std::endl;
78  x_new = x_trial + excess;
79  }
80 
81  return x_new;
82 
83 }
84 
85 inline float smart_legend_y(float y_trial, float y_height)
86 {
87  float excess = 0.92 - (y_trial + y_height);
88  float y_new = y_trial;
89 
90  if (excess < 0.0) {
91  std::cout << "smart_legend_y(): y height overshoots range - shifting by " << excess << std::endl;
92  y_new = y_trial + excess;
93  }
94 
95  return y_new;
96 
97 }
98 
99 inline TLegend* smart_legend(std::string where = "upper right", float legend_width = 0.30, float legend_height = 0.10)
100 {
101  TLegend* legend = nullptr;
102 
103  float legend_x = 0.65;
104  float legend_y = 0.85;
105 
106  if (where == "upper right") {
107  legend_x = smart_legend_x(0.65, legend_width);
108  legend_y = smart_legend_y(0.82, legend_height);
109  } else if (where == "center right") {
110  legend_x = smart_legend_x(0.65, legend_width);
111  legend_y = smart_legend_y(0.55, legend_height);
112  } else if (where == "lower right") {
113  legend_x = smart_legend_x(0.65, legend_width);
114  legend_y = smart_legend_y(0.20, legend_height);
115  } else if (where == "upper left") {
116  legend_x = smart_legend_x(0.15, legend_width);
117  legend_y = smart_legend_y(0.82, legend_height);
118  } else if (where == "center left") {
119  legend_x = smart_legend_x(0.15, legend_width);
120  legend_y = smart_legend_y(0.55, legend_height);
121  } else if (where == "lower left") {
122  legend_x = smart_legend_x(0.15, legend_width);
123  legend_y = smart_legend_y(0.20, legend_height);
124  } else {
125  std::cout << "You specified a placement of " << where << " that is unknown..." << std::endl;
126 
127  }
128 
129  legend = new TLegend(legend_x, legend_y, legend_x + legend_width, legend_y + legend_height);
130  legend->SetFillStyle(0);
131  legend->SetBorderSize(0);
132 
133  return legend;
134 
135 }
136 
137 inline void set_axis_range(TH1F* hist, float min, float max, std::string which = "X") {
138  hist->SetAxisRange(min, max, which.c_str());
139  std::cout << "set_axis_range: setting " << which << " axis range to: [" << min << ", " << max << "]" << std::endl;
140  if (which == "X") {
141  hist->GetXaxis()->SetLimits(min, max);
142  hist->GetXaxis()->SetRangeUser(min, max);
143  } else if (which == "Y") {
144  hist->GetYaxis()->SetLimits(min, max);
145  hist->GetYaxis()->SetRangeUser(min, max);
146  }
147 
148 }
149 
150 template <class T> void configure_plot(T* object, plot_config options, std::string which = "" )
151 {
152 
153  if (which == "") {
154  return;
155  } else if (which == "charm") {
156  object->SetLineColor(kBlue+1);
157  object->SetMarkerColor(kBlue+1);
158  object->SetMarkerStyle(kOpenDiamond);
159  } else if (which == "light") {
160  object->SetLineColor(kRed+1);
161  object->SetMarkerColor(kRed+1);
162  object->SetMarkerStyle(kFullCircle);
163  } else if (which == "errorband") {
164 
165  }
166 
167  object->SetLineWidth(2);
168  object->SetMarkerSize(2);
169 
170  // object->SetXTitle( options.xtitle );
171  // object->SetYTitle( options.ytitle );
172 }
173 
174 
175 inline TLatex make_title(TString text = "CC-DIS, 10GeVx275GeV, Q^{2}>100 GeV^{2}") {
176 
177  TLatex plot_title;
178  plot_title.SetTextSize(0.035);
179  plot_title.SetTextAlign(22); // center-center
180  plot_title.DrawLatexNDC(0.55, 0.97, text.Data());
181 
182  return plot_title;
183 }
184 
185 
186 inline std::vector<std::string> fileVector(const std::string& pattern){
187  glob_t glob_result;
188  glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
189  std::vector<std::string> files;
190  for(unsigned int i=0;i<glob_result.gl_pathc;++i){
191  files.push_back(std::string(glob_result.gl_pathv[i]));
192  }
193  globfree(&glob_result);
194  return files;
195 }
196 
197 
198 float LookupCrossSection(TString samplename)
199 {
200  float xsection = 0.0;
201  if (samplename.Contains("CC") && samplename.Contains("DIS")) {
202  // Q^2 > 100 GeV^2
203  xsection = 1.47637e-08*u_mb;
204  } else if (samplename.Contains("NC") && samplename.Contains("DIS")) {
205  // Q^2 > 50 GeV^2
206  xsection = 7.444e-06*u_mb;
207  }
208  return xsection;
209 }
210 
211 
212 
214  TTree* data,
215  TString name,
216  TString x,
217  TCut selection)
218 {
219  TH1F* plot = static_cast<TH1F*>(draw_config.htemplate->Clone(Form("%s_%d", name.Data(), getHistUID())));
220 
221  data->Project(plot->GetName(), x.Data(), selection);
222 
223  plot->SetLineColor(draw_config.linecolor);
224  plot->SetLineStyle(draw_config.linestyle);
225 
226  plot->SetFillStyle(draw_config.fillstyle);
227  plot->SetFillColor(draw_config.fillcolor);
228 
229  plot->SetMarkerColor(draw_config.markercolor);
230  plot->SetMarkerStyle(draw_config.markerstyle);
231 
232  plot->SetXTitle( draw_config.xtitle );
233  plot->SetYTitle( draw_config.ytitle );
234 
235  return plot;
236 }