Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DictionaryManager.cc
Go to the documentation of this file.
1 ///
2 /// @file DictionaryManager.cc
3 /// @brief Global container class for amplitudes, rates, FFs, data
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 <iostream>
13 #include <fstream>
14 #include <algorithm>
15 #include <boost/algorithm/string.hpp>
16 
17 #include "Hammer/Math/Tensor.hh"
21 #include "Hammer/ProvidersRepo.hh"
22 #include "Hammer/ExternalData.hh"
23 #include "Hammer/ProcRates.hh"
26 #include "Hammer/AmplitudeBase.hh"
28 #include "Hammer/FormFactorBase.hh"
30 #include "Hammer/Tools/Logging.hh"
31 #include "Hammer/Tools/Utils.hh"
32 #include "Hammer/Math/Histogram.hh"
34 
35 #include "yaml-cpp/yaml.h"
36 
37 using namespace std;
38 
39 namespace Hammer {
40 
41  namespace MD = MultiDimensional;
42 
43  DictionaryManager::DictionaryManager()
44  : _schemeDefs{new SchemeDefinitions{}},
45  _providers{new ProvidersRepo{_schemeDefs.get()}},
46  _external{new ExternalData{_providers.get()}},
47  _rates{new ProcRates{_external.get()}},
48  _purePSDefs{new PurePhaseSpaceDefs{}},
49  _procDefs{new ProcessDefinitions{}} {
50  }
51 
54  if(_rates) {
55  _rates->setSettingsHandler(sh);
56  }
57  if (_providers) {
58  _providers->setSettingsHandler(sh);
59  }
60  if (_external) {
61  _external->setSettingsHandler(sh);
62  }
63  }
64 
66  }
67 
69  return *_providers;
70  }
71 
73  return *_external;
74  }
75 
77  return *_external;
78  }
79 
81  return *_rates;
82  }
83 
85  return *_rates;
86  }
87 
89  return *_procDefs;
90  }
91 
93  return *_procDefs;
94  }
95 
97  return *_purePSDefs;
98  }
99 
101  return *_purePSDefs;
102  }
103 
105  return *_schemeDefs;
106  }
107 
109  return *_schemeDefs;
110  }
111 
112 
114  }
115 
117  // initSettings();
118  _providers->initialize();
119  _rates->init();
120  _purePSDefs->init();
121  _procDefs->init();
122  // do not change the order
123  _providers->initPreSchemeDefs();
124  _schemeDefs->init(_providers->getFFGroups());
125  _providers->initPostSchemeDefs();
126  auto tmp = _schemeDefs->getFFSchemeNames();
127  _external->init(_schemeDefs->getFFSchemeNames());
128  }
129 
131  return Log::getLog("Hammer.DictionaryManager");
132  }
133 
134  void DictionaryManager::write(flatbuffers::FlatBufferBuilder* msgwriter) const {
135  vector<flatbuffers::Offset<Serial::FBFFScheme>> schemes;
136  vector<flatbuffers::Offset<Serial::FBPurePS>> pureps;
137  vector<flatbuffers::Offset<Serial::FBSetting>> settings;
139  getSettingsHandler()->write(msgwriter, &settings);
140  _schemeDefs->write(msgwriter, &schemes);
141  _purePSDefs->write(msgwriter, &pureps);
142  _procDefs->write(msgwriter, &procs);
143  auto serialschemes = msgwriter->CreateVector(schemes);
144  auto serialps = msgwriter->CreateVector(pureps);
145  auto serialsettings = msgwriter->CreateVector(settings);
146  Serial::FBHeaderBuilder serialheader{*msgwriter};
147  serialheader.add_ffschemes(serialschemes);
148  serialheader.add_settings(serialsettings);
149  serialheader.add_pureps(serialps);
150  serialheader.add_processdefs(procs);
151  auto headoffset = serialheader.Finish();
152  msgwriter->Finish(headoffset);
153  }
154 
155  bool DictionaryManager::read(const Serial::FBHeader* msgreader, bool merge) {
156  bool result = true;
157  result &= _procDefs->read(msgreader->processdefs(), merge);
158  result &= _purePSDefs->read(msgreader, merge);
159  result &= _schemeDefs->read(msgreader, merge);
160  if(result) {
161  _providers->initPostSchemeDefs();
162  _external->init(_schemeDefs->getFFSchemeNames());
163  }
164  return result;
165  }
166 
167  void DictionaryManager::parseDecays(const string& data) {
168  YAML::Node config = YAML::Load(data);
169  processDecays(config);
170  }
171 
172  void DictionaryManager::readDecays(const string& name) {
173  YAML::Node config = YAML::LoadFile(name);
174  processDecays(config);
175  }
176 
177  void DictionaryManager::processDecays(const YAML::Node& config) {
178  if (config.IsMap()) {
179  if (config["Processes"]) {
180  try {
181  *_procDefs = config["Processes"].as<ProcessDefinitions>();
182  } catch (YAML::Exception&) {
183  MSG_ERROR("Problem parsing process input. Beware missing spaces after inline ':'.");
184  }
185  }
186  if (config["FormFactors"]) {
187  try {
188  *_schemeDefs = config["FormFactors"].as<SchemeDefinitions>();
189  } catch (YAML::Exception&) {
190  MSG_ERROR("Problem parsing form factor input. Beware missing spaces after inline ':'.");
191  }
192  }
193  if (config["PurePSVertices"]) {
194  try {
195  *_purePSDefs = config["PurePSVertices"].as<PurePhaseSpaceDefs>();
196  } catch (YAML::Exception&) {
197  MSG_ERROR("Problem parsing pure PS input. Beware missing spaces after inline ':'.");
198  }
199  }
200  } else {
201  MSG_ERROR("Problem parsing input. Beware missing spaces after inline ':'.");
202  }
203  }
204 
205  void DictionaryManager::saveDecays(const string& name) {
206  ofstream file;
207  YAML::Emitter emitter(file);
208  file.open(name.c_str());
209  if (file.is_open()) {
210  emitter << YAML::Comment(version() + string(" header card")) << YAML::Newline;
211  emitter << YAML::BeginMap;
212  emitter << YAML::Key << "Processes";
213  emitter << YAML::Value << *_procDefs;
214  emitter << YAML::Key << "FormFactors";
215  emitter << YAML::Value << *_schemeDefs;
216  auto purePSNode = YAML::convert<PurePhaseSpaceDefs>::encode(*_purePSDefs);
217  if(!purePSNode.IsNull()){
218  emitter << YAML::Key << "PurePSVertices";
219  emitter << YAML::Value << purePSNode;
220  }
221  emitter << YAML::EndMap;
222  emitter << YAML::Newline;
223  file.close();
224  }
225  }
226 
227 } // namespace Hammer
virtual const PurePhaseSpaceDefs & purePSDefs() const
std::string version()
Definition: Tools/Utils.hh:34
PDG codes to UID functions.
Decay process class.
Definition: ProcRates.hh:34
std::unique_ptr< ProcRates > _rates
void readDecays(const std::string &fileName)
read Hammer settings from a file
Hammer base form factor class.
Hammer settings manager class.
std::unique_ptr< PurePhaseSpaceDefs > _purePSDefs
Container class for values of WC and FF vectors.
void write(flatbuffers::FlatBufferBuilder *msgwriter, std::vector< flatbuffers::Offset< Serial::FBSetting >> *msgs) const
Container class for storing included/forbidden process info.
Interface class for amplitudes, rates, FFs dictionary container.
void processDecays(const YAML::Node &input)
Message logging routines.
bool read(const Serial::FBHeader *msgreader, bool merge)
Hammer base amplitude class.
virtual const ProvidersRepo & providers() const
virtual void defineSettings()
purely virtual function for a class to define new settings
std::unique_ptr< ExternalData > _external
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
virtual void setSettingsHandler(SettingsHandler &sh)
set link to settings repository handler.
Hammer histogram class.
Order-0 tensor data container.
Logging class.
Definition: Logging.hh:33
virtual const ExternalData & externalData() const
void saveDecays(const std::string &fileName)
write current Hammer settings to a file
Hammer settings manager class.
Container class for pure phase space vertices definitions.
void write(flatbuffers::FlatBufferBuilder *msgwriter) const
virtual const ProcRates & rates() const
SettingsHandler * getSettingsHandler() const
provide the pointer to the repository handler
std::unique_ptr< ProvidersRepo > _providers
Container class for Scheme Definitions.
Hammer settings manager class.
virtual void setSettingsHandler(SettingsHandler &sh)
set link to settings repository handler.
#define MSG_ERROR(x)
Definition: Logging.hh:367
std::unique_ptr< SchemeDefinitions > _schemeDefs
virtual const SchemeDefinitions & schemeDefs() const
Container class for process rate tensors.
Hammer available modules header.
virtual ~DictionaryManager() noexcept
Hammer tensor class.
Serialization related typedefs and includes.
Hammer settings manager class.
std::unique_ptr< ProcessDefinitions > _procDefs
virtual const ProcessDefinitions & processDefs() const
void parseDecays(const std::string &yamlData)
read Hammer settings from a string
Global container class for amplitudes, rates, FFs, data.