CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 using namespace mtd;
10 
12  : debug_(pset.getUntrackedParameter<bool>("debug", false)),
13  bxTime_(pset.getParameter<double>("bxTime")),
14  testBeamMIPTimeRes_(pset.getParameter<double>("TestBeamMIPTimeRes")),
15  ScintillatorRiseTime_(pset.getParameter<double>("ScintillatorRiseTime")),
16  ScintillatorDecayTime_(pset.getParameter<double>("ScintillatorDecayTime")),
17  ChannelTimeOffset_(pset.getParameter<double>("ChannelTimeOffset")),
18  smearChannelTimeOffset_(pset.getParameter<double>("smearChannelTimeOffset")),
19  EnergyThreshold_(pset.getParameter<double>("EnergyThreshold")),
20  TimeThreshold1_(pset.getParameter<double>("TimeThreshold1")),
21  TimeThreshold2_(pset.getParameter<double>("TimeThreshold2")),
22  ReferencePulseNpe_(pset.getParameter<double>("ReferencePulseNpe")),
23  SinglePhotonTimeResolution_(pset.getParameter<double>("SinglePhotonTimeResolution")),
24  DarkCountRate_(pset.getParameter<double>("DarkCountRate")),
25  SigmaElectronicNoise_(pset.getParameter<double>("SigmaElectronicNoise")),
26  SigmaClock_(pset.getParameter<double>("SigmaClock")),
27  smearTimeForOOTtails_(pset.getParameter<bool>("SmearTimeForOOTtails")),
28  Npe_to_pC_(pset.getParameter<double>("Npe_to_pC")),
29  Npe_to_V_(pset.getParameter<double>("Npe_to_V")),
30  adcNbits_(pset.getParameter<uint32_t>("adcNbits")),
31  tdcNbits_(pset.getParameter<uint32_t>("tdcNbits")),
32  adcSaturation_MIP_(pset.getParameter<double>("adcSaturation_MIP")),
33  adcBitSaturation_(std::pow(2, adcNbits_) - 1),
34  adcLSB_MIP_(adcSaturation_MIP_ / adcBitSaturation_),
35  adcThreshold_MIP_(pset.getParameter<double>("adcThreshold_MIP")),
36  toaLSB_ns_(pset.getParameter<double>("toaLSB_ns")),
37  tdcBitSaturation_(std::pow(2, tdcNbits_) - 1),
38  CorrCoeff_(pset.getParameter<double>("CorrelationCoefficient")),
39  cosPhi_(0.5 * (sqrt(1. + CorrCoeff_) + sqrt(1. - CorrCoeff_))),
40  sinPhi_(0.5 * CorrCoeff_ / cosPhi_),
41  ScintillatorDecayTime2_(ScintillatorDecayTime_ * ScintillatorDecayTime_),
42  ScintillatorDecayTimeInv_(1. / ScintillatorDecayTime_),
43  SPTR2_(SinglePhotonTimeResolution_ * SinglePhotonTimeResolution_),
44  DCRxRiseTime_(DarkCountRate_ * ScintillatorRiseTime_),
45  SigmaElectronicNoise2_(SigmaElectronicNoise_ * SigmaElectronicNoise_),
46  SigmaClock2_(SigmaClock_ * SigmaClock_) {}
47 
50  CLHEP::HepRandomEngine* hre) const {
51  MTDSimHitData chargeColl, toa1, toa2;
52 
53  for (MTDSimHitDataAccumulator::const_iterator it = input.begin(); it != input.end(); it++) {
54  // --- Digitize only the in-time bucket:
55  const unsigned int iBX = mtd_digitizer::kInTimeBX;
56 
57  chargeColl.fill(0.f);
58  toa1.fill(0.f);
59  toa2.fill(0.f);
60  for (size_t iside = 0; iside < 2; iside++) {
61  // --- Fluctuate the total number of photo-electrons
62  float Npe = CLHEP::RandPoissonQ::shoot(hre, (it->second).hit_info[2 * iside][iBX]);
63  if (Npe < EnergyThreshold_)
64  continue;
65 
66  // --- Get the time of arrival and add a channel time offset
67  float finalToA1 = (it->second).hit_info[1 + 2 * iside][iBX] + ChannelTimeOffset_;
68 
69  if (smearChannelTimeOffset_ > 0.) {
70  float timeSmearing = CLHEP::RandGaussQ::shoot(hre, 0., smearChannelTimeOffset_);
71  finalToA1 += timeSmearing;
72  }
73 
74  // --- Calculate and add the time walk: the time of arrival is read in correspondence
75  // with two thresholds on the signal pulse
76  std::array<float, 3> times =
78 
79  // --- If the pulse amplitude is smaller than TimeThreshold2, the trigger does not fire
80  if (times[1] == 0.)
81  continue;
82 
83  float finalToA2 = finalToA1 + times[1];
84  finalToA1 += times[0];
85 
86  // --- Estimate the time uncertainty due to photons from earlier OOT hits in the current BTL cell
88  float rate_oot = 0.;
89  // Loop on earlier OOT hits
90  for (int ibx = 0; ibx < mtd_digitizer::kInTimeBX; ++ibx) {
91  if ((it->second).hit_info[2 * iside][ibx] > 0.) {
92  float hit_time = (it->second).hit_info[1 + 2 * iside][ibx] + bxTime_ * (ibx - mtd_digitizer::kInTimeBX);
93  float npe_oot = CLHEP::RandPoissonQ::shoot(hre, (it->second).hit_info[2 * iside][ibx]);
94  rate_oot += npe_oot * exp(hit_time * ScintillatorDecayTimeInv_) * ScintillatorDecayTimeInv_;
95  }
96  } // ibx loop
97 
98  if (rate_oot > 0.) {
99  float sigma_oot = sqrt(rate_oot * ScintillatorRiseTime_) * ScintillatorDecayTime_ / Npe;
100  float smearing_oot = CLHEP::RandGaussQ::shoot(hre, 0., sigma_oot);
101  finalToA1 += smearing_oot;
102  finalToA2 += smearing_oot;
103  }
104  } // if smearTimeForOOTtails_
105 
106  // --- Uncertainty due to the fluctuations of the n-th photon arrival time:
107  if (testBeamMIPTimeRes_ > 0.) {
108  // In this case the time resolution is parametrized from the testbeam.
109  // The same parameterization is used for both thresholds.
110  float sigma = testBeamMIPTimeRes_ / sqrt(Npe);
111  float smearing_stat_thr1 = CLHEP::RandGaussQ::shoot(hre, 0., sigma);
112  float smearing_stat_thr2 = CLHEP::RandGaussQ::shoot(hre, 0., sigma);
113 
114  finalToA1 += smearing_stat_thr1;
115  finalToA2 += smearing_stat_thr2;
116 
117  } else {
118  // In this case the time resolution is taken from the literature.
119  // The fluctuations due to the first TimeThreshold1_ p.e. are common to both times
120  float smearing_stat_thr1 =
121  CLHEP::RandGaussQ::shoot(hre, 0., ScintillatorDecayTime_ * sqrt(sigma2_pe(TimeThreshold1_, Npe)));
122  float smearing_stat_thr2 = CLHEP::RandGaussQ::shoot(
124  finalToA1 += smearing_stat_thr1;
125  finalToA2 += smearing_stat_thr1 + smearing_stat_thr2;
126  }
127 
128  // --- Add in quadrature the uncertainties due to the SiPM timing resolution, the SiPM DCR,
129  // the electronic noise and the clock distribution:
130  float slew2 = ScintillatorDecayTime2_ / Npe / Npe;
131 
132  float sigma2_tot_thr1 =
134  float sigma2_tot_thr2 =
136 
137  // --- Smear the arrival times using the correlated uncertainties:
138  float smearing_thr1_uncorr = CLHEP::RandGaussQ::shoot(hre, 0., sqrt(sigma2_tot_thr1));
139  float smearing_thr2_uncorr = CLHEP::RandGaussQ::shoot(hre, 0., sqrt(sigma2_tot_thr2));
140 
141  finalToA1 += cosPhi_ * smearing_thr1_uncorr + sinPhi_ * smearing_thr2_uncorr;
142  finalToA2 += sinPhi_ * smearing_thr1_uncorr + cosPhi_ * smearing_thr2_uncorr;
143 
144  chargeColl[iside] = Npe * Npe_to_pC_; // the p.e. number is here converted to pC
145 
146  toa1[iside] = finalToA1;
147  toa2[iside] = finalToA2;
148 
149  } // iside loop
150 
151  //run the shaper to create a new data frame
152  BTLDataFrame rawDataFrame(it->first.detid_);
153  runTrivialShaper(rawDataFrame, chargeColl, toa1, toa2, it->first.row_, it->first.column_);
154  updateOutput(output, rawDataFrame);
155 
156  } // MTDSimHitDataAccumulator loop
157 }
158 
160  const mtd::MTDSimHitData& chargeColl,
161  const mtd::MTDSimHitData& toa1,
162  const mtd::MTDSimHitData& toa2,
163  const uint8_t row,
164  const uint8_t col) const {
165  bool debug = debug_;
166 #ifdef EDM_ML_DEBUG
167  for (int it = 0; it < (int)(chargeColl.size()); it++)
168  debug |= (chargeColl[it] > adcThreshold_MIP_);
169 #endif
170 
171  if (debug)
172  edm::LogVerbatim("BTLElectronicsSim") << "[runTrivialShaper]" << std::endl;
173 
174  //set new ADCs
175  for (int it = 0; it < (int)(chargeColl.size()); it++) {
176  BTLSample newSample;
177  newSample.set(false, false, 0, 0, 0, row, col);
178 
179  //brute force saturation, maybe could to better with an exponential like saturation
180  const uint32_t adc = std::min((uint32_t)std::floor(chargeColl[it] / adcLSB_MIP_), adcBitSaturation_);
181  const uint32_t tdc_time1 = std::min((uint32_t)std::floor(toa1[it] / toaLSB_ns_), tdcBitSaturation_);
182  const uint32_t tdc_time2 = std::min((uint32_t)std::floor(toa2[it] / toaLSB_ns_), tdcBitSaturation_);
183 
184  newSample.set(
185  chargeColl[it] > adcThreshold_MIP_, tdc_time1 == tdcBitSaturation_, tdc_time2, tdc_time1, adc, row, col);
186  dataFrame.setSample(it, newSample);
187 
188  if (debug)
189  edm::LogVerbatim("BTLElectronicsSim") << adc << " (" << chargeColl[it] << "/" << adcLSB_MIP_ << ") ";
190  }
191 
192  if (debug) {
193  std::ostringstream msg;
194  dataFrame.print(msg);
195  edm::LogVerbatim("BTLElectronicsSim") << msg.str() << std::endl;
196  }
197 }
198 
199 void BTLElectronicsSim::updateOutput(BTLDigiCollection& coll, const BTLDataFrame& rawDataFrame) const {
200  BTLDataFrame dataFrame(rawDataFrame.id());
201  dataFrame.resize(dfSIZE);
202  bool putInEvent(false);
203  for (int it = 0; it < dfSIZE; ++it) {
204  dataFrame.setSample(it, rawDataFrame[it]);
205  if (it == 0)
206  putInEvent = rawDataFrame[it].threshold();
207  }
208 
209  if (putInEvent) {
210  coll.push_back(dataFrame);
211  }
212 }
213 
214 float BTLElectronicsSim::sigma2_pe(const float& Q, const float& R) const {
215  float OneOverR = 1. / R;
216  float OneOverR2 = OneOverR * OneOverR;
217 
218  // --- This is Eq. (17) from Nucl. Instr. Meth. A 564 (2006) 185
219  float sigma2 = Q * OneOverR2 *
220  (1. + 2. * (Q + 1.) * OneOverR + (Q + 1.) * (6. * Q + 11) * OneOverR2 +
221  (Q + 1.) * (Q + 2.) * (2. * Q + 5.) * OneOverR2 * OneOverR);
222 
223  return sigma2;
224 }
Log< level::Info, true > LogVerbatim
const float Npe_to_pC_
const float DCRxRiseTime_
const float adcThreshold_MIP_
std::array< MTDSimData_t, nSamples > MTDSimHitData
const bool smearTimeForOOTtails_
float sigma2_pe(const float &Q, const float &R) const
void push_back(T const &t)
std::unordered_map< MTDCellId, MTDCellInfo > MTDSimHitDataAccumulator
const float ScintillatorDecayTimeInv_
Exp< T >::type exp(const T &t)
Definition: Exp.h:22
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:47
void updateOutput(BTLDigiCollection &coll, const BTLDataFrame &rawDataFrame) const
const float ScintillatorDecayTime2_
const float toaLSB_ns_
const BTLPulseShape btlPulseShape_
void resize(size_t s)
allow to set size
Definition: FTLDataFrameT.h:51
T sqrt(T t)
Definition: SSEVec.h:19
void run(const mtd::MTDSimHitDataAccumulator &input, BTLDigiCollection &output, CLHEP::HepRandomEngine *hre) const
const uint32_t adcBitSaturation_
void setSample(int i, const S &sample)
Definition: FTLDataFrameT.h:58
T min(T a, T b)
Definition: MathUtil.h:58
const float SigmaElectronicNoise2_
#define debug
Definition: HDRShower.cc:19
constexpr int kInTimeBX
const float EnergyThreshold_
tuple msg
Definition: mps_check.py:285
BTLElectronicsSim(const edm::ParameterSet &pset, edm::ConsumesCollector iC)
const uint32_t tdcBitSaturation_
std::array< float, 3 > timeAtThr(const float scale, const float threshold1, const float threshold2) const
Definition: MTDShapeBase.cc:9
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:37
const float ChannelTimeOffset_
Readout digi for HGC.
Definition: FTLDataFrameT.h:14
const float ScintillatorRiseTime_
const float testBeamMIPTimeRes_
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
const float smearChannelTimeOffset_
void print(std::ostream &out=std::cout)
Definition: FTLDataFrameT.h:62
int col
Definition: cuy.py:1009
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_
const D & id() const
det id
Definition: FTLDataFrameT.h:31