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), name(name),
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  //Add FastTimer Sentitive Detector Name
50  collectionName.insert(name);
51 
52 
53  //Parameters
54  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("FastTimerSD");
55  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
56 
57  SetVerboseLevel(verbn);
58 #ifdef EDM_ML_DEBUG
59  std::cout << "*******************************************************\n"
60  << "* *\n"
61  << "* Constructing a FastTimerSD with name " << name << "\n"
62  << "* *\n"
63  << "*******************************************************\n";
64 #endif
65 
66  slave = new TrackingSlaveSD(name);
67 
68  //
69  // attach detectors (LogicalVolumes)
70  //
71  std::vector<std::string> lvNames = clg.logicalNames(name);
72 
73  this->Register();
74 
75  for (std::vector<std::string>::iterator it=lvNames.begin();
76  it !=lvNames.end(); it++) {
77  this->AssignSD(*it);
78  edm::LogInfo("FastTimerSim") << "FastTimerSD : Assigns SD to LV " << (*it);
79  }
80 
81  std::string attribute = "ReadOutName";
82  DDSpecificsMatchesValueFilter filter{DDValue(attribute,name,0)};
83  DDFilteredView fv(cpv,filter);
84  fv.firstChild();
86  std::vector<int> temp = dbl_to_int(getDDDArray("Type",sv));
87  type_ = temp[0];
88 
89  edm::LogInfo("FastTimerSim") << "FastTimerSD: Instantiation completed for "
90  << name << " of type " << type_;
91 }
92 
93 
95  if (slave) delete slave;
96 }
97 
98 double FastTimerSD::getEnergyDeposit(G4Step* aStep) {
99  return aStep->GetTotalEnergyDeposit();
100 }
101 
102 void FastTimerSD::Initialize(G4HCofThisEvent * HCE) {
103 #ifdef EDM_ML_DEBUG
104  std::cout << "FastTimerSD : Initialize called for " << name << std::endl;
105 #endif
106 
108  if (hcID<0)
109  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
110  HCE->AddHitsCollection(hcID, theHC);
111 
112  tsID = -2;
113  primID = -2;
114 }
115 
116 
117 bool FastTimerSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
118 
119  if (aStep == nullptr) {
120  return true;
121  } else {
122  GetStepInfo(aStep);
123 #ifdef EDM_ML_DEBUG
124  std::cout << "FastTimerSD : number of hits = " << theHC->entries() <<"\n";
125 #endif
126  if (HitExists() == false && edeposit>0. ){
127  CreateNewHit();
128  return true;
129  }
130  }
131  return true;
132 }
133 
134 void FastTimerSD::GetStepInfo(G4Step* aStep) {
135 
136  preStepPoint = aStep->GetPreStepPoint();
137  postStepPoint= aStep->GetPostStepPoint();
138  theTrack = aStep->GetTrack();
139  hitPoint = preStepPoint->GetPosition();
140  currentPV = preStepPoint->GetPhysicalVolume();
141  hitPointExit = postStepPoint->GetPosition();
142 
143  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
144  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
145 
146 
147  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
148 #ifdef EDM_ML_DEBUG
149  std::cout << "FastTimerSD :particleType = "
150  << theTrack->GetDefinition()->GetParticleName() << std::endl;
151 #endif
152  if (particleCode == emPDG ||
153  particleCode == epPDG ||
154  particleCode == gammaPDG ) {
155  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
156  } else {
157  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
158  }
159  edeposit = aStep->GetTotalEnergyDeposit();
160  tSlice = (100*postStepPoint->GetGlobalTime() )/CLHEP::nanosecond;
161  tSliceID = (int) tSlice;
162  unitID = setDetUnitId(aStep);
163 #ifdef EDM_ML_DEBUG
164  std::cout << "FastTimerSD:unitID = " << std::hex << unitID << std::dec<<"\n";
165 #endif
166  primaryID = theTrack->GetTrackID();
167  // Position = hitPoint;
168  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/CLHEP::GeV;
169  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/CLHEP::nanosecond;
170  Eloss = aStep->GetTotalEnergyDeposit()/CLHEP::GeV;
171  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
172  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/CLHEP::deg;
173  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/CLHEP::deg;
174 
175  ParentId = theTrack->GetParentID();
176  Vx = theTrack->GetVertexPosition().x();
177  Vy = theTrack->GetVertexPosition().y();
178  Vz = theTrack->GetVertexPosition().z();
179  X = hitPoint.x();
180  Y = hitPoint.y();
181  Z = hitPoint.z();
182 }
183 
184 uint32_t FastTimerSD::setDetUnitId(G4Step * aStep) {
185 
186  //Find the depth segment
187  const G4VTouchable* touch = aStep->GetPreStepPoint()->GetTouchable();
188  G4ThreeVector global = aStep->GetPreStepPoint()->GetPosition();
189  G4ThreeVector local = touch->GetHistory()->GetTopTransform().TransformPoint(global);
190  int iz = (global.z() > 0) ? 1 : -1;
191  std::pair<int,int> izphi = ((ftcons) ? ((type_ == 1) ?
192  (ftcons->getZPhi(std::abs(local.z()),local.phi())) :
193  (ftcons->getEtaPhi(local.perp(),local.phi()))) :
194  (std::pair<int,int>(0,0)));
195  uint32_t id = FastTimeDetId(type_,izphi.first,izphi.second,iz).rawId();
196 #ifdef EDM_ML_DEBUG
197  std::cout << "Volume " << touch->GetVolume(0)->GetName() << ":" << global.z()
198  << " Iz(eta)phi " << izphi.first << ":" << izphi.second << ":"
199  << iz << " id " << std::hex << id << std::dec << std::endl;
200 #endif
201  return id;
202 }
203 
204 
206  if (primaryID<1) {
207  edm::LogWarning("FastTimerSim") << "***** FastTimerSD error: primaryID = "
208  << primaryID
209  << " maybe detector name changed";
210  }
211 
212  // Update if in the same detector, time-slice and for same track
213  if (tSliceID == tsID && unitID==previousUnitID) {
214  UpdateHit();
215  return true;
216  }
217  // Reset entry point for new primary
218  if (primaryID != primID)
220 
221  //look in the HitContainer whether a hit with the same primID, unitID,
222  //tSliceID already exists:
223 
224  G4bool found = false;
225 
226  for (int j=0; j<theHC->entries()&&!found; j++) {
227  BscG4Hit* aPreviousHit = (*theHC)[j];
228  if (aPreviousHit->getTrackID() == primaryID &&
229  aPreviousHit->getTimeSliceID() == tSliceID &&
230  aPreviousHit->getUnitID() == unitID ) {
231  currentHit = aPreviousHit;
232  found = true;
233  }
234  }
235 
236  if (found) {
237  UpdateHit();
238  return true;
239  } else {
240  return false;
241  }
242 }
243 
244 
248  incidentEnergy = preStepPoint->GetKineticEnergy();
249 }
250 
251 
253 
254  if (primID<0) return;
255  if (hit == nullptr) {
256  edm::LogWarning("FastTimerSim") << "FastTimerSD: hit to be stored is NULL !!";
257  } else {
258  theHC->insert( hit );
259  }
260 }
261 
262 
264 
265 #ifdef EDM_ML_DEBUG
266  std::cout << "FastTimerSD CreateNewHit for" << " PV "
267  << currentPV->GetName() << " PVid = " << currentPV->GetCopyNo()
268  << " Unit " << unitID << std::endl;
269  std::cout << " primary " << primaryID << " time slice " << tSliceID
270  << " For Track " << theTrack->GetTrackID() << " which is a "
271  << theTrack->GetDefinition()->GetParticleName();
272 
273  if (theTrack->GetTrackID()==1) {
274  std::cout << " of energy " << theTrack->GetTotalEnergy();
275  } else {
276  std::cout << " daughter of part. " << theTrack->GetParentID();
277  }
278 
279  std::cout << " and created by " ;
280  if (theTrack->GetCreatorProcess()!=NULL)
281  std::cout << theTrack->GetCreatorProcess()->GetProcessName() ;
282  else
283  std::cout << "NO process";
284  std::cout << std::endl;
285 #endif
286 
287  currentHit = new BscG4Hit;
292 
299 
301 
304 
306  currentHit->setVx(Vx);
307  currentHit->setVy(Vy);
308  currentHit->setVz(Vz);
309 
310  currentHit->setX(X);
311  currentHit->setY(Y);
312  currentHit->setZ(Z);
313 
314  UpdateHit();
315 
317 }
318 
319 
321 
322  if (Eloss > 0.) {
324 
325 #ifdef EDM_ML_DEBUG
326  std::cout << "updateHit: add eloss " << Eloss <<std::endl;
327  std::cout << "CurrentHit="<< currentHit<< ", PostStepPoint = "
328  << postStepPoint->GetPosition() << std::endl;
329 #endif
331  }
332 
333  // buffer for next steps:
334  tsID = tSliceID;
335  primID = primaryID;
337 }
338 
339 
340 G4ThreeVector FastTimerSD::SetToLocal(const G4ThreeVector& global){
341 
342  const G4VTouchable* touch= preStepPoint->GetTouchable();
343  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
344  return theEntryPoint;
345 }
346 
347 
348 G4ThreeVector FastTimerSD::SetToLocalExit(const G4ThreeVector& globalPoint){
349 
350  const G4VTouchable* touch= postStepPoint->GetTouchable();
351  theExitPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
352  return theExitPoint;
353 }
354 
355 
356 void FastTimerSD::EndOfEvent(G4HCofThisEvent* ) {
357 
358  // here we loop over transient hits and make them persistent
359  for (int j=0; j<theHC->entries(); j++) {
360  BscG4Hit* aHit = (*theHC)[j];
361 #ifdef EDM_ML_DEBUG
362  std::cout << "hit number " << j << " unit ID = " << std::hex
363  << aHit->getUnitID() << std::dec << " entry z "
364  << aHit->getEntry().z() << " entry theta "
365  << aHit->getThetaAtEntry() << std::endl;
366 #endif
367  Local3DPoint locExitPoint(0,0,0);
368  Local3DPoint locEntryPoint(aHit->getEntry().x(),
369  aHit->getEntry().y(),
370  aHit->getEntry().z());
371  slave->processHits(PSimHit(locEntryPoint,locExitPoint,
372  aHit->getPabs(),
373  aHit->getTof(),
374  aHit->getEnergyLoss(),
375  aHit->getParticleType(),
376  aHit->getUnitID(),
377  aHit->getTrackID(),
378  aHit->getThetaAtEntry(),
379  aHit->getPhiAtEntry()));
380  }
381  Summarize();
382 }
383 
385 
387 
389 
391 #ifdef EDM_ML_DEBUG
392  std::cout << "FastTimerSD: Collection " << theHC->GetName() << std::endl;
393 #endif
394  theHC->PrintAllHits();
395 }
396 
398  if (slave->name() == n) c=slave->hits();
399 }
400 
401 void FastTimerSD::update(const BeginOfJob * job) {
402 
403  const edm::EventSetup* es = (*job)();
405  es->get<IdealGeometryRecord>().get(fdc);
406  if (fdc.isValid()) {
407  ftcons = &(*fdc);
408  } else {
409  edm::LogError("FastTimerSim") << "FastTimerSD : Cannot find FastTimeDDDConstants";
410  throw cms::Exception("Unknown", "FastTimerSD") << "Cannot find FastTimeDDDConstants\n";
411  }
412 #ifdef EDM_ML_DEBUG
413  std::cout << "FastTimerSD::Initialized with FastTimeDDDConstants\n";
414 #endif
415 }
416 
418 #ifdef EDM_ML_DEBUG
419  std::cout << "Dispatched BeginOfEvent for " << GetName() << " !\n" ;
420 #endif
421  clearHits();
422  eventno = (*i)()->GetEventID();
423 }
424 
426 
427  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
428  G4String particleName;
429  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
430  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
431  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
432 
433 }
434 
435 void FastTimerSD::update (const ::EndOfEvent*) {}
436 
438  slave->Initialize();
439 }
440 
441 std::vector<std::string> FastTimerSD::getNames(){
442  std::vector<std::string> temp;
443  temp.push_back(slave->name());
444  return temp;
445 }
446 
447 std::vector<double> FastTimerSD::getDDDArray(const std::string & str,
448  const DDsvalues_type & sv) {
449 
450  DDValue value(str);
451  if (DDfetch(&sv,value)) {
452  const std::vector<double> & fvec = value.doubles();
453  int nval = fvec.size();
454  if (nval < 1) {
455  edm::LogError("FastTimerSim") << "FastTimerSD : # of " << str
456  << " bins " << nval << " < 1 ==> illegal";
457  throw cms::Exception("DDException") << "FastTimerSD: cannot get array " << str;
458  }
459  return fvec;
460  } else {
461  edm::LogError("FastTimerSim") << "FastTimerSD: cannot get array " << str;
462  throw cms::Exception("DDException") << "FastTimerSD: cannot get array " << str;
463  }
464 }
std::string name
Definition: FastTimerSD.h:94
G4ThreeVector hitPoint
Definition: FastTimerSD.h:111
void clear() override
Definition: FastTimerSD.cc:386
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:112
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
void setVx(float p)
Definition: BSCG4Hit.cc:177
G4ThreeVector SetToLocalExit(const G4ThreeVector &globalPoint)
Definition: FastTimerSD.cc:348
int getParticleType() const
Definition: BSCG4Hit.cc:151
G4int getTrackID() const
Definition: BSCG4Hit.cc:133
std::pair< int, int > getEtaPhi(double r, double phi) const
float edepositHAD
Definition: FastTimerSD.h:128
G4ThreeVector theEntryPoint
Definition: FastTimerSD.h:89
G4int tSliceID
Definition: FastTimerSD.h:104
virtual double getEnergyDeposit(G4Step *step)
Definition: FastTimerSD.cc:98
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:320
uint32_t setDetUnitId(G4Step *) override
Definition: FastTimerSD.cc:184
#define NULL
Definition: scimark2.h:8
BscG4Hit * currentHit
Definition: FastTimerSD.h:100
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:103
float edeposit
Definition: FastTimerSD.h:109
const std::vector< std::string > & logicalNames(const std::string &readoutName) const
void setVy(float p)
Definition: BSCG4Hit.cc:180
#define nullptr
void setY(float t)
Definition: BSCG4Hit.cc:168
short ParticleType
Definition: FastTimerSD.h:117
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:401
virtual void AssignSD(const std::string &vname)
int getTimeSliceID() const
Definition: BSCG4Hit.cc:141
G4int tsID
Definition: FastTimerSD.h:99
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
const FastTimeDDDConstants * ftcons
Definition: FastTimerSD.h:85
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:340
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:205
void setEntryLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:119
std::vector< PSimHit > & hits()
TrackingSlaveSD * slave
Definition: FastTimerSD.h:84
G4int gammaPDG
Definition: FastTimerSD.h:131
std::string const collectionName[nCollections]
Definition: Collections.h:47
G4int primID
Definition: FastTimerSD.h:92
virtual void Initialize()
FastTimerSD(std::string, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: FastTimerSD.cc:40
void setPhiAtEntry(float f)
Definition: BSCG4Hit.cc:162
void clearHits() override
Definition: FastTimerSD.cc:437
G4StepPoint * postStepPoint
Definition: FastTimerSD.h:108
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:114
G4VPhysicalVolume * currentPV
Definition: FastTimerSD.h:102
void ResetForNewPrimary()
Definition: FastTimerSD.cc:245
void StoreHit(BscG4Hit *)
Definition: FastTimerSD.cc:252
std::vector< std::string > getNames() override
Definition: FastTimerSD.cc:441
G4ThreeVector hitPointLocal
Definition: FastTimerSD.h:113
G4Track * theTrack
Definition: FastTimerSD.h:101
void DrawAll() override
Definition: FastTimerSD.cc:388
float incidentEnergy
Definition: FastTimerSD.h:91
float getThetaAtEntry() const
Definition: BSCG4Hit.cc:158
BscG4HitCollection * theHC
Definition: FastTimerSD.h:96
~FastTimerSD() override
Definition: FastTimerSD.cc:94
void addEnergyDeposit(double em, double hd)
Definition: BSCG4Hit.cc:143
G4StepPoint * preStepPoint
Definition: FastTimerSD.h:107
void setTimeSlice(double d)
Definition: BSCG4Hit.cc:140
void setParticleType(short i)
Definition: BSCG4Hit.cc:156
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: FastTimerSD.cc:356
void setVz(float p)
Definition: BSCG4Hit.cc:183
const T & get() const
Definition: EventSetup.h:55
G4ThreeVector entrancePoint
Definition: FastTimerSD.h:88
std::vector< double > getDDDArray(const std::string &, const DDsvalues_type &)
Definition: FastTimerSD.cc:447
DDsvalues_type mergedSpecifics() const
G4ThreeVector theExitPoint
Definition: FastTimerSD.h:89
void fillHits(edm::PSimHitContainer &, std::string use) override
Definition: FastTimerSD.cc:397
uint32_t previousUnitID
Definition: FastTimerSD.h:103
float PhiAtEntry
Definition: FastTimerSD.h:118
G4ThreeVector getEntry() const
Definition: BSCG4Hit.cc:115
float edepositEM
Definition: FastTimerSD.h:128
G4double tSlice
Definition: FastTimerSD.h:105
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:118
void Summarize()
Definition: FastTimerSD.cc:384
G4int hcID
Definition: FastTimerSD.h:95
G4ThreeVector exitPoint
Definition: FastTimerSD.h:88
void Initialize(G4HCofThisEvent *HCE) override
Definition: FastTimerSD.cc:102
void CreateNewHit()
Definition: FastTimerSD.cc:263
float getEnergyLoss() const
Definition: BSCG4Hit.cc:150
G4int primaryID
Definition: FastTimerSD.h:104
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: FastTimerSD.cc:117
void GetStepInfo(G4Step *aStep)
Definition: FastTimerSD.cc:134
void PrintAll() override
Definition: FastTimerSD.cc:390