CorrectionLib

2.8.0 · active · verified Sat Apr 11

CorrectionLib is a generic Python library providing a well-structured JSON data format for various ad-hoc correction factors commonly found in high-energy physics (HEP) analysis. It offers a companion evaluation tool suitable for use in C++ and Python programs, supporting multi-dimensional binned lookups, categorical maps, and formulas. The library is actively maintained, with its current version being 2.8.0, and releases occur regularly to introduce new features, improve compatibility, and address bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple 2D binned correction using CorrectionLib's JSON schema, save it to a file, and then load and evaluate it in Python. It shows how to retrieve a specific correction from a `CorrectionSet` and calculate the corrected value for given inputs.

import correctionlib
import os
import json

# Create a dummy JSON correction file content
correction_json_content = '''
{
  "schema_version": 2,
  "description": "Simple example corrections",
  "corrections": [
    {
      "version": 1,
      "name": "my_sf",
      "inputs": [
        {"name": "x", "type": "real"},
        {"name": "y", "type": "real"}
      ],
      "output": {"name": "scale_factor", "type": "real"},
      "data": {
        "nodetype": "binning",
        "input": "x",
        "edges": [0.0, 5.0, 10.0],
        "content": [
          {
            "nodetype": "binning",
            "input": "y",
            "edges": [0.0, 5.0, 10.0],
            "content": [
              0.9,
              1.0
            ]
          },
          {
            "nodetype": "binning",
            "input": "y",
            "edges": [0.0, 5.0, 10.0],
            "content": [
              1.1,
              1.2
            ]
          }
        ],
        "flow": "clamp"
      }
    }
  ]
}
'''

# Save the content to a temporary file
file_path = "example_correction.json"
with open(file_path, "w") as f:
    f.write(correction_json_content)

# Load the corrections from the file
corrections = correctionlib.CorrectionSet.from_file(file_path)

# Access a specific correction
my_correction = corrections["my_sf"]

# Evaluate the correction
# Example 1: x=3.0, y=2.0 (falls in first x-bin, first y-bin)
result1 = my_correction.evaluate(3.0, 2.0)
print(f"Correction for x=3.0, y=2.0: {result1}")

# Example 2: x=7.0, y=6.0 (falls in second x-bin, second y-bin)
result2 = my_correction.evaluate(7.0, 6.0)
print(f"Correction for x=7.0, y=6.0: {result2}")

# Clean up the temporary file
os.remove(file_path)

view raw JSON →