Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Histogram.hh
Go to the documentation of this file.
1 ///
2 /// @file Histogram.hh
3 /// @brief Hammer histogram class
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_MATH_HISTOGRAM
13 #define HAMMER_MATH_HISTOGRAM
14 
15 #include <utility>
16 #include <vector>
17 #include <string>
18 #include <memory>
19 
21 #include "Hammer/IndexTypes.hh"
25 #include "Hammer/Math/Tensor.hh"
28 #include "Hammer/Tools/IOTypes.hh"
29 
30 namespace Hammer {
31 
32  class Log;
33 
34  /// @brief Multidimensional histogram class with Tensor as cell bins
35  ///
36  /// Contains the tensor version of an histogram defined by the user
37  ///
38  /// @ingroup Math
39  class Histogram {
40 
41  protected:
42  /// @brief Bin class with Tensor contents
43  ///
44  /// Contains an histogram bin, with value, error, etc.
45  ///
46  /// @ingroup Math
47  class Bin {
48 
49  public:
50  /// @brief constructor based on a tensor (for determining shape and labels)
51  /// @param[in] t the model tensor
52  /// @param[in] withErrors whether errors should also be initialized (squared tensor weights)
53  Bin(const Tensor& t, bool withErrors = false);
54 
55  /// @brief serialization constructor
56  /// @param[in] w the reader for the sum of tensor weights
57  /// @param[in] w2 the reader for the sum of the squared tensor weights (if available)
58  /// @param[in] num the number of entries
59  Bin(const Serial::FBTensor* w, const Serial::FBTensor* w2, size_t num);
60 
61  Bin(const Bin& other) = default;
62  Bin& operator=(const Bin& other) = default;
63  Bin(Bin&& other) = default;
64  Bin& operator=(Bin&& other) = default;
65  ~Bin() = default;
66 
67  /// @brief clears the contents of the bin (including the shapes of the tensors)
68  void clear();
69 
70  /// @brief add the values of a tensor to the current bin. Throws if the shapes do not match
71  /// @param[in] t the tensor
72  void addWeight(const Tensor& t);
73 
74  /// @brief the sum of tensor weights contained in the bin
75  /// @return the weights
76  const Tensor& getWeight() const;
77 
78  /// @brief the sum of (outer) squared tensor weights contained in the bin,
79  /// or an empty tensor if the errors are not tracked
80  /// @return the squared weights
81  const Tensor& getSquaredWeight() const;
82 
83  /// @brief the number of entries in the bin (number of times `addWeight` has been called)
84  /// @return the number of events
85  size_t getNumEvents() const;
86 
87  /// @brief
88  /// @param[in] other
89  /// @return
90  Bin& operator+=(const Bin& other);
91 
92  private:
93  Tensor _weight; ///< tensor sum of weights
94  Tensor _weight2; ///< tensor sum of squared weights (in the outer square sense)
95  size_t _NEvents; ///< number of entries
97  };
98 
99  public:
100  /// @name Constructors
101  //@{
102 
103  /// @brief constructor
104  /// @param[in] withErrors whether to track squared weights
105  Histogram();
106 
107 
108  Histogram(const Histogram& other) = default;
109  Histogram& operator=(const Histogram& other) = default;
110  Histogram(Histogram&& other) = default;
111  Histogram& operator=(Histogram&& other) = default;
112 
113  /// destructor
114  virtual ~Histogram();
115 
116  //@}
117 
118  public:
119 
120  using DataType = std::vector<Bin>;
121  using ElementType = Bin;
123  using const_reference = const ElementType&;
124 
125  using iterator = DataType::iterator;
126  using const_iterator = DataType::const_iterator;
127 
128 
129  iterator begin();
130  const_iterator begin() const;
131 
132  iterator end();
133  const_iterator end() const;
134 
137 
138  public:
139  /// @brief dimensionality of the histograms
140  /// @return the number of components
141  size_t rank() const;
142 
143  /// @brief dimension of a specific coordinate
144  /// @param[in] index the coordinate index
145  /// @return the dimension
146  IndexType dim(IndexType index) const;
147 
148  /// @brief get the dimensions of all the coordinates at once
149  /// @return the list of dimensions
150  const IndexList& dims() const;
151 
152  /// @brief get the histogram name
153  /// @return the name
154  const std::string& name() const;
155 
156  public:
157  /// @brief set the value of a histogram bin based on the bin indices
158  /// @param[in] value value of the bin
159  /// @param[in] rest variadic argument with remaining indices
160  template <typename... Args>
161  void fillBin(const Tensor& value, Args... rest);
162 
163  /// @brief set the value of a histogram bin based on the bin indices
164  /// @param[in] value value of the bin
165  /// @param[in] position list of coordinates
166  void fillBin(const Tensor& value, const IndexList& position);
167 
168  /// @brief reset the histogram
169  void clear();
170 
171  /// @brief reset the histogram contents but not the shape of the histogram
172  void clearData();
173 
174  /// @brief get the value of a histogram bin based on an index of the bin
175  /// @param[in] rest variadic argument with remaining indices
176  /// @return value of the bin
177  template <typename... Args>
178  const Tensor& getWeight(Args... rest) const;
179 
180  /// @brief get the value of a histogram bin based on an index of the bin
181  /// @param[in] rest variadic argument with remaining indices
182  /// @return value of the bin
183  template <typename... Args>
184  const Tensor& getSquaredWeight(Args... rest) const;
185 
186  /// @brief get the value of a histogram bin based on an index of the bin
187  /// @param[in] rest variadic argument with remaining indices
188  /// @return value of the bin
189  template <typename... Args>
190  size_t getNumEvents(Args... rest) const;
191 
192  /// @brief
193  /// @param[in] other
194  /// @return
195  Histogram& operator+=(const Histogram& other);
196 
197  /// @brief convert the indices into the position in the flattened
198  /// contents into vector organized as row-major
199  /// @param[in] indices index of coordinate in multidimensional histogram
200  /// @return the absolute bin position
201  // PositionType indicesToPos(const IndexList& indices) const;
202 
203  protected:
204  /// @brief access an element given its coordinates
205  /// @param[in] indices the list of coordinates, 0-based
206  /// @return a reference to the bin
207  Bin& element(const IndexList& indices);
208 
209  /// @brief access an element given its coordinates (const version)
210  /// @param[in] indices the list of coordinates, 0-based
211  /// @return a reference to the bin
212  const Bin& element(const IndexList& indices) const;
213 
214  /// @brief the number of elements (product of all the dimensions)
215  size_t numValues() const;
216 
217  public:
218  /// @brief write the contents of the histogram for serialization
219  /// @param[in] msgwriter the writer object
220  std::unique_ptr<Serial::FBHistogramBuilder> write(flatbuffers::FlatBufferBuilder* msgwriter) const;
221 
222  /// @brief read the contents of the histogram for serialization
223  /// @param[in] msgreader the reader object
224  /// @param[in] def the location where to write
225  void read(const Serial::FBHistogram* msgreader, const HistogramDefinition& def);
226 
227  public:
228  /// @brief
229  /// @return
230  const LabelsList& getLabelVector() const;
231 
232  /// @brief
233  /// @param[in] externalData
234  /// @return
235  IOHistogram evalToList(const Tensor& externalData) const;
236 
237 
238  std::unique_ptr<Histogram> collapseIntoNew(const std::string& newName, const HistogramDefinition& newDef, const std::set<uint16_t>& collapsedIndexPositions) const;
239 
240  protected:
241  /// @brief logging facility
242  /// @return stream to be used for logging
243  Log& getLog() const;
244 
245  private:
246 
247  friend std::unique_ptr<Histogram> makeHistogram(const std::string&, const HistogramDefinition&, const Tensor&);
248  friend std::unique_ptr<Histogram> makeHistogram(const std::string&, const HistogramDefinition&, const IndexList&, const LabelsList&);
249 
250  /// @brief set the dimensions of the various coordinates
251  /// @param[in] def the dimensions list (its size specifies the histogram dimensionality)
252  void setDefinition(const HistogramDefinition& def);
253 
254  /// @brief give the histogram a name
255  /// @param[in] name the name
256  void setName(const std::string& name);
257 
258  /// @brief set the value to be used for out-of-bounds coordinates if extrapolation is off
259  /// @param[in] value the default value
260  void setDefaultValue(const Tensor& value);
261  void setDefaultValue(const IndexList& defaultDims, const LabelsList& defaultLabels);
262 
263  /// @brief allocate and initialize the histogram
264  void initHistogram();
265 
266  private:
269 
270  std::string _name;
271 
273  std::string _defaultTensorName;
274 
275  /// whether the grid is initialized
277  };
278 
279  /// @brief
280  /// \overload makeHistogram
281  /// @param[in] name
282  /// @param[in] def
283  /// @param[in] defaultValue
284  /// @return
285  std::unique_ptr<Histogram> makeHistogram(const std::string& name, const HistogramDefinition& def, const Tensor& defaultValue);
286 
287  /// @brief
288  /// \overload makeHistogram
289  /// @param[in] name
290  /// @param[in] def
291  /// @param[in] defaultTensorDims
292  /// @param[in] defaultTensorLabels
293  /// @return
294  std::unique_ptr<Histogram> makeHistogram(const std::string& name, const HistogramDefinition& def,
295  const IndexList& defaultTensorDims, const LabelsList& defaultTensorLabels);
296 
297  /// @brief
298  /// @param[in] a
299  /// @param[in] b
300  /// @return
301  Histogram operator+(const Histogram& a, const Histogram& b);
302 
303 } // namespace Hammer
304 
306 
307 #endif
void initHistogram()
allocate and initialize the histogram
Definition: Histogram.cc:209
const std::string & name() const
get the histogram name
Definition: Histogram.cc:151
void setDefinition(const HistogramDefinition &def)
set the dimensions of the various coordinates
Definition: Histogram.cc:107
Tensor _weight
tensor sum of weights
Definition: Histogram.hh:93
std::vector< Bin > DataType
Definition: Histogram.hh:120
const IndexList & dims() const
get the dimensions of all the coordinates at once
Definition: Histogram.cc:147
Forward declaration of serialization related typedefs and includes.
Bin class with Tensor contents.
Definition: Histogram.hh:47
reference operator[](PositionType pos)
Definition: Histogram.cc:127
Multidimensional histogram class with Tensor as cell bins.
Definition: Histogram.hh:39
Hammer data types declarations.
const Tensor & getWeight(Args...rest) const
get the value of a histogram bin based on an index of the bin
const Tensor & getSquaredWeight(Args...rest) const
get the value of a histogram bin based on an index of the bin
size_t getNumEvents(Args...rest) const
get the value of a histogram bin based on an index of the bin
void clear()
reset the histogram
Definition: Histogram.cc:171
size_t PositionType
void setDefaultValue(const Tensor &value)
set the value to be used for out-of-bounds coordinates if extrapolation is off
Definition: Histogram.cc:155
iterator begin()
Definition: Histogram.cc:111
std::string _name
Definition: Histogram.hh:270
std::vector< BinContents > IOHistogram
Definition: IOTypes.hh:132
size_t getNumEvents() const
the number of entries in the bin (number of times addWeight has been called)
Definition: Histogram.cc:71
IOHistogram evalToList(const Tensor &externalData) const
Definition: Histogram.cc:333
void clear()
clears the contents of the bin (including the shapes of the tensors)
Definition: Histogram.cc:49
Non-sparse tensor indexer.
Hammer configuration definitions.
uint16_t IndexType
Histogram & operator+=(const Histogram &other)
Definition: Histogram.cc:319
unique_ptr< Histogram > makeHistogram(const string &name, const HistogramDefinition &def, const Tensor &defaultValue)
Definition: Histogram.cc:393
const Tensor & getWeight() const
the sum of tensor weights contained in the bin
Definition: Histogram.cc:63
Bin(const Tensor &t, bool withErrors=false)
constructor based on a tensor (for determining shape and labels)
Definition: Histogram.cc:25
void addWeight(const Tensor &t)
add the values of a tensor to the current bin.
Definition: Histogram.cc:55
std::unique_ptr< Histogram > collapseIntoNew(const std::string &newName, const HistogramDefinition &newDef, const std::set< uint16_t > &collapsedIndexPositions) const
Definition: Histogram.cc:375
std::vector< IndexType > IndexList
std::unique_ptr< Serial::FBHistogramBuilder > write(flatbuffers::FlatBufferBuilder *msgwriter) const
write the contents of the histogram for serialization
Definition: Histogram.cc:225
Logging class.
Definition: Logging.hh:33
IndexType dim(IndexType index) const
dimension of a specific coordinate
Definition: Histogram.cc:143
Bin & element(const IndexList &indices)
convert the indices into the position in the flattened contents into vector organized as row-major ...
Definition: Histogram.cc:189
Tensor labeled indexer.
Histogram definition class.
const Tensor & getSquaredWeight() const
the sum of (outer) squared tensor weights contained in the bin, or an empty tensor if the errors are ...
Definition: Histogram.cc:67
Multidimensional tensor class with complex numbers as elements.
Definition: Tensor.hh:33
Hammer histogram class template methods definitions.
size_t numValues() const
the number of elements (product of all the dimensions)
Definition: Histogram.cc:135
Histogram()
constructor
Definition: Histogram.cc:95
DataType::iterator iterator
Definition: Histogram.hh:125
Bin & operator+=(const Bin &other)
Definition: Histogram.cc:75
size_t _NEvents
number of entries
Definition: Histogram.hh:95
MultiDimensional::LabeledIndexing< MultiDimensional::AlignedIndexing > _defaultTensorIndexing
Definition: Histogram.hh:272
std::vector< IndexLabel > LabelsList
void clearData()
reset the histogram contents but not the shape of the histogram
Definition: Histogram.cc:216
Tensor _weight2
tensor sum of squared weights (in the outer square sense)
Definition: Histogram.hh:94
std::string _defaultTensorName
Definition: Histogram.hh:273
void read(const Serial::FBHistogram *msgreader, const HistogramDefinition &def)
read the contents of the histogram for serialization
Definition: Histogram.cc:269
Declarations for Hammer IO structs.
Histogram & operator=(const Histogram &other)=default
void fillBin(const Tensor &value, Args...rest)
set the value of a histogram bin based on the bin indices
DataType::const_iterator const_iterator
Definition: Histogram.hh:126
const HistogramDefinition * _definition
Definition: Histogram.hh:267
Log & getLog() const
logging facility
Definition: Histogram.cc:177
iterator end()
Definition: Histogram.cc:119
size_t rank() const
dimensionality of the histograms
Definition: Histogram.cc:139
Histogram operator+(const Histogram &first, const Histogram &second)
Definition: Histogram.cc:412
Hammer tensor class.
const LabelsList & getLabelVector() const
Definition: Histogram.cc:329
bool _initialized
whether the grid is initialized
Definition: Histogram.hh:276
Bin & operator=(const Bin &other)=default
virtual ~Histogram()
destructor
Definition: Histogram.cc:99
void setName(const std::string &name)
give the histogram a name
Definition: Histogram.cc:103
Sparse tensor indexer.