WCSim
verification_HitsChargeTime_2.C
Go to the documentation of this file.
1 #include <iostream>
2 #include <TH1F.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 // Simple example of reading a generated Root file
6 void verification_HitsChargeTime(char *filename="wcsimtest.root", char *filename2="../../WCSim_clean/verification-test-scripts/wcsimtest.root", bool verbose=false)
7 {
8  // Clear global scope
9  //gROOT->Reset();
10 
11  gStyle->SetOptStat(0);
12  gStyle->SetCanvasColor(0);
13  gStyle->SetTitleColor(1);
14  gStyle->SetStatColor(0);
15  gStyle->SetFrameFillColor(0);
16  gStyle->SetPadColor(0);
17  gStyle->SetPadTickX(1);
18  gStyle->SetPadTickY(1);
19  gStyle->SetTitleSize(0.04);
20  gStyle->SetCanvasBorderMode(0);
21  gStyle->SetFrameBorderMode(0);
22  gStyle->SetFrameLineWidth(2);
23  gStyle->SetPadBorderMode(0);
24  gStyle->SetPalette(1);
25  gStyle->SetTitleAlign(23);
26  gStyle->SetTitleX(.5);
27  gStyle->SetTitleY(0.99);
28  gStyle->SetTitleBorderSize(0);
29  gStyle->SetTitleFillColor(0);
30  gStyle->SetHatchesLineWidth(2);
31  gStyle->SetLineWidth(1.5);
32  gStyle->SetTitleFontSize(0.07);
33  gStyle->SetLabelSize(0.05,"X");
34  gStyle->SetLabelSize(0.05,"Y");
35  gStyle->SetTitleSize(0.04,"X");
36  gStyle->SetTitleSize(0.04,"Y");
37  gStyle->SetTitleBorderSize(0);
38  gStyle->SetCanvasBorderMode(0);
39 
40 
41  // Load the library with class dictionary info
42  // (create with "gmake shared")
43  char* wcsimdirenv;
44  wcsimdirenv = getenv ("WCSIMDIR");
45  if(wcsimdirenv != NULL){
46  gSystem->Load("${WCSIMDIR}/libWCSimRoot.so");
47  }else{
48  gSystem->Load("../libWCSimRoot.so");
49  }
50 
51  TFile *f = new TFile(filename,"read");
52  if (!f->IsOpen()){
53  cout << "Error, could not open input file: " << filename << endl;
54  return -1;
55  }
56 
57  TFile *f2 = new TFile(filename2,"read");
58  if (!f2->IsOpen()){
59  cout << "Error, could not open input file: " << filename2 << endl;
60  return -1;
61  }
62 
63 
64  TTree *wcsimT = f->Get("wcsimT");
65  int nevent = wcsimT->GetEntries();
66  TTree *wcsimT2 = f2->Get("wcsimT");
67  int nevent2 = wcsimT2->GetEntries();
68 
69  // Create a WCSimRootEvent to put stuff from the tree in and set the branch address for reading from the tree
70  WCSimRootEvent *wcsimrootsuperevent = new WCSimRootEvent();
71  wcsimT->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent);
72  WCSimRootEvent *wcsimrootsuperevent2 = new WCSimRootEvent();
73  wcsimT2->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent2);
74 
75  // Force deletion to prevent memory leak when issuing multiple calls to GetEvent()
76  wcsimT->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
77  wcsimT2->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
78 
79  // Print the first event from the modified WCSim version
80  wcsimT->GetEvent(0);
81  WCSimRootTrigger *wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
82  cout << "Stats for the first event in your version of WCSim using " << filename << endl;
83  cout << "Number of tube hits " << wcsimrootevent->GetNumTubesHit() << endl;
84  cout << "Number of digitized tube hits " << wcsimrootevent->GetNumDigiTubesHit() << endl;
85  cout << "Number of photoelectron hit times " << wcsimrootevent->GetCherenkovHitTimes()->GetEntries() << endl;
86 
87  // Print the first event from the "clean" WCSim version
88  wcsimT2->GetEvent(0);
89  WCSimRootTrigger *wcsimrootevent2 = wcsimrootsuperevent2->GetTrigger(0);
90  cout << "***********************************************************" << endl;
91  cout << "Stats for the first event of WCSim version on GitHub using "<< filename2 << endl;
92  cout << "Number of tube hits " << wcsimrootevent2->GetNumTubesHit() << endl;
93  cout << "Number of digitized tube hits " << wcsimrootevent2->GetNumDigiTubesHit() << endl;
94  cout << "Number of photoelectron hit times " << wcsimrootevent2->GetCherenkovHitTimes()->GetEntries() << endl;
95 
96  // Compare these two events
97  cout << "***********************************************************" << endl;
98  if (abs(wcsimrootevent->GetNumTubesHit() - wcsimrootevent2->GetNumTubesHit())>1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of hit tubes do not match" << endl;}
99  else {cout << "FIRST EVENT TEST PASSED: Number of hit tubes matches" << endl;}
100  if (abs(wcsimrootevent->GetNumDigiTubesHit() - wcsimrootevent2->GetNumDigiTubesHit())>1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of digitized tubes do not match" << endl; }
101  else {cout << "FIRST EVENT TEST PASSED: Number of digitized tubes matches" << endl; }
102  if (abs(wcsimrootevent->GetCherenkovHitTimes()->GetEntries() - wcsimrootevent2->GetCherenkovHitTimes()->GetEntries())> 1.0e-6){cout << "FIRST EVENT TEST FAILED: Number of hit times do not match" << endl;}
103  else {cout << "FIRST EVENT TEST PASSED: Number of hit times matches" << endl;}
104 
105  if (nevent != nevent2) {
106  cout << "***********************************************************" << endl;
107  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;
108  return -1;
109  }
110 
111 
112  // Histograms for the modified WCSim version
113  TH1F *hits = new TH1F("PMT Hits", "# Digitized Hits", 500, 0, 3000);
114  TH1F *time = new TH1F("Average Time", "Average Time", 600, 900, 2000);
115  TH1F *charge = new TH1F("Q/# Digitized PMT", "Average Charge", 200, 0, 5);
116  // ... and for the "clean" version
117  TH1F *hits2 = new TH1F("PMT Hits 2", "Digitized Hits", 500, 0, 3000);
118  TH1F *time2 = new TH1F("Average Time 2", "Average Time", 600, 900, 2000);
119  TH1F *charge2 = new TH1F("Q/# Digitized PMT 2", "Average Charge", 200, 0, 5);
120 
121 
122  TH1F *h1 = new TH1F("PMT Hits", "# Digitized Hits", 500, 0, 25000);
123  TH1F *time = new TH1F("Average time", "Average time", 600, 500, 5000);
124  TH1F *pe = new TH1F("Q/# Digitzed PMT", "Average Charge", 200, 0, 5);
125  TH1F *hit_pmts = new TH1F("Hit PMTs","# Hit PMTs",500,0,35000);
126 
127  TH1F *tot_charge = new TH1F("tot","Total Charge in event",500,0,20000);
128 
129  TH1F *charge = new TH1F("charge","",200,0,1000);
130  TH1F *ttime = new TH1F("ttime","",200,900,2000);
131  TH2F *t_q = new TH2F("tq","",200,900,2000,200,0,20);
132  //TH2F *tubeID_q = new TH2F("tubeIDq","",150,185100,185250,200,0,100);
133  TH2F *tubeID_q = new TH2F("tubeIDq","",300,1400,1700,100,0,300);
134  TH2F *mPMTid_q = new TH2F("mPMTidq","",34,1,35,100,0,300);
135  TH1F *occupancy = new TH1F("occupancy","",20000,0,20000);
136  TH1F *occupancy_mPMT = new TH1F("occupancy_mPMT","",35,0,35);
137  TH1F *occupancy_mPMT2 = new TH1F("occupancy_mPMT2","",200,0,20000);
138  TH1F *nhit_pmt = new TH1F("nhit_pmt","",30,0,30);
139  std::cout << "nevent: " << nevent << std::endl;
140  // Now loop over events
141  for (int ev=0; ev<nevent; ev++)
142  {
143  // Read the event from the tree into the WCSimRootEvent instance
144  wcsimT->GetEvent(ev);
145  wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
146  if(verbose){
147  printf("********************************************************");
148  printf("Evt, date %d %d\n", wcsimrootevent->GetHeader()->GetEvtNum(),
149  wcsimrootevent->GetHeader()->GetDate());
150  printf("Mode %d\n", wcsimrootevent->GetMode());
151  printf("Number of subevents %d\n",
152  wcsimrootsuperevent->GetNumberOfSubEvents());
153 
154  printf("Vtxvol %d\n", wcsimrootevent->GetVtxvol());
155  printf("Vtx %f %f %f\n", wcsimrootevent->GetVtx(0),
156  wcsimrootevent->GetVtx(1),wcsimrootevent->GetVtx(2));
157  }
158 
159 
160  //Calculate distance vertex to center, in z plane
161  // dWall
162  // muon range
163  // end point to center
164  double vtx_x = wcsimrootevent->GetVtx(0);
165  double vtx_y = wcsimrootevent->GetVtx(1);
166  double vtx_z = wcsimrootevent->GetVtx(2);
167  double dist = sqrt(vtx_x*vtx_x + vtx_y * vtx_y);
168 
169  //GetTracks, pick muon, use GetStart and GetStop
170  for (int i = 0; i < wcsimrootevent->GetNtrack() ; i++){
171  WCSimRootTrack *track = dynamic_cast<WCSimRootTrack*>((wcsimrootevent->GetTracks())->At(i));
172  if( track->GetIpnu() == 13 ) { // mu-
173  //GetStart(0)
174  //GetStop(0)
175  break;
176  }
177  }
178 
179  //std::cout << "test: " << wcsimrootsuperevent->GetNumberOfEvents() << std::endl;
180  //if(wcsimrootevent->GetNumTubesHit() > wcsimrootevent->GetNcherenkovdigihits() + 2000)
181  if(wcsimrootsuperevent->GetNumberOfEvents() > 1)
182  //std::cout << "test2: " << ev << " " << wcsimrootsuperevent->GetNumberOfEvents() << " " << wcsimrootevent->GetNumTubesHit() << " " << wcsimrootevent->GetNcherenkovdigihits() << std::endl;
183 
184  std::cout << wcsimrootsuperevent->GetNumberOfEvents() << std::endl;
185 
186  for (int index = 0 ; index < wcsimrootsuperevent->GetNumberOfEvents(); index++)
187  {
188  int ncherenkovdigihits = wcsimrootevent->GetNcherenkovdigihits();
189  h1->Fill(ncherenkovdigihits);
190 
191  // std::cout << "DEBUG " << ev << " " << index << " " << wcsimrootevent->GetNumTubesHit() << std::endl;
192  hit_pmts->Fill(wcsimrootevent->GetNumTubesHit());
193  float totalq = 0.;
194  float totalt = 0.;
195 
196  std::cout << ncherenkovdigihits << std::endl;
197  std::cout << wcsimrootevent->GetNumTubesHit() << std::endl;
198  //TH1F *occup_per_event = new TH1F("occup_per_event","",20000,0,20000);
199  // Loop through elements in the TClonesArray of WCSimRootCherenkovHits
200  for (int i=0; i< ncherenkovdigihits; i++){
201  TObject *Digi = (wcsimrootevent->GetCherenkovDigiHits())->At(i);
202  WCSimRootCherenkovDigiHit *wcsimrootcherenkovdigihit =
203  dynamic_cast<WCSimRootCherenkovDigiHit*>(Digi);
204 
205  float q = wcsimrootcherenkovdigihit->GetQ();
206  float t = wcsimrootcherenkovdigihit->GetT();
207  charge->Fill(q);
208  ttime->Fill(t);
209  t_q->Fill(t,q);
210  occupancy->Fill(tubeNumber);
211  //occup_per_event->Fill(tubeNumber);
212  occupancy_mPMT->Fill(tubeNumber%33 == 0 ? 33 : tubeNumber%33);
213  occupancy_mPMT2->Fill(tubeNumber/33);
214  totalq+=q;
215  totalt+=t;
216 
217  tubeID_q->Fill(tubeNumber,q);
218  mPMTid_q->Fill(tubeNumber%33 == 0 ? 33 : tubeNumber%33,q);
219  }
220 
221  tot_charge->Fill(totalq);
222  float av_time = totalt/ncherenkovdigihits;
223  float av_q = totalq/ncherenkovdigihits;
224 
225  /*
226  for(int j = 1; j < 15000; j++ ){//occupancy->GetNbinsX() ; j++){
227  if(occup_per_event->GetBinContent(j) > 0 )
228  nhit_pmt->Fill(occup_per_event->GetBinContent(j));
229  }*/
230 
231  }
232  pe->Fill(av_q);
233  time->Fill(av_time);
234 
235  // reinitialize super event between loops.
236  wcsimrootsuperevent->ReInitialize();
237  }// End of loop over events
238 
239 
240  // Create a WCSimRootEvent to put stuff from the tree in
241 
242  WCSimRootEvent* wcsimrootsuperevent = new WCSimRootEvent();
243 
244  // Set the branch address for reading from the tree
245  TBranch *branch = wcsimT2->GetBranch("wcsimrootevent");
246  branch->SetAddress(&wcsimrootsuperevent);
247 
248  // Force deletion to prevent memory leak
249  wcsimT2->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
250 
251 
252  // start with the main "subevent", as it contains most of the info
253  // and always exists.
254  WCSimRootTrigger* wcsimrootevent;
255 
256  TH1F *h2 = new TH1F("PMT Hits 2", "# Digitized Hits", 500, 0, 25000);
257  TH1F *time2 = new TH1F("Average time 2", "Average time", 600, 500, 5000);
258  TH1F *pe2 = new TH1F("Q/# Digitzed PMT 2", "Q/# Digitzed PMT", 200, 0, 5);
259  TH1F *hit_pmts2 = new TH1F("Hit PMTs 2","# Hit PMTs",500,0,35000);
260 
261  TH1F *tot_charge2 = new TH1F("tot2","Total Charge in event",500,0,20000);
262 
263  TH1F *charge2 = new TH1F("charge2","",200,0,1000);
264  TH1F *ttime2 = new TH1F("ttime2","",200,900,2000);
265  TH1F *occupancy2 = new TH1F("occupancy2","",200,0,500000);
266  TH1F *occupancy2_mPMT = new TH1F("occupancy2_mPMT","",34,0,34);
267  TH1F *occupancy2_mPMT2 = new TH1F("occupancy2_mPMT2","",200,0,20000);
268 
269  // Now loop over events
270  for (int ev=0; ev<nevent2; ev++){
271  // Read the event from the tree into the WCSimRootEvent instance
272  wcsimT2->GetEvent(ev);
273  wcsimrootevent2 = wcsimrootsuperevent2->GetTrigger(0);
274 
275  if(verbose){
276  printf("********************************************************");
277  printf("Evt, date %d %d\n", wcsimrootevent2->GetHeader()->GetEvtNum(),
278  wcsimrootevent2->GetHeader()->GetDate());
279  printf("Mode %d\n", wcsimrootevent2->GetMode());
280  printf("Number of subevents %d\n",
281  wcsimrootsuperevent2->GetNumberOfSubEvents());
282 
283  printf("Vtxvol %d\n", wcsimrootevent2->GetVtxvol());
284  printf("Vtx %f %f %f\n", wcsimrootevent2->GetVtx(0),
285  wcsimrootevent2->GetVtx(1),wcsimrootevent2->GetVtx(2));
286  }
287 
288  for (int index = 0 ; index < wcsimrootsuperevent->GetNumberOfEvents(); index++)
289  {
290  int ncherenkovdigihits = wcsimrootevent->GetNcherenkovdigihits();
291  h2->Fill(ncherenkovdigihits);
292  hit_pmts2->Fill(wcsimrootevent->GetNumTubesHit());
293 
294  float totalq = 0.;
295  float totalt = 0.;
296  // Loop through elements in the TClonesArray of WCSimRootCherenkovHits
297  for (int i=0; i< ncherenkovdigihits; i++){
298  TObject *Digi = (wcsimrootevent2->GetCherenkovDigiHits())->At(i);
299  WCSimRootCherenkovDigiHit *wcsimrootcherenkovdigihit =
300  dynamic_cast<WCSimRootCherenkovDigiHit*>(Digi);
301 
302  float q = wcsimrootcherenkovdigihit->GetQ();
303  float t = wcsimrootcherenkovdigihit->GetT();
304  charge2->Fill(q);
305  ttime2->Fill(t);
306  totalq+=q;
307  totalt+=t;
308  }
309 
310  tot_charge2->Fill(totalq);
311  if(ncherenkovdigihits > 0){
312  float av_time = totalt/ncherenkovdigihits;
313  float av_q = totalq/ncherenkovdigihits;
314  }
315  }
316  pe2->Fill(av_q);
317  time2->Fill(av_time);
318  // reinitialize super event between loops.
319  wcsimrootsuperevent2->ReInitialize();
320  }// End of loop over events
321 
322  Double_t ks_hits = hits->KolmogorovTest(hits2);
323  Double_t ks_charge = charge->KolmogorovTest(charge2);
324  Double_t ks_time = time->KolmogorovTest(time2);
325  cout << "***********************************************************" << endl;
326  cout << "ks test for # of digitized hits: " << ks_hits << endl;
327  cout << "ks test for average charge: " << ks_charge << endl;
328  cout << "ks test for average time: " << ks_time << endl;
329 
330  // TCanvas c1("c1");
331  float win_scale = 0.75;
332  int n_wide(2);
333  int n_high(2);
334  TCanvas* c1 = new TCanvas("c1", "Test Plots", 500*n_wide*win_scale, 500*n_high*win_scale);
335  c1->Draw();
336  c1->Divide(2,2);
337  c1->cd(1);
338  hits2->SetLineColor(kRed);
339  hits->Draw();
340  c1->cd(1); hits2->Draw("SAME");
341 
342  TLegend *leg = new TLegend(0.2,0.7,0.55,0.85, "");
343  leg->SetFillColor(0);
344  leg->SetBorderSize(0);
345  // leg->AddEntry(h1,filename, "l");
346  //leg->AddEntry(h2,filename2, "l");
347  leg->AddEntry(h1,"mPMT", "l");
348  leg->AddEntry(h2,"20in", "l");
349  leg->Draw();
350 
351  c1->cd(2);
352  charge->GetXaxis()->SetTitle("Total Charge / # digitized hits");
353  charge->Draw();
354  charge2->SetLineColor(kRed);
355  c1->cd(2); charge2->Draw("SAME");
356  c1->cd(3);
357  time->GetXaxis()->SetTitle("Total Time / # digitized hits (ns)");
358  time->Draw();
359  time2->SetLineColor(kRed);
360  c1->cd(3); time2->Draw("SAME");
361 
362 
363  c1->cd(4);
364  hit_pmts->Draw();
365  hit_pmts2->SetLineColor(kRed);
366  hit_pmts2->Draw("same");
367 
368  TCanvas* c2 = new TCanvas("c2", "Test Plots", 500*n_wide*win_scale, 500*n_high*win_scale);
369  gStyle->SetOptStat(1);
370  gStyle->SetOptFit(111);
371  /* TF1 *func = new TF1("func","gaus(0)",0,20000);
372  func->SetParameters(10,5000,10);
373  TF1 *func2 = new TF1("func2","gaus(0)",0,20000);
374  func2->SetParameters(10,5000,10); */
375  tot_charge->Draw();
376  //tot_charge->Fit("gaus","","",tot_charge->GetMean()-1.5*tot_charge->GetRMS(),tot_charge->GetMean()+1.5*tot_charge->GetRMS());
377  tot_charge2->SetLineColor(kRed);
378  tot_charge2->Draw("same");
379  //tot_charge2->Fit("gaus","","",tot_charge->GetMean()-1.5*tot_charge->GetRMS(),tot_charge->GetMean()+1.5*tot_charge->GetRMS());
380  //std::cout << "Tot charge ratio : " << func->GetParameter(0)/func2->GetParameter(0) << std::endl;
381 
382  TCanvas *c3 = new TCanvas();
383  c3->Divide(2,2);
384  c3->cd(1);
385  occupancy_mPMT2->Draw();
386  c3->cd(2);
387  occupancy_mPMT->Draw();
388  c3->cd(3);
389  ttime->Draw();
390  ttime2->SetLineColor(kRed);
391  ttime2->Draw("same");
392  c3->cd(4);
393  charge->Draw();
394  charge2->SetLineColor(kRed);
395  charge2->Draw("same");
396 
397 
398  TCanvas *c4 = new TCanvas();
399 
400  nhit_pmt->Draw();
401 
402  TCanvas *c5 = new TCanvas();
403  t_q->Draw("colz");
404 }
TCanvas * c3
Int_t GetNumberOfSubEvents() const
Class holding digitised hit (aka digit or digi) information (after PMT & electronics simulation + tri...
Int_t GetIpnu() const
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
TClonesArray * GetTracks() const
Float_t GetVtx(Int_t i=0)
Int_t GetNcherenkovdigihits() const
void verification_HitsChargeTime(char *filename="wcsimtest.root", 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 GetNtrack() const
Double_t ks_hits
TClonesArray * GetCherenkovDigiHits() const
Double_t ks_charge
Class holding true track information.
Double_t ks_time