10 #ifndef _HELPERFUNCTIONS_H_
11 #define _HELPERFUNCTIONS_H_
51 const uint32_t instrument,
52 const size_t& ChannelNumber)
57 ACQIRIS::Device::instruments_t::const_iterator instrumentIt
58 (device.instruments().find(instrument));
59 if (instrumentIt == device.instruments().end())
60 throw invalid_argument(
"extactRightChannel(): The requested Instrument '" +
61 toString(instrument) +
"' is not in the datastream");
63 (instrumentIt->second.channels());
64 if ((ChannelNumber >= instrumentChannels.size()))
65 throw invalid_argument(
"extactRightChannel(): The requested channel '" +
66 toString(ChannelNumber) +
"' does not exist in Instrument '" +
68 return &(instrumentChannels[ChannelNumber]);
86 void linearRegression(
const size_t nbrPoints,
const double x[],
const double y[],
double &m,
double &c)
88 double SumXsq=0.,SumX=0.,SumY=0.,SumXY=0.;
89 for (
size_t i=0;i<nbrPoints;++i)
94 SumXsq += (x[i]*x[i]);
97 double a1 = ((SumX*SumX) - (nbrPoints*SumXsq));
99 m = ((SumX*SumY) - (nbrPoints*SumXY)) / a1;
100 c = ((SumX*SumXY) - (SumY*SumXsq)) / a1;
122 double SumXsq=0.,SumX=0.,SumY=0.,SumXY=0.,SumWeight=0.;
123 for (
size_t i=0;i<nbrPoints;++i)
125 double weight = (fabs(x[i]-correctX) > 1e-10) ? 1./fabs(x[i]-correctX): 100.;
127 SumX += (x[i]*weight);
128 SumY += (y[i]*weight);
129 SumXY += (x[i]*y[i]*weight);
130 SumXsq += (x[i]*x[i]*weight);
133 double a1 = ((SumX*SumX) - (SumWeight*SumXsq));
135 m = ((SumX*SumY) - (SumWeight*SumXY)) / a1;
136 c = ((SumX*SumXY) - (SumY*SumXsq)) / a1;
157 template <
typename T>
160 double f_x0_x1 = (y[1]-y[0]) / (x[1]-x[0]);
161 double f_x1_x2 = (y[2]-y[1]) / (x[2]-x[1]);
162 double f_x2_x3 = (y[3]-y[2]) / (x[3]-x[2]);
164 double f_x0_x1_x2 = (f_x1_x2 - f_x0_x1) / (x[2]-x[0]);
165 double f_x1_x2_x3 = (f_x2_x3 - f_x1_x2) / (x[3]-x[1]);
167 double f_x0_x1_x2_x3 = (f_x1_x2_x3 - f_x0_x1_x2) / (x[3]-x[0]);
171 coeff[2] = f_x0_x1_x2;
172 coeff[3] = f_x0_x1_x2_x3;
188 template <
typename T>
191 double returnValue = coeff[3];
192 returnValue = returnValue * (X - x[2]) + coeff[2];
193 returnValue = returnValue * (X - x[1]) + coeff[1];
194 returnValue = returnValue * (X - x[0]) + coeff[0];
220 template <
typename T>
221 double findXForGivenY(
const double *
x,
const double * coeff,
const double Y,
const double Start)
224 typedef std::pair<double,double> punkt_t;
226 punkt_t Low(x[1], evalNewtonPolynomial<T>(x,coeff,x[1]));
227 punkt_t Up (x[2], evalNewtonPolynomial<T>(x,coeff,x[2]));
230 punkt_t p (Start, evalNewtonPolynomial<T>(x,coeff,Start));
238 bool Neg = (Low.second > Up.second)?
true:
false;
245 else if (p.second < Y)
254 else if (p.second < Y)
261 while((Up.first-Low.first) > 0.005)
265 p.first = 0.5 * (Up.first+Low.first);
267 p.second = evalNewtonPolynomial<T>(
x,coeff,p.first);
273 else if (p.second < Y)
282 else if (p.second < Y)
291 return ((Up.first + Low.first)*0.5);
302 template <
typename T>
306 const double vGain (c.
gain());
307 const int32_t vOff (static_cast<int32_t>(c.
offset() / vGain));
308 const size_t wLength (c.
waveform().size());
309 const int maxposval (static_cast<int>(s[
maxpos]+0.1));
314 const double HalfMax = 0.5*s[
maximum];
317 for (
int i(maxposval); i>=0; --i)
319 if (abs(
Data[i]-vOff) < HalfMax)
327 for (
size_t i(maxposval); i<wLength;++i)
329 if (abs(
Data[i]-vOff) < HalfMax)
340 if (!fwhm_r || !fwhm_l)
345 lx[0] = fwhm_l-2; ly[0] = abs(
Data[fwhm_l-2]-vOff);
346 lx[1] = fwhm_l-1; ly[1] = abs(
Data[fwhm_l-1]-vOff);
347 lx[2] = fwhm_l-0; ly[2] = abs(
Data[fwhm_l-0]-vOff);
348 lx[3] = fwhm_l+1; ly[3] = abs(
Data[fwhm_l+1]-vOff);
352 rx[0] = fwhm_r-1; ry[0] = abs(
Data[fwhm_r-1]-vOff);
353 rx[1] = fwhm_r-0; ry[1] = abs(
Data[fwhm_r-0]-vOff);
354 rx[2] = fwhm_r+1; ry[2] = abs(
Data[fwhm_r+1]-vOff);
355 rx[3] = fwhm_r+2; ry[3] = abs(
Data[fwhm_r+2]-vOff);
357 double mLeft,cLeft,mRight,cRight;
358 linearRegression<T>(4,lx,ly,mLeft,cLeft);
359 linearRegression<T>(4,rx,ry,mRight,cRight);
362 const double fwhm_L = (HalfMax-cLeft)/mLeft;
363 const double fwhm_R = (HalfMax-cRight)/mRight;
366 s[
fwhm] = fwhm_R-fwhm_L;
403 template <
typename T>
408 const double vGain (c.
gain());
409 const int32_t vOff (static_cast<int32_t>(c.
offset() / vGain));
410 const double horpos (c.
horpos()*1.e9);
412 const int32_t
threshold (static_cast<int32_t>(thresh/vGain));
417 const size_t start (static_cast<size_t>(s[
startpos]+0.1));
418 const size_t stop (static_cast<size_t>(s[
stoppos]+0.1));
420 for (
size_t i = start; i<=stop;++i)
422 integralval += (abs(
Data[i]-vOff)-threshold);
423 wichtung += ((abs(
Data[i]-vOff)-threshold)*i);
425 s[
com] = (wichtung/integralval + horpos)*sampleInterval;
439 template <
typename T>
443 const double vGain (c.
gain());
444 const int32_t vOff (static_cast<int32_t>(c.
offset()/vGain));
445 const int32_t wLength (c.
waveform().size());
447 const double horpos (c.
horpos()*1e9);
448 const int32_t
threshold (static_cast<int32_t>(thresh/vGain));
452 const int32_t center (static_cast<int32_t>((s[
time]/sampInt - horpos)+0.1));
457 for (i = center; i>=0; --i)
458 if ((abs(
Data[i]-vOff) < threshold) || (((
Data[i]-vOff) * (
Data[i+1]-vOff)) < 0) )
464 for (i = center; i< wLength; ++i)
465 if ((abs(
Data[i]-vOff) < threshold) || (((
Data[i]-vOff) * (
Data[i-1]-vOff)) < 0) )
481 template <
typename T>
485 const double vGain (c.
gain());
486 const int32_t vOff (static_cast<int32_t>(c.
offset()/vGain));
488 const size_t start (static_cast<size_t>(s[
startpos]+0.1));
489 const size_t stop (static_cast<size_t>(s[
stoppos]+0.1));
490 double& maximumval (s[
maximum]);
491 double& maxposval (s[
maxpos]);
494 for (
size_t i(start); i<=stop;++i)
496 if (abs(
Data[i]-vOff) > maximumval)
498 maximumval = abs(
Data[i]-vOff);
503 s[
height] = maximumval * vGain;
std::vector< int16_t > waveform_t
define the waveform
Event to store all LCLS Data.
double findXForGivenY(const double *x, const double *coeff, const double Y, const double Start)
Achims Numerical Approximation.
file contains the classes that describe how to analyze the waveform and stores the result...
waveform_t & waveform()
setter
void gewichtetlinearRegression(const size_t nbrPoints, const double x[], const double y[], const double correctX, double &m, double &c)
A weighted linear Regression.
void startstop(const Channel &c, SignalProducer::signal_t &s, const double &thresh)
find start and stop of pulse
const Channel * extactRightChannel(const CASSEvent &evt, const uint32_t instrument, const size_t &ChannelNumber)
extracts the requested channel from the data
file contains declaration of the CASSEvent
file contains the classes that describe an acqiris channel
double & sampleInterval()
setter
void createNewtonPolynomial(const double *x, const double *y, double *coeff)
create Newton Polynomial
std::vector< double > signal_t
devices_t & devices()
setters
Binary function for thresholding.
double evalNewtonPolynomial(const double *x, const double *coeff, double X)
evaluate Newton Polynomial
void linearRegression(const size_t nbrPoints, const double x[], const double y[], double &m, double &c)
linear Regression
file contains the declaration of the acqiris part of the CASSEvent
std::string toString(const Type &t)
convert any type to a string
void getmaximum(const Channel &c, SignalProducer::signal_t &s, const double &)
find Maximum of puls and calcs the height
A Channel of an Acqiris Instrument.
void getfwhm(const Channel &c, SignalProducer::signal_t &s, const double &)
extract full width at half maximum (fwhm)
std::vector< Channel > channels_t
a vector of Channels
void CoM(const Channel &c, SignalProducer::signal_t &s, const double &thresh)
Center of Mass.