Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
track2cluster_plot.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file track2cluster_plot.C
1 // ------------------------------------------------------------------------//
2 // Code Purpose
3 // ------------------------------------------------------------------------//
4 // To show the difference between the track extrapolation code and truth
5 // track information
6 // ------------------------------------------------------------------------//
7 //
8 // ------------------------------------------------------------------------//
9 //
10 //
11 //
12 // Main Code
13 //
14 //
15 //
16 // ------------------------------------------------------------------------//
17 
18 const int num_files=4;
19 
20 const int verbosity = 1;
21 
23 {
24  gROOT->SetBatch(kTRUE);
25  // ------------------------------------------------------------------------//
26  // Uploading Files and Cluster Trees
27  // ------------------------------------------------------------------------//
28 
29 
30  TFile *f_1 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e1DIS.root");
31  TTree *t_truth_1 = (TTree*)f_1->Get("event_truth");
32  TTree *t_cluster_1 = (TTree*)f_1->Get("event_cluster");
33 
34  TFile *f_2 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e2DIS.root");
35  TTree *t_truth_2 = (TTree*)f_2->Get("event_truth");
36  TTree *t_cluster_2 = (TTree*)f_2->Get("event_cluster");
37 
38  TFile *f_5 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e5DIS.root");
39  TTree *t_truth_5 = (TTree*)f_5->Get("event_truth");
40  TTree *t_cluster_5 = (TTree*)f_5->Get("event_cluster");
41 
42  TFile *f_10 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e10DIS.root");
43  TTree *t_truth_10 = (TTree*)f_10->Get("event_truth");
44  TTree *t_cluster_10 = (TTree*)f_10->Get("event_cluster");
45 
46  /*TFile *f_20 = new TFile("/sphenix/user/gregtom3/data/Summer2018/track2cluster_studies/e20DIS.root");
47  TTree *t_truth_20 = (TTree*)f_20->Get("event_truth");
48  TTree *t_cluster_20 = (TTree*)f_20->Get("event_cluster");*/
49 
50  // ------------------------------------------------------------------------//
51  // Creating Histograms (Phi, Eta Theta, momentum
52  // posx, posy, posz, spatial distance,
53  // ------------------------------------------------------------------------//
54  const int nbins = 200;
55  TH2F *h2_phi_base = new TH2F("","",nbins,-3.5,3.5,nbins,-3.5,3.5);
56  TH2F *h2_eta_base = new TH2F("","",nbins,-10,10,nbins,-10,10);
57  TH2F *h2_theta_base = new TH2F("","",nbins,-3.5,3.5,nbins,-3.5,3.5);
58  TH2F *h2_mom_base = new TH2F("","",nbins,0,35,nbins,0,35);
59  TH2F *h2_posx_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
60  TH2F *h2_posy_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
61  TH2F *h2_posz_base = new TH2F("","",nbins,-320,320,nbins,-320,320);
62  TH1F *h1_spatial_base = new TH1F("","",nbins,0,30);
63  // ------------------------------------------------------------------------//
64  // Creating Histogram Clones
65  // ------------------------------------------------------------------------//
66  TH2F *h2_phi = (TH2F*)h2_phi_base->Clone();
67  TH2F *h2_eta = (TH2F*)h2_eta_base->Clone();
68  TH2F *h2_theta = (TH2F*)h2_theta_base->Clone();
69  TH2F *h2_mom = (TH2F*)h2_mom_base->Clone();
70  TH2F *h2_posx = (TH2F*)h2_posx_base->Clone();
71  TH2F *h2_posy = (TH2F*)h2_posy_base->Clone();
72  TH2F *h2_posz = (TH2F*)h2_posz_base->Clone();
73  TH1F *h1_spatial_cemc = (TH1F*)h1_spatial_base->Clone();
74  TH1F *h1_spatial_eemc = (TH1F*)h1_spatial_base->Clone();
75  TH1F *h1_spatial_femc = (TH1F*)h1_spatial_base->Clone();
76  h1_spatial_cemc->SetLineColor(kRed);
77  h1_spatial_eemc->SetLineColor(kBlue);
78  h1_spatial_femc->SetLineColor(kGreen);
79  // ------------------------------------------------------------------------//
80  // Which momentums would you like to iterate through?
81  // ------------------------------------------------------------------------//
82  std::vector<int> iter_p;
83  iter_p.push_back(1);
84  iter_p.push_back(2);
85  iter_p.push_back(5); // Only these momenta are currently available
86  iter_p.push_back(10);
87  iter_p.push_back(20);
88  // ------------------------------------------------------------------------//
89  // How many events per tree would you like to plot? nEvents < 0 = all evts
90  // ------------------------------------------------------------------------//
91  const int nEvents = 100000;
92  // ------------------------------------------------------------------------//
93  // Analyze each tree
94  // ------------------------------------------------------------------------//
95  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_1,t_cluster_1,nEvents);
96  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"1GeV_electron.png");
97 
98  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_2,t_cluster_2,nEvents);
99  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"2GeV_electron.png");
100 
101  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_5,t_cluster_5,nEvents);
102  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"5GeV_electron.png");
103 
104  analyzeTree(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,t_truth_10,t_cluster_10,nEvents);
105  printHists(h2_phi,h2_eta,h2_theta,h2_mom,h2_posx,h2_posy,h2_posz,h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,"10GeV_electron.png");
106  return 0;
107 }
108 void printHists(TH2F *h2_phi, TH2F* h2_eta, TH2F* h2_theta, TH2F* h2_mom, TH2F* h2_posx, TH2F* h2_posy, TH2F* h2_posz, TH1F* h1_spatial_cemc, TH1F *h1_spatial_eemc, TH1F* h1_spatial_femc,TString save)
109 {
110  hist_to_png(h2_phi,TString(TString("phi_")+save),"phi");
111  hist_to_png(h2_eta,TString(TString("eta_")+save),"eta");
112  hist_to_png(h2_theta,TString(TString("theta_")+save),"theta");
113  hist_to_png(h2_mom,TString(TString("mom_")+save),"mom");
114  hist_to_png(h2_posx,TString(TString("posx_")+save),"posx");
115  hist_to_png(h2_posy,TString(TString("posy_")+save),"posy");
116  hist_to_png(h2_posz,TString(TString("posz_")+save),"posz");
117  hist_to_png_h1(h1_spatial_cemc,h1_spatial_eemc,h1_spatial_femc,TString(TString("spatial_")+save),"spatial");
118 }
119 void analyzeTree(TH2F *h2_phi, TH2F* h2_eta, TH2F* h2_theta, TH2F* h2_mom, TH2F* h2_posx, TH2F* h2_posy, TH2F* h2_posz, TH1F* h1_spatial_cemc, TH1F* h1_spatial_eemc, TH1F* h1_spatial_femc, TTree* t_truth, TTree* t_cluster, const int nEvents)
120 {
121  h2_phi->Reset();
122  h2_eta->Reset();
123  h2_theta->Reset();
124  h2_mom->Reset();
125  h2_posx->Reset();
126  h2_posy->Reset();
127  h2_posz->Reset();
128  h1_spatial_cemc->Reset();
129  h1_spatial_eemc->Reset();
130  h1_spatial_femc->Reset();
131  // -------------------------------//
132  // Declare tree variables
133  // -------------------------------//
134  // Track & Cluster
135  std::vector<float> track_phi; std::vector<float> cluster_phi;
136  std::vector<float> track_eta; std::vector<float> cluster_eta;
137  std::vector<float> track_theta; std::vector<float> cluster_theta;
138  std::vector<float> track_p; std::vector<float> cluster_p; //cluster energy
139  std::vector<float> track_posx; std::vector<float> cluster_posx;
140  std::vector<float> track_posy; std::vector<float> cluster_posy;
141  std::vector<float> track_posz; std::vector<float> cluster_posz;
142  // Truth Particle Info
143  std::vector<float> truth_phi; std::vector<float> truth_eta;
144  std::vector<float> truth_theta; std::vector<float> truth_p;
145  // -------------------------------//
146  // Point to tree variables
147  // -------------------------------//
148  // Track
149  std::vector<float>* track_phi_pointer = &track_phi;
150  std::vector<float>* track_eta_pointer = &track_eta;
151  std::vector<float>* track_theta_pointer = &track_theta;
152  std::vector<float>* track_p_pointer = &track_p;
153  std::vector<float>* track_posx_pointer = &track_posx;
154  std::vector<float>* track_posy_pointer = &track_posy;
155  std::vector<float>* track_posz_pointer = &track_posz;
156  // Cluster
157  std::vector<float>* cluster_phi_pointer = &cluster_phi;
158  std::vector<float>* cluster_eta_pointer = &cluster_eta;
159  std::vector<float>* cluster_theta_pointer = &cluster_theta;
160  std::vector<float>* cluster_p_pointer = &cluster_p;
161  std::vector<float>* cluster_posx_pointer = &cluster_posx;
162  std::vector<float>* cluster_posy_pointer = &cluster_posy;
163  std::vector<float>* cluster_posz_pointer = &cluster_posz;
164  // Truth Particle
165  std::vector<float>* truth_phi_pointer = &truth_phi;
166  std::vector<float>* truth_eta_pointer = &truth_eta;
167  std::vector<float>* truth_theta_pointer = &truth_theta;
168  std::vector<float>* truth_p_pointer = &truth_p;
169  // -------------------------------//
170  // Set tree branch addresses
171  // -------------------------------//
172  // Track
173  t_cluster->SetBranchAddress("em_track_phi2cluster",&track_phi_pointer);
174  t_cluster->SetBranchAddress("em_track_eta2cluster",&track_eta_pointer);
175  t_cluster->SetBranchAddress("em_track_theta2cluster",&track_theta_pointer);
176  t_cluster->SetBranchAddress("em_track_ptotal",&track_p_pointer);
177  t_cluster->SetBranchAddress("em_track_x2cluster",&track_posx_pointer);
178  t_cluster->SetBranchAddress("em_track_y2cluster",&track_posy_pointer);
179  t_cluster->SetBranchAddress("em_track_z2cluster",&track_posz_pointer);
180  // Cluster
181  t_cluster->SetBranchAddress("em_cluster_phi",&cluster_phi_pointer);
182  t_cluster->SetBranchAddress("em_cluster_eta",&cluster_eta_pointer);
183  t_cluster->SetBranchAddress("em_cluster_theta",&cluster_theta_pointer);
184  t_cluster->SetBranchAddress("em_cluster_e",&cluster_p_pointer);
185  t_cluster->SetBranchAddress("em_cluster_posx",&cluster_posx_pointer);
186  t_cluster->SetBranchAddress("em_cluster_posy",&cluster_posy_pointer);
187  t_cluster->SetBranchAddress("em_cluster_posz",&cluster_posz_pointer);
188  // Truth Particle
189  t_truth->SetBranchAddress("em_evtgen_phi",&truth_phi_pointer);
190  t_truth->SetBranchAddress("em_evtgen_eta",&truth_eta_pointer);
191  t_truth->SetBranchAddress("em_evtgen_theta",&truth_theta_pointer);
192  t_truth->SetBranchAddress("em_evtgen_ptotal",&truth_p_pointer);
193  // -------------------------------//
194  // Set tree branch addresses
195  // -------------------------------//
196  Int_t nentries_truth = Int_t(t_truth->GetEntries());
197  Int_t nentries_cluster= Int_t(t_cluster->GetEntries());
198  Int_t nentries=0;
199  // Check if event_truth and event_cluster are equal
200 
201  if(nentries_truth!=nentries_cluster)
202  {
203  cout << "Warning: Entries in 'event_truth'("<<nentries_truth<<") does not equal Entries in 'event_cluster'("<<nentries_cluster<<"). Using the smaller of the two" << endl;
204  if(nentries_truth>nentries_cluster) nentries = nentries_cluster;
205  }
206  else
207  {
208  nentries = nentries_truth;
209  }
210 
211  // Check nEvents with nentries
212 
213  if(nEvents>nentries)
214  cout << "Warning : Number of Events specified to run is greater than entries in the tree, using all entries" << endl;
215  else if(nEvents>0)
216  nentries = nEvents;
217 
218  // Iterate through tree
219  for(Int_t entryInChain=0; entryInChain<nentries; entryInChain++)
220  {
221 
222  // Print out progress
223  if(entryInChain%1000==0&&verbosity>0)
224  cout << "Processing event " << entryInChain << " of " << nentries << endl;
225 
226  // Not sure what this line does
227 
228  Int_t entryInTree_truth = t_truth->LoadTree(entryInChain);
229  if (entryInTree_truth < 0) break;
230  Int_t entryInTree_cluster = t_cluster->LoadTree(entryInChain);
231  if (entryInTree_cluster < 0) break;
232 
233  // Get entries (filling vectors)
234 
235  t_truth->GetEntry(entryInChain);
236  t_cluster->GetEntry(entryInChain);
237 
238  // Iterate through all truth particles and see what we were able
239  // to come up with (Fill histograms)
240  for(unsigned k = 0; k<cluster_p.size(); k++)
241  {
242  if(cluster_eta.at(k)<-1.1)
243  {
244  h2_phi->Fill(track_phi.at(k),cluster_phi.at(k));
245  h2_eta->Fill(track_eta.at(k),cluster_eta.at(k));
246  h2_theta->Fill(track_theta.at(k),cluster_theta.at(k));
247  h2_mom->Fill(track_p.at(k),cluster_p.at(k));
248  h2_posx->Fill(track_posx.at(k),cluster_posx.at(k));
249  h2_posy->Fill(track_posy.at(k),cluster_posy.at(k));
250  h2_posz->Fill(track_posz.at(k),cluster_posz.at(k));
251  }
252  if(cluster_eta.at(k)>1)
253  {
254  h1_spatial_femc
255  ->Fill(sqrt( (track_posx.at(k)-cluster_posx.at(k))*
256  (track_posx.at(k)-cluster_posx.at(k))
257  +
258  (track_posy.at(k)-cluster_posy.at(k))*
259  (track_posy.at(k)-cluster_posy.at(k))
260  + 0*
261  (track_posz.at(k)-cluster_posz.at(k))*
262  (track_posz.at(k)-cluster_posz.at(k))));
263  }
264  else if(cluster_eta.at(k)<1&&cluster_eta.at(k)>-1.1)
265  {
266  h1_spatial_cemc
267  ->Fill(sqrt( (track_posx.at(k)-cluster_posx.at(k))*
268  (track_posx.at(k)-cluster_posx.at(k))
269  +
270  (track_posy.at(k)-cluster_posy.at(k))*
271  (track_posy.at(k)-cluster_posy.at(k))
272  +
273  (track_posz.at(k)-cluster_posz.at(k))*
274  (track_posz.at(k)-cluster_posz.at(k))));
275  }
276  else
277  {
278  h1_spatial_eemc
279  ->Fill(sqrt( (track_posx.at(k)-cluster_posx.at(k))*
280  (track_posx.at(k)-cluster_posx.at(k))
281  +
282  (track_posy.at(k)-cluster_posy.at(k))*
283  (track_posy.at(k)-cluster_posy.at(k))
284  + 0*
285  (track_posz.at(k)-cluster_posz.at(k))*
286  (track_posz.at(k)-cluster_posz.at(k))));
287 
288  }
289  }
290  }
291 }
292 void openFiles()
293 {
294 
295 }
296 
297 void openTrees()
298 {
299 
300 }
301 void BinLog(TH2F *h)
302 {
303 
304  TAxis *axis = h->GetXaxis();
305  int bins = axis->GetNbins();
306 
307  Axis_t from = axis->GetXmin();
308  Axis_t to = axis->GetXmax();
309  Axis_t width = (to - from) / bins;
310  Axis_t *new_bins = new Axis_t[bins + 1];
311 
312  for (int i = 0; i <= bins; i++) {
313  new_bins[i] = TMath::Power(10, from + i * width);
314  }
315  axis->Set(bins, new_bins);
316 
317  TAxis *axis2 = h->GetYaxis();
318  int bins2 = axis2->GetNbins();
319 
320  Axis_t from2 = axis2->GetXmin();
321  Axis_t to2 = axis2->GetXmax();
322  Axis_t width2 = (to2 - from2) / bins2;
323  Axis_t *new_bins2 = new Axis_t[bins2 + 1];
324 
325  for (int i = 0; i <= bins2; i++) {
326  new_bins2[i] = TMath::Power(10, from2 + i * width2);
327  }
328  axis2->Set(bins2, new_bins2);
329 
330  delete new_bins;
331  delete new_bins2;
332 }
333 
334 void hist_to_png_h1(TH1F * h_c, TH1F* h_e, TH1F* h_f, TString saveTitle, TString type_string)
335 {
336  TCanvas *cPNG = new TCanvas(saveTitle,"",700,500);
337  TImage *img = TImage::Create();
338  char * type = type_string.Data();
339  gStyle->SetOptStat(0);
340  gPad->SetLogy();
341  auto legend = new TLegend(0.7,0.7,0.9,0.9);
342  legend->SetTextSize(0.06);
343  legend->AddEntry(h_c,"CEMC","l");
344  legend->AddEntry(h_e,"EEMC","l");
345  legend->AddEntry(h_f,"FEMC","l");
346 
347  if(strcmp(type,"spatial")==0)
348  {
349  h_e->GetXaxis()->SetTitle("Extrapolation distance from cluster [cm]");
350  h_e->GetYaxis()->SetTitle("Counts");
351  }
352  h_e->Draw();
353  h_c->Draw("SAME");
354  h_f->Draw("SAME");
355  legend->Draw();
356  img->FromPad(cPNG);
357  img->WriteImage(saveTitle);
358 }
359 void hist_to_png(TH2F * h, TString saveTitle, TString type_string)
360 {
361  TCanvas *cPNG = new TCanvas(saveTitle,"",700,500);
362  TImage *img = TImage::Create();
363  char * type = type_string.Data();
364  gPad->SetLogz();
365  gStyle->SetOptStat(0);
366  if(strcmp(type,"phi")==0)
367  {
368  h->GetXaxis()->SetTitle("Track Phi");
369  h->GetYaxis()->SetTitle("Cluster Phi");
370  }
371  else if(strcmp(type,"eta")==0)
372  {
373  h->GetXaxis()->SetTitle("Track Eta");
374  h->GetYaxis()->SetTitle("Cluster Eta");
375  }
376  else if(strcmp(type,"theta")==0)
377  {
378  h->GetXaxis()->SetTitle("Track Theta");
379  h->GetYaxis()->SetTitle("Cluster Theta");
380  }
381  else if(strcmp(type,"mom")==0)
382  {
383  h->GetXaxis()->SetTitle("Track Momentum [GeV]");
384  h->GetYaxis()->SetTitle("Cluster Momentum [GeV]");
385  }
386  else if(strcmp(type,"posx")==0)
387  {
388  h->GetXaxis()->SetTitle("Track Position X [cm]");
389  h->GetYaxis()->SetTitle("Cluster Position X [cm]");
390  }
391  else if(strcmp(type,"posy")==0)
392  {
393  h->GetXaxis()->SetTitle("Track Position Y [cm]");
394  h->GetYaxis()->SetTitle("Cluster Position Y [cm]");
395  }
396  else if(strcmp(type,"posz")==0)
397  {
398  h->GetXaxis()->SetTitle("Track Position Z [cm]");
399  h->GetYaxis()->SetTitle("Cluster Position Z [cm]");
400  }
401  h->Draw("colz9");
402  img->FromPad(cPNG);
403  img->WriteImage(saveTitle);
404 
405  delete img;
406 }