CMS 3D CMS Logo

SteppingAction.cc
Go to the documentation of this file.
1 
5 
6 #include "G4LogicalVolumeStore.hh"
7 #include "G4ParticleTable.hh"
8 #include "G4PhysicalVolumeStore.hh"
9 #include "G4RegionStore.hh"
10 #include "G4UnitsTable.hh"
11 #include "G4SystemOfUnits.hh"
12 
15 
16 //#define DebugLog
17 
19  const CMSSteppingVerbose* sv, bool hasW)
20  : eventAction_(e), tracker(nullptr), calo(nullptr), steppingVerbose(sv),
21  nWarnings(0),initialized(false), killBeamPipe(false),hasWatcher(hasW)
22 {
24  (p.getParameter<double>("CriticalEnergyForVacuum")*CLHEP::MeV);
25  if(0.0 < theCriticalEnergyForVacuum) { killBeamPipe = true; }
27  (p.getParameter<double>("CriticalDensity")*CLHEP::g/CLHEP::cm3);
28  maxTrackTime = p.getParameter<double>("MaxTrackTime")*CLHEP::ns;
29  maxTrackTimes = p.getParameter<std::vector<double> >("MaxTrackTimes");
30  maxTimeNames = p.getParameter<std::vector<std::string> >("MaxTimeNames");
31  deadRegionNames = p.getParameter<std::vector<std::string> >("DeadRegions");
32  ekinMins = p.getParameter<std::vector<double> >("EkinThresholds");
33  ekinNames = p.getParameter<std::vector<std::string> >("EkinNames");
34  ekinParticles = p.getParameter<std::vector<std::string> >("EkinParticles");
35 
36  edm::LogVerbatim("SimG4CoreApplication")
37  << "SteppingAction:: KillBeamPipe = " << killBeamPipe << " CriticalDensity = "
38  << theCriticalDensity*CLHEP::cm3/CLHEP::g << " g/cm3;"
39  << " CriticalEnergyForVacuum = " << theCriticalEnergyForVacuum/CLHEP::MeV
40  << " Mev;" << " MaxTrackTime = " << maxTrackTime/CLHEP::ns << " ns";
41 
42  numberTimes = maxTrackTimes.size();
43  if(numberTimes > 0) {
44  for (unsigned int i=0; i<numberTimes; i++) {
45  edm::LogVerbatim("SimG4CoreApplication")
46  << "SteppingAction::MaxTrackTime for " << maxTimeNames[i] << " is "
47  << maxTrackTimes[i] << " ns ";
48  maxTrackTimes[i] *= ns;
49  }
50  }
51 
52  ndeadRegions = deadRegionNames.size();
53  if(ndeadRegions > 0) {
54  edm::LogVerbatim("SimG4CoreApplication")
55  << "SteppingAction: Number of DeadRegions where all trackes are killed "
56  << ndeadRegions;
57  for(unsigned int i=0; i<ndeadRegions; ++i) {
58  edm::LogVerbatim("SimG4CoreApplication")
59  << "SteppingAction: DeadRegion " << i << ". " << deadRegionNames[i];
60  }
61  }
62  numberEkins = ekinNames.size();
63  numberPart = ekinParticles.size();
64  if(0 == numberPart) { numberEkins = 0; }
65 
66  if(numberEkins > 0) {
67 
68  edm::LogVerbatim("SimG4CoreApplication")
69  << "SteppingAction::Kill following " << numberPart
70  << " particles in " << numberEkins << " volumes";
71  for (unsigned int i=0; i<numberPart; ++i) {
72  edm::LogVerbatim("SimG4CoreApplication")
73  << "SteppingAction::Particle " << i << " " << ekinParticles[i]
74  << " Threshold = " << ekinMins[i] << " MeV";
75  ekinMins[i] *= CLHEP::MeV;
76  }
77  for (unsigned int i=0; i<numberEkins; ++i) {
78  edm::LogVerbatim("SimG4CoreApplication")
79  << "SteppingAction::LogVolume[" << i << "] = " << ekinNames[i];
80  }
81  }
82 }
83 
85 
86 void SteppingAction::UserSteppingAction(const G4Step * aStep)
87 {
88  if (!initialized) { initialized = initPointer(); }
89 
90  //if(hasWatcher) { m_g4StepSignal(aStep); }
91  m_g4StepSignal(aStep);
92 
93  G4Track * theTrack = aStep->GetTrack();
94  TrackStatus tstat = (theTrack->GetTrackStatus() == fAlive) ? sAlive : sKilledByProcess;
95 
96  G4StepPoint* postStep = aStep->GetPostStepPoint();
97 
98  if(sAlive == tstat && postStep->GetPhysicalVolume() != nullptr) {
99 
100  G4StepPoint* preStep = aStep->GetPreStepPoint();
101  const G4Region* theRegion =
102  preStep->GetPhysicalVolume()->GetLogicalVolume()->GetRegion();
103 
104  // kill in dead regions
105  if(isInsideDeadRegion(theRegion)) { tstat = sDeadRegion; }
106 
107  // NaN energy deposit
108  if(sAlive == tstat && edm::isNotFinite(aStep->GetTotalEnergyDeposit())) {
109  tstat = sEnergyDepNaN;
110  if(nWarnings < 20) {
111  ++nWarnings;
112  edm::LogWarning("SimG4CoreApplication")
113  << "Track #" << theTrack->GetTrackID()
114  << " " << theTrack->GetDefinition()->GetParticleName()
115  << " E(MeV)= " << preStep->GetKineticEnergy()/MeV
116  << " is killed due to edep=NaN inside PV: "
117  << preStep->GetPhysicalVolume()->GetName()
118  << " at " << theTrack->GetPosition()
119  << " StepLen(mm)= " << aStep->GetStepLength();
120  }
121  }
122 
123  // kill out of time
124  if(sAlive == tstat && isOutOfTimeWindow(theTrack, theRegion)) { tstat = sOutOfTime; }
125 
126  // kill low-energy in volumes on demand
127  if(sAlive == tstat && numberEkins > 0 && isLowEnergy(aStep)) { tstat = sLowEnergy; }
128 
129  // kill low-energy in vacuum
130  G4double kinEnergy = theTrack->GetKineticEnergy();
131  if(sAlive == tstat && killBeamPipe && kinEnergy < theCriticalEnergyForVacuum
132  && theTrack->GetDefinition()->GetPDGCharge() != 0.0 && kinEnergy > 0.0
133  && theTrack->GetNextVolume()->GetLogicalVolume()->GetMaterial()->GetDensity()
134  <= theCriticalDensity) {
135  tstat = sLowEnergyInVacuum;
136  }
137 
138  // check transition tracker/calo
139  if(sAlive == tstat) {
140 
141  if(isThisVolume(preStep->GetTouchable(),tracker) &&
142  isThisVolume(postStep->GetTouchable(),calo)) {
143 
144  math::XYZVectorD pos((preStep->GetPosition()).x(),
145  (preStep->GetPosition()).y(),
146  (preStep->GetPosition()).z());
147 
148  math::XYZTLorentzVectorD mom((preStep->GetMomentum()).x(),
149  (preStep->GetMomentum()).y(),
150  (preStep->GetMomentum()).z(),
151  preStep->GetTotalEnergy());
152 
153  uint32_t id = theTrack->GetTrackID();
154 
155  std::pair<math::XYZVectorD,math::XYZTLorentzVectorD> p(pos,mom);
157  }
158  } else {
159  theTrack->SetTrackStatus(fStopAndKill);
160 #ifdef DebugLog
161  PrintKilledTrack(theTrack, tstat);
162 #endif
163  }
164  }
165  if(nullptr != steppingVerbose) {
166  steppingVerbose->NextStep(aStep, fpSteppingManager, (1 < tstat));
167  }
168 }
169 
170 bool SteppingAction::isLowEnergy(const G4Step * aStep) const
171 {
172  bool flag = false;
173  const G4StepPoint* sp = aStep->GetPostStepPoint();
174  G4LogicalVolume* lv = sp->GetPhysicalVolume()->GetLogicalVolume();
175  for (unsigned int i=0; i<numberEkins; ++i) {
176  if (lv == ekinVolumes[i]) {
177  flag = true;
178  break;
179  }
180  }
181  if (flag) {
182  double ekin = sp->GetKineticEnergy();
183  int pCode = aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
184  for (unsigned int i=0; i<numberPart; ++i) {
185  if (pCode == ekinPDG[i]) {
186  return (ekin <= ekinMins[i]) ? true : false;
187  }
188  }
189  }
190  return false;
191 }
192 
194 {
195  const G4PhysicalVolumeStore * pvs = G4PhysicalVolumeStore::GetInstance();
196  if (pvs) {
197  std::vector<G4VPhysicalVolume*>::const_iterator pvcite;
198  for (pvcite = pvs->begin(); pvcite != pvs->end(); ++pvcite) {
199  if ((*pvcite)->GetName() == "Tracker") tracker = (*pvcite);
200  if ((*pvcite)->GetName() == "CALO") calo = (*pvcite);
201  if (tracker && calo) break;
202  }
203  if (tracker || calo) {
204  edm::LogVerbatim("SimG4CoreApplication")
205  << "Pointer for Tracker " << tracker << " and for Calo " << calo;
206  if (tracker) LogDebug("SimG4CoreApplication") << "Tracker vol name "
207  << tracker->GetName();
208  if (calo) LogDebug("SimG4CoreApplication") << "Calorimeter vol name "
209  << calo->GetName();
210  }
211  }
212 
213  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
214  if (numberEkins > 0) {
215  if (lvs) {
216  ekinVolumes.resize(numberEkins, nullptr);
217  std::vector<G4LogicalVolume*>::const_iterator lvcite;
218  for (lvcite = lvs->begin(); lvcite != lvs->end(); ++lvcite) {
219  for (unsigned int i=0; i<numberEkins; ++i) {
220  if ((*lvcite)->GetName() == (G4String)(ekinNames[i])) {
221  ekinVolumes[i] = (*lvcite);
222  break;
223  }
224  }
225  }
226  }
227  for (unsigned int i=0; i<numberEkins; ++i) {
228  edm::LogVerbatim("SimG4CoreApplication")
229  << ekinVolumes[i]->GetName() <<" with pointer " << ekinVolumes[i];
230  }
231  }
232 
233  if(numberPart > 0) {
234  G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
235  G4String partName;
236  ekinPDG.resize(numberPart, 0);
237  for (unsigned int i=0; i<numberPart; ++i) {
238  ekinPDG[i] =
239  theParticleTable->FindParticle(partName=ekinParticles[i])->GetPDGEncoding();
240  edm::LogVerbatim("SimG4CoreApplication")
241  << "Particle " << ekinParticles[i] << " with PDG code " << ekinPDG[i]
242  << " and KE cut off " << ekinMins[i]/MeV << " MeV";
243  }
244  }
245 
246  const G4RegionStore * rs = G4RegionStore::GetInstance();
247  if (numberTimes > 0) {
248  maxTimeRegions.resize(numberTimes, nullptr);
249  std::vector<G4Region*>::const_iterator rcite;
250  for (rcite = rs->begin(); rcite != rs->end(); ++rcite) {
251  for (unsigned int i=0; i<numberTimes; ++i) {
252  if ((*rcite)->GetName() == (G4String)(maxTimeNames[i])) {
253  maxTimeRegions[i] = (*rcite);
254  break;
255  }
256  }
257  }
258  }
259  if (ndeadRegions > 0) {
260  deadRegions.resize(ndeadRegions, nullptr);
261  std::vector<G4Region*>::const_iterator rcite;
262  for (rcite = rs->begin(); rcite != rs->end(); ++rcite) {
263  for (unsigned int i=0; i<ndeadRegions; ++i) {
264  if ((*rcite)->GetName() == (G4String)(deadRegionNames[i])) {
265  deadRegions[i] = (*rcite);
266  break;
267  }
268  }
269  }
270  }
271  return true;
272 }
273 
274 void SteppingAction::PrintKilledTrack(const G4Track* aTrack,
275  const TrackStatus& tst) const
276 {
277  std::string vname = "";
278  std::string rname = "";
279  std::string typ = " ";
280  switch (tst) {
281  case sKilledByProcess:
282  typ = " G4Process ";
283  break;
284  case sDeadRegion:
285  typ = " in dead region ";
286  break;
287  case sOutOfTime:
288  typ = " out of time window ";
289  break;
290  case sLowEnergy:
291  typ = " low energy limit ";
292  break;
293  case sLowEnergyInVacuum:
294  typ = " low energy limit in vacuum ";
295  break;
296  case sEnergyDepNaN:
297  typ = " energy deposition is NaN ";
298  break;
299  default:
300  break;
301  }
302  G4VPhysicalVolume* pv = aTrack->GetNextVolume();
303  if(pv) {
304  vname = pv->GetLogicalVolume()->GetName();
305  rname = pv->GetLogicalVolume()->GetRegion()->GetName();
306  }
307 
308  edm::LogVerbatim("SimG4CoreApplication")
309  << "Track #" << aTrack->GetTrackID()
310  << " " << aTrack->GetDefinition()->GetParticleName()
311  << " E(MeV)= " << aTrack->GetKineticEnergy()/MeV
312  << " T(ns)= " << aTrack->GetGlobalTime()/ns
313  << " is killed due to " << typ
314  << " inside LV: " << vname << " (" << rname
315  << ") at " << aTrack->GetPosition();
316 }
#define LogDebug(id)
T getParameter(std::string const &) const
void UserSteppingAction(const G4Step *aStep) final
const G4VPhysicalVolume * tracker
std::vector< int > ekinPDG
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
std::vector< const G4Region * > deadRegions
double theCriticalDensity
SimActivityRegistry::G4StepSignal m_g4StepSignal
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
~SteppingAction() override
void NextStep(const G4Step *, const G4SteppingManager *ptr, bool isKilled) const
#define nullptr
unsigned int numberPart
const CMSSteppingVerbose * steppingVerbose
bool isOutOfTimeWindow(G4Track *theTrack, const G4Region *reg) const
unsigned int numberEkins
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
const double MeV
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > XYZVectorD
spatial vector with cartesian internal representation
Definition: Vector3D.h:8
TrackStatus
std::vector< std::string > deadRegionNames
const G4VPhysicalVolume * calo
def pv(vc)
Definition: MetAnalyzer.py:7
unsigned int numberTimes
std::vector< double > maxTrackTimes
std::vector< G4LogicalVolume * > ekinVolumes
double theCriticalEnergyForVacuum
EventAction * eventAction_
std::vector< double > ekinMins
std::vector< std::string > maxTimeNames
std::vector< std::string > ekinNames
void addTkCaloStateInfo(uint32_t t, const std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > &p)
Definition: EventAction.cc:81
bool isLowEnergy(const G4Step *aStep) const
bool isInsideDeadRegion(const G4Region *reg) const
SteppingAction(EventAction *ea, const edm::ParameterSet &ps, const CMSSteppingVerbose *, bool hasW)
void PrintKilledTrack(const G4Track *, const TrackStatus &) const
unsigned int ndeadRegions
std::vector< std::string > ekinParticles
bool isThisVolume(const G4VTouchable *touch, const G4VPhysicalVolume *pv) const
std::vector< const G4Region * > maxTimeRegions
unsigned int nWarnings