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 
16 
23 
25 
27 
28 #include <vector>
29 #include <unordered_map>
30 #include <unordered_set>
31 #include <memory>
32 #include <tuple>
33 
34 namespace mtd_digitizer {
35 
36  namespace MTDHelpers {
37  // index , det id, time
38  typedef std::tuple<int, uint32_t, float> MTDCaloHitTuple_t;
39 
41  unsigned int detId_a(std::get<1>(a)), detId_b(std::get<1>(b));
42 
43  if (detId_a < detId_b)
44  return true;
45  if (detId_a > detId_b)
46  return false;
47 
48  double time_a(std::get<2>(a)), time_b(std::get<2>(b));
49  if (time_a < time_b)
50  return true;
51 
52  return false;
53  }
54  } // namespace MTDHelpers
55 
56  inline void saveSimHitAccumulator(PMTDSimAccumulator& simResult,
57  const MTDSimHitDataAccumulator& simData,
58  const float minCharge,
59  const float maxCharge) {
60  constexpr auto nEnergies = std::tuple_size<decltype(MTDCellInfo().hit_info)>::value;
61  static_assert(nEnergies == PMTDSimAccumulator::Data::energyMask + 1,
62  "PMTDSimAccumulator bit pattern needs to be updated");
64  "PMTDSimAccumulator bit pattern needs to be updated");
65 
66  const float minPackChargeLog = minCharge > 0.f ? std::log(minCharge) : -2;
67  const float maxPackChargeLog = std::log(maxCharge);
68  constexpr uint16_t base = PMTDSimAccumulator::Data::dataMask;
69 
70  simResult.reserve(simData.size());
71  // mimicking the digitization
72  for (const auto& elem : simData) {
73  // store only non-zero
74  for (size_t iEn = 0; iEn < nEnergies; ++iEn) {
75  const auto& samples = elem.second.hit_info[iEn];
76  for (size_t iSample = 0; iSample < nSamples; ++iSample) {
77  if (samples[iSample] > minCharge) {
78  unsigned short packed;
79  if (iEn == 1 || iEn == 3) {
80  // assuming linear range for tof of 0..25
81  packed = samples[iSample] / PREMIX_MAX_TOF * base;
82  } else {
83  packed = logintpack::pack16log(samples[iSample], minPackChargeLog, maxPackChargeLog, base);
84  }
85  simResult.emplace_back(elem.first.detid_, elem.first.row_, elem.first.column_, iEn, iSample, packed);
86  }
87  }
88  }
89  }
90  }
91 
93  const PMTDSimAccumulator& simAccumulator,
94  const float minCharge,
95  const float maxCharge) {
96  const float minPackChargeLog = minCharge > 0.f ? std::log(minCharge) : -2;
97  const float maxPackChargeLog = std::log(maxCharge);
98  constexpr uint16_t base = PMTDSimAccumulator::Data::dataMask;
99 
100  for (const auto& detIdIndexHitInfo : simAccumulator) {
101  auto foo = simData.emplace(
102  MTDCellId(detIdIndexHitInfo.detId(), detIdIndexHitInfo.row(), detIdIndexHitInfo.column()), MTDCellInfo());
103  auto simIt = foo.first;
104  auto& hit_info = simIt->second.hit_info;
105 
106  size_t iEn = detIdIndexHitInfo.energyIndex();
107  size_t iSample = detIdIndexHitInfo.sampleIndex();
108 
110  throw cms::Exception("MTDDigitixer::loadSimHitAccumulator")
111  << "Index out of range: iEn = " << iEn << " iSample = " << iSample << std::endl;
112 
113  float value;
114  if (iEn == 1 || iEn == 3) {
115  value = static_cast<float>(detIdIndexHitInfo.data()) / base * PREMIX_MAX_TOF;
116  } else {
117  value = logintpack::unpack16log(detIdIndexHitInfo.data(), minPackChargeLog, maxPackChargeLog, base);
118  }
119 
120  if (iEn == 0 || iEn == 2) {
121  hit_info[iEn][iSample] += value;
122  } else if (hit_info[iEn][iSample] == 0 || value < hit_info[iEn][iSample]) {
123  // For iEn==1 the digitizers just set the TOF of the first SimHit
124  hit_info[iEn][iSample] = value;
125  }
126  }
127  }
128 
129  template <class Traits>
131  public:
132  typedef typename Traits::DeviceSim DeviceSim;
133  typedef typename Traits::ElectronicsSim ElectronicsSim;
135 
137  : MTDDigitizerBase(config, producesCollector, iC),
138  geomToken_(iC.esConsumes()),
139  geom_(nullptr),
140  deviceSim_(config.getParameterSet("DeviceSimulation"), iC),
141  electronicsSim_(config.getParameterSet("ElectronicsSimulation"), iC),
142  maxSimHitsAccTime_(config.getParameter<uint32_t>("maxSimHitsAccTime")) {}
143 
144  ~MTDDigitizer() override {}
145 
149  void accumulate(edm::Event const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
150  void accumulate(PileUpEventPrincipal const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
152  int bxCrossing,
153  CLHEP::HepRandomEngine* hre) override;
154  // for premixing
155  void accumulate(const PMTDSimAccumulator& simAccumulator) override;
156 
160  void initializeEvent(edm::Event const& e, edm::EventSetup const& c) override;
161  void finalizeEvent(edm::Event& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) override;
162 
163  private:
165 
168 
169  // implementations
170  DeviceSim deviceSim_; // processes a given simhit into an entry in a MTDSimHitDataAccumulator
171  ElectronicsSim electronicsSim_; // processes a MTDSimHitDataAccumulator into a BTLDigiCollection/ETLDigiCollection
172 
173  //handle sim hits
176  };
177 
178  template <class Traits>
179  void MTDDigitizer<Traits>::accumulate(edm::Event const& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) {
181  e.getByLabel(inputSimHits_, simHits);
182  if (simHits.isValid())
183  accumulate(simHits, 0, hre);
184  else
185  edm::LogWarning("MTDMix") << "MTDDigitizer:: Cannot find hits for " << inputSimHits_;
186  }
187 
188  template <class Traits>
190  edm::EventSetup const& c,
191  CLHEP::HepRandomEngine* hre) {
193  e.getByLabel(inputSimHits_, simHits);
194  if (simHits.isValid())
195  accumulate(simHits, e.bunchCrossing(), hre);
196  else
197  edm::LogWarning("MTDMix") << "MTDDigitizer:: Cannot find hits for " << inputSimHits_;
198  }
199 
200  template <class Traits>
202  int bxCrossing,
203  CLHEP::HepRandomEngine* hre) {
204  using namespace MTDHelpers;
205 
206  //create list of tuples (pos in container, RECO DetId, time) to be sorted first
207  int nchits = (int)hits->size();
208  std::vector<MTDCaloHitTuple_t> hitRefs;
209  hitRefs.reserve(nchits);
210  for (int i = 0; i < nchits; ++i) {
211  const auto& the_hit = hits->at(i);
212 
213  DetId id = the_hit.detUnitId();
214 
215  if (verbosity_ > 0) {
216  edm::LogInfo("MTDDigitizer") << " i/p " << std::hex << the_hit.detUnitId() << std::dec << " o/p " << id.rawId()
217  << std::endl;
218  }
219 
220  if (0 != id.rawId()) {
221  hitRefs.emplace_back(i, id.rawId(), the_hit.tof());
222  }
223  }
224  std::sort(hitRefs.begin(), hitRefs.end(), MTDHelpers::orderByDetIdThenTime);
225 
226  deviceSim_.getHitsResponse(hitRefs, hits, &simHitAccumulator_, hre);
227 
228  hitRefs.clear();
229  }
230 
231  template <class Traits>
233  loadSimHitAccumulator(simHitAccumulator_, simAccumulator, premixStage1MinCharge_, premixStage1MaxCharge_);
234  }
235 
236  template <class Traits>
238  geom_ = &c.getData(geomToken_);
239 
240  deviceSim_.getEvent(e);
241  deviceSim_.getEventSetup(c);
242  if (not premixStage1_) {
243  electronicsSim_.getEvent(e);
244  electronicsSim_.getEventSetup(c);
245  }
246  }
247 
248  template <class Traits>
249  void MTDDigitizer<Traits>::finalizeEvent(edm::Event& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) {
250  if (premixStage1_) {
251  auto simResult = std::make_unique<PMTDSimAccumulator>();
252  saveSimHitAccumulator(*simResult, simHitAccumulator_, premixStage1MinCharge_, premixStage1MaxCharge_);
253  e.put(std::move(simResult), digiCollection_);
254  } else {
255  auto digiCollection = std::make_unique<DigiCollection>();
256  electronicsSim_.run(simHitAccumulator_, *digiCollection, hre);
257  e.put(std::move(digiCollection), digiCollection_);
258  }
259 
260  //release memory for next event
261  resetSimHitDataAccumulator();
262  }
263 } // namespace mtd_digitizer
264 
265 #endif
void accumulate(edm::Event const &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
handle SimHit accumulation
Definition: MTDDigitizer.h:179
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
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
void reserve(size_t size)
base
Main Program
Definition: newFWLiteAna.py:92
MTDSimHitDataAccumulator simHitAccumulator_
Definition: MTDDigitizer.h:175
std::unordered_map< MTDCellId, MTDCellInfo > MTDSimHitDataAccumulator
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:62
Definition: config.py:1
void finalizeEvent(edm::Event &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
Definition: MTDDigitizer.h:249
constexpr float PREMIX_MAX_TOF
Traits::ElectronicsSim ElectronicsSim
Definition: MTDDigitizer.h:133
Traits::DigiCollection DigiCollection
Definition: MTDDigitizer.h:134
std::tuple< int, uint32_t, float > MTDCaloHitTuple_t
Definition: MTDDigitizer.h:38
Definition: value.py:1
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
actions at the start/end of event
Definition: MTDDigitizer.h:237
Log< level::Info, false > LogInfo
Definition: DetId.h:17
const edm::ESGetToken< MTDGeometry, MTDDigiGeometryRecord > geomToken_
Definition: MTDDigitizer.h:166
bool orderByDetIdThenTime(const MTDCaloHitTuple_t &a, const MTDCaloHitTuple_t &b)
Definition: MTDDigitizer.h:40
double b
Definition: hdecay.h:118
ParameterSet const & getParameterSet(ParameterSetID const &id)
MTDDigitizer(const edm::ParameterSet &config, edm::ProducesCollector producesCollector, edm::ConsumesCollector &iC)
Definition: MTDDigitizer.h:136
const MTDGeometry * geom_
Definition: MTDDigitizer.h:167
double a
Definition: hdecay.h:119
static constexpr unsigned dataMask
static constexpr unsigned sampleMask
constexpr size_t nSamples
ElectronicsSim electronicsSim_
Definition: MTDDigitizer.h:171
Log< level::Warning, false > LogWarning
Traits::DeviceSim DeviceSim
Definition: MTDDigitizer.h:132
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:27
void loadSimHitAccumulator(MTDSimHitDataAccumulator &simData, const PMTDSimAccumulator &simAccumulator, const float minCharge, const float maxCharge)
Definition: MTDDigitizer.h:92
def move(src, dest)
Definition: eostools.py:511