/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/WaveformUtil.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/WaveformUtil.hh Source File
Ratpac-two
WaveformUtil.hh
1 #ifndef __RAT_WaveformUtil__
2 #define __RAT_WaveformUtil__
3 
4 #include <RtypesCore.h>
5 
6 #include <tuple>
7 #include <vector>
8 
9 namespace RAT {
10 
11 namespace WaveformUtil {
12 
13 const int INVALID = 9999;
14 
15 // converts waveform from ADC counts to voltage (mV); expects pedestal in ADC counts if given
16 std::vector<double> ADCtoVoltage(const std::vector<UShort_t>& adcWaveform, double voltageRes, double pedestal = 0);
17 
18 // Calculate baseline
19 template <typename T>
20 double CalculatePedestal(const std::vector<T>& waveform, size_t pedWindowLow, size_t pedWindowHigh) {
21  /*
22  Template: Calculate the baseline in the window between low - high samples.
23  */
24 
25  double pedestal = 0;
26 
27  if (pedWindowLow >= waveform.size()) {
28  Log::Die("WaveformUtil: Start of pedestal window must be before end of waveform.");
29  } else if (pedWindowLow >= pedWindowHigh) {
30  Log::Die("WaveformUtil: Start of pedestal window must be before end of pedestal window.");
31  } else if (pedWindowHigh > waveform.size()) {
32  Log::Die("WaveformUtil: End of pedestal window must be at most end of waveform.");
33  }
34 
35  // Ensure end of pedestal window is less than waveform size
36  pedWindowHigh = (pedWindowHigh > waveform.size()) ? waveform.size() : pedWindowHigh;
37 
38  for (size_t i = pedWindowLow; i < pedWindowHigh; i++) {
39  pedestal += waveform[i];
40  }
41  pedestal /= (pedWindowHigh - pedWindowLow);
42  return pedestal;
43 }
44 
45 // Calculate baseline in ADC counts
46 inline double CalculatePedestalADC(const std::vector<UShort_t>& waveform, int pedWindowLow, int pedWindowHigh) {
47  return CalculatePedestal<UShort_t>(waveform, pedWindowLow, pedWindowHigh);
48 };
49 
50 // Calculate baseline in mV
51 inline double CalculatePedestalmV(const std::vector<double>& waveform, int pedWindowLow, int pedWindowHigh) {
52  return CalculatePedestal<double>(waveform, pedWindowLow, pedWindowHigh);
53 };
54 
55 std::pair<int, double> FindHighestPeak(
56  const std::vector<double>& voltageWaveform); // Returns pair (peak_sample, peak_voltage)
57 
58 // Find the sample where a threshold crossing occurs before a given peak
59 int GetThresholdCrossingBeforePeak(const std::vector<double>& waveform, int peakSample, double voltageThreshold,
60  int lookBack, double timeStep);
61 
62 // Find total number of threshold crossings
63 int GetNCrossings(const std::vector<double>& waveform, double voltageThreshold);
64 
65 // Find total number of threshold crossings, time over threshold, and voltage over threshold
66 std::tuple<int, double, double> GetCrossingsInfo(
67  const std::vector<double>& waveform, double voltageThreshold,
68  double timeStep); // Returns tuple (nCrossings, time_over_threshold, voltage_over_threshold)
69 
70 // Apply a constant fraction discriminator to calculate the threshold crossing for a given peak
71 double CalculateTimeCFD(const std::vector<double>& waveform, int peakSample, int lookBack, double timeStep,
72  double constFrac = INVALID, double voltageThreshold = INVALID);
73 
74 // calculate charge (pC) from voltage (mV)
75 inline double VoltagetoCharge(double voltage, double timeStep, double termOhms) {
76  return (-voltage * timeStep) / termOhms;
77 };
78 
79 // Integrate around a peak to find charge
80 double IntegratePeak(const std::vector<double>& waveform, int peakSample, int intWindowLow, int intWindowHigh,
81  double timeStep, double termOhms);
82 
83 // Integrate waveform in sliding windows to find total charge
84 double IntegrateSliding(const std::vector<double>& waveform, int slidingWindow, double chargeThresh, double timeStep,
85  double termOhms);
86 
87 // Perform convolution using Fast Fourier Transform (FFT).
88 // Output of vectors of length and N and M have length N + M - 1.
89 // Equivalent to scipy.signal.fftconvolve(mode="full")
90 std::vector<double> ConvolveFFT(const std::vector<double>& a, const std::vector<double>& b, double dt = 1.0);
91 
92 // Find local maxima in a waveform.
93 // @param wfm The waveform data.
94 // @param threshold The minimum value for a peak to be considered valid.
95 // @param peak_direction The direction of the peak to search for. If greater than 0, finds upward peaks; if less than 0,
96 // finds downward peaks.
97 // @return indices of the peaks in the waveform.
98 std::vector<size_t> FindPeaks(const std::vector<double>& wfm, double threshold = 0.0, int peak_direction = 1);
99 } // namespace WaveformUtil
100 
101 } // namespace RAT
102 
103 #endif
static void Die(std::string message, int return_code=1)
Definition: Log.cc:90
Definition: CCCrossSecMessenger.hh:29