Belle II Software  release-06-01-15
EclConfigurationPure.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/EclConfigurationPure.h>
9 #include <ecl/digitization/ECLSampledShaper.h>
10 #include <algorithm>
11 #include <iostream>
12 #include <cassert>
13 
14 using namespace Belle2;
15 using namespace Belle2::ECL;
16 using namespace std;
17 
18 double EclConfigurationPure::m_tickPure = EclConfiguration::m_tick / EclConfiguration::m_ntrg * 8;
19 
20 void EclConfigurationPure::signalsamplepure_t::InitSample(const TH1F* sampledfun, const TH1F* sampledfunDerivative)
21 {
22  const int N = m_ns * m_nlPure;
23  double r1 = 32 / m_ns;
24  double r2 = EclConfiguration::m_tick / EclConfiguration::m_ntrg * 8 / EclConfigurationPure::m_tickPure ;
25 
26  ECLSampledShaper dsp(sampledfun, round(r1 / r2));
27  dsp.fillarray(N, m_ft);
28  double maxval = * max_element(m_ft, m_ft + N);
29 
30  for_each(m_ft1, m_ft1 + N, [maxval](double & a) { a /= maxval; });
31  double maxval2 = * max_element(m_ft, m_ft + N);
32  assert(maxval2 - 1.0 < 0.001);
33  double sum = 0;
34  for (int i = 0; i < N; i++) sum += m_ft[i];
35  m_sumscale = m_ns / sum;
36  // for (int i = 0; i < N; ++i) m_ft1[i] = sampledfunDerivative->GetBinContent(i + 1);
37  ECLSampledShaper dsp1(sampledfunDerivative, round(r1 / r2));
38  dsp1.fillarray(N, m_ft1);
39  for_each(m_ft1, m_ft1 + N, [r1, r2, maxval](double & a) { a *= (r1 / r2) / maxval; });
40 }
41 
42 void EclConfigurationPure::adccountspure_t::AddHit(const double a, const double t0,
44 {
45  total += s.Accumulate(a, t0, c);
46 }
47 
48 double EclConfigurationPure::signalsamplepure_t::Accumulate(const double a, const double t0, double* s) const
49 {
50  // input parameters
51  // a -- signal amplitude
52  // t -- signal offset
53  // output parameter
54  // s -- output array with added signal
55  const double itick = 1 / m_tickPure; // reciprocal to avoid division in usec^-1 (has to be evaluated at compile time)
56  const double tlen = m_nlPure - 1.0 / m_ns; // length of the sampled signal in ADC clocks units
57  const double tmax = m_tmin + m_nsmp - 1; // upper range of the fit region
58 
59  double t = t0 * itick; // rescale time in usec to ADC clocks
60  double x0 = t, x1 = t + tlen;
61 
62  if (x0 > tmax) return 0; // signal starts after the upper range of output array -- do nothing
63  if (x0 < m_tmin) {
64  if (x1 < m_tmin) return 0; // signal ends before lower range of output array -- do nothing
65  x0 = m_tmin; // adjust signal with range of output array
66  }
67 
68  int imax = m_nsmp; // length of sampled signal is long enough so
69  // the last touched element is the last element
70  // of the output array
71  if (x1 < tmax) { // if initial time is too early we need to adjust
72  // the last touched element of output array to avoid
73  // out-of-bound situation in m_ft
74  imax = x1 - m_tmin; // imax is always positive so floor can be
75  // replace by simple typecast
76  imax += 1; // like s.end()
77  }
78 
79  double epsilon = 1.0 / m_ns / 10.;
80  double imind = ceil(x0 - m_tmin + epsilon); // store result in double to avoid int->double conversion below
81  // the ceil function today at modern CPUs is surprisingly fast (before it was horribly slow)
82  int imin = imind; // starting point to fill output array
83  double w = ((m_tmin - t) + imind - 1) * double(m_ns);
84  int jmin = w ; // starting point in sampled signal array
85  w -= jmin;
86 
87  // use linear interpolation between samples. Since signal samples
88  // are aligned with output samples only two weights are need to
89  // calculate to fill output array
90  const double w1 = a * w, w0 = a - w1;
91  double sum = 0;
92  //cout <<"Filling energy: " << a << " time " << t << endl;
93  //cout <<"imin: " << imin << " imax: " << imax << endl;
94  for (int i = imin, j = jmin; i < imax; i++, j += m_ns) {
95  double amp = 0;
96  if (j >= 0) amp = w0 * m_ft[j] + w1 * m_ft[j + 1];
97  //double amp = a * m_ft[j];
98  // cout << i << ":" << j << " " << m_ft[j] << " " << w * m_ft[j] + (1-w) * m_ft[j+1] << endl;
99  s[i] += amp;
100  sum += amp;
101  }
102  //cout << endl;
103  return sum * m_sumscale;
104 }
static constexpr double m_tick
== 72/127 digitization clock tick (in microseconds ???)
static constexpr int m_ntrg
number of trigger counts per ADC clock tick
Abstract base class for different kinds of events.
a struct for a signal sample for the pure CsI calorimeter