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  std::cout << "Name of the mother volume " << motherName_ << std::endl;
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  std::cout << "Collection name[" << k << "] " << name << std::endl;
37  ++k;
38 #endif
39  }
40 }
41 
43 }
44 
46 
47  for (unsigned int k=0; k<LVNames_.size(); ++k) {
48  std::unique_ptr<edm::PassiveHitContainer> hgcPH(new edm::PassiveHitContainer);
49  endOfEvent(*hgcPH, k);
50  e.put(std::move(hgcPH),Form("%sPassiveHits",LVNames_[k].c_str()));
51  }
52 }
53 
55 
56  topPV_ = getTopPV();
57  if (topPV_ == nullptr) {
58  edm::LogWarning("HGCPassive") << "Cannot find top level volume\n";
59  } else {
60  init_ = true;
61  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
62  for (auto lvcite : *lvs) {
63  findLV(lvcite);
64  }
65 
66 #ifdef EDM_ML_DEBUG
67  std::cout << "HGCPassive::Finds " << mapLV_.size() << " logical volumes\n";
68  unsigned int k(0);
69  for (const auto& lvs : mapLV_) {
70  std::cout << "Entry[" << k << "] " << lvs.first << ": ("
71  << (lvs.second).first << ", " << (lvs.second).second << ")\n";
72  ++k;
73  }
74 #endif
75  }
76 }
77 
78 //=================================================================== per EVENT
79 void HGCPassive::update(const BeginOfEvent * evt) {
80 
81  int iev = (*evt)()->GetEventID();
82  edm::LogInfo("ValidHGCal") << "HGCPassive: =====> Begin event = "
83  << iev << std::endl;
84 
85  ++count_;
86  store_.clear();
87 }
88 
89 //=================================================================== each STEP
90 void HGCPassive::update(const G4Step * aStep) {
91 
92  if (aStep != nullptr) {
93 
94  G4VSensitiveDetector* curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
95  if (curSD==nullptr) {
96 
97  G4TouchableHistory* touchable = (G4TouchableHistory*)aStep->GetPreStepPoint()->GetTouchable();
98  G4LogicalVolume* plv = (G4LogicalVolume*)touchable->GetVolume()->GetLogicalVolume();
99  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
100  if (((aStep->GetPostStepPoint() == nullptr) ||
101  (aStep->GetTrack()->GetNextVolume() == nullptr)) &&
102  (aStep->IsLastStepInVolume())) {
103 #ifdef EDM_ML_DEBUG
104  std::cout << plv->GetName() << " F|L Step "
105  << aStep->IsFirstStepInVolume() << ":"
106  << aStep->IsLastStepInVolume() << " Position"
107  << aStep->GetPreStepPoint()->GetPosition() << " Track "
108  << aStep->GetTrack()->GetDefinition()->GetParticleName()
109  << " at" << aStep->GetTrack()->GetPosition() << " Volume "
110  << aStep->GetTrack()->GetVolume() << ":"
111  << aStep->GetTrack()->GetNextVolume() << " Status "
112  << aStep->GetTrack()->GetTrackStatus() << " KE "
113  << aStep->GetTrack()->GetKineticEnergy() << " Deposit "
114  << aStep->GetTotalEnergyDeposit() << " Map "
115  << (it != mapLV_.end()) << std::endl;
116 #endif
117  double time = aStep->GetTrack()->GetGlobalTime();
118  double energy = (aStep->GetPreStepPoint()->GetKineticEnergy() +
119  aStep->GetTotalEnergyDeposit())/CLHEP::GeV;
120  if (it != mapLV_.end()) {
121  storeInfo(it, plv, 0, time, energy);
122  } else if (topLV_ != nullptr) {
123  auto itr = (init_) ? mapLV_.find(topLV_) : findLV(topLV_);
124  if (itr != mapLV_.end()) {
125  storeInfo(itr, topLV_, 0, time, energy);
126  }
127  }
128  } else if (it != mapLV_.end()) {
129  unsigned int copy = (unsigned int)(touchable->GetReplicaNumber(0) +
130  1000*touchable->GetReplicaNumber(1));
131  double time = (aStep->GetPostStepPoint()->GetGlobalTime());
132  double edeposit = (aStep->GetTotalEnergyDeposit())/CLHEP::GeV;
133  storeInfo(it, plv, copy, time, edeposit);
134  } else if (topLV_ != nullptr) {
135  auto itr = findLV(topLV_);
136  if (itr != mapLV_.end()) {
137  double time = (aStep->GetPostStepPoint()->GetGlobalTime());
138  double edeposit = (aStep->GetTotalEnergyDeposit())/CLHEP::GeV;
139  storeInfo(itr, topLV_, 100, time, edeposit);
140  }
141  }
142  }//if (curSD==NULL)
143  }//if (aStep != NULL)
144 
145 
146 }//end update aStep
147 
148 //================================================================ End of EVENT
149 
151 #ifdef EDM_ML_DEBUG
152  unsigned int kount(0);
153 #endif
154  for (const auto& element : store_) {
155  G4LogicalVolume* lv = (element.first).first;
156  auto it = mapLV_.find(lv);
157  if (it != mapLV_.end()) {
158  if ((it->second).first == k) {
159  PassiveHit hit((it->second).second,(element.first).second,
160  (element.second).second,(element.second).first);
161  hgcPH.push_back(hit);
162 #ifdef EDM_ML_DEBUG
163  std::cout << "HGCPassive[" << k << "] Hit[" << kount << "] " << hit
164  << std::endl;
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_) topLV_ = plv;
190  }
191  return itr;
192 }
193 
195  G4LogicalVolume* plv, unsigned int copy,
196  double time, double energy) {
197 
198  std::pair<G4LogicalVolume*,unsigned int> key(plv,copy);
199  auto itr = store_.find(key);
200  if (itr == store_.end()) {
201  store_[key] = std::pair<double,double>(time,energy);
202  } else {
203  (itr->second).second += energy;
204  }
205 #ifdef EDM_ML_DEBUG
206  std::cout << "HGCPassive: Element " << (it->second).first << ":"
207  << (it->second).second << ":" << copy << " T "
208  << (itr->second).first << " E " << (itr->second).second
209  << std::endl;
210 #endif
211 }
212 
214 
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:127
const double GeV
Definition: MathUtil.h:16
G4LogicalVolume * topLV_
Definition: HGCPassive.h:70
#define nullptr
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:150
volumeIterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:176
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::pair< double, double > > store_
Definition: HGCPassive.h:77
G4VPhysicalVolume * topPV_
Definition: HGCPassive.h:69
U second(std::pair< T, U > const &p)
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:172
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:22
std::vector< std::string > LVNames_
Definition: HGCPassive.h:68
~HGCPassive() override
Definition: HGCPassive.cc:42
void storeInfo(const volumeIterator itr, G4LogicalVolume *plv, unsigned int copy, double time, double energy)
Definition: HGCPassive.cc:194
std::string motherName_
Definition: HGCPassive.h:72
void produce(edm::Event &, const edm::EventSetup &) override
Definition: HGCPassive.cc:45
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:57
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.h:71
int k[5][pyjets_maxn]
unsigned int count_
Definition: HGCPassive.h:75
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:54
bool init_
Definition: HGCPassive.h:76
def move(src, dest)
Definition: eostools.py:510
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator volumeIterator
Definition: HGCPassive.h:60