PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ROOTFrameWriter.h
Go to the documentation of this file.
1#ifndef PODIO_ROOTFRAMEWRITER_H
2#define PODIO_ROOTFRAMEWRITER_H
3
7
8#include "TFile.h"
9
10#include <memory>
11#include <string>
12#include <tuple>
13#include <unordered_map>
14#include <vector>
15
16// forward declarations
17class TTree;
18
19namespace podio {
20class Frame;
21class CollectionBase;
22class GenericParameters;
23
25public:
26 ROOTFrameWriter(const std::string& filename);
27 ~ROOTFrameWriter() = default;
28
31
32 /** Store the given frame with the given category. Store all available
33 * collections from the Frame.
34 *
35 * NOTE: The contents of the first Frame that is written in this way
36 * determines the contents that will be written for all subsequent Frames.
37 */
38 void writeFrame(const podio::Frame& frame, const std::string& category);
39
40 /** Store the given Frame with the given category. Store only the
41 * collections that are passed.
42 *
43 * NOTE: The contents of the first Frame that is written in this way
44 * determines the contents that will be written for all subsequent Frames.
45 */
46 void writeFrame(const podio::Frame& frame, const std::string& category, const std::vector<std::string>& collsToWrite);
47
48 /** Write the current file, including all the necessary metadata to read it again.
49 */
50 void finish();
51
52private:
53 using StoreCollection = std::pair<const std::string&, podio::CollectionBase*>;
54
55 // collectionID, collectionType, subsetCollection
56 // NOTE: same as in rootUtils.h private header!
57 using CollectionInfoT = std::tuple<int, std::string, bool>;
58
59 /**
60 * Helper struct to group together all necessary state to write / process a
61 * given category. Created during the first writing of a category
62 */
63 struct CategoryInfo {
64 TTree* tree{nullptr}; ///< The TTree to which this category is written
65 std::vector<root_utils::CollectionBranches> branches{}; ///< The branches for this category
66 std::vector<CollectionInfoT> collInfo{}; ///< Collection info for this category
67 podio::CollectionIDTable idTable{}; ///< The collection id table for this category
68 std::vector<std::string> collsToWrite{}; ///< The collections to write for this category
69 };
70
71 /// Initialize the branches for this category
72 void initBranches(CategoryInfo& catInfo, const std::vector<StoreCollection>& collections,
73 /*const*/ podio::GenericParameters& parameters);
74
75 /// Get the (potentially uninitialized category information for this category)
76 CategoryInfo& getCategoryInfo(const std::string& category);
77
78 static void resetBranches(std::vector<root_utils::CollectionBranches>& branches,
79 const std::vector<ROOTFrameWriter::StoreCollection>& collections,
80 /*const*/ podio::GenericParameters* parameters);
81
82 std::unique_ptr<TFile> m_file{nullptr}; ///< The storage file
83 std::unordered_map<std::string, CategoryInfo> m_categories{}; ///< All categories
84
85 DatamodelDefinitionCollector m_datamodelCollector{};
86};
87
88} // namespace podio
89
90#endif // PODIO_ROOTFRAMEWRITER_H
ROOTFrameWriter(const ROOTFrameWriter &)=delete
ROOTFrameWriter & operator=(const ROOTFrameWriter &)=delete
void writeFrame(const podio::Frame &frame, const std::string &category)