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 ("_noElectrons", "_noMuons", "_noLeptons", "_tauSeedEleRM"):
40 name = name.replace(suffix, "")
41 return name
42
43def get_calib_cfg_path(context: str, jetcollection: str) -> str:
44 """ Look up the calibration config file for a jet collection and context.
45 The config file itself holds any run-dependent settings, in 'RunX:' blocks. """
46 data = load_calib_cfg()
47
48 contexts = data.get(jetcollection)
49 if contexts is None or jetcollection == COMMONPATH_KEY:
50 known = [k for k in data if k != COMMONPATH_KEY]
51 raise KeyError(f"No calibrations listed in {CONFIG_FILE} for jet collection "
52 f"'{jetcollection}'. Known collections: {known}")
53
54 rel = contexts.get(context)
55 if rel is None:
56 raise KeyError(f"No '{context}' calibration listed in {CONFIG_FILE} for jet collection "
57 f"'{jetcollection}'. Known contexts: {list(contexts)}")
58
59 return full_calib_path(rel)
60
61def get_calib_cfg(context: str, jetcollection: str):
62 from JetCalibTools.JetCalibStepsConfig import load_yaml_cfg
63 return load_yaml_cfg(get_calib_cfg_path(context, jetcollection))
64
65# This method actually sets up the tool
66def defineJetCalibTool(jetdef, modspec):
67 from JetCalibTools.JetCalibStepsConfig import calibToolFromConfigFile
68
69 # Get the yaml file and calibration sequence
70 cfg, calibSeqKey = getJetCalibToolSettings(jetdef, modspec)
71 path_configFile = PathResolver.FindCalibFile(cfg)
72 toolname = "jetcalib_new_{0}_{1}".format(jetdef.basename,modspec)
73 jct = calibToolFromConfigFile(jetdef._cflags, path_configFile, toolname, calibSeqKey)
74
75 return jct
76
77# This method extends the basic config getter to specify the requisite jet
78# moments or other inputs
79def getJetCalibToolPrereqs(jetdef, modspec):
80 from JetCalibTools.JetCalibStepsConfig import load_yaml_cfg
81
82 cfg, _ = getJetCalibToolSettings(jetdef, modspec)
83 configDic = load_yaml_cfg(cfg)
84
85 prereqs = ["mod:ConstitFourMom"]
86 pvname = "PrimaryVertices" # this can be set dinamically in future
87
88 for step, step_config in configDic.items():
89 # JetArea
90 if step_config.get("DoJetArea", False):
91 if modspec.startswith("Trig"):
92 prereqs.append("input:HLT_EventDensity")
93 elif pvname == "PrimaryVertices_initial":
94 prereqs.append("input:EventDensityCustomVtxGNN")
95 elif pvname != "PrimaryVertices":
96 prereqs.append("input:EventDensityCustomVtx")
97 else:
98 prereqs.append(inputsFromContext("EventDensity")(jetdef))
99
100 # read prereqs from context or default config block
101 prereq_block = step_config.get("prereqs", {})
102 step_prereqs = prereq_block.get(modspec, prereq_block.get("default", []))
103 prereqs.extend(step_prereqs)
104
105 # remove duplication and keep order
106 seen = set()
107 prereqs_unique = []
108 for p in prereqs:
109 if p not in seen:
110 prereqs_unique.append(p)
111 seen.add(p)
112
113 return prereqs_unique
114
115# Get specific settings for JetCalibTools
116def getJetCalibToolSettings(jetdef, modspec):
117
118 calibspecs = modspec.split(':')
119
120 context = calibspecs[0] # T0/Trigger/etc. - used to extract calbration sequence from YAML config file
121
122 jetcollection = get_jet_collection_name(jetdef.basename)
123
124 cfg = get_calib_cfg_path(context, jetcollection)
125
126 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)