9 #include <analysis/modules/VariablesToHistogram/VariablesToHistogramModule.h>
11 #include <analysis/dataobjects/ParticleList.h>
12 #include <analysis/VariableManager/Manager.h>
13 #include <framework/logging/Logger.h>
14 #include <framework/pcore/ProcHandler.h>
15 #include <framework/utilities/MakeROOTCompatible.h>
16 #include <framework/core/ModuleParam.templateDetails.h>
17 #include <framework/utilities/RootFileCreationManager.h>
32 setDescription(
"Calculate variables specified by the user for a given ParticleList and save them into one or two dimensional histograms.");
33 setPropertyFlags(c_ParallelProcessingCertified | c_TerminateInAllProcesses);
35 std::vector<std::tuple<std::string, int, float, float>> emptylist;
36 std::vector<std::tuple<std::string, int, float, float, std::string, int, float, float>> emptylist_2d;
37 addParam(
"particleList", m_particleList,
38 "Name of particle list with reconstructed particles. If no list is provided the variables are saved once per event (only possible for event-type variables)",
40 addParam(
"variables", m_variables,
41 "List of variables to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
43 addParam(
"variables_2d", m_variables_2d,
44 "List of variable pairs to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
47 addParam(
"fileName", m_fileName,
"Name of ROOT file for output.",
string(
"VariablesToHistogram.root"));
48 addParam(
"directory", m_directory,
"Directory for all histograms **inside** the file to allow for histograms from multiple "
49 "particlelists in the same file without conflicts", m_directory);
54 void VariablesToHistogramModule::initialize()
56 if (not m_particleList.empty())
60 m_file = RootFileCreationManager::getInstance().getFile(m_fileName);
63 TDirectory::TContext directoryGuard(m_file.get());
64 if (not m_directory.empty()) {
66 m_file->mkdir(m_directory.c_str());
67 m_file->cd(m_directory.c_str());
70 for (
const auto& varTuple : m_variables) {
75 std::tie(varStr, varNbins, low, high) = varTuple;
78 auto ptr = std::make_unique<StoreObjPtr<RootMergeable<TH1D>>>(
"", DataStore::c_Persistent);
79 ptr->registerInDataStore(m_fileName + m_directory + varStr, DataStore::c_DontWriteOut);
80 ptr->construct(compatibleName.c_str(), compatibleName.c_str(), varNbins, low, high);
81 m_hists.emplace_back(std::move(ptr));
86 B2ERROR(
"Variable '" << varStr <<
"' is not available in Variable::Manager!");
88 m_functions.push_back(var->function);
92 for (
const auto& varTuple : m_variables_2d) {
101 std::tie(varStr1, varNbins1, low1, high1, varStr2, varNbins2, low2, high2) = varTuple;
105 auto ptr2d = std::make_unique<StoreObjPtr<RootMergeable<TH2D>>>(
"", DataStore::c_Persistent);
106 ptr2d->registerInDataStore(m_fileName + m_directory + varStr1 + varStr2, DataStore::c_DontWriteOut);
107 ptr2d->construct((compatibleName1 + compatibleName2).c_str(), (compatibleName1 + compatibleName2).c_str(),
108 varNbins1, low1, high1, varNbins2, low2, high2);
109 m_2d_hists.emplace_back(std::move(ptr2d));
114 B2ERROR(
"Variable '" << varStr1 <<
"' is not available in Variable::Manager!");
116 m_functions_2d_1.push_back(var1->function);
122 B2ERROR(
"Variable '" << varStr2 <<
"' is not available in Variable::Manager!");
124 m_functions_2d_2.push_back(var2->
function);
130 void VariablesToHistogramModule::event()
132 unsigned int nVars = m_variables.size();
133 unsigned int nVars_2d = m_variables_2d.size();
134 std::vector<float> vars(nVars);
135 std::vector<float> vars_2d_1(nVars_2d);
136 std::vector<float> vars_2d_2(nVars_2d);
138 if (m_particleList.empty()) {
139 for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
140 vars[iVar] = m_functions[iVar](
nullptr);
141 (*m_hists[iVar])->get().Fill(vars[iVar]);
143 for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
144 vars_2d_1[iVar] = m_functions_2d_1[iVar](
nullptr);
145 vars_2d_2[iVar] = m_functions_2d_2[iVar](
nullptr);
146 (*m_2d_hists[iVar])->get().Fill(vars_2d_1[iVar], vars_2d_2[iVar]);
151 unsigned int nPart = particlelist->getListSize();
152 for (
unsigned int iPart = 0; iPart < nPart; iPart++) {
153 const Particle* particle = particlelist->getParticle(iPart);
154 for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
155 vars[iVar] = m_functions[iVar](particle);
156 (*m_hists[iVar])->get().Fill(vars[iVar]);
158 for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
159 vars_2d_1[iVar] = m_functions_2d_1[iVar](particle);
160 vars_2d_2[iVar] = m_functions_2d_2[iVar](particle);
161 (*m_2d_hists[iVar])->get().Fill(vars_2d_1[iVar], vars_2d_2[iVar]);
167 void VariablesToHistogramModule::terminate()
169 if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
170 TDirectory::TContext directoryGuard(m_file.get());
171 if (not m_directory.empty()) {
172 m_file->cd(m_directory.c_str());
174 B2INFO(
"Writing Histograms to " << gDirectory->GetPath());
175 unsigned int nVars = m_variables.size();
176 for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
177 (*m_hists[iVar])->write(gDirectory);
179 unsigned int nVars_2d = m_variables_2d.size();
180 for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
181 (*m_2d_hists[iVar])->write(gDirectory);
184 const bool writeError = m_file->TestBit(TFile::kWriteError);
187 B2FATAL(
"A write error occurred while saving '" << m_fileName <<
"', please check if enough disk space is available.");
Class to store reconstructed particles.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
Module to calculate variables specified by the user for a given ParticleList and save them into an Hi...
std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
FunctionPtr function
Pointer to function.