9#include <top/utilities/TOPTemplateFitter.h>
10#include <top/geometry/TOPGeometryPar.h>
11#include <framework/logging/Logger.h>
16using namespace Belle2::TOP;
30 const double averageRMS)
31 : m_wf(wf), m_sampleTimes(sampleTimes), m_averageRMS(averageRMS)
63 B2FATAL(
"rising edge of template function invalid!");
68 const auto& tdc = geo->getNominalTDC();
69 const double dt = tdc.getSampleWidth();
79 const double fitRange)
98 int minidx = std::distance(std::begin(
m_chisq_vec), it);
99 if (minidx > 0 && minidx < (
int)
m_chisq_vec.size() - 1) {
110 double denom = (p1.first - p2.first) * (p1.first - p3.first) * (p2.first - p3.first);
111 double a = (p3.first * (p2.second - p1.second) + p2.first * (p1.second - p3.second) + p1.first *
112 (p3.second - p2.second)) / denom;
113 double b = (p3.first * p3.first * (p1.second - p2.second) + p2.first * p2.first * (p3.second - p1.second)
114 + p1.first * p1.first * (p2.second - p3.second)) / denom;
115 double c = (p2.first * p3.first * (p2.first - p3.first) * p1.second + p3.first * p1.first * (p3.first - p1.first) * p2.second
116 + p1.first * p2.first * (p1.first - p2.first) * p3.second) / denom;
118 vertex.first = -b / (2 * a);
119 vertex.second = c - b * b / (4 * a);
123 const std::vector<float>& timingCorrection,
const double risingEdgeCFD,
const double fitRange)
125 if (samples.size() != pedestals.size() || samples.size() != timingCorrection.size()) {
126 B2FATAL(
"Size of sample, pedestal and timing correction vectors has to be equal!");
137 for (
int signalSampleIndex = risingEdgeCFD - fitRange; signalSampleIndex < risingEdgeCFD + fitRange; ++signalSampleIndex) {
138 if (signalSampleIndex < 0 || signalSampleIndex > (
int)samples.size() - 1)
continue;
139 int templateIndex = signalSampleIndex *
s_templateResolution - totalTemplateOffset + timingCorrection[signalSampleIndex];
140 if (templateIndex < 0 || templateIndex > (
int)
s_templateSamples.size() - 1)
continue;
141 double weight = 1. / (pedestals[signalSampleIndex] * pedestals[signalSampleIndex]);
143 double Sy = weight * samples[signalSampleIndex];
149 sums.
Syy += Sy * samples[signalSampleIndex];
167 const double determinant = sums.
S1 * sums.
Sxx - sums.
Sx * sums.
Sx;
168 result.amplitude = (-sums.
Sx * sums.
Sy + sums.
S1 * sums.
Sxy) / determinant;
169 result.backgroundOffset = (sums.
Sxx * sums.
Sy - sums.
Sx * sums.
Sxy) / determinant;
170 result.amplitudeError = sums.
S1 / determinant;
171 result.backgroundOffsetError = sums.
Sxx / determinant;
172 return sums.
Syy - result.backgroundOffset * sums.
Sy - result.amplitude * sums.
Sxy;
const TOPSignalShape & getSignalShape() const
Returns single photon signal shape.
Calibration constants of a singe ASIC channel: time axis (sample times)
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
static int s_fineOffsetRange
range for offset between template and signal
static void InitializeTemplateFit()
Intializes the template fit using default values.
static TemplateParameters s_templateParameters
parameters used for the template calculation
std::vector< double > m_chisq_vec
all computed chi square values from template fit
const TOPRawWaveform m_wf
raw sampled waveforms
void PerformTemplateFitMinimize(const std::vector< short > &samples, const std::vector< short > &pedestals, const std::vector< float > &timingCorrection, const double risingEdgeCFD, const double fitRange)
performs the template fit
void performTemplateFit(const double risingEdgeStart, const double fitRange)
Prepares data and performs the template fit in sample space.
static bool s_templateReInitialize
flag showing that the template samples have to be recomputed
const double m_averageRMS
average RMS of waveform samples, no database for this
static int s_templateResolution
resolution of template with respect to normal sample spacing
void CalculateParabolaVertex(const Point &p1, const Point &p2, const Point &p3, Point &vertex)
Calculate vertex coordinates of parabola given three data points.
static void setTemplateParameters(const TemplateParameters ¶ms)
Sets the template parameters.
double m_chisq
chi square value from template fit
static void setTemplateSamples(int nSamples)
Set the total number of template samples.
FitResult m_result
fit result from template fit
TOPTemplateFitter(const TOPRawWaveform &wf, const TOPSampleTimes &sampleTimes, const double averageRMS)
full constructor
double ComputeMinimizedParametersAndChisq(const MinimizationSums &sums, FitResult &result)
Compute the minimized parameters and chi square value.
static std::vector< double > s_templateSamples
precomputed template samples
static bool s_useParabola
try improving fit by making use of parabolic shape of chisq values
static void setTemplateResolution(int resolution)
Set the template resolution.
static int s_totalTemplateSamples
number of samples used for template
Abstract base class for different kinds of events.
structure holding values from template fit
double amplitude
fitted amplitude
double amplitudeError
fitted amplitude error
double risingEdge
fitted rising edge
Variables used during template fit minimization.
double Sxy
sum of signal sample *template*weight
double Syy
sum of signal sample * signal sample * weight
double Sx
sum of template*weight
double Sxx
sum of template*template*weight
void clear()
set sums used during template fit to initial values
double Sy
sum of signal sample * weight
double S1
sum of sample weights
Parameters of the template function.
double amplitude
amplitude of template function
double risingEdge
rising edge position of template in samples