ATLAS Offline Software
METRecoCfg.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
4 from AthenaConfiguration.ComponentFactory import CompFactory
5 
6 from AthenaCommon import Logging
7 metlog = Logging.logging.getLogger('METConfig')
8 
9 
11 
12 defaultInputKey = {
13  'Ele' :'Electrons',
14  'Gamma' :'Photons',
15  'Tau' :'TauJets',
16  'Jet' :'AntiKt4EMTopoJets',
17  'Muon' :'Muons',
18  'SoftTrk' :'InDetTrackParticles',
19  'SoftClus' :'CaloCalTopoClusters',
20  'SoftPFlow':'JetETMissNeutralParticleFlowObjects',
21  'PrimaryVx':'PrimaryVertices',
22  'Truth' :'TruthEvents',
23  'Calo' :'AllCalo',
24  'LCOCSoftClus':'LCOriginTopoClusters',
25  'EMOCSoftClus':'EMOriginTopoClusters',
26  }
27 
28 defaultOutputKey = {
29  'Ele' :'RefEle',
30  'Gamma' :'RefGamma',
31  'Tau' :'RefTau',
32  'Jet' :'RefJet',
33  'Muon' :'Muons',
34  'SoftTrk' :'SoftTrk',
35  'SoftClus' :'SoftClus',
36  'SoftPFlow':'SoftPFlow',
37  'Total' :'Final',
38  'Truth' :'Truth',
39  'Calo' :'Calo'
40  }
41 
42 prefix = 'METRecoConfig: '
43 
44 
46 
48  def __init__(self,objType='',outputKey='',inputKey=''):
49  self.objType = objType
50  self.outputKey = outputKey
51  self.inputKey = inputKey
52 
53 def getBuilder(config,suffix,doTracks,doCells,doTriggerMET,doOriginCorrClus):
54  tool = None
55  # Construct tool and set defaults for case-specific configuration
56  if config.objType == 'SoftTrk':
57  tool = CompFactory.getComp("met::METSoftTermsTool")('MET_SoftTrkTool_'+suffix)
58  tool.InputComposition = 'Tracks'
59  tool.TrackKey = defaultInputKey[config.objType]
60  config.inputKey = defaultInputKey[config.objType]
61  if config.objType.endswith('SoftClus'):
62  tool = CompFactory.getComp("met::METSoftTermsTool")('MET_SoftClusTool_'+suffix)
63  tool.InputComposition = 'Clusters'
64  tool.CaloClusterKey = defaultInputKey[config.objType]
65  config.inputKey = defaultInputKey[config.objType]
66  if config.objType == 'SoftPFlow':
67  tool = CompFactory.getComp("met::METSoftTermsTool")('MET_SoftPFlowTool_'+suffix)
68  tool.InputComposition = 'PFlow'
69  pfotool = CompFactory.RetrievePFOTool('MET_PFOTool_'+suffix)
70  tool.PFOTool = pfotool
71  if suffix == 'Truth':
72  tool = CompFactory.getComp("met::METTruthTool")('MET_TruthTool_'+config.objType)
73  tool.InputComposition = config.objType
74  tool.InputCollection = defaultInputKey['Truth']
75  config.inputKey = defaultInputKey['Truth']
76  config.outputKey = config.objType
77  if suffix == 'Calo':
78  tool = CompFactory.getComp("met::METCaloRegionsTool")('MET_CaloRegionsTool')
79  if doCells:
80  tool.UseCells = True
81  tool.DoTriggerMET = doTriggerMET
82  tool.CaloCellKey = defaultInputKey['Calo']
83  config.inputKey = defaultInputKey['Calo']
84  else:
85  tool.UseCells = False
86  tool.DoTriggerMET = False
87  tool.CaloClusterKey = defaultInputKey['SoftClus']
88  config.inputKey = defaultInputKey['SoftClus']
89  config.outputKey = config.objType
90 
91  # set input/output key names
92  if config.inputKey == '':
93  tool.InputCollection = defaultInputKey[config.objType]
94  config.inputKey = tool.InputCollection
95  elif hasattr(tool, 'InputCollection'):
96  tool.InputCollection = config.inputKey
97  if not suffix=='Calo':
98  if config.outputKey == '':
99  tool.MissingETKey = defaultOutputKey[config.objType]
100  config.outputKey = tool.MissingETKey
101  else:
102  tool.MissingETKey = config.outputKey
103  return tool
104 
105 
107 
108 class RefConfig:
109  def __init__(self,myType='',outputKey=''):
110  self.type = myType
111  self.outputKey = outputKey
112 
113 def getRefiner(flags,config,suffix,trkseltool=None,trkvxtool=None,trkisotool=None,caloisotool=None):
114  tool = None
115 
116  if config.type == 'TrackFilter':
117  tool = CompFactory.getComp("met::METTrackFilterTool")('MET_TrackFilterTool_'+suffix)
118  tool.InputPVKey = defaultInputKey['PrimaryVx']
119  tool.TrackSelectorTool=trkseltool
120  tool.TrackVxAssocTool=trkvxtool
121  #
122  tool.UseIsolationTools = False #True
123  tool.TrackIsolationTool = trkisotool
124  tool.CaloIsolationTool = caloisotool
125  #
126  tool.DoPVSel = flags.MET.UseTracks
127  tool.DoVxSep = flags.MET.UseTracks
128  tool.MissingETKey = config.outputKey
129  return tool
130 
131 
133 
134 def getRegions(config,suffix):
135  if suffix == 'Truth':
136  config.outputKey = config.objType
137  tool = CompFactory.getComp("met::METRegionsTool")('MET_'+config.outputKey+'Regions_'+suffix)
138  tool.InputMETContainer = 'MET_'+suffix
139  tool.InputMETMap = 'METMap_'+suffix
140  tool.InputMETKey = config.outputKey
141  tool.RegionValues = [ 1.5, 3.2, 10 ]
142  return tool
143 
144 
146 
147 class METConfig:
148  def outputCollection(self):
149  return 'MET_'+self.suffix
150  #
151  def outputMap(self):
152  return 'METMap_'+self.suffix
153  #
154  def setupBuilders(self,buildconfigs):
155  metlog.info("{} Setting up builders for MET config {}".format(prefix,self.suffix))
156  for config in buildconfigs:
157  if config.objType in self.builders:
158  metlog.error("{} Config {} already contains a builder of type {}".format(prefix,self.suffix,config.objType))
159  raise LookupError
160  else:
161  builder = getBuilder(config,self.suffix,self.doTracks,self.doCells,
162  self.doTriggerMET,self.doOriginCorrClus)
163  self.builders[config.objType] = builder
164  self.buildlist.append(builder)
165  metlog.info("{} Added {} tool named {}".format(prefix,config.objType,builder.name))
166  #
167  def setupRefiners(self,flags,refconfigs):
168  metlog.info("{} Setting up refiners for MET config {}".format(prefix,self.suffix))
169  for config in refconfigs:
170  # need to enforce this?
171  if config.type in self.refiners:
172  metlog.error("Config {} already contains a refiner of type {}".format(self.suffix,config.type))
173  raise LookupError
174  else:
175  refiner = getRefiner(flags, config=config,suffix=self.suffix,
176  trkseltool=self.trkseltool,trkvxtool=self.trkvxtool,
177  trkisotool=self.trkisotool,caloisotool=self.caloisotool)
178  self.refiners[config.type] = refiner
179  self.reflist.append(refiner)
180  metlog.info("{} Added {} tool named {}".format(prefix,config.type,refiner.name))
181  #
182  def setupRegions(self,buildconfigs):
183  metlog.info("{} Setting up regions for MET config {}".format(prefix,self.suffix))
184  for config in buildconfigs:
185  if config.objType in self.regions:
186  metlog.error("{} Config {} already contains a region tool of type {}".format(prefix,self.suffix,config.objType))
187  raise LookupError
188  else:
189  regions = getRegions(config,self.suffix)
190  self.regions[config.objType] = regions
191  self.reglist.append(regions)
192  metlog.info("{} Added {} region tool named {}".format(prefix,config.objType,regions.name))
193  #
194  def __init__(self,suffix,inputFlags,buildconfigs=[],refconfigs=[],
195  doTracks=False,doSum=False,doRegions=False,
196  doCells=False,doTriggerMET=True,duplicateWarning=True,
197  doOriginCorrClus=False):
198  metlog.info("{} Creating MET config {}".format(prefix,suffix))
200  self.suffix = suffix
201  self.doSum = doSum
202  self.doTracks = doTracks
203  self.doRegions = doRegions
204  self.doCells = doCells,
205  self.doOriginCorrClus = doOriginCorrClus
206  self.doTriggerMET = doTriggerMET
207  self.duplicateWarning = duplicateWarning
208  #
209  self.builders = {}
210  self.buildlist = [] # need an ordered list
211  #
212  self.refiners = {}
213  self.reflist = [] # need an ordered list
214  #
215  self.regions = {}
216  self.reglist = [] # need an ordered list
217  if doRegions:
218  self.setupRegions(buildconfigs)
219  #
220  if self.suffix != 'Truth':
221  # TODO: These Z0 and D0 cuts are left over from R21. The track vertex association can now use looser ones.
222  # To be investigated and possibly updated by the MET group.
223  self.trkseltool=CompFactory.getComp("InDet::InDetTrackSelectionTool")("IDTrkSel_MET",
224  CutLevel="TightPrimary",
225  maxZ0SinTheta=3,
226  maxD0=2,
227  minPt=500)
228  #
229  from TrackVertexAssociationTool.TrackVertexAssociationToolConfig import TTVAToolCfg
230  self.trkvxtool = self.accumulator.popToolsAndMerge(
231  TTVAToolCfg(inputFlags, "TrackVertexAssociationTool_MET", addDecoAlg=True,
232  WorkingPoint="Nonprompt_All_MaxWeight", HardScatterLinkDeco=""))
233  #
234  self.trkisotool = CompFactory.getComp("xAOD::TrackIsolationTool")("TrackIsolationTool_MET")
235  self.trkisotool.TrackSelectionTool = self.trkseltool # As configured above
236 
237  from TrkConfig.AtlasExtrapolatorConfig import AtlasExtrapolatorCfg
238  extrapCfg = AtlasExtrapolatorCfg(inputFlags)
239  CaloExtensionTool= CompFactory.getComp("Trk::ParticleCaloExtensionTool")(Extrapolator = self.accumulator.popToolsAndMerge(extrapCfg))
240  CaloCellAssocTool = CompFactory.getComp("Rec::ParticleCaloCellAssociationTool")(ParticleCaloExtensionTool = CaloExtensionTool)
241  self.caloisotool = CompFactory.getComp("xAOD::CaloIsolationTool")("CaloIsolationTool_MET",
242  saveOnlyRequestedCorrections=True,
243  ParticleCaloExtensionTool = CaloExtensionTool,
244  ParticleCaloCellAssociationTool = CaloCellAssocTool)
245 
246  self.setupBuilders(buildconfigs)
247  self.setupRefiners(inputFlags,refconfigs)
248 
249 # Set up a top-level tool with mostly defaults
250 def getMETRecoTool(topconfig):
251  recoTool = CompFactory.getComp("met::METRecoTool")('MET_RecoTool_'+topconfig.suffix,
252  METBuilders = topconfig.buildlist,
253  METRefiners = topconfig.reflist,
254  METContainer = topconfig.outputCollection(),
255  METComponentMap = topconfig.outputMap(),
256  WarnIfDuplicate = topconfig.duplicateWarning)
257  if topconfig.doSum:
258  recoTool.METFinalName = defaultOutputKey['Total']
259 
260  return recoTool
261 
262 # Set up a METRecoTool that builds MET regions
263 def getRegionRecoTool(topconfig):
264  regTool = CompFactory.getComp("met::METRecoTool")('MET_RegionTool_'+topconfig.suffix,
265  METBuilders = [],
266  METRefiners = topconfig.reglist,
267  METContainer = topconfig.outputCollection()+'Regions',
268  METComponentMap = topconfig.outputMap()+'Regions',
269  WarnIfDuplicate = topconfig.duplicateWarning)
270  return regTool
271 
272 # Allow user to configure reco tools directly or get more default configurations
273 def getMETRecoAlg(algName='METReconstruction',configs={}):
274  recoTools = []
275  for key,conf in configs.items():
276  metlog.info("{} Generate METRecoTool for MET_{}".format(prefix,key))
277  recotool = getMETRecoTool(conf)
278  recoTools.append(recotool)
279  if conf.doRegions:
280  regiontool = getRegionRecoTool(conf)
281  recoTools.append(regiontool)
282  for tool in recoTools:
283  metlog.info("{} Added METRecoTool {} to alg {}".format(prefix,tool.name,algName))
284  recoAlg = CompFactory.getComp("met::METRecoAlg")(name=algName,RecoTools=recoTools)
285  return recoAlg
METRecoCfg.getRefiner
def getRefiner(flags, config, suffix, trkseltool=None, trkvxtool=None, trkisotool=None, caloisotool=None)
Definition: METRecoCfg.py:113
METRecoCfg.getMETRecoAlg
def getMETRecoAlg(algName='METReconstruction', configs={})
Definition: METRecoCfg.py:273
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
METRecoCfg.METConfig.doOriginCorrClus
doOriginCorrClus
Definition: METRecoCfg.py:202
METRecoCfg.METConfig.suffix
suffix
Definition: METRecoCfg.py:197
vtune_athena.format
format
Definition: vtune_athena.py:14
METRecoCfg.METConfig.regions
regions
Definition: METRecoCfg.py:212
METRecoCfg.METConfig.setupBuilders
def setupBuilders(self, buildconfigs)
Definition: METRecoCfg.py:154
METRecoCfg.RefConfig.__init__
def __init__(self, myType='', outputKey='')
Definition: METRecoCfg.py:109
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
METRecoCfg.METConfig.doRegions
doRegions
Definition: METRecoCfg.py:200
METRecoCfg.METConfig.setupRefiners
def setupRefiners(self, flags, refconfigs)
Definition: METRecoCfg.py:167
python.AtlasExtrapolatorConfig.AtlasExtrapolatorCfg
def AtlasExtrapolatorCfg(flags, name='AtlasExtrapolator')
Definition: AtlasExtrapolatorConfig.py:63
METRecoCfg.METConfig.__init__
def __init__(self, suffix, inputFlags, buildconfigs=[], refconfigs=[], doTracks=False, doSum=False, doRegions=False, doCells=False, doTriggerMET=True, duplicateWarning=True, doOriginCorrClus=False)
Definition: METRecoCfg.py:194
METRecoCfg.METConfig.refiners
refiners
Definition: METRecoCfg.py:209
TrackVertexAssociationToolConfig.TTVAToolCfg
def TTVAToolCfg(flags, name, addDecoAlg=True, VertexContName="PrimaryVertices", **kwargs)
Definition: TrackVertexAssociationToolConfig.py:21
METRecoCfg.METConfig.reglist
reglist
Definition: METRecoCfg.py:213
METRecoCfg.METConfig.trkseltool
trkseltool
Definition: METRecoCfg.py:220
METRecoCfg.METConfig.duplicateWarning
duplicateWarning
Definition: METRecoCfg.py:204
METRecoCfg.METConfig.buildlist
buildlist
Definition: METRecoCfg.py:207
METRecoCfg.BuildConfig.outputKey
outputKey
Definition: METRecoCfg.py:50
METRecoCfg.getBuilder
def getBuilder(config, suffix, doTracks, doCells, doTriggerMET, doOriginCorrClus)
Definition: METRecoCfg.py:53
METRecoCfg.METConfig.doTriggerMET
doTriggerMET
Definition: METRecoCfg.py:203
METRecoCfg.BuildConfig
Configuration of builders.
Definition: METRecoCfg.py:47
METRecoCfg.RefConfig.outputKey
outputKey
Definition: METRecoCfg.py:111
METRecoCfg.METConfig.reflist
reflist
Definition: METRecoCfg.py:210
METRecoCfg.METConfig.trkvxtool
trkvxtool
Definition: METRecoCfg.py:227
METRecoCfg.getRegions
def getRegions(config, suffix)
Region tools are a special case of refiners.
Definition: METRecoCfg.py:134
METRecoCfg.METConfig.outputCollection
def outputCollection(self)
Definition: METRecoCfg.py:148
METRecoCfg.METConfig.doCells
doCells
Definition: METRecoCfg.py:201
METRecoCfg.getRegionRecoTool
def getRegionRecoTool(topconfig)
Definition: METRecoCfg.py:263
METRecoCfg.BuildConfig.inputKey
inputKey
Definition: METRecoCfg.py:51
METRecoCfg.METConfig.doTracks
doTracks
Definition: METRecoCfg.py:199
METRecoCfg.BuildConfig.objType
objType
Definition: METRecoCfg.py:49
METRecoCfg.RefConfig.type
type
Definition: METRecoCfg.py:110
METRecoCfg.getMETRecoTool
def getMETRecoTool(topconfig)
Definition: METRecoCfg.py:250
METRecoCfg.METConfig.builders
builders
Definition: METRecoCfg.py:206
METRecoCfg.METConfig.setupRegions
def setupRegions(self, buildconfigs)
Definition: METRecoCfg.py:182
METRecoCfg.METConfig.accumulator
accumulator
Definition: METRecoCfg.py:196
METRecoCfg.BuildConfig.__init__
def __init__(self, objType='', outputKey='', inputKey='')
Definition: METRecoCfg.py:48
METRecoCfg.METConfig.trkisotool
trkisotool
Definition: METRecoCfg.py:231
METRecoCfg.METConfig.doSum
doSum
Definition: METRecoCfg.py:198
METRecoCfg.RefConfig
Configuration of refiners.
Definition: METRecoCfg.py:108
METRecoCfg.METConfig.caloisotool
caloisotool
Definition: METRecoCfg.py:238
METRecoCfg.METConfig.outputMap
def outputMap(self)
Definition: METRecoCfg.py:151
METRecoCfg.METConfig
Top level MET configuration.
Definition: METRecoCfg.py:147