Belle II Software development
ConcreteVariablesToNtuplePersistenceManager Class Reference

A PersistenceManager that writes variables to a ROOT TTree. More...

#include <ConcreteVariablesToNtuplePersistenceManager.h>

Inheritance diagram for ConcreteVariablesToNtuplePersistenceManager:
PersistenceManager

Public Member Functions

 ConcreteVariablesToNtuplePersistenceManager ()
 Default constructor.
 
void initialize (const std::string &fileName, const std::string &treeName, Variables &variables) override
 Initializes the manager by opening a ROOT file and preparing a TTree.
 
void addEntry (const EvaluatedVariables &evaluatedVariables) override
 Adds a single event's worth of variable data to the TTree.
 
void store () override
 Writes the current buffered data to disk.
 

Private Member Functions

void openFileWithGuards ()
 Safely opens the ROOT file specified by m_fileName.
 
void registerBranches ()
 Registers TTree branches for each variable in m_variables.
 
void updateBranch (const std::string &variableName, const VariableType &value)
 Updates the branch buffer for a given variable with a new value.
 

Private Attributes

std::string m_fileName
 Name of the ROOT file where TTree data is stored.
 
std::string m_treeName
 Name of the TTree that will be created and filled.
 
Variables m_variables
 The list of variables that will be written to the TTree.
 
int m_basketSize {1600}
 Basket size for the TTree branches.
 
std::shared_ptr< TFile > m_file {nullptr}
 Pointer to the ROOT file object.
 
StoreObjPtr< RootMergeable< TTree > > m_tree
 A store pointer to the RootMergeable wrapper for the TTree.
 
std::map< std::string, double > m_branchesDouble
 Maps of variable names to storage for double, int, and bool TTree branches.
 
std::map< std::string, int > m_branchesInt
 Storage for int branches.
 
std::map< std::string, bool > m_branchesBool
 Storage for bool branches.
 

Detailed Description

A PersistenceManager that writes variables to a ROOT TTree.

This class manages the creation and population of a ROOT TTree for a given set of variables. It handles opening a ROOT file, booking TTree branches, and filling them with user-supplied data each event. The TTree can then be merged in a multi-process environment through the RootMergeable mechanism.

Definition at line 32 of file ConcreteVariablesToNtuplePersistenceManager.h.

Constructor & Destructor Documentation

◆ ConcreteVariablesToNtuplePersistenceManager()

Default constructor.

Definition at line 32 of file ConcreteVariablesToNtuplePersistenceManager.cc.

34 {
35
36 }
@ c_Persistent
Object is available during entire execution time.
Definition DataStore.h:60
StoreObjPtr< RootMergeable< TTree > > m_tree
A store pointer to the RootMergeable wrapper for the TTree.

Member Function Documentation

◆ addEntry()

void addEntry ( const EvaluatedVariables & evaluatedVariables)
overridevirtual

Adds a single event's worth of variable data to the TTree.

Parameters
evaluatedVariablesMap of variable names to their values, which will be written to the TTree branches.

Each call updates the branch buffers for the registered variables. The actual storage of these buffers occurs when store() is called.

Implements PersistenceManager.

Definition at line 50 of file ConcreteVariablesToNtuplePersistenceManager.cc.

51 {
52 for (const auto& [variableName, value] : evaluatedVariables) {
53 std::string branchName = Belle2::MakeROOTCompatible::makeROOTCompatible(variableName.c_str());
54 updateBranch(branchName, value);
55 }
56 m_tree->get().Fill();
57 }
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
void updateBranch(const std::string &variableName, const VariableType &value)
Updates the branch buffer for a given variable with a new value.

◆ initialize()

void initialize ( const std::string & fileName,
const std::string & treeName,
Variables & variables )
overridevirtual

Initializes the manager by opening a ROOT file and preparing a TTree.

Parameters
fileNameName of the ROOT file to create or update.
treeNameName of the TTree to create.
variablesList of variables that will be stored in the TTree.

This method configures the persistence manager with file and tree names, and sets up the necessary memory structures. It does not actually write data; that is handled by addEntry().

Implements PersistenceManager.

Definition at line 38 of file ConcreteVariablesToNtuplePersistenceManager.cc.

◆ openFileWithGuards()

void openFileWithGuards ( )
private

Safely opens the ROOT file specified by m_fileName.

This function takes care of ensuring the file can be written to or created if it does not exist. Throws an exception on error.

Definition at line 74 of file ConcreteVariablesToNtuplePersistenceManager.cc.

75 {
76 if (m_fileName.empty()) {
77 B2FATAL("Output root file name is not set.");
78 }
80
81 if (not m_file) {
82 B2FATAL("Could not create file: " << m_fileName << ".");
83 }
84
85 TDirectory::TContext directoryGuard{m_file.get()};
86
87 if (m_file->Get(m_treeName.c_str())) {
88 B2FATAL("A tree with the name: " << m_treeName << "already exists in the file: " << m_fileName << ".");
89 }
90
92 m_tree.construct(m_treeName.c_str(), "");
93 m_tree->get().SetCacheSize(100000);
94 }
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
std::shared_ptr< TFile > getFile(std::string, bool ignoreErrors=false)
Get a file with a specific name, if is does not exist it will be created.
static RootFileCreationManager & getInstance()
Interface for the FileManager.

◆ registerBranches()

void registerBranches ( )
private

Registers TTree branches for each variable in m_variables.

Initializes branch addresses for each variable type (double, int, bool). Also sets the basket size using m_basketSize.

Definition at line 96 of file ConcreteVariablesToNtuplePersistenceManager.cc.

97 {
98 for (const auto& variable : m_variables) {
99 std::visit([&](const auto & typedVariable) {
100 using T = std::decay_t<decltype(typedVariable)>;
101 if constexpr(std::is_same_v<T, TypedVariable>) {
102 std::string branchName = Belle2::MakeROOTCompatible::makeROOTCompatible(typedVariable.getName().c_str());
103 std::string leafName = typedVariableToLeafName(typedVariable);
104
105 switch (typedVariable.getDataType()) {
106 case VariableDataType::c_double:
107 m_branchesDouble[branchName] = double{};
108 m_tree->get().Branch(branchName.c_str(), &m_branchesDouble[branchName], leafName.c_str());
109 break;
110 case VariableDataType::c_int:
111 m_branchesInt[branchName] = int{};
112 m_tree->get().Branch(branchName.c_str(), &m_branchesInt[branchName], leafName.c_str());
113 break;
114 case VariableDataType::c_bool:
115 m_branchesBool[branchName] = bool{};
116 m_tree->get().Branch(branchName.c_str(), &m_branchesBool[branchName], leafName.c_str());
117 break;
118 default:
119 break;
120 }
121 } else {
122 B2WARNING("Incompatible variable type. Skipping branch registration.");
123 }
124 }, variable);
125 }
126 m_tree->get().SetBasketSize("*", m_basketSize);
127 }
std::map< std::string, double > m_branchesDouble
Maps of variable names to storage for double, int, and bool TTree branches.

◆ store()

void store ( )
overridevirtual

Writes the current buffered data to disk.

After populating branch buffers through addEntry(), this function commits them to the ROOT file, ensuring data is safely stored.

Implements PersistenceManager.

Definition at line 59 of file ConcreteVariablesToNtuplePersistenceManager.cc.

60 {
62 B2INFO("Writing Ntuple: " << m_treeName);
63 TDirectory::TContext directoryGuard{m_file.get()};
64 m_tree->write(m_file.get());
65
66 const bool writeError = m_file->TestBit(TFile::kWriteError);
67 m_file.reset();
68 if (writeError) {
69 B2FATAL("A write error occurred while saving '" << m_fileName << "', please check if enough disk space is available.");
70 }
71 }
72 }
static bool isOutputProcess()
Return true if the process is an output process.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.

◆ updateBranch()

void updateBranch ( const std::string & variableName,
const VariableType & value )
private

Updates the branch buffer for a given variable with a new value.

Parameters
variableNameThe name of the variable (branch).
valueThe new value to be stored in the branch buffer.

Depending on the type of the variant, the appropriate map (m_branchesDouble, m_branchesInt, or m_branchesBool) is updated.

Definition at line 129 of file ConcreteVariablesToNtuplePersistenceManager.cc.

131 {
132 std::visit([&](auto&& value) {
133 using T = std::decay_t<decltype(value)>;
134
135 if constexpr(std::is_same_v<T, double>) {
136 m_branchesDouble[branchName] = value;
137 } else if constexpr(std::is_same_v<T, int>) {
138 m_branchesInt[branchName] = value;
139 } else if constexpr(std::is_same_v<T, bool>) {
140 m_branchesBool[branchName] = value;
141 }
142 }, evaluatedValue);
143 }

Member Data Documentation

◆ m_basketSize

int m_basketSize {1600}
private

Basket size for the TTree branches.

Affects I/O efficiency.

Definition at line 90 of file ConcreteVariablesToNtuplePersistenceManager.h.

90{1600};

◆ m_branchesBool

std::map<std::string, bool> m_branchesBool
private

Storage for bool branches.

Definition at line 107 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_branchesDouble

std::map<std::string, double> m_branchesDouble
private

Maps of variable names to storage for double, int, and bool TTree branches.

Storage for double branches.

Definition at line 105 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_branchesInt

std::map<std::string, int> m_branchesInt
private

Storage for int branches.

Definition at line 106 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_file

std::shared_ptr<TFile> m_file {nullptr}
private

Pointer to the ROOT file object.

Definition at line 95 of file ConcreteVariablesToNtuplePersistenceManager.h.

95{nullptr};

◆ m_fileName

std::string m_fileName
private

Name of the ROOT file where TTree data is stored.

Definition at line 75 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_tree

StoreObjPtr<RootMergeable<TTree> > m_tree
private

A store pointer to the RootMergeable wrapper for the TTree.

Definition at line 100 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_treeName

std::string m_treeName
private

Name of the TTree that will be created and filled.

Definition at line 80 of file ConcreteVariablesToNtuplePersistenceManager.h.

◆ m_variables

Variables m_variables
private

The list of variables that will be written to the TTree.

Definition at line 85 of file ConcreteVariablesToNtuplePersistenceManager.h.


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