WCSim
complete_comparison.C
Go to the documentation of this file.
1 /*
2  Macro that compares the events in two WCSim output files
3  Utilizes the WCSimRoot comparison methods: CompareAllVariables(),
4  to ensure that everything is equivalent
5  */
6 
7 #include <iostream>
8 #include <string>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include <TFile.h>
13 #include <TTree.h>
14 #include <TSystem.h>
15 #include <TMath.h>
16 
17 #include "WCSimRootGeom.hh"
18 #include "WCSimRootEvent.hh"
19 #include "WCSimRootOptions.hh"
20 
21 using namespace std;
22 
23 void PrintHeaderInfo(WCSimRootEvent * wcsimrootsuperevent, const char * filename)
24 {
25  cout << "********************************************************" << endl;
26  WCSimRootTrigger * wcsimrootevent;
27  for(int itrigger = 0; itrigger < wcsimrootsuperevent->GetNumberOfEvents(); itrigger++) {
28  wcsimrootevent = wcsimrootsuperevent->GetTrigger(itrigger);
29  if(itrigger == 0)
30  cout << "Header+ in WCSim file: " << filename << endl;
31  else
32  cout << "-----------" << endl;
33  cout << "Evt, subevent: " << wcsimrootevent->GetHeader()->GetEvtNum() << ", "
34  << itrigger << endl;
35  cout << "Date: " << wcsimrootevent->GetHeader()->GetDate() << endl;
36  cout << "Mode: " << wcsimrootevent->GetMode() << endl;
37  cout << "Number of subevents: " << wcsimrootsuperevent->GetNumberOfSubEvents() << endl;
38  if(itrigger == 0) {
39  cout << "Vtxvol: " << wcsimrootevent->GetVtxvol() << endl;
40  for(int i = 0; i < 3; i++)
41  cout << "Vtx[" << i << "]: " << wcsimrootevent->GetVtx(i) << endl;
42  cout << "Number of photoelectron hit times " << wcsimrootevent->GetCherenkovHitTimes()->GetEntries() << endl;
43  cout << "Number of tube hits " << wcsimrootevent->GetNumTubesHit() << endl;
44  }
45  cout << "Number of tracks " << wcsimrootevent->GetNtrack() << endl;
46  cout << "Number of digitized tube hits " << wcsimrootevent->GetNumDigiTubesHit() << endl;
47  }//itrigger
48 }
49 
51 {
52  cout << "********************************************************" << endl;
53  cout << "Geom summary+ in WCSim file: " << filename << endl;
54  cout << "WC radius " << geo->GetWCCylRadius() << endl;
55  cout << "WC length " << geo->GetWCCylLength() << endl;
56  cout << "Number of PMTs " << geo->GetWCNumPMT() << endl;
57  cout << "PMT radius " << geo->GetWCPMTRadius() << endl;
58 }
59 
60 TTree * GetTree(const char * filename, const char * treename)
61 {
62  TFile *f = new TFile(filename,"read");
63  if (!f->IsOpen()){
64  cout << "Error, could not open input file: " << filename << endl;
65  exit(-1);
66  }
67 
68  TTree * wcsimT = 0;
69  f->GetObject(treename, wcsimT);
70  if(!wcsimT) {
71  cerr << treename << " tree could not be opened in input file: " << filename << endl;
72  exit(-1);
73  }
74  return wcsimT;
75 }
76 
77 WCSimRootEvent * GetRootEvent(TTree * wcsimT, int & nevent)
78 {
79  nevent = wcsimT->GetEntries();
80  WCSimRootEvent * wcsimrootsuperevent = 0;
81  wcsimT->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent);
82 
83  // Force deletion to prevent memory leak when issuing multiple
84  // calls to GetEvent()
85  wcsimT->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);
86 
87  wcsimT->GetEvent(0);
88  return wcsimrootsuperevent;
89 }
90 
91 WCSimRootGeom * GetRootGeom(TTree * wcsimT, int & nevent)
92 {
93  nevent = wcsimT->GetEntries();
94  WCSimRootGeom * wcsimrootgeom = 0;
95  wcsimT->SetBranchAddress("wcsimrootgeom", &wcsimrootgeom);
96  wcsimT->GetEvent(0);
97  return wcsimrootgeom;
98 }
99 
100 // Simple example of reading a generated Root file
101 void complete_comparison(const char *filename1, const char *filename2, string treename = "wcsimT", bool verbose=false, const Long64_t oneevent = -1)
102 {
103  int nevents1;
104  TTree * wcsimT1 = GetTree(filename1, treename.c_str());
105 
106  int nevents2;
107  TTree * wcsimT2 = GetTree(filename2, treename.c_str());
108 
109  WCSimRootEvent * wcsimrootsuperevent1 = 0;
110  WCSimRootGeom * wcsimrootgeom1 = 0;
111  WCSimRootEvent * wcsimrootsuperevent2 = 0;
112  WCSimRootGeom * wcsimrootgeom2 = 0;
113  if(treename == "wcsimT") {
114  wcsimrootsuperevent1 = GetRootEvent(wcsimT1, nevents1);
115  wcsimrootsuperevent2 = GetRootEvent(wcsimT2, nevents2);
116  }
117  else if(treename == "wcsimGeoT") {
118  wcsimrootgeom1 = GetRootGeom(wcsimT1, nevents1);
119  wcsimrootgeom2 = GetRootGeom(wcsimT2, nevents2);
120  }
121  else {
122  cerr << treename << " is not a valid treename for complete_comparison(). Exiting" << endl;
123  return;
124  }
125 
126  cout << "Tree " << treename << " in file 1 (2) has " << nevents1 << " (" << nevents2 << ") events" << endl;
127 
128  // Now loop over events
129  Long64_t nevents_failed = 0, nevents_analysed = 0;
130  Long64_t nevent = TMath::Min(nevents1, nevents2);
131  Long64_t ev = 0;
132  if(oneevent >= 0) {
133  ev = oneevent;
134  nevent = oneevent + 1;
135  }
136  for ( ; ev < nevent; ev++)
137  {
138  cout << "Comparing event " << ev << endl;
139  nevents_analysed++;
140  // Read the event from the tree into the WCSimRootEvent instance
141  wcsimT1->GetEntry(ev);
142  wcsimT2->GetEntry(ev);
143 
144  //print
145  if(verbose || ev == 0){
146  if(wcsimrootsuperevent1) {
147  PrintHeaderInfo(wcsimrootsuperevent1, filename1);
148  PrintHeaderInfo(wcsimrootsuperevent2, filename2);
149  }
150  else if(wcsimrootgeom1) {
151  PrintGeomSummary(wcsimrootgeom1, filename1);
152  PrintGeomSummary(wcsimrootgeom2, filename2);
153  }
154  }
155 
156  //compare
157  bool failed = false;
158  if(wcsimrootsuperevent1) {
159  failed = !(wcsimrootsuperevent1->CompareAllVariables(wcsimrootsuperevent2, true));
160  }
161  else if(wcsimrootgeom1) {
162  failed = !(wcsimrootgeom1->CompareAllVariables(wcsimrootgeom2));
163  }
164 
165  if(verbose || ev == 0)
166  cout << "Event " << ev << " comparison " << (failed ? "FAILED" : "PASSED") << endl;
167  if(failed)
168  nevents_failed++;
169 
170  if(wcsimrootsuperevent1) {
171  // reinitialize super event between loops.
172  wcsimrootsuperevent1->ReInitialize();
173  wcsimrootsuperevent2->ReInitialize();
174  }
175  }// End of loop over events
176  cout << nevents_failed << " events out of " << nevents_analysed << " have failures " << endl;
177 }
Detector geometry information (also containing PMT information arrays)
Float_t GetWCPMTRadius(bool hybridsecondtype=false) const
Float_t GetWCCylLength() const
Int_t GetNumberOfSubEvents() const
Float_t GetWCCylRadius() const
WCSimRootEvent * GetRootEvent(TTree *wcsimT, int &nevent)
Int_t GetNumDigiTubesHit() const
void complete_comparison(const char *filename1, const char *filename2, string treename="wcsimT", bool verbose=false, const Long64_t oneevent=-1)
int64_t GetDate() const
WCSimRootEventHeader * GetHeader()
Definition: json.hpp:5295
Int_t GetVtxvol() const
Float_t GetVtx(Int_t i=0)
Int_t GetWCNumPMT(bool hybridsecondtype=false) const
void PrintHeaderInfo(WCSimRootEvent *wcsimrootsuperevent, const char *filename)
Class storing trigger information.
WCSimRootTrigger * GetTrigger(int number)
TClonesArray * GetCherenkovHitTimes() const
Int_t GetEvtNum() const
Int_t GetMode() const
Int_t GetNumTubesHit() const
string filename
Definition: MakeKin.py:235
Int_t GetNumberOfEvents() const
Class containing event information.
WCSimRootGeom * geo
TTree * GetTree(const char *filename, const char *treename)
void PrintGeomSummary(WCSimRootGeom *geo, const char *filename)
Int_t GetNtrack() const
bool CompareAllVariables(const WCSimRootGeom *c) const
WCSimRootGeom * GetRootGeom(TTree *wcsimT, int &nevent)