Belle II Software  release-05-02-19
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. More...
 
 ~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. More...
 
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. More...
 

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

28  {
29  TTree* tmp{nullptr};
30  file->GetObject(name.c_str(), tmp);
31  if (!tmp) throw std::runtime_error("No " + label + " tree found");
32  destination.reset(tmp);
33  };
34  // So get the trees from the file and store them in the members
35  getTree("persistent", c_treeNames[DataStore::c_Persistent], m_persistent);
36  getTree("event", c_treeNames[DataStore::c_Event], m_events);
37  // And finally check the number of entries in the persistent tree
38  if (auto npersistent = m_persistent->GetEntries(); npersistent != 1) {
39  throw std::runtime_error("Expected exactly one entry in persistent tree, found " + std::to_string(npersistent));
40  }
41  }
42 
43  const FileMetaData& RootFileInfo::getFileMetaData()
44  {
45  if (!m_metadata) {
46  // object not set yet, get it from file
47  FileMetaData* metadata{nullptr};
48  auto branchStatus = m_persistent->SetBranchAddress("FileMetaData", &metadata);
49  if (branchStatus < 0) throw std::runtime_error("Error retrieving FileMetaData branch: " + std::to_string(branchStatus));

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

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


The documentation for this class was generated from the following files:
Belle2::RootIOUtilities::RootFileInfo::m_persistent
std::unique_ptr< TTree > m_persistent
Pointer to the persistent tree.
Definition: RootFileInfo.h:58
Belle2::RootIOUtilities::c_treeNames
const std::string c_treeNames[]
Names of trees.
Definition: RootIOUtilities.cc:20
Belle2::RootIOUtilities::RootFileInfo::getFileMetaData
const FileMetaData & getFileMetaData()
Return the event metadata from the file.
Definition: RootFileInfo.cc:51
Belle2::DataStore::c_Persistent
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:62
Belle2::RootIOUtilities::RootFileInfo::m_events
std::unique_ptr< TTree > m_events
Pointer to the event tree.
Definition: RootFileInfo.h:60
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::RootIOUtilities::RootFileInfo::m_metadata
std::unique_ptr< FileMetaData > m_metadata
Pointer to the file metadata once it has been read.
Definition: RootFileInfo.h:62