From 0d0350cbcde24bd7873fb6ca14047181d20343bb Mon Sep 17 00:00:00 2001
From: zoujh <zoujh@ihep.ac.cn>
Date: Mon, 28 Oct 2019 15:12:11 +0800
Subject: [PATCH] make LCIODataSvc be compatible with CEPCDataSvc

---
 Examples/options/LCIO_read.py         | 64 ++++++++++++++++-----------
 FWCore/FWCore/LCIODataSvc.h           |  2 +
 FWCore/src/components/LCIODataSvc.cpp | 32 +++++++++++---
 FWCore/src/components/LCIOInput.cpp   |  3 +-
 FWCore/src/components/LCIOInput.h     |  1 +
 5 files changed, 71 insertions(+), 31 deletions(-)

diff --git a/Examples/options/LCIO_read.py b/Examples/options/LCIO_read.py
index 07b4b0ee..8beafb93 100644
--- a/Examples/options/LCIO_read.py
+++ b/Examples/options/LCIO_read.py
@@ -2,9 +2,14 @@
 
 from Gaudi.Configuration import *
 
-from Configurables import LCIODataSvc
-# dsvc = LCIODataSvc("EventDataSvc", input="/cefs/data/FullSim/CEPC240/CEPC_v4/higgs/E240.Pe2e2h_bb.e0.p0.whizard195/e2e2h_bb.e0.p0.00001_000000_sim.slcio")
-dsvc = LCIODataSvc("EventDataSvc", input="/cefs/data/DstData/CEPC240/CEPC_v4/higgs/E240.Pe2e2h_X.e0.p0.whizard195/e2e2h_X.e0.p0.00001_001000_dst.slcio")
+from Configurables import LCIODataSvc, CEPCDataSvc
+
+svcname = "LCIODataSvc"
+rsvc = LCIODataSvc(svcname, inputs = [
+"/cefs/data/FullSim/CEPC240/CEPC_v4/higgs/smart_final_states/E240.Pffh_invi.e0.p0.whizard195//ffh_inv.e0.p0.00001_1000_sim.slcio"
+])
+
+wsvc = CEPCDataSvc("EventDataSvc")
 
 from Configurables import PlcioReadAlg
 alg = PlcioReadAlg("PlcioReadAlg")
@@ -15,33 +20,42 @@ from Configurables import LCIOInput
 lcioinput = LCIOInput("LCIOReader", collections=[
     "EventHeader",
     "MCParticle",
-    "COILCollection",
-    "EcalBarrelSiliconCollection",
-    "EcalBarrelSiliconPreShowerCollection",
-    "EcalEndcapRingCollection",
-    "EcalEndcapRingPreShowerCollection",
-    "EcalEndcapSiliconCollection",
-    "EcalEndcapSiliconPreShowerCollection",
-    "FTD_PIXELCollection",
-    "FTD_STRIPCollection",
-    "HcalBarrelCollection",
-    "HcalEndCapRingsCollection",
-    "HcalEndCapsCollection",
-    "LumiCalCollection",
-    "MuonBarrelCollection",
-    "MuonEndCapCollection",
-    "SETCollection",
-    "SITCollection",
-    "TPCCollection",
-    "TPCSpacePointCollection",
-    "VXDCollection"
+    "TPCCollection"
+    #"EventHeader",
+    #"MCParticle",
+    #"COILCollection",
+    #"EcalBarrelSiliconCollection",
+    #"EcalBarrelSiliconPreShowerCollection",
+    #"EcalEndcapRingCollection",
+    #"EcalEndcapRingPreShowerCollection",
+    #"EcalEndcapSiliconCollection",
+    #"EcalEndcapSiliconPreShowerCollection",
+    #"FTD_PIXELCollection",
+    #"FTD_STRIPCollection",
+    #"HcalBarrelCollection",
+    #"HcalEndCapRingsCollection",
+    #"HcalEndCapsCollection",
+    #"LumiCalCollection",
+    #"MuonBarrelCollection",
+    #"MuonEndCapCollection",
+    #"SETCollection",
+    #"SITCollection",
+    #"TPCCollection",
+    #"TPCSpacePointCollection",
+    #"VXDCollection"
     ])
+lcioinput.DataSvc = svcname
+
+from Configurables import PodioOutput
+plcioout = PodioOutput("PlcioWriter")
+plcioout.filename = "lcio2plcio.root"
+plcioout.outputCommands = ["keep *"]
 
 # ApplicationMgr
 from Configurables import ApplicationMgr
-ApplicationMgr( TopAlg = [lcioinput, alg],
+ApplicationMgr( TopAlg = [lcioinput, alg, plcioout],
                 EvtSel = 'NONE',
                 EvtMax = 10,
-                ExtSvc = [dsvc],
+                ExtSvc = [rsvc, wsvc],
                 OutputLevel=DEBUG
 )
diff --git a/FWCore/FWCore/LCIODataSvc.h b/FWCore/FWCore/LCIODataSvc.h
index 180f7b8d..8bfa3261 100644
--- a/FWCore/FWCore/LCIODataSvc.h
+++ b/FWCore/FWCore/LCIODataSvc.h
@@ -73,6 +73,8 @@ private:
   IO::LCReader* m_reader;
   /// LCIO reader for ROOT files
   plcio::EventHeaderCollection* pl_evtcol;
+  /// the handle of DataProvider
+  IDataProviderSvc* m_pIDP{nullptr};
   /// podio::ROOTReader m_reader;
   /// LCIO EventStore, used to initialise collections
   /// podio::EventStore m_provider;
diff --git a/FWCore/src/components/LCIODataSvc.cpp b/FWCore/src/components/LCIODataSvc.cpp
index 4901b1ce..3656f3ff 100644
--- a/FWCore/src/components/LCIODataSvc.cpp
+++ b/FWCore/src/components/LCIODataSvc.cpp
@@ -27,6 +27,14 @@ StatusCode LCIODataSvc::initialize() {
   m_cnvSvc = svc_loc->service("EventPersistencySvc");
   status = setDataLoader(m_cnvSvc);
 
+  if ( name() != "EventDataSvc" ) {
+    service("EventDataSvc", m_pIDP, true);
+    if ( m_pIDP == nullptr ) {
+      error() << "Could not get the EventDataSvc instance" << endmsg;
+      return StatusCode::FAILURE;
+    }
+  }
+
   m_reader = IOIMPL::LCFactory::getInstance()->createLCReader();
 
   if (m_filename != "") {
@@ -115,6 +123,8 @@ LCIODataSvc::~LCIODataSvc() {}
 
 
 StatusCode LCIODataSvc::readCollection(const std::string& collName, int collectionID) {
+
+  StatusCode stat = StatusCode::SUCCESS;
   podio::CollectionBase* collection(nullptr);
 
   if( evt == nullptr ){
@@ -135,9 +145,14 @@ StatusCode LCIODataSvc::readCollection(const std::string& collName, int collecti
     int id = m_collectionIDs->add("EventHeader");
     pl_evtcol->setID(id);
     wrapper->setData(pl_evtcol);
-    m_readCollections.emplace_back(std::make_pair("EventHeader", pl_evtcol));
 
-    DataSvc::registerObject("EventHeader", wrapper);
+    if ( m_pIDP ) {
+      m_pIDP->registerObject("EventHeader", wrapper);
+    }
+    else {
+      m_readCollections.emplace_back(std::make_pair("EventHeader", pl_evtcol));
+      DataSvc::registerObject("EventHeader", wrapper);
+    }
   }
 
   debug() << "reading collection name: " << collName  << "." << endmsg;
@@ -147,7 +162,8 @@ StatusCode LCIODataSvc::readCollection(const std::string& collName, int collecti
   if( it != vec_colns.end() ){
     lc_col = evt->getCollection(collName); 
   }
-  else return StatusCode::SUCCESS;
+  else
+    return stat;
 //  debug() << "Got collection: " << collName  << "." << endmsg;
 
   std::string TypeName = lc_col->getTypeName();
@@ -167,11 +183,17 @@ StatusCode LCIODataSvc::readCollection(const std::string& collName, int collecti
   int id = m_collectionIDs->add(collName);
   collection->setID(id);
   wrapper->setData(collection);
-  m_readCollections.emplace_back(std::make_pair(collName, collection));
 
 //  info() << "readCollection completed." << endmsg;
 
-  return DataSvc::registerObject(collName, wrapper);
+  if ( m_pIDP ) {
+    stat = m_pIDP->registerObject(collName, wrapper);
+  }
+  else {
+    m_readCollections.emplace_back(std::make_pair(collName, collection));
+    stat = DataSvc::registerObject(collName, wrapper);
+  }
+  return stat;
 }
 
 StatusCode LCIODataSvc::registerObject(const std::string& fullPath, DataObject* pObject) {
diff --git a/FWCore/src/components/LCIOInput.cpp b/FWCore/src/components/LCIOInput.cpp
index c6a81070..dab64588 100644
--- a/FWCore/src/components/LCIOInput.cpp
+++ b/FWCore/src/components/LCIOInput.cpp
@@ -14,7 +14,8 @@ StatusCode LCIOInput::initialize() {
   if (GaudiAlgorithm::initialize().isFailure()) return StatusCode::FAILURE;
 
   // check whether we have the LCIOEvtSvc active
-  m_LCIODataSvc = dynamic_cast<LCIODataSvc*>(evtSvc().get());
+  auto pSvc = service( m_dataSvc );
+  m_LCIODataSvc = dynamic_cast<LCIODataSvc*>(pSvc.get());
   if (nullptr == m_LCIODataSvc) return StatusCode::FAILURE;
 
   auto idTable = m_LCIODataSvc->getCollectionIDs();
diff --git a/FWCore/src/components/LCIOInput.h b/FWCore/src/components/LCIOInput.h
index f8492279..1ede535f 100644
--- a/FWCore/src/components/LCIOInput.h
+++ b/FWCore/src/components/LCIOInput.h
@@ -33,6 +33,7 @@ public:
 
 private:
   /// Name of collections to read. Set by option collections (this is temporary)
+  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "LCIOInputSvc" };
   Gaudi::Property<std::vector<std::string>> m_collectionNames{this, "collections", {}, "Places of collections to read"};
   /// Collection IDs (retrieved with CollectionIDTable from ROOT file, using collection names)
   std::vector<int> m_collectionIDs;
-- 
GitLab