-
Notifications
You must be signed in to change notification settings - Fork 0
/
psalg.h
549 lines (506 loc) · 21 KB
/
psalg.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#ifndef PSALG_PSALG_H
#define PSALG_PSALG_H
// according to the web, this is the best way to pick up
// things like uint16_t in an OS independent way (had trouble
// with RHEL5/RHEL6). we already have the boost dependency, so
// not much extra pain.
#include <boost/cstdint.hpp>
#include <list>
#include "ndarray/ndarray.h"
class psalg {
public:
/// @addtogroup psalg
/**
* @ingroup psalg
*
* @brief Data manipulation algorithms
*
* @author Matt Weaver
*/
/**
* @ingroup finite_impulse_response
*
* @brief Finite impulse response filter.
*
* Creates the 1-dimensional filtered response array from the
* sample input array and the impulse response filter array.
*
*/
static ndarray<double,1>
finite_impulse_response(const ndarray<const double,1>& filter,
const ndarray<const double,1>& sample);
/**
*
* Fills the 1-dimensional output array with the
* impulse response filter array applied to the input sample
* array.
*
*/
static void
finite_impulse_response(const ndarray<const double,1>& filter,
const ndarray<const double,1>& input,
ndarray<double,1>& output);
/**
* @ingroup Variance
*
* @brief Variance
*
* The root-mean-square is calculated by first accumulating the
* first and second moment arrays and then computing from those arrays
* with the known number of accumulations. The array shapes must all
* be equal.
*/
/*
* Accumulate the first and second moment arrays.
*/
static void variance_accumulate(const ndarray<const double,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2);
/*
* Accumulate the first and second moment arrays with a weight factor
*/
static void variance_accumulate(double wt,
const ndarray<const double,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2);
/**
* Accumulate the first and second moment arrays and calculate the root-mean-squares of all array elements
* from the accumulated n events.
*/
static void variance_calculate (double wt,
const ndarray<const double,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2,
unsigned n,
ndarray<double,2>& rms);
/*
* Accumulate the first and second moment arrays from unsigned data.
*/
static void variance_accumulate(double off,
const ndarray<const unsigned,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2);
/*
* Accumulate the first and second moment arrays with a weight factor
*/
static void variance_accumulate(double wt,
double off,
const ndarray<const unsigned,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2);
/**
* Accumulate the first and second moment arrays and calculate the root-mean-squares of all array elements
* from the accumulated n events.
*/
static void variance_calculate (double wt,
double off,
const ndarray<const unsigned,2>& input,
ndarray<double,2>& mom1,
ndarray<double,2>& mom2,
unsigned n,
ndarray<unsigned,2>& rms);
/**
* @ingroup Moments
*
* @brief Calculate moments of 1-D array
*
* The moments are { sum of bin_values,
* sum of bin_value*bin_position,
* sum of bin_value*bin_position**2 }
*
* The bin_position is calculate as bin_offset + bin_index*bin_scale
*
*/
static ndarray<double,1> moments(const ndarray<const double ,1>& input,
double bin_offset,
double bin_scale);
static ndarray<double,1> moments(const ndarray<const double ,1>& num,
const ndarray<const double ,1>& den,
double bin_offset,
double bin_scale);
/*
* @brief Calculate moments of 2-D array
*
* The moments are { sum of bins,
* sum of bin_values,
* sum of bin_values**2,
* sum of bin_value*bin_xposition,
* sum of bin_value*bin_yposition }
*
* The bin_value is calculated as the array element value minus the
* value_offset. The bin_xposition(yposition) is simply the array index
* for dimension 1(0).
*
* Integral = moments[1]
* Mean = moments[1]/moments[0]
* RMS = sqrt((moments[2]/moments[0] - (moments[1]/moments[0])**2)
* Contrast = sqrt(moments[0]*moments[2]/moments[1]**2 - 1)
* X-center-of-mass = moments[3]/moments[1]
* Y-center-of-mass = moments[4]/moments[1]
*/
static ndarray<double,1> moments(const ndarray<const unsigned,2>& input,
double value_offset);
static ndarray<double,1> moments(const ndarray<const unsigned,2>& input,
double value_offset,
unsigned bounds[][2]);
static ndarray<double,1> moments(const ndarray<const double,2>& input,
double value_offset);
static ndarray<double,1> moments(const ndarray<const double,2>& input,
double value_offset,
unsigned bounds[][2]);
/*
* Only calculate moments for elements that have a mask bit set.
* input[i][j] is used if:
* row_mask[i>>5] & (1<<(i&0x1f)), and
* mask[i][j>>5] & (1<<(j&0x1f))
*/
static ndarray<double,1> moments(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
double value_offset);
static ndarray<double,1> moments(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
double value_offset,
unsigned bounds[][2]);
static ndarray<double,1> moments(const ndarray<const double,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
double value_offset);
static ndarray<double,1> moments(const ndarray<const double,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
double value_offset,
unsigned bounds[][2]);
/*
* @brief Find extremes of 2-D array
*
* The extremes are { minimum bin value,
* maximum bin value }
*/
static ndarray<unsigned,1> extremes(const ndarray<const unsigned,2>& input);
static ndarray<unsigned,1> extremes(const ndarray<const unsigned,2>& input,
unsigned bounds[][2]);
static ndarray<double,1> extremes(const ndarray<const double,2>& input);
static ndarray<double,1> extremes(const ndarray<const double,2>& input,
unsigned bounds[][2]);
/*
* Only find extremes for elements that have a mask bit set.
* input[i][j] is used if:
* row_mask[i>>5] & (1<<(i&0x1f)), and
* mask[i][j>>5] & (1<<(j&0x1f))
*/
static ndarray<unsigned,1> extremes(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask);
static ndarray<unsigned,1> extremes(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
unsigned bounds[][2]);
static ndarray<double,1> extremes(const ndarray<const double,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask);
static ndarray<double,1> extremes(const ndarray<const double,2>& input,
const ndarray<const unsigned,1>& row_mask,
const ndarray<const unsigned,2>& mask,
unsigned bounds[][2]);
/**
* @ingroup EdgeFinder
*
* @brief Waveform pulse edge finder
*
* Generates an array of hit times and amplitudes for waveform
* leading (trailing) edges using a constant fraction discriminator
* algorithm. The baseline and minimum amplitude threshold are used
* for discriminating hits. The pulse height fraction at which the hit
* time is derived is also required as input. Note that if the threshold
* is less than the baseline value, then leading edges are "falling" and
* trailing edges are "rising". In order for two pulses to be discriminated,
* the waveform samples below the two pulses must fall below (or above for
* negative pulses) the fractional value of the threshold; i.e.
* waveform[i] < fraction*(threshold+baseline).
*
* The results are stored in a 2D array such that result[i][0] is the time
* (waveform sample) of the i'th hit and result[i][1] is the maximum amplitude
* of the i'th hit.
*
*/
static ndarray<double,2>
find_edges(const ndarray<const double,1>& waveform,
double baseline,
double threshold,
double fraction=0.5,
double deadtime=0,
bool leading_edges=true);
/**
* @ingroup HitFinder
*
* @brief Image hit finder
*
* Generates a 2D map of hits, where a hit is defined as a local maximum above
* some threshold. The threshold can be a single value or a map of values.
*
* The results are stored in a 2D array with the same dimensions as the input image.
*
*/
/*
* Increment an output element when the input element is a local maximum and is
* above threshold. Threshold is either a constant or a map of threshold values.
*/
static void count_hits(const ndarray<const unsigned,2>& input,
unsigned threshold,
ndarray<unsigned,2>& output);
static void count_hits(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,2>& threshold,
ndarray<unsigned,2>& output);
/*
* Sum the input element's value into the output element when the input is a local
* maximum and is above threshold. The value of offset is subtracted from the
* input value before adding to the output.
*/
static void sum_hits(const ndarray<const unsigned,2>& input,
unsigned threshold,
unsigned offset,
ndarray<unsigned,2>& output);
static void sum_hits(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,2>& threshold,
unsigned offset,
ndarray<unsigned,2>& output);
/*
* Increment output elements for all input elements above threshold.
* The threshold can be a single value or a map of values.
*/
static void count_excess(const ndarray<const unsigned,2>& input,
unsigned threshold,
ndarray<unsigned,2>& output);
static void count_excess(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,2>& threshold,
ndarray<unsigned,2>& output);
/*
* Sum the input element's value into the output element when the input is
* above threshold. The value of offset is subtracted from the input value
* before adding to the output.
*/
static void sum_excess(const ndarray<const unsigned,2>& input,
unsigned threshold,
unsigned offset,
ndarray<unsigned,2>& output);
static void sum_excess(const ndarray<const unsigned,2>& input,
const ndarray<const unsigned,2>& threshold,
unsigned offset,
ndarray<unsigned,2>& output);
/**
* @ingroup PeakFit
*
* @brief 1D Peak find
*
* Find the peak value in the array.
* Variable norm is number of entries summed into each bin.
*
*/
static double find_peak(const ndarray<const double,1>& input,
double norm,
const ndarray<const double,1>& baseline,
unsigned& index_peak);
static double find_peak(const ndarray<const double,1>& input,
const ndarray<const double,1>& norm,
const ndarray<const double,1>& baseline,
unsigned& index_peak);
/*
* Find up to <max_peaks> peaks in the array where each
* neighboring peak settles to <frac> of its amplitude before the
* another peak is allowed.
*/
static std::list<unsigned> find_peaks(const ndarray<const double,1>&,
double frac,
unsigned max_peaks);
/*
* @brief 1D Linear fit
*
* Variable norm is number of entries summed into each bin.
*
*/
static ndarray<double,1> line_fit(const ndarray<const double,1>& input,
const ndarray<const unsigned,1>& pos,
double norm);
static ndarray<double,1> line_fit(const ndarray<const double,1>& input,
const ndarray<const unsigned,1>& pos,
const ndarray<const double,1>& norm);
/*
* @brief Distribution Root-mean-square
*
* Width of distribution is estimated by the root-mean-square.
* A baseline polynomial { f(i) = b[0] + i*b[1] + i*i*b[2] + ... }
* is subtracted from each point prior to the rms calculation.
* Points below the baseline contribute negatively to the rms.
*/
static double dist_rms(const ndarray<const double,1>& input,
double norm,
const ndarray<const double,1>& baseline);
static double dist_rms(const ndarray<const double,1>& input,
const ndarray<const double,1>& norm,
const ndarray<const double,1>& baseline);
/*
* @brief Distribution Full-width-half-maximum
*
* Width of distribution is estimated by the minimum full-width
* half-maximum around the peak value.
*/
static double dist_fwhm(const ndarray<const double,1>& input,
double norm,
const ndarray<const double,1>& baseline);
static double dist_fwhm(const ndarray<const double,1>& input,
const ndarray<const double,1>& norm,
const ndarray<const double,1>& baseline);
/*
* @brief Parabolic interpolation
*
* Perform a quadratic interpolation around the peak of the distribution.
* A baseline polynomial { f(i) = b[0] + i*b[1] + i*i*b[2] + ... }
* is subtracted from each point prior to the calculation.
* Return value is an array of [ amplitude, position ]
*/
static ndarray<double,1> parab_interp(const ndarray<const double,1>& input,
double norm,
const ndarray<const double,1>& baseline);
static ndarray<double,1> parab_interp(const ndarray<const double,1>& input,
const ndarray<const double,1>& norm,
const ndarray<const double,1>& baseline);
/*
* Perform a least squares fit of the waveform to a 2nd-order polynomial.
* Assumes all points have equal uncertainty.
* Return value is an array of polynomial coefficients, such that
* y(x) = a[0] + a[1]*x + a[2]*x**2
* Maximum/minimum value is a[0]-a[1]*a[1]/(4*a[2]) at x=-a[1]/(2*a[2]).
* Return array is [0,0,0] when fit fails.
*/
static ndarray<double,1> parab_fit(const ndarray<const double,1>& input);
/*
* Fits the upper <frac> portion of the peak at location <x0> with
* the above routine.
*/
static ndarray<double,1> parab_fit(const ndarray<const double,1>& input,
unsigned x0,
double frac);
template <typename T>
static void commonMode(const T* data, const uint16_t* mask, const unsigned length, const T threshold, const T maxCorrection, T& cm);
template <typename T>
static void commonMode(T* data, const uint16_t* mask, const unsigned length, const T threshold, const T maxCorrection, T& cm);
template <typename T>
static void commonModeMedian(const T* data, const uint16_t* mask, const unsigned length, const T threshold, const T maxCorrection, T& cm);
template <typename T>
static void commonModeMedian(T* data, const uint16_t* mask, const unsigned length, const T threshold, const T maxCorrection, T& cm);
/**
*
* Calculate a common-mode in left-right halves for odd-even pixels
*
*/
static ndarray<const double,1> commonModeLROE(const ndarray<const int32_t,1>& a,
const ndarray<const double,1>& baseline);
/**
*
* Calculate a common-mode in left-right halves for odd-even pixels
*
*/
static ndarray<const double,2> commonModeLROE(const ndarray<const int32_t,2>& a,
const ndarray<const double,2>& baseline);
/**
* @ingroup ndarray manipulation
*
* @brief Project ndarray
*
* Creates a 1-dimensional response array from the
* projection of an N-dimensional ndarray over a region of interest (inclusive).
*
* pdim is the dimension to project onto.
* All other dimensions are integrated over the ROI
*/
static ndarray<const int,1>
project(const ndarray<const uint16_t,2>& input,
const unsigned* roi_lo,
const unsigned* roi_hi,
unsigned pedestal,
unsigned pdim);
/**
* @ingroup ndarray manipulation
*
* @brief Project ndarray
*
* Creates a 1-dimensional response array from the
* projection of an N-dimensional ndarray over a region of interest (inclusive).
*
* pdim is the dimension to project onto.
* All other dimensions are integrated over the ROI
*/
static ndarray<double,1>
project(const ndarray<const double,2>& input,
const unsigned* roi_lo,
const unsigned* roi_hi,
double pedestal,
unsigned pdim);
/**
* @ingroup ndarray manipulation
*
* @brief Project ndarray
*
* Creates a 1-dimensional response array from the
* projection of an N-dimensional ndarray over the full array.
*
* pdim is the dimension to project onto.
* All other dimensions are integrated
*/
static ndarray<const int,1>
project(const ndarray<const uint16_t,2>& input,
unsigned pedestal,
unsigned pdim);
/**
* @ingroup ndarray manipulation
*
* @brief Project ndarray
*
* Creates a 1-dimensional response array from the
* projection of an N-dimensional ndarray over the full array.
*
* pdim is the dimension to project onto.
* All other dimensions are integrated
*/
static ndarray<double,1>
project(const ndarray<const double,2>& input,
double pedestal,
unsigned pdim);
/**
* @ingroup ndarray manipulation
*
* @brief Roi ndarray
*
* Creates a 2-dimensional array from a 2-dimensional ndarray
* usin a region of interest (inclusive).
*
*/
static ndarray<const int,2>
roi(const ndarray<const uint16_t,2>& input,
const unsigned* roi_lo,
const unsigned* roi_hi,
unsigned pedestal);
/**
*
* Accumulate a rolling average where each accumulation contributes
* a fixed fraction to the average.
*
*/
template <typename I>
static void rolling_average(const ndarray<const I,1>& a,
ndarray<double,1>& avg,
double fraction);
/**
*
* Accumulate a rolling average where each accumulation contributes
* a fixed fraction to the average.
*
*/
template <typename I>
static void rolling_average(const ndarray<const I,2>& a,
ndarray<double,2>& avg,
double fraction);
};
#endif // PSALG_PSALG_H