Belle II Software development
RootFileInfo Class Reference

Helper class to factorize some necessary tasks when working with Belle2 output files. More...

#include <RootFileInfo.h>

Public Member Functions

 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 FileMetaDatagetFileMetaData ()
 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 (const 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.
 
TFile & getFile ()
 Return a reference to the underlying TFile for read access to non-tree objects.
 

Private Attributes

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< FileMetaDatam_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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ RootFileInfo()

RootFileInfo ( const std::string & filename)
explicit

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 21 of file RootFileInfo.cc.

22 {
23 // make sure TDirectory is reset after this function
24 TDirectory::TContext directoryGuard;
25 // Open the file
26 m_file.reset(TFile::Open(filename.c_str(), "READ"));
27 if (!m_file || !m_file->IsOpen()) throw std::invalid_argument("Could not open file");
28 // We want to get two trees the same way but only need that here ... lambda it is
29 auto getTree = [&file = this->m_file](const std::string & label, const std::string & name, std::unique_ptr<TTree>& destination) {
30 TTree* tmp{nullptr};
31 file->GetObject(name.c_str(), tmp);
32 if (!tmp) throw std::runtime_error("No " + label + " tree found");
33 destination.reset(tmp);
34 };
35 // So get the trees from the file and store them in the members
36 getTree("persistent", c_treeNames[DataStore::c_Persistent], m_persistent);
37 auto description = getFileMetaData().getDataDescription();
38 auto isNtuple = description.find("isNtupleMetaData");
39 if ((isNtuple == description.end()) || (isNtuple->second != "True")) {
40 getTree("event", c_treeNames[DataStore::c_Event], m_events);
41 }
42 // And finally check the number of entries in the persistent tree
43 if (auto npersistent = m_persistent->GetEntries(); npersistent != 1) {
44 throw std::runtime_error("Expected exactly one entry in persistent tree, found " + std::to_string(npersistent));
45 }
46 }

Member Function Documentation

◆ 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 94 of file RootFileInfo.cc.

95 {
96 // So let's get the list of existing branches
97 const auto& existing = getBranchNames(persistent);
98 // and fill a list of branches which are in required but not existing
99 std::vector<std::string> missing;
100 std::set_difference(required.begin(), required.end(), existing.begin(), existing.end(),
101 std::inserter(missing, missing.begin()));
102 // and if that is not empty we complain
103 if (!missing.empty()) {
104 std::stringstream message;
105 message << "Branches missing from event tree: ";
106 for (const auto& name : missing) {
107 message << name << ", ";
108 }
109 throw std::runtime_error(message.str());
110 }
111 }

◆ 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 62 of file RootFileInfo.cc.

63 {
64 // which set of branches are we looking at?
65 std::optional<std::set<std::string>>& cache{persistent ? m_persistentBranches : m_eventBranches};
66 // if we didn't create the list already do it now
67 if (!cache) {
68 TTree& tree{persistent ? *m_persistent :* m_events};
69 std::set<std::string> result;
70 for (const TObject* obj : * (tree.GetListOfBranches())) {
71 const auto* branch = dynamic_cast<const TBranch*>(obj);
72 if (!branch) throw std::runtime_error("Entry in list of branches is no branch");
73 result.emplace(branch->GetName());
74 }
75 // and remember
76 cache.emplace(std::move(result));
77 }
78 // and just return the content
79 return *cache;
80 }

◆ getEventTree()

TTree & getEventTree ( )
inline

Return a reference to the event tree.

Definition at line 42 of file RootFileInfo.h.

42{ return *m_events; }

◆ getFile()

TFile & getFile ( )
inline

Return a reference to the underlying TFile for read access to non-tree objects.

Definition at line 58 of file RootFileInfo.h.

58{ return *m_file; }

◆ getFileMetaData()

const FileMetaData & 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 48 of file RootFileInfo.cc.

49 {
50 if (!m_metadata) {
51 // object not set yet, get it from file
52 FileMetaData* metadata{nullptr};
53 auto branchStatus = m_persistent->SetBranchAddress("FileMetaData", &metadata);
54 if (branchStatus < 0) throw std::runtime_error("Error retrieving FileMetaData branch: " + std::to_string(branchStatus));
55 if (m_persistent->GetEntry(0) <= 0 || !metadata) throw std::runtime_error("Cannot read FileMetaData");
56 // and remembber
57 m_metadata.reset(metadata);
58 }
59 return *m_metadata;
60 }

◆ getNtupleBranchNames()

const std::set< std::string > getNtupleBranchNames ( const std::string & treeName)

Return a set of branch names residing in the passed tree.

Definition at line 82 of file RootFileInfo.cc.

83 {
84 std::set<std::string> branches;
85 auto* tree = dynamic_cast<TTree*>(m_file->Get(treeName.c_str()));
86 for (const TObject* obj : * (tree->GetListOfBranches())) {
87 const auto* branch = dynamic_cast<const TBranch*>(obj);
88 if (!branch) throw std::runtime_error("Entry in list of branches is no branch");
89 branches.emplace(branch->GetName());
90 }
91 return branches;
92 }

◆ getPersistentTree()

TTree & getPersistentTree ( )
inline

Return a reference to the persistent tree.

Definition at line 40 of file RootFileInfo.h.

40{ return *m_persistent; }

◆ getTreeNames()

const std::set< std::string > getTreeNames ( )

Return a set of event tree names in the file.

Definition at line 113 of file RootFileInfo.cc.

114 {
115 std::set<std::string> treeNames;
116 TList* keylist = m_file->GetListOfKeys();
117 TIter next(keylist);
118 while (TObject* keyObj = next()) {
119 auto* key = dynamic_cast<TKey*>(keyObj);
120 if (!key) continue;
121 if (strcmp(key->GetName(), "persistent") == 0) continue;
122 TClass* cls = TClass::GetClass(key->GetClassName());
123 if (cls && cls->InheritsFrom(TTree::Class())) {
124 treeNames.emplace(key->GetName());
125 }
126 }
127 return treeNames;
128 }

Member Data Documentation

◆ m_eventBranches

std::optional<std::set<std::string> > m_eventBranches
private

Cached set of event branch names.

Definition at line 71 of file RootFileInfo.h.

◆ m_events

std::unique_ptr<TTree> m_events
private

Pointer to the event tree.

Definition at line 65 of file RootFileInfo.h.

◆ m_file

std::unique_ptr<TFile> m_file
private

Pointer to the file object.

Definition at line 61 of file RootFileInfo.h.

◆ m_metadata

std::unique_ptr<FileMetaData> m_metadata
private

Pointer to the file metadata once it has been read.

Definition at line 67 of file RootFileInfo.h.

◆ m_persistent

std::unique_ptr<TTree> m_persistent
private

Pointer to the persistent tree.

Definition at line 63 of file RootFileInfo.h.

◆ m_persistentBranches

std::optional<std::set<std::string> > m_persistentBranches
private

Cached set of persistent branch names.

Definition at line 69 of file RootFileInfo.h.


The documentation for this class was generated from the following files: