31 #include "G4SDManager.hh"
32 #include "G4RunManager.hh"
34 #include "G4UnitsTable.hh"
37 #include "G4Threading.hh"
47 G4SDManager* SDman = G4SDManager::GetSDMpointer();
53 collcaloID = SDman->GetCollectionID(
"CopClusterHits");
66 if( colltrackerID < 0 && collcaloID < 0 )
69 G4HCofThisEvent *HCE = evt->GetHCofThisEvent();
73 if ( colltrackerID > -1 ) {
74 nb_hits += THC->entries();
77 if ( collcaloID > -1 ) {
78 nb_hits += CHC->entries();
88 G4int istart = 0, iend = evt->GetNumberOfPrimaryVertex();
90 for( G4int i = istart; i < iend ; i++ ){
91 G4int jstart = 0, jend = evt->GetPrimaryVertex(i)->GetNumberOfParticle();
92 for( G4int j = jstart; j < jend; j++ ) {
94 G4PrimaryParticle *prim = evt->GetPrimaryVertex(i)->GetPrimary(j);
100 << prim->GetTrackID() <<
" "
104 << (std::sqrt( prim->GetMomentum().mag2() + prim->GetMass() * prim->GetMass()) - prim->GetMass())/CLHEP::keV <<
" "
105 << prim->GetPx() <<
" " << prim->GetPy() <<
" " << prim->GetPz()
109 if(THC && colltrackerID != -1)
111 int n_hit = THC->entries();
112 for (
int i = 0 ; i < n_hit; i++) {
117 << (*THC)[i]->GetPrimaryID() <<
" "
120 << (*THC)[i]->GetDetID() <<
" "
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 <<
" "
125 << (*THC)[i]->GetToF()/CLHEP::ns
129 if(CHC && collcaloID != -1)
131 int n_hit = CHC->entries();
132 for (
int i = 0 ; i < n_hit; i++) {
139 << (*CHC)[i]->GetDetID() <<
" "
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 <<
" "
144 << (*CHC)[i]->GetToF()/CLHEP::ns
154 fBaseName(
"SToGS_Ascii"),
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;
165 std::string aline; getline(file,aline);
166 while ( file.good() && !file.eof() ) {
168 if ( aline[0] ==
'#' ){
173 std::string key, what, option; std::istringstream decode(aline);
176 if ( key ==
"path:" ) {
179 if ( key ==
"basename:" ) {
182 if ( key ==
"modulo:" ) {
185 if ( key ==
"max_event:" ) {
188 if ( key ==
"record_option:" ) {
202 thread_id = G4Threading::G4GetThreadId();
204 std::ostringstream filename;
206 filename << fPathToData <<
"/" << fBaseName <<
"_" << std::setfill(
'0') << std::setw(2) << thread_id <<
"_" << std::setw(4) << run_id <<
".g4event";
210 fOutputFile.open(filename.str().data());
213 G4cout <<
" The File " << filename.str() <<
" is open to record data " << G4endl;
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;
227 G4cout <<
" *** ERROR *** cannot open " << filename.str() <<
" to record data " << G4endl;
233 if ( fOutputFile.is_open() )
249 G4Run* therun = 0x0; G4cout <<
" In AsciiAction, Generate a new Run " << G4endl;
253 if ( G4Threading::IsWorkerThread() ) {
258 else therun = G4UserRunAction::GenerateRun();
267 G4cout <<
" In AsciiAction, Begin of Run " << aRun->GetRunID()
268 <<
", Number of events to be simulated " << aRun->GetNumberOfEventToBeProcessed() << G4endl;
270 OpenFile(aRun->GetRunID());
274 G4cout <<
" In AsciiAction, End of Run " << aRun->GetRunID() <<
" " << aRun->GetNumberOfEvent() << G4endl;
280 G4int evtNb = evt->GetEventID();
281 if ( evtNb % fPrintModulo == 0 ) {
282 G4cout <<
" In AsciiAction, Begin of event: " << evtNb << G4endl;
287 G4int evtNb = evt->GetEventID();
288 if ( evtNb % fPrintModulo == 0 ) {
289 G4cout <<
" In AsciiAction, End of event: " << evtNb << G4endl;
virtual void EndOfRunAction(const G4Run *)
G4THitsCollection< TrackerHit > TrackerHitsCollection
virtual void BeginOfEventAction(const G4Event *)
AsciiRun(std::ofstream &out)
A new event in the file starts/ends with the following characters.
virtual void EndOfEventAction(const G4Event *)
G4THitsCollection< CopClusterHit > CopClusterHitsCollection
virtual void BeginOfRunAction(const G4Run *)
virtual G4Run * GenerateRun()
G4int fMaxEvents
max numer of event per files ... better to limit because of ascii file could be uged ! ...
G4int fRecordOption
0 [default] means keep all, 1 only events which gives at least one hit in the full detector ...
AsciiAction(G4String conffile="setup/SToGS_ascii_actions.conf")
G4String fBaseName
base for all the files
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.
virtual void RecordEvent(const G4Event *evt)
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
G4String fPathToData
directory where to output data