From 4c69bb20f0a12e636a6aca743c226c328b306608 Mon Sep 17 00:00:00 2001
From: Emmanuel Le Guirriec <emmanuel.le.guirriec@cern.ch>
Date: Wed, 16 Oct 2019 12:29:44 +0000
Subject: [PATCH] btaggingLink not hard coded. first try to btag one jet
 collection twice (two trainings)

---
 .../src/InDetImprovedJetFitterVxFinder.cxx    |  2 +-
 .../BTagging/BTagging/JetBTaggerAlg.h         |  1 +
 .../BTagging/python/BTagRun3Config.py         | 23 +++++----
 .../BTagging/python/BTagSecVertexingConfig.py | 19 ++++----
 .../BTagging/python/JetBTaggerAlgConfig.py    | 48 +++++++++----------
 .../JetTagAlgs/BTagging/src/JetBTaggerAlg.cxx |  5 +-
 6 files changed, 48 insertions(+), 50 deletions(-)

diff --git a/InnerDetector/InDetRecTools/InDetSecVxFinderTool/src/InDetImprovedJetFitterVxFinder.cxx b/InnerDetector/InDetRecTools/InDetSecVxFinderTool/src/InDetImprovedJetFitterVxFinder.cxx
index 9912bc47aa1..cf1fe8f1323 100755
--- a/InnerDetector/InDetRecTools/InDetSecVxFinderTool/src/InDetImprovedJetFitterVxFinder.cxx
+++ b/InnerDetector/InDetRecTools/InDetSecVxFinderTool/src/InDetImprovedJetFitterVxFinder.cxx
@@ -881,7 +881,7 @@ namespace InDet
         if (myCandidate->chiSquared()<0 ||
             myCandidate->numberDoF()<0)
         {
-          msg(MSG::WARNING) << " Fit for V0 candidate failed: chi2 or ndf negative. Deleting candidate..." << endmsg;
+          msg(MSG::DEBUG) << " Fit for V0 candidate failed: chi2 or ndf negative. Deleting candidate..." << endmsg;
           delete myCandidate;
           myCandidate=0;
           continue;
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/BTagging/JetBTaggerAlg.h b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/BTagging/JetBTaggerAlg.h
index adb87f15c4c..0ce1dde2f51 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/BTagging/JetBTaggerAlg.h
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/BTagging/JetBTaggerAlg.h
@@ -48,6 +48,7 @@ class  JetBTaggerAlg:
     SG::WriteHandleKey<xAOD::BTaggingContainer> m_BTaggingCollectionName {this, "BTaggingCollectionName", "", "Output BTagging container"};
 
     std::string m_JetName;
+    std::string m_BTagLink;
 
     ToolHandle< IBTagTool > m_bTagTool;
     ToolHandle< IBTagTrackAssociation > m_BTagTrackAssocTool;
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagRun3Config.py b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagRun3Config.py
index e72ef98dd55..79a42e1366f 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagRun3Config.py
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagRun3Config.py
@@ -52,9 +52,12 @@ def registerOutputContainersForJetCollection(flags, JetCollection, suffix = ''):
 
       input: JetCollection:       The name of the jet collection."""
       ItemList = []
+      # btaggingLink
+      ItemList.append('xAOD::JetContainer#'+JetCollection+'Jets.btaggingLink' + suffix)
+      ItemList.append('xAOD::JetAuxContainer#'+JetCollection+'JetsAux.btaggingLink'+ suffix)
+
       OutputFilesSVname = "SecVtx"
       OutputFilesJFVxname = "JFVtx"
-
       OutputFilesBaseName = "xAOD::BTaggingContainer#"
       OutputFilesBaseAuxName = "xAOD::BTaggingAuxContainer#"
       OutputFilesBaseNameSecVtx = "xAOD::VertexContainer#"
@@ -62,6 +65,9 @@ def registerOutputContainersForJetCollection(flags, JetCollection, suffix = ''):
       OutputFilesBaseNameJFSecVtx = "xAOD::BTagVertexContainer#"
       OutputFilesBaseAuxNameJFSecVtx= "xAOD::BTagVertexAuxContainer#"
 
+      if suffix:
+          suffix = '_' + suffix
+
       author = flags.BTagging.OutputFiles.Prefix + JetCollection + suffix
       ItemList.append(OutputFilesBaseName + author)
       ItemList.append(OutputFilesBaseAuxName + author + 'Aux.-BTagTrackToJetAssociatorBB')
@@ -95,14 +101,6 @@ def BTagRedoESDCfg(flags, jet):
 def BTagESDtoESDCfg(flags, jet, new):
     acc=ComponentAccumulator()
 
-    #Rename the element link of the BTagging container from the Jet container
-    from SGComps.SGCompsConf import AddressRemappingSvc, ProxyProviderSvc
-    AddressRemappingSvc = AddressRemappingSvc("AddressRemappingSvc")
-    # The new btaggingLink will point to the time-stamped BTagging container
-    AddressRemappingSvc.TypeKeyRenameMaps += ['xAOD::JetAuxContainer#AntiKt4EMTopoJets.btaggingLink->AntiKt4EMTopoJets.btaggingLink_old']
-    acc.addService(AddressRemappingSvc)
-    acc.addService(ProxyProviderSvc(ProviderNames = [ "AddressRemappingSvc" ]))
-
     #Register input ESD container in output
     ESDItemList = registerOutputContainersForJetCollection(flags, jet)
 
@@ -162,8 +160,9 @@ def BTagCfg(inputFlags,**kwargs):
     for jet in JetCollection:
         if timestamp:
             #Time-stamped BTagging container (21.2)
-            result.merge(BTagESDtoESDCfg(inputFlags, jet, timestamp))
-            kwargs.setdefault('TimeStamp', timestamp)
+            for ts in timestamp:
+              result.merge(BTagESDtoESDCfg(inputFlags, jet, ts))
+            kwargs['TimeStamp'] = timestamp
         else:
             result.merge(BTagRedoESDCfg(inputFlags, jet))
 
@@ -218,7 +217,7 @@ if __name__=="__main__":
 
     kwargs = {}
     if args.release == "21.2":
-        kwargs["TimeStamp"] = "_201810"
+        kwargs["TimeStamp"] = ['201810','201903']
     acc.merge(BTagCfg(cfgFlags, **kwargs))
 
     acc.setAppProperty("EvtMax",-1)
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagSecVertexingConfig.py b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagSecVertexingConfig.py
index 8f5619dec8d..f17e5785c45 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagSecVertexingConfig.py
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/BTagSecVertexingConfig.py
@@ -9,7 +9,7 @@ from BTagging.MSVVariablesFactoryConfig import MSVVariablesFactoryCfg
 
 from BTagging.BTaggingConf import Analysis__BTagSecVertexing
 
-def BTagSecVtxToolCfg(flags, name, JetCollection, outputObjs = None, **options):
+def BTagSecVtxToolCfg(flags, Name, JetCollection, TimeStamp = "", **options):
     """Adds a SecVtxTool instance and registers it.
 
     input: name:               The tool's name.
@@ -26,6 +26,8 @@ def BTagSecVtxToolCfg(flags, name, JetCollection, outputObjs = None, **options):
     secVtxFinderList = []
     secVtxFinderTrackNameList = []
     secVtxFinderxAODBaseNameList = []
+    if TimeStamp:
+        TimeStamp = '_' + TimeStamp
 
     newJetFitterVxFinder = acc.popToolsAndMerge(NewJetFitterVxFinderCfg(flags, 'JFVxFinder'))
     secVtxFinderList.append(newJetFitterVxFinder)
@@ -46,10 +48,7 @@ def BTagSecVtxToolCfg(flags, name, JetCollection, outputObjs = None, **options):
 
     varFactory = acc.popToolsAndMerge(MSVVariablesFactoryCfg("MSVVarFactory"))
 
-    btagname = flags.BTagging.OutputFiles.Prefix + jetcol
-    timestamp = options.get('TimeStamp', None)
-    if timestamp:
-        btagname += timestamp
+    btagname = flags.BTagging.OutputFiles.Prefix + jetcol + TimeStamp
 
     options = {}
     options.setdefault('SecVtxFinderList', secVtxFinderList)
@@ -57,14 +56,12 @@ def BTagSecVtxToolCfg(flags, name, JetCollection, outputObjs = None, **options):
     options.setdefault('SecVtxFinderxAODBaseNameList', secVtxFinderxAODBaseNameList)
     options.setdefault('PrimaryVertexName',BTaggingFlags.PrimaryVertexCollectionName)
     options.setdefault('vxPrimaryCollectionName',BTaggingFlags.PrimaryVertexCollectionName)
-    options.setdefault('BTagJFVtxCollectionName', btagname + OutputFilesJFVxname)
-    options.setdefault('BTagSVCollectionName', btagname + OutputFilesSVname)
+    options['BTagJFVtxCollectionName'] = btagname + OutputFilesJFVxname
+    options['BTagSVCollectionName'] = btagname + OutputFilesSVname
     options.setdefault('JetFitterVariableFactory', jetFitterVF)
     options.setdefault('MSVVariableFactory', varFactory)
-    options['name'] = name
-    if outputObjs:
-        outputObjs['xAOD::VertexContainer'] = options['BTagSVCollectionName']
-        outputObjs['xAOD::BTagVertexContainer'] = options['BTagJFVtxCollectionName']
+    options['name'] = Name+TimeStamp
+
     tool = Analysis__BTagSecVertexing(**options)
 
     acc.setPrivateTools(tool)
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/JetBTaggerAlgConfig.py b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/JetBTaggerAlgConfig.py
index 2a7388af202..2426a380288 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/JetBTaggerAlgConfig.py
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/python/JetBTaggerAlgConfig.py
@@ -1,7 +1,9 @@
 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 
 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
-from BTagging.BTaggingFlags import BTaggingFlags
+from BTagging.BTagSecVertexingConfig import BTagSecVtxToolCfg
+from BTagging.BTagTrackAssociationConfig import BTagTrackAssociationCfg
+from BTagging.BTagToolConfig import BTagToolCfg
 
 # import the JetBTaggerAlg configurable
 from BTagging.BTaggingConf import Analysis__JetBTaggerAlg as JetBTaggerAlg
@@ -11,34 +13,32 @@ def JetBTaggerAlgCfg(ConfigFlags, JetCollection="", TaggerList=[], SetupScheme="
     acc=ComponentAccumulator()
     jetcol = JetCollection
 
-    objs = {}
-    #options = dict(options)
-    options.setdefault('OutputLevel', BTaggingFlags.OutputLevel)
-    
     # setup the Analysis__BTagTrackAssociation tool
-    from BTagging.BTagTrackAssociationConfig import BTagTrackAssociationCfg
     options.setdefault('BTagTrackAssocTool', acc.popToolsAndMerge(BTagTrackAssociationCfg(ConfigFlags, 'TrackAssociation'+ ConfigFlags.BTagging.GeneralToolSuffix, jetcol, TaggerList )))
     
-    # setup the secondary vertexing tool
-    from BTagging.BTagSecVertexingConfig import BTagSecVtxToolCfg
-    options.setdefault('BTagSecVertexing', acc.popToolsAndMerge(BTagSecVtxToolCfg(ConfigFlags, 'SecVx'+ConfigFlags.BTagging.GeneralToolSuffix, jetcol, outputObjs = objs, **options)))
-
-    from BTagging.BTagToolConfig import BTagToolCfg
     options.setdefault('BTagTool', acc.popToolsAndMerge(BTagToolCfg(ConfigFlags, jetcol, TaggerList)))
 
-    btagname = ConfigFlags.BTagging.OutputFiles.Prefix + jetcol
     timestamp = options.get('TimeStamp', None)
-    if timestamp:
-        btagname += timestamp
+    if not timestamp:
+        timestamp = ['']
+    else:
         del options['TimeStamp']
-
-    # Set remaining options
-    options.setdefault('name', (btagname + ConfigFlags.BTagging.GeneralToolSuffix).lower())
-    options.setdefault('JetCollectionName', jetcol.replace('Track','PV0Track') + "Jets")
-    options.setdefault('JetCalibrationName', jetcol.replace('Track','PV0Track'))
-    options.setdefault('BTaggingCollectionName', btagname)
-
-    # -- create main BTagging algorithm
-    acc.addEventAlgo(JetBTaggerAlg(**options))
-    
+     
+    for ts in timestamp:
+        # setup the secondary vertexing tool
+        options['BTagSecVertexing'] = acc.popToolsAndMerge(BTagSecVtxToolCfg(ConfigFlags, 'SecVx'+ConfigFlags.BTagging.GeneralToolSuffix, jetcol, TimeStamp = ts, **options))
+
+        btagname = ConfigFlags.BTagging.OutputFiles.Prefix + jetcol
+        if ts:
+            btagname += '_'+ts
+        # Set remaining options
+        options['name'] = (btagname + ConfigFlags.BTagging.GeneralToolSuffix).lower()
+        options['JetCollectionName']= jetcol.replace('Track','PV0Track') + "Jets"
+        options['JetCalibrationName']= jetcol.replace('Track','PV0Track')
+        options['BTaggingCollectionName'] = btagname
+        options['BTaggingLink'] = '.btaggingLink'+ts
+
+        # -- create main BTagging algorithm
+        acc.addEventAlgo(JetBTaggerAlg(**options))
+            
     return acc
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetBTaggerAlg.cxx b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetBTaggerAlg.cxx
index 22ed04897b9..717f65cf2db 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetBTaggerAlg.cxx
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetBTaggerAlg.cxx
@@ -25,12 +25,14 @@ namespace Analysis {
   JetBTaggerAlg::JetBTaggerAlg(const std::string& n, ISvcLocator *p) : 
     AthAlgorithm(n,p),
     m_JetName(""),
+    m_BTagLink(".btaggingLink"),
     m_bTagTool("Analysis::BTagTool",this),
     m_BTagTrackAssocTool("Analysis::BTagTrackAssociation",this),
     m_bTagSecVtxTool("Analysis::BTagSecVertexing",this),
     m_magFieldSvc("AtlasFieldSvc",n)
   {
     declareProperty("JetCalibrationName", m_JetName);
+    declareProperty("BTaggingLink", m_BTagLink);
     declareProperty("BTagTool", m_bTagTool);
     declareProperty("BTagTrackAssocTool", m_BTagTrackAssocTool);
     declareProperty("BTagSecVertexing", m_bTagSecVtxTool);
@@ -49,7 +51,7 @@ namespace Analysis {
     // by job configuration.
     ATH_CHECK( m_JetCollectionName.initialize() );
     ATH_CHECK( m_BTaggingCollectionName.initialize() );
-    m_jetBTaggingLinkName = m_JetCollectionName.key()+".btaggingLink";
+    m_jetBTaggingLinkName = m_JetCollectionName.key() + m_BTagLink;
     ATH_CHECK( m_jetBTaggingLinkName.initialize() );
    
     /// retrieve the main BTagTool
@@ -137,7 +139,6 @@ namespace Analysis {
     StatusCode jetIsAssociated;
     if (!m_BTagTrackAssocTool.empty()) {
       ATH_MSG_VERBOSE("#BTAG# Track association tool is not empty");
-      //jetIsAssociated = m_BTagTrackAssocTool->BTagTrackAssociation_exec(&jets, h_BTaggingCollectionName.ptr());
       jetIsAssociated = m_BTagTrackAssocTool->BTagTrackAssociation_exec(h_JetCollectionName.ptr(), h_BTaggingCollectionName.ptr());
       if ( jetIsAssociated.isFailure() ) {
         ATH_MSG_ERROR("#BTAG# Failed to associate tracks to jet ");
-- 
GitLab