CMS 3D CMS Logo

SimG4HGCalValidation.cc
Go to the documentation of this file.
1 // File: SimG4HGCalValidation.cc
3 // Description: Main analysis class for HGCal Validation of G4 Hits
5 
12 
13 // to retreive hits
21 
27 
31 
35 
36 #include "G4Step.hh"
37 #include "G4Track.hh"
38 #include "G4NavigationHistory.hh"
39 #include "G4TouchableHistory.hh"
40 
41 #include "CLHEP/Units/GlobalSystemOfUnits.h"
42 #include "CLHEP/Units/GlobalPhysicalConstants.h"
43 
44 #include <cmath>
45 #include <iostream>
46 #include <iomanip>
47 #include <memory>
48 #include <vector>
49 #include <string>
50 
51 class SimG4HGCalValidation : public SimProducer, public Observer<const BeginOfEvent*>, public Observer<const G4Step*> {
52 public:
54  ~SimG4HGCalValidation() override;
55 
57  void produce(edm::Event&, const edm::EventSetup&) override;
58  void beginRun(edm::EventSetup const&) override;
59 
60 private:
61  SimG4HGCalValidation(const SimG4HGCalValidation&); // stop default
63 
64  void init();
65 
66  // observer classes
67  void update(const BeginOfEvent* evt) override;
68  void update(const G4Step* step) override;
69 
70  // analysis related class
72  void clear();
73 
74 private:
75  //HGCal numbering scheme
76  std::vector<edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord> > ddconsToken_;
77  std::vector<HGCNumberingScheme*> hgcNumbering_;
78  std::vector<HGCalNumberingScheme*> hgcalNumbering_;
79 
80  // to read from parameter set
81  std::vector<std::string> names_;
82  std::vector<int> types_, detTypes_, subdet_;
84  std::vector<std::string> nameXs_;
85 
86  // parameters from geometry
88 
89  // some private members for ananlysis
90  unsigned int count_;
93  std::vector<double> hgcEEedep_, hgcHEFedep_, hgcHEBedep_;
94  std::vector<unsigned int> dets_, hgchitDets_, hgchitIndex_;
95  std::vector<double> hgchitX_, hgchitY_, hgchitZ_;
96 };
97 
98 SimG4HGCalValidation::SimG4HGCalValidation(const edm::ParameterSet& p) : levelT1_(999), levelT2_(999), count_(0) {
99  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("SimG4HGCalValidation");
100  names_ = m_Anal.getParameter<std::vector<std::string> >("Names");
101  types_ = m_Anal.getParameter<std::vector<int> >("Types");
102  detTypes_ = m_Anal.getParameter<std::vector<int> >("DetTypes");
103  labelLayer_ = m_Anal.getParameter<std::string>("LabelLayerInfo");
104  verbosity_ = m_Anal.getUntrackedParameter<int>("Verbosity", 0);
105 
106  produces<PHGCalValidInfo>(labelLayer_);
107 
108  if (verbosity_ > 0) {
109  edm::LogVerbatim("ValidHGCal") << "HGCalTestAnalysis:: Initialised as "
110  << "observer of begin events and of G4step "
111  << "with Parameter values: \n\tLabel : " << labelLayer_ << " and with "
112  << names_.size() << " detectors";
113  for (unsigned int k = 0; k < names_.size(); ++k)
114  edm::LogVerbatim("ValidHGCal") << " [" << k << "] " << names_[k] << " Type " << types_[k] << " DetType "
115  << detTypes_[k];
116  }
117 }
118 
120  for (auto number : hgcNumbering_)
121  delete number;
122  for (auto number : hgcalNumbering_)
123  delete number;
124 }
125 
127  for (unsigned int type = 0; type < types_.size(); ++type) {
128  int detType = detTypes_[type];
129  G4String nameX = "HGCal";
130  if (types_[type] <= 1) {
131  if (types_[type] == 0) {
133  if (detType == 0)
134  dets_.emplace_back((int)(DetId::HGCalEE));
135  else if (detType == 1)
136  dets_.emplace_back((int)(DetId::HGCalHSi));
137  else
138  dets_.emplace_back((int)(DetId::HGCalHSc));
139  } else {
140  dets_.emplace_back((unsigned int)(DetId::Forward));
141  if (detType == 0)
142  subdet_.emplace_back((int)(ForwardSubdetector::HGCEE));
143  else if (detType == 1)
144  subdet_.emplace_back((int)(ForwardSubdetector::HGCHEF));
145  else
146  subdet_.emplace_back((int)(ForwardSubdetector::HGCHEB));
147  }
148  if (detType == 0)
149  nameX = "HGCalEESensitive";
150  else if (detType == 1)
151  nameX = "HGCalHESiliconSensitive";
152  else
153  nameX = "HGCalHEScintillatorSensitive";
154  nameXs_.emplace_back(nameX);
155  ddconsToken_.emplace_back(
157  edm::LogVerbatim("ValidHGCal") << "Defines ESGetToken for type " << type << ":" << dets_.back() << ":"
158  << subdet_.back() << ":" << nameX;
159  } else {
160  edm::LogError("ValidHGCal") << "Wrong Type " << types_[type];
161  throw cms::Exception("Unknown", "ValidHGCal") << "Wrong Type " << types_[type] << "\n";
162  }
163  }
164 }
165 
167  std::unique_ptr<PHGCalValidInfo> productLayer(new PHGCalValidInfo);
168  layerAnalysis(*productLayer);
169  e.put(std::move(productLayer), labelLayer_);
170 }
171 
173  for (unsigned int type = 0; type < types_.size(); ++type) {
174  int layers(0);
175  int detType = detTypes_[type];
177  if (hdc.isValid()) {
178  levelT1_ = hdc->levelTop(0);
179  levelT2_ = hdc->levelTop(1);
180  if (hdc->tileTrapezoid()) {
181  types_[type] = -1;
182  hgcalNumbering_.emplace_back(new HGCalNumberingScheme(*hdc, (DetId::Detector)(dets_[type]), nameXs_[type], ""));
183  } else if (hdc->waferHexagon6()) {
184  types_[type] = 1;
185  hgcNumbering_.emplace_back(new HGCNumberingScheme(*hdc, nameXs_[type]));
186  } else {
187  types_[type] = 0;
188  hgcalNumbering_.emplace_back(new HGCalNumberingScheme(*hdc, (DetId::Detector)(dets_[type]), nameXs_[type], ""));
189  }
190  layers = hdc->layers(false);
191  } else {
192  edm::LogError("ValidHGCal") << "Cannot find HGCalDDDConstants for " << nameXs_[type];
193  throw cms::Exception("Unknown", "ValidHGCal") << "Cannot find HGCalDDDConstants for " << nameXs_[type] << "\n";
194  }
195  if (detType == 0) {
196  for (int i = 0; i < layers; ++i)
197  hgcEEedep_.emplace_back(0);
198  } else if (detType == 1) {
199  for (int i = 0; i < layers; ++i)
200  hgcHEFedep_.emplace_back(0);
201  } else {
202  for (int i = 0; i < layers; ++i)
203  hgcHEBedep_.emplace_back(0);
204  }
205  if (verbosity_ > 0)
206  edm::LogVerbatim("ValidHGCal") << "[" << type << "]: " << nameXs_[type] << " det " << dets_[type] << " subdet "
207  << subdet_[type] << " with " << layers << " layers";
208  }
209 }
210 
211 //=================================================================== per EVENT
213  int iev = (*evt)()->GetEventID();
214  if (verbosity_ > 0)
215  edm::LogVerbatim("ValidHGCal") << "SimG4HGCalValidation: =====> Begin "
216  << "event = " << iev;
217 
218  ++count_;
219  edepEE_ = edepHEF_ = edepHEB_ = 0.;
220 
221  //HGCal variables
222  for (unsigned int i = 0; i < hgcEEedep_.size(); i++)
223  hgcEEedep_[i] = 0.;
224  for (unsigned int i = 0; i < hgcHEFedep_.size(); i++)
225  hgcHEFedep_[i] = 0.;
226  for (unsigned int i = 0; i < hgcHEBedep_.size(); i++)
227  hgcHEBedep_[i] = 0.;
228 
229  //Cache reset
230  clear();
231 }
232 
233 //=================================================================== each STEP
234 void SimG4HGCalValidation::update(const G4Step* aStep) {
235  if (aStep != nullptr) {
236  G4VPhysicalVolume* curPV = aStep->GetPreStepPoint()->GetPhysicalVolume();
237  const G4String& name = curPV->GetName();
238  G4VSensitiveDetector* curSD = aStep->GetPreStepPoint()->GetSensitiveDetector();
239  if (verbosity_ > 1)
240  edm::LogVerbatim("ValidHGCal") << "ValidHGCal::Step in " << name << " at "
241  << aStep->GetPreStepPoint()->GetPosition();
242 
243  // Only for Sensitive detector
244  if (curSD != nullptr) {
245  int type(-1);
246  for (unsigned int k = 0; k < names_.size(); ++k) {
247  if (name.find(names_[k].c_str()) != std::string::npos) {
248  type = k;
249  break;
250  }
251  }
252  int detType = (type >= 0) ? detTypes_[type] : -1;
253  if (verbosity_ > 0)
254  edm::LogVerbatim("ValidHGCal") << "ValidHGCal::In SD " << name << " type " << type << ":" << detType;
255 
256  // Right type of SD
257  if (type >= 0) {
258  //Get the 32-bit index of the hit cell
259  const G4TouchableHistory* touchable =
260  static_cast<const G4TouchableHistory*>(aStep->GetPreStepPoint()->GetTouchable());
261  unsigned int index(0);
262  int layer(0);
263  G4ThreeVector hitPoint = aStep->GetPreStepPoint()->GetPosition();
264  // HGCal
265  G4ThreeVector localpos = touchable->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
266  float globalZ = touchable->GetTranslation(0).z();
267  int iz(globalZ > 0 ? 1 : -1);
268  int module(-1), cell(-1);
269  if (types_[type] == 1) {
270  if (touchable->GetHistoryDepth() == levelT1_) {
271  layer = touchable->GetReplicaNumber(0);
272  } else {
273  layer = touchable->GetReplicaNumber(2);
274  module = touchable->GetReplicaNumber(1);
275  cell = touchable->GetReplicaNumber(0);
276  }
277  index =
278  hgcNumbering_[type]->getUnitID((ForwardSubdetector)(subdet_[type]), layer, module, cell, iz, localpos);
279  } else {
280  if ((touchable->GetHistoryDepth() == levelT1_) || (touchable->GetHistoryDepth() == levelT2_)) {
281  layer = touchable->GetReplicaNumber(0);
282  } else {
283  layer = touchable->GetReplicaNumber(3);
284  module = touchable->GetReplicaNumber(2);
285  cell = touchable->GetReplicaNumber(1);
286  }
287  double weight(0);
288  index = hgcalNumbering_[type]->getUnitID(layer, module, cell, iz, hitPoint, weight);
289  }
290  if (verbosity_ > 1)
291  edm::LogVerbatim("ValidHGCal") << "HGCal: " << name << " Layer " << layer << " Module " << module << " Cell "
292  << cell;
293 
294  double edeposit = aStep->GetTotalEnergyDeposit();
295  if (verbosity_ > 0)
296  edm::LogVerbatim("ValidHGCal") << "Layer " << layer << " Index " << std::hex << index << std::dec << " Edep "
297  << edeposit << " hit at " << hitPoint;
298  if (detType == 0) {
299  edepEE_ += edeposit;
300  if (layer < (int)(hgcEEedep_.size()))
301  hgcEEedep_[layer] += edeposit;
302  } else if (detType == 1) {
303  edepHEF_ += edeposit;
304  if (layer < (int)(hgcHEFedep_.size()))
305  hgcHEFedep_[layer] += edeposit;
306  } else {
307  edepHEB_ += edeposit;
308  if (layer < (int)(hgcHEBedep_.size()))
309  hgcHEBedep_[layer] += edeposit;
310  }
311  G4String nextVolume("XXX");
312  if (aStep->GetTrack()->GetNextVolume() != nullptr)
313  nextVolume = aStep->GetTrack()->GetNextVolume()->GetName();
314 
315  if (nextVolume.c_str() != name.c_str()) { //save hit when it exits cell
316  if (std::find(hgchitIndex_.begin(), hgchitIndex_.end(), index) == hgchitIndex_.end()) {
317  hgchitDets_.emplace_back(dets_[type]);
318  hgchitIndex_.emplace_back(index);
319  hgchitX_.emplace_back(hitPoint.x());
320  hgchitY_.emplace_back(hitPoint.y());
321  hgchitZ_.emplace_back(hitPoint.z());
322  }
323  }
324  } // it is right type of SD
325  } // it is in a SD
326  } //if aStep!=NULL
327 } //end update aStep
328 
329 //================================================================ End of EVENT
330 
332  if (verbosity_ > 0)
333  edm::LogVerbatim("ValidHGCal") << "\n ===>>> SimG4HGCalValidation: Energy "
334  << "deposit\n at EE : " << std::setw(6) << edepEE_ / CLHEP::MeV
335  << "\n at HEF: " << std::setw(6) << edepHEF_ / CLHEP::MeV
336  << "\n at HEB: " << std::setw(6) << edepHEB_ / CLHEP::MeV;
337 
338  //Fill HGC Variables
341 }
342 
343 //---------------------------------------------------
345  hgchitDets_.erase(hgchitDets_.begin(), hgchitDets_.end());
346  hgchitIndex_.erase(hgchitIndex_.begin(), hgchitIndex_.end());
347  hgchitX_.erase(hgchitX_.begin(), hgchitX_.end());
348  hgchitY_.erase(hgchitY_.begin(), hgchitY_.end());
349  hgchitZ_.erase(hgchitZ_.begin(), hgchitZ_.end());
350 }
351 
std::vector< unsigned int > hgchitIndex_
Log< level::Info, true > LogVerbatim
std::vector< double > hgchitY_
std::vector< int > subdet_
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< HGCalNumberingScheme * > hgcalNumbering_
void produce(edm::Event &, const edm::EventSetup &) override
SimG4HGCalValidation(const edm::ParameterSet &p)
std::vector< std::string > nameXs_
std::vector< double > hgchitZ_
Definition: weight.py:1
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
ForwardSubdetector
bool waferHexagon6() const
std::vector< int > detTypes_
constexpr std::array< uint8_t, layerIndexSize > layer
void beginRun(edm::EventSetup const &) override
T getUntrackedParameter(std::string const &, T const &) const
void update(const BeginOfEvent *evt) override
This routine will be called when the appropriate signal arrives.
void fillhgcLayers(const double edepEE, const double edepHEF, const double edepHEB, const std::vector< double > &eedep, const std::vector< double > &hefdep, const std::vector< double > &hebdep)
void registerConsumes(edm::ConsumesCollector) override
const SimG4HGCalValidation & operator=(const SimG4HGCalValidation &)
std::vector< unsigned int > dets_
void layerAnalysis(PHGCalValidInfo &)
unsigned int layers(bool reco) const
bool tileTrapezoid() const
std::vector< unsigned int > hgchitDets_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
std::vector< double > hgcHEFedep_
bool isValid() const
Definition: ESHandle.h:44
std::vector< HGCNumberingScheme * > hgcNumbering_
std::vector< std::string > names_
std::vector< edm::ESGetToken< HGCalDDDConstants, IdealGeometryRecord > > ddconsToken_
std::vector< double > hgcHEBedep_
Detector
Definition: DetId.h:24
HitContainer const *__restrict__ TkSoA const *__restrict__ Quality const *__restrict__ CAHitNtupletGeneratorKernelsGPU::HitToTuple const *__restrict__ int32_t int32_t int iev
std::vector< int > types_
std::vector< double > hgcEEedep_
void fillhgcHits(const std::vector< unsigned int > &hitdets, const std::vector< unsigned int > &hitindex, const std::vector< double > &hitvtxX, const std::vector< double > &hitvtxY, const std::vector< double > &hitvtxZ)
step
Definition: StallMonitor.cc:98
int levelTop(int ind=0) const
std::vector< double > hgchitX_
def move(src, dest)
Definition: eostools.py:511