Skip to content

Commit

Permalink
Migrate analyze subcommand to use ConvFun
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Jun 17, 2024
1 parent be221cd commit cfcf027
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
16 changes: 8 additions & 8 deletions pineappl_cli/src/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::helpers::{self, ConvoluteMode};
use super::helpers::{self, ConvoluteMode, VecConvFun};
use super::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::builder::TypedValueParser;
use clap::{value_parser, Parser, ValueHint};
use prettytable::{cell, Row};
use std::path::PathBuf;
use std::process::ExitCode;
use std::slice;

/// Perform various analyses with grids.
#[derive(Parser)]
Expand Down Expand Up @@ -40,9 +39,10 @@ pub struct CkfOpts {
/// Path to the input grid.
#[arg(value_hint = ValueHint::FilePath)]
input: PathBuf,
/// LHAPDF id or name of the PDF set.
#[arg(value_parser = helpers::parse_pdfset)]
pdfset: String,
/// 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,
/// Order defining the K factors.
#[arg(value_parser = helpers::parse_order)]
order: (u32, u32),
Expand All @@ -66,7 +66,7 @@ pub struct CkfOpts {
impl Subcommand for CkfOpts {
fn run(&self, cfg: &GlobalConfiguration) -> Result<ExitCode> {
let grid = helpers::read_grid(&self.input)?;
let mut pdf = helpers::create_pdf(&self.pdfset)?;
let mut conv_funs = helpers::create_conv_funs(&self.conv_funs.0)?;

let orders_den = if self.orders_den.is_empty() {
grid.orders()
Expand All @@ -88,7 +88,7 @@ impl Subcommand for CkfOpts {
lumi_mask[lumi] = true;
helpers::convolve(
&grid,
slice::from_mut(&mut pdf),
&mut conv_funs,
&[self.order],
&[],
&lumi_mask,
Expand All @@ -104,7 +104,7 @@ impl Subcommand for CkfOpts {
lumi_mask[lumi] = true;
helpers::convolve(
&grid,
slice::from_mut(&mut pdf),
&mut conv_funs,
&orders_den,
&[],
&lumi_mask,
Expand Down
13 changes: 12 additions & 1 deletion pineappl_cli/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::GlobalConfiguration;
use anyhow::{anyhow, ensure, Context, Result};
use anyhow::{ensure, Context, Result};
use lhapdf::{Pdf, PdfSet};
use ndarray::Array3;
use pineappl::convolutions::LumiCache;
Expand Down Expand Up @@ -37,6 +37,17 @@ impl FromStr for ConvFun {
}
}

#[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>> {
Ok(funs
.iter()
Expand Down
4 changes: 2 additions & 2 deletions pineappl_cli/tests/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Options:

const CKF_HELP_STR: &str = "Compare K-factors with channel K factors (ckf)
Usage: pineappl analyze ckf [OPTIONS] <INPUT> <PDFSET> <ORDER> [ORDERS_DEN]...
Usage: pineappl analyze ckf [OPTIONS] <INPUT> <CONV_FUNS> <ORDER> [ORDERS_DEN]...
Arguments:
<INPUT> Path to the input grid
<PDFSET> LHAPDF id or name of the PDF set
<CONV_FUNS> LHAPDF ID(s) or name(s) of the PDF(s)/FF(s)
<ORDER> Order defining the K factors
[ORDERS_DEN]... Normalizing orders of the K factors
Expand Down

0 comments on commit cfcf027

Please sign in to comment.