CMS 3D CMS Logo

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