CMS 3D CMS Logo

EcalLiteDTUCoder.cc
Go to the documentation of this file.
6 
7 #include <iostream>
8 
10  bool PreMix1,
11  EcalLiteDTUCoder::Noisifier* ebCorrNoise0,
12  EcalLiteDTUCoder::Noisifier* ebCorrNoise1)
13  : m_peds(nullptr),
14  m_gainRatios(0),
15  m_intercals(nullptr),
16  m_maxEneEB(ecalPh2::maxEneEB), // Maximum for CATIA: LSB gain 10: 0.048 MeV
17  m_addNoise(addNoise),
18  m_PreMix1(PreMix1),
19  m_ebCorrNoise{ebCorrNoise0, ebCorrNoise1}
20 
21 {}
22 
24 
26 
27 void EcalLiteDTUCoder::setPedestals(const EcalLiteDTUPedestalsMap* pedestals) { m_peds = pedestals; }
28 
29 void EcalLiteDTUCoder::setGainRatios(float gainRatios) { m_gainRatios = gainRatios; }
30 
32 
33 double EcalLiteDTUCoder::fullScaleEnergy(const DetId& detId) const { return m_maxEneEB; }
34 
35 void EcalLiteDTUCoder::analogToDigital(CLHEP::HepRandomEngine* engine,
36  const EcalSamples& clf,
37  EcalDataFrame_Ph2& df) const {
38  df.setSize(clf.size());
39  encode(clf, df, engine);
40 }
41 
42 void EcalLiteDTUCoder::encode(const EcalSamples& ecalSamples,
44  CLHEP::HepRandomEngine* engine) const {
45  const int nSamples(ecalSamples.size());
46 
47  DetId detId = ecalSamples.id();
48  double Emax = fullScaleEnergy(detId);
49 
50  //N Gains set to 2 in the .h
51  double pedestals[ecalPh2::NGAINS];
52  double widths[ecalPh2::NGAINS];
53  double LSB[ecalPh2::NGAINS];
54  double trueRMS[ecalPh2::NGAINS];
55  int nSaturatedSamples = 0;
56  double icalconst = 1.;
57  findIntercalibConstant(detId, icalconst);
58 
59  for (unsigned int igain(0); igain < ecalPh2::NGAINS; ++igain) {
60  // fill in the pedestal and width
61  findPedestal(detId, igain, pedestals[igain], widths[igain]);
62  // insert an absolute value in the trueRMS
63  trueRMS[igain] = std::sqrt(std::abs(widths[igain] * widths[igain] - 1. / 12.));
64 
65  LSB[igain] = Emax / (ecalPh2::MAXADC * ecalPh2::gains[igain]);
66  }
67 
68  CaloSamples noiseframe[ecalPh2::NGAINS] = {
69  CaloSamples(detId, nSamples),
70  CaloSamples(detId, nSamples),
71  };
72 
73  const Noisifier* noisy[ecalPh2::NGAINS] = {m_ebCorrNoise[0], m_ebCorrNoise[1]};
74 
75  if (m_addNoise) {
76  for (unsigned int ig = 0; ig < ecalPh2::NGAINS; ++ig) {
77  noisy[ig]->noisify(noiseframe[ig], engine);
78  }
79  }
80 
81  std::vector<std::vector<int>> adctrace(nSamples);
82  int firstSaturatedSample[ecalPh2::NGAINS] = {0, 0};
83 
84  for (int i(0); i != nSamples; ++i)
85  adctrace[i].resize(ecalPh2::NGAINS);
86 
87  for (unsigned int igain = 0; igain < ecalPh2::NGAINS; ++igain) {
88  for (int i(0); i != nSamples; ++i) {
89  adctrace[i][igain] = -1;
90  }
91  }
92 
93  // fill ADC trace in gain 0 (x10) and gain 1 (x1)
94  for (unsigned int igain = 0; igain < ecalPh2::NGAINS; ++igain) {
95  for (int i(0); i != nSamples; ++i) {
96  double asignal = 0;
97  if (!m_PreMix1) {
98  asignal = pedestals[igain] + ecalSamples[i] / (LSB[igain] * icalconst) + trueRMS[igain] * noiseframe[igain][i];
99  //Analog signal value for each sample in ADC.
100  //It is corrected by the intercalibration constants
101 
102  } else {
103  // no noise nor pedestal when premixing
104  asignal = ecalSamples[i] / (LSB[igain] * icalconst);
105  }
106  int isignal = asignal;
107 
108  unsigned int adc = asignal - (double)isignal < 0.5 ? isignal : isignal + 1;
109  // gain 0 (x10) channel is saturated, readout will use gain 1 (x10), but I count the number of saturated samples
110  if (adc > ecalPh2::MAXADC) {
112  if (nSaturatedSamples == 0)
113  firstSaturatedSample[igain] = i;
114  nSaturatedSamples++;
115  }
116  adctrace[i][igain] = adc;
117  }
118  if (nSaturatedSamples == 0) {
119  break; // gain 0 (x10) is not saturated, so don't bother with gain 1
120  }
121  } // for igain
122 
123  int igain = 0;
124 
125  //Limits of gain 1:
126  //The Lite DTU sends 5 samples before the saturating one, and 10 after with gain 1.
127  //we put the maximum in bin 5, but could happen that the system saturates before.
128 
129  int previousSaturatedSamples = 5;
130  int nextSaturatedSamples = 10;
131  int startingLowerGainSample = 0;
132  int endingLowerGainSample = (firstSaturatedSample[0] + nextSaturatedSamples + (nSaturatedSamples));
133 
134  if (nSaturatedSamples != 0 and (firstSaturatedSample[0] - previousSaturatedSamples) < 0) {
135  startingLowerGainSample = 0;
136  } else {
137  startingLowerGainSample = (firstSaturatedSample[0] - previousSaturatedSamples);
138  }
139 
140  //Setting values to the samples:
141  for (int j = 0; j < nSamples; ++j) {
142  if (nSaturatedSamples != 0 and j >= startingLowerGainSample and j < endingLowerGainSample) {
143  igain = 1;
144  } else {
145  igain = 0;
146  }
147  df.setSample(j, EcalLiteDTUSample(adctrace[j][igain], igain));
148  }
149 }
150 
151 void EcalLiteDTUCoder::findPedestal(const DetId& detId, int gainId, double& ped, double& width) const {
153  if (itped != m_peds->getMap().end()) {
154  ped = (*itped).mean(gainId);
155  width = (*itped).rms(gainId);
156  LogDebug("EcalLiteDTUCoder") << "Pedestals for " << detId.rawId() << " gain range " << gainId << " : \n"
157  << "Mean = " << ped << " rms = " << width;
158  } else {
159  LogDebug("EcalLiteDTUCoder") << "Pedestals not found, put default values (ped: 12; width: 2.5) \n";
160  ped = 12.;
161  width = 2.5;
162  }
163 }
164 
165 void EcalLiteDTUCoder::findIntercalibConstant(const DetId& detId, double& icalconst) const {
166  EcalIntercalibConstantMC thisconst = 1.;
167  // find intercalib constant for this xtal
168  const EcalIntercalibConstantMCMap& icalMap = m_intercals->getMap();
169  EcalIntercalibConstantMCMap::const_iterator icalit = icalMap.find(detId);
170  if (icalit != icalMap.end()) {
171  thisconst = (*icalit);
172  } else {
173  LogDebug("EcalLiteDTUCoder") << "Intercalib Constant not found, put default value \n";
174  thisconst = 1.;
175  }
176 
177  if (icalconst == 0.)
178  thisconst = 1.;
179 
180  icalconst = thisconst;
181 }
const EcalLiteDTUPedestalsMap * m_peds
adds noise to the given frame.
Definition: EcalCoder.h:12
static constexpr unsigned int NGAINS
Definition: EcalConstants.h:23
void encode(const EcalSamples &ecalSamples, EcalDataFrame_Ph2 &df, CLHEP::HepRandomEngine *) const
produce the pulse-shape
void setIntercalibConstants(const EcalIntercalibConstantsMC *ical)
void noisify(T &frame, CLHEP::HepRandomEngine *, const VecDou *rangau=nullptr) const
static constexpr double maxEneEB
Definition: EcalConstants.h:31
const Noisifier * m_ebCorrNoise[ecalPh2::NGAINS]
DetId id() const
double fullScaleEnergy(const DetId &did) const
limit on the energy scale due to the electronics range
void setFullScaleEnergy(double EBscale)
T sqrt(T t)
Definition: SSEVec.h:19
void setGainRatios(float gainRatios)
const EcalIntercalibConstantsMC * m_intercals
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static constexpr const float * gains
Definition: EcalConstants.h:24
const_iterator find(uint32_t rawId) const
Definition: DetId.h:17
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void findPedestal(const DetId &detId, int gainId, double &pedestal, double &width) const
std::vector< Item >::const_iterator const_iterator
void findIntercalibConstant(const DetId &detId, double &icalconst) const
virtual void analogToDigital(CLHEP::HepRandomEngine *, const EcalSamples &clf, EcalDataFrame_Ph2 &df) const
from EcalSamples to EcalDataFrame_Ph2
virtual ~EcalLiteDTUCoder()
dtor
const_iterator end() const
float EcalIntercalibConstantMC
void setPedestals(const EcalLiteDTUPedestalsMap *pedestals)
can be fetched every event from the EventSetup
EcalLiteDTUCoder(bool addNoise, bool PreMix1, Noisifier *ebCorrNoise0, Noisifier *ebCorrNoise1=nullptr)
ctor
static constexpr unsigned int MAXADC
Definition: EcalConstants.h:29
uint16_t *__restrict__ uint16_t const *__restrict__ adc
uint32_t size() const
#define LogDebug(id)