14 #include <analysis/modules/VariablesToNtuple/VariablesToNtupleModule.h>
17 #include <analysis/dataobjects/ParticleList.h>
18 #include <analysis/VariableManager/Manager.h>
19 #include <analysis/VariableManager/Utility.h>
22 #include <framework/logging/Logger.h>
23 #include <framework/pcore/ProcHandler.h>
24 #include <framework/core/ModuleParam.templateDetails.h>
27 #include <framework/utilities/MakeROOTCompatible.h>
28 #include <framework/utilities/RootFileCreationManager.h>
43 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 separate rows.");
44 setPropertyFlags(c_ParallelProcessingCertified | c_TerminateInAllProcesses);
46 vector<string> emptylist;
47 addParam(
"particleList", m_particleList,
48 "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)",
50 addParam(
"variables", m_variables,
51 "List of variables (or collections) to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
54 addParam(
"fileName", m_fileName,
"Name of ROOT file for output.",
string(
"VariablesToNtuple.root"));
55 addParam(
"treeName", m_treeName,
"Name of the NTuple in the saved file.",
string(
"ntuple"));
57 std::tuple<std::string, std::map<int, unsigned int>> default_sampling{
"", {}};
58 addParam(
"sampling", m_sampling,
59 "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.",
63 void VariablesToNtupleModule::initialize()
65 m_eventMetaData.isRequired();
66 if (not m_particleList.empty())
71 if (m_fileName.empty()) {
72 B2FATAL(
"Output root file name is not set. Please set a vaild root output file name (\"fileName\" module parameter).");
76 m_file = RootFileCreationManager::getInstance().getFile(m_fileName);
78 B2ERROR(
"Could not create file \"" << m_fileName <<
79 "\". Please set a vaild root output file name (\"fileName\" module parameter).");
83 TDirectory::TContext directoryGuard(m_file.get());
86 if (m_file->Get(m_treeName.c_str())) {
87 B2FATAL(
"Tree with the name \"" << m_treeName
88 <<
"\" already exists in the file \"" << m_fileName <<
"\"\n"
89 <<
"\nYou probably want to either set the output fileName or the treeName to something else:\n\n"
90 <<
" from modularAnalysis import variablesToNtuple\n"
91 <<
" variablesToNtuple('pi+:all', ['p'], treename='pions', filename='variablesToNtuple.root')\n"
92 <<
" variablesToNtuple('gamma:all', ['p'], treename='photons', filename='variablesToNtuple.root') # two trees, same file\n"
94 <<
" from modularAnalysis import variablesToNtuple\n"
95 <<
" variablesToNtuple('pi+:all', ['p'], filename='pions.root')\n"
96 <<
" variablesToNtuple('gamma:all', ['p'], filename='photons.root') # two files\n"
102 m_tree.registerInDataStore(m_fileName + m_treeName, DataStore::c_DontWriteOut);
103 m_tree.construct(m_treeName.c_str(),
"");
104 m_tree->get().SetCacheSize(100000);
107 m_tree->get().Branch(
"__experiment__", &m_experiment,
"__experiment__/I");
108 m_tree->get().Branch(
"__run__", &m_run,
"__run__/I");
109 m_tree->get().Branch(
"__event__", &m_event,
"__event__/I");
110 if (not m_particleList.empty()) {
111 m_tree->get().Branch(
"__candidate__", &m_candidate,
"__candidate__/I");
112 m_tree->get().Branch(
"__ncandidates__", &m_ncandidates,
"__ncandidates__/I");
114 for (
const auto& variable : m_variables)
115 if (Variable::isCounterVariable(variable)) {
116 B2WARNING(
"The counter '" << variable
117 <<
"' is handled automatically by VariablesToNtuple, you don't need to add it.");
121 m_variables = Variable::Manager::Instance().resolveCollections(m_variables);
122 m_branchAddresses.resize(m_variables.size() + 1);
123 m_tree->get().Branch(
"__weight__", &m_branchAddresses[0],
"__weight__/D");
124 size_t enumerate = 1;
125 for (
const string& varStr : m_variables) {
127 m_tree->get().Branch(branchName.c_str(), &m_branchAddresses[enumerate], (branchName +
"/D").c_str());
132 B2ERROR(
"Variable '" << varStr <<
"' is not available in Variable::Manager!");
134 if (m_particleList.empty() && var->description.find(
"[Eventbased]") == string::npos) {
135 B2ERROR(
"Variable '" << varStr <<
"' is not an event-based variable, "
136 "but you are using VariablesToNtuple without a decay string, i.e. in the event-wise mode.\n"
137 "If you have created an event-based alias you can wrap your alias with `eventCached` to "
138 "declare it as event based, which avoids this error.\n\n"
139 "vm.addAlias('myAliasName', 'eventCached(myAlias)')");
142 m_functions.push_back(var->function);
146 m_tree->get().SetBasketSize(
"*", 1600);
148 m_sampling_name = std::get<0>(m_sampling);
149 m_sampling_rates = std::get<1>(m_sampling);
151 if (m_sampling_name !=
"") {
152 m_sampling_variable = Variable::Manager::Instance().getVariable(m_sampling_name);
153 if (m_sampling_variable ==
nullptr) {
154 B2FATAL(
"Couldn't find sample variable " << m_sampling_name <<
" via the Variable::Manager. Check the name!");
156 for (
const auto& pair : m_sampling_rates)
157 m_sampling_counts[pair.first] = 0;
159 m_sampling_variable =
nullptr;
164 float VariablesToNtupleModule::getInverseSamplingRateWeight(
const Particle* particle)
166 if (m_sampling_variable ==
nullptr)
169 long target = std::lround(m_sampling_variable->function(particle));
170 if (m_sampling_rates.find(target) != m_sampling_rates.end() and m_sampling_rates[target] > 0) {
171 m_sampling_counts[target]++;
172 if (m_sampling_counts[target] % m_sampling_rates[target] != 0)
175 m_sampling_counts[target] = 0;
176 return m_sampling_rates[target];
182 void VariablesToNtupleModule::event()
184 m_event = m_eventMetaData->getEvent();
185 m_run = m_eventMetaData->getRun();
186 m_experiment = m_eventMetaData->getExperiment();
188 if (m_particleList.empty()) {
189 m_branchAddresses[0] = getInverseSamplingRateWeight(
nullptr);
190 if (m_branchAddresses[0] > 0) {
191 for (
unsigned int iVar = 0; iVar < m_variables.size(); iVar++) {
192 m_branchAddresses[iVar + 1] = m_functions[iVar](
nullptr);
194 m_tree->get().Fill();
199 m_ncandidates = particlelist->getListSize();
200 for (
unsigned int iPart = 0; iPart < m_ncandidates; iPart++) {
202 const Particle* particle = particlelist->getParticle(iPart);
203 m_branchAddresses[0] = getInverseSamplingRateWeight(particle);
204 if (m_branchAddresses[0] > 0) {
205 for (
unsigned int iVar = 0; iVar < m_variables.size(); iVar++) {
206 m_branchAddresses[iVar + 1] = m_functions[iVar](particle);
208 m_tree->get().Fill();
214 void VariablesToNtupleModule::terminate()
216 if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
217 B2INFO(
"Writing NTuple " << m_treeName);
218 TDirectory::TContext directoryGuard(m_file.get());
219 m_tree->write(m_file.get());
221 const bool writeError = m_file->TestBit(TFile::kWriteError);
224 B2FATAL(
"A write error occured while saving '" << m_fileName <<
"', please check if enough disk space is available.");