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> 
   28 VariablesToHistogramModule::VariablesToHistogramModule() :
 
   32   setDescription(
"Calculate variables specified by the user for a given ParticleList and save them into one or two dimensional histograms.");
 
   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;
 
   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)",
 
   41            "List of variables to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
 
   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);
 
   63   TDirectory::TContext directoryGuard(
m_file.get());
 
   75     std::tie(varStr, varNbins, low, high) = varTuple;
 
   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!");
 
  101     std::tie(varStr1, varNbins1, low1, high1, varStr2, varNbins2, low2, high2) = varTuple;
 
  107     ptr2d->construct((compatibleName1 + compatibleName2).c_str(), (compatibleName1 + compatibleName2).c_str(),
 
  108                      varNbins1, low1, high1, varNbins2, low2, high2);
 
  114       B2ERROR(
"Variable '" << varStr1 << 
"' is not available in Variable::Manager!");
 
  122       B2ERROR(
"Variable '" << varStr2 << 
"' is not available in Variable::Manager!");
 
  134   std::vector<float> vars(nVars);
 
  135   std::vector<float> vars_2d_1(nVars_2d);
 
  136   std::vector<float> vars_2d_2(nVars_2d);
 
  139     for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
 
  140       if (std::holds_alternative<double>(
m_functions[iVar](
nullptr))) {
 
  141         vars[iVar] = std::get<double>(
m_functions[iVar](
nullptr));
 
  142       } 
else if (std::holds_alternative<int>(
m_functions[iVar](
nullptr))) {
 
  143         vars[iVar] = std::get<int>(
m_functions[iVar](
nullptr));
 
  144       } 
else if (std::holds_alternative<bool>(
m_functions[iVar](
nullptr))) {
 
  145         vars[iVar] = std::get<bool>(
m_functions[iVar](
nullptr));
 
  147       (*
m_hists[iVar])->get().Fill(vars[iVar]);
 
  149     for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
 
  164       (*
m_2d_hists[iVar])->get().Fill(vars_2d_1[iVar], vars_2d_2[iVar]);
 
  169     unsigned int nPart = particlelist->getListSize();
 
  170     for (
unsigned int iPart = 0; iPart < nPart; iPart++) {
 
  171       const Particle* particle = particlelist->getParticle(iPart);
 
  172       for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
 
  173         if (std::holds_alternative<double>(
m_functions[iVar](particle))) {
 
  174           vars[iVar] = std::get<double>(
m_functions[iVar](particle));
 
  175         } 
else if (std::holds_alternative<int>(
m_functions[iVar](particle))) {
 
  176           vars[iVar] = std::get<int>(
m_functions[iVar](particle));
 
  177         } 
else if (std::holds_alternative<bool>(
m_functions[iVar](particle))) {
 
  178           vars[iVar] = std::get<bool>(
m_functions[iVar](particle));
 
  180         (*
m_hists[iVar])->get().Fill(vars[iVar]);
 
  182       for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
 
  187         } 
else if (std::holds_alternative<bool>(
m_functions_2d_1[iVar](particle))) {
 
  194         } 
else if (std::holds_alternative<bool>(
m_functions_2d_2[iVar](particle))) {
 
  197         (*
m_2d_hists[iVar])->get().Fill(vars_2d_1[iVar], vars_2d_2[iVar]);
 
  206     TDirectory::TContext directoryGuard(
m_file.get());
 
  210     B2INFO(
"Writing Histograms to " << gDirectory->GetPath());
 
  212     for (
unsigned int iVar = 0; iVar < nVars; iVar++) {
 
  213       (*
m_hists[iVar])->write(gDirectory);
 
  216     for (
unsigned int iVar = 0; iVar < nVars_2d; iVar++) {
 
  220     const bool writeError = 
m_file->TestBit(TFile::kWriteError);
 
  223       B2FATAL(
"A write error occurred while saving '" << 
m_fileName  << 
"', please check if enough disk space is available.");
 
@ 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.
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Class to store reconstructed particles.
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.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
static Manager & Instance()
get singleton instance.
std::string m_directory
Name of the Directory.
virtual void initialize() override
Initialises the module.
std::vector< std::tuple< std::string, int, float, float > > m_variables
List of variables to save.
virtual void event() override
Method called for each event.
virtual void terminate() override
Write TTree to file, and close file if necessary.
std::vector< Variable::Manager::FunctionPtr > m_functions_2d_1
List of function pointers corresponding to given variables.
std::string m_fileName
Name of ROOT file for output.
std::vector< std::tuple< std::string, int, float, float, std::string, int, float, float > > m_variables_2d
List of pairs of variables to save.
std::vector< Variable::Manager::FunctionPtr > m_functions
List of function pointers corresponding to given variables.
std::string m_particleList
Name of particle list with reconstructed particles.
std::shared_ptr< TFile > m_file
ROOT file for output.
std::vector< Variable::Manager::FunctionPtr > m_functions_2d_2
List of function pointers corresponding to given variables.
std::vector< std::unique_ptr< StoreObjPtr< RootMergeable< TH2D > > > > m_2d_hists
The ROOT TH2Ds for output.
std::vector< std::unique_ptr< StoreObjPtr< RootMergeable< TH1D > > > > m_hists
The ROOT TH1Ds for output.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
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.
#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.