Skip to content

Commit

Permalink
make kfactors working again with nnpdf settings
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomomagni committed Jun 10, 2024
1 parent c87c39e commit 41a4649
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
6 changes: 3 additions & 3 deletions docs/source/theory/kfactors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Kfactors
Another useful tool that `pineko` includes is ``pineko kfactor`` which allows the embedding of a kfactor
as a proper order in a grid. The usage is the following::

pineko kfactor GRIDS_FOLDER KFACTOR_FOLDER YAMLDB_FILE TARGET_FOLDER PTO_TO_UPDATE [--order_exists]
pineko kfactor THEORY_ID DATASET KFACTOR_FOLDER TARGET_FOLDER PTO_TO_UPDATE [--order_exists]

where:

- ``GRIDS_FOLDER`` is the folder containing the grids to update,
- ``THEORY_ID`` is the theory ID of the source grid,
- ``DATASET`` is the dataset name,
- ``KFACTOR_FOLDER`` is the folder containing the kfactor files,
- ``YAMLDB_FILE`` is the path to the yamldb file of the requested dataset,
- ``TARGET_FOLDER`` is the folder where the new updated grids is going to be saved,
- ``PTO_TO_UPDATE`` is the :math:`\alpha_s` perturbative order to update or create,
with the convention that ``LO=1``, ``NLO=2`` and so on, irrespectively to the powers of :math:`\alpha_s`.
Expand Down
19 changes: 10 additions & 9 deletions src/pineko/cli/kfactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
import click

from .. import kfactor
from ._base import command
from ._base import command, config_option, load_config


@command.command("kfactor")
@click.argument("grids_folder", type=click.Path(exists=True))
@config_option
@click.argument("theoryID", type=int)
@click.argument("dataset", type=str)
@click.argument("kfactor_folder", type=click.Path(exists=True))
@click.argument("yamldb_file", type=click.Path(exists=True))
@click.argument("target_folder", type=click.Path(exists=True))
@click.argument("pto_to_update", type=int)
@click.option("--order_exists", is_flag=True, help="Overwrite an existing order.")
def kfactor_inclusion(
grids_folder,
cfg,
theoryid,
dataset,
kfactor_folder,
yamldb_file,
target_folder,
pto_to_update,
order_exists,
):
"""Construct new grid with kfactor included."""
grids_folder = pathlib.Path(grids_folder)
load_config(cfg)
kfactor_folder = pathlib.Path(kfactor_folder)
yamldb_file = pathlib.Path(yamldb_file)
target_folder = pathlib.Path(target_folder)
kfactor.apply_to_dataset(
grids_folder,
theoryid,
dataset,
kfactor_folder,
yamldb_file,
pto_to_update,
target_folder=target_folder,
order_exists=order_exists,
Expand Down
71 changes: 38 additions & 33 deletions src/pineko/kfactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import yaml
from pineappl import import_only_subgrid

from . import scale_variations
from . import configs, fonll, scale_variations, utils
from .scale_variations import orders_as_tuple

DEFAULT_PDF_SET = "NNPDF40_nnlo_as_01180"
Expand Down Expand Up @@ -136,7 +136,7 @@ def construct_new_order(grid, order, order_to_update, central_kfactor, alphas):
new_grid = scale_variations.initialize_new_grid(grid, order_to_update)
original_order_index = grid_orders.index(order)

for lumi_index in range(len(new_grid.lumi())):
for lumi_index in range(len(new_grid.channels())):
for bin_index in range(grid.bins()):
subgrid = grid.subgrid(original_order_index, bin_index, lumi_index)
mu2_ren_grid = [mu2.ren for mu2 in subgrid.mu2_grid()]
Expand Down Expand Up @@ -287,9 +287,9 @@ def to_list(grid, central_kfactors):


def apply_to_dataset(
grids_folder,
theoryid,
dataset,
kfactor_folder,
yamldb_path,
pto_to_update,
target_folder,
order_exists=False,
Expand All @@ -298,8 +298,10 @@ def apply_to_dataset(
Parameters
----------
grids_folder : pathlib.Path()
pineappl grids folder
theoryid : int
theory ID of the source grid
dataset : str
datset name
kfactor_folder : pathlib.Path()
kfactors folder
yamldb_path : pathlib.Path()
Expand All @@ -315,30 +317,33 @@ def apply_to_dataset(
import lhapdf # pylint: disable=import-error,import-outside-toplevel

# Extracting info from yaml file
with open(yamldb_path, encoding="utf-8") as f:
yamldict = yaml.safe_load(f)

# loop on operands
for grid_list in yamldict["operands"]:
# loop on grids
for grid in grid_list:
# TODO: generalize for other type of kfactors ?
kfactor_path = kfactor_folder / f"CF_QCD_{grid}.dat"
if "ATLASDY2D8TEV" in grid:
kfactor_path = kfactor_folder / f"CF_QCDEWK_{grid}.dat"

grid_name = f"{grid}.pineappl.lz4"
current_grid = pineappl.grid.Grid.read(grids_folder / grid_name)

central_kfactor, pdf_set = read_from_file(kfactor_path)
central_kfactor_filtered = to_list(current_grid, central_kfactor)
alphas = lhapdf.mkAlphaS(pdf_set)

apply_to_grid(
central_kfactor_filtered,
alphas,
current_grid,
pto_to_update,
target_folder / grid_name,
order_exists,
)
grid_list = utils.read_grids_from_nnpdf(dataset, configs.configs)
if grid_list is None:
grid_list = fonll.grids_names(
configs.configs["paths"]["ymldb"] / f"{dataset}.yaml"
)

# loop on grids_name
for grid in grid_list:
# TODO: generalize for other type of kfactors ?
grid_name = grid.split(".")[0]
kfactor_path = kfactor_folder / f"CF_QCD_{grid_name}.dat"
if "ATLASDY2D8TEV" in grid:
kfactor_path = kfactor_folder / f"CF_QCDEWK_{grid_name}.dat"

current_grid = pineappl.grid.Grid.read(
configs.configs["paths"]["grids"] / str(theoryid) / grid
)

central_kfactor, pdf_set = read_from_file(kfactor_path)
central_kfactor_filtered = to_list(current_grid, central_kfactor)
alphas = lhapdf.mkAlphaS(pdf_set)

apply_to_grid(
central_kfactor_filtered,
alphas,
current_grid,
pto_to_update,
target_folder / grid,
order_exists,
)
4 changes: 2 additions & 2 deletions src/pineko/scale_variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def initialize_new_grid(grid, new_order):
bin_limits = [
float(bin) for bin in range(grid.bins() + 1)
] # The +1 explanation is that n bins have n+1 bin limits, and range generates numbers from a half-open interval (range(n) generates n numbers).
lumi_grid = [pineappl.lumi.LumiEntry(mylum) for mylum in grid.lumi()]
lumi_grid = [pineappl.lumi.LumiEntry(mylum) for mylum in grid.channels()]
subgrid_params = pineappl.subgrid.SubgridParams()
new_order = [pineappl.grid.Order(*new_order)]
# create new_grid with same lumi and bin_limits of the original grid but with new_order
Expand Down Expand Up @@ -179,7 +179,7 @@ def construct_and_dump_order_exists_grid(ori_grid, to_construct_order):
"""
# TODO: can we make this function simpler ??
bin_limits = [float(bin) for bin in range(ori_grid.bins() + 1)]
lumi_grid = [pineappl.lumi.LumiEntry(mylum) for mylum in ori_grid.lumi()]
lumi_grid = [pineappl.lumi.LumiEntry(mylum) for mylum in ori_grid.channels()]
subgrid_params = pineappl.subgrid.SubgridParams()
ori_grid_orders = orders_as_tuple(ori_grid)
new_orders = [
Expand Down

0 comments on commit 41a4649

Please sign in to comment.