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  accumulate(simHits, 0, hre);
183  }
184 
185  template <class Traits>
187  edm::EventSetup const& c,
188  CLHEP::HepRandomEngine* hre) {
190  e.getByLabel(inputSimHits_, simHits);
191  accumulate(simHits, e.bunchCrossing(), hre);
192  }
193 
194  template <class Traits>
196  int bxCrossing,
197  CLHEP::HepRandomEngine* hre) {
198  using namespace MTDHelpers;
199 
200  //create list of tuples (pos in container, RECO DetId, time) to be sorted first
201  int nchits = (int)hits->size();
202  std::vector<MTDCaloHitTuple_t> hitRefs;
203  hitRefs.reserve(nchits);
204  for (int i = 0; i < nchits; ++i) {
205  const auto& the_hit = hits->at(i);
206 
207  DetId id = the_hit.detUnitId();
208 
209  if (verbosity_ > 0) {
210  edm::LogInfo("MTDDigitizer") << " i/p " << std::hex << the_hit.detUnitId() << std::dec << " o/p " << id.rawId()
211  << std::endl;
212  }
213 
214  if (0 != id.rawId()) {
215  hitRefs.emplace_back(i, id.rawId(), the_hit.tof());
216  }
217  }
218  std::sort(hitRefs.begin(), hitRefs.end(), MTDHelpers::orderByDetIdThenTime);
219 
220  deviceSim_.getHitsResponse(hitRefs, hits, &simHitAccumulator_, hre);
221 
222  hitRefs.clear();
223  }
224 
225  template <class Traits>
227  loadSimHitAccumulator(simHitAccumulator_, simAccumulator, premixStage1MinCharge_, premixStage1MaxCharge_);
228  }
229 
230  template <class Traits>
232  geom_ = &c.getData(geomToken_);
233 
234  deviceSim_.getEvent(e);
235  deviceSim_.getEventSetup(c);
236  if (not premixStage1_) {
237  electronicsSim_.getEvent(e);
238  electronicsSim_.getEventSetup(c);
239  }
240  }
241 
242  template <class Traits>
243  void MTDDigitizer<Traits>::finalizeEvent(edm::Event& e, edm::EventSetup const& c, CLHEP::HepRandomEngine* hre) {
244  if (premixStage1_) {
245  auto simResult = std::make_unique<PMTDSimAccumulator>();
246  saveSimHitAccumulator(*simResult, simHitAccumulator_, premixStage1MinCharge_, premixStage1MaxCharge_);
247  e.put(std::move(simResult), digiCollection_);
248  } else {
249  auto digiCollection = std::make_unique<DigiCollection>();
250  electronicsSim_.run(simHitAccumulator_, *digiCollection, hre);
251  e.put(std::move(digiCollection), digiCollection_);
252  }
253 
254  //release memory for next event
255  resetSimHitDataAccumulator();
256  }
257 } // namespace mtd_digitizer
258 
259 #endif
logintpack::pack16log
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:27
mtd_digitizer::MTDDigitizer::resetSimHitDataAccumulator
void resetSimHitDataAccumulator()
Definition: MTDDigitizer.h:164
mtd_digitizer::nSamples
constexpr size_t nSamples
Definition: MTDDigitizerTypes.h:11
mtd_digitizer::MTDDigitizer::simHitAccumulator_
MTDSimHitDataAccumulator simHitAccumulator_
Definition: MTDDigitizer.h:175
BTLDetId.h
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
PixelTopology.h
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
mtd_digitizer::MTDCellInfo
Definition: MTDDigitizerTypes.h:17
mtd_digitizer::MTDDigitizer::electronicsSim_
ElectronicsSim electronicsSim_
Definition: MTDDigitizer.h:171
MTDDigitizerTraits.h
PMTDSimAccumulator
Definition: PMTDSimAccumulator.h:9
liblogintpack.h
MTDGeometry.h
MTDDigiGeometryRecord.h
GeomDetType.h
mtd_digitizer::MTDDigitizer::MTDDigitizer
MTDDigitizer(const edm::ParameterSet &config, edm::ProducesCollector producesCollector, edm::ConsumesCollector &iC)
Definition: MTDDigitizer.h:136
FastTrackerRecHitCombiner_cfi.simHits
simHits
Definition: FastTrackerRecHitCombiner_cfi.py:5
mtd_digitizer::MTDDigitizer::initializeEvent
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
actions at the start/end of event
Definition: MTDDigitizer.h:231
PileUpEventPrincipal
Definition: PileUpEventPrincipal.h:19
mtd_digitizer::MTDDigitizer::DigiCollection
Traits::DigiCollection DigiCollection
Definition: MTDDigitizer.h:134
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
ETLDetId.h
edm::Handle< edm::PSimHitContainer >
MTDDigitizerBase
Definition: MTDDigitizerBase.h:31
mtd_digitizer::MTDDigitizer::ElectronicsSim
Traits::ElectronicsSim ElectronicsSim
Definition: MTDDigitizer.h:133
ProducesCollector.h
mtd_digitizer::MTDDigitizer::DeviceSim
Traits::DeviceSim DeviceSim
Definition: MTDDigitizer.h:132
EgammaValidation_cff.samples
samples
Definition: EgammaValidation_cff.py:19
PMTDSimAccumulator::reserve
void reserve(size_t size)
Definition: PMTDSimAccumulator.h:52
config
Definition: config.py:1
DetId
Definition: DetId.h:17
mtd_digitizer::MTDDigitizer::deviceSim_
DeviceSim deviceSim_
Definition: MTDDigitizer.h:170
RectangularMTDTopology.h
PileUpEventPrincipal.h
mtd_digitizer::loadSimHitAccumulator
void loadSimHitAccumulator(MTDSimHitDataAccumulator &simData, const PMTDSimAccumulator &simAccumulator, const float minCharge, const float maxCharge)
Definition: MTDDigitizer.h:92
logintpack::unpack16log
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:62
b
double b
Definition: hdecay.h:118
mtd_digitizer::PREMIX_MAX_TOF
constexpr float PREMIX_MAX_TOF
Definition: MTDDigitizerTypes.h:24
edm::ParameterSet
Definition: ParameterSet.h:47
a
double a
Definition: hdecay.h:119
Event.h
mtd_digitizer::MTDDigitizer::geom_
const MTDGeometry * geom_
Definition: MTDDigitizer.h:167
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
MTDDigitizerBase.h
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
mtd_digitizer::MTDHelpers::MTDCaloHitTuple_t
std::tuple< int, uint32_t, float > MTDCaloHitTuple_t
Definition: MTDDigitizer.h:38
mtd_digitizer::MTDDigitizer
Definition: MTDDigitizer.h:130
mtd_digitizer::MTDCellId
Definition: MTDDigitizerTypes.h:26
apvshotsanalyzer_cfi.digiCollection
digiCollection
Definition: apvshotsanalyzer_cfi.py:4
edm::EventSetup
Definition: EventSetup.h:58
mtd_digitizer::MTDDigitizer::accumulate
void accumulate(edm::Event const &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
handle SimHit accumulation
Definition: MTDDigitizer.h:179
mtd_digitizer::MTDDigitizer::geomToken_
const edm::ESGetToken< MTDGeometry, MTDDigiGeometryRecord > geomToken_
Definition: MTDDigitizer.h:166
mtd_digitizer::saveSimHitAccumulator
void saveSimHitAccumulator(PMTDSimAccumulator &simResult, const MTDSimHitDataAccumulator &simData, const float minCharge, const float maxCharge)
Definition: MTDDigitizer.h:56
edm::ESGetToken< MTDGeometry, MTDDigiGeometryRecord >
PMTDSimAccumulator::Data::dataMask
constexpr static unsigned dataMask
Definition: PMTDSimAccumulator.h:35
mtd_digitizer::MTDDigitizer::finalizeEvent
void finalizeEvent(edm::Event &e, edm::EventSetup const &c, CLHEP::HepRandomEngine *hre) override
Definition: MTDDigitizer.h:243
mtd_digitizer::MTDHelpers::orderByDetIdThenTime
bool orderByDetIdThenTime(const MTDCaloHitTuple_t &a, const MTDCaloHitTuple_t &b)
Definition: MTDDigitizer.h:40
ProxyMTDTopology.h
MTDGeometry
Definition: MTDGeometry.h:14
siStripShotFilter_cfi.DigiCollection
DigiCollection
Definition: siStripShotFilter_cfi.py:6
edm::getParameterSet
ParameterSet const & getParameterSet(ParameterSetID const &id)
Definition: ParameterSet.cc:862
eostools.move
def move(src, dest)
Definition: eostools.py:511
edm::ProducesCollector
Definition: ProducesCollector.h:43
mtd_digitizer::MTDDigitizer::~MTDDigitizer
~MTDDigitizer() override
Definition: MTDDigitizer.h:144
DetId.h
Frameworkfwd.h
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Exception
Definition: hltDiff.cc:245
mtd_digitizer::MTDSimHitDataAccumulator
std::unordered_map< MTDCellId, MTDCellInfo > MTDSimHitDataAccumulator
Definition: MTDDigitizerTypes.h:39
PMTDSimAccumulator::Data::energyMask
constexpr static unsigned energyMask
Definition: PMTDSimAccumulator.h:33
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
PMTDSimAccumulator::emplace_back
void emplace_back(unsigned int detId, unsigned char row, unsigned char column, unsigned short energyIndex, unsigned short sampleIndex, unsigned short data)
Definition: PMTDSimAccumulator.h:69
ConsumesCollector.h
mtd_digitizer
Definition: MTDDigitizer.h:34
mtd_digitizer::MTDDigitizer::maxSimHitsAccTime_
const int maxSimHitsAccTime_
Definition: MTDDigitizer.h:174
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
DeDxTools::esConsumes
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
edm::Event
Definition: Event.h:73
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
TauDecayModes.dec
dec
Definition: TauDecayModes.py:143
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
PMTDSimAccumulator::Data::sampleMask
constexpr static unsigned sampleMask
Definition: PMTDSimAccumulator.h:34