9#include <reconstruction/persistenceManager/ConcreteVariablesToNtuplePersistenceManager.h>
11#include <framework/utilities/MakeROOTCompatible.h>
12#include <framework/utilities/RootFileCreationManager.h>
13#include <framework/pcore/ProcHandler.h>
16 using namespace Belle2::VariablePersistenceManager;
18 std::string typedVariableToLeafName(
const TypedVariable& variable)
20 std::map<VariableDataType, std::string> suffix = {
21 {VariableDataType::c_double,
"/D"},
22 {VariableDataType::c_int,
"/I"},
23 {VariableDataType::c_bool,
"/O"}
27 return branchName + suffix[variable.getDataType()];
31namespace Belle2::VariablePersistenceManager {
39 const std::string& treeName,
52 for (
const auto& [variableName, value] : evaluatedVariables) {
63 TDirectory::TContext directoryGuard{
m_file.get()};
66 const bool writeError =
m_file->TestBit(TFile::kWriteError);
69 B2FATAL(
"A write error occurred while saving '" <<
m_fileName <<
"', please check if enough disk space is available.");
77 B2FATAL(
"Output root file name is not set.");
82 B2FATAL(
"Could not create file: " <<
m_fileName <<
".");
85 TDirectory::TContext directoryGuard{
m_file.get()};
88 B2FATAL(
"A tree with the name: " <<
m_treeName <<
"already exists in the file: " <<
m_fileName <<
".");
93 m_tree->get().SetCacheSize(100000);
99 std::visit([&](
const auto & typedVariable) {
100 using T = std::decay_t<
decltype(typedVariable)>;
101 if constexpr(std::is_same_v<T, TypedVariable>) {
103 std::string leafName = typedVariableToLeafName(typedVariable);
105 switch (typedVariable.getDataType()) {
106 case VariableDataType::c_double:
110 case VariableDataType::c_int:
114 case VariableDataType::c_bool:
122 B2WARNING(
"Incompatible variable type. Skipping branch registration.");
130 const VariableType& evaluatedValue)
132 std::visit([&](
auto&& value) {
133 using T = std::decay_t<
decltype(value)>;
135 if constexpr(std::is_same_v<T, double>) {
137 }
else if constexpr(std::is_same_v<T, int>) {
139 }
else if constexpr(std::is_same_v<T, bool>) {
In the store you can park objects that have to be accessed by various modules.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
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.
Variables m_variables
The list of variables that will be written to the TTree.
void registerBranches()
Registers TTree branches for each variable in m_variables.
void store() override
Writes the current buffered data to disk.
void updateBranch(const std::string &variableName, const VariableType &value)
Updates the branch buffer for a given variable with a new value.
std::map< std::string, bool > m_branchesBool
Storage for bool branches.
int m_basketSize
Basket size for the TTree branches.
void addEntry(const EvaluatedVariables &evaluatedVariables) override
Adds a single event's worth of variable data to the TTree.
std::map< std::string, double > m_branchesDouble
Maps of variable names to storage for double, int, and bool TTree branches.
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.
std::string m_fileName
Name of the ROOT file where TTree data is stored.
void openFileWithGuards()
Safely opens the ROOT file specified by m_fileName.
ConcreteVariablesToNtuplePersistenceManager()
Default constructor.
StoreObjPtr< RootMergeable< TTree > > m_tree
A store pointer to the RootMergeable wrapper for the TTree.
std::map< std::string, int > m_branchesInt
Storage for int branches.
std::shared_ptr< TFile > m_file
Pointer to the ROOT file object.
std::string m_treeName
Name of the TTree that will be created and filled.
A variable with a specified data type.
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.