CMS 3D CMS Logo

EcalUncalibRecHitMultiFitAlgo.cc
Go to the documentation of this file.
2 
4 
7 
9  : _computeErrors(true),
10  _doPrefit(false),
11  _prefitMaxChiSq(1.0),
12  _dynamicPedestals(false),
13  _mitigateBadSamples(false),
14  _selectiveBadSampleCriteria(false),
15  _addPedestalUncertainty(0.),
16  _simplifiedNoiseModelForGainSwitch(true),
17  _gainSwitchUseMaxSample(false) {
18  _singlebx.resize(1);
19  _singlebx << 0;
20 
24 }
25 
28  const EcalPedestals::Item *aped,
29  const EcalMGPAGainRatio *aGain,
30  const SampleMatrixGainArray &noisecors,
31  const FullSampleVector &fullpulse,
32  const FullSampleMatrix &fullpulsecov,
33  const BXVector &activeBX) {
34  uint32_t flags = 0;
35 
36  const unsigned int nsample = EcalDataFrame::MAXSAMPLES;
37 
38  double maxamplitude = -std::numeric_limits<double>::max();
39  const unsigned int iSampleMax = 5;
40  const unsigned int iFullPulseMax = 9;
41 
42  double pedval = 0.;
43 
44  SampleVector amplitudes;
45  SampleGainVector gainsNoise;
46  SampleGainVector gainsPedestal;
47  SampleGainVector badSamples = SampleGainVector::Zero();
48  bool hasSaturation = dataFrame.isSaturated();
49  bool hasGainSwitch = hasSaturation || dataFrame.hasSwitchToGain6() || dataFrame.hasSwitchToGain1();
50 
51  //no dynamic pedestal in case of gain switch, since then the fit becomes too underconstrained
52  bool dynamicPedestal = _dynamicPedestals && !hasGainSwitch;
53 
54  for (unsigned int iSample = 0; iSample < nsample; iSample++) {
55  const EcalMGPASample &sample = dataFrame.sample(iSample);
56 
57  double amplitude = 0.;
58  int gainId = sample.gainId();
59 
60  double pedestal = 0.;
61  double gainratio = 1.;
62 
63  if (gainId == 0 || gainId == 3) {
64  pedestal = aped->mean_x1;
65  gainratio = aGain->gain6Over1() * aGain->gain12Over6();
66  gainsNoise[iSample] = 2;
67  gainsPedestal[iSample] = dynamicPedestal ? 2 : -1; //-1 for static pedestal
68  } else if (gainId == 1) {
69  pedestal = aped->mean_x12;
70  gainratio = 1.;
71  gainsNoise[iSample] = 0;
72  gainsPedestal[iSample] = dynamicPedestal ? 0 : -1; //-1 for static pedestal
73  } else if (gainId == 2) {
74  pedestal = aped->mean_x6;
75  gainratio = aGain->gain12Over6();
76  gainsNoise[iSample] = 1;
77  gainsPedestal[iSample] = dynamicPedestal ? 1 : -1; //-1 for static pedestals
78  }
79 
80  if (dynamicPedestal) {
81  amplitude = (double)(sample.adc()) * gainratio;
82  } else {
83  amplitude = ((double)(sample.adc()) - pedestal) * gainratio;
84  }
85 
86  if (gainId == 0) {
87  edm::LogError("EcalUncalibRecHitMultiFitAlgo")
88  << "Saturation encountered. Multifit is not intended to be used for saturated channels.";
89  //saturation
90  if (dynamicPedestal) {
91  amplitude = 4095. * gainratio;
92  } else {
93  amplitude = (4095. - pedestal) * gainratio;
94  }
95  }
96 
97  amplitudes[iSample] = amplitude;
98 
99  if (iSample == iSampleMax) {
100  maxamplitude = amplitude;
101  pedval = pedestal;
102  }
103  }
104 
105  double amplitude, amperr, chisq;
106  bool status = false;
107 
108  //special handling for gain switch, where sample before maximum is potentially affected by slew rate limitation
109  //optionally apply a stricter criteria, assuming slew rate limit is only reached in case where maximum sample has gain switched but previous sample has not
110  //option 1: use simple max-sample algorithm
111  if (hasGainSwitch && _gainSwitchUseMaxSample) {
112  double maxpulseamplitude = maxamplitude / fullpulse[iFullPulseMax];
113  EcalUncalibratedRecHit rh(dataFrame.id(), maxpulseamplitude, pedval, 0., 0., flags);
114  rh.setAmplitudeError(0.);
115  for (unsigned int ipulse = 0; ipulse < _pulsefunc.BXs().rows(); ++ipulse) {
116  int bx = _pulsefunc.BXs().coeff(ipulse);
117  if (bx != 0) {
118  rh.setOutOfTimeAmplitude(bx + 5, 0.0);
119  }
120  }
121  return rh;
122  }
123 
124  //option2: A floating negative single-sample offset is added to the fit
125  //such that the affected sample is treated only as a lower limit for the true amplitude
126  bool mitigateBadSample = _mitigateBadSamples && hasGainSwitch && iSampleMax > 0;
127  mitigateBadSample &=
128  (!_selectiveBadSampleCriteria || (gainsNoise.coeff(iSampleMax - 1) != gainsNoise.coeff(iSampleMax)));
129  if (mitigateBadSample) {
130  badSamples[iSampleMax - 1] = 1;
131  }
132 
133  //compute noise covariance matrix, which depends on the sample gains
134  SampleMatrix noisecov;
135  if (hasGainSwitch) {
136  std::array<double, 3> pedrmss = {{aped->rms_x12, aped->rms_x6, aped->rms_x1}};
137  std::array<double, 3> gainratios = {{1., aGain->gain12Over6(), aGain->gain6Over1() * aGain->gain12Over6()}};
139  int gainidxmax = gainsNoise[iSampleMax];
140  noisecov = gainratios[gainidxmax] * gainratios[gainidxmax] * pedrmss[gainidxmax] * pedrmss[gainidxmax] *
141  noisecors[gainidxmax];
142  if (!dynamicPedestal && _addPedestalUncertainty > 0.) {
143  //add fully correlated component to noise covariance to inflate pedestal uncertainty
144  noisecov += _addPedestalUncertainty * _addPedestalUncertainty * SampleMatrix::Ones();
145  }
146  } else {
147  noisecov = SampleMatrix::Zero();
148  for (unsigned int gainidx = 0; gainidx < noisecors.size(); ++gainidx) {
149  SampleGainVector mask = gainidx * SampleGainVector::Ones();
150  SampleVector pedestal = (gainsNoise.array() == mask.array()).cast<SampleVector::value_type>();
151  if (pedestal.maxCoeff() > 0.) {
152  //select out relevant components of each correlation matrix, and assume no correlation between samples with
153  //different gain
154  noisecov += gainratios[gainidx] * gainratios[gainidx] * pedrmss[gainidx] * pedrmss[gainidx] *
155  pedestal.asDiagonal() * noisecors[gainidx] * pedestal.asDiagonal();
156  if (!dynamicPedestal && _addPedestalUncertainty > 0.) {
157  //add fully correlated component to noise covariance to inflate pedestal uncertainty
158  noisecov += gainratios[gainidx] * gainratios[gainidx] * _addPedestalUncertainty * _addPedestalUncertainty *
159  pedestal.asDiagonal() * SampleMatrix::Ones() * pedestal.asDiagonal();
160  }
161  }
162  }
163  }
164  } else {
165  noisecov = aped->rms_x12 * aped->rms_x12 * noisecors[0];
166  if (!dynamicPedestal && _addPedestalUncertainty > 0.) {
167  //add fully correlated component to noise covariance to inflate pedestal uncertainty
168  noisecov += _addPedestalUncertainty * _addPedestalUncertainty * SampleMatrix::Ones();
169  }
170  }
171 
172  //optimized one-pulse fit for hlt
173  bool usePrefit = false;
174  if (_doPrefit) {
175  status =
176  _pulsefuncSingle.DoFit(amplitudes, noisecov, _singlebx, fullpulse, fullpulsecov, gainsPedestal, badSamples);
177  amplitude = status ? _pulsefuncSingle.X()[0] : 0.;
178  amperr = status ? _pulsefuncSingle.Errors()[0] : 0.;
179  chisq = _pulsefuncSingle.ChiSq();
180 
181  if (chisq < _prefitMaxChiSq) {
182  usePrefit = true;
183  }
184  }
185 
186  if (!usePrefit) {
187  if (!_computeErrors)
189  status = _pulsefunc.DoFit(amplitudes, noisecov, activeBX, fullpulse, fullpulsecov, gainsPedestal, badSamples);
190  chisq = _pulsefunc.ChiSq();
191 
192  if (!status) {
193  edm::LogWarning("EcalUncalibRecHitMultiFitAlgo::makeRecHit") << "Failed Fit" << std::endl;
194  }
195 
196  unsigned int ipulseintime = 0;
197  for (unsigned int ipulse = 0; ipulse < _pulsefunc.BXs().rows(); ++ipulse) {
198  if (_pulsefunc.BXs().coeff(ipulse) == 0) {
199  ipulseintime = ipulse;
200  break;
201  }
202  }
203 
204  amplitude = status ? _pulsefunc.X()[ipulseintime] : 0.;
205  amperr = status ? _pulsefunc.Errors()[ipulseintime] : 0.;
206  }
207 
208  double jitter = 0.;
209 
210  EcalUncalibratedRecHit rh(dataFrame.id(), amplitude, pedval, jitter, chisq, flags);
211  rh.setAmplitudeError(amperr);
212 
213  if (!usePrefit) {
214  for (unsigned int ipulse = 0; ipulse < _pulsefunc.BXs().rows(); ++ipulse) {
215  int bx = _pulsefunc.BXs().coeff(ipulse);
216  if (bx != 0 && std::abs(bx) < 100) {
217  rh.setOutOfTimeAmplitude(bx + 5, status ? _pulsefunc.X().coeff(ipulse) : 0.);
218  } else if (bx == (100 + gainsPedestal[iSampleMax])) {
219  rh.setPedestal(status ? _pulsefunc.X().coeff(ipulse) : 0.);
220  }
221  }
222  }
223 
224  return rh;
225 }
EcalDataFrame::MAXSAMPLES
static constexpr int MAXSAMPLES
Definition: EcalDataFrame.h:48
EcalUncalibratedRecHit::setAmplitudeError
void setAmplitudeError(float amplitudeerror)
Definition: EcalUncalibratedRecHit.h:41
EcalDataFrame::sample
EcalMGPASample sample(int i) const
Definition: EcalDataFrame.h:29
ecalLiteDTU::gainId
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
Definition: EcalLiteDTUSample.h:14
EcalPedestals.h
CustomPhysics_cfi.amplitude
amplitude
Definition: CustomPhysics_cfi.py:12
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
EcalDataFrame::hasSwitchToGain6
bool hasSwitchToGain6() const
Definition: EcalDataFrame.cc:17
SampleMatrixGainArray
std::array< SampleMatrix, NGains > SampleMatrixGainArray
Definition: EigenMatrixTypes.h:28
simplePhotonAnalyzer_cfi.sample
sample
Definition: simplePhotonAnalyzer_cfi.py:12
EcalUncalibRecHitMultiFitAlgo.h
mps_update.status
status
Definition: mps_update.py:69
FullSampleMatrix
Eigen::Matrix< double, FullSampleVectorSize, FullSampleVectorSize > FullSampleMatrix
Definition: EigenMatrixTypes.h:18
EcalUncalibRecHitMultiFitAlgo::EcalUncalibRecHitMultiFitAlgo
EcalUncalibRecHitMultiFitAlgo()
Definition: EcalUncalibRecHitMultiFitAlgo.cc:8
SampleMatrix
Eigen::Matrix< double, SampleVectorSize, SampleVectorSize > SampleMatrix
Definition: EigenMatrixTypes.h:17
l1GtPatternGenerator_cfi.bx
bx
Definition: l1GtPatternGenerator_cfi.py:18
EcalUncalibRecHitMultiFitAlgo::_gainSwitchUseMaxSample
bool _gainSwitchUseMaxSample
Definition: EcalUncalibRecHitMultiFitAlgo.h:52
PulseChiSqSNNLS::X
const PulseVector & X() const
Definition: PulseChiSqSNNLS.h:28
EcalUncalibRecHitMultiFitAlgo::_mitigateBadSamples
bool _mitigateBadSamples
Definition: EcalUncalibRecHitMultiFitAlgo.h:48
SampleGainVector
Eigen::Matrix< char, SampleVectorSize, 1 > SampleGainVector
Definition: EigenMatrixTypes.h:16
EcalDataFrame
Definition: EcalDataFrame.h:16
EcalUncalibRecHitMultiFitAlgo::_simplifiedNoiseModelForGainSwitch
bool _simplifiedNoiseModelForGainSwitch
Definition: EcalUncalibRecHitMultiFitAlgo.h:51
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
BXVector
Definition: BXVector.h:15
PulseChiSqSNNLS::setMaxIters
void setMaxIters(int n)
Definition: PulseChiSqSNNLS.h:34
EcalUncalibRecHitMultiFitAlgo::_computeErrors
bool _computeErrors
Definition: EcalUncalibRecHitMultiFitAlgo.h:44
PulseChiSqSNNLS::DoFit
bool DoFit(const SampleVector &samples, const SampleMatrix &samplecov, const BXVector &bxs, const FullSampleVector &fullpulse, const FullSampleMatrix &fullpulsecov, const SampleGainVector &gains=-1 *SampleGainVector::Ones(), const SampleGainVector &badSamples=SampleGainVector::Zero())
Definition: PulseChiSqSNNLS.cc:59
EcalUncalibRecHitMultiFitAlgo::makeRecHit
EcalUncalibratedRecHit makeRecHit(const EcalDataFrame &dataFrame, const EcalPedestals::Item *aped, const EcalMGPAGainRatio *aGain, const SampleMatrixGainArray &noisecors, const FullSampleVector &fullpulse, const FullSampleMatrix &fullpulsecov, const BXVector &activeBX)
compute rechits
Definition: EcalUncalibRecHitMultiFitAlgo.cc:27
EcalMGPAGainRatio::gain12Over6
float gain12Over6() const
Definition: EcalMGPAGainRatio.h:19
PulseChiSqSNNLS::BXs
const BXVector & BXs() const
Definition: PulseChiSqSNNLS.h:30
EcalDataFrame::id
DetId id() const
Definition: EcalDataFrame.h:24
EcalUncalibRecHitMultiFitAlgo::_singlebx
BXVector _singlebx
Definition: EcalUncalibRecHitMultiFitAlgo.h:53
EcalUncalibRecHitMultiFitAlgo::_addPedestalUncertainty
double _addPedestalUncertainty
Definition: EcalUncalibRecHitMultiFitAlgo.h:50
funct::true
true
Definition: Factorize.h:173
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
EcalCondObjectContainer::Item
T Item
Definition: EcalCondObjectContainer.h:15
EcalUncalibRecHitMultiFitAlgo::_selectiveBadSampleCriteria
bool _selectiveBadSampleCriteria
Definition: EcalUncalibRecHitMultiFitAlgo.h:49
EcalDataFrame::isSaturated
bool isSaturated() const
Definition: EcalDataFrame.h:38
EcalDataFrame::hasSwitchToGain1
bool hasSwitchToGain1() const
Definition: EcalDataFrame.cc:25
EcalCondDBWriter_cfi.pedestal
pedestal
Definition: EcalCondDBWriter_cfi.py:49
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
EcalUncalibRecHitMultiFitAlgo::_pulsefunc
PulseChiSqSNNLS _pulsefunc
Definition: EcalUncalibRecHitMultiFitAlgo.h:42
EcalGainRatios.h
SampleVector
Eigen::Matrix< double, SampleVectorSize, 1 > SampleVector
Definition: EigenMatrixTypes.h:12
BXVector::resize
void resize(int bx, unsigned size)
EcalMGPASample
Definition: EcalMGPASample.h:22
FullSampleVector
Eigen::Matrix< double, FullSampleVectorSize, 1 > FullSampleVector
Definition: EigenMatrixTypes.h:13
EcalUncalibratedRecHit
Definition: EcalUncalibratedRecHit.h:8
PulseChiSqSNNLS::ChiSq
double ChiSq() const
Definition: PulseChiSqSNNLS.h:32
EcalMGPAGainRatio::gain6Over1
float gain6Over1() const
Definition: EcalMGPAGainRatio.h:20
EcalUncalibRecHitMultiFitAlgo::_dynamicPedestals
bool _dynamicPedestals
Definition: EcalUncalibRecHitMultiFitAlgo.h:47
EcalUncalibRecHitMultiFitAlgo::_doPrefit
bool _doPrefit
Definition: EcalUncalibRecHitMultiFitAlgo.h:45
PulseChiSqSNNLS::disableErrorCalculation
void disableErrorCalculation()
Definition: PulseChiSqSNNLS.h:33
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
EcalUncalibRecHitMultiFitAlgo::_prefitMaxChiSq
double _prefitMaxChiSq
Definition: EcalUncalibRecHitMultiFitAlgo.h:46
PulseChiSqSNNLS::Errors
const PulseVector & Errors() const
Definition: PulseChiSqSNNLS.h:29
PulseChiSqSNNLS::setMaxIterWarnings
void setMaxIterWarnings(bool b)
Definition: PulseChiSqSNNLS.h:35
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13216
EcalUncalibRecHitMultiFitAlgo::_pulsefuncSingle
PulseChiSqSNNLS _pulsefuncSingle
Definition: EcalUncalibRecHitMultiFitAlgo.h:43
EcalMGPAGainRatio
Definition: EcalMGPAGainRatio.h:13