Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Plot-Measured_Energy-EMC.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Plot-Measured_Energy-EMC.C
1 #include <iostream>
2 #include <cstdlib>
3 #include <memory>
4 #include <sstream>
5 #include <string>
6 #include <cstring>
7 #include <vector>
8 #include <cassert>
9 
10 #include "TFile.h"
11 #include "TTree.h"
12 #include "TCanvas.h"
13 #include "TLegend.h"
14 #include "TROOT.h"
15 #include "TH1F.h"
16 #include "TColor.h"
17 #include "TImage.h"
18 
19 /*
20  * sPHENIX Style
21  */
22 #include "/sphenix/user/gregtom3/SBU/research/macros/macros/sPHENIXStyle/sPhenixStyle.C"
23 
24 #define NELEMS(arr) (sizeof(arr)/sizeof(arr[0]))
25 
26 /* The particle types we have */
28 /* The detectors we have */
29 enum detector { cemc, eemc, femc };
30 
31 TTree *load_tree(const char *const file_name, const char *const tree_name);
32 void draw_histogram(TH1F * const h, const particle_type p,
33  const int energy_level_gev, const detector d);
34 void fill_histogram(TH1F * const h, TTree * const t, const Float_t min_value);
36  const int particle_energy_gev, const detector d);
37 char *generate_file_path(const particle_type p, const int particle_energy_gev,
38  const detector d);
42 char *generate_legend_header(const particle_type p, const detector d);
43 char *generate_legend_entry_label(const int particle_energy_gev);
44 
45 /* Directory where data is stored for plots */
46 const char *const data_directory =
47  "/sphenix/user/gregtom3/data/Summer2018/ECAL_energy_studies";
48 /* The energy levels we have in GeV */
49 const static int energy_levels[] = { 1, 2, 5, 10, 20 };
50 
51 /* How to change lines for each energy level */
52 const static int color_offsets[] = { -10, -9, -7, -4, 0 };
53 
54 /* Detectors that we want to plot */
55 const static enum detector detectors[] = { cemc, eemc, femc };
56 
57 /* Particles we want to plot */
58 const static enum particle_type particles[] = { pion, electron };
59 
60 /* The color of the plot corresponding to each particle */
61 const static Color_t plot_colors[] = { kBlue, kRed, kGreen, kOrange, kViolet };
62 
63 /* These should not be necessary, but root is buggy */
67 
69 {
70  /* sanity check */
72 
74  gROOT->SetBatch(kTRUE);
75 
76  /*
77  * Base Histogram (Recreated from Matching Plots)
78  */
79  TH1F *const h_base = new TH1F("h_base", "", 100, 0.0, 25);
80 
81  std::vector < TCanvas * >particle_canvases;
82  for (int i = 0; i < nparticles; ++i) {
83  TCanvas *const c =
84  new TCanvas(generate_canvas_name(particles[i]),
86  gStyle->GetCanvasDefW() * ndetectors,
87  gStyle->GetCanvasDefH());
88  c->Divide(ndetectors, 1);
89  particle_canvases.push_back(c);
90  }
91 
92  /* iterate through energy levels, detectors, and particles and create plots for each */
93  for (int e = 0; e < nenergy_levels; ++e)
94  for (int d = 0; d < ndetectors; ++d)
95  for (int p = 0; p < nparticles; ++p) {
96  /* +1 to account for 1-based indexing */
97  particle_canvases[p]->cd(d + 1);
98  TH1F *const h = (TH1F *) h_base->Clone();
99  h->SetLineColor(plot_colors[e]);
101  energy_levels[e], detectors[d]);
102  }
103 
104  /* add legends */
105  for (int d = 0; d < ndetectors; ++d)
106  for (int p = 0; p < nparticles; ++p) {
107  particle_canvases[p]->cd(d + 1);
108  gPad->RedrawAxis();
109  if (d == 0) {
110  TLegend *const l =
111  new TLegend(0.60, .90, .9, 0.625,
113  [p],
114  detectors
115  [d]));
116  l->SetTextSize(0.05);
117  for (int e = 0; e < nenergy_levels; ++e)
118  l->AddEntry(generate_histogram_name
119  (particles[p],
120  energy_levels[e],
121  detectors[d]),
123  (energy_levels[e]), "l");
124 
125  l->Draw();
126  } else {
127  TLegend *const l =
128  new TLegend(0.60, .90, .9, 0.85,
130  [p],
131  detectors
132  [d]));
133  l->SetTextSize(0.05);
134  l->Draw();
135  }
136 
137  gPad->SetLogy();
138  }
139 
140  /* save canvases to PNG */
141  for (int p = 0; p < nparticles; ++p) {
142  TImage *const img = TImage::Create();
143  img->FromPad(particle_canvases[p]);
144  char *const path = generate_save_file_path(particles[p]);
145  img->WriteImage(path);
146  delete img;
147  }
148 }
149 
150 void draw_histogram(TH1F * const h, const particle_type p,
151  const int energy_level_gev, const detector d)
152 {
153  h->SetName(generate_histogram_name(p, energy_level_gev, d));
154 
155  TTree *const t = load_tree(generate_file_path(p, energy_level_gev, d),
156  "ntp_cluster");
157 
158  fill_histogram(h, t, 0.3);
159  h->Scale(1 / h->GetEntries());
160  h->GetYaxis()->SetRangeUser(0.0001, 10);
161  h->SetXTitle("E_{cluster} (GeV)");
162  h->SetYTitle("entries / #scale[0.5]{#sum} entries ");
163 
164  h->Draw("SAME");
165 }
166 
167 TTree *load_tree(const char *const file_name, const char *const tree_name)
168 {
169  return (TTree *) (new TFile(file_name, "READ"))->Get(tree_name);
170 }
171 
172 /*
173  * This function, when fed a histogram, tree,
174  * a floating point min_value variable, and a boolean, will fill each entry
175  * inside the specificed branch into the histogram, so long as each entry
176  * is a floating point number GREATER than the min_value. If normalize is
177  * set to 'true', the histogram will be normalize based on its number of
178  * entries. The axes titles are furthermore assumed to be generic and have
179  * been already set.
180  */
181 void fill_histogram(TH1F * const h, TTree * const t, const Float_t min_value)
182 {
183  Float_t measured_energy;
184  Float_t true_energy;
185  t->SetBranchAddress("e", &measured_energy);
186  t->SetBranchAddress("ge", &true_energy);
187  Int_t nentries = Int_t(t->GetEntries());
188 
189  for (Int_t i = 0; i < nentries; ++i) {
190  if (t->LoadTree(i) < 0)
191  break;
192 
193  t->GetEntry(i);
194  if (measured_energy > min_value && true_energy > 0.1)
195  h->Fill(measured_energy);
196  }
197 }
198 
199 char *strdup(const char *s)
200 {
201  char *const t = new char[strlen(s) + 1];
202  return strcpy(t, s);
203 }
204 
206  const int particle_energy_gev, const detector d)
207 {
208  std::stringstream name;
209  name << "Histogram";
210  switch (p) {
211  case electron:
212  name << "_Electron";
213  break;
214  case pion:
215  name << "_Pion";
216  break;
217  }
218 
219  name << "_" << particle_energy_gev << "GeV";
220  switch (d) {
221  case cemc:
222  name << "_CEMC";
223  break;
224  case eemc:
225  name << "_EEMC";
226  break;
227  }
228 
229  return strdup(name.str().c_str());
230 }
231 
233  const int particle_energy_gev, const detector d)
234 {
235  std::stringstream path;
236  path << data_directory;
237 
238  switch (p) {
239  case electron:
240  path << "/Electrons/Electrons";
241  break;
242  case pion:
243  path << "/Pions/Pions";
244  break;
245  }
246 
247  path << particle_energy_gev;
248  switch (d) {
249  case cemc:
250  path << "C";
251  break;
252  case eemc:
253  path << "E";
254  break;
255  case femc:
256  path << "F";
257  break;
258  }
259 
260  path << ".root";
261 
262  return strdup(path.str().c_str());
263 }
264 
266 {
267  std::stringstream name;
268 
269  switch (p) {
270  case pion:
271  name << "Pion";
272  break;
273  case electron:
274  name << "Electron";
275  break;
276  }
277 
278  name << "-Measured_Energy";
279  for (int d = 0; d < ndetectors; ++d)
280  switch (detectors[d]) {
281  case cemc:
282  name << "-CEMC";
283  break;
284  case eemc:
285  name << "-EEMC";
286  break;
287  case femc:
288  name << "-FEMC";
289  break;
290  }
291 
292  name << ".png";
293 
294  return strdup(name.str().c_str());
295 }
296 
298 {
299  std::stringstream canvas_name;
300 
301  switch (p) {
302  case pion:
303  canvas_name << "Pion";
304  break;
305  case electron:
306  canvas_name << "Electron";
307  break;
308  }
309 
310  return strdup(canvas_name.str().c_str());
311 }
312 
314 {
315  std::stringstream canvas_title;
316 
317  switch (p) {
318  case pion:
319  canvas_title << "Pion";
320  break;
321  case electron:
322  canvas_title << "Electron";
323  break;
324  }
325 
326  return strdup(canvas_title.str().c_str());
327 }
328 
330 {
331  std::stringstream legend_header;
332 
333  switch (d) {
334  case cemc:
335  legend_header << "CEMC ";
336  break;
337  case eemc:
338  legend_header << "EEMC ";
339  break;
340  case femc:
341  legend_header << "FEMC ";
342  break;
343  }
344 
345  switch (p) {
346  case pion:
347  legend_header << "Pions ";
348  break;
349  case electron:
350  legend_header << "Electrons ";
351  break;
352  }
353 
354  return strdup(legend_header.str().c_str());
355 }
356 
357 char *generate_legend_entry_label(const int particle_energy_gev)
358 {
359  std::stringstream legend_entry;
360  legend_entry << particle_energy_gev << " GeV";
361 
362  return strdup(legend_entry.str().c_str());
363 }