5 #include "vdt/vdtMath.h"
16 adcSaturation_fC_{-1.0},
19 tdcSaturation_fC_{-1.0},
23 tdcResolutionInNs_{1
e-9},
24 targetMIPvalue_ADC_{},
26 jitterConstant2_ns_{},
28 toaMode_(WEIGHTEDBYE) {
29 edm::LogVerbatim(
"HGCFE") <<
"[HGCFEElectronics] running with version " << fwVersion_ << std::endl;
30 if (ps.exists(
"adcPulse")) {
31 auto temp = ps.getParameter<std::vector<double> >(
"adcPulse");
32 for (
unsigned i = 0;
i <
temp.size(); ++
i) {
36 for (
unsigned i = 0;
i < adcPulse_.size(); ++
i) {
37 adcPulse_[
i] = adcPulse_[
i] / adcPulse_[2];
39 temp = ps.getParameter<std::vector<double> >(
"pulseAvgT");
40 for (
unsigned i = 0;
i <
temp.size(); ++
i) {
44 if (ps.exists(
"adcNbits")) {
45 uint32_t
adcNbits = ps.getParameter<uint32_t>(
"adcNbits");
46 adcSaturation_fC_ = ps.getParameter<
double>(
"adcSaturation_fC");
49 <<
" with LSB=" << adcLSB_fC_ <<
" saturation to occur @ " << adcSaturation_fC_
53 if (ps.exists(
"tdcNbits")) {
54 uint32_t
tdcNbits = ps.getParameter<uint32_t>(
"tdcNbits");
55 tdcSaturation_fC_ = ps.getParameter<
double>(
"tdcSaturation_fC");
59 tdcSaturation_fC_ *= (1. - 1
e-6);
61 <<
" saturation to occur @ " << tdcSaturation_fC_
62 <<
" (NB lowered by 1 part in a million)" << std::endl;
64 if (ps.exists(
"targetMIPvalue_ADC"))
65 targetMIPvalue_ADC_ = ps.getParameter<uint32_t>(
"targetMIPvalue_ADC");
66 if (ps.exists(
"adcThreshold_fC"))
67 adcThreshold_fC_ = ps.getParameter<
double>(
"adcThreshold_fC");
68 if (ps.exists(
"tdcOnset_fC"))
69 tdcOnset_fC_ = ps.getParameter<
double>(
"tdcOnset_fC");
70 if (ps.exists(
"tdcForToAOnset_fC")) {
71 auto temp = ps.getParameter<std::vector<double> >(
"tdcForToAOnset_fC");
72 if (
temp.size() == tdcForToAOnset_fC_.size()) {
73 std::copy_n(
temp.begin(),
temp.size(), tdcForToAOnset_fC_.begin());
75 throw cms::Exception(
"BadConfiguration") <<
" HGCFEElectronics wrong size for ToA thresholds ";
78 if (ps.exists(
"toaLSB_ns"))
79 toaLSB_ns_ = ps.getParameter<
double>(
"toaLSB_ns");
80 if (ps.exists(
"tdcChargeDrainParameterisation")) {
81 for (
auto val : ps.getParameter<std::vector<double> >(
"tdcChargeDrainParameterisation")) {
82 tdcChargeDrainParameterisation_.push_back((
float)
val);
85 if (ps.exists(
"tdcResolutionInPs"))
86 tdcResolutionInNs_ = ps.getParameter<
double>(
"tdcResolutionInPs") * 1
e-3;
87 if (ps.exists(
"toaMode"))
88 toaMode_ = ps.getParameter<uint32_t>(
"toaMode");
90 if (ps.exists(
"jitterNoise_ns")) {
91 auto temp = ps.getParameter<std::vector<double> >(
"jitterNoise_ns");
92 if (
temp.size() == jitterNoise2_ns_.size()) {
93 std::copy_n(
temp.begin(),
temp.size(), jitterNoise2_ns_.begin());
95 throw cms::Exception(
"BadConfiguration") <<
" HGCFEElectronics wrong size for ToA jitterNoise ";
98 if (ps.exists(
"jitterConstant_ns")) {
99 auto temp = ps.getParameter<std::vector<double> >(
"jitterConstant_ns");
100 if (
temp.size() == jitterConstant2_ns_.size()) {
101 std::copy_n(
temp.begin(),
temp.size(), jitterConstant2_ns_.begin());
103 throw cms::Exception(
"BadConfiguration") <<
" HGCFEElectronics wrong size for ToA jitterConstant ";
111 DFr& dataFrame,
HGCSimHitData& chargeColl, uint32_t thrADC,
float lsbADC, uint32_t gainIdx,
float maxADC) {
115 for (
int it = 0; it < (
int)(chargeColl.size()); it++)
116 debug |= (chargeColl[it] > adcThreshold_fC_);
128 maxADC = adcSaturation_fC_ * (1 - 1
e-6);
129 for (
int it = 0; it < (
int)(chargeColl.size()); it++) {
131 const uint32_t
adc = std::floor(
std::min(chargeColl[it], maxADC) / lsbADC);
133 newSample.
set(
adc > thrADC,
false, gainIdx, 0,
adc);
134 dataFrame.setSample(it, newSample);
141 std::ostringstream
msg;
142 dataFrame.print(
msg);
159 for (
int it = 0; it < (
int)(chargeColl.size()); it++) {
160 const float charge(chargeColl[it]);
171 for (
int ipulse = -2; ipulse < (
int)(
adcPulse.size()) - 2; ipulse++) {
174 if (it + ipulse >= (
int)(dataFrame.size()))
177 newCharge[it + ipulse] += chargeLeak;
187 for (
int it = 0; it < (
int)(newCharge.size()); it++) {
189 const uint32_t
adc = std::floor(
std::min(newCharge[it], maxADC) / lsbADC);
191 newSample.
set(
adc > thrADC,
false, gainIdx, 0,
adc);
192 dataFrame.setSample(it, newSample);
199 std::ostringstream
msg;
200 dataFrame.print(
msg);
210 CLHEP::HepRandomEngine* engine,
217 busyFlags.fill(
false);
218 totFlags.fill(
false);
219 toaFlags.fill(
false);
221 toaFromToT.fill(0.
f);
224 constexpr
bool debug_state(
true);
226 constexpr
bool debug_state(
false);
229 bool debug = debug_state;
239 if (toaColl[fireBX] != 0.
f) {
240 timeToA = toaColl[fireBX];
241 float jitter = getTimeJitter(chargeColl[fireBX],
thickness);
243 timeToA = CLHEP::RandGaussQ::shoot(engine, timeToA, jitter);
244 else if (tdcResolutionInNs_ != 0)
245 timeToA = CLHEP::RandGaussQ::shoot(engine, timeToA, tdcResolutionInNs_);
246 if (timeToA >= 0.
f && timeToA <= 25.
f)
247 toaFlags[fireBX] =
true;
253 for (
int it = 0; it < (
int)(chargeColl.size()); ++it) {
260 float charge = chargeColl[it];
261 if (
charge < tdcOnset_fC_) {
272 edm::LogVerbatim(
"HGCFE") <<
"\t q=" <<
charge <<
" fC with <toa>=" << toa <<
" ns, triggers ToT @ " << it
278 float totalCharge(
charge), finalToA(toa), integTime(0);
283 float charge_offset = 0.f;
284 const float charge_kfC(totalCharge * 1
e-3);
285 if (charge_kfC < tdcChargeDrainParameterisation_[3]) {
287 }
else if (charge_kfC < tdcChargeDrainParameterisation_[7]) {
289 charge_offset = tdcChargeDrainParameterisation_[3];
293 charge_offset = tdcChargeDrainParameterisation_[7];
296 const float charge_mod = charge_kfC - charge_offset;
297 const float newIntegTime =
298 ((tdcChargeDrainParameterisation_[poffset] * charge_mod + tdcChargeDrainParameterisation_[poffset + 1]) *
300 tdcChargeDrainParameterisation_[poffset + 2]);
302 const int newBusyBxs = std::floor(newIntegTime / 25.
f) + 1;
306 integTime = newIntegTime;
307 if (newBusyBxs == busyBxs)
313 edm::LogVerbatim(
"HGCFE") <<
"\t Intial busy estimate=" << integTime <<
" ns = " << newBusyBxs <<
" bxs"
316 edm::LogVerbatim(
"HGCFE") <<
"\t ...integrated charge overflows initial busy estimate, interating again"
321 busyBxs = newBusyBxs;
325 if (toaMode_ == WEIGHTEDBYE)
329 for (
int jt = 0; jt < it; ++jt) {
330 const unsigned int deltaT = (it - jt);
331 if ((deltaT + 2) >=
adcPulse.size() || chargeColl[jt] == 0.f || totFlags[jt] || busyFlags[jt])
334 const float leakCharge = chargeColl[jt] *
adcPulse[deltaT + 2];
335 totalCharge += leakCharge;
336 if (toaMode_ == WEIGHTEDBYE)
337 finalToA += leakCharge * pulseAvgT_[deltaT + 2];
340 edm::LogVerbatim(
"HGCFE") <<
"\t\t leaking " << chargeColl[jt] <<
" fC @ deltaT=-" << deltaT <<
" -> +"
341 << leakCharge <<
" with avgT=" << pulseAvgT_[deltaT + 2] << std::endl;
345 for (
int jt = it + 1; jt < it + busyBxs && jt < dataFrame.size(); ++jt) {
348 busyFlags[jt] =
true;
350 const float extraCharge = chargeColl[jt];
351 if (extraCharge == 0.
f)
354 edm::LogVerbatim(
"HGCFE") <<
"\t\t adding " << extraCharge <<
" fC @ deltaT=+" << (jt - it) << std::endl;
356 totalCharge += extraCharge;
357 if (toaMode_ == WEIGHTEDBYE)
358 finalToA += extraCharge * toaColl[jt];
362 if (toaMode_ == WEIGHTEDBYE)
363 finalToA /= totalCharge;
365 newCharge[it] = (totalCharge - tdcOnset_fC_);
368 edm::LogVerbatim(
"HGCFE") <<
"\t Final busy estimate=" << integTime <<
" ns = " << busyBxs <<
" bxs" << std::endl
369 <<
"\t Total integrated=" << totalCharge <<
" fC <toa>=" << toaFromToT[it]
370 <<
" (raw=" << finalToA <<
") ns " << std::endl;
373 if (it + busyBxs < (
int)(newCharge.size())) {
374 const float deltaT2nextBx((busyBxs * 25 - integTime));
375 const float tdcOnsetLeakage(tdcOnset_fC_ *
vdt::fast_expf(-deltaT2nextBx / tdcChargeDrainParameterisation_[11]));
377 edm::LogVerbatim(
"HGCFE") <<
"\t Leaking remainder of TDC onset " << tdcOnset_fC_ <<
" fC, to be dissipated in "
378 << deltaT2nextBx <<
" DeltaT/tau=" << deltaT2nextBx <<
" / "
379 << tdcChargeDrainParameterisation_[11] <<
" ns, adds " << tdcOnsetLeakage <<
" fC @ "
380 << it + busyBxs <<
" bx (first free bx)" << std::endl;
381 newCharge[it + busyBxs] += tdcOnsetLeakage;
386 auto runChargeSharing = [&]() {
388 for (
int it = 0; it < (
int)(chargeColl.size()); ++it) {
391 if (!totFlags[it] & !busyFlags[it]) {
394 for (ipulse =
start; ipulse < stop; ++ipulse) {
395 const int itoffset = it + ipulse - 2;
399 if (!totFlags[itoffset] & !busyFlags[itoffset]) {
400 newCharge[itoffset] += chargeColl[it] *
adcPulse[ipulse];
435 maxADC = adcSaturation_fC_;
436 for (
int it = 0; it < (
int)(newCharge.size()); it++) {
438 edm::LogVerbatim(
"HGCFE") << chargeColl[it] <<
" -> " << newCharge[it] <<
" ";
441 if (totFlags[it] || busyFlags[it]) {
444 const float saturatedCharge(
std::min(newCharge[it], tdcSaturation_fC_));
447 true,
true, gainIdx, (uint16_t)(timeToA / toaLSB_ns_), (uint16_t)(std::floor(saturatedCharge / tdcLSB_fC_)));
451 newSample.
set(
false,
true, gainIdx, 0, 0);
455 const uint16_t
adc = std::floor(
std::min(newCharge[it], maxADC) / lsbADC);
457 newSample.
set(
adc > thrADC,
false, gainIdx, (uint16_t)(timeToA / toaLSB_ns_),
adc);
461 dataFrame.setSample(it, newSample);
465 std::ostringstream
msg;
466 dataFrame.print(
msg);