12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 #include "cetlib_except/coded_exception.h"
14 #include "larvecutils/MarqFitAlg/MarqFitAlg.h"
26 LArFFTW(
int transformSize,
const void* fplan,
const void* rplan,
int fitbins);
29 template <
class T>
void DoFFT(std::vector<T>& input);
36 template <
class T>
void Convolute(std::vector<T>& func, std::vector<T>& resp);
40 template <
class T>
void Deconvolute(std::vector<T>& func, std::vector<T>& resp);
44 template <
class T>
void Correlate(std::vector<T>& func, std::vector<T>& resp);
47 template <
class T>
void ShiftData(std::vector<T> & input,
double shift);
49 template <
class T>
void AlignedSum(std::vector<T> & input, std::vector<T> &
output,
51 template <
class T> T
PeakCorrelation(std::vector<T> &shape1,std::vector<T> &shape2);
79 for(
size_t p = 0;
p < input.size(); ++
p){
80 ((
double *)
fIn)[
p] = input[
p];
84 fftw_execute_dft_r2c((fftw_plan)
fPlan,(
double*)
fIn,(fftw_complex*)
fOut);
95 for(
size_t p = 0;
p < input.size(); ++
p){
96 ((
double *)fIn)[
p] = input[
p];
100 fftw_execute_dft_r2c((fftw_plan)fPlan,(
double*)fIn,(fftw_complex*)fOut);
102 for(
int i = 0; i < fFreqSize; ++i){
103 output[i].real(((fftw_complex*)fOut)[i][0]);
104 output[i].imag(((fftw_complex*)fOut)[i][1]);
116 fftw_execute_dft_c2r((fftw_plan)rPlan,(fftw_complex*)rIn,(
double*)rOut);
119 double factor = 1.0/(double) fSize;
120 const double *
array = (
const double*)(rOut);
121 for(
int i = 0; i < fSize; ++i){
122 output[i] = factor*array[i];
134 for(
int i = 0; i < fFreqSize; ++i){
135 ((fftw_complex*)rIn)[i][0] = input[i].real();
136 ((fftw_complex*)rIn)[i][1] = input[i].imag();
140 fftw_execute_dft_c2r((fftw_plan)rPlan,(fftw_complex*)rIn,(
double*)rOut);
143 double factor = 1.0/(double) fSize;
144 const double *
array = (
const double*)(rOut);
145 for(
int i = 0; i < fSize; ++i){
146 output[i] = factor*array[i];
162 throw cet::exception(
"LArFFTW") <<
"Bad time series size = " << n <<
"\n";
166 throw cet::exception(
"LArFFTW") <<
"Bad kernel size = " << n <<
"\n";
172 for(
int i = 0; i < fFreqSize; ++i){
173 double re = ((fftw_complex*)fOut)[i][0];
174 double im = ((fftw_complex*)fOut)[i][1];
175 ((fftw_complex*)rIn)[i][0] = re*kern[i].real()-im*kern[i].imag();
176 ((fftw_complex*)rIn)[i][1] = re*kern[i].imag()+im*kern[i].real();
187 std::vector<T>& func2){
190 int n = func1.size();
192 throw cet::exception(
"LArFFTW") <<
"Bad 1st time series size = " << n <<
"\n";
196 throw cet::exception(
"LArFFTW") <<
"Bad 2nd time series size = " << n <<
"\n";
200 for(
int i = 0; i < fFreqSize; ++i){
201 fKern[i].real(((fftw_complex*)fOut)[i][0]);
202 fKern[i].imag(((fftw_complex*)fOut)[i][1]);
207 for(
int i = 0; i < fFreqSize; ++i){
208 double re = ((fftw_complex*)fOut)[i][0];
209 double im = ((fftw_complex*)fOut)[i][1];
210 ((fftw_complex*)rIn)[i][0] = re*fKern[i].real()-im*fKern[i].imag();
211 ((fftw_complex*)rIn)[i][1] = re*fKern[i].imag()+im*fKern[i].real();
227 throw cet::exception(
"LArFFTW") <<
"Bad time series size = " << n <<
"\n";
231 throw cet::exception(
"LArFFTW") <<
"Bad kernel size = " << n <<
"\n";
238 for(
int i = 0; i < fFreqSize; ++i){
239 a = ((fftw_complex*)fOut)[i][0];
240 b = ((fftw_complex*)fOut)[i][1];
244 ((fftw_complex*)rIn)[i][0] = (a*c+b*d)*e;
245 ((fftw_complex*)rIn)[i][1] = (b*c-a*d)*e;
256 std::vector<T>& resp){
261 throw cet::exception(
"LArFFTW") <<
"Bad 1st time series size = " << n <<
"\n";
265 throw cet::exception(
"LArFFTW") <<
"Bad 2nd time series size = " << n <<
"\n";
269 for(
int i = 0; i < fFreqSize; ++i){
270 fKern[i].real(((fftw_complex*)fOut)[i][0]);
271 fKern[i].imag(((fftw_complex*)fOut)[i][1]);
277 for(
int i = 0; i < fFreqSize; ++i){
278 a = ((fftw_complex*)fOut)[i][0];
279 b = ((fftw_complex*)fOut)[i][1];
283 ((fftw_complex*)rIn)[i][0] = (a*c+b*d)*e;
284 ((fftw_complex*)rIn)[i][1] = (b*c-a*d)*e;
301 throw cet::exception(
"LArFFTW") <<
"Bad time series size = " << n <<
"\n";
305 throw cet::exception(
"LArFFTW") <<
"Bad kernel size = " << n <<
"\n";
311 for(
int i = 0; i < fFreqSize; ++i){
312 double re = ((fftw_complex*)fOut)[i][0];
313 double im = ((fftw_complex*)fOut)[i][1];
314 ((fftw_complex*)rIn)[i][0] = re*kern[i].real()+im*kern[i].imag();
315 ((fftw_complex*)rIn)[i][1] = -re*kern[i].imag()+im*kern[i].real();
327 std::vector<T>& func2){
330 int n = func1.size();
332 throw cet::exception(
"LArFFTW") <<
"Bad 1st time series size = " << n <<
"\n";
336 throw cet::exception(
"LArFFTW") <<
"Bad 2nd time series size = " << n <<
"\n";
340 for(
int i = 0; i < fFreqSize; ++i){
341 fKern[i].real(((fftw_complex*)fOut)[i][0]);
342 fKern[i].imag(((fftw_complex*)fOut)[i][1]);
347 for(
int i = 0; i < fFreqSize; ++i){
348 double re = ((fftw_complex*)fOut)[i][0];
349 double im = ((fftw_complex*)fOut)[i][1];
350 ((fftw_complex*)rIn)[i][0] = re*fKern[i].real()+im*fKern[i].imag();
351 ((fftw_complex*)rIn)[i][1] = -re*fKern[i].imag()+im*fKern[i].real();
364 DoFFT(input,fCompTemp);
365 ShiftData(fCompTemp,shift);
366 DoInvFFT(fCompTemp,input);
377 std::vector<T> & shape2,
380 double shift = PeakCorrelation(shape1,shape2);
382 ShiftData(shape1,shift);
384 if(add)
for(
int i = 0; i < fSize; i++) shape1[i]+=shape2[i];
394 std::vector<T> & shape2)
396 float chiSqr = std::numeric_limits<float>::max();
397 float dchiSqr = std::numeric_limits<float>::max();
398 const float chiCut = 1
e-3;
400 std::vector<float>
p;
402 std::vector<T> holder = shape1;
403 Correlate(holder,shape2);
405 int maxT = max_element(holder.begin(), holder.end())-holder.begin();
406 float startT = maxT-fFitBins/2;
409 for(
int i = 0; i < fFitBins; i++) {
410 if(startT+i < 0) offset=fSize;
411 else if(startT+i > fSize) offset=-fSize;
413 if(holder[i+startT+offset]<=0.) {
416 fConvHist[i]=holder[i+startT+
offset];
420 p[0] = *max_element(fConvHist.begin(), fConvHist.end());
429 fitResult=fMarqFitAlg->mrqdtfit(lambda, &p[0], &fConvHist[0], 3, fFitBins, chiSqr, dchiSqr);
432 mf::LogWarning(
"LArFFTW") <<
"Peak Correlation Fitting failed";
434 }
else if (trial>100){
438 while (fabs(dchiSqr) >= chiCut);
439 if (!fitResult)p1=p[1];
441 return p1 + 0.5 + startT;
std::vector< double > DoubleVector
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
double lambda(double a, double b, double c)
std::vector< std::complex< double >> ComplexVector
LArFFTW(int transformSize, const void *fplan, const void *rplan, int fitbins)
T PeakCorrelation(std::vector< T > &shape1, std::vector< T > &shape2)
void AlignedSum(std::vector< T > &input, std::vector< T > &output, bool add=true)
std::vector< float > fConvHist
void Deconvolute(std::vector< T > &func, const ComplexVector &kern)
then echo ***************************************echo array
void ShiftData(ComplexVector &input, double shift)
void Convolute(std::vector< T > &func, const ComplexVector &kern)
void Correlate(std::vector< T > &func, const ComplexVector &kern)
BEGIN_PROLOG sequence::SlidingWindowTriggerPatternsOppositeWindows END_PROLOG simSlidingORM6O6 effSlidingORW output
gshf::MarqFitAlg * fMarqFitAlg
std::vector< float > FloatVector
void DoInvFFT(std::vector< T > &output)
void DoFFT(std::vector< T > &input)
physics associatedGroupsWithLeft p1