CMS 3D CMS Logo

MTDDigitizer.h
Go to the documentation of this file.
1 #ifndef FastTimingSimProducers_FastTimingCommon_MTDDigitizer_h
2 #define FastTimingSimProducers_FastTimingCommon_MTDDigitizer_h
3 
6 
11 
15 
23 
25 
27 
28 #include <vector>
29 #include <unordered_map>
30 #include <unordered_set>
31 #include <memory>
32 #include <tuple>
33 
34 
35 namespace mtd_digitizer {
36 
37  namespace MTDHelpers {
38  // index , det id, time
39  typedef std::tuple<int,uint32_t,float> MTDCaloHitTuple_t;
40 
41  bool orderByDetIdThenTime(const MTDCaloHitTuple_t &a, const MTDCaloHitTuple_t &b)
42  {
43  unsigned int detId_a(std::get<1>(a)), detId_b(std::get<1>(b));
44 
45  if(detId_a<detId_b) return true;
46  if(detId_a>detId_b) return false;
47 
48  double time_a(std::get<2>(a)), time_b(std::get<2>(b));
49  if(time_a<time_b) return true;
50 
51  return false;
52  }
53  }
54 
55  inline
56  void saveSimHitAccumulator(PMTDSimAccumulator& simResult, const MTDSimHitDataAccumulator& simData, const float minCharge, const float maxCharge) {
57  constexpr auto nEnergies = std::tuple_size<decltype(MTDCellInfo().hit_info)>::value;
58  static_assert(nEnergies <= PMTDSimAccumulator::Data::energyMask+1, "PMTDSimAccumulator bit pattern needs to updated");
59  static_assert(nSamples <= PMTDSimAccumulator::Data::sampleMask+1, "PMTDSimAccumulator bit pattern needs to updated");
60 
61  const float minPackChargeLog = minCharge > 0.f ? std::log(minCharge) : -2;
62  const float maxPackChargeLog = std::log(maxCharge);
64 
65  simResult.reserve(simData.size());
66  // mimicing the digitization
67  for(const auto& elem: simData) {
68  // store only non-zero
69  for(size_t iEn = 0; iEn < nEnergies; ++iEn) {
70  const auto& samples = elem.second.hit_info[iEn];
71  for(size_t iSample = 0; iSample < nSamples; ++iSample) {
72  if(samples[iSample] > minCharge) {
73  unsigned short packed;
74  if(iEn == 1) {
75  // assuming linear range for tof of 0..26
76  packed = samples[iSample]/PREMIX_MAX_TOF * base;
77  }
78  else {
79  packed = logintpack::pack16log(samples[iSample], minPackChargeLog, maxPackChargeLog, base);
80  }
81  simResult.emplace_back(elem.first.detid_, elem.first.row_, elem.first.column_,
82  iEn, iSample, packed);
83  }
84  }
85  }
86  }
87  }
88 
89  inline
90  void loadSimHitAccumulator(MTDSimHitDataAccumulator& simData, const PMTDSimAccumulator& simAccumulator, const float minCharge, const float maxCharge) {
91  const float minPackChargeLog = minCharge > 0.f ? std::log(minCharge) : -2;
92  const float maxPackChargeLog = std::log(maxCharge);
94 
95  for(const auto& detIdIndexHitInfo: simAccumulator) {
96  auto foo = simData.emplace(MTDCellId(detIdIndexHitInfo.detId(), detIdIndexHitInfo.row(), detIdIndexHitInfo.column()),
97  MTDCellInfo());
98  auto simIt = foo.first;
99  auto& hit_info = simIt->second.hit_info;
100 
101  size_t iEn = detIdIndexHitInfo.energyIndex();
102  size_t iSample = detIdIndexHitInfo.sampleIndex();
103 
104  float value;
105  if(iEn == 1) {
106  value = static_cast<float>(detIdIndexHitInfo.data())/base*PREMIX_MAX_TOF;
107  }
108  else {
109  value = logintpack::unpack16log(detIdIndexHitInfo.data(), minPackChargeLog, maxPackChargeLog, base);
110  }
111 
112  if(iEn == 0) {
113  hit_info[iEn][iSample] += value;
114  }
115  else if(hit_info[iEn][iSample] == 0) {
116  // For iEn==1 the digitizers just set the TOF of the first SimHit
117  hit_info[iEn][iSample] = value;
118  }
119  }
120  }
121 
122  template<class Traits>
124  {
125  public:
126 
127  typedef typename Traits::DeviceSim DeviceSim ;
128  typedef typename Traits::ElectronicsSim ElectronicsSim;
129  typedef typename Traits::DigiCollection DigiCollection;
130 
134  MTDDigitizerBase(config,iC,parent),
135  geom_(nullptr),
136  deviceSim_( config.getParameterSet("DeviceSimulation") ),
137  electronicsSim_( config.getParameterSet("ElectronicsSimulation") ),
138  maxSimHitsAccTime_( config.getParameter< uint32_t >("maxSimHitsAccTime") ) { }
139 
140  ~MTDDigitizer() override { }
141 
145  void accumulate(edm::Event const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
146  void accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
147  void accumulate(edm::Handle<edm::PSimHitContainer> const &hits, int bxCrossing, CLHEP::HepRandomEngine* hre) override;
148  // for premixing
149  void accumulate(const PMTDSimAccumulator& simAccumulator) override;
150 
154  void initializeEvent(edm::Event const& e, edm::EventSetup const& c) override;
155  void finalizeEvent(edm::Event& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
156 
160  void beginRun(const edm::EventSetup & es) override;
161  void endRun() override {}
162 
163  private :
164 
166  MTDSimHitDataAccumulator().swap(simHitAccumulator_);
167  }
168 
170 
171  // implementations
172  DeviceSim deviceSim_; // processes a given simhit into an entry in a MTDSimHitDataAccumulator
173  ElectronicsSim electronicsSim_; // processes a MTDSimHitDataAccumulator into a BTLDigiCollection/ETLDigiCollection
174 
175  //handle sim hits
178 
179  };
180 
181  template<class Traits>
183  edm::EventSetup const& c,
184  CLHEP::HepRandomEngine* hre) {
186  e.getByLabel(inputSimHits_, simHits);
187  accumulate(simHits,0,hre);
188  }
189 
190  template<class Traits>
192  edm::EventSetup const& c,
193  CLHEP::HepRandomEngine* hre){
195  e.getByLabel(inputSimHits_, simHits);
196  accumulate(simHits,e.bunchCrossing(),hre);
197  }
198 
199  template<class Traits>
201  int bxCrossing,
202  CLHEP::HepRandomEngine* hre) {
203  using namespace MTDHelpers;
204 
205  //create list of tuples (pos in container, RECO DetId, time) to be sorted first
206  int nchits=(int)hits->size();
207  std::vector< MTDCaloHitTuple_t > hitRefs;
208  hitRefs.reserve(nchits);
209  for(int i=0; i<nchits; ++i) {
210  const auto& the_hit = hits->at(i);
211 
212  DetId id = the_hit.detUnitId();
213 
214  if (verbosity_>0) {
215  edm::LogInfo("MTDDigitizer") << " i/p " << std::hex << the_hit.detUnitId() << std::dec
216  << " o/p " << id.rawId() << std::endl;
217  }
218 
219  if( 0 != id.rawId() ) {
220  hitRefs.emplace_back( i, id.rawId(), the_hit.tof() );
221  }
222  }
223  std::sort(hitRefs.begin(),hitRefs.end(),MTDHelpers::orderByDetIdThenTime);
224 
225  deviceSim_.getHitsResponse(hitRefs, hits, &simHitAccumulator_, hre);
226 
227  hitRefs.clear();
228 
229  }
230 
231  template<class Traits>
233  loadSimHitAccumulator(simHitAccumulator_, simAccumulator, premixStage1MinCharge_, premixStage1MaxCharge_);
234  }
235 
236  template<class Traits>
238  deviceSim_.getEvent(e);
239  if(not premixStage1_) {
240  electronicsSim_.getEvent(e);
241  }
242  }
243 
244  template<class Traits>
246  CLHEP::HepRandomEngine* hre) {
247  if(premixStage1_) {
248  auto simResult = std::make_unique<PMTDSimAccumulator>();
249  saveSimHitAccumulator(*simResult, simHitAccumulator_, premixStage1MinCharge_, premixStage1MaxCharge_);
250  e.put(std::move(simResult), digiCollection_);
251  }
252  else {
253  auto digiCollection = std::make_unique<DigiCollection>();
254  electronicsSim_.run(simHitAccumulator_,*digiCollection, hre);
255  e.put(std::move(digiCollection),digiCollection_);
256  }
257 
258  //release memory for next event
259  resetSimHitDataAccumulator();
260  }
261 
262 
263  template<class Traits>
265 
267  es.get<MTDDigiGeometryRecord>().get(geom);
268  geom_ = geom.product();
269 
270  deviceSim_.getEventSetup(es);
271  if(not premixStage1_) {
272  electronicsSim_.getEventSetup(es);
273  }
274 
275  }
276 }
277 
278 #endif
void accumulate(edm::Event const &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
handle SimHit accumulation
Definition: MTDDigitizer.h:182
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
static constexpr unsigned energyMask
void emplace_back(unsigned int detId, unsigned char row, unsigned char column, unsigned short energyIndex, unsigned short sampleIndex, unsigned short data)
void saveSimHitAccumulator(PMTDSimAccumulator &simResult, const MTDSimHitDataAccumulator &simData, const float minCharge, const float maxCharge)
Definition: MTDDigitizer.h:56
ParameterSet const & getParameterSet(ParameterSetID const &id)
void reserve(size_t size)
#define nullptr
MTDSimHitDataAccumulator simHitAccumulator_
Definition: MTDDigitizer.h:177
std::unordered_map< MTDCellId, MTDCellInfo > MTDSimHitDataAccumulator
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:55
Definition: config.py:1
void finalizeEvent(edm::Event &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
Definition: MTDDigitizer.h:245
constexpr float PREMIX_MAX_TOF
void beginRun(const edm::EventSetup &es) override
actions at the start/end of run
Definition: MTDDigitizer.h:264
Traits::ElectronicsSim ElectronicsSim
Definition: MTDDigitizer.h:128
Traits::DigiCollection DigiCollection
Definition: MTDDigitizer.h:129
base
Make Sure CMSSW is Setup ##.
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:19
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
actions at the start/end of event
Definition: MTDDigitizer.h:237
Definition: DetId.h:18
bool orderByDetIdThenTime(const MTDCaloHitTuple_t &a, const MTDCaloHitTuple_t &b)
Definition: MTDDigitizer.h:41
double b
Definition: hdecay.h:120
std::tuple< int, uint32_t, float > MTDCaloHitTuple_t
Definition: MTDDigitizer.h:39
const MTDGeometry * geom_
Definition: MTDDigitizer.h:169
double a
Definition: hdecay.h:121
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
T get() const
Definition: EventSetup.h:71
MTDDigitizer(const edm::ParameterSet &config, edm::ConsumesCollector &iC, edm::ProducerBase &parent)
Definition: MTDDigitizer.h:131
static constexpr unsigned sampleMask
constexpr size_t nSamples
ElectronicsSim electronicsSim_
Definition: MTDDigitizer.h:173
static constexpr unsigned sampleOffset
Traits::DeviceSim DeviceSim
Definition: MTDDigitizer.h:127
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:26
void loadSimHitAccumulator(MTDSimHitDataAccumulator &simData, const PMTDSimAccumulator &simAccumulator, const float minCharge, const float maxCharge)
Definition: MTDDigitizer.h:90
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:511
#define constexpr