Skip to content

Commit a87ce7b

Browse files
Merge pull request #380 from NNPDF/various-fixes
Various API fixes
2 parents 6175d29 + 898c828 commit a87ce7b

File tree

12 files changed

+166
-25
lines changed

12 files changed

+166
-25
lines changed

.github/actions/cache-test-data/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ runs:
1010
uses: actions/cache@v4
1111
with:
1212
path: test-data
13-
key: test-data-v23
13+
key: test-data-v24
1414
- name: Download test data if cache miss
1515
if: steps.cache.outputs.cache-hit != 'true'
1616
run: |

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- added an actual implementation of `pineappl_grid_metadata` and
13+
`pineappl_grid_set_metadata` in the APIs
14+
15+
### Fixed
16+
17+
- fixed a bug in the Newton's convergence method by raising the maximum number
18+
of iteration
19+
- fixed a bug in the implementation of `pineappl_channels_add` of the Fortran
20+
API
21+
1022
### Changed
1123

1224
- raised MSRV to 1.94.0

examples/cpp/set-subgrids.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ int main() {
207207
);
208208
pineappl_grid_optimize(grid);
209209

210+
// ---
211+
// Metadata testing
212+
pineappl_grid_set_metadata(grid, "comment", "This is a toy SIDIS grid");
213+
pineappl_grid_set_metadata(grid, "experiment", "ToyExp");
214+
215+
char* comment = pineappl_grid_metadata(grid, "comment");
216+
char* experiment = pineappl_grid_metadata(grid, "experiment");
217+
pineappl_string_delete(comment);
218+
pineappl_string_delete(experiment);
219+
210220
// ---
211221
// Write the grid to disk - the filename can be anything ...
212222
std::string filename = "sidis-toygrid.pineappl.lz4";

examples/fortran/dyaa.f90

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ program dyaa
109109
! above in 'channels')
110110
! - for PDF parameters 'x1, x2, q2',
111111
! - with the given 'weight'
112-
call pineappl_grid_fill2(grid, order_idx, abs(yll), channel_idx, [ x1, x2, q2 ], weight)
112+
call pineappl_grid_fill2(grid, order_idx, abs(yll), channel_idx, [ q2, x1, x2 ], weight)
113113
end do
114114

115115
! set metadata - this isn't strictly needed, but usually useful (plot script, ...)
116-
call pineappl_grid_set_key_value(grid, 'arxiv', '1310.7291')
117-
call pineappl_grid_set_key_value(grid, 'description', 'CMS double-differential Drell—Yan cross section at 7 TeV')
118-
call pineappl_grid_set_key_value(grid, 'hepdata', '10.17182/hepdata.62207.v1/t8')
119-
call pineappl_grid_set_key_value(grid, 'x1_label', 'yll')
120-
call pineappl_grid_set_key_value(grid, 'x1_label_tex', '$y_{\ell\bar{\ell}}$')
116+
call pineappl_grid_set_metadata(grid, 'arxiv', '1310.7291')
117+
call pineappl_grid_set_metadata(grid, 'description', 'CMS double-differential Drell—Yan cross section at 7 TeV')
118+
call pineappl_grid_set_metadata(grid, 'hepdata', '10.17182/hepdata.62207.v1/t8')
119+
call pineappl_grid_set_metadata(grid, 'x1_label', 'yll')
120+
call pineappl_grid_set_metadata(grid, 'x1_label_tex', '$y_{\ell\bar{\ell}}$')
121121
! rapidity doesn't have a unit (other observables could be GeV, TeV, ...)
122-
call pineappl_grid_set_key_value(grid, 'x1_unit', '')
123-
call pineappl_grid_set_key_value(grid, 'y_unit', 'pb')
122+
call pineappl_grid_set_metadata(grid, 'x1_unit', '')
123+
call pineappl_grid_set_metadata(grid, 'y_unit', 'pb')
124124

125125
! optimize the grid representation (makes the file smaller)
126126
call pineappl_grid_optimize(grid)

examples/fortran/pineappl.f90

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ subroutine channels_add(channels, combinations, pdg_id_combinations, factors) &
173173

174174
type (c_ptr) function channels_new(convolutions) bind(c, name = 'pineappl_channels_new')
175175
use iso_c_binding
176-
integer (c_int32_t), value :: convolutions
176+
integer (c_size_t), value :: convolutions
177177
end function
178178

179179
integer (c_size_t) function grid_bin_count(grid) bind(c, name = 'pineappl_grid_bin_count')
@@ -271,6 +271,13 @@ function grid_key_value(grid, key) bind(c, name = 'pineappl_grid_key_value')
271271
type (c_ptr) :: grid_key_value
272272
end function
273273

274+
function grid_metadata(grid, key) bind(c, name = 'pineappl_grid_metadata')
275+
use iso_c_binding
276+
type (c_ptr), value :: grid
277+
character (c_char) :: key(*)
278+
type (c_ptr) :: grid_metadata
279+
end function
280+
274281
function grid_channels(grid) bind(c, name = 'pineappl_grid_channels')
275282
use iso_c_binding
276283
type (c_ptr), value :: grid
@@ -358,6 +365,12 @@ subroutine grid_set_key_value(grid, key, valju) bind(c, name = 'pineappl_grid_se
358365
character (c_char) :: key(*), valju(*)
359366
end subroutine
360367

368+
subroutine grid_set_metadata(grid, key, valju) bind(c, name = 'pineappl_grid_set_metadata')
369+
use iso_c_binding
370+
type (c_ptr), value :: grid
371+
character (c_char) :: key(*), valju(*)
372+
end subroutine
373+
361374
subroutine grid_set_remapper(grid, dimensions, normalizations, limits) bind(c, name = 'pineappl_grid_set_remapper')
362375
use iso_c_binding
363376
type (c_ptr), value :: grid
@@ -434,9 +447,9 @@ function c_f_string(c_str) result(f_str)
434447
type (pineappl_channels) function pineappl_channels_new(convolutions)
435448
implicit none
436449

437-
integer (c_int32_t), value :: convolutions
450+
integer, intent(in) :: convolutions
438451

439-
pineappl_channels_new = pineappl_channels(channels_new(convolutions))
452+
pineappl_channels_new = pineappl_channels(channels_new(int(convolutions, c_size_t)))
440453
end function
441454

442455
integer function pineappl_grid_bin_count(grid)
@@ -538,7 +551,7 @@ function pineappl_grid_convolve(grid, xfx, alphas, pdfs_state, alphas_state, ord
538551
alphas_state, &
539552
[(logical(order_mask(i), c_bool), i = 1, size(order_mask))], &
540553
[(logical(channel_mask(i), c_bool), i = 1, size(channel_mask))], &
541-
[(int(bin_indices, c_size_t), i = 1, size(bin_indices))], &
554+
[(int(bin_indices(i), c_size_t), i = 1, size(bin_indices))], &
542555
int(nb_scales, c_size_t), &
543556
mu_scales, &
544557
res &
@@ -605,6 +618,18 @@ function pineappl_grid_key_value(grid, key) result(res)
605618
res = c_f_string(grid_key_value(grid%ptr, key // c_null_char))
606619
end function
607620

621+
function pineappl_grid_metadata(grid, key) result(res)
622+
use iso_c_binding
623+
624+
implicit none
625+
626+
type (pineappl_grid), intent(in) :: grid
627+
character (*), intent(in) :: key
628+
character (:), allocatable :: res
629+
630+
res = c_f_string(grid_metadata(grid%ptr, key // c_null_char))
631+
end function
632+
608633
type (pineappl_channels) function pineappl_grid_channels(grid)
609634
implicit none
610635

@@ -752,6 +777,17 @@ subroutine pineappl_grid_set_key_value(grid, key, valju)
752777
call grid_set_key_value(grid%ptr, key // c_null_char, valju // c_null_char)
753778
end subroutine
754779

780+
subroutine pineappl_grid_set_metadata(grid, key, valju)
781+
use iso_c_binding
782+
783+
implicit none
784+
785+
type (pineappl_grid), intent(in) :: grid
786+
character (*), intent(in) :: key, valju
787+
788+
call grid_set_metadata(grid%ptr, key // c_null_char, valju // c_null_char)
789+
end subroutine
790+
755791
subroutine pineappl_grid_set_remapper(grid, dimensions, normalizations, limits)
756792
use iso_c_binding
757793

@@ -788,10 +824,10 @@ subroutine pineappl_channels_add(channels, combinations, pdg_id_combinations, fa
788824

789825
implicit none
790826

791-
type (pineappl_channels), intent(in) :: channels
792-
integer, intent(in) :: combinations
793-
integer, dimension(2 * combinations), intent(in) :: pdg_id_combinations
794-
real (dp), dimension(combinations), intent(in) :: factors
827+
type (pineappl_channels), intent(in) :: channels
828+
integer, intent(in) :: combinations
829+
integer, dimension(*), intent(in) :: pdg_id_combinations
830+
real (dp), dimension(combinations), intent(in) :: factors
795831

796832
call channels_add(channels%ptr, int(combinations, c_size_t), pdg_id_combinations, factors)
797833
end subroutine

examples/fortran/test.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ program test_pineappl
247247

248248
call pineappl_grid_set_key_value(grid, "set_key_value", "set_key_value: success")
249249

250+
call pineappl_grid_set_metadata(grid, "set_metadata", "set_metadata: success")
251+
252+
if (pineappl_grid_metadata(grid, "set_metadata") /= "set_metadata: success") then
253+
write(*, *) "pineappl_grid_metadata(): '", pineappl_grid_metadata(grid, "set_metadata"), "'"
254+
error stop "error: pineappl_grid_metadata"
255+
end if
256+
250257
! NOTE: At this point we have the bins: [0, 1, 2, 3]
251258
call pineappl_grid_set_remapper(grid, 2, [1.0_dp, 1.0_dp, 1.0_dp], &
252259
[0.0_dp, 1.0_dp, 10.0_dp, 11.0_dp, 1.0_dp, 3.0_dp, 11.0_dp, 13.0_dp, 15.0_dp, 20.0_dp])

examples/object-oriented-cpp/PineAPPL.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,18 @@ struct Grid {
229229
* @param key key
230230
* @param value value
231231
*/
232-
void set_key_value(const std::string &key, const std::string &value) const {
233-
pineappl_grid_set_key_value(this->raw, key.c_str(), value.c_str());
232+
void set_metadata(const std::string &key, const std::string &value) const {
233+
pineappl_grid_set_metadata(this->raw, key.c_str(), value.c_str());
234234
}
235235

236236
/**
237237
* @brief Get a metadata entry.
238238
* @param key key
239239
* @return value
240240
*/
241-
std::string get_key_value(const std::string &key) const {
242-
auto *value = pineappl_grid_key_value(this->raw, key.c_str());
243-
std::string res(value);
241+
std::string get_metadata(const std::string &key) const {
242+
auto *value = pineappl_grid_metadata(this->raw, key.c_str());
243+
std::string res(value != nullptr ? value : "");
244244
// delete the allocated object
245245
pineappl_string_delete(value);
246246
return res;

examples/object-oriented-cpp/dyaa.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ int main() {
199199
}
200200

201201
// store some metadata in the grid
202-
grid.set_key_value("events", "10000000");
202+
grid.set_metadata("events", "10000000");
203203

204204
// read out the stored value and print it on stdout
205-
const auto value = grid.get_key_value("events");
205+
const auto value = grid.get_metadata("events");
206206
std::printf("Finished running %s events.\n", value.c_str());
207207

208208
// write the grid to disk - with `.lz4` suffix the grid is automatically LZ4

maintainer/download-test-data.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ files=(
5252
'https://data.nnpdf.science/pineappl/test-data/ZEUS_2JET_319GEV_374PB-1_DIF_ETQ2_BIN6.tar'
5353
'https://data.nnpdf.science/pineappl/test-data/LHCB_WP_8TEV.pineappl.lz4'
5454
'https://data.nnpdf.science/pineappl/test-data/LHCB_WP_8TEV.tar'
55+
'https://data.nnpdf.science/pineappl/test-data/test_newton_convergence.pineappl.lz4'
5556
'https://ploughshare.web.cern.ch/ploughshare/db/applfast/applfast-atlas-dijets-fnlo-arxiv-1312.3524/grids/applfast-atlas-dijets-fnlo-arxiv-1312.3524-xsec000.tab.gz'
5657
'https://ploughshare.web.cern.ch/ploughshare/db/applfast/applfast-h1-dijets-appl-arxiv-0010054/grids/applfast-h1-dijets-appl-arxiv-0010054-xsec000.appl'
5758
'https://ploughshare.web.cern.ch/ploughshare/db/applfast/applfast-h1-incjets-fnlo-arxiv-0706.3722/grids/applfast-h1-incjets-fnlo-arxiv-0706.3722-xsec000.tab.gz'

pineappl/src/interpolation.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod applgrid {
2020
let mut yp = y;
2121
let mut deltap = f64::INFINITY;
2222

23-
for _ in 0..10 {
23+
for _ in 0..15 {
2424
let x = (-yp).exp();
2525
let delta = (1.0 - x).mul_add(-5.0, y - yp);
2626
if (delta == 0.0) || ((delta.abs() < 2e-15) && (delta.abs() >= deltap.abs())) {
@@ -804,4 +804,14 @@ mod tests {
804804
ulps = 4
805805
);
806806
}
807+
808+
#[test]
809+
fn issue_372() {
810+
assert_approx_eq!(
811+
f64,
812+
applgrid::fx2(3.751520963950722),
813+
0.4221667753589648,
814+
ulps = 4
815+
);
816+
}
807817
}

0 commit comments

Comments
 (0)