23 namespace MD = MultiDimensional;
25 Histogram::Bin::Bin(
const Tensor& t,
bool withErrors) : _NEvents{0}, _withErrors{withErrors} {
30 _weight2.outerSquare();
96 _name{
"Histogram"}, _defaultTensorName{
"Bin"}, _initialized{
false} {
112 return _data.begin();
116 return _data.begin();
156 if(value.
rank() > 0) {
164 if(defaultDims.size() > 0) {
185 MSG_ERROR(
string(
"Error setting element: ") + e.what() + string(
" Element not set."));
191 throw RangeError(
"Not enough indices (" + to_string(indices.size()) +
") for tensor '" +
_name +
193 ": all your base are belong to us.");
201 throw RangeError(
"Not enough indices (" + to_string(indices.size()) +
") for tensor '" +
_name +
203 ": all your base are belong to us.");
225 unique_ptr<Serial::FBHistogramBuilder>
Histogram::write(flatbuffers::FlatBufferBuilder* msgwriter)
const {
226 vector<flatbuffers::Offset<Serial::FBTensor>> weightbins;
227 vector<flatbuffers::Offset<Serial::FBTensor>> weightbins2;
228 vector<uint16_t> poslist;
229 vector<uint64_t> nEvs;
230 weightbins.reserve(
_data.size());
231 nEvs.reserve(
_data.size());
232 poslist.reserve(
_data.size());
233 if (
_data.size() > 65535ul) {
234 MSG_ERROR(
"Error saving binary histogram: too many entries!");
237 weightbins2.reserve(
_data.size());
239 for (
size_t i = 0; i<
_data.size(); ++i) {
242 _data[i].getWeight().write(msgwriter, &val);
243 weightbins.push_back(val);
245 poslist.push_back(static_cast<uint16_t>(i));
248 _data[i].getSquaredWeight().write(msgwriter, &val2);
249 weightbins2.push_back(val2);
253 auto serialW = msgwriter->CreateVector(weightbins);
254 auto serialW2 = msgwriter->CreateVector(weightbins2);
255 auto serialPos = msgwriter->CreateVector(poslist);
256 auto serialNEvs = msgwriter->CreateVector(nEvs);
257 auto name = msgwriter->CreateString(
_name);
259 unique_ptr<Serial::FBHistogramBuilder> serialHisto{
new Serial::FBHistogramBuilder{*msgwriter}};
260 serialHisto->add_w(serialW);
261 serialHisto->add_w2(serialW2);
262 serialHisto->add_n(serialNEvs);
263 serialHisto->add_pos(serialPos);
264 serialHisto->add_name(
name);
271 if (msgreader !=
nullptr) {
272 auto w = msgreader->w();
273 auto w2 = msgreader->w2();
274 auto num = msgreader->n();
275 auto pos = msgreader->pos();
278 _name = msgreader->name()->c_str();
281 _data.reserve(fullSize);
282 size_t currentIndex = 0ul;
283 bool firstread =
true;
285 for (
unsigned int i = 0; i < w->size(); ++i) {
286 auto elemW = w->Get(i);
287 auto elemN = num->Get(i);
288 auto elemPos = pos->Get(i);
289 const Serial::FBTensor* elemW2 =
nullptr;
293 Bin bin(elemW,elemW2,elemN);
298 if(defaultDimensions.size() > 0) {
307 if(elemPos > currentIndex) {
308 _data.insert(
_data.end(), elemPos-currentIndex,
Bin(empty, keepErrors));
309 currentIndex = elemPos;
311 _data.push_back(bin);
314 size_t currentSize =
_data.size();
315 _data.insert(
_data.end(), fullSize - currentSize,
Bin(empty, keepErrors));
321 for (
size_t i = 0; i <
_data.size(); ++i) {
326 throw Error(
"Histogram dimension/sizes/binning do not match");
347 for(
auto& elem:
_data) {
348 size_t num = elem.getNumEvents();
352 Tensor w = elem.getWeight();
355 MSG_ERROR(
"Invalid rank, " + to_string(w.
rank()) +
", at end of evaluation.");
359 Tensor w2 = elem.getSquaredWeight();
361 w2.
dot(externalData2);
364 MSG_ERROR(
"Invalid rank at end of evaluation");
377 unique_ptr<Histogram> result{
new Histogram{}};
381 result->_name = newName;
382 result->initHistogram();
383 for (
size_t i = 0; i <
_data.size(); ++i) {
388 (result->_data)[pospairs.first] +=
_data[i];
394 unique_ptr<Histogram> res(
new Histogram{});
396 res->setDefinition(def);
397 res->setDefaultValue(defaultValue);
398 res->initHistogram();
404 unique_ptr<Histogram> res(
new Histogram{});
406 res->setDefinition(def);
407 res->setDefaultValue(defaultTensorDims, defaultTensorLabels);
408 res->initHistogram();
void initHistogram()
allocate and initialize the histogram
const std::string & name() const
get the histogram name
void setDefinition(const HistogramDefinition &def)
set the dimensions of the various coordinates
Tensor _weight
tensor sum of weights
BasicIndexing::StrideMap getBinStrides(const UniqueIndexList &positions) const
TensorData makeEmptySparse(const IndexList &dimensions, const LabelsList &labels)
std::complex< double > & element(const IndexList &indices={})
access an element given its indices
const IndexList & dims() const
get the dimensions of all the coordinates at once
const MD::BinnedIndexing< MD::SequentialIndexing > & getIndexing() const
Bin class with Tensor contents.
reference operator[](PositionType pos)
Multidimensional histogram class with Tensor as cell bins.
LabelsList labels() const
get the labels of all the indices at once
const std::string & name() const
get the tensor name
Tensor outerSquare(const Tensor &first)
creates a tensor with twice the rank by multiplying the tensor with it's hermitean conjugate ...
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
void setDefaultValue(const Tensor &value)
set the value to be used for out-of-bounds coordinates if extrapolation is off
const IndexList & dims() const
get the dimensions of all the indices at once
std::vector< BinContents > IOHistogram
size_t getNumEvents() const
the number of entries in the bin (number of times addWeight has been called)
IOHistogram evalToList(const Tensor &externalData) const
bool isSameBinShape(const BinEdgeList &otherEdges, const IndexList &otherIndices, bool otherUnderOverFlow) const
void clear()
clears the contents of the bin (including the shapes of the tensors)
PositionPair splitPosition(PositionType position, const StrideMap &conversion) const
const LabelsList & labels() const
get the labels of all the indices at once
bool shouldKeepErrors() const
Histogram & operator+=(const Histogram &other)
PositionType indicesToPos(const IndexList &indices) const
convert the indices into the position indicizing a sparse tensor container organized as row-major ...
unique_ptr< Histogram > makeHistogram(const string &name, const HistogramDefinition &def, const Tensor &defaultValue)
size_t rank() const
rank of the tensor
static Log & getLog(const std::string &name)
Get a logger with the given name.
const Tensor & getWeight() const
the sum of tensor weights contained in the bin
size_t rank() const
rank of the tensor
Bin(const Tensor &t, bool withErrors=false)
constructor based on a tensor (for determining shape and labels)
void addWeight(const Tensor &t)
add the values of a tensor to the current bin.
Hammer exception definitions.
size_t rank() const
rank of the tensor
std::unique_ptr< Histogram > collapseIntoNew(const std::string &newName, const HistogramDefinition &newDef, const std::set< uint16_t > &collapsedIndexPositions) const
Sparse tensor data container.
std::vector< IndexType > IndexList
std::unique_ptr< Serial::FBHistogramBuilder > write(flatbuffers::FlatBufferBuilder *msgwriter) const
write the contents of the histogram for serialization
Order-0 tensor data container.
IndexType dim(IndexType index) const
dimension of a specific coordinate
Bin & element(const IndexList &indices)
convert the indices into the position in the flattened contents into vector organized as row-major ...
const Tensor & getSquaredWeight() const
the sum of (outer) squared tensor weights contained in the bin, or an empty tensor if the errors are ...
Multidimensional tensor class with complex numbers as elements.
contents of a histogram bin after full contraction (real weights) to be used to export the histogram ...
size_t numValues() const
the number of elements (product of all the dimensions)
IndexType dim(IndexType index) const
dimension of a specific component
PositionType numValues() const
the number of elements (product of all the dimensions)
TensorData makeEmptyScalar()
DataType::iterator iterator
Bin & operator+=(const Bin &other)
size_t _NEvents
number of entries
MultiDimensional::LabeledIndexing< MultiDimensional::AlignedIndexing > _defaultTensorIndexing
std::vector< IndexLabel > LabelsList
void clearData()
reset the histogram contents but not the shape of the histogram
Tensor _weight2
tensor sum of squared weights (in the outer square sense)
std::string _defaultTensorName
void read(const Serial::FBHistogram *msgreader, const HistogramDefinition &def)
read the contents of the histogram for serialization
Tensor & dot(const Tensor &other, const UniqueLabelsList &indices={})
contract this tensor with another and stores the result in this tensor
IndexList dims() const
get the dimensions of all the indices at once
void fillBin(const Tensor &value, Args...rest)
set the value of a histogram bin based on the bin indices
DataType::const_iterator const_iterator
const HistogramDefinition * _definition
Log & getLog() const
logging facility
Out-of-range error class.
size_t rank() const
dimensionality of the histograms
Histogram operator+(const Histogram &first, const Histogram &second)
Serialization related typedefs and includes.
const LabelsList & getLabelVector() const
bool _initialized
whether the grid is initialized
virtual ~Histogram()
destructor
void setName(const std::string &name)
give the histogram a name