CMS 3D CMS Logo

PPSPixelSD.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PPS
4 // Class : PPSPixelSD
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: F.Ferro
10 // Created: May 4, 2015
11 //
12 
13 // system include files
14 
15 // user include files
18 
22 
25 
28 
29 #include "G4SDManager.hh"
30 #include "G4Step.hh"
31 #include "G4Track.hh"
32 #include "G4VProcess.hh"
33 
34 #include "G4PhysicalConstants.hh"
35 #include "G4SystemOfUnits.hh"
36 
38  const edm::EventSetup& es,
39  const SensitiveDetectorCatalog& clg,
40  edm::ParameterSet const& p,
41  SimTrackManager const* manager)
42  : SensitiveTkDetector(name_, es, clg, p),
43  numberingScheme_(nullptr),
44  hcID_(-1),
45  theHC_(nullptr),
46  theManager_(manager),
47  currentHit_(nullptr),
48  theTrack_(nullptr),
49  currentPV_(nullptr),
50  unitID_(0),
51  previousUnitID_(0),
52  preStepPoint_(nullptr),
53  postStepPoint_(nullptr),
54  eventno_(0) {
55  //Add PPS Sentitive Detector Names
56  collectionName.insert(name_);
57 
58  //Parameters
59  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("PPSPixelSD");
60  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
61  SetVerboseLevel(verbn);
62  slave_ = std::make_unique<TrackingSlaveSD>(name_);
63  if (name_ == "CTPPSPixelHits") {
64  numberingScheme_ = std::make_unique<PPSPixelNumberingScheme>();
65  } else {
66  edm::LogWarning("PPSSim") << "PPSPixelSD: ReadoutName not supported\n";
67  }
68 
69  edm::LogInfo("PPSSim") << "PPSPixelSD: Instantiation completed";
70 }
71 
73 
74 bool PPSPixelSD::ProcessHits(G4Step* aStep, G4TouchableHistory*) {
75  if (!aStep)
76  return true;
77 
78  stepInfo(aStep);
79  if (!hitExists() && edeposit_ > 0.)
80  createNewHit();
81  return true;
82 }
83 
84 uint32_t PPSPixelSD::setDetUnitId(const G4Step* aStep) {
85  return (numberingScheme_ == nullptr ? 0 : numberingScheme_->unitID(aStep));
86 }
87 
88 void PPSPixelSD::Initialize(G4HCofThisEvent* HCE) {
89  LogDebug("PPSSim") << "PPSPixelSD : Initialize called for " << name_;
90 
91  theHC_ = new PPSPixelG4HitCollection(GetName(), collectionName[0]);
92  G4SDManager::GetSDMpointer()->AddNewCollection(name_, collectionName[0]);
93 
94  if (hcID_ < 0)
95  hcID_ = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
96  HCE->AddHitsCollection(hcID_, theHC_);
97 
98  tsID_ = -2;
99  primID_ = -2;
100 }
101 
102 void PPSPixelSD::EndOfEvent(G4HCofThisEvent*) {
103  // here we loop over transient hits and make them persistent
104  for (unsigned int j = 0; j < (unsigned int)theHC_->entries() && j < maxPixelHits_; j++) {
105  PPSPixelG4Hit* aHit = (*theHC_)[j];
106 #ifdef debug
107  LogDebug("PPSSim") << "HIT NUMERO " << j << "unit ID = " << aHit->unitID() << "\n"
108  << " "
109  << "enrty z " << aHit->entry().z() << "\n"
110  << " "
111  << "theta " << aHit->thetaAtEntry() << "\n";
112 #endif
113 
114  Local3DPoint Enter(aHit->entryPoint().x(), aHit->entryPoint().y(), aHit->entryPoint().z());
115  Local3DPoint Exit(aHit->exitPoint().x(), aHit->exitPoint().y(), aHit->exitPoint().z());
116  slave_->processHits(PSimHit(Enter,
117  Exit,
118  aHit->p(),
119  aHit->tof(),
120  aHit->energyLoss(),
121  aHit->particleType(),
122  aHit->unitID(),
123  aHit->trackID(),
124  aHit->thetaAtEntry(),
125  aHit->phiAtEntry()));
126  }
127  summarize();
128 }
129 
131 
133 
135  LogDebug("PPSSim") << "PPSPixelSD: Collection " << theHC_->GetName();
136  theHC_->PrintAllHits();
137 }
138 
140  if (slave_->name() == n) {
141  c = slave_->hits();
142  }
143 }
144 
146  LogDebug("PPSSim") << " Dispatched BeginOfEvent for " << GetName() << " !";
147  clearHits();
148  eventno_ = (*i)()->GetEventID();
149 }
150 
151 void PPSPixelSD::update(const ::EndOfEvent*) {}
152 
153 void PPSPixelSD::clearHits() { slave_->Initialize(); }
154 
155 G4ThreeVector PPSPixelSD::setToLocal(const G4ThreeVector& global) {
156  G4ThreeVector localPoint;
157  const G4VTouchable* touch = preStepPoint_->GetTouchable();
158  localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
159  return localPoint;
160 }
161 
162 void PPSPixelSD::stepInfo(const G4Step* aStep) {
163  preStepPoint_ = aStep->GetPreStepPoint();
164  postStepPoint_ = aStep->GetPostStepPoint();
165  theTrack_ = aStep->GetTrack();
168 
169 #ifdef _PRINT_HITS_
170  LogDebug("PPSSim") << "theEntryPoint_ " << TheEntryPoint << "\n";
171  LogDebug("PPSSim") << "position " << preStepPoint_->GetPosition() << "\n";
172 #endif
173  hitPoint_ = preStepPoint_->GetPosition();
174  currentPV_ = preStepPoint_->GetPhysicalVolume();
175 
176  G4String name_ = currentPV_->GetName();
177  name_.assign(name_, 0, 4);
178  G4String particleType = theTrack_->GetDefinition()->GetParticleName();
179  edeposit_ = aStep->GetTotalEnergyDeposit();
180 
181  tSlice_ = (postStepPoint_->GetGlobalTime()) / nanosecond;
182  tSliceID_ = (int)tSlice_;
183  unitID_ = setDetUnitId(aStep);
184 #ifdef debug
185  LogDebug("PPSSim") << "UNIT ID " << unitID_;
186 #endif
187  primaryID_ = theTrack_->GetTrackID();
188 
189  theEntryPoint_.setX(TheEntryPoint.x());
190  theEntryPoint_.setY(TheEntryPoint.y());
191  theEntryPoint_.setZ(TheEntryPoint.z());
192  theExitPoint_.setX(TheExitPoint.x());
193  theExitPoint_.setY(TheExitPoint.y());
194  theExitPoint_.setZ(TheExitPoint.z());
195 
197  Pabs_ = aStep->GetPreStepPoint()->GetMomentum().mag() / GeV;
198  Tof_ = aStep->GetPostStepPoint()->GetGlobalTime() / nanosecond;
199 
200  Eloss_ = aStep->GetTotalEnergyDeposit() / GeV;
201  ParticleType_ = theTrack_->GetDefinition()->GetPDGEncoding();
202 
203  ThetaAtEntry_ = aStep->GetPreStepPoint()->GetPosition().theta();
204  PhiAtEntry_ = aStep->GetPreStepPoint()->GetPosition().phi();
205 
206  ParentId_ = theTrack_->GetParentID();
207  Vx_ = theTrack_->GetVertexPosition().x();
208  Vy_ = theTrack_->GetVertexPosition().y();
209  Vz_ = theTrack_->GetVertexPosition().z();
210 }
211 
213  if (primaryID_ < 1) {
214  edm::LogWarning("PPSSim") << "***** PPSPixelSD error: primaryID = " << primaryID_ << " maybe detector name changed";
215  }
216 
217  // Update if in the same detector, time-slice and for same track
218  if (tSliceID_ == tsID_ && unitID_ == previousUnitID_) {
219  updateHit();
220  return true;
221  }
222 
223  // Reset entry point for new primary
224  if (primaryID_ != primID_)
226 
227  //look in the HitContainer whether a hit with the same primID_, unitID_,
228  //tSliceID_ already exists:
229  bool found = false;
230 
231  for (int j = 0; j < theHC_->entries() && !found; j++) {
232  PPSPixelG4Hit* aPreviousHit = (*theHC_)[j];
233  if (aPreviousHit->trackID() == primaryID_ && aPreviousHit->timeSliceID() == tSliceID_ &&
234  aPreviousHit->unitID() == unitID_) {
235  currentHit_ = aPreviousHit;
236  found = true;
237  }
238  }
239 
240  if (found) {
241  updateHit();
242  return true;
243  }
244  return false;
245 }
246 
248 #ifdef debug
249  LogDebug("PPSSim") << "PPSPixelSD CreateNewHit for"
250  << " PV " << currentPV_->GetName() << " PVid = " << currentPV_->GetCopyNo()
251  << " MVid = " << currentPV_->GetMother()->GetCopyNo() << " Unit " << unitID_ << "\n"
252  << " primary " << primaryID_ << " time slice " << tSliceID_ << " For Track "
253  << theTrack_->GetTrackID() << " which is a " << theTrack_->GetDefinition()->GetParticleName();
254 
255  if (theTrack_->GetTrackID() == 1) {
256  LogDebug("PPSSim") << " of energy " << theTrack_->GetTotalEnergy();
257  } else {
258  LogDebug("PPSSim") << " daughter of part. " << theTrack_->GetParentID();
259  }
260 
261  if (theTrack_->GetCreatorProcess() != NULL)
262  LogDebug("PPSSim") << theTrack_->GetCreatorProcess()->GetProcessName();
263  else
264  LogDebug("PPSSim") << "NO process";
265 #endif
266 
272 
279 
283 
288 
289  updateHit();
290 
292 }
293 
295  if (Eloss_ > 0.) {
296 #ifdef debug
297  LogDebug("PPSSim") << "G4PPSPixelSD updateHit: add eloss " << Eloss_ << "\nCurrentHit=" << currentHit_
298  << ", PostStepPoint=" << postStepPoint_->GetPosition();
299 #endif
301  }
302  // buffer for next steps:
303  tsID_ = tSliceID_;
306 }
307 
309  if (primID_ < 0)
310  return;
311  if (hit == nullptr) {
312  edm::LogWarning("PPSSim") << "PPSPixelSD: hit to be stored is NULL !!";
313  return;
314  }
315 
316  theHC_->insert(hit);
317 }
318 
321 
322  incidentEnergy_ = preStepPoint_->GetKineticEnergy();
323 }
324 
#define LogDebug(id)
void setParticleType(short i)
void DrawAll() override
Definition: PPSPixelSD.cc:132
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
float thetaAtEntry() const
const double GeV
Definition: MathUtil.h:16
uint32_t previousUnitID_
Definition: PPSPixelSD.h:103
G4ThreeVector setToLocal(const G4ThreeVector &globalPoint)
Definition: PPSPixelSD.cc:155
G4ThreeVector theEntryPoint_
Definition: PPSPixelSD.h:113
uint32_t setDetUnitId(const G4Step *) override
Definition: PPSPixelSD.cc:84
void stepInfo(const G4Step *aStep)
Definition: PPSPixelSD.cc:162
float Pabs_
Definition: PPSPixelSD.h:115
G4int hcID_
Definition: PPSPixelSD.h:95
const G4ThreeVector & exitPoint() const
void clearHits() override
Definition: PPSPixelSD.cc:153
float Tof_
Definition: PPSPixelSD.h:116
void PrintAll() override
Definition: PPSPixelSD.cc:134
void setExitPoint(const G4ThreeVector &)
#define nullptr
G4VPhysicalVolume * currentPV_
Definition: PPSPixelSD.h:102
float Vy_
Definition: PPSPixelSD.h:124
T y() const
Definition: PV3DBase.h:60
G4StepPoint * preStepPoint_
Definition: PPSPixelSD.h:107
#define NULL
Definition: scimark2.h:8
short ParticleType_
Definition: PPSPixelSD.h:118
void setP(float e)
void createNewHit()
Definition: PPSPixelSD.cc:247
void storeHit(PPSPixelG4Hit *)
Definition: PPSPixelSD.cc:308
int ParentId_
Definition: PPSPixelSD.h:123
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: PPSPixelSD.cc:74
void setVy(float p)
float Vx_
Definition: PPSPixelSD.h:124
float phiAtEntry() const
float energyLoss() const
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
Definition: PPSPixelSD.cc:145
int eventno_
Definition: PPSPixelSD.h:126
int primaryID_
Definition: PPSPixelSD.h:104
PPSPixelG4Hit * currentHit_
Definition: PPSPixelSD.h:100
G4int primID_
Definition: PPSPixelSD.h:92
void setPhiAtEntry(float f)
void resetForNewPrimary()
Definition: PPSPixelSD.cc:319
G4Track * theTrack_
Definition: PPSPixelSD.h:101
int timeSliceID() const
void setParentId(int p)
bool hitExists()
Definition: PPSPixelSD.cc:212
Local3DPoint FinalStepPosition(const G4Step *step, coordinates) const
void Initialize(G4HCofThisEvent *HCE) override
Definition: PPSPixelSD.cc:88
T z() const
Definition: PV3DBase.h:61
void clear() override
Definition: PPSPixelSD.cc:130
const G4ThreeVector & entryPoint() const
float incidentEnergy_
Definition: PPSPixelSD.h:91
std::unique_ptr< TrackingSlaveSD > slave_
Definition: PPSPixelSD.h:82
void setTof(float e)
std::string name_
Definition: PPSPixelSD.h:94
float z() const
int particleType() const
void setTimeSlice(double d)
void setEntryPoint(const G4ThreeVector &)
PPSPixelG4HitCollection * theHC_
Definition: PPSPixelSD.h:96
float tof() const
double tSlice_
Definition: PPSPixelSD.h:105
float Eloss_
Definition: PPSPixelSD.h:117
void setThetaAtEntry(float t)
float PhiAtEntry_
Definition: PPSPixelSD.h:121
static unsigned int maxPixelHits_
Definition: PPSPixelSD.h:71
std::unique_ptr< PPSVDetectorOrganization > numberingScheme_
Definition: PPSPixelSD.h:83
G4ThreeVector theExitPoint_
Definition: PPSPixelSD.h:114
void setVx(float p)
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: PPSPixelSD.cc:139
uint32_t unitID() const
~PPSPixelSD() override
Definition: PPSPixelSD.cc:72
G4ThreeVector position_
Definition: PPSPixelSD.h:112
void setTrackID(int i)
int tSliceID_
Definition: PPSPixelSD.h:104
int trackID() const
void setUnitID(uint32_t i)
G4ThreeVector hitPoint_
Definition: PPSPixelSD.h:110
G4THitsCollection< PPSPixelG4Hit > PPSPixelG4HitCollection
float p() const
void setVz(float p)
G4StepPoint * postStepPoint_
Definition: PPSPixelSD.h:108
std::vector< PSimHit > PSimHitContainer
uint32_t unitID_
Definition: PPSPixelSD.h:103
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: PPSPixelSD.cc:102
PPSPixelSD(const std::string &, const edm::EventSetup &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, SimTrackManager const *)
Definition: PPSPixelSD.cc:37
float edeposit_
Definition: PPSPixelSD.h:109
void updateHit()
Definition: PPSPixelSD.cc:294
T x() const
Definition: PV3DBase.h:59
void setIncidentEnergy(double e)
void setEnergyLoss(float e)
G4ThreeVector entrancePoint_
Definition: PPSPixelSD.h:90
float ThetaAtEntry_
Definition: PPSPixelSD.h:120
void summarize()
Definition: PPSPixelSD.cc:325
float Vz_
Definition: PPSPixelSD.h:124
void setMeanPosition(const G4ThreeVector &a)
Definition: PPSPixelG4Hit.h:54
Local3DPoint InitialStepPosition(const G4Step *step, coordinates) const