 |
Belle II Software
release-05-01-25
|
23 #include <framework/core/FrameworkExceptions.h>
24 #include <framework/logging/Logger.h>
43 template<
class DataType>
44 class MinMaxCollector {
66 bool addMin(DataType newVal,
bool allow2Grow) {
return addEntry(newVal,
true, allow2Grow); }
71 bool addMax(DataType newVal,
bool allow2Grow) {
return addEntry(newVal,
false, allow2Grow); }
76 bool addEntry(DataType newVal,
bool isMinContainer,
bool allow2Grow)
99 void sortIn(DataType newVal,
bool isMinContainer)
105 if (isMinContainer) {
118 unsigned getIndex(DataType aQuantile)
const
120 {
return unsigned(
double(
m_sampleSize) *
double(aQuantile) + 0.5); }
130 if (newCalcThreshold > container.size() ) {
return true; }
154 BELLE2_DEFINE_EXCEPTION(Quantile_out_of_bounds,
"The quantileCut (%1%) you gave is illegal (only allowed between 0-1)");
159 "The quantiles you asked for (%1% and %2%) are not within the collected range of data (0-%3% and %4%-1) to prevent this happening, you have to pass a bigger value for QuantileCut when constructing a MinMaxCollector-Instance!");
168 MinMaxCollector(DataType quantileCut = 0.025,
unsigned warmUpThreshold = 10) :
173 if (0 > quantileCut or 0.5 < quantileCut) {
throw (Quantile_out_of_bounds() << quantileCut); }
179 friend std::ostream&
operator<< (std::ostream& out,
const MinMaxCollector& mmCol) { out << mmCol.getName();
return out; }
184 std::pair<DataType, DataType>
getMinMax(DataType minQuantile = 0., DataType maxQuantile = 1.)
const
187 if (0 > minQuantile or 1 < minQuantile) {
throw (Quantile_out_of_bounds() << minQuantile); }
188 if (0 > maxQuantile or 1 < maxQuantile) {
throw (Quantile_out_of_bounds() << maxQuantile); }
193 unsigned minIndex =
getIndex(minQuantile);
195 unsigned maxIndex =
getIndex(1. - maxQuantile);
199 if (minIndex > (
m_smallestValues.size() - 1)) { B2ERROR(
"minIndex " << minIndex <<
" calculated for minQuantile " << minQuantile <<
" bigger than nSmallestValues " <<
m_smallestValues.size() <<
"!"); }
200 if (finalMaxIndex > (
m_biggestValues.size() - 1)) { B2ERROR(
"maxIndex " << maxIndex <<
" calculated for maxQuantile " << maxQuantile <<
" bigger than nBiggestValues " <<
m_biggestValues.size() <<
"!"); }
207 void insert(DataType newVal)
208 {
append(std::move(newVal));}
214 {
append(std::move(newVal));}
219 void append(DataType newVal)
238 void merge(
const MinMaxCollector<DataType>& other)
241 B2WARNING(
"MinMaxCollector::merge: other collector has differing size in quantileCut. If this is not the purpose, this could indicate unintended behavior!");
262 unsigned size()
const
286 void print(
bool printFull =
false)
const
287 { B2INFO(
getName(printFull)); }
292 std::string
getName(
bool printFull =
false)
const
297 string out =
"MinMaxCollector with sampleSize " + to_string(
m_sampleSize) +
299 " has nSmallestValues/nBiggestValues: " + to_string(nSmallest) +
300 "/" + to_string(nBiggest) +
"\n";
302 if (!printFull) out +=
"The 5 values each describe for the valueContainer pos[0], pos[1], pos[mean], pos[max-1], pos[max]\n";
303 out +=
"SmallestValues: ";
305 if (printFull or
size() < 6) {
307 out +=
"\n" + string(
"BiggestValues: ");
308 for (DataType entry :
m_biggestValues) { out += to_string(entry) +
", "; }
311 DataType smallestTotal = 0, biggestTotal = 0, smallestMean, biggestMean;
313 smallestMean = smallestTotal / DataType(nSmallest);
316 +
", mean: " + to_string(smallestMean)
320 biggestMean = biggestTotal / DataType(nBiggest);
321 out +=
"\n" + string(
"BiggestValues: ");
324 +
", mean: " + to_string(biggestMean)
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.
bool addMax(DataType newVal, bool allow2Grow)
add entry to maxContainer if it fits in
unsigned size() const
returns the size (in a sense of roughly collected data)
void push_back(DataType newVal)
for convenience reasons, pipe to append.
unsigned m_warmUpThreshold
sets a threshold for warm-up.
bool addEntry(DataType newVal, bool isMinContainer, bool allow2Grow)
add entry to container if it fits in
void append(DataType newVal)
append new value
void insert(DataType newVal)
for convenience reasons, pipe to append.
DataType m_quantileCut
sets the threshold for storing data.
A container for collecting data, where min- and max-quantiles near q(0) and q(1) are to be found.
void merge(const MinMaxCollector< DataType > &other)
fill the stuff of the other one with this one
BELLE2_DEFINE_EXCEPTION(Quantile_out_of_bounds, "The quantileCut (%1%) you gave is illegal (only allowed between 0-1)")
returns true, if the valueContainers were increased in size.
MinMaxCollector(DataType quantileCut=0.025, unsigned warmUpThreshold=10)
constructor.
Abstract base class for different kinds of events.
bool empty() const
returns if internal containers are empty
bool checkVectorSize(std::deque< DataType > &container)
returns true, if vector is allowed to grow
std::deque< DataType > m_biggestValues
collects biggest values occured so far
unsigned sampleSize() const
returns actual sampleSize
bool addMin(DataType newVal, bool allow2Grow)
add entry to minContainer if it fits in
std::deque< DataType > m_smallestValues
collects smallest values occured so far
void print(bool printFull=false) const
print an overview of the entries collected.
void sortIn(DataType newVal, bool isMinContainer)
add newVal to appropriate container
unsigned totalSize() const
returns the combined size of the containers storing the values
std::string getName(bool printFull=false) const
return a string of an overview of the entries collected.
void clear()
deletes all values collected so far and resets to constructor-settings.
friend std::ostream & operator<<(std::ostream &out, const MinMaxCollector &mmCol)
overloaded '<<' stream operator.
unsigned m_sampleSize
counts numbre of values added so far
unsigned getIndex(DataType aQuantile) const
the correct access-index for given quantile will be determined