9#include <framework/io/RootFileInfo.h>
10#include <framework/io/RootIOUtilities.h>
11#include <framework/dataobjects/FileMetaData.h>
12#include <framework/datastore/DataStore.h>
21 TDirectory::TContext directoryGuard;
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");
26 auto getTree = [&file = this->
m_file](
const std::string & label,
const std::string & name, std::unique_ptr<TTree>& destination) {
28 file->GetObject(name.c_str(), tmp);
29 if (!tmp)
throw std::runtime_error(
"No " + label +
" tree found");
30 destination.reset(tmp);
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));
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");
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());
69 cache.emplace(std::move(result));
80 std::vector<std::string> missing;
81 std::set_difference(required.begin(), required.end(), existing.begin(), existing.end(),
82 std::inserter(missing, missing.begin()));
84 if (!missing.empty()) {
85 std::stringstream message;
86 message <<
"Branches missing from event tree: ";
87 for (
const auto& name : missing) {
88 message << name <<
", ";
90 throw std::runtime_error(message.str());
@ 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.
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 branch...
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::unique_ptr< TFile > m_file
Pointer to the file object.
std::optional< std::set< std::string > > m_eventBranches
Cached set of event branch names.
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.
std::unique_ptr< TTree > m_persistent
Pointer to the persistent tree.
const std::set< std::string > & getBranchNames(bool persistent=false)
Return a set of branch names for either the event or the persistent tree.
Some constants and helpers common to the RootInput and RootOutput modules.
const std::string c_treeNames[]
Names of trees.