Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:10

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