LHAPDF  6.5.5
GridPDF.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2024 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 
18  /// @brief A PDF defined via an interpolation grid
19  class GridPDF : public PDF {
20  public:
21 
22  /// Default constructor, making an empty PDF to be populated by hand.
23  GridPDF() {
24  _mempath = "";
25  _info = PDFInfo();
26  _forcePos = -1;
27  }
28 
29  /// @brief Constructor from a file path
30  ///
31  /// We allow this to exist and be user-callable for testing and other
32  /// special case uses, since if you are explicitly instantiating a GridPDF
33  /// rather than acquiring it via a pointer/reference of PDF type, then you
34  /// probably (hopefully) know what you're doing and aren't putting it into
35  /// public production code!
36  GridPDF(const std::string& path) {
37  _loadInfo(path); // Sets _mempath
39  _loadData(_mempath);
40  _forcePos = -1;
41  }
42 
43  /// Constructor from a set name and member ID
44  GridPDF(const std::string& setname, int member) {
45  _loadInfo(setname, member); // Sets _mempath
46  _loadData(_mempath);
48  _forcePos = -1;
49  }
50 
51  /// Constructor from an LHAPDF ID
52  GridPDF(int lhaid) {
53  _loadInfo(lhaid); // Sets _mempath
54  _loadData(_mempath);
56  _forcePos = -1;
57  }
58 
59  /// Virtual destructor to allow inheritance
60  virtual ~GridPDF() { }
61 
62 
63  protected:
64 
65  /// Load the interpolator, based on current metadata
67 
68  /// Load the PDF grid data block, based on current metadata
70 
71  /// Load the alphaS, interpolator, and extrapolator based on current metadata
72  void _loadPlugins() {
73  _loadAlphaS();
76  }
77 
78  /// Load the PDF grid data block (not the metadata) from the given PDF member file
79  void _loadData(const std::string& mempath);
80 
81  /// Precompute polynomial coefficients and approximate derivatives at knot positions
82  void _computePolynomialCoefficients(bool logspace);
83 
84 
85  public:
86 
87  /// @name Interpolators and extrapolators
88  ///@{
89 
90  /// @brief Set the interpolator by pointer
91  ///
92  /// The provided Interpolator must have been new'd, as it will not be copied
93  /// and ownership passes to this GridPDF: delete will be called on this ptr
94  /// when this GridPDF goes out of scope or another setInterpolator call is made.
95  void setInterpolator(Interpolator* ipol);
96 
97  /// @brief Set the interpolator by value
98  ///
99  /// The passed value must be a concrete instantiation of the Interpolator
100  /// interface. It will be copied and heap-assigned for use inside this GridPDF.
101  ///
102  /// @todo Use SFINAE magic to restrict INTERPOLATOR to subclasses of Interpolator?
103  template <typename INTERPOLATOR>
104  void setInterpolator(INTERPOLATOR ipol) {
105  setInterpolator(new INTERPOLATOR(ipol));
106  }
107 
108  /// @brief Set the interpolator by name
109  ///
110  /// Use the interpolator specified by the given name, as passed to the
111  /// createInterpolator factory function.
112  void setInterpolator(const std::string& ipolname);
113 
114  /// Find whether an extrapolator has been set on this PDF
115  bool hasInterpolator() const { return bool(_interpolator); }
116 
117  /// Get the current interpolator
118  const Interpolator& interpolator() const;
119 
120 
121  /// @brief Set the extrapolator by pointer
122  ///
123  /// The provided Extrapolator must have been new'd, as it will not be copied
124  /// and ownership passes to this GridPDF: delete will be called on this ptr
125  /// when this GridPDF goes out of scope or another setExtrapolator call is made.
126  void setExtrapolator(Extrapolator* xpol);
127 
128  /// @brief Set the extrapolator by value
129  ///
130  /// The passed value must be a concrete instantiation of the Extrapolator
131  /// interface. It will be copied and heap-assigned for use inside this GridPDF.
132  ///
133  /// @todo Use SFINAE magic to restrict EXTRAPOLATOR to subclasses of Extrapolator?
134  template <typename EXTRAPOLATOR>
135  void setExtrapolator(EXTRAPOLATOR xpol) {
136  setExtrapolator(new EXTRAPOLATOR(xpol));
137  }
138 
139  /// @brief Set the extrapolator by name
140  ///
141  /// Use the extrapolator specified by the given name, as passed to the
142  /// createExtrapolator factory function.
143  void setExtrapolator(const std::string& xpolname);
144 
145  /// Find whether an extrapolator has been set on this PDF
146  bool hasExtrapolator() const { return bool(_extrapolator); }
147 
148  /// Get the current extrapolator
149  const Extrapolator& extrapolator() const;
150 
151  ///@}
152 
153 
154  protected:
155 
156  /// @brief Get PDF xf(x,Q2) value (via grid inter/extrapolators)
157  double _xfxQ2(int id, double x, double q2) const;
158 
159  void _xfxQ2(double x, double q2, std::vector<double>& ret) const;
160 
161 
162  public:
163 
164  /// @name Info about the grid, and access to the raw data points
165  ///@{
166 
167  // accerror to new data structure
168  const KnotArray& knotarray() const {
169  return data;
170  }
171 
172  KnotArray& Data() {
173  return data;
174  }
175 
176  /// @brief Return a representative list of interpolation knots in x
177  ///
178  /// The x knot array for the first flavor grid of the lowest-Q2 subgrid is returned.
179  const vector<double>& xKnots() const;
180 
181  /// @brief Return a representative list of interpolation knots in Q2
182  ///
183  /// Constructed and cached by walking over all subgrids and concatenating their Q2 lists: expensive!
184  const vector<double>& q2Knots() const;
185 
186  public:
187 
188  /// Check if x is in the grid range
189  bool inRangeX(double x) const {
190  return data.inRangeX(x);
191  }
192 
193  /// Check if q2 is in the grid range
194  bool inRangeQ2(double q2) const {
195  return data.inRangeQ2(q2);
196  }
197  ///@}
198 
199  private:
200  // *new* memory object, to handle basically everything
201  // name?
202  KnotArray data;
203 
204  /// Typedef of smart pointer for ipol memory handling
206 
207  /// Typedef of smart pointer for xpol memory handling
209 
210  /// Associated interpolator (mutable to allow laziness)
212 
213  /// Associated extrapolator (mutable to allow laziness)
215 
216  };
217 
218 
219 }
220 #endif