-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathMahiFit.h
259 lines (201 loc) · 7.23 KB
/
MahiFit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#ifndef RecoLocalCalo_HcalRecAlgos_MahiFit_HH
#define RecoLocalCalo_HcalRecAlgos_MahiFit_HH
#include <climits>
#include <utility>
#include <memory>
#include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
#include "DataFormats/HcalDetId/interface/HcalDetId.h"
#include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
#include "CalibFormats/HcalObjects/interface/HcalCoder.h"
#include "RecoLocalCalo/HcalRecAlgos/interface/EigenMatrixTypes.h"
#include "DataFormats/HcalRecHit/interface/HBHEChannelInfo.h"
#include "CalibCalorimetry/HcalAlgos/interface/HcalPulseShapes.h"
#include "CalibCalorimetry/HcalAlgos/interface/HcalTimeSlew.h"
#include "RecoLocalCalo/HcalRecAlgos/interface/PulseShapeFunctor.h"
struct MahiNnlsWorkspace {
unsigned int nPulseTot;
unsigned int tsSize = 0U;
unsigned int tsOffset;
int bxOffset;
int maxoffset;
float dt;
//holds active bunch crossings
BXVector bxs;
//holds data samples
SampleVector amplitudes;
//holds diagonal noise terms
SampleVector noiseTerms;
//holds diagonal pedestal noise terms
SampleVector pedVals;
//holds flat pedestal uncertainty
float pedVal;
float noisecorr;
//holds full covariance matrix for a pulse shape
//varied in time
std::array<SampleMatrix, MaxPVSize> pulseCovArray;
//holds matrix of pulse shape templates for each BX
SamplePulseMatrix pulseMat;
//holds matrix of pulse shape derivatives for each BX
SamplePulseMatrix pulseDerivMat;
//for FNNLS algorithm
unsigned int nP;
PulseVector ampVec;
SamplePulseMatrix invcovp;
PulseMatrix aTaMat; // A-transpose A (matrix)
PulseVector aTbVec; // A-transpose b (vector)
SampleDecompLLT covDecomp;
};
struct MahiDebugInfo {
int nSamples;
int soi;
bool use3;
float inTimeConst;
float inDarkCurrent;
float inPedAvg;
float inGain;
float inNoiseADC[MaxSVSize];
float inNoiseDC[MaxSVSize];
float inNoisePhoto[MaxSVSize];
float inPedestal[MaxSVSize];
float totalUCNoise[MaxSVSize];
float mahiEnergy;
float chiSq;
float arrivalTime;
float pedEnergy;
float ootEnergy[7];
float count[MaxSVSize];
float inputTS[MaxSVSize];
int inputTDC[MaxSVSize];
float itPulse[MaxSVSize];
float ootPulse[7][MaxSVSize];
};
class MahiFit {
public:
MahiFit();
~MahiFit() {}
void setParameters(bool iDynamicPed,
double iTS4Thresh,
double chiSqSwitch,
bool iApplyTimeSlew,
HcalTimeSlew::BiasSetting slewFlavor,
bool iCalculateArrivalTime,
int iTimeAlgo,
double iThEnergeticPulses,
double iMeanTime,
double iTimeSigmaHPD,
double iTimeSigmaSiPM,
const std::vector<int>& iActiveBXs,
int iNMaxItersMin,
int iNMaxItersNNLS,
double iDeltaChiSqThresh,
double iNnlsThresh);
void phase1Apply(const HBHEChannelInfo& channelData,
float& reconstructedEnergy,
float& soiPlusOneEnergy,
float& reconstructedTime,
bool& useTriple,
float& chi2) const;
void phase1Debug(const HBHEChannelInfo& channelData, MahiDebugInfo& mdi) const;
void doFit(std::array<float, 4>& correctedOutput, const int nbx) const;
// PulseShapeCollection is expected to be either HcalPulseShapes
// or HcalPulseShapeLookup
template <class PulseShapeCollection>
void setPulseShapeTemplate(const int pulseShapeId,
const PulseShapeCollection& ps,
const bool hasTimeInfo,
const HcalTimeSlew* hcalTimeSlewDelay,
const unsigned int nSamples,
const float gain0) {
if (hcalTimeSlewDelay != hcalTimeSlewDelay_) {
assert(hcalTimeSlewDelay);
hcalTimeSlewDelay_ = hcalTimeSlewDelay;
tsDelay1GeV_ = hcalTimeSlewDelay->delay(1.0, slewFlavor_);
}
if (pulseShapeId != currentPulseShapeId_) {
resetPulseShapeTemplate(pulseShapeId, ps, nSamples);
}
// 1 sigma time constraint
nnlsWork_.dt = hasTimeInfo ? timeSigmaSiPM_ : timeSigmaHPD_;
if (nnlsWork_.tsSize != nSamples) {
nnlsWork_.tsSize = nSamples;
nnlsWork_.amplitudes.resize(nSamples);
nnlsWork_.noiseTerms.resize(nSamples);
nnlsWork_.pedVals.resize(nSamples);
}
// threshold in GeV for ccTime
thEnergeticPulsesFC_ = thEnergeticPulses_ / gain0;
}
typedef BXVector::Index Index;
const HcalTimeSlew* hcalTimeSlewDelay_ = nullptr;
float thEnergeticPulses_;
float thEnergeticPulsesFC_;
private:
typedef std::pair<int, std::shared_ptr<FitterFuncs::PulseShapeFunctor> > ShapeWithId;
const float minimize() const;
void onePulseMinimize() const;
void updateCov(const SampleMatrix& invCovMat) const;
template <class PulseShapeCollection>
void resetPulseShapeTemplate(const int pulseShapeId, const PulseShapeCollection& ps, unsigned int /* nSamples */) {
++cntsetPulseShape_;
psfPtr_ = nullptr;
for (auto& elem : knownPulseShapes_) {
if (elem.first == pulseShapeId) {
psfPtr_ = &*elem.second;
break;
}
}
if (!psfPtr_) {
// only the pulse shape itself from PulseShapeFunctor is used for Mahi
// the uncertainty terms calculated inside PulseShapeFunctor are used for Method 2 only
auto p = std::make_shared<FitterFuncs::PulseShapeFunctor>(
ps.getShape(pulseShapeId), false, false, false, 1, 0, 0, hcal::constants::maxSamples);
knownPulseShapes_.emplace_back(pulseShapeId, p);
psfPtr_ = &*p;
}
currentPulseShapeId_ = pulseShapeId;
}
float ccTime(const float itQ) const;
void updatePulseShape(const float itQ,
FullSampleVector& pulseShape,
FullSampleVector& pulseDeriv,
FullSampleMatrix& pulseCov) const;
float calculateArrivalTime(const unsigned int iBX) const;
float calculateChiSq() const;
void nnls() const;
void resetWorkspace() const;
void nnlsUnconstrainParameter(Index idxp) const;
void nnlsConstrainParameter(Index minratioidx) const;
void solveSubmatrix(PulseMatrix& mat, PulseVector& invec, PulseVector& outvec, unsigned nP) const;
mutable MahiNnlsWorkspace nnlsWork_;
//hard coded in initializer
static constexpr int pedestalBX_ = 100;
// used to restrict returned time value to a 25 ns window centered
// on the nominal arrival time
static constexpr float timeLimit_ = 12.5f;
// Python-configurables
int timeAlgo_;
bool dynamicPed_;
float ts4Thresh_;
float chiSqSwitch_;
bool applyTimeSlew_;
HcalTimeSlew::BiasSetting slewFlavor_;
float tsDelay1GeV_ = 0.f;
float norm_ = (1.f / std::sqrt(12));
bool calculateArrivalTime_;
float meanTime_;
float timeSigmaHPD_;
float timeSigmaSiPM_;
std::vector<int> activeBXs_;
int nMaxItersMin_;
int nMaxItersNNLS_;
float deltaChiSqThresh_;
float nnlsThresh_;
unsigned int bxSizeConf_;
int bxOffsetConf_;
//for pulse shapes
int currentPulseShapeId_ = INT_MIN;
int cntsetPulseShape_ = 0;
FitterFuncs::PulseShapeFunctor* psfPtr_ = nullptr;
std::vector<ShapeWithId> knownPulseShapes_;
};
#endif