CMS 3D CMS Logo

MuonSensitiveDetector.cc
Go to the documentation of this file.
9 
11 
14 
18 
22 
23 #include "G4SDManager.hh"
24 #include "G4VProcess.hh"
25 #include "G4EventManager.hh"
26 #include "G4SystemOfUnits.hh"
27 
29 
30 #include <iostream>
31 
33  const DDCompactView & cpv,
34  const SensitiveDetectorCatalog & clg,
35  edm::ParameterSet const & p,
36  const SimTrackManager* manager)
37  : SensitiveTkDetector(name, cpv, clg, p),
38  thePV(nullptr), theHit(nullptr), theDetUnitId(0), theTrackID(0), theManager(manager)
39 {
40  edm::ParameterSet m_MuonSD = p.getParameter<edm::ParameterSet>("MuonSD");
41  STenergyPersistentCut = m_MuonSD.getParameter<double>("EnergyThresholdForPersistency");//Default 1. GeV
42  STallMuonsPersistent = m_MuonSD.getParameter<bool>("AllMuonsPersistent");
43  printHits = m_MuonSD.getParameter<bool>("PrintHits");
44 
45  //
46  // Here simply create 1 MuonSlaveSD for the moment
47  //
48 
49  LogDebug("MuonSimDebug") << "create MuonSubDetector "<<name<<std::endl;
50 
51  detector = new MuonSubDetector(name);
52 
53  LogDebug("MuonSimDebug") << "create MuonFrameRotation"<<std::endl;
54 
55  //The constants take time to calculate and are needed by many helpers
57  if (detector->isEndcap()) {
58  // cout << "MuonFrameRotation create MuonEndcapFrameRotation"<<endl;
60  } else if (detector->isRPC()) {
61  // cout << "MuonFrameRotation create MuonRPCFrameRotation"<<endl;
62  theRotation=new MuonRPCFrameRotation( constants );
63  } else if (detector->isGEM()) {
64  // cout << "MuonFrameRotation create MuonGEMFrameRotation"<<endl;
65  theRotation=new MuonGEMFrameRotation( constants );
66  } else if (detector->isME0()) {
67  // cout << "MuonFrameRotation create MuonME0FrameRotation"<<endl;
68  theRotation=new MuonME0FrameRotation( constants );
69  } else {
70  theRotation = nullptr;
71  }
72  LogDebug("MuonSimDebug") << "create MuonSlaveSD"<<std::endl;
74  LogDebug("MuonSimDebug") << "create MuonSimHitNumberingScheme"<<std::endl;
76  g4numbering = new MuonG4Numbering(constants);
77 
78  if (printHits) {
79  thePrinter = new SimHitPrinter("HitPositionOSCAR.dat");
80  }
81 
82 
83  edm::LogInfo("MuonSimDebug") << " EnergyThresholdForPersistency "
84  << STenergyPersistentCut << " AllMuonsPersistent "
86 
89 }
90 
92  delete g4numbering;
93  delete numbering;
94  delete slaveMuon;
95  delete theRotation;
96  delete detector;
97 
99 
100  delete myG4TrackToParticleID;
101 }
102 
104  clearHits();
105 
106  //----- Initialize variables to check if two steps belong to same hit
107  thePV = nullptr;
108  theDetUnitId = 0;
109  theTrackID = 0;
110 
111 }
112 
113 void MuonSensitiveDetector::update(const ::EndOfEvent *)
114 {}
115 
116 
118 {
119  LogDebug("MuonSimDebug") << "MuonSensitiveDetector::clearHits"<<std::endl;
121 }
122 
123 bool MuonSensitiveDetector::ProcessHits(G4Step * aStep, G4TouchableHistory * ROhist)
124 {
125  LogDebug("MuonSimDebug") <<" MuonSensitiveDetector::ProcessHits "<<InitialStepPosition(aStep,WorldCoordinates)<<std::endl;
126 
127  // TimeMe t1( theHitTimer, false);
128 
129  if (aStep->GetTotalEnergyDeposit()>0.){
130  // do not count neutrals that are killed by User Limits MinEKine
131  if( aStep->GetTrack()->GetDynamicParticle()->GetCharge() != 0 ){
132 
133  if (newHit(aStep)) {
134  saveHit();
135  createHit(aStep);
136  } else {
137  updateHit(aStep);
138  }
139  return true;
140  } else {
141  storeVolumeAndTrack(aStep);
142  return false;
143  }
144  }
145  return false;
146 }
147 
148 uint32_t MuonSensitiveDetector::setDetUnitId(const G4Step * aStep)
149 {
150  // G4VPhysicalVolume * pv = aStep->GetPreStepPoint()->GetPhysicalVolume();
152 
153  std::stringstream MuonBaseNumber;
154  // LogDebug :: Print MuonBaseNumber
155  MuonBaseNumber << "MuonNumbering :: number of levels = "<<num.getLevels()<<std::endl;
156  MuonBaseNumber << "Level \t SuperNo \t BaseNo"<<std::endl;
157  for (int level=1;level<=num.getLevels();level++) {
158  MuonBaseNumber << level << " \t " << num.getSuperNo(level)
159  << " \t " << num.getBaseNo(level) << std::endl;
160  }
161  std::string MuonBaseNumbr = MuonBaseNumber.str();
162 
163  LogDebug("MuonSimDebug") <<"MuonSensitiveDetector::setDetUnitId :: "<<MuonBaseNumbr;
164  LogDebug("MuonSimDebug") <<"MuonSensitiveDetector::setDetUnitId :: MuonDetUnitId = "<<(numbering->baseNumberToUnitNumber(num));
165  return numbering->baseNumberToUnitNumber(num);
166 }
167 
168 
170  if (theRotation !=nullptr ) {
171  return theRotation->transformPoint(in,step);
172  }
173  return (in);
174 }
175 
177  return Local3DPoint(in.x()/cm,in.y()/cm,in.z()/cm);
178 }
179 
181  return Global3DPoint(in.x()/cm,in.y()/cm,in.z()/cm);
182 }
183 
184 void MuonSensitiveDetector::storeVolumeAndTrack(const G4Step * aStep) {
185  G4VPhysicalVolume* pv = aStep->GetPreStepPoint()->GetPhysicalVolume();
186  G4Track * t = aStep->GetTrack();
187  thePV=pv;
188  theTrackID=t->GetTrackID();
189 }
190 
191 bool MuonSensitiveDetector::newHit(G4Step * aStep){
192 
193  G4VPhysicalVolume* pv = aStep->GetPreStepPoint()->GetPhysicalVolume();
194  G4Track * t = aStep->GetTrack();
195  uint32_t currentUnitId=setDetUnitId(aStep);
196  LogDebug("MuonSimDebug") <<"MuonSensitiveDetector::newHit :: currentUnitId = "<<currentUnitId;
197  unsigned int currentTrackID=t->GetTrackID();
198  //unsigned int currentEventID=G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID();
199  bool changed=((pv!=thePV) ||
200  (currentUnitId!=theDetUnitId) ||
201  (currentTrackID!=theTrackID));
202  return changed;
203 }
204 
205 void MuonSensitiveDetector::createHit(G4Step * aStep){
206 
207  G4Track * theTrack = aStep->GetTrack();
208 
209  Local3DPoint theEntryPoint;
210  Local3DPoint theExitPoint;
211 
212  if (detector->isBarrel()) {
213  theEntryPoint= toOrcaUnits(toOrcaRef(InitialStepPositionVsParent(aStep,1),aStep)); // 1 level up
214  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPositionVsParent(aStep,1),aStep));
215  } else if (detector->isEndcap()) {
216  // save local z at current level
217  theEntryPoint= toOrcaUnits(toOrcaRef(InitialStepPosition(aStep,LocalCoordinates),aStep));
218  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPosition(aStep,LocalCoordinates),aStep));
219  float zentry = theEntryPoint.z();
220  float zexit = theExitPoint.z();
221  Local3DPoint tempEntryPoint= toOrcaUnits(toOrcaRef(InitialStepPositionVsParent(aStep,4),aStep)); // 4 levels up
222  Local3DPoint tempExitPoint= toOrcaUnits(toOrcaRef(FinalStepPositionVsParent(aStep,4),aStep));
223  // reset local z from z wrt deep-parent volume to z wrt low-level volume
224  theEntryPoint = Local3DPoint( tempEntryPoint.x(), tempEntryPoint.y(), zentry );
225  theExitPoint = Local3DPoint( tempExitPoint.x(), tempExitPoint.y(), zexit );
226  } else {
227  theEntryPoint= toOrcaUnits(toOrcaRef(InitialStepPosition(aStep,LocalCoordinates),aStep));
228  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPosition(aStep,LocalCoordinates),aStep));
229  }
230 
231  float thePabs = aStep->GetPreStepPoint()->GetMomentum().mag()/GeV;
232  float theTof = aStep->GetPreStepPoint()->GetGlobalTime()/nanosecond;
233  float theEnergyLoss = aStep->GetTotalEnergyDeposit()/GeV;
234  // int theParticleType = theTrack->GetDefinition()->GetPDGEncoding();
235  int theParticleType = myG4TrackToParticleID->particleID(theTrack);
236  G4ThreeVector gmd = aStep->GetPreStepPoint()->GetMomentumDirection();
237  G4ThreeVector lmd = ((const G4TouchableHistory *)(aStep->GetPreStepPoint()->GetTouchable()))->GetHistory()
238  ->GetTopTransform().TransformAxis(gmd);
239  Local3DPoint lnmd = toOrcaRef(ConvertToLocal3DPoint(lmd),aStep);
240  float theThetaAtEntry = lnmd.theta();
241  float thePhiAtEntry = lnmd.phi();
242 
243  storeVolumeAndTrack( aStep );
244  theDetUnitId = setDetUnitId(aStep);
245 
246  Global3DPoint theGlobalPos;
247  if (printHits) {
248  Local3DPoint theGlobalHelp = InitialStepPosition(aStep,WorldCoordinates);
249  theGlobalEntry = toOrcaUnits(Global3DPoint (theGlobalHelp.x(),theGlobalHelp.y(),theGlobalHelp.z()));
250 
251  G4StepPoint * preStepPoint = aStep->GetPreStepPoint();
252  const G4TouchableHistory * theTouchable=(const G4TouchableHistory *)
253  (preStepPoint->GetTouchable());
254  theGlobalHelp=ConvertToLocal3DPoint(theTouchable->GetTranslation());
255  theGlobalPos = toOrcaUnits(Global3DPoint (theGlobalHelp.x(),theGlobalHelp.y(),theGlobalHelp.z()));
256  // const G4RotationMatrix * theGlobalRot = theTouchable->GetRotation();
257  }
258 
259  LogDebug("MuonSimDebug") << "MuonSensitiveDetector::createHit UpdatablePSimHit"<<std::endl;
260 
261  theHit = new UpdatablePSimHit(theEntryPoint,theExitPoint,thePabs,theTof,
262  theEnergyLoss,theParticleType,theDetUnitId,
263  theTrackID,theThetaAtEntry,thePhiAtEntry,
264  theG4ProcessTypeEnumerator->processId(theTrack->GetCreatorProcess()));
265 
266 
267  LogDebug("MuonSimDebug") <<"=== NEW ==================> ELOSS = "<<theEnergyLoss<<" "
268  <<thePV->GetLogicalVolume()->GetName()<<std::endl;
269  const G4VProcess* p = aStep->GetPostStepPoint()->GetProcessDefinedStep();
270  const G4VProcess* p2 = aStep->GetPreStepPoint()->GetProcessDefinedStep();
271  if (p)
272  LogDebug("MuonSimDebug") <<" POST PROCESS = "<<p->GetProcessName()<<std::endl;
273  if (p2)
274  LogDebug("MuonSimDebug") <<" PRE PROCESS = "<<p2->GetProcessName()<<std::endl;
275  LogDebug("MuonSimDebug") << "newhit theta " << theThetaAtEntry<<std::endl;
276  LogDebug("MuonSimDebug") << "newhit phi " << thePhiAtEntry<<std::endl;
277  LogDebug("MuonSimDebug") << "newhit pabs " << thePabs<<std::endl;
278  LogDebug("MuonSimDebug") << "newhit tof " << theTof<<std::endl;
279  LogDebug("MuonSimDebug") << "newhit track " << theTrackID<<std::endl;
280  LogDebug("MuonSimDebug") << "newhit entry " << theEntryPoint<<std::endl;
281  LogDebug("MuonSimDebug") << "newhit exit " << theExitPoint<<std::endl;
282  LogDebug("MuonSimDebug") << "newhit eloss " << theEnergyLoss << std::endl;
283  LogDebug("MuonSimDebug") << "newhit detid " << theDetUnitId<<std::endl;
284  LogDebug("MuonSimDebug") << "newhit delta " << (theExitPoint-theEntryPoint)<<std::endl;
285  LogDebug("MuonSimDebug") << "newhit deltu " << (theExitPoint-theEntryPoint).unit();
286  LogDebug("MuonSimDebug") << " " << (theExitPoint-theEntryPoint).mag()<<std::endl;
287  LogDebug("MuonSimDebug") << "newhit glob " << theGlobalEntry<<std::endl;
288  LogDebug("MuonSimDebug") << "newhit dpos " << theGlobalPos<<std::endl;
289  LogDebug("MuonSimDebug") << "newhit drot " << std::endl;
290  // theGlobalRot->print(LogDebug("MuonSimDebug"));
291 
292 
293  //
294  //----- SimTracks: Make it persistent?
295  //
296  int thePID = theTrack->GetDefinition()->GetPDGEncoding();
297  LogDebug("MuonSimDebug") << " checking simtrack " << thePID << " " << thePabs << " STenergyPersistentCut " << STenergyPersistentCut << std::endl;
298 
299  if( thePabs*GeV > STenergyPersistentCut
300  || ( abs(thePID) == 13 && STallMuonsPersistent ) ){
302  LogDebug("MuonSimDebug") <<" track leaving hit in muons made selected for persistency"<<std::endl;
303 
304  info->storeTrack(true);
305  }
306 
307 }
308 
309 void MuonSensitiveDetector::updateHit(G4Step * aStep){
310  // float thePabs = aStep->GetPreStepPoint()->GetMomentum().mag()/GeV;
311  // Local3DPoint theEntryPoint= InitialStepPosition(aStep,LocalCoordinates);
312 
313 
314  Local3DPoint theExitPoint;
315 
316  if (detector->isBarrel()) {
317  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPositionVsParent(aStep,1),aStep));
318  } else if (detector->isEndcap()) {
319  // save local z at current level
320  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPosition(aStep,LocalCoordinates),aStep));
321  float zexit = theExitPoint.z();
322  Local3DPoint tempExitPoint= toOrcaUnits(toOrcaRef(FinalStepPositionVsParent(aStep,4),aStep));
323  theExitPoint = Local3DPoint( tempExitPoint.x(), tempExitPoint.y(), zexit );
324  } else {
325  theExitPoint= toOrcaUnits(toOrcaRef(FinalStepPosition(aStep,LocalCoordinates),aStep));
326  }
327 
328  float theEnergyLoss = aStep->GetTotalEnergyDeposit()/GeV;
329 
330  if( theHit == nullptr ){
331  std::cerr << "!!ERRROR in MuonSensitiveDetector::updateHit. It is called when there is no hit " << std::endl;
332  }
333 
334  theHit->updateExitPoint(theExitPoint);
335  theHit->addEnergyLoss(theEnergyLoss);
336 
337  LogDebug("MuonSimDebug") <<"=== UPDATE ===============> ELOSS = "<<theEnergyLoss<<" "
338  <<thePV->GetLogicalVolume()->GetName()<<std::endl;
339  const G4VProcess* p = aStep->GetPostStepPoint()->GetProcessDefinedStep();
340  const G4VProcess* p2 = aStep->GetPreStepPoint()->GetProcessDefinedStep();
341  if (p)
342  LogDebug("MuonSimDebug") <<" POST PROCESS = "<<p->GetProcessName()<<std::endl;
343  if (p2)
344  LogDebug("MuonSimDebug") <<" PRE PROCESS = "<<p2->GetProcessName()<<std::endl;
345  LogDebug("MuonSimDebug") << "updhit exit " << theExitPoint<<std::endl;
346  LogDebug("MuonSimDebug") << "updhit eloss " << theHit->energyLoss() <<std::endl;
347  LogDebug("MuonSimDebug") << "updhit detid " << theDetUnitId<<std::endl;
348  LogDebug("MuonSimDebug") << "updhit delta " << (theExitPoint-theHit->entryPoint())<<std::endl;
349  LogDebug("MuonSimDebug") << "updhit deltu " << (theExitPoint-theHit->entryPoint()).unit();
350  LogDebug("MuonSimDebug") << " " << (theExitPoint-theHit->entryPoint()).mag()<<std::endl;
351 
352 }
353 
355 
356  if (theHit) {
357  if (printHits) {
360  // thePrinter->printTrack(theHit->trackId());
361  //thePrinter->printPabs(theHit->pabs());
362  //thePrinter->printEloss(theHit->energyLoss());
365  }
367  // seems the hit does not want to be deleted
368  // done by the hit collection?
369  delete theHit;
370  theHit = nullptr; //set it to 0, because you are checking that is 0
371  }
372 
373 }
374 
376 {
377  G4VUserTrackInformation* temp = gTrack->GetUserInformation();
378  if (temp == nullptr){
379  std::cerr <<" ERROR: no G4VUserTrackInformation available"<<std::endl;
380  abort();
381  }else{
382  TrackInformation* info = dynamic_cast<TrackInformation*>(temp);
383  if (info ==nullptr){
384  std::cerr <<" ERROR: TkSimTrackSelection: the UserInformation does not appear to be a TrackInformation"<<std::endl;
385  abort();
386  }
387  return info;
388  }
389 }
390 
391 void MuonSensitiveDetector::EndOfEvent(G4HCofThisEvent*)
392 {
393 // TimeMe t("MuonSensitiveDetector::EndOfEvent", false);
394  // LogDebug("MuonSimDebug") << "MuonSensitiveDetector::EndOfEvent saving last hit en event " << std::endl;
395  saveHit();
396 }
397 
398 
400  if (slaveMuon->name() == hname) { cc=slaveMuon->hits(); }
401 }
402 
404 
405  G4StepPoint * preStepPoint = currentStep->GetPreStepPoint();
406  const G4ThreeVector& globalCoordinates = preStepPoint->GetPosition();
407 
408  const G4TouchableHistory * theTouchable=(const G4TouchableHistory *)
409  (preStepPoint->GetTouchable());
410 
411  G4int depth = theTouchable->GetHistory()->GetDepth();
412  G4ThreeVector localCoordinates = theTouchable->GetHistory()
413  ->GetTransform(depth-levelsUp).TransformPoint(globalCoordinates);
414 
415  return ConvertToLocal3DPoint(localCoordinates);
416 }
417 
418 Local3DPoint MuonSensitiveDetector::FinalStepPositionVsParent(G4Step * currentStep, G4int levelsUp) {
419 
420  G4StepPoint * postStepPoint = currentStep->GetPostStepPoint();
421  G4StepPoint * preStepPoint = currentStep->GetPreStepPoint();
422  const G4ThreeVector& globalCoordinates = postStepPoint->GetPosition();
423 
424  const G4TouchableHistory * theTouchable = (const G4TouchableHistory *)
425  (preStepPoint->GetTouchable());
426 
427  G4int depth = theTouchable->GetHistory()->GetDepth();
428  G4ThreeVector localCoordinates = theTouchable->GetHistory()
429  ->GetTransform(depth-levelsUp).TransformPoint(globalCoordinates);
430 
431  return ConvertToLocal3DPoint(localCoordinates);
432 }
#define LogDebug(id)
T getParameter(std::string const &) const
Local3DPoint FinalStepPositionVsParent(G4Step *currentStep, G4int levelsUp)
static const TGPicture * info(bool iBackgroundIsBlack)
const double GeV
Definition: MathUtil.h:16
bool storeTrack() const
G4bool ProcessHits(G4Step *, G4TouchableHistory *) override
Local3DPoint ConvertToLocal3DPoint(const G4ThreeVector &point) const
void printLocal(LocalPoint, LocalPoint) const
virtual Local3DPoint transformPoint(const Local3DPoint &, const G4Step *) const =0
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
std::string name() const
int getBaseNo(int level) const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
T y() const
Definition: PV3DBase.h:63
void storeVolumeAndTrack(const G4Step *)
std::string name()
#define nullptr
type of data representation of DDCompactView
Definition: DDCompactView.h:90
MuonSubDetector * detector
void fillHits(edm::PSimHitContainer &, const std::string &) override
Local3DPoint toOrcaUnits(const Local3DPoint &)
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
std::vector< PSimHit > & hits()
Point3DBase< float, GlobalTag > Global3DPoint
Definition: GlobalPoint.h:7
unsigned int processId(const G4VProcess *p) const
Local3DPoint exitPoint() const
Exit point in the local Det frame.
Definition: PSimHit.h:38
void updateExitPoint(const Local3DPoint &exit)
virtual void Initialize()
G4ProcessTypeEnumerator * theG4ProcessTypeEnumerator
Local3DPoint FinalStepPosition(const G4Step *step, coordinates) const
T z() const
Definition: PV3DBase.h:64
def pv(vc)
Definition: MetAnalyzer.py:6
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double p2[4]
Definition: TauolaWrapper.h:90
const SimTrackManager * theManager
void printGlobal(GlobalPoint) const
static int particleID(const G4Track *)
MuonFrameRotation * theRotation
MuonSensitiveDetector(const std::string &, const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *)
MuonBaseNumber PhysicalVolumeToBaseNumber(const G4Step *aStep)
uint32_t setDetUnitId(const G4Step *) override
Point3DBase< float, LocalTag > Local3DPoint
Definition: LocalPoint.h:9
void addEnergyLoss(float eloss)
void startNewSimHit(std::string)
void EndOfEvent(G4HCofThisEvent *) override
MuonSimHitNumberingScheme * numbering
G4TrackToParticleID * myG4TrackToParticleID
void printId(int) const
int getLevels() const
void update(const BeginOfEvent *) override
This routine will be called when the appropriate signal arrives.
float energyLoss() const
The energy deposit in the PSimHit, in ???.
Definition: PSimHit.h:75
int getSuperNo(int level) const
G4VPhysicalVolume * thePV
virtual bool processHits(const PSimHit &)
std::vector< PSimHit > PSimHitContainer
step
Local3DPoint InitialStepPositionVsParent(G4Step *currentStep, G4int levelsUp)
T x() const
Definition: PV3DBase.h:62
int baseNumberToUnitNumber(const MuonBaseNumber &) override
TrackInformation * getOrCreateTrackInformation(const G4Track *theTrack)
Local3DPoint entryPoint() const
Entry point in the local Det frame.
Definition: PSimHit.h:35
Local3DPoint toOrcaRef(Local3DPoint in, G4Step *)
unsigned int detUnitId() const
Definition: PSimHit.h:93
MuonG4Numbering * g4numbering
Local3DPoint InitialStepPosition(const G4Step *step, coordinates) const