EDepSimTrajectory.cc
Go to the documentation of this file.
1 #include <string>
2 #include <sstream>
3 #include <cmath>
4 
5 #include <G4VProcess.hh>
6 #include <G4ParticleTable.hh>
7 #include <G4AttDefStore.hh>
8 #include <G4AttDef.hh>
9 #include <G4AttValue.hh>
10 #include <G4UnitsTable.hh>
11 
12 #include "EDepSimTrajectory.hh"
14 #include "EDepSimTrajectoryMap.hh"
15 
16 G4Allocator<EDepSim::Trajectory> aTrajAllocator;
17 
19  : fPositionRecord(0), fTrackID(0), fParentID(0),
20  fPDGEncoding(0), fPDGCharge(0.0), fParticleName(""),
21  fProcessName(""), fInitialMomentum(G4ThreeVector()),
22  fSDEnergyDeposit(0), fSDTotalEnergyDeposit(0), fSDLength(0),
23  fSaveTrajectory(false) {;}
24 
25 EDepSim::Trajectory::Trajectory(const G4Track* aTrack) {
27  // Following is for the first trajectory point
28  fPositionRecord->push_back(new EDepSim::TrajectoryPoint(aTrack));
29  fTrackID = aTrack->GetTrackID();
30  fParentID = aTrack->GetParentID();
31  G4ParticleDefinition * fpParticleDefinition = aTrack->GetDefinition();
32  fPDGEncoding = fpParticleDefinition->GetPDGEncoding();
33  fPDGCharge = fpParticleDefinition->GetPDGCharge();
34  fParticleName = fpParticleDefinition->GetParticleName();
35  const G4VProcess* proc = aTrack->GetCreatorProcess();
36  if (proc) fProcessName = proc->GetProcessName();
37  else fProcessName = "primary";
38  fInitialMomentum = aTrack->GetMomentum();
39  fSDEnergyDeposit = 0.0;
41  fSDLength = 0.0;
42  fSaveTrajectory = false;
43 }
44 
46  fTrackID = right.fTrackID;
47  fParentID = right.fParentID;
48  fPDGEncoding = right.fPDGEncoding;
49  fPDGCharge = right.fPDGCharge;
51  fProcessName = right.fProcessName;
55  fSDLength = right.fSDLength;
57 
59  for(size_t i=0;i<right.fPositionRecord->size();++i) {
60  EDepSim::TrajectoryPoint* rightPoint
61  = (EDepSim::TrajectoryPoint*)((*(right.fPositionRecord))[i]);
62  fPositionRecord->push_back(new EDepSim::TrajectoryPoint(*rightPoint));
63  }
64 }
65 
67  for(size_t i=0;i<fPositionRecord->size();++i){
68  delete (*fPositionRecord)[i];
69  }
70  fPositionRecord->clear();
71 
72  delete fPositionRecord;
73 }
74 
76  const G4ParticleDefinition* p = GetParticleDefinition();
77  double mom = GetInitialMomentum().mag();
78  if (!p) return mom;
79  double mass = p->GetPDGMass();
80  double kin = std::sqrt(mom*mom + mass*mass) - mass;
81  if (kin<0.0) return 0.0;
82  return kin;
83 }
84 
86  if (GetPointEntries()<2) return 0.0;
87  G4ThreeVector first
88  = dynamic_cast<EDepSim::TrajectoryPoint*>(GetPoint(0))->GetPosition();
89  G4ThreeVector last
91  ->GetPosition();
92  return (last - first).mag();
93 }
94 
96  fSaveTrajectory = true;
97  if (!save) return;
98  // Mark all parents to be saved as well.
99  G4VTrajectory* g4Traj = EDepSim::TrajectoryMap::Get(fParentID);
100  if (!g4Traj) return;
101  EDepSim::Trajectory* traj = dynamic_cast<EDepSim::Trajectory*>(g4Traj);
102  if (!traj) return;
103  // Protect against infinite loops.
104  if (this == traj) return;
105  traj->MarkTrajectory(save);
106 }
107 
108 const std::map<G4String,G4AttDef>* EDepSim::Trajectory::GetAttDefs() const {
109  G4bool isNew;
110  std::map<G4String,G4AttDef>* store
111  = G4AttDefStore::GetInstance("EDepSim::Trajectory",isNew);
112 
113  if (isNew) {
114  G4String ID("ID");
115  (*store)[ID] = G4AttDef(ID,
116  "Track ID",
117  "Bookkeeping","","G4int");
118 
119  G4String PID("PID");
120  (*store)[PID] = G4AttDef(PID,
121  "Parent ID",
122  "Bookkeeping","","G4int");
123 
124  G4String PN("PN");
125  (*store)[PN] = G4AttDef(PN,
126  "Particle Name",
127  "Physics","","G4String");
128 
129  G4String Ch("Ch");
130  (*store)[Ch] = G4AttDef(Ch,
131  "Charge",
132  "Physics","","G4double");
133 
134  G4String PDG("PDG");
135  (*store)[PDG] = G4AttDef(PDG,
136  "PDG Encoding",
137  "Physics","","G4int");
138 
139  G4String IMom("IMom");
140  (*store)[IMom] = G4AttDef(IMom,
141  "Momentum of track at start of trajectory",
142  "Physics","","G4ThreeVector");
143 
144  G4String NTP("NTP");
145  (*store)[NTP] = G4AttDef(NTP,
146  "No. of points",
147  "Physics","","G4int");
148 
149  }
150  return store;
151 }
152 
153 std::vector<G4AttValue>* EDepSim::Trajectory::CreateAttValues() const {
154  std::string c;
155  std::ostringstream s(c);
156 
157  std::vector<G4AttValue>* values = new std::vector<G4AttValue>;
158 
159  s.seekp(std::ios::beg);
160  s << fTrackID << std::ends;
161  values->push_back(G4AttValue("ID",c.c_str(),""));
162 
163  s.seekp(std::ios::beg);
164  s << fParentID << std::ends;
165  values->push_back(G4AttValue("PID",c.c_str(),""));
166 
167  values->push_back(G4AttValue("PN",fParticleName,""));
168 
169  s.seekp(std::ios::beg);
170  s << fPDGCharge << std::ends;
171  values->push_back(G4AttValue("Ch",c.c_str(),""));
172 
173  s.seekp(std::ios::beg);
174  s << fPDGEncoding << std::ends;
175  values->push_back(G4AttValue("PDG",c.c_str(),""));
176 
177  s.seekp(std::ios::beg);
178  s << G4BestUnit(fInitialMomentum,"Energy") << std::ends;
179  values->push_back(G4AttValue("IMom",c.c_str(),""));
180 
181  s.seekp(std::ios::beg);
182  s << GetPointEntries() << std::ends;
183  values->push_back(G4AttValue("NTP",c.c_str(),""));
184 
185  return values;
186 }
187 
188 void EDepSim::Trajectory::AppendStep(const G4Step* aStep) {
190  fPositionRecord->push_back(point);
191 }
192 
193 G4ParticleDefinition* EDepSim::Trajectory::GetParticleDefinition() const {
194  return (G4ParticleTable::GetParticleTable()->FindParticle(fParticleName));
195 }
196 
197 void EDepSim::Trajectory::MergeTrajectory(G4VTrajectory* secondTrajectory) {
198  if(!secondTrajectory) return;
199  EDepSim::Trajectory* second = (EDepSim::Trajectory*)secondTrajectory;
200  G4int ent = second->GetPointEntries();
201  // initial point of the second trajectory should not be merged
202  for(G4int i=1; i<ent; ++i) {
203  fPositionRecord->push_back((*(second->fPositionRecord))[i]);
204  }
205  delete (*second->fPositionRecord)[0];
206  second->fPositionRecord->clear();
207 }
208 
209 
G4double GetInitialKineticEnergy() const
G4ParticleDefinition * GetParticleDefinition() const
Get the full definition of the particle.
TrajectoryPointContainer * fPositionRecord
std::string string
Definition: nybbler.cc:12
unsigned int ID
G4double GetRange() const
Get the range of the particle that created this trajectory.
virtual void AppendStep(const G4Step *aStep)
G4ThreeVector GetInitialMomentum() const
Get the initial momentum of the particle that created this trajectory.
G4Allocator< EDepSim::Trajectory > aTrajAllocator
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)
G4ThreeVector fInitialMomentum
virtual G4VTrajectoryPoint * GetPoint(G4int i) const
Get a particular trajectory point.
const uint PDG
Definition: qregexp.cpp:140
void MarkTrajectory(bool save=true)
Mark this trajectory as one that should be saved in the output.
p
Definition: test.py:223
def save(obj, fname)
Definition: root.py:19
Q_UINT16 values[128]
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
std::vector< G4VTrajectoryPoint * > TrajectoryPointContainer
virtual std::vector< G4AttValue > * CreateAttValues() const
virtual int GetPointEntries() const
Get the number of trajectory points saved with this trajectory.
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
static G4VTrajectory * Get(int trackId)
Provide a map between the track id and the trajectory object.
static QCString * s
Definition: config.cpp:1042