Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistogramSet.cc
Go to the documentation of this file.
1 ///
2 /// @file HistogramSet.cc
3 /// @brief Container class for histograms belonging to different event types
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 #include <functional>
13 
15 #include "Hammer/Math/Histogram.hh"
17 
19 
20 #include "Hammer/Math/Utils.hh"
21 
22 using namespace std;
23 
24 namespace Hammer {
25 
26  HistogramSet::HistogramSet(bool compressed) : _isCompressed{compressed} {
27  }
28 
29  HistogramSet::HistogramSet(const HistogramSet& other, const string& newName, const HistogramDefinition& newDef, const std::set<uint16_t>& collapsedIndexPositions)
30  : _compressionDict{other._compressionDict},
31  _compressionReverseDict{other._compressionReverseDict},
32  _labelsDict{other._labelsDict},
33  _labelsReverseDict{other._labelsReverseDict},
34  _isCompressed{other._isCompressed} {
35  transform(other._data.begin(), other._data.end(), back_inserter(_data),
36  [&](const unique_ptr<Histogram>& original) -> unique_ptr<Histogram> {
37  return original->collapseIntoNew(newName, newDef, collapsedIndexPositions);
38  });
39  }
40 
42  auto it = _compressionDict.find(id);
43  if(it != _compressionDict.end()) {
44  return _data[it->second].get();
45  }
46  else {
47  return nullptr;
48  }
49  }
50 
52  if(id < _data.size()) {
53  return _data[id].get();
54  }
55  else {
56  return nullptr;
57  }
58  }
59 
60  size_t HistogramSet::addEventId(const EventUID& id, const LabelsList& labels) {
61  LabelsList tmpLabels = labels;
62  sort(tmpLabels.begin(), tmpLabels.end());
63  auto it = _compressionDict.find(id);
64  if(it != _compressionDict.end()) {
65  return it->second;
66  }
67  size_t pos = _data.size();
68  if (_isCompressed) {
69  auto itl = _labelsReverseDict.find(tmpLabels);
70  if(itl != _labelsReverseDict.end()) {
71  pos = itl->second;
72  _compressionReverseDict.find(pos)->second.insert(id);
73  _compressionDict.insert({id, pos});
74  return pos;
75  }
76  else {
77  _labelsReverseDict.insert({tmpLabels, pos});
78  }
79  }
80  _labelsDict.insert({pos, tmpLabels});
81  _compressionDict.insert({id, pos});
82  _compressionReverseDict.insert({pos, {id}});
83  return pos;
84  }
85 
86  Histogram* HistogramSet::addHistogram(size_t id, std::unique_ptr<Histogram> hist) {
87  ASSERT(id <= _data.size());
88  if(id == _data.size()) {
89  _data.push_back(move(hist));
90  }
91  return _data[id].get();
92  }
93 
95  _data.clear();
96  _compressionDict.clear();
98  _labelsDict.clear();
99  _labelsReverseDict.clear();
100  }
101 
102  EventUIDGroup HistogramSet::read(const Serial::FBHistogram* msgreader, const HistogramDefinition& def, bool merge) {
103  auto idsets = msgreader->idset()->evtids();
104  vector<EventUID> keys;
105  keys.reserve(idsets->size());
106  for (unsigned int i = 0; i < idsets->size(); ++i) {
107  auto procids = idsets->Get(i)->ids();
108  EventUID key;
109  for (unsigned int j = 0; j < procids->size(); ++j) {
110  key.insert(procids->Get(j));
111  }
112  keys.push_back(key);
113  }
114  size_t newK = _data.size();
115  set<size_t> candKs;
116  auto itNewKeys = partition(keys.begin(), keys.end(), [&](const EventUID& elem) -> bool { auto it = _compressionDict.find(elem);
117  if(it != _compressionDict.end()) {
118  candKs.insert(it->second);
119  return true;
120  }
121  return false; });
122  if(candKs.size() > 1) {
123  throw Error("Event ID grouping in Histogram merging is not consistent.\n"
124  "Mahna Mahna\nDo doo be-do-do\nMahna Mahna\nDo do-do do\n"
125  "Mahna Mahna\nDo doo be-do-do be-do-do be-do-do be-do-do-doodle do do do-doo do!");
126  }
127  auto itCompRev = _compressionReverseDict.begin();
128  if(candKs.size() > 0) {
129  newK = *candKs.begin();
130  itCompRev = _compressionReverseDict.find(newK);
131  }
132  else {
133  itCompRev = _compressionReverseDict.insert({newK, set<EventUID>{}}).first;
134  }
135  for(auto it = itNewKeys; it != keys.end(); ++it) {
136  _compressionDict.insert({*it, newK});
137  itCompRev->second.insert(*it);
138  }
139  if (newK == _data.size()) {
140  auto idlabels = msgreader->idset()->labels();
141  LabelsList labels;
142  for (unsigned int l = 0; l < idlabels->size(); ++l) {
143  labels.push_back(static_cast<IndexLabel>(idlabels->Get(l)));
144  }
145  _labelsDict.insert({newK, labels});
146  if (_isCompressed) {
147  _labelsReverseDict.insert({labels, newK});
148  }
149  _data.emplace_back(unique_ptr<Histogram>{new Histogram{}});
150  _data.back()->read(msgreader, def);
151  }
152  else {
153  if(merge) {
154  Histogram h{};
155  h.read(msgreader, def);
156  *_data[newK] += h;
157  }
158  else {
159  _data[newK].reset(new Histogram{});
160  _data[newK]->read(msgreader, def);
161  }
162  }
163  EventUIDGroup result{keys.begin(), keys.end()};
164  return result;
165  }
166 
167  unique_ptr<Serial::FBHistogramBuilder> HistogramSet::write(flatbuffers::FlatBufferBuilder* msgwriter, const EventUID& id) const {
168  auto it = _compressionDict.find(id);
169  if(it != _compressionDict.end()) {
170  auto& evtids = _compressionReverseDict.find(it->second)->second;
171  vector<flatbuffers::Offset<Serial::FBIdSet>> serialEvtIds;
172  for(auto& elem: evtids) {
173  vector<uint64_t> procids;
174  procids.reserve(elem.size());
175  copy(elem.begin(), elem.end(), back_inserter(procids));
176  auto serialProcids = msgwriter->CreateVector(procids);
177  Serial::FBIdSetBuilder serialEventId{*msgwriter};
178  serialEventId.add_ids(serialProcids);
179  auto key = serialEventId.Finish();
180  serialEvtIds.push_back(key);
181  }
182  auto serialIds = msgwriter->CreateVector(serialEvtIds);
183  auto& evtLabels = _labelsDict.find(it->second)->second;
184  vector<int16_t> labels;
185  for (auto elem : evtLabels) {
186  labels.push_back(static_cast<int16_t>(elem));
187  }
188  auto serialLabels = msgwriter->CreateVector(labels);
189  Serial::FBEvtIdSetBuilder serialEventIdSet{*msgwriter};
190  serialEventIdSet.add_evtids(serialIds);
191  serialEventIdSet.add_labels(serialLabels);
192  auto idset = serialEventIdSet.Finish();
193  auto hbuilder = _data[it->second]->write(msgwriter);
194  hbuilder->add_idset(idset);
195  return hbuilder;
196  }
197  else {
198  return nullptr;
199  }
200  }
201 
202  static BinContents operator+(const BinContents& a, const BinContents& b) {
203  BinContents result = a;
204  result.sumWi += b.sumWi;
205  result.sumWi2 += b.sumWi2;
206  result.n += b.n;
207  return result;
208  }
209 
210  EventIdGroupDict<IOHistogram> HistogramSet::specializeEventHistograms(const vector<Tensor>& externalData) const {
212  vector<IOHistogram> iodata;
213  for(size_t i = 0; i < _data.size(); ++i) {
214  auto it = _compressionReverseDict.find(i);
215  result.emplace(it->second, _data[i]->evalToList(externalData[i]));
216  }
217  return result;
218  }
219 
220  IOHistogram HistogramSet::specializeSumHistogram(const vector<Tensor>& externalData) const {
221  if(_data.size() == 0) {
222  return {};
223  }
224  IOHistogram result = _data[0]->evalToList(externalData[0]);
225  for(size_t i = 1; i < _data.size(); ++i) {
226  auto result_temp = _data[i]->evalToList(externalData[i]);
227  transform(result.begin(), result.end(), result_temp.begin(), result.begin(), plus<BinContents>()); }
228  return result;
229  }
230 
232  EventUIDGroup result;
233  for(auto& elem: _compressionDict) {
234  result.insert(elem.first);
235  }
236  return result;
237  }
238 
239  vector<LabelsList> HistogramSet::getHistogramLabels() const {
240  ASSERT(_labelsDict.size() == _data.size());
241  vector<LabelsList> result;
242  result.reserve(_labelsDict.size());
243  transform(_labelsDict.begin(), _labelsDict.end(), back_inserter(result), [](const pair<size_t, LabelsList>& val) -> LabelsList { return val.second; });
244  return result;
245  }
246 
247  vector<EventUID> HistogramSet::getEventUIDRepresentatives() const {
248  vector<EventUID> result;
249  result.reserve(_compressionReverseDict.size());
250  transform(_compressionReverseDict.begin(), _compressionReverseDict.end(), back_inserter(result), [](const pair<size_t, set<EventUID>>& val) -> EventUID {return *val.second.begin(); });
251  return result;
252  }
253 
254 }
std::set< ProcessUID > EventUID
Definition: IndexTypes.hh:53
Multidimensional histogram class with Tensor as cell bins.
Definition: Histogram.hh:39
#define ASSERT(x)
Definition: Exceptions.hh:95
size_t addEventId(const EventUID &id, const LabelsList &labels)
Definition: HistogramSet.cc:60
std::vector< BinContents > IOHistogram
Definition: IOTypes.hh:132
EventUIDGroup read(const Serial::FBHistogram *msgreader, const HistogramDefinition &def, bool merge)
std::set< EventUID > EventUIDGroup
Definition: IndexTypes.hh:56
Container class for histograms belonging to different event types.
Histogram * getHistogram(const EventUID &id)
Definition: HistogramSet.cc:41
std::vector< std::unique_ptr< Histogram > > _data
Definition: HistogramSet.hh:64
std::vector< LabelsList > getHistogramLabels() const
EventIdGroupDict< IOHistogram > specializeEventHistograms(const std::vector< Tensor > &externalData) const
std::unique_ptr< Serial::FBHistogramBuilder > write(flatbuffers::FlatBufferBuilder *msgwriter, const EventUID &id) const
UMap< LabelsList, size_t > _labelsReverseDict
Definition: HistogramSet.hh:62
Hammer histogram class.
EventUIDGroup getEventIdsInHistogram() const
EventIdDict< size_t > _compressionDict
Definition: HistogramSet.hh:58
std::map< size_t, LabelsList > _labelsDict
Definition: HistogramSet.hh:61
UMap< EventUIDGroup, T > EventIdGroupDict
Definition: IndexTypes.hh:58
Histogram definition class.
HistogramSet(bool compressed=false)
Definition: HistogramSet.cc:26
IOHistogram specializeSumHistogram(const std::vector< Tensor > &externalData) const
Generic error class.
Definition: Exceptions.hh:23
contents of a histogram bin after full contraction (real weights) to be used to export the histogram ...
Definition: IOTypes.hh:126
std::map< size_t, EventUIDGroup > _compressionReverseDict
Definition: HistogramSet.hh:59
std::vector< IndexLabel > LabelsList
void read(const Serial::FBHistogram *msgreader, const HistogramDefinition &def)
read the contents of the histogram for serialization
Definition: Histogram.cc:269
std::vector< EventUID > getEventUIDRepresentatives() const
double sumWi
sum of weights
Definition: IOTypes.hh:127
double sumWi2
sum of squared weights
Definition: IOTypes.hh:128
Histogram operator+(const Histogram &first, const Histogram &second)
Definition: Histogram.cc:412
Serialization related typedefs and includes.
Histogram * addHistogram(size_t id, std::unique_ptr< Histogram > hist)
Definition: HistogramSet.cc:86