test
CMS 3D CMS Logo

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