CMS 3D CMS Logo

TotemSD.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Forward
4 // Class : TotemSD
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author:
10 // Created: Tue May 16 10:14:34 CEST 2006
11 //
12 
13 // system include files
14 
15 // user include files
18 
22 
25 
31 
34 
35 #include "G4SDManager.hh"
36 #include "G4Step.hh"
37 #include "G4Track.hh"
38 #include "G4VProcess.hh"
39 
40 #include "G4PhysicalConstants.hh"
41 #include "G4SystemOfUnits.hh"
42 
43 //#define EDM_ML_DEBUG
44 //
45 // constructors and destructor
46 //
48  const SensitiveDetectorCatalog& clg,
49  edm::ParameterSet const& p,
50  const SimTrackManager* manager)
51  : SensitiveTkDetector(name, clg),
52  numberingScheme(nullptr),
53  hcID(-1),
54  theHC(nullptr),
55  theManager(manager),
56  currentHit(nullptr),
57  theTrack(nullptr),
58  currentPV(nullptr),
59  unitID(0),
60  previousUnitID(0),
61  preStepPoint(nullptr),
62  postStepPoint(nullptr) {
63  //Parameters
64  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("TotemSD");
65  int verbn = m_p.getUntrackedParameter<int>("Verbosity");
66 
67  SetVerboseLevel(verbn);
68 
69  slave = new TrackingSlaveSD(name);
70 
71  if (name == "TotemHitsT1") {
73  } else if (name == "TotemHitsT2Si") {
75  } else if (name == "TotemHitsT2Gem") {
77  } else if (name == "TotemHitsRP") {
79  } else {
80  edm::LogWarning("ForwardSim") << "TotemSD: ReadoutName not supported\n";
81  }
82 }
83 
85  delete slave;
86  delete numberingScheme;
87 }
88 
89 bool TotemSD::ProcessHits(G4Step* aStep, G4TouchableHistory*) {
90  getStepInfo(aStep);
91  if (!hitExists() && edeposit > 0.) {
92  createNewHit();
93  return true;
94  }
95  if (!hitExists() && (((unitID == 1111 || unitID == 2222) && ParentId == 0 && ParticleType == 2212))) {
97  }
98  return true;
99 }
100 
101 uint32_t TotemSD::setDetUnitId(const G4Step* aStep) {
102  return (numberingScheme == nullptr ? 0 : numberingScheme->getUnitID(aStep));
103 }
104 
105 void TotemSD::Initialize(G4HCofThisEvent* HCE) {
106 #ifdef EDM_ML_DEBUG
107  edm::LogVerbatim("ForwardSim") << "TotemSD : Initialize called for " << GetName();
108 #endif
109 
110  theHC = new TotemG4HitCollection(GetName(), collectionName[0]);
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 void TotemSD::EndOfEvent(G4HCofThisEvent*) {
120  // here we loop over transient hits and make them persistent
121  int thehc_entries = theHC->entries();
122  for (int j = 0; j < thehc_entries && j < 15000; j++) {
123  TotemG4Hit* aHit = (*theHC)[j];
124 #ifdef EDM_ML_DEBUG
125  edm::LogVerbatim("ForwardSim") << "HIT NUMERO " << j << "unit ID = " << aHit->getUnitID()
126  << "\n enrty z " << aHit->getEntry().z() << "\n theta "
127  << aHit->getThetaAtEntry() << "\n";
128 #endif
129  Local3DPoint theExitPoint(0, 0, 0);
130  Local3DPoint Entrata(aHit->getEntry().x(), aHit->getEntry().y(), aHit->getEntry().z());
131  slave->processHits(PSimHit(Entrata,
132  theExitPoint,
133  aHit->getPabs(),
134  aHit->getTof(),
135  aHit->getEnergyLoss(),
136  aHit->getParticleType(),
137  aHit->getUnitID(),
138  aHit->getTrackID(),
139  aHit->getThetaAtEntry(),
140  aHit->getPhiAtEntry()));
141  }
142 }
143 
145 #ifdef EDM_ML_DEBUG
146  edm::LogVerbatim("ForwardSim") << "TotemSD: Collection " << theHC->GetName();
147 #endif
148  theHC->PrintAllHits();
149 }
150 
152  if (slave->name() == hname) {
153  cc = slave->hits();
154  }
155 }
156 
158 #ifdef EDM_ML_DEBUG
159  edm::LogVerbatim("ForwardSim") << " Dispatched BeginOfEvent for " << GetName() << " !";
160 #endif
161  clearHits();
162 }
163 
165 
166 G4ThreeVector TotemSD::setToLocal(const G4ThreeVector& global) {
167  G4ThreeVector localPoint;
168  const G4VTouchable* touch = preStepPoint->GetTouchable();
169  localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
170  return localPoint;
171 }
172 
173 void TotemSD::getStepInfo(const G4Step* aStep) {
174  preStepPoint = aStep->GetPreStepPoint();
175  postStepPoint = aStep->GetPostStepPoint();
176  theTrack = aStep->GetTrack();
177  hitPoint = preStepPoint->GetPosition();
178  currentPV = preStepPoint->GetPhysicalVolume();
179 
180  // double weight = 1;
181  G4String name = currentPV->GetName();
182  name.assign(name, 0, 4);
183  G4String particleType = theTrack->GetDefinition()->GetParticleName();
184  edeposit = aStep->GetTotalEnergyDeposit();
185 
186  tSlice = (postStepPoint->GetGlobalTime()) / nanosecond;
187  tSliceID = (int)tSlice;
188  unitID = setDetUnitId(aStep);
189 #ifdef EDM_ML_DEBUG
190  edm::LogVerbatim("ForwardSim") << "UNITa " << unitID;
191 #endif
192  primaryID = theTrack->GetTrackID();
193 
194  Posizio = hitPoint;
195  Pabs = aStep->GetPreStepPoint()->GetMomentum().mag() / CLHEP::GeV;
196  Tof = aStep->GetPostStepPoint()->GetGlobalTime() / CLHEP::nanosecond;
197 
198  Eloss = aStep->GetTotalEnergyDeposit() / CLHEP::GeV;
199  ParticleType = theTrack->GetDefinition()->GetPDGEncoding();
200 
201  ThetaAtEntry = aStep->GetPreStepPoint()->GetPosition().theta() / CLHEP::deg;
202  PhiAtEntry = aStep->GetPreStepPoint()->GetPosition().phi() / CLHEP::deg;
203 
204  ParentId = theTrack->GetParentID();
205  Vx = theTrack->GetVertexPosition().x();
206  Vy = theTrack->GetVertexPosition().y();
207  Vz = theTrack->GetVertexPosition().z();
208 }
209 
211  if (primaryID < 1) {
212  edm::LogWarning("ForwardSim") << "***** TotemSD error: primaryID = " << primaryID << " maybe detector name changed";
213  }
214 
215  // Update if in the same detector, time-slice and for same track
216  // if (primaryID == primID && tSliceID == tsID && unitID==previousUnitID) {
217  if (tSliceID == tsID && unitID == previousUnitID) {
218  updateHit();
219  return true;
220  }
221 
222  // Reset entry point for new primary
223  if (primaryID != primID)
225 
226  //look in the HitContainer whether a hit with the same primID, unitID,
227  //tSliceID already exists:
228 
229  bool found = false;
230  int thehc_entries = theHC->entries();
231  for (int j = 0; j < thehc_entries && !found; j++) {
232  TotemG4Hit* aPreviousHit = (*theHC)[j];
233  if (aPreviousHit->getTrackID() == primaryID && aPreviousHit->getTimeSliceID() == tSliceID &&
234  aPreviousHit->getUnitID() == unitID) {
235  currentHit = aPreviousHit;
236  found = true;
237  break;
238  }
239  }
240 
241  if (found) {
242  updateHit();
243  return true;
244  } else {
245  return false;
246  }
247 }
248 
250 #ifdef EDM_ML_DEBUG
251  edm::LogVerbatim("ForwardSim") << "TotemSD CreateNewHit for PV " << currentPV->GetName()
252  << " PVid = " << currentPV->GetCopyNo() << " Unit " << unitID << "\n primary "
253  << primaryID << " time slice " << tSliceID << " For Track " << theTrack->GetTrackID()
254  << " which is a " << theTrack->GetDefinition()->GetParticleName();
255 
256  if (theTrack->GetTrackID() == 1) {
257  edm::LogVerbatim("ForwardSim") << " of energy " << theTrack->GetTotalEnergy();
258  } else {
259  edm::LogVerbatim("ForwardSim") << " daughter of part. " << theTrack->GetParentID();
260  }
261 
262  if (theTrack->GetCreatorProcess() != nullptr)
263  edm::LogVerbatim("ForwardSim") << theTrack->GetCreatorProcess()->GetProcessName();
264  else
265  edm::LogVerbatim("ForwardSim") << "NO process";
266 #endif
267 
268  currentHit = new TotemG4Hit;
273 
280 
281  currentHit->setEntry(Posizio.x(), Posizio.y(), Posizio.z());
282 
284  currentHit->setVx(Vx);
285  currentHit->setVy(Vy);
286  currentHit->setVz(Vz);
287 
288  updateHit();
289 
291 }
292 
294  // edm::LogVerbatim("ForwardSim") << "INSIDE CREATE NEW HIT EVO ";
295 
296  currentHit = new TotemG4Hit;
301 
308 
309  // edm::LogVerbatim("ForwardSim") << Posizio.x() << " " << Posizio.y() << " " << Posizio.z();
310 
312  currentHit->setVx(Vx);
313  currentHit->setVy(Vy);
314  currentHit->setVz(Vz);
315 
316  G4ThreeVector _PosizioEvo;
317  int flagAcc = 0;
318  _PosizioEvo = posizioEvo(Posizio, Vx, Vy, Vz, Pabs, flagAcc);
319 
320  if (flagAcc == 1) {
321  currentHit->setEntry(_PosizioEvo.x(), _PosizioEvo.y(), _PosizioEvo.z());
322 
323  updateHit();
324 
326  }
327  // edm::LogVerbatim("ForwardSim") << "STORED HIT IN: " << unitID;
328 }
329 
330 G4ThreeVector TotemSD::posizioEvo(
331  const G4ThreeVector& Pos, double vx, double vy, double vz, double pabs, int& accettanza) {
332  accettanza = 0;
333  //Pos.xyz() in mm
334  G4ThreeVector PosEvo;
335  double ThetaX = std::atan((Pos.x() - vx) / (Pos.z() - vz));
336  double ThetaY = std::atan((Pos.y() - vy) / (Pos.z() - vz));
337  double X_at_0 = (vx - ((Pos.x() - vx) / (Pos.z() - vz)) * vz) / 1000.;
338  double Y_at_0 = (vy - ((Pos.y() - vy) / (Pos.z() - vz)) * vz) / 1000.;
339 
340  // double temp_evo_X;
341  // double temp_evo_Y;
342  // double temp_evo_Z;
343  // temp_evo_Z = std::abs(Pos.z())/Pos.z()*220000.;
344 
345  //csi=-dp/d
346  double csi = std::abs((7000. - pabs) / 7000.);
347 
348  // all in m
349  const int no_rp = 4;
350  double x_par[no_rp + 1];
351  double y_par[no_rp + 1];
352  //rp z position
353  double rp[no_rp] = {141., 149., 198., 220.};
354  //{lx0,mlx} for each rp; Lx=lx0+mlx*csi
355  double leffx[][2] = {{122.5429, -46.9312}, {125.4194, -49.1849}, {152.6, -81.157}, {98.8914, -131.8390}};
356  //{ly0,mly} for each rp; Ly=ly0+mly*csi
357  double leffy[][2] = {{124.2314, -55.4852}, {127.7825, -57.4503}, {179.455, -76.274}, {273.0931, -40.4626}};
358  //{vx0,mvx0} for each rp; vx=vx0+mvx*csi
359  double avx[][2] = {{0.515483, -1.0123}, {0.494122, -1.0534}, {0.2217, -1.483}, {0.004633, -1.0719}};
360  //{vy0,mvy0} for each rp; vy=vy0+mvy*csi
361  double avy[][2] = {{0.371418, -1.6327}, {0.349035, -1.6955}, {0.0815, -2.59}, {0.007592, -4.0841}};
362  //{D0,md,a,b} for each rp; D=D0+(md+a*thetax)*csi+b*thetax
363  double ddx[][4] = {{-0.082336, -0.092513, 112.3436, -82.5029},
364  {-0.086927, -0.097670, 114.9513, -82.9835},
365  {-0.092117, -0.0915, 180.6236, -82.443},
366  {-0.050470, 0.058837, 208.1106, 20.8198}};
367  // {10sigma_x+0.5mm,10sigma_y+0.5mm}
368  double detlim[][2] = {{0, 0}, {0.0028, 0.0021}, {0, 0}, {0.0008, 0.0013}};
369  //{rmax,dmax}
370  double pipelim[][2] = {{0.026, 0.026}, {0.04, 0.04}, {0.0226, 0.0177}, {0.04, 0.04}};
371 
372  for (int j = 0; j < no_rp; j++) {
373  //y=Ly*thetay+vy*y0
374  //x=Lx*thetax+vx*x0-csi*D
375  y_par[j] = ThetaY * (leffy[j][0] + leffy[j][1] * csi) + (avy[j][0] + avy[j][1] * csi) * Y_at_0;
376  x_par[j] = ThetaX * (leffx[j][0] + leffx[j][1] * csi) + (avx[j][0] + avx[j][1] * csi) * X_at_0 -
377  csi * (ddx[j][0] + (ddx[j][1] + ddx[j][2] * ThetaX) * csi + ddx[j][3] * ThetaX);
378  }
379 
380  //pass TAN@141
381  if (std::abs(y_par[0]) < pipelim[0][1] && sqrt((y_par[0] * y_par[0]) + (x_par[0] * x_par[0])) < pipelim[0][0]) {
382  //pass 149
383  if ((sqrt((y_par[1] * y_par[1]) + (x_par[1] * x_par[1])) < pipelim[1][0]) &&
384  (std::abs(y_par[1]) > detlim[1][1] || x_par[1] > detlim[1][0])) {
385  accettanza = 1;
386  }
387  }
388 
389  //pass TAN@141
390  if (std::abs(y_par[0]) < pipelim[0][1] && sqrt((y_par[0]) * (y_par[0]) + (x_par[0]) * (x_par[0])) < pipelim[0][0]) {
391  //pass Q5@198
392  if (std::abs(y_par[2]) < pipelim[2][1] && sqrt((y_par[2] * y_par[2]) + (x_par[2] * x_par[2])) < pipelim[2][0]) {
393  //pass 220
394  if ((sqrt((y_par[3] * y_par[3]) + (x_par[3] * x_par[3])) < pipelim[3][0]) &&
395  (std::abs(y_par[3]) > detlim[3][1] || x_par[3] > detlim[3][0])) {
396  accettanza = 1;
397 
398  PosEvo.setX(1000 * x_par[3]);
399  PosEvo.setY(1000 * y_par[3]);
400  PosEvo.setZ(1000 * rp[3]);
401  if (Pos.z() < vz)
402  PosEvo.setZ(-1000 * rp[3]);
403  }
404  }
405  }
406  /*
407  edm::LogVerbatim("ForwardSim") << "\n"
408  << "ACCETTANZA: "<<accettanza << "\n"
409  << "CSI: "<< csi << "\n"
410  << "Theta_X: " << ThetaX << "\n"
411  << "Theta_Y: " << ThetaY << "\n"
412  << "X_at_0: "<< X_at_0 << "\n"
413  << "Y_at_0: "<< Y_at_0 << "\n"
414  << "x_par[3]: "<< x_par[3] << "\n"
415  << "y_par[3]: "<< y_par[3] << "\n"
416  << "pos " << Pos.x() << " " << Pos.y() << " "
417  << Pos.z() << "\n" << "V "<< vx << " " << vy << " "
418  << vz << "\n"
419 */
420  // --------------
421  return PosEvo;
422 }
423 
425  //
426  if (Eloss > 0.) {
427 #ifdef EDM_ML_DEBUG
428  edm::LogVerbatim("ForwardSim") << "G4TotemT1SD updateHit: add eloss " << Eloss << "\nCurrentHit=" << currentHit
429  << ", PostStepPoint=" << postStepPoint->GetPosition();
430 #endif
431 
433  }
434 
435  // buffer for next steps:
436  tsID = tSliceID;
437  primID = primaryID;
439 }
440 
442  if (primID < 0)
443  return;
444  if (hit == nullptr) {
445  edm::LogWarning("ForwardSim") << "TotemSD: hit to be stored is NULL !!";
446  return;
447  }
448 
449  theHC->insert(hit);
450 }
451 
454  incidentEnergy = preStepPoint->GetKineticEnergy();
455 }
void setVz(float p)
Definition: TotemG4Hit.cc:178
G4ThreeVector setToLocal(const G4ThreeVector &globalPoint)
Definition: TotemSD.cc:166
Log< level::Info, true > LogVerbatim
void resetForNewPrimary()
Definition: TotemSD.cc:452
int ParentId
Definition: TotemSD.h:114
void Initialize(G4HCofThisEvent *HCE) override
Definition: TotemSD.cc:105
float getThetaAtEntry() const
Definition: TotemG4Hit.cc:153
std::string name() const
void setPabs(float e)
Definition: TotemG4Hit.cc:148
uint32_t unitID
Definition: TotemSD.h:96
float Pabs
Definition: TotemSD.h:106
G4ThreeVector posizioEvo(const G4ThreeVector &, double, double, double, double, int &)
Definition: TotemSD.cc:330
float getPabs() const
Definition: TotemG4Hit.cc:143
G4ThreeVector Posizio
Definition: TotemSD.h:105
TotemG4HitCollection * theHC
Definition: TotemSD.h:89
float Tof
Definition: TotemSD.h:107
void setEnergyLoss(float e)
Definition: TotemG4Hit.cc:150
int getTimeSliceID() const
Definition: TotemG4Hit.cc:134
float getTof() const
Definition: TotemG4Hit.cc:144
G4ThreeVector entrancePoint
Definition: TotemSD.h:84
void setEntry(double x, double y, double z)
Definition: TotemG4Hit.h:55
double tSlice
Definition: TotemSD.h:98
int primaryID
Definition: TotemSD.h:97
bool ProcessHits(G4Step *, G4TouchableHistory *) override
Definition: TotemSD.cc:89
void createNewHit()
Definition: TotemSD.cc:249
uint32_t previousUnitID
Definition: TotemSD.h:96
float incidentEnergy
Definition: TotemSD.h:85
void storeHit(TotemG4Hit *)
Definition: TotemSD.cc:441
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
Definition: TotemSD.cc:157
TotemVDetectorOrganization * numberingScheme
Definition: TotemSD.h:77
G4int hcID
Definition: TotemSD.h:88
int tSliceID
Definition: TotemSD.h:97
float getPhiAtEntry() const
Definition: TotemG4Hit.cc:154
void setVy(float p)
Definition: TotemG4Hit.cc:175
const G4StepPoint * preStepPoint
Definition: TotemSD.h:100
void setTimeSlice(double d)
Definition: TotemG4Hit.cc:133
T getUntrackedParameter(std::string const &, T const &) const
std::vector< PSimHit > & hits()
float getEnergyLoss() const
Definition: TotemG4Hit.cc:145
virtual void Initialize()
float Eloss
Definition: TotemSD.h:108
T sqrt(T t)
Definition: SSEVec.h:19
uint32_t getUnitID() const
Definition: TotemG4Hit.cc:129
float edeposit
Definition: TotemSD.h:102
float Vx
Definition: TotemSD.h:115
G4ThreeVector hitPoint
Definition: TotemSD.h:103
void setVx(float p)
Definition: TotemG4Hit.cc:172
void fillHits(edm::PSimHitContainer &, const std::string &) override
Definition: TotemSD.cc:151
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
~TotemSD() override
Definition: TotemSD.cc:84
void setParentId(int p)
Definition: TotemG4Hit.cc:169
TotemG4Hit * currentHit
Definition: TotemSD.h:93
G4VPhysicalVolume * currentPV
Definition: TotemSD.h:95
void setIncidentEnergy(double e)
Definition: TotemG4Hit.cc:124
uint32_t setDetUnitId(const G4Step *) override
Definition: TotemSD.cc:101
void PrintAll() override
Definition: TotemSD.cc:144
short ParticleType
Definition: TotemSD.h:109
void setThetaAtEntry(float t)
Definition: TotemG4Hit.cc:156
float PhiAtEntry
Definition: TotemSD.h:112
const G4StepPoint * postStepPoint
Definition: TotemSD.h:101
void setUnitID(uint32_t i)
Definition: TotemG4Hit.cc:130
void setTrackID(int i)
Definition: TotemG4Hit.cc:127
virtual uint32_t getUnitID(const G4Step *aStep) const =0
void setPhiAtEntry(float f)
Definition: TotemG4Hit.cc:157
void EndOfEvent(G4HCofThisEvent *eventHC) override
Definition: TotemSD.cc:119
void updateHit()
Definition: TotemSD.cc:424
void setParticleType(short i)
Definition: TotemG4Hit.cc:151
int getParticleType() const
Definition: TotemG4Hit.cc:146
void setTof(float e)
Definition: TotemG4Hit.cc:149
float Vz
Definition: TotemSD.h:115
G4int primID
Definition: TotemSD.h:86
math::XYZPoint getEntry() const
Definition: TotemG4Hit.cc:115
TotemSD(const std::string &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
Definition: TotemSD.cc:47
TrackingSlaveSD * slave
Definition: TotemSD.h:76
void createNewHitEvo()
Definition: TotemSD.cc:293
static TrackerG4SimHitNumberingScheme & numberingScheme(const GeometricDet &det)
G4Track * theTrack
Definition: TotemSD.h:94
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
Log< level::Warning, false > LogWarning
int tsID
Definition: TotemSD.h:92
G4THitsCollection< TotemG4Hit > TotemG4HitCollection
float Vy
Definition: TotemSD.h:115
void clearHits() override
Definition: TotemSD.cc:164
float ThetaAtEntry
Definition: TotemSD.h:111
bool hitExists()
Definition: TotemSD.cc:210
int getTrackID() const
Definition: TotemG4Hit.cc:126
void getStepInfo(const G4Step *aStep)
Definition: TotemSD.cc:173