Belle II Software development
SVDSimulationTools.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10#ifndef _SVD_SIMULATION_SIMULATIONTOOLS_H
11#define _SVD_SIMULATION_SIMULATIONTOOLS_H
12
13#include <cmath>
14#include <array>
15#include <algorithm>
16#include <functional>
17#include <sstream>
18#include <framework/gearbox/Unit.h>
19#include <framework/utilities/MathHelpers.h>
20
21namespace Belle2 {
26 namespace SVD {
27
28// ==============================================================================
29// APV25 sample data
30// ------------------------------------------------------------------------------
31// Constants:
33 const std::size_t nAPVSamples = 6;
34
36 const double dt_APV = 31.44 * Unit::ns;
37
39 typedef double apvSampleBaseType;
40 typedef std::array<apvSampleBaseType, nAPVSamples> apvSamples;
41
44 {{ -dt_APV, 0.0, dt_APV, 2 * dt_APV, 3 * dt_APV, 4 * dt_APV}};
45
46// ==============================================================================
47// APV25 waveform shapes
48// ------------------------------------------------------------------------------
49
55 typedef std::function<double(double)> WaveformShape;
56
62 inline double w_expo(double t)
63 {
64 if (t < 0.0) return 0.0;
65 else return t * exp(1.0 - t);
66 }
67
74 inline double w_poly3(double t)
75 {
76 if (t < 0.0 || t > 1.0)
77 return 0.0;
78 else
79 return 6.75 * t * (1.0 - t) * (1.0 - t);
80 }
81
87 inline double w_betaprime(double t)
88 {
89 if (t < 0.0)
90 return 0.0;
91 else {
92 // Replace pow(1.+t, -10) by a division and a sequential calculation of the tenth power for improved execution time
93 return 149.012 * square(t) / (pow4(1.0 + t) * pow4(1.0 + t) * square(1.0 + t));
94 }
95 }
96
102 inline double w_adjacentU(double t)
103 {
104
105 double p[8];
106
107 // waveform parameters
108 p[0] = -7.379;
109 p[1] = -0.3594;
110 p[2] = 0.07564;
111 p[3] = 7.391;
112 p[4] = -0.08004;
113 p[5] = -2.139e-5;
114 p[6] = -1.781e-7;
115 p[7] = 0.01827;
116
117 if (t < 0.0)
118 return 0.0;
119 else
120 return (p[0] + p[1] * t) * exp(-p[2] * t) + (p[3] + p[4] * t + p[5] * square(t) + p[6] * cube(t)) * exp(-p[7] * t);
121 }
122
128 inline double w_adjacentV(double t)
129 {
130
131 double p[8];
132
133 // waveform parameters
134 p[0] = 0.2043;
135 p[1] = -0.0197;
136 p[2] = 0.01448;
137 p[3] = -0.2076;
138 p[4] = 0.0387;
139 p[5] = 0.01055;
140 p[6] = -4.03e-6;
141 p[7] = 0.07193;
142
143 if (t < 0.0)
144 return 0.0;
145 else
146 return (p[0] + p[1] * t) * exp(-p[2] * t) + (p[3] + p[4] * t + p[5] * square(t) + p[6] * cube(t)) * exp(-p[7] * t);
147 }
148
149
154
156 public:
159 m_samples({{0, 0, 0, 0, 0, 0}}), m_waveform(waveform)
160 {}
161
162 void setWaveform(WaveformShape waveform) { m_waveform = waveform; }
164 const apvSamples& operator()(double t0, double tau)
165 {
166 std::transform(
167 apvTimeBase.begin(), apvTimeBase.end(),
168 m_samples.begin(),
169 [this, t0, tau](double t)->double { return m_waveform((t - t0) / tau); }
170 );
171 return m_samples;
172 }
173 private:
176 };
177
178
179 // ==============================================================================
180 // The 3-samples filter
181 // ------------------------------------------------------------------------------
182
184 template<typename T>
185 inline void zeroSuppress(T& a, double thr)
186 {
187 std::replace_if(a.begin(), a.end(), std::bind(std::less<double>(), std::placeholders::_1, thr), 0.0);
188 }
189
190// ==============================================================================
191// The 3-samples filter
192// ------------------------------------------------------------------------------
193
195 template<typename T>
196 inline bool pass3Samples(const T& a, double thr)
197 {
198 return (search_n(a.begin(), a.end(), 3, thr, std::greater<double>()) != a.end());
199 }
200
201// ==============================================================================
202// Tau (scale) conversion and encoding
203// ------------------------------------------------------------------------------
204
208 inline double tau_raw2real(double raw_tau) { return 7.32313 * raw_tau; }
209
216 public:
223 TauEncoder(double min_amplitude, double max_amplitude,
224 double min_tau, double max_tau):
225 m_minAmplitude(min_amplitude), m_maxAmplitude(max_amplitude),
226 m_minTau(min_tau), m_maxTau(max_tau)
227 {
229 }
230
232 TauEncoder(): TauEncoder(0, 100, 0, 100) {}
233
240 void setBounds(double min_amplitude, double max_amplitude,
241 double min_tau, double max_tau)
242 {
243 m_minAmplitude = min_amplitude;
244 m_maxAmplitude = max_amplitude;
245 m_minTau = min_tau;
246 m_maxTau = max_tau;
247 // Re-initialize the range ratio setting
249 }
250
255 double encodeTau(double tau) const
256 { return m_minAmplitude + m_ATRatio * (tau - m_minTau); }
257
262 double decodeTau(double scaledTau) const
263 { return m_minTau + 1.0 / m_ATRatio * (scaledTau - m_minAmplitude); }
264
266 void print(std::ostringstream& os) const
267 {
268 os << "TauEncoder object, "
269 << "Amplitude range: " << m_minAmplitude << " to " << m_maxAmplitude
270 << " Tau range: " << m_minTau << " to " << m_maxTau
271 << " ATRatio: " << m_ATRatio << std::endl;
272 }
273
274 private:
277 double m_minTau;
278 double m_maxTau;
279 double m_ATRatio;
280 };
281// ------------------------------------------------------------------------------
282
283 } // namespace SVD
285} // namespace Belle2
286
287#endif
TauEncoder()
Default constructor Parameterless constructor to default-construct the class.
double m_minAmplitude
lower bound of amplitude range
double m_maxTau
upper bound of tau range
double m_ATRatio
ratio of amplitude to tau ranges
double m_minTau
lower bound of tau range
double m_maxAmplitude
upper bound of amplitdue range
void print(std::ostringstream &os) const
print tool
double decodeTau(double scaledTau) const
Scale back a previously scaled tau value.
TauEncoder(double min_amplitude, double max_amplitude, double min_tau, double max_tau)
Constructor takes amplitude and tau ranges, typically from a network xml file.
void setBounds(double min_amplitude, double max_amplitude, double min_tau, double max_tau)
Set encoder baounds (e.g.
double encodeTau(double tau) const
Return encoded value for a waveform width tau value.
WaveformShape m_waveform
the wave function
apvSamples m_samples
for storage of computed data
void setWaveform(WaveformShape waveform)
Set waveform.
const apvSamples & operator()(double t0, double tau)
Operator () returns 6 APV samples.
WaveGenerator(WaveformShape waveform=w_betaprime)
Constructor takes waveform function.
static const double ns
Standard of [time].
Definition Unit.h:48
constexpr T cube(const T &x)
Calculate the cube of the input.
Definition MathHelpers.h:29
constexpr T square(const T &x)
Calculate the square of the input.
Definition MathHelpers.h:21
constexpr T pow4(const T &x)
Calculate the fourth power of the input.
Definition MathHelpers.h:37
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
std::array< apvSampleBaseType, nAPVSamples > apvSamples
vector od apvSample BaseType objects
double w_poly3(double t)
Polynomial waveform shape, x.
double tau_raw2real(double raw_tau)
Convert Hao's raw tau (integral, in latency units) to correct betaprime scale.
double w_adjacentU(double t)
Adjacent-channel waveform U-side.
const std::size_t nAPVSamples
Number of APV samples.
double w_betaprime(double t)
Beta-prime waveform shape, x^alpha/(1+x)^beta.
std::function< double(double)> WaveformShape
WaveformShape type.
double w_adjacentV(double t)
Adjacent-channel waveform V-side.
const apvSamples apvTimeBase
APV time base - times for the 6 signals.
double w_expo(double t)
Gamma waveform shape, x.exp(-x) This is only historically useful.
void zeroSuppress(T &a, double thr)
pass zero suppression
double apvSampleBaseType
Vector of input samples in float form.
const double dt_APV
APV sampling time.
bool pass3Samples(const T &a, double thr)
pass 3-samples
Abstract base class for different kinds of events.