SToGS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
SToGS_Ascii.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 #include "SToGS_Ascii.hh"
28 #include "SToGS_G4_TrackerSD.hh"
29 #include "SToGS_G4_CopClusterSD.hh"
30 
31 #include "G4SDManager.hh"
32 #include "G4RunManager.hh"
33 #include "G4Event.hh"
34 #include "G4UnitsTable.hh"
35 
36 #if G4MULTITHREADED
37 #include "G4Threading.hh"
38 #endif
39 
40 SToGS::AsciiRun::AsciiRun(std::ofstream &out) :
41  G4Run(),
42  colltrackerID(-1),
43  collcaloID(-1),
44  fOutputFile(out)
45 // fEventMarkOff('$','\0')
46 {
47  G4SDManager* SDman = G4SDManager::GetSDMpointer();
48  if ( SDman ) {
49  // keep the collection ID associated to this collection
50  colltrackerID = SDman->GetCollectionID("TrackerHits");
51 
52  // keep the collection ID associated to this collection
53  collcaloID = SDman->GetCollectionID("CopClusterHits");
54  }
55 }
56 
58 {
59 }
60 
61 void SToGS::AsciiRun::RecordEvent(const G4Event* evt)
62 {
63  SToGS::TrackerHitsCollection *THC = 0x0; SToGS::CopClusterHitsCollection *CHC = 0x0; G4int nb_hits = 0;
64 
65  // check there is something to write in files
66  if( colltrackerID < 0 && collcaloID < 0 )
67  return;
68 
69  G4HCofThisEvent *HCE = evt->GetHCofThisEvent();
70  if(HCE)
71  {
72  THC = (SToGS::TrackerHitsCollection *)(HCE->GetHC(colltrackerID));
73  if ( colltrackerID > -1 ) {
74  nb_hits += THC->entries();
75  }
76  CHC = (SToGS::CopClusterHitsCollection*)(HCE->GetHC(collcaloID));
77  if ( collcaloID > -1 ) {
78  nb_hits += CHC->entries();
79  }
80  }
81  /*
82  if ( fRecordOption == 1 ) {
83  if ( nb_hits == 0 )
84  return;
85  }
86  */
87  // write primary part
88  G4int istart = 0, iend = evt->GetNumberOfPrimaryVertex();
89 
90  for( G4int i = istart; i < iend ; i++ ){
91  G4int jstart = 0, jend = evt->GetPrimaryVertex(i)->GetNumberOfParticle(); // to get the next vertex
92  for( G4int j = jstart; j < jend; j++ ) {
93 
94  G4PrimaryParticle *prim = evt->GetPrimaryVertex(i)->GetPrimary(j); // get the next particle for vertex #i
95 
96  fOutputFile
97  << std::setw(2)
98  << "P " << " "
99  << std::setw(4)
100  << prim->GetTrackID() << " "
101  << std::setw(3)
102  << iend*jend << " "
103  << std::setw(7)
104  << (std::sqrt( prim->GetMomentum().mag2() + prim->GetMass() * prim->GetMass()) - prim->GetMass())/CLHEP::keV << " "
105  << prim->GetPx() << " " << prim->GetPy() << " " << prim->GetPz()
106  << std::endl;
107  }
108  }
109  if(THC && colltrackerID != -1)
110  {
111  int n_hit = THC->entries();
112  for (int i = 0 ; i < n_hit; i++) {
113  fOutputFile
114  << std::setw(2)
115  << "H " << " "
116  << std::setw(4)
117  << (*THC)[i]->GetPrimaryID() << " "
118  << 1 << " "
119  << std::setw(3)
120  << (*THC)[i]->GetDetID() << " "
121  << std::setw(7)
122  << (*THC)[i]->GetEdep()/CLHEP::keV << " "
123  << (*THC)[i]->GetPos().x()/CLHEP::cm << " " << (*THC)[i]->GetPos().y()/CLHEP::cm << " " << (*THC)[i]->GetPos().z()/CLHEP::cm << " "
124  << std::setw(2)
125  << (*THC)[i]->GetToF()/CLHEP::ns
126  << std::endl;
127  }
128  }
129  if(CHC && collcaloID != -1)
130  {
131  int n_hit = CHC->entries();
132  for (int i = 0 ; i < n_hit; i++) {
133  fOutputFile
134  << std::setw(2)
135  << "H " << " "
136  << std::setw(4)
137  << -1 << " "
138  << std::setw(3)
139  << (*CHC)[i]->GetDetID() << " "
140  << std::setw(7)
141  << (*CHC)[i]->GetEdep()/CLHEP::keV << " "
142  << (*CHC)[i]->GetPos().x()/CLHEP::cm << " " << (*CHC)[i]->GetPos().y()/CLHEP::cm << " " << (*CHC)[i]->GetPos().z()/CLHEP::cm << " "
143  << std::setw(2)
144  << (*CHC)[i]->GetToF()/CLHEP::ns
145  << std::endl;
146  }
147  }
148 }
149 
151  AllActions(conf),
152  fOutputFile(),
153  fPathToData("./"),
154  fBaseName("SToGS_Ascii"),
155  fMaxEvents(500000),
156  fRecordOption(0),
157  fPrintModulo(1000)
158 {
159  // open the ascii file
160  std::ifstream file(conf.data());
161  if ( file.is_open() == false ) {
162  G4cout << " ** SToGS::Ascii WARNING ** Cannot open file " << conf << ", default parameters to be used "<< G4endl;
163  }
164  else {
165  std::string aline; getline(file,aline);
166  while ( file.good() && !file.eof() ) {
167 
168  if ( aline[0] == '#' ){
169  getline(file,aline);
170  continue;
171  } // this line is a comment
172 
173  std::string key, what, option; std::istringstream decode(aline);
174  decode >> key;
175 
176  if ( key == "path:" ) {
177  decode >> fPathToData;
178  }
179  if ( key == "basename:" ) {
180  decode >> fBaseName;
181  }
182  if ( key == "modulo:" ) {
183  decode >> fPrintModulo;
184  }
185  if ( key == "max_event:" ) {
186  decode >> fMaxEvents;
187  }
188  if ( key == "record_option:" ) {
189  decode >> fRecordOption;
190  }
191  getline(file,aline);
192  }
193  file.close();
194  }
195 }
196 
198 {
199  // for MT, thread id used in the name of the file to avoid pb
200  G4int thread_id = 0;
201 #if G4MULTITHREADED
202  thread_id = G4Threading::G4GetThreadId();
203 #endif
204  std::ostringstream filename;
205  filename.clear();
206  filename << fPathToData << "/" << fBaseName << "_" << std::setfill('0') << std::setw(2) << thread_id << "_" << std::setw(4) << run_id << ".g4event";
207 
208  // close the file if already open
209  CloseFile();
210  fOutputFile.open(filename.str().data());
211  //
212  if ( fOutputFile ) {
213  G4cout << " The File " << filename.str() << " is open to record data " << G4endl;
214 
215  fOutputFile << "#" << std::endl;
216  fOutputFile << "# FORMAT: 'C' ID1 ID2 Energy(keV) x(cm) y(cm) z(cm) " << std::endl;
217  fOutputFile << "# with X=P for primary gammas and in this case:" << std::endl;
218  fOutputFile << "# ID1 is the vertexID, ID2 the total number of primaries" << std::endl;
219  fOutputFile << "# E the energy of the emitted gamma " << std::endl;
220  fOutputFile << "# x,y,z represents the momentum" << std::endl;
221  fOutputFile << "# with X=H for a single hit and in this case:" << std::endl;
222  fOutputFile << "# ID1 is the vertexID it comes from, ID2 the detector number" << std::endl;
223  fOutputFile << "# E the energy of the impact " << std::endl;
224  fOutputFile << "# x,y,z represents its position" << std::endl;
225  fOutputFile << "#" << std::endl;
226  } else {
227  G4cout << " *** ERROR *** cannot open " << filename.str() << " to record data " << G4endl;
228  }
229 }
230 
232 {
233  if ( fOutputFile.is_open() )
234  {
235  fOutputFile.close();
236  fOutputFile.clear();
237  }
238 }
239 
240 /*
241 std::pair<G4int, G4int> SToGS::AsciiAction::HitsinCollection(const G4Event)
242 {
243 
244 }
245 */
246 
248 {
249  G4Run* therun = 0x0; G4cout << " In AsciiAction, Generate a new Run " << G4endl;
250 
251  // 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 ?
252 #if G4MULTITHREADED
253  if ( G4Threading::IsWorkerThread() ) {
254 // if ( IsMaster() ) {
255  SToGS::AsciiRun *loc_therun = new SToGS::AsciiRun(fOutputFile);
256  therun = loc_therun;
257  }
258  else therun = G4UserRunAction::GenerateRun();
259 #else
260  therun = new SToGS::AsciiRun(fOutputFile);
261 #endif
262  return therun;
263 }
264 
266 {
267  G4cout << " In AsciiAction, Begin of Run " << aRun->GetRunID()
268  << ", Number of events to be simulated " << aRun->GetNumberOfEventToBeProcessed() << G4endl;
269  //
270  OpenFile(aRun->GetRunID());
271 }
272 void SToGS::AsciiAction::EndOfRunAction(const G4Run* aRun)
273 {
274  G4cout << " In AsciiAction, End of Run " << aRun->GetRunID() << " " << aRun->GetNumberOfEvent() << G4endl;
275  //
276  CloseFile();
277 }
279 {
280  G4int evtNb = evt->GetEventID();
281  if ( evtNb % fPrintModulo == 0 ) {
282  G4cout << " In AsciiAction, Begin of event: " << evtNb << G4endl;
283  }
284 }
285 void SToGS::AsciiAction::EndOfEventAction(const G4Event *evt)
286 {
287  G4int evtNb = evt->GetEventID();
288  if ( evtNb % fPrintModulo == 0 ) {
289  G4cout << " In AsciiAction, End of event: " << evtNb << G4endl;
290  }
291 }
292 
virtual void EndOfRunAction(const G4Run *)
Definition: SToGS_Ascii.cc:272
G4THitsCollection< TrackerHit > TrackerHitsCollection
virtual void BeginOfEventAction(const G4Event *)
Definition: SToGS_Ascii.cc:278
virtual ~AsciiRun()
Definition: SToGS_Ascii.cc:57
AsciiRun(std::ofstream &out)
A new event in the file starts/ends with the following characters.
Definition: SToGS_Ascii.cc:40
virtual void EndOfEventAction(const G4Event *)
Definition: SToGS_Ascii.cc:285
G4THitsCollection< CopClusterHit > CopClusterHitsCollection
virtual void BeginOfRunAction(const G4Run *)
Definition: SToGS_Ascii.cc:265
virtual G4Run * GenerateRun()
Definition: SToGS_Ascii.cc:247
G4int fMaxEvents
max numer of event per files ... better to limit because of ascii file could be uged ! ...
Definition: SToGS_Ascii.hh:76
G4int fRecordOption
0 [default] means keep all, 1 only events which gives at least one hit in the full detector ...
Definition: SToGS_Ascii.hh:78
AsciiAction(G4String conffile="setup/SToGS_ascii_actions.conf")
Definition: SToGS_Ascii.cc:150
G4String fBaseName
base for all the files
Definition: SToGS_Ascii.hh:74
virtual void CloseFile()
Make sure ths file is closed properly.
Definition: SToGS_Ascii.cc:231
virtual void OpenFile(G4int run_id)
Just check if there are collected hits in the collection.
Definition: SToGS_Ascii.cc:197
virtual void RecordEvent(const G4Event *evt)
Definition: SToGS_Ascii.cc:61
Base class that regroups in the same space all user's hooks. Convenient for sharing similar data...
G4int fPrintModulo
to print out status any fPrintModulo events
Definition: SToGS_Ascii.hh:80
G4String fPathToData
directory where to output data
Definition: SToGS_Ascii.hh:72