9#include <analysis/modules/VariablesToNtuple/VariablesToNtupleModule.h>
12#include <analysis/dataobjects/ParticleList.h>
13#include <analysis/VariableManager/Manager.h>
14#include <analysis/VariableManager/Utility.h>
15#include <analysis/dataobjects/StringWrapper.h>
18#include <framework/logging/Logger.h>
19#include <framework/pcore/ProcHandler.h>
20#include <framework/core/ModuleParam.templateDetails.h>
21#include <framework/core/Environment.h>
22#include <framework/core/RandomNumbers.h>
23#include <framework/database/Database.h>
26#include <framework/utilities/MakeROOTCompatible.h>
27#include <framework/utilities/RootFileCreationManager.h>
28#include <framework/io/RootIOUtilities.h>
34using namespace RootIOUtilities;
44 setDescription(
"Calculate variables specified by the user for a given ParticleList and save them into a TNtuple. The TNtuple is candidate-based, meaning that the variables of each candidate are saved into separate rows.");
47 vector<string> emptylist;
49 "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)",
52 "List of variables (or collections) to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
55 addParam(
"fileName",
m_fileName,
"Name of ROOT file for output. Can be overridden using the -o argument of basf2.",
56 string(
"VariablesToNtuple.root"));
57 addParam(
"treeName",
m_treeName,
"Name of the NTuple in the saved file.",
string(
"ntuple"));
60 std::tuple<std::string, std::map<int, unsigned int>> default_sampling{
"", {}};
62 "Tuple of variable name and a map of integer values and inverse sampling rate. E.g. (signal, {1: 0, 0:10}) selects all signal candidates and every 10th background candidate.",
66 "Name of signal-side particle list to store the index of the signal-side particle when one calls the module in a for_each loop over the RestOfEvent",
73 "Use float type for floating-point numbers.",
false);
76 "If true, the branch __eventType__ is added. The eventType information is available from MC16 on.",
true);
79 "Additional dictionary of "
80 "name->value pairs to be added to the file metadata to describe the data",
84 "Ignore override of file name via command line argument -o. Useful if you have multiple output modules in one path.",
false);
99 if (!outputFileArgument.empty())
107 B2FATAL(
"Output root file name is not set. Please set a valid root output file name (\"fileName\" module parameter).");
113 B2ERROR(
"Could not create file \"" <<
m_fileName <<
114 "\". Please set a valid root output file name (\"fileName\" module parameter).");
118 TDirectory::TContext directoryGuard(
m_file.get());
123 <<
"\" already exists in the file \"" <<
m_fileName <<
"\"\n"
124 <<
"or is reserved for FileMetaData.\n"
125 <<
"\nYou probably want to either set the output fileName or the treeName to something else:\n\n"
126 <<
" from modularAnalysis import variablesToNtuple\n"
127 <<
" variablesToNtuple('pi+:all', ['p'], treename='pions', filename='variablesToNtuple.root')\n"
128 <<
" variablesToNtuple('gamma:all', ['p'], treename='photons', filename='variablesToNtuple.root') # two trees, same file\n"
130 <<
" from modularAnalysis import variablesToNtuple\n"
131 <<
" variablesToNtuple('pi+:all', ['p'], filename='pions.root')\n"
132 <<
" variablesToNtuple('gamma:all', ['p'], filename='photons.root') # two files\n"
140 m_tree->get().SetCacheSize(100000);
150 m_tree->get().Branch(
"__run__", &
m_run,
"__run__/I");
162 if (not
m_roe.isOptional(
"RestOfEvent")) {
163 B2WARNING(
"The signalSideParticleList is set outside of a for_each loop over the RestOfEvent. "
164 <<
"__signalSideCandidates__ and __nSignalSideCandidate__ will be always -1 and 0, respectively.");
174 B2INFO(
"EventExtraInfo is not registered. __eventType__ will be empty. The eventType is available from MC16 on.");
178 if (Variable::isCounterVariable(variable)) {
179 B2WARNING(
"The counter '" << variable
180 <<
"' is handled automatically by VariablesToNtuple, you don't need to add it.");
186 unordered_set<string> seen;
188 if (seen.find(varStr) != std::end(seen)) return true;
204 size_t enumerate = 1;
214 B2ERROR(
"Variable '" << varStr <<
"' is not available in Variable::Manager!");
216 if (
m_particleList.empty() && var->description.find(
"[Eventbased]") == string::npos) {
217 B2ERROR(
"Variable '" << varStr <<
"' is not an event-based variable, "
218 "but you are using VariablesToNtuple without a decay string, i.e. in the event-wise mode.\n"
219 "If you have created an event-based alias you can wrap your alias with `eventCached` to "
220 "declare it as event based, which avoids this error.\n\n"
221 "vm.addAlias('myAliasName', 'eventCached(myAlias)')");
224 if (var->variabletype == Variable::Manager::VariableDataType::c_double) {
230 }
else if (var->variabletype == Variable::Manager::VariableDataType::c_int) {
232 }
else if (var->variabletype == Variable::Manager::VariableDataType::c_bool) {
235 m_functions.push_back(std::make_pair(var->function, var->variabletype));
247 B2FATAL(
"Couldn't find sample variable " <<
m_sampling_name <<
" via the Variable::Manager. Check the name!");
331 if (
m_roe.isValid()) {
350 for (
unsigned int iVar = 0; iVar <
m_variables.size(); iVar++) {
351 auto var_result = std::get<0>(
m_functions[iVar])(
nullptr);
353 if (std::holds_alternative<double>(var_result)) {
354 if (var_type != Variable::Manager::VariableDataType::c_double)
355 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
356 "'. Expected Variable::Manager::VariableDataType::c_double. Exported data for this variable might be incorrect.");
362 }
else if (std::holds_alternative<int>(var_result)) {
363 if (var_type != Variable::Manager::VariableDataType::c_int)
364 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
365 "'. Expected Variable::Manager::VariableDataType::c_int. Exported data for this variable might be incorrect.");
367 }
else if (std::holds_alternative<bool>(var_result)) {
368 if (var_type != Variable::Manager::VariableDataType::c_bool)
369 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
370 "'. Expected Variable::Manager::VariableDataType::c_bool. Exported data for this variable might be incorrect.");
380 for (
unsigned int iPart = 0; iPart <
m_ncandidates; iPart++) {
382 const Particle* particle = particlelist->getParticle(iPart);
390 for (
unsigned int iVar = 0; iVar <
m_variables.size(); iVar++) {
391 auto var_result = std::get<0>(
m_functions[iVar])(particle);
393 if (std::holds_alternative<double>(var_result)) {
394 if (var_type != Variable::Manager::VariableDataType::c_double)
395 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
396 "'. Expected Variable::Manager::VariableDataType::c_double. Exported data for this variable might be incorrect.");
402 }
else if (std::holds_alternative<int>(var_result)) {
403 if (var_type != Variable::Manager::VariableDataType::c_int)
404 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
405 "'. Expected Variable::Manager::VariableDataType::c_int. Exported data for this variable might be incorrect.");
407 }
else if (std::holds_alternative<bool>(var_result)) {
408 if (var_type != Variable::Manager::VariableDataType::c_bool)
409 B2WARNING(
"Wrong registered data type for variable '" +
m_variables[iVar] +
410 "'. Expected Variable::Manager::VariableDataType::c_bool. Exported data for this variable might be incorrect.");
452 persistent->Branch(
"FileMetaData", &outputFileMetaData);
454 persistent->Write(
"persistent", TObject::kWriteDelete);
461 TDirectory::TContext directoryGuard(
m_file.get());
467 const bool writeError =
m_file->TestBit(TFile::kWriteError);
470 B2FATAL(
"A write error occurred while saving '" <<
m_fileName <<
"', please check if enough disk space is available.");
In the store you can park objects that have to be accessed by various modules.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
@ c_Persistent
Object is available during entire execution time.
unsigned int getNumberOfMCEvents() const
Number of generated events (from EventInfoSetter).
const std::string & getOutputFileOverride() const
Return overridden output file name, or "" if none was set.
static Environment & Instance()
Static method to get a reference to the Environment instance.
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.
static std::string getSeed()
Get the random number generator seed.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
static Manager & Instance()
get singleton instance.
void checkDeprecatedVariable(const std::string &name)
Check if a variable is deprecated.
StoreObjPtr< RestOfEvent > m_roe
ROE object.
StoreObjPtr< FileMetaData > m_inputFileMetaData
Pointer to the input file meta data.
bool m_useFloat
Use float type for floating-point numbers.
void fillFileMetaData()
Create and fill FileMetaData object.
std::vector< std::string > m_variables
List of variables to save.
std::vector< float > m_branchAddressesFloat
Branch addresses of variables of type float.
int m_eventHigh
highest event number
virtual void initialize() override
Initialises the module.
std::map< int, unsigned int > m_sampling_rates
Inverse sampling rates.
VariablesToNtupleModule()
Constructor.
virtual void event() override
Method called for each event.
unsigned int m_ncandidates
total n candidates
virtual void terminate() override
Write TTree to file, and close file if necessary.
StoreObjPtr< EventMetaData > m_eventMetaData
the event information
std::map< std::string, std::string > m_dataDescription
Additional metadata description.
std::map< int, unsigned long int > m_sampling_counts
Current number of samples with this value.
std::string m_fileName
Name of ROOT file for output.
std::vector< std::pair< Variable::Manager::FunctionPtr, Variable::Manager::VariableDataType > > m_functions
List of pairs of function pointers and respective data type corresponding to given variables.
std::tuple< std::string, std::map< int, unsigned int > > m_sampling
Tuple of variable name and a map of integer values and inverse sampling rate.
int m_runLow
lowest run number
std::vector< int > m_branchAddressesInt
Branch addresses of variables of type int (or bool)
int m_basketsize
Size of TBaskets in the output ROOT file in bytes.
int m_production
production ID (to distinguish MC samples)
unsigned int m_nSignalSideCandidates
total n signal-side candidates
unsigned int m_eventCount
event counter
int m_eventLow
lowest event number
StoreObjPtr< StringWrapper > m_stringWrapper
string wrapper storing the MCDecayString
int m_experiment
experiment number
bool m_storeEventType
If true, the branch eventType is added.
std::string m_particleList
Name of particle list with reconstructed particles.
bool m_ignoreCommandLineOverride
if true, ignore override of filename
std::string m_eventType
EventType to be filled.
StoreObjPtr< FileMetaData > m_outputFileMetaData
File meta data to be stored in the output ntuple file.
StoreObjPtr< RootMergeable< TTree > > m_tree
The ROOT TNtuple for output.
std::shared_ptr< TFile > m_file
ROOT file for output.
std::vector< double > m_branchAddressesDouble
Branch addresses of variables of type double.
std::string m_sampling_name
Variable name of sampling variable.
float getInverseSamplingRateWeight(const Particle *particle)
Calculate inverse sampling rate weight.
int m_experimentLow
lowest experiment number
StoreObjPtr< EventExtraInfo > m_eventExtraInfo
pointer to EventExtraInfo
std::string m_treeName
Name of the TTree.
std::string m_signalSideParticleList
Name of signal-side particle list
const Variable::Manager::Var * m_sampling_variable
Variable Pointer to target variable.
int m_experimentHigh
highest experiment number
int m_signalSideCandidate
signal-side candidate counter
std::vector< std::string > m_parentLfns
Vector of parent file LFNs.
int m_runHigh
highest run number
std::string m_fileNameSuffix
Suffix to be appended to the output file name.
int m_candidate
candidate counter
std::string m_MCDecayString
MC decay string to be filled.
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 Database & Instance()
Instance of a singleton Database.
static RootFileCreationManager & getInstance()
Interface for the FileManager.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
const std::string c_treeNames[]
Names of trees.
void setCreationData(FileMetaData &metadata)
Fill the creation info of a file meta data: site, user, data.
Abstract base class for different kinds of events.
VariableDataType variabletype
data type of variable
A variable returning a floating-point value for a given Particle.
FunctionPtr function
Pointer to function.