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 
7 #include "HGCPassive.h"
9 
10 #include "CLHEP/Units/GlobalPhysicalConstants.h"
11 #include "CLHEP/Units/GlobalSystemOfUnits.h"
12 #include "G4TransportationManager.hh"
13 
14 #include <cmath>
15 #include <iomanip>
16 #include <iostream>
17 #include <memory>
18 #include <utility>
19 
20 //#define EDM_ML_DEBUG
21 
22 HGCPassive::HGCPassive(const edm::ParameterSet& p) : topPV_(nullptr), topLV_(nullptr), count_(0), init_(false) {
23  edm::ParameterSet m_Passive = p.getParameter<edm::ParameterSet>("HGCPassive");
24  LVNames_ = m_Passive.getParameter<std::vector<std::string> >("LVNames");
25  motherName_ = m_Passive.getParameter<std::string>("MotherName");
26 
27 #ifdef EDM_ML_DEBUG
28  edm::LogVerbatim("ValidHGCal") << "Name of the mother volume " << motherName_;
29  unsigned k(0);
30 #endif
31  for (auto name : LVNames_) {
32  produces<edm::PassiveHitContainer>(Form("%sPassiveHits", name.c_str()));
33 #ifdef EDM_ML_DEBUG
34  edm::LogVerbatim("ValidHGCal") << "Collection name[" << k << "] " << name;
35  ++k;
36 #endif
37  }
38 }
39 
41 
43  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
44  std::unique_ptr<edm::PassiveHitContainer> hgcPH(new edm::PassiveHitContainer);
45  endOfEvent(*hgcPH, k);
46  e.put(std::move(hgcPH), Form("%sPassiveHits", LVNames_[k].c_str()));
47  }
48 }
49 
51  topPV_ = getTopPV();
52  if (topPV_ == nullptr) {
53  edm::LogWarning("HGCPassive") << "Cannot find top level volume\n";
54  } else {
55  init_ = true;
56  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
57  for (auto lvcite : *lvs) {
58  findLV(lvcite);
59  }
60 
61 #ifdef EDM_ML_DEBUG
62  edm::LogVerbatim("ValidHGCal") << "HGCPassive::Finds " << mapLV_.size() << " logical volumes";
63  unsigned int k(0);
64  for (const auto& lvs : mapLV_) {
65  edm::LogVerbatim("ValidHGCal") << "Entry[" << k << "] " << lvs.first << ": (" << (lvs.second).first << ", "
66  << (lvs.second).second << ")";
67  ++k;
68  }
69 #endif
70  }
71 }
72 
73 //=================================================================== per EVENT
74 void HGCPassive::update(const BeginOfEvent* evt) {
75  int iev = (*evt)()->GetEventID();
76  edm::LogVerbatim("ValidHGCal") << "HGCPassive: =====> Begin event = " << iev << std::endl;
77 
78  ++count_;
79  store_.clear();
80 }
81 
82 // //=================================================================== each
83 // STEP
84 void HGCPassive::update(const G4Step* aStep) {
85  if (aStep != nullptr) {
86  G4VSensitiveDetector* curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
87  const G4VTouchable* touchable = aStep->GetPreStepPoint()->GetTouchable();
88 
89  if (curSD == nullptr) {
90  G4LogicalVolume* plv = touchable->GetVolume()->GetLogicalVolume();
91  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
92  double time = aStep->GetTrack()->GetGlobalTime();
93  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
94 
95  unsigned int copy(0);
96  if (((aStep->GetPostStepPoint() == nullptr) || (aStep->GetTrack()->GetNextVolume() == nullptr)) &&
97  (aStep->IsLastStepInVolume())) {
98 #ifdef EDM_ML_DEBUG
99  edm::LogVerbatim("ValidHGCal") << plv->GetName() << " F|L Step " << aStep->IsFirstStepInVolume() << ":"
100  << aStep->IsLastStepInVolume() << " Position"
101  << aStep->GetPreStepPoint()->GetPosition() << " Track "
102  << aStep->GetTrack()->GetDefinition()->GetParticleName() << " at"
103  << aStep->GetTrack()->GetPosition() << " Volume "
104  << aStep->GetTrack()->GetVolume() << ":" << aStep->GetTrack()->GetNextVolume()
105  << " Status " << aStep->GetTrack()->GetTrackStatus() << " KE "
106  << aStep->GetTrack()->GetKineticEnergy() << " Deposit "
107  << aStep->GetTotalEnergyDeposit() << " Map " << (it != mapLV_.end());
108 #endif
109  energy += (aStep->GetPreStepPoint()->GetKineticEnergy() / CLHEP::GeV);
110  } else {
111  time = (aStep->GetPostStepPoint()->GetGlobalTime());
112  copy = (unsigned int)(touchable->GetReplicaNumber(0) + 1000 * touchable->GetReplicaNumber(1));
113  }
114  if (it != mapLV_.end()) {
115  storeInfo(it, plv, copy, time, energy, true);
116  } else if (topLV_ != nullptr) {
117  auto itr = findLV(topLV_);
118  if (itr != mapLV_.end()) {
119  storeInfo(itr, topLV_, copy, time, energy, true);
120  }
121  }
122  } // if (curSD==NULL)
123 
124  // Now for the mother volumes
125  int level = (touchable->GetHistoryDepth());
126  if (level > 0) {
127  double energy = (aStep->GetTotalEnergyDeposit()) / CLHEP::GeV;
128  double time = (aStep->GetTrack()->GetGlobalTime());
129 
130  for (int i = level; i > 0; --i) {
131  G4LogicalVolume* plv = touchable->GetVolume(i)->GetLogicalVolume();
132  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
133 #ifdef EDM_ML_DEBUG
134  edm::LogVerbatim("ValidHGCal") << "Level: " << ii << ":" << i << " " << plv->GetName() << " flag in the List "
135  << (it != mapLV_.end());
136 #endif
137  if (it != mapLV_.end()) {
138  unsigned int copy =
139  (i == level) ? 0
140  : (unsigned int)(touchable->GetReplicaNumber(i) + 1000 * touchable->GetReplicaNumber(i + 1));
141  storeInfo(it, plv, copy, time, energy, false);
142  }
143  }
144  }
145  } // if (aStep != NULL)
146 
147 } // end update aStep
148 
149 //================================================================ End of EVENT
150 
152 #ifdef EDM_ML_DEBUG
153  unsigned int kount(0);
154 #endif
155  for (const auto& element : store_) {
156  G4LogicalVolume* lv = (element.first).first;
157  auto it = mapLV_.find(lv);
158  if (it != mapLV_.end()) {
159  if ((it->second).first == k) {
160  PassiveHit hit(
161  (it->second).second, (element.first).second, (element.second)[1], (element.second)[2], (element.second)[0]);
162  hgcPH.push_back(hit);
163 #ifdef EDM_ML_DEBUG
164  edm::LogVerbatim("ValidHGCal") << "HGCPassive[" << k << "] Hit[" << kount << "] " << hit;
165  ++kount;
166 #endif
167  }
168  }
169  }
170 }
171 
172 G4VPhysicalVolume* HGCPassive::getTopPV() {
173  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
174 }
175 
177  auto itr = mapLV_.find(plv);
178  if (itr == mapLV_.end()) {
179  std::string name = plv->GetName();
180  for (unsigned int k = 0; k < LVNames_.size(); ++k) {
181  if (name.find(LVNames_[k]) != std::string::npos) {
182  mapLV_[plv] = std::pair<unsigned int, std::string>(k, name);
183  itr = mapLV_.find(plv);
184  break;
185  }
186  }
187  }
188  if (topLV_ == nullptr) {
189  if (std::string(plv->GetName()) == motherName_)
190  topLV_ = plv;
191  }
192  return itr;
193 }
194 
196  G4LogicalVolume* plv,
197  unsigned int copy,
198  double time,
199  double energy,
200  bool flag) {
201  std::pair<G4LogicalVolume*, unsigned int> key(plv, copy);
202  auto itr = store_.find(key);
203  double ee = (flag) ? energy : 0;
204  if (itr == store_.end()) {
205  store_[key] = {{time, energy, energy}};
206  } else {
207  (itr->second)[1] += ee;
208  (itr->second)[2] += energy;
209  }
210 #ifdef EDM_ML_DEBUG
211  itr = store_.find(key);
212  edm::LogVerbatim("ValidHGCal") << "HGCPassive: Element " << (it->second).first << ":" << (it->second).second << ":"
213  << copy << " T " << (itr->second)[0] << " E " << (itr->second)[1] << ":"
214  << (itr->second)[2];
215 #endif
216 }
217 
T getParameter(std::string const &) const
#define DEFINE_SIMWATCHER(type)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
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:195
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator volumeIterator
Definition: HGCPassive.h:59
G4LogicalVolume * topLV_
Definition: HGCPassive.h:68
#define nullptr
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:151
volumeIterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:176
G4VPhysicalVolume * topPV_
Definition: HGCPassive.h:67
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:172
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:22
std::vector< std::string > LVNames_
Definition: HGCPassive.h:66
~HGCPassive() override
Definition: HGCPassive.cc:40
std::string motherName_
Definition: HGCPassive.h:70
void produce(edm::Event &, const edm::EventSetup &) override
Definition: HGCPassive.cc:42
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:75
ii
Definition: cuy.py:590
unsigned int count_
Definition: HGCPassive.h:73
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::array< double, 3 > > store_
Definition: HGCPassive.h:75
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:50
bool init_
Definition: HGCPassive.h:74
def move(src, dest)
Definition: eostools.py:511
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.h:69