From 7f7184afa97cc633410dddab70e6a0ba198f18e5 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 7 Feb 2025 01:21:01 +0100 Subject: [PATCH] Nuke `v0` in OOP C++ --- examples/object-oriented-cpp/Makefile | 4 - .../object-oriented-cpp/dyaa-deprecated.cpp | 157 -------- examples/object-oriented-cpp/dyaa.cpp | 6 +- examples/object-oriented-cpp/output | 31 -- pineappl_capi/include/PineAPPL.hpp | 357 ++++-------------- 5 files changed, 74 insertions(+), 481 deletions(-) delete mode 100644 examples/object-oriented-cpp/dyaa-deprecated.cpp diff --git a/examples/object-oriented-cpp/Makefile b/examples/object-oriented-cpp/Makefile index 0fe6343d..5c24660f 100644 --- a/examples/object-oriented-cpp/Makefile +++ b/examples/object-oriented-cpp/Makefile @@ -4,7 +4,6 @@ PINEAPPL_DEPS != pkg-config --cflags --libs pineappl_capi LHAPDF_DEPS != pkg-config --cflags --libs lhapdf PROGRAMS = \ - dyaa-deprecated \ dyaa \ all: $(PROGRAMS) @@ -15,9 +14,6 @@ test-examples: $(PROGRAMS) dyaa: dyaa.cpp $(CXX) $(CXXFLAGS) $< $(PINEAPPL_DEPS) $(LHAPDF_DEPS) -o $@ -dyaa-deprecated: dyaa-deprecated.cpp - $(CXX) $(CXXFLAGS) $< $(PINEAPPL_DEPS) $(LHAPDF_DEPS) -o $@ - PHONY: clean clean: diff --git a/examples/object-oriented-cpp/dyaa-deprecated.cpp b/examples/object-oriented-cpp/dyaa-deprecated.cpp deleted file mode 100644 index 4f58c533..00000000 --- a/examples/object-oriented-cpp/dyaa-deprecated.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -#include "PineAPPL.hpp" - -double int_photo(double s, double t, double u) { - double alpha0 = 1.0 / 137.03599911; - return alpha0 * alpha0 / 2.0 / s * (t / u + u / t); -} - -struct Psp2to2 { - double s; - double t; - double u; - double x1; - double x2; - double jacobian; -}; - -Psp2to2 hadronic_pspgen(std::mt19937& rng, double mmin, double mmax) { - using std::acos; - using std::log; - using std::pow; - - double smin = mmin * mmin; - double smax = mmax * mmax; - - double r1 = std::generate_canonical(rng); - double r2 = std::generate_canonical(rng); - double r3 = std::generate_canonical(rng); - - double tau0 = smin / smax; - double tau = pow(tau0, r1); - double y = pow(tau, 1.0 - r2); - double x1 = y; - double x2 = tau / y; - double s = tau * smax; - - double jacobian = tau * log(tau0) * log(tau0) * r1; - - // theta integration (in the CMS) - double cos_theta = 2.0 * r3 - 1.0; - jacobian *= 2.0; - - double t = -0.5 * s * (1.0 - cos_theta); - double u = -0.5 * s * (1.0 + cos_theta); - - // phi integration - jacobian *= 2.0 * acos(-1.0); - - return {s, t, u, x1, x2, jacobian}; -} - -void fill_grid(PineAPPL::Grid& grid, std::size_t calls) { - using std::acosh; - using std::fabs; - using std::log; - using std::sqrt; - - auto rng = std::mt19937(); - - // in GeV^2 pbarn - double hbarc2 = 389379372.1; - - for (std::size_t i = 0; i != calls; ++i) { - // generate a phase-space point - auto tmp = hadronic_pspgen(rng, 10.0, 7000.0); - auto s = tmp.s; - auto t = tmp.t; - auto u = tmp.u; - auto x1 = tmp.x1; - auto x2 = tmp.x2; - auto jacobian = tmp.jacobian; - - double ptl = sqrt((t * u / s)); - double mll = sqrt(s); - double yll = 0.5 * log(x1 / x2); - double ylp = fabs(yll + acosh(0.5 * mll / ptl)); - double ylm = fabs(yll - acosh(0.5 * mll / ptl)); - - jacobian *= hbarc2 / calls; - - // cuts for LO for the invariant-mass slice containing the - // Z-peak from CMSDY2D11 - if ((ptl < 14.0) || (fabs(yll) > 2.4) || (ylp > 2.4) || (ylm > 2.4) || - (mll < 60.0) || (mll > 120.0)) { - continue; - } - - auto weight = jacobian * int_photo(s, t, u); - double q2 = 90.0 * 90.0; - - grid.fill(x1, x2, q2, 0, fabs(yll), 0, weight); - } -} - -int main() { - // create a new luminosity function for the $\gamma\gamma$ initial state - PineAPPL::Lumi lumi; - lumi.add({PineAPPL::LumiEntry{22, 22, 1.0}}); - - // only LO $\alpha_\mathrm{s}^0 \alpha^2 \log^0(\xi_\mathrm{R}) - // \log^0(\xi_\mathrm{F})$ - std::vector orders = {PineAPPL::Order{0, 2, 0, 0}}; - - // we bin in rapidity from 0 to 2.4 in steps of 0.1 - std::vector bins = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, - 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, - 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4}; - - // create the PineAPPL grid with default interpolation and binning parameters - PineAPPL::KeyVal kv; - PineAPPL::Grid grid(lumi, orders, bins, kv); - - // fill the grid with phase-space points - fill_grid(grid, 10000000); - - // Optimize the Grid - grid.optimize(); - - LHAPDF::setVerbosity(0); - // perform a convolution of the grid with PDFs - std::unique_ptr pdf( - LHAPDF::mkPDF("NNPDF31_nlo_as_0118_luxqed", 0)); - std::vector dxsec = grid.convolve_with_one(2212, *pdf.get()); - - // print the results - for (std::size_t j = 0; j != dxsec.size(); ++j) { - std::printf("%02zu %.1f %.1f %.3e\n", j, bins[j], bins[j + 1], dxsec[j]); - } - - // store some metadata in the grid - grid.set_key_value("events", "10000000"); - - // read out the stored value and print it on stdout - const auto value = grid.get_key_value("events"); - std::printf("Finished running %s events.\n", value.c_str()); - - // write the grid to disk - with `.lz4` suffix the grid is automatically LZ4 - // compressed - const std::string filename = "DY-LO-AA-deprecated.pineappl.lz4"; - grid.write(filename); - - std::printf( - "Generated %s containing a a -> l+ l-.\n\n" - "Try running (PDF sets must contain non-zero photon PDF):\n" - " - pineappl convolve %s NNPDF31_nnlo_as_0118_luxqed\n" - " - pineappl --silence-lhapdf plot %s NNPDF31_nnlo_as_0118_luxqed " - "MSHT20qed_nnlo > plot_script.py\n" - " - pineappl --help\n", - filename.c_str(), filename.c_str(), filename.c_str()); -} diff --git a/examples/object-oriented-cpp/dyaa.cpp b/examples/object-oriented-cpp/dyaa.cpp index ea06e1e0..f553a3c9 100644 --- a/examples/object-oriented-cpp/dyaa.cpp +++ b/examples/object-oriented-cpp/dyaa.cpp @@ -58,7 +58,7 @@ Psp2to2 hadronic_pspgen(std::mt19937& rng, double mmin, double mmax) { return {s, t, u, x1, x2, jacobian}; } -void fill_grid(PineAPPL::GridV1& grid, std::size_t calls) { +void fill_grid(PineAPPL::Grid& grid, std::size_t calls) { using std::acosh; using std::fabs; using std::log; @@ -114,7 +114,7 @@ int main() { // --- Instatiate the Order object // only LO $\alpha_\mathrm{s}^0 \alpha^2 \log^0(\xi_\mathrm{R}) // \log^0(\xi_\mathrm{F}) \log^0(\xi_\mathrm{A})$ - std::vector orders = {PineAPPL::OrderV1{0, 2, 0, 0, 0}}; + std::vector orders = {PineAPPL::Order{0, 2, 0, 0, 0}}; // --- Define the binning // we bin in rapidity from 0 to 2.4 in steps of 0.1 @@ -155,7 +155,7 @@ int main() { // Define the μ scale std::vector mu_scales = {1, 1, 1}; - PineAPPL::GridV1 grid(orders, channels, pid_basis, pids, convolution_types, + PineAPPL::Grid grid(orders, channels, pid_basis, pids, convolution_types, kinematics, interpolations, bins, mu_scales); // fill the grid with phase-space points diff --git a/examples/object-oriented-cpp/output b/examples/object-oriented-cpp/output index bf004954..199382d7 100644 --- a/examples/object-oriented-cpp/output +++ b/examples/object-oriented-cpp/output @@ -1,34 +1,3 @@ -00 0.0 0.1 5.263e-01 -01 0.1 0.2 5.255e-01 -02 0.2 0.3 5.247e-01 -03 0.3 0.4 5.188e-01 -04 0.4 0.5 5.175e-01 -05 0.5 0.6 5.009e-01 -06 0.6 0.7 4.905e-01 -07 0.7 0.8 4.676e-01 -08 0.8 0.9 4.393e-01 -09 0.9 1.0 3.993e-01 -10 1.0 1.1 3.707e-01 -11 1.1 1.2 3.265e-01 -12 1.2 1.3 2.849e-01 -13 1.3 1.4 2.487e-01 -14 1.4 1.5 2.110e-01 -15 1.5 1.6 1.797e-01 -16 1.6 1.7 1.471e-01 -17 1.7 1.8 1.206e-01 -18 1.8 1.9 9.492e-02 -19 1.9 2.0 7.256e-02 -20 2.0 2.1 5.057e-02 -21 2.1 2.2 3.492e-02 -22 2.2 2.3 1.968e-02 -23 2.3 2.4 5.565e-03 -Finished running 10000000 events. -Generated DY-LO-AA-deprecated.pineappl.lz4 containing a a -> l+ l-. - -Try running (PDF sets must contain non-zero photon PDF): - - pineappl convolve DY-LO-AA-deprecated.pineappl.lz4 NNPDF31_nnlo_as_0118_luxqed - - pineappl --silence-lhapdf plot DY-LO-AA-deprecated.pineappl.lz4 NNPDF31_nnlo_as_0118_luxqed MSHT20qed_nnlo > plot_script.py - - pineappl --help Generated DY-LO-AA.pineappl.lz4 containing a a -> l+ l-. Try running (PDF sets must contain non-zero photon PDF): diff --git a/pineappl_capi/include/PineAPPL.hpp b/pineappl_capi/include/PineAPPL.hpp index 17f4b53f..b10f5898 100644 --- a/pineappl_capi/include/PineAPPL.hpp +++ b/pineappl_capi/include/PineAPPL.hpp @@ -1,5 +1,5 @@ /** - * @brief Object oriented interface to PineAPPL. + * @brief Object oriented interface to PineAPPL using only v1 features. */ #ifndef PineAPPL_HPP_ #define PineAPPL_HPP_ @@ -7,124 +7,21 @@ #include #include -#include #include #include -#include #include #include -/** @brief Object oriented interface to PineAPPL. */ +/** @brief Object oriented interface to PineAPPL.*/ namespace PineAPPL { -/** @brief Key-value storage for passing optional information during grid - * creation. */ -struct KeyVal { - /** @brief Underlying raw object. */ - pineappl_keyval *raw; - - /** @brief Constructor. */ - KeyVal() : raw(pineappl_keyval_new()) {} - KeyVal(const KeyVal &) = delete; - KeyVal(KeyVal &&) = delete; - - KeyVal &operator=(const KeyVal &) = delete; - KeyVal &operator=(KeyVal &&) = delete; - - /** @brief Destructor. */ - ~KeyVal() { pineappl_keyval_delete(this->raw); } - - /** @name Setter. */ - ///@{ - void set_double(const std::string &key, const double value) const { - pineappl_keyval_set_double(this->raw, key.c_str(), value); - } - void set_bool(const std::string &key, const bool value) const { - pineappl_keyval_set_bool(this->raw, key.c_str(), value); - } - void set_int(const std::string &key, const int value) const { - pineappl_keyval_set_int(this->raw, key.c_str(), value); - } - void set_string(const std::string &key, const std::string &value) const { - pineappl_keyval_set_string(this->raw, key.c_str(), value.c_str()); - } - ///@} - - /** @name Getter. */ - ///@{ - double get_double(const std::string &key) const { - return pineappl_keyval_double(this->raw, key.c_str()); - } - bool get_bool(const std::string &key) const { - return pineappl_keyval_bool(this->raw, key.c_str()); - } - int get_int(const std::string &key) const { - return pineappl_keyval_int(this->raw, key.c_str()); - } - std::string get_string(const std::string &key) const { - return pineappl_keyval_string(this->raw, key.c_str()); - } - ///@} -}; - -/** @brief Entry in luminosity function. */ -struct LumiEntry { - /** @brief First parton id. */ - std::int32_t pid1; - - /** @brief Second parton id. */ - std::int32_t pid2; - - /** @brief Relative weight. */ - double weight; -}; - -/** @brief Luminosity function. */ -struct Lumi { - /** @brief Underlying raw object. */ - pineappl_lumi *raw; - - /** @brief Constructor. */ - Lumi() : raw(pineappl_lumi_new()) {} - Lumi(const Lumi &) = delete; - Lumi(Lumi &&) = delete; - - Lumi &operator=(const Lumi &) = delete; - Lumi &operator=(Lumi &&) = delete; - - /** @brief Destructor. */ - ~Lumi() { pineappl_lumi_delete(this->raw); } + // TODO: Add checks within various functions/calls + // TODO: Implement `grid_convolve` - /** @brief Number of elements. */ - std::size_t count() const { return pineappl_lumi_count(this->raw); } - - /** - * @brief Add a luminosity function. - * @param c luminosity function - */ - void add(const std::vector &c) const { - const std::size_t n = c.size(); - std::vector pids; - std::vector weights; - for (const LumiEntry &l : c) { - pids.push_back(l.pid1); - pids.push_back(l.pid2); - weights.push_back(l.weight); - } - pineappl_lumi_add(this->raw, n, pids.data(), weights.data()); - } - - /** - * @brief Returns the number of combinations of the luminosity function - * `lumi` for the specified entry. - * @param entry position in lumi - */ - std::size_t combinations(const std::size_t entry) const { - return pineappl_lumi_combinations(this->raw, entry); - } -}; - -/** @brief Entry in sub-channel function. */ +/** @brief Entry in sub-channel function. A sub-channel consists of a vector of + * tuple. Each tuple contains two elements. The first elements store the list of + * PIDs as a vector while the second represent the weighting factor. Note that + * the number of PIDs must correspond to the number of convolutions involved.*/ struct SubChannelEntry { /** @brief First parton id. */ std::vector, double>> entry; @@ -188,25 +85,8 @@ struct Channels { } }; -/** @brief Coupling powers for each grid. */ -struct Order { - /** @brief Exponent of the strong coupling. */ - std::uint32_t alphas; - - /** @brief Exponent of the electromagnetic coupling. */ - std::uint32_t alpha; - - /** @brief Exponent of the logarithm of the scale factor of the - * renomalization scale. */ - std::uint32_t logxir; - - /** @brief Exponent of the logarithm of the scale factor of the factorization - * scale. */ - std::uint32_t logxif; -}; - /** @brief Extension of `Order` to accommodate for v1 representation. */ -struct OrderV1 { +struct Order { /** @brief Exponent of the strong coupling. */ std::uint8_t alphas; @@ -227,25 +107,79 @@ struct OrderV1 { }; /** @brief Base Grid struct that contains the common data in v0 and v1. */ -struct GridBase { +struct Grid { /** @brief Underlying raw object. */ pineappl_grid *raw; /** @brief Constructor (protected to avoid direct instantiation). */ protected: - GridBase(pineappl_grid *grid) : raw(grid) {} + Grid(pineappl_grid *grid) : raw(grid) {} /** @brief Deleted copy/move semantics. */ - GridBase() = delete; - GridBase(const GridBase &) = delete; - GridBase(GridBase &&) = delete; + Grid() = delete; + Grid(const Grid &) = delete; + Grid(Grid &&) = delete; - GridBase &operator=(const GridBase &) = delete; - GridBase &operator=(GridBase &&) = delete; + Grid &operator=(const Grid &) = delete; + Grid &operator=(Grid &&) = delete; public: /** @brief Destructor. */ - virtual ~GridBase() { pineappl_grid_delete(this->raw); } + virtual ~Grid() { pineappl_grid_delete(this->raw); } + + /** + * @brief Constructor. + * @param orders orders + * @param channels channels + * @param pid_basis pid_basis + * @param pids pids + * @param convolutions_types convolution_types + * @param interp interp + * @param bin_limits bin_limits + * @param mu_scales indexed representing the scales + */ + Grid(std::vector &orders, const Channels &channels, + pineappl_pid_basis pid_basis, std::vector pids, + std::vector &convolution_types, + std::vector &kinematics, + std::vector &interp, + std::vector &bin_limits, std::vector &mu_scales) + : Grid(nullptr) { + const std::size_t n_orders = orders.size(); + const std::size_t n_bins = bin_limits.size() - 1; + const std::size_t n_convs = convolution_types.size(); + + // Cast the Orders + std::vector raw_orders; + for (const Order &order : orders) { + raw_orders.push_back(order.alphas); + raw_orders.push_back(order.alpha); + raw_orders.push_back(order.logxir); + raw_orders.push_back(order.logxif); + raw_orders.push_back(order.logxia); + } + + this->raw = pineappl_grid_new2( + pid_basis, channels.raw, n_orders, raw_orders.data(), n_bins, + bin_limits.data(), n_convs, convolution_types.data(), pids.data(), + kinematics.data(), interp.data(), mu_scales.data()); + // + } + + /** + * @brief Fill grid for the given parameters. + * @param order perturbative order + * @param observable value of the observable + * @param channel index of the channel + * @param ntuples tuples containing the kinematics {q2, x1, ...} + * @param weight value of the weighting factor + */ + void fill(const std::size_t order, const double observable, + const std::size_t channel, std::vector &ntuples, + const double weight) const { + pineappl_grid_fill2(this->raw, order, observable, channel, ntuples.data(), + weight); + } /** * @brief Number of orders. @@ -304,155 +238,6 @@ struct GridBase { void optimize() const { pineappl_grid_optimize(this->raw); } }; -/** @brief The central grid object. */ -struct Grid : public GridBase { - - /** - * @brief Constructor. - * @param lumi luminosity - * @param orders orders - * @param bin_limits bin limits - * @param key_val additional informations - */ - Grid(const Lumi &lumi, const std::vector &orders, - const std::vector &bin_limits, const KeyVal &key_val) - : GridBase(nullptr) { - // cast orders - const std::size_t n_orders = orders.size(); - std::vector raw_orders; - for (const Order &order : orders) { - raw_orders.push_back(order.alphas); - raw_orders.push_back(order.alpha); - raw_orders.push_back(order.logxir); - raw_orders.push_back(order.logxif); - } - this->raw = pineappl_grid_new(lumi.raw, n_orders, raw_orders.data(), - bin_limits.size() - 1, bin_limits.data(), - key_val.raw); - } - - /** - * @brief Number of orders. - * @return number of orders - */ - std::size_t order_count() const { - return pineappl_grid_order_count(this->raw); - } - - /** - * @brief Fill grid for the given parameters. - * @param x1 first momentum fraction - * @param x2 second momentum fraction - * @param q2 scale - * @param order order index - * @param observable observable value - * @param lumi luminosity index - * @param weight weight - */ - void fill(const double x1, const double x2, const double q2, - const std::size_t order, const double observable, - const std::size_t lumi, const double weight) const { - pineappl_grid_fill(this->raw, x1, x2, q2, order, observable, lumi, weight); - } - - /** - * @brief Perform a convolution of the grid with PDFs. - * @param pdg_id hadron ID - * @param pdf PDF - * @param xi_ren renormalization scale variation - * @param xi_fac factorization scale variation - * @param order_mask order mask - * @param lumi_mask luminosity mask - * @return prediction for each bin - */ - std::vector convolve_with_one( - const std::int32_t pdg_id, LHAPDF::PDF &pdf, const double xi_ren = 1.0, - const double xi_fac = 1.0, const std::vector &order_mask = {}, - const std::vector &lumi_mask = {}) const { - // prepare LHAPDF stuff - auto xfx = [](std::int32_t id, double x, double q2, void *pdf) { - return static_cast(pdf)->xfxQ2(id, x, q2); - }; - auto alphas = [](double q2, void *pdf) { - return static_cast(pdf)->alphasQ2(q2); - }; - // cast order_mask - std::unique_ptr raw_order_mask; - if (!order_mask.empty()) { - raw_order_mask = std::unique_ptr(new bool[order_mask.size()]); - std::copy(order_mask.begin(), order_mask.end(), &raw_order_mask[0]); - } - // cast lumi mask - std::unique_ptr raw_lumi_mask; - if (!lumi_mask.empty()) { - raw_lumi_mask = std::unique_ptr(new bool[lumi_mask.size()]); - std::copy(lumi_mask.begin(), lumi_mask.end(), &raw_lumi_mask[0]); - } - // do it! - std::vector results(this->bin_count()); - pineappl_grid_convolve_with_one(this->raw, pdg_id, xfx, alphas, &pdf, - raw_order_mask.get(), raw_lumi_mask.get(), - xi_ren, xi_fac, results.data()); - return results; - } -}; - -/** @brief The central grid object in the v1 representation. */ -struct GridV1 : public GridBase { - /** - * @brief Constructor. - * @param orders orders - * @param channels channels - * @param pid_basis pid_basis - * @param pids pids - * @param convolutions_types convolution_types - * @param interp interp - * @param bin_limits bin_limits - * @param mu_scales indexed representing the scales - */ - GridV1(std::vector &orders, const Channels &channels, - pineappl_pid_basis pid_basis, std::vector pids, - std::vector &convolution_types, - std::vector &kinematics, std::vector &interp, - std::vector &bin_limits, std::vector &mu_scales) - : GridBase(nullptr) { - const std::size_t n_orders = orders.size(); - const std::size_t n_bins = bin_limits.size() - 1; - const std::size_t n_convs = convolution_types.size(); - - // Cast the Orders - std::vector raw_orders; - for (const OrderV1 &order : orders) { - raw_orders.push_back(order.alphas); - raw_orders.push_back(order.alpha); - raw_orders.push_back(order.logxir); - raw_orders.push_back(order.logxif); - raw_orders.push_back(order.logxia); - } - - this->raw = pineappl_grid_new2( - pid_basis, channels.raw, n_orders, raw_orders.data(), n_bins, - bin_limits.data(), n_convs, convolution_types.data(), pids.data(), - kinematics.data(), interp.data(), mu_scales.data()); - // - } - - /** - * @brief Fill grid for the given parameters. - * @param order perturbative order - * @param observable value of the observable - * @param channel index of the channel - * @param ntuples tuples containing the kinematics {q2, x1, ...} - * @param weight value of the weighting factor - */ - void fill(const std::size_t order, const double observable, - const std::size_t channel, std::vector &ntuples, - const double weight) const { - pineappl_grid_fill2(this->raw, order, observable, channel, ntuples.data(), - weight); - } -}; - } // namespace PineAPPL #endif // PineAPPL_HPP_