Belle II Software development
shaperdsp.cc
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#include <ecl/digitization/shaperdsp.h>
9#include <cmath>
10#include <iostream>
11
12using namespace std;
13using namespace Belle2::ECL;
14
15const double ShaperDSP_t::_defs[] = {0.5, 0.6483, 0.4017, 0.3741, 0.8494, 0.00144547, 4.7071, 0.8156, 0.5556, 0.2752}; // default parameters
16
17ostream& operator<<(ostream& o, const ShaperDSP_t::sv123shift_t& s)
18{
19 o << s.t << " " << s.c0 << " " << s.c0 << " " << s.c1 << " " << s.s1 << " " << s.e0 << " " << s.e1 << " " << s.es << " " << s.ed;
20 return o;
21}
22
23ostream& operator<<(ostream& o, const ShaperDSP_t::shaperdspshift_t& s)
24{
25 const ShaperDSP_t::sv123shift_t& s0 = static_cast<const ShaperDSP_t::sv123shift_t&>(s);
26 o << s0 << " " << s.et0 << " " << s.et1;
27 return o;
28}
29
31{
32 t = _t;
33 sincos(t * _p._dw0, &s0, &c0);
34 sincos(t * _p._dw1, &s1, &c1);
35 e0 = exp(-t * _p._dks0);
36 e1 = exp(-t * _p._dks1);
37 es = exp(-t * _p._ds);
38 ed = exp(-t * _p._dd);
39}
40
42{
43 t += r.t;
44
45 double c0r = r.c0 * c0 - r.s0 * s0;
46 double s0r = r.c0 * s0 + r.s0 * c0;
47 c0 = c0r;
48 s0 = s0r;
49
50 double c1r = r.c1 * c1 - r.s1 * s1;
51 double s1r = r.c1 * s1 + r.s1 * c1;
52 c1 = c1r;
53 s1 = s1r;
54
55 e0 *= r.e0;
56 e1 *= r.e1;
57 es *= r.es;
58 ed *= r.ed;
59 return *this;
60}
61
63{
65 a.t = t + r.t;
66
67 a.c0 = r.c0 * c0 - r.s0 * s0;
68 a.s0 = r.c0 * s0 + r.s0 * c0;
69 a.c1 = r.c1 * c1 - r.s1 * s1;
70 a.s1 = r.c1 * s1 + r.s1 * c1;
71
72 a.e0 = e0 * r.e0;
73 a.e1 = e1 * r.e1;
74 a.es = es * r.es;
75 a.ed = ed * r.ed;
76 return a;
77}
78
79// cppcheck-suppress duplInheritedMember ; each level initialises its own fields
81{
82 sv123shift_t::init(_t, _p);
83 et0 = exp(-t * _p._dt0);
84 et1 = exp(-t * _p._dt1);
85}
86
88{
89 const sv123shift_t& r0 = static_cast<const ShaperDSP_t::sv123shift_t&>(r);
90
92 et0 *= r.et0;
93 et1 *= r.et1;
94 return *this;
95}
96
98{
99 const sv123shift_t& r0 = static_cast<const ShaperDSP_t::sv123shift_t&>(r);
101 sv123shift_t& a0 = static_cast<ShaperDSP_t::sv123shift_t&>(a);
102
104 a.et0 = et0 * r.et0;
105 a.et1 = et1 * r.et1;
106 return a;
107}
108
110{
111 ShaperDSP_t::shaperdspshift_t a = c0.operator + (c1);
112 return a;
113}
114
115dd_t operator +(const dd_t& c0, const dd_t& c1)
116{
117 return dd_t(c0.first + c1.first, c0.second + c1.second);
118}
119
120dd_t operator *(double a, const dd_t& c)
121{
122 return dd_t(a * c.first, a * c.second);
123}
124
125void ShaperDSP_t::Sv123_init(double t01, double tb1, double t02, double tb2, double td1, double ts1)
126{
127 double dks0, dks1, dksm,
128 dw0, dw1, dwp, dwm, das1, dac1, das0, dac0, dzna, dksm2, ds, dd,
129 dcs0, dsn0, dzn0, td, ts, dr,
130 dcs0s, dsn0s, dcs0d, dsn0d, dcs1s, dsn1s, dcs1d, dsn1d;
131
132 dr = (ts1 - td1) / td1;
133 if (std::abs(dr) >= 0.0000001) {
134 td = td1;
135 ts = ts1;
136 } else {
137 td = td1;
138 if (ts1 > td1) {
139 ts = td1 * 1.00001;
140 } else {
141 ts = td1 * 0.99999;
142 }
143 }
144
145 dr = ((t01 - t02) * (t01 - t02) + (tb1 - tb2) * (tb1 - tb2)) / (t01 * t01 + tb1 * tb1);
146 dks0 = 1 / t01;
147 dks1 = 1 / t02;
148 if (dr < 0.0000000001) {
149 if (dks0 > dks1) {
150 dks0 = dks1 * 1.00001;
151 } else {
152 dks0 = dks1 * 0.99999;
153 }
154 }
155
156 dksm = dks1 - dks0;
157
158 ds = 1 / ts;
159 dd = 1 / td;
160
161 dw0 = 1 / tb1;
162 dw1 = 1 / tb2;
163 dwp = dw0 + dw1;
164 dwm = dw1 - dw0;
165
166 dksm2 = dksm * dksm;
167
168 dzna = (dksm2 + dwm * dwm) * (dksm2 + dwp * dwp);
169
170 das0 = dw1 * (dksm2 + dwp * dwm);
171 dac0 = -2 * dksm * dw0 * dw1;
172 das1 = dw0 * (dksm2 - dwp * dwm);
173 dac1 = -dac0;
174
175 dsn0 = (ds - dks0);
176 dcs0 = -dw0;
177 dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
178
179 dsn0s = (dsn0 * das0 - dcs0 * dac0) / dzn0;
180 dcs0s = (dcs0 * das0 + dsn0 * dac0) / dzn0;
181
182 dsn0 = (ds - dks1);
183 dcs0 = -dw1;
184 dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
185
186 dsn1s = (dsn0 * das1 - dcs0 * dac1) / dzn0;
187 dcs1s = (dcs0 * das1 + dsn0 * dac1) / dzn0;
188
189 dsn0 = (dd - dks0);
190 dcs0 = -dw0;
191 dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
192
193 dsn0d = (dsn0 * das0 - dcs0 * dac0) / dzn0;
194 dcs0d = (dcs0 * das0 + dsn0 * dac0) / dzn0;
195
196 dsn0 = (dd - dks1);
197 dcs0 = -dw1;
198 dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
199
200 dsn1d = (dsn0 * das1 - dcs0 * dac1) / dzn0;
201 dcs1d = (dcs0 * das1 + dsn0 * dac1) / dzn0;
202
203 _dw0 = dw0;
204 _dw1 = dw1;
205 _dks0 = dks0;
206 _dks1 = dks1;
207 _ds = ds;
208 _dd = dd;
209
210 double sc = 1 / (dzna * (ts - td));
211 _cs0 = sc * (dsn0s - dsn0d);
212 _cc0 = sc * (dcs0s - dcs0d);
213 _cs1 = sc * (dsn1s - dsn1d);
214 _cc1 = sc * (dcs1s - dcs1d);
215 _ces = sc * (dcs0s + dcs1s);
216 _ced = sc * (dcs0d + dcs1d);
217}
218
219double ShaperDSP_t::Sv123(const sv123shift_t& c) const
220{
221 double f0 = (_cs0 * c.s0 + _cc0 * c.c0) * c.e0;
222 double f1 = (_cs1 * c.s1 + _cc1 * c.c1) * c.e1;
223 double fs = _ces * c.es;
224 double fd = _ced * c.ed;
225
226 return (f0 + f1) + (fd - fs);
227}
228
230{
231 double f0 = (_cs0 * c.s0 + _cc0 * c.c0) * c.e0;
232 double f1 = (_cs1 * c.s1 + _cc1 * c.c1) * c.e1;
233 double fs = _ces * c.es;
234 double fd = _ced * c.ed;
235
236 double f0p = (_dw0 * c.e0) * (_cs0 * c.c0 - _cc0 * c.s0) - (_dks0 * f0);
237 double f1p = (_dw1 * c.e1) * (_cs1 * c.c1 - _cc1 * c.s1) - (_dks1 * f1);
238 double fsp = _ds * fs;
239 double fdp = _dd * fd;
240
241 double f = (f0 + f1) + (fd - fs);
242 double fp = (f0p + f1p) - (fdp - fsp);
243 return dd_t(f, fp);
244}
245
247{
248 if (t0.validshift(_tp)) {
249 double ft0p = Sv123(t0 + _tp), ft0 = 0, ft0m = 0;
250 if (t0.t > 0) {
251 ft0 = Sv123(t0);
252 if (t0.validshift(_tm))
253 ft0m = Sv123(t0 + _tm);
254 }
255 return _w0 * ft0 + _w1 * (ft0p + ft0m);
256 }
257 return 0;
258}
259
261{
262 if (t0.validshift(_tp)) {
263 dd_t ft0p = ddSv123(t0 + _tp), ft0 = dd_t(0, 0), ft0m = dd_t(0, 0);
264 if (t0.t > 0) {
265 ft0 = ddSv123(t0);
266 if (t0.validshift(_tm))
267 ft0m = ddSv123(t0 + _tm);
268 }
269 return _w0 * ft0 + _w1 * (ft0p + ft0m);
270 }
271 return dd_t(0, 0);
272}
273
275{
276 const sv123shift_t& t00 = static_cast<const ShaperDSP_t::sv123shift_t&>(t0);
277 double f = Sv123_filtered(t00);
278 if (t0.t > 0) {
279 double z = t0.t * _dt1;
280 double z2 = z * z;
281 double odd = 1 + z2 * (1 / 6. + z2 * (1 / 120.));
282 double evn = 1 + z2 * (1 / 2. + z2 * (1 / 24.));
283 double texp = evn + z * odd;
284 double df = _ccc * t0.et0 * (1 - t0.et1 * texp);
285 f -= df;
286 }
287 return f;
288}
289
291{
292 const sv123shift_t& t00 = static_cast<const ShaperDSP_t::sv123shift_t&>(t0);
293 dd_t f = ddSv123_filtered(t00);
294 if (t0.t > 0) {
295 double z = t0.t * _dt1;
296 double z2 = z * z;
297 double odd = 1 + z2 * (1 / 6. + z2 * (1 / 120.));
298 double evn = 1 + z2 * (1 / 2. + z2 * (1 / 24.));
299 double texp = evn + z * odd;
300 double u = t0.et0 * (1 - t0.et1 * texp);
301 f.first -= _ccc * u;
302
303 double up = -_dt0 * u + ((_dt1 * t0.et0) * (t0.et1 * z2)) * ((z2 * z) * (1 / 120.));
304 f.second -= _ccc * up;
305 }
306 return f;
307}
308
309void ShaperDSP_t::init(const double* s, double unitscale)
310{
311 double t01 = s[2];
312 double tb1 = s[3];
313 double t02 = s[7];
314 double tb2 = s[8];
315 double td1 = s[1];
316 double ts1 = s[4];
317 Sv123_init(t01, tb1, t02, tb2, td1, ts1);
318 _dt0 = 1 / s[6];
319 _dt1 = 1 / s[2];
320
321 _toff = s[0];
322 _w0 = 1.0 - s[9];
323 _w1 = 0.5 * s[9];
324 _ccc = s[5];
325
326 _ccc *= unitscale;
327 _cs0 *= unitscale;
328 _cc0 *= unitscale;
329 _cs1 *= unitscale;
330 _cc1 *= unitscale;
331 _ces *= unitscale;
332 _ced *= unitscale;
333
334 _tp.init(_filterdt, *this);
335 _tm.init(-_filterdt, *this);
336}
337
338void ShaperDSP_t::init(const double* s)
339{
340 init(s, -1);
341}
342
343void ShaperDSP_t::init(const std::vector<double>& s, double unitscale)
344{
345 if (s.size() == 10)
346 init(s.data(), unitscale);
347 else
348 init(_defs, unitscale);
349}
350
351double ShaperDSP_t::operator()(double t) const
352{
353 shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
354 return ShaperDSP(t0);
355}
356
357double ShaperDSP_t::operator()(const double* x, double*)
358{
359 return (*this)(x[0]);
360}
361
363{
364 _tstride.init(dt, *this);
365}
366
368{
369 _toffset.init(dt, *this);
370}
371
373{
374 _tzero.init(t0 - _toff, *this);
375}
376
378{
379 _tzero += _toffset;
380}
381
382void ShaperDSP_t::fillarray(int n, double* s) const
383{
385
386 for (int i = 0; i < n; i++, t0 += _tstride) {
387 s[i] = ShaperDSP(t0);
388 }
389}
390
391void ShaperDSP_t::fillarray(int n, dd_t* s) const
392{
394
395 for (int i = 0; i < n; i++, t0 += _tstride) {
396 s[i] = ddShaperDSP(t0);
397 }
398}
399
400void ShaperDSP_t::fillarray(double t, int n, double* s) const
401{
402 shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
403
404 for (int i = 0; i < n; i++, t0 += _tstride) {
405 s[i] = ShaperDSP(t0);
406 }
407}
408
409void ShaperDSP_t::fillarray(double t, int n, dd_t* s) const
410{
411 shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
412
413 for (int i = 0; i < n; i++, t0 += _tstride) {
414 s[i] = ddShaperDSP(t0);
415 }
416}
417
418void ShaperDSP_t::fillvector(std::vector<double>& s) const
419{
420 fillarray(s.size(), s.data());
421}
422
423void ShaperDSP_t::fillvector(std::vector<dd_t>& s) const
424{
425 fillarray(s.size(), s.data());
426}
427
428void ShaperDSP_t::fillvector(double t, std::vector<double>& s) const
429{
430 fillarray(t, s.size(), s.data());
431}
432
433void ShaperDSP_t::fillvector(double t, std::vector<dd_t>& s) const
434{
435 fillarray(t, s.size(), s.data());
436}
double _dw0
circular frequency of the first Bessel stage
Definition shaperdsp.h:91
void fillvector(std::vector< double > &) const
fill vector with response function values and its derivative
Definition shaperdsp.cc:418
dd_t ddSv123(const sv123shift_t &) const
calculate derivative of the Sv123 function
Definition shaperdsp.cc:229
double _cs1
linear coefficient before sin of the second Bessel stage
Definition shaperdsp.h:83
void init(const double *, double)
calculate some values for Sv123 function
Definition shaperdsp.cc:309
double _dks1
decrement of the second Bessel stage
Definition shaperdsp.h:97
double _cc0
linear coefficient before cos of the first Bessel stage
Definition shaperdsp.h:81
double _dt0
coefficient for first exponent factor
Definition shaperdsp.h:103
void settimeseed(double)
set initial time
Definition shaperdsp.cc:372
double Sv123_filtered(const sv123shift_t &) const
Numerical calculation of the time convolution.
Definition shaperdsp.cc:246
double _cc1
linear coefficient before cos of the second Bessel stage
Definition shaperdsp.h:85
dd_t ddSv123_filtered(const sv123shift_t &) const
This is derivative of the confolution.
Definition shaperdsp.cc:260
shaperdspshift_t _tzero
initial time
Definition shaperdsp.h:129
shaperdspshift_t _tstride
time step of the grid for response function calculation
Definition shaperdsp.h:124
double _cs0
linear coefficient before sin of the first Bessel stage
Definition shaperdsp.h:79
double _w1
weight coefficient at sv123(t+_filterdt) +sv123(t-_filterdt) = a/2
Definition shaperdsp.h:112
double _dw1
circular frequency of the second Bessel stage
Definition shaperdsp.h:93
ShaperDSP_t()
class constructor
Definition shaperdsp.h:156
double _ced
linear coefficient before second part of tail section
Definition shaperdsp.h:89
void settimestride(double)
set grid step for function calculation
Definition shaperdsp.cc:362
double Sv123(const sv123shift_t &) const
calculate Sv123 function
Definition shaperdsp.cc:219
double _toff
time offset
Definition shaperdsp.h:108
dd_t ddShaperDSP(const shaperdspshift_t &) const
calculate derivative of the response function
Definition shaperdsp.cc:290
double _ds
inverse scintillation decay time
Definition shaperdsp.h:99
void nextseed()
substruct toffset to tzero
Definition shaperdsp.cc:377
double _dd
inverse time of the differential stage
Definition shaperdsp.h:101
double ShaperDSP(const shaperdspshift_t &) const
calculate response function
Definition shaperdsp.cc:274
double _dks0
decrement of the first Bessel stage
Definition shaperdsp.h:95
double _w0
weight coefficient at sv123(t) = (1-a)
Definition shaperdsp.h:110
void setseedoffset(double)
set timeoffset
Definition shaperdsp.cc:367
static constexpr double _filterdt
time shift that include in response function for numerical calculation time convolutions.
Definition shaperdsp.h:76
void fillarray(int, double *) const
fill array for amplitude and time calculation
Definition shaperdsp.cc:382
static const double _defs[]
parameters of the response function that use as default
Definition shaperdsp.h:73
shaperdspshift_t _toffset
time offset
Definition shaperdsp.h:127
double operator()(double) const
wrapper of the function
Definition shaperdsp.cc:351
double _ccc
exponent factor for tail part of the signal
Definition shaperdsp.h:115
double _ces
linear coefficient before first part of tail section
Definition shaperdsp.h:87
double _dt1
coefficient for second exponent factor
Definition shaperdsp.h:105
sv123shift_t _tp
_filterdt
Definition shaperdsp.h:118
void Sv123_init(double t01, double tb1, double t02, double tb2, double td1, double ts1)
calculate some values for Sv123 function
Definition shaperdsp.cc:125
B2Vector3< DataType > operator*(DataType a, const B2Vector3< DataType > &p)
non-memberfunction Scaling of 3-vectors with a real number
Definition B2Vector3.h:543
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition B2Vector3.h:550
STL namespace.
struct for a shift of the shaper dsp
Definition shaperdsp.h:59
shaperdspshift_t operator+(const shaperdspshift_t &) const
addition operator
Definition shaperdsp.cc:97
shaperdspshift_t & operator+=(const shaperdspshift_t &)
increment operator
Definition shaperdsp.cc:87
void init(double, const ShaperDSP_t &)
initialise
Definition shaperdsp.cc:80
struct to encapsulate the electronic response from energy deposit
Definition shaperdsp.h:30
double e0
exponent factor for first Bessel stage
Definition shaperdsp.h:42
double es
first exponent factor for tail part of the signal.
Definition shaperdsp.h:46
double c0
cos of the first Bessel stage
Definition shaperdsp.h:36
double s1
sin of the second Bessel stage
Definition shaperdsp.h:38
double ed
second exponent factor for tail part of the signal.
Definition shaperdsp.h:48
sv123shift_t operator+(const sv123shift_t &) const
addition operator
Definition shaperdsp.cc:62
bool validshift(const sv123shift_t &x) const
check for a valid shift
Definition shaperdsp.h:55
double c1
cos of the second Bessel stage
Definition shaperdsp.h:40
sv123shift_t()
default constructor
Definition shaperdsp.h:50
double s0
sin of the first Bessel stage
Definition shaperdsp.h:34
double e1
exponent factor for second Bessel stage
Definition shaperdsp.h:44
void init(double, const ShaperDSP_t &)
initialise
Definition shaperdsp.cc:30
sv123shift_t & operator+=(const sv123shift_t &)
increment operator
Definition shaperdsp.cc:41