Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:25:08

0001 #ifndef PODIO_UTILITIES_ROOTHELPERS_H
0002 #define PODIO_UTILITIES_ROOTHELPERS_H
0003 
0004 #include "podio/GenericParameters.h"
0005 
0006 #include "ROOT/RVec.hxx"
0007 #include "TBranch.h"
0008 
0009 #include <string>
0010 #include <tuple>
0011 #include <vector>
0012 
0013 namespace podio {
0014 class CollectionBase;
0015 
0016 namespace root_utils {
0017 
0018   // A collection of additional information that describes the collection: the
0019   // collectionID, the collection (data) type, whether it is a subset
0020   // collection, and its schema version
0021   struct CollectionWriteInfo {
0022     uint32_t collectionID{static_cast<uint32_t>(-1)}; ///< collection id
0023     std::string dataType{};                           ///< The fully qualified data type of the collection
0024     bool isSubset{false};                             ///< Whether this collection is a subset collection or not
0025     unsigned int schemaVersion{0};                    ///< The schema version of the collection type
0026     std::string name{};                               ///< The name of the collection
0027     std::string storageType{};                        ///< The type in which the data is actually stored
0028   };
0029   // The format used until version 1.2
0030   using CollectionWriteInfoT = std::tuple<uint32_t, std::string, bool, unsigned int>;
0031 
0032   // for backwards compatibility
0033   using CollectionInfoWithoutSchemaT = std::tuple<int, std::string, bool>;
0034 
0035   /// A collection name and a base pointer grouped together for writing
0036   using StoreCollection = std::tuple<const std::string&, podio::CollectionBase*>;
0037 
0038   /// Small helper struct to collect all branches that are necessary to read or
0039   /// write a collection. Needed to cache the branch pointers and avoid having to
0040   /// get them from a TTree/TChain for every event.
0041   struct CollectionBranches {
0042     CollectionBranches() = default;
0043     ~CollectionBranches() = default;
0044     CollectionBranches(const CollectionBranches&) = delete;
0045     CollectionBranches& operator=(const CollectionBranches&) = delete;
0046     CollectionBranches(CollectionBranches&&) = default;
0047     CollectionBranches& operator=(CollectionBranches&&) = default;
0048 
0049     CollectionBranches(TBranch* dataBranch) : data(dataBranch) {
0050     }
0051 
0052     TBranch* data{nullptr};
0053     std::vector<TBranch*> refs{};
0054     std::vector<TBranch*> vecs{};
0055     std::vector<std::string> refNames{}; ///< The names of the relation branches
0056     std::vector<std::string> vecNames{}; ///< The names of the vector member branches
0057   };
0058 
0059   /// Pair of keys and values for one type of the ones that can be stored in
0060   /// GenericParameters
0061   template <typename T>
0062   struct ParamStorage {
0063     ParamStorage() = default;
0064     ~ParamStorage() = default;
0065     ParamStorage(const ParamStorage&) = delete;
0066     ParamStorage& operator=(const ParamStorage&) = delete;
0067     ParamStorage(ParamStorage&&) = default;
0068     ParamStorage& operator=(ParamStorage&&) = default;
0069 
0070     ParamStorage(std::tuple<std::vector<std::string>, std::vector<std::vector<T>>> keysValues) :
0071         keys(std::move(std::get<0>(keysValues))), values(std::move(std::get<1>(keysValues))) {
0072     }
0073 
0074     /// Get a pointer to the stored keys for binding it to a TBranch
0075     auto keysPtr() {
0076       m_keysPtr = &keys;
0077       return &m_keysPtr;
0078     }
0079 
0080     /// Get a pointer to the stored vectors for binding it to a TBranch
0081     auto valuesPtr() {
0082       m_valuesPtr = &values;
0083       return &m_valuesPtr;
0084     }
0085 
0086     std::vector<std::string> keys{};      ///< The keys for this type
0087     std::vector<std::vector<T>> values{}; ///< The values for this type
0088 
0089   private:
0090     std::vector<std::string>* m_keysPtr{nullptr};
0091     std::vector<std::vector<T>>* m_valuesPtr{nullptr};
0092   };
0093 
0094   GenericParameters
0095   loadParamsFrom(ROOT::VecOps::RVec<std::string> intKeys, ROOT::VecOps::RVec<std::vector<int>> intValues,
0096                  ROOT::VecOps::RVec<std::string> floatKeys, ROOT::VecOps::RVec<std::vector<float>> floatValues,
0097                  ROOT::VecOps::RVec<std::string> doubleKeys, ROOT::VecOps::RVec<std::vector<double>> doubleValues,
0098                  ROOT::VecOps::RVec<std::string> stringKeys, ROOT::VecOps::RVec<std::vector<std::string>> stringValues);
0099 
0100 } // namespace root_utils
0101 } // namespace podio
0102 
0103 #endif // PODIO_UTILITIES_ROOTHELPERS_H