Skip to content

Commit d7cc1b0

Browse files
committed
[SiPM] Major performance updates
1 parent 48a6487 commit d7cc1b0

10 files changed

+628
-705
lines changed

include/SiPMAnalogSignal.h

+8-17
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
#ifndef SIPM_SIPMSIGNAL_H
2020
#define SIPM_SIPMSIGNAL_H
2121

22-
#include <algorithm>
2322
#include <cmath>
2423
#include <cstdint>
25-
#include <iomanip>
2624
#include <iostream>
2725
#include <sstream>
2826
#include <vector>
@@ -33,27 +31,22 @@ class SiPMAnalogSignal {
3331
SiPMAnalogSignal() = default;
3432

3533
SiPMAnalogSignal(const std::vector<float>& wav, const double sampling) noexcept
36-
: m_Waveform(wav), m_Sampling(sampling){};
34+
: m_Waveform(std::move(wav)), m_Sampling(sampling) {};
3735

38-
SiPMAnalogSignal& operator=(const std::vector<float>& v) {
39-
m_Waveform = std::move(v);
40-
return *this;
41-
}
42-
43-
std::vector<float>::iterator begin() noexcept { return m_Waveform.begin(); }
44-
std::vector<float>::iterator end() noexcept { return m_Waveform.end(); }
36+
float* data() noexcept { return m_Waveform.data(); }
4537

4638
inline float& operator[](const uint32_t i) noexcept { return m_Waveform[i]; }
4739
inline float operator[](const uint32_t i) const noexcept { return m_Waveform[i]; }
4840

4941
/// @brief Returns the number of points in the waveform
5042
inline uint32_t size() const { return m_Waveform.size(); }
51-
/// @brief Resets the class to its initial state
52-
void clear() { m_Waveform.clear(); }
5343
/// @brief Returns the sampling time of the signal in ns
54-
double sampling() const { return m_Sampling; }
44+
inline double sampling() const { return m_Sampling; }
45+
/// @brief Returns the signal length in ns
46+
inline double length() const { return (double)m_Waveform.size() / m_Sampling; }
5547
/// @brief Returns the waveform in an accessible data structure
56-
std::vector<float> waveform() const { return m_Waveform; }
48+
inline const std::vector<float>& waveform() const noexcept {return m_Waveform; }
49+
5750

5851
/// @brief Returns integral of the signal
5952
double integral(const double, const double, const double) const;
@@ -72,12 +65,10 @@ class SiPMAnalogSignal {
7265
return ss.str();
7366
}
7467
friend std::ostream& operator<<(std::ostream&, const SiPMAnalogSignal&);
75-
friend class SiPMSensor;
7668

7769
private:
78-
void setSampling(const double sampling) { m_Sampling = sampling; };
7970
std::vector<float> m_Waveform;
80-
double m_Sampling = 1;
71+
double m_Sampling;
8172
} /* SiPMAnalogSignal */;
8273

8374
} /* namespace sipm */

include/SiPMHit.h

+15-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cstdint>
1717
#include <iomanip>
1818
#include <iostream>
19+
#include <memory>
1920
#include <sstream>
2021

2122
namespace sipm {
@@ -24,7 +25,7 @@ class SiPMHit {
2425
/** @enum HitType
2526
* Defines the generating process for the hit
2627
*/
27-
enum class HitType {
28+
enum class HitType : uint8_t {
2829
kPhotoelectron, ///< Hit generated by a photoelectron
2930
kDarkCount, ///< Hit generated by a dark count event
3031
kOpticalCrosstalk, ///< Hit generated by an optical crosstalk
@@ -33,9 +34,10 @@ class SiPMHit {
3334
kSlowAfterPulse ///< Hit generated by a slow afterpulse
3435
};
3536

36-
constexpr SiPMHit(const double time, const double amp, const uint32_t r, const uint32_t c,
37-
const HitType type) noexcept
38-
: m_Time(time), m_Amplitude(amp), m_Row(r), m_Col(c), m_HitType(type), m_Hash((uint64_t)r << 32 | c) {}
37+
constexpr SiPMHit(double time, float amp, uint32_t row, uint32_t col, HitType type,
38+
const SiPMHit* parentPtr = nullptr) noexcept
39+
: m_Time(time), m_Amplitude(amp), m_Row(row), m_Col(col), m_HitType(type), m_ParentPtr(parentPtr) {}
40+
3941

4042
/// @brief Comparison operator for hits
4143
/**
@@ -45,16 +47,16 @@ class SiPMHit {
4547
* arrving/generating time.
4648
*/
4749
constexpr bool operator<(const SiPMHit& rhs) const noexcept { return m_Time < rhs.m_Time; }
48-
constexpr bool operator<=(const SiPMHit& rhs) const noexcept { return m_Time <= rhs.m_Time; }
49-
constexpr bool operator>(const SiPMHit& rhs) const noexcept { return m_Time > rhs.m_Time; }
50-
constexpr bool operator>=(const SiPMHit& rhs) const noexcept { return m_Time >= rhs.m_Time; }
50+
constexpr bool operator<=(const SiPMHit& rhs) const noexcept { return !(rhs < *this); }
51+
constexpr bool operator>(const SiPMHit& rhs) const noexcept { return rhs < *this; }
52+
constexpr bool operator>=(const SiPMHit& rhs) const noexcept { return !(*this < rhs); }
5153

5254
/// @brief Operator used to check if the hit is generated in the same cell
5355
/**
5456
* Hits are considered equal if they have same row and column,
5557
* hence the same SiPM cell
5658
*/
57-
constexpr bool operator==(const SiPMHit& rhs) const noexcept { return m_Hash == rhs.m_Hash; }
59+
constexpr bool operator==(const SiPMHit& rhs) const noexcept { return m_Row == rhs.m_Row && m_Col == rhs.m_Col; }
5860

5961
/// @brief Returns hit time
6062
constexpr double time() const noexcept { return m_Time; }
@@ -63,11 +65,11 @@ class SiPMHit {
6365
/// @brief Returns column of hitted cell
6466
constexpr uint32_t col() const noexcept { return m_Col; }
6567
/// @brief Returns amplitude of the signal produced by the hit
66-
double amplitude() const noexcept { return m_Amplitude; }
67-
double& amplitude() { return m_Amplitude; }
68+
float amplitude() const noexcept { return m_Amplitude; }
69+
float& amplitude() { return m_Amplitude; }
6870
/// @brief Returns hit type to identify the hits
6971
constexpr HitType hitType() const noexcept { return m_HitType; }
70-
constexpr uint64_t hash() const noexcept { return m_Hash; }
72+
constexpr const SiPMHit* parent() const noexcept { return m_ParentPtr; }
7173

7274
friend std::ostream& operator<<(std::ostream&, const SiPMHit&);
7375
std::string toString() const {
@@ -83,11 +85,11 @@ class SiPMHit {
8385
// Once a hit is constructed only
8486
// amplitude can be changed
8587
const double m_Time;
86-
double m_Amplitude;
88+
const SiPMHit* m_ParentPtr;
89+
float m_Amplitude;
8790
const uint32_t m_Row;
8891
const uint32_t m_Col;
8992
const HitType m_HitType;
90-
const uint64_t m_Hash;
9193
};
9294

9395
inline std::ostream& operator<<(std::ostream& out, const SiPMHit& obj) {

include/SiPMProperties.h

+26-56
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
#ifndef SIPM_SIPMPROPERTIES_H
1515
#define SIPM_SIPMPROPERTIES_H
1616

17-
#include <algorithm>
1817
#include <cmath>
1918
#include <cstdint>
20-
#include <fstream>
21-
#include <iomanip>
2219
#include <iostream>
2320
#include <map>
2421
#include <sstream>
@@ -45,6 +42,8 @@ class SiPMProperties {
4542
kGaussian ///< 95% of photons have a gaussian distribution
4643
};
4744

45+
SiPMProperties();
46+
4847
/// @brief Used to read settings from a json file
4948
static SiPMProperties readSettings(const std::string&);
5049

@@ -55,13 +54,13 @@ class SiPMProperties {
5554
constexpr uint32_t pitch() const { return m_Pitch; }
5655

5756
/// @brief Returns total number of cells in the sensor
58-
constexpr uint32_t nCells() const;
57+
constexpr uint32_t nCells() const { return m_Ncells; }
5958

6059
/// @brief Returns number of cells in the side of the sensor
61-
constexpr uint32_t nSideCells() const;
60+
constexpr uint32_t nSideCells() const { return m_SideCells; }
6261

6362
/// @brief Returns total number of points in the signal
64-
constexpr uint32_t nSignalPoints() const;
63+
constexpr uint32_t nSignalPoints() const { return m_SignalPoints; }
6564

6665
/// @brief Returns @ref HitDistribution type of the sensor
6766
constexpr HitDistribution hitDistribution() const { return m_HitDistribution; }
@@ -114,7 +113,7 @@ class SiPMProperties {
114113
constexpr double apSlowFraction() const { return m_ApSlowFraction; }
115114

116115
/// @brief Returns value of cell-to-cell gain variation.
117-
constexpr double ccgv() const { return m_Ccgv; }
116+
constexpr float ccgv() const { return m_Ccgv; }
118117

119118
/// @brief Returns relative gain.
120119
constexpr double gain() const { return m_Gain; }
@@ -123,13 +122,13 @@ class SiPMProperties {
123122
constexpr double snrdB() const { return m_SnrdB; }
124123

125124
/// @brief Returns RMS of the noise.
126-
constexpr double snrLinear() const;
125+
constexpr double snrLinear() const { return m_SnrLinear; }
127126

128127
/// @brief Returns value of PDE if PdeType::kSimplePde is set.
129128
constexpr double pde() const { return m_Pde; }
130129

131130
/// @brief Returns wavelength-PDE values if PdeType::kSpectrumPde is set
132-
const std::map<double, double>& pdeSpectrum() const { return m_PdeSpectrum; }
131+
std::map<double, double> pdeSpectrum() const { return m_PdeSpectrum; }
133132

134133
/// @brief Returns type of PDE calculation used.
135134
constexpr PdeType pdeType() { return m_HasPde; }
@@ -170,11 +169,17 @@ class SiPMProperties {
170169
}
171170

172171
/// @brief Set sampling time of the signal in ns
173-
void setSampling(const double);
172+
void setSampling(const double x) {
173+
m_Sampling = x;
174+
m_SignalPoints = m_SignalLength / m_Sampling;
175+
}
174176

175177
/// @brief Set length of the signa in ns
176-
/// @parame x Signal length in ns
177-
constexpr void setSignalLength(const double x) { m_SignalLength = x; }
178+
/// @param x Signal length in ns
179+
constexpr void setSignalLength(const double x) {
180+
m_SignalLength = x;
181+
m_SignalPoints = m_SignalLength / m_Sampling;
182+
}
178183

179184
/// @brief Set rising time constant of signal @sa SiPMSensor::signalShape
180185
/// @param x Signal risign time constant in ns
@@ -220,11 +225,11 @@ class SiPMProperties {
220225

221226
/// @brief Set probability to have slow afterpulses over fast ones
222227
/// @param x Fraction of afterpulses generated using slow component
223-
constexpr void setTauApSlowFraction(const double x) { m_ApSlowFraction = x; }
228+
constexpr void setApSlowFraction(const double x) { m_ApSlowFraction = x; }
224229

225230
/// @brief Set cell-to-cell gain variation @sa m_Ccgv
226231
/// @param x Value of ccgv as a fraction of signal
227-
constexpr void setCcgv(const double x) { m_Ccgv = x; }
232+
constexpr void setCcgv(const float x) { m_Ccgv = x; }
228233

229234
/// @brief Set value for PDE (and sets @ref PdeType::kSimplePde)
230235
/// @param x Flat value of PDE to be applied
@@ -304,17 +309,17 @@ class SiPMProperties {
304309
private:
305310
double m_Size = 1;
306311
double m_Pitch = 25;
307-
mutable uint32_t m_Ncells = 0;
308-
mutable uint32_t m_SideCells = 0;
312+
uint32_t m_Ncells;
313+
uint32_t m_SideCells;
309314
HitDistribution m_HitDistribution = HitDistribution::kUniform;
310315

311316
double m_Sampling = 1;
312317
double m_SignalLength = 500;
313-
mutable uint32_t m_SignalPoints = 0;
318+
uint32_t m_SignalPoints = 0;
314319
double m_RiseTime = 1;
315320
double m_FallTimeFast = 50;
316321
double m_FallTimeSlow = 100;
317-
double m_SlowComponentFraction;
322+
double m_SlowComponentFraction = 0.2;
318323
double m_RecoveryTime = 50;
319324

320325
double m_Dcr = 200e3;
@@ -325,10 +330,10 @@ class SiPMProperties {
325330
double m_TauApFastComponent = 10;
326331
double m_TauApSlowComponent = 80;
327332
double m_ApSlowFraction = 0.5;
328-
double m_Ccgv = 0.05;
333+
float m_Ccgv = 0.05;
329334
double m_SnrdB = 30;
330-
double m_Gain = 1.0;
331-
mutable double m_SnrLinear = 0;
335+
float m_Gain = 1.0;
336+
double m_SnrLinear;
332337

333338
double m_Pde = 1;
334339
std::map<double, double> m_PdeSpectrum;
@@ -340,40 +345,5 @@ class SiPMProperties {
340345
bool m_HasAp = true;
341346
bool m_HasSlowComponent = false;
342347
};
343-
// Constexpr assumes inline
344-
345-
constexpr uint32_t SiPMProperties::nCells() const {
346-
// m_SideCells and m_Ncells are cached
347-
if ((m_SideCells == 0) || (m_Ncells == 0)) {
348-
m_SideCells = 1000 * m_Size / m_Pitch;
349-
m_Ncells = m_SideCells * m_SideCells;
350-
}
351-
return m_Ncells;
352-
}
353-
354-
constexpr uint32_t SiPMProperties::nSideCells() const {
355-
// m_SideCells and m_Ncells are cached
356-
if ((m_SideCells == 0) || (m_Ncells == 0)) {
357-
m_SideCells = 1000 * m_Size / m_Pitch;
358-
m_Ncells = m_SideCells * m_SideCells;
359-
}
360-
return m_SideCells;
361-
}
362-
363-
constexpr uint32_t SiPMProperties::nSignalPoints() const {
364-
// m_Signalpoints is cached
365-
if (m_SignalPoints == 0) {
366-
m_SignalPoints = m_SignalLength / m_Sampling;
367-
}
368-
return m_SignalPoints;
369-
}
370-
371-
constexpr double SiPMProperties::snrLinear() const {
372-
// m_SnrLinear is cached
373-
if (m_SnrLinear == 0) {
374-
m_SnrLinear = pow(10, -m_SnrdB / 20);
375-
}
376-
return m_SnrLinear;
377-
}
378348
} // namespace sipm
379349
#endif /* SIPM_SIPMPROPERTIES_H */

0 commit comments

Comments
 (0)