Skip to content

Commit

Permalink
Add --rewrite-channel feature for write subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Dec 8, 2023
1 parent 40759ce commit e006c95
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pineappl_cli/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum OpsArg {
Remap(String),
RemapNorm(f64),
RemapNormIgnore(Vec<usize>),
RewriteChannel((usize, LumiEntry)),
Scale(f64),
ScaleByBin(Vec<f64>),
ScaleByOrder(Vec<f64>),
Expand Down Expand Up @@ -179,6 +180,22 @@ impl FromArgMatches for MoreArgs {
});
}
}
"rewrite_channel" => {
let arguments: Vec<Vec<String>> = matches
.remove_occurrences(&id)
.unwrap()
.map(Iterator::collect)
.collect();

for (index, arg) in indices.into_iter().zip(arguments.into_iter()) {
assert_eq!(arg.len(), 2);

args[index] = Some(OpsArg::RewriteChannel((
str::parse(&arg[0]).unwrap(),
str::parse(&arg[1]).unwrap(),
)));
}
}
"scale_by_bin" | "scale_by_order" => {
let arguments: Vec<Vec<_>> = matches
.remove_occurrences(&id)
Expand Down Expand Up @@ -341,6 +358,14 @@ impl Args for MoreArgs {
.value_name("DIM1,...")
.value_parser(value_parser!(usize)),
)
.arg(
Arg::new("rewrite_channel")
.action(ArgAction::Append)
.help("Rewrite the definition of the channel with index IDX")
.long("rewrite-channel")
.num_args(2)
.value_names(["IDX", "CHAN"])
)
.arg(
Arg::new("scale")
.action(ArgAction::Append)
Expand Down Expand Up @@ -522,6 +547,12 @@ impl Subcommand for Opts {
BinRemapper::new(normalizations, remapper.limits().to_vec()).unwrap(),
)?;
}
OpsArg::RewriteChannel((index, new_channel)) => {
let mut channels = grid.lumi().to_vec();
// TODO: check that `index` is valid
channels[*index] = new_channel.clone();
grid.set_lumis(channels);
}
OpsArg::Scale(factor) => grid.scale(*factor),
OpsArg::Optimize(true) => grid.optimize(),
OpsArg::OptimizeFkTable(assumptions) => {
Expand Down
67 changes: 67 additions & 0 deletions pineappl_cli/tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Options:
--remap <REMAPPING> Modify the bin dimensions and widths
--remap-norm <NORM> Modify the bin normalizations with a common factor
--remap-norm-ignore <DIM1,...> Modify the bin normalizations by multiplying with the bin lengths for the given dimensions
--rewrite-channel <IDX> <CHAN> Rewrite the definition of the channel with index IDX
-s, --scale <SCALE> Scales all grids with the given factor
--scale-by-bin <BIN1,BIN2,...> Scale each bin with a different factor
--scale-by-order <AS,AL,LR,LF> Scales all grids with order-dependent factors
Expand Down Expand Up @@ -146,6 +147,28 @@ const REMAP_STR: &str = "b etal x2 x3 dsig/detal
const REMAP_NO_REMAPPER_STR: &str = "Error: grid does not have a remapper
";

const REWRITE_CHANNELS_CONVOLUTE_STR: &str = "b etal dsig/detal
[] [pb]
-+----+----+-----------
0 2 2.25 7.5534392e2
1 2.25 2.5 6.9342538e2
2 2.5 2.75 6.0526279e2
3 2.75 3 4.9140786e2
4 3 3.25 3.6782869e2
5 3.25 3.5 2.5085041e2
6 3.5 4 1.1874486e2
7 4 4.5 2.8214633e1
";

const REWRITE_CHANNELS_LUMIS_STR: &str = "l entry entry entry entry entry entry
-+--------------------------------+-------------------------------+-----------------------+--------------------------------+-----------------------+---------------------
0 0.0000128881 × ( 2, -5) 0.050940490000000005 × ( 2, -3) 0.9490461561 × ( 2, -1) 0.0017222500000000003 × ( 4, -5) 0.9473907556 × ( 4, -3) 0.05089536 × ( 4, -1)
1 0.0017351381000000003 × (-5, 21) 0.9983312456 × (-3, 21) 0.9999415161 × (-1, 21)
2 1 × (22, -3) 1 × (22, -1)
3 0.9999995342 × ( 2, 21) 1.0000083656 × ( 4, 21)
4 1 × ( 2, 22) 1 × ( 4, 22)
";

const SCALE_BY_BIN_STR: &str = "b etal dsig/detal
[] [pb]
-+----+----+-----------
Expand Down Expand Up @@ -659,3 +682,47 @@ fn multiple_arguments() {
.success()
.stdout(MULTIPLE_ARGUMENTS_STR);
}

#[test]
fn rewrite_channels() {
let output = NamedTempFile::new("ckm_channels.pineappl.lz4").unwrap();

// 0 1 × ( 2, -1) 1 × ( 4, -3)
// 1 1 × (21, -3) 1 × (21, -1)
// 2 1 × (22, -3) 1 × (22, -1)
// 3 1 × ( 2, 21) 1 × ( 4, 21)
// 4 1 × ( 2, 22) 1 × ( 4, 22)

Command::cargo_bin("pineappl")
.unwrap()
.args([
"write",
"--rewrite-channel", "0", "0.9490461561 * ( 2, -1) + 0.050940490000000005 * (2, -3) + 0.0000128881 * (2, -5) + 0.05089536 * (4, -1) + 0.9473907556 * (4, -3) + 0.0017222500000000003 * (4, -5)",
"--rewrite-channel", "1", "0.9999415161 * (-1, 21) + 0.9983312456 * (-3, 21) + 0.0017351381000000003 * (-5, 21)",
"--rewrite-channel", "3", "0.9999995342 * ( 2, 21) + 1.0000083656 * ( 4, 21)",
"../test-data/LHCB_WP_7TEV.pineappl.lz4",
output.path().to_str().unwrap(),
])
.assert()
.success()
.stdout("");

Command::cargo_bin("pineappl")
.unwrap()
.args(["read", "--lumis", output.path().to_str().unwrap()])
.assert()
.success()
.stdout(REWRITE_CHANNELS_LUMIS_STR);

Command::cargo_bin("pineappl")
.unwrap()
.args([
"--silence-lhapdf",
"convolute",
output.path().to_str().unwrap(),
"NNPDF31_nlo_as_0118_luxqed",
])
.assert()
.success()
.stdout(REWRITE_CHANNELS_CONVOLUTE_STR);
}

0 comments on commit e006c95

Please sign in to comment.