From 1bc878fecde9ede96ffe56040dcbe9d20f1d034f Mon Sep 17 00:00:00 2001
From: Emmanuel Le Guirriec <emmanuel.le.guirriec@cern.ch>
Date: Mon, 3 Aug 2020 12:35:55 +0200
Subject: [PATCH] Modify several algorithms to deal with empty jet containers

---
 .../BTagging/src/HighLevelBTagAlg.cxx         |  8 +++-
 .../BTagging/src/JetSecVertexingAlg.cxx       | 46 ++++++++++---------
 .../BTagging/src/JetSecVtxFindingAlg.cxx      | 13 ++++--
 3 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/HighLevelBTagAlg.cxx b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/HighLevelBTagAlg.cxx
index 444b50c7a580..0f88f4e57371 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/HighLevelBTagAlg.cxx
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/HighLevelBTagAlg.cxx
@@ -68,11 +68,17 @@ namespace Analysis {
   StatusCode HighLevelBTagAlg::execute() {
     //retrieve the BTagging container
     SG::ReadHandle< xAOD::BTaggingContainer > h_btagContainer( m_BTagCollectionName);
-    SG::ReadDecorHandle<xAOD::BTaggingContainer, ElementLink< xAOD::JetContainer > > h_btagJetLinkName (m_btagJetLinkName);
     if (!h_btagContainer.isValid()) {
       ATH_MSG_ERROR( " cannot retrieve BTagging container with key " << m_BTagCollectionName.key()  );
       return StatusCode::FAILURE;
     }
+
+    if (h_btagContainer->size() == 0) {
+     ATH_MSG_DEBUG("#BTAG# Empty BTagging collection");
+     return StatusCode::SUCCESS;
+    }
+
+    SG::ReadDecorHandle<xAOD::BTaggingContainer, ElementLink< xAOD::JetContainer > > h_btagJetLinkName (m_btagJetLinkName);
     if (!h_btagJetLinkName.isAvailable()) {
       ATH_MSG_ERROR( " cannot retrieve Jet container EL decoration with key " << m_btagJetLinkName.key()  );
       return StatusCode::FAILURE;
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVertexingAlg.cxx b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVertexingAlg.cxx
index 0d22f0481711..486a60655e39 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVertexingAlg.cxx
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVertexingAlg.cxx
@@ -98,6 +98,31 @@ namespace Analysis {
       return StatusCode::FAILURE;
     }
 
+    SG::WriteHandle<xAOD::VertexContainer> h_BTagSVCollectionName;
+    SG::WriteHandle<xAOD::BTagVertexContainer> h_BTagJFVtxCollectionName;
+
+    std::string basename =  m_secVertexFinderBaseName;
+    /* Record the BTagging JF Vertex  output container */
+    if (basename == "JetFitter") {
+      ATH_MSG_DEBUG("#BTAG# Record the BTagging JF Vertex  output container");
+      h_BTagJFVtxCollectionName = SG::WriteHandle<xAOD::BTagVertexContainer>(m_BTagJFVtxCollectionName);
+      ATH_CHECK( h_BTagJFVtxCollectionName.record(std::make_unique<xAOD::BTagVertexContainer>(),
+                                        std::make_unique<xAOD::BTagVertexAuxContainer>()) );
+    }
+
+    /* Record the BTagging Secondary Vertex output container */
+    if ((basename == "SV1") || (basename == "MSV")) {
+      ATH_MSG_DEBUG("#BTAG# Record the BTagging Secondary Vertex output container");
+      h_BTagSVCollectionName = SG::WriteHandle<xAOD::VertexContainer>(m_BTagSVCollectionName);
+      ATH_CHECK( h_BTagSVCollectionName.record(std::make_unique<xAOD::VertexContainer>(),
+                                        std::make_unique<xAOD::VertexAuxContainer>()) );
+    }
+
+    if (h_JetCollectionName->size() == 0) {
+     ATH_MSG_DEBUG("#BTAG# Empty Jet collection");
+     return StatusCode::SUCCESS;
+    }
+
     SG::ReadDecorHandle<xAOD::JetContainer, std::vector<ElementLink< xAOD::TrackParticleContainer> > > h_jetParticleLinkName (m_jetParticleLinkName);
     if (!h_jetParticleLinkName.isAvailable()) {
       ATH_MSG_ERROR( " cannot retrieve jet container particle EL decoration with key " << m_jetParticleLinkName.key()  );
@@ -138,7 +163,6 @@ namespace Analysis {
       }
     }
 
-
     if (! primaryVertex) {
       ATH_MSG_DEBUG("#BTAG#  No vertex labeled as VxType::PriVtx!");
       xAOD::VertexContainer::const_iterator fz = h_VertexCollectionName->begin();
@@ -149,26 +173,6 @@ namespace Analysis {
     }
 
     const xAOD::Vertex& PrimaryVtx = *primaryVertex;
-    std::string basename =  m_secVertexFinderBaseName;
-
-    SG::WriteHandle<xAOD::VertexContainer> h_BTagSVCollectionName;
-    SG::WriteHandle<xAOD::BTagVertexContainer> h_BTagJFVtxCollectionName;
-
-    /* Record the BTagging JF Vertex  output container */
-    if (basename == "JetFitter") {
-      ATH_MSG_DEBUG("#BTAG# Record the BTagging JF Vertex  output container");
-      h_BTagJFVtxCollectionName = SG::WriteHandle<xAOD::BTagVertexContainer>(m_BTagJFVtxCollectionName);
-      ATH_CHECK( h_BTagJFVtxCollectionName.record(std::make_unique<xAOD::BTagVertexContainer>(),
-                                        std::make_unique<xAOD::BTagVertexAuxContainer>()) );
-    }
-
-    /* Record the BTagging Secondary Vertex output container */
-    if ((basename == "SV1") || (basename == "MSV")) {
-      ATH_MSG_DEBUG("#BTAG# Record the BTagging Secondary Vertex output container");
-      h_BTagSVCollectionName = SG::WriteHandle<xAOD::VertexContainer>(m_BTagSVCollectionName);
-      ATH_CHECK( h_BTagSVCollectionName.record(std::make_unique<xAOD::VertexContainer>(),
-                                        std::make_unique<xAOD::VertexAuxContainer>()) );
-    }
 
     Trk::VxSecVertexInfoContainer::const_iterator infoSVIter = h_VxSecVertexInfoName->begin();
     for (xAOD::JetContainer::const_iterator jetIter = h_JetCollectionName->begin(); jetIter != h_JetCollectionName->end(); ++jetIter, ++infoSVIter) {
diff --git a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVtxFindingAlg.cxx b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVtxFindingAlg.cxx
index 2156aa32e257..7f1ac927e0ec 100644
--- a/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVtxFindingAlg.cxx
+++ b/PhysicsAnalysis/JetTagging/JetTagAlgs/BTagging/src/JetSecVtxFindingAlg.cxx
@@ -58,6 +58,15 @@ namespace Analysis {
       return StatusCode::FAILURE;
     }
 
+    /* Record the VxSecVertexInfo output container */
+    SG::WriteHandle<Trk::VxSecVertexInfoContainer> h_VxSecVertexInfoName (m_VxSecVertexInfoName);
+    ATH_CHECK( h_VxSecVertexInfoName.record(std::make_unique<Trk::VxSecVertexInfoContainer>()));
+
+    if (h_JetCollectionName->size() == 0) {
+      ATH_MSG_DEBUG("#BTAG# Empty Jet collection");
+      return StatusCode::SUCCESS;
+    }
+
     SG::ReadDecorHandle<xAOD::JetContainer, std::vector<ElementLink< xAOD::TrackParticleContainer> > > h_jetParticleLinkName (m_jetParticleLinkName);
     if (!h_jetParticleLinkName.isAvailable()) {
       ATH_MSG_ERROR( " cannot retrieve jet container particle EL decoration with key " << m_jetParticleLinkName.key()  );
@@ -96,10 +105,6 @@ namespace Analysis {
 
     const xAOD::Vertex& PrimaryVtx = *primaryVertex;
 
-    /* Record the VxSecVertexInfo output container */
-    SG::WriteHandle<Trk::VxSecVertexInfoContainer> h_VxSecVertexInfoName (m_VxSecVertexInfoName);
-    ATH_CHECK( h_VxSecVertexInfoName.record(std::make_unique<Trk::VxSecVertexInfoContainer>()));
-
     for (xAOD::JetContainer::const_iterator jetIter = h_JetCollectionName->begin(); jetIter != h_JetCollectionName->end(); ++jetIter) {
       const xAOD::Jet& jetToTag = **jetIter;
 
-- 
GitLab