CMS 3D CMS Logo

TkAccumulatingSensitiveDetector.cc
Go to the documentation of this file.
2 
5 
8 
14 
18 
22 
26 
27 #include "G4Track.hh"
28 #include "G4StepPoint.hh"
29 #include "G4VProcess.hh"
30 
31 #include "G4SystemOfUnits.hh"
32 
33 #include <vector>
34 #include <iostream>
35 
37 
38 //#define FAKEFRAMEROTATION
39 
40 static
43 {
44  static thread_local TrackerG4SimHitNumberingScheme s_scheme(cpv, det);
45  return s_scheme;
46 }
47 
49  const DDCompactView & cpv,
50  const SensitiveDetectorCatalog & clg,
51  edm::ParameterSet const & p,
52  const SimTrackManager* manager) :
53  SensitiveTkDetector(name, cpv, clg, p),theManager(manager),
54  rTracker(1200.*CLHEP::mm),zTracker(3000.*CLHEP::mm),mySimHit(nullptr),
55  lastId(0),lastTrack(0),oldVolume(nullptr),px(0.0f),py(0.0f),pz(0.0f),eventno(0),pname("")
56 {
57  edm::ParameterSet m_TrackerSD = p.getParameter<edm::ParameterSet>("TrackerSD");
58  allowZeroEnergyLoss = m_TrackerSD.getParameter<bool>("ZeroEnergyLoss");
59  neverAccumulate = m_TrackerSD.getParameter<bool>("NeverAccumulate");
60  printHits = m_TrackerSD.getParameter<bool>("PrintHits");
61  theTofLimit = m_TrackerSD.getParameter<double>("ElectronicSigmaInNanoSeconds")*3*CLHEP::ns; // 3 sigma
62  energyCut = m_TrackerSD.getParameter<double>("EnergyThresholdForPersistencyInGeV")*CLHEP::GeV; //default must be 0.5
63  energyHistoryCut = m_TrackerSD.getParameter<double>("EnergyThresholdForHistoryInGeV")*CLHEP::GeV;//default must be 0.05
65 
66  // No Rotation given in input, automagically choose one based upon the name
67  std::string rotType;
68  theRotation.reset(new TrackerFrameRotation());
69  rotType = "TrackerFrameRotation";
70 
71 #ifdef FAKEFRAMEROTATION
72  theRotation.reset(new FakeFrameRotation());
73  rotType = "FakeFrameRotation";
74 #endif
75 
76  edm::LogInfo("TrackerSimInfo")<<" TkAccumulatingSensitiveDetector: "
77  <<" Criteria for Saving Tracker SimTracks: \n"
78  <<" History: "<<energyHistoryCut<< " MeV; Persistency: "
79  << energyCut<<" MeV; TofLimit: " << theTofLimit << " ns"
80  <<"\n FrameRotation type " << rotType
81  <<" rTracker(cm)= " << rTracker/CLHEP::cm
82  <<" zTracker(cm)= " << zTracker/CLHEP::cm
83  <<" allowZeroEnergyLoss: " << allowZeroEnergyLoss
84  <<" neverAccumulate: " << neverAccumulate
85  <<" printHits: " << printHits;
86 
87  slaveLowTof.reset(new TrackingSlaveSD(name+"LowTof"));
88  slaveHighTof.reset(new TrackingSlaveSD(name+"HighTof"));
89 
90  std::vector<std::string> temp;
91  temp.push_back(slaveLowTof.get()->name());
92  temp.push_back(slaveHighTof.get()->name());
93  setNames(temp);
94 
96  theNumberingScheme.reset(nullptr);
97 }
98 
100 {}
101 
102 bool TkAccumulatingSensitiveDetector::ProcessHits(G4Step * aStep, G4TouchableHistory *)
103 {
104  LogDebug("TrackerSimDebug")<< " Entering a new Step " << aStep->GetTotalEnergyDeposit() << " "
105  << aStep->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume()->GetName();
106 
107  if (aStep->GetTotalEnergyDeposit()>0. || allowZeroEnergyLoss)
108  {
109  if (!mySimHit)
110  {
111  createHit(aStep);
112  }
113  else if(neverAccumulate || newHit(aStep))
114  {
115  sendHit();
116  createHit(aStep);
117  }
118  else
119  {
120  updateHit(aStep);
121  }
122  return true;
123  }
124  return false;
125 }
126 
128 {
129  return theNumberingScheme.get()->g4ToNumberingScheme(step->GetPreStepPoint()->GetTouchable());
130 }
131 
133  const G4Track* gTrack = (*bot)();
134 
135 #ifdef DUMPPROCESSES
136  if (gTrack->GetCreatorProcess()) {
137  edm::LogVerbatim("TrackerSimInfo")<<" -> PROCESS CREATOR : "
138  <<gTrack->GetCreatorProcess()->GetProcessName();
139  } else {
140  edm::LogVerbatim("TrackerSimInfo") <<" -> No Creator process";
141  }
142 #endif
143 
144  //
145  //Position
146  //
147  const G4ThreeVector& pos = gTrack->GetPosition();
148  LogDebug("TrackerSimDebug")
149  << " update(..) of " << gTrack->GetDefinition()->GetParticleName()
150  << " trackID= " << gTrack->GetTrackID()
151  <<" E(MeV)= "<<gTrack->GetKineticEnergy() <<" Ecut= " << energyCut
152  << " R(mm)= " << pos.perp() << " Z(mm)= " << pos.z();
153 
154  //
155  // Check if in Tracker Volume
156  //
157  if (pos.x()*pos.x() + pos.y()*pos.y() < rTracker2
158  && std::abs(pos.z()) < zTracker){
159  //
160  // inside the Tracker
161  //
162  TrackInformation* info = nullptr;
163  if (gTrack->GetKineticEnergy() > energyCut){
164  info = cmsTrackInformation(gTrack);
165  info->storeTrack(true);
166  }
167  //
168  // Save History?
169  //
170  if (gTrack->GetKineticEnergy() > energyHistoryCut){
171  if(!info) { info = cmsTrackInformation(gTrack); }
172  info->putInHistory();
173  LogDebug("TrackerSimDebug")<<" Track inside the tracker selected for HISTORY"
174  <<" Track ID= "<<gTrack->GetTrackID();
175  }
176  }
177 }
178 
180 {
181  if (mySimHit == nullptr) return;
182  if (printHits)
183  {
184  TkSimHitPrinter thePrinter("TkHitPositionOSCAR.dat");
185  thePrinter.startNewSimHit(GetName(),oldVolume->GetLogicalVolume()->GetName(),
187  thePrinter.printLocal(mySimHit->entryPoint(),mySimHit->exitPoint());
190  thePrinter.printGlobalMomentum(px,py,pz);
191  LogDebug("TrackerSimDebug")<< " Storing PSimHit: " << mySimHit->detUnitId()
192  << " " << mySimHit->trackId() << " " << mySimHit->energyLoss()
193  << " " << mySimHit->entryPoint() << " " << mySimHit->exitPoint();
194  }
195 
196  if (mySimHit->timeOfFlight() < theTofLimit) {
197  slaveLowTof.get()->processHits(*mySimHit); // implicit conversion (slicing) to PSimHit!!!
198  } else {
199  slaveHighTof.get()->processHits(*mySimHit); // implicit conversion (slicing) to PSimHit!!!
200  }
201  //
202  // clean up
203  delete mySimHit;
204  mySimHit = nullptr;
205  lastTrack = 0;
206  lastId = 0;
207 }
208 
210 {
211  // VI: previous hit should be already deleted
212  // in past here was a check if a hit is inside a sensitive detector,
213  // this is not needed, because call to senstive detector happens
214  // only inside the volume
215  const G4Track * theTrack = aStep->GetTrack();
216  Local3DPoint theExitPoint = theRotation.get()->transformPoint(LocalPostStepPosition(aStep));
217  Local3DPoint theEntryPoint;
218  //
219  // Check particle type - for gamma and neutral hadrons energy deposition
220  // should be local (VI)
221  //
222  if(0.0 == theTrack->GetDefinition()->GetPDGCharge()) {
223  theEntryPoint = theExitPoint;
224  } else {
225  theEntryPoint = theRotation.get()->transformPoint(LocalPreStepPosition(aStep));
226  }
227 
228  //
229  // This allows to send he skipEvent if it is outside!
230  //
231  const G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
232  float thePabs = preStepPoint->GetMomentum().mag()/GeV;
233  float theTof = preStepPoint->GetGlobalTime()/nanosecond;
234  float theEnergyLoss = aStep->GetTotalEnergyDeposit()/GeV;
235  int theParticleType = G4TrackToParticleID::particleID(theTrack);
236  uint32_t theDetUnitId = setDetUnitId(aStep);
237  int theTrackID = theTrack->GetTrackID();
238  if (theDetUnitId == 0)
239  {
240  edm::LogWarning("TkAccumulatingSensitiveDetector::createHit")
241  << " theDetUnitId is not valid for " << GetName();
242  throw cms::Exception("TkAccumulatingSensitiveDetector::createHit")
243  << "cannot get theDetUnitId for G4Track " << theTrackID;
244  }
245 
246  // To whom assign the Hit?
247  // First iteration: if the track is to be stored, use the current number;
248  // otherwise, get to the mother
249  unsigned int theTrackIDInsideTheSimHit=theTrackID;
250 
251  const TrackInformation* temp = cmsTrackInformation(theTrack);
252  if (!temp->storeTrack()) {
253  // Go to the mother!
254  theTrackIDInsideTheSimHit = theTrack->GetParentID();
255  LogDebug("TrackerSimDebug")
256  << " TkAccumulatingSensitiveDetector::createHit(): setting the TrackID from "
257  << theTrackIDInsideTheSimHit
258  << " to the mother one " << theTrackIDInsideTheSimHit << " " << theEnergyLoss;
259  } else {
260  LogDebug("TrackerSimDebug")
261  << " TkAccumulatingSensitiveDetector:createHit(): leaving the current TrackID "
262  << theTrackIDInsideTheSimHit;
263  }
264 
265  const G4ThreeVector& gmd = preStepPoint->GetMomentumDirection();
266  // convert it to local frame
267  G4ThreeVector lmd = ((G4TouchableHistory *)(preStepPoint->GetTouchable()))->GetHistory()
268  ->GetTopTransform().TransformAxis(gmd);
269  Local3DPoint lnmd = theRotation.get()->transformPoint(ConvertToLocal3DPoint(lmd));
270  float theThetaAtEntry = lnmd.theta();
271  float thePhiAtEntry = lnmd.phi();
272 
273  mySimHit = new UpdatablePSimHit(theEntryPoint,theExitPoint,thePabs,theTof,
274  theEnergyLoss,theParticleType,theDetUnitId,
275  theTrackIDInsideTheSimHit,theThetaAtEntry,thePhiAtEntry,
276  theG4ProcTypeEnumerator.get()->processId(theTrack->GetCreatorProcess()));
277  lastId = theDetUnitId;
278  lastTrack = theTrackID;
279 
280  // only for debugging
281  if (printHits) {
282  // point on Geant4 unit (mm)
283  globalEntryPoint = ConvertToLocal3DPoint(preStepPoint->GetPosition());
284  globalExitPoint = ConvertToLocal3DPoint(aStep->GetPostStepPoint()->GetPosition());
285  // in CMS unit (GeV)
286  px = preStepPoint->GetMomentum().x()/CLHEP::GeV;
287  py = preStepPoint->GetMomentum().y()/CLHEP::GeV;
288  pz = preStepPoint->GetMomentum().z()/CLHEP::GeV;
289  oldVolume = preStepPoint->GetPhysicalVolume();
290  pname = theTrack->GetDefinition()->GetParticleName();
291  LogDebug("TrackerSimDebug")<< " Created PSimHit: " << pname
292  << " " << mySimHit->detUnitId() << " " << mySimHit->trackId()
293  << " " << theTrackID
294  << " p= " << aStep->GetPreStepPoint()->GetMomentum().mag()
295  << " " << mySimHit->energyLoss() << " " << mySimHit->entryPoint()
296  << " " << mySimHit->exitPoint();
297  }
298 }
299 
301 {
302  // VI: in past here was a check if a hit is inside a sensitive detector,
303  // this is not needed, because call to senstive detector happens
304  // only inside the volume
305  Local3DPoint theExitPoint = theRotation.get()->transformPoint(LocalPostStepPosition(aStep));
306  float theEnergyLoss = aStep->GetTotalEnergyDeposit()/GeV;
307  mySimHit->setExitPoint(theExitPoint);
308  mySimHit->addEnergyLoss(theEnergyLoss);
309  if (printHits) {
310  globalExitPoint = ConvertToLocal3DPoint(aStep->GetPostStepPoint()->GetPosition());
311  LogDebug("TrackerSimDebug")
312  << " updateHit: for " << aStep->GetTrack()->GetDefinition()->GetParticleName()
313  << " trackID= " << aStep->GetTrack()->GetTrackID() << " deltaEloss= " << theEnergyLoss
314  << "\n Updated PSimHit: " << mySimHit->detUnitId() << " " << mySimHit->trackId()
315  << " " << mySimHit->energyLoss() << " " << mySimHit->entryPoint()
316  << " " << mySimHit->exitPoint();
317  }
318 }
319 
320 bool TkAccumulatingSensitiveDetector::newHit(const G4Step * aStep)
321 {
322  const G4Track * theTrack = aStep->GetTrack();
323 
324  // for neutral particles do not merge hits (V.I.)
325  if(0.0 == theTrack->GetDefinition()->GetPDGCharge()) return true;
326 
327  uint32_t theDetUnitId = setDetUnitId(aStep);
328  int theTrackID = theTrack->GetTrackID();
329 
330  LogDebug("TrackerSimDebug")<< "newHit: OLD(detID,trID) = (" << lastId << "," << lastTrack
331  << "), NEW = (" << theDetUnitId << "," << theTrackID
332  << ") Step length(mm)= " << aStep->GetStepLength()
333  << " Edep= " << aStep->GetTotalEnergyDeposit()
334  << " p= " << aStep->GetPreStepPoint()->GetMomentum().mag();
335  return ((theTrackID == lastTrack) && (lastId == theDetUnitId) && closeHit(aStep))
336  ? false : true;
337 }
338 
340 {
341  const float tolerance2 = 0.0025f; // (0.5 mm)^2 are allowed between entry and exit
342  Local3DPoint theEntryPoint = theRotation.get()->transformPoint(LocalPreStepPosition(aStep));
343  LogDebug("TrackerSimDebug")<< " closeHit: distance = "
344  << (mySimHit->exitPoint()-theEntryPoint).mag();
345 
346  return ((mySimHit->exitPoint()-theEntryPoint).mag2() < tolerance2) ? true : false;
347 }
348 
350 {
351  LogDebug("TrackerSimDebug")<< " Saving the last hit in a ROU " << GetName();
352  if (mySimHit != nullptr) sendHit();
353 }
354 
356 {
357  clearHits();
358  eventno = (*i)()->GetEventID();
359  delete mySimHit;
360  mySimHit = nullptr;
361 }
362 
364 {
366  const edm::EventSetup* es = (*i)();
367  es->get<IdealGeometryRecord>().get( pDD );
368 
370  es->get<IdealGeometryRecord>().get(pView);
371 
372  theNumberingScheme.reset(&(numberingScheme(*pView,*pDD)));
373 }
374 
376 {
377  slaveLowTof.get()->Initialize();
378  slaveHighTof.get()->Initialize();
379 }
380 
382 
383  if (slaveLowTof.get()->name() == hname) { cc=slaveLowTof.get()->hits(); }
384  else if (slaveHighTof.get()->name() == hname) { cc=slaveHighTof.get()->hits();}
385 }
#define LogDebug(id)
T getParameter(std::string const &) const
static const TGPicture * info(bool iBackgroundIsBlack)
const double GeV
Definition: MathUtil.h:16
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
std::unique_ptr< TrackingSlaveSD > slaveHighTof
bool storeTrack() const
std::unique_ptr< FrameRotation > theRotation
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector &point) const
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
std::unique_ptr< const G4ProcessTypeEnumerator > theG4ProcTypeEnumerator
void printHitData(const std::string &, float, float, float) const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
TkAccumulatingSensitiveDetector(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
void setExitPoint(const Local3DPoint &exit)
bool ProcessHits(G4Step *, G4TouchableHistory *) override
uint32_t setDetUnitId(const G4Step *) override
#define nullptr
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
void printGlobalMomentum(float, float, float) const
std::unique_ptr< TrackingSlaveSD > slaveLowTof
static TrackerG4SimHitNumberingScheme & numberingScheme(const DDCompactView &cpv, const GeometricDet &det)
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
void fillHits(edm::PSimHitContainer &, const std::string &) override
Local3DPoint exitPoint() const
Exit point in the local Det frame.
Definition: PSimHit.h:38
float timeOfFlight() const
Definition: PSimHit.h:69
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
float pabs() const
fast and more accurate access to momentumAtEntry().mag()
Definition: PSimHit.h:63
TrackInformation * cmsTrackInformation(const G4Track *aTrack)
static int particleID(const G4Track *)
std::unique_ptr< TrackerG4SimHitNumberingScheme > theNumberingScheme
void addEnergyLoss(float eloss)
Local3DPoint LocalPostStepPosition(const G4Step *step) const
void printGlobal(const Local3DPoint &, const Local3DPoint &) const
void setNames(const std::vector< std::string > &)
float energyLoss() const
The energy deposit in the PSimHit, in ???.
Definition: PSimHit.h:75
T get() const
Definition: EventSetup.h:62
Local3DPoint LocalPreStepPosition(const G4Step *step) const
unsigned int trackId() const
Definition: PSimHit.h:102
std::vector< PSimHit > PSimHitContainer
step
void EndOfEvent(G4HCofThisEvent *) override
void printLocal(const Local3DPoint &, const Local3DPoint &) const
Local3DPoint entryPoint() const
Entry point in the local Det frame.
Definition: PSimHit.h:35
unsigned int detUnitId() const
Definition: PSimHit.h:93
void startNewSimHit(const std::string &, const std::string &, int, int, int, int)