From 3f744bca3ef312ca83af34eff7c024af63c7a3df Mon Sep 17 00:00:00 2001
From: cranshaw <Jack.Cranshaw@cern.ch>
Date: Thu, 9 Aug 2018 10:50:57 -0500
Subject: [PATCH] Changes for generic BookkeeperTool

Former-commit-id: a70a4fd27443a58a280cbec9355243e39831ba74
---
 .../AthenaServices/src/AthenaOutputStream.cxx |  46 ++++--
 .../AthenaServices/src/AthenaOutputStream.h   |   2 +-
 Control/AthenaServices/src/MetaDataSvc.cxx    |  13 +-
 .../EventBookkeeperTools/BookkeeperTool.h     |  34 +++--
 .../Root/BookkeeperTool.cxx                   | 144 +++++++++++-------
 .../xAODCutFlow/CutBookkeeperAuxContainer.h   |   6 +
 6 files changed, 162 insertions(+), 83 deletions(-)

diff --git a/Control/AthenaServices/src/AthenaOutputStream.cxx b/Control/AthenaServices/src/AthenaOutputStream.cxx
index 69749354341..793c30c1e27 100644
--- a/Control/AthenaServices/src/AthenaOutputStream.cxx
+++ b/Control/AthenaServices/src/AthenaOutputStream.cxx
@@ -318,19 +318,13 @@ StatusCode AthenaOutputStream::initialize() {
 
 StatusCode AthenaOutputStream::stop()
 {
+   ATH_MSG_INFO("AthenaOutputStream " << this->name() << " ::stop()");
       for (std::vector<ToolHandle<IAthenaOutputTool> >::iterator iter = m_helperTools.begin();
            iter != m_helperTools.end(); iter++) {
          if (!(*iter)->preFinalize().isSuccess()) {
              ATH_MSG_ERROR("Cannot finalize helper tool");
          }
       }
-      // Always force a final commit in stop - mainly applies to AthenaPool
-      if (m_writeOnFinalize) {
-         if (write().isFailure()) {  // true mean write AND commit
-            ATH_MSG_ERROR("Cannot write on finalize");
-         }
-         ATH_MSG_INFO("Records written: " << m_events);
-      }
       FileIncident fileInc(name(),"MetaDataStop","","Serial");
       ServiceHandle<MetaDataSvc> mdsvc("MetaDataSvc", name());
       if (mdsvc.retrieve().isFailure()) {
@@ -339,6 +333,13 @@ StatusCode AthenaOutputStream::stop()
       else {
          ATH_CHECK(mdsvc->prepareOutput(fileInc));
       }
+      // Always force a final commit in stop - mainly applies to AthenaPool
+      if (m_writeOnFinalize) {
+         if (write().isFailure()) {  // true mean write AND commit
+            ATH_MSG_ERROR("Cannot write on finalize");
+         }
+         ATH_MSG_INFO("Records written: " << m_events);
+      }
 
       if (!m_metadataItemList.value().empty()) {
          m_currentStore = &m_metadataStore;
@@ -390,6 +391,16 @@ void AthenaOutputStream::handle(const Incident& inc) {
              ATH_MSG_ERROR("Cannot finalize helper tool");
          }
       }
+      FileIncident fileInc(name(),"MetaDataStop","","Serial");
+      ServiceHandle<MetaDataSvc> mdsvc("MetaDataSvc", name());
+      if (mdsvc.retrieve().isFailure()) {
+         ATH_MSG_ERROR("Could not retrieve MetaDataSvc for stop actions");
+      }
+      else {
+         if(mdsvc->prepareOutput(fileInc).isFailure()) {
+           ATH_MSG_ERROR("MetaDataStop prepareOutput failed");
+         }
+      }
       // Always force a final commit in stop - mainly applies to AthenaPool
       if (m_writeOnFinalize) {
          if (write().isFailure()) {  // true mean write AND commit
@@ -551,6 +562,9 @@ StatusCode AthenaOutputStream::write() {
          }
       }
 
+      for (SG::IFolder::const_iterator i = m_p2BWritten->begin(), iEnd = m_p2BWritten->end(); i != iEnd; i++) {
+         //ATH_MSG_INFO("BLARG cao" << i->id() << " " << i->key());
+      }
       StatusCode currentStatus = m_streamer->streamObjects(m_objects);
       // Do final check of streaming
       if (!currentStatus.isSuccess()) {
@@ -572,6 +586,7 @@ StatusCode AthenaOutputStream::write() {
       }
    }
    if (failed) {
+      ATH_MSG_ERROR("Could not connectOutput");
       return(StatusCode::FAILURE);
    }
    return(StatusCode::SUCCESS);
@@ -591,11 +606,23 @@ void AthenaOutputStream::collectAllObjects() {
       }
    }
    m_p2BWritten->updateItemList(true);
+   std::vector<CLID> folderclids;
    // Collect all objects that need to be persistified:
-//FIXME refactor: move this in folder. Treat as composite
+   //FIXME refactor: move this in folder. Treat as composite
    for (SG::IFolder::const_iterator i = m_p2BWritten->begin(), iEnd = m_p2BWritten->end(); i != iEnd; i++) {
-       //ATH_MSG_INFO("BLARG " << i->id() << " " << i->key());
+      //ATH_MSG_INFO("BLARG cao" << i->id() << " " << i->key());
       addItemObjects(*i);
+      folderclids.push_back(i->id());
+   }
+   for (auto it = m_objects.begin(); it != m_objects.end(); ++it) {
+      if (std::find(folderclids.begin(),folderclids.end(),(*it)->clID())==folderclids.end()) {
+         m_objects.erase(it);
+      }
+   }
+   for (auto it = m_objects.begin(); it != m_objects.end(); ++it) {
+      if (std::find(folderclids.begin(),folderclids.end(),(*it)->clID())==folderclids.end()) {
+         ATH_MSG_ERROR("Object found that was not in itemlist");
+      }
    }
 }
 
@@ -751,7 +778,6 @@ void AthenaOutputStream::addItemObjects(const SG::FolderItem& item)
                            attributes.insert(attr);
                            std::stringstream temp;
                            temp << tns.str() << attr;
-                           //ATH_MSG_INFO("BLARG " << this->name() << " " << temp.str());
                            if (m_itemSvc->addStreamItem(this->name(),temp.str()).isFailure()) {
                               ATH_MSG_WARNING("Unable to record item " << temp.str() << " in Svc");
                            }
diff --git a/Control/AthenaServices/src/AthenaOutputStream.h b/Control/AthenaServices/src/AthenaOutputStream.h
index b578976339b..584d8a52889 100644
--- a/Control/AthenaServices/src/AthenaOutputStream.h
+++ b/Control/AthenaServices/src/AthenaOutputStream.h
@@ -143,7 +143,7 @@ public:
    virtual StatusCode initialize();
    virtual StatusCode finalize();
    virtual StatusCode execute();
-   virtual StatusCode stop();
+   virtual StatusCode stop() override;
    //@}
    /// Stream the data
    virtual StatusCode write();
diff --git a/Control/AthenaServices/src/MetaDataSvc.cxx b/Control/AthenaServices/src/MetaDataSvc.cxx
index 9f295ab5e76..b812809f2fb 100644
--- a/Control/AthenaServices/src/MetaDataSvc.cxx
+++ b/Control/AthenaServices/src/MetaDataSvc.cxx
@@ -39,7 +39,8 @@ MetaDataSvc::MetaDataSvc(const std::string& name, ISvcLocator* pSvcLocator) : ::
 	m_allowMetaDataStop(false),
 	m_persToClid(),
 	m_toolForClid(),
-	m_streamForKey() {
+	m_streamForKey(),
+        m_metaDataTools(this) {
    // declare properties
    declareProperty("MetaDataContainer", m_metaDataCont = "");
    declareProperty("MetaDataTools", m_metaDataTools);
@@ -117,10 +118,11 @@ StatusCode MetaDataSvc::initialize() {
       ATH_MSG_FATAL("Cannot get IncidentSvc.");
       return(StatusCode::FAILURE);
    }
-   if (!m_metaDataTools.retrieve().isSuccess()) {
+   if (m_metaDataTools.retrieve().isFailure()) {
       ATH_MSG_FATAL("Cannot get " << m_metaDataTools);
       return(StatusCode::FAILURE);
    }
+   ATH_MSG_INFO("Found " << m_metaDataTools);
 
    m_incSvc->addListener(this, "FirstInputFile", 90);
    m_incSvc->addListener(this, "BeginInputFile", 90);
@@ -205,8 +207,8 @@ StatusCode MetaDataSvc::stop() {
    }
 
    // Set to be listener for end of event
-   //Incident metaDataStopIncident(name(), "MetaDataStop");
-   //m_incSvc->fireIncident(metaDataStopIncident);
+   Incident metaDataStopIncident(name(), "MetaDataStop");
+   m_incSvc->fireIncident(metaDataStopIncident);
 
    // finalizing tools via metaDataStop
    //ATH_CHECK(this->prepareOutput(metaDataStopIncident));
@@ -327,7 +329,6 @@ StatusCode MetaDataSvc::prepareOutput(const Incident& inc)
       return StatusCode::FAILURE;
    }
    const std::string guid = fileInc->fileGuid();
-   ATH_MSG_INFO("BLARG guid=" << guid);
    StatusCode rc(StatusCode::SUCCESS);
    for (auto it = m_metaDataTools.begin(); it != m_metaDataTools.end(); ++it) {
       ATH_MSG_DEBUG(" calling metaDataStop for " << (*it)->name());
@@ -391,7 +392,7 @@ void MetaDataSvc::handle(const Incident& inc) {
          ATH_MSG_ERROR("Could not retire metadata source " << fileName);
       }
    } else if (inc.type() == "LastInputFile") {
-      if (!m_metaDataTools.release().isSuccess()) {
+      if (m_metaDataTools.release().isFailure()) {
          ATH_MSG_WARNING("Cannot release " << m_metaDataTools);
       }
    } else if (inc.type() == "ShmProxy") {
diff --git a/Event/EventBookkeeperTools/EventBookkeeperTools/BookkeeperTool.h b/Event/EventBookkeeperTools/EventBookkeeperTools/BookkeeperTool.h
index 9d012350382..6879e02f069 100644
--- a/Event/EventBookkeeperTools/EventBookkeeperTools/BookkeeperTool.h
+++ b/Event/EventBookkeeperTools/EventBookkeeperTools/BookkeeperTool.h
@@ -11,12 +11,9 @@
  *  $Id: BookkeeperTool.h 663679 2015-04-29 08:31:54Z krasznaa $
  **/
 
-//#include "GaudiKernel/AlgTool.h"
 #include "AthenaBaseComps/AthAlgTool.h"
 #include "AsgTools/AsgMetadataTool.h"
-#ifdef ASGTOOL_ATHENA
 #include "AthenaKernel/IMetaDataTool.h"
-#endif // ASGTOOL_ATHENA
 #include "GaudiKernel/IIncidentListener.h"
 #include "GaudiKernel/ServiceHandle.h"
 #include "AthenaKernel/ICutFlowSvc.h"
@@ -32,28 +29,26 @@
  **/
 
 
-class BookkeeperTool : public asg::AsgMetadataTool
-#ifdef ASGTOOL_ATHENA
-                     , public virtual ::IMetaDataTool
-#endif // ASGTOOL_ATHENA
+class BookkeeperTool : public AthAlgTool, public virtual ::IMetaDataTool
 {
-   ASG_TOOL_CLASS0(BookkeeperTool)
+   //ASG_TOOL_CLASS0(BookkeeperTool)
 public: // Constructor and Destructor
    /// Standard Service Constructor
-   BookkeeperTool(const std::string& name = "BookkeeperTool");
+   BookkeeperTool(const std::string& type, 
+                  const std::string& name,
+                  const IInterface*  parent);
    /// Destructor
    virtual ~BookkeeperTool();
 
 public:
-   //void handle(const Incident& incident);
-   virtual StatusCode metaDataStop();
    virtual StatusCode metaDataStop(const SG::SourceID&);
-   virtual StatusCode beginInputFile();
    virtual StatusCode beginInputFile(const SG::SourceID& sid = "Serial");
-   virtual StatusCode endInputFile();
    virtual StatusCode endInputFile(const SG::SourceID& sid = "Serial");
    virtual StatusCode initialize();
    virtual StatusCode finalize();
+protected:
+   ServiceHandle<StoreGateSvc> inputMetaStore() const;
+   ServiceHandle<StoreGateSvc> outputMetaStore() const;
 
 private:
   
@@ -71,7 +66,8 @@ private:
   StatusCode addCutFlow();
  
   /// Pointer to cut flow svc 
-  //ServiceHandle<ICutFlowSvc> m_cutflowsvc;
+  ServiceHandle<StoreGateSvc> m_inputMetaStore;
+  ServiceHandle<StoreGateSvc> m_outputMetaStore;
 
   /// The name of the output CutBookkeeperContainer
   std::string m_outputCollName;
@@ -92,7 +88,15 @@ private:
 
 };
 
-METACONT_DEF( xAOD::CutBookkeeperAuxContainer, 1422102 );
+inline ServiceHandle<StoreGateSvc> BookkeeperTool::inputMetaStore() const
+{
+  return m_inputMetaStore;
+}
+
+inline ServiceHandle<StoreGateSvc> BookkeeperTool::outputMetaStore() const
+{
+  return m_outputMetaStore;
+}
 
 #endif
 
diff --git a/Event/EventBookkeeperTools/Root/BookkeeperTool.cxx b/Event/EventBookkeeperTools/Root/BookkeeperTool.cxx
index 8c6b126a2e0..4cc72afe68a 100644
--- a/Event/EventBookkeeperTools/Root/BookkeeperTool.cxx
+++ b/Event/EventBookkeeperTools/Root/BookkeeperTool.cxx
@@ -27,8 +27,12 @@
 #include "AthContainersInterfaces/IConstAuxStoreMeta.h"
 #endif
 
-BookkeeperTool::BookkeeperTool(const std::string& name)
-  : asg::AsgMetadataTool(name),
+BookkeeperTool::BookkeeperTool(const std::string& type, 
+                               const std::string& name,
+                               const IInterface*  parent)
+  : AthAlgTool(type,name,parent),
+    m_inputMetaStore( "StoreGateSvc/InputMetaDataStore", name ),
+    m_outputMetaStore( "StoreGateSvc/MetaDataStore", name ),
     m_cutflowTaken(false),
     m_markIncomplete(true)
 {
@@ -67,14 +71,17 @@ BookkeeperTool::initialize()
 
 
 //__________________________________________________________________________
+/*
 StatusCode BookkeeperTool::beginInputFile()
 {
   return this->beginInputFile("Serial");
 }
+*/
 
 #ifdef ASGTOOL_ATHENA
 StatusCode BookkeeperTool::beginInputFile(const SG::SourceID& sid)
 {
+  ATH_MSG_INFO("beginInputFile " << this->name() << "\n" << outputMetaStore()->dump());
   //OPENING NEW INPUT FILE
   //Things to do:
   // 1) note that a file is currently opened
@@ -82,16 +89,17 @@ StatusCode BookkeeperTool::beginInputFile(const SG::SourceID& sid)
   //    2a) if incomplete from input, directly propagate to output
   //    2b) if complete from input, wait for EndInputFile to decide what to do in output
 
+  const std::string storename("MetaDataStore+");
   if (m_inputCollName != "") {  // are there inputs
     //  IF NO METACONT IN OUTPUT STORE YET
     //     Initialize MetaCont for incomplete and tmp containers in output store
     //
-    std::string tmp_name = m_outputCollName+"tmp";
+    std::string tmp_name = storename+m_outputCollName+"tmp";
     ATH_CHECK(buildAthenaInterface(m_inputCollName,tmp_name,sid));
 
     // Do the following if we want incomplete processings marked
     if (m_markIncomplete) {
-      std::string inc_name = "Incomplete"+m_outputCollName; 
+      std::string inc_name = storename+"Incomplete"+m_outputCollName; 
       std::string input_inc_name = "Incomplete"+m_inputCollName;
       ATH_CHECK(buildAthenaInterface(input_inc_name,inc_name,sid));
     }
@@ -106,12 +114,12 @@ StatusCode BookkeeperTool::beginInputFile(const SG::SourceID& sid)
 }
 #endif
 
-
+/*
 StatusCode BookkeeperTool::endInputFile()
 {
   return this->endInputFile("Serial");
 }
-
+*/
 
 //#ifdef ASGTOOL_ATHENA
 StatusCode BookkeeperTool::endInputFile(const SG::SourceID& sid)
@@ -126,6 +134,7 @@ StatusCode BookkeeperTool::endInputFile(const SG::SourceID& sid)
 
 StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
 {
+  const std::string storename("MetaDataStore+");
   if (m_inputCollName != "") {  // are there inputs
     //TERMINATING THE JOB (EndRun)
     //Things to do:
@@ -133,7 +142,7 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
     // 2) Print cut flow summary
     // 3) Write root file if requested
     // Now retrieve pointers for the MetaConts
-    std::string tmp_name = m_outputCollName+"tmpCont";
+    std::string tmp_name = storename+m_outputCollName+"tmpCont";
     const MetaCont<xAOD::CutBookkeeperContainer>* tmp;
     ATH_CHECK(outputMetaStore()->retrieve(tmp,tmp_name));
     xAOD::CutBookkeeperContainer* outcom = new xAOD::CutBookkeeperContainer();
@@ -141,28 +150,33 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
     outcom->setStore(outcom_aux);
 
     if (m_markIncomplete) {
-      std::string inc_name = "Incomplete"+m_outputCollName+"Cont"; 
-      const MetaCont<xAOD::CutBookkeeperContainer>* inc;
-      ATH_CHECK(outputMetaStore()->retrieve(inc,inc_name));
+      std::string inc_name = storename+"Incomplete"+m_outputCollName+"Cont"; 
+      // Build incomplete container to fill
       xAOD::CutBookkeeperContainer* outinc = new xAOD::CutBookkeeperContainer();
       xAOD::CutBookkeeperAuxContainer* outinc_aux = new xAOD::CutBookkeeperAuxContainer();
       outinc->setStore(outinc_aux);
-
-      // Incomplete inputs can just be merged
-      auto sids_inc = inc->sources();
-      xAOD::CutBookkeeperContainer* contptr(nullptr);
-      for (auto it = sids_inc.begin(); it != sids_inc.end(); ++it) {
-        if (!inc->find(*it,contptr)) {
-          ATH_MSG_ERROR("Container sid list did not match contents");
-        } else {
-          ATH_CHECK(updateContainer(outinc,contptr));
+      // Check if there were any incomplete inputs
+      const MetaCont<xAOD::CutBookkeeperContainer>* inc;
+      if(outputMetaStore()->retrieve(inc,inc_name).isSuccess()) {
+
+        // Incomplete inputs can just be merged
+        auto sids_inc = inc->sources();
+        xAOD::CutBookkeeperContainer* contptr(nullptr);
+        for (auto it = sids_inc.begin(); it != sids_inc.end(); ++it) {
+          if (!inc->find(*it,contptr)) {
+            ATH_MSG_ERROR("Container sid list did not match contents");
+          } else {
+            ATH_CHECK(updateContainer(outinc,contptr));
+          }
+          contptr = nullptr; 
         }
-        contptr = nullptr; 
+      } else {
+        ATH_MSG_INFO("Did not find MetaCont for " << inc_name << ", assuming input had no incomplete bookkeepers");
       }
 
       // Loop over containers and mark based on end files seen
       auto sids_tmp = tmp->sources();
-      contptr = nullptr;
+      xAOD::CutBookkeeperContainer* contptr(nullptr);
       for (auto it = sids_tmp.begin(); it != sids_tmp.end(); ++it) {
         if (!tmp->find(*it,contptr)) {
           ATH_MSG_ERROR("Container sid list did not match contents");
@@ -183,16 +197,20 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
 
       std::string incout_name = "Incomplete"+m_outputCollName;
       // Do any cleanup
-      if (inputMetaStore()->contains<xAOD::CutBookkeeperContainer>(incout_name) ) {
+/*
+*/
+      if (outputMetaStore()->contains<xAOD::CutBookkeeperContainer>(incout_name) ) {
         ATH_MSG_INFO("Cleaning up xAOD::CutBookkeeperContainer for " << incout_name);
         const xAOD::CutBookkeeperContainer* tmpBook(nullptr);
         if ( outputMetaStore()->retrieve(tmpBook,incout_name).isSuccess() ) {
           const SG::IConstAuxStore* tmpBookAux = tmpBook->getConstStore();
+          //const xAOD::CutBookkeeperAuxContainer* tba = (const xAOD::CutBookkeeperAuxContainer*)tmpBookAux;
           ATH_CHECK(outputMetaStore()->removeDataAndProxy(tmpBook));
           ATH_CHECK(outputMetaStore()->removeDataAndProxy(tmpBookAux));
         }
         else ATH_MSG_ERROR("StoreGate failed retrieve after contains=true");
       }
+      ATH_MSG_INFO("GLARB incout_name" << incout_name);
       ATH_CHECK(outputMetaStore()->record(outinc,incout_name));
       ATH_CHECK(outputMetaStore()->record(outinc_aux,incout_name+"Aux."));
     }  // markIncomplete
@@ -211,7 +229,9 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
     }
 
     // Record container objects directly in store for output
-    if (inputMetaStore()->contains<xAOD::CutBookkeeperContainer>(m_outputCollName) ) {
+/*
+*/
+    if (outputMetaStore()->contains<xAOD::CutBookkeeperContainer>(m_outputCollName) ) {
       ATH_MSG_INFO("Cleaning up xAOD::CutBookkeeperContainer for " << m_outputCollName);
       const xAOD::CutBookkeeperContainer* tmpBook(nullptr);
       if ( outputMetaStore()->retrieve(tmpBook,m_outputCollName).isSuccess() ) {
@@ -221,6 +241,7 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
       }
       else ATH_MSG_ERROR("StoreGate failed retrieve after contains=true");
     }
+    ATH_MSG_INFO("GLARB m_outputCollName " << m_outputCollName);
     ATH_CHECK(outputMetaStore()->record(outcom,m_outputCollName));
     ATH_CHECK(outputMetaStore()->record(outcom_aux,m_outputCollName+"Aux."));
   } // inputCollName if
@@ -233,6 +254,7 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
   else {
     ATH_MSG_DEBUG("Cutflow information written into container before metaDataStop");
   }
+  //ATH_MSG_INFO("BLARG 2 " << outputMetaStore()->dump());
 
   // Reset after metadata stop
   m_cutflowTaken = false;
@@ -255,11 +277,12 @@ StatusCode BookkeeperTool::metaDataStop(const SG::SourceID&)
 }
 //#endif
 
+/*
 StatusCode BookkeeperTool::metaDataStop()
 {
   return this->metaDataStop("Serial");
 }
-
+*/
 
 StatusCode
 BookkeeperTool::finalize()
@@ -271,66 +294,85 @@ BookkeeperTool::finalize()
 
 StatusCode BookkeeperTool::initOutputContainer( const std::string& sgkey)
 {
-  // Now create the primary container
-  xAOD::CutBookkeeperContainer* coll = new xAOD::CutBookkeeperContainer();
-  xAOD::CutBookkeeperAuxContainer* auxcoll = new xAOD::CutBookkeeperAuxContainer();
-  coll->setStore(auxcoll);
+  // Append datastore name to key
+  //std::string key = "MetaDataStore+"+sgkey;
+  std::string key = sgkey;
+  // Create the primary container
+  //xAOD::CutBookkeeperContainer* coll = new xAOD::CutBookkeeperContainer();
+  //xAOD::CutBookkeeperAuxContainer* auxcoll = new xAOD::CutBookkeeperAuxContainer();
+  //coll->setStore(auxcoll);
   // Put it in a MetaCont
-  MetaCont<xAOD::CutBookkeeperContainer>* mcont = new MetaCont<xAOD::CutBookkeeperContainer>(DataObjID("xAOD::CutBookkeeperContainer",sgkey));
+  MetaCont<xAOD::CutBookkeeperContainer>* mcont = new MetaCont<xAOD::CutBookkeeperContainer>(DataObjID("xAOD::CutBookkeeperContainer",key));
   // Do the same for the auxiliary container
-  std::string auxkey(sgkey+"Aux.");
+  std::string auxkey(key+"Aux.");
   MetaCont<xAOD::CutBookkeeperAuxContainer>* acont = new MetaCont<xAOD::CutBookkeeperAuxContainer>(DataObjID("xAOD::CutBookkeeperAuxContainer",auxkey));
-  ATH_CHECK(outputMetaStore()->record(std::move(mcont),sgkey));
+  ATH_MSG_INFO("GLARB mcont key " << key);
+  ATH_MSG_INFO("GLARB mcont auxkey " << auxkey);
+  ATH_CHECK(outputMetaStore()->record(std::move(mcont),key));
   ATH_CHECK(outputMetaStore()->record(std::move(acont),auxkey));
+  //ATH_MSG_INFO("BLARG 1 " << outputMetaStore()->dump());
+  /*
+  */
   ATH_CHECK(outputMetaStore()->symLink
           (
             ClassID_traits<MetaCont<xAOD::CutBookkeeperAuxContainer> >::ID(),
             auxkey,
             ClassID_traits<xAOD::CutBookkeeperAuxContainer>::ID()
           ));
+  ATH_MSG_INFO("BLARG 2 " << outputMetaStore()->dump());
 
   return StatusCode::SUCCESS;
 }
 
-
+//--------------------------------------------------------//
+//  MetaConts are only needed when reading in Athena
+//  This builds them and populates them with bookeepers from the input store
+//--------------------------------------------------------//
 StatusCode BookkeeperTool::buildAthenaInterface(const std::string& inputName,
                                                 const std::string& outputName,
                                                 const SG::SourceID& sid)
 {
-  std::string inc_name = outputName+"Cont"; 
-  if( !(outputMetaStore()->contains<MetaCont<xAOD::CutBookkeeperContainer> >(inc_name)) ) {
-    ATH_CHECK(this->initOutputContainer(inc_name));
+  // Make sure the MetaCont is ready in the output store for outputName
+  //   If not, then create it
+  std::string name = outputName+"Cont"; 
+  if( !(outputMetaStore()->contains<MetaCont<xAOD::CutBookkeeperContainer> >(name)) ) {
+    ATH_CHECK(this->initOutputContainer(name));
   }
   else {
     ATH_MSG_WARNING("incomplete collection already exists");
   }
 
-  // Now retrieve pointers for the MetaConts
-  MetaCont<xAOD::CutBookkeeperContainer>* inc;
-  ATH_CHECK(outputMetaStore()->retrieve(inc,inc_name));
+  // Retrieve pointer for the MetaCont
+  MetaCont<xAOD::CutBookkeeperContainer>* mc;
+  ATH_CHECK(outputMetaStore()->retrieve(mc,name));
 
-  // Make sure sid does not already exist in container
-  if ( std::find(inc->sources().begin(),inc->sources().end(),sid) 
-                                        != inc->sources().end() ) {
+  // Make sure sid does not already exist in the MetaCont
+  if ( std::find(mc->sources().begin(),mc->sources().end(),sid) 
+                                        != mc->sources().end() ) {
     ATH_MSG_ERROR("Metadata already exists for sid " << sid);
-    return StatusCode::FAILURE;
+    return StatusCode::FAILURE;   // Fail if sid already exists
   }
 
-  // NOW GET NEW INFORMATION FROM INPUT STORE
-  //
-  // Get the incomplete bookkeeper collection of the input metadata store
-  xAOD::CutBookkeeperContainer* input_inc = 0;
+  // Get the input bookkeeper of the input metadata store
+  const xAOD::CutBookkeeperContainer* cbc;
   if (inputMetaStore()->contains<xAOD::CutBookkeeperContainer>(inputName) ) {
-    StatusCode ssc = inputMetaStore()->retrieve( input_inc, inputName );
+    ATH_MSG_INFO("BLARG input " << inputMetaStore()->dump());
+    StatusCode ssc = inputMetaStore()->retrieve( cbc, inputName );
     if (ssc.isSuccess()) {
-      // retrieve the incomplete output container
-      if ( !inc->insert(sid,input_inc) ) {
-        ATH_MSG_ERROR("Unable to insert " << inputName << " for " << sid << " with key " << inc_name);
+      // Insert input bookkeeper into MetaCont for this sid
+      xAOD::CutBookkeeperContainer* tostore = new xAOD::CutBookkeeperContainer(*cbc);
+      if ( !mc->insert(sid,tostore) ) {
+        ATH_MSG_ERROR("Unable to insert " << inputName << " for " << sid << " with key " << name);
+        return StatusCode::FAILURE;   // Fail if insert to mc fails
       }
     }
+    else {
+      ATH_MSG_ERROR("Could not retrieve CutBookkeeperContainer with name " << inputName << " in input store");
+      return StatusCode::FAILURE;   // Fail if store contains, but not retrieves
+    }
   }
   else {
-    ATH_MSG_INFO("No " << inputName << " bookkeepers in this file ");
+    ATH_MSG_WARNING("No " << inputName << " bookkeepers in this file ");
   }
 
   return StatusCode::SUCCESS;
diff --git a/Event/xAOD/xAODCutFlow/xAODCutFlow/CutBookkeeperAuxContainer.h b/Event/xAOD/xAODCutFlow/xAODCutFlow/CutBookkeeperAuxContainer.h
index dfba7d648e1..61ecf36f32c 100644
--- a/Event/xAOD/xAODCutFlow/xAODCutFlow/CutBookkeeperAuxContainer.h
+++ b/Event/xAOD/xAODCutFlow/xAODCutFlow/CutBookkeeperAuxContainer.h
@@ -20,5 +20,11 @@ namespace xAOD {
 // Set up a CLID for the class:
 #include "xAODCore/CLASS_DEF.h"
 CLASS_DEF( xAOD::CutBookkeeperAuxContainer, 1147935274, 1 )
+#ifndef XAOD_STANDALONE
+#include "AthenaKernel/MetaCont.h"
+CLASS_DEF( MetaCont<xAOD::CutBookkeeperAuxContainer> , 1422109 , 1 )
+#include "SGTools/BaseInfo.h"
+SG_BASE( MetaCont<xAOD::CutBookkeeperAuxContainer>, MetaContBase );
+#endif // not XAOD_STANDALONE
 
 #endif // XAODCUTFLOW_CUTBOOKKEEPERAUXCONTAINER_H
-- 
GitLab