CMS 3D CMS Logo

BTLElectronicsSim.cc
Go to the documentation of this file.
2 
5 
6 #include "CLHEP/Random/RandPoissonQ.h"
7 #include "CLHEP/Random/RandGaussQ.h"
8 
9 #include "Math/ChebyshevPol.h"
10 
11 using namespace mtd;
12 
14  : debug_(pset.getUntrackedParameter<bool>("debug", false)),
15  bxTime_(pset.getParameter<double>("bxTime")),
16  testBeamMIPTimeRes_(pset.getParameter<double>("TestBeamMIPTimeRes")),
17  ScintillatorRiseTime_(pset.getParameter<double>("ScintillatorRiseTime")),
18  ScintillatorDecayTime_(pset.getParameter<double>("ScintillatorDecayTime")),
19  ChannelTimeOffset_(pset.getParameter<double>("ChannelTimeOffset")),
20  smearChannelTimeOffset_(pset.getParameter<double>("smearChannelTimeOffset")),
21  EnergyThreshold_(pset.getParameter<double>("EnergyThreshold")),
22  TimeThreshold1_(pset.getParameter<double>("TimeThreshold1")),
23  TimeThreshold2_(pset.getParameter<double>("TimeThreshold2")),
24  ReferencePulseNpe_(pset.getParameter<double>("ReferencePulseNpe")),
25  SinglePhotonTimeResolution_(pset.getParameter<double>("SinglePhotonTimeResolution")),
26  DarkCountRate_(pset.getParameter<double>("DarkCountRate")),
27  SigmaElectronicNoise_(pset.getParameter<double>("SigmaElectronicNoise")),
28  SigmaClock_(pset.getParameter<double>("SigmaClock")),
29  smearTimeForOOTtails_(pset.getParameter<bool>("SmearTimeForOOTtails")),
30  Npe_to_pC_(pset.getParameter<double>("Npe_to_pC")),
31  Npe_to_V_(pset.getParameter<double>("Npe_to_V")),
32  sigmaRelTOFHIRenergy_(pset.getParameter<std::vector<double>>("SigmaRelTOFHIRenergy")),
33  adcNbits_(pset.getParameter<uint32_t>("adcNbits")),
34  tdcNbits_(pset.getParameter<uint32_t>("tdcNbits")),
35  adcSaturation_MIP_(pset.getParameter<double>("adcSaturation_MIP")),
36  adcBitSaturation_(std::pow(2, adcNbits_) - 1),
37  adcLSB_MIP_(adcSaturation_MIP_ / adcBitSaturation_),
38  adcThreshold_MIP_(pset.getParameter<double>("adcThreshold_MIP")),
39  toaLSB_ns_(pset.getParameter<double>("toaLSB_ns")),
40  tdcBitSaturation_(std::pow(2, tdcNbits_) - 1),
41  CorrCoeff_(pset.getParameter<double>("CorrelationCoefficient")),
42  cosPhi_(0.5 * (sqrt(1. + CorrCoeff_) + sqrt(1. - CorrCoeff_))),
43  sinPhi_(0.5 * CorrCoeff_ / cosPhi_),
44  ScintillatorDecayTime2_(ScintillatorDecayTime_ * ScintillatorDecayTime_),
45  ScintillatorDecayTimeInv_(1. / ScintillatorDecayTime_),
46  SPTR2_(SinglePhotonTimeResolution_ * SinglePhotonTimeResolution_),
47  DCRxRiseTime_(DarkCountRate_ * ScintillatorRiseTime_),
48  SigmaElectronicNoise2_(SigmaElectronicNoise_ * SigmaElectronicNoise_),
49  SigmaClock2_(SigmaClock_ * SigmaClock_) {
50 #ifdef EDM_ML_DEBUG
51  float lightOutput = 4.2f * pset.getParameter<double>("LightYield") * pset.getParameter<double>("LightCollectionEff") *
52  pset.getParameter<double>("PhotonDetectionEff"); // average npe for 4.2 MeV
53  float s1 = sigma_stochastic(lightOutput);
54  float s2 = std::sqrt(sigma2_DCR(lightOutput));
55  float s3 = std::sqrt(sigma2_electronics(lightOutput));
57  float s5 = SigmaClock_;
58  LogDebug("BTLElectronicsSim") << " BTL time resolution model (ns, 1 SiPM), for an average light output of "
59  << std::fixed << std::setw(14) << lightOutput << " N_pe:"
60  << "\n sigma stochastic = " << std::setw(14) << s1
61  << "\n sigma DCR = " << std::setw(14) << s2
62  << "\n sigma electronics = " << std::setw(14) << s3
63  << "\n sigma sptr = " << std::setw(14) << s4
64  << "\n sigma clock = " << std::setw(14) << s5 << "\n ---------------------"
65  << "\n sigma total = " << std::setw(14)
66  << std::sqrt(s1 * s1 + s2 * s2 + s3 * s3 + s4 * s4 + s5 * s5);
67 #endif
68 }
69 
72  CLHEP::HepRandomEngine* hre) const {
73  MTDSimHitData chargeColl, toa1, toa2;
74 
75  for (MTDSimHitDataAccumulator::const_iterator it = input.begin(); it != input.end(); it++) {
76  // --- Digitize only the in-time bucket:
77  const unsigned int iBX = mtd_digitizer::kInTimeBX;
78 
79  chargeColl.fill(0.f);
80  toa1.fill(0.f);
81  toa2.fill(0.f);
82  for (size_t iside = 0; iside < 2; iside++) {
83  // --- Fluctuate the total number of photo-electrons
84  float npe = CLHEP::RandPoissonQ::shoot(hre, (it->second).hit_info[2 * iside][iBX]);
85  if (npe < EnergyThreshold_)
86  continue;
87 
88  // --- Get the time of arrival and add a channel time offset
89  float finalToA1 = (it->second).hit_info[1 + 2 * iside][iBX] + ChannelTimeOffset_;
90 
91  if (smearChannelTimeOffset_ > 0.) {
92  float timeSmearing = CLHEP::RandGaussQ::shoot(hre, 0., smearChannelTimeOffset_);
93  finalToA1 += timeSmearing;
94  }
95 
96  // --- Calculate and add the time walk: the time of arrival is read in correspondence
97  // with two thresholds on the signal pulse
98  std::array<float, 3> times =
100 
101  // --- If the pulse amplitude is smaller than TimeThreshold2, the trigger does not fire
102  if (times[1] == 0.)
103  continue;
104 
105  float finalToA2 = finalToA1 + times[1];
106  finalToA1 += times[0];
107 
108  // --- Estimate the time uncertainty due to photons from earlier OOT hits in the current BTL cell
109  if (smearTimeForOOTtails_) {
110  float rate_oot = 0.;
111  // Loop on earlier OOT hits
112  for (int ibx = 0; ibx < mtd_digitizer::kInTimeBX; ++ibx) {
113  if ((it->second).hit_info[2 * iside][ibx] > 0.) {
114  float hit_time = (it->second).hit_info[1 + 2 * iside][ibx] + bxTime_ * (ibx - mtd_digitizer::kInTimeBX);
115  float npe_oot = CLHEP::RandPoissonQ::shoot(hre, (it->second).hit_info[2 * iside][ibx]);
116  rate_oot += npe_oot * exp(hit_time * ScintillatorDecayTimeInv_) * ScintillatorDecayTimeInv_;
117  }
118  } // ibx loop
119 
120  if (rate_oot > 0.) {
121  float sigma_oot = sqrt(rate_oot * ScintillatorRiseTime_) * ScintillatorDecayTime_ / npe;
122  float smearing_oot = CLHEP::RandGaussQ::shoot(hre, 0., sigma_oot);
123  finalToA1 += smearing_oot;
124  finalToA2 += smearing_oot;
125  }
126  } // if smearTimeForOOTtails_
127 
128  // --- Uncertainty due to the fluctuations of the n-th photon arrival time:
129  if (testBeamMIPTimeRes_ > 0.) {
130  // In this case the time resolution is parametrized from the testbeam.
131  // The same parameterization is used for both thresholds.
132  float sigma = sigma_stochastic(npe);
133  float smearing_stat_thr1 = CLHEP::RandGaussQ::shoot(hre, 0., sigma);
134  float smearing_stat_thr2 = CLHEP::RandGaussQ::shoot(hre, 0., sigma);
135 
136  finalToA1 += smearing_stat_thr1;
137  finalToA2 += smearing_stat_thr2;
138 
139  } else {
140  // In this case the time resolution is taken from the literature.
141  // The fluctuations due to the first TimeThreshold1_ p.e. are common to both times
142  float smearing_stat_thr1 =
143  CLHEP::RandGaussQ::shoot(hre, 0., ScintillatorDecayTime_ * sqrt(sigma2_pe(TimeThreshold1_, npe)));
144  float smearing_stat_thr2 = CLHEP::RandGaussQ::shoot(
146  finalToA1 += smearing_stat_thr1;
147  finalToA2 += smearing_stat_thr1 + smearing_stat_thr2;
148  }
149 
150  // --- Add in quadrature the uncertainties due to the SiPM timing resolution, the SiPM DCR,
151  // the electronic noise and the clock distribution:
152  float sigma2_tot_thr1 = SPTR2_ / TimeThreshold1_ + sigma2_DCR(npe) + sigma2_electronics(npe) + SigmaClock2_;
153  float sigma2_tot_thr2 = SPTR2_ / TimeThreshold2_ + sigma2_DCR(npe) + sigma2_electronics(npe) + SigmaClock2_;
154 
155  // --- Smear the arrival times using the correlated uncertainties:
156  float smearing_thr1_uncorr = CLHEP::RandGaussQ::shoot(hre, 0., sqrt(sigma2_tot_thr1));
157  float smearing_thr2_uncorr = CLHEP::RandGaussQ::shoot(hre, 0., sqrt(sigma2_tot_thr2));
158 
159  finalToA1 += cosPhi_ * smearing_thr1_uncorr + sinPhi_ * smearing_thr2_uncorr;
160  finalToA2 += sinPhi_ * smearing_thr1_uncorr + cosPhi_ * smearing_thr2_uncorr;
161 
162  //Smear the energy according to TOFHIR energy branch measured resolution
163  float tofhir_ampnoise_relsigma = ROOT::Math::Chebyshev4(npe,
169  float smearing_tofhir = CLHEP::RandGaussQ::shoot(hre, 0., tofhir_ampnoise_relsigma);
170  // the amplitude resolution already includes the photostatistics fluctuation, use the original average deposit
171  chargeColl[iside] = (it->second).hit_info[2 * iside][iBX] * Npe_to_pC_ *
172  (1. + smearing_tofhir); // the p.e. number is here converted to pC
173 
174  toa1[iside] = finalToA1;
175  toa2[iside] = finalToA2;
176 
177  } // iside loop
178 
179  //run the shaper to create a new data frame
180  BTLDataFrame rawDataFrame(it->first.detid_);
181  runTrivialShaper(rawDataFrame, chargeColl, toa1, toa2, it->first.row_, it->first.column_);
182  updateOutput(output, rawDataFrame);
183 
184  } // MTDSimHitDataAccumulator loop
185 }
186 
188  const mtd::MTDSimHitData& chargeColl,
189  const mtd::MTDSimHitData& toa1,
190  const mtd::MTDSimHitData& toa2,
191  const uint8_t row,
192  const uint8_t col) const {
193  bool debug = debug_;
194 #ifdef EDM_ML_DEBUG
195  for (int it = 0; it < (int)(chargeColl.size()); it++)
196  debug |= (chargeColl[it] > adcThreshold_MIP_);
197 #endif
198 
199  if (debug)
200  edm::LogVerbatim("BTLElectronicsSim") << "[runTrivialShaper]" << std::endl;
201 
202  //set new ADCs
203  for (int it = 0; it < (int)(chargeColl.size()); it++) {
204  BTLSample newSample;
205  newSample.set(false, false, 0, 0, 0, row, col);
206 
207  //brute force saturation, maybe could to better with an exponential like saturation
208  const uint32_t adc = std::min((uint32_t)std::floor(chargeColl[it] / adcLSB_MIP_), adcBitSaturation_);
209  const uint32_t tdc_time1 = std::min((uint32_t)std::floor(toa1[it] / toaLSB_ns_), tdcBitSaturation_);
210  const uint32_t tdc_time2 = std::min((uint32_t)std::floor(toa2[it] / toaLSB_ns_), tdcBitSaturation_);
211 
212  newSample.set(
213  chargeColl[it] > adcThreshold_MIP_, tdc_time1 == tdcBitSaturation_, tdc_time2, tdc_time1, adc, row, col);
214  dataFrame.setSample(it, newSample);
215 
216  if (debug)
217  edm::LogVerbatim("BTLElectronicsSim") << adc << " (" << chargeColl[it] << "/" << adcLSB_MIP_ << ") ";
218  }
219 
220  if (debug) {
221  std::ostringstream msg;
222  dataFrame.print(msg);
223  edm::LogVerbatim("BTLElectronicsSim") << msg.str() << std::endl;
224  }
225 }
226 
227 void BTLElectronicsSim::updateOutput(BTLDigiCollection& coll, const BTLDataFrame& rawDataFrame) const {
228  BTLDataFrame dataFrame(rawDataFrame.id());
229  dataFrame.resize(dfSIZE);
230  bool putInEvent(false);
231  for (int it = 0; it < dfSIZE; ++it) {
232  dataFrame.setSample(it, rawDataFrame[it]);
233  if (it == 0)
234  putInEvent = rawDataFrame[it].threshold();
235  }
236 
237  if (putInEvent) {
238  coll.push_back(dataFrame);
239  }
240 }
241 
242 float BTLElectronicsSim::sigma2_pe(const float& Q, const float& R) const {
243  float OneOverR = 1. / R;
244  float OneOverR2 = OneOverR * OneOverR;
245 
246  // --- This is Eq. (17) from Nucl. Instr. Meth. A 564 (2006) 185
247  float sigma2 = Q * OneOverR2 *
248  (1. + 2. * (Q + 1.) * OneOverR + (Q + 1.) * (6. * Q + 11) * OneOverR2 +
249  (Q + 1.) * (Q + 2.) * (2. * Q + 5.) * OneOverR2 * OneOverR);
250 
251  return sigma2;
252 }
253 
254 float BTLElectronicsSim::sigma_stochastic(const float& npe) const { return testBeamMIPTimeRes_ / std::sqrt(npe); }
255 
256 float BTLElectronicsSim::sigma2_DCR(const float& npe) const {
257  // trick to safely switch off the electronics contribution for resolution studies
258 
259  if (DCRxRiseTime_ == 0.) {
260  return 0.;
261  }
262  return DCRxRiseTime_ * ScintillatorDecayTime2_ / npe / npe;
263 }
264 
265 float BTLElectronicsSim::sigma2_electronics(const float npe) const {
266  // trick to safely switch off the electronics contribution for resolution studies
267 
268  if (SigmaElectronicNoise2_ == 0.) {
269  return 0.;
270  }
271  return SigmaElectronicNoise2_ * ScintillatorDecayTime2_ / npe / npe;
272 }
float sigma_stochastic(const float &npe) const
Log< level::Info, true > LogVerbatim
std::array< float, 3 > timeAtThr(const float scale, const float threshold1, const float threshold2) const
Definition: MTDShapeBase.cc:13
const float Npe_to_pC_
const float DCRxRiseTime_
const float adcThreshold_MIP_
std::array< MTDSimData_t, nSamples > MTDSimHitData
const bool smearTimeForOOTtails_
const float SigmaClock_
void updateOutput(BTLDigiCollection &coll, const BTLDataFrame &rawDataFrame) const
void push_back(T const &t)
std::unordered_map< MTDCellId, MTDCellInfo > MTDSimHitDataAccumulator
const float ScintillatorDecayTimeInv_
const float TimeThreshold2_
wrapper for a data word
Definition: BTLSample.h:13
const float adcLSB_MIP_
static constexpr int dfSIZE
static std::string const input
Definition: EdmProvDump.cc:50
const float ScintillatorDecayTime2_
float sigma2_electronics(const float npe) const
const std::vector< double > sigmaRelTOFHIRenergy_
const float toaLSB_ns_
void runTrivialShaper(BTLDataFrame &dataFrame, const mtd::MTDSimHitData &chargeColl, const mtd::MTDSimHitData &toa1, const mtd::MTDSimHitData &toa2, const uint8_t row, const uint8_t col) const
float sigma2_pe(const float &Q, const float &R) const
const BTLPulseShape btlPulseShape_
void resize(size_t s)
allow to set size
Definition: FTLDataFrameT.h:50
T sqrt(T t)
Definition: SSEVec.h:23
const D & id() const
det id
Definition: FTLDataFrameT.h:30
const uint32_t adcBitSaturation_
float sigma2_DCR(const float &npe) const
double f[11][100]
void setSample(int i, const S &sample)
Definition: FTLDataFrameT.h:57
const float SigmaElectronicNoise2_
#define debug
Definition: HDRShower.cc:19
constexpr int kInTimeBX
const float EnergyThreshold_
tuple msg
Definition: mps_check.py:286
BTLElectronicsSim(const edm::ParameterSet &pset, edm::ConsumesCollector iC)
const float SinglePhotonTimeResolution_
const uint32_t tdcBitSaturation_
const float ScintillatorDecayTime_
const float TimeThreshold1_
void set(bool thr, bool mode, uint16_t toa2, uint16_t toa, uint16_t data, uint8_t row, uint8_t col)
Definition: BTLSample.h:38
const float ChannelTimeOffset_
col
Definition: cuy.py:1009
Readout digi for HGC.
Definition: FTLDataFrameT.h:14
const float ScintillatorRiseTime_
const float testBeamMIPTimeRes_
Definition: output.py:1
const float smearChannelTimeOffset_
void print(std::ostream &out=std::cout)
Definition: FTLDataFrameT.h:61
void run(const mtd::MTDSimHitDataAccumulator &input, BTLDigiCollection &output, CLHEP::HepRandomEngine *hre) const
const float SigmaClock2_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
uint16_t *__restrict__ uint16_t const *__restrict__ adc
const float ReferencePulseNpe_
#define LogDebug(id)