ATLAS Offline Software
Loading...
Searching...
No Matches
JetCalibToolsCfg.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3from JetRecConfig.StandardJetConstits import inputsFromContext
4from PathResolver import PathResolver
5
6from AthenaCommon import Logging
7jetcaliblog = Logging.logging.getLogger('JetCalibToolsConfig')
8
9
10all = ['getJetCalibTool']
11
12import yaml
13from functools import lru_cache
14
15# Index mapping jet collection and calibration context to the config file
16CONFIG_FILE = "JetCalibTools/calibDict.yaml"
17
18# Optional: once the config files are distributed via cvmfs
19# it can be dropped from the yaml and the paths left for PathResolver.
20COMMONPATH_KEY = "commonPath"
21
22@lru_cache(maxsize=1)
24 path = PathResolver.FindCalibFile(CONFIG_FILE)
25 if not path:
26 raise FileNotFoundError("Could not locate %s via PathResolver" % CONFIG_FILE)
27 with open(path, "r", encoding="utf-8") as f:
28 return yaml.safe_load(f)
29
30def full_calib_path(rel_path: str) -> str:
31 commonPath = load_calib_cfg().get(COMMONPATH_KEY)
32 if not commonPath or rel_path.startswith("/"):
33 return rel_path
34 return commonPath.rstrip("/") + "/" + rel_path
35
36def get_jet_collection_name(name: str) -> str:
37 # For some specific jet collections, e.g. lepton-free PFlow jets,
38 # we want to apply the calibrations of the default PFlow jets
39 for suffix in (
40 "_noElectrons", "_noMuons", "_noLeptons",
41 "_inclMuons", "_tauSeedEleRM",
42 "ByVertex", "CustomVtxGNN", "CustomVtx",
43 ):
44 name = name.replace(suffix, "")
45 return name
46
47def get_calib_cfg_path(context: str, jetcollection: str) -> str:
48 """ Look up the calibration config file for a jet collection and context.
49 The config file itself holds any run-dependent settings, in 'RunX:' blocks. """
50 data = load_calib_cfg()
51
52 contexts = data.get(jetcollection)
53 if contexts is None or jetcollection == COMMONPATH_KEY:
54 known = [k for k in data if k != COMMONPATH_KEY]
55 raise KeyError(f"No calibrations listed in {CONFIG_FILE} for jet collection "
56 f"'{jetcollection}'. Known collections: {known}")
57
58 rel = contexts.get(context)
59 if rel is None:
60 raise KeyError(f"No '{context}' calibration listed in {CONFIG_FILE} for jet collection "
61 f"'{jetcollection}'. Known contexts: {list(contexts)}")
62
63 return full_calib_path(rel)
64
65def get_calib_cfg(context: str, jetcollection: str):
66 from JetCalibTools.JetCalibStepsConfig import load_yaml_cfg
67 return load_yaml_cfg(get_calib_cfg_path(context, jetcollection))
68
69# This method actually sets up the tool
70def defineJetCalibTool(jetdef, modspec):
71 from JetCalibTools.JetCalibStepsConfig import calibToolFromConfigFile
72
73 # Get the yaml file and calibration sequence
74 cfg, calibSeqKey = getJetCalibToolSettings(jetdef, modspec)
75 path_configFile = PathResolver.FindCalibFile(cfg)
76 toolname = "jetcalib_new_{0}_{1}".format(jetdef.basename,modspec)
77 jct = calibToolFromConfigFile(jetdef._cflags, path_configFile, toolname, calibSeqKey)
78
79 return jct
80
81# This method extends the basic config getter to specify the requisite jet
82# moments or other inputs
83def getJetCalibToolPrereqs(jetdef, modspec):
84 from JetCalibTools.JetCalibStepsConfig import load_yaml_cfg
85
86 cfg, _ = getJetCalibToolSettings(jetdef, modspec)
87 configDic = load_yaml_cfg(cfg)
88
89 prereqs = ["mod:ConstitFourMom"]
90 pvname = "PrimaryVertices" # this can be set dinamically in future
91
92 for step, step_config in configDic.items():
93 # JetArea
94 if step_config.get("DoJetArea", False):
95 if modspec.startswith("Trig"):
96 prereqs.append("input:HLT_EventDensity")
97 elif pvname == "PrimaryVertices_initial":
98 prereqs.append("input:EventDensityCustomVtxGNN")
99 elif pvname != "PrimaryVertices":
100 prereqs.append("input:EventDensityCustomVtx")
101 else:
102 prereqs.append(inputsFromContext("EventDensity")(jetdef))
103
104 # read prereqs from context or default config block
105 prereq_block = step_config.get("prereqs", {})
106 step_prereqs = prereq_block.get(modspec, prereq_block.get("default", []))
107 prereqs.extend(step_prereqs)
108
109 # remove duplication and keep order
110 seen = set()
111 prereqs_unique = []
112 for p in prereqs:
113 if p not in seen:
114 prereqs_unique.append(p)
115 seen.add(p)
116
117 return prereqs_unique
118
119# Get specific settings for JetCalibTools
120def getJetCalibToolSettings(jetdef, modspec):
121
122 calibspecs = modspec.split(':')
123
124 context = calibspecs[0] # T0/Trigger/etc. - used to extract calbration sequence from YAML config file
125
126 jetcollection = get_jet_collection_name(jetdef.basename)
127
128 cfg = get_calib_cfg_path(context, jetcollection)
129
130 return cfg, context
static std::string FindCalibFile(const std::string &logical_file_name)
STL class.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
str full_calib_path(str rel_path)
getJetCalibToolPrereqs(jetdef, modspec)
getJetCalibToolSettings(jetdef, modspec)
get_calib_cfg(str context, str jetcollection)
str get_jet_collection_name(str name)
str get_calib_cfg_path(str context, str jetcollection)
defineJetCalibTool(jetdef, modspec)