CMS 3D CMS Logo

HGCPassive.cc
Go to the documentation of this file.
1 // File: HGCPassive.cc
3 // copied from SimG4HGCalValidation
4 // Description: Main analysis class for HGCal Validation of G4 Hits
6 
11 
12 // to retreive hits
14 
20 
21 #include "G4LogicalVolumeStore.hh"
22 #include "G4PhysicalVolumeStore.hh"
23 #include "G4Step.hh"
24 #include "G4TransportationManager.hh"
25 #include "G4TouchableHistory.hh"
26 #include "G4Track.hh"
27 
28 #include <array>
29 #include <cmath>
30 #include <map>
31 #include <string>
32 #include <vector>
33 
34 //#define EDM_ML_DEBUG
35 
36 class HGCPassive : public SimProducer,
37  public Observer<const BeginOfRun *>,
38  public Observer<const BeginOfEvent *>,
39  public Observer<const G4Step *> {
40 public:
42  HGCPassive(const HGCPassive &) = delete; // stop default
43  const HGCPassive &operator=(const HGCPassive &) = delete;
44  ~HGCPassive() override;
45 
46  void produce(edm::Event &, const edm::EventSetup &) override;
47 
48 private:
49  // observer classes
50  void update(const BeginOfRun *run) override;
51  void update(const BeginOfEvent *evt) override;
52  void update(const G4Step *step) override;
53 
54  // void endOfEvent(edm::PassiveHitContainer &HGCEEAbsE);
55  void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k);
56 
57  typedef std::map<G4LogicalVolume *, std::pair<unsigned int, std::string>>::iterator volumeIterator;
58  G4VPhysicalVolume *getTopPV();
59  volumeIterator findLV(G4LogicalVolume *plv);
60  void storeInfo(
61  const volumeIterator itr, G4LogicalVolume *plv, unsigned int copy, double time, double energy, bool flag);
62 
63 private:
65  const std::vector<std::string> LVNames_;
67  const int addlevel_;
68  G4VPhysicalVolume *topPV_;
69  G4LogicalVolume *topLV_;
70  std::map<G4LogicalVolume *, std::pair<unsigned int, std::string>> mapLV_;
71 
72  // some private members for ananlysis
73  unsigned int count_;
74  bool init_;
75  std::map<std::pair<G4LogicalVolume *, unsigned int>, std::array<double, 3>> store_;
76 };
77 
79  : m_Passive(p.getParameter<edm::ParameterSet>("HGCPassive")),
80  LVNames_(m_Passive.getParameter<std::vector<std::string>>("LVNames")),
81  motherName_(m_Passive.getParameter<std::string>("MotherName")),
82  addlevel_((m_Passive.getParameter<bool>("IfDD4hep")) ? 1 : 0),
83  topPV_(nullptr),
84  topLV_(nullptr),
85  count_(0),
86  init_(false) {
87 #ifdef EDM_ML_DEBUG
88  edm::LogVerbatim("HGCSim") << "Name of the mother volume " << motherName_ << " AddLevel " << addlevel_;
89  unsigned k(0);
90 #endif
91  for (const auto &name : LVNames_) {
92  produces<edm::PassiveHitContainer>(Form("%sPassiveHits", name.c_str()));
93 #ifdef EDM_ML_DEBUG
94  edm::LogVerbatim("HGCSim") << "Collection name[" << k << "] " << name;
95  ++k;
96 #endif
97  }
98 }
99 
101 
103  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
104  std::unique_ptr<edm::PassiveHitContainer> hgcPH(new edm::PassiveHitContainer);
105  endOfEvent(*hgcPH, k);
106  e.put(std::move(hgcPH), Form("%sPassiveHits", LVNames_[k].c_str()));
107  }
108 }
109 
111  topPV_ = getTopPV();
112  if (topPV_ == nullptr) {
113  edm::LogWarning("HGCSim") << "Cannot find top level volume\n";
114  } else {
115  init_ = true;
116  const G4LogicalVolumeStore *lvs = G4LogicalVolumeStore::GetInstance();
117  for (auto lvcite : *lvs) {
118  findLV(lvcite);
119  }
120 
121 #ifdef EDM_ML_DEBUG
122  edm::LogVerbatim("HGCSim") << "HGCPassive::Finds " << mapLV_.size() << " logical volumes";
123  unsigned int k(0);
124  for (const auto &lvs : mapLV_) {
125  edm::LogVerbatim("HGCSim") << "Entry[" << k << "] " << lvs.first << ": (" << (lvs.second).first << ", "
126  << (lvs.second).second << ")";
127  ++k;
128  }
129 #endif
130  }
131 }
132 
133 //=================================================================== per EVENT
135  int iev = (*evt)()->GetEventID();
136  edm::LogVerbatim("HGCSim") << "HGCPassive: =====> Begin event = " << iev << std::endl;
137 
138  ++count_;
139  store_.clear();
140 }
141 
142 // //=================================================================== each
143 // STEP
144 void HGCPassive::update(const G4Step *aStep) {
145  if (aStep != nullptr) {
146  G4VSensitiveDetector *curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
147  const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();
148 
149  int level = (touchable->GetHistoryDepth());
150  if (curSD == nullptr) {
151  G4LogicalVolume *plv = touchable->GetVolume()->GetLogicalVolume();
152  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
153  double time = aStep->GetTrack()->GetGlobalTime();
154  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
155 
156  unsigned int copy(0);
157  if (((aStep->GetPostStepPoint() == nullptr) || (aStep->GetTrack()->GetNextVolume() == nullptr)) &&
158  (aStep->IsLastStepInVolume())) {
159 #ifdef EDM_ML_DEBUG
160  edm::LogVerbatim("HGCSim") << DD4hep2DDDName::noNameSpace(static_cast<std::string>(plv->GetName()))
161  << " F|L Step " << aStep->IsFirstStepInVolume() << ":" << aStep->IsLastStepInVolume()
162  << " Position" << aStep->GetPreStepPoint()->GetPosition() << " Track "
163  << aStep->GetTrack()->GetDefinition()->GetParticleName() << " at"
164  << aStep->GetTrack()->GetPosition() << " Volume " << aStep->GetTrack()->GetVolume()
165  << ":" << aStep->GetTrack()->GetNextVolume() << " Status "
166  << aStep->GetTrack()->GetTrackStatus() << " KE "
167  << aStep->GetTrack()->GetKineticEnergy() << " Deposit "
168  << aStep->GetTotalEnergyDeposit() << " Map " << (it != mapLV_.end());
169 #endif
170  energy += (aStep->GetPreStepPoint()->GetKineticEnergy() / CLHEP::GeV);
171  } else {
172  time = (aStep->GetPostStepPoint()->GetGlobalTime());
173  copy = (level < 2)
174  ? 0
175  : static_cast<unsigned int>(touchable->GetReplicaNumber(0) + 1000 * touchable->GetReplicaNumber(1));
176  }
177  if (it != mapLV_.end()) {
178  storeInfo(it, plv, copy, time, energy, true);
179  } else if (topLV_ != nullptr) {
180  auto itr = findLV(topLV_);
181  if (itr != mapLV_.end()) {
182  storeInfo(itr, topLV_, copy, time, energy, true);
183  }
184  }
185  } // if (curSD==NULL)
186 
187  // Now for the mother volumes
188  if (level > 0) {
189  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
190  double time = (aStep->GetTrack()->GetGlobalTime());
191 
192  for (int i = level; i > 0; --i) {
193  G4LogicalVolume *plv = touchable->GetVolume(i)->GetLogicalVolume();
194  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
195 #ifdef EDM_ML_DEBUG
196  edm::LogVerbatim("HGCSim") << "Level: " << level << ":" << i << " "
197  << DD4hep2DDDName::noNameSpace(static_cast<std::string>(plv->GetName()))
198  << " flag in the List " << (it != mapLV_.end());
199 #endif
200  if (it != mapLV_.end()) {
201  unsigned int copy =
202  (i == level) ? 0
203  : (unsigned int)(touchable->GetReplicaNumber(i) + 1000 * touchable->GetReplicaNumber(i + 1));
204  storeInfo(it, plv, copy, time, energy, false);
205  }
206  }
207  }
208  } // if (aStep != NULL)
209 
210 } // end update aStep
211 
212 //================================================================ End of EVENT
213 
215 #ifdef EDM_ML_DEBUG
216  unsigned int kount(0);
217 #endif
218  for (const auto &element : store_) {
219  G4LogicalVolume *lv = (element.first).first;
220  auto it = mapLV_.find(lv);
221  if (it != mapLV_.end()) {
222  if ((it->second).first == k) {
223  PassiveHit hit(
224  (it->second).second, (element.first).second, (element.second)[1], (element.second)[2], (element.second)[0]);
225  hgcPH.push_back(hit);
226 #ifdef EDM_ML_DEBUG
227  edm::LogVerbatim("HGCSim") << "HGCPassive[" << k << "] Hit[" << kount << "] " << hit;
228  ++kount;
229 #endif
230  }
231  }
232  }
233 }
234 
235 G4VPhysicalVolume *HGCPassive::getTopPV() {
236  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
237 }
238 
240  auto itr = mapLV_.find(plv);
241  if (itr == mapLV_.end()) {
242  std::string name = DD4hep2DDDName::noNameSpace(static_cast<std::string>(plv->GetName()));
243  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
244  if (name.find(LVNames_[k]) != std::string::npos) {
245  mapLV_[plv] = std::pair<unsigned int, std::string>(k, name);
246  itr = mapLV_.find(plv);
247  break;
248  }
249  }
250  }
251  if (topLV_ == nullptr) {
252  if (DD4hep2DDDName::noNameSpace(static_cast<std::string>(plv->GetName())) == motherName_)
253  topLV_ = plv;
254  }
255  return itr;
256 }
257 
259  G4LogicalVolume *plv,
260  unsigned int copy,
261  double time,
262  double energy,
263  bool flag) {
264  std::pair<G4LogicalVolume *, unsigned int> key(plv, copy);
265  auto itr = store_.find(key);
266  double ee = (flag) ? energy : 0;
267  if (itr == store_.end()) {
268  store_[key] = {{time, energy, energy}};
269  } else {
270  (itr->second)[1] += ee;
271  (itr->second)[2] += energy;
272  }
273 #ifdef EDM_ML_DEBUG
274  itr = store_.find(key);
275  edm::LogVerbatim("HGCSim") << "HGCPassive: Element " << (it->second).first << ":" << (it->second).second << ":"
276  << copy << " T " << (itr->second)[0] << " E " << (itr->second)[1] << ":"
277  << (itr->second)[2];
278 #endif
279 }
280 
283 
Log< level::Info, true > LogVerbatim
#define DEFINE_SIMWATCHER(type)
void storeInfo(const volumeIterator itr, G4LogicalVolume *plv, unsigned int copy, double time, double energy, bool flag)
Definition: HGCPassive.cc:258
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator volumeIterator
Definition: HGCPassive.cc:57
G4LogicalVolume * topLV_
Definition: HGCPassive.cc:69
TkSoAView< TrackerTraits > HitToTuple< TrackerTraits > const *__restrict__ int32_t int32_t int iev
const int addlevel_
Definition: HGCPassive.cc:67
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:214
volumeIterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:239
G4VPhysicalVolume * topPV_
Definition: HGCPassive.cc:68
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:235
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:78
~HGCPassive() override
Definition: HGCPassive.cc:100
const std::string motherName_
Definition: HGCPassive.cc:66
key
prepare the HTCondor submission files and eventually submit them
const edm::ParameterSet m_Passive
Definition: HGCPassive.cc:64
void produce(edm::Event &, const edm::EventSetup &) override
Definition: HGCPassive.cc:102
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:100
unsigned int count_
Definition: HGCPassive.cc:73
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::array< double, 3 > > store_
Definition: HGCPassive.cc:75
HLT enums.
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:110
const std::vector< std::string > LVNames_
Definition: HGCPassive.cc:65
std::string noNameSpace(const std::string &name)
step
Definition: StallMonitor.cc:83
Log< level::Warning, false > LogWarning
const HGCPassive & operator=(const HGCPassive &)=delete
bool init_
Definition: HGCPassive.cc:74
def move(src, dest)
Definition: eostools.py:511
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.cc:70