Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProvidersRepo.cc
Go to the documentation of this file.
1 ///
2 /// @file ProvidersRepo.cc
3 /// @brief Interface class for amplitudes, rates, FFs dictionary container
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 
16 #include "Hammer/ProvidersRepo.hh"
19 #include "Hammer/AmplitudeBase.hh"
21 #include "Hammer/FormFactorBase.hh"
23 #include "Hammer/Tools/Logging.hh"
24 #include "Hammer/Tools/Utils.hh"
25 #include "Hammer/Math/Histogram.hh"
27 #include "Hammer/Tools/Pdg.hh"
28 
29 using namespace std;
30 
31 namespace Hammer {
32 
33  namespace MD = MultiDimensional;
34 
35  ProvidersRepo::ProvidersRepo(const SchemeDefinitions* schemeDefs) : _schemeDefs{schemeDefs} {
36  ADD_AMPLITUDES(_amplitudes);
37  ADD_FORMFACTORS(_formFactors);
38  ADD_RATES(_rates);
39  PID& pdg = PID::instance();
40  _partialWidths = pdg.getPartialWidths();
41  }
42 
44  _amplitudes.clear();
45  _rates.clear();
46  _formFactors.clear();
47  _partialWidths.clear();
49  _WCProviders.clear();
50  _FFErrProviders.clear();
51  _schemeDefs = nullptr;
52  }
53 
55  }
56 
59  for (auto& elem : _amplitudes) {
60  elem.second->setSettingsHandler(sh);
61  }
62  for (auto& elem : _rates) {
63  elem.second->setSettingsHandler(sh);
64  }
65  for (auto& elem : _formFactors) {
66  for (auto& elem2 : elem.second) {
67  elem2->setSettingsHandler(sh);
68  }
69  }
70  }
71 
73  initializeWCs();
75  }
76 
78  for (auto& elem : _amplitudes) {
79  auto info = elem.second->getWCInfo();
80  if (info.second != IndexLabel::NONE && info.first != "") {
81  if (_WCProviders.find(info.first) == _WCProviders.end()) {
82  _WCProviders.insert({info.first, {info.second, elem.second.get()}});
83  }
84  }
85  }
86  }
87 
89  for(auto& elem: _formFactors) {
90  for(auto& elem2: elem.second) {
91  auto info = elem2->getFFErrInfo();
92  if(info.second != IndexLabel::NONE && info.first.get() != "") {
93  if (_FFErrProviders.find(info.first) == _FFErrProviders.end()) {
94  _FFErrProviders.insert({info.first, {info.second, elem2.get()}});
95  }
96  }
97  }
98  }
99  }
100 
101  AmplitudeBase* ProvidersRepo::getAmplitude(PdgId parent, const vector<PdgId>& daughters,
102  const vector<PdgId>& granddaughters) const {
103  auto tmp = combineDaughters(daughters, granddaughters);
104  HashId id = processID(parent, tmp);
105  auto it = _amplitudes.find(id);
106  if (it != _amplitudes.end()) {
107  return &(*it->second);
108  }
109  tmp = combineDaughters(flipSigns(daughters), flipSigns(granddaughters));
110  id = processID(flipSign(parent), tmp);
111  it = _amplitudes.find(id);
112  if (it != _amplitudes.end()) {
113  return &(*it->second);
114  }
115  return nullptr;
116  }
117 
118  RateBase* ProvidersRepo::getRate(PdgId parent, const vector<PdgId>& daughters, const vector<PdgId>& granddaughters) const {
119  auto tmp = combineDaughters(daughters, granddaughters);
120  HashId id = processID(parent, tmp);
121  auto it = _rates.find(id);
122  if (it != _rates.end()) {
123  return &(*it->second);
124  }
125  tmp = combineDaughters(flipSigns(daughters), flipSigns(granddaughters));
126  id = processID(flipSign(parent), tmp);
127  it = _rates.find(id);
128  if (it != _rates.end()) {
129  return &(*it->second);
130  }
131  return nullptr;
132  }
133 
134  const double* ProvidersRepo::getPartialWidth(PdgId parent, const vector<PdgId>& daughters, const vector<PdgId>& granddaughters) const {
135  auto tmp = combineDaughters(daughters, granddaughters);
136  HashId id = processID(parent, tmp);
137  auto it = _partialWidths.find(id);
138  if (it != _partialWidths.end()) {
139  return &(it->second);
140  }
141  tmp = combineDaughters(flipSigns(daughters), flipSigns(granddaughters));
142  id = processID(flipSign(parent), tmp);
143  it = _partialWidths.find(id);
144  if (it != _partialWidths.end()) {
145  return &(it->second);
146  }
147  return nullptr;
148  }
149 
150  vector<FormFactorBase*> ProvidersRepo::getFormFactor(HashId processId) const {
151  vector<FormFactorBase*> res{};
152  auto it = _formFactors.find(processId);
153  if (it != _formFactors.end()) {
154  for (auto& elem : it->second) {
155  res.push_back(&(*elem));
156  }
157  }
158  return res;
159  }
160 
161  // vector<FormFactorBase*> ProvidersRepo::getFormFactor(PdgId parent, const vector<PdgId>& daughters, const vector<PdgId>& granddaughters) const {
162  // AmplitudeBase* amp = getAmplitude(parent, daughters, granddaughters);
163  // if(amp != nullptr) {
164  // HashId ffId = amp->hadronicId();
165  // return getFormFactor(ffId);
166  // }
167  // else {
168  // return vector<FormFactorBase*>{};
169  // }
170  // }
171 
172  map<HashId, vector<string>> ProvidersRepo::getFFGroups() const {
173  map<HashId, vector<string>> FFAssocs;
174  for (auto& elem : _formFactors) {
175  auto res = FFAssocs.insert({elem.first, vector<string>{}});
176  if (res.second) {
177  for (auto& elem2 : elem.second) {
178  res.first->second.push_back(elem2->group());
179  }
180  }
181  }
182  return FFAssocs;
183  }
184 
186  // this one is required by SchemeDefs init
187  auto formFactDups = _schemeDefs->getFFDuplicates();
188  SettingsHandler* settings = getSettingsHandler();
189  if(settings != nullptr) {
190  fixWCValues();
191  for (auto& elem : _amplitudes) {
192  elem.second->init();
193  }
194  for (auto& elem : _rates) {
195  elem.second->init();
196  }
197  for (auto& elem : _formFactors) {
198  auto itDups = formFactDups.find(elem.first);
199  size_t initsize = elem.second.size();
200  for(size_t i=0; i < initsize; ++i) {
201  if (itDups != formFactDups.end()) {
202  auto itList = itDups->second.find(elem.second[i]->group());
203  if(itList != itDups->second.end()) {
204  for(auto& token: itList->second) {
205  duplicateFormFactor(elem.first,i,token);
206  }
207  }
208  }
209  }
210  for(auto& elem2 : elem.second){
211  elem2->init();
212  }
213  }
214  initializeFFErrs(); // so that also the duplicates are initialized
215  }
216  }
217 
219  // this one needs SchemeDefs to be initialized
221  auto oldlevel = Log::getLog("Hammer.SchemeDefinitions").getLevel();
222  Log::setLevel("Hammer.SchemeDefinitions",Log::CRITICAL);
223  for(auto& elem: _formFactors) {
224  auto schemes = _schemeDefs->getFFSchemesForProcess(elem.first);
225  for(auto elem2: schemes) {
226  auto prefgroup = elem.second[elem2.second]->getFFErrPrefixGroup();
227  _schemeNamesFromPrefixGroup[prefgroup].insert(elem2.first);
228  }
229  }
230  for(auto& elem: _formFactors) {
231  for(auto& elem2: elem.second) {
232  elem2->calcUnits();
233  }
234  }
235  Log::setLevel("Hammer.SchemeDefinitions",oldlevel);
236  }
237 
238  FFIndex ProvidersRepo::duplicateFormFactor(HashId processId, FFIndex index, const string& newLabel) {
239  auto it = _formFactors.find(processId);
240  if(it != _formFactors.end()) {
241  if(index < it->second.size()) {
242  FFIndex newpos = it->second.size();
243  it->second.push_back(unique_ptr<FormFactorBase>(it->second[index]->clone(newLabel)));
244  return newpos;
245  }
246  }
247  return 0;
248  }
249 
251  return Log::getLog("Hammer.ProvidersRepo");
252  }
253 
255  SettingsHandler* settings = getSettingsHandler();
256  if(settings != nullptr) {
257  for(auto& elem: _amplitudes) {
258  string prefix = elem.second->getWCInfo().first;
259  auto coeffs = settings->getSettings(prefix);
260  auto coeffsNum = settings->getSettings(prefix, WTerm::NUMERATOR);
261  auto coeffsDen = settings->getSettings(prefix, WTerm::DENOMINATOR);
262  if(coeffs.size() > coeffsNum.size()) {
263  auto it1 = coeffs.begin();
264  auto it2 = coeffsNum.begin();
265  while (it1 != coeffs.end() && it2 != coeffsNum.end()) {
266  if(*it1<*it2) {
267  settings->cloneSetting(prefix, *it1, *settings->getEntry(prefix, *it1, WTerm::COMMON), WTerm::NUMERATOR);
268  settings->resetSetting(prefix, *it1, WTerm::NUMERATOR);
269  ++it1;
270  }
271  else if(*it2<*it1) {
272  ++it2;
273  }
274  else {
275  ++it1;
276  ++it2;
277  }
278  }
279  }
280  if(coeffs.size() > coeffsDen.size()) {
281  auto it1 = coeffs.begin();
282  auto it2 = coeffsDen.begin();
283  while (it1 != coeffs.end() && it2 != coeffsDen.end()) {
284  if(*it1<*it2) {
285  settings->cloneSetting(prefix, *it1, *settings->getEntry(prefix, *it1, WTerm::COMMON), WTerm::DENOMINATOR);
286  settings->resetSetting(prefix, *it1, WTerm::DENOMINATOR);
287  ++it1;
288  }
289  else if(*it2<*it1) {
290  ++it2;
291  }
292  else {
293  ++it1;
294  ++it2;
295  }
296  }
297  }
298  }
299  }
300  }
301 
302  const std::set<std::string> ProvidersRepo::schemeNamesFromPrefixAndGroup(const FFPrefixGroup& value) const {
303  return getOrDefault(_schemeNamesFromPrefixGroup, value, {});
304  }
305 
307  for(auto& elem: _formFactors) {
308  for(auto& elem2: elem.second) {
309  if (elem2->getFFErrPrefixGroup() == value) {
310  return true;
311  }
312  }
313  }
314  return false;
315  }
316 
317  AmplitudeBase* ProvidersRepo::getWCProvider(const string& process) const {
318  return getOrDefault(_WCProviders, process, {IndexLabel::NONE,nullptr}).second;
319  }
320 
321  IndexLabel ProvidersRepo::getWCLabel(const string& process) const {
322  return getOrDefault(_WCProviders, process, {IndexLabel::NONE, nullptr}).first;
323  }
324 
325  map<IndexLabel, AmplitudeBase*> ProvidersRepo::getAllWCProviders() const {
326  map<IndexLabel, AmplitudeBase*> results;
327  for(auto& elem: _WCProviders) {
328  results.insert(elem.second);
329  }
330  return results;
331  }
332 
334  return getOrDefault(_FFErrProviders, process, {IndexLabel::NONE, nullptr}).second;
335  }
336 
338  return getOrDefault(_FFErrProviders, process, {IndexLabel::NONE, nullptr}).first;
339  }
340 
341  std::map<IndexLabel, SchemeDict<FormFactorBase*>>
343  map<IndexLabel, SchemeDict<FormFactorBase*>> results;
344  for (auto& elem : _FFErrProviders) {
345  auto schemeNames = schemeNamesFromPrefixAndGroup(elem.first);
346  for(auto& elem2: schemeNames) {
347  results[elem.second.first][elem2] = elem.second.second;
348  }
349  }
350  return results;
351  }
352 
353 
354 } // namespace Hammer
PDG codes to UID functions.
std::map< IndexLabel, AmplitudeBase * > getAllWCProviders() const
Hammer base form factor class.
Base class for form factors.
WCPrefixDict< std::pair< IndexLabel, AmplitudeBase * > > _WCProviders
Base class for amplitudes.
virtual SchemeDict< FFIndex > getFFSchemesForProcess(HadronicUID id) const
bool checkFFPrefixAndGroup(const FFPrefixGroup &value) const
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...
size_t FFIndex
Definition: IndexTypes.hh:69
std::map< IndexLabel, SchemeDict< FormFactorBase * > > getAllFFErrProviders() const
VertexIdDict< double > _partialWidths
Known partial widths.
FFIndex duplicateFormFactor(HadronicUID processId, FFIndex baseIndex, const std::string &newLabel)
Clones a FF parametrization.
Setting * cloneSetting(const std::string &path, const std::string &name, const Setting &value, WTerm group=WTerm::COMMON)
AmplitudeIdDict< std::unique_ptr< AmplitudeBase > > _amplitudes
Known amplitudes.
Setting * resetSetting(const std::string &path, const std::string &name, WTerm group=WTerm::COMMON)
HadronicIdDict< std::vector< std::string > > getFFGroups() const
FFPrefixGroupDict< std::set< std::string > > _schemeNamesFromPrefixGroup
Interface class for amplitudes, rates, FFs dictionary container.
const SchemeDefinitions * _schemeDefs
Message logging routines.
Hammer base amplitude class.
#define ADD_FORMFACTORS(CONT)
IndexLabel getWCLabel(const std::string &wcPrefix) const
virtual std::vector< FormFactorBase * > getFormFactor(HadronicUID processId) const
virtual AmplitudeBase * getAmplitude(PdgId parent, const std::vector< PdgId > &daughters, const std::vector< PdgId > &granddaughters={}) const
static Log & getLog(const std::string &name)
Get a logger with the given name.
Definition: Logging.cc:139
std::vector< PdgId > flipSigns(const std::vector< PdgId > &list)
return the PDG codes of the conjugate particles (itself if self-conjugate) for all the PDG codes in a...
ValueType getOrDefault(const std::map< KeyType, ValueType > &data, KeyType key, ValueType fallback)
Definition: Tools/Utils.hh:39
virtual ~ProvidersRepo() noexcept
virtual RateBase * getRate(PdgId parent, const std::vector< PdgId > &daughters, const std::vector< PdgId > &granddaughters={}) const
Obtains a rate class.
virtual void setSettingsHandler(SettingsHandler &sh)
set link to settings repository handler.
virtual void setSettingsHandler(SettingsHandler &sh)
set link to settings repository handler.
Hammer histogram class.
Log & getLog() const
Setting * getEntry(const std::string &path, const std::string &name, WTerm group)
virtual const double * getPartialWidth(PdgId parent, const std::vector< PdgId > &daughters, const std::vector< PdgId > &granddaughters={}) const
Obtains a partial width.
#define ADD_RATES(CONT)
PdgId flipSign(const PdgId &id)
return the PDG code of the conjugate particle (itself if self-conjugate)
IndexLabel getFFErrLabel(const FFPrefixGroup &process) const
Order-0 tensor data container.
Logging class.
Definition: Logging.hh:33
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
Hammer class for dealing with particle data.
Definition: Pdg.hh:32
static PID & instance()
Definition: Pdg.cc:283
Hammer particle data class.
SettingsHandler * getSettingsHandler() const
provide the pointer to the repository handler
Container class for Scheme Definitions.
Hammer settings manager class.
HadronicIdDict< NewLabelsByGroup > getFFDuplicates() const
FormFactorBase * getFFErrProvider(const FFPrefixGroup &process) const
HadronicIdDict< std::vector< std::unique_ptr< FormFactorBase > > > _formFactors
Known formfactors.
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...
AmplitudeBase * getWCProvider(const std::string &wcPrefix) const
size_t HashId
Definition: IndexTypes.hh:31
#define ADD_AMPLITUDES(CONT)
virtual void defineSettings()
purely virtual function for a class to define new settings
VertexIdDict< std::unique_ptr< RateBase > > _rates
Known rates.
Hammer available modules header.
int getLevel() const
Get the priority level of this logger.
Definition: Logging.hh:312
int PdgId
Definition: Pdg.fhh:17
std::set< std::string > getSettings(const std::string &path="", WTerm group=WTerm::COMMON) const
access the list of registered settings
Serialization related typedefs and includes.
Hammer settings manager class.
Base class for rates.
Definition: RateBase.hh:39
const std::set< std::string > schemeNamesFromPrefixAndGroup(const FFPrefixGroup &value) const
std::map< HashId, double > getPartialWidths()
get partial Widths
Definition: Pdg.cc:292
FFPrefixGroupDict< std::pair< IndexLabel, FormFactorBase * > > _FFErrProviders
static void setLevel(const std::string &name, int level)
Set the log levels.
Definition: Logging.cc:105