RootOutputTree.h
Go to the documentation of this file.
1 #ifndef art_Framework_IO_Root_RootOutputTree_h
2 #define art_Framework_IO_Root_RootOutputTree_h
3 // vim: set sw=2:
4 
5 // Used by ROOT output modules.
6 
10 #include "cetlib/exempt_ptr.h"
11 
12 #include "TTree.h"
13 
14 #include <atomic>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <vector>
19 
20 class TFile;
21 class TBranch;
22 
23 namespace art {
25  public: // STATIC MEMBER FUNCTIONS
26  static TTree* makeTTree(TFile*, std::string const& name, int splitLevel);
27  // This routine MAY THROW if art converts
28  // a ROOT error message to an exception.
29  static void writeTTree(TTree*) noexcept(false);
30 
31  public: // MEMBER FUNCTIONS -- Special Member Functions
32  // Constructor for trees with no fast cloning
33  template <typename Aux>
35  BranchType const branchType,
36  Aux const*& pAux,
37  ProductProvenances*& pProductProvenanceVector,
38  int const bufSize,
39  int const splitLevel,
40  int64_t const treeMaxVirtualSize,
41  int64_t const saveMemoryObjectThreshold)
42  : filePtr_{filePtr}
43  {
44  tree_ = makeTTree(
45  filePtr.get(), BranchTypeToProductTreeName(branchType), splitLevel);
46  metaTree_ =
47  makeTTree(filePtr.get(), BranchTypeToMetaDataTreeName(branchType), 0);
48  fastCloningEnabled_ = false;
49  basketSize_ = bufSize;
50  splitLevel_ = splitLevel;
51  saveMemoryObjectThreshold_ = saveMemoryObjectThreshold;
52  nEntries_ = 0;
53  if (treeMaxVirtualSize >= 0) {
54  tree_.load()->SetMaxVirtualSize(treeMaxVirtualSize);
55  }
56  auto auxBranch = tree_.load()->Branch(
57  BranchTypeToAuxiliaryBranchName(branchType).c_str(), &pAux, bufSize, 0);
58  delete pAux;
59  pAux = nullptr;
60  readBranches_.push_back(auxBranch);
61  auto productProvenanceBranch = metaTree_.load()->Branch(
62  productProvenanceBranchName(branchType).c_str(),
63  &pProductProvenanceVector,
64  bufSize,
65  0);
66  metaBranches_.push_back(productProvenanceBranch);
67  }
68  RootOutputTree(RootOutputTree const&) = delete;
69  RootOutputTree& operator=(RootOutputTree const&) = delete;
70 
71  public: // MEMBER FUNCTIONS -- API
72  bool isValid() const;
74  void addOutputBranch(BranchDescription const&, void const*& pProd);
77  void fillTree();
78  void writeTree() const;
79  TTree*
80  tree() const
81  {
82  return tree_.load();
83  }
84  TTree*
85  metaTree() const
86  {
87  return metaTree_.load();
88  }
89  void
91  {
92  // The member trees are filled by filling their
93  // branches individually, which ends up not setting
94  // the tree entry count. Tell the trees to set their
95  // entry count based on their branches (all branches
96  // must have the same number of entries).
97  if (tree_.load()->GetNbranches() != 0) {
98  tree_.load()->SetEntries(-1);
99  }
100  if (metaTree_.load()->GetNbranches() != 0) {
101  metaTree_.load()->SetEntries(-1);
102  }
103  }
104  void
105  beginInputFile(bool fastCloning)
106  {
107  fastCloningEnabled_ = fastCloning;
108  }
109  bool
110  uncloned(std::string const& branchName) const
111  {
112  return unclonedReadBranchNames_.find(branchName) !=
114  }
115 
116  private: // MEMBER DATA
118  std::atomic<TTree*> tree_;
119  std::atomic<TTree*> metaTree_;
120  // does not include cloned branches
121  std::vector<TBranch*> producedBranches_{};
122  std::vector<TBranch*> metaBranches_{};
123  std::vector<TBranch*> readBranches_{};
124  std::vector<TBranch*> unclonedReadBranches_{};
125  std::set<std::string> unclonedReadBranchNames_{};
126  // The default for 'fastCloningEnabled_' is false so that SubRuns
127  // and Runs are not fast-cloned. We explicitly set this variable
128  // to true for the event tree.
129  std::atomic<bool> fastCloningEnabled_;
130  std::atomic<int> basketSize_;
131  std::atomic<int> splitLevel_;
132  std::atomic<int64_t> saveMemoryObjectThreshold_;
133  std::atomic<int> nEntries_;
134  };
135 } // namespace art
136 
137 // Local Variables:
138 // mode: c++
139 // End:
140 #endif /* art_Framework_IO_Root_RootOutputTree_h */
bool uncloned(std::string const &branchName) const
bool checkSplitLevelAndBasketSize(cet::exempt_ptr< TTree const >) const
std::string const & BranchTypeToProductTreeName(BranchType const bt)
Definition: BranchType.cc:71
std::string const & productProvenanceBranchName(BranchType const bt)
Definition: BranchType.cc:91
std::set< std::string > unclonedReadBranchNames_
std::string string
Definition: nybbler.cc:12
void writeTree() const
std::vector< TBranch * > readBranches_
std::string const & BranchTypeToMetaDataTreeName(BranchType const bt)
Definition: BranchType.cc:77
bool isValid() const
cet::exempt_ptr< TFile > filePtr_
RootOutputTree(cet::exempt_ptr< TFile > filePtr, BranchType const branchType, Aux const *&pAux, ProductProvenances *&pProductProvenanceVector, int const bufSize, int const splitLevel, int64_t const treeMaxVirtualSize, int64_t const saveMemoryObjectThreshold)
std::atomic< bool > fastCloningEnabled_
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const bt)
Definition: BranchType.cc:83
static void writeTTree(TTree *) noexcept(false)
std::vector< TBranch * > producedBranches_
std::atomic< int > nEntries_
TTree * tree() const
void addOutputBranch(BranchDescription const &, void const *&pProd)
std::atomic< int > splitLevel_
std::vector< TBranch * > unclonedReadBranches_
RootOutputTree & operator=(RootOutputTree const &)=delete
TTree * metaTree() const
void resetOutputBranchAddress(BranchDescription const &)
std::atomic< TTree * > tree_
static TTree * makeTTree(TFile *, std::string const &name, int splitLevel)
signed __int64 int64_t
Definition: stdint.h:131
BranchType
Definition: BranchType.h:18
bool fastCloneTree(cet::exempt_ptr< TTree const >)
std::vector< ProductProvenance > ProductProvenances
std::atomic< int > basketSize_
void beginInputFile(bool fastCloning)
std::atomic< TTree * > metaTree_
std::vector< TBranch * > metaBranches_
std::atomic< int64_t > saveMemoryObjectThreshold_