10#ifndef IMPBFF_DECAYCONVOLUTION_H
11#define IMPBFF_DECAYCONVOLUTION_H
13#include <IMP/bff/bff_config.h>
20#include <IMP/bff/DecayRoutines.h>
22#include <IMP/bff/DecayCurve.h>
23#include <IMP/bff/DecayModifier.h>
24#include <IMP/bff/DecayLifetimeHandler.h>
47 double irf_shift_channels,
48 double irf_background_counts
51 std::clog <<
"DecayConvolution::compute_corrected_irf:" << std::endl;
52 std::clog <<
"-- irf_shift_channels:" << irf_shift_channels << std::endl;
53 std::clog <<
"-- irf_background_counts:" << irf_background_counts << std::endl;
54 std::clog <<
"-- irf->ptr():" << irf << std::endl;
55 std::clog <<
"-- irf->size():" << irf->
size() << std::endl;
56 std::clog <<
"-- corrected_irf->ptr():" << corrected_irf << std::endl;
57 std::clog <<
"-- corrected_irf->size():" << corrected_irf->
size() << std::endl;
60 for (
size_t i = 0; i < irf->
size(); i++)
61 corrected_irf->_y[i] = std::max(irf->_y[i] - irf_background_counts, 0.0);
62 corrected_irf->
set_shift(irf_shift_channels);
81 std::vector<double> irf_histogram,
82 std::vector<double> decay_histogram,
83 double micro_time_resolution
86 std::clog <<
"DecayConvolution::compute_mean_lifetime" << std::endl;
87 std::clog <<
"-- micro_time_resolution:" << micro_time_resolution << std::endl;
88 std::clog <<
"-- irf_histogram.size():" << irf_histogram.size() << std::endl;
89 std::clog <<
"-- decay_histogram.size():" << decay_histogram.size() << std::endl;
91 double m0_irf = std::accumulate(irf_histogram.begin(), irf_histogram.end(),0.0);
92 double m0_data = std::accumulate(decay_histogram.begin(), decay_histogram.end(), 0.0);
94 std::clog <<
"-- m0_irf:" << m0_irf << std::endl;
95 std::clog <<
"-- m0_data:" << m0_data << std::endl;
99 for(
size_t i = 0; i<irf_histogram.size(); i++)
100 m1_irf += (
double) i * irf_histogram[i];
101 for(
size_t i = 0; i<decay_histogram.size(); i++)
102 m1_data += (
double) i * decay_histogram[i];
104 std::clog <<
"-- m1_irf:" << m1_irf << std::endl;
105 std::clog <<
"-- m1_data:" << m1_data << std::endl;
107 double g1 = m0_data / m0_irf;
108 double g2 = (m1_data - g1 * m1_irf) / m0_irf;
109 double tau1 = g2 / g1 * micro_time_resolution;
122 double irf_shift_channels = 0.0;
125 double irf_background_counts = 0.0;
128 int convolution_method = 0;
131 double excitation_period = std::numeric_limits<double>::max();
134 bool corrected_irf_valid =
false;
136 void convolve_lifetimes(
DecayCurve* decay,
bool zero_fill =
true){
139 auto lt = lh.data();
int ln = lh.size();
156 double* my = decay->
get_y().data();
157 int nm = (int) decay->
get_y().size();
158 double* mx = decay->
get_x().data();
161 double* iy = irfc->get_y().data();
162 int ni = (int) irfc->get_y().size();
166 std::fill(my, my + nm, 0.0);
185 }
else if (cm ==
FAST) {
194 std::clog <<
"DecayConvolution::convolve_lifetimes" << std::endl;
195 std::clog <<
"-- convolution_method: " << cm << std::endl;
196 std::clog <<
"-- convolution start, stop: " << start <<
", " << stop << std::endl;
197 std::clog <<
"-- histogram spacing (dt): " << dt << std::endl;
198 std::clog <<
"-- excitation_period: " << ex_per << std::endl;
199 std::clog <<
"-- lifetime spectrum: ";
for(
auto &v: l) std::clog << v <<
", "; std::clog <<
"\n";
201 std::clog << std::endl;
209 corrected_irf->
set_x(irf->x);
210 corrected_irf->
set_y(irf->y);
215 if(!corrected_irf_valid) {
221 corrected_irf_valid =
true;
241 double n_channels = std::max(1., (
double)
get_irf()->size());
242 irf_shift_channels = std::fmod(v, n_channels);
243 corrected_irf_valid =
false;
247 return irf_shift_channels;
251 irf_background_counts = v;
252 corrected_irf_valid =
false;
256 return irf_background_counts;
261 return *corrected_irf;
278 convolution_method = std::max(0, std::min(5, v));
282 return convolution_method;
286 excitation_period = v;
290 return excitation_period;
295 auto data = decay->y;
302 double excitation_period = 100,
303 double irf_shift_channels = 0.0,
304 double irf_background_counts = 0
314 DecayCurve *instrument_response_function =
nullptr,
315 int convolution_method =
FAST,
316 double excitation_period = 100,
317 double irf_shift_channels = 0.0,
318 double irf_background_counts = 0,
319 int start = 0,
int stop = -1,
321 ) :
DecayModifier(instrument_response_function, start, stop, active){
323 std::clog <<
"DecayConvolution::DecayConvolution" << std::endl;
329 this->lifetime_handler = lifetime_handler;
334 irf_background_counts
339 delete corrected_irf;
346 convolve_lifetimes(out);
IMPBFFEXPORT void decay_fconv(double *fit, double *x, double *lamp, int numexp, int start, int stop, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, low repetition rate)
IMPBFFEXPORT void decay_fconv_avx(double *fit, double *x, double *lamp, int numexp, int start, int stop, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, AVX optimized for large lifeti...
IMPBFFEXPORT void decay_fconv_per_avx(double *fit, double *x, double *lamp, int numexp, int start, int stop, int n_points, double period, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, high repetition rate),...
IMPBFFEXPORT void decay_fconv_cs_time_axis(double *inplace_output, int n_output, double *time_axis, int n_time_axis, double *irf, int n_irf, double *lifetime_spectrum, int n_lifetime_spectrum, int convolution_start=0, int convolution_stop=-1)
IMPBFFEXPORT void decay_fconv_per(double *fit, double *x, double *lamp, int numexp, int start, int stop, int n_points, double period, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, high repetition rate)
IMPBFFEXPORT void decay_fconv_per_cs_time_axis(double *model, int n_model, double *time_axis, int n_time_axis, double *irf, int n_irf, double *lifetime_spectrum, int n_lifetime_spectrum, int convolution_start=0, int convolution_stop=-1, double period=100.0)
Definition DecayConvolution.h:31
void set_irf_shift_channels(double v)
Definition DecayConvolution.h:240
~DecayConvolution() override
Definition DecayConvolution.h:338
double get_irf_shift_channels() const
Definition DecayConvolution.h:246
double get_irf_background_counts() const
Definition DecayConvolution.h:255
DecayCurve & get_corrected_irf()
Definition DecayConvolution.h:259
ConvolutionType
Definition DecayConvolution.h:35
@ FAST_PERIODIC_AVX
Definition DecayConvolution.h:41
@ FAST_TIME
Definition DecayConvolution.h:37
@ FAST
Definition DecayConvolution.h:39
@ FAST_PERIODIC
Definition DecayConvolution.h:38
@ FAST_PERIODIC_TIME
Definition DecayConvolution.h:36
@ FAST_AVX
Definition DecayConvolution.h:40
void set_irf_background_counts(double v)
Definition DecayConvolution.h:250
double get_excitation_period() const
Definition DecayConvolution.h:289
void set_irf(DecayCurve *v)
Definition DecayConvolution.h:229
static double compute_mean_lifetime(std::vector< double > irf_histogram, std::vector< double > decay_histogram, double micro_time_resolution)
Definition DecayConvolution.h:80
static void compute_corrected_irf(DecayCurve *irf, DecayCurve *corrected_irf, double irf_shift_channels, double irf_background_counts)
Definition DecayConvolution.h:44
DecayConvolution(DecayLifetimeHandler *lifetime_handler=nullptr, DecayCurve *instrument_response_function=nullptr, int convolution_method=FAST, double excitation_period=100, double irf_shift_channels=0.0, double irf_background_counts=0, int start=0, int stop=-1, bool active=true)
Definition DecayConvolution.h:312
double get_mean_lifetime(DecayCurve *decay)
Definition DecayConvolution.h:293
void add(DecayCurve *out) override
Definition DecayConvolution.h:342
int get_convolution_method() const
Definition DecayConvolution.h:281
DecayCurve * get_irf()
Definition DecayConvolution.h:233
void set_convolution_method(int v)
The method used for convolution.
Definition DecayConvolution.h:277
void set_excitation_period(double v)
Definition DecayConvolution.h:285
void set(int convolution_method=FAST_PERIODIC_TIME, double excitation_period=100, double irf_shift_channels=0.0, double irf_background_counts=0)
Definition DecayConvolution.h:300
Class for fluorescence decay curves.
Definition DecayCurve.h:38
std::vector< double > & get_y()
Returns the y values of the decay curve.
void set_shift(double v)
Sets the time shift of the decay curve.
void set_y(std::vector< double > &v)
Sets the y values of the decay curve.
std::vector< double > & get_x()
Get the x-values of the curve.
double get_average_dx()
Get the average x-value difference.
void set_x(const std::vector< double > &v)
size_t size() const
Get the size of the curve.
void resize(size_t n, double v=0.0, double dx=1.0)
Resize the curve.
Definition DecayLifetimeHandler.h:24
std::vector< double > & get_lifetime_spectrum()
A decorator that modifies a DecayCurve within a specified range.
Definition DecayModifier.h:29
DecayCurve * data
Definition DecayModifier.h:35
DecayCurve * default_data
Definition DecayModifier.h:36
virtual DecayCurve * get_data()
virtual void set_data(DecayCurve *v)
size_t get_stop(DecayCurve *d=nullptr) const
Get the stop index of the decay range.
size_t get_start(DecayCurve *d=nullptr) const
Get the start index of the decay range.