LHAPDF is hosted by Hepforge, IPPP Durham
LHAPDF  6.1.6
GridPDF.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2016 The LHAPDF collaboration (see AUTHORS for details)
5 //
6 #pragma once
7 #ifndef LHAPDF_GridPDF_H
8 #define LHAPDF_GridPDF_H
9 
10 #include "LHAPDF/PDF.h"
11 #include "LHAPDF/Interpolator.h"
12 #include "LHAPDF/Extrapolator.h"
13 #include "LHAPDF/KnotArray.h"
14 
15 namespace LHAPDF {
16 
17 
19  class GridPDF : public PDF {
20  public:
21 
23 
24 
26  GridPDF() {
27  _mempath = "";
28  _info = PDFInfo();
29  _forcePos = -1;
30  }
31 
39  GridPDF(const std::string& path) {
40  _loadInfo(path); // Sets _mempath
42  _forcePos = -1;
43  }
44 
46  GridPDF(const std::string& setname, int member) {
47  _loadInfo(setname, member); // Sets _mempath
49  _forcePos = -1;
50  }
51 
53  GridPDF(int lhaid) {
54  _loadInfo(lhaid); // Sets _mempath
56  _forcePos = -1;
57  }
58 
60  virtual ~GridPDF() { }
61 
63 
64 
65  protected:
66 
68  void _loadData(const std::string& mempath);
69 
70 
71  public:
72 
74 
75 
82  _interpolator.reset(ipol);
83  _interpolator->bind(this);
84  }
85 
92  template <typename INTERPOLATOR>
93  void setInterpolator(INTERPOLATOR ipol) {
94  setInterpolator(new INTERPOLATOR(ipol));
95  }
96 
101  void setInterpolator(const std::string& ipolname) {
102  setInterpolator(mkInterpolator(ipolname));
103  }
104 
106  bool hasInterpolator() const {
107  return _interpolator.get() != 0;
108  }
109 
111  const Interpolator& interpolator() const {
112  if (_interpolator.get() == 0) { // Load the default interpolator lazily
113  // NB. The following is equiv to set-by-name but is explicitly implemented here for const correctness
114  const string ipolname = info().get_entry("Interpolator");
115  Interpolator* ipol = mkInterpolator(ipolname);
116  _interpolator.reset(ipol);
117  _interpolator->bind(this);
118  }
119  return *_interpolator;
120  }
121 
122 
123 
130  _extrapolator.reset(xpol);
131  _extrapolator->bind(this);
132  }
133 
140  template <typename EXTRAPOLATOR>
141  void setExtrapolator(EXTRAPOLATOR xpol) {
142  setExtrapolator(new EXTRAPOLATOR(xpol));
143  }
144 
149  void setExtrapolator(const std::string& xpolname) {
150  setExtrapolator(mkExtrapolator(xpolname));
151  }
152 
154  bool hasExtrapolator() const {
155  return _extrapolator.get() != 0;
156  }
157 
159  const Extrapolator& extrapolator() const {
160  if (_extrapolator.get() == 0) { // Load the default extrapolator lazily
161  // NB. The following is equiv to set-by-name but is explicitly implemented here for const correctness
162  const string xpolname = info().get_entry("Extrapolator");
163  Extrapolator* xpol = mkExtrapolator(xpolname);
164  _extrapolator.reset(xpol);
165  _extrapolator->bind(this);
166  }
167  return *_extrapolator;
168  }
169 
171 
172 
173  protected:
174 
176  double _xfxQ2(int id, double x, double q2) const;
177 
178 
179  public:
180 
182 
183 
185  std::map<double, KnotArrayNF>& knotarrays() {
186  return _knotarrays;
187  }
188 
190  const KnotArrayNF& subgrid(double q2) const {
191  assert(q2 >= 0);
192  assert(!q2Knots().empty());
193  map<double, KnotArrayNF>::const_iterator it = _knotarrays.upper_bound(q2);
194  if (it == _knotarrays.begin())
195  throw GridError("Requested Q2 " + to_str(q2) + " is lower than any available Q2 subgrid (lowest Q2 = " + to_str(q2Knots().front()) + ")");
196  if (it == _knotarrays.end() && q2 > q2Knots().back())
197  throw GridError("Requested Q2 " + to_str(q2) + " is higher than any available Q2 subgrid (highest Q2 = " + to_str(q2Knots().back()) + ")");
198  --it; // upper_bound (and lower_bound) returns the entry *above* q2: we need to decrement by one element
199  // std::cout << "Using subgrid #" << std::distance(_knotarrays.begin(), it) << std::endl;
200  return it->second;
201  }
202 
204  const KnotArray1F& subgrid(int id, double q2) const {
205  return subgrid(q2).get_pid(id);
206  }
207 
211  const vector<double>& xKnots() const {
212  const KnotArrayNF& subgrid1 = _knotarrays.begin()->second;
213  const KnotArray1F& grid1 = subgrid1.get_first();
214  return grid1.xs();
215  }
216 
220  const vector<double>& q2Knots() const {
221  if (_q2knots.empty()) {
222  // Get the list of Q2 knots by combining all subgrids
224  for (map<double, KnotArrayNF>::const_iterator isub = _knotarrays.begin(); isub != _knotarrays.end(); ++isub) {
225  const KnotArrayNF& subgrid = isub->second;
226  const KnotArray1F& grid1 = subgrid.get_first();
227  if (grid1.q2s().empty()) continue; //< @todo This shouldn't be possible, right? Throw instead, or ditch the check?
228  for (double q2 : grid1.q2s()) {
229  if (_q2knots.empty() || q2 != _q2knots.back()) _q2knots.push_back(q2);
230  }
231  }
232  }
233  return _q2knots;
234  }
235 
236 
237  public:
238 
240  bool inRangeX(double x) const {
241  assert(!xKnots().empty());
242  if (x < xKnots().front()) return false;
243  if (x > xKnots().back()) return false;
244  return true;
245  }
246 
248  bool inRangeQ2(double q2) const {
249  assert(!q2Knots().empty());
250  if (q2 < q2Knots().front()) return false;
251  if (q2 > q2Knots().back()) return false;
252  return true;
253  }
254 
256 
257 
258  private:
259 
261  std::map<double, KnotArrayNF> _knotarrays;
262 
263  // /// Caching vector of x knot values
264  // mutable std::vector<double> _xknots;
265 
267  mutable std::vector<double> _q2knots;
268 
270  typedef unique_ptr<Interpolator> InterpolatorPtr;
271 
273  typedef unique_ptr<Extrapolator> ExtrapolatorPtr;
274 
276  mutable InterpolatorPtr _interpolator;
277 
279  mutable ExtrapolatorPtr _extrapolator;
280 
281  };
282 
283 
284 }
285 #endif
const KnotArray1F & subgrid(int id, double q2) const
Get the 1-flavour subgrid for PID=id containing Q2 = q2.
Definition: GridPDF.h:204
PDF is the general interface for access to parton density information.
Definition: PDF.h:26
const vector< double > & xKnots() const
Return a representative list of interpolation knots in x.
Definition: GridPDF.h:211
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:61
const KnotArrayNF & subgrid(double q2) const
Get the N-flavour subgrid containing Q2 = q2.
Definition: GridPDF.h:190
unique_ptr< Extrapolator > ExtrapolatorPtr
Typedef of smart pointer for xpol memory handling.
Definition: GridPDF.h:273
InterpolatorPtr _interpolator
Associated interpolator (mutable to allow laziness)
Definition: GridPDF.h:276
void setExtrapolator(const std::string &xpolname)
Set the extrapolator by name.
Definition: GridPDF.h:149
const std::vector< double > & xs() const
x knot accessor
Definition: KnotArray.h:84
A collection of {KnotArray1F}s accessed by PID code.
Definition: KnotArray.h:179
const vector< double > & q2Knots() const
Return a representative list of interpolation knots in Q2.
Definition: GridPDF.h:220
void _loadData(const std::string &mempath)
Load the PDF grid data block (not the metadata) from the given PDF member file.
const Interpolator & interpolator() const
Get the current interpolator.
Definition: GridPDF.h:111
void setInterpolator(const std::string &ipolname)
Set the interpolator by name.
Definition: GridPDF.h:101
bool hasInterpolator() const
Find whether an extrapolator has been set on this PDF.
Definition: GridPDF.h:106
const std::vector< double > & q2s() const
Q2 knot accessor.
Definition: KnotArray.h:113
A PDF defined via an interpolation grid.
Definition: GridPDF.h:19
PDFInfo & info()
Get the info class that actually stores and handles the metadata.
Definition: PDF.h:393
void setInterpolator(Interpolator *ipol)
Set the interpolator by pointer.
Definition: GridPDF.h:81
GridPDF(int lhaid)
Constructor from an LHAPDF ID.
Definition: GridPDF.h:53
const KnotArray1F & get_first() const
Convenience accessor for any valid subgrid, to get access to the x/Q2/etc. arrays.
Definition: KnotArray.h:200
std::vector< double > _q2knots
Caching vector of Q2 knot values.
Definition: GridPDF.h:267
const KnotArray1F & get_pid(int id) const
Get the KnotArray1F for PID code id.
Definition: KnotArray.h:194
GridPDF(const std::string &setname, int member)
Constructor from a set name and member ID.
Definition: GridPDF.h:46
const Extrapolator & extrapolator() const
Get the current extrapolator.
Definition: GridPDF.h:159
Error for general PDF grid problems.
Definition: Exceptions.h:30
void setExtrapolator(Extrapolator *xpol)
Set the extrapolator by pointer.
Definition: GridPDF.h:129
void setExtrapolator(EXTRAPOLATOR xpol)
Set the extrapolator by value.
Definition: GridPDF.h:141
virtual ~GridPDF()
Virtual destructor to allow inheritance.
Definition: GridPDF.h:60
std::map< double, KnotArrayNF > _knotarrays
Map of multi-flavour KnotArrays "binned" for lookup by low edge in Q2.
Definition: GridPDF.h:261
Metadata class for PDF members.
Definition: PDFInfo.h:18
PDFInfo _info
Metadata container.
Definition: PDF.h:558
Namespace for all LHAPDF functions and classes.
Definition: AlphaS.h:14
GridPDF(const std::string &path)
Constructor from a file path.
Definition: GridPDF.h:39
bool inRangeQ2(double q2) const
Check if q2 is in the grid range.
Definition: GridPDF.h:248
Extrapolator * mkExtrapolator(const std::string &name)
The general interface for interpolating between grid points.
Definition: Interpolator.h:21
bool hasExtrapolator() const
Find whether an extrapolator has been set on this PDF.
Definition: GridPDF.h:154
std::map< double, KnotArrayNF > & knotarrays()
Directly access the knot arrays in non-const mode, for programmatic filling.
Definition: GridPDF.h:185
void setInterpolator(INTERPOLATOR ipol)
Set the interpolator by value.
Definition: GridPDF.h:93
double _xfxQ2(int id, double x, double q2) const
Get PDF xf(x,Q2) value (via grid inter/extrapolators)
int _forcePos
Cached flag for whether to return only positive (or postive definite) PDF values. ...
Definition: PDF.h:571
std::string _mempath
Member data file path.
Definition: PDF.h:555
ExtrapolatorPtr _extrapolator
Associated extrapolator (mutable to allow laziness)
Definition: GridPDF.h:279
Interpolator * mkInterpolator(const std::string &name)
Internal storage class for PDF data point grids.
Definition: KnotArray.h:20
bool inRangeX(double x) const
Check if x is in the grid range.
Definition: GridPDF.h:240
unique_ptr< Interpolator > InterpolatorPtr
Typedef of smart pointer for ipol memory handling.
Definition: GridPDF.h:270
The general interface for extrapolating beyond grid boundaries.
Definition: Extrapolator.h:20
void _loadInfo(const std::string &mempath)
Definition: PDF.h:50
const std::string & get_entry(const std::string &key) const
Retrieve a metadata string by key name.
GridPDF()
Default constructor, making an empty PDF to be populated by hand.
Definition: GridPDF.h:26