CMS 3D CMS Logo

TimingSD.cc
Go to the documentation of this file.
1 //#define EDM_ML_DEBUG
2 
4 // File: TimingSD.cc
5 // Date: 02.2006
6 // Description: Sensitive Detector class for Timing
7 // Modifications:
9 
11 
15 
18 
19 #include "G4Step.hh"
20 #include "G4StepPoint.hh"
21 #include "G4Track.hh"
22 #include "G4VPhysicalVolume.hh"
23 #include "G4SDManager.hh"
24 #include "G4VProcess.hh"
25 
26 #include "G4PhysicalConstants.hh"
27 #include "G4SystemOfUnits.hh"
28 
29 #include <vector>
30 #include <iostream>
31 
32 static const float invgev = 1.0 / CLHEP::GeV;
33 static const double invns = 1.0 / CLHEP::nanosecond;
34 static const double invdeg = 1.0 / CLHEP::deg;
35 
36 //-------------------------------------------------------------------
38  : SensitiveTkDetector(name, clg),
39  theManager(manager),
40  theHC(nullptr),
41  currentHit(nullptr),
42  theTrack(nullptr),
43  preStepPoint(nullptr),
44  postStepPoint(nullptr),
45  unitID(0),
46  previousUnitID(0),
47  primID(-2),
48  hcID(-1),
49  tsID(-2),
50  primaryID(0),
51  tSliceID(-1),
52  timeFactor(1.0),
53  energyCut(1.e+9),
54  energyHistoryCut(1.e+9) {
55  slave = new TrackingSlaveSD(name);
57 }
58 
60  delete slave;
61  delete theEnumerator;
62 }
63 
64 void TimingSD::Initialize(G4HCofThisEvent* HCE) {
65  edm::LogVerbatim("TimingSim") << "TimingSD : Initialize called for " << GetName() << " time slice factor "
66  << timeFactor << "\n MC truth cuts in are " << energyCut / CLHEP::GeV << " GeV and "
67  << energyHistoryCut / CLHEP::GeV << " GeV";
68 
69  theHC = new BscG4HitCollection(GetName(), collectionName[0]);
70  if (hcID < 0) {
71  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
72  }
73  HCE->AddHitsCollection(hcID, theHC);
74 
75  tsID = -2;
76  primID = -2;
77 }
78 
80  if (val <= 0.0) {
81  return;
82  }
83  timeFactor = val;
84 #ifdef EDM_ML_DEBUG
85  edm::LogVerbatim("TimingSim") << "TimingSD : for " << GetName() << " time slice factor is set to " << timeFactor;
86 #endif
87 }
88 
89 void TimingSD::setCuts(double eCut, double historyCut) {
90  if (eCut > 0.) {
91  energyCut = eCut;
92  }
93  if (historyCut > 0.) {
94  energyHistoryCut = historyCut;
95  }
96 #ifdef EDM_ML_DEBUG
97  edm::LogVerbatim("TimingSim") << "TimingSD : for " << GetName() << " MC truth cuts in are " << energyCut / CLHEP::GeV
98  << " GeV and " << energyHistoryCut / CLHEP::GeV << " GeV";
99 #endif
100 }
101 
102 bool TimingSD::ProcessHits(G4Step* aStep, G4TouchableHistory*) {
103  edeposit = aStep->GetTotalEnergyDeposit();
104  if (edeposit > 0.f) {
105  getStepInfo(aStep);
106  if (!hitExists(aStep)) {
107  createNewHit(aStep);
108  }
109  }
110  return true;
111 }
112 
113 void TimingSD::getStepInfo(const G4Step* aStep) {
114  preStepPoint = aStep->GetPreStepPoint();
115  postStepPoint = aStep->GetPostStepPoint();
116  hitPointExit = postStepPoint->GetPosition();
118  const G4Track* newTrack = aStep->GetTrack();
119 
120  // neutral particles deliver energy post step
121  // charged particle start deliver energy pre step
122  if (newTrack->GetDefinition()->GetPDGCharge() == 0.0) {
125  tof = (float)(postStepPoint->GetGlobalTime() * invns);
126  } else {
127  hitPoint = preStepPoint->GetPosition();
129  tof = (float)(preStepPoint->GetGlobalTime() * invns);
130  }
131 
132 #ifdef EDM_ML_DEBUG
133  double distGlobal =
134  std::sqrt(std::pow(hitPoint.x() - hitPointExit.x(), 2) + std::pow(hitPoint.y() - hitPointExit.y(), 2) +
135  std::pow(hitPoint.z() - hitPointExit.z(), 2));
136  double distLocal = std::sqrt(std::pow(hitPointLocal.x() - hitPointLocalExit.x(), 2) +
139  LogDebug("TimingSim") << "TimingSD:"
140  << "\n Global entry point: " << hitPoint << "\n Global exit point: " << hitPointExit
141  << "\n Global step length: " << distGlobal << "\n Local entry point: " << hitPointLocal
142  << "\n Local exit point: " << hitPointLocalExit << "\n Local step length: " << distLocal;
143  if (std::fabs(distGlobal - distLocal) > 1.e-6) {
144  LogDebug("TimingSim") << "DIFFERENCE IN DISTANCE \n";
145  }
146 #endif
147 
148  incidentEnergy = preStepPoint->GetKineticEnergy();
149 
150  // should MC truth be saved
151  if (newTrack != theTrack) {
152  theTrack = newTrack;
153  TrackInformation* info = nullptr;
154  if (incidentEnergy > energyCut) {
156  info->setStoreTrack();
157  }
159  if (nullptr == info) {
161  }
162  info->putInHistory();
163  }
164 #ifdef EDM_ML_DEBUG
165  if (info != nullptr) {
166  LogDebug("TimingSim") << "TrackInformation for ID = " << theTrack->GetTrackID();
167  info->Print();
168  }
169 #endif
170  }
171 
172  edeposit *= invgev;
175  edepositHAD = 0.f;
176  } else {
177  edepositEM = 0.f;
179  }
180  // time slice is defined for the entry point
181  tSlice = timeFactor * preStepPoint->GetGlobalTime() * invns;
182  tSliceID = (int)tSlice;
183 
184  unitID = setDetUnitId(aStep);
186 }
187 
188 bool TimingSD::hitExists(const G4Step* aStep) {
189  if (!currentHit) {
190  return false;
191  }
192 
193  // Update if in the same detector and time-slice
194  if (tSliceID == tsID && unitID == previousUnitID) {
195  updateHit();
196  return true;
197  }
198 
199  //look in the HitContainer whether a hit with the same primID, unitID,
200  //tSliceID already exists:
201 
202  bool found = false;
203  int thehc_entries = theHC->entries();
204  for (int j = 0; j < thehc_entries; ++j) {
205  BscG4Hit* aHit = (*theHC)[j];
206  if (aHit->getTimeSliceID() == tSliceID && aHit->getUnitID() == unitID) {
207  if (checkHit(aStep, aHit)) {
208  currentHit = aHit;
209  found = true;
210  break;
211  }
212  }
213  }
214  if (found) {
215  updateHit();
216  }
217  return found;
218 }
219 
220 bool TimingSD::checkHit(const G4Step*, BscG4Hit* hit) {
221  // change hit info to fastest primary particle
222  if (tof < hit->getTof()) {
223  hit->setTrackID(primaryID);
224  hit->setIncidentEnergy((float)incidentEnergy);
225  hit->setPabs(float(preStepPoint->GetMomentum().mag()) * invgev);
226  hit->setTof(tof);
227  hit->setParticleType(theTrack->GetDefinition()->GetPDGEncoding());
228 
229  float ThetaAtEntry = (float)(hitPointLocal.theta() * invdeg);
230  float PhiAtEntry = (float)(hitPointLocal.phi() * invdeg);
231 
232  hit->setThetaAtEntry(ThetaAtEntry);
233  hit->setPhiAtEntry(PhiAtEntry);
234 
235  hit->setEntry(hitPoint);
236  hit->setEntryLocalP(hitPointLocal);
237  hit->setExitLocalP(hitPointLocalExit);
238 
239  hit->setParentId(theTrack->GetParentID());
240  hit->setProcessId(theEnumerator->processId(theTrack->GetCreatorProcess()));
241 
242  hit->setVertexPosition(theTrack->GetVertexPosition());
243  }
244  return true;
245 }
246 
248  if (primID < 0)
249  return;
250  if (hit == nullptr) {
251  edm::LogWarning("BscSim") << "BscSD: hit to be stored is NULL !!";
252  return;
253  }
254 
255  theHC->insert(hit);
256 }
257 
258 void TimingSD::createNewHit(const G4Step* aStep) {
259 #ifdef EDM_ML_DEBUG
260  const G4VPhysicalVolume* currentPV = preStepPoint->GetPhysicalVolume();
261  edm::LogVerbatim("TimingSim") << "TimingSD CreateNewHit for " << GetName() << " PV " << currentPV->GetName()
262  << " PVid = " << currentPV->GetCopyNo() << " Unit " << unitID << "\n primary "
263  << primaryID << " Tof(ns)= " << tof << " time slice " << tSliceID
264  << " E(MeV)= " << incidentEnergy << " trackID " << theTrack->GetTrackID() << " "
265  << theTrack->GetDefinition()->GetParticleName() << " parentID "
266  << theTrack->GetParentID();
267 
268  if (theTrack->GetCreatorProcess() != nullptr) {
269  edm::LogVerbatim("TimingSim") << theTrack->GetCreatorProcess()->GetProcessName();
270  } else {
271  edm::LogVerbatim("TimingSim") << " is primary particle";
272  }
273 #endif
274 
275  currentHit = new BscG4Hit;
280 
281  currentHit->setPabs(float(preStepPoint->GetMomentum().mag() * invgev));
283  currentHit->setParticleType(theTrack->GetDefinition()->GetPDGEncoding());
284 
285  float ThetaAtEntry = hitPointLocal.theta() * invdeg;
286  float PhiAtEntry = hitPointLocal.phi() * invdeg;
287 
288  currentHit->setThetaAtEntry(ThetaAtEntry);
289  currentHit->setPhiAtEntry(PhiAtEntry);
290 
294 
295  currentHit->setParentId(theTrack->GetParentID());
296  currentHit->setProcessId(theEnumerator->processId(theTrack->GetCreatorProcess()));
297 
298  currentHit->setVertexPosition(theTrack->GetVertexPosition());
299 
300  updateHit();
302 }
303 
306 
307 #ifdef EDM_ML_DEBUG
308  edm::LogVerbatim("TimingSim") << "updateHit: " << GetName() << " add eloss(GeV) " << edeposit
309  << "CurrentHit=" << currentHit << ", PostStepPoint= " << postStepPoint->GetPosition();
310 #endif
311 
312  // buffer for next steps:
313  tsID = tSliceID;
314  primID = primaryID;
316 }
317 
318 void TimingSD::setToLocal(const G4StepPoint* stepPoint, const G4ThreeVector& globalPoint, G4ThreeVector& localPoint) {
319  const G4VTouchable* touch = stepPoint->GetTouchable();
320  localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
321 }
322 
323 void TimingSD::EndOfEvent(G4HCofThisEvent*) {
324  int nhits = theHC->entries();
325  if (0 == nhits) {
326  return;
327  }
328  // here we loop over transient hits and make them persistent
329  for (int j = 0; j < nhits; ++j) {
330  BscG4Hit* aHit = (*theHC)[j];
331  Local3DPoint locEntryPoint = ConvertToLocal3DPoint(aHit->getEntryLocalP());
332  Local3DPoint locExitPoint = ConvertToLocal3DPoint(aHit->getExitLocalP());
333 
334 #ifdef EDM_ML_DEBUG
335  edm::LogVerbatim("TimingSim") << "TimingSD: Hit for storage \n"
336  << *aHit << "\n Entry point: " << locEntryPoint << "\n Exit point: " << locExitPoint;
337 #endif
338 
339  slave->processHits(PSimHit(locEntryPoint,
340  locExitPoint,
341  aHit->getPabs(),
342  aHit->getTof(),
343  aHit->getEnergyLoss(),
344  aHit->getParticleType(),
345  aHit->getUnitID(),
346  aHit->getTrackID(),
347  aHit->getThetaAtEntry(),
348  aHit->getPhiAtEntry(),
349  aHit->getProcessId()));
350  }
351 }
352 
354 #ifdef EDM_ML_DEBUG
355  edm::LogVerbatim("TimingSim") << "TimingSD: Collection " << theHC->GetName();
356 #endif
357  theHC->PrintAllHits();
358 }
359 
361  if (slave->name() == hname) {
362  cc = slave->hits();
363  }
364 }
365 
367 #ifdef EDM_ML_DEBUG
368  edm::LogVerbatim("TimingSim") << " Dispatched BeginOfEvent for " << GetName();
369 #endif
370  clearHits();
371 }
372 
374 
375 int TimingSD::getTrackID(const G4Track* aTrack) {
376  LogDebug("TimingSim") << "primary ID: " << aTrack->GetTrackID();
377  return aTrack->GetTrackID();
378 }
uint32_t previousUnitID
Definition: TimingSD.h:86
Log< level::Info, true > LogVerbatim
void setTof(float e)
Definition: BscG4Hit.h:73
void setEntryLocalP(const G4ThreeVector &xyz)
Definition: BscG4Hit.h:33
std::string name() const
static const TGPicture * info(bool iBackgroundIsBlack)
void setEntry(const G4ThreeVector &xyz)
Definition: BSCG4Hit.cc:98
void setTimeFactor(double)
Definition: TimingSD.cc:79
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: TimingSD.cc:323
void clearHits() override
Definition: TimingSD.cc:373
double timeFactor
Definition: TimingSD.h:100
void setIncidentEnergy(float e)
Definition: BscG4Hit.h:51
int hcID
Definition: TimingSD.h:89
float edepositHAD
Definition: TimingSD.h:108
uint32_t getUnitID() const
Definition: BscG4Hit.h:56
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
G4ThreeVector hitPointLocal
Definition: TimingSD.h:96
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: TimingSD.cc:360
int getParticleType() const
Definition: BscG4Hit.h:70
void setTrackID(int id)
Definition: BscG4Hit.h:54
void setParentId(int p)
Definition: BscG4Hit.h:99
int primID
Definition: TimingSD.h:88
virtual uint32_t setDetUnitId(const G4Step *step)=0
virtual bool checkHit(const G4Step *, BscG4Hit *)
Definition: TimingSD.cc:220
G4ThreeVector hitPointLocalExit
Definition: TimingSD.h:97
void createNewHit(const G4Step *)
Definition: TimingSD.cc:258
constexpr int pow(int x)
Definition: conifer.h:24
int getProcessId() const
Definition: BscG4Hit.h:94
BscG4Hit * currentHit
Definition: TimingSD.h:81
float getPhiAtEntry() const
Definition: BscG4Hit.h:78
float edepositEM
Definition: TimingSD.h:108
void setCuts(double eCut, double historyCut)
Definition: TimingSD.cc:89
void setParticleType(int i)
Definition: BscG4Hit.h:75
float getTof() const
Definition: BscG4Hit.h:68
void setToLocal(const G4StepPoint *stepPoint, const G4ThreeVector &globalPoint, G4ThreeVector &localPoint)
Definition: TimingSD.cc:318
void setThetaAtEntry(float t)
Definition: BscG4Hit.h:80
TimingSD(const std::string &, const SensitiveDetectorCatalog &, const SimTrackManager *)
Definition: TimingSD.cc:37
void storeHit(BscG4Hit *)
Definition: TimingSD.cc:247
float getPabs() const
Definition: BscG4Hit.h:67
void setPabs(float e)
Definition: BscG4Hit.h:72
std::vector< PSimHit > & hits()
void getStepInfo(const G4Step *)
Definition: TimingSD.cc:113
static const float invgev
Definition: TimingSD.cc:32
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
Definition: TimingSD.cc:366
void setUnitID(uint32_t id)
Definition: BscG4Hit.h:57
float tof
Definition: TimingSD.h:106
virtual void Initialize()
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: TimingSD.cc:102
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector &point) const
void setPhiAtEntry(float f)
Definition: BscG4Hit.h:81
int tsID
Definition: TimingSD.h:90
T sqrt(T t)
Definition: SSEVec.h:19
int getTrackID() const
Definition: BscG4Hit.h:53
float edeposit
Definition: TimingSD.h:107
TrackingSlaveSD * slave
Definition: TimingSD.h:75
G4ProcessTypeEnumerator * theEnumerator
Definition: TimingSD.h:76
static const double invdeg
Definition: TimingSD.cc:34
double f[11][100]
bool hitExists(const G4Step *)
Definition: TimingSD.cc:188
TrackInformation * cmsTrackInformation(const G4Track *aTrack)
const G4Track * theTrack
Definition: TimingSD.h:82
double energyHistoryCut
Definition: TimingSD.h:103
void PrintAll() override
Definition: TimingSD.cc:353
const G4StepPoint * preStepPoint
Definition: TimingSD.h:83
~TimingSD() override
Definition: TimingSD.cc:59
int tSliceID
Definition: TimingSD.h:92
void setTimeSlice(double d)
Definition: BscG4Hit.h:60
void addEnergyDeposit(float em, float hd)
Definition: BSCG4Hit.cc:113
double energyCut
Definition: TimingSD.h:102
float getThetaAtEntry() const
Definition: BscG4Hit.h:77
uint32_t unitID
Definition: TimingSD.h:86
void setExitLocalP(const G4ThreeVector &xyz)
Definition: BscG4Hit.h:36
double tSlice
Definition: TimingSD.h:99
void setVertexPosition(const G4ThreeVector &)
Definition: BSCG4Hit.cc:125
static const double invns
Definition: TimingSD.cc:33
void updateHit()
Definition: TimingSD.cc:304
float getEnergyLoss() const
Definition: BscG4Hit.h:69
int getTimeSliceID() const
Definition: BscG4Hit.h:61
const G4StepPoint * postStepPoint
Definition: TimingSD.h:84
BscG4HitCollection * theHC
Definition: TimingSD.h:79
static bool isGammaElectronPositron(int pdgCode)
const G4ThreeVector & getExitLocalP() const
Definition: BscG4Hit.h:35
virtual int getTrackID(const G4Track *)
Definition: TimingSD.cc:375
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
double incidentEnergy
Definition: TimingSD.h:105
unsigned int processId(const G4VProcess *p) const
G4THitsCollection< BscG4Hit > BscG4HitCollection
Log< level::Warning, false > LogWarning
void setProcessId(int p)
Definition: BscG4Hit.h:100
const G4ThreeVector & getEntryLocalP() const
Definition: BscG4Hit.h:32
void Initialize(G4HCofThisEvent *HCE) override
Definition: TimingSD.cc:64
G4ThreeVector hitPointExit
Definition: TimingSD.h:95
G4ThreeVector hitPoint
Definition: TimingSD.h:94
int primaryID
Definition: TimingSD.h:91
#define LogDebug(id)