Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExternalData.cc
Go to the documentation of this file.
1 ///
2 /// @file ExternalData.cc
3 /// @brief Container class for values of WC and FF vectors
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/Math/Tensor.hh"
17 #include "Hammer/ProvidersRepo.hh"
18 #include "Hammer/ExternalData.hh"
20 #include "Hammer/AmplitudeBase.hh"
22 #include "Hammer/FormFactorBase.hh"
24 #include "Hammer/Tools/Logging.hh"
25 #include "Hammer/Tools/Utils.hh"
26 #include "Hammer/Math/Histogram.hh"
28 
29 using namespace std;
30 
31 namespace Hammer {
32 
33  namespace MD = MultiDimensional;
34 
35  ExternalData::ExternalData(const ProvidersRepo* provs) : _providers{provs} {
36  _processWilsonCoefficients.reset(new processWilsonCoefficientsType{});
37  _processFFEigenVectors.reset(new processFFEigenVectorsType{});
38  }
39 
42  if (_processWilsonCoefficients.get() != nullptr) {
44  }
45  if (_processFFEigenVectors.get() != nullptr) {
46  _processFFEigenVectors.reset();
47  }
48  if (_externalVectors.get() != nullptr) {
49  _externalVectors.reset();
50  }
51  _schemes.clear();
52  }
53 
55  }
56 
57  const Tensor& ExternalData::getExternalVectors(string schemeName, LabelsList labels) const {
58  // sort(labels.begin(), labels.end(), std::greater<IndexLabel>());
59  if(_externalVectors.get() == nullptr) {
61  }
62  auto& ext = (*_externalVectors)[schemeName];
63  auto ite = ext.find(labels);
64  if (ite != ext.end()) {
65  return ite->second;
66  }
67  if(labels.size() == 0) {
68  auto res = ext.insert({labels, Tensor{"External", MD::makeScalar(1.)}});
69  return res.first->second;
70  }
71  vector<pair<MD::SharedTensorData, bool>> elements;
72  elements.reserve(labels.size());
73  size_t index = (schemeName == "Denominator") ? 1 : 0;
74  for (auto elem : labels) {
75  bool hc = elem < 0;
76  IndexLabel lab = (elem > 0) ? elem : static_cast<IndexLabel>(-elem);
77  if(_processWilsonCoefficients.get() != nullptr) {
78  auto it = _processWilsonCoefficients->find(lab);
79  if (it != _processWilsonCoefficients->end()) {
80  elements.push_back({it->second[index], hc});
81  continue;
82  }
83  }
84  if (_processFFEigenVectors.get() != nullptr) {
85  auto it2 = _processFFEigenVectors->find(lab);
86  if (it2 != _processFFEigenVectors->end()) {
87  auto it3 = it2->second.find(schemeName);
88  if (it3 != it2->second.end()) {
89  elements.push_back({it3->second, hc});
90  continue;
91  }
92  }
93  }
94  }
95  auto res = ext.emplace(labels, Tensor{"External", move(elements)});
96  return res.first->second;
97  }
98 
99  MD::SharedTensorData ExternalData::getWilsonCoefficients(PdgId parent, const std::vector<PdgId>& daughters,
100  const std::vector<PdgId>& granddaughters, WTerm what) const {
101  AmplitudeBase* amp = _providers->getAmplitude(parent, daughters, granddaughters);
102  return getWilsonCoefficients(amp, what);
103  }
104 
106  if(_processWilsonCoefficients.get() != nullptr) {
107  auto it = _processWilsonCoefficients->find(ampl->getWCInfo().second);
108  if (it != _processWilsonCoefficients->end()) {
109  switch (what) {
110  case WTerm::NUMERATOR:
111  return it->second[0];
112  case WTerm::DENOMINATOR:
113  return it->second[1];
114  case WTerm::COMMON:
115  throw Error("Invalid option");
116  }
117  }
118  }
119  return nullptr;
120  }
121 
123  auto it = _processSpecializedWilsonCoefficients.find(ampl->getWCInfo().second);
124  if (it != _processSpecializedWilsonCoefficients.end()) {
125  return it->second;
126  }
127  return nullptr;
128  }
129 
130  void ExternalData::specializeWCInWeights(const string& prefixName, const map<string, complex<double>>& settings){
131  auto ampl = _providers->getWCProvider(prefixName);
132  if (ampl != nullptr) {
133  ampl->updateWCSettings(settings, WTerm::NUMERATOR);
134  auto vec = ampl->getWCVectorFromSettings(WTerm::NUMERATOR);
135  specializeWCInWeights(prefixName, vec);
136  } else {
137  MSG_ERROR("Wilson coefficients " + prefixName + " not found.");
138  }
139  }
140 
141  void ExternalData::specializeWCInWeights(const string& prefixName, const vector<complex<double>>& values){
142  auto ampl = _providers->getWCProvider(prefixName);
143  if (ampl != nullptr) {
144  _isWCSpecializedDict[prefixName] = true;
145  auto label = _providers->getWCLabel(prefixName);
146  auto itSWC = _processSpecializedWilsonCoefficients.find(label);
148  ampl->updateWCSettings(values, WTerm::NUMERATOR);
149  ampl->updateWCTensor(values, itSWC->second);
150  MSG_INFO(prefixName + " Wilson coefficients will be specialized in weights. Histogram specialization will be superceded!");
151  } else {
152  MSG_ERROR("Wilson coefficients " + prefixName + " not found.");
153  }
154 
155  }
156 
157  void ExternalData::resetSpecializeWCInWeights(const string& prefixName){
158  auto ampl = _providers->getWCProvider(prefixName);
159  if (ampl != nullptr) {
160  _isWCSpecializedDict[prefixName] = false;
161  } else {
162  MSG_ERROR("Wilson coefficients " + prefixName + " not found.");
163  }
164  }
165 
167  auto it = _isWCSpecializedDict.find(ampl->getWCInfo().first);
168  if(it != _isWCSpecializedDict.end()){
169  return it->second;
170  }
171  return false;
172  }
173 
174  void ExternalData::setWilsonCoefficients(const string& prefixName, const vector<complex<double>>& values,
175  WTerm what) {
176  auto ampl = _providers->getWCProvider(prefixName);
177  if (ampl != nullptr) {
178  auto label = _providers->getWCLabel(prefixName);
179  ASSERT(_processWilsonCoefficients.get() != nullptr);
180  auto itWC = _processWilsonCoefficients->find(label);
181  ASSERT(itWC != _processWilsonCoefficients->end());
182  ampl->updateWCSettings(values, what);
183  switch(what) {
184  case WTerm::NUMERATOR:
185  ampl->updateWCTensor(values, itWC->second[0]);
186  break;
187  case WTerm::DENOMINATOR:
188  ampl->updateWCTensor(values, itWC->second[1]);
189  break;
190  case WTerm::COMMON:
191  ampl->updateWCTensor(values, itWC->second[0]);
192  ampl->updateWCTensor(values, itWC->second[1]);
193  break;
194  }
195  }
196  }
197 
198  void ExternalData::setWilsonCoefficients(const string& prefixName, const map<string, complex<double>>& values, WTerm what) {
199  auto ampl = _providers->getWCProvider(prefixName);
200  if (ampl != nullptr) {
201  auto label = _providers->getWCLabel(prefixName);
202  ASSERT(_processWilsonCoefficients.get() != nullptr);
203  auto itWC = _processWilsonCoefficients->find(label);
204  ASSERT(itWC != _processWilsonCoefficients->end());
205  ampl->updateWCSettings(values, what);
206  auto vec = ampl->getWCVectorFromSettings(what);
207  switch (what) {
208  case WTerm::NUMERATOR:
209  ampl->updateWCTensor(vec, itWC->second[0]);
210  break;
211  case WTerm::DENOMINATOR:
212  ampl->updateWCTensor(vec, itWC->second[1]);
213  break;
214  case WTerm::COMMON:
215  ampl->updateWCTensor(vec, itWC->second[0]);
216  ampl->updateWCTensor(vec, itWC->second[1]);
217  break;
218  }
219  }
220  }
221 
222  void ExternalData::setWilsonCoefficientsLocal(const std::string& prefixName,
223  const std::vector<std::complex<double>>& values) {
224  if(_processWilsonCoefficients.get() == nullptr) {
226  }
227  auto ampl = _providers->getWCProvider(prefixName);
228  if (ampl != nullptr) {
229  auto label = _providers->getWCLabel(prefixName);
230  ASSERT(_processWilsonCoefficients.get() != nullptr);
231  auto itWC = _processWilsonCoefficients->find(label);
232  ASSERT(itWC != _processWilsonCoefficients->end());
233  ampl->updateWCTensor(values, itWC->second[0]);
234  }
235  }
236 
237  void ExternalData::setWilsonCoefficientsLocal(const std::string& prefixName,
238  const std::map<std::string, std::complex<double>>& values) {
239  if(_processWilsonCoefficients.get() == nullptr) {
241  }
242  auto ampl = _providers->getWCProvider(prefixName);
243  if (ampl != nullptr) {
244  auto label = _providers->getWCLabel(prefixName);
245  ASSERT(_processWilsonCoefficients.get() != nullptr);
246  auto itWC = _processWilsonCoefficients->find(label);
247  ASSERT(itWC != _processWilsonCoefficients->end());
248  auto vec = ampl->getWCVectorFromDict(values);
249  vec[0] = 1.;
250  ampl->updateWCTensor(vec, itWC->second[0]);
251  }
252  }
253 
254  void ExternalData::resetWilsonCoefficients(const string& prefixName, WTerm what) {
255  auto ampl = _providers->getWCProvider(prefixName);
256  if (ampl != nullptr) {
257  auto label = _providers->getWCLabel(prefixName);
258  ASSERT(_processWilsonCoefficients.get() != nullptr);
259  auto itWC = _processWilsonCoefficients->find(label);
260  ASSERT(itWC != _processWilsonCoefficients->end());
261  auto vec = ampl->getWCVectorFromSettings(what);
262  auto n = vec.size();
263  vec.clear();
264  vec.resize(n);
265  vec[0]=1.0;
266  ampl->updateWCSettings(vec, what);
267  switch (what) {
268  case WTerm::NUMERATOR:
269  ampl->updateWCTensor(vec, itWC->second[0]);
270  break;
271  case WTerm::DENOMINATOR:
272  ampl->updateWCTensor(vec, itWC->second[1]);
273  break;
274  case WTerm::COMMON:
275  ampl->updateWCTensor(vec, itWC->second[0]);
276  ampl->updateWCTensor(vec, itWC->second[1]);
277  break;
278  }
279  }
280  }
281 
283  const vector<complex<double>>& values) const {
284  auto ampl = _providers->getWCProvider(prefixName);
286  if (ampl != nullptr) {
287  ampl->updateWCTensor(values, t);
288  }
289  return t;
290  }
291 
293  const map<string, complex<double>>& values) const {
294  auto ampl = _providers->getWCProvider(prefixName);
296  if (ampl != nullptr) {
297  auto vec = ampl->getWCVectorFromDict(values);
298  ampl->updateWCTensor(vec, t);
299  }
300  return t;
301  }
302 
304  ASSERT(_processFFEigenVectors.get() != nullptr);
305  auto it = _processFFEigenVectors->find(ff->getFFErrInfo().second);
306  if (it != _processFFEigenVectors->end()) {
307  auto it2 = it->second.find(schemeName);
308  if(it2 != it->second.end()) {
309  return it2->second;
310  }
311  }
312  return nullptr;
313  }
314 
315 
317  const vector<double>& values) {
318  auto ff = _providers->getFFErrProvider(process);
319  if (ff != nullptr) {
320  auto label = _providers->getFFErrLabel(process);
321  ASSERT(_processFFEigenVectors.get() != nullptr);
322  auto itFFE1 = _processFFEigenVectors->find(label);
323  ASSERT(itFFE1 != _processFFEigenVectors->end());
324  ff->updateFFErrSettings(values);
325  auto schemes = _providers->schemeNamesFromPrefixAndGroup(process);
326  vector<double> tmpvalues;
327  tmpvalues.reserve(values.size() + 1);
328  tmpvalues.push_back(1.);
329  tmpvalues.insert(tmpvalues.end(), values.begin(),values.end());
330  for(auto& elem: schemes) {
331  auto itFFE2 = itFFE1->second.find(elem);
332  if(itFFE2 != itFFE1->second.end()) {
333  ff->updateFFErrTensor(tmpvalues, itFFE2->second);
334  }
335  }
336  }
337  }
338 
339  void ExternalData::setFFEigenVectors(const FFPrefixGroup& process, const map<string, double>& values) {
340  auto ff = _providers->getFFErrProvider(process);
341  if (ff != nullptr) {
342  auto label = _providers->getFFErrLabel(process);
343  ASSERT(_processFFEigenVectors.get() != nullptr);
344  auto itFFE1 = _processFFEigenVectors->find(label);
345  ASSERT(itFFE1 != _processFFEigenVectors->end());
346  ff->updateFFErrSettings(values);
347  auto vec = ff->getErrVectorFromSettings();
348  auto schemes = _providers->schemeNamesFromPrefixAndGroup(process);
349  for (auto& elem : schemes) {
350  auto itFFE2 = itFFE1->second.find(elem);
351  if (itFFE2 != itFFE1->second.end()) {
352  ff->updateFFErrTensor(vec, itFFE2->second);
353  }
354  }
355  }
356  }
357 
358  void ExternalData::setFFEigenVectorsLocal(const FFPrefixGroup& process, const vector<double>& values) {
359  if (_processFFEigenVectors.get() == nullptr) {
361  }
362  auto ff = _providers->getFFErrProvider(process);
363  if (ff != nullptr) {
364  auto label = _providers->getFFErrLabel(process);
365  ASSERT(_processFFEigenVectors.get() != nullptr);
366  auto itFFE1 = _processFFEigenVectors->find(label);
367  ASSERT(itFFE1 != _processFFEigenVectors->end());
368  auto schemes = _providers->schemeNamesFromPrefixAndGroup(process);
369  vector<double> tmpvalues;
370  tmpvalues.reserve(values.size() + 1);
371  tmpvalues.push_back(1.);
372  tmpvalues.insert(tmpvalues.end(), values.begin(), values.end());
373  for (auto& elem : schemes) {
374  auto itFFE2 = itFFE1->second.find(elem);
375  if (itFFE2 != itFFE1->second.end()) {
376  ff->updateFFErrTensor(tmpvalues, itFFE2->second);
377  }
378  }
379  }
380  }
381 
382  void ExternalData::setFFEigenVectorsLocal(const FFPrefixGroup& process, const map<string, double>& values) {
383  if (_processFFEigenVectors.get() == nullptr) {
385  }
386  auto ff = _providers->getFFErrProvider(process);
387  if (ff != nullptr) {
388  auto label = _providers->getFFErrLabel(process);
389  ASSERT(_processFFEigenVectors.get() != nullptr);
390  auto itFFE1 = _processFFEigenVectors->find(label);
391  ASSERT(itFFE1 != _processFFEigenVectors->end());
392  auto vec = ff->getErrVectorFromDict(values);
393  auto schemes = _providers->schemeNamesFromPrefixAndGroup(process);
394  for (auto& elem : schemes) {
395  auto itFFE2 = itFFE1->second.find(elem);
396  if (itFFE2 != itFFE1->second.end()) {
397  ff->updateFFErrTensor(vec, itFFE2->second);
398  }
399  }
400  }
401  }
402 
403 
405  auto ff = _providers->getFFErrProvider(process);
406  if (ff != nullptr) {
407  auto label = _providers->getFFErrLabel(process);
408  ASSERT(_processFFEigenVectors.get() != nullptr);
409  auto itFFE1 = _processFFEigenVectors->find(label);
410  ASSERT(itFFE1 != _processFFEigenVectors->end());
411  auto n = ff->getErrVectorFromSettings().size();
412  vector<double> tmpvalues(n-1);
413  tmpvalues.reserve(n);
414  ff->updateFFErrSettings(tmpvalues);
415  tmpvalues.insert(tmpvalues.begin(),1.);
416  auto schemes = _providers->schemeNamesFromPrefixAndGroup(process);
417  for(auto& elem: schemes) {
418  auto itFFE2 = itFFE1->second.find(elem);
419  if(itFFE2 != itFFE1->second.end()) {
420  ff->updateFFErrTensor(tmpvalues, itFFE2->second);
421  }
422  }
423  }
424  }
425 
427  const vector<double>& values) const {
428  auto ff = _providers->getFFErrProvider(process);
430  if (ff != nullptr) {
431  ff->updateFFErrTensor(values, t);
432  }
433  return t;
434  }
435 
437  const map<string, double>& values) const {
438  auto ff = _providers->getFFErrProvider(process);
440  if (ff != nullptr) {
441  auto vec = ff->getErrVectorFromDict(values);
442  ff->updateFFErrTensor(vec, t);
443  }
444  return t;
445  }
446 
447 
451  for (auto& elem : _providers->getAllWCProviders()) {
452  auto res = _processWilsonCoefficients->emplace(elem.first, array<MD::SharedTensorData, 2>{});
453  if (res.second) {
454  auto vecN = elem.second->getWCVectorFromSettings(WTerm::NUMERATOR);
455  elem.second->updateWCTensor(vecN, res.first->second[0]);
456  auto vecD = elem.second->getWCVectorFromSettings(WTerm::DENOMINATOR);
457  elem.second->updateWCTensor(vecD, res.first->second[1]);
458  }
459  }
460  for (auto& elem: *_processWilsonCoefficients){
461  _processSpecializedWilsonCoefficients.insert({elem.first, elem.second[0]});
462  }
463  }
464 
465  void ExternalData::initFormFactorErrors() {
466  _processFFEigenVectors.reset(new processFFEigenVectorsType{});
467  for (auto& elem : _providers->getAllFFErrProviders()) {
468  auto res = _processFFEigenVectors->emplace(elem.first, SchemeDict<MD::SharedTensorData>{});
469  if (res.second) {
470  for (auto& elem2 : elem.second) {
471  auto res2 = res.first->second.emplace(elem2.first, MD::SharedTensorData{});
472  if (res2.second) {
473  bool useDefault = (elem2.first == "Denominator");
474  auto vecN = elem2.second->getErrVectorFromSettings(useDefault);
475  elem2.second->updateFFErrTensor(vecN, res2.first->second);
476  }
477  }
478  }
479  }
480  }
481 
482  void ExternalData::initExternalVectors() {
483  _externalVectors.reset(new externalVectorsType{});
484  _externalVectors->reserve(_schemes.size() + 1);
485  for (auto& elem : _schemes) {
486  _externalVectors->insert({elem, UMap<LabelsList, Tensor>{}});
487  }
488  _externalVectors->insert({"Denominator", UMap<LabelsList, Tensor>{}});
489  }
490 
491  void ExternalData::init(vector<string> schemeNames) {
492  SettingsHandler* settings = getSettingsHandler();
493  _schemes = schemeNames;
494  initExternalVectors();
495  if (settings != nullptr) {
496  initWilsonCoefficients();
497  initFormFactorErrors();
498  }
499  }
500 
501 
502  Log& ExternalData::getLog() const {
503  return Log::getLog("Hammer.ExternalData");
504  }
505 
506 
507 } // namespace Hammer
PDG codes to UID functions.
std::map< IndexLabel, AmplitudeBase * > getAllWCProviders() const
virtual MultiDimensional::SharedTensorData getWilsonCoefficients(PdgId parent, const std::vector< PdgId > &daughters, const std::vector< PdgId > &granddaughters={}, WTerm what=WTerm::NUMERATOR) const
Definition: ExternalData.cc:99
virtual void defineSettings()
purely virtual function for a class to define new settings
Definition: ExternalData.cc:54
MultiDimensional::SharedTensorData getTempFFEigenVectors(const FFPrefixGroup &process, const std::vector< double > &values) const
void setWilsonCoefficients(const std::string &prefixName, const std::vector< std::complex< double >> &values, WTerm what=WTerm::NUMERATOR)
void setWilsonCoefficientsLocal(const std::string &prefixName, const std::vector< std::complex< double >> &values)
std::unordered_map< K, V, boost::hash< K >> UMap
Definition: Tools/Utils.hh:104
Hammer base form factor class.
Base class for form factors.
Base class for amplitudes.
std::pair< std::string, IndexLabel > getWCInfo() const
virtual MultiDimensional::SharedTensorData getFFEigenVectors(FormFactorBase *ff, const std::string &schemeName) const
void resetFFEigenVectors(const FFPrefixGroup &process)
#define ASSERT(x)
Definition: Exceptions.hh:95
boost::thread_specific_ptr< processWilsonCoefficientsType > _processWilsonCoefficients
Container class for values of WC and FF vectors.
virtual MultiDimensional::SharedTensorData getSpecializedWilsonCoefficients(AmplitudeBase *amp) const
void resetWilsonCoefficients(const std::string &prefixName, WTerm what=WTerm::NUMERATOR)
std::map< IndexLabel, std::array< MultiDimensional::SharedTensorData, 2 >> processWilsonCoefficientsType
virtual ~ExternalData() noexcept
Definition: ExternalData.cc:40
std::map< IndexLabel, MultiDimensional::SharedTensorData > _processSpecializedWilsonCoefficients
Interface class for amplitudes, rates, FFs dictionary container.
Message logging routines.
Hammer base amplitude class.
IndexLabel getWCLabel(const std::string &wcPrefix) const
void setFFEigenVectorsLocal(const FFPrefixGroup &process, const std::vector< double > &values)
std::map< std::string, bool > _isWCSpecializedDict
std::pair< FFPrefixGroup, IndexLabel > getFFErrInfo() const
virtual AmplitudeBase * getAmplitude(PdgId parent, const std::vector< PdgId > &daughters, const std::vector< PdgId > &granddaughters={}) const
std::shared_ptr< IContainer > SharedTensorData
boost::thread_specific_ptr< externalVectorsType > _externalVectors
std::map< IndexLabel, SchemeDict< MultiDimensional::SharedTensorData >> processFFEigenVectorsType
Hammer histogram class.
IndexLabel getFFErrLabel(const FFPrefixGroup &process) const
Order-0 tensor data container.
Logging class.
Definition: Logging.hh:33
void setFFEigenVectors(const FFPrefixGroup &process, const std::vector< double > &values)
std::unordered_map< std::string, UMap< LabelsList, Tensor >> externalVectorsType
virtual const Tensor & getExternalVectors(std::string schemeName, LabelsList labels) const
Definition: ExternalData.cc:57
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
Multidimensional tensor class with complex numbers as elements.
Definition: Tensor.hh:33
Generic error class.
Definition: Exceptions.hh:23
TensorData makeScalar(complex< double > value)
MultiDimensional::SharedTensorData getTempWilsonCoefficients(const std::string &prefixName, const std::vector< std::complex< double >> &values) const
std::vector< std::string > _schemes
Hammer settings manager class.
std::vector< IndexLabel > LabelsList
void specializeWCInWeights(const std::string &prefixName, const std::map< std::string, std::complex< double >> &settings)
const ProvidersRepo * _providers
std::map< SchemeName, T > SchemeDict
Definition: IndexTypes.hh:64
#define MSG_ERROR(x)
Definition: Logging.hh:367
FormFactorBase * getFFErrProvider(const FFPrefixGroup &process) const
#define MSG_INFO(x)
Definition: Logging.hh:365
AmplitudeBase * getWCProvider(const std::string &wcPrefix) const
void updateWCSettings(const std::vector< std::complex< double >> &values, WTerm what)
boost::thread_specific_ptr< processFFEigenVectorsType > _processFFEigenVectors
Hammer available modules header.
void resetSpecializeWCInWeights(const std::string &prefixName)
bool isWCSpecialized(AmplitudeBase *ampl) const
int PdgId
Definition: Pdg.fhh:17
Hammer tensor class.
Serialization related typedefs and includes.
const std::set< std::string > schemeNamesFromPrefixAndGroup(const FFPrefixGroup &value) const