Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify multiple convolution functions with CLI #293

Merged
merged 29 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
31cc779
pdf input to a Vec of strings
t7phy Jun 9, 2024
2392652
Merge remote-tracking branch 'origin/master' into ts050624
t7phy Jun 9, 2024
5efc0f1
Undo changes from commit 31cc779
cschwan Jun 10, 2024
0ec7393
Prepare CLI for multiple convolution functions
cschwan Jun 10, 2024
9556a50
Add struct `ConvFun` and use it in `channels`
cschwan Jun 10, 2024
98de0ba
add two conv funcs support in convolve_scales
t7phy Jun 10, 2024
ed4cbc2
Make `helpers::convolve_scales` a bit more generic
cschwan Jun 11, 2024
5148a6c
Fix bug from commit ed4cbc2
cschwan Jun 11, 2024
f69ad05
add flag to choose the pdfset from which alphas is taken
t7phy Jun 12, 2024
cc01da9
add flag to choose the pdfset from which alphas is taken
t7phy Jun 12, 2024
f47b808
allow users to choose the conv_func for alphas
t7phy Jun 13, 2024
044c774
Use zero-based indexing for `use_alphas_from` parameter
cschwan Jun 13, 2024
be221cd
Use new struct `ConvFun` in most subcommands
cschwan Jun 15, 2024
cfcf027
Migrate `analyze` subcommand to use `ConvFun`
cschwan Jun 17, 2024
a507af8
Replace `ConvFun` with `ConvFuns`
cschwan Jun 17, 2024
1436618
Migrate subcommand `convolve` to `ConvFuns`
cschwan Jun 17, 2024
699851c
Remove switch `--pdf-with-scale-cov` from `uncert` subcommand
cschwan Jun 17, 2024
5f8e576
Migrate `uncert` to use `ConvFun`
cschwan Jun 18, 2024
0020bde
Fix main test
cschwan Jun 19, 2024
ab9bd08
Add `members` variable to `ConvFuns`
cschwan Jun 21, 2024
4ff0319
Add new helper function `create_conv_funs_for_set` and use it in `unc…
cschwan Jun 21, 2024
a29639b
Migrate `pull` subcommand to use `ConvFuns`
cschwan Jun 21, 2024
da7b948
Change help text of `--pull-from` in `pull`
cschwan Jun 21, 2024
8e0b172
Try to fix compilation error in CI
cschwan Jun 21, 2024
da1137f
Merge branch 'master' into ts050624
cschwan Jun 27, 2024
a981f7e
Add missing units for PDF uncertainties
cschwan Jun 27, 2024
1d4af96
Migrate parts of `plot` to `ConvFuns`
cschwan Jun 27, 2024
c59f38e
Remove obsolete helper functions
cschwan Jun 27, 2024
ddbf227
Migrate the remaining parts of `plot` to `ConvFuns`
cschwan Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pineappl_cli/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 @@ -87,7 +88,7 @@ impl Subcommand for CkfOpts {
lumi_mask[lumi] = true;
helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&[self.order],
&[],
&lumi_mask,
Expand All @@ -103,7 +104,7 @@ impl Subcommand for CkfOpts {
lumi_mask[lumi] = true;
helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&orders_den,
&[],
&lumi_mask,
Expand Down
12 changes: 6 additions & 6 deletions pineappl_cli/src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::helpers::{self, ConvoluteMode};
use super::helpers::{self, ConvFun, ConvoluteMode};
use super::{GlobalConfiguration, Subcommand};
use anyhow::Result;
use clap::builder::TypedValueParser;
Expand All @@ -14,9 +14,9 @@ pub struct Opts {
/// 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 of the PDF(s)/FF(s).
#[arg(num_args = 1, required = true, value_delimiter = ',')]
conv_funs: Vec<ConvFun>,
/// Show absolute numbers of each contribution.
#[arg(long, short)]
absolute: bool,
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct Opts {
impl Subcommand for Opts {
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)?;

let mut channels: Vec<_> = self.channels.iter().cloned().flatten().collect();
channels.sort_unstable();
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Subcommand for Opts {
channel_mask[channel] = true;
helpers::convolve(
&grid,
&mut pdf,
&mut conv_funs,
&self.orders,
&[],
&channel_mask,
Expand Down
5 changes: 3 additions & 2 deletions pineappl_cli/src/convolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use prettytable::{cell, Row};
use std::ops::RangeInclusive;
use std::path::PathBuf;
use std::process::ExitCode;
use std::slice;

/// Convolutes a PineAPPL grid with a PDF set.
#[derive(Parser)]
Expand Down Expand Up @@ -54,7 +55,7 @@ impl Subcommand for Opts {

let results = helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&self.orders,
&bins,
&[],
Expand Down Expand Up @@ -83,7 +84,7 @@ impl Subcommand for Opts {
let mut pdf = helpers::create_pdf(pdfset).unwrap();
helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&self.orders,
&bins,
&[],
Expand Down
9 changes: 5 additions & 4 deletions pineappl_cli/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use prettytable::{cell, Row};
use std::collections::HashSet;
use std::path::PathBuf;
use std::process::ExitCode;
use std::slice;

/// Compares the numerical content of two grids with each other.
#[derive(Parser)]
Expand Down Expand Up @@ -149,7 +150,7 @@ impl Subcommand for Opts {

let results1 = helpers::convolve(
&grid1,
&mut pdf,
slice::from_mut(&mut pdf),
&orders1,
&[],
&[],
Expand All @@ -159,7 +160,7 @@ impl Subcommand for Opts {
);
let results2 = helpers::convolve(
&grid2,
&mut pdf,
slice::from_mut(&mut pdf),
&orders2,
&[],
&[],
Expand Down Expand Up @@ -205,7 +206,7 @@ impl Subcommand for Opts {
.map(|&order| {
helpers::convolve(
&grid1,
&mut pdf,
slice::from_mut(&mut pdf),
&[order],
&[],
&[],
Expand All @@ -220,7 +221,7 @@ impl Subcommand for Opts {
.map(|&order| {
helpers::convolve(
&grid2,
&mut pdf,
slice::from_mut(&mut pdf),
&[order],
&[],
&[],
Expand Down
5 changes: 3 additions & 2 deletions pineappl_cli/src/evolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use pineappl::fk_table::FkTable;
use pineappl::grid::Grid;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::slice;

#[cfg(feature = "evolve")]
mod eko {
Expand Down Expand Up @@ -545,7 +546,7 @@ impl Subcommand for Opts {
let mut pdf = helpers::create_pdf(&self.pdfset)?;
let results = helpers::convolve_scales(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&self.orders,
&[],
&[],
Expand All @@ -565,7 +566,7 @@ impl Subcommand for Opts {
)?;
let evolved_results = helpers::convolve_scales(
fk_table.grid(),
&mut pdf,
slice::from_mut(&mut pdf),
&[],
&[],
&[],
Expand Down
3 changes: 2 additions & 1 deletion pineappl_cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use pineappl::boc::Order;
use pineappl::grid::Grid;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::slice;

#[cfg(feature = "applgrid")]
mod applgrid;
Expand Down Expand Up @@ -158,7 +159,7 @@ impl Subcommand for Opts {
let mut pdf = helpers::create_pdf(&self.pdfset)?;
let reference_results = helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&orders,
&[],
&[],
Expand Down
137 changes: 113 additions & 24 deletions pineappl_cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,48 @@ use pineappl::convolutions::LumiCache;
use pineappl::grid::Grid;
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
use prettytable::Table;
use std::convert::Infallible;
use std::fs::{File, OpenOptions};
use std::iter;
use std::ops::RangeInclusive;
use std::path::Path;
use std::process::ExitCode;
use std::str::FromStr;

#[derive(Clone)]
pub struct ConvFun {
lhapdf_name: String,
label: String,
}

impl FromStr for ConvFun {
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(),
label: arg.to_owned(),
},
|(lhapdf_name, label)| ConvFun {
lhapdf_name: lhapdf_name.to_owned(),
label: label.to_owned(),
},
))
}
}

pub fn create_conv_funs(funs: &[ConvFun]) -> Result<Vec<Pdf>> {
Ok(funs
.iter()
.map(|fun| {
fun.lhapdf_name.parse().map_or_else(
|_| Pdf::with_setname_and_nmem(&fun.lhapdf_name),
Pdf::with_lhaid,
)
})
.collect::<Result<_, _>>()?)
}

pub fn create_pdf(pdf: &str) -> Result<Pdf> {
let pdf = pdf.split_once('=').map_or(pdf, |(name, _)| name);
Expand Down Expand Up @@ -127,7 +164,7 @@ pub enum ConvoluteMode {

pub fn convolve_scales(
grid: &Grid,
lhapdf: &mut Pdf,
conv_funs: &mut [Pdf],
orders: &[(u32, u32)],
bins: &[usize],
channels: &[bool],
Expand All @@ -146,29 +183,81 @@ pub fn convolve_scales(
})
.collect();

// if the field 'Particle' is missing we assume it's a proton PDF
let pdf_pdg_id = lhapdf
.set()
.entry("Particle")
.map_or(Ok(2212), |string| string.parse::<i32>())
.unwrap();

if cfg.force_positive {
lhapdf.set_force_positive(1);
}

let x_max = lhapdf.x_max();
let x_min = lhapdf.x_min();
let mut pdf = |id, x, q2| {
if !cfg.allow_extrapolation && (x < x_min || x > x_max) {
0.0
} else {
lhapdf.xfx_q2(id, x, q2)
let mut results = match conv_funs {
[fun] => {
// if the field 'Particle' is missing we assume it's a proton PDF
let pdg_id = fun
.set()
.entry("Particle")
.map_or(Ok(2212), |string| string.parse::<i32>())
.unwrap();

if cfg.force_positive {
fun.set_force_positive(1);
}

let x_max = fun.x_max();
let x_min = fun.x_min();
let mut alphas = |q2| fun.alphas_q2(q2);
let mut fun = |id, x, q2| {
if !cfg.allow_extrapolation && (x < x_min || x > x_max) {
0.0
} else {
fun.xfx_q2(id, x, q2)
}
};

let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas);

grid.convolve(&mut cache, &orders, bins, channels, scales)
}
[fun1, fun2] => {
let pdg_id1 = fun1
.set()
.entry("Particle")
.map_or(Ok(2212), |string| string.parse::<i32>())
.unwrap();

let pdg_id2 = fun2
.set()
.entry("Particle")
.map_or(Ok(2212), |string| string.parse::<i32>())
.unwrap();

if cfg.force_positive {
fun1.set_force_positive(1);
fun2.set_force_positive(1);
}

let x_max1 = fun1.x_max();
let x_min1 = fun1.x_min();
let mut alphas1 = |q2| fun1.alphas_q2(q2);
let mut fun1 = |id, x, q2| {
if !cfg.allow_extrapolation && (x < x_min1 || x > x_max1) {
0.0
} else {
fun1.xfx_q2(id, x, q2)
}
};
let x_max2 = fun2.x_max();
let x_min2 = fun2.x_min();
// is the following line needed?
// let mut alphas2 = |q2| fun2.alphas_q2(q2);
let mut fun2 = |id, x, q2| {
if !cfg.allow_extrapolation && (x < x_min2 || x > x_max2) {
0.0
} else {
fun2.xfx_q2(id, x, q2)
}
};
felixhekhorn marked this conversation as resolved.
Show resolved Hide resolved

let mut cache =
LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas1);

grid.convolve(&mut cache, &orders, bins, channels, scales)
}
_ => unimplemented!(),
};
let mut alphas = |q2| lhapdf.alphas_q2(q2);
let mut cache = LumiCache::with_one(pdf_pdg_id, &mut pdf, &mut alphas);
let mut results = grid.convolve(&mut cache, &orders, bins, channels, scales);

match mode {
ConvoluteMode::Asymmetry => {
Expand Down Expand Up @@ -212,7 +301,7 @@ pub fn convolve_scales(

pub fn convolve(
grid: &Grid,
lhapdf: &mut Pdf,
conv_funs: &mut [Pdf],
orders: &[(u32, u32)],
bins: &[usize],
lumis: &[bool],
Expand All @@ -222,7 +311,7 @@ pub fn convolve(
) -> Vec<f64> {
convolve_scales(
grid,
lhapdf,
conv_funs,
orders,
bins,
lumis,
Expand Down
3 changes: 2 additions & 1 deletion pineappl_cli/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{Parser, ValueHint};
use pineappl::grid::Grid;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::slice;

#[cfg(feature = "applgrid")]
mod applgrid;
Expand Down Expand Up @@ -277,7 +278,7 @@ impl Subcommand for Opts {
let mut pdf = helpers::create_pdf(&self.pdfset)?;
let results = helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&[],
&[],
&[],
Expand Down
3 changes: 2 additions & 1 deletion pineappl_cli/src/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::{Parser, ValueHint};
use prettytable::{cell, Row};
use std::path::PathBuf;
use std::process::ExitCode;
use std::slice;

/// Shows the predictions for all bin for each order separately.
#[derive(Parser)]
Expand Down Expand Up @@ -66,7 +67,7 @@ impl Subcommand for Opts {
.map(|order| {
helpers::convolve(
&grid,
&mut pdf,
slice::from_mut(&mut pdf),
&[(order.alphas, order.alpha)],
&[],
&[],
Expand Down
Loading
Loading