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
20namespace Belle2 {
25 namespace SVD {
26
27// ==============================================================================
28// APV25 sample data
29// ------------------------------------------------------------------------------
30// Constants:
32 const std::size_t nAPVSamples = 6;
33
35 const double dt_APV = 31.44 * Unit::ns;
36
38 typedef double apvSampleBaseType;
39 typedef std::array<apvSampleBaseType, nAPVSamples> apvSamples;
43 {{ -dt_APV, 0.0, dt_APV, 2 * dt_APV, 3 * dt_APV, 4 * dt_APV}};
44
45// ==============================================================================
46// APV25 waveform shapes
47// ------------------------------------------------------------------------------
48
54 typedef std::function<double(double)> WaveformShape;
55
61 inline double w_expo(double t)
62 {
63 if (t < 0.0) return 0.0;
64 else return t * exp(1.0 - t);
65 }
66
73 inline double w_poly3(double t)
74 {
75 if (t < 0.0 || t > 1.0)
76 return 0.0;
77 else
78 return 6.75 * t * (1.0 - t) * (1.0 - t);
79 }
80
86 inline double w_betaprime(double t)
87 {
88 if (t < 0.0)
89 return 0.0;
90 else
91 return 149.012 * pow(t, 2) * pow(1.0 + t, -10);
92 }
93
99 inline double w_adjacentU(double t)
100 {
101
102 double p[8];
103
104 // waveform parameters
105 p[0] = -7.379;
106 p[1] = -0.3594;
107 p[2] = 0.07564;
108 p[3] = 7.391;
109 p[4] = -0.08004;
110 p[5] = -2.139e-5;
111 p[6] = -1.781e-7;
112 p[7] = 0.01827;
113
114 if (t < 0.0)
115 return 0.0;
116 else
117 return (p[0] + p[1] * t) * exp(-p[2] * t) + (p[3] + p[4] * t + p[5] * pow(t, 2) + p[6] * pow(t, 3)) * exp(-p[7] * t);
118 }
119
125 inline double w_adjacentV(double t)
126 {
127
128 double p[8];
129
130 // waveform parameters
131 p[0] = 0.2043;
132 p[1] = -0.0197;
133 p[2] = 0.01448;
134 p[3] = -0.2076;
135 p[4] = 0.0387;
136 p[5] = 0.01055;
137 p[6] = -4.03e-6;
138 p[7] = 0.07193;
139
140 if (t < 0.0)
141 return 0.0;
142 else
143 return (p[0] + p[1] * t) * exp(-p[2] * t) + (p[3] + p[4] * t + p[5] * pow(t, 2) + p[6] * pow(t, 3)) * exp(-p[7] * t);
144 }
145
146
153 public:
156 m_samples({{0, 0, 0, 0, 0, 0}}), m_waveform(waveform)
157 {}
159 void setWaveform(WaveformShape waveform) { m_waveform = waveform; }
161 const apvSamples& operator()(double t0, double tau)
162 {
163 std::transform(
164 apvTimeBase.begin(), apvTimeBase.end(),
165 m_samples.begin(),
166 [this, t0, tau](double t)->double { return m_waveform((t - t0) / tau); }
167 );
168 return m_samples;
169 }
170 private:
173 };
174
175
176 // ==============================================================================
177 // The 3-samples filter
178 // ------------------------------------------------------------------------------
179
181 template<typename T>
182 inline void zeroSuppress(T& a, double thr)
183 {
184 std::replace_if(a.begin(), a.end(), std::bind(std::less<double>(), std::placeholders::_1, thr), 0.0);
185 }
186
187// ==============================================================================
188// The 3-samples filter
189// ------------------------------------------------------------------------------
190
192 template<typename T>
193 inline bool pass3Samples(const T& a, double thr)
194 {
195 return (search_n(a.begin(), a.end(), 3, thr, std::greater<double>()) != a.end());
196 }
197
198// ==============================================================================
199// Tau (scale) conversion and encoding
200// ------------------------------------------------------------------------------
201
205 inline double tau_raw2real(double raw_tau) { return 7.32313 * raw_tau; }
206
213 public:
220 TauEncoder(double min_amplitude, double max_amplitude,
221 double min_tau, double max_tau):
222 m_minAmplitude(min_amplitude), m_maxAmplitude(max_amplitude),
223 m_minTau(min_tau), m_maxTau(max_tau)
224 {
226 }
229 TauEncoder(): TauEncoder(0, 100, 0, 100) {}
230
237 void setBounds(double min_amplitude, double max_amplitude,
238 double min_tau, double max_tau)
239 {
240 m_minAmplitude = min_amplitude;
241 m_maxAmplitude = max_amplitude;
242 m_minTau = min_tau;
243 m_maxTau = max_tau;
244 // Re-initialize the range ratio setting
246 }
247
252 double encodeTau(double tau) const
253 { return m_minAmplitude + m_ATRatio * (tau - m_minTau); }
254
259 double decodeTau(double scaledTau) const
260 { return m_minTau + 1.0 / m_ATRatio * (scaledTau - m_minAmplitude); }
261
263 void print(std::ostringstream& os) const
264 {
265 os << "TauEncoder object, "
266 << "Amplitude range: " << m_minAmplitude << " to " << m_maxAmplitude
267 << " Tau range: " << m_minTau << " to " << m_maxTau
268 << " ATRatio: " << m_ATRatio << std::endl;
269 }
270
271 private:
274 double m_minTau;
275 double m_maxTau;
276 double m_ATRatio;
277 };
278// ------------------------------------------------------------------------------
279
280 } // namespace SVD
282} // namespace Belle2
283
284#endif
Encoder/decoder for neural network tau values.
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.
Waveform generator This is a functor to calculate APV samples from waveform.
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
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.