EDepSimUserEventAction.cc
Go to the documentation of this file.
1 #include <TGeoManager.h>
2 #include <TGeoNode.h>
3 
5 #include "EDepSimException.hh"
7 #include "EDepSimVertexInfo.hh"
9 #include "EDepSimTrajectory.hh"
10 #include "EDepSimHitSegment.hh"
12 
13 #include "EDepSimLog.hh"
14 
15 #include <G4Event.hh>
16 #include <G4EventManager.hh>
17 #include <G4UnitsTable.hh>
18 #include <G4PrimaryVertex.hh>
19 #include <G4PrimaryParticle.hh>
20 #include <G4ParticleDefinition.hh>
21 #include <G4ios.hh>
22 #include <G4SDManager.hh>
23 #include <G4HCtable.hh>
24 #include <Randomize.hh>
25 
27 
29 
31  EDepSimNamedLog("Event", "Begin Event: " << evt->GetEventID()
32  << " w/ " << evt->GetNumberOfPrimaryVertex()
33  << " vertices");
34 
35  // The last chance to create the user information object. This should be
36  // created in the primary particle generater
37  if (!evt->GetUserInformation()) {
38  G4EventManager::GetEventManager()->
39  SetUserInformation(new EDepSim::UserEventInformation);
40  }
41 
42  if (!gGeoManager) {
43  EDepSimError("The geometry manager is not defined.");
44  EDepSimThrow("Missing Geometry Manager");
45  }
46 
47  int vtxNumber=0;
48  for (G4PrimaryVertex* vtx = evt->GetPrimaryVertex();
49  vtx;
50  vtx = vtx->GetNext()) {
51  ++vtxNumber;
52  gGeoManager->PushPath();
54  G4ThreeVector(vtx->GetX0(), vtx->GetY0(), vtx->GetZ0()));
56  "Event",
57  "Vertex: " << vtxNumber
58  << " w/ " << vtx->GetNumberOfParticle() << " primaries"
59  " in " << gGeoManager->GetPath());
60  gGeoManager->PopPath();
62  "Event",
63  "Position: "
64  << " (" << G4BestUnit(vtx->GetX0(),"Length")
65  << ", " << G4BestUnit(vtx->GetY0(),"Length")
66  << ", " << G4BestUnit(vtx->GetZ0(),"Length")
67  << ", " << G4BestUnit(vtx->GetT0(),"Time") << ")");
68  EDepSim::VertexInfo* vInfo
69  = dynamic_cast<EDepSim::VertexInfo*>(vtx->GetUserInformation());
70  if (vInfo) {
71  EDepSimNamedInfo("Event","Generator: " << vInfo->GetName());
72  EDepSimNamedInfo("Event","Reaction: " << vInfo->GetReaction());
73  int infoVertices = vInfo->GetNumberOfInformationalVertex();
74  for (int iVert = 0; iVert<infoVertices; ++iVert) {
75  const G4PrimaryVertex* ivtx
76  = vInfo->GetInformationalVertex(iVert);
77  for (int p=0; p<ivtx->GetNumberOfParticle(); ++p) {
78  G4PrimaryParticle* prim = ivtx->GetPrimary(p);
79  G4ParticleDefinition* partDef = prim->GetG4code();
80  G4ThreeVector dir = prim->GetMomentum().unit();
81  if (partDef) {
83  "Event",
84  "Info: " << partDef->GetParticleName()
85  << " w/ "
86  << G4BestUnit(prim->GetMomentum().mag(),"Energy")
87  << " Dir: (" << dir.x()
88  << ", " << dir.y()
89  << ", " << dir.z() << ")");
90  }
91  else {
93  "Event",
94  "Info: " << prim->GetPDGcode()
95  << " w/ "
96  << G4BestUnit(prim->GetMomentum().mag(),"Energy")
97  << " Dir: (" << dir.x()
98  << ", " << dir.y()
99  << ", " << dir.z() << ")");
100  }
101  }
102  }
103  }
104  for (int p=0; p<vtx->GetNumberOfParticle(); ++p) {
105  G4PrimaryParticle* prim = vtx->GetPrimary(p);
106  G4ParticleDefinition* partDef = prim->GetG4code();
107  G4ThreeVector dir = prim->GetMomentum().unit();
108  if (partDef) {
110  "Event",
111  partDef->GetParticleName()
112  << " w/ "
113  << G4BestUnit(prim->GetMomentum().mag(),"Energy")
114  << " Dir: (" << dir.x()
115  << ", " << dir.y()
116  << ", " << dir.z() << ")");
117  }
118  else {
120  "Event",
121  prim->GetPDGcode()
122  << " w/ "
123  << G4BestUnit(prim->GetMomentum().mag(),"Energy")
124  << " Dir: (" << dir.x()
125  << ", " << dir.y()
126  << ", " << dir.z() << ")");
127  }
128  }
129  }
130 
132 }
133 
135  EDepSimInfo("Event " << evt->GetEventID() << " completed.");
136 
137  // Fill the trajectories with the amount of energy deposited into
138  // sensitive detectors.
139  G4HCofThisEvent* HCofEvent = evt->GetHCofThisEvent();
140  if (!HCofEvent) return;
141  G4SDManager *sdM = G4SDManager::GetSDMpointer();
142  G4HCtable *hcT = sdM->GetHCtable();
143 
144  for (int i=0; i<hcT->entries(); ++i) {
145  G4String SDname = hcT->GetSDname(i);
146  G4String HCname = hcT->GetHCname(i);
147  int HCId = sdM->GetCollectionID(SDname+"/"+HCname);
148  G4VHitsCollection* g4Hits = HCofEvent->GetHC(HCId);
149  if (g4Hits->GetSize()<1) {
150  EDepSimWarn("No hits for " << SDname << "/" << HCname);
151  continue;
152  }
153  for (unsigned int h=0; h<g4Hits->GetSize(); ++h) {
154  EDepSim::HitSegment* g4Hit
155  = dynamic_cast<EDepSim::HitSegment*>(g4Hits->GetHit(h));
156  double energy = g4Hit->GetEnergyDeposit();
157  int trackId = g4Hit->GetContributors().front();
158  G4VTrajectory* g4Traj = EDepSim::TrajectoryMap::Get(trackId);
159  if (!g4Traj) {
160  EDepSimError("Missing trackId " << trackId);
161  continue;
162  }
163  EDepSim::Trajectory* traj
164  = dynamic_cast<EDepSim::Trajectory*>(g4Traj);
165  if (!traj) {
166  EDepSimError("Not a EDepSim::Trajectory " << trackId);
167  continue;
168  }
169  traj->AddSDEnergyDeposit(energy);
170  traj->AddSDLength(g4Hit->GetTrackLength());
171  for (int loopCount = 0; ; ++loopCount) {
172  int parentId = traj->GetParentID();
173  if (!parentId) break;
174  g4Traj = EDepSim::TrajectoryMap::Get(parentId);
175  if (!g4Traj) {
176  EDepSimError("Missing parentId " << parentId);
177  break;
178  }
179  traj = dynamic_cast<EDepSim::Trajectory*>(g4Traj);
180  if (!traj) {
181  EDepSimError("Not a EDepSim::Trajectory " << trackId);
182  break;
183  }
184  traj->AddSDDaughterEnergyDeposit(energy);
185  if (loopCount>9999) {
186  EDepSimError("Infinite loop for trajectory id " << trackId);
187  EDepSimThrow("Infinite loop trap");
188  }
189  }
190  }
191  }
192 }
#define EDepSimNamedInfo(trace, outStream)
Definition: EDepSimLog.hh:770
double GetTrackLength(void) const
void EndOfEventAction(const G4Event *)
#define EDepSimNamedVerbose(trace, outStream)
Definition: EDepSimLog.hh:805
const G4String & GetReaction() const
Get the reaction code that created this vertex.
#define EDepSimThrow(message)
Print an error message, and then throw an exception.
#define EDepSimNamedLog(trace, outStream)
Definition: EDepSimLog.hh:735
#define EDepSimInfo(outStream)
Definition: EDepSimLog.hh:752
string dir
void AddSDLength(double len)
Add the length that has been deposited into a sensitive detector.
p
Definition: test.py:223
void BeginOfEventAction(const G4Event *)
virtual int GetNumberOfInformationalVertex() const
Return the number of informational vertices.
void AddSDEnergyDeposit(double energy)
Add energy deposited into a sensitive detector.
#define EDepSimError(outStream)
Definition: EDepSimLog.hh:503
#define EDepSimWarn(outStream)
Definition: EDepSimLog.hh:576
virtual const G4PrimaryVertex * GetInformationalVertex(int i=0) const
int GetNodeId(const G4ThreeVector &pos)
Get a volume ID base on the volume position.
G4String GetName() const
std::vector< int > & GetContributors()
Provide public access to the contributors for internal G4 classes.
TCEvent evt
Definition: DataStructs.cxx:7
static G4VTrajectory * Get(int trackId)
Provide a map between the track id and the trajectory object.
double GetEnergyDeposit(void) const
Get the total energy deposited in this hit.
G4int GetParentID() const
static EDepSim::RootGeometryManager * Get(void)
If a persistency manager has not been created, create one.
void AddSDDaughterEnergyDeposit(double energy)
Add energy deposited into a sensitive detector by a daughter.