Belle II Software  release-05-02-19
RawDataCollectedMinMax.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler (jakob.lettenbichler@oeaw.ac.at) *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <tracking/trackFindingVXD/sectorMapTools/MinMaxCollector.h>
14 #include <framework/logging/Logger.h>
15 
16 #include <vector>
17 #include <utility> // std::pair
18 #include <limits> // std::numeric_limits
19 #include <algorithm> // std::sort
20 
21 
22 namespace Belle2 {
28  class RawDataCollectedMinMax {
30  protected:
31  unsigned m_currentSize;
33  std::pair<double, double> m_minMaxQuantiles;
34  std::vector<std::pair<double, double>> m_intermediateValues;
35  MinMaxCollector<double> m_collector;
36  public:
37 
39  RawDataCollectedMinMax(unsigned expectedSize,
40  std::pair<double, double> quantiles,
41  unsigned maxSizeThreshold = 100000) :
43  m_fillIntermediateThreshold(std::numeric_limits<unsigned>::max()),
44  m_minMaxQuantiles(quantiles),
45  m_collector((quantiles.first > (1. - quantiles.second) ? quantiles.first * 2. : (1. - quantiles.second) * 2.))
46  {
47  if (double(expectedSize) / (double(maxSizeThreshold) * 0.05) > double(maxSizeThreshold))
48  { B2FATAL("RawDataCollectedMinMax: expected data to big, can not execute!"); }
49 
50  if (maxSizeThreshold < expectedSize) {
51  m_fillIntermediateThreshold = maxSizeThreshold / 10;
52  }
53  }
54 
56  void add(double newVal)
57  {
58  m_collector.append(newVal);
59  m_currentSize++;
60 
61  // if threshold reached, collect results and fill into intermediate value-container:
63  std::pair<double, double> results = m_collector.getMinMax(m_minMaxQuantiles.first, m_minMaxQuantiles.second);
64  m_intermediateValues.push_back(std::move(results));
66  }
67  }
68 
70  unsigned getSampleSize() const { return m_currentSize; }
71 
73  std::pair<double, double> getMinMax()
74  {
75  if (m_intermediateValues.empty()) {
77  }
78 
79  // issue: m_collector-sample could be too small and therefore distort results for small intermediateValue-samples. Therefore neglect m_collector for that case.
80  if (m_intermediateValues.size() == 1) {
81  return { m_intermediateValues.at(0).first, m_intermediateValues.at(0).second};
82  }
83  if (m_intermediateValues.size() == 2) {
84  return {
85  0.5 * (m_intermediateValues.at(0).first + m_intermediateValues.at(1).first),
86  0.5 * (m_intermediateValues.at(0).second + m_intermediateValues.at(1).second) };
87  }
88 
89  if (!m_collector.empty()) {
90  std::pair<double, double> results = m_collector.getMinMax(m_minMaxQuantiles.first, m_minMaxQuantiles.second);
91  m_intermediateValues.push_back(results);
92  }
93 
94  unsigned index = std::floor(double(m_intermediateValues.size()) * 0.5);
95  double min, max;
96 
97  std::sort(m_intermediateValues.begin(), m_intermediateValues.end(),
98  [](const std::pair<double, double>& a, const std::pair<double, double>& b) -> bool { return a.first < b.first; });
99  min = m_intermediateValues.at(index).first;
100 
101  std::sort(m_intermediateValues.begin(), m_intermediateValues.end(),
102  [](const std::pair<double, double>& a, const std::pair<double, double>& b) -> bool { return a.second < b.second; });
103  max = m_intermediateValues.at(index).second;
104 
105  return {min, max};
106  }
107  };
109 }
110 
Belle2::MinMaxCollector::getMinMax
std::pair< DataType, DataType > getMinMax(DataType minQuantile=0., DataType maxQuantile=1.) const
for given pair of quantiles, the according cuts (min, max) will be returned.
Definition: MinMaxCollector.h:192
Belle2::RawDataCollectedMinMax::RawDataCollectedMinMax
RawDataCollectedMinMax(unsigned expectedSize, std::pair< double, double > quantiles, unsigned maxSizeThreshold=100000)
constructor. please use for quantiles [min, max] min ~0 & max ~1 (range 0-1)
Definition: RawDataCollectedMinMax.h:47
Belle2::RawDataCollectedMinMax::m_currentSize
unsigned m_currentSize
the current size of the data sample.
Definition: RawDataCollectedMinMax.h:39
Belle2::RawDataCollectedMinMax::m_fillIntermediateThreshold
unsigned m_fillIntermediateThreshold
an internal threshold taking care of collecting intermediate results during sample collection
Definition: RawDataCollectedMinMax.h:40
Belle2::RawDataCollectedMinMax::getMinMax
std::pair< double, double > getMinMax()
returns current best estimates for min and max cuts.
Definition: RawDataCollectedMinMax.h:81
Belle2::RawDataCollectedMinMax::m_collector
MinMaxCollector< double > m_collector
collects raw data in an RAM-saving way.
Definition: RawDataCollectedMinMax.h:43
Belle2::MinMaxCollector::append
void append(DataType newVal)
append new value
Definition: MinMaxCollector.h:227
Belle2::RawDataCollectedMinMax::m_intermediateValues
std::vector< std::pair< double, double > > m_intermediateValues
collects intermediate threshold if expected size is too big.
Definition: RawDataCollectedMinMax.h:42
Belle2::RawDataCollectedMinMax::add
void add(double newVal)
adds value to collector.
Definition: RawDataCollectedMinMax.h:64
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MinMaxCollector::empty
bool empty() const
returns if internal containers are empty
Definition: MinMaxCollector.h:276
Belle2::RawDataCollectedMinMax::m_minMaxQuantiles
std::pair< double, double > m_minMaxQuantiles
the quantiles to be collected in the end (defined in [0;1])
Definition: RawDataCollectedMinMax.h:41
Belle2::MinMaxCollector::totalSize
unsigned totalSize() const
returns the combined size of the containers storing the values
Definition: MinMaxCollector.h:264
Belle2::RawDataCollectedMinMax::getSampleSize
unsigned getSampleSize() const
returns current sample size (which is not the actual size of the container).
Definition: RawDataCollectedMinMax.h:78
Belle2::MinMaxCollector::clear
void clear()
deletes all values collected so far and resets to constructor-settings.
Definition: MinMaxCollector.h:281