ATLAS Offline Software
BTagESD.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
4 from AthenaConfiguration.ComponentFactory import CompFactory
5 
6 from BTagging.BTagTrackAugmenterAlgConfig import BTagTrackAugmenterAlgCfg
7 from FlavorTagDiscriminants.BTagJetAugmenterAlgConfig import (
8  BTagJetAugmenterAlgCfg)
9 
10 from FlavorTagDiscriminants.FlavorTagNNConfig import FlavorTagNNCfg
11 
14 
15  #Delete BTagging container read from input ESD
16  AddressRemappingSvc, ProxyProviderSvc=CompFactory.getComps("AddressRemappingSvc","ProxyProviderSvc",)
17  AddressRemappingSvc = AddressRemappingSvc("AddressRemappingSvc")
18  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::JetAuxContainer#AntiKt4EMTopoJets.btaggingLink->AntiKt4EMTopoJets.btaggingLink_' + suffix]
19  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingContainer#BTagging_AntiKt4EMTopo->BTagging_AntiKt4EMTopo_' + suffix]
20  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingAuxContainer#BTagging_AntiKt4EMTopoAux.->BTagging_AntiKt4EMTopo_' + suffix+"Aux."]
21  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::VertexContainer#BTagging_AntiKt4EMTopoSecVtx->BTagging_AntiKt4EMTopoSecVtx_' + suffix]
22  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::VertexAuxContainer#BTagging_AntiKt4EMTopoSecVtxAux.->BTagging_AntiKt4EMTopoSecVtx_' + suffix+"Aux."]
23  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTagVertexContainer#BTagging_AntiKt4EMTopoJFVtx->BTagging_AntiKt4EMTopoJFVtx_' + suffix]
24  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTagVertexAuxContainer#BTagging_AntiKt4EMTopoJFVtxAux.->BTagging_AntiKt4EMTopoJFVtx_' + suffix+"Aux."]
25  acc.addService(AddressRemappingSvc)
26  acc.addService(ProxyProviderSvc(ProviderNames = [ "AddressRemappingSvc" ]))
27 
28  return acc
29 
30 def RenameHLTaggerCfg(JetCollection, Tagger, suffix):
32  AddressRemappingSvc, ProxyProviderSvc=CompFactory.getComps("AddressRemappingSvc","ProxyProviderSvc",)
33  AddressRemappingSvc = AddressRemappingSvc("AddressRemappingSvc")
34  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingAuxContainer#BTagging_' + JetCollection + '.' + Tagger + '_pu->BTagging_' + JetCollection + '.'+ Tagger + '_pu' + suffix]
35  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingAuxContainer#BTagging_' + JetCollection + '.' + Tagger + '_pc->BTagging_' + JetCollection + '.'+ Tagger + '_pc' + suffix]
36  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingAuxContainer#BTagging_' + JetCollection + '.' + Tagger + '_pb->BTagging_' + JetCollection + '.'+ Tagger + '_pb' + suffix]
37  AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::BTaggingAuxContainer#BTagging_' + JetCollection + '.' + Tagger + '_ptau->BTagging_' + JetCollection + '.'+ Tagger + '_ptau' + suffix]
38  acc.addService(AddressRemappingSvc)
39  acc.addService(ProxyProviderSvc(ProviderNames = [ "AddressRemappingSvc" ]))
40 
41  return acc
42 
43 
44 def BTagHLTaggersCfg(inputFlags, JetCollection = []):
45  acc = ComponentAccumulator()
46 
47  # Nothing written out
48  from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
49  acc.merge(OutputStreamCfg(cfgFlags,"ESD", ItemList=[]))
50 
51  DeprecatedHLTaggers = ['rnnip', 'DL1']
52  postTagDL2JetToTrainingMap={
53  'AntiKt4EMPFlow': [
54  #'BTagging/201903/smt/antikt4empflow/network.json',
55  'BTagging/201903/rnnip/antikt4empflow/network.json',
56  'BTagging/201903/dl1r/antikt4empflow/network.json',
57  'BTagging/201903/dl1/antikt4empflow/network.json',
58  #'BTagging/201903/dl1rmu/antikt4empflow/network.json',
59  ]
60  }
61  for jet in JetCollection:
62  if jet in postTagDL2JetToTrainingMap:
63  for tagger in DeprecatedHLTaggers:
64  acc.merge(RenameHLTaggerCfg(jet, tagger, '_old'))
65  #Track Augmenter
66  acc.merge(BTagTrackAugmenterAlgCfg(cfgFlags))
67 
68  #HighLevel taggers can not be run with time stamped containers
69  acc.merge(RunHighLevelTaggersCfg(cfgFlags, jet, 'BTagTrackToJetAssociator', postTagDL2JetToTrainingMap[jet], ""))
70 
71  return acc
72 
73 
74 def RunHighLevelTaggersCfg(inputFlags, jet, tracks, Associator, taggers):
75  result = ComponentAccumulator()
76 
77  BTagCollection = inputFlags.BTagging.OutputFiles.Prefix+JetCollection
78  result.merge(BTagJetAugmenterAlgCfg(inputFlags, JetCollection=jet, BTagCollection=BTagCollection, Associator=Associator, TrackCollection=tracks) )
79  for dl2 in taggers:
80  result.merge(FlavorTagNNCfg(inputFlags, BTagCollection, TrackCollection=tracks, NNFile=dl2) )
81 
82  return result
83 
84 
85 def str2bool(v):
86  if v.lower() in ('yes', 'true', 't', 'y', '1'):
87  return True
88  elif v.lower() in ('no', 'false', 'f', 'n', '0'):
89  return False
90  else:
91  raise argparse.ArgumentTypeError('Boolean value expected.')
92 
93 
94 def PrepareStandAloneBTagCfg(inputFlags):
95  result=ComponentAccumulator()
96 
97  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
98  result.merge(PoolReadCfg(inputFlags))
99 
100  # get standard config for magnetic field - map and cache
101  from MagFieldServices.MagFieldServicesConfig import AtlasFieldCacheCondAlgCfg
102  result.merge(AtlasFieldCacheCondAlgCfg( inputFlags ))
103 
104  #load folders needed for Run2 ID alignment
105  from IOVDbSvc.IOVDbSvcConfig import addFolders
106  result.merge(addFolders(inputFlags,['/TRT/Align'],'TRT_OFL'))
107 
108  return result
109 
110 def BTagRedoESDCfg(flags, jet, extraContainers=[]):
112 
113  acc.merge(RenameInputContainerCfg("old"))
114 
115  #Register input ESD container in output
116  ESDItemList = registerOutputBTaggingContainers(flags, jet)
117  ESDItemList += registerJetCollectionEL(flags, jet)
118  print(ESDItemList)
119  from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
120  acc.merge(OutputStreamCfg(flags,"ESD", ItemList=ESDItemList+extraContainers))
121 
122  return acc
123 
124 def registerJetCollectionEL(flags, JetCollection):
125  ItemList = []
126  # btaggingLink
127  ItemList.append('xAOD::JetContainer#'+JetCollection+'Jets')
128  ItemList.append('xAOD::JetAuxContainer#'+JetCollection+'JetsAux.btaggingLink')
129  return ItemList
130 
131 def registerOutputBTaggingContainers(flags, JetCollection, suffix = ''):
132  """Registers the jet collection to various containers in BTaggingFlags which govern which
133  containers will be parsed to the output xAOD and ESD files. This used to happen in
134  ./share/BTagging_jobOptions.py.
135 
136  input: JetCollection: The name of the jet collection."""
137  ItemList = []
138 
139  OutputFilesSVname = "SecVtx"
140  OutputFilesJFVxname = "JFVtx"
141  OutputFilesBaseName = "xAOD::BTaggingContainer#"
142  OutputFilesBaseAuxName = "xAOD::BTaggingAuxContainer#"
143  OutputFilesBaseNameSecVtx = "xAOD::VertexContainer#"
144  OutputFilesBaseAuxNameSecVtx = "xAOD::VertexAuxContainer#"
145  OutputFilesBaseNameJFSecVtx = "xAOD::BTagVertexContainer#"
146  OutputFilesBaseAuxNameJFSecVtx= "xAOD::BTagVertexAuxContainer#"
147 
148  author = flags.BTagging.OutputFiles.Prefix + JetCollection
149  # SecVert
150  ItemList.append(OutputFilesBaseNameSecVtx + author + OutputFilesSVname)
151  ItemList.append(OutputFilesBaseAuxNameSecVtx + author + OutputFilesSVname + 'Aux.-vxTrackAtVertex')
152  # JFSeCVert
153  ItemList.append(OutputFilesBaseNameJFSecVtx + author + OutputFilesJFVxname)
154  ItemList.append(OutputFilesBaseAuxNameJFSecVtx + author + OutputFilesJFVxname + 'Aux.')
155 
156  if suffix:
157  author += '_' + suffix
158 
159  ItemList.append(OutputFilesBaseName + author)
160  ItemList.append(OutputFilesBaseAuxName + author + 'Aux.*')
161  #ItemList.append(OutputFilesBaseAuxName + author + 'Aux.-BTagTrackToJetAssociatorBB')
162 
163  return ItemList
164 
165 if __name__=="__main__":
166 
167  inputESD = "/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/RecExRecoTest/mc21_13p6TeV/ESDFiles/mc21_13p6TeV.421450.PhPy8EG_A14_ttbar_hdamp258p75_SingleLep_fct.recon.ESD.e8445_e8447_s3822_r13565/ESD.28877240._000046.pool.root.1"
168  import argparse
169  parser = argparse.ArgumentParser(prog="BTagConfig: An example configuration module for btagging reconstruction reading an ESD",
170  usage="Call with an input file, pass -n=0 to skip execution, -t 0 for serial or 1 for threaded execution.")
171  parser.add_argument("-f", "--filesIn", default = inputESD, type=str, help="Comma-separated list of input files")
172  parser.add_argument("-t", "--nThreads", default=1, type=int, help="The number of concurrent threads to run. 0 uses serial Athena.")
173  parser.add_argument("-l", "--highlevel", type=str2bool, default=False, help="Run only high level taggers.")
174 
175  args = parser.parse_args()
176 
177  from AthenaConfiguration.AllConfigFlags import initConfigFlags
178  cfgFlags = initConfigFlags()
179  cfgFlags.Input.Files= args.filesIn.split(",")
180 
181  cfgFlags.Output.ESDFileName="esdOut.pool.root"
182 
183  # Flags relating to multithreaded execution
184  cfgFlags.Concurrency.NumThreads = args.nThreads
185  if args.nThreads>0:
186  cfgFlags.Scheduler.ShowDataDeps = True
187  cfgFlags.Scheduler.ShowDataFlow = True
188  cfgFlags.Scheduler.ShowControlFlow = True
189  cfgFlags.Concurrency.NumConcurrentEvents = args.nThreads
190 
191  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
192  acc=MainServicesCfg(cfgFlags)
193  acc.getService("MessageSvc").Format = "% F%80W%S%7W%R%T %0W%M"
194 
195  # Prevent the flags from being modified
196  cfgFlags.lock()
197 
198  acc.merge(PrepareStandAloneBTagCfg(cfgFlags))
199 
200  JetCollection = ['AntiKt4EMTopo', 'AntiKt4EMPFlow']
201 
202  if args.highlevel:
203  acc.merge(BTagHLTaggersCfg(cfgFlags, JetCollection = JetCollection))
204 
205  acc.setAppProperty("EvtMax",-1)
206 
207  f=open("BTag.pkl","wb")
208  acc.store(f)
209  f.close()
210 
211  acc.run()
AddressRemappingSvc
This class provides the interface to the LCG POOL persistency software.
Definition: AddressRemappingSvc.h:41
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
python.BTagESD.BTagRedoESDCfg
def BTagRedoESDCfg(flags, jet, extraContainers=[])
Definition: BTagESD.py:110
python.OutputStreamConfig.OutputStreamCfg
def OutputStreamCfg(flags, streamName, ItemList=[], MetadataItemList=[], disableEventTag=False, trigNavThinningSvc=None, takeItemsFromInput=False, extendProvenanceRecord=True, AcceptAlgs=[], HelperTools=[])
Definition: OutputStreamConfig.py:12
python.BTagESD.BTagHLTaggersCfg
def BTagHLTaggersCfg(inputFlags, JetCollection=[])
Definition: BTagESD.py:44
python.BTagESD.RunHighLevelTaggersCfg
def RunHighLevelTaggersCfg(inputFlags, jet, tracks, Associator, taggers)
Definition: BTagESD.py:74
python.BTagESD.registerJetCollectionEL
def registerJetCollectionEL(flags, JetCollection)
Definition: BTagESD.py:124
python.BTagTrackAugmenterAlgConfig.BTagTrackAugmenterAlgCfg
def BTagTrackAugmenterAlgCfg(flags, TrackCollection='InDetTrackParticles', PrimaryVertexCollectionName='PrimaryVertices', prefix=None)
Definition: BTagTrackAugmenterAlgConfig.py:9
ProxyProviderSvc
manages the address providers and add proxies on demand to the store
Definition: ProxyProviderSvc.h:43
python.BTagESD.PrepareStandAloneBTagCfg
def PrepareStandAloneBTagCfg(inputFlags)
Definition: BTagESD.py:94
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:256
python.IOVDbSvcConfig.addFolders
def addFolders(flags, folderStrings, detDb=None, className=None, extensible=False, tag=None, db=None, modifiers='')
Definition: IOVDbSvcConfig.py:72
python.BTagESD.str2bool
str2bool
Definition: BTagESD.py:173
Trk::open
@ open
Definition: BinningType.h:40
python.BTagJetAugmenterAlgConfig.BTagJetAugmenterAlgCfg
def BTagJetAugmenterAlgCfg(ConfigFlags, BTagCollection, Associator, TrackCollection, doFlipTagger=False)
Definition: BTagJetAugmenterAlgConfig.py:6
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
python.BTagESD.registerOutputBTaggingContainers
def registerOutputBTaggingContainers(flags, JetCollection, suffix='')
Definition: BTagESD.py:131
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.MagFieldServicesConfig.AtlasFieldCacheCondAlgCfg
def AtlasFieldCacheCondAlgCfg(flags, **kwargs)
Definition: MagFieldServicesConfig.py:8
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
python.FlavorTagNNConfig.FlavorTagNNCfg
def FlavorTagNNCfg(flags, BTaggingCollection, TrackCollection, NNFile, FlipConfig="STANDARD", variableRemapping={})
Definition: FlavorTagNNConfig.py:134
python.BTagESD.RenameInputContainerCfg
def RenameInputContainerCfg(suffix)
Definition: BTagESD.py:12
python.BTagESD.RenameHLTaggerCfg
def RenameHLTaggerCfg(JetCollection, Tagger, suffix)
Definition: BTagESD.py:30