Skip to content

Commit

Permalink
Generalize channel macro
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Oct 28, 2024
1 parent 48d6f26 commit 1145cb7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- the macro `channel!` now accepts a channel specification that is of the
format `factor * (pid, ..) + ...`
- Python API: dropped top-level Python interface layer
- Python API: renamed `lumi` to `channel` in PyO3 Python interface. This
concerns 1) the argument names of `convolute_with_one` and similar functions;
Expand Down
33 changes: 19 additions & 14 deletions pineappl/src/boc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,12 @@ impl Channel {
/// use pineappl::boc::Channel;
/// use pineappl::channel;
///
/// let entry = channel![103, 11, 10.0].translate(&|evol_id| match evol_id {
/// let entry = channel![10.0 * (103, 11)].translate(&|evol_id| match evol_id {
/// 103 => vec![(2, 1.0), (-2, -1.0), (1, -1.0), (-1, 1.0)],
/// _ => vec![(evol_id, 1.0)],
/// });
///
/// assert_eq!(entry, channel![2, 11, 10.0; -2, 11, -10.0; 1, 11, -10.0; -1, 11, 10.0]);
/// assert_eq!(entry, channel![10.0 * (2, 11) + -10.0 * (-2, 11) + -10.0 * (1, 11) + 10.0 * (-1, 11)]);
/// ```
#[must_use]
pub fn translate(&self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self {
Expand Down Expand Up @@ -606,7 +606,7 @@ impl Channel {
/// use pineappl::channel;
/// use pineappl::boc::Channel;
///
/// let entry = channel![4, 4, 1.0; 2, 2, 1.0];
/// let entry = channel![1.0 * (4, 4) + 1.0 * (2, 2)];
///
/// assert_eq!(entry.entry(), [(vec![2, 2], 1.0), (vec![4, 4], 1.0)]);
/// ```
Expand Down Expand Up @@ -639,11 +639,11 @@ impl Channel {
/// ```rust
/// use pineappl::channel;
///
/// let ch1 = channel![2, 2, 2.0; 4, 4, 2.0];
/// let ch2 = channel![4, 4, 1.0; 2, 2, 1.0];
/// let ch3 = channel![3, 4, 1.0; 2, 2, 1.0];
/// let ch4 = channel![4, 3, 1.0; 2, 3, 2.0];
/// let ch5 = channel![2, 2, 1.0; 4, 4, 2.0];
/// let ch1 = channel![2.0 * (2, 2) + 2.0 * (4, 4)];
/// let ch2 = channel![1.0 * (4, 4) + 1.0 * (2, 2)];
/// let ch3 = channel![1.0 * (3, 4) + 1.0 * (2, 2)];
/// let ch4 = channel![1.0 * (4, 3) + 2.0 * (2, 3)];
/// let ch5 = channel![1.0 * (2, 2) + 2.0 * (4, 4)];
///
/// // ch1 is ch2 multiplied by two
/// assert_eq!(ch1.common_factor(&ch2), Some(2.0));
Expand Down Expand Up @@ -741,16 +741,21 @@ impl FromStr for Channel {
/// ```rust
/// use pineappl::channel;
///
/// let entry1 = channel![2, 2, 1.0; 4, 4, 1.0];
/// let entry2 = channel![4, 4, 1.0; 2, 2, 1.0];
/// let entry1 = channel![1.0 * (2, 2) + 1.0 * (4, 4)];
/// let entry2 = channel![1.0 * (4, 4) + 1.0 * (2, 2)];
///
/// assert_eq!(entry1, entry2);
/// ```
#[macro_export]
macro_rules! channel {
// TODO: generalize this to accept an arbitrary number of PIDs
($a:expr, $b:expr, $factor:expr $(; $c:expr, $d:expr, $fac:expr)*) => {
$crate::boc::Channel::new(vec![(vec![$a, $b], $factor), $((vec![$c, $d], $fac)),*])
($factor:literal * ($($pids:expr),+) $(+ $more_factors:literal * ($($more_pids:expr),+))*) => {
$crate::boc::Channel::new(
vec![
(vec![$($pids),+], $factor) $(,
(vec![$($more_pids),+], $more_factors)
)*
]
)
};
}

Expand Down Expand Up @@ -965,7 +970,7 @@ mod tests {
fn channel_from_str() {
assert_eq!(
str::parse::<Channel>(" 1 * ( 2 , -2) + 2* (4,-4)").unwrap(),
channel![2, -2, 1.0; 4, -4, 2.0]
channel![1.0 * (2, -2) + 2.0 * (4, -4)]
);

assert_eq!(
Expand Down
39 changes: 21 additions & 18 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,8 @@ mod tests {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
Expand All @@ -1690,8 +1690,8 @@ mod tests {
PidBasis::Pdg,
vec![
// differently ordered than `grid`
channel![1, 1, 1.0; 3, 3, 1.0],
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
],
vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
Expand All @@ -1718,8 +1718,8 @@ mod tests {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
Expand All @@ -1740,8 +1740,8 @@ mod tests {
let mut other = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![
Order::new(1, 2, 0, 0, 0),
Expand Down Expand Up @@ -1777,8 +1777,8 @@ mod tests {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
Expand All @@ -1798,7 +1798,10 @@ mod tests {

let mut other = Grid::new(
PidBasis::Pdg,
vec![channel![22, 22, 1.0], channel![2, 2, 1.0; 4, 4, 1.0]],
vec![
channel![1.0 * (22, 22)],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
vec![Conv::new(ConvType::UnpolPDF, 2212); 2],
Expand Down Expand Up @@ -1826,8 +1829,8 @@ mod tests {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5],
Expand All @@ -1849,8 +1852,8 @@ mod tests {
PidBasis::Pdg,
vec![
// channels are differently sorted
channel![1, 1, 1.0; 3, 3, 1.0],
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.5, 0.75, 1.0],
Expand Down Expand Up @@ -1878,7 +1881,7 @@ mod tests {
fn grid_convolutions() {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![channel![21, 21, 1.0]],
vec![channel![1.0 * (21, 21)]],
vec![Order {
alphas: 0,
alpha: 0,
Expand Down Expand Up @@ -1923,8 +1926,8 @@ mod tests {
let mut grid = Grid::new(
PidBasis::Pdg,
vec![
channel![2, 2, 1.0; 4, 4, 1.0],
channel![1, 1, 1.0; 3, 3, 1.0],
channel![1.0 * (2, 2) + 1.0 * (4, 4)],
channel![1.0 * (1, 1) + 1.0 * (3, 3)],
],
vec![Order::new(0, 2, 0, 0, 0)],
vec![0.0, 0.25, 0.5, 0.75, 1.0],
Expand Down
26 changes: 20 additions & 6 deletions pineappl/src/pids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ mod tests {
#[test]
fn inverse_inverse_evol() {
for pid in [-6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6] {
let result = &channel![pid, pid, 1.0]
let result = &channel![1.0 * (pid, pid)]
.translate(&pdg_mc_pids_to_evol)
.translate(&evol_to_pdg_mc_ids);

Expand Down Expand Up @@ -1031,14 +1031,28 @@ mod tests {

#[test]
fn translate() {
let channel = PidBasis::Evol.translate(PidBasis::Pdg, channel![103, 203, 2.0]);
let channel = PidBasis::Evol.translate(PidBasis::Pdg, channel![2.0 * (103, 203)]);

assert_eq!(
channel,
channel![ 2, 2, 2.0; 2, -2, -2.0; 2, 1, -2.0; 2, -1, 2.0;
-2, 2, 2.0; -2, -2, -2.0; -2, 1, -2.0; -2, -1, 2.0;
1, 2, -2.0; 1, -2, 2.0; 1, 1, 2.0; 1, -1, -2.0;
-1, 2, -2.0; -1, -2, 2.0; -1, 1, 2.0; -1, -1, -2.0]
channel![
2.0 * (2, 2)
+ -2.0 * (2, -2)
+ -2.0 * (2, 1)
+ 2.0 * (2, -1)
+ 2.0 * (-2, 2)
+ -2.0 * (-2, -2)
+ -2.0 * (-2, 1)
+ 2.0 * (-2, -1)
+ -2.0 * (1, 2)
+ 2.0 * (1, -2)
+ 2.0 * (1, 1)
+ -2.0 * (1, -1)
+ -2.0 * (-1, 2)
+ 2.0 * (-1, -2)
+ 2.0 * (-1, 1)
+ -2.0 * (-1, -1)
]
);
}
}
10 changes: 5 additions & 5 deletions pineappl/tests/drell_yan_lo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ fn hadronic_pspgen(rng: &mut impl Rng, mmin: f64, mmax: f64) -> Psp2to2 {
fn fill_drell_yan_lo_grid(rng: &mut impl Rng, calls: u32, dynamic: bool, reweight: bool) -> Grid {
let channels = vec![
// photons
channel![22, 22, 1.0],
channel![1.0 * (22, 22)],
// up-antiup
channel![2, -2, 1.0; 4, -4, 1.0],
channel![1.0 * (2, -2) + 1.0 * (4, -4)],
// antiup-up
channel![-2, 2, 1.0; -4, 4, 1.0],
channel![1.0 * (-2, 2) + 1.0 * (-4, 4)],
// down-antidown
channel![1, -1, 1.0; 3, -3, 1.0; 5, -5, 1.0],
channel![1.0 * (1, -1) + 1.0 * (3, -3) + 1.0 * (5, -5)],
// antidown-down
channel![-1, 1, 1.0; -3, 3, 1.0; -5, 5, 1.0],
channel![1.0 * (-1, 1) + 1.0 * (-3, 3) + 1.0 * (-5, 5)],
];

let orders = vec![
Expand Down
6 changes: 3 additions & 3 deletions pineappl_cli/src/import/fktable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Context, Result};
use flate2::read::GzDecoder;
use ndarray::s;
use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales};
use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales};
use pineappl::channel;
use pineappl::convolutions::{Conv, ConvType};
use pineappl::grid::Grid;
Expand Down Expand Up @@ -86,14 +86,14 @@ fn read_fktable(reader: impl BufRead) -> Result<Grid> {
.iter()
.enumerate()
.filter(|&(_, &value)| value)
.map(|(index, _)| channel![basis[index / 14], basis[index % 14], 1.0])
.map(|(index, _)| channel![1.0 * (basis[index / 14], basis[index % 14])])
.collect()
} else {
flavor_mask
.iter()
.enumerate()
.filter(|&(_, &value)| value)
.map(|(index, _)| Channel::new(vec![(vec![basis[index]], 1.0)]))
.map(|(index, _)| channel![1.0 * (basis[index])])
.collect()
};

Expand Down

0 comments on commit 1145cb7

Please sign in to comment.