CMS 3D CMS Logo

SteppingAction.cc
Go to the documentation of this file.
4 
5 #include "G4LogicalVolumeStore.hh"
6 #include "G4ParticleTable.hh"
7 #include "G4PhysicalVolumeStore.hh"
8 #include "G4RegionStore.hh"
9 #include "G4UnitsTable.hh"
10 #include "G4SystemOfUnits.hh"
11 
14 
15 //#define DebugLog
16 
18  : eventAction_(e),
19  tracker(nullptr),
20  calo(nullptr),
21  steppingVerbose(sv),
22  nWarnings(0),
23  initialized(false),
24  killBeamPipe(false),
25  hasWatcher(hasW) {
26  theCriticalEnergyForVacuum = (p.getParameter<double>("CriticalEnergyForVacuum") * CLHEP::MeV);
27  if (0.0 < theCriticalEnergyForVacuum) {
28  killBeamPipe = true;
29  }
30  theCriticalDensity = (p.getParameter<double>("CriticalDensity") * CLHEP::g / CLHEP::cm3);
31  maxZCentralCMS = p.getParameter<double>("MaxZCentralCMS") * CLHEP::m;
32  maxTrackTime = p.getParameter<double>("MaxTrackTime") * CLHEP::ns;
33  maxTrackTimeForward = p.getParameter<double>("MaxTrackTimeForward") * CLHEP::ns;
34  maxTrackTimes = p.getParameter<std::vector<double> >("MaxTrackTimes");
35  maxTimeNames = p.getParameter<std::vector<std::string> >("MaxTimeNames");
36  deadRegionNames = p.getParameter<std::vector<std::string> >("DeadRegions");
37  maxNumberOfSteps = p.getParameter<int>("MaxNumberOfSteps");
38  ekinMins = p.getParameter<std::vector<double> >("EkinThresholds");
39  ekinNames = p.getParameter<std::vector<std::string> >("EkinNames");
40  ekinParticles = p.getParameter<std::vector<std::string> >("EkinParticles");
41 
42  edm::LogVerbatim("SimG4CoreApplication")
43  << "SteppingAction:: KillBeamPipe = " << killBeamPipe
44  << " CriticalDensity = " << theCriticalDensity * CLHEP::cm3 / CLHEP::g << " g/cm3\n"
45  << " CriticalEnergyForVacuum = " << theCriticalEnergyForVacuum / CLHEP::MeV << " Mev;"
46  << " MaxTrackTime = " << maxTrackTime / CLHEP::ns << " ns;"
47  << " MaxZCentralCMS = " << maxZCentralCMS / CLHEP::m << " m"
48  << " MaxTrackTimeForward = " << maxTrackTimeForward / CLHEP::ns << " ns"
49  << " MaxNumberOfSteps = " << maxNumberOfSteps;
50 
51  numberTimes = maxTrackTimes.size();
52  if (numberTimes > 0) {
53  for (unsigned int i = 0; i < numberTimes; i++) {
54  edm::LogVerbatim("SimG4CoreApplication")
55  << "SteppingAction::MaxTrackTime for " << maxTimeNames[i] << " is " << maxTrackTimes[i] << " ns ";
56  maxTrackTimes[i] *= ns;
57  }
58  }
59 
61  if (ndeadRegions > 0) {
62  edm::LogVerbatim("SimG4CoreApplication")
63  << "SteppingAction: Number of DeadRegions where all trackes are killed " << ndeadRegions;
64  for (unsigned int i = 0; i < ndeadRegions; ++i) {
65  edm::LogVerbatim("SimG4CoreApplication") << "SteppingAction: DeadRegion " << i << ". " << deadRegionNames[i];
66  }
67  }
68  numberEkins = ekinNames.size();
69  numberPart = ekinParticles.size();
70  if (0 == numberPart) {
71  numberEkins = 0;
72  }
73 
74  if (numberEkins > 0) {
75  edm::LogVerbatim("SimG4CoreApplication")
76  << "SteppingAction::Kill following " << numberPart << " particles in " << numberEkins << " volumes";
77  for (unsigned int i = 0; i < numberPart; ++i) {
78  edm::LogVerbatim("SimG4CoreApplication")
79  << "SteppingAction::Particle " << i << " " << ekinParticles[i] << " Threshold = " << ekinMins[i] << " MeV";
80  ekinMins[i] *= CLHEP::MeV;
81  }
82  for (unsigned int i = 0; i < numberEkins; ++i) {
83  edm::LogVerbatim("SimG4CoreApplication") << "SteppingAction::LogVolume[" << i << "] = " << ekinNames[i];
84  }
85  }
86 }
87 
89 
90 void SteppingAction::UserSteppingAction(const G4Step* aStep) {
91  if (!initialized) {
93  }
94 
95  //if(hasWatcher) { m_g4StepSignal(aStep); }
96  m_g4StepSignal(aStep);
97 
98  G4Track* theTrack = aStep->GetTrack();
99  TrackStatus tstat = (theTrack->GetTrackStatus() == fAlive) ? sAlive : sKilledByProcess;
100 
101  const G4StepPoint* preStep = aStep->GetPreStepPoint();
102  const G4StepPoint* postStep = aStep->GetPostStepPoint();
103 
104  // NaN energy deposit
105  if (edm::isNotFinite(aStep->GetTotalEnergyDeposit())) {
106  tstat = sEnergyDepNaN;
107  if (nWarnings < 5) {
108  ++nWarnings;
109  edm::LogWarning("SimG4CoreApplication")
110  << "Track #" << theTrack->GetTrackID() << " " << theTrack->GetDefinition()->GetParticleName()
111  << " E(MeV)= " << preStep->GetKineticEnergy() / MeV << " Nstep= " << theTrack->GetCurrentStepNumber()
112  << " is killed due to edep=NaN inside PV: " << preStep->GetPhysicalVolume()->GetName() << " at "
113  << theTrack->GetPosition() << " StepLen(mm)= " << aStep->GetStepLength();
114  }
115  }
116 
117  // the track is killed by the process
118  if (tstat == sKilledByProcess) {
119  if (nullptr != steppingVerbose) {
120  steppingVerbose->NextStep(aStep, fpSteppingManager, false);
121  }
122  return;
123  }
124 
125  if (sAlive == tstat && theTrack->GetCurrentStepNumber() > maxNumberOfSteps) {
126  tstat = sNumberOfSteps;
127  if (nWarnings < 5) {
128  ++nWarnings;
129  edm::LogWarning("SimG4CoreApplication")
130  << "Track #" << theTrack->GetTrackID() << " " << theTrack->GetDefinition()->GetParticleName()
131  << " E(MeV)= " << preStep->GetKineticEnergy() / MeV << " Nstep= " << theTrack->GetCurrentStepNumber()
132  << " is killed due to limit on number of steps;/n PV: " << preStep->GetPhysicalVolume()->GetName() << " at "
133  << theTrack->GetPosition() << " StepLen(mm)= " << aStep->GetStepLength();
134  }
135  }
136  const double time = theTrack->GetGlobalTime();
137 
138  // check Z-coordinate
139  if (sAlive == tstat && std::abs(theTrack->GetPosition().z()) >= maxZCentralCMS) {
141  }
142 
143  // check G4Region
144  if (sAlive == tstat) {
145  // next logical volume and next region
146  const G4LogicalVolume* lv = postStep->GetPhysicalVolume()->GetLogicalVolume();
147  const G4Region* theRegion = lv->GetRegion();
148 
149  // kill in dead regions
150  if (isInsideDeadRegion(theRegion))
151  tstat = sDeadRegion;
152 
153  // kill out of time
154  if (sAlive == tstat) {
155  if (isOutOfTimeWindow(theRegion, time))
156  tstat = sOutOfTime;
157  }
158 
159  // kill low-energy in volumes on demand
160  if (sAlive == tstat && numberEkins > 0) {
161  if (isLowEnergy(lv, theTrack))
162  tstat = sLowEnergy;
163  }
164 
165  // kill low-energy in vacuum
166  if (sAlive == tstat && killBeamPipe) {
167  if (theTrack->GetKineticEnergy() < theCriticalEnergyForVacuum &&
168  theTrack->GetDefinition()->GetPDGCharge() != 0.0 && lv->GetMaterial()->GetDensity() <= theCriticalDensity) {
169  tstat = sLowEnergyInVacuum;
170  }
171  }
172  }
173  // check transition tracker/calo
174  bool isKilled = false;
175  if (sAlive == tstat || sVeryForward == tstat) {
176  if (preStep->GetPhysicalVolume() == tracker && postStep->GetPhysicalVolume() == calo) {
177  math::XYZVectorD pos((postStep->GetPosition()).x(), (postStep->GetPosition()).y(), (postStep->GetPosition()).z());
178 
179  math::XYZTLorentzVectorD mom((postStep->GetMomentum()).x(),
180  (postStep->GetMomentum()).y(),
181  (postStep->GetMomentum()).z(),
182  postStep->GetTotalEnergy());
183 
184  uint32_t id = theTrack->GetTrackID();
185 
186  std::pair<math::XYZVectorD, math::XYZTLorentzVectorD> p(pos, mom);
188  }
189  } else {
190  theTrack->SetTrackStatus(fStopAndKill);
191  isKilled = true;
192 #ifdef DebugLog
193  PrintKilledTrack(theTrack, tstat);
194 #endif
195  }
196  if (nullptr != steppingVerbose) {
197  steppingVerbose->NextStep(aStep, fpSteppingManager, isKilled);
198  }
199 }
200 
201 bool SteppingAction::isLowEnergy(const G4LogicalVolume* lv, const G4Track* theTrack) const {
202  const double ekin = theTrack->GetKineticEnergy();
203  int pCode = theTrack->GetDefinition()->GetPDGEncoding();
204 
205  for (auto& vol : ekinVolumes) {
206  if (lv == vol) {
207  for (unsigned int i = 0; i < numberPart; ++i) {
208  if (pCode == ekinPDG[i]) {
209  return (ekin <= ekinMins[i]);
210  }
211  }
212  break;
213  }
214  }
215  return false;
216 }
217 
219  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
220  for (auto const& pvcite : *pvs) {
221  const G4String& pvname = pvcite->GetName();
222  if (pvname == "Tracker" || pvname == "tracker:Tracker_1")
223  tracker = pvcite;
224  else if (pvname == "CALO" || pvname == "caloBase:CALO_1")
225  calo = pvcite;
226 
227  if (tracker && calo)
228  break;
229  }
230  edm::LogVerbatim("SimG4CoreApplication")
231  << "SteppingAction: pointer for Tracker " << tracker << " and for Calo " << calo;
232 
233  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
234  if (numberEkins > 0) {
235  ekinVolumes.resize(numberEkins, nullptr);
236  for (auto const& lvcite : *lvs) {
237  const G4String& lvname = lvcite->GetName();
238  for (unsigned int i = 0; i < numberEkins; ++i) {
239  if (lvname == (G4String)(ekinNames[i])) {
240  ekinVolumes[i] = lvcite;
241  break;
242  }
243  }
244  }
245  for (unsigned int i = 0; i < numberEkins; ++i) {
246  edm::LogVerbatim("SimG4CoreApplication") << ekinVolumes[i]->GetName() << " with pointer " << ekinVolumes[i];
247  }
248  }
249 
250  if (numberPart > 0) {
251  G4ParticleTable* theParticleTable = G4ParticleTable::GetParticleTable();
252  ekinPDG.resize(numberPart, 0);
253  for (unsigned int i = 0; i < numberPart; ++i) {
254  const G4ParticleDefinition* part = theParticleTable->FindParticle(ekinParticles[i]);
255  if (nullptr != part)
256  ekinPDG[i] = part->GetPDGEncoding();
257  edm::LogVerbatim("SimG4CoreApplication") << "Particle " << ekinParticles[i] << " with PDG code " << ekinPDG[i]
258  << " and KE cut off " << ekinMins[i] / MeV << " MeV";
259  }
260  }
261 
262  const G4RegionStore* rs = G4RegionStore::GetInstance();
263  if (numberTimes > 0) {
264  maxTimeRegions.resize(numberTimes, nullptr);
265  for (auto const& rcite : *rs) {
266  const G4String& rname = rcite->GetName();
267  for (unsigned int i = 0; i < numberTimes; ++i) {
268  if (rname == (G4String)(maxTimeNames[i])) {
269  maxTimeRegions[i] = rcite;
270  break;
271  }
272  }
273  }
274  }
275  if (ndeadRegions > 0) {
276  deadRegions.resize(ndeadRegions, nullptr);
277  for (auto const& rcite : *rs) {
278  const G4String& rname = rcite->GetName();
279  for (unsigned int i = 0; i < ndeadRegions; ++i) {
280  if (rname == (G4String)(deadRegionNames[i])) {
281  deadRegions[i] = rcite;
282  break;
283  }
284  }
285  }
286  }
287  return true;
288 }
289 
290 void SteppingAction::PrintKilledTrack(const G4Track* aTrack, const TrackStatus& tst) const {
291  std::string vname = "";
292  std::string rname = "";
293  std::string typ = " ";
294  switch (tst) {
295  case sDeadRegion:
296  typ = " in dead region ";
297  break;
298  case sOutOfTime:
299  typ = " out of time window ";
300  break;
301  case sLowEnergy:
302  typ = " low energy limit ";
303  break;
304  case sLowEnergyInVacuum:
305  typ = " low energy limit in vacuum ";
306  break;
307  case sEnergyDepNaN:
308  typ = " energy deposition is NaN ";
309  break;
310  case sVeryForward:
311  typ = " very forward track ";
312  break;
313  case sNumberOfSteps:
314  typ = " too many steps ";
315  break;
316  default:
317  break;
318  }
319  G4VPhysicalVolume* pv = aTrack->GetNextVolume();
320  vname = pv->GetLogicalVolume()->GetName();
321  rname = pv->GetLogicalVolume()->GetRegion()->GetName();
322 
323  const double ekin = aTrack->GetKineticEnergy();
324  if (ekin < 2 * CLHEP::MeV) {
325  return;
326  }
327  edm::LogWarning("SimG4CoreApplication")
328  << "Track #" << aTrack->GetTrackID() << " StepN= " << aTrack->GetCurrentStepNumber() << " "
329  << aTrack->GetDefinition()->GetParticleName() << " E(MeV)=" << ekin / CLHEP::MeV
330  << " T(ns)=" << aTrack->GetGlobalTime() / CLHEP::ns << " is killed due to " << typ << "\n LV: " << vname << " ("
331  << rname << ") at " << aTrack->GetPosition() << " step(cm)=" << aTrack->GetStep()->GetStepLength() / CLHEP::cm;
332 }
Log< level::Info, true > LogVerbatim
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
unsigned int numberPart
bool isOutOfTimeWindow(const G4Region *reg, const double &time) const
const CMSSteppingVerbose * steppingVerbose
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
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > XYZVectorD
spatial vector with cartesian internal representation
Definition: Vector3D.h:8
TrackStatus
G4int maxNumberOfSteps
void PrintKilledTrack(const G4Track *, const TrackStatus &) const
std::vector< std::string > deadRegionNames
const G4VPhysicalVolume * calo
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isLowEnergy(const G4LogicalVolume *, const G4Track *) const
unsigned int numberTimes
std::vector< double > maxTrackTimes
bool isInsideDeadRegion(const G4Region *reg) const
std::vector< G4LogicalVolume * > ekinVolumes
double theCriticalEnergyForVacuum
EventAction * eventAction_
double maxZCentralCMS
std::vector< double > ekinMins
std::vector< std::string > maxTimeNames
part
Definition: HCALResponse.h:20
std::vector< std::string > ekinNames
double maxTrackTimeForward
const G4String rname[NREG]
void addTkCaloStateInfo(uint32_t t, const std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > &p)
Definition: EventAction.cc:72
void NextStep(const G4Step *, const G4SteppingManager *ptr, bool isKilled) const
SteppingAction(EventAction *ea, const edm::ParameterSet &ps, const CMSSteppingVerbose *, bool hasW)
Log< level::Warning, false > LogWarning
unsigned int ndeadRegions
std::vector< std::string > ekinParticles
Definition: Common.h:9
std::vector< const G4Region * > maxTimeRegions
unsigned int nWarnings