CMS 3D CMS Logo

BscSD.cc
Go to the documentation of this file.
1 // File: BscSD.cc
3 // Date: 02.2006
4 // Description: Sensitive Detector class for Bsc
5 // Modifications:
7 
11 
14 
15 
20 
24 
26 
27 #include "G4Track.hh"
28 #include "G4SDManager.hh"
29 #include "G4VProcess.hh"
30 #include "G4EventManager.hh"
31 #include "G4Step.hh"
32 #include "G4ParticleTable.hh"
33 
34 #include "G4PhysicalConstants.hh"
35 #include "G4SystemOfUnits.hh"
36 
37 #include <string>
38 #include <vector>
39 #include <iostream>
40 
42 
43 #define debug
44 //-------------------------------------------------------------------
46  const SensitiveDetectorCatalog & clg,
47  edm::ParameterSet const & p, const SimTrackManager* manager) :
48  SensitiveTkDetector(name, cpv, clg, p), numberingScheme(nullptr),
49  hcID(-1), theHC(nullptr), theManager(manager), currentHit(nullptr), theTrack(nullptr),
50  currentPV(nullptr), unitID(0), previousUnitID(0), preStepPoint(nullptr),
51  postStepPoint(nullptr), eventno(0){
52 
53  //Parameters
55  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
56  //int verbn = 1;
57 
58  SetVerboseLevel(verbn);
59 
60  slave = new TrackingSlaveSD(name);
61 
62  if (name == "BSCHits") {
63  if (verbn > 0) {
64  edm::LogInfo("BscSim") << "name = BSCHits and new BscNumberingSchem";
65  }
67  } else {
68  edm::LogWarning("BscSim") << "BscSD: ReadoutName "<<name<<" not supported";
69  }
70 }
71 
73  //AZ:
74  if (slave) delete slave;
75 
76  if (numberingScheme)
77  delete numberingScheme;
78 
79 }
80 
81 double BscSD::getEnergyDeposit(const G4Step* aStep) {
82  return aStep->GetTotalEnergyDeposit();
83 }
84 
85 void BscSD::Initialize(G4HCofThisEvent * HCE) {
86 #ifdef debug
87  LogDebug("BscSim") << "BscSD : Initialize called for " << GetName();
88 #endif
89 
90  theHC = new BscG4HitCollection(GetName(), collectionName[0]);
91  if (hcID<0)
92  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
93  HCE->AddHitsCollection(hcID, theHC);
94 
95  tsID = -2;
96  primID = -2;
97 
99 }
100 
101 bool BscSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
102 
103  if (aStep == nullptr) {
104  return true;
105  } else {
106  GetStepInfo(aStep);
107  // LogDebug("BscSim") << edeposit <<std::endl;
108 
109  //AZ
110 #ifdef debug
111  LogDebug("BscSim") << "BscSD : number of hits = " << theHC->entries() << std::endl;
112 #endif
113 
114  if (HitExists() == false && edeposit>0. ){
115  CreateNewHit();
116  return true;
117  }
118  }
119  return true;
120 }
121 
122 void BscSD::GetStepInfo(G4Step* aStep) {
123 
124  preStepPoint = aStep->GetPreStepPoint();
125  postStepPoint= aStep->GetPostStepPoint();
126  theTrack = aStep->GetTrack();
127  hitPoint = preStepPoint->GetPosition();
128  currentPV = preStepPoint->GetPhysicalVolume();
129  hitPointExit = postStepPoint->GetPosition();
130 
131  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
132  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
133 
134 
135  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
136  LogDebug("BscSim") << " BscSD :particleType = " << theTrack->GetDefinition()->GetParticleName() <<std::endl;
137  if (particleCode == emPDG ||
138  particleCode == epPDG ||
139  particleCode == gammaPDG ) {
140  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
141  } else {
142  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
143  }
144  edeposit = aStep->GetTotalEnergyDeposit();
145  tSlice = (postStepPoint->GetGlobalTime() )/nanosecond;
146  tSliceID = (int) tSlice;
147  unitID = setDetUnitId(aStep);
148 #ifdef debug
149  LogDebug("BscSim") << "unitID=" << unitID <<std::endl;
150 #endif
151  primaryID = theTrack->GetTrackID();
152  // Position = hitPoint;
153  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/GeV;
154  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/nanosecond;
155  Eloss = aStep->GetTotalEnergyDeposit()/GeV;
156  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
157  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/deg;
158  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/deg;
159 
160  ParentId = theTrack->GetParentID();
161  Vx = theTrack->GetVertexPosition().x();
162  Vy = theTrack->GetVertexPosition().y();
163  Vz = theTrack->GetVertexPosition().z();
164  X = hitPoint.x();
165  Y = hitPoint.y();
166  Z = hitPoint.z();
167 }
168 
169 uint32_t BscSD::setDetUnitId(const G4Step * aStep) {
170  return (numberingScheme == nullptr ? 0 : numberingScheme->getUnitID(aStep));
171 }
172 
174  if (primaryID<1) {
175  edm::LogWarning("BscSim") << "***** BscSD error: primaryID = "
176  << primaryID
177  << " maybe detector name changed";
178  }
179 
180  // Update if in the same detector, time-slice and for same track
181  // if (primaryID == primID && tSliceID == tsID && unitID==previousUnitID) {
182  if (tSliceID == tsID && unitID==previousUnitID) {
183  //AZ:
184  UpdateHit();
185  return true;
186  }
187  // Reset entry point for new primary
188  if (primaryID != primID)
190 
191  //look in the HitContainer whether a hit with the same primID, unitID,
192  //tSliceID already exists:
193 
194  G4bool found = false;
195 
196  // LogDebug("BscSim") << "BscSD: HCollection= " << theHC->entries() <<std::endl;
197 
198  for (int j=0; j<theHC->entries()&&!found; j++) {
199  BscG4Hit* aPreviousHit = (*theHC)[j];
200  if (aPreviousHit->getTrackID() == primaryID &&
201  aPreviousHit->getTimeSliceID() == tSliceID &&
202  aPreviousHit->getUnitID() == unitID ) {
203  //AZ:
204  currentHit = aPreviousHit;
205  found = true;
206  }
207  }
208 
209  if (found) {
210  //AZ:
211  UpdateHit();
212  return true;
213  } else {
214  return false;
215  }
216 }
217 
219 
222  incidentEnergy = preStepPoint->GetKineticEnergy();
223 
224 }
225 
227 
228  if (primID<0) return;
229  if (hit == nullptr ) {
230  edm::LogWarning("BscSim") << "BscSD: hit to be stored is NULL !!";
231  return;
232  }
233 
234  theHC->insert( hit );
235 }
236 
238 
239 #ifdef debug
240  LogDebug("BscSim") << "BscSD CreateNewHit for"
241  << " PV " << currentPV->GetName()
242  << " PVid = " << currentPV->GetCopyNo()
243  << " Unit " << unitID <<std::endl;
244  LogDebug("BscSim") << " primary " << primaryID
245  << " time slice " << tSliceID
246  << " For Track " << theTrack->GetTrackID()
247  << " which is a " << theTrack->GetDefinition()->GetParticleName();
248 
249  if (theTrack->GetTrackID()==1) {
250  LogDebug("BscSim") << " of energy " << theTrack->GetTotalEnergy();
251  } else {
252  LogDebug("BscSim") << " daughter of part. " << theTrack->GetParentID();
253  }
254 
255  LogDebug("BscSim") << " and created by " ;
256  if (theTrack->GetCreatorProcess()!=nullptr)
257  LogDebug("BscSim") << theTrack->GetCreatorProcess()->GetProcessName() ;
258  else
259  LogDebug("BscSim") << "NO process";
260  LogDebug("BscSim") << std::endl;
261 #endif
262 
263  currentHit = new BscG4Hit;
268 
275 
277 
280 
282  currentHit->setVx(Vx);
283  currentHit->setVy(Vy);
284  currentHit->setVz(Vz);
285 
286  currentHit->setX(X);
287  currentHit->setY(Y);
288  currentHit->setZ(Z);
289 
290  UpdateHit();
291 
293 }
294 
296 
297  if (Eloss > 0.) {
299 
300 #ifdef debug
301  LogDebug("BscSim") << "updateHit: add eloss " << Eloss <<std::endl;
302  LogDebug("BscSim") << "CurrentHit=" << currentHit
303  << ", PostStepPoint=" << postStepPoint->GetPosition();
304 #endif
305  //AZ
307  }
308 
309  // buffer for next steps:
310  tsID = tSliceID;
311  primID = primaryID;
313 }
314 
315 G4ThreeVector BscSD::SetToLocal(const G4ThreeVector& global){
316 
317  const G4VTouchable* touch= preStepPoint->GetTouchable();
318  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
319  return theEntryPoint;
320 }
321 
322 G4ThreeVector BscSD::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 BscSD::EndOfEvent(G4HCofThisEvent* ) {
331 
332  // here we loop over transient hits and make them persistent
333  for (int j=0; j<theHC->entries(); j++) {
334  //AZ:
335  BscG4Hit* aHit = (*theHC)[j];
336  LogDebug("BscSim") << "hit number" << j << "unit ID = "<<aHit->getUnitID()<< "\n";
337  LogDebug("BscSim") << "entry z " << aHit->getEntry().z()<< "\n";
338  LogDebug("BscSim") << "entr theta " << aHit->getThetaAtEntry()<< "\n";
339 
340  Local3DPoint locExitPoint(0,0,0);
341  Local3DPoint locEntryPoint(aHit->getEntry().x(),
342  aHit->getEntry().y(),
343  aHit->getEntry().z());
344  slave->processHits(PSimHit(locEntryPoint,locExitPoint,
345  aHit->getPabs(),
346  aHit->getTof(),
347  aHit->getEnergyLoss(),
348  aHit->getParticleType(),
349  aHit->getUnitID(),
350  aHit->getTrackID(),
351  aHit->getThetaAtEntry(),
352  aHit->getPhiAtEntry()));
353  }
354  Summarize();
355 }
356 
358 }
359 
360 void BscSD::clear() {
361 }
362 
364 }
365 
367  LogDebug("BscSim") << "BscSD: Collection " << theHC->GetName() << "\n";
368  theHC->PrintAllHits();
369 }
370 
372  if (slave->name() == hname) { cc=slave->hits(); }
373 }
374 
375 void BscSD::update (const BeginOfEvent * i) {
376  LogDebug("BscSim") << " Dispatched BeginOfEvent for " << GetName()
377  << " !" ;
378  clearHits();
379  eventno = (*i)()->GetEventID();
380 }
381 
382 void BscSD::update(const BeginOfRun *) {
383 
384  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
385  G4String particleName;
386  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
387  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
388  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
389 
390 }
391 
392 void BscSD::update (const ::EndOfEvent*) {
393 }
394 
396  //AZ:
397  slave->Initialize();
398 }
399 
#define LogDebug(id)
void setTof(float e)
Definition: BSCG4Hit.cc:154
T getParameter(std::string const &) const
G4int gammaPDG
Definition: BscSD.h:129
T getUntrackedParameter(std::string const &, T const &) const
G4int emPDG
Definition: BscSD.h:127
G4int epPDG
Definition: BscSD.h:128
void setEntry(const G4ThreeVector &xyz)
Definition: BSCG4Hit.cc:116
const double GeV
Definition: MathUtil.h:16
void Summarize()
Definition: BscSD.cc:357
uint32_t previousUnitID
Definition: BscSD.h:96
float getPhiAtEntry() const
Definition: BSCG4Hit.cc:159
void setVx(float p)
Definition: BSCG4Hit.cc:177
float Y
Definition: BscSD.h:118
TrackingSlaveSD * slave
Definition: BscSD.h:78
G4ThreeVector SetToLocal(const G4ThreeVector &global)
Definition: BscSD.cc:315
int getParticleType() const
Definition: BSCG4Hit.cc:151
float PhiAtEntry
Definition: BscSD.h:114
G4int getTrackID() const
Definition: BSCG4Hit.cc:133
G4bool HitExists()
Definition: BscSD.cc:173
float Vx
Definition: BscSD.h:117
float edeposit
Definition: BscSD.h:102
G4int tSliceID
Definition: BscSD.h:97
float Tof
Definition: BscSD.h:109
std::string name() const
void setTrackID(int i)
Definition: BSCG4Hit.cc:134
void setEnergyLoss(float e)
Definition: BSCG4Hit.cc:155
int ParentId
Definition: BscSD.h:116
void setParentId(int p)
Definition: BSCG4Hit.cc:174
void GetStepInfo(G4Step *aStep)
Definition: BscSD.cc:122
float X
Definition: BscSD.h:118
void CreateNewHit()
Definition: BscSD.cc:237
float edepositEM
Definition: BscSD.h:126
void setUnitID(unsigned int i)
Definition: BSCG4Hit.cc:137
float Eloss
Definition: BscSD.h:110
float getTof() const
Definition: BSCG4Hit.cc:149
float getPabs() const
Definition: BSCG4Hit.cc:148
G4int primID
Definition: BscSD.h:86
BscG4HitCollection * theHC
Definition: BscSD.h:89
void setVy(float p)
Definition: BSCG4Hit.cc:180
#define nullptr
void setY(float t)
Definition: BSCG4Hit.cc:168
G4ThreeVector hitPointLocalExit
Definition: BscSD.h:107
type of data representation of DDCompactView
Definition: DDCompactView.h:90
unsigned int getUnitID() const
Definition: BSCG4Hit.cc:136
G4int tsID
Definition: BscSD.h:92
static TrackerG4SimHitNumberingScheme & numberingScheme(const DDCompactView &cpv, const GeometricDet &det)
virtual unsigned int getUnitID(const G4Step *aStep) const
void UpdateHit()
Definition: BscSD.cc:295
int getTimeSliceID() const
Definition: BSCG4Hit.cc:141
G4VPhysicalVolume * currentPV
Definition: BscSD.h:95
void setThetaAtEntry(float t)
Definition: BSCG4Hit.cc:161
void setX(float t)
Definition: BSCG4Hit.cc:165
void setPabs(float e)
Definition: BSCG4Hit.cc:153
void setEntryLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:119
std::vector< PSimHit > & hits()
void update(const BeginOfRun *) override
This routine will be called when the appropriate signal arrives.
Definition: BscSD.cc:382
double getEnergyDeposit(const G4Step *step)
Definition: BscSD.cc:81
uint32_t unitID
Definition: BscSD.h:96
G4int primaryID
Definition: BscSD.h:97
std::string const collectionName[nCollections]
Definition: Collections.h:47
virtual void Initialize()
void setPhiAtEntry(float f)
Definition: BSCG4Hit.cc:162
G4StepPoint * preStepPoint
Definition: BscSD.h:100
float ThetaAtEntry
Definition: BscSD.h:113
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: BscSD.cc:101
G4int hcID
Definition: BscSD.h:88
float Z
Definition: BscSD.h:118
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: BscSD.cc:371
float edepositHAD
Definition: BscSD.h:126
G4ThreeVector hitPoint
Definition: BscSD.h:104
float getThetaAtEntry() const
Definition: BSCG4Hit.cc:158
void addEnergyDeposit(double em, double hd)
Definition: BSCG4Hit.cc:143
void PrintAll() override
Definition: BscSD.cc:366
void setTimeSlice(double d)
Definition: BSCG4Hit.cc:140
void setParticleType(short i)
Definition: BSCG4Hit.cc:156
void StoreHit(BscG4Hit *)
Definition: BscSD.cc:226
void setVz(float p)
Definition: BSCG4Hit.cc:183
G4ThreeVector theEntryPoint
Definition: BscSD.h:82
void clearHits() override
Definition: BscSD.cc:395
G4StepPoint * postStepPoint
Definition: BscSD.h:101
short ParticleType
Definition: BscSD.h:111
G4ThreeVector hitPointExit
Definition: BscSD.h:105
BscNumberingScheme * numberingScheme
Definition: BscSD.h:79
float incidentEnergy
Definition: BscSD.h:85
G4ThreeVector exitPoint
Definition: BscSD.h:81
G4ThreeVector theExitPoint
Definition: BscSD.h:83
G4ThreeVector SetToLocalExit(const G4ThreeVector &globalPoint)
Definition: BscSD.cc:322
G4Track * theTrack
Definition: BscSD.h:94
int eventno
Definition: BscSD.h:122
G4double tSlice
Definition: BscSD.h:98
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: BscSD.cc:330
G4ThreeVector getEntry() const
Definition: BSCG4Hit.cc:115
void setZ(float t)
Definition: BSCG4Hit.cc:171
G4ThreeVector entrancePoint
Definition: BscSD.h:81
void DrawAll() override
Definition: BscSD.cc:363
void clear() override
Definition: BscSD.cc:360
void Initialize(G4HCofThisEvent *HCE) override
Definition: BscSD.cc:85
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
~BscSD() override
Definition: BscSD.cc:72
void setExitLocalP(const G4ThreeVector &xyz1)
Definition: BSCG4Hit.cc:122
float Vz
Definition: BscSD.h:117
BscSD(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: BscSD.cc:45
G4THitsCollection< BscG4Hit > BscG4HitCollection
void setIncidentEnergy(double e)
Definition: BSCG4Hit.cc:131
float Pabs
Definition: BscSD.h:108
float Vy
Definition: BscSD.h:117
uint32_t setDetUnitId(const G4Step *) override
Definition: BscSD.cc:169
float getEnergyLoss() const
Definition: BSCG4Hit.cc:150
G4ThreeVector hitPointLocal
Definition: BscSD.h:106
BscG4Hit * currentHit
Definition: BscSD.h:93
void ResetForNewPrimary()
Definition: BscSD.cc:218