Skip to content

Commit

Permalink
Replace ConvFun with ConvFuns
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Jun 17, 2024
1 parent cfcf027 commit a507af8
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 95 deletions.
8 changes: 3 additions & 5 deletions pineappl_cli/src/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvoluteMode, VecConvFun};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::builder::TypedValueParser;
Expand Down Expand Up @@ -40,9 +40,7 @@ pub struct CkfOpts {
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
/// LHAPDF ID(s) or name(s) of the PDF(s)/FF(s).
#[arg(value_parser = helpers::parse_conv_funs)]
// TODO: it would be better to use `Vec<ConvFun>`, but this consumes all following arguments
conv_funs: VecConvFun,
conv_funs: ConvFuns,
/// Order defining the K factors.
#[arg(value_parser = helpers::parse_order)]
order: (u32, u32),
Expand All @@ -66,7 +64,7 @@ pub struct CkfOpts {
impl Subcommand for CkfOpts {
fn run(&self, cfg: &GlobalConfiguration) -> Result<ExitCode> {
let grid = helpers::read_grid(&self.input)?;
let mut conv_funs = helpers::create_conv_funs(&self.conv_funs.0)?;
let mut conv_funs = helpers::create_conv_funs(&self.conv_funs)?;

let orders_den = if self.orders_den.is_empty() {
grid.orders()
Expand Down
5 changes: 2 additions & 3 deletions pineappl_cli/src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::builder::TypedValueParser;
Expand All @@ -15,8 +15,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
/// LHAPDF ID(s) or name(s) of the PDF(s)/FF(s).
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// Show absolute numbers of each contribution.
#[arg(long, short)]
absolute: bool,
Expand Down
5 changes: 2 additions & 3 deletions pineappl_cli/src/diff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::{bail, Result};
use clap::{Parser, ValueHint};
Expand All @@ -17,8 +17,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
input2: PathBuf,
/// LHAPDF ID(s) or name(s) of the PDF(s)/FF(s).
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// Ignore differences in the orders and sum them.
#[arg(long)]
ignore_orders: bool,
Expand Down
5 changes: 2 additions & 3 deletions pineappl_cli/src/evolve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::{anyhow, Result};
use clap::{Parser, ValueHint};
Expand Down Expand Up @@ -507,8 +507,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
output: PathBuf,
/// LHAPDF ID(s) or name of the PDF(s)/FF(s).
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// Relative threshold between the table and the converted grid when comparison fails.
#[arg(default_value = "1e-3", long)]
accuracy: f64,
Expand Down
11 changes: 5 additions & 6 deletions pineappl_cli/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::{anyhow, Result};
use clap::builder::{PossibleValuesParser, TypedValueParser};
Expand All @@ -15,7 +15,7 @@ mod applgrid;
fn convert_into_applgrid(
output: &Path,
grid: &Grid,
conv_funs: &[ConvFun],
conv_funs: &ConvFuns,
member: usize,
_: usize,
discard_non_matching_scales: bool,
Expand All @@ -33,7 +33,7 @@ fn convert_into_applgrid(
fn convert_into_applgrid(
_: &Path,
_: &Grid,
_: &[ConvFun],
_: &ConvFuns,
_: usize,
_: usize,
_: bool,
Expand All @@ -46,7 +46,7 @@ fn convert_into_applgrid(
fn convert_into_grid(
output: &Path,
grid: &Grid,
conv_funs: &[ConvFun],
conv_funs: &ConvFuns,
member: usize,
scales: usize,
discard_non_matching_scales: bool,
Expand Down Expand Up @@ -77,8 +77,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
output: PathBuf,
/// LHAPDF ID(s) or name of the PDF(s)/FF(s) to check the converted grid with.
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// Relative threshold between the table and the converted grid when comparison fails.
#[arg(default_value = "1e-10", long)]
accuracy: f64,
Expand Down
8 changes: 4 additions & 4 deletions pineappl_cli/src/export/applgrid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::ConvFun;
use super::helpers::ConvFuns;
use anyhow::{anyhow, bail, Result};
use cxx::{let_cxx_string, UniquePtr};
use float_cmp::approx_eq;
Expand Down Expand Up @@ -303,15 +303,15 @@ pub fn convert_into_applgrid(
}

// TODO: deduplicate this function from import
pub fn convolve_applgrid(grid: Pin<&mut grid>, conv_funs: &[ConvFun], member: usize) -> Vec<f64> {
pub fn convolve_applgrid(grid: Pin<&mut grid>, conv_funs: &ConvFuns, member: usize) -> Vec<f64> {
let nloops = grid.nloops();

// TODO: add support for convolving an APPLgrid with two functions
assert_eq!(conv_funs.len(), 1);
assert_eq!(conv_funs.lhapdf_names.len(), 1);

ffi::grid_convolve(
grid,
&conv_funs[0].lhapdf_name,
&conv_funs.lhapdf_names[0],
member.try_into().unwrap(),
nloops,
1.0,
Expand Down
39 changes: 14 additions & 25 deletions pineappl_cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,36 @@ use std::process::ExitCode;
use std::str::FromStr;

#[derive(Clone)]
pub struct ConvFun {
pub lhapdf_name: String,
label: String,
pub struct ConvFuns {
pub lhapdf_names: Vec<String>,
pub label: String,
}

impl FromStr for ConvFun {
impl FromStr for ConvFuns {
type Err = Infallible;

fn from_str(arg: &str) -> std::result::Result<Self, Self::Err> {
Ok(arg.split_once('=').map_or_else(
|| ConvFun {
lhapdf_name: arg.to_owned(),
|| Self {
lhapdf_names: arg.split(',').map(ToOwned::to_owned).collect(),
label: arg.to_owned(),
},
|(lhapdf_name, label)| ConvFun {
lhapdf_name: lhapdf_name.to_owned(),
|(lhapdf_names, label)| Self {
lhapdf_names: lhapdf_names.split(',').map(ToOwned::to_owned).collect(),
label: label.to_owned(),
},
))
}
}

#[derive(Clone)]
pub struct VecConvFun(pub Vec<ConvFun>);

pub fn parse_conv_funs(arg: &str) -> std::result::Result<VecConvFun, String> {
Ok(VecConvFun(
arg.split(',')
.map(|conv_fun| ConvFun::from_str(conv_fun).map_err(|err| format!("{err}")))
.collect::<Result<_, _>>()?,
))
}

pub fn create_conv_funs(funs: &[ConvFun]) -> Result<Vec<Pdf>> {
pub fn create_conv_funs(funs: &ConvFuns) -> Result<Vec<Pdf>> {
Ok(funs
.lhapdf_names
.iter()
.map(|fun| {
fun.lhapdf_name.parse().map_or_else(
|_| Pdf::with_setname_and_nmem(&fun.lhapdf_name),
Pdf::with_lhaid,
)
.map(|lhapdf_name| {
lhapdf_name
.parse()
.map_or_else(|_| Pdf::with_setname_and_nmem(lhapdf_name), Pdf::with_lhaid)
})
.collect::<Result<_, _>>()?)
}
Expand Down
19 changes: 9 additions & 10 deletions pineappl_cli/src/import.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::{anyhow, Result};
use clap::builder::{PossibleValuesParser, TypedValueParser};
Expand All @@ -18,7 +18,7 @@ mod fktable;
fn convert_applgrid(
input: &Path,
alpha: u32,
conv_funs: &[ConvFun],
conv_funs: &ConvFuns,
member: usize,
dis_pid: i32,
_: usize,
Expand All @@ -38,7 +38,7 @@ fn convert_applgrid(
fn convert_applgrid(
_: &Path,
_: u32,
_: &[ConvFun],
_: &ConvFuns,
_: usize,
_: i32,
_: usize,
Expand All @@ -52,7 +52,7 @@ fn convert_applgrid(
fn convert_fastnlo(
input: &Path,
alpha: u32,
conv_funs: &[ConvFun],
conv_funs: &ConvFuns,
member: usize,
dis_pid: i32,
scales: usize,
Expand All @@ -63,11 +63,11 @@ fn convert_fastnlo(
use std::ptr;

// TODO: convert this into an error?
assert_eq!(conv_funs.len(), 1);
assert_eq!(conv_funs.lhapdf_names.len(), 1);

let mut file = ffi::make_fastnlo_lhapdf_with_name_file_set(
input.to_str().unwrap(),
&conv_funs[0].lhapdf_name,
&conv_funs.lhapdf_names[0],
member.try_into().unwrap(),
);

Expand Down Expand Up @@ -121,7 +121,7 @@ fn convert_fastnlo(
fn convert_fastnlo(
_: &Path,
_: u32,
_: &str,
_: &ConvFuns,
_: usize,
_: i32,
_: usize,
Expand Down Expand Up @@ -150,7 +150,7 @@ fn convert_fktable(_: &Path, _: i32) -> Result<(&'static str, Grid, Vec<f64>, us
fn convert_grid(
input: &Path,
alpha: u32,
conv_funs: &[ConvFun],
conv_funs: &ConvFuns,
member: usize,
dis_pid: i32,
scales: usize,
Expand Down Expand Up @@ -214,8 +214,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
output: PathBuf,
/// LHAPDF ID(s) or name of the PDF(s)/FF(s) to check the converted grid with.
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// LO coupling power in alpha.
#[arg(default_value_t = 0, long)]
alpha: u32,
Expand Down
8 changes: 4 additions & 4 deletions pineappl_cli/src/import/applgrid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::ConvFun;
use super::helpers::ConvFuns;
use anyhow::Result;
use pineappl::boc::{Channel, Order};
use pineappl::convolutions::Convolution;
Expand Down Expand Up @@ -246,15 +246,15 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul
Ok(grid0)
}

pub fn convolve_applgrid(grid: Pin<&mut grid>, conv_funs: &[ConvFun], member: usize) -> Vec<f64> {
pub fn convolve_applgrid(grid: Pin<&mut grid>, conv_funs: &ConvFuns, member: usize) -> Vec<f64> {
let nloops = grid.nloops();

// TODO: add support for convolving an APPLgrid with two functions
assert_eq!(conv_funs.len(), 1);
assert_eq!(conv_funs.lhapdf_names.len(), 1);

ffi::grid_convolve(
grid,
&conv_funs[0].lhapdf_name,
&conv_funs.lhapdf_names[0],
member.try_into().unwrap(),
nloops,
1.0,
Expand Down
5 changes: 2 additions & 3 deletions pineappl_cli/src/orders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::helpers::{self, ConvFuns, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::{Parser, ValueHint};
Expand All @@ -13,8 +13,7 @@ pub struct Opts {
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
/// LHAPDF ID(s) or name(s) of the PDF(s)/FF(s).
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
conv_funs: ConvFuns,
/// Show absolute numbers of each perturbative order.
#[arg(long, short)]
absolute: bool,
Expand Down
10 changes: 5 additions & 5 deletions pineappl_cli/tests/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use assert_cmd::Command;

const HELP_STR: &str = "Shows the contribution for each partonic channel
Usage: pineappl channels [OPTIONS] <INPUT> <CONV_FUNS>...
Usage: pineappl channels [OPTIONS] <INPUT> <CONV_FUNS>
Arguments:
<INPUT> Path to the input grid
<CONV_FUNS>... LHAPDF ID(s) or name(s) of the PDF(s)/FF(s)
<INPUT> Path to the input grid
<CONV_FUNS> LHAPDF ID(s) or name(s) of the PDF(s)/FF(s)
Options:
-a, --absolute Show absolute numbers of each contribution
Expand Down Expand Up @@ -133,9 +133,9 @@ const DONT_SORT_STR: &str = "b etal c size c size c size c size c size
";

const MISSING_CONV_FUN_STR: &str = "error: the following required arguments were not provided:
<CONV_FUNS>...
<CONV_FUNS>
Usage: pineappl channels <INPUT> <CONV_FUNS>...
Usage: pineappl channels <INPUT> <CONV_FUNS>
For more information, try '--help'.
";
Expand Down
8 changes: 4 additions & 4 deletions pineappl_cli/tests/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use assert_fs::NamedTempFile;

const HELP_STR: &str = "Compares the numerical content of two grids with each other
Usage: pineappl diff [OPTIONS] <INPUT1> <INPUT2> <CONV_FUNS>...
Usage: pineappl diff [OPTIONS] <INPUT1> <INPUT2> <CONV_FUNS>
Arguments:
<INPUT1> Path to the first grid
<INPUT2> Path to the second grid
<CONV_FUNS>... LHAPDF ID(s) or name(s) of the PDF(s)/FF(s)
<INPUT1> Path to the first grid
<INPUT2> Path to the second grid
<CONV_FUNS> LHAPDF ID(s) or name(s) of the PDF(s)/FF(s)
Options:
--ignore-orders Ignore differences in the orders and sum them
Expand Down
10 changes: 5 additions & 5 deletions pineappl_cli/tests/evolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use assert_fs::NamedTempFile;

const HELP_STR: &str = "Evolve a grid with an evolution kernel operator to an FK table
Usage: pineappl evolve [OPTIONS] <INPUT> <EKO> <OUTPUT> <CONV_FUNS>...
Usage: pineappl evolve [OPTIONS] <INPUT> <EKO> <OUTPUT> <CONV_FUNS>
Arguments:
<INPUT> Path to the input grid
<EKO> Path to the evolution kernel operator
<OUTPUT> Path to the converted grid
<CONV_FUNS>... LHAPDF ID(s) or name of the PDF(s)/FF(s)
<INPUT> Path to the input grid
<EKO> Path to the evolution kernel operator
<OUTPUT> Path to the converted grid
<CONV_FUNS> LHAPDF ID(s) or name of the PDF(s)/FF(s)
Options:
--accuracy <ACCURACY> Relative threshold between the table and the converted grid when comparison fails [default: 1e-3]
Expand Down
8 changes: 4 additions & 4 deletions pineappl_cli/tests/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use assert_fs::NamedTempFile;

const HELP_STR: &str = "Converts PineAPPL grids to APPLgrid files
Usage: pineappl export [OPTIONS] <INPUT> <OUTPUT> <CONV_FUNS>...
Usage: pineappl export [OPTIONS] <INPUT> <OUTPUT> <CONV_FUNS>
Arguments:
<INPUT> Path to the input grid
<OUTPUT> Path to the converted grid
<CONV_FUNS>... LHAPDF ID(s) or name of the PDF(s)/FF(s) to check the converted grid with
<INPUT> Path to the input grid
<OUTPUT> Path to the converted grid
<CONV_FUNS> LHAPDF ID(s) or name of the PDF(s)/FF(s) to check the converted grid with
Options:
--accuracy <ACCURACY> Relative threshold between the table and the converted grid when comparison fails [default: 1e-10]
Expand Down
Loading

0 comments on commit a507af8

Please sign in to comment.