CMS 3D CMS Logo

FP420SD.cc
Go to the documentation of this file.
1 // File: FP420SD.cc
3 // Date: 02.2006
4 // Description: Sensitive Detector class for FP420
5 // Modifications:
7 
12 
16 
21 
25 
27 
28 #include "G4Track.hh"
29 #include "G4SDManager.hh"
30 #include "G4VProcess.hh"
31 #include "G4EventManager.hh"
32 #include "G4Step.hh"
33 #include "G4ParticleTable.hh"
34 
35 #include "G4SystemOfUnits.hh"
36 
37 #include <string>
38 #include <vector>
39 #include <iostream>
40 
42 
43 
44 //#define debug
45 //-------------------------------------------------------------------
47  const SensitiveDetectorCatalog & clg,
48  edm::ParameterSet const & p, const SimTrackManager* manager) :
49  SensitiveTkDetector(name, cpv, clg, p), numberingScheme(nullptr),
50  hcID(-1), theHC(nullptr), theManager(manager), currentHit(nullptr), theTrack(nullptr),
51  currentPV(nullptr), unitID(0), previousUnitID(0), preStepPoint(nullptr),
52  postStepPoint(nullptr), eventno(0){
53 
54  //Parameters
56  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
57  //int verbn = 1;
58 
59  SetVerboseLevel(verbn);
60 
61  slave = new TrackingSlaveSD(name);
62 
63  if (name == "FP420SI") {
64  if (verbn > 0) {
65  edm::LogInfo("FP420Sim") << "name = FP420SI and new FP420NumberingSchem";
66  }
68  } else {
69  edm::LogWarning("FP420Sim") << "FP420SD: ReadoutName not supported\n";
70  }
71 }
72 
74  delete slave;
75  delete numberingScheme;
76 }
77 
78 double FP420SD::getEnergyDeposit(G4Step* aStep) {
79  return aStep->GetTotalEnergyDeposit();
80 }
81 
82 void FP420SD::Initialize(G4HCofThisEvent * HCE) {
83 #ifdef debug
84  LogDebug("FP420Sim") << "FP420SD : Initialize called for " << name << std::endl;
85 #endif
86 
87  theHC = new FP420G4HitCollection(GetName(), collectionName[0]);
88  if (hcID<0)
89  hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
90  HCE->AddHitsCollection(hcID, theHC);
91 
92  tsID = -2;
93  // primID = -2;
94  primID = 0;
95 
97 }
98 
99 
100 bool FP420SD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
101 
102  if (aStep == nullptr) {
103  return true;
104  } else {
105  GetStepInfo(aStep);
106  // LogDebug("FP420Sim") << edeposit <<std::endl;
107 
108  //AZ
109 #ifdef debug
110  LogDebug("FP420Sim") << "FP420SD : number of hits = " << theHC->entries() << std::endl;
111 #endif
112 
113  if (HitExists() == false && edeposit>0. && theHC->entries()< 200 ){
114  CreateNewHit();
115  return true;
116  }
117  }
118  return true;
119 }
120 
121 void FP420SD::GetStepInfo(G4Step* aStep) {
122 
123  preStepPoint = aStep->GetPreStepPoint();
124  postStepPoint= aStep->GetPostStepPoint();
125  theTrack = aStep->GetTrack();
126  hitPoint = preStepPoint->GetPosition();
127  currentPV = preStepPoint->GetPhysicalVolume();
128  hitPointExit = postStepPoint->GetPosition();
129 
130  hitPointLocal = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
131  hitPointLocalExit = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPointExit);
132 
133 
134  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
135  if (particleCode == emPDG ||
136  particleCode == epPDG ||
137  particleCode == gammaPDG ) {
138  edepositEM = getEnergyDeposit(aStep); edepositHAD = 0.;
139  } else {
140  edepositEM = 0.; edepositHAD = getEnergyDeposit(aStep);
141  }
142  edeposit = aStep->GetTotalEnergyDeposit();
143  tSlice = (postStepPoint->GetGlobalTime() )/nanosecond;
144  tSliceID = (int) tSlice;
145  unitID = setDetUnitId(aStep);
146 #ifdef debug
147  LogDebug("FP420Sim") << "unitID=" << unitID <<std::endl;
148 #endif
149  primaryID = theTrack->GetTrackID();
150  // Position = hitPoint;
151  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag()/GeV;
152  //Tof = 1400. + aStep->GetPostStepPoint()->GetGlobalTime()/nanosecond;
153  Tof = aStep->GetPostStepPoint()->GetGlobalTime()/nanosecond;
154  Eloss = aStep->GetTotalEnergyDeposit()/GeV;
155  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
156  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta()/deg;
157  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi()/deg;
158 
159  ParentId = theTrack->GetParentID();
160  Vx = theTrack->GetVertexPosition().x();
161  Vy = theTrack->GetVertexPosition().y();
162  Vz = theTrack->GetVertexPosition().z();
163  X = hitPoint.x();
164  Y = hitPoint.y();
165  Z = hitPoint.z();
166 }
167 
168 uint32_t FP420SD::setDetUnitId(const G4Step * aStep) {
169 
170  return (numberingScheme == nullptr ? 0 : numberingScheme->getUnitID(aStep));
171 }
172 
174  if (primaryID<1) {
175  edm::LogWarning("FP420Sim") << "***** FP420SD 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("FP420Sim") << "FP420SD: HCollection= " << theHC->entries() <<std::endl;
197 
198  for (int j=0; j<theHC->entries()&&!found; j++) {
199  FP420G4Hit* 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 
218 
220 
223  incidentEnergy = preStepPoint->GetKineticEnergy();
224 
225 }
226 
227 
229 
230  // if (primID<0) return;
231  if (hit == nullptr ) {
232  edm::LogWarning("FP420Sim") << "FP420SD: hit to be stored is NULL !!";
233  return;
234  }
235 
236  theHC->insert( hit );
237 }
238 
239 
241 
242 #ifdef debug
243  // << " MVid = " << currentPV->GetMother()->GetCopyNo()
244  LogDebug("FP420Sim") << "FP420SD CreateNewHit for"
245  << " PV " << currentPV->GetName()
246  << " PVid = " << currentPV->GetCopyNo()
247  << " Unit " << unitID <<std::endl;
248  LogDebug("FP420Sim") << " primary " << primaryID
249  << " time slice " << tSliceID
250  << " For Track " << theTrack->GetTrackID()
251  << " which is a " << theTrack->GetDefinition()->GetParticleName();
252 
253  if (theTrack->GetTrackID()==1) {
254  LogDebug("FP420Sim") << " of energy " << theTrack->GetTotalEnergy();
255  } else {
256  LogDebug("FP420Sim") << " daughter of part. " << theTrack->GetParentID();
257  }
258 
259  LogDebug("FP420Sim") << " and created by " ;
260  if (theTrack->GetCreatorProcess()!=NULL)
261  LogDebug("FP420Sim") << theTrack->GetCreatorProcess()->GetProcessName() ;
262  else
263  LogDebug("FP420Sim") << "NO process";
264  LogDebug("FP420Sim") << std::endl;
265 #endif
266 
267 
268  currentHit = new FP420G4Hit;
273 
280 
281 // currentHit->setEntry(entrancePoint);
283 
286 
288  currentHit->setVx(Vx);
289  currentHit->setVy(Vy);
290  currentHit->setVz(Vz);
291 
292  currentHit->setX(X);
293  currentHit->setY(Y);
294  currentHit->setZ(Z);
295  //AZ:12.10.2007
296  // UpdateHit();
297  // buffer for next steps:
298  tsID = tSliceID;
299  primID = primaryID;
301 
303 }
304 
305 
307  //
308  if (Eloss > 0.) {
310 
311 #ifdef debug
312  LogDebug("FP420Sim") << "updateHit: add eloss " << Eloss <<std::endl;
313  LogDebug("FP420Sim") << "CurrentHit=" << currentHit
314  << ", PostStepPoint=" << postStepPoint->GetPosition() << std::endl;
315 #endif
316  //AZ
317  // currentHit->setEnergyLoss(Eloss);
319  }
320 
321 
322 
323  // buffer for next steps:
324  tsID = tSliceID;
325  primID = primaryID;
327 }
328 
329 
330 G4ThreeVector FP420SD::SetToLocal(const G4ThreeVector& global){
331 
332  const G4VTouchable* touch= preStepPoint->GetTouchable();
333  theEntryPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
334  return theEntryPoint;
335 }
336 
337 
338 G4ThreeVector FP420SD::SetToLocalExit(const G4ThreeVector& globalPoint){
339 
340  const G4VTouchable* touch= postStepPoint->GetTouchable();
341  theExitPoint = touch->GetHistory()->GetTopTransform().TransformPoint(globalPoint);
342  return theExitPoint;
343 }
344 
345 
346 void FP420SD::EndOfEvent(G4HCofThisEvent* ) {
347 
348  // here we loop over transient hits and make them persistent
349 
350  // if(theHC->entries() > 100){
351  // LogDebug("FP420Sim") << "FP420SD: warning!!! Number of hits exceed 100 and =" << theHC->entries() << "\n";
352  // }
353  // for (int j=0; j<theHC->entries() && j<100; j++) {
354  int nhitsHPS240=0;
355  int nhitsFP420=0;
356  for (int j=0; j<theHC->entries(); j++) {
357  FP420G4Hit* aHit = (*theHC)[j];
358  if((fabs(aHit->getTof())> 780. && fabs(aHit->getTof())< 840.)) ++nhitsHPS240;
359  if((fabs(aHit->getTof())>1380. && fabs(aHit->getTof())<1450.)) ++nhitsFP420;
360  // if(fabs(aHit->getTof()) < 1700.) {
361  if((fabs(aHit->getTof())>780. && fabs(aHit->getTof())<840. && nhitsHPS240<200.) || ( fabs(aHit->getTof())>1380. && fabs(aHit->getTof())<1450. && nhitsFP420<200.)) {
362 
363 
364 #ifdef ddebug
365  // LogDebug("FP420SD") << " FP420Hit " << j << " " << *aHit << std::endl;
366  LogDebug("FP420Sim") << "hit number" << j << "unit ID = "<<aHit->getUnitID()<< "\n";
367  LogDebug("FP420Sim") << "entry z " << aHit->getEntry().z()<< "\n";
368  LogDebug("FP420Sim") << "entr theta " << aHit->getThetaAtEntry()<< "\n";
369 #endif
370 
371  // Local3DPoint locExitPoint(0,0,0);
372  // Local3DPoint locEntryPoint(aHit->getEntry().x(),
373  // aHit->getEntry().y(),
374  // aHit->getEntry().z());
375  Local3DPoint locExitPoint(aHit->getExitLocalP().x(),
376  aHit->getExitLocalP().y(),
377  aHit->getExitLocalP().z());
378  Local3DPoint locEntryPoint(aHit->getEntryLocalP().x(),
379  aHit->getEntryLocalP().y(),
380  aHit->getEntryLocalP().z());
381  // implicit conversion (slicing) to PSimHit!!!
382  slave->processHits(PSimHit(locEntryPoint,locExitPoint,//entryPoint(), exitPoint() Local3DPoint
383  aHit->getPabs(),// pabs() float
384  aHit->getTof(), // tof() float
385  aHit->getEnergyLoss(), // energyLoss() float
386  aHit->getParticleType(),// particleType() int
387  aHit->getUnitID(), // detUnitId() unsigned int
388  aHit->getTrackID(),// trackId() unsigned int
389  aHit->getThetaAtEntry(),// thetaAtEntry() float
390  aHit->getPhiAtEntry())); // phiAtEntry() float
391 
392  //PSimHit( const Local3DPoint& entry, const Local3DPoint& exit,
393  // float pabs, float tof, float eloss, int particleType,
394  // unsigned int detId, unsigned int trackId,
395  // float theta, float phi, unsigned short processType=0) :
396 
397  // LocalVector direction = hit.exitPoint() - hit.entryPoint();
398  //hit.energyLoss
399 
400 
401  /*
402  aHit->getEM(), -
403  aHit->getHadr(), -
404  aHit->getIncidentEnergy(), -
405  aHit->getTimeSlice(), -
406  aHit->getEntry(), -
407  aHit->getParentId(),
408  aHit->getEntryLocalP(), -
409  aHit->getExitLocalP(), -
410  aHit->getX(), -
411  aHit->getY(), -
412  aHit->getZ(), -
413  aHit->getVx(), -
414  aHit->getVy(), -
415  aHit->getVz())); -
416  */
417  }//if Tof<1600. if nhits<100
418  } // for loop on hits
419 
420  Summarize();
421 }
422 
423 
425 }
426 
427 
429 }
430 
431 
433 }
434 
435 
437  LogDebug("FP420Sim") << "FP420SD: Collection " << theHC->GetName() << "\n";
438  theHC->PrintAllHits();
439 }
440 
441 
442 //void FP420SD::SetNumberingScheme(FP420NumberingScheme* scheme){
443 //
444 // if (numberingScheme)
445 // delete numberingScheme;
446 // numberingScheme = scheme;
447 //
448 //}
449 
451  if (slave->name() == hname) { cc=slave->hits(); }
452 }
453 
455  LogDebug("ForwardSim") << " Dispatched BeginOfEvent for " << GetName()
456  << " !" ;
457  clearHits();
458  eventno = (*i)()->GetEventID();
459 }
460 
462 
463  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
464  G4String particleName;
465  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
466  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
467  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
468 
469 }
470 
471 void FP420SD::update (const ::EndOfEvent*) {
472 }
473 
475  //AZ:
476  slave->Initialize();
477 }
478 
#define LogDebug(id)
int eventno
Definition: FP420SD.h:133
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
G4ThreeVector theExitPoint
Definition: FP420SD.h:90
G4int emPDG
Definition: FP420SD.h:138
void setY(float t)
Definition: FP420G4Hit.cc:169
const double GeV
Definition: MathUtil.h:16
G4ThreeVector SetToLocalExit(const G4ThreeVector &globalPoint)
Definition: FP420SD.cc:338
void setPhiAtEntry(float f)
Definition: FP420G4Hit.cc:163
float getTof() const
Definition: FP420G4Hit.cc:149
void DrawAll() override
Definition: FP420SD.cc:432
float ThetaAtEntry
Definition: FP420SD.h:122
void setVz(float p)
Definition: FP420G4Hit.cc:184
void setEntry(const G4ThreeVector &xyz)
Definition: FP420G4Hit.cc:116
virtual double getEnergyDeposit(G4Step *step)
Definition: FP420SD.cc:78
G4ThreeVector hitPointExit
Definition: FP420SD.h:114
std::string name() const
G4ThreeVector getExitLocalP() const
Definition: FP420G4Hit.cc:121
void setZ(float t)
Definition: FP420G4Hit.cc:172
float Pabs
Definition: FP420SD.h:117
void setPabs(float e)
Definition: FP420G4Hit.cc:153
TrackingSlaveSD * slave
Definition: FP420SD.h:85
#define NULL
Definition: scimark2.h:8
G4StepPoint * preStepPoint
Definition: FP420SD.h:109
void ResetForNewPrimary()
Definition: FP420SD.cc:219
float getPabs() const
Definition: FP420G4Hit.cc:148
#define nullptr
type of data representation of DDCompactView
Definition: DDCompactView.h:90
void setVx(float p)
Definition: FP420G4Hit.cc:178
G4int hcID
Definition: FP420SD.h:94
void setTrackID(int i)
Definition: FP420G4Hit.cc:134
unsigned int getTrackID() const
Definition: FP420G4Hit.cc:133
void addEnergyDeposit(double em, double hd)
Definition: FP420G4Hit.cc:143
G4int tsID
Definition: FP420SD.h:98
static TrackerG4SimHitNumberingScheme & numberingScheme(const DDCompactView &cpv, const GeometricDet &det)
unsigned int primaryID
Definition: FP420SD.h:105
float Eloss
Definition: FP420SD.h:119
unsigned int primID
Definition: FP420SD.h:105
float getPhiAtEntry() const
Definition: FP420G4Hit.cc:160
G4ThreeVector exitPoint
Definition: FP420SD.h:88
float edeposit
Definition: FP420SD.h:111
unsigned int getUnitID() const
Definition: FP420G4Hit.cc:136
void setEnergyLoss(float e)
Definition: FP420G4Hit.cc:156
std::vector< PSimHit > & hits()
void clearHits() override
Definition: FP420SD.cc:474
std::string const collectionName[nCollections]
Definition: Collections.h:47
G4ThreeVector theEntryPoint
Definition: FP420SD.h:89
void setTof(float e)
Definition: FP420G4Hit.cc:154
virtual void Initialize()
float edepositHAD
Definition: FP420SD.h:137
float Vz
Definition: FP420SD.h:126
void GetStepInfo(G4Step *aStep)
Definition: FP420SD.cc:121
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: FP420SD.cc:450
G4int gammaPDG
Definition: FP420SD.h:140
FP420G4HitCollection * theHC
Definition: FP420SD.h:95
void CreateNewHit()
Definition: FP420SD.cc:240
G4Track * theTrack
Definition: FP420SD.h:100
void Summarize()
Definition: FP420SD.cc:424
G4int tSliceID
Definition: FP420SD.h:104
G4ThreeVector getEntryLocalP() const
Definition: FP420G4Hit.cc:118
G4bool HitExists()
Definition: FP420SD.cc:173
void update(const BeginOfRun *) override
This routine will be called when the appropriate signal arrives.
Definition: FP420SD.cc:461
void setTimeSlice(double d)
Definition: FP420G4Hit.cc:140
virtual unsigned int getUnitID(const G4Step *aStep) const
float getEnergyLoss() const
Definition: FP420G4Hit.cc:150
void setEntryLocalP(const G4ThreeVector &xyz1)
Definition: FP420G4Hit.cc:119
uint32_t setDetUnitId(const G4Step *) override
Definition: FP420SD.cc:168
G4THitsCollection< FP420G4Hit > FP420G4HitCollection
void setIncidentEnergy(double e)
Definition: FP420G4Hit.cc:131
uint32_t previousUnitID
Definition: FP420SD.h:103
int getTimeSliceID() const
Definition: FP420G4Hit.cc:141
G4VPhysicalVolume * currentPV
Definition: FP420SD.h:101
void Initialize(G4HCofThisEvent *HCE) override
Definition: FP420SD.cc:82
FP420SD(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: FP420SD.cc:46
void setX(float t)
Definition: FP420G4Hit.cc:166
float Tof
Definition: FP420SD.h:118
void setExitLocalP(const G4ThreeVector &xyz1)
Definition: FP420G4Hit.cc:122
void clear() override
Definition: FP420SD.cc:428
int getParticleType() const
Definition: FP420G4Hit.cc:151
uint32_t unitID
Definition: FP420SD.h:103
float PhiAtEntry
Definition: FP420SD.h:123
float edepositEM
Definition: FP420SD.h:137
short ParticleType
Definition: FP420SD.h:120
void setParticleType(short i)
Definition: FP420G4Hit.cc:157
void setThetaAtEntry(float t)
Definition: FP420G4Hit.cc:162
G4ThreeVector hitPointLocalExit
Definition: FP420SD.h:116
FP420NumberingScheme * numberingScheme
Definition: FP420SD.h:86
G4double tSlice
Definition: FP420SD.h:107
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: FP420SD.cc:100
void PrintAll() override
Definition: FP420SD.cc:436
G4ThreeVector SetToLocal(const G4ThreeVector &global)
Definition: FP420SD.cc:330
float incidentEnergy
Definition: FP420SD.h:92
void setUnitID(unsigned int i)
Definition: FP420G4Hit.cc:137
int ParentId
Definition: FP420SD.h:125
G4ThreeVector entrancePoint
Definition: FP420SD.h:88
float Z
Definition: FP420SD.h:127
float Vy
Definition: FP420SD.h:126
virtual bool processHits(const PSimHit &)
void UpdateHit()
Definition: FP420SD.cc:306
std::vector< PSimHit > PSimHitContainer
G4ThreeVector getEntry() const
Definition: FP420G4Hit.cc:115
~FP420SD() override
Definition: FP420SD.cc:73
G4int epPDG
Definition: FP420SD.h:139
G4StepPoint * postStepPoint
Definition: FP420SD.h:110
void setParentId(int p)
Definition: FP420G4Hit.cc:175
float X
Definition: FP420SD.h:127
FP420G4Hit * currentHit
Definition: FP420SD.h:99
G4ThreeVector hitPointLocal
Definition: FP420SD.h:115
G4ThreeVector hitPoint
Definition: FP420SD.h:113
void addEnergyLoss(float e)
Definition: FP420G4Hit.cc:155
float getThetaAtEntry() const
Definition: FP420G4Hit.cc:159
float Y
Definition: FP420SD.h:127
void StoreHit(FP420G4Hit *)
Definition: FP420SD.cc:228
void setVy(float p)
Definition: FP420G4Hit.cc:181
float Vx
Definition: FP420SD.h:126
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: FP420SD.cc:346