Belle II Software development
ConcreteVariablesToHistogramPersistenceManager Class Reference

A PersistenceManager that writes variables to ROOT histograms (TH1D). More...

#include <ConcreteVariablesToHistogramPersistenceManager.h>

Inheritance diagram for ConcreteVariablesToHistogramPersistenceManager:
PersistenceManager

Public Member Functions

 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.
 
void addEntry (const EvaluatedVariables &evaluatedVariables) override
 Fills histograms with the current set of evaluated variables.
 
void store () override
 Writes histogram data to disk.
 

Private Member Functions

void openFileWithGuards ()
 Safely opens the ROOT file specified by m_fileName.
 
void registerHistograms ()
 Registers (books) the histograms based on m_variables.
 

Private Attributes

std::string m_fileName
 Name of the ROOT file where histograms will be written.
 
std::string m_directory
 Name of the directory (folder) within the ROOT file where histograms are stored.
 
Variables m_variables
 A list of variables associated with the histograms.
 
std::shared_ptr< TFile > m_file {nullptr}
 Pointer to the ROOT file object.
 
std::map< std::string, std::unique_ptr< StoreObjPtr< RootMergeable< TH1D > > > > m_histograms
 A map of histogram names to their respective objects (wrapped in RootMergeable).
 

Detailed Description

A PersistenceManager that writes variables to ROOT histograms (TH1D).

This class creates and manages one-dimensional histograms for a set of variables. It handles opening a ROOT file, booking the histograms, and filling them with user-supplied data each event. The histograms can be merged in a multi-process environment through the RootMergeable mechanism.

Definition at line 27 of file ConcreteVariablesToHistogramPersistenceManager.h.

Constructor & Destructor Documentation

◆ ConcreteVariablesToHistogramPersistenceManager()

Member Function Documentation

◆ addEntry()

void addEntry ( const EvaluatedVariables & evaluatedVariables)
overridevirtual

Fills histograms with the current set of evaluated variables.

Parameters
evaluatedVariablesA map of variable names to their current values.

Each call updates the corresponding histogram with the provided variable values.

Implements PersistenceManager.

Definition at line 33 of file ConcreteVariablesToHistogramPersistenceManager.cc.

34 {
35 for (const auto& [variableName, value] : evaluatedVariables) {
36 std::visit([&](auto&& val) {
37 (*m_histograms[variableName])->get().Fill(val);
38 }, value);
39 }
40 }

◆ initialize()

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

Initializes the manager by opening a ROOT file and creating histograms.

Parameters
fileNameThe name of the ROOT file to create or update.
directoryThe directory (folder) within the ROOT file where histograms will be placed.
variablesA list of variables that will be associated with the histograms.

This method configures the persistence manager with file/directory names and prepares the memory structures for histogram creation. It does not fill any data yet; that is handled by addEntry().

Implements PersistenceManager.

Definition at line 21 of file ConcreteVariablesToHistogramPersistenceManager.cc.

24 {
25 m_fileName = fileName;
26 m_directory = directoryName;
27 m_variables = variables;
28
29 openFileWithGuards();
30 registerHistograms();
31 }

◆ openFileWithGuards()

void openFileWithGuards ( )
private

Safely opens the ROOT file specified by m_fileName.

Throws an exception if the file cannot be opened for writing or if any other file-related error occurs.

Definition at line 61 of file ConcreteVariablesToHistogramPersistenceManager.cc.

62 {
63 m_file = RootFileCreationManager::getInstance().getFile(m_fileName);
64
65 if (!m_file) return;
66
67 TDirectory::TContext directoryGuard(m_file.get());
68 if (not m_directory.empty()) {
69 m_directory = MakeROOTCompatible::makeROOTCompatible(m_directory);
70 m_file->mkdir(m_directory.c_str());
71 m_file->cd(m_directory.c_str());
72 }
73 }

◆ registerHistograms()

void registerHistograms ( )
private

Registers (books) the histograms based on m_variables.

For each variable that is defined as "binned" (i.e., has bin information), a TH1D histogram is created in m_histograms.

Definition at line 75 of file ConcreteVariablesToHistogramPersistenceManager.cc.

76 {
77 for (const auto& variable : m_variables) {
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();
85
86 std::string compatibleName = MakeROOTCompatible::makeROOTCompatible(varStr);
87
88 auto ptr = std::make_unique<StoreObjPtr<RootMergeable<TH1D>>>("", DataStore::c_Persistent);
89 ptr->registerInDataStore(m_fileName + m_directory + varStr, DataStore::c_DontWriteOut);
90 ptr->construct(compatibleName.c_str(), compatibleName.c_str(), varNbins, low, high);
91
92 m_histograms[compatibleName] = std::move(ptr);
93 } else {
94 B2WARNING("Incompatible variable type. Skipping histogram registration.");
95 }
96 }, variable);
97 }
98 }

◆ store()

void store ( )
overridevirtual

Writes histogram data to disk.

After filling the histograms through addEntry(), this function ensures the data is written (or merged) into the ROOT file.

Implements PersistenceManager.

Definition at line 42 of file ConcreteVariablesToHistogramPersistenceManager.cc.

43 {
44 if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
45 TDirectory::TContext directoryGuard(m_file.get());
46 if (not m_directory.empty()) {
47 m_file->cd(m_directory.c_str());
48 }
49 B2INFO("Writing Histograms to " << gDirectory->GetPath());
50 for (auto it = m_histograms.begin(); it != m_histograms.end(); ++it) {
51 (*it->second)->write(gDirectory);
52 }
53 const bool writeError = m_file->TestBit(TFile::kWriteError);
54 m_file.reset();
55 if (writeError) {
56 B2FATAL("A write error occurred while saving '" << m_fileName << "', please check if enough disk space is available.");
57 }
58 }
59 }

Member Data Documentation

◆ m_directory

std::string m_directory
private

Name of the directory (folder) within the ROOT file where histograms are stored.

Definition at line 73 of file ConcreteVariablesToHistogramPersistenceManager.h.

◆ m_file

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

Pointer to the ROOT file object.

Definition at line 83 of file ConcreteVariablesToHistogramPersistenceManager.h.

83{nullptr};

◆ m_fileName

std::string m_fileName
private

Name of the ROOT file where histograms will be written.

Definition at line 68 of file ConcreteVariablesToHistogramPersistenceManager.h.

◆ m_histograms

std::map<std::string, std::unique_ptr<StoreObjPtr<RootMergeable<TH1D> > > > m_histograms
private

A map of histogram names to their respective objects (wrapped in RootMergeable).

Definition at line 88 of file ConcreteVariablesToHistogramPersistenceManager.h.

◆ m_variables

Variables m_variables
private

A list of variables associated with the histograms.

Definition at line 78 of file ConcreteVariablesToHistogramPersistenceManager.h.


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