CMS 3D CMS Logo

HGCDigitizer.cc
Go to the documentation of this file.
20 
21 #include <algorithm>
22 #include <boost/foreach.hpp>
23 
24 using namespace hgc_digi;
25 
26 namespace {
27 
28  constexpr std::array<double,3> occupancyGuesses = { { 0.5,0.2,0.2 } };
29 
30  float getPositionDistance(const HGCalGeometry* geom, const DetId& id) {
31  return geom->getPosition(id).mag();
32  }
33 
34  float getPositionDistance(const HcalGeometry* geom, const DetId& id) {
35  return geom->getGeometry(id)->getPosition().mag();
36  }
37 
38  void getValidDetIds(const HGCalGeometry* geom, std::unordered_set<DetId>& valid) {
39  const std::vector<DetId>& ids = geom->getValidDetIds();
40  valid.reserve(ids.size());
41  valid.insert(ids.begin(),ids.end());
42  }
43 
44  void getValidDetIds(const HcalGeometry* geom, std::unordered_set<DetId>& valid) {
45  const std::vector<DetId>& ids = geom->getValidDetIds();
46  for( const auto& id : ids ) {
47  if( HcalEndcap == id.subdetId() &&
48  DetId::Hcal == id.det() )
49  valid.emplace(id);
50  }
51  valid.reserve(valid.size());
52  }
53 
54  DetId simToReco(const HcalGeometry* geom, unsigned simid) {
55  DetId result(0);
56  const auto& topo = geom->topology();
57  const auto* dddConst = topo.dddConstants();
58  HcalDetId id = HcalHitRelabeller::relabel(simid,dddConst);
59 
60  if (id.subdet()==int(HcalEndcap)) {
61  result = id;
62  }
63 
64  return result;
65  }
66 
67  DetId simToReco(const HGCalGeometry* geom, unsigned simId) {
68  DetId result(0);
69  const auto& topo = geom->topology();
70  const auto& dddConst = topo.dddConstants();
71 
72  int subdet, layer, cell, sec, subsec, zp;
73 
74  const bool isSqr = (dddConst.geomMode() == HGCalGeometryMode::Square);
75  if (isSqr) {
76  HGCalTestNumbering::unpackSquareIndex(simId, zp, layer, sec, subsec, cell);
77  } else {
78  HGCalTestNumbering::unpackHexagonIndex(simId, subdet, zp, layer, sec, subsec, cell);
79  //sec is wafer and subsec is celltyp
80  }
81  //skip this hit if after ganging it is not valid
82  std::pair<int,int> recoLayerCell=dddConst.simToReco(cell,layer,sec,topo.detectorType());
83  cell = recoLayerCell.first;
84  layer = recoLayerCell.second;
85  if (layer<0 || cell<0) {
86  return result;
87  }
88 
89  //assign the RECO DetId
90  result = HGCalDetId((ForwardSubdetector)subdet,zp,layer,subsec,sec,cell);
91 
92  return result;
93  }
94 }
95 
96 //
99  simHitAccumulator_( new HGCSimHitDataAccumulator() ),
100  mySubDet_(ForwardSubdetector::ForwardEmpty),
101  refSpeed_(0.1*CLHEP::c_light), //[CLHEP::c_light]=mm/ns convert to cm/ns
102  averageOccupancies_(occupancyGuesses),
103  nEvents_(1)
104 {
105  //configure from cfg
106  hitCollection_ = ps.getParameter< std::string >("hitCollection");
107  digiCollection_ = ps.getParameter< std::string >("digiCollection");
108  maxSimHitsAccTime_ = ps.getParameter< uint32_t >("maxSimHitsAccTime");
109  bxTime_ = ps.getParameter< double >("bxTime");
110  digitizationType_ = ps.getParameter< uint32_t >("digitizationType");
111  verbosity_ = ps.getUntrackedParameter< uint32_t >("verbosity",0);
112  tofDelay_ = ps.getParameter< double >("tofDelay");
113 
114  std::unordered_set<DetId>().swap(validIds_);
115 
116  iC.consumes<std::vector<PCaloHit> >(edm::InputTag("g4SimHits",hitCollection_));
117 
118  if(hitCollection_.find("HitsEE")!=std::string::npos) {
120  theHGCEEDigitizer_=std::unique_ptr<HGCEEDigitizer>(new HGCEEDigitizer(ps) );
121  }
122  if(hitCollection_.find("HitsHEfront")!=std::string::npos)
123  {
125  theHGCHEfrontDigitizer_=std::unique_ptr<HGCHEfrontDigitizer>(new HGCHEfrontDigitizer(ps) );
126  }
127  if(hitCollection_.find("HcalHits")!=std::string::npos)
128  {
130  theHGCHEbackDigitizer_=std::unique_ptr<HGCHEbackDigitizer>(new HGCHEbackDigitizer(ps) );
131  }
132 }
133 
134 //
136 {
137  // reserve memory for a full detector
139  switch(mySubDet_) {
141  idx = 0;
142  break;
144  idx = 1;
145  break;
147  idx = 2;
148  break;
149  default:
150  break;
151  }
152  simHitAccumulator_->reserve( averageOccupancies_[idx]*validIds_.size() );
153 }
154 
155 //
156 void HGCDigitizer::finalizeEvent(edm::Event& e, edm::EventSetup const& es, CLHEP::HepRandomEngine* hre)
157 {
158 
159  const CaloSubdetectorGeometry* theGeom = ( nullptr == gHGCal_ ?
160  static_cast<const CaloSubdetectorGeometry*>(gHcal_) :
161  static_cast<const CaloSubdetectorGeometry*>(gHGCal_) );
162 
163  ++nEvents_;
165  switch(mySubDet_) {
167  idx = 0;
168  break;
170  idx = 1;
171  break;
173  idx = 2;
174  break;
175  default:
176  break;
177  }
178  // release memory for unfilled parts of hash table
179  if( validIds_.size()*averageOccupancies_[idx] > simHitAccumulator_->size() ) {
180  simHitAccumulator_->reserve(simHitAccumulator_->size());
181  }
182  //update occupancy guess
183  const double thisOcc = simHitAccumulator_->size()/((double)validIds_.size());
185 
186  if( producesEEDigis() )
187  {
188  std::unique_ptr<HGCEEDigiCollection> digiResult(new HGCEEDigiCollection() );
189  theHGCEEDigitizer_->run(digiResult,*simHitAccumulator_,theGeom,validIds_,digitizationType_, hre);
190  edm::LogInfo("HGCDigitizer") << " @ finalize event - produced " << digiResult->size() << " EE hits";
191  e.put(std::move(digiResult),digiCollection());
192  }
193  if( producesHEfrontDigis())
194  {
195  std::unique_ptr<HGCHEDigiCollection> digiResult(new HGCHEDigiCollection() );
197  edm::LogInfo("HGCDigitizer") << " @ finalize event - produced " << digiResult->size() << " HE front hits";
198  e.put(std::move(digiResult),digiCollection());
199  }
200  if( producesHEbackDigis() )
201  {
202  std::unique_ptr<HGCBHDigiCollection> digiResult(new HGCBHDigiCollection() );
204  edm::LogInfo("HGCDigitizer") << " @ finalize event - produced " << digiResult->size() << " HE back hits";
205  e.put(std::move(digiResult),digiCollection());
206  }
207 
209 }
210 
211 //
212 void HGCDigitizer::accumulate(edm::Event const& e, edm::EventSetup const& eventSetup, CLHEP::HepRandomEngine* hre) {
213 
214  //get inputs
216  e.getByLabel(edm::InputTag("g4SimHits",hitCollection_),hits);
217  if( !hits.isValid() ){
218  edm::LogError("HGCDigitizer") << " @ accumulate : can't find " << hitCollection_ << " collection of g4SimHits";
219  return;
220  }
221 
222  //accumulate in-time the main event
223  if( nullptr != gHGCal_ ) {
224  accumulate(hits, 0, gHGCal_, hre);
225  } else if( nullptr != gHcal_ ) {
226  accumulate(hits, 0, gHcal_, hre);
227  } else {
228  throw cms::Exception("BadConfiguration")
229  << "HGCDigitizer is not producing EE, FH, or BH digis!";
230  }
231 }
232 
233 //
234 void HGCDigitizer::accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& eventSetup, CLHEP::HepRandomEngine* hre) {
235 
236  //get inputs
238  e.getByLabel(edm::InputTag("g4SimHits",hitCollection_),hits);
239  if( !hits.isValid() ){
240  edm::LogError("HGCDigitizer") << " @ accumulate : can't find " << hitCollection_ << " collection of g4SimHits";
241  return;
242  }
243 
244  //accumulate for the simulated bunch crossing
245  if( nullptr != gHGCal_ ) {
246  accumulate(hits, e.bunchCrossing(), gHGCal_, hre);
247  } else if ( nullptr != gHcal_ ) {
248  accumulate(hits, e.bunchCrossing(), gHcal_, hre);
249  } else {
250  throw cms::Exception("BadConfiguration")
251  << "HGCDigitizer is not producing EE, FH, or BH digis!";
252  }
253 }
254 
255 //
256 template<typename GEOM>
258  int bxCrossing,
259  const GEOM* geom,
260  CLHEP::HepRandomEngine* hre) {
261  if( nullptr == geom ) return;
262 
263 
264 
265  //configuration to apply for the computation of time-of-flight
266  bool weightToAbyEnergy(false);
267  float tdcOnset(0.f),keV2fC(0.f);
268  switch( mySubDet_ ) {
270  weightToAbyEnergy = theHGCEEDigitizer_->toaModeByEnergy();
271  tdcOnset = theHGCEEDigitizer_->tdcOnset();
272  keV2fC = theHGCEEDigitizer_->keV2fC();
273  break;
275  weightToAbyEnergy = theHGCHEfrontDigitizer_->toaModeByEnergy();
276  tdcOnset = theHGCHEfrontDigitizer_->tdcOnset();
277  keV2fC = theHGCHEfrontDigitizer_->keV2fC();
278  break;
280  weightToAbyEnergy = theHGCHEbackDigitizer_->toaModeByEnergy();
281  tdcOnset = theHGCHEbackDigitizer_->tdcOnset();
282  keV2fC = theHGCHEbackDigitizer_->keV2fC();
283  break;
284  default:
285  break;
286  }
287 
288  //create list of tuples (pos in container, RECO DetId, time) to be sorted first
289  int nchits=(int)hits->size();
290  std::vector< HGCCaloHitTuple_t > hitRefs;
291  hitRefs.reserve(nchits);
292  for(int i=0; i<nchits; ++i) {
293  const auto& the_hit = hits->at(i);
294 
295  DetId id = simToReco(geom,the_hit.id());
296 
297  if (verbosity_>0) {
298  if (producesEEDigis())
299  edm::LogInfo("HGCDigitizer") << " i/p " << std::hex << the_hit.id() << std::dec << " o/p " << id.rawId() << std::endl;
300  else
301  edm::LogInfo("HGCDigitizer") << " i/p " << std::hex << the_hit.id() << std::dec << " o/p " << id.rawId() << std::endl;
302  }
303 
304  if( 0 != id.rawId() ) {
305  hitRefs.emplace_back( i, id.rawId(), (float)the_hit.time() );
306  }
307  }
308  std::sort(hitRefs.begin(),hitRefs.end(),this->orderByDetIdThenTime);
309 
310  //loop over sorted hits
311  nchits = hitRefs.size();
312  for(int i=0; i<nchits; ++i) {
313  const int hitidx = std::get<0>(hitRefs[i]);
314  const uint32_t id = std::get<1>(hitRefs[i]);
315 
316  //get the data for this cell, if not available then we skip it
317 
318  if( !validIds_.count(id) ) continue;
319  HGCSimHitDataAccumulator::iterator simHitIt = simHitAccumulator_->emplace(id,HGCCellInfo()).first;
320 
321  if(id==0) continue; // to be ignored at RECO level
322 
323  const float toa = std::get<2>(hitRefs[i]);
324  const PCaloHit &hit=hits->at( hitidx );
325  const float charge = hit.energy()*1e6*keV2fC;
326 
327  //distance to the center of the detector
328  const float dist2center( getPositionDistance(geom,id) );
329 
330  //hit time: [time()]=ns [centerDist]=cm [refSpeed_]=cm/ns + delay by 1ns
331  //accumulate in 15 buckets of 25ns (9 pre-samples, 1 in-time, 5 post-samples)
332  const float tof = toa-dist2center/refSpeed_+tofDelay_ ;
333  const int itime= std::floor( tof/bxTime_ ) + 9;
334 
335  //no need to add bx crossing - tof comes already corrected from the mixing module
336  //itime += bxCrossing;
337  //itime += 9;
338 
339  if(itime<0 || itime>14) continue;
340 
341  //check if time index is ok and store energy
342  if(itime >= (int)simHitIt->second.hit_info[0].size() ) continue;
343 
344  (simHitIt->second).hit_info[0][itime] += charge;
345  float accCharge=(simHitIt->second).hit_info[0][itime];
346 
347  //time-of-arrival (check how to be used)
348  if(weightToAbyEnergy) (simHitIt->second).hit_info[1][itime] += charge*tof;
349  else if((simHitIt->second).hit_info[1][itime]==0) {
350  if( accCharge>tdcOnset)
351  {
352  //extrapolate linear using previous simhit if it concerns to the same DetId
353  float fireTDC=tof;
354  if(i>0)
355  {
356  uint32_t prev_id = std::get<1>(hitRefs[i-1]);
357  if(prev_id==id)
358  {
359  float prev_toa = std::get<2>(hitRefs[i-1]);
360  float prev_tof(prev_toa-dist2center/refSpeed_+tofDelay_);
361  //float prev_charge = std::get<3>(hitRefs[i-1]);
362  float deltaQ2TDCOnset = tdcOnset-((simHitIt->second).hit_info[0][itime]-charge);
363  float deltaQ = charge;
364  float deltaT = (tof-prev_tof);
365  fireTDC = deltaT*(deltaQ2TDCOnset/deltaQ)+prev_tof;
366  }
367  }
368 
369  (simHitIt->second).hit_info[1][itime]=fireTDC;
370  }
371  }
372  }
373  hitRefs.clear();
374 }
375 
376 //
378 {
379  //get geometry
381  es.get<CaloGeometryRecord>().get(geom);
382 
383  gHGCal_ = nullptr;
384  gHcal_ = nullptr;
385 
386  if( producesEEDigis() ) gHGCal_ = dynamic_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCEE));
387  if( producesHEfrontDigis() ) gHGCal_ = dynamic_cast<const HGCalGeometry*>(geom->getSubdetectorGeometry(DetId::Forward, HGCHEF));
388  if( producesHEbackDigis() ) gHcal_ = dynamic_cast<const HcalGeometry*>(geom->getSubdetectorGeometry(DetId::Hcal, HcalEndcap));
389 
390  int nadded(0);
391  //valid ID lists
392  if( nullptr != gHGCal_ ) {
393  getValidDetIds( gHGCal_, validIds_ );
394  } else if( nullptr != gHcal_ ) {
395  getValidDetIds( gHcal_, validIds_ );
396  } else {
397  throw cms::Exception("BadConfiguration")
398  << "HGCDigitizer is not producing EE, FH, or BH digis!";
399  }
400 
401  if (verbosity_ > 0)
402  edm::LogInfo("HGCDigitizer")
403  << "Added " << nadded << ":" << validIds_.size()
404  << " detIds without " << hitCollection_
405  << " in first event processed" << std::endl;
406 }
407 
408 //
410 {
411  std::unordered_set<DetId>().swap(validIds_);
412 }
413 
414 //
416 {
417  for( HGCSimHitDataAccumulator::iterator it = simHitAccumulator_->begin(); it!=simHitAccumulator_->end(); it++)
418  {
419  it->second.hit_info[0].fill(0.);
420  it->second.hit_info[1].fill(0.);
421  }
422 }
423 
424 template void HGCDigitizer::accumulate<HcalGeometry>(edm::Handle<edm::PCaloHitContainer> const &hits, int bxCrossing,const HcalGeometry *geom, CLHEP::HepRandomEngine* hre);
425 template void HGCDigitizer::accumulate<HGCalGeometry>(edm::Handle<edm::PCaloHitContainer> const &hits, int bxCrossing,const HGCalGeometry *geom, CLHEP::HepRandomEngine* hre);
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
T getParameter(std::string const &) const
uint32_t nEvents_
Definition: HGCDigitizer.h:116
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:45
T getUntrackedParameter(std::string const &, T const &) const
const HcalDDDRecConstants * dddConstants() const
Definition: HcalTopology.h:161
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::SortedCollection< HGCEEDataFrame > HGCEEDigiCollection
void finalizeEvent(edm::Event &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre)
ForwardSubdetector mySubDet_
Definition: HGCDigitizer.h:103
int digitizationType_
Definition: HGCDigitizer.h:84
std::string hitCollection_
Definition: HGCDigitizer.h:81
void resetSimHitDataAccumulator()
void initializeEvent(edm::Event const &e, edm::EventSetup const &c)
actions at the start/end of event
GlobalPoint getPosition(const DetId &id) const
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
Definition: HcalGeometry.h:108
ForwardSubdetector
const HGCalGeometry * gHGCal_
Definition: HGCDigitizer.h:99
bool producesEEDigis()
Definition: HGCDigitizer.h:67
#define constexpr
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const
Get a list of valid detector ids (for the given subdetector)
Definition: HcalGeometry.cc:75
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const override
Get a list of valid detector ids (for the given subdetector)
Definition: HGCalGeometry.h:76
std::string digiCollection_
Definition: HGCDigitizer.h:81
const HcalTopology & topology() const
Definition: HcalGeometry.h:117
std::unordered_map< uint32_t, HGCCellInfo > HGCSimHitDataAccumulator
T mag() const
Definition: PV3DBase.h:67
edm::SortedCollection< HGCBHDataFrame > HGCBHDigiCollection
void beginRun(const edm::EventSetup &es)
actions at the start/end of run
const HGCalTopology & topology() const
Definition: HGCalGeometry.h:96
double f[11][100]
std::unordered_set< DetId > validIds_
Definition: HGCDigitizer.h:98
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:416
bool producesHEfrontDigis()
Definition: HGCDigitizer.h:68
std::unique_ptr< HGCHEbackDigitizer > theHGCHEbackDigitizer_
Definition: HGCDigitizer.h:94
Definition: DetId.h:18
bool producesHEbackDigis()
Definition: HGCDigitizer.h:69
const HGCalDDDConstants & dddConstants() const
static void unpackSquareIndex(const uint32_t &idx, int &z, int &lay, int &sec, int &subsec, int &cell)
const T & get() const
Definition: EventSetup.h:56
int maxSimHitsAccTime_
Definition: HGCDigitizer.h:87
std::unique_ptr< HGCHEfrontDigitizer > theHGCHEfrontDigitizer_
Definition: HGCDigitizer.h:95
std::unique_ptr< HGCEEDigitizer > theHGCEEDigitizer_
Definition: HGCDigitizer.h:93
void accumulate(edm::Event const &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre)
handle SimHit accumulation
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
HGCDigitizer(const edm::ParameterSet &ps, edm::ConsumesCollector &iC)
Definition: HGCDigitizer.cc:97
std::array< double, 3 > averageOccupancies_
Definition: HGCDigitizer.h:115
DetId relabel(const uint32_t testId) const
std::unique_ptr< hgc::HGCSimHitDataAccumulator > simHitAccumulator_
Definition: HGCDigitizer.h:89
static bool orderByDetIdThenTime(const HGCCaloHitTuple_t &a, const HGCCaloHitTuple_t &b)
Definition: HGCDigitizer.h:37
double bxTime_
Definition: HGCDigitizer.h:88
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
std::string digiCollection()
Definition: HGCDigitizer.h:70
edm::SortedCollection< HGCHEDataFrame > HGCHEDigiCollection
const HcalGeometry * gHcal_
Definition: HGCDigitizer.h:100
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
def move(src, dest)
Definition: eostools.py:510
uint32_t verbosity_
Definition: HGCDigitizer.h:106