Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProcRates.cc
Go to the documentation of this file.
1 ///
2 /// @file ProcRates.cc
3 /// @brief Container class for process rate tensors
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 <algorithm>
14 #include <boost/algorithm/string.hpp>
15 
17 #include "Hammer/ProcRates.hh"
18 #include "Hammer/ExternalData.hh"
20 #include "Hammer/Tools/Logging.hh"
21 #include "Hammer/Tools/Utils.hh"
23 
24 using namespace std;
25 
26 namespace Hammer {
27 
28  namespace MD = MultiDimensional;
29 
30  ProcRates::ProcRates(const ExternalData* ext) : _external{ext} {
31  }
32 
33  ProcRates::~ProcRates() noexcept {
34  _processRates.clear();
35  _external = nullptr;
36  }
37 
39  }
40 
41  void ProcRates::init() {
42  _processRates.clear();
43  }
44 
46  auto it = _processRates.find(id);
47  if(it == _processRates.end()) {
48  auto res = _processRates.insert({id, SchemeDict<Tensor>{}});
49  if(res.second) {
50  return &(res.first->second);
51  }
52  else {
53  return nullptr;
54  }
55  }
56  return &(it->second);
57  }
58 
59  double ProcRates::getVertexRate(const HashId& id, const string& scheme) const {
60  auto it = _processRates.find(id);
61  if (it != _processRates.end()){
62  auto it2 = it->second.find(scheme);
63  if(it2 != it->second.end()){
64  if(it2->second.labels().size() > 0) {
65  auto t = _external->getExternalVectors(scheme, it2->second.labels());
66  t.dot(it2->second);
67  if(t.rank() != 0) {
68  MSG_ERROR("Invalid rank at end of evaluation");
69  }
70  return t.element().real();
71  }
72  else {
73  if(it2->second.rank() != 0) {
74  MSG_ERROR("Invalid rank at end of evaluation");
75  }
76  return it2->second.element().real();
77  }
78  }
79  MSG_ERROR("Rate not found for specified FF scheme. Oh, the humanity.");
80  return 0.;
81  }
82  MSG_ERROR("Rate not found for specified vertex ID. Oh, the humanity.");
83  return 0.;
84  }
85 
87  return Log::getLog("Hammer.ProcRates");
88  }
89 
90  bool ProcRates::read(const Serial::FBRates* msgreader, bool merge) {
91  auto rates = msgreader->rates();
92  if(!merge) {
93  _processRates.clear();
94  }
95  bool result = true;
96  for(unsigned int i = 0; i< rates->size() && result; ++i) {
97  auto res = _processRates.insert({rates->Get(i)->id(), SchemeDict<Tensor>{}});
98  if(merge && !res.second) {
99  res.first = _processRates.find(rates->Get(i)->id());
100  if(res.first == _processRates.end()) {
101  return false;
102  }
103  }
104  auto vals = rates->Get(i)->ratevals();
105  auto chs = rates->Get(i)->ratenames();
106  for(unsigned int j = 0; j < vals->size() && result; ++j) {
107  auto it = res.first->second.find(chs->Get(j)->c_str());
108  if(merge && it != res.first->second.end() && strcmp(chs->Get(j)->c_str(), "Denominator") != 0) {
109  MSG_ERROR("Try to merge two rates with factor schemes with same name '" +
110  string(chs->Get(j)->c_str()) + "'!");
111  result = false;
112 
113  }
114  else {
115  auto res2 = res.first->second.insert({chs->Get(j)->c_str(), Tensor{}});
116  res2.first->second.read(vals->Get(j));
117  }
118  }
119  }
120  return result;
121  }
122 
123  void ProcRates::write(flatbuffers::FlatBufferBuilder* msgwriter) const {
124  vector<flatbuffers::Offset<Serial::FBRate>> rates;
125  rates.reserve(_processRates.size());
126  for (auto& elem : _processRates) {
127  vector<flatbuffers::Offset<flatbuffers::String>> chs;
128  vector<flatbuffers::Offset<Serial::FBTensor>> rts;
129  for(auto& elem2: elem.second) {
130  auto serCh = msgwriter->CreateString(elem2.first);
132  elem2.second.write(&(*msgwriter),&val);
133  chs.push_back(serCh);
134  rts.push_back(val);
135  }
136  auto serialChs = msgwriter->CreateVector(chs);
137  auto serialrts = msgwriter->CreateVector(rts);
138  Serial::FBRateBuilder serialFF{*msgwriter};
139  serialFF.add_id(elem.first);
140  serialFF.add_ratevals(serialrts);
141  serialFF.add_ratenames(serialChs);
142  auto resFF = serialFF.Finish();
143  rates.push_back(resFF);
144  }
145  auto serialrates = msgwriter->CreateVector(rates);
146  Serial::FBRatesBuilder serialout{*msgwriter};
147  serialout.add_rates(serialrates);
148  auto headoffset = serialout.Finish();
149  msgwriter->Finish(headoffset);
150  }
151 
152 
153 } // namespace Hammer
PDG codes to UID functions.
virtual void defineSettings()
purely virtual function for a class to define new settings
Definition: ProcRates.cc:38
Container class for values of WC and FF vectors.
virtual double getVertexRate(const ProcessUID &id, const std::string &schemeName) const
Definition: ProcRates.cc:59
~ProcRates() noexcept
Definition: ProcRates.cc:33
Message logging routines.
Log & getLog() const
logging facility
Definition: ProcRates.cc:86
const ExternalData * _external
Definition: ProcRates.hh:85
bool read(const Serial::FBRates *msgreader, bool merge)
Definition: ProcRates.cc:90
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
Order-0 tensor data container.
Logging class.
Definition: Logging.hh:33
virtual const Tensor & getExternalVectors(std::string schemeName, LabelsList labels) const
Definition: ExternalData.cc:57
Multidimensional tensor class with complex numbers as elements.
Definition: Tensor.hh:33
ProcIdDict< SchemeDict< Tensor > > _processRates
Definition: ProcRates.hh:84
void write(flatbuffers::FlatBufferBuilder *msgwriter) const
Definition: ProcRates.cc:123
std::map< SchemeName, T > SchemeDict
Definition: IndexTypes.hh:64
Tensor & dot(const Tensor &other, const UniqueLabelsList &indices={})
contract this tensor with another and stores the result in this tensor
Definition: Tensor.cc:114
#define MSG_ERROR(x)
Definition: Logging.hh:367
size_t HashId
Definition: IndexTypes.hh:31
Container class for process rate tensors.
virtual SchemeDict< Tensor > * getProcessRates(ProcessUID id)
Definition: ProcRates.cc:45
Serialization related typedefs and includes.