Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvcs_plot.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dvcs_plot.C
1 // ------------------------------------------------------------------------//
2 // Declaring Cut Structs
3 // ------------------------------------------------------------------------//
5  float min_Q2;
6  float max_Q2;
7  float min_y;
8  float max_y;
9  float min_t;
10  float max_t;
12 };
13 
14 struct lepton_cut{
15  float min_eta;
16  float max_eta;
17  float min_mom;
18  float max_mom;
19  float min_theta;
20  float max_theta;
21 };
22 
23 struct gamma_cut{
24  float min_eta;
25  float max_eta;
26  float min_mom;
27  float max_mom;
28  float min_theta;
29  float max_theta;
30 };
31 
32 struct hadron_cut{
33  float min_eta;
34  float max_eta;
35  float min_mom;
36  float max_mom;
37  float min_theta;
38  float max_theta;
39 };
40 
41 // ------------------------------------------------------------------------//
42 // Standard Cuts
43 // ------------------------------------------------------------------------//
44 const event_kinematics_cut ev_standard_cut = {0,1000,0,100,0,100,0};
45 const lepton_cut l_standard_cut = {-999,999,-999,999,-999,999};
46 const hadron_cut h_standard_cut = {-999,999,-999,999,-999,999};
47 const gamma_cut g_standard_cut = {-999,999,-999,999,-999,999};
48 // ------------------------------------------------------------------------//
49 // Relevant Arrays and Array Index Storing Variable
50 // ------------------------------------------------------------------------//
51 TH2F **h2array;
56 TString *pngNames;
57 int index=0;
59 // ------------------------------------------------------------------------//
60 // Other Global Variables
61 // ------------------------------------------------------------------------//
62 int nEvents;
63 // ------------------------------------------------------------------------//
64 // Creating Base Histograms for Types of Plots
65 // ------------------------------------------------------------------------//
66 
67 const int nbins=80;
68 TH2F *h2_x_Q2_base = new TH2F("x_Q2","",nbins,-4,0,nbins,0,2);
69 TH2F *h2_lepton_base = new TH2F("lepton_mom_eta","",nbins,-4,4,nbins,0,30);
70 TH2F *h2_gamma_base = new TH2F("gamma_mom_eta","",nbins,-4,4,nbins,0,30);
71 TH2F *h2_hadron_base = new TH2F("hadron_mom_eta","",nbins,-1,20,nbins,0,300);
72 TH2F *h2_hadron_scatter_base = new TH2F("scatter","",nbins,0,1.5,nbins,0,0.006);
73 TH2F *h2_electron_photon_angle_base = new TH2F("angle_separation","",nbins,0,3.2,nbins,0,1);
74 TH2F *h2_nils_plot = new TH2F("nils_plot","",nbins,0,9,nbins,0,360);
75 
76 // ------------------------------------------------------------------------//
77 //
78 //
79 //
80 // Main Code
81 //
82 //
83 //
84 // ------------------------------------------------------------------------//
85 
86 int dvcs_plot()
87 {
88  // ------------------------------------------------------------------------//
89  // Uploading File and Truth Tree
90  // ------------------------------------------------------------------------//
91 
92  TFile *f = new TFile("/sphenix/user/gregtom3/data/Summer2018/DVCS_studies/500k_10x250_sartre_dvcs.root","READ");
93  TTree *t= (TTree*)f->Get("event_truth");
94 
95  // ------------------------------------------------------------------------//
96  // Creating Plotting Histograms
97  // 1) Edit 'arraySize' to fit how many plots you will like to make
98  // 2) Create your own event kinematics, lepton, hadron, or gamma cuts
99  // Or simply select the standard ones already created
100  // 3) Add the desired histogram to the array using 'add_to_array'
101  // ------------------------------------------------------------------------//
102 
103  arraySize = 1;
104 
105  h2array=malloc(arraySize * sizeof(TH2F));
106  ev_cutarray=malloc(arraySize * sizeof(event_kinematics_cut));
107  l_cutarray=malloc(arraySize * sizeof(lepton_cut));
108  g_cutarray=malloc(arraySize * sizeof(gamma_cut));
109  h_cutarray=malloc(arraySize * sizeof(hadron_cut));
110  pngNames=malloc(arraySize * sizeof(TString));
111 
112  // event_kinematics_cut *** = {float min_Q2, float max_Q2,
113  // float min_y , float max_y ,
114  // float min_t , float max_t ,
115  // float min_angle_separation}
116 
117  // lepton_cut *** = {float min_eta, float max_eta,
118  // float min_mom, float max_mom,
119  // float min_theta, float max_theta}
120 
121  // gamma_cut *** = {float min_eta, float max_eta,
122  // float min_mom, float max_mom,
123  // float min_theta, float max_theta}
124 
125  // hadron_cut *** = {float min_eta, float max_eta,
126  // float min_mom, float max_mom,
127  // float min_theta, float max_theta}
128 
129  gamma_cut EEMC = {-4,-1,-999,999,-999,999};
130  gamma_cut CEMC = {-1,1,-999,999,-999,999};
131  gamma_cut FEMC = {1,4,-999,999,-999,999};
132 
133  // Adding to Histogram Plot Instructions
134  // add_to_array(char *arg_1, TString arg_2, TString arg_3
135  // event_kinematics_cut arg_4, lepton_cut arg_5, gamma_cut arg_6, hadron_cut arg_7)
136  //
137  // arg_1 : Type of Histogram to draw
138  // a) x_Q2
139  // b) gamma_mom_eta
140  // c) lepton_mom_eta
141  // d) proton_mom_eta
142  // e) scatter
143  // f) angle_separation
144  // g) nils_plot
145  // arg_2 : After code is executed, histogram saves as png
146  // arg_3 : Histogram Title
147  // arg_4 : Cut on event kinematics and other special properties
148  // arg_5 : Cut on the scattered electron's kinematics
149  // arg_6 : Cut on the scattered photon's kinematics
150  // arg_7 : Cut on the scattered proton's kinematics
151  // **Note: Leaving any of arg_4 -> arg_7 empty defaults to standard cuts
152  // ev_standard_cut , l_standard_cut , g_standard_cut , h_standard_cut
153 
154  add_to_array("x_Q2","/sphenix/user/gregtom3/data/Summer2018/DVCS_studies/png/milou_cuts/10x250_x_Q2_EEMC_plot.png","x-Q2 Distribution 10x250 DVCS EEMC (LoI cuts)",ev_standard_cut,l_standard_cut,EEMC,h_standard_cut);
155 
156 
157 
158  // ------------------------------------------------------------------------//
159  // Events to Run (If nEvents < 0, all events used)
160  // ------------------------------------------------------------------------//
161 
162  nEvents = -1;
163 
164  // ------------------------------------------------------------------------//
165  // Declare all relevant event variables
166  // ------------------------------------------------------------------------//
167 
168  Float_t Q2;
169  Float_t W;
170  Float_t x;
171  Float_t y;
172  Float_t T;
173 
174  std::vector<float> energy;
175  std::vector<float> eta;
176  std::vector<float> pid;
177  std::vector<float> theta;
178  std::vector<float> phi;
179  std::vector<float> xvector;
180 
181  std::vector<float>* energy_pointer=&energy;
182  std::vector<float>* eta_pointer=&eta;
183  std::vector<float>* pid_pointer=&pid;
184  std::vector<float>* theta_pointer=&theta;
185  std::vector<float>* phi_pointer=&phi;
186  std::vector<float>* xvector_pointer=&xvector;
187 
188  t->SetBranchAddress("evtgen_Q2",&Q2);
189  t->SetBranchAddress("evtgen_x",&x);
190  t->SetBranchAddress("evtgen_y",&y);
191  t->SetBranchAddress("evtgen_W",&W);
192  t->SetBranchAddress("evtgen_t",&T);
193  t->SetBranchAddress("em_evtgen_ptotal",&energy_pointer);
194  t->SetBranchAddress("em_evtgen_eta",&eta_pointer);
195  t->SetBranchAddress("em_evtgen_pid",&pid_pointer);
196  t->SetBranchAddress("em_evtgen_theta",&theta_pointer);
197  t->SetBranchAddress("em_evtgen_phi",&phi_pointer);
198  t->SetBranchAddress("em_reco_x_e",&xvector_pointer);
199 
200  // Momentum, Eta, Theta , Phi of Scattered Photon
201  float gamma_eta;
202  float gamma_mom;
203  float gamma_theta;
204  float gamma_phi;
205 
206  // Momentum, Theta, Phi of Scattered Electron
207  float lepton_eta;
208  float lepton_mom;
209  float lepton_theta;
210  float lepton_phi;
211 
212  // Momentum, Theta, Phi of Scattered Proton
213  float hadron_eta;
214  float hadron_mom;
215  float hadron_theta;
216  float hadron_phi;
217 
218  // ------------------------------------------------------------------------//
219  // Loop through Tree
220  // ------------------------------------------------------------------------//
221 
222  Int_t nentries = Int_t(t->GetEntries());
223  if(nEvents>nentries)
224  cout << "Warning : Number of Events specified to run is greater than entries in the tree, using all entries" << endl;
225  else if(nEvents>0)
226  nentries = nEvents;
227  for(Int_t entryInChain=0; entryInChain<nentries; entryInChain++)
228  {
229  if(entryInChain%1000==0)
230  cout << "Processing event " << entryInChain << " of " << nentries << endl;
231  Int_t entryInTree = t->LoadTree(entryInChain);
232  if (entryInTree < 0) break;
233  t->GetEntry(entryInChain);
234  // Flip through all the particles in the event (only 3)
235  for(unsigned i = 0; i<energy.size(); i++)
236  {
237  if(pid.at(i)==22) // Scattered photon
238  {
239  gamma_eta = eta.at(i);
240  gamma_mom = energy.at(i);
241  gamma_theta = theta.at(i);
242  gamma_phi = phi.at(i);
243  }
244  if(pid.at(i)==11) // Scattered electron
245  {
246  lepton_eta = eta.at(i);
247  lepton_mom = energy.at(i);
248  lepton_theta = theta.at(i);
249  lepton_phi = phi.at(i);
250  x=xvector.at(i);
251  }
252  if(pid.at(i)==2212) // Scattered proton
253  {
254  hadron_eta = eta.at(i);
255  hadron_mom = energy.at(i);
256  hadron_theta = theta.at(i);
257  hadron_phi = phi.at(i);
258  }
259  }
260 
261  // Angle between Scattered Electron and Photon
262  float angle_separation = get_angle_separation(lepton_phi, lepton_eta, gamma_phi, gamma_eta);
263 
264  // Set 't' to abs for plotting
265  if(T<0)
266  T=-T;
267  // Fill the histogram array
268  for(int n = 0; n<arraySize; n++)
269  {
270  // This is really ugly, but we check each event parameters with the
271  // cuts predefined by the plot.
272  event_kinematics_cut myCut = ev_cutarray[n];
273  lepton_cut lCut = l_cutarray[n];
274  gamma_cut gCut = g_cutarray[n];
275  hadron_cut hCut = h_cutarray[n];
276  char *histTitle = h2array[n]->GetName();
277  float sep = angle_separation;
278 
279  bool passed_Q2 = (Q2 >= myCut.min_Q2 && Q2<= myCut.max_Q2);
280  bool passed_y = (y >= myCut.min_y && y <= myCut.max_y);
281  bool passed_t = (T >= myCut.min_t && T <= myCut.max_t);
282  bool passed_separation = ( sep >= myCut.min_angle_separation );
283  bool ev_passed = passed_Q2 && passed_y && passed_t && passed_separation;
284 
285  bool passed_lepton_eta = (lepton_eta >= lCut.min_eta && lepton_eta <= lCut.max_eta);
286  bool passed_lepton_mom = (lepton_mom >= lCut.min_mom && lepton_mom <= lCut.max_mom);
287  bool passed_lepton_theta = (lepton_theta >= lCut.min_theta && lepton_theta <= lCut.max_theta);
288  bool lepton_passed = passed_lepton_eta && passed_lepton_mom && passed_lepton_theta;
289 
290  bool passed_gamma_eta = (gamma_eta >= gCut.min_eta && gamma_eta <= gCut.max_eta);
291  bool passed_gamma_mom = (gamma_mom >= gCut.min_mom && gamma_mom <= gCut.max_mom);
292  bool passed_gamma_theta = (gamma_theta >= gCut.min_theta && gamma_theta <= gCut.max_theta);
293  bool gamma_passed = passed_gamma_eta && passed_gamma_mom && passed_gamma_theta;
294 
295  bool passed_hadron_eta = (hadron_eta >= hCut.min_eta && hadron_eta <= hCut.max_eta);
296  bool passed_hadron_mom = (hadron_mom >= hCut.min_mom && hadron_mom <= hCut.max_mom);
297  bool passed_hadron_theta = (hadron_theta >= hCut.min_theta && hadron_theta <= hCut.max_theta);
298  bool hadron_passed = passed_hadron_eta && passed_hadron_mom && passed_hadron_theta;
299 
300  bool passed = ev_passed && lepton_passed && gamma_passed && hadron_passed;
301  if(passed)
302  {
303  if(strcmp(histTitle,"x_Q2")==0)
304  {
305  h2array[n]->Fill(x,Q2);
306  }
307  else if(strcmp(histTitle,"lepton_mom_eta")==0)
308  {
309  h2array[n]->Fill(lepton_eta,lepton_mom);
310  }
311  else if(strcmp(histTitle,"gamma_mom_eta")==0)
312  {
313  h2array[n]->Fill(gamma_eta,gamma_mom);
314  }
315  else if(strcmp(histTitle,"hadron_mom_eta")==0)
316  {
317  h2array[n]->Fill(hadron_eta,hadron_mom);
318  }
319  else if(strcmp(histTitle,"scatter")==0)
320  {
321  h2array[n]->Fill(T,hadron_theta);
322  }
323  else if(strcmp(histTitle,"angle_separation")==0)
324  {
325  h2array[n]->Fill(lepton_theta,angle_separation);
326  }
327  else if(strcmp(histTitle,"nils_plot")==0)
328  {
329  float eta_diff = lepton_eta-gamma_eta;
330  float phi_diff = lepton_phi-gamma_phi;
331  if(eta_diff<0) eta_diff=-eta_diff;
332  if(phi_diff<0) phi_diff=-phi_diff;
333  h2array[n]->Fill(eta_diff,phi_diff*180/3.14159265);
334  }
335  else
336  {
337  cout << "Error: Histogram type '"<< histTitle << "' not found. Please use one of the specified types" << endl;
338  return -1;
339  }
340  }
341  }
342  }
343 
344  // ------------------------------------------------------------------------//
345  // Save PNG
346  // ------------------------------------------------------------------------//
347  for(int n = 0; n<arraySize; n++)
348  {
350  }
351 return 0;
352 
353 }
354 
355 float get_angle_separation(float e_phi, float e_theta, float g_phi, float g_theta)
356 {
357  float photon_x = cos(g_phi)*sin(g_theta);
358  float photon_y = sin(g_phi)*sin(g_theta);
359  float photon_z = cos(g_theta);
360 
361  float electron_x = cos(e_phi)*sin(e_theta);
362  float electron_y = sin(e_phi)*sin(e_theta);
363  float electron_z = cos(e_theta);
364 
365  float dot_prod = photon_x*electron_x+photon_y*electron_y+photon_z*electron_z;
366 
367  if(dot_prod>=1)
368  return 0;
369  else if(dot_prod<=-1)
370  return 3.14159265;
371  else
372  return acos(dot_prod);
373 }
374 
375 void hist_to_png(TH2F * h, TString saveTitle)
376 {
377  TCanvas *cPNG = new TCanvas(saveTitle,"",700,500);
378  TImage *img = TImage::Create();
379  gStyle->SetOptStat(0);
380  if(strcmp(h->GetName(),"x_Q2")==0)
381  {
382  gPad->SetLogx();
383  gPad->SetLogy();
384  h->GetXaxis()->SetTitle("x");
385  h->GetYaxis()->SetTitle("Q2 [GeV^{2}]");
386  }
387  else if(strcmp(h->GetName(),"gamma_mom_eta")==0)
388  {
389  h->GetXaxis()->SetTitle("Photon #eta");
390  h->GetYaxis()->SetTitle("Photon Momentum [GeV]");
391  gPad->SetLogz();
392  }
393  else if(strcmp(h->GetName(),"lepton_mom_eta")==0)
394  {
395  h->GetXaxis()->SetTitle("Electron #eta");
396  h->GetYaxis()->SetTitle("Electron Momentum [GeV]");
397  gPad->SetLogz();
398  }
399  else if(strcmp(h->GetName(),"hadron_mom_eta")==0)
400  {
401  h->GetXaxis()->SetTitle("Proton #eta");
402  h->GetYaxis()->SetTitle("Proton Momentum [GeV]");
403  gPad->SetLogz();
404  }
405  else if(strcmp(h->GetName(),"scatter")==0)
406  {
407  h->GetXaxis()->SetTitle("|t| [GeV^2]");
408  h->GetYaxis()->SetTitle("Proton Scatter Angle [rad]");
409  }
410  else if(strcmp(h->GetName(),"angle_separation")==0)
411  {
412  h->GetXaxis()->SetTitle("Electron #theta");
413  h->GetYaxis()->SetTitle("Electron Photon Angle Separation [rad]");
414  }
415  else if(strcmp(h->GetName(),"nils_plot")==0)
416  {
417  h->GetXaxis()->SetTitle("Electron Photon Eta Separation");
418  h->GetYaxis()->SetTitle("Electron Photon Phi Separation [deg]");
419  }
420  h->Draw("colz9");
421  img->FromPad(cPNG);
422  img->WriteImage(saveTitle);
423 
424  delete img;
425 }
426 
427 void add_to_array(char *type, TString pngName, TString title,
428  event_kinematics_cut ev_c=ev_standard_cut,
429  lepton_cut l_c=l_standard_cut,
430  gamma_cut g_c=g_standard_cut,
431  hadron_cut h_c=h_standard_cut)
432 {
433  if(index+1>arraySize)
434  {
435  cout << "Warning: Set 'arraySize' to a value which matches the number of histograms you wish to plot" << endl;
436  cout << "Not plotting Plot Index " << index << endl;
437  exit();
438  }
439  else
440  {
441  TH2F *theH=NULL;
442  if(strcmp(type,"x_Q2")==0)
443  {
444  theH = (TH2F*)h2_x_Q2_base->Clone();
445  BinLog(theH);
446  }
447  else if(strcmp(type,"lepton_mom_eta")==0)
448  {
449  theH = (TH2F*)h2_lepton_base->Clone();
450  }
451  else if(strcmp(type,"gamma_mom_eta")==0)
452  {
453  theH = (TH2F*)h2_gamma_base->Clone();
454  }
455  else if(strcmp(type,"hadron_mom_eta")==0)
456  {
457  theH = (TH2F*)h2_hadron_base->Clone();
458  }
459  else if(strcmp(type,"scatter")==0)
460  {
461  theH = (TH2F*)h2_hadron_scatter_base->Clone();
462  }
463  else if(strcmp(type,"angle_separation")==0)
464  {
465  theH = (TH2F*)h2_electron_photon_angle_base->Clone();
466  }
467  else if(strcmp(type,"nils_plot")==0)
468  {
469  theH = (TH2F*)h2_nils_plot->Clone();
470  }
471  h2array[index] = theH;
472  ev_cutarray[index] = ev_c;
473  l_cutarray[index] = l_c;
474  g_cutarray[index] = g_c;
475  h_cutarray[index] = h_c;
476  pngNames[index] = pngName;
477  index++;
478  theH->SetTitle(title);
479  }
480 }
481 
482 void BinLog(TH2F *h)
483 {
484 
485  TAxis *axis = h->GetXaxis();
486  int bins = axis->GetNbins();
487 
488  Axis_t from = axis->GetXmin();
489  Axis_t to = axis->GetXmax();
490  Axis_t width = (to - from) / bins;
491  Axis_t *new_bins = new Axis_t[bins + 1];
492 
493  for (int i = 0; i <= bins; i++) {
494  new_bins[i] = TMath::Power(10, from + i * width);
495  }
496  axis->Set(bins, new_bins);
497 
498  TAxis *axis2 = h->GetYaxis();
499  int bins2 = axis2->GetNbins();
500 
501  Axis_t from2 = axis2->GetXmin();
502  Axis_t to2 = axis2->GetXmax();
503  Axis_t width2 = (to2 - from2) / bins2;
504  Axis_t *new_bins2 = new Axis_t[bins2 + 1];
505 
506  for (int i = 0; i <= bins2; i++) {
507  new_bins2[i] = TMath::Power(10, from2 + i * width2);
508  }
509  axis2->Set(bins2, new_bins2);
510 
511  delete new_bins;
512  delete new_bins2;
513 }