CMS 3D CMS Logo

CaloTrkProcessing.cc
Go to the documentation of this file.
5 
7 
13 
14 #include "G4EventManager.hh"
15 
16 #include "G4LogicalVolumeStore.hh"
17 #include "G4LogicalVolume.hh"
18 #include "G4Step.hh"
19 #include "G4Track.hh"
20 
21 #include "G4SystemOfUnits.hh"
22 
23 //#define DebugLog
24 
26  const DDCompactView & cpv,
27  const SensitiveDetectorCatalog & clg,
28  edm::ParameterSet const & p,
29  const SimTrackManager* manager) :
30  SensitiveCaloDetector(name, cpv, clg, p), lastTrackID(-1),
31  m_trackManager(manager) {
32 
33  //Initialise the parameter set
34  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("CaloTrkProcessing");
35  testBeam = m_p.getParameter<bool>("TestBeam");
36  eMin = m_p.getParameter<double>("EminTrack")*MeV;
37  putHistory = m_p.getParameter<bool>("PutHistory");
38 
39  edm::LogInfo("CaloSim") << "CaloTrkProcessing: Initailised with TestBeam = "
40  << testBeam << " Emin = " << eMin << " MeV and"
41  << " History flag " << putHistory;
42 
43  //Get the names
44  G4String attribute = "ReadOutName";
45  DDSpecificsMatchesValueFilter filter{DDValue(attribute,name,0)};
46  DDFilteredView fv(cpv,filter);
47  fv.firstChild();
49 
50  G4String value = "Calorimeter";
51  std::vector<std::string> caloNames = getNames (value, sv);
52 #ifdef DebugLog
53  LogDebug("CaloSim") << "CaloTrkProcessing: Names for " << value << ":";
54  for (unsigned int i=0; i<caloNames.size(); i++)
55  LogDebug("CaloSim") << " (" << i << ") " << caloNames[i];
56 #endif
57 
58  value = "Levels";
59  std::vector<double> levels = getNumbers (value, sv);
60 #ifdef DebugLog
61  LogDebug("CaloSim") << "CaloTrkProcessing: Names for " << value << ":";
62  for (unsigned int i=0; i<levels.size(); i++)
63  LogDebug("CaloSim") << " (" << i << ") " << levels[i];
64 #endif
65 
66  value = "Neighbours";
67  std::vector<double> neighbours = getNumbers (value, sv);
68 #ifdef DebugLog
69  LogDebug("CaloSim") << "CaloTrkProcessing: Names for " << value << ":";
70  for (unsigned int i=0; i<neighbours.size(); i++)
71  LogDebug("CaloSim") << " (" << i << ") " << neighbours[i];
72 #endif
73 
74  value = "Inside";
75  std::vector<std::string> insideNames = getNames (value, sv);
76 #ifdef DebugLog
77  LogDebug("CaloSim") << "CaloTrkProcessing: Names for " << value << ":";
78  for (unsigned int i=0; i<insideNames.size(); i++)
79  LogDebug("CaloSim") << " (" << i << ") " << insideNames[i];
80 #endif
81 
82  value = "InsideLevel";
83  std::vector<double> insideLevel = getNumbers (value, sv);
84 #ifdef DebugLog
85  LogDebug("CaloSim") << "CaloTrkProcessing: Names for " << value << ":";
86  for (unsigned int i=0; i<insideLevel.size(); i++)
87  LogDebug("CaloSim") << " (" << i << ") " << insideLevel[i];
88 #endif
89 
90  if (caloNames.size() < neighbours.size()) {
91  edm::LogError("CaloSim") << "CaloTrkProcessing: # of Calorimeter bins "
92  << caloNames.size() << " does not match with "
93  << neighbours.size() << " ==> illegal ";
94  throw cms::Exception("Unknown", "CaloTrkProcessing")
95  << "Calorimeter array size does not match with size of neighbours\n";
96  }
97 
98  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
99  std::vector<G4LogicalVolume *>::const_iterator lvcite;
100  int istart = 0;
101  for (unsigned int i=0; i<caloNames.size(); i++) {
102  G4LogicalVolume* lv = nullptr;
103  G4String name = caloNames[i];
104  int number = static_cast<int>(neighbours[i]);
105  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
106  if ((*lvcite)->GetName() == name) {
107  lv = (*lvcite);
108  break;
109  }
110  }
111  if (lv != nullptr) {
113  detector.name = name;
114  detector.lv = lv;
115  detector.level = static_cast<int>(levels[i]);
116  if (istart+number > (int)(insideNames.size())) {
117  edm::LogError("CaloSim") << "CaloTrkProcessing: # of InsideNames bins "
118  << insideNames.size() <<" too few compaerd to "
119  << istart+number << " requested ==> illegal ";
120  throw cms::Exception("Unknown", "CaloTrkProcessing")
121  << "InsideNames array size does not match with list of neighbours\n";
122  }
123  std::vector<std::string> inside;
124  std::vector<G4LogicalVolume*> insideLV;
125  std::vector<int> insideLevels;
126  for (int k = 0; k < number; k++) {
127  lv = nullptr;
128  name = insideNames[istart+k];
129  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++)
130  if ((*lvcite)->GetName() == name) {
131  lv = (*lvcite);
132  break;
133  }
134  inside.push_back(name);
135  insideLV.push_back(lv);
136  insideLevels.push_back(static_cast<int>(insideLevel[istart+k]));
137  }
138  detector.fromDets = inside;
139  detector.fromDetL = insideLV;
140  detector.fromLevels = insideLevels;
141  detectors.push_back(detector);
142  }
143  istart += number;
144  }
145 
146  edm::LogInfo("CaloSim") << "CaloTrkProcessing: with " << detectors.size()
147  << " calorimetric volumes";
148  for (unsigned int i=0; i<detectors.size(); i++) {
149  edm::LogInfo("CaloSim") << "CaloTrkProcessing: Calorimeter volume " << i
150  << " " << detectors[i].name << " LV "
151  << detectors[i].lv << " at level "
152  << detectors[i].level << " with "
153  << detectors[i].fromDets.size() << " neighbours";
154  for (unsigned int k=0; k<detectors[i].fromDets.size(); k++)
155  edm::LogInfo("CaloSim") << " Element " << k << " "
156  << detectors[i].fromDets[k] << " LV "
157  << detectors[i].fromDetL[k] << " at level "
158  << detectors[i].fromLevels[k];
159  }
160 }
161 
163  edm::LogInfo("CaloSim") << "CaloTrkProcessing: Deleted";
164 }
165 
167  lastTrackID = -1;
168 }
169 
170 void CaloTrkProcessing::update(const G4Step * aStep) {
171 
172  // define if you are at the surface of CALO
173 
174  G4Track* theTrack = aStep->GetTrack();
175  int id = theTrack->GetTrackID();
176 
177  TrackInformation* trkInfo = dynamic_cast<TrackInformation*>
178  (theTrack->GetUserInformation());
179 
180  if (trkInfo == nullptr) {
181  edm::LogError("CaloSim") << "CaloTrkProcessing: No trk info !!!! abort ";
182  throw cms::Exception("Unknown", "CaloTrkProcessing")
183  << "cannot get trkInfo for Track " << id << "\n";
184  }
185 
186  if (testBeam) {
187  if (trkInfo->getIDonCaloSurface() == 0) {
188 #ifdef DebugLog
189  LogDebug("CaloSim") << "CaloTrkProcessing set IDonCaloSurface to " << id
190  << " at step Number "
191  << theTrack->GetCurrentStepNumber();
192 #endif
193  trkInfo->setIDonCaloSurface(id,0,0,
194  theTrack->GetDefinition()->GetPDGEncoding(),
195  theTrack->GetMomentum().mag());
196  lastTrackID = id;
197  if (theTrack->GetKineticEnergy()/MeV > eMin)
198  trkInfo->putInHistory();
199  }
200  } else {
201  if (putHistory) {
202  trkInfo->putInHistory();
203  // trkInfo->setAncestor();
204  }
205 #ifdef DebugLog
206  LogDebug("CaloSim") << "CaloTrkProcessing Entered for " << id
207  << " at stepNumber "<< theTrack->GetCurrentStepNumber()
208  << " IDonCaloSur.. " << trkInfo->getIDonCaloSurface()
209  << " CaloCheck " << trkInfo->caloIDChecked();
210 #endif
211  if (trkInfo->getIDonCaloSurface() != 0) {
212  if (trkInfo->caloIDChecked() == false) {
213  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
214  const G4VTouchable* post_touch = postStepPoint->GetTouchable();
215 
216  if (isItInside(post_touch, trkInfo->getIDCaloVolume(),
217  trkInfo->getIDLastVolume()) > 0) {
218  trkInfo->setIDonCaloSurface(0, -1, -1, 0, 0);
219  } else {
220  trkInfo->setCaloIDChecked(true);
221  }
222  }
223  } else {
224  G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
225  const G4VTouchable* post_touch = postStepPoint->GetTouchable();
226  int ical = isItCalo(post_touch);
227  if (ical >= 0) {
228  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
229  const G4VTouchable* pre_touch = preStepPoint->GetTouchable();
230  int inside = isItInside(pre_touch, ical, -1);
231  if (inside >= 0 || (theTrack->GetCurrentStepNumber()==1)) {
232  trkInfo->setIDonCaloSurface(id,ical,inside,
233  theTrack->GetDefinition()->GetPDGEncoding(),
234  theTrack->GetMomentum().mag());
235  trkInfo->setCaloIDChecked(true);
236  lastTrackID = id;
237  if (theTrack->GetKineticEnergy()/MeV > eMin) trkInfo->putInHistory();
238 #ifdef DebugLog
239  LogDebug("CaloSim") <<"CaloTrkProcessing: set ID on Calo " << ical
240  << " surface (Inside " << inside << ") to "
241  << id << " of a Track with Kinetic Energy "
242  << theTrack->GetKineticEnergy()/MeV << " MeV";
243 #endif
244  }
245  }
246  }
247  }
248 }
249 
250 std::vector<std::string> CaloTrkProcessing::getNames(const G4String str,
251  const DDsvalues_type &sv){
252 
253 #ifdef DebugLog
254  LogDebug("CaloSim") << "CaloTrkProcessing::getNames called for " << str;
255 #endif
256  DDValue value(str);
257  if (DDfetch(&sv,value)) {
258 #ifdef DebugLog
259  LogDebug("CaloSim") << value;
260 #endif
261  const std::vector<std::string> & fvec = value.strings();
262  int nval = fvec.size();
263  if (nval < 1) {
264 
265  edm::LogError("CaloSim") << "CaloTrkProcessing: # of " << str
266  << " bins " << nval << " < 1 ==> illegal ";
267  throw cms::Exception("Unknown", "CaloTrkProcessing")
268  << "nval < 2 for array " << str << "\n";
269  }
270 
271  return fvec;
272  } else {
273  edm::LogError("CaloSim") << "CaloTrkProcessing: cannot get array " << str ;
274  throw cms::Exception("Unknown", "CaloTrkProcessing")
275  << "cannot get array " << str << "\n";
276  }
277 }
278 
279 std::vector<double> CaloTrkProcessing::getNumbers(const G4String str,
280  const DDsvalues_type &sv) {
281 
282 #ifdef DebugLog
283  LogDebug("CaloSim") << "CaloTrkProcessing::getNumbers called for " << str;
284 #endif
285  DDValue value(str);
286  if (DDfetch(&sv,value)) {
287 #ifdef DebugLog
288  LogDebug("CaloSim") << value;
289 #endif
290  const std::vector<double> & fvec = value.doubles();
291  int nval = fvec.size();
292  if (nval < 1) {
293  edm::LogError("CaloSim") << "CaloTrkProcessing: # of " << str
294  << " bins " << nval << " < 1 ==> illegal ";
295  throw cms::Exception("Unknown", "CaloTrkProcessing")
296  << "nval < 2 for array " << str << "\n";
297  }
298 
299  return fvec;
300  } else {
301  edm::LogError("CaloSim") << "CaloTrkProcessing: cannot get array " << str ;
302  throw cms::Exception("Unknown", "CaloTrkProcessing")
303  << "cannot get array " << str << "\n";
304  }
305 }
306 
307 int CaloTrkProcessing::isItCalo(const G4VTouchable* touch) {
308 
309  int lastLevel = -1;
310  G4LogicalVolume* lv=nullptr;
311  for (unsigned int it=0; it < detectors.size(); it++) {
312  if (lastLevel != detectors[it].level) {
313  lastLevel = detectors[it].level;
314  lv = detLV(touch, lastLevel);
315 #ifdef DebugLog
316  std::string name1 = "Unknown";
317  if (lv != 0) name1 = lv->GetName();
318  LogDebug("CaloSim") << "CaloTrkProcessing: volume " << name1
319  << " at Level " << lastLevel;
320  int levels = detLevels(touch);
321  if (levels > 0) {
322  G4String name2[20]; int copyno2[20];
323  detectorLevel(touch, levels, copyno2, name2);
324  for (int i2=0; i2<levels; i2++)
325  LogDebug("CaloSim") << " " << i2 << " " << name2[i2] << " "
326  << copyno2[i2];
327  }
328 #endif
329  }
330  bool ok = (lv == detectors[it].lv);
331  if (ok) return it;
332  }
333  return -1;
334 }
335 
336 int CaloTrkProcessing::isItInside(const G4VTouchable* touch, int idcal,
337  int idin) {
338  int lastLevel = -1;
339  G4LogicalVolume* lv=nullptr;
340  int id1, id2;
341  if (idcal < 0) {id1 = 0; id2 = static_cast<int>(detectors.size());}
342  else {id1 = idcal; id2 = id1+1;}
343  for (int it1 = id1; it1 < id2; it1++) {
344  if (idin < 0) {
345  for (unsigned int it2 = 0; it2 < detectors[it1].fromDets.size(); it2++) {
346  if (lastLevel != detectors[it1].fromLevels[it2]) {
347  lastLevel = detectors[it1].fromLevels[it2];
348  lv = detLV(touch,lastLevel);
349 #ifdef DebugLog
350  std::string name1 = "Unknown";
351  if (lv != 0) name1 = lv->GetName();
352  LogDebug("CaloSim") << "CaloTrkProcessing: volume " << name1
353  << " at Level " << lastLevel;
354  int levels = detLevels(touch);
355  if (levels > 0) {
356  G4String name2[20]; int copyno2[20];
357  detectorLevel(touch, levels, copyno2, name2);
358  for (int i2=0; i2<levels; i2++)
359  LogDebug("CaloSim") << " " << i2 << " " << name2[i2] << " "
360  << copyno2[i2];
361  }
362 #endif
363  }
364  bool ok = (lv == detectors[it1].fromDetL[it2]);
365  if (ok) return it2;
366  }
367  } else {
368  lastLevel = detectors[it1].fromLevels[idin];
369  lv = detLV(touch,lastLevel);
370 #ifdef DebugLog
371  std::string name1 = "Unknown";
372  if (lv != 0) name1 = lv->GetName();
373  LogDebug("CaloSim") << "CaloTrkProcessing: volume " << name1
374  << " at Level " << lastLevel;
375  int levels = detLevels(touch);
376  if (levels > 0) {
377  G4String name2[20]; int copyno2[20];
378  detectorLevel(touch, levels, copyno2, name2);
379  for (int i2=0; i2<levels; i2++)
380  LogDebug("CaloSim") << " " << i2 << " " << name2[i2] << " "
381  << copyno2[i2];
382  }
383 #endif
384  bool ok = (lv == detectors[it1].fromDetL[idin]);
385  if (ok) return idin;
386  }
387  }
388  return -1;
389 }
390 
391 int CaloTrkProcessing::detLevels(const G4VTouchable* touch) const {
392 
393  //Return number of levels
394  if (touch)
395  return ((touch->GetHistoryDepth())+1);
396  else
397  return 0;
398 }
399 
400 G4LogicalVolume* CaloTrkProcessing::detLV(const G4VTouchable* touch,
401  int currentlevel) const {
402 
403  G4LogicalVolume* lv=nullptr;
404  if (touch) {
405  int level = ((touch->GetHistoryDepth())+1);
406  if (level > 0 && level >= currentlevel) {
407  int ii = level - currentlevel;
408  lv = touch->GetVolume(ii)->GetLogicalVolume();
409  return lv;
410  }
411  }
412  return lv;
413 }
414 
415 void CaloTrkProcessing::detectorLevel(const G4VTouchable* touch, int& level,
416  int* copyno, G4String* name) const {
417 
418  static const std::string unknown("Unknown");
419  //Get name and copy numbers
420  if (level > 0) {
421  for (int ii = 0; ii < level; ii++) {
422  int i = level - ii - 1;
423  G4VPhysicalVolume* pv = touch->GetVolume(i);
424  if (pv != nullptr)
425  name[ii] = pv->GetName();
426  else
427  name[ii] = unknown;
428  copyno[ii] = touch->GetReplicaNumber(i);
429  }
430  }
431 }
#define LogDebug(id)
void update(const BeginOfEvent *evt) override
This routine will be called when the appropriate signal arrives.
T getParameter(std::string const &) const
G4LogicalVolume * detLV(const G4VTouchable *, int) const
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:140
CaloTrkProcessing(const std::string &aSDname, const DDCompactView &cpv, const SensitiveDetectorCatalog &clg, edm::ParameterSet const &p, const SimTrackManager *)
int getIDonCaloSurface() const
int detLevels(const G4VTouchable *) const
int isItCalo(const G4VTouchable *)
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:81
void detectorLevel(const G4VTouchable *, int &, int *, G4String *) const
const double MeV
int getIDCaloVolume() const
int isItInside(const G4VTouchable *, int, int)
const std::vector< std::string > & getNames() const
std::vector< int > fromLevels
std::vector< Detector > detectors
def pv(vc)
Definition: MetAnalyzer.py:7
std::vector< double > getNumbers(G4String, const DDsvalues_type &)
std::vector< G4LogicalVolume * > fromDetL
Definition: value.py:1
int getIDLastVolume() const
std::vector< std::string > fromDets
ii
Definition: cuy.py:590
const std::vector< std::string > & strings() const
a reference to the std::string-valued values stored in the given instance of DDValue ...
Definition: DDValue.h:61
int k[5][pyjets_maxn]
~CaloTrkProcessing() override
bool caloIDChecked() const
DDsvalues_type mergedSpecifics() const
void setCaloIDChecked(bool f)
void setIDonCaloSurface(int id, int ical, int last, int pdgID, double p)
std::vector< std::pair< unsigned int, DDValue > > DDsvalues_type
Definition: DDsvalues.h:12
bool firstChild()
set the current node to the first child ...
#define str(s)