From 7ce1cab7ff2255345282c3d16be7e69ae2e9b7f1 Mon Sep 17 00:00:00 2001
From: Matthew Joseph Basso <matthew.joseph.basso@cern.ch>
Date: Wed, 16 Aug 2023 09:50:54 -0700
Subject: [PATCH 1/2] Set nEntries to 0 if no tree is found in the input

---
 QFramework/Root/TQSampleInitializerBase.cxx | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/QFramework/Root/TQSampleInitializerBase.cxx b/QFramework/Root/TQSampleInitializerBase.cxx
index e854c579a..bebab2199 100644
--- a/QFramework/Root/TQSampleInitializerBase.cxx
+++ b/QFramework/Root/TQSampleInitializerBase.cxx
@@ -257,8 +257,8 @@ bool TQSampleInitializerBase::getTreeInformation(TQSample* sample, const TString
   }
   else {
     treeName = "CollectionTree";
-    TTree* tree =(TTree*)f->Get(treeName.Data());
-    nEntries =tree->GetEntries();
+    TTree* tree = (TTree*)f->Get(treeName.Data());
+    nEntries = (tree) ? tree->GetEntries() : 0;
     nEvents = 0;
   }
 
-- 
GitLab


From 925bca0aec9e3c8d00ebc4a7eb85c0d070555d68 Mon Sep 17 00:00:00 2001
From: Matthew Joseph Basso <matthew.joseph.basso@cern.ch>
Date: Wed, 16 Aug 2023 09:51:42 -0700
Subject: [PATCH 2/2] Only merge TruthMetaDataContainers if available in the
 input

---
 CAFxAODUtils/Root/TQxAODskimmingAlgorithm.cxx | 72 +++++++++----------
 1 file changed, 33 insertions(+), 39 deletions(-)

diff --git a/CAFxAODUtils/Root/TQxAODskimmingAlgorithm.cxx b/CAFxAODUtils/Root/TQxAODskimmingAlgorithm.cxx
index 76752ccd4..2c9cf724d 100644
--- a/CAFxAODUtils/Root/TQxAODskimmingAlgorithm.cxx
+++ b/CAFxAODUtils/Root/TQxAODskimmingAlgorithm.cxx
@@ -288,50 +288,44 @@ bool TQxAODskimmingAlgorithm::initializeSingleton(const std::vector<TQSample*>&
   }
 
   //Take care of TruthMetaData:
-  const xAOD::EventInfo* eventInfo = nullptr;
-  if( !this->fEvent->retrieve(eventInfo, "EventInfo").isSuccess() ) {
-    throw std::runtime_error("Failed to retrieve EventInfo!");
-    return false;
-  }
-  if( eventInfo->eventType(xAOD::EventInfo::EventType::IS_SIMULATION) ) { // Only needed for simulation
-    std::string tmdName = "TruthMetaData"; // Should be fixed
-    xAOD::TruthMetaDataContainer* outTmd = nullptr;
-    const xAOD::TruthMetaDataContainer* inTmd = nullptr;
-    if ( fEvent->retrieveMetaInput(inTmd,tmdName).isSuccess() ) {
-      int oldErrorIgnoreLevel = gErrorIgnoreLevel;
-      gErrorIgnoreLevel = 2000; //silence errors (we sort of expect them)
-      if (inTmd->size() == 0) {
-        throw std::runtime_error(TString::Format("No elements found in TruthMetaDataContainer named '%s'",tmdName.c_str()).Data());
+  std::string tmdName = "TruthMetaData"; // Should be fixed
+  xAOD::TruthMetaDataContainer* outTmd = nullptr;
+  const xAOD::TruthMetaDataContainer* inTmd = nullptr;
+  if( fEvent->retrieveMetaInput(inTmd,tmdName).isSuccess() ) {
+    int oldErrorIgnoreLevel = gErrorIgnoreLevel;
+    gErrorIgnoreLevel = 2000; //silence errors (we sort of expect them)
+    if (inTmd->size() == 0) {
+      throw std::runtime_error(TString::Format("No elements found in TruthMetaDataContainer named '%s'",tmdName.c_str()).Data());
+      return false;
+    }
+    else if (inTmd->size() > 1) {
+      WARNclass("%d elements found in TruthMetaDataContainer named '%s' while only 1 was expected", inTmd->size(), tmdName);
+    }
+    if ( fEvent->retrieveMetaOutput<xAOD::TruthMetaDataContainer>(outTmd,tmdName).isSuccess() ) { //we got a matching TruthMetaDataContainer in the output file, let's merge with the new one.
+      if (!truthMetaDataEqual(inTmd, outTmd)) {
+        throw std::runtime_error(TString::Format("Input/output TruthMetaDataContainers named '%s' are not equal - are they different DSIDs?", tmdName.c_str()).Data());
         return false;
       }
-      else if (inTmd->size() > 1) {
-        WARNclass("%d elements found in TruthMetaDataContainer named '%s' while only 1 was expected", inTmd->size(), tmdName);
+    } else {
+      xAOD::TruthMetaDataContainer* inCopy = new xAOD::TruthMetaDataContainer();
+      xAOD::TruthMetaDataAuxContainer* inCopyAux = new xAOD::TruthMetaDataAuxContainer();
+      inCopy->setStore(inCopyAux);
+      xAOD::TruthMetaData* ele = new xAOD::TruthMetaData();
+      inCopy->push_back(ele);
+      *ele = *(inTmd->at(0));
+      if (!fEvent->recordMeta<xAOD::TruthMetaDataContainer>(inCopy,tmdName).isSuccess()) {
+        throw std::runtime_error(TString::Format("Failed to record TruthMetaDataContainer '%s'",tmdName.c_str()).Data());
+        return false;
       }
-      if ( fEvent->retrieveMetaOutput<xAOD::TruthMetaDataContainer>(outTmd,tmdName).isSuccess() ) { //we got a matching TruthMetaDataContainer in the output file, let's merge with the new one.
-        if (!truthMetaDataEqual(inTmd, outTmd)) {
-          throw std::runtime_error(TString::Format("Input/output TruthMetaDataContainers named '%s' are not equal - are they different DSIDs?", tmdName.c_str()).Data());
-          return false;
-        }
-      } else {
-        xAOD::TruthMetaDataContainer* inCopy = new xAOD::TruthMetaDataContainer();
-        xAOD::TruthMetaDataAuxContainer* inCopyAux = new xAOD::TruthMetaDataAuxContainer();
-        inCopy->setStore(inCopyAux);
-        xAOD::TruthMetaData* ele = new xAOD::TruthMetaData();
-        inCopy->push_back(ele);
-        *ele = *(inTmd->at(0));
-        if (!fEvent->recordMeta<xAOD::TruthMetaDataContainer>(inCopy,tmdName).isSuccess()) {
-          throw std::runtime_error(TString::Format("Failed to record TruthMetaDataContainer '%s'",tmdName.c_str()).Data());
-          return false;
-        }
-        if (!fEvent->recordMeta<xAOD::TruthMetaDataAuxContainer>(inCopyAux,tmdName+"Aux.").isSuccess()) {
-          throw std::runtime_error(TString::Format("Failed to record TruthMetaDataAuxContainer '%sAux.'",tmdName.c_str()).Data());
-          return false;
-        }
+      if (!fEvent->recordMeta<xAOD::TruthMetaDataAuxContainer>(inCopyAux,tmdName+"Aux.").isSuccess()) {
+        throw std::runtime_error(TString::Format("Failed to record TruthMetaDataAuxContainer '%sAux.'",tmdName.c_str()).Data());
+        return false;
       }
-      gErrorIgnoreLevel = oldErrorIgnoreLevel;
-    } else {
-      WARNclass("Failed to obtain TruthMetaDataContainer named '%s' from input file",tmdName.c_str());
     }
+    gErrorIgnoreLevel = oldErrorIgnoreLevel;
+  }
+  else {
+    WARNclass("Failed to obtain TruthMetaDataContainer named '%s' from input file - perhaps you are processing data?",tmdName.c_str());
   }
 
   return true;
-- 
GitLab