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,
218 busyFlags.fill(
false);
219 totFlags.fill(
false);
220 toaFlags.fill(
false);
222 toaFromToT.fill(0.
f);
225 constexpr
bool debug_state(
true);
227 constexpr
bool debug_state(
false);
230 bool debug = debug_state;
240 if (toaColl[fireBX] != 0.
f) {
241 timeToA = toaColl[fireBX];
242 float jitter = getTimeJitter(chargeColl[fireBX],
thickness);
244 timeToA = CLHEP::RandGaussQ::shoot(engine, timeToA, jitter);
245 else if (tdcResolutionInNs_ != 0)
246 timeToA = CLHEP::RandGaussQ::shoot(engine, timeToA, tdcResolutionInNs_);
247 if (timeToA >= 0.
f && timeToA <= 25.
f)
248 toaFlags[fireBX] =
true;
254 for (
int it = 0; it < (
int)(chargeColl.size()); ++it) {
260 if (tdcOnsetAuto < 0) {
261 tdcOnsetAuto = tdcOnset_fC_;
264 float charge = chargeColl[it];
265 if (
charge < tdcOnsetAuto) {
276 edm::LogVerbatim(
"HGCFE") <<
"\t q=" <<
charge <<
" fC with <toa>=" << toa <<
" ns, triggers ToT @ " << it
282 float totalCharge(
charge), finalToA(toa), integTime(0);
287 float charge_offset = 0.f;
288 const float charge_kfC(totalCharge * 1
e-3);
289 if (charge_kfC < tdcChargeDrainParameterisation_[3]) {
291 }
else if (charge_kfC < tdcChargeDrainParameterisation_[7]) {
293 charge_offset = tdcChargeDrainParameterisation_[3];
297 charge_offset = tdcChargeDrainParameterisation_[7];
300 const float charge_mod = charge_kfC - charge_offset;
301 const float newIntegTime =
302 ((tdcChargeDrainParameterisation_[poffset] * charge_mod + tdcChargeDrainParameterisation_[poffset + 1]) *
304 tdcChargeDrainParameterisation_[poffset + 2]);
306 const int newBusyBxs = std::floor(newIntegTime / 25.
f) + 1;
310 integTime = newIntegTime;
311 if (newBusyBxs == busyBxs)
317 edm::LogVerbatim(
"HGCFE") <<
"\t Intial busy estimate=" << integTime <<
" ns = " << newBusyBxs <<
" bxs"
320 edm::LogVerbatim(
"HGCFE") <<
"\t ...integrated charge overflows initial busy estimate, interating again"
325 busyBxs = newBusyBxs;
329 if (toaMode_ == WEIGHTEDBYE)
333 for (
int jt = 0; jt < it; ++jt) {
334 const unsigned int deltaT = (it - jt);
335 if ((deltaT + 2) >=
adcPulse.size() || chargeColl[jt] == 0.f || totFlags[jt] || busyFlags[jt])
338 const float leakCharge = chargeColl[jt] *
adcPulse[deltaT + 2];
339 totalCharge += leakCharge;
340 if (toaMode_ == WEIGHTEDBYE)
341 finalToA += leakCharge * pulseAvgT_[deltaT + 2];
344 edm::LogVerbatim(
"HGCFE") <<
"\t\t leaking " << chargeColl[jt] <<
" fC @ deltaT=-" << deltaT <<
" -> +"
345 << leakCharge <<
" with avgT=" << pulseAvgT_[deltaT + 2] << std::endl;
349 for (
int jt = it + 1; jt < it + busyBxs && jt < dataFrame.size(); ++jt) {
352 busyFlags[jt] =
true;
354 const float extraCharge = chargeColl[jt];
355 if (extraCharge == 0.
f)
358 edm::LogVerbatim(
"HGCFE") <<
"\t\t adding " << extraCharge <<
" fC @ deltaT=+" << (jt - it) << std::endl;
360 totalCharge += extraCharge;
361 if (toaMode_ == WEIGHTEDBYE)
362 finalToA += extraCharge * toaColl[jt];
366 if (toaMode_ == WEIGHTEDBYE)
367 finalToA /= totalCharge;
369 newCharge[it] = (totalCharge - tdcOnsetAuto);
372 edm::LogVerbatim(
"HGCFE") <<
"\t Final busy estimate=" << integTime <<
" ns = " << busyBxs <<
" bxs" << std::endl
373 <<
"\t Total integrated=" << totalCharge <<
" fC <toa>=" << toaFromToT[it]
374 <<
" (raw=" << finalToA <<
") ns " << std::endl;
377 if (it + busyBxs < (
int)(newCharge.size())) {
378 const float deltaT2nextBx((busyBxs * 25 - integTime));
379 const float tdcOnsetLeakage(tdcOnsetAuto *
vdt::fast_expf(-deltaT2nextBx / tdcChargeDrainParameterisation_[11]));
381 edm::LogVerbatim(
"HGCFE") <<
"\t Leaking remainder of TDC onset " << tdcOnsetAuto <<
" fC, to be dissipated in "
382 << deltaT2nextBx <<
" DeltaT/tau=" << deltaT2nextBx <<
" / "
383 << tdcChargeDrainParameterisation_[11] <<
" ns, adds " << tdcOnsetLeakage <<
" fC @ "
384 << it + busyBxs <<
" bx (first free bx)" << std::endl;
385 newCharge[it + busyBxs] += tdcOnsetLeakage;
390 auto runChargeSharing = [&]() {
392 for (
int it = 0; it < (
int)(chargeColl.size()); ++it) {
395 if (!totFlags[it] & !busyFlags[it]) {
398 for (ipulse =
start; ipulse < stop; ++ipulse) {
399 const int itoffset = it + ipulse - 2;
403 if (!totFlags[itoffset] & !busyFlags[itoffset]) {
404 newCharge[itoffset] += chargeColl[it] *
adcPulse[ipulse];
439 maxADC = adcSaturation_fC_;
440 for (
int it = 0; it < (
int)(newCharge.size()); it++) {
442 edm::LogVerbatim(
"HGCFE") << chargeColl[it] <<
" -> " << newCharge[it] <<
" ";
445 if (totFlags[it] || busyFlags[it]) {
448 const float saturatedCharge(
std::min(newCharge[it], tdcSaturation_fC_));
451 true,
true, gainIdx, (uint16_t)(timeToA / toaLSB_ns_), (uint16_t)(std::floor(saturatedCharge / tdcLSB_fC_)));
455 newSample.
set(
false,
true, gainIdx, 0, 0);
459 const uint16_t
adc = std::floor(
std::min(newCharge[it], maxADC) / lsbADC);
461 newSample.
set(
adc > thrADC,
false, gainIdx, (uint16_t)(timeToA / toaLSB_ns_),
adc);
465 dataFrame.setSample(it, newSample);
469 std::ostringstream
msg;
470 dataFrame.print(
msg);