CMS 3D CMS Logo

MuonSensitiveDetector.cc
Go to the documentation of this file.
8 
16 
21 
25 
26 #include "G4VProcess.hh"
27 #include "G4EventManager.hh"
28 #include "G4SystemOfUnits.hh"
29 #include "G4Step.hh"
30 #include "G4StepPoint.hh"
31 #include "G4Track.hh"
32 
34 
35 #include <iostream>
36 
37 //#define EDM_ML_DEBUG
38 
40  const MuonOffsetMap* offmap,
42  const SensitiveDetectorCatalog& clg,
43  edm::ParameterSet const& p,
44  const SimTrackManager* manager)
45  : SensitiveTkDetector(name, clg),
46  thePV(nullptr),
47  theHit(nullptr),
48  theDetUnitId(0),
49  newDetUnitId(0),
50  theTrackID(0),
51  thePrinter(nullptr),
52  theManager(manager) {
53  // Here simply create 1 MuonSlaveSD for the moment
54  //
55  bool dd4hep = p.getParameter<bool>("g4GeometryDD4hepSource");
56  edm::ParameterSet muonSD = p.getParameter<edm::ParameterSet>("MuonSD");
57  printHits_ = muonSD.getParameter<bool>("PrintHits");
58  ePersistentCutGeV_ = muonSD.getParameter<double>("EnergyThresholdForPersistency") / CLHEP::GeV; //Default 1. GeV
59  allMuonsPersistent_ = muonSD.getParameter<bool>("AllMuonsPersistent");
60  haveDemo_ = muonSD.getParameter<bool>("HaveDemoChambers");
61  demoGEM_ = muonSD.getParameter<bool>("UseDemoHitGEM");
62  demoRPC_ = muonSD.getParameter<bool>("UseDemoHitRPC");
63 
64 #ifdef EDM_ML_DEBUG
65  edm::LogVerbatim("MuonSim") << "create MuonSubDetector " << name << " with dd4hep flag " << dd4hep
66  << " Flags for Demonstration chambers " << haveDemo_ << " for GEM " << demoGEM_
67  << " for RPC " << demoRPC_;
68 #endif
70 
71  G4String sdet = "unknown";
72  if (detector->isEndcap()) {
74  sdet = "Endcap";
75  } else if (detector->isRPC()) {
77  sdet = "RPC";
78  } else if (detector->isGEM()) {
80  sdet = "GEM";
81  } else if (detector->isME0()) {
83  sdet = "ME0";
84  } else {
86  if (detector->isBarrel())
87  sdet = "Barrel";
88  }
92 
93  if (printHits_) {
94  thePrinter = new SimHitPrinter("HitPositionOSCAR.dat");
95  }
96 
97  edm::LogVerbatim("MuonSim") << " of type " << sdet << " <" << GetName() << "> EnergyThresholdForPersistency(GeV) "
98  << ePersistentCutGeV_ / CLHEP::GeV << " allMuonsPersistent: " << allMuonsPersistent_;
99 
101 }
102 
104  delete g4numbering;
105  delete numbering;
106  delete slaveMuon;
107  delete theRotation;
108  delete detector;
110 }
111 
113  clearHits();
114  //----- Initialize variables to check if two steps belong to same hit
115  thePV = nullptr;
116  theDetUnitId = 0;
117  theTrackID = 0;
118 }
119 
121 #ifdef EDM_ML_DEBUG
122  edm::LogVerbatim("MuonSim") << "MuonSensitiveDetector::clearHits";
123 #endif
125 }
126 
127 bool MuonSensitiveDetector::ProcessHits(G4Step* aStep, G4TouchableHistory* ROhist) {
128 #ifdef EDM_ML_DEBUG
129  edm::LogVerbatim("MuonSim") << " MuonSensitiveDetector::ProcessHits " << InitialStepPosition(aStep, WorldCoordinates);
130 #endif
131 
132  if (aStep->GetTotalEnergyDeposit() > 0.) {
133  newDetUnitId = setDetUnitId(aStep);
134 #ifdef EDM_ML_DEBUG
135  G4VPhysicalVolume* vol = aStep->GetPreStepPoint()->GetTouchable()->GetVolume(0);
136  std::string namx = static_cast<std::string>(vol->GetName());
137  std::string name = namx.substr(0, 2);
138  if (name == "RE")
139  edm::LogVerbatim("MuonSim") << "DETID " << namx << " " << RPCDetId(newDetUnitId);
140 #endif
141  if (newHit(aStep)) {
142  saveHit();
143  createHit(aStep);
144  } else {
145  updateHit(aStep);
146  }
147  thePV = aStep->GetPreStepPoint()->GetPhysicalVolume();
148  theTrackID = aStep->GetTrack()->GetTrackID();
150  }
151  return true;
152 }
153 
154 uint32_t MuonSensitiveDetector::setDetUnitId(const G4Step* aStep) {
156 
157 #ifdef EDM_ML_DEBUG
158  std::stringstream MuonBaseNumber;
159  MuonBaseNumber << "MuonNumbering :: number of levels = " << num.getLevels() << std::endl;
160  MuonBaseNumber << "Level \t SuperNo \t BaseNo" << std::endl;
161  for (int level = 1; level <= num.getLevels(); level++) {
162  MuonBaseNumber << level << " \t " << num.getSuperNo(level) << " \t " << num.getBaseNo(level) << std::endl;
163  }
164  std::string MuonBaseNumbr = MuonBaseNumber.str();
165 
166  edm::LogVerbatim("MuonSim") << "MuonSensitiveDetector::setDetUnitId :: " << MuonBaseNumbr;
167  edm::LogVerbatim("MuonSim") << "MuonSensitiveDetector::setDetUnitId :: MuonDetUnitId = "
169 #endif
171 }
172 
173 bool MuonSensitiveDetector::newHit(const G4Step* aStep) {
174  return (!theHit || (aStep->GetTrack()->GetTrackID() != theTrackID) ||
175  (aStep->GetPreStepPoint()->GetPhysicalVolume() != thePV) || newDetUnitId != theDetUnitId);
176 }
177 
178 void MuonSensitiveDetector::createHit(const G4Step* aStep) {
179  Local3DPoint theEntryPoint;
180  Local3DPoint theExitPoint;
181 
182  if (detector->isBarrel()) {
183  // 1 levels up
184  theEntryPoint = cmsUnits(theRotation->transformPoint(InitialStepPositionVsParent(aStep, 1), aStep));
185  theExitPoint = cmsUnits(theRotation->transformPoint(FinalStepPositionVsParent(aStep, 1), aStep));
186  } else if (detector->isEndcap()) {
187  // save local z at current level
188  theEntryPoint = theRotation->transformPoint(InitialStepPosition(aStep, LocalCoordinates), aStep);
189  theExitPoint = theRotation->transformPoint(FinalStepPosition(aStep, LocalCoordinates), aStep);
190  float zentry = theEntryPoint.z();
191  float zexit = theExitPoint.z();
192  // 4 levels up
195  // reset local z from z wrt deep-parent volume to z wrt low-level volume
196  theEntryPoint = cmsUnits(Local3DPoint(tempEntry.x(), tempEntry.y(), zentry));
197  theExitPoint = cmsUnits(Local3DPoint(tempExit.x(), tempExit.y(), zexit));
198  } else {
199  theEntryPoint = cmsUnits(theRotation->transformPoint(InitialStepPosition(aStep, LocalCoordinates), aStep));
200  theExitPoint = cmsUnits(theRotation->transformPoint(FinalStepPosition(aStep, LocalCoordinates), aStep));
201  }
202 
203  const G4Track* theTrack = aStep->GetTrack();
204  const G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
205 
206  float thePabs = preStepPoint->GetMomentum().mag() / CLHEP::GeV;
207  float theTof = preStepPoint->GetGlobalTime() / CLHEP::nanosecond;
208  float theEnergyLoss = aStep->GetTotalEnergyDeposit() / CLHEP::GeV;
209  int theParticleType = G4TrackToParticleID::particleID(theTrack);
210 
212  thePV = preStepPoint->GetPhysicalVolume();
213  theTrackID = theTrack->GetTrackID();
214 
215  // convert momentum direction it to local frame
216  const G4ThreeVector& gmd = preStepPoint->GetMomentumDirection();
217  G4ThreeVector lmd = static_cast<const G4TouchableHistory*>(preStepPoint->GetTouchable())
218  ->GetHistory()
219  ->GetTopTransform()
220  .TransformAxis(gmd);
222  lnmd = theRotation->transformPoint(lnmd, aStep);
223  float theThetaAtEntry = lnmd.theta();
224  float thePhiAtEntry = lnmd.phi();
225 
226  theHit = new UpdatablePSimHit(theEntryPoint,
227  theExitPoint,
228  thePabs,
229  theTof,
230  theEnergyLoss,
231  theParticleType,
232  theDetUnitId,
233  theTrackID,
234  theThetaAtEntry,
235  thePhiAtEntry,
236  theG4ProcessTypeEnumerator->processId(theTrack->GetCreatorProcess()));
237 
238  // Make track persistent
239  int thePID = std::abs(theTrack->GetDefinition()->GetPDGEncoding());
240  //---VI - in parameters cut in energy is declared but applied to momentum
241  if (thePabs > ePersistentCutGeV_ || (thePID == 13 && allMuonsPersistent_)) {
243  info->storeTrack(true);
244  }
245 
246 #ifdef EDM_ML_DEBUG
247  edm::LogVerbatim("MuonSim") << "=== NEW Muon hit for " << GetName() << " Edep(GeV)= " << theEnergyLoss << " "
248  << thePV->GetLogicalVolume()->GetName();
249  const G4VProcess* p = aStep->GetPostStepPoint()->GetProcessDefinedStep();
250  const G4VProcess* p2 = aStep->GetPreStepPoint()->GetProcessDefinedStep();
251  G4String sss = "";
252  if (p)
253  sss += " POST PROCESS: " + p->GetProcessName();
254  if (p2)
255  sss += "; PRE PROCESS: " + p2->GetProcessName();
256  if (!sss.empty())
257  edm::LogVerbatim("MuonSim") << sss;
258  edm::LogVerbatim("MuonSim") << " theta= " << theThetaAtEntry << " phi= " << thePhiAtEntry << " Pabs(GeV/c) "
259  << thePabs << " Eloss(GeV)= " << theEnergyLoss << " Tof(ns)= " << theTof
260  << " trackID= " << theTrackID << " detID= " << theDetUnitId << "\n Local: entry "
261  << theEntryPoint << " exit " << theExitPoint << " delta "
262  << (theExitPoint - theEntryPoint) << "\n Global: entry "
263  << aStep->GetPreStepPoint()->GetPosition() << " exit "
264  << aStep->GetPostStepPoint()->GetPosition();
265 #endif
266 }
267 
268 void MuonSensitiveDetector::updateHit(const G4Step* aStep) {
269  Local3DPoint theExitPoint;
270 
271  if (detector->isBarrel()) {
272  theExitPoint = cmsUnits(theRotation->transformPoint(FinalStepPositionVsParent(aStep, 1), aStep));
273  } else if (detector->isEndcap()) {
274  // save local z at current level
275  theExitPoint = theRotation->transformPoint(FinalStepPosition(aStep, LocalCoordinates), aStep);
276  float zexit = theExitPoint.z();
277  Local3DPoint tempExitPoint = theRotation->transformPoint(FinalStepPositionVsParent(aStep, 4), aStep);
278  theExitPoint = cmsUnits(Local3DPoint(tempExitPoint.x(), tempExitPoint.y(), zexit));
279  } else {
280  theExitPoint = cmsUnits(theRotation->transformPoint(FinalStepPosition(aStep, LocalCoordinates), aStep));
281  }
282 
283  float theEnergyLoss = aStep->GetTotalEnergyDeposit() / CLHEP::GeV;
284 
285  theHit->updateExitPoint(theExitPoint);
286  theHit->addEnergyLoss(theEnergyLoss);
287 
288 #ifdef EDM_ML_DEBUG
289  edm::LogVerbatim("MuonSim") << "=== NEW Update muon hit for " << GetName() << " Edep(GeV)= " << theEnergyLoss << " "
290  << thePV->GetLogicalVolume()->GetName();
291  const G4VProcess* p = aStep->GetPostStepPoint()->GetProcessDefinedStep();
292  const G4VProcess* p2 = aStep->GetPreStepPoint()->GetProcessDefinedStep();
293  G4String sss = "";
294  if (p)
295  sss += " POST PROCESS: " + p->GetProcessName();
296  if (p2)
297  sss += "; PRE PROCESS: " + p2->GetProcessName();
298  if (!sss.empty())
299  edm::LogVerbatim("MuonSim") << sss;
300  edm::LogVerbatim("MuonSim") << " delEloss(GeV)= " << theEnergyLoss
301  << " Tof(ns)= " << aStep->GetPreStepPoint()->GetGlobalTime() / CLHEP::nanosecond
302  << " trackID= " << theTrackID << " detID= " << theDetUnitId << " exit " << theExitPoint;
303 #endif
304 }
305 
307  if (theHit) {
308  if (acceptHit(theHit->detUnitId())) {
309  if (printHits_) {
313  }
314  // hit is included into hit collection
316  }
317  delete theHit;
318  theHit = nullptr;
319  }
320 }
321 
322 void MuonSensitiveDetector::EndOfEvent(G4HCofThisEvent*) { saveHit(); }
323 
325  if (slaveMuon->name() == hname) {
326  cc = slaveMuon->hits();
327  }
328 }
329 
330 Local3DPoint MuonSensitiveDetector::InitialStepPositionVsParent(const G4Step* currentStep, G4int levelsUp) {
331  const G4StepPoint* preStepPoint = currentStep->GetPreStepPoint();
332  const G4ThreeVector& globalCoordinates = preStepPoint->GetPosition();
333 
334  const G4TouchableHistory* theTouchable = (const G4TouchableHistory*)(preStepPoint->GetTouchable());
335 
336  G4int depth = theTouchable->GetHistory()->GetDepth();
337  G4ThreeVector localCoordinates =
338  theTouchable->GetHistory()->GetTransform(depth - levelsUp).TransformPoint(globalCoordinates);
339 
340  return ConvertToLocal3DPoint(localCoordinates);
341 }
342 
343 Local3DPoint MuonSensitiveDetector::FinalStepPositionVsParent(const G4Step* currentStep, G4int levelsUp) {
344  const G4StepPoint* postStepPoint = currentStep->GetPostStepPoint();
345  const G4StepPoint* preStepPoint = currentStep->GetPreStepPoint();
346  const G4ThreeVector& globalCoordinates = postStepPoint->GetPosition();
347 
348  const G4TouchableHistory* theTouchable = (const G4TouchableHistory*)(preStepPoint->GetTouchable());
349 
350  G4int depth = theTouchable->GetHistory()->GetDepth();
351  G4ThreeVector localCoordinates =
352  theTouchable->GetHistory()->GetTransform(depth - levelsUp).TransformPoint(globalCoordinates);
353 
354  return ConvertToLocal3DPoint(localCoordinates);
355 }
356 
358  if (id == 0) {
359 #ifdef EDM_ML_DEBUG
360  edm::LogVerbatim("MuonSim") << "DetId " << id << " Flag " << false;
361 #endif
362  return false;
363  }
364  bool flag(true);
365  if (haveDemo_) {
366  int subdet = DetId(id).subdetId();
367  if (subdet == MuonSubdetId::GEM) {
368  if (GEMDetId(id).station() == 2)
369  flag = demoGEM_;
370  } else if (subdet == MuonSubdetId::RPC) {
371  if ((RPCDetId(id).region() != 0) && (RPCDetId(id).ring() == 1) && (RPCDetId(id).station() > 2))
372  flag = demoRPC_;
373  }
374  }
375 #ifdef EDM_ML_DEBUG
376  int subdet = DetId(id).subdetId();
377  if (subdet == MuonSubdetId::RPC)
378  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " RPC " << RPCDetId(id) << " Flag "
379  << flag;
380  else if (subdet == MuonSubdetId::GEM)
381  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " GEM " << GEMDetId(id) << " Flag "
382  << flag;
383  else if (subdet == MuonSubdetId::ME0)
384  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " " << ME0DetId(id) << " Flag " << flag;
385  else if (subdet == MuonSubdetId::CSC)
386  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " CSC Flag " << flag;
387  else if (subdet == MuonSubdetId::DT)
388  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " DT Flag " << flag;
389  else
390  edm::LogVerbatim("MuonSim") << "DetId " << std::hex << id << std::dec << " Unknown Flag " << flag;
391 #endif
392  return flag;
393 }
Log< level::Info, true > LogVerbatim
const G4VPhysicalVolume * thePV
std::string name() const
static const TGPicture * info(bool iBackgroundIsBlack)
Local3DPoint FinalStepPositionVsParent(const G4Step *currentStep, G4int levelsUp)
static constexpr int GEM
Definition: MuonSubdetId.h:14
int baseNumberToUnitNumber(const MuonBaseNumber &) const override
G4bool ProcessHits(G4Step *, G4TouchableHistory *) override
unsigned int detUnitId() const
Definition: PSimHit.h:97
void updateHit(const G4Step *)
T z() const
Definition: PV3DBase.h:61
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
virtual Local3DPoint transformPoint(const Local3DPoint &, const G4Step *) const
Local3DPoint exitPoint() const
Exit point in the local Det frame.
Definition: PSimHit.h:46
std::string name()
MuonSubDetector * detector
Local3DPoint FinalStepPosition(const G4Step *step, coordinates) const
void fillHits(edm::PSimHitContainer &, const std::string &) override
void printLocal(LocalPoint, LocalPoint) const
void printId(int) const
Local3DPoint cmsUnits(const Local3DPoint &v)
std::vector< PSimHit > & hits()
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void createHit(const G4Step *)
void updateExitPoint(const Local3DPoint &exit)
virtual void Initialize()
G4ProcessTypeEnumerator * theG4ProcessTypeEnumerator
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector &point) const
T mag() const
Definition: PV3DBase.h:64
bool newHit(const G4Step *)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const MuonFrameRotation * theRotation
Local3DPoint entryPoint() const
Entry point in the local Det frame.
Definition: PSimHit.h:43
MuonSensitiveDetector(const std::string &, const MuonOffsetMap *, const MuonGeometryConstants &, const SensitiveDetectorCatalog &, edm::ParameterSet const &p, const SimTrackManager *)
static constexpr int ME0
Definition: MuonSubdetId.h:15
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
Local3DPoint InitialStepPosition(const G4Step *step, coordinates) const
const SimTrackManager * theManager
TrackInformation * cmsTrackInformation(const G4Track *aTrack)
static int particleID(const G4Track *)
MuonBaseNumber PhysicalVolumeToBaseNumber(const G4Step *aStep)
uint32_t setDetUnitId(const G4Step *) override
Definition: DetId.h:17
void addEnergyLoss(float eloss)
void startNewSimHit(std::string)
void EndOfEvent(G4HCofThisEvent *) override
MuonSimHitNumberingScheme * numbering
static constexpr int RPC
Definition: MuonSubdetId.h:13
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
unsigned int processId(const G4VProcess *p) const
static constexpr int DT
Definition: MuonSubdetId.h:11
static constexpr int CSC
Definition: MuonSubdetId.h:12
Local3DPoint InitialStepPositionVsParent(const G4Step *currentStep, G4int levelsUp)
Geom::Theta< T > theta() const
Definition: PV3DBase.h:72
UpdatablePSimHit * theHit
MuonG4Numbering * g4numbering
Point3DBase< float, LocalTag > Local3DPoint
Definition: LocalPoint.h:9