Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:35:49

0001 /*
0002  * Copyright (c) 2014-2024 Key4hep-Project.
0003  *
0004  * This file is part of Key4hep.
0005  * See https://key4hep.github.io/key4hep-doc/ for further info.
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License");
0008  * you may not use this file except in compliance with the License.
0009  * You may obtain a copy of the License at
0010  *
0011  *     http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software
0014  * distributed under the License is distributed on an "AS IS" BASIS,
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016  * See the License for the specific language governing permissions and
0017  * limitations under the License.
0018  */
0019 #ifndef FWCORE_PODIODATASVC_H
0020 #define FWCORE_PODIODATASVC_H
0021 
0022 #include "GaudiKernel/DataSvc.h"
0023 #include "GaudiKernel/IConversionSvc.h"
0024 // PODIO
0025 #include <utility>
0026 #include "podio/CollectionBase.h"
0027 #include "podio/CollectionIDTable.h"
0028 #include "podio/Frame.h"
0029 #include "podio/ROOTReader.h"
0030 // Forward declarations
0031 #include "k4FWCore/DataWrapper.h"
0032 class DataWrapperBase;
0033 class PodioOutput;
0034 template <typename T> class MetaDataHandle;
0035 
0036 /** @class PodioEvtSvc EvtDataSvc.h
0037  *
0038  *   An EvtDataSvc for PODIO classes
0039  *
0040  *  @author B. Hegner
0041  */
0042 class PodioDataSvc : public DataSvc {
0043   template <typename T> friend class MetaDataHandle;
0044   friend class PodioOutput;
0045   friend class Lcio2EDM4hepTool;
0046 
0047 public:
0048   typedef std::vector<std::pair<std::string, podio::CollectionBase*>> CollRegistry;
0049 
0050   StatusCode initialize() final;
0051   StatusCode reinitialize() final;
0052   StatusCode finalize() final;
0053   StatusCode clearStore() final;
0054   StatusCode i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) final;
0055   StatusCode i_setRoot(std::string root_path, DataObject* pRootObj) final;
0056 
0057   /// Standard Constructor
0058   PodioDataSvc(const std::string& name, ISvcLocator* svc);
0059 
0060   // Use DataSvc functionality except where we override
0061   using DataSvc::registerObject;
0062   /// Overriding standard behaviour of evt service
0063   /// Register object with the data store.
0064   StatusCode registerObject(std::string_view parentPath, std::string_view fullPath, DataObject* pObject) final;
0065 
0066   const std::string_view getCollectionType(const std::string& collName);
0067 
0068   template <typename T> StatusCode readCollection(const std::string& collName) {
0069     DataObject* objectPtr = nullptr;
0070     if (DataSvc::findObject("/Event", "/" + collName, objectPtr)) {
0071       debug() << "Collection " << collName << " already read, not reading it again" << endmsg;
0072       return StatusCode::SUCCESS;
0073     }
0074     const T* collection(nullptr);
0075     collection = static_cast<const T*>(m_eventframe.get(collName));
0076     if (collection == nullptr) {
0077       error() << "Collection " << collName << " does not exist." << endmsg;
0078     }
0079     auto wrapper = new DataWrapper<T>;
0080     wrapper->setData(collection);
0081     m_podio_datawrappers.push_back(wrapper);
0082     return DataSvc::registerObject("/Event", "/" + collName, wrapper);
0083   }
0084 
0085   const podio::Frame& getEventFrame() const { return m_eventframe; }
0086 
0087   /// Resets caches of reader and event store, increases event counter
0088   void endOfRead();
0089 
0090   /// TODO: Make this private again after conversions have been properly solved
0091   podio::Frame& getMetaDataFrame() { return m_metadataframe; }
0092 
0093 private:
0094   /// PODIO reader for ROOT files
0095   podio::ROOTReader m_reader;
0096   /// PODIO Frame, used to initialise collections
0097   podio::Frame m_eventframe;
0098   /// PODIO Frame, used to store metadata
0099   podio::Frame m_metadataframe;
0100   /// Counter of the event number
0101   int m_eventNum{0};
0102   /// Number of events in the file / to process
0103   int m_numAvailableEvents{-1};
0104   int m_requestedEventMax{-1};
0105   /// Whether reading from file at all
0106   bool m_reading_from_file{false};
0107 
0108   SmartIF<IConversionSvc> m_cnvSvc;
0109 
0110   // Registry of data wrappers; needed for memory management
0111   std::vector<DataWrapperBase*> m_podio_datawrappers;
0112 
0113 protected:
0114   /// ROOT file name the input is read from. Set by option filename
0115   std::vector<std::string> m_filenames;
0116   std::string              m_filename;
0117   /// Jump to nth events at the beginning. Set by option FirstEventEntry
0118   /// This option is helpful when we want to debug an event in the middle of a file
0119   unsigned m_1stEvtEntry{0};
0120   bool     m_bounds_check_needed{true};
0121 };
0122 #endif  // CORE_PODIODATASVC_H