LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
RootInputTree.cc
Go to the documentation of this file.
2 // vim: set sw=2:
3 
4 #include "TBranch.h"
5 #include "TFile.h"
6 #include "TLeaf.h"
7 #include "TTree.h"
11 #include <cstdint>
12 #include <string>
13 #include <utility>
14 
15 namespace art {
16 
17  RootInputTree::RootInputTree(cet::exempt_ptr<TFile> filePtr,
18  BranchType const branchType,
19  int64_t saveMemoryObjectThreshold,
20  cet::exempt_ptr<RootInputFile> primaryFile,
21  bool const compactSubRunRanges,
22  bool const missingOK)
23  : filePtr_{filePtr}
24  , branchType_{branchType}
25  , saveMemoryObjectThreshold_{saveMemoryObjectThreshold}
26  , primaryFile_{primaryFile}
27  , compactSubRunRanges_{compactSubRunRanges}
28  {
29  if (filePtr_) {
30  tree_ = static_cast<TTree*>(
31  filePtr->Get(BranchTypeToProductTreeName(branchType).c_str()));
32  metaTree_ = static_cast<TTree*>(
33  filePtr->Get(BranchTypeToMetaDataTreeName(branchType).c_str()));
34  }
35  if (tree_) {
36  auxBranch_ =
38  entries_ = tree_->GetEntries();
39  }
40  if (metaTree_) {
43  }
44  if (!(missingOK || isValid())) {
46  << "RootInputTree for branch type " << BranchTypeToString(branchType)
47  << " could not be initialized correctly from input file.\n";
48  }
49  }
50 
51  bool
53  {
54  if ((metaTree_ == nullptr) || (metaTree_->GetNbranches() == 0)) {
55  return tree_ && auxBranch_ && (tree_->GetNbranches() == 1);
56  }
58  }
59 
60  bool
61  RootInputTree::hasBranch(std::string const& branchName) const
62  {
63  return tree_->GetBranch(branchName.c_str()) != nullptr;
64  }
65 
66  void
68  {
69  assert(isValid());
70  TBranch* branch = tree_->GetBranch(pd.branchName().c_str());
71  assert(pd.present() == (branch != nullptr));
72  input::BranchInfo info{pd, branch};
73  branches_.emplace(key, std::move(info));
74  }
75 
76  void
77  RootInputTree::dropBranch(std::string const& branchName)
78  {
79  TBranch* branch = tree_->GetBranch(branchName.c_str());
80  if (!branch) {
81  return;
82  }
83  TObjArray* leaves = tree_->GetListOfLeaves();
84  int entries = leaves->GetEntries();
85  for (int i = 0; i < entries; ++i) {
86  auto leaf = reinterpret_cast<TLeaf*>((*leaves)[i]);
87  if (leaf == nullptr) {
88  continue;
89  }
90  TBranch* br = leaf->GetBranch();
91  if (br == nullptr) {
92  continue;
93  }
94  if (br->GetMother() == branch) {
95  leaves->Remove(leaf);
96  }
97  }
98  leaves->Compress();
99  tree_->GetListOfBranches()->Remove(branch);
100  tree_->GetListOfBranches()->Compress();
101  delete branch;
102  }
103 
104  std::unique_ptr<DelayedReader>
106  FileFormatVersion const fileFormatVersion,
107  cet::exempt_ptr<BranchIDLists const> branchIDLists,
108  BranchType const branchType,
109  EntryNumbers const& entrySet,
110  EventID const eID)
111  {
112  return makeDelayedReader(
113  fileFormatVersion, nullptr, branchIDLists, branchType, entrySet, eID);
114  }
115 
116  std::unique_ptr<DelayedReader>
118  FileFormatVersion const fileFormatVersion,
119  sqlite3* inputDB,
120  cet::exempt_ptr<BranchIDLists const> branchIDLists,
121  BranchType const branchType,
122  EntryNumbers const& entrySet,
123  EventID const eID)
124  {
125  return std::make_unique<RootDelayedReader>(fileFormatVersion,
126  inputDB,
127  entrySet,
128  branches_,
129  this,
131  primaryFile_,
132  branchIDLists,
133  branchType,
134  eID,
136  }
137 
138  void
139  RootInputTree::setCacheSize(unsigned int cacheSize) const
140  {
141  tree_->SetCacheSize(static_cast<Long64_t>(cacheSize));
142  }
143 
144  void
145  RootInputTree::setTreeMaxVirtualSize(int treeMaxVirtualSize)
146  {
147  if (treeMaxVirtualSize >= 0) {
148  tree_->SetMaxVirtualSize(static_cast<Long64_t>(treeMaxVirtualSize));
149  }
150  }
151 
152  void
154  {
155  // Note: An entry number of -1 is ok, this can be used
156  // to put the tree an an invalid entry.
157  entryNumber_ = theEntryNumber;
158  auto err = tree_->LoadTree(theEntryNumber);
159  if (err == -2) {
160  // FIXME: Throw an error here!
161  // FIXME: -2 means entry number too big.
162  }
163  }
164 
165  std::unique_ptr<BranchMapper>
167  {
168  return std::make_unique<BranchMapperWithReader>(productProvenanceBranch_,
169  entryNumber_);
170  }
171 
172 } // namespace art
std::string const & BranchTypeToProductTreeName(BranchType const bt)
Definition: BranchType.cc:71
std::string const & productProvenanceBranchName(BranchType const bt)
Definition: BranchType.cc:91
std::unique_ptr< DelayedReader > makeDelayedReader(FileFormatVersion, cet::exempt_ptr< BranchIDLists const > branchIDLists, BranchType, std::vector< EntryNumber > const &entrySet, EventID)
cet::exempt_ptr< TFile > filePtr_
std::string const & BranchTypeToMetaDataTreeName(BranchType const bt)
Definition: BranchType.cc:77
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
EntryNumber entries() const
int64_t const saveMemoryObjectThreshold_
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const bt)
Definition: BranchType.cc:83
input::EntryNumbers EntryNumbers
Definition: RootInputTree.h:78
BranchType branchType_
void setCacheSize(unsigned int cacheSize) const
TBranch * productProvenanceBranch_
cet::exempt_ptr< RootInputFile > primaryFile_
void addBranch(BranchKey const &, BranchDescription const &)
bool hasBranch(std::string const &branchName) const
EntryNumber entryNumber_
std::unique_ptr< BranchMapper > makeBranchMapper() const
void dropBranch(std::string const &branchName)
input::EntryNumber EntryNumber
Definition: RootInputTree.h:77
std::string const & BranchTypeToString(BranchType const bt)
Definition: BranchType.cc:65
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
RootInputTree(cet::exempt_ptr< TFile >, BranchType, int64_t saveMemoryObjectThreshold, cet::exempt_ptr< RootInputFile >, bool compactSubRunRanges=false, bool missingOK=false)
BranchType
Definition: BranchType.h:18
HLT enums.
bool const compactSubRunRanges_
std::string const & branchName() const
void setEntryNumber(EntryNumber theEntryNumber)
EntryNumber entries_
bool isValid() const