Belle II Software  release-08-01-10
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 
12 using namespace std;
13 using namespace Belle2::ECL;
14 
15 const 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 
17 ostream& 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 
23 ostream& 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 
30 void ShaperDSP_t::sv123shift_t::init(double _t, const ShaperDSP_t& _p)
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 
41 ShaperDSP_t::sv123shift_t& ShaperDSP_t::sv123shift_t::operator +=(const ShaperDSP_t::sv123shift_t& r)
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 {
64  sv123shift_t a;
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 void ShaperDSP_t::shaperdspshift_t::init(double _t, const ShaperDSP_t& _p)
80 {
81  sv123shift_t::init(_t, _p);
82  et0 = exp(-t * _p._dt0);
83  et1 = exp(-t * _p._dt1);
84 }
85 
86 ShaperDSP_t::shaperdspshift_t& ShaperDSP_t::shaperdspshift_t::operator +=(const ShaperDSP_t::shaperdspshift_t& r)
87 {
88  const sv123shift_t& r0 = static_cast<const ShaperDSP_t::sv123shift_t&>(r);
89 
90  sv123shift_t::operator +=(r0);
91  et0 *= r.et0;
92  et1 *= r.et1;
93  return *this;
94 }
95 
97 {
98  const sv123shift_t& r0 = static_cast<const ShaperDSP_t::sv123shift_t&>(r);
100  sv123shift_t& a0 = static_cast<ShaperDSP_t::sv123shift_t&>(a);
101 
102  a0 = sv123shift_t::operator+(r0);
103  a.et0 = et0 * r.et0;
104  a.et1 = et1 * r.et1;
105  return a;
106 }
107 
109 {
110  ShaperDSP_t::shaperdspshift_t a = c0.operator + (c1);
111  return a;
112 }
113 
114 dd_t operator +(const dd_t& c0, const dd_t& c1)
115 {
116  return dd_t(c0.first + c1.first, c0.second + c1.second);
117 }
118 
119 dd_t operator *(double a, const dd_t& c)
120 {
121  return dd_t(a * c.first, a * c.second);
122 }
123 
124 void ShaperDSP_t::Sv123_init(double t01, double tb1, double t02, double tb2, double td1, double ts1)
125 {
126  double dks0, dks1, dksm,
127  dw0, dw1, dwp, dwm, das1, dac1, das0, dac0, dzna, dksm2, ds, dd,
128  dcs0, dsn0, dzn0, td, ts, dr,
129  dcs0s, dsn0s, dcs0d, dsn0d, dcs1s, dsn1s, dcs1d, dsn1d;
130 
131  dr = (ts1 - td1) / td1;
132  if (std::abs(dr) >= 0.0000001) {
133  td = td1;
134  ts = ts1;
135  } else {
136  td = td1;
137  if (ts1 > td1) {
138  ts = td1 * 1.00001;
139  } else {
140  ts = td1 * 0.99999;
141  }
142  }
143 
144  dr = ((t01 - t02) * (t01 - t02) + (tb1 - tb2) * (tb1 - tb2)) / (t01 * t01 + tb1 * tb1);
145  dks0 = 1 / t01;
146  dks1 = 1 / t02;
147  if (dr < 0.0000000001) {
148  if (dks0 > dks1) {
149  dks0 = dks1 * 1.00001;
150  } else {
151  dks0 = dks1 * 0.99999;
152  }
153  }
154 
155  dksm = dks1 - dks0;
156 
157  ds = 1 / ts;
158  dd = 1 / td;
159 
160  dw0 = 1 / tb1;
161  dw1 = 1 / tb2;
162  dwp = dw0 + dw1;
163  dwm = dw1 - dw0;
164 
165  dksm2 = dksm * dksm;
166 
167  dzna = (dksm2 + dwm * dwm) * (dksm2 + dwp * dwp);
168 
169  das0 = dw1 * (dksm2 + dwp * dwm);
170  dac0 = -2 * dksm * dw0 * dw1;
171  das1 = dw0 * (dksm2 - dwp * dwm);
172  dac1 = -dac0;
173 
174  dsn0 = (ds - dks0);
175  dcs0 = -dw0;
176  dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
177 
178  dsn0s = (dsn0 * das0 - dcs0 * dac0) / dzn0;
179  dcs0s = (dcs0 * das0 + dsn0 * dac0) / dzn0;
180 
181  dsn0 = (ds - dks1);
182  dcs0 = -dw1;
183  dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
184 
185  dsn1s = (dsn0 * das1 - dcs0 * dac1) / dzn0;
186  dcs1s = (dcs0 * das1 + dsn0 * dac1) / dzn0;
187 
188  dsn0 = (dd - dks0);
189  dcs0 = -dw0;
190  dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
191 
192  dsn0d = (dsn0 * das0 - dcs0 * dac0) / dzn0;
193  dcs0d = (dcs0 * das0 + dsn0 * dac0) / dzn0;
194 
195  dsn0 = (dd - dks1);
196  dcs0 = -dw1;
197  dzn0 = dcs0 * dcs0 + dsn0 * dsn0;
198 
199  dsn1d = (dsn0 * das1 - dcs0 * dac1) / dzn0;
200  dcs1d = (dcs0 * das1 + dsn0 * dac1) / dzn0;
201 
202  _dw0 = dw0;
203  _dw1 = dw1;
204  _dks0 = dks0;
205  _dks1 = dks1;
206  _ds = ds;
207  _dd = dd;
208 
209  double sc = 1 / (dzna * (ts - td));
210  _cs0 = sc * (dsn0s - dsn0d);
211  _cc0 = sc * (dcs0s - dcs0d);
212  _cs1 = sc * (dsn1s - dsn1d);
213  _cc1 = sc * (dcs1s - dcs1d);
214  _ces = sc * (dcs0s + dcs1s);
215  _ced = sc * (dcs0d + dcs1d);
216 }
217 
218 double ShaperDSP_t::Sv123(const sv123shift_t& c) const
219 {
220  double f0 = (_cs0 * c.s0 + _cc0 * c.c0) * c.e0;
221  double f1 = (_cs1 * c.s1 + _cc1 * c.c1) * c.e1;
222  double fs = _ces * c.es;
223  double fd = _ced * c.ed;
224 
225  return (f0 + f1) + (fd - fs);
226 }
227 
228 dd_t ShaperDSP_t::ddSv123(const sv123shift_t& c) const
229 {
230  double f0 = (_cs0 * c.s0 + _cc0 * c.c0) * c.e0;
231  double f1 = (_cs1 * c.s1 + _cc1 * c.c1) * c.e1;
232  double fs = _ces * c.es;
233  double fd = _ced * c.ed;
234 
235  double f0p = (_dw0 * c.e0) * (_cs0 * c.c0 - _cc0 * c.s0) - (_dks0 * f0);
236  double f1p = (_dw1 * c.e1) * (_cs1 * c.c1 - _cc1 * c.s1) - (_dks1 * f1);
237  double fsp = _ds * fs;
238  double fdp = _dd * fd;
239 
240  double f = (f0 + f1) + (fd - fs);
241  double fp = (f0p + f1p) - (fdp - fsp);
242  return dd_t(f, fp);
243 }
244 
245 double ShaperDSP_t::Sv123_filtered(const sv123shift_t& t0) const
246 {
247  if (t0.validshift(_tp)) {
248  double ft0p = Sv123(t0 + _tp), ft0 = 0, ft0m = 0;
249  if (t0.t > 0) {
250  ft0 = Sv123(t0);
251  if (t0.validshift(_tm))
252  ft0m = Sv123(t0 + _tm);
253  }
254  return _w0 * ft0 + _w1 * (ft0p + ft0m);
255  }
256  return 0;
257 }
258 
259 dd_t ShaperDSP_t::ddSv123_filtered(const sv123shift_t& t0) const
260 {
261  if (t0.validshift(_tp)) {
262  dd_t ft0p = ddSv123(t0 + _tp), ft0 = dd_t(0, 0), ft0m = dd_t(0, 0);
263  if (t0.t > 0) {
264  ft0 = ddSv123(t0);
265  if (t0.validshift(_tm))
266  ft0m = ddSv123(t0 + _tm);
267  }
268  return _w0 * ft0 + _w1 * (ft0p + ft0m);
269  }
270  return dd_t(0, 0);
271 }
272 
273 double ShaperDSP_t::ShaperDSP(const shaperdspshift_t& t0) const
274 {
275  const sv123shift_t& t00 = static_cast<const ShaperDSP_t::sv123shift_t&>(t0);
276  double f = Sv123_filtered(t00);
277  if (t0.t > 0) {
278  double z = t0.t * _dt1;
279  double z2 = z * z;
280  double odd = 1 + z2 * (1 / 6. + z2 * (1 / 120.));
281  double evn = 1 + z2 * (1 / 2. + z2 * (1 / 24.));
282  double texp = evn + z * odd;
283  double df = _ccc * t0.et0 * (1 - t0.et1 * texp);
284  f -= df;
285  }
286  return f;
287 }
288 
289 dd_t ShaperDSP_t::ddShaperDSP(const shaperdspshift_t& t0) const
290 {
291  const sv123shift_t& t00 = static_cast<const ShaperDSP_t::sv123shift_t&>(t0);
292  dd_t f = ddSv123_filtered(t00);
293  if (t0.t > 0) {
294  double z = t0.t * _dt1;
295  double z2 = z * z;
296  double odd = 1 + z2 * (1 / 6. + z2 * (1 / 120.));
297  double evn = 1 + z2 * (1 / 2. + z2 * (1 / 24.));
298  double texp = evn + z * odd;
299  double u = t0.et0 * (1 - t0.et1 * texp);
300  f.first -= _ccc * u;
301 
302  double up = -_dt0 * u + ((_dt1 * t0.et0) * (t0.et1 * z2)) * ((z2 * z) * (1 / 120.));
303  f.second -= _ccc * up;
304  }
305  return f;
306 }
307 
308 void ShaperDSP_t::init(const double* s, double unitscale)
309 {
310  double t01 = s[2];
311  double tb1 = s[3];
312  double t02 = s[7];
313  double tb2 = s[8];
314  double td1 = s[1];
315  double ts1 = s[4];
316  Sv123_init(t01, tb1, t02, tb2, td1, ts1);
317  _dt0 = 1 / s[6];
318  _dt1 = 1 / s[2];
319 
320  _toff = s[0];
321  _w0 = 1.0 - s[9];
322  _w1 = 0.5 * s[9];
323  _ccc = s[5];
324 
325  _ccc *= unitscale;
326  _cs0 *= unitscale;
327  _cc0 *= unitscale;
328  _cs1 *= unitscale;
329  _cc1 *= unitscale;
330  _ces *= unitscale;
331  _ced *= unitscale;
332 
333  _tp.init(_filterdt, *this);
334  _tm.init(-_filterdt, *this);
335 }
336 
337 void ShaperDSP_t::init(const double* s)
338 {
339  init(s, -1);
340 }
341 
342 void ShaperDSP_t::init(const std::vector<double>& s, double unitscale)
343 {
344  if (s.size() == 10)
345  init(s.data(), unitscale);
346  else
347  init(_defs, unitscale);
348 }
349 
350 double ShaperDSP_t::operator()(double t) const
351 {
352  shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
353  return ShaperDSP(t0);
354 }
355 
356 double ShaperDSP_t::operator()(double* x, double*)
357 {
358  return (*this)(x[0]);
359 }
360 
361 void ShaperDSP_t::settimestride(double dt)
362 {
363  _tstride.init(dt, *this);
364 }
365 
366 void ShaperDSP_t::setseedoffset(double dt)
367 {
368  _toffset.init(dt, *this);
369 }
370 
371 void ShaperDSP_t::settimeseed(double t0)
372 {
373  _tzero.init(t0 - _toff, *this);
374 }
375 
376 void ShaperDSP_t::nextseed()
377 {
378  _tzero += _toffset;
379 }
380 
381 void ShaperDSP_t::fillarray(int n, double* s) const
382 {
383  shaperdspshift_t t0 = _tzero;
384 
385  for (int i = 0; i < n; i++, t0 += _tstride) {
386  s[i] = ShaperDSP(t0);
387  }
388 }
389 
390 void ShaperDSP_t::fillarray(int n, dd_t* s) const
391 {
392  shaperdspshift_t t0 = _tzero;
393 
394  for (int i = 0; i < n; i++, t0 += _tstride) {
395  s[i] = ddShaperDSP(t0);
396  }
397 }
398 
399 void ShaperDSP_t::fillarray(double t, int n, double* s) const
400 {
401  shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
402 
403  for (int i = 0; i < n; i++, t0 += _tstride) {
404  s[i] = ShaperDSP(t0);
405  }
406 }
407 
408 void ShaperDSP_t::fillarray(double t, int n, dd_t* s) const
409 {
410  shaperdspshift_t t0 = shaperdspshift_t(t - _toff, *this);
411 
412  for (int i = 0; i < n; i++, t0 += _tstride) {
413  s[i] = ddShaperDSP(t0);
414  }
415 }
416 
417 void ShaperDSP_t::fillvector(std::vector<double>& s) const
418 {
419  fillarray(s.size(), s.data());
420 }
421 
422 void ShaperDSP_t::fillvector(std::vector<dd_t>& s) const
423 {
424  fillarray(s.size(), s.data());
425 }
426 
427 void ShaperDSP_t::fillvector(double t, std::vector<double>& s) const
428 {
429  fillarray(t, s.size(), s.data());
430 }
431 
432 void ShaperDSP_t::fillvector(double t, std::vector<dd_t>& s) const
433 {
434  fillarray(t, s.size(), s.data());
435 }
Class include function that calculate electronic response from energy deposit
Definition: shaperdsp.h:26
double _dw0
circular frequency of the first Bessel stage
Definition: shaperdsp.h:90
double _dks1
decrement of the second Bessel stage
Definition: shaperdsp.h:96
double _dt0
coefficient for first exponent factor
Definition: shaperdsp.h:102
double _dw1
circular frequency of the second Bessel stage
Definition: shaperdsp.h:92
double _ds
inverse scintillation decay time
Definition: shaperdsp.h:98
double _dd
inverse time of the differential stage
Definition: shaperdsp.h:100
double _dks0
decrement of the first Bessel stage
Definition: shaperdsp.h:94
double _dt1
coefficient for second exponent factor
Definition: shaperdsp.h:104
B2Vector3< DataType > operator*(DataType a, const B2Vector3< DataType > &p)
non-memberfunction Scaling of 3-vectors with a real number
Definition: B2Vector3.h:537
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition: B2Vector3.h:544
struct for a shift of the shaper dsp
Definition: shaperdsp.h:59
struct to encapsulate the electronic response from energy deposit
Definition: shaperdsp.h:30
bool validshift(const sv123shift_t &x) const
check for a valid shift
Definition: shaperdsp.h:55