CMS 3D CMS Logo

BHMSD.cc
Go to the documentation of this file.
4 
8 
12 
14 
15 #include "G4Track.hh"
16 #include "G4SDManager.hh"
17 #include "G4VProcess.hh"
18 #include "G4EventManager.hh"
19 #include "G4Step.hh"
20 #include "G4ParticleTable.hh"
21 
22 #include <string>
23 #include <vector>
24 #include <iostream>
25 
27 
28 //#define debug
29 //-------------------------------------------------------------------
31  const SensitiveDetectorCatalog & clg,
32  edm::ParameterSet const & p, const SimTrackManager* manager) :
33  SensitiveTkDetector(name, cpv, clg, p), numberingScheme(nullptr),
34  hcID(-1), theHC(nullptr), theManager(manager), currentHit(nullptr), theTrack(nullptr),
35  currentPV(nullptr), unitID(0), previousUnitID(0), preStepPoint(nullptr),
36  postStepPoint(nullptr), eventno(0){
37 
38  //Parameters
40  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
41  //int verbn = 1;
42 
43  SetVerboseLevel(verbn);
44 
45  slave = new TrackingSlaveSD(name);
46 
47  if (verbn > 0) {
48  edm::LogInfo("BHMSim") << "name = " <<name <<" and new BHMNumberingScheme";
49  }
51 }
52 
54 
55  if (slave) delete slave;
56  if (numberingScheme) delete numberingScheme;
57 }
58 
59 double BHMSD::getEnergyDeposit(const G4Step* aStep) {
60  return aStep->GetTotalEnergyDeposit();
61 }
62 
63 void BHMSD::Initialize(G4HCofThisEvent * HCE) {
64 #ifdef debug
65  LogDebug("BHMSim") << "BHMSD : Initialize called for " << GetName();
66 #endif
67 
68  theHC = new BscG4HitCollection(GetName(), collectionName[0]);
69  if (hcID<0)
70  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
71  HCE->AddHitsCollection(hcID, theHC);
72 
73  tsID = -2;
74  primID = -2;
75 }
76 
77 
78 bool BHMSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
79 
80  if (aStep == nullptr) {
81  return true;
82  } else {
83  GetStepInfo(aStep);
84 #ifdef debug
85  LogDebug("BHMSim") << "BHMSD : number of hits = " << theHC->entries() << std::endl;
86 #endif
87  if (HitExists() == false && edeposit>0. ){
88  CreateNewHit();
89  return true;
90  }
91  }
92  return true;
93 }
94 
95 void BHMSD::GetStepInfo(G4Step* aStep) {
96 
97  preStepPoint = aStep->GetPreStepPoint();
98  postStepPoint= aStep->GetPostStepPoint();
99  theTrack = aStep->GetTrack();
100  hitPoint = preStepPoint->GetPosition();
101  currentPV = preStepPoint->GetPhysicalVolume();
102  hitPointExit = postStepPoint->GetPosition();
103 
104  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
105  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
106 
107 
108  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
109  LogDebug("BHMSim") << " BHMSD :particleType = " << theTrack->GetDefinition()->GetParticleName() <<std::endl;
110  if (particleCode == emPDG ||
111  particleCode == epPDG ||
112  particleCode == gammaPDG ) {
113  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
114  } else {
115  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
116  }
117  edeposit = aStep->GetTotalEnergyDeposit();
118  tSlice = (postStepPoint->GetGlobalTime() )/CLHEP::nanosecond;
119  tSliceID = (int) tSlice;
120  unitID = setDetUnitId(aStep);
121 #ifdef debug
122  LogDebug("BHMSim") << "unitID=" << unitID <<std::endl;
123 #endif
124  primaryID = theTrack->GetTrackID();
125  // Position = hitPoint;
126  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/CLHEP::GeV;
127  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/CLHEP::nanosecond;
128  Eloss = aStep->GetTotalEnergyDeposit()/CLHEP::GeV;
129  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
130  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/CLHEP::deg;
131  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/CLHEP::deg;
132 
133  ParentId = theTrack->GetParentID();
134  Vx = theTrack->GetVertexPosition().x();
135  Vy = theTrack->GetVertexPosition().y();
136  Vz = theTrack->GetVertexPosition().z();
137  X = hitPoint.x();
138  Y = hitPoint.y();
139  Z = hitPoint.z();
140 }
141 
142 uint32_t BHMSD::setDetUnitId(const G4Step * aStep) {
143  return (numberingScheme == nullptr ? 0 : numberingScheme->getUnitID(aStep));
144 }
145 
147  if (primaryID<1) {
148  edm::LogWarning("BHMSim") << "***** BHMSD error: primaryID = "
149  << primaryID
150  << " maybe detector name changed";
151  }
152 
153  // Update if in the same detector, time-slice and for same track
154  if (tSliceID == tsID && unitID==previousUnitID) {
155  UpdateHit();
156  return true;
157  }
158  // Reset entry point for new primary
159  if (primaryID != primID)
161 
162  //look in the HitContainer whether a hit with the same primID, unitID,
163  //tSliceID already exists:
164 
165  G4bool found = false;
166 
167  for (int j=0; j<theHC->entries()&&!found; j++) {
168  BscG4Hit* aPreviousHit = (*theHC)[j];
169  if (aPreviousHit->getTrackID() == primaryID &&
170  aPreviousHit->getTimeSliceID() == tSliceID &&
171  aPreviousHit->getUnitID() == unitID ) {
172  currentHit = aPreviousHit;
173  found = true;
174  }
175  }
176 
177  if (found) {
178  UpdateHit();
179  return true;
180  } else {
181  return false;
182  }
183 }
184 
186 
189  incidentEnergy = preStepPoint->GetKineticEnergy();
190 }
191 
193 
194  if (primID<0) return;
195  if (hit == nullptr ) {
196  edm::LogWarning("BHMSim") << "BHMSD: hit to be stored is NULL !!";
197  return;
198  }
199 
200  theHC->insert( hit );
201 }
202 
204 
205 #ifdef debug
206  LogDebug("BHMSim") << "BHMSD CreateNewHit for"
207  << " PV " << currentPV->GetName()
208  << " PVid = " << currentPV->GetCopyNo()
209  << " Unit " << unitID <<std::endl;
210  LogDebug("BHMSim") << " primary " << primaryID
211  << " time slice " << tSliceID
212  << " For Track " << theTrack->GetTrackID()
213  << " which is a " << theTrack->GetDefinition()->GetParticleName();
214 
215  if (theTrack->GetTrackID()==1) {
216  LogDebug("BHMSim") << " of energy " << theTrack->GetTotalEnergy();
217  } else {
218  LogDebug("BHMSim") << " daughter of part. " << theTrack->GetParentID();
219  }
220 
221  LogDebug("BHMSim") << " and created by " ;
222  if (theTrack->GetCreatorProcess()!=nullptr)
223  LogDebug("BHMSim") << theTrack->GetCreatorProcess()->GetProcessName() ;
224  else
225  LogDebug("BHMSim") << "NO process";
226  LogDebug("BHMSim") << std::endl;
227 #endif
228 
229  currentHit = new BscG4Hit;
234 
241 
243 
246 
248  currentHit->setVx(Vx);
249  currentHit->setVy(Vy);
250  currentHit->setVz(Vz);
251 
252  currentHit->setX(X);
253  currentHit->setY(Y);
254  currentHit->setZ(Z);
255 
256  UpdateHit();
257 
259 }
260 
262 
263  if (Eloss > 0.) {
265 
266 #ifdef debug
267  LogDebug("BHMSim") << "updateHit: add eloss " << Eloss <<std::endl;
268  LogDebug("BHMSim") << "CurrentHit=" << currentHit
269  << ", PostStepPoint=" << postStepPoint->GetPosition();
270 #endif
272  }
273 
274  // buffer for next steps:
275  tsID = tSliceID;
276  primID = primaryID;
278 }
279 
280 G4ThreeVector BHMSD::SetToLocal(const G4ThreeVector& global) {
281 
282  const G4VTouchable* touch= preStepPoint->GetTouchable();
283  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
284  return theEntryPoint;
285 }
286 
287 G4ThreeVector BHMSD::SetToLocalExit(const G4ThreeVector& globalPoint) {
288 
289  const G4VTouchable* touch= postStepPoint->GetTouchable();
290  theExitPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
291  return theExitPoint;
292 }
293 
294 void BHMSD::EndOfEvent(G4HCofThisEvent* ) {
295 
296  // here we loop over transient hits and make them persistent
297  for (int j=0; j<theHC->entries(); j++) {
298  BscG4Hit* aHit = (*theHC)[j];
299  LogDebug("BHMSim") << "hit number" << j << "unit ID = "<<aHit->getUnitID()<< "\n";
300  LogDebug("BHMSim") << "entry z " << aHit->getEntry().z()<< "\n";
301  LogDebug("BHMSim") << "entr theta " << aHit->getThetaAtEntry()<< "\n";
302 
303  Local3DPoint locExitPoint(0,0,0);
304  Local3DPoint locEntryPoint(aHit->getEntry().x(),
305  aHit->getEntry().y(),
306  aHit->getEntry().z());
307  slave->processHits(PSimHit(locEntryPoint,locExitPoint,
308  aHit->getPabs(),
309  aHit->getTof(),
310  aHit->getEnergyLoss(),
311  aHit->getParticleType(),
312  aHit->getUnitID(),
313  aHit->getTrackID(),
314  aHit->getThetaAtEntry(),
315  aHit->getPhiAtEntry()));
316  }
317  Summarize();
318 }
319 
321 }
322 
323 void BHMSD::clear() {
324 }
325 
327 }
328 
330  LogDebug("BHMSim") << "BHMSD: Collection " << theHC->GetName() << "\n";
331  theHC->PrintAllHits();
332 }
333 
335  if (slave->name() == hname) { cc=slave->hits(); }
336 }
337 
338 void BHMSD::update (const BeginOfEvent * i) {
339  LogDebug("BHMSim") << " Dispatched BeginOfEvent for " << GetName()
340  << " !" ;
341  clearHits();
342  eventno = (*i)()->GetEventID();
343 }
344 
345 void BHMSD::update(const BeginOfRun *) {
346 
347  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
348  G4String particleName;
349  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
350  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
351  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
352 
353 }
354 
355 void BHMSD::update (const ::EndOfEvent*) {
356 }
357 
359  slave->Initialize();
360 }
#define LogDebug(id)
float Z
Definition: BHMSD.h:115
void setTof(float e)
Definition: BSCG4Hit.cc:154
float incidentEnergy
Definition: BHMSD.h:85
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
G4double tSlice
Definition: BHMSD.h:98
G4ThreeVector hitPointExit
Definition: BHMSD.h:105
void setEntry(const G4ThreeVector &xyz)
Definition: BSCG4Hit.cc:116
const double GeV
Definition: MathUtil.h:16
void clear() override
Definition: BHMSD.cc:323
void ResetForNewPrimary()
Definition: BHMSD.cc:185
float getPhiAtEntry() const
Definition: BSCG4Hit.cc:159
float PhiAtEntry
Definition: BHMSD.h:111
void setVx(float p)
Definition: BSCG4Hit.cc:177
BHMSD(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: BHMSD.cc:30
int getParticleType() const
Definition: BSCG4Hit.cc:151
G4int getTrackID() const
Definition: BSCG4Hit.cc:133
void Initialize(G4HCofThisEvent *HCE) override
Definition: BHMSD.cc:63
float Eloss
Definition: BHMSD.h:109
G4ThreeVector theEntryPoint
Definition: BHMSD.h:83
std::string name() const
void setTrackID(int i)
Definition: BSCG4Hit.cc:134
void setEnergyLoss(float e)
Definition: BSCG4Hit.cc:155
void setParentId(int p)
Definition: BSCG4Hit.cc:174
BHMNumberingScheme * numberingScheme
Definition: BHMSD.h:80
G4StepPoint * preStepPoint
Definition: BHMSD.h:100
G4ThreeVector hitPoint
Definition: BHMSD.h:104
void DrawAll() override
Definition: BHMSD.cc:326
void setUnitID(unsigned int i)
Definition: BSCG4Hit.cc:137
float getTof() const
Definition: BSCG4Hit.cc:149
G4int tSliceID
Definition: BHMSD.h:97
float getPabs() const
Definition: BSCG4Hit.cc:148
int eventno
Definition: BHMSD.h:117
void CreateNewHit()
Definition: BHMSD.cc:203
G4ThreeVector hitPointLocal
Definition: BHMSD.h:106
void setVy(float p)
Definition: BSCG4Hit.cc:180
#define nullptr
void setY(float t)
Definition: BSCG4Hit.cc:168
type of data representation of DDCompactView
Definition: DDCompactView.h:90
unsigned int getUnitID() const
Definition: BSCG4Hit.cc:136
static TrackerG4SimHitNumberingScheme & numberingScheme(const DDCompactView &cpv, const GeometricDet &det)
float X
Definition: BHMSD.h:115
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: BHMSD.cc:294
int getTimeSliceID() const
Definition: BSCG4Hit.cc:141
float edepositEM
Definition: BHMSD.h:121
TrackingSlaveSD * slave
Definition: BHMSD.h:79
float edepositHAD
Definition: BHMSD.h:121
void setThetaAtEntry(float t)
Definition: BSCG4Hit.cc:161
void setX(float t)
Definition: BSCG4Hit.cc:165
void setPabs(float e)
Definition: BSCG4Hit.cc:153
void setEntryLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:119
std::vector< PSimHit > & hits()
float Vx
Definition: BHMSD.h:114
float Vz
Definition: BHMSD.h:114
std::string const collectionName[nCollections]
Definition: Collections.h:47
G4ThreeVector SetToLocalExit(const G4ThreeVector &globalPoint)
Definition: BHMSD.cc:287
void Summarize()
Definition: BHMSD.cc:320
float Y
Definition: BHMSD.h:115
virtual void Initialize()
uint32_t previousUnitID
Definition: BHMSD.h:96
void setPhiAtEntry(float f)
Definition: BSCG4Hit.cc:162
G4int tsID
Definition: BHMSD.h:92
void PrintAll() override
Definition: BHMSD.cc:329
uint32_t unitID
Definition: BHMSD.h:96
float Vy
Definition: BHMSD.h:114
float Tof
Definition: BHMSD.h:109
void update(const BeginOfRun *) override
This routine will be called when the appropriate signal arrives.
Definition: BHMSD.cc:345
G4ThreeVector theExitPoint
Definition: BHMSD.h:83
virtual unsigned int getUnitID(const G4Step *aStep) const
float getThetaAtEntry() const
Definition: BSCG4Hit.cc:158
void addEnergyDeposit(double em, double hd)
Definition: BSCG4Hit.cc:143
G4Track * theTrack
Definition: BHMSD.h:94
void setTimeSlice(double d)
Definition: BSCG4Hit.cc:140
G4ThreeVector entrancePoint
Definition: BHMSD.h:82
void setParticleType(short i)
Definition: BSCG4Hit.cc:156
void setVz(float p)
Definition: BSCG4Hit.cc:183
void clearHits() override
Definition: BHMSD.cc:358
float edeposit
Definition: BHMSD.h:102
G4StepPoint * postStepPoint
Definition: BHMSD.h:101
short ParticleType
Definition: BHMSD.h:110
G4int primaryID
Definition: BHMSD.h:97
G4int primID
Definition: BHMSD.h:86
BscG4Hit * currentHit
Definition: BHMSD.h:93
BscG4HitCollection * theHC
Definition: BHMSD.h:89
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: BHMSD.cc:334
~BHMSD() override
Definition: BHMSD.cc:53
G4int gammaPDG
Definition: BHMSD.h:124
int ParentId
Definition: BHMSD.h:113
void UpdateHit()
Definition: BHMSD.cc:261
G4ThreeVector SetToLocal(const G4ThreeVector &global)
Definition: BHMSD.cc:280
float ThetaAtEntry
Definition: BHMSD.h:111
G4ThreeVector getEntry() const
Definition: BSCG4Hit.cc:115
void GetStepInfo(G4Step *aStep)
Definition: BHMSD.cc:95
void setZ(float t)
Definition: BSCG4Hit.cc:171
uint32_t setDetUnitId(const G4Step *) override
Definition: BHMSD.cc:142
double getEnergyDeposit(const G4Step *step)
Definition: BHMSD.cc:59
virtual bool processHits(const PSimHit &)
G4ThreeVector hitPointLocalExit
Definition: BHMSD.h:107
std::vector< PSimHit > PSimHitContainer
G4VPhysicalVolume * currentPV
Definition: BHMSD.h:95
void setExitLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:122
G4int hcID
Definition: BHMSD.h:88
G4THitsCollection< BscG4Hit > BscG4HitCollection
float Pabs
Definition: BHMSD.h:109
void setIncidentEnergy(double e)
Definition: BSCG4Hit.cc:131
G4int epPDG
Definition: BHMSD.h:123
G4bool HitExists()
Definition: BHMSD.cc:146
void StoreHit(BscG4Hit *)
Definition: BHMSD.cc:192
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: BHMSD.cc:78
float getEnergyLoss() const
Definition: BSCG4Hit.cc:150
G4int emPDG
Definition: BHMSD.h:122
G4ThreeVector exitPoint
Definition: BHMSD.h:82