9#include <reconstruction/persistenceManager/ConcreteVariablesToHistogramPersistenceManager.h>
11#include <framework/logging/Logger.h>
12#include <framework/pcore/ProcHandler.h>
13#include <framework/core/ModuleParam.templateDetails.h>
14#include <framework/core/Environment.h>
15#include <framework/utilities/MakeROOTCompatible.h>
16#include <framework/utilities/RootFileCreationManager.h>
18namespace Belle2::VariablePersistenceManager {
22 const std::string& directoryName,
35 for (
const auto& [variableName, value] : evaluatedVariables) {
36 std::visit([&](
auto&& val) {
45 TDirectory::TContext directoryGuard(
m_file.get());
49 B2INFO(
"Writing Histograms to " << gDirectory->GetPath());
51 (*it->second)->write(gDirectory);
53 const bool writeError =
m_file->TestBit(TFile::kWriteError);
56 B2FATAL(
"A write error occurred while saving '" <<
m_fileName <<
"', please check if enough disk space is available.");
67 TDirectory::TContext directoryGuard(
m_file.get());
78 std::visit([&](
const auto & typedVariable) {
79 using T = std::decay_t<
decltype(typedVariable)>;
80 if constexpr(std::is_same_v<T, BinnedVariable>) {
81 std::string varStr = typedVariable.getName();
82 int varNbins = typedVariable.getNbins();
83 float low = typedVariable.getLowBin();
84 float high = typedVariable.getHighBin();
90 ptr->construct(compatibleName.c_str(), compatibleName.c_str(), varNbins, low, high);
94 B2WARNING(
"Incompatible variable type. Skipping histogram registration.");
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
@ c_Persistent
Object is available during entire execution time.
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
A list of variables associated with the histograms.
void store() override
Writes histogram data to disk.
std::string m_directory
Name of the directory (folder) within the ROOT file where histograms are stored.
void addEntry(const EvaluatedVariables &evaluatedVariables) override
Fills histograms with the current set of evaluated variables.
void registerHistograms()
Registers (books) the histograms based on m_variables.
std::string m_fileName
Name of the ROOT file where histograms will be written.
void openFileWithGuards()
Safely opens the ROOT file specified by m_fileName.
std::map< std::string, std::unique_ptr< StoreObjPtr< RootMergeable< TH1D > > > > m_histograms
A map of histogram names to their respective objects (wrapped in RootMergeable).
ConcreteVariablesToHistogramPersistenceManager()
Default constructor.
void initialize(const std::string &fileName, const std::string &directory, Variables &variables) override
Initializes the manager by opening a ROOT file and creating histograms.
std::shared_ptr< TFile > m_file
Pointer to the ROOT file object.
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.