8 #ifndef ICARUSALG_UTILITIES_FIXEDBINS_H
9 #define ICARUSALG_UTILITIES_FIXEDBINS_H
23 namespace icarus::ns::util {
25 template <
typename T,
typename C =
unsigned int>
class FixedBins;
27 template <
typename T,
typename C>
29 template <typename T, typename C>
31 template <typename T, typename C>
33 template <typename T, typename C>
35 template <typename T, typename C>
37 template <typename T, typename C>
79 template <typename T, typename C >
89 using Interval_t = decltype(std::declval<T>() - std::declval<T>());
118 BinIndex_t add(Data_t
value);
126 void clear() noexcept;
143 Interval_t binWidth()
const noexcept;
159 Data_t lowerEdge(BinIndex_t index)
const noexcept;
166 Data_t upperEdge(BinIndex_t index)
const noexcept;
174 BinIndex_t binWith(Data_t
value)
const noexcept;
181 Interval_t range()
const noexcept;
189 BinIndex_t minBin()
const noexcept;
199 BinIndex_t maxBin()
const noexcept;
207 Data_t min()
const noexcept;
215 Data_t max()
const noexcept;
225 Count_t
count(BinIndex_t index)
const noexcept;
236 Count_t countFor(Data_t
value)
const noexcept;
246 Count_t operator[](BinIndex_t index)
const noexcept;
272 static constexpr Count_t CountZero = 0;
287 bool hasStorageIndex(
std::ptrdiff_t stIndex)
const noexcept;
294 std::
size_t initializeWith(
Data_t value);
303 namespace icarus::ns::util {
327 template <
typename T,
typename C >
330 : fWidth{ width }, fOffset{ offset }, fMinBin{}
332 assert(fWidth != Interval_t{0});
337 template <
typename T,
typename C >
340 std::size_t
const stIndex
342 ++fCounters[stIndex];
348 template <
typename T,
typename C >
353 template <
typename T,
typename C >
355 {
return fCounters.size(); }
359 template <
typename T,
typename C >
361 {
return fCounters.empty(); }
365 template <
typename T,
typename C >
371 template <
typename T,
typename C >
377 template <
typename T,
typename C >
380 {
return offset() + (minBin() + index) * binWidth(); }
384 template <
typename T,
typename C >
387 {
return lowerEdge(index + 1); }
391 template <
typename T,
typename C >
394 {
return empty()? 0: minBin() + relativeBinIndex(
value, min()); }
398 template <
typename T,
typename C >
400 {
return binWidth() * nBins(); }
404 template <
typename T,
typename C >
409 template <
typename T,
typename C >
411 {
return minBin() + nBins() - 1; }
415 template <
typename T,
typename C >
421 template <
typename T,
typename C >
423 {
return min() + range(); }
427 template <
typename T,
typename C >
431 auto const stIndex = storageIndex(index);
432 return hasStorageIndex(stIndex)? fCounters[stIndex]: CountZero;
437 template <
typename T,
typename C >
444 template <
typename T,
typename C >
447 {
return count(index); }
451 template <
typename T,
typename C >
457 template <
typename T,
typename C >
459 {
return fCounters.begin(); }
463 template <
typename T,
typename C >
469 template <
typename T,
typename C >
471 {
return fCounters.end(); }
475 template <
typename T,
typename C >
481 template <
typename T,
typename C >
484 {
return index - fMinBin; }
488 template <
typename T,
typename C >
490 (std::ptrdiff_t stIndex)
const noexcept
493 (stIndex >= 0) && (
static_cast<std::size_t
>(stIndex) < fCounters.size());
498 template <
typename T,
typename C >
503 return static_cast<BinIndex_t>(floor((value - ref) / binWidth()));
508 template <
typename T,
typename C >
513 fMin =
offset() + binWidth() * relativeBinIndex(value,
offset());
514 fCounters.push_back(CountZero);
515 return static_cast<std::size_t
>(0);
521 template <
typename T,
typename C >
530 std::size_t
const nExtend =
static_cast<std::size_t
>(-stIndex);
531 Storage_t data(nExtend + fCounters.size());
532 auto const itOldFirst = std::next(data.begin(), nExtend);
533 std::fill(data.begin(), itOldFirst, CountZero);
534 std::copy(fCounters.cbegin(), fCounters.cend(), itOldFirst);
535 fCounters = std::move(data);
538 fMin -= nExtend * binWidth();
541 else if (static_cast<std::size_t>(stIndex) >= fCounters.size()) {
543 fCounters.resize(static_cast<std::size_t>(stIndex) + 1, CountZero);
545 assert(hasStorageIndex(stIndex));
546 return static_cast<std::size_t
>(stIndex);
554 template <
typename T,
typename C>
556 {
return bins.empty(); }
560 template <
typename T,
typename C>
562 {
return bins.size(); }
566 template <
typename T,
typename C>
568 {
return bins.cbegin(); }
572 template <
typename T,
typename C>
574 {
return bins.begin(); }
578 template <
typename T,
typename C>
580 {
return bins.cend(); }
584 template <
typename T,
typename C>
586 {
return bins.end(); }
591 #endif // ICARUSALG_UTILITIES_FIXEDBINS_H
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
auto cend() const noexcept
Returns an iterator pointing to the content of the first bin with storage.
BinIndex_t relativeBinIndex(Data_t value, Data_t ref) const noexcept
Returns the number of bins passing from ref to value.
std::size_t size() const noexcept
Returns the number of bins with storage.
FixedBins(Interval_t width, Data_t offset=Data_t{}) noexcept
Constructor: initializes the binning.
BinIndex_t add(Data_t value)
Increases by a unit the count at the bin including value.
std::size_t size(FixedBins< T, C > const &) noexcept
auto begin() const noexcept
Returns an iterator pointing to the content of the first bin with storage.
auto cbegin(FixedBins< T, C > const &) noexcept
Data_t max() const noexcept
double Data_t
Type on the bin axis.
decltype(std::declval< double >()-std::declval< double >()) Interval_t
Type of interval in on the bin axis.
Count_t count(BinIndex_t index) const noexcept
Returns the count of the bin with the specified index.
Data_t min() const noexcept
auto cend(FixedBins< T, C > const &) noexcept
BinIndex_t binWith(Data_t value) const noexcept
Returns the index of the bin including the specified value.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
bool empty() const noexcept
Returns whether there is no storage at all.
Data_t upperEdge(BinIndex_t index) const noexcept
Count_t countFor(Data_t value) const noexcept
Returns the count of the bin including the specified value.
BinIndex_t maxBin() const noexcept
auto end(FixedBins< T, C > const &) noexcept
std::vector< Count_t > Storage_t
Type of storage for bin counts.
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
BinIndex_t minBin() const noexcept
void clear() noexcept
Resets all counts to 0.
Data_t offset() const noexcept
Returns the alignment offset of the bins.
std::ptrdiff_t BinIndex_t
Type of bin index.
std::size_t nBins() const noexcept
Returns how many bins currently have storage.
bool hasStorageIndex(std::ptrdiff_t stIndex) const noexcept
Returns whether the specified stotage index is available.
Interval_t range() const noexcept
auto begin(FixedBins< T, C > const &) noexcept
std::size_t allocateBin(BinIndex_t index)
Data_t lowerEdge(BinIndex_t index) const noexcept
C Count_t
Type on the bin content.
std::size_t initializeWith(Data_t value)
std::size_t count(Cont const &cont)
std::ptrdiff_t storageIndex(BinIndex_t index) const noexcept
Returns the index in the data storage corresponding to bin index.
FixedBins(T) -> FixedBins< T >
bool empty(FixedBins< T, C > const &) noexcept
auto cbegin() const noexcept
Returns an iterator pointing to the content of the first bin with storage.
auto end() const noexcept
Returns an iterator pointing to the content of the first bin with storage.
Interval_t binWidth() const noexcept
Returns the width of the bins.