Helper class to factorize some necessary tasks when working with Belle2 output files.
More...
#include <RootFileInfo.h>
|
| | RootFileInfo (const std::string &filename) |
| | Create an object from a given filename or url.
|
| |
|
| ~RootFileInfo () |
| | Close the file and delete all structures associated with it.
|
| |
| TTree & | getPersistentTree () |
| | Return a reference to the persistent tree.
|
| |
| TTree & | getEventTree () |
| | Return a reference to the event tree.
|
| |
| const FileMetaData & | getFileMetaData () |
| | Return the event metadata from the file.
|
| |
| const std::set< std::string > & | getBranchNames (bool persistent=false) |
| | Return a set of branch names for either the event or the persistent tree.
|
| |
| const std::set< std::string > | getNtupleBranchNames (std::string treeName) |
| | Return a set of branch names residing in the passed tree.
|
| |
| const std::set< std::string > | getTreeNames () |
| | Return a set of event tree names in the file.
|
| |
| void | checkMissingBranches (const std::set< std::string > &required, bool persistent=false) |
| | Check if the event or persistent tree contain at least all the branches in the set of required branches.
|
| |
|
| std::unique_ptr< TFile > | m_file |
| | Pointer to the file object.
|
| |
| std::unique_ptr< TTree > | m_persistent |
| | Pointer to the persistent tree.
|
| |
| std::unique_ptr< TTree > | m_events |
| | Pointer to the event tree.
|
| |
| std::unique_ptr< FileMetaData > | m_metadata |
| | Pointer to the file metadata once it has been read.
|
| |
| std::optional< std::set< std::string > > | m_persistentBranches |
| | Cached set of persistent branch names.
|
| |
| std::optional< std::set< std::string > > | m_eventBranches |
| | Cached set of event branch names.
|
| |
Helper class to factorize some necessary tasks when working with Belle2 output files.
Like the correct way to obtain the FileMetaData from a file or to check the list of existing branches
Definition at line 27 of file RootFileInfo.h.
◆ RootFileInfo()
Create an object from a given filename or url.
This will open the file and read the tree headers. It will throw exceptions if it encounters any problems:
- std::invalid_argument if the file cannot be opened at all
- std::runtime_error if any of the trees is missing or the number of entries in the persistent tree is !=1
Definition at line 19 of file RootFileInfo.cc.
20 {
21
22 TDirectory::TContext directoryGuard;
23
24 m_file.reset(TFile::Open(filename.c_str(),
"READ"));
25 if (!
m_file || !
m_file->IsOpen())
throw std::invalid_argument(
"Could not open file");
26
27 auto getTree = [&file = this->
m_file](
const std::string & label,
const std::string & name, std::unique_ptr<TTree>& destination) {
28 TTree* tmp{nullptr};
29 file->GetObject(name.c_str(), tmp);
30 if (!tmp) throw std::runtime_error("No " + label + " tree found");
31 destination.reset(tmp);
32 };
33
36 auto isNtuple = description.find("isNtupleMetaData");
37 if ((isNtuple == description.end()) || (isNtuple->second != "True")) {
39 }
40
41 if (
auto npersistent =
m_persistent->GetEntries(); npersistent != 1) {
42 throw std::runtime_error("Expected exactly one entry in persistent tree, found " + std::to_string(npersistent));
43 }
44 }
@ c_Persistent
Object is available during entire execution time.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
const FileMetaData & getFileMetaData()
Return the event metadata from the file.
std::unique_ptr< TTree > m_events
Pointer to the event tree.
std::unique_ptr< TFile > m_file
Pointer to the file object.
std::unique_ptr< TTree > m_persistent
Pointer to the persistent tree.
const std::string c_treeNames[]
Names of trees.
◆ checkMissingBranches()
| void checkMissingBranches |
( |
const std::set< std::string > & |
required, |
|
|
bool |
persistent = false |
|
) |
| |
Check if the event or persistent tree contain at least all the branches in the set of required branches.
Throw a std::runtime_error if not
Definition at line 92 of file RootFileInfo.cc.
93 {
94
96
97 std::vector<std::string> missing;
98 std::set_difference(required.begin(), required.end(), existing.begin(), existing.end(),
99 std::inserter(missing, missing.begin()));
100
101 if (!missing.empty()) {
102 std::stringstream message;
103 message << "Branches missing from event tree: ";
104 for (const auto& name : missing) {
105 message << name << ", ";
106 }
107 throw std::runtime_error(message.str());
108 }
109 }
const std::set< std::string > & getBranchNames(bool persistent=false)
Return a set of branch names for either the event or the persistent tree.
◆ getBranchNames()
| const std::set< std::string > & getBranchNames |
( |
bool |
persistent = false | ) |
|
Return a set of branch names for either the event or the persistent tree.
Definition at line 60 of file RootFileInfo.cc.
61 {
62
64
65 if (!cache) {
67 std::set<std::string> result;
68 for (const TObject* obj : * (tree.GetListOfBranches())) {
69 const auto* branch = dynamic_cast<const TBranch*>(obj);
70 if (!branch) throw std::runtime_error("Entry in list of branches is no branch");
71 result.emplace(branch->GetName());
72 }
73
74 cache.emplace(std::move(result));
75 }
76
77 return *cache;
78 }
std::optional< std::set< std::string > > m_persistentBranches
Cached set of persistent branch names.
std::optional< std::set< std::string > > m_eventBranches
Cached set of event branch names.
◆ getEventTree()
Return a reference to the event tree.
Definition at line 42 of file RootFileInfo.h.
◆ getFileMetaData()
Return the event metadata from the file.
On the first call it will be read from the persistent tree. If this fails a std::runtime_error is thrown.
Definition at line 46 of file RootFileInfo.cc.
47 {
49
50 FileMetaData* metadata{nullptr};
51 auto branchStatus =
m_persistent->SetBranchAddress(
"FileMetaData", &metadata);
52 if (branchStatus < 0) throw std::runtime_error("Error retrieving FileMetaData branch: " + std::to_string(branchStatus));
53 if (
m_persistent->GetEntry(0) <= 0 || !metadata)
throw std::runtime_error(
"Cannot read FileMetaData");
54
56 }
58 }
std::unique_ptr< FileMetaData > m_metadata
Pointer to the file metadata once it has been read.
◆ getNtupleBranchNames()
| const std::set< std::string > getNtupleBranchNames |
( |
std::string |
treeName | ) |
|
Return a set of branch names residing in the passed tree.
Definition at line 80 of file RootFileInfo.cc.
81 {
82 std::set<std::string> branches;
83 auto* tree =
dynamic_cast<TTree*
>(
m_file->Get(treeName.c_str()));
84 for (const TObject* obj : * (tree->GetListOfBranches())) {
85 const auto* branch = dynamic_cast<const TBranch*>(obj);
86 if (!branch) throw std::runtime_error("Entry in list of branches is no branch");
87 branches.emplace(branch->GetName());
88 }
89 return branches;
90 }
◆ getPersistentTree()
| TTree & getPersistentTree |
( |
| ) |
|
|
inline |
Return a reference to the persistent tree.
Definition at line 40 of file RootFileInfo.h.
◆ getTreeNames()
| const std::set< std::string > getTreeNames |
( |
| ) |
|
Return a set of event tree names in the file.
Definition at line 111 of file RootFileInfo.cc.
112 {
113 std::set<std::string> treeNames;
114 TList* keylist =
m_file->GetListOfKeys();
115 TIter next(keylist);
116 while (auto&& key = next()) {
117 if (strcmp(key->GetName(), "persistent") != 0) {
118 treeNames.emplace(key->GetName());
119 }
120 }
121 return treeNames;
122 }
◆ m_eventBranches
| std::optional<std::set<std::string> > m_eventBranches |
|
private |
◆ m_events
| std::unique_ptr<TTree> m_events |
|
private |
◆ m_file
| std::unique_ptr<TFile> m_file |
|
private |
◆ m_metadata
Pointer to the file metadata once it has been read.
Definition at line 65 of file RootFileInfo.h.
◆ m_persistent
| std::unique_ptr<TTree> m_persistent |
|
private |
◆ m_persistentBranches
| std::optional<std::set<std::string> > m_persistentBranches |
|
private |
Cached set of persistent branch names.
Definition at line 67 of file RootFileInfo.h.
The documentation for this class was generated from the following files: