Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 10:58:22

0001 #ifndef PODIO_USERDATACOLLECTION_H
0002 #define PODIO_USERDATACOLLECTION_H
0003 
0004 #include "podio/CollectionBase.h"
0005 #include "podio/CollectionBuffers.h"
0006 #include "podio/DatamodelRegistry.h"
0007 #include "podio/ObjectID.h"
0008 #include "podio/SchemaEvolution.h"
0009 #include "podio/detail/Pythonizations.h"
0010 #include "podio/utilities/TypeHelpers.h"
0011 
0012 #include <cstddef>
0013 #include <cstdint>
0014 #include <iostream>
0015 #include <string>
0016 #include <string_view>
0017 #include <tuple>
0018 #include <type_traits>
0019 #include <utility>
0020 #include <vector>
0021 
0022 #define PODIO_ADD_USER_TYPE(type)                                                                                      \
0023   template <>                                                                                                          \
0024   consteval const char* userDataTypeName<type>() {                                                                     \
0025     return #type;                                                                                                      \
0026   }                                                                                                                    \
0027   template <>                                                                                                          \
0028   consteval const char* userDataCollTypeName<type>() {                                                                 \
0029     return "podio::UserDataCollection<" #type ">";                                                                     \
0030   }
0031 
0032 namespace podio {
0033 
0034 /// tuple of basic types supported in user vector
0035 using SupportedUserDataTypes =
0036     std::tuple<float, double, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t>;
0037 
0038 /// Alias template to be used to enable template specializations only for the types listed in the
0039 /// SupportedUserDataTypes list
0040 /// EnableIfSupportedUserType is kept because cppyy does not seem to like
0041 /// when UsedDataCollection is used with the concept
0042 template <typename T>
0043 concept SupportedUserDataType = detail::isInTuple<T, SupportedUserDataTypes>;
0044 template <typename T>
0045 using EnableIfSupportedUserType = std::enable_if_t<detail::isInTuple<T, SupportedUserDataTypes>>;
0046 
0047 /// helper template to provide readable type names for basic types with macro
0048 /// PODIO_ADD_USER_TYPE(type)
0049 template <SupportedUserDataType BasicType>
0050 consteval const char* userDataTypeName();
0051 
0052 /// Helper template to provide the fully qualified name of a UserDataCollection.
0053 /// Implementations are populated by the PODIO_ADD_USER_TYPE macro.
0054 template <SupportedUserDataType BasicType>
0055 consteval const char* userDataCollTypeName();
0056 
0057 PODIO_ADD_USER_TYPE(float)
0058 PODIO_ADD_USER_TYPE(double)
0059 
0060 PODIO_ADD_USER_TYPE(int8_t)
0061 PODIO_ADD_USER_TYPE(int16_t)
0062 PODIO_ADD_USER_TYPE(int32_t)
0063 PODIO_ADD_USER_TYPE(int64_t)
0064 PODIO_ADD_USER_TYPE(uint8_t)
0065 PODIO_ADD_USER_TYPE(uint16_t)
0066 PODIO_ADD_USER_TYPE(uint32_t)
0067 PODIO_ADD_USER_TYPE(uint64_t)
0068 
0069 /// Collection of basic types for additional user data not defined in the EDM.
0070 /// The data is stored in an std::vector<basic_type>. Supported are all basic
0071 /// types supported in PODIO, i.e. float, double and 8-64 bit fixed size signed
0072 /// and unsigned integers - @see SupportedUserDataTypes.
0073 ///
0074 /// @author F.Gaede, DESY
0075 /// @date Sep 2021
0076 template <typename BasicType, typename = EnableIfSupportedUserType<BasicType>>
0077 class UserDataCollection : public CollectionBase {
0078 
0079 private:
0080   std::vector<BasicType> _vec{};
0081   // Pointer to the actual storage, necessary for I/O. In order to have
0082   // simpler move-semantics this will be set and properly initialized on
0083   // demand during the call to getBuffers
0084   std::vector<BasicType>* _vecPtr{nullptr};
0085   uint32_t m_collectionID{0};
0086   CollRefCollection m_refCollections{};
0087   VectorMembersInfo m_vecmem_info{};
0088 
0089 public:
0090   using value_type = typename std::vector<BasicType>::value_type;
0091   using mutable_type = value_type;
0092   using const_iterator = typename std::vector<BasicType>::const_iterator;
0093   using iterator = typename std::vector<BasicType>::iterator;
0094   using difference_type = typename std::vector<BasicType>::difference_type;
0095   using size_type = typename std::vector<BasicType>::size_type;
0096   using const_reverse_iterator = typename std::vector<BasicType>::const_reverse_iterator;
0097   using reverse_iterator = typename std::vector<BasicType>::reverse_iterator;
0098 
0099   UserDataCollection() = default;
0100   /// Constructor from an existing vector (which will be moved from!)
0101   UserDataCollection(std::vector<BasicType>&& vec) : _vec(std::move(vec)) {
0102   }
0103   UserDataCollection(const UserDataCollection&) = delete;
0104   UserDataCollection& operator=(const UserDataCollection&) = delete;
0105   UserDataCollection(UserDataCollection&&) = default;
0106   UserDataCollection& operator=(UserDataCollection&&) = default;
0107   ~UserDataCollection() override = default;
0108 #if defined(__cpp_lib_containers_ranges)
0109   /// Constructor to enable construction via std::ranges::to. See @ref from
0110   template <detail::RangeConvertibleTo<BasicType> R>
0111   UserDataCollection(std::from_range_t, R&& range) : UserDataCollection() {
0112     _vec = detail::to_vector<BasicType>(std::forward<R>(range));
0113   }
0114 #endif
0115 
0116   /// Construct a UserDataCollection from a range of values
0117   ///
0118   /// @param range A range of values that can convert to BasicType
0119   ///
0120   /// @returns a UserDataCollection populated with the values from the input
0121   ///          range
0122   template <detail::RangeConvertibleTo<BasicType> R>
0123   static UserDataCollection from(R&& range) {
0124     UserDataCollection coll;
0125     coll._vec = detail::to_vector<BasicType>(std::forward<R>(range));
0126     return coll;
0127   }
0128 
0129   /// The schema version of UserDataCollections
0130   static constexpr SchemaVersionT schemaVersion = 1;
0131 
0132   static constexpr std::string_view typeName = userDataCollTypeName<BasicType>();
0133   static constexpr std::string_view valueTypeName = userDataTypeName<BasicType>();
0134   static constexpr std::string_view dataTypeName = userDataTypeName<BasicType>();
0135 
0136   /// prepare buffers for serialization
0137   void prepareForWrite() const override {
0138   }
0139 
0140   /// re-create collection from buffers after read
0141   void prepareAfterRead() override {
0142   }
0143 
0144   /// initialize references after read
0145   bool setReferences(const ICollectionProvider*) override {
0146     return true;
0147   }
0148 
0149   /// set collection ID
0150   void setID(uint32_t id) override {
0151     m_collectionID = id;
0152   }
0153 
0154   /// get collection ID
0155   uint32_t getID() const override {
0156     return m_collectionID;
0157   }
0158 
0159   /// Get the collection buffers for this collection
0160   podio::CollectionWriteBuffers getBuffers() override {
0161     _vecPtr = &_vec; // Set the pointer to the correct internal vector
0162     return {&_vecPtr, _vecPtr, &m_refCollections, &m_vecmem_info};
0163   }
0164 
0165   /// check if the collection has a valid ID
0166   bool hasID() const override {
0167     return getID() != static_cast<uint32_t>(podio::ObjectID::untracked) &&
0168         getID() != static_cast<uint32_t>(podio::ObjectID::invalid);
0169   }
0170 
0171   /// number of elements in the collection
0172   size_t size() const override {
0173     return _vec.size();
0174   }
0175 
0176   /// maximal number of elements in the collection
0177   size_t max_size() const override {
0178     return _vec.max_size();
0179   }
0180 
0181   /// Is the collection empty
0182   bool empty() const override {
0183     return _vec.empty();
0184   }
0185 
0186   /// fully qualified type name
0187   const std::string_view getTypeName() const override {
0188     return typeName;
0189   }
0190 
0191   /// fully qualified type name of elements - with namespace
0192   const std::string_view getValueTypeName() const override {
0193     return valueTypeName;
0194   }
0195 
0196   /// fully qualified type name of stored POD elements - with namespace
0197   const std::string_view getDataTypeName() const override {
0198     return dataTypeName;
0199   }
0200 
0201   /// Cppyy protocol to setup the pythonizations for this class. Not to be called directly.
0202   static void __cppjit_pythonize__(PyObject* klass, const std::string& name) {
0203     podio::detail::pythonizations::pythonize_subscript(klass, name);
0204   }
0205   /// Pythonizations for ROOT <= 6.40 before the switch to cppjit
0206   static void __cppyy_pythonize__(PyObject* klass, const std::string& name) {
0207     __cppjit_pythonize__(klass, name);
0208   }
0209 
0210   /// clear the collection and all internal states. Invalidates all iterators.
0211   void clear() override {
0212     _vec.clear();
0213   }
0214 
0215   /// check if this collection is a subset collection - no subset possible
0216   bool isSubsetCollection() const override {
0217     return false;
0218   }
0219 
0220   /// declare this collection to be a subset collection - no effect
0221   void setSubsetCollection(bool) override {
0222   }
0223 
0224   /// The schema version is fixed manually
0225   SchemaVersionT getSchemaVersion() const final {
0226     return schemaVersion;
0227   }
0228 
0229   /// Print this collection to the passed stream
0230   void print(std::ostream& os = std::cout, bool flush = true) const override {
0231     os << "[";
0232     if (!_vec.empty()) {
0233       os << _vec[0];
0234       for (size_t i = 1; i < _vec.size(); ++i) {
0235         os << ", " << _vec[i];
0236       }
0237     }
0238     os << "]";
0239 
0240     if (flush) {
0241       os.flush(); // Necessary for python
0242     }
0243   }
0244 
0245   size_t getDatamodelRegistryIndex() const override {
0246     return DatamodelRegistry::NoDefinitionNecessary;
0247   }
0248 
0249   // ----- some wrappers for std::vector and access to the complete std::vector (if really needed)
0250 
0251   /// Invalidates iterators if the underlying vector reallocates.
0252   typename std::vector<BasicType>::reference create() {
0253     return _vec.emplace_back();
0254   }
0255 
0256   iterator begin() {
0257     return _vec.begin();
0258   }
0259   iterator end() {
0260     return _vec.end();
0261   }
0262   const_iterator begin() const {
0263     return _vec.begin();
0264   }
0265   const_iterator end() const {
0266     return _vec.end();
0267   }
0268   const_iterator cbegin() const {
0269     return _vec.cbegin();
0270   }
0271   const_iterator cend() const {
0272     return _vec.cend();
0273   }
0274   // reverse iterators
0275   reverse_iterator rbegin() {
0276     return _vec.rbegin();
0277   }
0278   const_reverse_iterator rbegin() const {
0279     return _vec.rbegin();
0280   }
0281   const_reverse_iterator crbegin() const {
0282     return _vec.crbegin();
0283   }
0284   reverse_iterator rend() {
0285     return _vec.rend();
0286   }
0287   const_reverse_iterator rend() const {
0288     return _vec.rend();
0289   }
0290   const_reverse_iterator crend() const {
0291     return _vec.crend();
0292   }
0293 
0294   typename std::vector<BasicType>::reference operator[](size_t idx) {
0295     return _vec[idx];
0296   }
0297   typename std::vector<BasicType>::const_reference operator[](size_t idx) const {
0298     return _vec[idx];
0299   }
0300 
0301   typename std::vector<BasicType>::reference at(size_t idx) {
0302     return _vec.at(idx);
0303   }
0304   typename std::vector<BasicType>::const_reference at(size_t idx) const {
0305     return _vec.at(idx);
0306   }
0307 
0308   /// Invalidates iterators if the underlying vector reallocates.
0309   void resize(size_t count) {
0310     _vec.resize(count);
0311   }
0312   /// Invalidates iterators if the underlying vector reallocates.
0313   void push_back(const BasicType& value) {
0314     _vec.push_back(value);
0315   }
0316 
0317   /// access to the actual data vector
0318   typename std::vector<BasicType>& vec() {
0319     return _vec;
0320   }
0321 
0322   /// const access to the actual data vector
0323   const typename std::vector<BasicType>& vec() const {
0324     return _vec;
0325   }
0326 };
0327 
0328 using UserDataCollectionTypes = decltype(std::apply(
0329     []<typename... Ts>(Ts...) { return utils::TypeList<UserDataCollection<Ts>...>{}; }, SupportedUserDataTypes{}));
0330 
0331 // don't make this macro public as it should only be used internally here...
0332 #undef PODIO_ADD_USER_TYPE
0333 
0334 template <SupportedUserDataType BasicType>
0335 std::ostream& operator<<(std::ostream& o, const podio::UserDataCollection<BasicType>& coll) {
0336   coll.print(o);
0337   return o;
0338 }
0339 
0340 // This is needed to avoid triggering opening every library in LD_LIBRARY_PATH
0341 // until it's fixed in ROOT. See https://github.com/root-project/root/issues/18489
0342 // and https://github.com/AIDASoft/podio/issues/770
0343 #if defined(__clang__)
0344   #pragma clang diagnostic push
0345   #pragma clang diagnostic ignored "-Wunknown-warning-option"
0346   #pragma clang diagnostic ignored "-Wdeprecated-redundant-constexpr-static-def"
0347   #pragma clang diagnostic ignored "-Wdeprecated"
0348 template <typename BasicType, typename U>
0349 constexpr std::string_view UserDataCollection<BasicType, U>::typeName;
0350 template <typename BasicType, typename U>
0351 constexpr std::string_view UserDataCollection<BasicType, U>::valueTypeName;
0352 template <typename BasicType, typename U>
0353 constexpr std::string_view UserDataCollection<BasicType, U>::dataTypeName;
0354   #pragma clang diagnostic pop
0355 #elif defined(__GNUC__)
0356   #pragma GCC diagnostic push
0357   #pragma GCC diagnostic ignored "-Wdeprecated"
0358 template <typename BasicType, typename U>
0359 constexpr std::string_view UserDataCollection<BasicType, U>::typeName;
0360 template <typename BasicType, typename U>
0361 constexpr std::string_view UserDataCollection<BasicType, U>::valueTypeName;
0362 template <typename BasicType, typename U>
0363 constexpr std::string_view UserDataCollection<BasicType, U>::dataTypeName;
0364   #pragma GCC diagnostic pop
0365 #endif
0366 
0367 } // namespace podio
0368 
0369 #endif