SToGS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ParisUserAction.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 "ParisUserAction.hh"
28 #include "SToGS_G4_TrackerSD.hh"
29 #include "SToGS_G4_CopClusterSD.hh"
31 
32 #include "TTree.h"
33 
34 #include "G4OpticalPhoton.hh"
35 #include "G4VProcess.hh"
36 #include "G4Event.hh"
37 #if G4MULTITHREADED
38 #include "G4Threading.hh"
39 #endif
40 #ifdef G4MULTITHREADED
41 #include "G4AutoLock.hh"
42 namespace { G4Mutex buildMutex = G4MUTEX_INITIALIZER; }
43 #endif
44 
46  SToGS::BaseROOTTreeRun(),
47  fTree(tree),
48  fPrimaryEvent(),
49  fEvent()
50 {
51  fTree->Branch("Pr.", &fPrimaryEvent);
52  fTree->Branch("Ev.", &fEvent);
53 }
54 
55 void ParisEventRun::RecordEvent(const G4Event* evt)
56 {
57  SToGS::TrackerHitsCollection *THC = 0x0; SToGS::CopClusterHitsCollection *CHC = 0x0; G4int nb_hits_tot = 0, nb_hits_trac = 0, nb_hits_calo = 0;
58 
59  // check there is something to write in files
60  if( colltrackerID < 0 && collcaloID < 0 )
61  return;
62 
63  fPrimaryEvent.Clear("");
64  fEvent.Clear("");
65 
66  G4HCofThisEvent *HCE = evt->GetHCofThisEvent();
67  if(HCE)
68  {
69  THC = (SToGS::TrackerHitsCollection *)(HCE->GetHC(colltrackerID));
70  if ( colltrackerID > -1 ) {
71  nb_hits_trac += THC->entries();
72  }
73  CHC = (SToGS::CopClusterHitsCollection*)(HCE->GetHC(collcaloID));
74  if ( collcaloID > -1 ) {
75  nb_hits_calo += CHC->entries();
76  }
77  nb_hits_tot = nb_hits_trac + nb_hits_calo;
78  }
79 
80  // write primary part
81  G4int istart = 0, iend = evt->GetNumberOfPrimaryVertex();
82  G4int K = 0; G4double H = 0.0;
83  for( G4int i = istart; i < iend ; i++ ){
84 
85  G4int jstart = 0, jend = evt->GetPrimaryVertex(i)->GetNumberOfParticle(); // to get the next vertex
86  for( G4int j = jstart; j < jend; j++ ) {
87 
88  PHit *hit = fPrimaryEvent.AddHit();
89 
90  K++;
91  G4PrimaryParticle *prim = evt->GetPrimaryVertex(i)->GetPrimary(j); // get the next particle for vertex #i
92 
93  hit->fE = (std::sqrt( prim->GetMomentum().mag2() + prim->GetMass() * prim->GetMass()) - prim->GetMass())/CLHEP::keV;
94  hit->fX = prim->GetPx();
95  hit->fY = prim->GetPy();
96  hit->fZ = prim->GetPz();
97 
98  hit->fFlag = prim->GetTrackID()-1;
99 
100  H += (std::sqrt( prim->GetMomentum().mag2() + prim->GetMass() * prim->GetMass()) - prim->GetMass())/CLHEP::MeV;
101  }
102  }
103  fPrimaryEvent.SetHK(H,K);
104 
105  if ( nb_hits_trac ) {
106  int n_hit = nb_hits_trac;
107 
108  K = n_hit; H = 0.0;
109  for (int i = 0 ; i < n_hit; i++) {
110 
111  PHit *hit = fEvent.AddHit();
112 
113  if ( hit == 0x0 ) {
114  G4cout << " Cannot add more that " << i << " hits in the collection " << G4endl;
115  break;
116  }
117 
118  hit->fID = PHit::kGamma ;
119 
120  hit->fE = (*THC)[i]->GetEdep()/CLHEP::keV;
121  hit->fX = (*THC)[i]->GetPos().x()/CLHEP::cm;
122  hit->fY = (*THC)[i]->GetPos().y()/CLHEP::cm;
123  hit->fZ = (*THC)[i]->GetPos().z()/CLHEP::cm;
124  hit->fT = (*THC)[i]->GetToF();
125 
126  hit->fFlag = (*THC)[i]->GetPrimaryID()-1 ;
127  hit->fUID = (*THC)[i]->GetDetID();
128 
129  H += (*THC)[i]->GetEdep()/CLHEP::MeV;
130  }
131  fEvent.SetHK(H,K);
132  }
133  if ( nb_hits_calo )
134  {
135  int n_hit = nb_hits_calo;
136 
137  K = n_hit; H = 0.0;
138  for (int i = 0 ; i < n_hit; i++) {
139 
140  PHit *hit = fEvent.AddHit();
141  if ( hit == 0x0 ) {
142  G4cout << " Cannot add more that " << i << " hits in the collection " << G4endl;
143  break;
144  }
145  hit->fID = PHit::kUnknown ;
146 
147  hit->fE = (*CHC)[i]->GetEdep()/CLHEP::keV;
148  hit->fX = (*CHC)[i]->GetPos().x()/CLHEP::cm;
149  hit->fY = (*CHC)[i]->GetPos().y()/CLHEP::cm;
150  hit->fZ = (*CHC)[i]->GetPos().z()/CLHEP::cm;
151  hit->fT = (*CHC)[i]->GetToF();
152 
153  hit->fFlag = (*CHC)[i]->GetNbHits() ;
154  hit->fUID = (*CHC)[i]->GetDetID();
155 
156  H += (*CHC)[i]->GetEdep()/CLHEP::MeV;
157  }
158  fEvent.SetHK(H,K);
159  }
160  if ( fRecordOption == 1 ) {
161  if ( fEvent.GetK() > 0 ) fTree->Fill();
162  }
163  else fTree->Fill();
164 }
165 
167  SToGS::BaseROOTTreeAction(conffile),
168  fisOptical(0),
169  fOpticalEventBeg(0x0),
170  fOpticalEventEnd(0x0)
171 {
172  // open the ascii file
173  std::ifstream file(conffile.data());
174  if ( file.is_open() == false ) {
175  G4cout << " ** ParisUserAction WARNING ** Cannot open file " << conffile << ", default parameters to be used "<< G4endl;
176  }
177  else {
178  std::string aline; getline(file,aline);
179  while ( file.good() && !file.eof() ) {
180 
181  if ( aline[0] == '#' ){
182  getline(file,aline);
183  continue;
184  } // this line is a comment
185 
186  std::string key, what, option; std::istringstream decode(aline); decode.clear();
187  decode >> key;
188 
189  if ( key == "scintillation:" ) { // to switch on following scintillation photons
190  decode >> fisOptical ;
191  }
192  getline(file,aline);
193  }
194  file.close();
195  }
196 }
197 
199 {
200 #ifdef G4MULTITHREADED
201  G4AutoLock lock(&buildMutex);
202 #endif
203 
204  G4Run* therun = 0x0; G4cout << " In ParisUserAction, Generate a new Run " << G4endl;
205 
206  if ( fTree == 0x0 ) {
207  fTree = new TTree(fTreeName.data(),fTreeTitle.data());
208  fTree->SetDirectory(0x0);
209 // fTree->SetMaxTreeSize(fMaxEvents);
210 
211  if (fisOptical > 0) {
213  //
214  fTree->Branch("OpticalEvBegin.", fOpticalEventBeg);
215  fTree->Branch("OpticalEvEnd.", fOpticalEventEnd);
216  }
217  }
218 
219  // 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 ?
220 #if G4MULTITHREADED
221  if ( G4Threading::IsWorkerThread() ) {
222  // if ( IsMaster() ) {
223  ParisEventRun *loc_therun = new ParisEventRun(fTree);
224  therun = loc_therun;
225  }
226  else therun = G4UserRunAction::GenerateRun();
227 #else
228  therun = new ParisEventRun(fTree);
229 #endif
230  return therun;
231 }
232 
233 void ParisUserAction::EndOfRunAction(const G4Run *therun)
234 {
236  if (fisOptical > 0) {
237  delete fOpticalEventBeg; delete fOpticalEventEnd;
238  }
239 }
240 
241 void ParisUserAction::BeginOfEventAction(const G4Event *evt)
242 {
244 
245  // clear events
246  if ( fisOptical > 0 ) {
247  fOpticalEventBeg->Clear("");
248  fOpticalEventEnd->Clear("");
249  }
250 }
251 
252 void ParisUserAction::PreUserTrackingAction(const G4Track *atrack)
253 {
254  if ( fisOptical < 1 ) {
255  return;
256  }
257  //Count what process generated the optical photons
258  if(atrack->GetDefinition()==G4OpticalPhoton::OpticalPhotonDefinition()){
259  // particle is optical photon
260  if(atrack->GetParentID()>0){
261  // particle is secondary
262  if(atrack->GetCreatorProcess()->GetProcessName()=="Scintillation") {
263  // G4cout << "Scintillation Pre " << atrack->GetTrackID() << " " << atrack->GetTrackLength() << G4endl;
264 
265  SToGS::PrimaryTrackInformation *pinfo = (SToGS::PrimaryTrackInformation *)atrack->GetUserInformation();
266  G4int pid = atrack->GetTrackID();
267  if ( pinfo ) {
268  pid = pinfo->GetPrimaryID();
269  }
270 
272 
273  hit->fX = atrack->GetPosition().x();
274  hit->fY = atrack->GetPosition().y();
275  hit->fZ = atrack->GetPosition().z();
276 
277  hit->fTA = atrack->GetGlobalTime();
278  hit->fTL = atrack->GetLocalTime();
279 
280  hit->fLength = atrack->GetTrackLength();
281  hit->fNbSteps = atrack->GetCurrentStepNumber();
282 
283  hit->fPrimaryID = pid-1;
284  hit->fSecondaryID = atrack->GetVolume()->GetCopyNo(); // hit->fUID
285 
286  }
287  }
288  }
289 }
290 void ParisUserAction::PostUserTrackingAction(const G4Track *atrack)
291 {
292  if ( fisOptical < 1 ) {
293  return;
294  }
295  //Count what process generated the optical photons
296  if(atrack->GetDefinition()==G4OpticalPhoton::OpticalPhotonDefinition()){
297  // particle is optical photon
298  if(atrack->GetParentID()>0){
299  // particle is secondary
300  if(atrack->GetCreatorProcess()->GetProcessName()=="Scintillation") {
301  // G4cout << "Scintillation Post " << atrack->GetTrackID() << " " << atrack->GetTrackLength() << G4endl;
302 
303  SToGS::PrimaryTrackInformation *pinfo = (SToGS::PrimaryTrackInformation *)atrack->GetUserInformation();
304  G4int pid = atrack->GetTrackID();
305  if ( pinfo ) {
306  pid = pinfo->GetPrimaryID();
307  }
308 
310 
311  hit->fX = atrack->GetPosition().x();
312  hit->fY = atrack->GetPosition().y();
313  hit->fZ = atrack->GetPosition().z();
314 
315  hit->fTA = atrack->GetGlobalTime();
316  hit->fTL = atrack->GetLocalTime();
317 
318  hit->fLength = atrack->GetTrackLength();
319  hit->fNbSteps = atrack->GetCurrentStepNumber();
320 
321  hit->fPrimaryID = pid-1; // PrimaryID[(*THC)[i]->GetTrackID()]-1 ;
322  hit->fSecondaryID = atrack->GetVolume()->GetCopyNo(); // hit->fUID
323  }
324  }
325  }
326 }
327 
328 
329 
Double32_t fZ
Definition: ParisEvents.h:63
G4THitsCollection< TrackerHit > TrackerHitsCollection
virtual void PostUserTrackingAction(const G4Track *)
virtual void EndOfRunAction(const G4Run *)
POpticalEvent * fOpticalEventBeg
emitted optical photons
POpticalEvent * fOpticalEventEnd
end of optical photons
Int_t fSecondaryID
Definition: ParisEvents.h:152
Int_t fPrimaryID
Definition: ParisEvents.h:151
G4int fisOptical
true if one has also photons from scintillations
Int_t fUID
Definition: ParisEvents.h:66
ParisEventRun(TTree *tree)
Double32_t fX
Definition: ParisEvents.h:143
Double32_t fY
Definition: ParisEvents.h:144
Int_t GetK() const
Definition: ParisEvents.h:131
Double32_t fX
Definition: ParisEvents.h:61
G4THitsCollection< CopClusterHit > CopClusterHitsCollection
virtual void EndOfRunAction(const G4Run *)
Double32_t fLength
Definition: ParisEvents.h:149
virtual void BeginOfEventAction(const G4Event *)
virtual G4Run * GenerateRun()
Double32_t fTA
Definition: ParisEvents.h:146
virtual void RecordEvent(const G4Event *evt)
Double32_t fT
Definition: ParisEvents.h:64
void Clear(Option_t *opt)
clear the collection of hits, set H, K to 0
Definition: ParisEvents.C:122
The ParisEventRun has the charge to fill Paris Event from the SToGS G4 hit collections.
Double32_t fE
Definition: ParisEvents.h:59
void Clear(Option_t *opt)
Definition: ParisEvents.C:164
Double32_t fY
Definition: ParisEvents.h:62
TTree * fTree
the Tree to be filled
ParisUserAction(G4String conffile="setup/paris_actions.conf")
POpticalHit * AddHit()
add a hit to the current event
Definition: ParisEvents.C:169
Double32_t fTL
Definition: ParisEvents.h:147
PHit * AddHit()
add a hit to the current event
Definition: ParisEvents.C:130
Int_t fID
Definition: ParisEvents.h:60
void SetHK(Double_t h, Int_t k)
Definition: ParisEvents.h:126
G4int GetPrimaryID() const
Get the primary ID.
Int_t fFlag
Definition: ParisEvents.h:65
Double32_t fZ
Definition: ParisEvents.h:145
virtual void BeginOfEventAction(const G4Event *)
User Track Information that kept the origine of any track.
Int_t fNbSteps
Definition: ParisEvents.h:153
virtual void PreUserTrackingAction(const G4Track *)