4 // See the class description in the header file.
6 #include "TG4StepManager.h"
7 #include "TG4GeometryServices.h"
8 #include "TG4ParticlesManager.h"
9 #include "TG4PhysicsManager.h"
10 #include "TG4VSensitiveDetector.h"
11 #include "TG4Limits.h"
12 #include "TG4Globals.h"
13 #include "TG4G3Units.h"
15 #include <G4SteppingManager.hh>
16 #include <G4UserLimits.hh>
17 #include <G4UImanager.hh>
18 #include <G4AffineTransform.hh>
19 #include <G4TransportationManager.hh>
20 #include <G4Navigator.hh>
21 #include <G4VProcess.hh>
22 #include <G4ProcessManager.hh>
23 #include <G4ProcessVector.hh>
25 #include <Randomize.hh>
26 #include <TLorentzVector.h>
28 TG4StepManager* TG4StepManager::fgInstance = 0;
30 TG4StepManager::TG4StepManager()
33 fStepStatus(kNormalStep),
38 TG4Globals::Exception(
39 "TG4StepManager: attempt to create two instances of singleton.");
45 TG4StepManager::TG4StepManager(const TG4StepManager& right) {
47 TG4Globals::Exception(
48 "Attempt to copy TG4StepManager singleton.");
51 TG4StepManager::~TG4StepManager() {
57 TG4StepManager& TG4StepManager::operator=(const TG4StepManager& right)
59 // check assignement to self
60 if (this == &right) return *this;
62 TG4Globals::Exception(
63 "Attempt to assign TG4StepManager singleton.");
70 void TG4StepManager::CheckTrack() const
72 // Gives exception in case the track is not defined.
76 TG4Globals::Exception("TG4StepManager: Track is not defined.");
80 void TG4StepManager::CheckStep(const G4String& method) const
82 // Gives exception in case the step is not defined.
86 G4String text = "TG4StepManager::";
87 text = text + method + ": Step is not defined.";
88 TG4Globals::Exception(text);
93 void TG4StepManager::CheckSteppingManager() const
95 // Gives exception in case the step is not defined.
98 if (!fSteppingManager)
99 TG4Globals::Exception("TG4StepManager: Stepping manager is not defined.");
103 void TG4StepManager::SetTLorentzVector(G4ThreeVector xyz, G4double t,
104 TLorentzVector& lv) const
106 // Fills TLorentzVector with G4ThreeVector and G4double.
115 G4VPhysicalVolume* TG4StepManager::GetCurrentOffPhysicalVolume(G4int off) const
117 // Returns the physical volume of the off-th mother's
118 // of the current volume.
121 G4VPhysicalVolume* physVolume = GetCurrentPhysicalVolume();
123 G4VPhysicalVolume* mother = physVolume;
127 if (mother) mother = mother->GetMother();
132 G4String text = "TG4StepManager::CurrentVolOff: \n";
133 text = text + " Volume ";
134 text = text + physVolume->GetName();
135 text = text + " has not defined mother in the required level.";
136 TG4Globals::Warning(text);
144 void TG4StepManager::StopTrack()
146 // Stops the current track and skips to the next.
147 // ?? do we want to invoke rest processes?
148 // ?? do we want to stop secondaries too?
149 // possible "stop" track status from G4:
150 // fStopButAlive, // Invoke active rest physics processes and
151 // // and kill the current track afterward
152 // fStopAndKill, // Kill the current track
153 // fKillTrackAndSecondaries,
154 // // Kill the current track and also associated
162 fTrack->SetTrackStatus(fStopAndKill);
163 // fTrack->SetTrackStatus(fStopButAlive);
164 // fTrack->SetTrackStatus(fKillTrackAndSecondaries);
167 void TG4StepManager::StopEvent()
169 // Aborts the current event processing.
176 fTrack->SetTrackStatus(fKillTrackAndSecondaries);
177 //StopTrack(); // cannot be used as it keeps secondaries
178 G4UImanager::GetUIpointer()->ApplyCommand("/event/abort");
179 G4UImanager::GetUIpointer()->ApplyCommand("/alStacking/clearStack");
182 void TG4StepManager::SetMaxStep(Float_t step)
184 // Maximum step allowed in the current logical volume.
187 G4LogicalVolume* curLogVolume
188 = GetCurrentPhysicalVolume()->GetLogicalVolume();
189 G4UserLimits* userLimits
190 = curLogVolume->GetUserLimits();
192 if (userLimits == 0) {
194 userLimits = new TG4Limits();
196 // set limits to all logical volumes
197 // corresponding to the current "G3" volume
198 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
199 G4int nofLV = geometryServices->SetUserLimits(userLimits, curLogVolume);
203 userLimits->SetMaxAllowedStep(step*TG4G3Units::Length());
207 void TG4StepManager::SetMaxNStep(Int_t maxNofSteps)
209 // Not yet implemented.
213 "TG4StepManager::SetMaxNStep(..) is not yet implemented.");
216 void TG4StepManager::SetUserDecay(Int_t pdg)
218 // Not yet implemented.
221 TG4Globals::Exception(
222 "TG4StepManager::SetUserDecay(..) is not yet implemented.");
225 G4VPhysicalVolume* TG4StepManager::GetCurrentPhysicalVolume() const
227 // Returns the current physical volume.
228 // According to fStepStatus the volume from track vertex,
229 // pre step point or post step point is returned.
232 G4VPhysicalVolume* physVolume;
233 if (fStepStatus == kNormalStep) {
236 CheckStep("GetCurrentPhysicalVolume");
239 physVolume = fStep->GetPreStepPoint()->GetPhysicalVolume();
241 else if (fStepStatus == kBoundary) {
244 CheckStep("GetCurrentPhysicalVolume");
247 physVolume = fStep->GetPostStepPoint()->GetPhysicalVolume();
255 G4ThreeVector position = fTrack->GetPosition();
256 G4Navigator* navigator =
257 G4TransportationManager::GetTransportationManager()->
258 GetNavigatorForTracking();
260 = navigator->LocateGlobalPointAndSetup(position);
266 Int_t TG4StepManager::CurrentVolID(Int_t& copyNo) const
268 // Returns the current sensitive detector ID
269 // and the copy number of the current physical volume.
272 G4VPhysicalVolume* physVolume = GetCurrentPhysicalVolume();
273 copyNo = physVolume->GetCopyNo() + 1;
275 // sensitive detector ID
276 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
277 return geometryServices->GetVolumeID(physVolume->GetLogicalVolume());
280 Int_t TG4StepManager::CurrentVolOffID(Int_t off, Int_t& copyNo) const
282 // Returns the off-th mother's of the current volume
283 // the sensitive detector ID and the copy number.
286 if (off == 0) return CurrentVolID(copyNo);
288 G4VPhysicalVolume* mother = GetCurrentOffPhysicalVolume(off);
291 copyNo = mother->GetCopyNo() + 1;
293 // sensitive detector ID
294 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
295 return geometryServices->GetVolumeID(mother->GetLogicalVolume());
303 const char* TG4StepManager::CurrentVolName() const
305 // Returns the current physical volume name.
308 return GetCurrentPhysicalVolume()->GetName();
311 const char* TG4StepManager::CurrentVolOffName(Int_t off) const
313 // Returns the off-th mother's physical volume name.
316 if (off == 0) return CurrentVolName();
318 G4VPhysicalVolume* mother = GetCurrentOffPhysicalVolume(off);
321 return mother->GetName();
326 Int_t TG4StepManager::CurrentMaterial(Float_t &a, Float_t &z, Float_t &dens,
327 Float_t &radl, Float_t &absl) const
329 // Returns the parameters of the current material during transport
330 // the return value is the number of elements in the mixture.
333 G4VPhysicalVolume* physVolume = GetCurrentPhysicalVolume();
336 = physVolume->GetLogicalVolume()->GetMaterial();
339 G4int nofElements = material->GetNumberOfElements();
340 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
341 a = geometryServices->GetEffA(material);
342 z = geometryServices->GetEffZ(material);
345 dens = material->GetDensity();
346 dens /= TG4G3Units::MassDensity();
349 radl = material->GetRadlen();
350 radl /= TG4G3Units::Length();
352 absl = 0.; // this parameter is not defined in Geant4
356 TG4Globals::Exception(
357 "TG4StepManager::CurrentMaterial(..): material is not defined.");
362 void TG4StepManager::Gmtod(Float_t* xm, Float_t* xd, Int_t iflag)
364 // Transforms a position from the world reference frame
365 // to the current volume reference frame.
367 // Geant3 desription:
368 // ==================
369 // Computes coordinates XD (in DRS)
370 // from known coordinates XM in MRS
371 // The local reference system can be initialized by
372 // - the tracking routines and GMTOD used in GUSTEP
373 // - a call to GMEDIA(XM,NUMED)
374 // - a call to GLVOLU(NLEVEL,NAMES,NUMBER,IER)
375 // (inverse routine is GDTOM)
377 // If IFLAG=1 convert coordinates
378 // IFLAG=2 convert direction cosinus
382 G4AffineTransform affineTransform;
384 if (fStepStatus == kVertex) {
385 G4Navigator* navigator =
386 G4TransportationManager::GetTransportationManager()->
387 GetNavigatorForTracking();
389 affineTransform = navigator->GetGlobalToLocalTransform();
398 = fStep->GetPreStepPoint()->GetTouchable()->GetHistory()
402 G4ThreeVector theGlobalPoint(xm[0],xm[1],xm[2]);
403 G4ThreeVector theLocalPoint;
405 theLocalPoint = affineTransform.TransformPoint(theGlobalPoint);
406 else if ( iflag == 2)
407 theLocalPoint = affineTransform.TransformAxis(theGlobalPoint);
409 TG4Globals::Exception(
410 "TG4StepManager::Gmtod(..,iflag): iflag is not in 1..2");
412 xd[0] = theLocalPoint.x();
413 xd[1] = theLocalPoint.y();
414 xd[2] = theLocalPoint.z();
417 void TG4StepManager::Gdtom(Float_t* xd, Float_t* xm, Int_t iflag)
419 // Transforms a position from the current volume reference frame
420 // to the world reference frame.
422 // Geant3 desription:
423 // ==================
424 // Computes coordinates XM (Master Reference System
425 // knowing the coordinates XD (Detector Ref System)
426 // The local reference system can be initialized by
427 // - the tracking routines and GDTOM used in GUSTEP
428 // - a call to GSCMED(NLEVEL,NAMES,NUMBER)
429 // (inverse routine is GMTOD)
431 // If IFLAG=1 convert coordinates
432 // IFLAG=2 convert direction cosinus
436 G4AffineTransform affineTransform;
438 if (fStepStatus == kVertex) {
439 G4Navigator* navigator =
440 G4TransportationManager::GetTransportationManager()->
441 GetNavigatorForTracking();
443 affineTransform = navigator->GetLocalToGlobalTransform();
454 = fStep->GetPreStepPoint()->GetTouchable()->GetHistory()
455 ->GetTopTransform().Inverse();
458 G4ThreeVector theLocalPoint(xd[0],xd[1],xd[2]);
459 G4ThreeVector theGlobalPoint;
461 theGlobalPoint = affineTransform.TransformPoint(theLocalPoint);
463 theGlobalPoint = affineTransform.TransformAxis(theLocalPoint);
466 "TG4StepManager::Gdtom(...,iflag): iflag is not in 1..2");
468 xm[0] = theGlobalPoint.x();
469 xm[1] = theGlobalPoint.y();
470 xm[2] = theGlobalPoint.z();
473 Float_t TG4StepManager::MaxStep() const
475 // Returns maximum step allowed in the current logical volume
479 G4LogicalVolume* curLogVolume
480 = GetCurrentPhysicalVolume()->GetLogicalVolume();
483 G4UserLimits* userLimits
484 = curLogVolume->GetUserLimits();
487 if (userLimits == 0) {
488 G4String text = "User Limits are not defined for log volume ";
489 text = text + curLogVolume->GetName();
490 TG4Globals::Warning(text);
494 const G4Track& trackRef = *(fTrack);
495 maxStep = userLimits->GetMaxAllowedStep(trackRef);
496 maxStep /= TG4G3Units::Length();
501 Int_t TG4StepManager::GetMaxNStep() const
503 // Not yet implemented.
507 "Method GetMaxNStep is not yet implemented in TG4StepManager.");
511 void TG4StepManager::TrackPosition(TLorentzVector& position) const
513 // Current particle position (in the world reference frame)
514 // and the local time since the current track is created
515 // (position of the PostStepPoint).
523 // check if this is == to PostStepPoint position !!
524 G4ThreeVector positionVector = fTrack->GetPosition();
525 positionVector *= 1./(TG4G3Units::Length());
528 G4double time = fTrack->GetLocalTime();
529 time /= TG4G3Units::Time();
531 SetTLorentzVector(positionVector, time, position);
534 Int_t TG4StepManager::GetMedium() const
536 // Returns the second index of the current material (corresponding to
537 // G3 tracking medium index).
541 G4Material* curMaterial
542 = GetCurrentPhysicalVolume()->GetLogicalVolume()->GetMaterial();
545 TG4GeometryServices* geometryServices = TG4GeometryServices::Instance();
546 return geometryServices->GetMediumId(curMaterial);
549 void TG4StepManager::TrackMomentum(TLorentzVector& momentum) const
551 // Current particle "momentum" (px, py, pz, Etot).
558 G4ThreeVector momentumVector = fTrack->GetMomentum();
559 momentumVector *= 1./(TG4G3Units::Energy());
561 G4double energy = fTrack->GetDynamicParticle()->GetTotalEnergy();
562 energy /= TG4G3Units::Energy();
564 SetTLorentzVector(momentumVector, energy, momentum);
567 void TG4StepManager::TrackVertexPosition(TLorentzVector& position) const
569 // The vertex particle position (in the world reference frame)
570 // and the local time since the current track is created.
578 G4ThreeVector positionVector = fTrack->GetVertexPosition();
579 positionVector *= 1./(TG4G3Units::Length());
583 G4double time = fTrack->GetLocalTime();
584 time /= TG4G3Units::Time();
586 SetTLorentzVector(positionVector, time, position);
589 void TG4StepManager::TrackVertexMomentum(TLorentzVector& momentum) const
591 // The vertex particle "momentum" (px, py, pz, Ekin)
592 // to do: change Ekin -> Etot
599 G4ThreeVector momentumVector = fTrack->GetVertexMomentumDirection();
600 momentumVector *= 1./(TG4G3Units::Energy());
602 G4double energy = fTrack->GetVertexKineticEnergy();
603 energy /= TG4G3Units::Energy();
605 SetTLorentzVector(momentumVector, energy, momentum);
608 Float_t TG4StepManager::TrackStep() const
610 // Returns the current step length.
614 if (fStepStatus == kNormalStep) {
617 CheckStep("TrackStep");
620 length = fStep->GetStepLength();
621 length /= TG4G3Units::Length();
629 Float_t TG4StepManager::TrackLength() const
631 // Returns the length of the current track from its origin.
638 G4double length = fTrack->GetTrackLength();
639 length /= TG4G3Units::Length();
643 Float_t TG4StepManager::TrackTime() const
645 // Returns the local time since the current track is created.
647 // in Geant4: there is also defined proper time as
648 // the proper time of the dynamical particle of the current track.
655 G4double time = fTrack->GetLocalTime();
656 time /= TG4G3Units::Time();
660 Float_t TG4StepManager::Edep() const
662 // Returns total energy deposit in this step.
665 G4double energyDeposit;
666 if (fStepStatus == kNormalStep) {
672 energyDeposit = fStep->GetTotalEnergyDeposit();
673 energyDeposit /= TG4G3Units::Energy();
678 return energyDeposit;
681 Int_t TG4StepManager::TrackPid() const
683 // Returns the current particle PDG encoding.
690 G4ParticleDefinition* particle
691 = fTrack->GetDynamicParticle()->GetDefinition();
693 // ask TG4ParticlesManager to get PDG encoding
694 // (in order to get PDG from extended TDatabasePDG
695 // in case the standard PDG code is not defined)
697 = TG4ParticlesManager::Instance()->GetPDGEncodingFast(particle);
702 Float_t TG4StepManager::TrackCharge() const
704 // Returns the current particle charge.
712 = fTrack->GetDynamicParticle()->GetDefinition()
714 charge /= TG4G3Units::Charge();
718 Float_t TG4StepManager::TrackMass() const
720 // Returns current particle rest mass.
728 = fTrack->GetDynamicParticle()->GetDefinition()
730 mass /= TG4G3Units::Mass();
734 Float_t TG4StepManager::Etot() const
736 // Returns total energy of the current particle.
744 = fTrack->GetDynamicParticle()->GetTotalEnergy();
745 energy /= TG4G3Units::Energy();
749 Bool_t TG4StepManager::IsTrackInside() const
751 // Returns true if particle does not cross geometrical boundary
752 // and is not in vertex.
755 if (fStepStatus == kNormalStep && !(IsTrackExiting()) ) {
756 // track is always inside during a normal step
763 Bool_t TG4StepManager::IsTrackEntering() const
765 // Returns true if particle cross a geometrical boundary
769 if (fStepStatus != kNormalStep) {
770 // track is entering during a vertex or boundary step
777 Bool_t TG4StepManager::IsTrackExiting() const
779 // Returns true if particle cross a geometrical boundary.
782 if (fStepStatus == kNormalStep) {
785 CheckStep("IsTrackExiting");
788 if (fStep->GetPostStepPoint()->GetStepStatus() == fGeomBoundary)
795 Bool_t TG4StepManager::IsTrackOut() const
797 // Returns true if particle cross the world boundary
798 // at post-step point.
801 if (fStepStatus == kVertex) return false;
804 CheckStep("IsTrackCut");
809 = fStep->GetPostStepPoint()->GetStepStatus();
810 if (status == fWorldBoundary)
816 Bool_t TG4StepManager::IsTrackStop() const
818 // Returns true if particle has stopped
819 // or has been killed, suspended or postponed to next event.
821 // Possible track status from G4:
822 // fAlive, // Continue the tracking
823 // fStopButAlive, // Invoke active rest physics processes and
824 // // and kill the current track afterward
825 // fStopAndKill, // Kill the current track
826 // fKillTrackAndSecondaries,
827 // // Kill the current track and also associated
829 // fSuspend, // Suspend the current track
830 // fPostponeToNextEvent
831 // // Postpones the tracking of thecurrent track
832 // // to the next event.
841 = fTrack->GetTrackStatus();
842 if ((status == fStopAndKill) ||
843 (status == fKillTrackAndSecondaries) ||
844 (status == fSuspend) ||
845 (status == fPostponeToNextEvent)) {
852 Bool_t TG4StepManager::IsTrackDisappeared() const
854 // Returns true if particle has disappeared
855 // (due to any physical process)
856 // or has been killed, suspended or postponed to next event.
865 = fTrack->GetTrackStatus();
866 if ((status == fStopButAlive) ||
867 (status == fKillTrackAndSecondaries) ||
868 (status == fSuspend) ||
869 (status == fPostponeToNextEvent)) {
876 Bool_t TG4StepManager::IsTrackAlive() const
878 // Returns true if particle continues tracking.
886 = fTrack->GetTrackStatus();
887 if (status == fAlive)
893 Bool_t TG4StepManager::IsNewTrack() const
895 // Returns true when track performs the first step.
898 if (fStepStatus == kVertex)
904 Int_t TG4StepManager::NSecondaries() const
906 // Returns the number of secondary particles generated
907 // in the current step.
911 CheckSteppingManager();
914 G4int nofSecondaries = 0;
915 nofSecondaries += fSteppingManager->GetfN2ndariesAtRestDoIt();
916 nofSecondaries += fSteppingManager->GetfN2ndariesAlongStepDoIt();
917 nofSecondaries += fSteppingManager->GetfN2ndariesPostStepDoIt();
919 return nofSecondaries;
922 void TG4StepManager::GetSecondary(Int_t index, Int_t& particleId,
923 TLorentzVector& position, TLorentzVector& momentum)
925 // Fills the parameters (particle encoding, position, momentum)
926 // of the generated secondary particle which is specified by index.
927 // !! Check if indexing of secondaries is same !!
931 CheckSteppingManager();
934 G4int nofSecondaries = NSecondaries();
935 G4TrackVector* secondaryTracks = fSteppingManager->GetSecondary();
937 if (secondaryTracks){
938 if (index < nofSecondaries) {
940 // the index of the first secondary of this step
942 = secondaryTracks->size() - nofSecondaries;
943 // (the secondaryTracks vector contains secondaries
944 // produced by the track at previous steps, too)
946 = (*secondaryTracks)[startIndex + index];
950 = track->GetDynamicParticle()->GetDefinition()->GetPDGEncoding();
953 G4ThreeVector positionVector = track->GetPosition();
954 positionVector *= 1./(TG4G3Units::Length());
955 G4double time = track->GetLocalTime();
956 time /= TG4G3Units::Time();
957 SetTLorentzVector(positionVector, time, position);
960 G4ThreeVector momentumVector = track->GetMomentum();
961 G4double energy = track->GetDynamicParticle()->GetTotalEnergy();
962 energy /= TG4G3Units::Energy();
963 SetTLorentzVector(momentumVector, energy, momentum);
966 TG4Globals::Exception(
967 "TG4StepManager::GetSecondary(): wrong secondary track index.");
971 TG4Globals::Exception(
972 "TG4StepManager::GetSecondary(): secondary tracks vector is empty");
976 AliMCProcess TG4StepManager::ProdProcess(Int_t isec) const
978 // The process that has produced the secondary particles specified
979 // with isec index in the current step.
982 G4int nofSecondaries = NSecondaries();
983 if (fStepStatus == kVertex || !nofSecondaries) return kPNoProcess;
986 CheckStep("ProdProcess");
989 G4TrackVector* secondaryTracks = fSteppingManager->GetSecondary();
991 // should never happen
992 if (!secondaryTracks) {
993 TG4Globals::Exception(
994 "TG4StepManager::ProdProcess(): secondary tracks vector is empty.");
999 if (isec < nofSecondaries) {
1001 // the index of the first secondary of this step
1003 = secondaryTracks->size() - nofSecondaries;
1004 // the secondaryTracks vector contains secondaries
1005 // produced by the track at previous steps, too
1007 // the secondary track with specified isec index
1008 G4Track* track = (*secondaryTracks)[startIndex + isec];
1010 const G4VProcess* kpProcess = track->GetCreatorProcess();
1012 AliMCProcess mcProcess
1013 = TG4PhysicsManager::Instance()->GetMCProcess(kpProcess);
1015 // distinguish kPDeltaRay from kPEnergyLoss
1016 if (mcProcess == kPEnergyLoss) mcProcess = kPDeltaRay;
1021 TG4Globals::Exception(
1022 "TG4StepManager::GetSecondary(): wrong secondary track index.");
1029 Int_t TG4StepManager::StepProcesses(TArrayI &proc) const
1031 // Fills the array of processes that were active in the current step
1032 // and returns the number of them.
1033 // TBD: Distinguish between kPDeltaRay and kPEnergyLoss
1036 if (fStepStatus == kVertex) {
1037 G4cout << "kVertex" << G4endl;
1038 G4int nofProcesses = 1;
1039 proc.Set(nofProcesses);
1041 return nofProcesses;
1044 #ifdef TGEANT4_DEBUG
1045 CheckSteppingManager();
1046 CheckStep("StepProcesses");
1049 // along step processes
1050 G4ProcessManager* processManager
1051 = fStep->GetTrack()->GetDefinition()->GetProcessManager();
1052 G4ProcessVector* alongStepProcessVector
1053 = processManager->GetAlongStepProcessVector();
1054 G4int nofProcesses = alongStepProcessVector->entries();
1056 // process defined step
1057 const G4VProcess* kpLastProcess
1058 = fStep->GetPostStepPoint()->GetProcessDefinedStep();
1060 // fill the array of processes
1061 proc.Set(nofProcesses);
1062 TG4PhysicsManager* physicsManager = TG4PhysicsManager::Instance();
1064 for (i=0; i<nofProcesses-1; i++) {
1065 G4VProcess* g4Process = (*alongStepProcessVector)[i];
1066 // do not fill transportation along step process
1067 if (g4Process->GetProcessName() != "Transportation") {
1068 physicsManager->GetMCProcess(g4Process);
1069 proc[i] = physicsManager->GetMCProcess(g4Process);
1072 proc[nofProcesses-1] = physicsManager->GetMCProcess(kpLastProcess);
1074 return nofProcesses;