diff --git a/examples/cpp/advanced-filling.cpp b/examples/cpp/advanced-filling.cpp index 7b8be66c..4e330355 100644 --- a/examples/cpp/advanced-filling.cpp +++ b/examples/cpp/advanced-filling.cpp @@ -173,6 +173,37 @@ int main() { pineappl_grid_write(grid, "advanced-filling.pineappl.lz4"); + //-----------------------------------------------------------------------// + + // Remove the bins for which the convolution is zero. + std::vector zero_indices; + zero_indices.reserve(dxsec.size()); + for (size_t i = 0; i < dxsec.size(); ++i) { + if (dxsec[i] == 0.0) { + zero_indices.push_back(static_cast(i)); + } + } + pineappl_grid_delete_bins(grid, zero_indices.data(), zero_indices.size()); + + std::vector dxsec_no_zeros(pineappl_grid_bin_count(grid)); + pineappl_grid_convolve(grid, xfx, alphas, pdf_states, pdf.get(), order_mask, channel_mask, + nullptr, 1, mmu_scales.data(), dxsec_no_zeros.data()); + + // Print table header + std::cout << " " << std::endl; + std::cout << std::setw(10) << "bin left" + << std::setw(12) << "bin right" + << std::setw(15) << "dsig/dx" << std::endl; + std::cout << std::string(37, '-') << std::endl; + + // Loop through bins and print results + for (size_t i = 0; i < dxsec_no_zeros.size(); ++i) { + std::cout << std::setw(10) << bins[i] + << std::setw(12) << bins[i + 1] + << std::setw(15) << std::scientific << std::setprecision(3) << dxsec_no_zeros[i] + << std::endl; + } + // release memory pineappl_grid_delete(grid); } diff --git a/examples/cpp/advanced-filling.output b/examples/cpp/advanced-filling.output index bfcfdfe5..173db1e8 100644 --- a/examples/cpp/advanced-filling.output +++ b/examples/cpp/advanced-filling.output @@ -24,3 +24,7 @@ 2.100e+00 2.200e+00 0.000e+00 2.200e+00 2.300e+00 0.000e+00 2.300e+00 2.400e+00 0.000e+00 + + bin left bin right dsig/dx +------------------------------------- + 0.000e+00 1.000e-01 6.683e+03 diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index b2ef04a4..cdab06e6 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -860,6 +860,29 @@ pub unsafe extern "C" fn pineappl_grid_merge_bins(grid: *mut Grid, from: usize, grid.merge_bins(from..to).unwrap(); } +/// Deletes bins with the corresponding `bin_indices`. Repeated indices and indices larger or +/// equal the bin length are ignored. +/// +/// # Safety +/// +/// The parameter `grid` must be valid `Grid` object created by either `pineappl_grid_new` or +/// `pineappl_grid_read`. +/// The parameter `bin_indices_ptr` must be an array of size `bin_indices_len`. +/// +/// # Panics +/// +/// TODO +#[no_mangle] +pub unsafe extern "C" fn pineappl_grid_delete_bins( + grid: *mut Grid, + bin_indices_ptr: *const usize, + bin_indices_len: usize, +) { + let grid = unsafe { &mut *grid }; + let bin_indices = unsafe { std::slice::from_raw_parts(bin_indices_ptr, bin_indices_len) }; + grid.delete_bins(bin_indices); +} + /// Merges `other` into `grid` and subsequently deletes `other`. /// /// # Safety