Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProcResults.cc
Go to the documentation of this file.
1 ///
2 /// @file ProcResults.cc
3 /// @brief Container for process-related results of weight calculation
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++ -*-
13 #include "Hammer/ProcResults.hh"
14 #include "Hammer/Tools/Logging.hh"
15 #include "Hammer/Tools/Utils.hh"
16 
17 #include <iostream>
18 
19 using namespace std;
20 
21 namespace Hammer {
22 
23  ProcResults::ProcResults() {
24  // initSettings();
25  }
26 
27  ProcResults::ProcResults(const Serial::FBProcData* msgreader) {
28  // initSettings();
29  read(msgreader, false);
30  }
31 
32  // ProcResults::~ProcResults() {
33  // }
34 
35  const Tensor& ProcResults::processAmplitude(WTerm what) const {
36  return _processAmplitude.get(what);
37  }
38 
39  Tensor& ProcResults::processAmplitude(WTerm what) {
40  return _processAmplitude.get(what);
41  }
42 
43  const Tensor& ProcResults::processAmplitudeSquared(WTerm what) const {
44  return _processSquaredAmplitude.get(what);
45  }
46 
47  Tensor& ProcResults::processAmplitudeSquared(WTerm what) {
48  return _processSquaredAmplitude.get(what);
49  }
50 
51  vector<reference_wrapper<const Tensor>> ProcResults::processFormFactors(const string& name) const {
52  vector<reference_wrapper<const Tensor>> result;
53  auto it = _processFormFactors.find(name);
54  if (it != _processFormFactors.end()) {
55  transform(it->second.begin(), it->second.end(), back_inserter(result),
56  [](const Tensor& elem) -> reference_wrapper<const Tensor> { return cref(elem); });
57  }
58  return result;
59  }
60 
61  vector<reference_wrapper<Tensor>> ProcResults::processFormFactors(const string& name) {
62  vector<reference_wrapper<Tensor>> result;
63  auto it = _processFormFactors.find(name);
64  if (it != _processFormFactors.end()) {
65  transform(it->second.begin(), it->second.end(), back_inserter(result),
66  [](Tensor& elem) -> reference_wrapper<Tensor> { return ref(elem); });
67  }
68  return result;
69  }
70 
71  void ProcResults::appendFormFactor(const std::string& name, const Tensor& amp) {
72  _processFormFactors[name].push_back(amp);
73  }
74 
75  void ProcResults::appendFormFactor(const std::string& name, Tensor&& amp) {
76  _processFormFactors[name].push_back(move(amp));
77  }
78 
79  void ProcResults::clearFormFactors() {
80  _processFormFactors.clear();
81  }
82 
83  bool ProcResults::haveFormFactors() const {
84  return _processFormFactors.size() > 0;
85  }
86 
87  vector<string> ProcResults::availableSchemes() const {
88  vector<string> result;
89  result.reserve(_processFormFactors.size());
90  transform(_processFormFactors.begin(), _processFormFactors.end(), back_inserter(result), [](const pair<string, vector<Tensor>>& val) -> string { return val.first; });
91  return result;
92  }
93 
94  const Tensor& ProcResults::processWeight(const string& name) const {
95  return getOrThrow(_processWeights, name, RangeError("Invalid scheme name"));
96  }
97 
98  Tensor& ProcResults::processWeight(const string& name) {
99  return getOrThrow(_processWeights, name, RangeError("Invalid scheme name"));
100  }
101 
102  void ProcResults::setProcessWeight(const string& name, const Tensor& amp) {
103  _processWeights[name] = amp;
104  }
105 
106  void ProcResults::setProcessWeight(const string& name, Tensor&& amp) {
107  _processWeights[name] = move(amp);
108  }
109 
110  void ProcResults::clearWeights() {
111  _processWeights.clear();
112  }
113 
114  bool ProcResults::haveWeights() const {
115  return _processWeights.size() > 0;
116  }
117 
118 
119  void ProcResults::defineSettings() {
120  setPath("ProcessWrite");
121  addSetting<bool>("Amplitudes", true);
122  addSetting<bool>("SquaredAmplitudes", true);
123  addSetting<bool>("FormFactors", true);
124  }
125 
126  void ProcResults::write(flatbuffers::FlatBufferBuilder* msgwriter, flatbuffers::Offset<Serial::FBProcData>* msg) const {
127  vector<flatbuffers::Offset<Serial::FBFormFactor>> ffs;
128  vector<flatbuffers::Offset<Serial::FBTensor>> wgtsVals;
129  vector<string> wgtsNames;
130  ffs.reserve(_processFormFactors.size());
131  wgtsVals.reserve(_processWeights.size());
132  wgtsNames.reserve(_processWeights.size());
133  if(isOn("FormFactors")) {
134  for (auto& elem : _processFormFactors) {
135  auto name = msgwriter->CreateString(elem.first);
136  vector<flatbuffers::Offset<Serial::FBTensor>> ffVec;
137  ffVec.reserve(elem.second.size());
138  for (auto& elem2 : elem.second) {
140  elem2.write(msgwriter, &val);
141  ffVec.push_back(val);
142  }
143  auto serialFFVec = msgwriter->CreateVector(ffVec);
144  Serial::FBFormFactorBuilder serialFF{*msgwriter};
145  serialFF.add_id(name);
146  serialFF.add_ffsvals(serialFFVec);
147  auto resFF = serialFF.Finish();
148  ffs.push_back(resFF);
149  }
150  }
151  for (auto& elem : _processWeights) {
152  wgtsNames.push_back(elem.first);
154  elem.second.write(msgwriter, &val);
155  wgtsVals.push_back(val);
156  }
161  if(isOn("Amplitudes")) {
162  _processAmplitude.numerator.write(msgwriter, &ampnum);
163  _processAmplitude.denominator.write(msgwriter, &ampden);
164  }
165  if(isOn("SquaredAmplitudes")) {
166  _processSquaredAmplitude.numerator.write(msgwriter, &amp2num);
167  _processSquaredAmplitude.denominator.write(msgwriter, &amp2den);
168  }
170  if(isOn("FormFactors")) {
171  serialFF = msgwriter->CreateVector(ffs);
172  }
173  auto serialWgtN = msgwriter->CreateVectorOfStrings(wgtsNames);
174  auto serialWgtV = msgwriter->CreateVector(wgtsVals);
175  Serial::FBProcDataBuilder serialprocess{*msgwriter};
176  if(isOn("Amplitudes")) {
177  serialprocess.add_ampnum(ampnum);
178  serialprocess.add_ampden(ampden);
179  }
180  if(isOn("SquaredAmplitudes")) {
181  serialprocess.add_amp2num(amp2num);
182  serialprocess.add_amp2den(amp2den);
183  }
184  if(isOn("FormFactors")) {
185  serialprocess.add_formfacts(serialFF);
186  }
187  serialprocess.add_weightames(serialWgtN);
188  serialprocess.add_weighvalues(serialWgtV);
189  *msg = serialprocess.Finish();
190  }
191 
192  bool ProcResults::read(const Serial::FBProcData* msgreader, bool merge) {
193  if (msgreader != nullptr) {
194  bool result = true;
195  if(!merge && flatbuffers::IsFieldPresent(msgreader, Serial::FBProcData::VT_AMPNUM)) {
196  _processAmplitude.numerator.read(msgreader->ampnum());
197  _processAmplitude.denominator.read(msgreader->ampden());
198  }
199  if(!merge && flatbuffers::IsFieldPresent(msgreader, Serial::FBProcData::VT_AMP2NUM)) {
200  _processSquaredAmplitude.numerator.read(msgreader->amp2num());
201  _processSquaredAmplitude.denominator.read(msgreader->amp2den());
202  }
203  if(flatbuffers::IsFieldPresent(msgreader, Serial::FBProcData::VT_FORMFACTS)) {
204  auto formfacts = msgreader->formfacts();
205  if(!merge) {
206  _processFormFactors.clear();
207  }
208  for (unsigned int i = 0; i < formfacts->size() && result; ++i) {
209  auto elem = formfacts->Get(i);
210  auto it = _processFormFactors.find(elem->id()->c_str());
211  if(it != _processFormFactors.end() && strcmp(elem->id()->c_str(), "Denominator") != 0) {
212  MSG_ERROR("Try to merge two process form factor schemes with same name '" + string(elem->id()->c_str()) +
213  "'!");
214  result = false;
215  } else {
216  auto res = _processFormFactors.insert({elem->id()->c_str(), vector<Tensor>{}});
217  auto ffVec = elem->ffsvals();
218  for (unsigned int j = 0; j < ffVec->size(); ++j) {
219  auto elem2 = ffVec->Get(j);
220  Tensor t;
221  t.read(elem2);
222  res.first->second.push_back(move(t));
223  }
224  }
225  }
226  }
227  auto wName = msgreader->weightames();
228  auto wVal = msgreader->weighvalues();
229  if(!merge) {
230  _processWeights.clear();
231  }
232  for (unsigned int i = 0; i < wName->size() && result; ++i) {
233  auto it = _processWeights.find(wName->Get(i)->c_str());
234  if(it != _processWeights.end()) {
235  MSG_ERROR("Try to merge two process weights with same name '" + string(wName->Get(i)->c_str()) + "'!");
236  result = false;
237  }
238  else {
239  Tensor t;
240  t.read(wVal->Get(i));
241  _processWeights.insert({wName->Get(i)->c_str(), t});
242  }
243  }
244  return result;
245  }
246  return false;
247  }
248 
249  Log& ProcResults::getLog() const {
250  return Log::getLog("Hammer.ProcResults");
251  }
252 
253 } // namespace Hammer
auto const & getOrThrow(const std::map< KeyType, ValueType > &data, KeyType key, std::exception error)
Definition: Tools/Utils.hh:51
TensorData read(const Serial::FBTensor *msgreader)
Definition: Operations.cc:76
Message logging routines.
Logging class.
Definition: Logging.hh:33
Container for process-related results of weight calculation.
Multidimensional tensor class with complex numbers as elements.
Definition: Tensor.hh:33
#define MSG_ERROR(x)
Definition: Logging.hh:367
void read(const Serial::FBTensor *msgreader)
read the contents of the tensor for serialization
Definition: Tensor.cc:245
Out-of-range error class.
Definition: Exceptions.hh:49
Serialization related typedefs and includes.