Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jpsi_particle_plot.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file jpsi_particle_plot.C
1 // ----------------------------------------------------------------------------
2 // TFiles and TTrees
3 // ----------------------------------------------------------------------------
4 
5 TFile *f_1 = new TFile("/sphenix/user/gregtom3/data/Summer2018/ExclusiveReco_studies/latest_run/10x100_1M_dvmp.root","READ");
6 
7 TTree *t_1_exclusive = (TTree*)f_1->Get("event_exclusive");
8 TTree *t_1_reco = (TTree*)f_1->Get("event_reco");
9 TTree *t_1_truth = (TTree*)f_1->Get("event_truth");
10 
11 // ----------------------------------------------------------------------------
12 // Main Code
13 // ----------------------------------------------------------------------------
14 
16 {
17  gROOT->SetBatch(kTRUE);
18  // ----------------------------------------
19  // Creating Histograms
20  // ----------------------------------------
21  const int nbins = 100;
22  TH2F * h2_eta_phi_em_base = new TH2F("","",nbins,-4,4,nbins,-3.5,3.5);
23  TH2F * h2_eta_phi_pr_base = new TH2F("","",nbins,-20,20,nbins,-3.5,3.5);
24  TH2F * h2_mom_eta_em_base = new TH2F("","",nbins,-4,4,nbins,0,20);
25  TH2F * h2_mom_eta_pr_base = new TH2F("","",nbins,-20,20,nbins,0,150);
26  TH2F * h2_distance = new TH2F("","",nbins,-20,20,nbins,0,150);
27  // ----------------------------------------
28  // Setting Axes Titles
29  // ----------------------------------------
30  h2_eta_phi_em_base->GetXaxis()->SetTitle("#eta");
31  h2_eta_phi_em_base->GetYaxis()->SetTitle("#phi");
32  h2_eta_phi_pr_base->GetXaxis()->SetTitle("#eta");
33  h2_eta_phi_pr_base->GetYaxis()->SetTitle("#phi");
34 
35  h2_mom_eta_em_base->GetXaxis()->SetTitle("#eta");
36  h2_mom_eta_em_base->GetYaxis()->SetTitle("Momentum [GeV]");
37  h2_mom_eta_pr_base->GetXaxis()->SetTitle("#eta");
38  h2_mom_eta_pr_base->GetYaxis()->SetTitle("Momentum [GeV]");
39  // ----------------------------------------
40  // Writing out types of hisotgrams
41  // ----------------------------------------
42  std::vector<TString> types_eta_phi_em_reco;
43  std::vector<TString> types_eta_phi_em_true;
44  std::vector<TString> types_eta_phi_pr;
45  std::vector<TString> types_mom_eta_em_reco;
46  std::vector<TString> types_mom_eta_em_true;
47  std::vector<TString> types_mom_eta_pr;
48  types_eta_phi_em_reco.push_back("reco_decay_positron_eta_phi");
49  types_eta_phi_em_reco.push_back("reco_decay_electron_eta_phi");
50  types_eta_phi_em_reco.push_back("reco_scattered_electron_eta_phi");
51  types_mom_eta_em_reco.push_back("reco_decay_positron_mom_eta");
52  types_mom_eta_em_reco.push_back("reco_decay_electron_mom_eta");
53  types_mom_eta_em_reco.push_back("reco_scattered_electron_mom_eta");
54  types_eta_phi_em_true.push_back("true_decay_positron_eta_phi");
55  types_eta_phi_em_true.push_back("true_decay_electron_eta_phi");
56  types_eta_phi_em_true.push_back("true_scattered_electron_eta_phi");
57  types_eta_phi_pr.push_back("true_scattered_proton_eta_phi");
58  types_mom_eta_em_true.push_back("true_decay_positron_mom_eta");
59  types_mom_eta_em_true.push_back("true_decay_electron_mom_eta");
60  types_mom_eta_em_true.push_back("true_scattered_electron_mom_eta");
61  types_mom_eta_pr.push_back("true_scattered_proton_mom_eta");
62  // ----------------------------------------
63  // Filling histograms and saving them
64  // ----------------------------------------
65  for(unsigned idx = 0 ; idx < types_eta_phi_em_reco.size() ; idx++)
66  {
67  fillHist(h2_eta_phi_em_base, t_1_reco, t_1_truth, types_eta_phi_em_reco.at(idx), true);
68  hist_to_png(h2_eta_phi_em_base,"10x100", types_eta_phi_em_reco.at(idx));
69  }
70 
71  for(unsigned idx = 0 ; idx < types_eta_phi_em_true.size() ; idx++)
72  {
73  fillHist(h2_eta_phi_em_base, t_1_reco, t_1_truth, types_eta_phi_em_true.at(idx), false);
74  hist_to_png(h2_eta_phi_em_base,"10x100", types_eta_phi_em_true.at(idx));
75  }
76 
77  for(unsigned idx = 0 ; idx < types_eta_phi_pr.size() ; idx++)
78  {
79  fillHist(h2_eta_phi_pr_base, t_1_reco, t_1_truth, types_eta_phi_pr.at(idx), false);
80  hist_to_png(h2_eta_phi_pr_base,"10x100", types_eta_phi_pr.at(idx));
81  }
82 
83  for(unsigned idx = 0 ; idx < types_mom_eta_em_reco.size() ; idx++)
84  {
85  fillHist(h2_mom_eta_em_base, t_1_reco, t_1_truth, types_mom_eta_em_reco.at(idx), true);
86  hist_to_png(h2_mom_eta_em_base,"10x100", types_mom_eta_em_reco.at(idx));
87  }
88 
89  for(unsigned idx = 0 ; idx < types_mom_eta_em_true.size() ; idx++)
90  {
91  fillHist(h2_mom_eta_em_base, t_1_reco, t_1_truth, types_mom_eta_em_true.at(idx), false);
92  hist_to_png(h2_mom_eta_em_base,"10x100", types_mom_eta_em_true.at(idx));
93  }
94 
95 
96  for(unsigned idx = 0 ; idx < types_mom_eta_pr.size() ; idx++)
97  {
98  fillHist(h2_mom_eta_pr_base, t_1_reco, t_1_truth, types_mom_eta_pr.at(idx), false);
99  hist_to_png(h2_mom_eta_pr_base,"10x100", types_mom_eta_pr.at(idx));
100  }
101  return 0;
102 }
103 
104 void fillHist(TH2F * h, TTree *t_reco, TTree *t_truth, TString type_TString, bool reco)
105 {
106  // Create all vectors from tree
107  std::vector<float> reco_eta;
108  std::vector<float> true_eta;
109  std::vector<float> reco_phi;
110  std::vector<float> true_phi;
111  std::vector<float> reco_ptotal;
112  std::vector<float> true_ptotal;
113  std::vector<int> reco_charge;
114  std::vector<int> pid;
115  std::vector<bool> reco_is_scattered_lepton;
116  std::vector<bool> is_scattered_lepton;
117  std::vector<float> reco_cluster_e;
118 
119  // Point to these vectors
120  std::vector<float>* reco_eta_pointer = &reco_eta;
121  std::vector<float>* true_eta_pointer = &true_eta;
122  std::vector<float>* reco_phi_pointer = &reco_phi;
123  std::vector<float>* true_phi_pointer = &true_phi;
124  std::vector<float>* reco_ptotal_pointer = &reco_ptotal;
125  std::vector<float>* true_ptotal_pointer = &true_ptotal;
126  std::vector<int>* reco_charge_pointer = &reco_charge;
127  std::vector<int>* pid_pointer = &pid;
128  std::vector<bool>* reco_is_scattered_lepton_pointer = &reco_is_scattered_lepton;
129  std::vector<bool>* is_scattered_lepton_pointer = &is_scattered_lepton;
130  std::vector<float>* reco_cluster_e_pointer = &reco_cluster_e;
131 
132  // Empty out histogram
133  h->Reset();
134 
135  // Set branch addresses
136  t_reco->SetBranchAddress("eta",&reco_eta_pointer);
137  t_reco->SetBranchAddress("phi",&reco_phi_pointer);
138  t_reco->SetBranchAddress("ptotal",&reco_ptotal_pointer);
139  t_reco->SetBranchAddress("charge",&reco_charge_pointer);
140  t_reco->SetBranchAddress("is_scattered_lepton",&reco_is_scattered_lepton_pointer);
141  t_reco->SetBranchAddress("cluster_e",&reco_cluster_e_pointer);
142 
143  t_truth->SetBranchAddress("geta",&true_eta_pointer);
144  t_truth->SetBranchAddress("gphi",&true_phi_pointer);
145  t_truth->SetBranchAddress("gptotal",&true_ptotal_pointer);
146  t_truth->SetBranchAddress("pid",&pid_pointer);
147  t_truth->SetBranchAddress("is_scattered_lepton",&is_scattered_lepton_pointer);
148  // Are we filling in reco branches?
149  if(reco)
150  {
151  // Get number of entries in reco tree
152  Int_t nentries = Int_t(t_reco->GetEntries());
153 
154  // Get comparison string for knowing what to fill
155  char * type = type_TString.Data();
156  int count = 0;
157  // Loop over the tree
158  for(Int_t entryInChain=0; entryInChain<nentries; entryInChain++)
159  {
160  Int_t entryInTree = t_reco->LoadTree(entryInChain);
161  if (entryInTree < 0) break;
162  t_reco->GetEntry(entryInChain);
163  t_truth->GetEntry(entryInChain);
164  if(entryInChain%1000==0)
165  cout << "Processing event " << count << " of " << nentries << endl;
166  count++;
167  // Size of reco branch event
168  unsigned size_reco = reco_eta.size();
169 
170  // Loop over a single event
171  for(unsigned i = 0 ; i < size_reco ; i++)
172  {
173  // Booleans for knowing scatter vs decay electrons
174  bool is_scatter = reco_is_scattered_lepton.at(i);
175  bool is_decay = !is_scatter;
176 
177 
178  // Make sure scatter electron eta reco vs. true <0.2
179  for(unsigned temp = 0 ; temp < true_eta.size() ; temp++)
180  {
181  bool is_true_scatter = is_scattered_lepton.at(temp);
182  float eta_diff = abs(reco_eta.at(i)-true_eta.at(temp));
183  if(is_true_scatter&&is_scatter&&eta_diff>0.2)
184  {
185  is_scatter = false;
186  }
187  }
188 
189  // Ensure charge of electron and positron using truth info
190 
191  for(unsigned temp = 0 ; temp < true_eta.size() ; temp++)
192  {
193  float phi_diff = abs(reco_phi.at(i)-true_phi.at(temp));
194  float eta_diff = abs(reco_eta.at(i)-true_eta.at(temp));
195  if(phi_diff<0.5&&eta_diff<0.2&&pid.at(temp)==11)
196  reco_charge.at(i)==-1;
197  else if(phi_diff<0.5&&eta_diff<0.2&&pid.at(temp)==-11)
198  reco_charge.at(i)==1;
199  }
200 
201  // If the particle has an eta < -1.434, set the momentum of the particle to cluster energy
202  if(reco_eta.at(i)<-1.434)
203  reco_ptotal.at(i) = reco_cluster_e.at(i);
204 
205 
206  // Use char * type to select what we fill
207  if(strcmp(type,"reco_decay_positron_eta_phi")==0)
208  {
209  if(reco_charge.at(i)==1)
210  h->Fill(reco_eta.at(i),reco_phi.at(i));
211  }
212  else if(strcmp(type,"reco_decay_electron_eta_phi")==0)
213  {
214  if(reco_charge.at(i)==-1&&is_decay)
215  h->Fill(reco_eta.at(i),reco_phi.at(i));
216 
217  }
218  else if(strcmp(type,"reco_scattered_electron_eta_phi")==0)
219  {
220  if(reco_charge.at(i)==-1&&is_scatter)
221  h->Fill(reco_eta.at(i),reco_phi.at(i));
222  }
223  else if(strcmp(type,"reco_decay_positron_mom_eta")==0)
224  {
225  if(reco_charge.at(i)==1)
226  h->Fill(reco_eta.at(i),reco_ptotal.at(i));
227  }
228  else if(strcmp(type,"reco_decay_electron_mom_eta")==0)
229  {
230  if(reco_charge.at(i)==-1&&is_decay)
231  h->Fill(reco_eta.at(i),reco_ptotal.at(i));
232  }
233  else if(strcmp(type,"reco_scattered_electron_mom_eta")==0)
234  {
235  if(reco_charge.at(i)==-1&&is_scatter)
236  h->Fill(reco_eta.at(i),reco_ptotal.at(i));
237  }
238  }
239  }
240  }
241  else
242  {
243  Int_t nentries = Int_t(t_truth->GetEntries());
244  char * type = type_TString.Data();
245  for(Int_t entryInChain=0; entryInChain<nentries; entryInChain++)
246  {
247  Int_t entryInTree = t_truth->LoadTree(entryInChain);
248  if (entryInTree < 0) break;
249  t_truth->GetEntry(entryInChain);
250 
251  unsigned size_truth = true_eta.size();
252 
253  for(unsigned i = 0 ; i < size_truth ; i++)
254  {
255 
256  bool is_scatter = is_scattered_lepton.at(i);
257  bool is_decay = !is_scatter;
258 
259  if(strcmp(type,"true_decay_positron_eta_phi")==0)
260  {
261  if(pid.at(i)==-11)
262  h->Fill(true_eta.at(i),true_phi.at(i));
263  }
264  else if(strcmp(type,"true_decay_electron_eta_phi")==0)
265  {
266  if(pid.at(i)==11&&is_decay)
267  h->Fill(true_eta.at(i),true_phi.at(i));
268  }
269  else if(strcmp(type,"true_scattered_electron_eta_phi")==0)
270  {
271  if(pid.at(i)==11&&is_scatter)
272  h->Fill(true_eta.at(i),true_phi.at(i));
273  }
274  else if(strcmp(type,"true_decay_positron_mom_eta")==0)
275  {
276  if(pid.at(i)==-11&&is_decay)
277  h->Fill(true_eta.at(i),true_ptotal.at(i));
278  }
279  else if(strcmp(type,"true_decay_electron_mom_eta")==0)
280  {
281  if(pid.at(i)==11&&is_decay)
282  h->Fill(true_eta.at(i),true_ptotal.at(i));
283  }
284  else if(strcmp(type,"true_scattered_electron_mom_eta")==0)
285  {
286  if(pid.at(i)==11&&is_scatter)
287  h->Fill(true_eta.at(i),true_ptotal.at(i));
288  }
289  else if(strcmp(type,"true_scattered_proton_mom_eta")==0)
290  {
291  if(pid.at(i)==2212)
292  h->Fill(true_eta.at(i),true_ptotal.at(i));
293  }
294  else if(strcmp(type,"true_scattered_proton_eta_phi")==0)
295  {
296  if(pid.at(i)==2212)
297  h->Fill(true_eta.at(i),true_phi.at(i));
298  }
299  } // end loop over event
300  } // end loop over tree
301  } // end if-else for selecting reco or truth filling
302 } // end fillHist
303 
304 void hist_to_png(TH2F * h, TString saveTitle, TString type2)
305 {
306  saveTitle = type2 + "_" + saveTitle+".png";
307  TCanvas *cPNG = new TCanvas(saveTitle,"",700,500);
308  TImage *img = TImage::Create();
309  gStyle->SetOptStat(0);
310  gPad->SetLogz();
311  char * type = type2.Data();
312  // Title the histogram //
313  if(strcmp(type,"reco_decay_electron_eta_phi")==0)
314  {
315  h->SetTitle("#eta-#phi space : decay e^{-} reco");
316  }
317  else if(strcmp(type,"reco_decay_positron_eta_phi")==0)
318  {
319  h->SetTitle("#eta-#phi space : decay e^{+} reco");
320  }
321  else if(strcmp(type,"reco_scattered_electron_eta_phi")==0)
322  {
323  h->SetTitle("#eta-#phi space : scattered e^{-} reco");
324  }
325  else if(strcmp(type,"reco_decay_electron_mom_eta")==0)
326  {
327  h->SetTitle("Momentum vs Eta : decay e^{-} reco");
328  }
329  else if(strcmp(type,"reco_decay_positron_mom_eta")==0)
330  {
331  h->SetTitle("Momentum vs Eta : decay e^{+} reco");
332  }
333  else if(strcmp(type,"reco_scattered_electron_mom_eta")==0)
334  {
335  h->SetTitle("Momentum vs Eta : scattered e^{-} reco");
336  }
337  else if(strcmp(type,"true_decay_electron_eta_phi")==0)
338  {
339  h->SetTitle("#eta-#phi space : decay e^{-} truth");
340  }
341  else if(strcmp(type,"true_decay_positron_eta_phi")==0)
342  {
343  h->SetTitle("#eta-#phi space : decay e^{+} truth");
344  }
345  else if(strcmp(type,"true_scattered_electron_eta_phi")==0)
346  {
347  h->SetTitle("#eta-#phi space : scattered e^{-} truth");
348  }
349  else if(strcmp(type,"true_decay_electron_mom_eta")==0)
350  {
351  h->SetTitle("Momentum vs Eta : decay e^{-} truth");
352  }
353  else if(strcmp(type,"true_decay_positron_mom_eta")==0)
354  {
355  h->SetTitle("Momentum vs Eta : decay e^{+} truth");
356  }
357  else if(strcmp(type,"true_scattered_electron_mom_eta")==0)
358  {
359  h->SetTitle("Momentum vs Eta : scattered e^{-} truth");
360  }
361  else if(strcmp(type,"true_scattered_proton_mom_eta")==0)
362  {
363  h->SetTitle("Momentum vs Eta : scattered proton truth");
364  }
365  else if(strcmp(type,"true_scattered_proton_eta_phi")==0)
366  {
367  h->SetTitle("#eta-#phi space : scattered proton truth");
368  }
369 
370  h->Draw("colz");
371 
372  img->FromPad(cPNG);
373  img->WriteImage(saveTitle);
374  delete img;
375 }