Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-03 09:23:22

0001 #ifndef PODIO_ROOTWRITER_H
0002 #define PODIO_ROOTWRITER_H
0003 
0004 #include "podio/utilities/DatamodelRegistryIOHelpers.h"
0005 #include "podio/utilities/RootHelpers.h"
0006 
0007 #include "TFile.h"
0008 
0009 #include <string>
0010 #include <tuple>
0011 #include <unordered_map>
0012 #include <vector>
0013 
0014 // forward declarations
0015 class TTree;
0016 
0017 namespace podio {
0018 class Frame;
0019 class CollectionBase;
0020 class GenericParameters;
0021 
0022 /// The ROOTWriter writes podio files into ROOT files using TTrees.
0023 ///
0024 /// Each category gets its own TTree. Additionally, there is a podio_metadata
0025 /// TTree that contains metadata that is necessary for interpreting the files
0026 /// for reading.
0027 ///
0028 /// Files written with the ROOTWriter can be read with the ROOTReader.
0029 class ROOTWriter {
0030 public:
0031   /// Create a ROOTWriter to write to a file.
0032   ///
0033   /// @note Existing files will be overwritten without warning.
0034   ///
0035   /// @param filename The path to the file that will be created.
0036   ROOTWriter(const std::string& filename);
0037 
0038   /// ROOTWriter destructor
0039   ///
0040   /// This also takes care of writing all the necessary metadata to read files back again.
0041   ~ROOTWriter();
0042 
0043   /// The ROOTWriter is not copy-able
0044   ROOTWriter(const ROOTWriter&) = delete;
0045   /// The ROOTWriter is not copy-able
0046   ROOTWriter& operator=(const ROOTWriter&) = delete;
0047 
0048   /// Store the given frame with the given category.
0049   ///
0050   /// This stores all available collections from the Frame.
0051   ///
0052   /// @note The contents of the first Frame that is written in this way
0053   /// determines the contents that will be written for all subsequent Frames.
0054   ///
0055   /// @param frame    The Frame to store
0056   /// @param category The category name under which this Frame should be stored
0057   void writeFrame(const podio::Frame& frame, const std::string& category);
0058 
0059   /// Store the given Frame with the given category.
0060   ///
0061   /// This stores only the desired collections and not the complete frame.
0062   ///
0063   /// @note The contents of the first Frame that is written in this way
0064   /// determines the contents that will be written for all subsequent Frames.
0065   ///
0066   /// @param frame        The Frame to store
0067   /// @param category     The category name under which this Frame should be
0068   ///                     stored
0069   /// @param collsToWrite The collection names that should be written
0070   void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collsToWrite);
0071 
0072   /// Write the current file, including all the necessary metadata to read it
0073   /// again.
0074   ///
0075   /// @note The destructor will also call this, so letting a ROOTWriter go out
0076   /// of scope is also a viable way to write a readable file
0077   void finish();
0078 
0079   /// Check whether the collsToWrite are consistent with the state of the passed
0080   /// category.
0081   ///
0082   /// @note This will only be a meaningful check if the first Frame of the passed
0083   /// category has already been written. Also, this check is rather expensive as
0084   /// it has to effectively do two set differences.
0085   ///
0086   ///
0087   /// @param collsToWrite The collection names that should be checked for
0088   ///                     consistency
0089   /// @param category     The category name for which consistency should be
0090   ///                     checked
0091   ///
0092   /// @returns two vectors of collection names. The first one contains all the
0093   /// names that were missing from the collsToWrite but were present in the
0094   /// category. The second one contains the names that are present in the
0095   /// collsToWrite only. If both vectors are empty the category and the passed
0096   /// collsToWrite are consistent.
0097   std::tuple<std::vector<std::string>, std::vector<std::string>>
0098   checkConsistency(const std::vector<std::string>& collsToWrite, const std::string& category) const;
0099 
0100 private:
0101   /// Helper struct to group together all necessary state to write / process a
0102   /// given category. Created during the first writing of a category
0103   struct CategoryInfo {
0104     TTree* tree{nullptr};                                    ///< The TTree to which this category is written
0105     std::vector<root_utils::CollectionBranches> branches{};  ///< The branches for this category
0106     std::vector<root_utils::CollectionWriteInfo> collInfo{}; ///< Collection info for this category
0107     std::vector<std::string> collsToWrite{};                 ///< The collections to write for this category
0108 
0109     // Storage for the keys & values of all the parameters of this category
0110     // (resp. at least the current entry)
0111     root_utils::ParamStorage<int> intParams{};
0112     root_utils::ParamStorage<float> floatParams{};
0113     root_utils::ParamStorage<double> doubleParams{};
0114     root_utils::ParamStorage<std::string> stringParams{};
0115   };
0116 
0117   /// Initialize the branches for this category
0118   void initBranches(CategoryInfo& catInfo, const std::vector<root_utils::StoreCollection>& collections,
0119                     const podio::GenericParameters& parameters);
0120 
0121   /// Get the (potentially uninitialized category information for this category)
0122   CategoryInfo& getCategoryInfo(const std::string& category);
0123 
0124   static void resetBranches(CategoryInfo& categoryInfo, const std::vector<root_utils::StoreCollection>& collections);
0125 
0126   /// Fill the parameter keys and values into the CategoryInfo storage
0127   static void fillParams(CategoryInfo& catInfo, const GenericParameters& params);
0128 
0129   TFile m_file;                                                 ///< The storage file
0130   std::unordered_map<std::string, CategoryInfo> m_categories{}; ///< All categories
0131 
0132   DatamodelDefinitionCollector m_datamodelCollector{};
0133 };
0134 
0135 } // namespace podio
0136 
0137 #endif // PODIO_ROOTWRITER_H