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