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 "G4Track.hh"
11 #include "G4TouchableHistory.hh"
12 #include "G4TransportationManager.hh"
13 #include "CLHEP/Units/GlobalSystemOfUnits.h"
14 #include "CLHEP/Units/GlobalPhysicalConstants.h"
15 
16 #include <cmath>
17 #include <iostream>
18 #include <iomanip>
19 #include <memory>
20 #include <utility>
21 
22 //#define EDM_ML_DEBUG
23 
24 HGCPassive::HGCPassive(const edm::ParameterSet &p) : count_(0), init_(false) {
25 
26  edm::ParameterSet m_Passive = p.getParameter<edm::ParameterSet>("HGCPassive");
27  LVNames_ = m_Passive.getUntrackedParameter<std::vector<std::string> >("LVNames");
28 
29 #ifdef EDM_ML_DEBUG
30  unsigned int k(0);
31 #endif
32  for (const auto& name : LVNames_) {
33  produces<edm::PassiveHitContainer>(Form("%sPassiveHits",name.c_str()));
34 #ifdef EDM_ML_DEBUG
35  std::cout << "Collection name[" << k << "] " << name << std::endl;
36  ++k;
37 #endif
38  }
39 }
40 
42 }
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_ == 0) {
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  std::cout << "HGCPassive::Finds " << mapLV_.size() << " logical volumes\n";
67  unsigned int k(0);
68  for (const auto& lvs : mapLV_) {
69  std::cout << "Entry[" << k << "] " << lvs.first << ": ("
70  << (lvs.second).first << ", " << (lvs.second).second << ")\n";
71  ++k;
72  }
73 #endif
74  }
75 }
76 
77 //=================================================================== per EVENT
78 void HGCPassive::update(const BeginOfEvent * evt) {
79 
80  int iev = (*evt)()->GetEventID();
81  edm::LogInfo("ValidHGCal") << "HGCPassive: =====> Begin event = "
82  << iev << std::endl;
83 
84  ++count_;
85  store_.clear();
86 }
87 
88 //=================================================================== each STEP
89 void HGCPassive::update(const G4Step * aStep) {
90 
91  if (aStep != NULL) {
92 
93  G4VSensitiveDetector* curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
94  if (curSD==NULL) {
95 
96  G4TouchableHistory* touchable = (G4TouchableHistory*)aStep->GetPreStepPoint()->GetTouchable();
97  G4LogicalVolume* plv = (G4LogicalVolume*)touchable->GetVolume()->GetLogicalVolume();
98  auto it = (init_) ? mapLV_.find(plv) : findLV(plv);
99  if (it != mapLV_.end()) {
100  unsigned int copy = (unsigned int)(touchable->GetReplicaNumber(0) +
101  1000*touchable->GetReplicaNumber(1));
102  std::pair<G4LogicalVolume*,unsigned int> key(plv,copy);
103  auto itr = store_.find(key);
104  double time = (aStep->GetPostStepPoint()->GetGlobalTime());
105  if (itr == store_.end()) {
106  store_[key] = std::pair<double,double>(time,0.0);
107  itr = store_.find(key);
108  }
109  double edeposit = aStep->GetTotalEnergyDeposit();
110  (itr->second).second += edeposit;
111 #ifdef EDM_ML_DEBUG
112  std::cout << "HGCPassive: Element " << (it->second).first << ":"
113  << (it->second).second << ":" << copy << " T "
114  << (itr->second).first << " E " << (itr->second).second
115  << std::endl;
116 #endif
117  }//if( it != map.end() )
118  }//if (curSD==NULL)
119  }//if (aStep != NULL)
120 
121 
122 }//end update aStep
123 
124 
125 //================================================================ End of EVENT
126 
128 #ifdef EDM_ML_DEBUG
129  unsigned int kount(0);
130 #endif
131  for (const auto& element : store_) {
132  G4LogicalVolume* lv = (element.first).first;
133  auto it = mapLV_.find(lv);
134  if (it != mapLV_.end()) {
135  if ((it->second).first == k) {
136  PassiveHit hit((it->second).second,(element.first).second,
137  (element.second).second,(element.second).first);
138  hgcPH.push_back(hit);
139 #ifdef EDM_ML_DEBUG
140  std::cout << "HGCPassive[" << k << "] Hit[" << kount << "] " << hit
141  << std::endl;
142  ++kount;
143 #endif
144  }
145  }
146  }
147 }
148 
149 G4VPhysicalVolume * HGCPassive::getTopPV() {
150  return G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
151 }
152 
153 std::map<G4LogicalVolume*,std::pair<unsigned int,std::string>>::iterator HGCPassive::findLV(G4LogicalVolume * plv) {
154  auto itr = mapLV_.find(plv);
155  if (itr == mapLV_.end()) {
156  std::string name = plv->GetName();
157  for (unsigned int k=0; k<LVNames_.size(); ++k) {
158  if (name.find(LVNames_[k]) != std::string::npos) {
159  mapLV_[plv] = std::pair<unsigned int,std::string>(k,name);
160  itr = mapLV_.find(plv);
161  break;
162  }
163  }
164  }
165  return itr;
166 }
167 
169 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
#define DEFINE_SIMWATCHER(type)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
void update(const BeginOfRun *run)
This routine will be called when the appropriate signal arrives.
Definition: HGCPassive.cc:53
#define NULL
Definition: scimark2.h:8
void endOfEvent(edm::PassiveHitContainer &hgcPH, unsigned int k)
Definition: HGCPassive.cc:127
std::map< std::pair< G4LogicalVolume *, unsigned int >, std::pair< double, double > > store_
Definition: HGCPassive.h:69
G4VPhysicalVolume * topPV_
Definition: HGCPassive.h:63
U second(std::pair< T, U > const &p)
virtual ~HGCPassive()
Definition: HGCPassive.cc:41
G4VPhysicalVolume * getTopPV()
Definition: HGCPassive.cc:149
HGCPassive(const edm::ParameterSet &p)
Definition: HGCPassive.cc:24
std::vector< std::string > LVNames_
Definition: HGCPassive.h:62
std::vector< PassiveHit > PassiveHitContainer
Definition: PassiveHit.h:57
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > > mapLV_
Definition: HGCPassive.h:64
int k[5][pyjets_maxn]
unsigned int count_
Definition: HGCPassive.h:67
void produce(edm::Event &, const edm::EventSetup &)
Definition: HGCPassive.cc:44
bool init_
Definition: HGCPassive.h:68
def move(src, dest)
Definition: eostools.py:510
std::map< G4LogicalVolume *, std::pair< unsigned int, std::string > >::iterator findLV(G4LogicalVolume *plv)
Definition: HGCPassive.cc:153