Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PurePhaseSpaceDefs.cc
Go to the documentation of this file.
1 ///
2 /// @file PurePhaseSpaceDefs.cc
3 /// @brief Container class for pure phase space vertices definitions
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& PurePhaseSpaceDefs::getLog() const {
32  return Log::getLog("Hammer.PurePhaseSpaceDefs");
33  }
34 
35  void PurePhaseSpaceDefs::write(flatbuffers::FlatBufferBuilder* msgwriter,
36  vector<flatbuffers::Offset<Serial::FBPurePS>>* purePSs) const {
37  purePSs->clear();
38  auto numlabels = msgwriter->CreateVectorOfStrings(_purePhaseSpaceVerticesNames.numerator);
39  vector<uint64_t> ids;
40  ids.reserve(_purePhaseSpaceVertices.numerator.size());
41  copy(_purePhaseSpaceVertices.numerator.begin(), _purePhaseSpaceVertices.numerator.end(), back_inserter(ids));
42  auto numids = msgwriter->CreateVector(ids);
43  Serial::FBPurePSBuilder numBuilder{*msgwriter};
44  numBuilder.add_labels(numlabels);
45  numBuilder.add_ids(numids);
46  purePSs->push_back(numBuilder.Finish());
47  auto denlabels = msgwriter->CreateVectorOfStrings(_purePhaseSpaceVerticesNames.denominator);
48  ids.clear();
49  ids.reserve(_purePhaseSpaceVertices.denominator.size());
50  copy(_purePhaseSpaceVertices.denominator.begin(), _purePhaseSpaceVertices.denominator.end(), back_inserter(ids));
51  auto denids = msgwriter->CreateVector(ids);
52  Serial::FBPurePSBuilder denBuilder{*msgwriter};
53  denBuilder.add_labels(denlabels);
54  denBuilder.add_ids(denids);
55  purePSs->push_back(denBuilder.Finish());
56  }
57 
58  bool PurePhaseSpaceDefs::read(const Serial::FBHeader* msgreader, bool merge) {
59  if(msgreader != nullptr) {
60  auto pureps = msgreader->pureps();
61  ASSERT(pureps->size() != 0);
62  auto labels = pureps->Get(0)->labels();
63  auto ids = pureps->Get(0)->ids();
64  _purePhaseSpaceVerticesNames.numerator.clear();
65  _purePhaseSpaceVertices.numerator.clear();
66  if (!merge) {
67  _purePhaseSpaceVerticesNames.numerator.reserve(labels->size());
68  for (unsigned int i = 0; i < labels->size(); ++i) {
69  _purePhaseSpaceVerticesNames.numerator.push_back(labels->Get(i)->c_str());
70  }
71  for (unsigned int i = 0; i < ids->size(); ++i) {
72  _purePhaseSpaceVertices.numerator.insert(ids->Get(i));
73  }
74  }
75  labels = pureps->Get(1)->labels();
76  ids = pureps->Get(1)->ids();
77  _purePhaseSpaceVerticesNames.denominator.clear();
78  _purePhaseSpaceVertices.denominator.clear();
79  if (!merge) {
80  _purePhaseSpaceVerticesNames.denominator.reserve(labels->size());
81  for (unsigned int i = 0; i < labels->size(); ++i) {
82  _purePhaseSpaceVerticesNames.denominator.push_back(labels->Get(i)->c_str());
83  }
84  for (unsigned int i = 0; i < ids->size(); ++i) {
85  _purePhaseSpaceVertices.denominator.insert(ids->Get(i));
86  }
87  }
88  return true;
89  }
90  return false;
91  }
92 
93  void PurePhaseSpaceDefs::clearPurePhaseSpaceVertices(WTerm what) {
94  switch(what) {
95  case WTerm::NUMERATOR:
96  _purePhaseSpaceVerticesNames.numerator.clear();
97  break;
98  case WTerm::DENOMINATOR:
99  _purePhaseSpaceVerticesNames.denominator.clear();
100  break;
101  case WTerm::COMMON:
102  _purePhaseSpaceVerticesNames.numerator.clear();
103  _purePhaseSpaceVerticesNames.denominator.clear();
104  break;
105  }
106  }
107 
108  void PurePhaseSpaceDefs::addPurePhaseSpaceVertices(const set<string>& vertices, WTerm what) {
109  switch (what) {
110  case WTerm::NUMERATOR:
111  _purePhaseSpaceVerticesNames.numerator.insert(_purePhaseSpaceVerticesNames.numerator.end(),
112  vertices.begin(), vertices.end());
113  break;
114  case WTerm::DENOMINATOR:
115  _purePhaseSpaceVerticesNames.denominator.insert(_purePhaseSpaceVerticesNames.denominator.end(),
116  vertices.begin(), vertices.end());
117  break;
118  case WTerm::COMMON:
119  _purePhaseSpaceVerticesNames.numerator.insert(_purePhaseSpaceVerticesNames.numerator.end(),
120  vertices.begin(), vertices.end());
121  _purePhaseSpaceVerticesNames.denominator.insert(_purePhaseSpaceVerticesNames.denominator.end(),
122  vertices.begin(), vertices.end());
123  break;
124  }
125  }
126 
127  void PurePhaseSpaceDefs::addPurePhaseSpaceVertex(const string& decay, WTerm what) {
128  switch(what) {
129  case WTerm::NUMERATOR:
130  _purePhaseSpaceVerticesNames.numerator.push_back(decay);
131  break;
132  case WTerm::DENOMINATOR:
133  _purePhaseSpaceVerticesNames.denominator.push_back(decay);
134  break;
135  case WTerm::COMMON:
136  _purePhaseSpaceVerticesNames.numerator.push_back(decay);
137  _purePhaseSpaceVerticesNames.denominator.push_back(decay);
138  break;
139  }
140  }
141 
142  NumDenPair<set<HashId>> PurePhaseSpaceDefs::purePhaseSpaceVertices() const {
143  NumDenPair<set<HashId>> result{{},{}};
144  PID& pdg = PID::instance();
145  for (auto elem : _purePhaseSpaceVerticesNames.numerator) {
146  auto temp = pdg.expandToValidVertexUIDs(elem);
147  result.numerator.insert(temp.begin(),temp.end());
148  }
149  for (auto elem : _purePhaseSpaceVerticesNames.denominator) {
150  auto temp = pdg.expandToValidVertexUIDs(elem);
151  result.denominator.insert(temp.begin(),temp.end());
152  }
153  return result;
154  }
155 
156  NumDenPair<bool> PurePhaseSpaceDefs::isPurePhaseSpace(PdgId parent, const std::vector<PdgId>& daughters) const {
157  auto tmp = combineDaughters(daughters, {});
158  HashId id = processID(parent, tmp);
159  auto itNum = _purePhaseSpaceVertices.numerator.find(id);
160  auto itDen = _purePhaseSpaceVertices.denominator.find(id);
161  bool foundnum = itNum != _purePhaseSpaceVertices.numerator.end();
162  bool foundden = itDen != _purePhaseSpaceVertices.denominator.end();
163  return NumDenPair<bool>{foundnum, foundden};
164  }
165 
166  void PurePhaseSpaceDefs::init() {
167  _purePhaseSpaceVertices = purePhaseSpaceVertices();
168  }
169 
170  YAML::Emitter& operator<<(YAML::Emitter& out, const PurePhaseSpaceDefs& s) {
171  out << YAML::convert<PurePhaseSpaceDefs>::encode(s);
172  return out;
173  }
174 
175 } // namespace Hammer
176 
177 namespace YAML {
178 
179  Node convert<::Hammer::PurePhaseSpaceDefs>::encode(const ::Hammer::PurePhaseSpaceDefs& value) {
180  Node node;
181  if (value._purePhaseSpaceVerticesNames.numerator.size() > 0) {
182  Node tmpNode;
183  for (auto& elem : value._purePhaseSpaceVerticesNames.numerator) {
184  tmpNode.push_back(elem);
185  }
186  node["Numerator"] = tmpNode;
187  }
188  if (value._purePhaseSpaceVerticesNames.denominator.size() > 0) {
189  Node tmpNode;
190  for (auto& elem : value._purePhaseSpaceVerticesNames.denominator) {
191  tmpNode.push_back(elem);
192  }
193  node["Denominator"] = tmpNode;
194  }
195  return node;
196  }
197 
198  bool convert<::Hammer::PurePhaseSpaceDefs>::decode(const Node& node, ::Hammer::PurePhaseSpaceDefs& value) {
199  if (node.IsMap()) {
200  for (auto entry2 : node) {
201  string what = entry2.first.as<string>();
202  if (what == "Numerator") {
204  YAML::Node processes = entry2.second;
205  if(processes.IsSequence()) {
206  for (auto process : processes) {
207  value._purePhaseSpaceVerticesNames.numerator.push_back(process.as<string>());
208  }
209  } else {
210  return false;
211  }
212  } else if (what == "Denominator") {
214  YAML::Node processes = entry2.second;
215  if(processes.IsSequence()) {
216  for (auto process : processes) {
217  value._purePhaseSpaceVerticesNames.denominator.push_back(process.as<string>());
218  }
219  } else {
220  return false;
221  }
222  } else {
223  return false;
224  }
225  }
226  } else {
227  return false;
228  }
229  return true;
230  }
231 }
PDG codes to UID functions.
Hammer settings manager class.
TensorData read(const Serial::FBTensor *msgreader)
Definition: Operations.cc:76
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
std::vector< PdgId > combineDaughters(const std::vector< PdgId > &daughters, const std::vector< PdgId > &subDaughters)
combine list of codes of daughters and grandaughters (for processes which parameterise two subsequent...
std::vector< HashId > expandToValidVertexUIDs(const std::string &name, const bool &hadOnly=false) const
Definition: Pdg.cc:833
#define ASSERT(x)
Definition: Exceptions.hh:95
NumDenPair< std::vector< std::string > > _purePhaseSpaceVerticesNames
Hammer exception definitions.
Logging class.
Definition: Logging.hh:33
Hammer class for dealing with particle data.
Definition: Pdg.hh:32
Container class for pure phase space vertices definitions.
Hammer particle data class.
HashId processID(PdgId parent, const std::vector< PdgId > &allDaughters)
compute a unique ID for a given process based on the PDG codes of the parent particle and the ordered...
size_t HashId
Definition: IndexTypes.hh:31
int PdgId
Definition: Pdg.fhh:17
Serialization related typedefs and includes.