Belle II Software light-2406-ragdoll
RootFileInfo Class Reference

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

#include <RootFileInfo.h>

Collaboration diagram for RootFileInfo:

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

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

19 {
20 // make sure TDirectory is reset after this function
21 TDirectory::TContext directoryGuard;
22 // Open the file
23 m_file.reset(TFile::Open(filename.c_str(), "READ"));
24 if (!m_file || !m_file->IsOpen()) throw std::invalid_argument("Could not open file");
25 // We want to get two trees the same way but only need that here ... lambda it is
26 auto getTree = [&file = this->m_file](const std::string & label, const std::string & name, std::unique_ptr<TTree>& destination) {
27 TTree* tmp{nullptr};
28 file->GetObject(name.c_str(), tmp);
29 if (!tmp) throw std::runtime_error("No " + label + " tree found");
30 destination.reset(tmp);
31 };
32 // So get the trees from the file and store them in the members
33 getTree("persistent", c_treeNames[DataStore::c_Persistent], m_persistent);
34 getTree("event", c_treeNames[DataStore::c_Event], m_events);
35 // And finally check the number of entries in the persistent tree
36 if (auto npersistent = m_persistent->GetEntries(); npersistent != 1) {
37 throw std::runtime_error("Expected exactly one entry in persistent tree, found " + std::to_string(npersistent));
38 }
39 }
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
std::unique_ptr< TTree > m_events
Pointer to the event tree.
Definition: RootFileInfo.h:59
std::unique_ptr< TFile > m_file
Pointer to the file object.
Definition: RootFileInfo.h:55
std::unique_ptr< TTree > m_persistent
Pointer to the persistent tree.
Definition: RootFileInfo.h:57
const std::string c_treeNames[]
Names of trees.

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

76 {
77 // So let's get the list of existing branches
78 const auto& existing = getBranchNames(persistent);
79 // and fill a list of branches which are in required but not existing
80 std::vector<std::string> missing;
81 std::set_difference(required.begin(), required.end(), existing.begin(), existing.end(),
82 std::inserter(missing, missing.begin()));
83 // and if that is not empty we complain
84 if (!missing.empty()) {
85 std::stringstream message;
86 message << "Branches missing from event tree: ";
87 for (const auto& name : missing) {
88 message << name << ", ";
89 }
90 throw std::runtime_error(message.str());
91 }
92 }
const std::set< std::string > & getBranchNames(bool persistent=false)
Return a set of branch names for either the event or the persistent tree.
Definition: RootFileInfo.cc:55

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

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

◆ getEventTree()

TTree & getEventTree ( )
inline

Return a reference to the event tree.

Definition at line 42 of file RootFileInfo.h.

42{ return *m_events; }

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

42 {
43 if (!m_metadata) {
44 // object not set yet, get it from file
45 FileMetaData* metadata{nullptr};
46 auto branchStatus = m_persistent->SetBranchAddress("FileMetaData", &metadata);
47 if (branchStatus < 0) throw std::runtime_error("Error retrieving FileMetaData branch: " + std::to_string(branchStatus));
48 if (m_persistent->GetEntry(0) <= 0 || !metadata) throw std::runtime_error("Cannot read FileMetaData");
49 // and remembber
50 m_metadata.reset(metadata);
51 }
52 return *m_metadata;
53 }
std::unique_ptr< FileMetaData > m_metadata
Pointer to the file metadata once it has been read.
Definition: RootFileInfo.h:61

◆ getPersistentTree()

TTree & getPersistentTree ( )
inline

Return a reference to the persistent tree.

Definition at line 40 of file RootFileInfo.h.

40{ return *m_persistent; }

Member Data Documentation

◆ m_eventBranches

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

Cached set of event branch names.

Definition at line 65 of file RootFileInfo.h.

◆ m_events

std::unique_ptr<TTree> m_events
private

Pointer to the event tree.

Definition at line 59 of file RootFileInfo.h.

◆ m_file

std::unique_ptr<TFile> m_file
private

Pointer to the file object.

Definition at line 55 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 61 of file RootFileInfo.h.

◆ m_persistent

std::unique_ptr<TTree> m_persistent
private

Pointer to the persistent tree.

Definition at line 57 of file RootFileInfo.h.

◆ m_persistentBranches

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

Cached set of persistent branch names.

Definition at line 63 of file RootFileInfo.h.


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