Belle II Software development
ConcreteVariablesToHistogramPersistenceManager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <reconstruction/persistenceManager/ConcreteVariablesToHistogramPersistenceManager.h>
10
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>
17
18namespace Belle2::VariablePersistenceManager {
20
22 const std::string& directoryName,
23 Variables& variables)
24 {
25 m_fileName = fileName;
26 m_directory = directoryName;
27 m_variables = variables;
28
31 }
32
33 void ConcreteVariablesToHistogramPersistenceManager::addEntry(const EvaluatedVariables& evaluatedVariables)
34 {
35 for (const auto& [variableName, value] : evaluatedVariables) {
36 std::visit([&](auto&& val) {
37 (*m_histograms[variableName])->get().Fill(val);
38 }, value);
39 }
40 }
41
43 {
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 }
60
62 {
64
65 if (!m_file) return;
66
67 TDirectory::TContext directoryGuard(m_file.get());
68 if (not m_directory.empty()) {
70 m_file->mkdir(m_directory.c_str());
71 m_file->cd(m_directory.c_str());
72 }
73 }
74
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 }
99}
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
@ c_Persistent
Object is available during entire execution time.
Definition DataStore.h:60
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.
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.
std::map< std::string, std::unique_ptr< StoreObjPtr< RootMergeable< TH1D > > > > m_histograms
A map of histogram names to their respective objects (wrapped in RootMergeable).
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 > 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.