CMS 3D CMS Logo

FastTimerSD.cc
Go to the documentation of this file.
2 
4 
11 
15 
17 
20 
24 
25 #include "G4Track.hh"
26 #include "G4SDManager.hh"
27 #include "G4VProcess.hh"
28 #include "G4EventManager.hh"
29 #include "G4Step.hh"
30 #include "G4ParticleTable.hh"
31 
32 #include <string>
33 #include <vector>
34 #include <iostream>
35 
37 
38 //#define EDM_ML_DEBUG
39 //-------------------------------------------------------------------
41  const SensitiveDetectorCatalog & clg,
42  edm::ParameterSet const & p,
43  const SimTrackManager* manager) :
44  SensitiveTkDetector(name, cpv, clg, p), ftcons(nullptr),
45  hcID(-1), theHC(nullptr), theManager(manager), currentHit(nullptr), theTrack(nullptr),
46  currentPV(nullptr), unitID(0), previousUnitID(0), preStepPoint(nullptr),
47  postStepPoint(nullptr), eventno(0) {
48 
49  //Parameters
50  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("FastTimerSD");
51  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
52 
53  SetVerboseLevel(verbn);
54 
55  slave = new TrackingSlaveSD(name);
56 
57  std::string attribute = "ReadOutName";
58  DDSpecificsMatchesValueFilter filter{DDValue(attribute,name,0)};
59  DDFilteredView fv(cpv,filter);
60  fv.firstChild();
62  std::vector<int> temp = dbl_to_int(getDDDArray("Type",sv));
63  type_ = temp[0];
64 
65  edm::LogInfo("FastTimerSim") << "FastTimerSD: Instantiation completed for "
66  << name << " of type " << type_;
67 }
68 
70  if (slave) delete slave;
71 }
72 
73 double FastTimerSD::getEnergyDeposit(const G4Step* aStep) {
74  return aStep->GetTotalEnergyDeposit();
75 }
76 
77 void FastTimerSD::Initialize(G4HCofThisEvent * HCE) {
78 #ifdef EDM_ML_DEBUG
79  std::cout << "FastTimerSD : Initialize called for " << name << std::endl;
80 #endif
81 
83  if (hcID<0)
84  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
85  HCE->AddHitsCollection(hcID, theHC);
86 
87  tsID = -2;
88  primID = -2;
89 }
90 
91 bool FastTimerSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
92 
93  if (aStep == nullptr) {
94  return true;
95  } else {
96  GetStepInfo(aStep);
97 #ifdef EDM_ML_DEBUG
98  std::cout << "FastTimerSD : number of hits = " << theHC->entries() <<"\n";
99 #endif
100  if (HitExists() == false && edeposit>0. ){
101  CreateNewHit();
102  return true;
103  }
104  }
105  return true;
106 }
107 
108 void FastTimerSD::GetStepInfo(G4Step* aStep) {
109 
110  preStepPoint = aStep->GetPreStepPoint();
111  postStepPoint= aStep->GetPostStepPoint();
112  theTrack = aStep->GetTrack();
113  hitPoint = preStepPoint->GetPosition();
114  currentPV = preStepPoint->GetPhysicalVolume();
115  hitPointExit = postStepPoint->GetPosition();
116 
117  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
118  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
119 
120 
121  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
122 #ifdef EDM_ML_DEBUG
123  std::cout << "FastTimerSD :particleType = "
124  << theTrack->GetDefinition()->GetParticleName() << std::endl;
125 #endif
126  if (particleCode == emPDG ||
127  particleCode == epPDG ||
128  particleCode == gammaPDG ) {
129  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
130  } else {
131  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
132  }
133  edeposit = aStep->GetTotalEnergyDeposit();
134  tSlice = (100*postStepPoint->GetGlobalTime() )/CLHEP::nanosecond;
135  tSliceID = (int) tSlice;
136  unitID = setDetUnitId(aStep);
137 #ifdef EDM_ML_DEBUG
138  std::cout << "FastTimerSD:unitID = " << std::hex << unitID << std::dec<<"\n";
139 #endif
140  primaryID = theTrack->GetTrackID();
141  // Position = hitPoint;
142  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/CLHEP::GeV;
143  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/CLHEP::nanosecond;
144  Eloss = aStep->GetTotalEnergyDeposit()/CLHEP::GeV;
145  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
146  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/CLHEP::deg;
147  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/CLHEP::deg;
148 
149  ParentId = theTrack->GetParentID();
150  Vx = theTrack->GetVertexPosition().x();
151  Vy = theTrack->GetVertexPosition().y();
152  Vz = theTrack->GetVertexPosition().z();
153  X = hitPoint.x();
154  Y = hitPoint.y();
155  Z = hitPoint.z();
156 }
157 
158 uint32_t FastTimerSD::setDetUnitId(const G4Step * aStep) {
159 
160  //Find the depth segment
161  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
162  G4ThreeVector global = aStep->GetPreStepPoint()->GetPosition();
163  G4ThreeVector local = touch->GetHistory()->GetTopTransform().TransformPoint(global);
164  int iz = (global.z() > 0) ? 1 : -1;
165  std::pair<int,int> izphi = ((ftcons) ? ((type_ == 1) ?
166  (ftcons->getZPhi(std::abs(local.z()),local.phi())) :
167  (ftcons->getEtaPhi(local.perp(),local.phi()))) :
168  (std::pair<int,int>(0,0)));
169  uint32_t id = FastTimeDetId(type_,izphi.first,izphi.second,iz).rawId();
170 #ifdef EDM_ML_DEBUG
171  std::cout << "Volume " << touch->GetVolume(0)->GetName() << ":" << global.z()
172  << " Iz(eta)phi " << izphi.first << ":" << izphi.second << ":"
173  << iz << " id " << std::hex << id << std::dec << std::endl;
174 #endif
175  return id;
176 }
177 
178 
180  if (primaryID<1) {
181  edm::LogWarning("FastTimerSim") << "***** FastTimerSD error: primaryID = "
182  << primaryID
183  << " maybe detector name changed";
184  }
185 
186  // Update if in the same detector, time-slice and for same track
187  if (tSliceID == tsID && unitID==previousUnitID) {
188  UpdateHit();
189  return true;
190  }
191  // Reset entry point for new primary
192  if (primaryID != primID)
194 
195  //look in the HitContainer whether a hit with the same primID, unitID,
196  //tSliceID already exists:
197 
198  G4bool found = false;
199 
200  for (int j=0; j<theHC->entries()&&!found; j++) {
201  BscG4Hit* aPreviousHit = (*theHC)[j];
202  if (aPreviousHit->getTrackID() == primaryID &&
203  aPreviousHit->getTimeSliceID() == tSliceID &&
204  aPreviousHit->getUnitID() == unitID ) {
205  currentHit = aPreviousHit;
206  found = true;
207  }
208  }
209 
210  if (found) {
211  UpdateHit();
212  return true;
213  } else {
214  return false;
215  }
216 }
217 
218 
222  incidentEnergy = preStepPoint->GetKineticEnergy();
223 }
224 
225 
227 
228  if (primID<0) return;
229  if (hit == nullptr) {
230  edm::LogWarning("FastTimerSim") << "FastTimerSD: hit to be stored is NULL !!";
231  } else {
232  theHC->insert( hit );
233  }
234 }
235 
236 
238 
239 #ifdef EDM_ML_DEBUG
240  std::cout << "FastTimerSD CreateNewHit for" << " PV "
241  << currentPV->GetName() << " PVid = " << currentPV->GetCopyNo()
242  << " Unit " << unitID << std::endl;
243  std::cout << " primary " << primaryID << " time slice " << tSliceID
244  << " For Track " << theTrack->GetTrackID() << " which is a "
245  << theTrack->GetDefinition()->GetParticleName();
246 
247  if (theTrack->GetTrackID()==1) {
248  std::cout << " of energy " << theTrack->GetTotalEnergy();
249  } else {
250  std::cout << " daughter of part. " << theTrack->GetParentID();
251  }
252 
253  std::cout << " and created by " ;
254  if (theTrack->GetCreatorProcess()!=NULL)
255  std::cout << theTrack->GetCreatorProcess()->GetProcessName() ;
256  else
257  std::cout << "NO process";
258  std::cout << std::endl;
259 #endif
260 
261  currentHit = new BscG4Hit;
266 
273 
275 
278 
280  currentHit->setVx(Vx);
281  currentHit->setVy(Vy);
282  currentHit->setVz(Vz);
283 
284  currentHit->setX(X);
285  currentHit->setY(Y);
286  currentHit->setZ(Z);
287 
288  UpdateHit();
289 
291 }
292 
293 
295 
296  if (Eloss > 0.) {
298 
299 #ifdef EDM_ML_DEBUG
300  std::cout << "updateHit: add eloss " << Eloss <<std::endl;
301  std::cout << "CurrentHit="<< currentHit<< ", PostStepPoint = "
302  << postStepPoint->GetPosition() << std::endl;
303 #endif
305  }
306 
307  // buffer for next steps:
308  tsID = tSliceID;
309  primID = primaryID;
311 }
312 
313 
314 G4ThreeVector FastTimerSD::SetToLocal(const G4ThreeVector& global){
315 
316  const G4VTouchable* touch= preStepPoint->GetTouchable();
317  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
318  return theEntryPoint;
319 }
320 
321 
322 G4ThreeVector FastTimerSD::SetToLocalExit(const G4ThreeVector& globalPoint){
323 
324  const G4VTouchable* touch= postStepPoint->GetTouchable();
325  theExitPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
326  return theExitPoint;
327 }
328 
329 
330 void FastTimerSD::EndOfEvent(G4HCofThisEvent* ) {
331 
332  // here we loop over transient hits and make them persistent
333  for (int j=0; j<theHC->entries(); j++) {
334  BscG4Hit* aHit = (*theHC)[j];
335 #ifdef EDM_ML_DEBUG
336  std::cout << "hit number " << j << " unit ID = " << std::hex
337  << aHit->getUnitID() << std::dec << " entry z "
338  << aHit->getEntry().z() << " entry theta "
339  << aHit->getThetaAtEntry() << std::endl;
340 #endif
341  Local3DPoint locExitPoint(0,0,0);
342  Local3DPoint locEntryPoint(aHit->getEntry().x(),
343  aHit->getEntry().y(),
344  aHit->getEntry().z());
345  slave->processHits(PSimHit(locEntryPoint,locExitPoint,
346  aHit->getPabs(),
347  aHit->getTof(),
348  aHit->getEnergyLoss(),
349  aHit->getParticleType(),
350  aHit->getUnitID(),
351  aHit->getTrackID(),
352  aHit->getThetaAtEntry(),
353  aHit->getPhiAtEntry()));
354  }
355  Summarize();
356 }
357 
359 
361 
363 
365 #ifdef EDM_ML_DEBUG
366  std::cout << "FastTimerSD: Collection " << theHC->GetName() << std::endl;
367 #endif
368  theHC->PrintAllHits();
369 }
370 
372  if (slave->name() == hname) { cc=slave->hits(); }
373 }
374 
375 void FastTimerSD::update(const BeginOfJob * job) {
376 
377  const edm::EventSetup* es = (*job)();
379  es->get<IdealGeometryRecord>().get(fdc);
380  if (fdc.isValid()) {
381  ftcons = &(*fdc);
382  } else {
383  edm::LogError("FastTimerSim") << "FastTimerSD : Cannot find FastTimeDDDConstants";
384  throw cms::Exception("Unknown", "FastTimerSD") << "Cannot find FastTimeDDDConstants\n";
385  }
386 #ifdef EDM_ML_DEBUG
387  std::cout << "FastTimerSD::Initialized with FastTimeDDDConstants\n";
388 #endif
389 }
390 
392 #ifdef EDM_ML_DEBUG
393  std::cout << "Dispatched BeginOfEvent for " << GetName() << " !\n" ;
394 #endif
395  clearHits();
396  eventno = (*i)()->GetEventID();
397 }
398 
400 
401  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
402  G4String particleName;
403  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
404  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
405  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
406 
407 }
408 
409 void FastTimerSD::update (const ::EndOfEvent*) {}
410 
412  slave->Initialize();
413 }
414 
415 std::vector<double> FastTimerSD::getDDDArray(const std::string & str,
416  const DDsvalues_type & sv) {
417 
418  DDValue value(str);
419  if (DDfetch(&sv,value)) {
420  const std::vector<double> & fvec = value.doubles();
421  int nval = fvec.size();
422  if (nval < 1) {
423  edm::LogError("FastTimerSim") << "FastTimerSD : # of " << str
424  << " bins " << nval << " < 1 ==> illegal";
425  throw cms::Exception("DDException") << "FastTimerSD: cannot get array " << str;
426  }
427  return fvec;
428  } else {
429  edm::LogError("FastTimerSim") << "FastTimerSD: cannot get array " << str;
430  throw cms::Exception("DDException") << "FastTimerSD: cannot get array " << str;
431  }
432 }
std::string name
Definition: FastTimerSD.h:89
G4ThreeVector hitPoint
Definition: FastTimerSD.h:106
void clear() override
Definition: FastTimerSD.cc:360
void setTof(float e)
Definition: BSCG4Hit.cc:154
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
G4ThreeVector hitPointExit
Definition: FastTimerSD.h:107
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:140
void setEntry(const G4ThreeVector &xyz)
Definition: BSCG4Hit.cc:116
const double GeV
Definition: MathUtil.h:16
float getPhiAtEntry() const
Definition: BSCG4Hit.cc:159
FastTimerSD(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: FastTimerSD.cc:40
void setVx(float p)
Definition: BSCG4Hit.cc:177
G4ThreeVector SetToLocalExit(const G4ThreeVector &globalPoint)
Definition: FastTimerSD.cc:322
int getParticleType() const
Definition: BSCG4Hit.cc:151
double getEnergyDeposit(const G4Step *step)
Definition: FastTimerSD.cc:73
G4int getTrackID() const
Definition: BSCG4Hit.cc:133
std::pair< int, int > getEtaPhi(double r, double phi) const
float edepositHAD
Definition: FastTimerSD.h:123
G4ThreeVector theEntryPoint
Definition: FastTimerSD.h:84
G4int tSliceID
Definition: FastTimerSD.h:99
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
void UpdateHit()
Definition: FastTimerSD.cc:294
#define NULL
Definition: scimark2.h:8
BscG4Hit * currentHit
Definition: FastTimerSD.h:95
void setUnitID(unsigned int i)
Definition: BSCG4Hit.cc:137
float getTof() const
Definition: BSCG4Hit.cc:149
float getPabs() const
Definition: BSCG4Hit.cc:148
uint32_t unitID
Definition: FastTimerSD.h:98
float edeposit
Definition: FastTimerSD.h:104
void setVy(float p)
Definition: BSCG4Hit.cc:180
#define nullptr
void setY(float t)
Definition: BSCG4Hit.cc:168
short ParticleType
Definition: FastTimerSD.h:112
type of data representation of DDCompactView
Definition: DDCompactView.h:90
unsigned int getUnitID() const
Definition: BSCG4Hit.cc:136
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:81
void update(const BeginOfJob *) override
This routine will be called when the appropriate signal arrives.
Definition: FastTimerSD.cc:375
int getTimeSliceID() const
Definition: BSCG4Hit.cc:141
G4int tsID
Definition: FastTimerSD.h:94
uint32_t rawId() const
get the raw id
Definition: DetId.h:44
const FastTimeDDDConstants * ftcons
Definition: FastTimerSD.h:80
void setThetaAtEntry(float t)
Definition: BSCG4Hit.cc:161
std::vector< int > dbl_to_int(const std::vector< double > &vecdbl)
Converts a std::vector of doubles to a std::vector of int.
Definition: DDutils.h:7
G4ThreeVector SetToLocal(const G4ThreeVector &global)
Definition: FastTimerSD.cc:314
std::pair< int, int > getZPhi(double z, double phi) const
void setX(float t)
Definition: BSCG4Hit.cc:165
void setPabs(float e)
Definition: BSCG4Hit.cc:153
G4bool HitExists()
Definition: FastTimerSD.cc:179
void setEntryLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:119
std::vector< PSimHit > & hits()
TrackingSlaveSD * slave
Definition: FastTimerSD.h:79
G4int gammaPDG
Definition: FastTimerSD.h:126
std::string const collectionName[nCollections]
Definition: Collections.h:47
G4int primID
Definition: FastTimerSD.h:87
virtual void Initialize()
void setPhiAtEntry(float f)
Definition: BSCG4Hit.cc:162
void clearHits() override
Definition: FastTimerSD.cc:411
G4StepPoint * postStepPoint
Definition: FastTimerSD.h:103
std::vector< std::pair< unsigned int, DDValue > > DDsvalues_type
std::maps an index to a DDValue. The index corresponds to the index assigned to the name of the std::...
Definition: DDsvalues.h:20
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
G4ThreeVector hitPointLocalExit
Definition: FastTimerSD.h:109
G4VPhysicalVolume * currentPV
Definition: FastTimerSD.h:97
void ResetForNewPrimary()
Definition: FastTimerSD.cc:219
void StoreHit(BscG4Hit *)
Definition: FastTimerSD.cc:226
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: FastTimerSD.cc:371
uint32_t setDetUnitId(const G4Step *) override
Definition: FastTimerSD.cc:158
G4ThreeVector hitPointLocal
Definition: FastTimerSD.h:108
G4Track * theTrack
Definition: FastTimerSD.h:96
void DrawAll() override
Definition: FastTimerSD.cc:362
float incidentEnergy
Definition: FastTimerSD.h:86
float getThetaAtEntry() const
Definition: BSCG4Hit.cc:158
BscG4HitCollection * theHC
Definition: FastTimerSD.h:91
~FastTimerSD() override
Definition: FastTimerSD.cc:69
void addEnergyDeposit(double em, double hd)
Definition: BSCG4Hit.cc:143
G4StepPoint * preStepPoint
Definition: FastTimerSD.h:102
void setTimeSlice(double d)
Definition: BSCG4Hit.cc:140
void setParticleType(short i)
Definition: BSCG4Hit.cc:156
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: FastTimerSD.cc:330
void setVz(float p)
Definition: BSCG4Hit.cc:183
const T & get() const
Definition: EventSetup.h:59
G4ThreeVector entrancePoint
Definition: FastTimerSD.h:83
std::vector< double > getDDDArray(const std::string &, const DDsvalues_type &)
Definition: FastTimerSD.cc:415
DDsvalues_type mergedSpecifics() const
G4ThreeVector theExitPoint
Definition: FastTimerSD.h:84
uint32_t previousUnitID
Definition: FastTimerSD.h:98
float PhiAtEntry
Definition: FastTimerSD.h:113
G4ThreeVector getEntry() const
Definition: BSCG4Hit.cc:115
float edepositEM
Definition: FastTimerSD.h:123
G4double tSlice
Definition: FastTimerSD.h:100
void setZ(float t)
Definition: BSCG4Hit.cc:171
bool firstChild()
set the current node to the first child ...
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
void setExitLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:122
G4THitsCollection< BscG4Hit > BscG4HitCollection
void setIncidentEnergy(double e)
Definition: BSCG4Hit.cc:131
float ThetaAtEntry
Definition: FastTimerSD.h:113
void Summarize()
Definition: FastTimerSD.cc:358
G4int hcID
Definition: FastTimerSD.h:90
G4ThreeVector exitPoint
Definition: FastTimerSD.h:83
void Initialize(G4HCofThisEvent *HCE) override
Definition: FastTimerSD.cc:77
void CreateNewHit()
Definition: FastTimerSD.cc:237
float getEnergyLoss() const
Definition: BSCG4Hit.cc:150
G4int primaryID
Definition: FastTimerSD.h:99
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: FastTimerSD.cc:91
void GetStepInfo(G4Step *aStep)
Definition: FastTimerSD.cc:108
void PrintAll() override
Definition: FastTimerSD.cc:364