Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProcessDefinitions.cc
Go to the documentation of this file.
1 ///
2 /// @file ProcessDefinitions.cc
3 /// @brief Container class for storing included/forbidden process info
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 <fstream>
13 
14 #include <boost/algorithm/string.hpp>
15 
16 #include "yaml-cpp/yaml.h"
17 
18 #include "Hammer/Exceptions.hh"
21 #include "Hammer/Tools/Pdg.hh"
22 #include "Hammer/Tools/Utils.hh"
23 #include "Hammer/Math/Utils.hh"
25 
26 using namespace std;
27 
28 namespace Hammer {
29 
30 
31  Log& ProcessDefinitions::getLog() const {
32  return Log::getLog("Hammer.ProcessDefinitions");
33  }
34 
35  void ProcessDefinitions::write(flatbuffers::FlatBufferBuilder* msgwriter,
37  vector<flatbuffers::Offset<Serial::FBVecString>> includes;
38  vector<flatbuffers::Offset<Serial::FBVecString>> forbids;
39  for(auto& elem: _includedDecays) {
40  auto serialvals = msgwriter->CreateVectorOfStrings(elem);
41  Serial::FBVecStringBuilder serialBuilder{*msgwriter};
42  serialBuilder.add_value(serialvals);
43  includes.push_back(serialBuilder.Finish());
44  }
45  for(auto& elem: _forbiddenDecays) {
46  auto serialvals = msgwriter->CreateVectorOfStrings(elem);
47  Serial::FBVecStringBuilder serialBuilder{*msgwriter};
48  serialBuilder.add_value(serialvals);
49  includes.push_back(serialBuilder.Finish());
50  }
51  vector<flatbuffers::Offset<Serial::FBIdSet>> includeprocsvals;
52  vector<uint64_t> includeprocskeys;
53  includeprocsvals.reserve(_includedProcesses.size());
54  includeprocskeys.reserve(_includedProcesses.size());
55  for(auto& elem: _includedProcesses) {
56  includeprocskeys.push_back(elem.first);
57  vector<uint64_t> ids;
58  ids.reserve(elem.second.size());
59  copy(elem.second.begin(),elem.second.end(), back_inserter(ids));
60  auto serialid = msgwriter->CreateVector(ids);
61  Serial::FBIdSetBuilder serialBuilder{*msgwriter};
62  serialBuilder.add_ids(serialid);
63  includeprocsvals.push_back(serialBuilder.Finish());
64  }
65  vector<uint64_t> forbidprocids;
66  forbidprocids.reserve(_forbiddenProcesses.size());
67  copy(_forbiddenProcesses.begin(),_forbiddenProcesses.end(), back_inserter(forbidprocids));
68  auto serialid = msgwriter->CreateVector(forbidprocids);
69  Serial::FBIdSetBuilder serialBuilder{*msgwriter};
70  serialBuilder.add_ids(serialid);
71  auto serialforbidprocs = serialBuilder.Finish();
72  auto serialincludes = msgwriter->CreateVector(includes);
73  auto serialforbids = msgwriter->CreateVector(forbids);
74  auto serialincludprocsvals = msgwriter->CreateVector(includeprocsvals);
75  auto serialincludprocskeys = msgwriter->CreateVector(includeprocskeys);
76  Serial::FBProcDefsBuilder builder{*msgwriter};
77  builder.add_includenames(serialincludes);
78  builder.add_forbidnames(serialforbids);
79  builder.add_includeprocsverts(serialincludprocsvals);
80  builder.add_includeprocsid(serialincludprocskeys);
81  builder.add_forbidprocs(serialforbidprocs);
82  *procdefs = builder.Finish();
83  }
84 
85  bool ProcessDefinitions::read(const Serial::FBProcDefs* msgreader, bool merge) {
86  if (msgreader != nullptr) {
87  auto includenames = msgreader->includenames();
88  _includedDecays.clear();
89  if(!merge) {
90  _includedDecays.reserve(includenames->size());
91  for (unsigned int i = 0; i < includenames->size(); ++i) {
92  auto vecstr = includenames->Get(i)->value();
93  vector<string> tmp;
94  tmp.reserve(vecstr->size());
95  for (unsigned int j = 0; j < vecstr->size(); ++j) {
96  tmp.push_back(vecstr->Get(j)->c_str());
97  }
98  _includedDecays.push_back(tmp);
99  }
100  }
101  auto forbidnames = msgreader->forbidnames();
102  _forbiddenDecays.clear();
103  if (!merge) {
104  _forbiddenDecays.reserve(forbidnames->size());
105  for (unsigned int i = 0; i < forbidnames->size(); ++i) {
106  auto vecstr = forbidnames->Get(i)->value();
107  vector<string> tmp;
108  tmp.reserve(vecstr->size());
109  for (unsigned int j = 0; j < vecstr->size(); ++j) {
110  tmp.push_back(vecstr->Get(j)->c_str());
111  }
112  _forbiddenDecays.push_back(tmp);
113  }
114  }
115  auto includeprocsverts = msgreader->includeprocsverts();
116  auto includeprocsid = msgreader->includeprocsid();
117  _includedProcesses.clear();
118  if (!merge) {
119  for (unsigned int i = 0; i < includeprocsid->size(); ++i) {
120  set<HashId> tmp;
121  auto idset = includeprocsverts->Get(i)->ids();
122  for (unsigned int j = 0; j < idset->size(); ++j) {
123  tmp.insert(idset->Get(j));
124  }
125  _includedProcesses[includeprocsid->Get(i)] = tmp;
126  }
127  }
128  _forbiddenProcesses.clear();
129  auto forbidprocs = msgreader->forbidprocs()->ids();
130  if (!merge) {
131  for (unsigned int j = 0; j < forbidprocs->size(); ++j) {
132  _forbiddenProcesses.insert(forbidprocs->Get(j));
133  }
134  }
135  return true;
136  }
137  return false;
138  }
139 
140  void ProcessDefinitions::addIncludedDecay(const vector<string>& decay) {
141  _includedDecays.push_back(decay);
142  }
143 
144  void ProcessDefinitions::addForbiddenDecay(const vector<string>& decay) {
145  _forbiddenDecays.push_back(decay);
146  }
147 
148  vector<set<HashId>> ProcessDefinitions::includedDecaySignatures() const {
149  return decaySignatures(_includedDecays);
150  }
151 
152  vector<set<HashId>> ProcessDefinitions::forbiddenDecaySignatures() const {
153  return decaySignatures(_forbiddenDecays);
154  }
155 
156  vector<set<HashId>> ProcessDefinitions::decaySignatures(const vector<vector<string>>& container) const {
157  vector<set<HashId>> result;
158  PID& pdg = PID::instance();
159  for(auto& elem: container) {
160  vector<set<HashId>> accum;
161  for(auto& elem2: elem) {
162  auto temp = pdg.expandToValidVertexUIDs(elem2);
163  if(temp.size() == 0) {
164  accum.clear();
165  break;
166  }
167  if(accum.size() == 0) {
168  for(auto id: temp) {
169  accum.push_back({id});
170  }
171  }
172  else {
173  vector<set<HashId>> tempprod(accum.size() * temp.size());
174  for(size_t i = 0; i< accum.size(); ++i) {
175  for(size_t j = 0; j< temp.size(); ++j) {
176  tempprod[i * temp.size() + j] = accum[i];
177  tempprod[i * temp.size() + j].insert(temp[j]);
178  }
179  }
180  accum.swap(tempprod);
181  }
182  }
183  result.insert(result.end(), accum.begin(),accum.end());
184  }
185  return result;
186  }
187 
188  bool ProcessDefinitions::isProcessIncluded(set<HashId> subamplitudes, HashId processId) const {
189  if (_includedDecayCombos.size() == 0) {
190  return true;
191  }
192  if (_includedProcesses.find(processId) != _includedProcesses.end()) {
193  return true;
194  }
195  if (_forbiddenProcesses.find(processId) != _forbiddenProcesses.end()) {
196  return false;
197  }
198  bool included = false;
199  for (auto& elem : _includedDecayCombos) {
200  if (includes(subamplitudes.begin(), subamplitudes.end(), elem.begin(), elem.end())) {
201  included = true;
202  break;
203  }
204  }
205  if (included) {
206  _includedProcesses.insert(make_pair(processId, subamplitudes));
207  return true;
208  } else {
209  _forbiddenProcesses.insert(processId);
210  return false;
211  }
212  }
213 
214  set<HashId> ProcessDefinitions::decayStringsToProcessIds(const vector<vector<string>>& names) const {
215  set<HashId> results;
216  auto subamplitudes = decaySignatures(names);
217  for (auto& elem : subamplitudes) {
218  for (auto& elem2 : _includedProcesses) {
219  if (includes(elem2.second.begin(), elem2.second.end(), elem.begin(), elem.end())) {
220  results.insert(elem2.first);
221  }
222  }
223  }
224  return results;
225  }
226 
227  void ProcessDefinitions::forbidProcess(HashId processId) const {
228  _forbiddenProcesses.insert(processId);
229  _includedProcesses.erase(processId);
230  }
231 
232  void ProcessDefinitions::init() {
233  _includedDecayCombos = includedDecaySignatures();
234  _includedProcesses.clear();
235  _forbiddenProcesses.clear();
236  }
237 
238  YAML::Emitter& operator<<(YAML::Emitter& out, const ProcessDefinitions& s) {
239  out << YAML::convert<ProcessDefinitions>::encode(s);
240  return out;
241  }
242 
243 } // namespace Hammer
244 
245 namespace YAML {
246 
247  Node convert<::Hammer::ProcessDefinitions>::encode(const ::Hammer::ProcessDefinitions& value) {
248  Node node;
249  if (value._includedDecays.size() > 0) {
250  Node tmpNode;
251  for (auto& elem : value._includedDecays) {
252  if (elem.size() == 1) {
253  tmpNode.push_back(elem[0]);
254  } else {
255  Node tmpNode2;
256  for (auto& elem2 : elem) {
257  tmpNode2.push_back(elem2);
258  }
259  tmpNode.push_back(tmpNode2);
260  }
261  }
262  node["Include"] = tmpNode;
263  }
264  if (value._forbiddenDecays.size() > 0) {
265  Node tmpNode;
266  for (auto& elem : value._forbiddenDecays) {
267  if (elem.size() == 1) {
268  tmpNode.push_back(elem[0]);
269  } else {
270  Node tmpNode2;
271  for (auto& elem2 : elem) {
272  tmpNode2.push_back(elem2);
273  }
274  tmpNode.push_back(tmpNode2);
275  }
276  }
277  node["Forbid"] = tmpNode;
278  }
279  return node;
280  }
281 
282  bool convert<::Hammer::ProcessDefinitions>::decode(const Node& node, ::Hammer::ProcessDefinitions& value) {
283  if (node.IsMap()) {
284  for (auto entry : node) {
285  string base = entry.first.as<string>();
286  if (base == "Include" || base == "Forbid") {
287  vector<vector<string>>* container = (base == "Include") ? &value._includedDecays : &value._forbiddenDecays;
288  container->clear();
289  if (entry.second.Type() == YAML::NodeType::Sequence) {
290  YAML::Node seqgroup = entry.second;
291  for (auto elem : seqgroup) {
292  if (elem.Type() == YAML::NodeType::Scalar) {
293  container->push_back({elem.as<string>()});
294  } else if (elem.Type() == YAML::NodeType::Sequence) {
295  try {
296  vector<string> tmpvec = elem.as<vector<string>>();
297  container->push_back(tmpvec);
298  } catch (YAML::Exception&) {
299  return false;
300  }
301  } else {
302  return false;
303  }
304  }
305  } else {
306  return false;
307  }
308  }
309  else {
310  return false;
311  }
312  }
313  }
314  else {
315  return false;
316  }
317  return true;
318  }
319 }
PDG codes to UID functions.
TensorData read(const Serial::FBTensor *msgreader)
Definition: Operations.cc:76
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
std::vector< HashId > expandToValidVertexUIDs(const std::string &name, const bool &hadOnly=false) const
Definition: Pdg.cc:833
std::vector< std::vector< std::string > > _forbiddenDecays
the Hammer forbidden decays
Container class for storing included/forbidden process info.
Hammer exception definitions.
Logging class.
Definition: Logging.hh:33
std::vector< std::vector< std::string > > _includedDecays
the Hammer included decays
Hammer settings manager class.
Hammer class for dealing with particle data.
Definition: Pdg.hh:32
Hammer particle data class.
size_t HashId
Definition: IndexTypes.hh:31
Serialization related typedefs and includes.