CMS 3D CMS Logo

CaloTrkProcessing.cc
Go to the documentation of this file.
5 
7 
9 
10 #include "G4EventManager.hh"
11 #include "G4LogicalVolumeStore.hh"
12 #include "G4LogicalVolume.hh"
13 #include "G4Step.hh"
14 #include "G4Track.hh"
15 #include "G4SystemOfUnits.hh"
16 #include "DD4hep/Filter.h"
17 
18 #include <sstream>
19 // #define EDM_ML_DEBUG
20 
22  const CaloSimulationParameters& csps,
23  const SensitiveDetectorCatalog& clg,
24  bool testBeam,
25  double eMin,
26  bool putHistory,
27  bool doFineCalo,
28  double eMinFine,
29  int addlevel,
30  const std::vector<std::string>& fineNames,
31  const std::vector<int>& fineLevels,
32  const std::vector<int>& useFines,
33  const SimTrackManager*)
35  testBeam_(testBeam),
36  eMin_(eMin),
37  putHistory_(putHistory),
38  doFineCalo_(doFineCalo),
39  eMinFine_(eMinFine),
40  addlevel_(addlevel),
41  lastTrackID_(-1) {
42  //Initialise the parameter set
43 
44  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: Initialised with TestBeam = " << testBeam_ << " Emin = " << eMin_
45  << " Flags " << putHistory_ << " (History), " << doFineCalo_ << " (Special Calorimeter)";
46  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: Have a possibility of " << fineNames.size()
47  << " fine calorimeters of which " << useFines.size() << " are selected";
48  for (unsigned int k = 0; k < fineNames.size(); ++k)
49  edm::LogVerbatim("CaloSim") << "[" << k << "] " << fineNames[k] << " at " << fineLevels[k];
50  std::ostringstream st1;
51  for (unsigned int k = 0; k < useFines.size(); ++k)
52  st1 << " [" << k << "] " << useFines[k] << ":" << fineNames[useFines[k]];
53  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing used calorimeters" << st1.str();
54 
55  // Debug prints
56  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: " << csps.caloNames_.size() << " entries for caloNames:";
57  for (unsigned int i = 0; i < csps.caloNames_.size(); i++)
58  edm::LogVerbatim("CaloSim") << " (" << i << ") " << csps.caloNames_[i];
59  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: " << csps.levels_.size() << " entries for levels:";
60  for (unsigned int i = 0; i < csps.levels_.size(); i++)
61  edm::LogVerbatim("CaloSim") << " (" << i << ") " << (csps.levels_[i] + addlevel_);
62  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: " << csps.neighbours_.size() << " entries for neighbours:";
63  for (unsigned int i = 0; i < csps.neighbours_.size(); i++)
64  edm::LogVerbatim("CaloSim") << " (" << i << ") " << csps.neighbours_[i];
65  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: " << csps.insideNames_.size() << " entries for insideNames:";
66  for (unsigned int i = 0; i < csps.insideNames_.size(); i++)
67  edm::LogVerbatim("CaloSim") << " (" << i << ") " << csps.insideNames_[i];
68  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: " << csps.insideLevel_.size() << " entries for insideLevel:";
69  for (unsigned int i = 0; i < csps.insideLevel_.size(); i++)
70  edm::LogVerbatim("CaloSim") << " (" << i << ") " << (csps.insideLevel_[i] + addlevel_);
71 
72  if (csps.caloNames_.size() < csps.neighbours_.size()) {
73  edm::LogError("CaloSim") << "CaloTrkProcessing: # of Calorimeter bins " << csps.caloNames_.size()
74  << " does not match with " << csps.neighbours_.size() << " ==> illegal ";
75  throw cms::Exception("Unknown", "CaloTrkProcessing")
76  << "Calorimeter array size does not match with size of neighbours\n";
77  }
78 
79  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
80  std::vector<G4LogicalVolume*>::const_iterator lvcite;
81  int istart = 0;
82  for (unsigned int i = 0; i < csps.caloNames_.size(); i++) {
83  G4LogicalVolume* lv = nullptr;
84  G4String name(csps.caloNames_[i]);
85  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
86  G4String namx(static_cast<std::string>(dd4hep::dd::noNamespace((*lvcite)->GetName())));
87  if (namx == name) {
88  lv = (*lvcite);
89  break;
90  }
91  }
92  if (lv != nullptr) {
94  detector.name = name;
95  detector.lv = lv;
96  detector.level = (csps.levels_[i] + addlevel_);
97  if (istart + csps.neighbours_[i] > static_cast<int>(csps.insideNames_.size())) {
98  edm::LogError("CaloSim") << "CaloTrkProcessing: # of InsideNames bins " << csps.insideNames_.size()
99  << " too few compaerd to " << istart + csps.neighbours_[i]
100  << " requested ==> illegal ";
101  throw cms::Exception("Unknown", "CaloTrkProcessing")
102  << "InsideNames array size does not match with list of neighbours\n";
103  }
104  std::vector<std::string> inside;
105  std::vector<G4LogicalVolume*> insideLV;
106  std::vector<int> insideLevels;
107  for (int k = 0; k < csps.neighbours_[i]; k++) {
108  lv = nullptr;
109  name = static_cast<G4String>(csps.insideNames_[istart + k]);
110  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
111  G4String namx(static_cast<std::string>(dd4hep::dd::noNamespace((*lvcite)->GetName())));
112  if (namx == name) {
113  lv = (*lvcite);
114  break;
115  }
116  }
117  inside.push_back(name);
118  insideLV.push_back(lv);
119  insideLevels.push_back(csps.insideLevel_[istart + k] + addlevel_);
120  }
121  detector.fromDets = inside;
122  detector.fromDetL = insideLV;
123  detector.fromLevels = insideLevels;
124  detectors_.emplace_back(detector);
125  }
126  istart += csps.neighbours_[i];
127  }
128 
129  for (unsigned int i = 0; i < useFines.size(); i++) {
130  G4LogicalVolume* lv = nullptr;
131  G4String name = static_cast<G4String>(fineNames[useFines[i]]);
132  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
133  G4String namx(static_cast<std::string>(dd4hep::dd::noNamespace((*lvcite)->GetName())));
134  if (namx == name) {
135  lv = (*lvcite);
136  break;
137  }
138  }
139  if (lv != nullptr) {
141  detector.name = name;
142  detector.lv = lv;
143  detector.level = fineLevels[useFines[i]];
144  detector.fromDets.clear();
145  detector.fromDetL.clear();
146  detector.fromLevels.clear();
147  fineDetectors_.emplace_back(detector);
148  }
149  }
150 
151  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: with " << detectors_.size() << " calorimetric volumes";
152  for (unsigned int i = 0; i < detectors_.size(); i++) {
153  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: Calorimeter volume " << i << " " << detectors_[i].name << " LV "
154  << detectors_[i].lv << " at level " << detectors_[i].level << " with "
155  << detectors_[i].fromDets.size() << " neighbours";
156  for (unsigned int k = 0; k < detectors_[i].fromDets.size(); k++)
157  edm::LogVerbatim("CaloSim") << " Element " << k << " " << detectors_[i].fromDets[k] << " LV "
158  << detectors_[i].fromDetL[k] << " at level " << detectors_[i].fromLevels[k];
159  }
160 
161  doFineCalo_ = doFineCalo_ && !(fineDetectors_.empty());
162  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: with " << fineDetectors_.size() << " special calorimetric volumes";
163  for (unsigned int i = 0; i < detectors_.size(); i++)
164  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: Calorimeter volume " << i << " " << detectors_[i].name << " LV "
165  << detectors_[i].lv << " at level " << detectors_[i].level;
166 }
167 
169 
171 
172 void CaloTrkProcessing::update(const G4Step* aStep) {
173  // define if you are at the surface of CALO
174 
175  G4Track* theTrack = aStep->GetTrack();
176  int id = theTrack->GetTrackID();
177 
178  TrackInformation* trkInfo = dynamic_cast<TrackInformation*>(theTrack->GetUserInformation());
179 
180  if (trkInfo == nullptr) {
181  edm::LogError("CaloSim") << "CaloTrkProcessing: No trk info !!!! abort ";
182  throw cms::Exception("Unknown", "CaloTrkProcessing") << "cannot get trkInfo for Track " << id << "\n";
183  }
184 
185  if (doFineCalo_) {
186  int prestepLV = isItCalo(aStep->GetPreStepPoint()->GetTouchable(), fineDetectors_);
187  int poststepLV = isItCalo(aStep->GetPostStepPoint()->GetTouchable(), fineDetectors_);
188 
189  // Once per track, determine whether track started in fine volume
190  if (!trkInfo->startedInFineVolumeIsSet())
191  trkInfo->setStartedInFineVolume(prestepLV >= 0);
192 
193  // Boundary-crossing logic
194  if (prestepLV < 0 && poststepLV >= 0) {
195 #ifdef EDM_ML_DEBUG
196  edm::LogVerbatim("DoFineCalo") << "Track " << id << " entered a fine volume:"
197  << " pdgid=" << theTrack->GetDefinition()->GetPDGEncoding()
198  << " theTrack->GetCurrentStepNumber()=" << theTrack->GetCurrentStepNumber()
199  << " prestepLV=" << prestepLV << " poststepLV=" << poststepLV
200  << " GetKineticEnergy[GeV]=" << theTrack->GetKineticEnergy() / CLHEP::GeV
201  << " prestepPosition[cm]=("
202  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().x() / CLHEP::cm << ","
203  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().y() / CLHEP::cm << ","
204  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().z() / CLHEP::cm << ")"
205  << " poststepPosition[cm]=("
206  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().x() / CLHEP::cm << ","
207  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().y() / CLHEP::cm << ","
208  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().z() / CLHEP::cm << ")"
209  << " position[cm]=(" << theTrack->GetPosition().x() / CLHEP::cm << ","
210  << theTrack->GetPosition().y() / CLHEP::cm << ","
211  << theTrack->GetPosition().z() / CLHEP::cm << ")"
212  << " vertex_position[cm]=(" << theTrack->GetVertexPosition().x() / CLHEP::cm << ","
213  << theTrack->GetVertexPosition().y() / CLHEP::cm << ","
214  << theTrack->GetVertexPosition().z() / CLHEP::cm << ")"
215  << " GetVertexKineticEnergy[GeV]="
216  << theTrack->GetVertexKineticEnergy() / CLHEP::GeV;
217 #endif
218  if (!trkInfo->startedInFineVolume() && !trkInfo->crossedBoundary()) {
219  trkInfo->setCrossedBoundary(theTrack);
220 #ifdef EDM_ML_DEBUG
221  edm::LogVerbatim("DoFineCalo") << "Track " << id << " marked as boundary-crossing; sanity check:"
222  << " theTrack->GetTrackID()=" << theTrack->GetTrackID()
223  << " trkInfo->crossedBoundary()=" << trkInfo->crossedBoundary();
224 #endif
225  }
226 #ifdef EDM_ML_DEBUG
227  else {
228  edm::LogVerbatim("DoFineCalo") << "Track " << id << " REENTERED a fine volume;"
229  << " not counting this boundary crossing!";
230  }
231 #endif
232 
233  }
234 #ifdef EDM_ML_DEBUG
235  else if (prestepLV >= 0 && poststepLV < 0) {
236  edm::LogVerbatim("DoFineCalo") << "Track " << id << " exited a fine volume:"
237  << " theTrack->GetCurrentStepNumber()=" << theTrack->GetCurrentStepNumber()
238  << " prestepLV=" << prestepLV << " poststepLV=" << poststepLV
239  << " GetKineticEnergy[GeV]=" << theTrack->GetKineticEnergy() / CLHEP::GeV
240  << " prestepPosition[cm]=("
241  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().x() / CLHEP::cm << ","
242  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().y() / CLHEP::cm << ","
243  << theTrack->GetStep()->GetPreStepPoint()->GetPosition().z() / CLHEP::cm << ")"
244  << " poststepPosition[cm]=("
245  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().x() / CLHEP::cm << ","
246  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().y() / CLHEP::cm << ","
247  << theTrack->GetStep()->GetPostStepPoint()->GetPosition().z() / CLHEP::cm << ")";
248  }
249 #endif
250  }
251 
252  if (testBeam_) {
253  if (trkInfo->getIDonCaloSurface() == 0) {
254 #ifdef EDM_ML_DEBUG
255  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing set IDonCaloSurface to " << id << " at step Number "
256  << theTrack->GetCurrentStepNumber();
257 #endif
258  trkInfo->setIDonCaloSurface(id, 0, 0, theTrack->GetDefinition()->GetPDGEncoding(), theTrack->GetMomentum().mag());
259  lastTrackID_ = id;
260  if (theTrack->GetKineticEnergy() / CLHEP::MeV > eMin_)
261  trkInfo->putInHistory();
262  }
263  } else {
264  if (putHistory_) {
265  trkInfo->putInHistory();
266  // trkInfo->setAncestor();
267  }
268 #ifdef EDM_ML_DEBUG
269  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing Entered for " << id << " at stepNumber "
270  << theTrack->GetCurrentStepNumber() << " IDonCaloSur.. "
271  << trkInfo->getIDonCaloSurface() << " CaloCheck " << trkInfo->caloIDChecked();
272 #endif
273  if (trkInfo->getIDonCaloSurface() != 0) {
274  if (trkInfo->caloIDChecked() == false) {
275  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
276  const G4VTouchable* post_touch = postStepPoint->GetTouchable();
277 
278  if (isItInside(post_touch, trkInfo->getIDCaloVolume(), trkInfo->getIDLastVolume()) > 0) {
279  trkInfo->setIDonCaloSurface(0, -1, -1, 0, 0);
280  } else {
281  trkInfo->setCaloIDChecked(true);
282  }
283  }
284  } else {
285  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
286  const G4VTouchable* post_touch = postStepPoint->GetTouchable();
287  int ical = isItCalo(post_touch, detectors_);
288  if (ical >= 0) {
289  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
290  const G4VTouchable* pre_touch = preStepPoint->GetTouchable();
291  int inside = isItInside(pre_touch, ical, -1);
292  if (inside >= 0 || (theTrack->GetCurrentStepNumber() == 1)) {
293  trkInfo->setIDonCaloSurface(
294  id, ical, inside, theTrack->GetDefinition()->GetPDGEncoding(), theTrack->GetMomentum().mag());
295  trkInfo->setCaloIDChecked(true);
296  if (!doFineCalo_)
297  trkInfo->setCrossedBoundary(theTrack);
298  lastTrackID_ = id;
299  if (theTrack->GetKineticEnergy() / CLHEP::MeV > eMin_)
300  trkInfo->putInHistory();
301 #ifdef EDM_ML_DEBUG
302  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: set ID on Calo " << ical << " surface (Inside " << inside
303  << ") to " << id << " of a Track with Kinetic Energy "
304  << theTrack->GetKineticEnergy() / CLHEP::MeV << " MeV";
305 #endif
306  }
307  }
308  }
309  }
310 }
311 
312 int CaloTrkProcessing::isItCalo(const G4VTouchable* touch, const std::vector<Detector>& detectors) {
313  int lastLevel = -1;
314  G4LogicalVolume* lv = nullptr;
315  for (unsigned int it = 0; it < detectors.size(); it++) {
316  if (lastLevel != detectors[it].level) {
317  lastLevel = detectors[it].level;
318  lv = detLV(touch, lastLevel);
319 #ifdef EDM_ML_DEBUG
320  std::string name1 = "Unknown";
321  if (lv != 0)
322  name1 = lv->GetName();
323  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: volume " << name1 << " at Level " << lastLevel;
324  int levels = detLevels(touch);
325  if (levels > 0) {
326  G4String name2[20];
327  int copyno2[20];
328  detectorLevel(touch, levels, copyno2, name2);
329  for (int i2 = 0; i2 < levels; i2++)
330  edm::LogVerbatim("CaloSim") << " " << i2 << " " << name2[i2] << " " << copyno2[i2];
331  }
332 #endif
333  }
334  bool ok = (lv == detectors[it].lv);
335  if (ok)
336  return it;
337  }
338  return -1;
339 }
340 
341 int CaloTrkProcessing::isItInside(const G4VTouchable* touch, int idcal, int idin) {
342  int lastLevel = -1;
343  G4LogicalVolume* lv = nullptr;
344  int id1, id2;
345  if (idcal < 0) {
346  id1 = 0;
347  id2 = static_cast<int>(detectors_.size());
348  } else {
349  id1 = idcal;
350  id2 = id1 + 1;
351  }
352  for (int it1 = id1; it1 < id2; it1++) {
353  if (idin < 0) {
354  for (unsigned int it2 = 0; it2 < detectors_[it1].fromDets.size(); it2++) {
355  if (lastLevel != detectors_[it1].fromLevels[it2]) {
356  lastLevel = detectors_[it1].fromLevels[it2];
357  lv = detLV(touch, lastLevel);
358 #ifdef EDM_ML_DEBUG
359  std::string name1 = "Unknown";
360  if (lv != 0)
361  name1 = lv->GetName();
362  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: volume " << name1 << " at Level " << lastLevel;
363  int levels = detLevels(touch);
364  if (levels > 0) {
365  G4String name2[20];
366  int copyno2[20];
367  detectorLevel(touch, levels, copyno2, name2);
368  for (int i2 = 0; i2 < levels; i2++)
369  edm::LogVerbatim("CaloSim") << " " << i2 << " " << name2[i2] << " " << copyno2[i2];
370  }
371 #endif
372  }
373  bool ok = (lv == detectors_[it1].fromDetL[it2]);
374  if (ok)
375  return it2;
376  }
377  } else {
378  lastLevel = detectors_[it1].fromLevels[idin];
379  lv = detLV(touch, lastLevel);
380 #ifdef EDM_ML_DEBUG
381  std::string name1 = "Unknown";
382  if (lv != 0)
383  name1 = lv->GetName();
384  edm::LogVerbatim("CaloSim") << "CaloTrkProcessing: volume " << name1 << " at Level " << lastLevel;
385  int levels = detLevels(touch);
386  if (levels > 0) {
387  G4String name2[20];
388  int copyno2[20];
389  detectorLevel(touch, levels, copyno2, name2);
390  for (int i2 = 0; i2 < levels; i2++)
391  edm::LogVerbatim("CaloSim") << " " << i2 << " " << name2[i2] << " " << copyno2[i2];
392  }
393 #endif
394  bool ok = (lv == detectors_[it1].fromDetL[idin]);
395  if (ok)
396  return idin;
397  }
398  }
399  return -1;
400 }
401 
402 int CaloTrkProcessing::detLevels(const G4VTouchable* touch) const {
403  //Return number of levels
404  if (touch)
405  return ((touch->GetHistoryDepth()) + 1);
406  else
407  return 0;
408 }
409 
410 G4LogicalVolume* CaloTrkProcessing::detLV(const G4VTouchable* touch, int currentlevel) const {
411  G4LogicalVolume* lv = nullptr;
412  if (touch) {
413  int level = ((touch->GetHistoryDepth()) + 1);
414  if (level > 0 && level >= currentlevel) {
415  int ii = level - currentlevel;
416  lv = touch->GetVolume(ii)->GetLogicalVolume();
417  return lv;
418  }
419  }
420  return lv;
421 }
422 
423 void CaloTrkProcessing::detectorLevel(const G4VTouchable* touch, int& level, int* copyno, G4String* name) const {
424  static const std::string unknown("Unknown");
425  //Get name and copy numbers
426  if (level > 0) {
427  for (int ii = 0; ii < level; ii++) {
428  int i = level - ii - 1;
429  G4VPhysicalVolume* pv = touch->GetVolume(i);
430  if (pv != nullptr)
431  name[ii] = pv->GetName();
432  else
433  name[ii] = unknown;
434  copyno[ii] = touch->GetReplicaNumber(i);
435  }
436  }
437 #ifdef EDM_ML_DEBUG
438  edm::LogVerbatim("CaloSimX") << "CaloTrkProcessing::detectorLevel "
439  << " with " << level << ":" << detLevels(touch) << " levels";
440  for (int ii = 0; ii < level; ii++)
441  edm::LogVerbatim("CaloSimX") << "[" << ii << "] " << name[ii] << ":" << copyno[ii];
442 #endif
443 }
void update(const BeginOfEvent *evt) override
This routine will be called when the appropriate signal arrives.
Log< level::Info, true > LogVerbatim
int getIDCaloVolume() const
bool crossedBoundary() const
std::vector< std::string > insideNames_
G4LogicalVolume * detLV(const G4VTouchable *, int) const
Log< level::Error, false > LogError
void setStartedInFineVolume(bool flag=true)
bool caloIDChecked() const
void detectorLevel(const G4VTouchable *, int &, int *, G4String *) const
int isItInside(const G4VTouchable *, int, int)
def detectors(dt=True, csc=True, me42=False, chambers=True, superlayers=False, layers=False)
std::vector< Detector > fineDetectors_
int detLevels(const G4VTouchable *) const
int getIDonCaloSurface() const
ii
Definition: cuy.py:589
CaloTrkProcessing(const std::string &aSDname, const CaloSimulationParameters &csps, const SensitiveDetectorCatalog &clg, bool testBeam, double eMin, bool putHistory, bool doFineCalo, double eMinFine, int addlevel, const std::vector< std::string > &fineNames, const std::vector< int > &fineLevels, const std::vector< int > &useFines, const SimTrackManager *)
int getIDLastVolume() const
int isItCalo(const G4VTouchable *, const std::vector< Detector > &)
~CaloTrkProcessing() override
bool startedInFineVolumeIsSet()
bool startedInFineVolume() const
void setCaloIDChecked(bool f)
void setIDonCaloSurface(int id, int ical, int last, int pdgID, double p)
void setCrossedBoundary(const G4Track *track)
std::vector< std::string > caloNames_
std::vector< Detector > detectors_