CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
19 
20 #include "G4LogicalVolumeStore.hh"
21 #include "G4PhysicalVolumeStore.hh"
22 #include "G4Step.hh"
23 #include "G4TransportationManager.hh"
24 #include "G4TouchableHistory.hh"
25 #include "G4Track.hh"
26 #include "DD4hep/Filter.h"
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:
64  std::vector<std::string> LVNames_;
65  G4VPhysicalVolume *topPV_;
66  G4LogicalVolume *topLV_;
67  std::map<G4LogicalVolume *, std::pair<unsigned int, std::string>> mapLV_;
69  int addlevel_;
70 
71  // some private members for ananlysis
72  unsigned int count_;
73  bool init_;
74  std::map<std::pair<G4LogicalVolume *, unsigned int>, std::array<double, 3>> store_;
75 };
76 
77 HGCPassive::HGCPassive(const edm::ParameterSet &p) : topPV_(nullptr), topLV_(nullptr), count_(0), init_(false) {
78  edm::ParameterSet m_Passive = p.getParameter<edm::ParameterSet>("HGCPassive");
79  LVNames_ = m_Passive.getParameter<std::vector<std::string>>("LVNames");
80  motherName_ = m_Passive.getParameter<std::string>("MotherName");
81  bool dd4hep = m_Passive.getParameter<bool>("IfDD4hep");
82  addlevel_ = dd4hep ? 1 : 0;
83 
84 #ifdef EDM_ML_DEBUG
85  edm::LogVerbatim("HGCSim") << "Name of the mother volume " << motherName_ << " AddLevel " << addlevel_;
86  unsigned k(0);
87 #endif
88  for (const auto &name : LVNames_) {
89  produces<edm::PassiveHitContainer>(Form("%sPassiveHits", name.c_str()));
90 #ifdef EDM_ML_DEBUG
91  edm::LogVerbatim("HGCSim") << "Collection name[" << k << "] " << name;
92  ++k;
93 #endif
94  }
95 }
96 
98 
100  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
101  std::unique_ptr<edm::PassiveHitContainer> hgcPH(new edm::PassiveHitContainer);
102  endOfEvent(*hgcPH, k);
103  e.put(std::move(hgcPH), Form("%sPassiveHits", LVNames_[k].c_str()));
104  }
105 }
106 
108  topPV_ = getTopPV();
109  if (topPV_ == nullptr) {
110  edm::LogWarning("HGCSim") << "Cannot find top level volume\n";
111  } else {
112  init_ = true;
113  const G4LogicalVolumeStore *lvs = G4LogicalVolumeStore::GetInstance();
114  for (auto lvcite : *lvs) {
115  findLV(lvcite);
116  }
117 
118 #ifdef EDM_ML_DEBUG
119  edm::LogVerbatim("HGCSim") << "HGCPassive::Finds " << mapLV_.size() << " logical volumes";
120  unsigned int k(0);
121  for (const auto &lvs : mapLV_) {
122  edm::LogVerbatim("HGCSim") << "Entry[" << k << "] " << lvs.first << ": (" << (lvs.second).first << ", "
123  << (lvs.second).second << ")";
124  ++k;
125  }
126 #endif
127  }
128 }
129 
130 //=================================================================== per EVENT
132  int iev = (*evt)()->GetEventID();
133  edm::LogVerbatim("HGCSim") << "HGCPassive: =====> Begin event = " << iev << std::endl;
134 
135  ++count_;
136  store_.clear();
137 }
138 
139 // //=================================================================== each
140 // STEP
141 void HGCPassive::update(const G4Step *aStep) {
142  if (aStep != nullptr) {
143  G4VSensitiveDetector *curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
144  const G4VTouchable *touchable = aStep->GetPreStepPoint()->GetTouchable();
145 
146  int level = (touchable->GetHistoryDepth());
147  if (curSD == nullptr) {
148  G4LogicalVolume *plv = touchable->GetVolume()->GetLogicalVolume();
149  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
150  double time = aStep->GetTrack()->GetGlobalTime();
151  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
152 
153  unsigned int copy(0);
154  if (((aStep->GetPostStepPoint() == nullptr) || (aStep->GetTrack()->GetNextVolume() == nullptr)) &&
155  (aStep->IsLastStepInVolume())) {
156 #ifdef EDM_ML_DEBUG
157  edm::LogVerbatim("HGCSim") << static_cast<std::string>(dd4hep::dd::noNamespace(plv->GetName())) << " F|L Step "
158  << aStep->IsFirstStepInVolume() << ":" << aStep->IsLastStepInVolume() << " Position"
159  << aStep->GetPreStepPoint()->GetPosition() << " Track "
160  << aStep->GetTrack()->GetDefinition()->GetParticleName() << " at"
161  << aStep->GetTrack()->GetPosition() << " Volume " << aStep->GetTrack()->GetVolume()
162  << ":" << aStep->GetTrack()->GetNextVolume() << " Status "
163  << aStep->GetTrack()->GetTrackStatus() << " KE "
164  << aStep->GetTrack()->GetKineticEnergy() << " Deposit "
165  << aStep->GetTotalEnergyDeposit() << " Map " << (it != mapLV_.end());
166 #endif
167  energy += (aStep->GetPreStepPoint()->GetKineticEnergy() / CLHEP::GeV);
168  } else {
169  time = (aStep->GetPostStepPoint()->GetGlobalTime());
170  copy = (level < 2)
171  ? 0
172  : static_cast<unsigned int>(touchable->GetReplicaNumber(0) + 1000 * touchable->GetReplicaNumber(1));
173  }
174  if (it != mapLV_.end()) {
175  storeInfo(it, plv, copy, time, energy, true);
176  } else if (topLV_ != nullptr) {
177  auto itr = findLV(topLV_);
178  if (itr != mapLV_.end()) {
179  storeInfo(itr, topLV_, copy, time, energy, true);
180  }
181  }
182  } // if (curSD==NULL)
183 
184  // Now for the mother volumes
185  if (level > 0) {
186  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
187  double time = (aStep->GetTrack()->GetGlobalTime());
188 
189  for (int i = level; i > 0; --i) {
190  G4LogicalVolume *plv = touchable->GetVolume(i)->GetLogicalVolume();
191  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
192 #ifdef EDM_ML_DEBUG
193  edm::LogVerbatim("HGCSim") << "Level: " << level << ":" << i << " "
194  << static_cast<std::string>(dd4hep::dd::noNamespace(plv->GetName()))
195  << " flag in the List " << (it != mapLV_.end());
196 #endif
197  if (it != mapLV_.end()) {
198  unsigned int copy =
199  (i == level) ? 0
200  : (unsigned int)(touchable->GetReplicaNumber(i) + 1000 * touchable->GetReplicaNumber(i + 1));
201  storeInfo(it, plv, copy, time, energy, false);
202  }
203  }
204  }
205  } // if (aStep != NULL)
206 
207 } // end update aStep
208 
209 //================================================================ End of EVENT
210 
212 #ifdef EDM_ML_DEBUG
213  unsigned int kount(0);
214 #endif
215  for (const auto &element : store_) {
216  G4LogicalVolume *lv = (element.first).first;
217  auto it = mapLV_.find(lv);
218  if (it != mapLV_.end()) {
219  if ((it->second).first == k) {
220  PassiveHit hit(
221  (it->second).second, (element.first).second, (element.second)[1], (element.second)[2], (element.second)[0]);
222  hgcPH.push_back(hit);
223 #ifdef EDM_ML_DEBUG
224  edm::LogVerbatim("HGCSim") << "HGCPassive[" << k << "] Hit[" << kount << "] " << hit;
225  ++kount;
226 #endif
227  }
228  }
229  }
230 }
231 
232 G4VPhysicalVolume *HGCPassive::getTopPV() {
233  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
234 }
235 
237  auto itr = mapLV_.find(plv);
238  if (itr == mapLV_.end()) {
239  std::string name = static_cast<std::string>(dd4hep::dd::noNamespace(plv->GetName()));
240  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
241  if (name.find(LVNames_[k]) != std::string::npos) {
242  mapLV_[plv] = std::pair<unsigned int, std::string>(k, name);
243  itr = mapLV_.find(plv);
244  break;
245  }
246  }
247  }
248  if (topLV_ == nullptr) {
249  if (static_cast<std::string>(dd4hep::dd::noNamespace(plv->GetName())) == motherName_)
250  topLV_ = plv;
251  }
252  return itr;
253 }
254 
256  G4LogicalVolume *plv,
257  unsigned int copy,
258  double time,
259  double energy,
260  bool flag) {
261  std::pair<G4LogicalVolume *, unsigned int> key(plv, copy);
262  auto itr = store_.find(key);
263  double ee = (flag) ? energy : 0;
264  if (itr == store_.end()) {
265  store_[key] = {{time, energy, energy}};
266  } else {
267  (itr->second)[1] += ee;
268  (itr->second)[2] += energy;
269  }
270 #ifdef EDM_ML_DEBUG
271  itr = store_.find(key);
272  edm::LogVerbatim("HGCSim") << "HGCPassive: Element " << (it->second).first << ":" << (it->second).second << ":"
273  << copy << " T " << (itr->second)[0] << " E " << (itr->second)[1] << ":"
274  << (itr->second)[2];
275 #endif
276 }
277 
280 
Log< level::Info, true > LogVerbatim
#define DEFINE_SIMWATCHER(type)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
const double GeV
Definition: MathUtil.h:16
void storeInfo(const volumeIterator itr, G4LogicalVolume *plv, unsigned int copy, double time, double energy, bool flag)
Definition: HGCPassive.cc:255
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator volumeIterator
Definition: HGCPassive.cc:57
int addlevel_
Definition: HGCPassive.cc:69
G4LogicalVolume * topLV_
Definition: HGCPassive.cc:66
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:211
volumeIterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:236
G4VPhysicalVolume * topPV_
Definition: HGCPassive.cc:65
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:232
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:77
tuple dd4hep
Definition: dd4hep_cff.py:3
std::vector< std::string > LVNames_
Definition: HGCPassive.cc:64
~HGCPassive() override
Definition: HGCPassive.cc:97
def move
Definition: eostools.py:511
tuple key
prepare the HTCondor submission files and eventually submit them
std::string motherName_
Definition: HGCPassive.cc:68
void produce(edm::Event &, const edm::EventSetup &) override
Definition: HGCPassive.cc:99
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:100
unsigned int count_
Definition: HGCPassive.cc:72
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::array< double, 3 > > store_
Definition: HGCPassive.cc:74
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:107
step
Definition: StallMonitor.cc:94
tuple level
Definition: testEve_cfg.py:47
Log< level::Warning, false > LogWarning
HitContainer const *__restrict__ TkSoA const *__restrict__ Quality const *__restrict__ CAHitNtupletGeneratorKernelsGPU::HitToTuple const *__restrict__ int32_t int iev
const HGCPassive & operator=(const HGCPassive &)=delete
bool init_
Definition: HGCPassive.cc:73
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.cc:67