Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/k4FWCore/PodioDataSvc.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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