Belle II Software development
TOPTemplateFitter Class Reference

Class to perform template fit on TOP waveform data Minimzation method is described here http://wwwa1.kph.uni-mainz.de/Vorlesungen/SS11/Statistik/. More...

#include <TOPTemplateFitter.h>

Classes

struct  FitResult
 structure holding values from template fit More...
 
struct  MinimizationSums
 Variables used during template fit minimization. More...
 
struct  TemplateParameters
 Parameters of the template function. More...
 

Public Member Functions

 TOPTemplateFitter (const TOPRawWaveform &wf, const TOPSampleTimes &sampleTimes, const double averageRMS)
 full constructor
 
 ~TOPTemplateFitter ()
 default destructor
 
double getChisq () const
 Returns fit chi square.
 
const std::vector< double > & getChisqVector () const
 Returns calculated chisq values.
 
const FitResultgetFitResult () const
 Returns fitted values with errors.
 
void performTemplateFit (const double risingEdgeStart, const double fitRange)
 Prepares data and performs the template fit in sample space.
 

Static Public Member Functions

static TemplateParametersgetTemplateParameters ()
 Returns the template parameters.
 
static int getTemplateSamples ()
 Returns the total number of template samples.
 
static int getTemplateResolution ()
 Returns the template resolution.
 
static bool getUseParabola ()
 Returns useParabola.
 
static void setTemplateParameters (const TemplateParameters &params)
 Sets the template parameters.
 
static void setTemplateSamples (int nSamples)
 Set the total number of template samples.
 
static void setTemplateResolution (int resolution)
 Set the template resolution.
 
static void setUseParabola (bool use)
 Enable Usage of parabola improvement.
 
static void InitializeTemplateFit ()
 Intializes the template fit using default values.
 

Private Member Functions

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
 
double ComputeMinimizedParametersAndChisq (const MinimizationSums &sums, FitResult &result)
 Compute the minimized parameters and chi square value.
 
void CalculateParabolaVertex (const Point &p1, const Point &p2, const Point &p3, Point &vertex)
 Calculate vertex coordinates of parabola given three data points.
 

Private Attributes

const TOPRawWaveform m_wf
 raw sampled waveforms
 
const TOPSampleTimes m_sampleTimes
 provides timing correction
 
const double m_averageRMS
 average RMS of waveform samples, no database for this
 
FitResult m_result
 fit result from template fit
 
double m_chisq = 0
 chi square value from template fit
 
std::vector< double > m_chisq_vec
 all computed chi square values from template fit
 

Static Private Attributes

static int s_totalTemplateSamples = 64
 number of samples used for template
 
static int s_templateResolution = 4
 resolution of template with respect to normal sample spacing
 
static TemplateParameters s_templateParameters
 parameters used for the template calculation
 
static std::vector< double > s_templateSamples
 precomputed template samples
 
static int s_fineOffsetRange = TOPTemplateFitter::s_templateResolution * 5
 range for offset between template and signal
 
static bool s_templateReInitialize = true
 flag showing that the template samples have to be recomputed
 
static bool s_useParabola = true
 try improving fit by making use of parabolic shape of chisq values
 

Detailed Description

Class to perform template fit on TOP waveform data Minimzation method is described here http://wwwa1.kph.uni-mainz.de/Vorlesungen/SS11/Statistik/.

Definition at line 30 of file TOPTemplateFitter.h.

Constructor & Destructor Documentation

◆ TOPTemplateFitter()

TOPTemplateFitter ( const TOPRawWaveform wf,
const TOPSampleTimes sampleTimes,
const double  averageRMS 
)

full constructor

Parameters
wfTOP raw waveform samples
sampleTimesdatabase object holding time correction
averageRMSaverage sample RMS

Definition at line 28 of file TOPTemplateFitter.cc.

31 : m_wf(wf), m_sampleTimes(sampleTimes), m_averageRMS(averageRMS)
32{
33 m_chisq = -1.;
36 }
37}
static void InitializeTemplateFit()
Intializes the template fit using default values.
const TOPRawWaveform m_wf
raw sampled waveforms
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
double m_chisq
chi square value from template fit
const TOPSampleTimes m_sampleTimes
provides timing correction

◆ ~TOPTemplateFitter()

~TOPTemplateFitter ( )
inline

default destructor

Definition at line 103 of file TOPTemplateFitter.h.

103{};

Member Function Documentation

◆ CalculateParabolaVertex()

void CalculateParabolaVertex ( const Point &  p1,
const Point &  p2,
const Point &  p3,
Point &  vertex 
)
private

Calculate vertex coordinates of parabola given three data points.

Parameters
p1data point 1
p2data point 2
p3data point 3
vertexvertex position

Definition at line 108 of file TOPTemplateFitter.cc.

109{
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;
117
118 vertex.first = -b / (2 * a);
119 vertex.second = c - b * b / (4 * a);
120}

◆ ComputeMinimizedParametersAndChisq()

double ComputeMinimizedParametersAndChisq ( const MinimizationSums sums,
FitResult result 
)
private

Compute the minimized parameters and chi square value.

Parameters
sumsminimization sums for chisq calculation
resultminimized parameters
Returns
chi square

Definition at line 165 of file TOPTemplateFitter.cc.

166{
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;
173}

◆ getChisq()

double getChisq ( ) const
inline

Returns fit chi square.

Returns
fit chi square

Definition at line 109 of file TOPTemplateFitter.h.

109{return m_chisq;}

◆ getChisqVector()

const std::vector< double > & getChisqVector ( ) const
inline

Returns calculated chisq values.

Returns
chisq vector

Definition at line 115 of file TOPTemplateFitter.h.

115{return m_chisq_vec;}
std::vector< double > m_chisq_vec
all computed chi square values from template fit

◆ getFitResult()

const FitResult & getFitResult ( ) const
inline

Returns fitted values with errors.

Returns
fitted values with errors

Definition at line 121 of file TOPTemplateFitter.h.

121{return m_result;}
FitResult m_result
fit result from template fit

◆ getTemplateParameters()

static TemplateParameters & getTemplateParameters ( )
inlinestatic

Returns the template parameters.

Returns
template parameters

Definition at line 127 of file TOPTemplateFitter.h.

127{return s_templateParameters;}
static TemplateParameters s_templateParameters
parameters used for the template calculation

◆ getTemplateResolution()

static int getTemplateResolution ( )
inlinestatic

Returns the template resolution.

Returns
template resolution

Definition at line 139 of file TOPTemplateFitter.h.

139{return s_templateResolution;}
static int s_templateResolution
resolution of template with respect to normal sample spacing

◆ getTemplateSamples()

static int getTemplateSamples ( )
inlinestatic

Returns the total number of template samples.

Returns
total number of template samples

Definition at line 133 of file TOPTemplateFitter.h.

static int s_totalTemplateSamples
number of samples used for template

◆ getUseParabola()

static bool getUseParabola ( )
inlinestatic

Returns useParabola.

Returns
useParabola

Definition at line 145 of file TOPTemplateFitter.h.

145{return s_useParabola;}
static bool s_useParabola
try improving fit by making use of parabolic shape of chisq values

◆ InitializeTemplateFit()

void InitializeTemplateFit ( )
static

Intializes the template fit using default values.

Definition at line 59 of file TOPTemplateFitter.cc.

60{
61 s_templateSamples.clear();
63 B2FATAL("rising edge of template function invalid!");
64 }
65
66 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
67 const auto& signalShape = geo->getSignalShape();
68 const auto& tdc = geo->getNominalTDC();
69 const double dt = tdc.getSampleWidth();
70
71 for (double x = 0; x < s_totalTemplateSamples; x += 1. / s_templateResolution) {
72 s_templateSamples.push_back(s_templateParameters.amplitude * signalShape.getValue((x - s_templateParameters.risingEdge)*dt));
73 }
74
76}
const TOPSignalShape & getSignalShape() const
Returns single photon signal shape.
Definition: TOPGeometry.h:224
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 std::vector< double > s_templateSamples
precomputed template samples
double amplitude
amplitude of template function
double risingEdge
rising edge position of template in samples

◆ performTemplateFit()

void performTemplateFit ( const double  risingEdgeStart,
const double  fitRange 
)

Prepares data and performs the template fit in sample space.

Parameters
risingEdgeStartinitial guess for rising edge position from CFD
fitRangerange of template fit

Definition at line 78 of file TOPTemplateFitter.cc.

80{
83 }//do this here to make sure nobody changed template settings? -> thread safety?
84
85 //access time base correction and calculate correction to waveform sample number
86 vector<short> pedestals(m_wf.getSize(), m_averageRMS);//for now assume constant pedestals for TOP
87 vector<float> timingCorrection(m_wf.getSize(),
88 0.);//timing correction, should be relative to each sample in in units of samples -> pick correct template sample
89
90 //perform template fit
91 m_chisq_vec.clear();
92 m_chisq = 1e6;
93 PerformTemplateFitMinimize(m_wf.getWaveform(), pedestals, timingCorrection, risingEdgeStart, fitRange);
94
95 //use paraboic shape of chi^2 distribution to improve fit result
96 if (s_useParabola) {
97 auto it = std::min_element(std::begin(m_chisq_vec), std::end(m_chisq_vec));
98 int minidx = std::distance(std::begin(m_chisq_vec), it);
99 if (minidx > 0 && minidx < (int)m_chisq_vec.size() - 1) {
100 Point vertex;
101 CalculateParabolaVertex({ -1, m_chisq_vec[minidx - 1]}, {0, m_chisq_vec[minidx]}, {1, m_chisq_vec[minidx + 1]}, vertex);
102 m_chisq = vertex.second;
103 m_result.risingEdge += vertex.first / s_templateResolution;
104 }
105 }
106}
unsigned getSize() const
Returns waveform size.
const std::vector< short > & getWaveform() const
Returns waveform.
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 CalculateParabolaVertex(const Point &p1, const Point &p2, const Point &p3, Point &vertex)
Calculate vertex coordinates of parabola given three data points.

◆ PerformTemplateFitMinimize()

void PerformTemplateFitMinimize ( const std::vector< short > &  samples,
const std::vector< short > &  pedestals,
const std::vector< float > &  timingCorrection,
const double  risingEdgeCFD,
const double  fitRange 
)
private

performs the template fit

Parameters
samplessample vector
pedestalspedestal vector
timingCorrectiontiming correction for samples
risingEdgeCFDrising edge from constant fraction discrimination
fitRangefit range

Definition at line 122 of file TOPTemplateFitter.cc.

124{
125 if (samples.size() != pedestals.size() || samples.size() != timingCorrection.size()) {
126 B2FATAL("Size of sample, pedestal and timing correction vectors has to be equal!");
127 }
128
129
130 MinimizationSums sums;
131 FitResult result;
132 int initialOffset = risingEdgeCFD - s_templateParameters.risingEdge;
133 //offset search loop
134 for (int fineOffset = -s_fineOffsetRange; fineOffset < s_fineOffsetRange; ++fineOffset) {
135 sums.clear();
136 int totalTemplateOffset = initialOffset * s_templateResolution + fineOffset;
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]);
142 double Sx = weight * s_templateSamples[templateIndex];
143 double Sy = weight * samples[signalSampleIndex];
144 sums.S1 += weight;
145 sums.Sx += Sx;
146 sums.Sy += Sy;
147 sums.Sxx += Sx * s_templateSamples[templateIndex];
148 sums.Sxy += Sy * s_templateSamples[templateIndex];
149 sums.Syy += Sy * samples[signalSampleIndex];
150 }
151 double chisq = ComputeMinimizedParametersAndChisq(sums, result);
152 m_chisq_vec.push_back(chisq);
153 if (chisq < m_chisq) { //save minimal chisq result
154 m_chisq = chisq;
155 m_result = result;
156 m_result.risingEdge = totalTemplateOffset;
157 }
158 }
159 //template offset back to sample space and scale amplitude
163}
static int s_fineOffsetRange
range for offset between template and signal
double ComputeMinimizedParametersAndChisq(const MinimizationSums &sums, FitResult &result)
Compute the minimized parameters and chi square value.
double amplitudeError
fitted amplitude error

◆ setTemplateParameters()

void setTemplateParameters ( const TemplateParameters params)
static

Sets the template parameters.

Parameters
paramstemplate Parameters

Definition at line 39 of file TOPTemplateFitter.cc.

40{
41 s_templateParameters = params;
43}

◆ setTemplateResolution()

void setTemplateResolution ( int  resolution)
static

Set the template resolution.

Parameters
resolutiontemplate resolution

Definition at line 52 of file TOPTemplateFitter.cc.

53{
54 s_templateResolution = resolution;
55 s_fineOffsetRange = resolution * 5;
57}

◆ setTemplateSamples()

void setTemplateSamples ( int  nSamples)
static

Set the total number of template samples.

Parameters
nSamplestotal number of template samples

Definition at line 45 of file TOPTemplateFitter.cc.

46{
47 s_totalTemplateSamples = nSamples;
49
50}

◆ setUseParabola()

static void setUseParabola ( bool  use)
inlinestatic

Enable Usage of parabola improvement.

Parameters
useenable/disable usage

Definition at line 169 of file TOPTemplateFitter.h.

169{s_useParabola = use;}

Member Data Documentation

◆ m_averageRMS

const double m_averageRMS
private

average RMS of waveform samples, no database for this

Definition at line 218 of file TOPTemplateFitter.h.

◆ m_chisq

double m_chisq = 0
private

chi square value from template fit

Definition at line 230 of file TOPTemplateFitter.h.

◆ m_chisq_vec

std::vector<double> m_chisq_vec
private

all computed chi square values from template fit

Definition at line 231 of file TOPTemplateFitter.h.

◆ m_result

FitResult m_result
private

fit result from template fit

Definition at line 229 of file TOPTemplateFitter.h.

◆ m_sampleTimes

const TOPSampleTimes m_sampleTimes
private

provides timing correction

Definition at line 217 of file TOPTemplateFitter.h.

◆ m_wf

const TOPRawWaveform m_wf
private

raw sampled waveforms

Definition at line 216 of file TOPTemplateFitter.h.

◆ s_fineOffsetRange

int s_fineOffsetRange = TOPTemplateFitter::s_templateResolution * 5
staticprivate

range for offset between template and signal

Definition at line 224 of file TOPTemplateFitter.h.

◆ s_templateParameters

TOPTemplateFitter::TemplateParameters s_templateParameters
staticprivate

parameters used for the template calculation

Definition at line 222 of file TOPTemplateFitter.h.

◆ s_templateReInitialize

bool s_templateReInitialize = true
staticprivate

flag showing that the template samples have to be recomputed

Definition at line 225 of file TOPTemplateFitter.h.

◆ s_templateResolution

int s_templateResolution = 4
staticprivate

resolution of template with respect to normal sample spacing

Definition at line 221 of file TOPTemplateFitter.h.

◆ s_templateSamples

std::vector< double > s_templateSamples
staticprivate

precomputed template samples

Definition at line 223 of file TOPTemplateFitter.h.

◆ s_totalTemplateSamples

int s_totalTemplateSamples = 64
staticprivate

number of samples used for template

Definition at line 220 of file TOPTemplateFitter.h.

◆ s_useParabola

bool s_useParabola = true
staticprivate

try improving fit by making use of parabolic shape of chisq values

Definition at line 226 of file TOPTemplateFitter.h.


The documentation for this class was generated from the following files: