WCSim
verification_HitsChargeTime.C
Go to the documentation of this file.
1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include "TH1F.h"
6 #include "TROOT.h"
7 #include "TStyle.h"
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "TCanvas.h"
11 #include "TLegend.h"
12 #include "TSystem.h"
13 
14 #include "WCSimRootEvent.hh"
15 
16 // Simple example of reading a generated Root file
17 int verification_HitsChargeTime(const char *filename="wcsimtest.root", const char *filename2="../../WCSim_clean/verification-test-scripts/wcsimtest.root", bool verbose=false)
18 {
19  // Clear global scope
20  //gROOT->Reset();
21 
22  TFile *f = new TFile(filename,"read");
23  if (!f->IsOpen()){
24  cout << "Error, could not open input file: " << filename << endl;
25  return -1;
26  }
27 
28  TFile *f2 = new TFile(filename2,"read");
29  if (!f2->IsOpen()){
30  cout << "Error, could not open input file: " << filename2 << endl;
31  return -1;
32  }
33 
34 
35  TTree *wcsimT = (TTree*)f->Get("wcsimT");
36  int nevent = wcsimT->GetEntries();
37  TTree *wcsimT2 = (TTree*)f2->Get("wcsimT");
38  int nevent2 = wcsimT2->GetEntries();
39 
40  // Create a WCSimRootEvent to put stuff from the tree in and set the branch address for reading from the tree
41  WCSimRootEvent *wcsimrootsuperevent = new WCSimRootEvent();
42  wcsimT->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent);
43  WCSimRootEvent *wcsimrootsuperevent2 = new WCSimRootEvent();
44  wcsimT2->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent2);
45 
46  // Force deletion to prevent memory leak when issuing multiple calls to GetEvent()
47  wcsimT->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
48  wcsimT2->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
49 
50  // Print the first event from the modified WCSim version
51  wcsimT->GetEvent(0);
52  WCSimRootTrigger *wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
53  cout << "Stats for the first event in your version of WCSim using " << filename << endl;
54  cout << "Number of tube hits " << wcsimrootevent->GetNumTubesHit() << endl;
55  cout << "Number of digitized tube hits " << wcsimrootevent->GetNumDigiTubesHit() << endl;
56  cout << "Number of photoelectron hit times " << wcsimrootevent->GetCherenkovHitTimes()->GetEntries() << endl;
57 
58  // Print the first event from the "clean" WCSim version
59  wcsimT2->GetEvent(0);
60  WCSimRootTrigger *wcsimrootevent2 = wcsimrootsuperevent2->GetTrigger(0);
61  cout << "***********************************************************" << endl;
62  cout << "Stats for the first event of WCSim version on GitHub using "<< filename2 << endl;
63  cout << "Number of tube hits " << wcsimrootevent2->GetNumTubesHit() << endl;
64  cout << "Number of digitized tube hits " << wcsimrootevent2->GetNumDigiTubesHit() << endl;
65  cout << "Number of photoelectron hit times " << wcsimrootevent2->GetCherenkovHitTimes()->GetEntries() << endl;
66 
67  // Compare these two events
68  cout << "***********************************************************" << endl;
69  if (abs(wcsimrootevent->GetNumTubesHit() - wcsimrootevent2->GetNumTubesHit())>1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of hit tubes do not match" << endl;}
70  else {cout << "FIRST EVENT TEST PASSED: Number of hit tubes matches" << endl;}
71  if (abs(wcsimrootevent->GetNumDigiTubesHit() - wcsimrootevent2->GetNumDigiTubesHit())>1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of digitized tubes do not match" << endl; }
72  else {cout << "FIRST EVENT TEST PASSED: Number of digitized tubes matches" << endl; }
73  if (abs(wcsimrootevent->GetCherenkovHitTimes()->GetEntries() - wcsimrootevent2->GetCherenkovHitTimes()->GetEntries())> 1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of hit times do not match" << endl;}
74  else {cout << "FIRST EVENT TEST PASSED: Number of hit times matches" << endl;}
75 
76  if (nevent != nevent2) {
77  cout << "***********************************************************" << endl;
78  cout << "The input files don’t contain the same number of events. Only the first events were compared. To see histograms of the number of hits, deposited charge and hit time, please choose two input files which contain the same number of events." << endl;
79  return -1;
80  }
81 
82 
83  // Histograms for the modified WCSim version
84  TH1F *hits = new TH1F("PMT Hits", "# Digitized Hits", 500, 0, 3000);
85  TH1F *time = new TH1F("Average Time", "Average Time", 600, 900, 2000);
86  TH1F *charge = new TH1F("Q/# Digitized PMT", "Average Charge", 200, 0, 5);
87  // ... and for the "clean" version
88  TH1F *hits2 = new TH1F("PMT Hits 2", "Digitized Hits", 500, 0, 3000);
89  TH1F *time2 = new TH1F("Average Time 2", "Average Time", 600, 900, 2000);
90  TH1F *charge2 = new TH1F("Q/# Digitized PMT 2", "Average Charge", 200, 0, 5);
91 
92 
93  // Now loop over events from the modified WCSim version and fill the histograms
94  for (int ev=0; ev<nevent; ev++){
95  // Read the event from the tree into the WCSimRootEvent instance
96  wcsimT->GetEvent(ev);
97  wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
98  if(verbose){
99  printf("********************************************************");
100  printf("Evt, date %d %d\n", wcsimrootevent->GetHeader()->GetEvtNum(),
101  wcsimrootevent->GetHeader()->GetDate());
102  printf("Mode %d\n", wcsimrootevent->GetMode());
103  printf("Number of subevents %d\n",
104  wcsimrootsuperevent->GetNumberOfSubEvents());
105 
106  printf("Vtxvol %d\n", wcsimrootevent->GetVtxvol());
107  printf("Vtx %f %f %f\n", wcsimrootevent->GetVtx(0),
108  wcsimrootevent->GetVtx(1),wcsimrootevent->GetVtx(2));
109  }
110 
111 
112  //Calculate distance vertex to center, in z plane
113  // dWall
114  // muon range
115  // end point to center
116  double vtx_x = wcsimrootevent->GetVtx(0);
117  double vtx_y = wcsimrootevent->GetVtx(1);
118  double vtx_z = wcsimrootevent->GetVtx(2);
119  double dist = sqrt(vtx_x*vtx_x + vtx_y * vtx_y);
120 
121  for (int index = 0 ; index < wcsimrootsuperevent->GetNumberOfEvents(); index++){
122  wcsimrootevent = wcsimrootsuperevent->GetTrigger(index);
123  int ncherenkovdigihits = wcsimrootevent->GetNcherenkovdigihits();
124  int ncherenkovdigihits_slots = wcsimrootevent->GetNcherenkovdigihits_slots();
125  hits->Fill(ncherenkovdigihits);
126 
127 
128  float totalq = 0.;
129  float totalt = 0.;
130 
131  std::cout << ncherenkovdigihits << std::endl;
132  std::cout << wcsimrootevent->GetNumTubesHit() << std::endl;
133  //TH1F *occup_per_event = new TH1F("occup_per_event","",20000,0,20000);
134  // Loop through elements in the TClonesArray of WCSimRootCherenkovHits
135  for (int i=0; i< ncherenkovdigihits_slots; i++){
136  TObject *Digi = (wcsimrootevent->GetCherenkovDigiHits())->At(i);
137  if(!Digi)
138  continue;
139  WCSimRootCherenkovDigiHit *wcsimrootcherenkovdigihit =
140  dynamic_cast<WCSimRootCherenkovDigiHit*>(Digi);
141 
142  float q = wcsimrootcherenkovdigihit->GetQ();
143  float t = wcsimrootcherenkovdigihit->GetT();
144  charge->Fill(q);
145  ttime->Fill(t);
146  t_q->Fill(t,q);
147  occupancy->Fill(tubeNumber);
148  //occup_per_event->Fill(tubeNumber);
149  occupancy_mPMT->Fill(tubeNumber%33 == 0 ? 33 : tubeNumber%33);
150  occupancy_mPMT2->Fill(tubeNumber/33);
151  totalq+=q;
152  totalt+=t;
153  }
154  float av_time = (ncherenkovdigihits > 0) ? totalt/ncherenkovdigihits : 0;
155  float av_q = (ncherenkovdigihits > 0) ? totalq/ncherenkovdigihits : 0;
156  charge->Fill(av_q);
157  time->Fill(av_time);
158  }
159 
160  // reinitialize super event between loops.
161  wcsimrootsuperevent->ReInitialize();
162  }// End of loop over events
163 
164 
165  for (int index = 0 ; index < wcsimrootsuperevent2->GetNumberOfEvents(); index++){
166  wcsimrootevent2 = wcsimrootsuperevent2->GetTrigger(index);
167  int ncherenkovdigihits = wcsimrootevent2->GetNcherenkovdigihits();
168  hits2->Fill(ncherenkovdigihits);
169 
170  float totalq = 0.;
171  float totalt = 0.;
172  // Loop through elements in the TClonesArray of WCSimRootCherenkovHits
173  for (int i=0; i< ncherenkovdigihits; i++){
174  TObject *Digi = (wcsimrootevent2->GetCherenkovDigiHits())->At(i);
175  WCSimRootCherenkovDigiHit *wcsimrootcherenkovdigihit =
176  dynamic_cast<WCSimRootCherenkovDigiHit*>(Digi);
177 
178  float q = wcsimrootcherenkovdigihit->GetQ();
179  float t = wcsimrootcherenkovdigihit->GetT();
180  charge2->Fill(q);
181  ttime2->Fill(t);
182  totalq+=q;
183  totalt+=t;
184  }
185  float av_time = (ncherenkovdigihits > 0) ? totalt/ncherenkovdigihits : 0;
186  float av_q = (ncherenkovdigihits > 0) ? totalq/ncherenkovdigihits : 0;
187  charge2->Fill(av_q);
188  time2->Fill(av_time);
189  }
190 
191  // reinitialize super event between loops.
192  wcsimrootsuperevent2->ReInitialize();
193  }// End of loop over events
194 
195  Double_t ks_hits = hits->KolmogorovTest(hits2);
196  Double_t ks_charge = charge->KolmogorovTest(charge2);
197  Double_t ks_time = time->KolmogorovTest(time2);
198  cout << "***********************************************************" << endl;
199  cout << "ks test for # of digitized hits: " << ks_hits << endl;
200  cout << "ks test for average charge: " << ks_charge << endl;
201  cout << "ks test for average time: " << ks_time << endl;
202 
203  // TCanvas c1("c1");
204  float win_scale = 0.75;
205  int n_wide(2);
206  int n_high(2);
207  TCanvas* c1 = new TCanvas("c1", "Test Plots", 500*n_wide*win_scale, 500*n_high*win_scale);
208  c1->Draw();
209  c1->Divide(2,2);
210  c1->cd(1);
211  hits2->SetLineColor(kRed);
212  hits->Draw();
213  c1->cd(1); hits2->Draw("SAME");
214 
215  TLegend *leg = new TLegend(0.2,0.7,0.55,0.85, "");
216  leg->SetFillColor(0);
217  leg->SetBorderSize(0);
218  leg->AddEntry(hits,filename, "l");
219  leg->AddEntry(hits2,filename2, "l");
220  leg->Draw();
221 
222  c1->cd(2);
223  charge->GetXaxis()->SetTitle("Total Charge / # digitized hits");
224  charge->Draw();
225  charge2->SetLineColor(kRed);
226  c1->cd(2); charge2->Draw("SAME");
227  c1->cd(3);
228  time->GetXaxis()->SetTitle("Total Time / # digitized hits (ns)");
229  time->Draw();
230  time2->SetLineColor(kRed);
231  c1->cd(3); time2->Draw("SAME");
232 
233  c1->cd(4);
234  hit_pmts->Draw();
235  hit_pmts2->SetLineColor(kRed);
236  hit_pmts2->Draw("same");
237 
238  TCanvas* c2 = new TCanvas("c2", "Test Plots", 500*n_wide*win_scale, 500*n_high*win_scale);
239  gStyle->SetOptStat(1);
240  gStyle->SetOptFit(111);
241  /* TF1 *func = new TF1("func","gaus(0)",0,20000);
242  func->SetParameters(10,5000,10);
243  TF1 *func2 = new TF1("func2","gaus(0)",0,20000);
244  func2->SetParameters(10,5000,10); */
245  tot_charge->Draw();
246  //tot_charge->Fit("gaus","","",tot_charge->GetMean()-1.5*tot_charge->GetRMS(),tot_charge->GetMean()+1.5*tot_charge->GetRMS());
247  tot_charge2->SetLineColor(kRed);
248  tot_charge2->Draw("same");
249  //tot_charge2->Fit("gaus","","",tot_charge->GetMean()-1.5*tot_charge->GetRMS(),tot_charge->GetMean()+1.5*tot_charge->GetRMS());
250  //std::cout << "Tot charge ratio : " << func->GetParameter(0)/func2->GetParameter(0) << std::endl;
251 
252  TCanvas *c3 = new TCanvas();
253  c3->Divide(2,2);
254  c3->cd(1);
255  occupancy_mPMT2->Draw();
256  c3->cd(2);
257  occupancy_mPMT->Draw();
258  c3->cd(3);
259  ttime->Draw();
260  ttime2->SetLineColor(kRed);
261  ttime2->Draw("same");
262  c3->cd(4);
263  charge->Draw();
264  charge2->SetLineColor(kRed);
265  charge2->Draw("same");
266 
267 
268  TCanvas *c4 = new TCanvas();
269 
270  nhit_pmt->Draw();
271 
272  TCanvas *c5 = new TCanvas();
273  t_q->Draw("colz");
274 
275  return 0;
276 }
TCanvas * c3
Int_t GetNumberOfSubEvents() const
Class holding digitised hit (aka digit or digi) information (after PMT & electronics simulation + tri...
Int_t GetNumDigiTubesHit() const
TCanvas * c4
double q[MAX_N_ACTIVE_TUBES]
Definition: jhfNtuple.h:44
int64_t GetDate() const
WCSimRootEventHeader * GetHeader()
Int_t GetVtxvol() const
Float_t GetVtx(Int_t i=0)
Int_t GetNcherenkovdigihits() const
int verification_HitsChargeTime(const char *filename="wcsimtest.root", const char *filename2="../../WCSim_clean/verification-test-scripts/wcsimtest.root", bool verbose=false)
Class storing trigger information.
WCSimRootTrigger * GetTrigger(int number)
TCanvas * c2
Int_t GetEvtNum() const
TClonesArray * GetCherenkovHitTimes() const
Int_t GetMode() const
Int_t GetNumTubesHit() const
string filename
Definition: MakeKin.py:235
Int_t GetNumberOfEvents() const
Class containing event information.
TCanvas * c5
double t[MAX_N_ACTIVE_TUBES]
Definition: jhfNtuple.h:45
TLegend * leg
Int_t GetNcherenkovdigihits_slots() const
Double_t ks_hits
TClonesArray * GetCherenkovDigiHits() const
Double_t ks_charge
Double_t ks_time