Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-14 10:19:09

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 "podio/CollectionBase.h"
0026 #include "podio/CollectionIDTable.h"
0027 #include "podio/Frame.h"
0028 #include "podio/ROOTReader.h"
0029 #include <utility>
0030 // Forward declarations
0031 #include "k4FWCore/DataWrapper.h"
0032 class DataWrapperBase;
0033 class PodioOutput;
0034 namespace k4FWCore {
0035 template <typename T>
0036 class MetaDataHandle;
0037 }
0038 
0039 /** @class PodioEvtSvc EvtDataSvc.h
0040  *
0041  *   An EvtDataSvc for PODIO classes
0042  *
0043  *  @author B. Hegner
0044  */
0045 class PodioDataSvc : public DataSvc {
0046   template <typename T>
0047   friend class k4FWCore::MetaDataHandle;
0048   friend class PodioOutput;
0049   friend class Lcio2EDM4hepTool;
0050 
0051 public:
0052   typedef std::vector<std::pair<std::string, podio::CollectionBase*>> CollRegistry;
0053 
0054   StatusCode initialize() final;
0055   StatusCode reinitialize() final;
0056   StatusCode finalize() final;
0057   StatusCode clearStore() final;
0058   StatusCode i_setRoot(std::string root_path, IOpaqueAddress* pRootAddr) final;
0059   StatusCode i_setRoot(std::string root_path, DataObject* pRootObj) final;
0060 
0061   /// Standard Constructor
0062   PodioDataSvc(const std::string& name, ISvcLocator* svc);
0063 
0064   // Use DataSvc functionality except where we override
0065   using DataSvc::registerObject;
0066   /// Overriding standard behaviour of evt service
0067   /// Register object with the data store.
0068   StatusCode registerObject(std::string_view parentPath, std::string_view fullPath, DataObject* pObject) final;
0069 
0070   const std::string_view getCollectionType(const std::string& collName);
0071 
0072   template <typename T>
0073   StatusCode readCollection(const std::string& collName) {
0074     DataObject* objectPtr = nullptr;
0075     if (DataSvc::findObject("/Event", "/" + collName, objectPtr)) {
0076       debug() << "Collection " << collName << " already read, not reading it again" << endmsg;
0077       return StatusCode::SUCCESS;
0078     }
0079     const T* collection(nullptr);
0080     collection = static_cast<const T*>(m_eventframe.get(collName));
0081     if (collection == nullptr) {
0082       error() << "Collection " << collName << " does not exist." << endmsg;
0083     }
0084     auto wrapper = new DataWrapper<T>;
0085     wrapper->setData(collection);
0086     m_podio_datawrappers.push_back(wrapper);
0087     return DataSvc::registerObject("/Event", "/" + collName, wrapper);
0088   }
0089 
0090   const podio::Frame& getEventFrame() const { return m_eventframe; }
0091 
0092   /// Resets caches of reader and event store, increases event counter
0093   void endOfRead();
0094 
0095   /// TODO: Make this private again after conversions have been properly solved
0096   podio::Frame& getMetaDataFrame() { return m_metadataframe; }
0097 
0098   void setCollsToRead(const std::vector<std::string>& collsToRead) { m_collsToRead = collsToRead; }
0099 
0100 private:
0101   /// PODIO reader for ROOT files
0102   podio::ROOTReader m_reader;
0103   /// PODIO Frame, used to initialise collections
0104   podio::Frame m_eventframe;
0105   /// PODIO Frame, used to store metadata
0106   podio::Frame m_metadataframe;
0107   /// Counter of the event number
0108   int m_eventNum{0};
0109   /// Number of events in the file / to process
0110   int m_numAvailableEvents{-1};
0111   int m_requestedEventMax{-1};
0112   /// Whether reading from file at all
0113   bool m_reading_from_file{false};
0114 
0115   SmartIF<IConversionSvc> m_cnvSvc;
0116 
0117   // Registry of data wrappers; needed for memory management
0118   std::vector<DataWrapperBase*> m_podio_datawrappers;
0119   /// The names of the collections to read (set externally)
0120   std::vector<std::string> m_collsToRead{};
0121 
0122 protected:
0123   /// ROOT file name the input is read from. Set by option filename
0124   std::vector<std::string> m_filenames;
0125   std::string m_filename;
0126   /// Jump to nth events at the beginning. Set by option FirstEventEntry
0127   /// This option is helpful when we want to debug an event in the middle of a file
0128   unsigned m_1stEvtEntry{0};
0129   bool m_bounds_check_needed{true};
0130 };
0131 #endif // CORE_PODIODATASVC_H