Skip to content

Commit

Permalink
Test deprecated CAPI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Jun 2, 2024
1 parent 08ab221 commit fd856cb
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PROGRAMS = \
advanced-convolution \
advanced-filling \
convolve-grid \
deprecated \
display-channels \
display-orders \
merge-grids \
Expand All @@ -28,6 +29,9 @@ advanced-filling: advanced-filling.cpp
convolve-grid: convolve-grid.cpp
$(CXX) $(CXXFLAGS) $< $(LHAPDF_DEPS) $(PINEAPPL_DEPS) -o $@

deprecated: deprecated.cpp
$(CXX) $(CXXFLAGS) $< $(LHAPDF_DEPS) $(PINEAPPL_DEPS) -o $@

display-channels: display-channels.cpp
$(CXX) $(CXXFLAGS) $< $(PINEAPPL_DEPS) -o $@

Expand Down
127 changes: 127 additions & 0 deletions examples/cpp/deprecated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <LHAPDF/PDF.h>
#include <pineappl_capi.h>

#include <cstddef>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char* argv[]) {
std::string filename = "drell-yan-rap-ll.pineappl.lz4";
std::string pdfset = "NNPDF31_nlo_as_0118_luxqed";

switch (argc) {
case 3:
pdfset = argv[2];
// fall through
case 2:
filename = argv[1];
case 1:
break;

default:
std::cout << "Usage: " << argv[0] << " [grid] [pdf]\n";
}

// disable LHAPDF banners to guarantee deterministic output
LHAPDF::setVerbosity(0);

// read the grid from a file
auto* grid = pineappl_grid_read(filename.c_str());

auto* pdf = LHAPDF::mkPDF(pdfset, 0);

// define callables for the PDFs and alphas
auto xfx1 = [](int32_t id, double x, double q2, void* pdf) {
return static_cast <LHAPDF::PDF*> (pdf)->xfxQ2(id, x, q2);
};
auto xfx2 = [](int32_t id, double x, double q2, void* pdf) {
return static_cast <LHAPDF::PDF*> (pdf)->xfxQ2(id, x, q2);
};
auto alphas = [](double q2, void* pdf) {
return static_cast <LHAPDF::PDF*> (pdf)->alphasQ2(q2);
};

// how many perturbative orders does the grid contain?
std::size_t orders = pineappl_grid_order_count(grid);

// how many bins does this grid have?
std::size_t bins = pineappl_grid_bin_count(grid);

auto* lumi = pineappl_grid_lumi(grid);

// how many channels does the grid have?
std::size_t channels = pineappl_lumi_count(lumi);

pineappl_lumi_delete(lumi);

// std::vector<bool> doesn't have `.data()` member
std::unique_ptr<bool[]> order_mask(new bool[orders]());

// allocate a vector holding the differential cross sections
std::vector<double> dxsec1(bins);

std::unique_ptr<bool[]> channel_mask(new bool[channels]());

// use the variables to select the included orders and channels
order_mask[0] = true;
channel_mask[0] = true;

// use these variables to perform scale variations
double xir = 1.0;
double xif = 1.0;

// use `pineappl_grid_convolve_with_one` instead
pineappl_grid_convolute_with_one(grid, 2212, xfx1, alphas, pdf, order_mask.get(),
channel_mask.get(), xir, xif, dxsec1.data());

// how does the grid know which PDFs it must be convolved with? This is determined by the
// metadata keys `initial_state_1` and `initial_state_2`, which are by default set to `2212`,
// the PDG MC ID for the proton. Let's change the second value to an antiproton:
pineappl_grid_set_key_value(grid, "initial_state_2", "-2212");

std::vector<double> dxsec2(bins);

// use `pineappl_grid_convolve_with_one` instead
pineappl_grid_convolute_with_one(grid, 2212, xfx1, alphas, pdf, order_mask.get(),
channel_mask.get(), xir, xif, dxsec2.data());

// what if we have a collision where we actually need two PDFs? Let's simulate the collision of
// protons with deuterons:
pineappl_grid_set_key_value(grid, "initial_state_2", "1000010020"); // 1000010020 = deuteron

std::vector<double> dxsec3(bins);

// use `pineappl_grid_convolve_with_two` instead
pineappl_grid_convolute_with_two(grid, 2212, xfx1, 1000010020, xfx2, alphas, pdf,
order_mask.get(), channel_mask.get(), xir, xif, dxsec3.data());

std::vector<double> dxsec4(bins);

// use `pineappl_grid_convolve_with_two` instead
pineappl_grid_convolve_with_two(grid, 2212, xfx1, 1000010020, xfx2, alphas, pdf, nullptr,
nullptr, xir, xif, dxsec4.data());

std::vector<double> normalizations(bins);

// read out the bin normalizations, which is usually the size of each bin
pineappl_grid_bin_normalizations(grid, normalizations.data());

// print table header
std::cout << "idx p-p c#0 l#0 p-p~ c#0 l# p-d c#0 l#0 p-d dx\n"
"--- ------------ ----------- ------------- ------------ ------\n";

for (std::size_t bin = 0; bin != bins; ++bin) {
// print the bin index
std::cout << std::setw(3) << bin << ' ';

// print the result together with the normalization
std::cout << std::scientific << dxsec1.at(bin) << ' ' << dxsec2.at(bin) << ' '
<< dxsec3.at(bin) << ' ' << dxsec4.at(bin) << ' ' << std::defaultfloat << std::setw(6)
<< normalizations.at(bin) << '\n';
}

pineappl_grid_delete(grid);
}
26 changes: 26 additions & 0 deletions examples/cpp/output
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ idx left right dsig/dx dx
21 2.1 2.2 3.491788e-02 0.1
22 2.2 2.3 1.967518e-02 0.1
23 2.3 2.4 5.565306e-03 0.1
idx p-p c#0 l#0 p-p~ c#0 l# p-d c#0 l#0 p-d dx
--- ------------ ----------- ------------- ------------ ------
0 5.263109e-01 5.263109e-01 5.263109e-01 5.263109e-01 0.1
1 5.254908e-01 5.254908e-01 5.254908e-01 5.254908e-01 0.1
2 5.246824e-01 5.246824e-01 5.246824e-01 5.246824e-01 0.1
3 5.188340e-01 5.188340e-01 5.188340e-01 5.188340e-01 0.1
4 5.175482e-01 5.175482e-01 5.175482e-01 5.175482e-01 0.1
5 5.008841e-01 5.008841e-01 5.008841e-01 5.008841e-01 0.1
6 4.905325e-01 4.905325e-01 4.905325e-01 4.905325e-01 0.1
7 4.675734e-01 4.675734e-01 4.675734e-01 4.675734e-01 0.1
8 4.393159e-01 4.393159e-01 4.393159e-01 4.393159e-01 0.1
9 3.992921e-01 3.992921e-01 3.992921e-01 3.992921e-01 0.1
10 3.706801e-01 3.706801e-01 3.706801e-01 3.706801e-01 0.1
11 3.264717e-01 3.264717e-01 3.264717e-01 3.264717e-01 0.1
12 2.849345e-01 2.849345e-01 2.849345e-01 2.849345e-01 0.1
13 2.486723e-01 2.486723e-01 2.486723e-01 2.486723e-01 0.1
14 2.110419e-01 2.110419e-01 2.110419e-01 2.110419e-01 0.1
15 1.797439e-01 1.797439e-01 1.797439e-01 1.797439e-01 0.1
16 1.471492e-01 1.471492e-01 1.471492e-01 1.471492e-01 0.1
17 1.205566e-01 1.205566e-01 1.205566e-01 1.205566e-01 0.1
18 9.491625e-02 9.491625e-02 9.491625e-02 9.491625e-02 0.1
19 7.255720e-02 7.255720e-02 7.255720e-02 7.255720e-02 0.1
20 5.056967e-02 5.056967e-02 5.056967e-02 5.056967e-02 0.1
21 3.491788e-02 3.491788e-02 3.491788e-02 3.491788e-02 0.1
22 1.967518e-02 1.967518e-02 1.967518e-02 1.967518e-02 0.1
23 5.565306e-03 5.565306e-03 5.565306e-03 5.565306e-03 0.1
0 1 x ( 22, 22)
1 1 x ( 1, -1) + 1 x ( 3, -3) + 1 x ( 5, -5)
0 O(as^0 a^2 lr^0 lf^0)
Expand Down

0 comments on commit fd856cb

Please sign in to comment.