Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistogramDefinition.cc
Go to the documentation of this file.
1 ///
2 /// @file HistogramDefinition.cc
3 /// @brief Histogram definition 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 
14 #include "Hammer/Tools/Logging.hh"
16 
17 using namespace std;
18 
19 namespace Hammer {
20 
21  namespace MD = MultiDimensional;
22 
23  HistogramDefinition::HistogramDefinition(bool hasUOFlow, const IndexList& numBins, const MD::BinEdgeList& binEdges,
24  bool shouldCompress, bool keepErrors)
25  : _indexing{numBins, binEdges, hasUOFlow}, _compressHistograms{shouldCompress}, _keepErrors{keepErrors} {
26  }
27 
28  HistogramDefinition::HistogramDefinition(bool hasUOFlow, const IndexList& numBins, const MD::BinRangeList& ranges,
29  bool shouldCompress, bool keepErrors)
30  : _indexing{numBins, ranges, hasUOFlow}, _compressHistograms{shouldCompress}, _keepErrors{keepErrors} {
31  }
32 
33  HistogramDefinition::HistogramDefinition(bool hasUOFlow, const MD::BinEdgeList& binEdges, bool shouldCompress, bool keepErrors)
34  : _indexing{binEdges, hasUOFlow}, _compressHistograms{shouldCompress}, _keepErrors{keepErrors} {
35  }
36 
37  HistogramDefinition::HistogramDefinition(const Serial::FBHistoDefinition* msgreader) {
38  read(msgreader);
39  }
40 
42  return Log::getLog("Hammer.HistogramDefinition");
43  }
44 
46  ASSERT(data->rank() == 1);
47  IndexLabel lab = data->labels()[0];
48  if(_fixedDataPool.find(lab) != _fixedDataPool.end()) {
49  MSG_WARNING("Fixed data already present for label " + to_string(lab) + ". Overwriting. ");
50  }
51  _fixedDataPool[lab] = data;
52  }
53 
55  _fixedDataPool.clear();
56  }
57 
58  const Tensor& HistogramDefinition::getFixedData(EventUID id, const SchemeName& FFscheme, LabelsList tensorLabels) const {
59  auto it = _fixedData.find({id,FFscheme});
60  if(it != _fixedData.end()) {
61  return it->second;
62  }
63  else {
64  vector<pair<MD::SharedTensorData, bool>> pTlist;
65  for(auto& elem: tensorLabels) {
66  auto itt = _fixedDataPool.find(elem);
67  if(itt != _fixedDataPool.end()) {
68  pTlist.push_back({itt->second, false});
69  continue;
70  }
71  itt = _fixedDataPool.find(static_cast<IndexLabel>(-elem));
72  if(itt != _fixedDataPool.end()) {
73  pTlist.push_back({itt->second, true});
74  }
75  }
76  Tensor t{"FixedData",move(pTlist)};
77  auto res = _fixedData.insert({{id, FFscheme}, t});
78  if(res.second) {
79  return res.first->second;
80  }
81  else {
82  throw Error("Unable to build fixed Data for histogram");
83  }
84  }
85  }
86 
88  return _indexing;
89  }
90 
92  return _compressHistograms;
93  }
94 
96  _compressHistograms = value;
97  }
98 
100  return _keepErrors;
101  }
102 
104  _keepErrors = value;
105  }
106 
107 
108  void HistogramDefinition::write(flatbuffers::FlatBufferBuilder* msgwriter,
109  const string& name) const {
110  vector<uint16_t> dims;
111  dims.reserve(_indexing.rank());
112  auto& fullDimensions = _indexing.dims();
113  copy(fullDimensions.begin(), fullDimensions.end(), back_inserter(dims));
114  vector<flatbuffers::Offset<Serial::FBVecDouble>> vecsedges;
115  auto edges = _indexing.edges();
116  for (auto& elem : edges) {
117  auto serialedges1d = msgwriter->CreateVector(elem);
118  Serial::FBVecDoubleBuilder serialBuilder{*msgwriter};
119  serialBuilder.add_value(serialedges1d);
120  vecsedges.push_back(serialBuilder.Finish());
121  }
122  vector<int16_t> labels;
123  labels.reserve(_fixedDataPool.size());
124  vector<flatbuffers::Offset<Serial::FBSingleTensor>> vectensor;
125  vectensor.reserve(_fixedDataPool.size());
126  for(auto& elem: _fixedDataPool) {
127  labels.push_back(static_cast<int16_t>(elem.first));
128  auto res = elem.second->write(msgwriter);
129  vectensor.push_back(flatbuffers::Offset<Serial::FBSingleTensor>(res.first.o));
130  }
131  auto serialedges = msgwriter->CreateVector(vecsedges);
132  auto serialdims = msgwriter->CreateVector(dims);
133  auto seriallabels = msgwriter->CreateVector(labels);
134  auto serialtensors = msgwriter->CreateVector(vectensor);
135  auto serialname = msgwriter->CreateString(name);
136  Serial::FBHistoDefinitionBuilder serialDefHisto{*msgwriter};
137  serialDefHisto.add_name(serialname);
138  serialDefHisto.add_dims(serialdims);
139  serialDefHisto.add_uoverflow(_indexing.hasUnderOverFlow());
140  serialDefHisto.add_edges(serialedges);
141  serialDefHisto.add_compress(_compressHistograms);
142  serialDefHisto.add_keeperrors(_keepErrors);
143  serialDefHisto.add_fixedlabels(seriallabels);
144  serialDefHisto.add_fixeddata(serialtensors);
145  auto out = serialDefHisto.Finish();
146  msgwriter->Finish(out);
147  }
148 
149 
150  void HistogramDefinition::read(const Serial::FBHistoDefinition* msgreader) {
151  if (msgreader != nullptr) {
152  IndexList dims;
153  auto sDims = msgreader->dims();
154  for (unsigned int j = 0; j < sDims->size(); ++j) {
155  dims.push_back(sDims->Get(j));
156  }
157  MD::BinEdgeList edges;
158  auto sEdges = msgreader->edges();
159  for (unsigned int j = 0; j < sEdges->size(); ++j) {
160  MD::BinValue edge;
161  auto sEdge = sEdges->Get(j)->value();
162  for (unsigned int k = 0; k < sEdge->size(); ++k) {
163  edge.push_back(sEdge->Get(k));
164  }
165  edges.push_back(edge);
166  }
167  _indexing = MD::BinnedIndexing<MD::SequentialIndexing>{dims, edges, msgreader->uoverflow()};
168  _compressHistograms = msgreader->compress();
169  _keepErrors = msgreader->keeperrors();
170  auto sLabs = msgreader->fixedlabels();
171  auto sTens = msgreader->fixeddata();
172  for (unsigned int j = 0; j < sLabs->size(); ++j) {
173  _fixedDataPool.insert({static_cast<IndexLabel>(sLabs->Get(j)), MD::SharedTensorData{new MD::VectorContainer{sTens->Get(j)}}});
174  }
175  }
176  else {
177  MSG_ERROR("Invalid record for histogram definition");
178  }
179  }
180 
181  bool HistogramDefinition::checkDefinition(const Serial::FBHistoDefinition* msgreader) const {
182  if (msgreader != nullptr) {
183  auto sDims = msgreader->dims();
184  bool result = true;
185  result &= _compressHistograms == msgreader->compress();
186  result &= _keepErrors == msgreader->keeperrors();
187  for (unsigned int j = 0; j < sDims->size() && result; ++j) {
188  result &= _indexing.dims()[j] == sDims->Get(j);
189  }
190  auto sEdges = msgreader->edges();
191  for (IndexType j = 0; j < sEdges->size() && result; ++j) {
192  auto& edge = _indexing.edge(j);
193  auto sEdge = sEdges->Get(j)->value();
194  for (unsigned int k = 0; k < sEdge->size() && result; ++k) {
195  result &= isZero(fabs(edge[k] - sEdge->Get(k)));
196  }
197  }
198  auto sLabs = msgreader->fixedlabels();
199  auto sTens = msgreader->fixeddata();
200  result &= sLabs->size() == _fixedDataPool.size();
201  for (unsigned int j = 0; j < sLabs->size() && result; ++j) {
202  auto it = _fixedDataPool.find(static_cast<IndexLabel>(sLabs->Get(j)));
203  result &= it != _fixedDataPool.end();
204  if(result) {
205  MD::VectorContainer t{sTens->Get(j)};
206  result &= t.compare(*(it->second));
207  }
208  }
209  return result;
210  }
211  return true;
212  }
213 
214 } // namespace Hammer
#define MSG_WARNING(x)
Definition: Logging.hh:366
std::set< ProcessUID > EventUID
Definition: IndexTypes.hh:53
Log & getLog() const
logging facility
const MD::BinnedIndexing< MD::SequentialIndexing > & getIndexing() const
bool compare(const IContainer &other) const override
std::string SchemeName
Definition: IndexTypes.hh:61
MD::BinnedIndexing< MD::SequentialIndexing > _indexing
void write(flatbuffers::FlatBufferBuilder *msgwriter, const std::string &name) const
#define ASSERT(x)
Definition: Exceptions.hh:95
Non-sparse tensor data container.
const BinEdgeList & edges() const
get the labels of all the indices at once
std::vector< std::vector< double >> BinEdgeList
const IndexList & dims() const
get the dimensions of all the indices at once
UMap< FixedKey, Tensor > _fixedData
Message logging routines.
uint16_t IndexType
void read(const Serial::FBHistoDefinition *msgreader)
std::shared_ptr< IContainer > SharedTensorData
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
size_t rank() const
rank of the tensor
HistogramDefinition(bool hasUnderOverFlow, const IndexList &numBins, const MD::BinEdgeList &binEdges, bool shouldCompress, bool keepErrors)
std::vector< IndexType > IndexList
const BinValue & edge(IndexType pos) const
Logging class.
Definition: Logging.hh:33
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
Histogram definition class.
const Tensor & getFixedData(EventUID id, const std::string &schemeName, LabelsList tensorLabels) const
Multidimensional tensor class with complex numbers as elements.
Definition: Tensor.hh:33
Generic error class.
Definition: Exceptions.hh:23
bool isZero(const std::complex< double > val)
Definition: Math/Utils.hh:25
std::map< IndexLabel, MultiDimensional::SharedTensorData > _fixedDataPool
std::vector< IndexLabel > LabelsList
#define MSG_ERROR(x)
Definition: Logging.hh:367
bool checkDefinition(const Serial::FBHistoDefinition *msgreader) const
std::vector< BinRange > BinRangeList
Serialization related typedefs and includes.
std::vector< double > BinValue
void addFixedData(MultiDimensional::SharedTensorData data)