ATLAS Offline Software
Loading...
Searching...
No Matches
EGammaVariableCorrectionConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
3from AthenaConfiguration.ComponentFactory import CompFactory
4from AthenaConfiguration.Enums import LHCPeriod
5from Campaigns.Utils import Campaign
6from PathResolver import PathResolver
7from AthenaCommon.Logging import logging
8
10 flags, name="ElectronVariableCorrectionTool", **kwargs):
11 """Configure the e/gamma variable correction tool for electrons"""
12 acc = ComponentAccumulator()
13 log = logging.getLogger("ElectronVariableCorrectionToolCfg")
14 # Can ultimately be configured differently between Run 2 and Run 3 configs
15 # TUNE27: e FUDGE FACTORS RUN2 FULL DATA, derived with rel 22.2
16 tuneFile = "EGammaVariableCorrection/TUNE27/ElVariableNominalCorrection.conf"
17 kwargs.setdefault("ConfigFile", tuneFile)
18 log.info("Setting as default FF file: %s", tuneFile)
19 acc.setPrivateTools(
20 CompFactory.ElectronPhotonVariableCorrectionTool(name, **kwargs))
21 return acc
22
24 flags, name="PhotonVariableCorrectionTool", **kwargs):
25 """Configure the e/gamma variable correction tool for photons"""
26 acc = ComponentAccumulator()
27 log = logging.getLogger("PhotonVariableCorrectionToolCfg")
28 # fallback tune file if there is no dedicated tuning
29 defaultTuneFile = "EGammaVariableCorrection/TUNE27E/ElPhVariableNominalCorrection.conf"
30 tuneFile = ""
31 mcCampaign = flags.Input.MCCampaign
32 runPeriod = flags.GeoModel.Run
33 if runPeriod is LHCPeriod.Run2:
34 # TUNE25: gamma FUDGE FACTORS RUN2 FULL DATA vs MC15-18, derived with r21.2
35 tuneFile = "EGammaVariableCorrection/TUNE25/ElPhVariableNominalCorrection.conf"
36 elif runPeriod is LHCPeriod.Run3:
37 if mcCampaign in [Campaign.MC23a, Campaign.MC23d]:
38 # TUNE27AD: gamma FUDGE FACTORS RUN3 2022-2023 vs MC23a-d, derived with r25
39 tuneFile = "EGammaVariableCorrection/TUNE27AD/ElPhVariableNominalCorrection.conf"
40 elif mcCampaign is Campaign.MC23e:
41 # TUNE27E: gamma FUDGE FACTORS RUN3 2024 vs MC23e, derived with r25
42 tuneFile = "EGammaVariableCorrection/TUNE27E/ElPhVariableNominalCorrection.conf"
43 else:
44 log.warning("No default FF file centrally provided for mc campaign %s", mcCampaign.value)
45 tuneFile = defaultTuneFile
46 else:
47 log.warning("No default FF file centrally provided for run period %s", runPeriod.value)
48 tuneFile = defaultTuneFile
49 log.info("Setting as default FF file: %s", tuneFile)
50 kwargs.setdefault("ConfigFile", tuneFile)
51 acc.setPrivateTools(
52 CompFactory.ElectronPhotonVariableCorrectionTool(name, **kwargs))
53 return acc
54
55
57 flags, name="PhotonVariableNFCorrectionTool",
58 forceFold=-1, **kwargs):
59 """Configure the Normalizing Flow-based photon shower shape correction tool"""
60 acc = ComponentAccumulator()
61
62 from AthenaConfiguration.Enums import LHCPeriod
63
64 if not flags.Input.isMC:
65 raise RuntimeError("ElectronPhotonVariableNFCorrectionToolCfg: "
66 "NF correction tool should not be called for data"
67 )
68
69 isFullSim = flags.Sim.ISF.Simulator.isFullSim()
70 isRun3 = flags.GeoModel.Run is LHCPeriod.Run3
71 isRun2 = flags.GeoModel.Run is LHCPeriod.Run2
72
73 if isFullSim and isRun3:
74 default_conf = "EGammaVariableCorrection/NF_y_TUNE1/Run3FS/ElectronPhotonVariableNFCorrectionTool.conf"
75 elif isFullSim and isRun2:
76 default_conf = "EGammaVariableCorrection/NF_y_TUNE1/Run2FS/ElectronPhotonVariableNFCorrectionTool.conf"
77 elif not isFullSim and isRun3:
78 default_conf = "EGammaVariableCorrection/NF_y_TUNE1/Run3AF3/ElectronPhotonVariableNFCorrectionTool.conf"
79 elif not isFullSim and isRun2:
80 # temporary the same Run3 AF3 models are applied to Run2 AF3
81 default_conf = "EGammaVariableCorrection/NF_y_TUNE1/Run3AF3/ElectronPhotonVariableNFCorrectionTool.conf"
82 else:
83 raise RuntimeError(
84 f"ElectronPhotonVariableNFCorrectionToolCfg: no NF correction config available for Run period {flags.GeoModel.Run} "
85 f"(isFullSim={isFullSim}). Only Run2 and Run3 are supported."
86 )
87
88 conf_key = kwargs.setdefault("ConfigFile", default_conf)
89 if forceFold>=0:
90 kwargs.setdefault("forceOneFold", True)
91
92 conf_file = PathResolver.FindCalibFile(conf_key)
93 if not conf_file:
94 raise RuntimeError(f"PathResolver cannot find {conf_key}")
95
96 # Parse NFolds and ONNXnamePattern from config file
97 n_folds = None
98 pattern = None
99 with open(conf_file, 'r') as f:
100 for line in f:
101 line = line.strip()
102 if not line or line.startswith('#'):
103 continue
104 key, _, value = line.partition(':')
105 key = key.strip()
106 value = value.strip()
107 if key == 'NFolds':
108 n_folds = int(value)
109 elif key == 'ONNXnamePattern':
110 pattern = value
111
112 if n_folds is None or pattern is None:
113 raise RuntimeError(f'NFolds or ONNXnamePattern not found in config: {conf_file}')
114
115 # Build forward and backward ONNX tools per fold
116 forward_tools = []
117 backward_tools = []
118 for i in range(n_folds):
119
120 if forceFold>=0 and i!=forceFold:
121 continue
122
123 fwd_session = CompFactory.AthOnnx.OnnxRuntimeSessionToolCPU(
124 f'NFCorrectionORTSessionToolForward_{i}',
125 ModelFileName=f'{pattern}_forward_{i}.onnx')
126 fwd_tool = CompFactory.AthOnnx.OnnxRuntimeInferenceTool(
127 f'NFCorrectionOnnxToolForward_{i}',
128 ORTSessionTool=fwd_session)
129 forward_tools.append(fwd_tool)
130
131 bwd_session = CompFactory.AthOnnx.OnnxRuntimeSessionToolCPU(
132 f'NFCorrectionORTSessionToolBackward_{i}',
133 ModelFileName=f'{pattern}_backward_{i}.onnx')
134 bwd_tool = CompFactory.AthOnnx.OnnxRuntimeInferenceTool(
135 f'NFCorrectionOnnxToolBackward_{i}',
136 ORTSessionTool=bwd_session)
137 backward_tools.append(bwd_tool)
138
139 kwargs.setdefault("OnnxInferenceToolsForward", forward_tools)
140 kwargs.setdefault("OnnxInferenceToolsBackward", backward_tools)
141
142 acc.setPrivateTools(
143 CompFactory.ElectronPhotonVariableNFCorrectionTool(name, **kwargs))
144 return acc
145
146
static std::string FindCalibFile(const std::string &logical_file_name)
ElectronVariableCorrectionToolCfg(flags, name="ElectronVariableCorrectionTool", **kwargs)
ElectronPhotonVariableNFCorrectionToolCfg(flags, name="PhotonVariableNFCorrectionTool", forceFold=-1, **kwargs)
PhotonVariableCorrectionToolCfg(flags, name="PhotonVariableCorrectionTool", **kwargs)