EventNavigator.cc
Go to the documentation of this file.
2 
6 
7 #include "TBranch.h"
8 #include "TFile.h"
9 #include "TTree.h"
10 
11 #include <iostream>
12 
13 namespace gallery {
14 
15  EventNavigator::EventNavigator(std::vector<std::string> const& iFileNames)
16  : fileNames_{iFileNames}, numberOfFiles_(fileNames_.size())
17  {
18  if (fileNames_.empty()) {
19  fileEntry_ = 0;
20  } else {
23  nextFile();
24  }
26  }
27  if (isValid()) {
28  eventsTree_->LoadTree(eventEntry_);
29  }
30  }
31 
32  void
34  {
36  eventEntry_ = 0;
37  return;
38  }
40  nextFile();
41  if (isValid()) {
42  eventsTree_->LoadTree(eventEntry_);
43  }
44  }
45 
46  void
48  {
50  // normal case, go to next event in same file
51  ++eventEntry_;
52  } else {
53  // handle odd cases, calling next when already at the end
54  // or after a call to nextFile() left us on a file with
55  // no events. (Neither of these cases should occur in a
56  // normal loop over events)
57  nextFile();
58  }
59  // if we hit the end of the file go to the next file
60  // also skip empty files
63  nextFile();
64  }
65  if (isValid()) {
66  eventsTree_->LoadTree(eventEntry_);
67  }
68  }
69 
70  void
72  {
73  // We will never get here is we're dealing with more than one file.
74  --eventEntry_;
75  if (isValid()) {
76  eventsTree_->LoadTree(eventEntry_);
77  }
78  }
79 
80  void
82  {
84  if (isValid()) {
85  eventsTree_->LoadTree(eventEntry_);
86  }
87  }
88 
89  void
91  {
92  // Be careful with this function. If the next file is empty this
93  // will leave it not pointing at a valid event.
94  if (atEnd()) {
96  << "Illegal call to EventNavigator::nextFile() when atEnd() is true";
97  }
98 
99  if (file_) {
100  std::cout << "Closing file, read " << file_->GetBytesRead()
101  << " bytes in " << file_->GetReadCalls() << " transactions\n";
102  }
103 
104  ++fileEntry_;
105  if (atEnd()) {
107  eventEntry_ = 0;
108  file_ = nullptr;
109  eventsTree_ = nullptr;
110  eventAuxiliaryBranch_ = nullptr;
111  eventHistoryTree_ = nullptr;
112  eventHistoryBranch_ = nullptr;
115  historyMap_.clear();
116  return;
117  }
118  file_.reset(TFile::Open(fileNames_[fileEntry_].c_str()));
119  if (!file_ || file_->IsZombie()) {
121  << "Failed opening file \'" << fileNames_[fileEntry_] << "\'";
122  }
123  std::cout << "Successfully opened file " << fileNames_[fileEntry_]
124  << std::endl;
127  entriesInCurrentFile_ = eventsTree_->GetEntries();
128  eventEntry_ = 0;
131  historyMap_.clear();
132  return;
133  }
134 
135  art::EventAuxiliary const&
137  {
141  }
142  return eventAuxiliary_;
143  }
144 
145  art::History const&
147  {
149  eventHistoryBranch_->GetEntry(eventEntry_);
151  }
152  return eventHistory_;
153  }
154 
155  art::ProcessHistoryID const&
157  {
159  eventHistoryBranch_->GetEntry(eventEntry_);
161  }
163  }
164 
165  art::ProcessHistory const&
167  {
168 
169  if (historyMap_.empty()) {
170 
171  std::unique_ptr<TTree> metaDataTree{
172  file_->Get<TTree>(art::rootNames::metaDataTreeName().c_str())};
173 
174  if (!metaDataTree) {
176  }
177 
178  auto pHistMapPtr = &historyMap_;
179  TBranch* processHistoryBranch = metaDataTree->GetBranch(
180  art::rootNames::metaBranchRootName<art::ProcessHistoryMap>());
181 
182  if (!processHistoryBranch) {
184  art::rootNames::metaBranchRootName<art::ProcessHistoryMap>());
185  }
186  processHistoryBranch->SetAddress(&pHistMapPtr);
187  processHistoryBranch->GetEntry(0);
188  }
189  return historyMap_[processHistoryID()];
190  }
191 
192  TFile*
194  {
195  return file_.get();
196  }
197 
198  TTree*
200  {
201  return eventsTree_;
202  }
203 
204  void
206  {
207  eventsTree_ = file_->Get<TTree>(art::rootNames::eventTreeName().c_str());
208  if (eventsTree_ == nullptr) {
210  }
211  if (eventsTree_->GetEntries() < 0) {
213  << "Unable to get the number of entries in events TTree.\n"
214  "This might be a corrupted file.\n";
215  }
217  file_->Get<TTree>(art::rootNames::eventHistoryTreeName().c_str());
218  if (eventHistoryTree_ == nullptr) {
220  }
221  }
222 
223  void
225  {
226  eventAuxiliaryBranch_ = eventsTree_->GetBranch(
228 
229  if (eventAuxiliaryBranch_ == nullptr) {
231  }
233 
236 
237  if (eventHistoryBranch_ == nullptr) {
239  }
240  eventHistoryBranch_->SetAddress(&pEventHistory_);
241  }
242 } // namespace gallery
art::ProcessHistoryMap historyMap_
std::unique_ptr< TFile > file_
std::vector< std::string > fileNames_
QList< Entry > entry
TFile * getTFile() const
TTree * getTTree() const
std::string const & eventHistoryTreeName()
Definition: rootNames.cc:56
art::ProcessHistory const & processHistory() const
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const bt)
Definition: BranchType.cc:83
void goToEntry(long long entry)
art::ProcessHistoryID const & processHistoryID() const
void throwTreeNotFound(std::string const &treeName)
art::History * pEventHistory_
std::string const & metaDataTreeName()
Definition: rootNames.cc:42
long long previousEventHistoryEntry_
art::EventAuxiliary eventAuxiliary_
void throwBranchNotFound(std::string const &branchName)
EventNavigator(std::vector< std::string > const &iFileNames)
art::EventAuxiliary * pEventAuxiliary_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string const & eventHistoryBranchName()
Definition: rootNames.cc:63
ProcessHistoryID const & processHistoryID() const noexcept
Definition: History.cc:31
long long previousEventAuxiliaryEntry_
std::string const & eventTreeName()
Definition: rootNames.cc:69
art::History const & history() const
QTextStream & endl(QTextStream &s)
art::EventAuxiliary const & eventAuxiliary() const