SToGS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
SToGS_BaseROOT.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //----------------------------------------------------------------------------------
28 
29 #include "SToGS_BaseROOT.hh"
30 
31 #include "G4Run.hh"
32 #include "G4Event.hh"
33 #include "G4SDManager.hh"
34 #include "G4RunManager.hh"
35 #include "G4UnitsTable.hh"
36 #if G4MULTITHREADED
37 #include "G4Threading.hh"
38 #endif
39 #ifdef G4MULTITHREADED
40 #include "G4AutoLock.hh"
41 namespace { G4Mutex buildMutex = G4MUTEX_INITIALIZER; }
42 #endif
43 
44 #include "TFile.h"
45 #include "TTree.h"
46 
47 #include <fstream>
48 #include <iomanip>
49 
51  G4Run(),
52  fRecordOption(record_option),
53  colltrackerID(-1),
54  collcaloID(-1)
55 {
56  G4SDManager* SDman = G4SDManager::GetSDMpointer();
57  if ( SDman ) {
58  // keep the collection ID associated to this collection
59  colltrackerID = SDman->GetCollectionID("TrackerHits");
60 
61  // keep the collection ID associated to this collection
62  collcaloID = SDman->GetCollectionID("CopClusterHits");
63  }
64 }
65 
66 /*
67 void SToGS::BaseROOTTreeRun::RecordEvent(const G4Event* evt)
68 {
69 }
70  */
71 
73 {
74 }
75 
77  AllActions(conf),
78  fRootFile(0x0),
79  fPathToData("./"),
80  fBaseName("SToGS_Ascii"),
81  fMaxEvents(1900000000),
82  fRecordOption(0),
83  fPrintModulo(1000)
84 {
85  // open the ascii file
86  std::ifstream file(conf.data());
87  if ( file.is_open() == false ) {
88  G4cout << " ** SToGS::Ascii WARNING ** Cannot open file " << conf << ", default parameters to be used "<< G4endl;
89  }
90  else {
91  std::string aline; getline(file,aline);
92  while ( file.good() && !file.eof() ) {
93 
94  if ( aline[0] == '#' ){
95  getline(file,aline);
96  continue;
97  } // this line is a comment
98 
99  std::string key, what, option; std::istringstream decode(aline);
100  decode >> key;
101 
102  if ( key == "path:" ) {
103  decode >> fPathToData;
104  }
105  if ( key == "basename:" ) {
106  decode >> fBaseName;
107  }
108  if ( key == "modulo:" ) {
109  decode >> fPrintModulo;
110  }
111  if ( key == "max_event:" ) {
112  decode >> fMaxEvents;
113  }
114  if ( key == "record_option:" ) {
115  decode >> fRecordOption;
116  }
117  getline(file,aline);
118  }
119  file.close();
120  }
121 }
122 /*
123 G4Run* SToGS::BaseROOTAction::GenerateRun()
124 {
125  G4Run* therun = 0x0; G4cout << " In BaseROOTAction, Generate a new Run " << G4endl;
126 
127  // creates a new run. As the file is open bu AsciiRun no need to open something for master ... maybe one day to keep some globals ?
128 #if G4MULTITHREADED
129  if ( G4Threading::IsWorkerThread() ) {
130  // if ( IsMaster() ) {
131  SToGS::AsciiRun *loc_therun = new SToGS::AsciiRun(fOutputFile);
132  therun = loc_therun;
133  }
134  else therun = G4UserRunAction::GenerateRun();
135 #else
136  therun = new SToGS::AsciiRun(fOutputFile);
137 #endif
138  return therun;
139 }
140  */
141 
143 {
144 #ifdef G4MULTITHREADED
145  G4AutoLock lock(&buildMutex); // better to be protected since root is doing some stuff in global
146 #endif
147 
148  // for MT, thread id used in the name of the file to avoid pb
149  G4int thread_id = 0;
150 #if G4MULTITHREADED
151  thread_id = G4Threading::G4GetThreadId();
152 #endif
153  std::ostringstream filename;
154  filename.clear();
155  filename << fPathToData << "/" << fBaseName << "_Thread" << std::setfill('0') << std::setw(2) << thread_id
156  << "_Run" << std::setw(3) << run_id << ".root";
157 
158  fRootFile = new TFile(filename.str().data(),"UPDATE");
159 }
161 {
162 #ifdef G4MULTITHREADED
163  G4AutoLock lock(&buildMutex); // better to be protected since root is doing some stuff in global
164 #endif
165  if ( fRootFile )
166  {
167  fRootFile->Close();
168  delete fRootFile; fRootFile = 0x0;
169  }
170 }
171 
173 {
174  G4cout << " In BaseROOTAction, Begin of Run " << aRun->GetRunID()
175  << ", Number of events to be simulated " << aRun->GetNumberOfEventToBeProcessed() << G4endl;
176  //
177  OpenFile(aRun->GetRunID());
178 }
180 {
181  G4cout << " In BaseROOTAction, End of Run " << aRun->GetRunID()
182  << ", Number of simulated events " << aRun->GetNumberOfEvent() << G4endl;
183  //
184  CloseFile();
185 }
187 {
188  G4int evtNb = evt->GetEventID();
189  if ( evtNb % fPrintModulo == 0 ) {
190  G4cout << " In BaseROOTAction, Begin of event: " << evtNb << G4endl;
191  }
192 }
194 {
195  G4int evtNb = evt->GetEventID();
196  if ( evtNb % fPrintModulo == 0 ) {
197  G4cout << " In BaseROOTAction, End of event: " << evtNb << G4endl;
198  }
199 }
200 
202  SToGS::BaseROOTAction(conffile),
203  fTree(0x0),
204  fTreeName("SToGSTree"),
205  fTreeTitle("Tree produced by SToGS")
206 {
207  // open the ascii file
208  std::ifstream file(conffile.data());
209  if ( file.is_open() == false ) {
210  G4cout << " ** SToGS::BaseROOTTreeAction WARNING ** Cannot open file " << conffile << ", default parameters to be used "<< G4endl;
211  }
212  else {
213  std::string aline; getline(file,aline);
214  while ( file.good() && !file.eof() ) {
215 
216  if ( aline[0] == '#' ){
217  getline(file,aline);
218  continue;
219  } // this line is a comment
220 
221  std::string key, what, option; std::istringstream decode(aline); decode.clear();
222  decode >> key;
223 
224  if ( key == "tree:" ) { // get name and title. Title could have spaces ...
225  decode >> fTreeName ;
226 
227  size_t pos_title = aline.find(fTreeName);
228  if ( pos_title != std::string::npos ) {
229  pos_title +=
230  fTreeName.length();
231  fTreeTitle = aline.substr(pos_title);
232  }
233  }
234  getline(file,aline);
235  }
236  file.close();
237  }
238 }
239 
241 {
242  ;
243 }
244 
246 {
247 #ifdef G4MULTITHREADED
248  G4AutoLock lock(&buildMutex);
249  // better to be protected since root is doing some stuff in global
250  // could not call BaseROOTAction::CloseFile() otherwise double lock !!!
251 #endif
252  // for MT, thread id used in the name of the file to avoid pb
253  G4int thread_id = 0;
254 #if G4MULTITHREADED
255  thread_id = G4Threading::G4GetThreadId();
256 #endif
257 
258  std::ostringstream filename;
259  filename.clear();
260  filename << fPathToData << "/" << fBaseName << "_Thread" << std::setfill('0') << std::setw(2) << thread_id
261  << "_Run" << std::setw(3) << run_id << ".root";
262 
263  fRootFile = new TFile(filename.str().data(),"UPDATE");
264  if ( fRootFile->IsOpen() ) {
265  G4cout << " The File " << filename.str() << " is open to record data " << G4endl;
266  // fTree->SetDirectory(fRootFile);
267  fTree->DirectoryAutoAdd(fRootFile);
268  }
269 }
271 {
272 #ifdef G4MULTITHREADED
273  G4AutoLock lock(&buildMutex);
274  // better to be protected since root is doing some stuff in global
275  // could not call BaseROOTAction::CloseFile() otherwise double lock !!!
276 #endif
277  TFile *current_root = fTree->GetCurrentFile();
278  if ( current_root ) {
279  G4cout << " Closing File " << current_root->GetName() << " " << G4endl;
280  current_root->Write();
281  fTree->SetDirectory(0x0);
282  delete current_root; fRootFile = 0x0; // this is not an error !
283  }
284  fTree->Print();
285  delete fTree; fTree = 0x0;
286 }
287 
288 
289 
290 
291 
G4String fBaseName
base for all the files
BaseROOTAction(G4String conffile="setup/SToGS_root_actions.conf")
BaseROOTTreeAction(G4String conffile="setup/SToGS_root_actions.conf")
virtual void CloseFile()
Make sure ths file is closed properly.
virtual void EndOfRunAction(const G4Run *)
virtual void OpenFile(G4int run_id)
Open the stream depending of the configuration and attach the Tree to the file.
G4int fRecordOption
0 [default] means keep all, 1 only events which gives at least one hit in the full detector ...
virtual void BeginOfRunAction(const G4Run *)
G4int fPrintModulo
to print out status any fPrintModulo events
virtual void EndOfEventAction(const G4Event *)
G4String fPathToData
directory where to output data
This class just print out once a new run begins/ends with the run number and the number of events to ...
G4int fMaxEvents
max numer of event per files ... better to limit because of BaseROOT file could be uged ! ...
virtual void BeginOfEventAction(const G4Event *)
virtual void CloseFile()
Make sure ths file is closed properly.
virtual void OpenFile(G4int run_id)
Just check if there are collected hits in the collection.
BaseROOTTreeRun(G4int record_option=0)
Base class that regroups in the same space all user's hooks. Convenient for sharing similar data...