WCSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WCSimRunAction.cc
Go to the documentation of this file.
1 #include "WCSimRunAction.hh"
3 
4 #include "G4Run.hh"
5 #include "G4UImanager.hh"
6 #include "G4VVisManager.hh"
7 #include "G4ios.hh"
8 
9 #include "jhfNtuple.h"
10 
11 #ifdef REFLEX_DICTIONARY
12 #include "Cintex/Cintex.h"
13 #endif
14 #include "TROOT.h"
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "TBranch.h"
18 #include "TStreamerInfo.h"
19 #include "WCSimRootEvent.hh"
20 #include "WCSimRootGeom.hh"
21 #include "WCSimPmtInfo.hh"
22 
23 #include <vector>
24 
25 int pawc_[500000]; // Declare the PAWC common
27 
29  : wcsimrandomparameters(rand), useTimer(false)
30 {
31  ntuples = 1;
32 
33  // Messenger to allow IO options
34  wcsimdetector = test;
36 
38 }
39 
41 {
42 
43 }
44 
45 void WCSimRunAction::BeginOfRunAction(const G4Run* /*aRun*/)
46 {
47  if(useTimer) {
48  timer.Reset();
49  timer.Start();
50  }
51 
52 // G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
56 
57 #ifdef REFLEX_DICTIONARY
58  ROOT::Cintex::Cintex::Enable();
59 #endif
60 
61  // Needed for Root 4.00/04
62  WCSimRootEvent::Class()->GetStreamerInfo()->Optimize(kFALSE);
63  // MF, aug 2006 ... you never know...
64  WCSimRootTrigger::Class()->GetStreamerInfo()->Optimize(kFALSE);
65 
66  // Create the Root file
67 
68  // Now controlled by the messenger
69 
70  G4String rootname = GetRootFileName();
71  TFile* hfile = new TFile(rootname.c_str(),"RECREATE","WCSim ROOT file");
72  hfile->SetCompressionLevel(2);
73 
74  // Event tree
75  WCSimTree = new TTree("wcsimT","WCSim Tree");
76 
77  wcsimrootsuperevent = new WCSimRootEvent(); //empty list
79  // wcsimrootsuperevent->AddSubEvent(); // make at least one event
80  wcsimrootsuperevent->Initialize(); // make at least one event
81  wcsimrootsuperevent_OD->Initialize(); // make at least one event
82  Int_t branchStyle = 1; //new style by default
83  TTree::SetBranchStyle(branchStyle);
84  Int_t bufsize = 64000;
85 
86  // TBranch *branch = tree->Branch("wcsimrootsuperevent", "Jhf2kmrootsuperevent", &wcsimrootsuperevent, bufsize,0);
87  // TBranch *branch = WCSimTree->Branch("wcsimrootevent", "WCSimRootEvent", &wcsimrootsuperevent, bufsize,2);
88  wcsimrooteventbranch = WCSimTree->Branch("wcsimrootevent", "WCSimRootEvent", &wcsimrootsuperevent, bufsize,2);
89  wcsimrooteventbranch_OD = WCSimTree->Branch("wcsimrootevent_OD", "WCSimRootEvent", &wcsimrootsuperevent_OD, bufsize,2);
90 
91  // Geometry tree
92 
93  geoTree = new TTree("wcsimGeoT","WCSim Geometry Tree");
95  TBranch *geoBranch = geoTree->Branch("wcsimrootgeom", "WCSimRootGeom", &wcsimrootgeom, bufsize,0);
96 
97  FillGeoTree();
98 
99  // Options tree
100  optionsTree = new TTree("wcsimRootOptionsT","WCSim Options Tree");
101  optionsTree->Branch("wcsimrootoptions", "WCSimRootOptions", &wcsimrootoptions, bufsize, 0);
102 
103  //set detector & random options
106 }
107 
109 {
110 //G4cout << "Number of Events Generated: "<< numberOfEventsGenerated << G4endl;
111 //G4cout << "Number of times OD hit: " << numberOfTimesMRDHit << G4endl;
112 //G4cout << "Number of times FGD hit: " << numberOfTimesFGDHit << G4endl;
113 //G4cout << "Number of times lArD hit: " << numberOfTimeslArDHit << G4endl;
114 //G4cout<<"Number of times waterTube hit: " << numberOfTimesWaterTubeHit<<G4endl;
115 // G4cout << ((double(numberOfTimesMRDHit)+double(numberOfTimesFGDHit))/double(numberOfEventsGenerated))*100.
116 // << "% hit FGD or MRD" << G4endl;
117 // G4cout << "Number of times Catcher hit: " << numberOfTimesCatcherHit<<G4endl;
118 // G4cout << "Number of times Rock hit: " << numberOfTimesRockHit<<G4endl;
119 // G4cout << (double(numberOfTimesCatcherHit)/double(numberOfEventsGenerated))*100.
120 // << "% through-going (hit Catcher)" << G4endl;
121 
122  //Write the options tree
123  G4cout << "EndOfRunAction" << G4endl;
124  optionsTree->Fill();
125  optionsTree->Write();
126 
127  // Close the Root file at the end of the run
128 
129  TFile* hfile = WCSimTree->GetCurrentFile();
130  // Moved from EventAction by G. Pronost on 2019/12/17
131  hfile->Write("",TObject::kOverwrite); // Need to overwrite to avoid multiple instance of wcsimT
132  hfile->Close();
133 
134  // Clean up stuff on the heap; I think deletion of hfile and trees
135  // is taken care of by the file close
136 
139  delete wcsimrootgeom; wcsimrootgeom=0;
140 
141  if(useTimer) {
142  timer.Stop();
143  G4cout << "WCSimRunAction ran from BeginOfRunAction() to EndOfRunAction() in:"
144  << "\t" << timer.CpuTime() << " seconds (CPU)"
145  << "\t" << timer.RealTime() << " seconds (real)" << G4endl;
146  }
147 }
148 
150  // Fill the geometry tree
151  G4int geo_type;
152  G4double cylinfo[3];
153  G4double pmtradius;
154  G4double pmtradiusOD;
155  G4int numpmt;
156  G4int numpmtOD;
157  G4int orientation;
158  Double_t offset[3];
159 
160  Int_t tubeNo;
161  Double_t pos[3];
162  Double_t rot[3];
163  Int_t cylLoc;
164 
166  geo_type = 2;
167  }
168  else {
169  geo_type = 0;
170  }
171  // geo_type 1 is for defunct mailbox design
172 
173  wcsimrootgeom-> SetGeo_Type(geo_type);
174 
175  if (geo_type == 0) {
176  //cylinder
177  cylinfo[1] = wcsimdetector->GetGeo_Dm(3);
178  cylinfo[2] = wcsimdetector->GetGeo_Dm(2);
179  wcsimrootgeom-> SetWCCylRadius(cylinfo[1]);
180  wcsimrootgeom-> SetWCCylLength(cylinfo[2]);
181  }
182 
183 
184  pmtradius = wcsimdetector->GetPMTSize1();
185  numpmt = wcsimdetector->GetTotalNumPmts();
186  pmtradiusOD = wcsimdetector->GetODPMTSize();
187  numpmtOD = wcsimdetector->GetTotalNumODPmts();
188  orientation = 0;
189 
190  wcsimrootgeom-> SetWCPMTRadius(pmtradius);
191  wcsimrootgeom-> SetODWCPMTRadius(pmtradiusOD);
192  wcsimrootgeom-> SetOrientation(orientation);
193 
194  G4ThreeVector offset1= wcsimdetector->GetWCOffset();
195  offset[0] = offset1[0];
196  offset[1] = offset1[1];
197  offset[2] = offset1[2];
198  wcsimrootgeom-> SetWCOffset(offset[0],offset[1],offset[2]);
199 
200  std::vector<WCSimPmtInfo*> *fpmts = wcsimdetector->Get_Pmts();
201  WCSimPmtInfo *pmt;
202  for (unsigned int i=0;i!=fpmts->size();i++){
203  pmt = ((WCSimPmtInfo*)fpmts->at(i));
204  pos[0] = pmt->Get_transx();
205  pos[1] = pmt->Get_transy();
206  pos[2] = pmt->Get_transz();
207  rot[0] = pmt->Get_orienx();
208  rot[1] = pmt->Get_orieny();
209  rot[2] = pmt->Get_orienz();
210  tubeNo = pmt->Get_tubeid();
211  cylLoc = pmt->Get_cylocation();
212  wcsimrootgeom-> SetPMT(i,tubeNo,cylLoc,rot,pos);
213  }
214  if (fpmts->size() != (unsigned int)numpmt) {
215  G4cout << "Mismatch between number of ID pmts and pmt list in geofile.txt!!"<<G4endl;
216  G4cout << fpmts->size() <<" vs. "<< numpmt <<G4endl;
217  }
218 
219  std::vector<WCSimPmtInfo*> *fODpmts = wcsimdetector->Get_ODPmts();
220  for (unsigned int i=0;i!=fODpmts->size();i++){
221  pmt = ((WCSimPmtInfo*)fODpmts->at(i));
222  pos[0] = pmt->Get_transx();
223  pos[1] = pmt->Get_transy();
224  pos[2] = pmt->Get_transz();
225  rot[0] = pmt->Get_orienx();
226  rot[1] = pmt->Get_orieny();
227  rot[2] = pmt->Get_orienz();
228  tubeNo = pmt->Get_tubeid();
229  cylLoc = pmt->Get_cylocation();
230  wcsimrootgeom-> SetPMT(i,tubeNo,cylLoc,rot,pos);
231  }
232  if (fpmts->size() != (unsigned int)numpmt) {
233  G4cout << "Mismatch between number of OD pmts and pmt list in geofile.txt!!"<<G4endl;
234  G4cout << fODpmts->size() <<" vs. "<< numpmt <<G4endl;
235  }
236 
237  wcsimrootgeom-> SetWCNumPMT(numpmt);
238  wcsimrootgeom-> SetODWCNumPMT(numpmtOD);
239 
240  geoTree->Fill();
241  geoTree->Write();
242 }
bool useTimer
Use the timer? Set by Messenger.
Double_t Get_orienx()
Definition: WCSimPmtInfo.hh:36
TBranch * wcsimrooteventbranch
WCSimRootOptions * wcsimrootoptions
Double_t Get_transy()
Definition: WCSimPmtInfo.hh:34
Double_t Get_orieny()
Definition: WCSimPmtInfo.hh:37
WCSimRootEvent * wcsimrootsuperevent
WCSimRunAction(WCSimDetectorConstruction *, WCSimRandomParameters *)
void SaveOptionsToOutput(WCSimRootOptions *wcopt)
WCSimDetectorConstruction * wcsimdetector
WCSimRunActionMessenger * messenger
TBranch * wcsimrooteventbranch_OD
WCSimRootGeom * wcsimrootgeom
Double_t Get_transz()
Definition: WCSimPmtInfo.hh:35
int pawc_[500000]
Double_t Get_transx()
Definition: WCSimPmtInfo.hh:33
int numberOfTimesWaterTubeHit
TStopwatch timer
A timer for runtime analysis.
WCSimRandomParameters * wcsimrandomparameters
Int_t Get_tubeid()
Definition: WCSimPmtInfo.hh:39
void SaveOptionsToOutput(WCSimRootOptions *wcopt)
WCSimRootEvent * wcsimrootsuperevent_OD
std::vector< WCSimPmtInfo * > * Get_Pmts()
G4String GetRootFileName()
std::vector< WCSimPmtInfo * > * Get_ODPmts()
struct ntupleStruct jhfNtuple
void EndOfRunAction(const G4Run *)
Double_t Get_orienz()
Definition: WCSimPmtInfo.hh:38
Int_t Get_cylocation()
Definition: WCSimPmtInfo.hh:32
void BeginOfRunAction(const G4Run *)