CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
8 //#include "Geometry/Vector/interface/LocalPoint.h"
9 //#include "Geometry/Vector/interface/LocalVector.h"
10 
14 
17 
18 
23 
27 
29 
30 #include "G4Track.hh"
31 #include "G4SDManager.hh"
32 #include "G4VProcess.hh"
33 #include "G4EventManager.hh"
34 #include "G4Step.hh"
35 #include "G4ParticleTable.hh"
36 
37 #include <string>
38 #include <vector>
39 #include <iostream>
40 
42 
43 #define debug
44 //-------------------------------------------------------------------
45 BscSD::BscSD(std::string name, const DDCompactView & cpv,
47  edm::ParameterSet const & p, const SimTrackManager* manager) :
48  SensitiveTkDetector(name, cpv, clg, p), numberingScheme(0), name(name),
49  hcID(-1), theHC(0), theManager(manager), currentHit(0), theTrack(0),
50  currentPV(0), unitID(0), previousUnitID(0), preStepPoint(0),
51  postStepPoint(0), eventno(0){
52 
53  //Add Bsc Sentitive Detector Name
54  collectionName.insert(name);
55 
56 
57  //Parameters
59  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
60  //int verbn = 1;
61 
62  SetVerboseLevel(verbn);
63  LogDebug("BscSim")
64  << "*******************************************************\n"
65  << "* *\n"
66  << "* Constructing a BscSD with name " << name << "\n"
67  << "* *\n"
68  << "*******************************************************";
69 
70 
71  slave = new TrackingSlaveSD(name);
72 
73  //
74  // attach detectors (LogicalVolumes)
75  //
76  std::vector<std::string> lvNames = clg.logicalNames(name);
77 
78  this->Register();
79 
80  for (std::vector<std::string>::iterator it=lvNames.begin();
81  it !=lvNames.end(); it++) {
82  this->AssignSD(*it);
83  edm::LogInfo("BscSim") << "BscSD : Assigns SD to LV " << (*it);
84  }
85 
86  if (name == "BSCHits") {
87  if (verbn > 0) {
88  edm::LogInfo("BscSim") << "name = BSCHits and new BscNumberingSchem";
89  }
91  } else {
92  edm::LogWarning("BscSim") << "BscSD: ReadoutName "<<name<<" not supported";
93  }
94 
95  edm::LogInfo("BscSim") << "BscSD: Instantiation completed";
96 }
97 
98 
99 
100 
102  //AZ:
103  if (slave) delete slave;
104 
105  if (numberingScheme)
106  delete numberingScheme;
107 
108 }
109 
110 double BscSD::getEnergyDeposit(G4Step* aStep) {
111  return aStep->GetTotalEnergyDeposit();
112 }
113 
114 void BscSD::Initialize(G4HCofThisEvent * HCE) {
115 #ifdef debug
116  LogDebug("BscSim") << "BscSD : Initialize called for " << name << std::endl;
117 #endif
118 
120  if (hcID<0)
121  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
122  HCE->AddHitsCollection(hcID, theHC);
123 
124  tsID = -2;
125  primID = -2;
126 
128 }
129 
130 
131 bool BscSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
132 
133  if (aStep == NULL) {
134  return true;
135  } else {
136  GetStepInfo(aStep);
137  // LogDebug("BscSim") << edeposit <<std::endl;
138 
139  //AZ
140 #ifdef debug
141  LogDebug("BscSim") << "BscSD : number of hits = " << theHC->entries() << std::endl;
142 #endif
143 
144  if (HitExists() == false && edeposit>0. ){
145  CreateNewHit();
146  return true;
147  }
148  }
149  return true;
150 }
151 
152 void BscSD::GetStepInfo(G4Step* aStep) {
153 
154  preStepPoint = aStep->GetPreStepPoint();
155  postStepPoint= aStep->GetPostStepPoint();
156  theTrack = aStep->GetTrack();
157  hitPoint = preStepPoint->GetPosition();
158  currentPV = preStepPoint->GetPhysicalVolume();
159  hitPointExit = postStepPoint->GetPosition();
160 
161  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
162  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
163 
164 
165  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
166  LogDebug("BscSim") << " BscSD :particleType = " << theTrack->GetDefinition()->GetParticleName() <<std::endl;
167  if (particleCode == emPDG ||
168  particleCode == epPDG ||
169  particleCode == gammaPDG ) {
170  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
171  } else {
172  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
173  }
174  edeposit = aStep->GetTotalEnergyDeposit();
175  tSlice = (postStepPoint->GetGlobalTime() )/nanosecond;
176  tSliceID = (int) tSlice;
177  unitID = setDetUnitId(aStep);
178 #ifdef debug
179  LogDebug("BscSim") << "unitID=" << unitID <<std::endl;
180 #endif
181  primaryID = theTrack->GetTrackID();
182  // Position = hitPoint;
183  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/GeV;
184  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/nanosecond;
185  Eloss = aStep->GetTotalEnergyDeposit()/GeV;
186  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
187  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/deg;
188  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/deg;
189 
190  ParentId = theTrack->GetParentID();
191  Vx = theTrack->GetVertexPosition().x();
192  Vy = theTrack->GetVertexPosition().y();
193  Vz = theTrack->GetVertexPosition().z();
194  X = hitPoint.x();
195  Y = hitPoint.y();
196  Z = hitPoint.z();
197 }
198 
199 uint32_t BscSD::setDetUnitId(G4Step * aStep) {
200 
201  return (numberingScheme == 0 ? 0 : numberingScheme->getUnitID(aStep));
202 }
203 
204 
206  if (primaryID<1) {
207  edm::LogWarning("BscSim") << "***** BscSD 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 (primaryID == primID && tSliceID == tsID && unitID==previousUnitID) {
214  if (tSliceID == tsID && unitID==previousUnitID) {
215  //AZ:
216  UpdateHit();
217  return true;
218  }
219  // Reset entry point for new primary
220  if (primaryID != primID)
222 
223  //look in the HitContainer whether a hit with the same primID, unitID,
224  //tSliceID already exists:
225 
226  G4bool found = false;
227 
228  // LogDebug("BscSim") << "BscSD: HCollection= " << theHC->entries() <<std::endl;
229 
230  for (int j=0; j<theHC->entries()&&!found; j++) {
231  BscG4Hit* aPreviousHit = (*theHC)[j];
232  if (aPreviousHit->getTrackID() == primaryID &&
233  aPreviousHit->getTimeSliceID() == tSliceID &&
234  aPreviousHit->getUnitID() == unitID ) {
235  //AZ:
236  currentHit = aPreviousHit;
237  found = true;
238  }
239  }
240 
241  if (found) {
242  //AZ:
243  UpdateHit();
244  return true;
245  } else {
246  return false;
247  }
248 }
249 
250 
252 
255  incidentEnergy = preStepPoint->GetKineticEnergy();
256 
257 }
258 
259 
261 
262  if (primID<0) return;
263  if (hit == 0 ) {
264  edm::LogWarning("BscSim") << "BscSD: hit to be stored is NULL !!";
265  return;
266  }
267 
268  theHC->insert( hit );
269 }
270 
271 
273 
274 #ifdef debug
275  LogDebug("BscSim") << "BscSD CreateNewHit for"
276  << " PV " << currentPV->GetName()
277  << " PVid = " << currentPV->GetCopyNo()
278  << " Unit " << unitID <<std::endl;
279  LogDebug("BscSim") << " primary " << primaryID
280  << " time slice " << tSliceID
281  << " For Track " << theTrack->GetTrackID()
282  << " which is a " << theTrack->GetDefinition()->GetParticleName();
283 
284  if (theTrack->GetTrackID()==1) {
285  LogDebug("BscSim") << " of energy " << theTrack->GetTotalEnergy();
286  } else {
287  LogDebug("BscSim") << " daughter of part. " << theTrack->GetParentID();
288  }
289 
290  LogDebug("BscSim") << " and created by " ;
291  if (theTrack->GetCreatorProcess()!=NULL)
292  LogDebug("BscSim") << theTrack->GetCreatorProcess()->GetProcessName() ;
293  else
294  LogDebug("BscSim") << "NO process";
295  LogDebug("BscSim") << std::endl;
296 #endif
297 
298  currentHit = new BscG4Hit;
303 
310 
312 
315 
317  currentHit->setVx(Vx);
318  currentHit->setVy(Vy);
319  currentHit->setVz(Vz);
320 
321  currentHit->setX(X);
322  currentHit->setY(Y);
323  currentHit->setZ(Z);
324 
325  UpdateHit();
326 
328 }
329 
330 
332 
333  if (Eloss > 0.) {
335 
336 #ifdef debug
337  LogDebug("BscSim") << "updateHit: add eloss " << Eloss <<std::endl;
338  LogDebug("BscSim") << "CurrentHit=" << currentHit
339  << ", PostStepPoint=" << postStepPoint->GetPosition();
340 #endif
341  //AZ
343  }
344 
345  // buffer for next steps:
346  tsID = tSliceID;
347  primID = primaryID;
349 }
350 
351 
352 G4ThreeVector BscSD::SetToLocal(G4ThreeVector global){
353 
354  const G4VTouchable* touch= preStepPoint->GetTouchable();
355  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
356  return theEntryPoint;
357 }
358 
359 
360 G4ThreeVector BscSD::SetToLocalExit(G4ThreeVector globalPoint){
361 
362  const G4VTouchable* touch= postStepPoint->GetTouchable();
363  theExitPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
364  return theExitPoint;
365 }
366 
367 
368 void BscSD::EndOfEvent(G4HCofThisEvent* ) {
369 
370  // here we loop over transient hits and make them persistent
371  for (int j=0; j<theHC->entries(); j++) {
372  //AZ:
373  BscG4Hit* aHit = (*theHC)[j];
374  LogDebug("BscSim") << "hit number" << j << "unit ID = "<<aHit->getUnitID()<< "\n";
375  LogDebug("BscSim") << "entry z " << aHit->getEntry().z()<< "\n";
376  LogDebug("BscSim") << "entr theta " << aHit->getThetaAtEntry()<< "\n";
377 
378  Local3DPoint locExitPoint(0,0,0);
379  Local3DPoint locEntryPoint(aHit->getEntry().x(),
380  aHit->getEntry().y(),
381  aHit->getEntry().z());
382  slave->processHits(PSimHit(locEntryPoint,locExitPoint,
383  aHit->getPabs(),
384  aHit->getTof(),
385  aHit->getEnergyLoss(),
386  aHit->getParticleType(),
387  aHit->getUnitID(),
388  aHit->getTrackID(),
389  aHit->getThetaAtEntry(),
390  aHit->getPhiAtEntry()));
391  }
392  Summarize();
393 }
394 
395 
397 }
398 
399 
400 void BscSD::clear() {
401 }
402 
403 
405 }
406 
407 
409  LogDebug("BscSim") << "BscSD: Collection " << theHC->GetName() << "\n";
410  theHC->PrintAllHits();
411 }
412 
413 
415  if (slave->name() == n) c=slave->hits();
416 }
417 
418 void BscSD::update (const BeginOfEvent * i) {
419  LogDebug("BscSim") << " Dispatched BeginOfEvent for " << GetName()
420  << " !" ;
421  clearHits();
422  eventno = (*i)()->GetEventID();
423 }
424 
425 void BscSD::update(const BeginOfRun *) {
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 BscSD::update (const ::EndOfEvent*) {
436 }
437 
439  //AZ:
440  slave->Initialize();
441 }
442 
443 std::vector<std::string> BscSD::getNames(){
444  std::vector<std::string> temp;
445  temp.push_back(slave->name());
446  return temp;
447 }
448 
#define LogDebug(id)
void setTof(float e)
Definition: BSCG4Hit.cc:154
T getParameter(std::string const &) const
G4int gammaPDG
Definition: BscSD.h:167
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
G4int emPDG
Definition: BscSD.h:165
G4ThreeVector SetToLocal(G4ThreeVector global)
Definition: BscSD.cc:352
void fillHits(edm::PSimHitContainer &, std::string use)
Definition: BscSD.cc:414
virtual ~BscSD()
Definition: BscSD.cc:101
std::vector< std::string > logicalNames(std::string &readoutName)
G4int epPDG
Definition: BscSD.h:166
void Summarize()
Definition: BscSD.cc:396
uint32_t previousUnitID
Definition: BscSD.h:131
float getPhiAtEntry() const
Definition: BSCG4Hit.cc:159
void setVx(float p)
Definition: BSCG4Hit.cc:177
float Y
Definition: BscSD.h:154
TrackingSlaveSD * slave
Definition: BscSD.h:110
int getParticleType() const
Definition: BSCG4Hit.cc:151
float PhiAtEntry
Definition: BscSD.h:150
void setEntry(G4ThreeVector xyz)
Definition: BSCG4Hit.cc:116
G4int getTrackID() const
Definition: BSCG4Hit.cc:133
G4bool HitExists()
Definition: BscSD.cc:205
float Vx
Definition: BscSD.h:153
float edeposit
Definition: BscSD.h:137
G4int tSliceID
Definition: BscSD.h:132
float Tof
Definition: BscSD.h:145
std::string name() const
virtual double getEnergyDeposit(G4Step *step)
Definition: BscSD.cc:110
void setTrackID(int i)
Definition: BSCG4Hit.cc:134
void setEnergyLoss(float e)
Definition: BSCG4Hit.cc:155
int ParentId
Definition: BscSD.h:152
void setParentId(int p)
Definition: BSCG4Hit.cc:174
void GetStepInfo(G4Step *aStep)
Definition: BscSD.cc:152
float X
Definition: BscSD.h:154
void CreateNewHit()
Definition: BscSD.cc:272
virtual bool ProcessHits(G4Step *, G4TouchableHistory *)
Definition: BscSD.cc:131
float edepositEM
Definition: BscSD.h:164
#define NULL
Definition: scimark2.h:8
void setUnitID(unsigned int i)
Definition: BSCG4Hit.cc:137
float Eloss
Definition: BscSD.h:146
float getTof() const
Definition: BSCG4Hit.cc:149
float getPabs() const
Definition: BSCG4Hit.cc:148
G4int primID
Definition: BscSD.h:118
BscG4HitCollection * theHC
Definition: BscSD.h:123
void setVy(float p)
Definition: BSCG4Hit.cc:180
void setY(float t)
Definition: BSCG4Hit.cc:168
G4ThreeVector hitPointLocalExit
Definition: BscSD.h:143
type of data representation of DDCompactView
Definition: DDCompactView.h:77
unsigned int getUnitID() const
Definition: BSCG4Hit.cc:136
virtual void Initialize(G4HCofThisEvent *HCE)
Definition: BscSD.cc:114
G4int tsID
Definition: BscSD.h:126
static TrackerG4SimHitNumberingScheme & numberingScheme(const DDCompactView &cpv, const GeometricDet &det)
virtual unsigned int getUnitID(const G4Step *aStep) const
virtual uint32_t setDetUnitId(G4Step *)
Definition: BscSD.cc:199
void UpdateHit()
Definition: BscSD.cc:331
int getTimeSliceID() const
Definition: BSCG4Hit.cc:141
G4VPhysicalVolume * currentPV
Definition: BscSD.h:129
void setThetaAtEntry(float t)
Definition: BSCG4Hit.cc:161
void setX(float t)
Definition: BSCG4Hit.cc:165
void setPabs(float e)
Definition: BSCG4Hit.cc:153
std::vector< PSimHit > & hits()
uint32_t unitID
Definition: BscSD.h:131
void setEntryLocalP(G4ThreeVector xyz1)
Definition: BSCG4Hit.cc:119
G4int primaryID
Definition: BscSD.h:132
std::string const collectionName[nCollections]
Definition: Collections.h:39
virtual void Initialize()
void setPhiAtEntry(float f)
Definition: BSCG4Hit.cc:162
G4ThreeVector SetToLocalExit(G4ThreeVector globalPoint)
Definition: BscSD.cc:360
G4StepPoint * preStepPoint
Definition: BscSD.h:135
virtual void clearHits()
Definition: BscSD.cc:438
std::string name
Definition: BscSD.h:121
float ThetaAtEntry
Definition: BscSD.h:149
int j
Definition: DBlmapReader.cc:9
G4int hcID
Definition: BscSD.h:122
float Z
Definition: BscSD.h:154
float edepositHAD
Definition: BscSD.h:164
virtual void DrawAll()
Definition: BscSD.cc:404
std::vector< std::string > getNames()
Definition: BscSD.cc:443
G4ThreeVector hitPoint
Definition: BscSD.h:139
float getThetaAtEntry() const
Definition: BSCG4Hit.cc:158
BscSD(std::string, const DDCompactView &, SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: BscSD.cc:45
virtual void clear()
Definition: BscSD.cc:400
void addEnergyDeposit(double em, double hd)
Definition: BSCG4Hit.cc:143
void setTimeSlice(double d)
Definition: BSCG4Hit.cc:140
void setParticleType(short i)
Definition: BSCG4Hit.cc:156
void StoreHit(BscG4Hit *)
Definition: BscSD.cc:260
void setVz(float p)
Definition: BSCG4Hit.cc:183
G4ThreeVector theEntryPoint
Definition: BscSD.h:114
G4StepPoint * postStepPoint
Definition: BscSD.h:136
short ParticleType
Definition: BscSD.h:147
G4ThreeVector hitPointExit
Definition: BscSD.h:141
BscNumberingScheme * numberingScheme
Definition: BscSD.h:111
float incidentEnergy
Definition: BscSD.h:117
G4ThreeVector exitPoint
Definition: BscSD.h:113
G4ThreeVector theExitPoint
Definition: BscSD.h:115
G4Track * theTrack
Definition: BscSD.h:128
virtual void AssignSD(std::string &vname)
int eventno
Definition: BscSD.h:160
G4double tSlice
Definition: BscSD.h:133
G4ThreeVector getEntry() const
Definition: BSCG4Hit.cc:115
void setZ(float t)
Definition: BSCG4Hit.cc:171
virtual void PrintAll()
Definition: BscSD.cc:408
G4ThreeVector entrancePoint
Definition: BscSD.h:113
void update(const BeginOfRun *)
This routine will be called when the appropriate signal arrives.
Definition: BscSD.cc:425
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
float Vz
Definition: BscSD.h:153
G4THitsCollection< BscG4Hit > BscG4HitCollection
void setIncidentEnergy(double e)
Definition: BSCG4Hit.cc:131
virtual void EndOfEvent(G4HCofThisEvent *eventHC)
Definition: BscSD.cc:368
float Pabs
Definition: BscSD.h:144
float Vy
Definition: BscSD.h:153
float getEnergyLoss() const
Definition: BSCG4Hit.cc:150
G4ThreeVector hitPointLocal
Definition: BscSD.h:142
BscG4Hit * currentHit
Definition: BscSD.h:127
void ResetForNewPrimary()
Definition: BscSD.cc:251
void setExitLocalP(G4ThreeVector xyz1)
Definition: BSCG4Hit.cc:122