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