CMS 3D CMS Logo

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