10 #ifndef ICARUSALG_UTILITIES_SAMPLEDFUNCTION_H
11 #define ICARUSALG_UTILITIES_SAMPLEDFUNCTION_H
19 #include "gsl/gsl_util"
91 template <
typename XType =
double,
typename YType = XType>
100 static constexpr
auto npos = std::numeric_limits<gsl::index>::max();
127 template <
typename Func>
131 gsl::index subsamples = 1
162 template <
typename Func,
typename UntilFunc>
165 gsl::index subsamples,
168 template <
typename Func,
typename UntilFunc>
171 gsl::index subsamples = 1
250 gsl::index
stepIndex(
X_t const x, gsl::index
const iSubsample)
const;
256 {
return (index >= 0) && (index <
size()); }
286 template <
typename Stream>
288 (
Stream&& out, std::string
const& indent, std::string
const& firstIndent)
292 template <
typename Stream>
293 void dump(
Stream&& out, std::string
const& indent =
"")
const
294 {
dump(std::forward<Stream>(out), indent, indent); }
318 (
Function_t const&
function, Range_t
const& range, gsl::index subsamples);
328 {
return fAllSamples.data() + fNSamples *
n; }
339 template <
typename UntilFunc>
350 template <
typename T>
360 template <
typename XType,
typename Func,
typename UntilFunc>
361 SampledFunction(Func
const&, XType, XType, UntilFunc&&, gsl::index, XType)
364 template <
typename XType,
typename Func>
368 template <
typename XType,
typename Func,
typename UntilFunc>
369 SampledFunction(Func
const&, XType, XType, UntilFunc&&, gsl::index = 1)
378 template <
typename XType,
typename YType>
379 template <
typename Func>
381 Func
const&
function,
384 gsl::index subsamples
395 template <
typename XType,
typename YType>
396 template <
typename Func,
typename UntilFunc>
398 Func
const&
function,
399 X_t lower,
X_t step, UntilFunc&& until,
400 gsl::index subsamples,
406 (function, lower, min_upper, step,
std::forward<UntilFunc>(until)),
413 template <
typename XType,
typename YType>
415 (
X_t const x, gsl::index
const iSubsample)
const
417 auto const dx =
static_cast<double>(x - subsampleOffset(iSubsample));
418 return static_cast<gsl::index
>(std::floor(dx / stepSize()));
423 template <
typename XType,
typename YType>
427 auto const dx =
static_cast<double>(x - lower());
428 auto const step =
static_cast<double>(stepSize());
429 auto const substep =
static_cast<double>(substepSize());
430 return static_cast<gsl::index
>(wrapUp(std::fmod(dx, step), step) / substep);
435 template <
typename XType,
typename YType>
436 template <
typename Stream>
438 (
Stream&& out, std::string
const& indent, std::string
const& firstIndent)
const
440 out << firstIndent <<
"Function sampled from " << lower() <<
" to " << upper()
441 <<
" (extent: " << rangeSize() <<
")"
442 <<
" with " <<
size() <<
" samples (" << stepSize() <<
" long)";
443 if (nSubsamples() > 1) {
444 out <<
" and " << nSubsamples() <<
" subsamples (" << substepSize()
448 out <<
"\n" << indent <<
"<subsample #" << iSub <<
">:";
450 auto const&
sub = subsample(iSub);
452 out <<
" [" << i <<
"] " << sample;
459 template <
typename XType,
typename YType>
463 gsl::index subsamples
465 : fLower(range.lower)
466 , fUpper(range.upper)
467 , fNSamples(range.nSamples)
468 , fNSubsamples(subsamples)
470 , fAllSamples(computeTotalSize())
473 assert(subsamples > 0);
479 template <
typename XType,
typename YType>
480 template <
typename UntilFunc>
486 assert(min_upper >= lower);
487 auto const startSamples
488 =
static_cast<gsl::index
>(std::ceil((min_upper - lower) / step));
491 = [lower, step](gsl::index iStep){
return lower + step * iStep; };
493 Range_t r { lower, endStep(startSamples), step, startSamples };
495 while (!until(
r.upper +
r.step,
function(
r.upper +
r.step))) {
498 r.upper = endStep(
r.nSamples);
506 template <
typename XType,
typename YType>
522 std::size_t
const dataSize = computeTotalSize();
523 assert(dataSize > 0U);
524 assert(fLower <= fUpper);
525 assert(fStep >
X_t{0});
530 fAllSamples.resize(dataSize);
535 auto iValue = fAllSamples.begin();
536 for (gsl::index
const iSubsample:
util::counter(nSubsamples())) {
537 X_t const offset = subsampleOffset(iSubsample);
539 X_t const x = offset + iStep * stepSize();
540 Y_t const y =
function(
x);
549 template <
typename XType,
typename YType>
550 template <
typename T>
552 while (value < T{ 0 }) value += range;
560 #endif // ICARUSALG_UTILITIES_SAMPLEDFUNCTION_H
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
gsl::index size() const
Returns the number of samples (in each subsample).
gsl::index fNSamples
Number of samples in the range.
std::vector< Y_t > fAllSamples
All samples, the entire first subsample first.
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
X_t subsampleOffset(gsl::index n) const
Returns the starting point of the subsample n.
X_t upper() const
Returns the upper limit of the covered range.
bool isValidStepIndex(gsl::index const index) const
Returns whether the specified step index is valid.
X_t lower() const
Returns the lower limit of the covered range.
X_t fLower
Lower limit of sampled range.
Y_t const * subsampleData(gsl::index n) const
Start of the block of values for subsample n (unchecked).
process_name opflash particleana ie x
Definition of util::enumerate().
gsl::index nSubsamples() const
Returns the number of subsamples.
static T wrapUp(T value, T range)
Returns value made non-negative by adding multiples of range.
nanoseconds X_t
Type of value accepted by the function.
gsl::index stepIndex(X_t const x, gsl::index const iSubsample) const
Returns the index of the step including x.
X_t substepSize() const
Returns the base offset of the subsamples.
X_t rangeSize() const
Returns the extension of the covered range.
static constexpr auto npos
Invalid index of sample, returned in case of error.
X_t stepSize() const
Returns the extension of a step.
Y_t value(gsl::index iSample, gsl::index n=0U) const
Returns the iSample value of the subsample with the specified index n.
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
gsl::index fNSubsamples
Number of subsamples.
void fillSamples(Function_t const &function)
Samples the function and fills the internal caches.
void dump(Stream &&out, std::string const &indent="") const
Dumps the full content of the sampling into out stream.
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
process_name opflash particleana ie ie y
gsl::index closestSubsampleIndex(X_t x) const
Returns the subsample closest to the value x.
Test of util::counter and support utilities.
SubsampleData_t subsample(gsl::index const n) const
Returns the data of the subsample with the specified index n.
std::size_t computeTotalSize() const
Computes the total size of the data.
static Range_t extendRange(Function_t const &function, X_t lower, X_t min_upper, X_t step, UntilFunc &&until)
ADCcount Y_t
Type of value returned by the function.
X_t fUpper
Upper limit of sampled range.
std::function< Y_t(X_t)> Function_t
Type of sampled function.
void dump(Stream &&out, std::string const &indent, std::string const &firstIndent) const
Dumps the full content of the sampling into out stream.
SampledFunction()=default
Record used during initialization.
Precomputed discrete sampling of a given function.
gsl::span< Y_t const > SubsampleData_t
Span of subsample data. Can be forward iterated.
Y_t * subsampleData(gsl::index n)
std::string sub(const std::string &a, const std::string &b)