Belle II Software development
PIDPriorsTable Class Reference

This class holds the prior distribution for a single particle species. More...

#include <PIDPriorsTable.h>

Public Member Functions

void setBinEdges (const std::vector< float > &binEdgesX, const std::vector< float > &binEdgesY)
 Sets the bin edges arrays.
 
void setPriorsTable (const std::vector< float > &priors)
 Sets the priors table from a 2D std::vector.
 
void setErrorsTable (const std::vector< float > &errors)
 Sets the priors error table from a 2D std::vector.
 
void setPriorValue (float x, float y, float value)
 Sets the prior value for a single bin.
 
void setErrorValue (float x, float y, float value)
 Sets the error on the prior value for a single bin.
 
void setAxisLabels (const std::string &labelX, const std::string &labelY)
 Sets axes labels.
 
float getPriorInBin (int iX, int iY) const
 Returns the prior probability for a given bin.
 
float getErrorInBin (int iX, int iY) const
 Returns the error on prior probability for a given bin.
 
float getPriorValue (float x, float y) const
 Returns the prior probability for a given value of (x,y).
 
float getErrorValue (float x, float y) const
 Returns the error on the prior probability for a given value of (x,y).
 
std::string getXAxisLabel () const
 Returns the X axis label.
 
std::string getYAxisLabel () const
 Returns the Y axis label.
 
void printPrior () const
 Prints the content of the table and the axes.
 
void printError () const
 Prints the content of the error table and the axes.
 

Private Member Functions

bool checkRange (const std::string &text, float val, const std::vector< float > &edges) const
 Checks if a value is within the range of an array.
 
short findBin (float val, std::vector< float > array) const
 This function returns the position of a number in a sorted array of bin edges.
 
short findBinFast (float val, std::vector< float > array) const
 This function returns the position of a number in a sorted array of bin edges This implementation should be more clever than the binary search implemented in findBin, but at the very end it seems to be slower.
 
short findBinWithFixedWidth (float val, std::vector< float > array) const
 This function returns the position of a number in a sorted array of bin edges, assuming that the latter are equally spaced.
 

Private Attributes

std::vector< float > m_binEdgesX = {}
 The array containing the bin edges for the X axis.
 
std::vector< float > m_binEdgesY = {}
 The array containing the bin edges for the Y axis.
 
std::vector< float > m_priors
 The matrix with the prior probabilities.
 
std::vector< float > m_errors
 The matrix with the errors on the prior probabilities.
 
std::string m_xAxisLabel = ""
 label of the X axis, indicating which variable is represented here
 
std::string m_yAxisLabel = ""
 label of the Y axis, indicating which variable is represented here
 

Detailed Description

This class holds the prior distribution for a single particle species.

Its basic data members are two 2D matrices of floats (one for the probability, one for it uncertainties) plus two arrays for the bins edges. All these elements, including the matrices, are implemented as 1 dimensional vectors. The PIDPriors class that is stored in the database is basically nothing but a collection of 6 of these objects, one per particle species.

Definition at line 27 of file PIDPriorsTable.h.

Member Function Documentation

◆ checkRange()

bool checkRange ( const std::string &  text,
float  val,
const std::vector< float > &  edges 
) const
private

Checks if a value is within the range of an array.

Parameters
texttext to display in the warning message
valthe value
edgesthe vector of bin edge values the val has to be found in
Returns
the position of the last edge below the input value

Definition at line 152 of file PIDPriorsTable.cc.

153{
154 const float& min = edges.front();
155 const float& max = edges.back();
156 if (val > max || val < min) {
157 B2WARNING("PriorsTable: " << text << LogVar("value", val) << LogVar("min", min) << LogVar("max", max));
158 return false;
159 }
160 return true;
161};
Class to store variables with their name which were sent to the logging service.

◆ findBin()

short findBin ( float  val,
std::vector< float >  array 
) const
private

This function returns the position of a number in a sorted array of bin edges.

Parameters
valthe value
arraythe std::vector the val has to be found
Returns
the position of the last edge below the input value

Definition at line 164 of file PIDPriorsTable.cc.

165{
166 auto it = std::lower_bound(array.cbegin(), array.cend(), val);
167 return std::distance(array.cbegin(), it) - 1;
168};

◆ findBinFast()

short findBinFast ( float  val,
std::vector< float >  array 
) const
private

This function returns the position of a number in a sorted array of bin edges This implementation should be more clever than the binary search implemented in findBin, but at the very end it seems to be slower.

It's kept in here just in case someone passes by and finds a way to speed it up.

Parameters
valthe value
arraythe std::vector the val has to be found
Returns
the position of the last edge below the input value

Definition at line 171 of file PIDPriorsTable.cc.

172{
173 // This function searches for the first bin the axis which is above the
174 // value. First it starts assuming the bins are equal, and then moves around
175 // the array until the correct bin is found
176 float averageBinSize = (array.back() - array.front()) / (array.size() - 1);
177 short bin = 1 + (short)((val - array[0]) / averageBinSize);
178 //adjusts forward
179
180 while (bin < static_cast<short>(array.size()) && array[bin] < val) {
181 bin++;
182 }
183 //adjusts backward
184 while (bin - 1 > 0 && array[bin - 1] > val) {
185 bin--;
186 }
187 return bin - 1;
188};

◆ findBinWithFixedWidth()

short findBinWithFixedWidth ( float  val,
std::vector< float >  array 
) const
private

This function returns the position of a number in a sorted array of bin edges, assuming that the latter are equally spaced.

Parameters
valthe value
arraythe std::vector the val has to be found
Returns
the position of the last edge below the input value

Definition at line 191 of file PIDPriorsTable.cc.

192{
193 float binWidth = (array.back() - array.front()) / (array.size() - 1.);
194 return (short)((val - array.back()) / binWidth);
195};

◆ getErrorInBin()

float getErrorInBin ( int  iX,
int  iY 
) const
inline

Returns the error on prior probability for a given bin.

If the bin is outside of the table boundaries, 0 will be returned.

Parameters
iXbin number on the X axis
iYbin number on the Y axis
Returns
the error on the prior probability value

Definition at line 111 of file PIDPriorsTable.h.

112 {
113 return m_errors[iX + (m_binEdgesX.size() - 1) * iY];
114 };
std::vector< float > m_errors
The matrix with the errors on the prior probabilities.
std::vector< float > m_binEdgesX
The array containing the bin edges for the X axis.

◆ getErrorValue()

float getErrorValue ( float  x,
float  y 
) const

Returns the error on the prior probability for a given value of (x,y).

If the bin is outside of the table boundaries, 0 will be returned.

Parameters
xthe x-value of the x coordinate
ythe y-value of the y coordinate
Returns
value the error on the prior probability value

Definition at line 89 of file PIDPriorsTable.cc.

90{
91 if (!checkRange("The value is out of range for the X axis", x, m_binEdgesX)
92 || !checkRange("The value is out of range for the Y axis", y, m_binEdgesY)) return 0;
93 auto binX = findBin(x, m_binEdgesX);
94 auto binY = findBin(y, m_binEdgesY);
95 return getErrorInBin(binX, binY);
96};
std::vector< float > m_binEdgesY
The array containing the bin edges for the Y axis.
bool checkRange(const std::string &text, float val, const std::vector< float > &edges) const
Checks if a value is within the range of an array.
short findBin(float val, std::vector< float > array) const
This function returns the position of a number in a sorted array of bin edges.
float getErrorInBin(int iX, int iY) const
Returns the error on prior probability for a given bin.

◆ getPriorInBin()

float getPriorInBin ( int  iX,
int  iY 
) const
inline

Returns the prior probability for a given bin.

If the bin is outside of the table boundaries, 0 will be returned.

Parameters
iXbin number on the X axis
iYbin number on the Y axis
Returns
value the prior probability value

Definition at line 99 of file PIDPriorsTable.h.

100 {
101 return m_priors[iX + (m_binEdgesX.size() - 1) * iY];
102 };
std::vector< float > m_priors
The matrix with the prior probabilities.

◆ getPriorValue()

float getPriorValue ( float  x,
float  y 
) const

Returns the prior probability for a given value of (x,y).

If the bin is outside of the table boundaries, 0 will be returned.

Parameters
xthe x-value of the x coordinate
ythe y-value of the y coordinate
Returns
the value the prior probability value

Definition at line 79 of file PIDPriorsTable.cc.

80{
81 if (!checkRange("The value is out of range for the X axis", x, m_binEdgesX)
82 || !checkRange("The value is out of range for the Y axis", y, m_binEdgesY)) return 0;
83 auto binX = findBin(x, m_binEdgesX);
84 auto binY = findBin(y, m_binEdgesY);
85 return getPriorInBin(binX, binY);
86};
float getPriorInBin(int iX, int iY) const
Returns the prior probability for a given bin.

◆ getXAxisLabel()

std::string getXAxisLabel ( ) const
inline

Returns the X axis label.

Returns
the X axis label

Definition at line 139 of file PIDPriorsTable.h.

140 {
141 return m_xAxisLabel;
142 };
std::string m_xAxisLabel
label of the X axis, indicating which variable is represented here

◆ getYAxisLabel()

std::string getYAxisLabel ( ) const
inline

Returns the Y axis label.

Returns
the Y axis label

Definition at line 148 of file PIDPriorsTable.h.

149 {
150 return m_yAxisLabel;
151 };
std::string m_yAxisLabel
label of the Y axis, indicating which variable is represented here

◆ printError()

void printError ( ) const

Prints the content of the error table and the axes.

Definition at line 125 of file PIDPriorsTable.cc.

126{
127 std::cout << " --- Error summary --- " << std::endl;
128 std::cout << " Size : " << m_errors.size() << " (" << m_binEdgesX.size() - 1 << " x " << m_binEdgesY.size() - 1 << ")" <<
129 std::endl;
130 std::cout << " X axis: " ;
131 for (auto edge : m_binEdgesX) {
132 std::cout << " " << edge << " " ;
133 }
134 std::cout << " " << std::endl;
135 std::cout << " Y axis ";
136 for (auto edge : m_binEdgesY) {
137 std::cout << " " << edge << " " ;
138 }
139 std::cout << " " << std::endl;
140 std::cout << " Values " << std::endl;
141 for (int iY = m_binEdgesY.size() - 2; iY >= 0; iY--) {
142 for (int iX = 0; iX < static_cast<int>(m_binEdgesX.size() - 1); iX++) {
143 std::cout << " " << getErrorInBin(iX, iY) << " ";
144 }
145 std::cout << " " << std::endl;
146 }
147 std::cout << " --- End of error summary --- " << std::endl;
148};

◆ printPrior()

void printPrior ( ) const

Prints the content of the table and the axes.

Definition at line 99 of file PIDPriorsTable.cc.

100{
101 std::cout << " --- Prior summary --- " << std::endl;
102 std::cout << " Size : " << m_priors.size() << " (" << m_binEdgesX.size() - 1 << " x " << m_binEdgesY.size() - 1 << ")" <<
103 std::endl;
104 std::cout << " X axis: " ;
105 for (auto edge : m_binEdgesX) {
106 std::cout << " " << edge << " " ;
107 }
108 std::cout << " " << std::endl;
109 std::cout << " Y axis ";
110 for (auto edge : m_binEdgesY) {
111 std::cout << " " << edge << " " ;
112 }
113 std::cout << " " << std::endl;
114 std::cout << " Values " << std::endl;
115 for (int iY = m_binEdgesY.size() - 2; iY >= 0; iY--) {
116 for (int iX = 0; iX < static_cast<int>(m_binEdgesX.size() - 1); iX++) {
117 std::cout << " " << getPriorInBin(iX, iY) << " ";
118 }
119 std::cout << " " << std::endl;
120 }
121 std::cout << " --- End of prior summary --- " << std::endl;
122};

◆ setAxisLabels()

void setAxisLabels ( const std::string &  labelX,
const std::string &  labelY 
)
inline

Sets axes labels.

Parameters
labelXthe label of the X axis
labelYthe label of the X axis

Definition at line 85 of file PIDPriorsTable.h.

86 {
87 m_xAxisLabel = labelX;
88 m_yAxisLabel = labelY;
89 return ;
90 }

◆ setBinEdges()

void setBinEdges ( const std::vector< float > &  binEdgesX,
const std::vector< float > &  binEdgesY 
)

Sets the bin edges arrays.

Please notice that setting the axes will also automatically clear the tables.

Parameters
binEdgesXthe vector of bin edges for the X axis
binEdgesYthe vector of bin edges for the Y axis

Definition at line 17 of file PIDPriorsTable.cc.

18{
19 m_binEdgesX = binEdgesX;
20 m_binEdgesY = binEdgesY;
21
22 // if less than two edges are provided, assumes the maximum range for floats
23 auto checkEdges = [](std::vector<float>& edges, const std::string & axis) {
24 if (edges.size() < 2) {
25 B2WARNING("You provided less than 2 bin edges for the " << axis <<
26 " axis. This is not enough to create one bin, so one will be created with range [-FLT_MAX, +FLT_MAX]");
27 edges = { -FLT_MAX, FLT_MAX};
28 }
29 auto prevVal = edges[0];
30 for (int iBin = 1; iBin < static_cast<int>(edges.size()); iBin++) {
31 auto edge = edges[iBin];
32 if (prevVal >= edge)
33 B2FATAL("Null or negative bin size found on the X axis. Please make sure that all the bin edges are sorted and non-equal");
34 prevVal = edge;
35 }
36 return;
37 };
38
39 checkEdges(m_binEdgesX, "X");
40 checkEdges(m_binEdgesY, "Y");
41
42 m_priors.clear();
43 m_errors.clear();
44
45 m_priors.resize((m_binEdgesY.size() - 1) * (m_binEdgesX.size() - 1), 0.);
46 m_errors.resize((m_binEdgesY.size() - 1) * (m_binEdgesX.size() - 1), 0.);
47 return;
48};

◆ setErrorsTable()

void setErrorsTable ( const std::vector< float > &  errors)
inline

Sets the priors error table from a 2D std::vector.

Parameters
errors2D std:vector of floats containing the error on the prior probability for each bin

Definition at line 55 of file PIDPriorsTable.h.

56 {
57 m_errors = errors;
58 return;
59 };

◆ setErrorValue()

void setErrorValue ( float  x,
float  y,
float  value 
)

Sets the error on the prior value for a single bin.

Parameters
xthe x-value of the x coordinate
ythe y-value of the y coordinate
valuethe x-value of the prior probability error associated to x and y

Definition at line 67 of file PIDPriorsTable.cc.

68{
69 if (!checkRange("The value is out of range for the X axis", x, m_binEdgesX)
70 || !checkRange("The value is out of range for the Y axis", y, m_binEdgesY)) return ;
71
72 auto binX = findBin(x, m_binEdgesX);
73 auto binY = findBin(y, m_binEdgesY);
74 m_errors[binX + (m_binEdgesX.size() - 1)*binY] = value;
75 return ;
76};

◆ setPriorsTable()

void setPriorsTable ( const std::vector< float > &  priors)
inline

Sets the priors table from a 2D std::vector.

Parameters
priors2D std:vector of floats containing the prior probability for each bin

Definition at line 44 of file PIDPriorsTable.h.

45 {
46 m_priors = priors;
47 return;
48 };

◆ setPriorValue()

void setPriorValue ( float  x,
float  y,
float  value 
)

Sets the prior value for a single bin.

Parameters
xthe x-value of the x coordinate
ythe y-value of the y coordinate
valuethe x-value of the prior probability associated to x and y

Definition at line 52 of file PIDPriorsTable.cc.

53{
54 if (!checkRange("The value is out of range for the X axis", x, m_binEdgesX)
55 || !checkRange("The value is out of range for the Y axis", y, m_binEdgesY)) return;
56 if (value > 1. || value < 0.) {
57 B2WARNING("The value " << value << " you are trying to set for the bin (" << x << ", " << y <<
58 ") does not look like a probability. The table will be filled, but i will not be usable as a proper prior probability table.");
59 }
60 auto binX = findBin(x, m_binEdgesX);
61 auto binY = findBin(y, m_binEdgesY);
62 m_priors[binX + (m_binEdgesX.size() - 1)*binY] = value;
63 return;
64};

Member Data Documentation

◆ m_binEdgesX

std::vector<float> m_binEdgesX = {}
private

The array containing the bin edges for the X axis.

Definition at line 167 of file PIDPriorsTable.h.

◆ m_binEdgesY

std::vector<float> m_binEdgesY = {}
private

The array containing the bin edges for the Y axis.

Definition at line 168 of file PIDPriorsTable.h.

◆ m_errors

std::vector<float> m_errors
private

The matrix with the errors on the prior probabilities.

Definition at line 170 of file PIDPriorsTable.h.

◆ m_priors

std::vector<float> m_priors
private

The matrix with the prior probabilities.

Definition at line 169 of file PIDPriorsTable.h.

◆ m_xAxisLabel

std::string m_xAxisLabel = ""
private

label of the X axis, indicating which variable is represented here

Definition at line 171 of file PIDPriorsTable.h.

◆ m_yAxisLabel

std::string m_yAxisLabel = ""
private

label of the Y axis, indicating which variable is represented here

Definition at line 172 of file PIDPriorsTable.h.


The documentation for this class was generated from the following files: