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("HGCSim") << "Name of the mother volume " << motherName_;
29  unsigned k(0);
30 #endif
31  for (const auto& name : LVNames_) {
32  produces<edm::PassiveHitContainer>(Form("%sPassiveHits", name.c_str()));
33 #ifdef EDM_ML_DEBUG
34  edm::LogVerbatim("HGCSim") << "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("HGCSim") << "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("HGCSim") << "HGCPassive::Finds " << mapLV_.size() << " logical volumes";
63  unsigned int k(0);
64  for (const auto& lvs : mapLV_) {
65  edm::LogVerbatim("HGCSim") << "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("HGCSim") << "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("HGCSim") << 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 " << aStep->GetTrack()->GetVolume()
104  << ":" << aStep->GetTrack()->GetNextVolume() << " Status "
105  << 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("HGCSim") << "Level: " << level << ":" << 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("HGCSim") << "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("HGCSim") << "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 
personalPlayback.level
level
Definition: personalPlayback.py:22
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
iev
const HitContainer *__restrict__ const TkSoA *__restrict__ const Quality *__restrict__ const CAHitNtupletGeneratorKernelsGPU::HitToTuple *__restrict__ int32_t int iev
Definition: CAHitNtupletGeneratorKernelsImpl.h:862
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
HGCPassive::topPV_
G4VPhysicalVolume * topPV_
Definition: HGCPassive.h:67
HGCPassive::~HGCPassive
~HGCPassive() override
Definition: HGCPassive.cc:40
protons_cff.time
time
Definition: protons_cff.py:35
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
HGCPassive::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: HGCPassive.cc:42
HGCPassive::motherName_
std::string motherName_
Definition: HGCPassive.h:70
HGCPassive::mapLV_
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.h:69
DEFINE_SIMWATCHER
#define DEFINE_SIMWATCHER(type)
Definition: SimWatcherFactory.h:13
HGCPassive.h
PassiveHit
Definition: PassiveHit.h:9
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
dqmdumpme.k
k
Definition: dqmdumpme.py:60
first
auto first
Definition: CAHitNtupletGeneratorKernelsImpl.h:125
HGCPassive::update
void update(const BeginOfRun *run) override
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:50
edm::ParameterSet
Definition: ParameterSet.h:47
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
GeV
const double GeV
Definition: MathUtil.h:16
HGCPassive::init_
bool init_
Definition: HGCPassive.h:74
createfilelist.int
int
Definition: createfilelist.py:10
edm::PassiveHitContainer
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:100
HGCPassive::HGCPassive
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:22
BeginOfEvent
Definition: BeginOfEvent.h:6
BeginOfRun
Definition: BeginOfRun.h:6
edm::EventSetup
Definition: EventSetup.h:58
HGCPassive::endOfEvent
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:151
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
HGCPassive::LVNames_
std::vector< std::string > LVNames_
Definition: HGCPassive.h:66
eostools.move
def move(src, dest)
Definition: eostools.py:511
HGCPassive::getTopPV
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:172
writedatasetfile.run
run
Definition: writedatasetfile.py:27
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
HGCPassive::volumeIterator
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator volumeIterator
Definition: HGCPassive.h:59
HGCPassive::storeInfo
void storeInfo(const volumeIterator itr, G4LogicalVolume *plv, unsigned int copy, double time, double energy, bool flag)
Definition: HGCPassive.cc:195
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
HGCPassive::topLV_
G4LogicalVolume * topLV_
Definition: HGCPassive.h:68
edm::Event
Definition: Event.h:73
crabWrapper.key
key
Definition: crabWrapper.py:19
HGCPassive::store_
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::array< double, 3 > > store_
Definition: HGCPassive.h:75
HGCPassive
Definition: HGCPassive.h:37
hit
Definition: SiStripHitEffFromCalibTree.cc:88
RemoveAddSevLevel.flag
flag
Definition: RemoveAddSevLevel.py:117
HGCPassive::findLV
volumeIterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:176
HGCPassive::count_
unsigned int count_
Definition: HGCPassive.h:73
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37