LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
LArDiscreteProbabilityVector.h
Go to the documentation of this file.
1 
8 #ifndef LAR_DISCRETE_PROBABILITY_VECTOR_H
9 #define LAR_DISCRETE_PROBABILITY_VECTOR_H 1
10 
11 #include "Pandora/PandoraInternal.h"
12 #include "Pandora/StatusCodes.h"
13 
14 #include <limits>
15 #include <random>
16 #include <utility>
17 #include <vector>
18 
19 namespace lar_content
20 {
21 
26 {
27 public:
28  template <typename TX, typename TY>
29  using InputDatum = std::pair<TX, TY>;
30 
31  template <typename TX, typename TY>
32  using InputData = std::vector<InputDatum<TX, TY>>;
33 
35 
36  typedef pandora::FloatVector ResamplingPoints;
37 
45  template <typename TX, typename TY>
46  DiscreteProbabilityVector(const InputData<TX, TY> &inputData, const TX xUpperBound, const bool useWidths);
47 
54  DiscreteProbabilityVector(const DiscreteProbabilityVector &discreteProbabilityVector, const ResamplingPoints &resamplingPoints);
55 
62  DiscreteProbabilityVector(const DiscreteProbabilityVector &discreteProbabilityVector, std::mt19937 &randomNumberGenerator);
63 
71  float EvaluateCumulativeProbability(const float x) const;
72 
78  unsigned int GetSize() const;
79 
87  float GetX(const unsigned int index) const;
88 
96  float GetProbability(const unsigned int index) const;
97 
105  float GetProbabilityDensity(const unsigned int index) const;
106 
114  float GetCumulativeProbability(const unsigned int index) const;
115 
123  float GetWidth(const unsigned int index) const;
124 
134  void GetAllAtIndex(const unsigned int index, float &x, float &probabilityDensity, float &cumulativeProbability, float &width) const;
135 
136 private:
141  {
142  public:
151  DiscreteProbabilityDatum(const float x, const float densityDatum, const float cumulativeDatum, const float width);
152 
158  float GetX() const;
159 
165  float GetDensityDatum() const;
166 
172  float GetCumulativeDatum() const;
173 
179  float GetWidth() const;
180 
181  private:
182  float m_x;
185  float m_width;
186  };
187 
188  typedef std::vector<DiscreteProbabilityDatum> DiscreteProbabilityData;
189 
197  template <typename TX, typename TY>
198  DiscreteProbabilityData InitialiseDiscreteProbabilityData(InputData<TX, TY> inputData) const;
199 
208  DiscreteProbabilityData ResampleDiscreteProbabilityData(
209  const DiscreteProbabilityVector &discreteProbabilityVector, const ResamplingPoints &resamplingPoints) const;
210 
220  DiscreteProbabilityData RandomiseDiscreteProbabilityData(
221  const DiscreteProbabilityVector &discreteProbabilityVector, std::mt19937 &randomNumberGenerator) const;
222 
231  template <typename TX, typename TY>
232  static bool SortInputDataByX(const InputDatum<TX, TY> &lhs, const InputDatum<TX, TY> &rhs);
233 
241  template <typename TX, typename TY>
242  float CalculateNormalisation(const InputData<TX, TY> &inputData) const;
243 
247  void VerifyCompleteData() const;
248 
254  void VerifyElementRequest(const unsigned int index) const;
255 
257  bool m_useWidths;
258  DiscreteProbabilityData m_discreteProbabilityData;
259 };
260 
261 //------------------------------------------------------------------------------------------------------------------------------------------
262 
263 inline unsigned int DiscreteProbabilityVector::GetSize() const
264 {
265  return m_discreteProbabilityData.size();
266 }
267 
268 //------------------------------------------------------------------------------------------------------------------------------------------
269 
270 inline float DiscreteProbabilityVector::GetX(const unsigned int index) const
271 {
272  this->VerifyElementRequest(index);
273 
274  return m_discreteProbabilityData.at(index).GetX();
275 }
276 
277 //------------------------------------------------------------------------------------------------------------------------------------------
278 
279 inline float DiscreteProbabilityVector::GetProbability(const unsigned int index) const
280 {
281  this->VerifyElementRequest(index);
282 
283  return m_discreteProbabilityData.at(index).GetDensityDatum() * (m_useWidths ? m_discreteProbabilityData.at(index).GetWidth() : 1.f);
284 }
285 
286 //------------------------------------------------------------------------------------------------------------------------------------------
287 
288 inline float DiscreteProbabilityVector::GetProbabilityDensity(const unsigned int index) const
289 {
290  this->VerifyElementRequest(index);
291 
292  return m_discreteProbabilityData.at(index).GetDensityDatum();
293 }
294 
295 //------------------------------------------------------------------------------------------------------------------------------------------
296 
297 inline float DiscreteProbabilityVector::GetCumulativeProbability(const unsigned int index) const
298 {
299  this->VerifyElementRequest(index);
300 
301  return m_discreteProbabilityData.at(index).GetCumulativeDatum();
302 }
303 
304 //------------------------------------------------------------------------------------------------------------------------------------------
305 
306 inline float DiscreteProbabilityVector::GetWidth(const unsigned int index) const
307 {
308  this->VerifyElementRequest(index);
309 
310  return m_discreteProbabilityData.at(index).GetWidth();
311 }
312 
313 //------------------------------------------------------------------------------------------------------------------------------------------
314 
316  const unsigned int index, float &x, float &probabilityDensity, float &cumulativeProbability, float &width) const
317 {
318  this->VerifyElementRequest(index);
319 
320  const DiscreteProbabilityDatum &theDatum(m_discreteProbabilityData.at(index));
321  x = theDatum.GetX();
322  probabilityDensity = theDatum.GetDensityDatum();
323  cumulativeProbability = theDatum.GetCumulativeDatum();
324  width = theDatum.GetWidth();
325 }
326 
327 //------------------------------------------------------------------------------------------------------------------------------------------
328 
330  const float x, const float densityDatum, const float cumulativeDatum, const float width) :
331  m_x(x),
332  m_densityDatum(densityDatum),
333  m_cumulativeDatum(cumulativeDatum),
334  m_width(width)
335 {
336 }
337 
338 //------------------------------------------------------------------------------------------------------------------------------------------
339 
341 {
342  return m_x;
343 }
344 
345 //------------------------------------------------------------------------------------------------------------------------------------------
346 
348 {
349  return m_densityDatum;
350 }
351 
352 //------------------------------------------------------------------------------------------------------------------------------------------
353 
355 {
356  return m_cumulativeDatum;
357 }
358 
359 //------------------------------------------------------------------------------------------------------------------------------------------
360 
362 {
363  return m_width;
364 }
365 
366 //------------------------------------------------------------------------------------------------------------------------------------------
367 
369 {
370  if (2 > m_discreteProbabilityData.size())
371  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_INITIALIZED);
372 
373  if (m_discreteProbabilityData.back().GetX() - m_xUpperBound > std::numeric_limits<float>::epsilon())
374  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
375 
376  return;
377 }
378 
379 //------------------------------------------------------------------------------------------------------------------------------------------
380 
381 inline void DiscreteProbabilityVector::VerifyElementRequest(const unsigned int index) const
382 {
383  if (this->GetSize() < index)
384  throw pandora::StatusCodeException(pandora::STATUS_CODE_OUT_OF_RANGE);
385 
386  return;
387 }
388 
389 } // namespace lar_content
390 
391 #endif // #ifndef LAR_DISCRETE_PROBABILITY_VECTOR_H
Float_t x
Definition: compare.C:6
std::vector< InputDatum< TX, TY >> InputData
float GetProbabilityDensity(const unsigned int index) const
Get the probability density value of the element in the vector.
void VerifyCompleteData() const
Verify the integrity of the complete probability vector.
float GetX(const unsigned int index) const
Get the x value of the element in the vector.
float EvaluateCumulativeProbability(const float x) const
Evaluate the cumulative probability at arbitrary x.
DiscreteProbabilityData ResampleDiscreteProbabilityData(const DiscreteProbabilityVector &discreteProbabilityVector, const ResamplingPoints &resamplingPoints) const
Get a resampled probability data vector by resampling another probability data vector.
std::vector< DiscreteProbabilityDatum > DiscreteProbabilityData
float CalculateNormalisation(const InputData< TX, TY > &inputData) const
Calculate the probability normalisation.
DiscreteProbabilityDatum(const float x, const float densityDatum, const float cumulativeDatum, const float width)
Constructor.
DiscreteProbabilityData InitialiseDiscreteProbabilityData(InputData< TX, TY > inputData) const
Get a initialised probability data vector from the input data.
float GetWidth(const unsigned int index) const
Get the width of the element in the vectorr.
void VerifyElementRequest(const unsigned int index) const
Verify the integrity of the element request.
float GetDensityDatum() const
Get the probability density for the datum.
void GetAllAtIndex(const unsigned int index, float &x, float &probabilityDensity, float &cumulativeProbability, float &width) const
Get all information stored at a particular index.
DiscreteProbabilityData RandomiseDiscreteProbabilityData(const DiscreteProbabilityVector &discreteProbabilityVector, std::mt19937 &randomNumberGenerator) const
Get a randomised probability data vector in which the x values are unchanged, the probability density...
static bool SortInputDataByX(const InputDatum< TX, TY > &lhs, const InputDatum< TX, TY > &rhs)
Sort the input data according to their x value.
float GetCumulativeProbability(const unsigned int index) const
Get the cumulative probability value of the element in the vector.
float GetProbability(const unsigned int index) const
Get the probability value of the element in the vector.
DiscreteProbabilityVector(const InputData< TX, TY > &inputData, const TX xUpperBound, const bool useWidths)
Constructor.
DiscreteProbabilityData m_discreteProbabilityData
the probability data
float m_xUpperBound
the upper bound of the probability vector
bool m_useWidths
controls whether bin widths are used in calculations
unsigned int GetSize() const
Get the size of the probability vector.
float GetCumulativeDatum() const
Get the cumulative probability for the datum.