Belle II Software  release-06-02-00
VariablesToNtupleModule.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 <analysis/modules/VariablesToNtuple/VariablesToNtupleModule.h>
10 
11 // analysis
12 #include <analysis/dataobjects/ParticleList.h>
13 #include <analysis/VariableManager/Manager.h>
14 #include <analysis/VariableManager/Utility.h>
15 
16 // framework
17 #include <framework/logging/Logger.h>
18 #include <framework/pcore/ProcHandler.h>
19 #include <framework/core/ModuleParam.templateDetails.h>
20 
21 // framework - root utilities
22 #include <framework/utilities/MakeROOTCompatible.h>
23 #include <framework/utilities/RootFileCreationManager.h>
24 
25 #include <cmath>
26 
27 using namespace std;
28 using namespace Belle2;
29 
30 // Register module in the framework
31 REG_MODULE(VariablesToNtuple)
32 
33 
35  Module(), m_tree("", DataStore::c_Persistent)
36 {
37  //Set module properties
38  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.");
39  setPropertyFlags(c_ParallelProcessingCertified | c_TerminateInAllProcesses);
40 
41  vector<string> emptylist;
42  addParam("particleList", m_particleList,
43  "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)",
44  std::string(""));
45  addParam("variables", m_variables,
46  "List of variables (or collections) to save. Variables are taken from Variable::Manager, and are identical to those available to e.g. ParticleSelector.",
47  emptylist);
48 
49  addParam("fileName", m_fileName, "Name of ROOT file for output.", string("VariablesToNtuple.root"));
50  addParam("treeName", m_treeName, "Name of the NTuple in the saved file.", string("ntuple"));
51 
52  std::tuple<std::string, std::map<int, unsigned int>> default_sampling{"", {}};
53  addParam("sampling", m_sampling,
54  "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.",
55  default_sampling);
56 }
57 
58 void VariablesToNtupleModule::initialize()
59 {
60  m_eventMetaData.isRequired();
61  if (not m_particleList.empty())
62  StoreObjPtr<ParticleList>().isRequired(m_particleList);
63 
64 
65  // Initializing the output root file
66  if (m_fileName.empty()) {
67  B2FATAL("Output root file name is not set. Please set a valid root output file name (\"fileName\" module parameter).");
68  }
69  // See if there is already a file in which case add a new tree to it ...
70  // otherwise create a new file (all handled by framework)
71  m_file = RootFileCreationManager::getInstance().getFile(m_fileName);
72  if (!m_file) {
73  B2ERROR("Could not create file \"" << m_fileName <<
74  "\". Please set a valid root output file name (\"fileName\" module parameter).");
75  return;
76  }
77 
78  TDirectory::TContext directoryGuard(m_file.get());
79 
80  // check if TTree with that name already exists
81  if (m_file->Get(m_treeName.c_str())) {
82  B2FATAL("Tree with the name \"" << m_treeName
83  << "\" already exists in the file \"" << m_fileName << "\"\n"
84  << "\nYou probably want to either set the output fileName or the treeName to something else:\n\n"
85  << " from modularAnalysis import variablesToNtuple\n"
86  << " variablesToNtuple('pi+:all', ['p'], treename='pions', filename='variablesToNtuple.root')\n"
87  << " variablesToNtuple('gamma:all', ['p'], treename='photons', filename='variablesToNtuple.root') # two trees, same file\n"
88  << "\n == Or ==\n"
89  << " from modularAnalysis import variablesToNtuple\n"
90  << " variablesToNtuple('pi+:all', ['p'], filename='pions.root')\n"
91  << " variablesToNtuple('gamma:all', ['p'], filename='photons.root') # two files\n"
92  );
93  return;
94  }
95 
96  // set up tree and register it in the datastore
97  m_tree.registerInDataStore(m_fileName + m_treeName, DataStore::c_DontWriteOut);
98  m_tree.construct(m_treeName.c_str(), "");
99  m_tree->get().SetCacheSize(100000);
100 
101  // declare counter branches - pass through variable list, remove counters added by user
102  m_tree->get().Branch("__experiment__", &m_experiment, "__experiment__/I");
103  m_tree->get().Branch("__run__", &m_run, "__run__/I");
104  m_tree->get().Branch("__event__", &m_event, "__event__/I");
105  m_tree->get().Branch("__production__", &m_production, "__production__/I");
106  if (not m_particleList.empty()) {
107  m_tree->get().Branch("__candidate__", &m_candidate, "__candidate__/I");
108  m_tree->get().Branch("__ncandidates__", &m_ncandidates, "__ncandidates__/I");
109  }
110  for (const auto& variable : m_variables)
111  if (Variable::isCounterVariable(variable)) {
112  B2WARNING("The counter '" << variable
113  << "' is handled automatically by VariablesToNtuple, you don't need to add it.");
114  }
115 
116  // declare branches and get the variable strings
117  m_variables = Variable::Manager::Instance().resolveCollections(m_variables);
118  m_branchAddresses.resize(m_variables.size() + 1);
119  m_tree->get().Branch("__weight__", &m_branchAddresses[0], "__weight__/D");
120  size_t enumerate = 1;
121  for (const string& varStr : m_variables) {
122  string branchName = makeROOTCompatible(varStr);
123 
124  // Check for deprecated variables
125  Variable::Manager::Instance().checkDeprecatedVariable(varStr);
126 
127  m_tree->get().Branch(branchName.c_str(), &m_branchAddresses[enumerate], (branchName + "/D").c_str());
128 
129  // also collection function pointers
130  const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(varStr);
131  if (!var) {
132  B2ERROR("Variable '" << varStr << "' is not available in Variable::Manager!");
133  } else {
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)')");
140  continue;
141  }
142  m_functions.push_back(var->function);
143  }
144  enumerate++;
145  }
146  m_tree->get().SetBasketSize("*", 1600);
147 
148  m_sampling_name = std::get<0>(m_sampling);
149  m_sampling_rates = std::get<1>(m_sampling);
150 
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!");
155  }
156  for (const auto& pair : m_sampling_rates)
157  m_sampling_counts[pair.first] = 0;
158  } else {
159  m_sampling_variable = nullptr;
160  }
161 }
162 
163 
164 float VariablesToNtupleModule::getInverseSamplingRateWeight(const Particle* particle)
165 {
166  if (m_sampling_variable == nullptr)
167  return 1.0;
168 
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)
173  return 0;
174  else {
175  m_sampling_counts[target] = 0;
176  return m_sampling_rates[target];
177  }
178  }
179  return 1.0;
180 }
181 
182 void VariablesToNtupleModule::event()
183 {
184  m_event = m_eventMetaData->getEvent();
185  m_run = m_eventMetaData->getRun();
186  m_experiment = m_eventMetaData->getExperiment();
187  m_production = m_eventMetaData->getProduction();
188 
189  if (m_particleList.empty()) {
190  m_branchAddresses[0] = getInverseSamplingRateWeight(nullptr);
191  if (m_branchAddresses[0] > 0) {
192  for (unsigned int iVar = 0; iVar < m_variables.size(); iVar++) {
193  m_branchAddresses[iVar + 1] = m_functions[iVar](nullptr);
194  }
195  m_tree->get().Fill();
196  }
197 
198  } else {
199  StoreObjPtr<ParticleList> particlelist(m_particleList);
200  m_ncandidates = particlelist->getListSize();
201  for (unsigned int iPart = 0; iPart < m_ncandidates; iPart++) {
202  m_candidate = iPart;
203  const Particle* particle = particlelist->getParticle(iPart);
204  m_branchAddresses[0] = getInverseSamplingRateWeight(particle);
205  if (m_branchAddresses[0] > 0) {
206  for (unsigned int iVar = 0; iVar < m_variables.size(); iVar++) {
207  m_branchAddresses[iVar + 1] = m_functions[iVar](particle);
208  }
209  m_tree->get().Fill();
210  }
211  }
212  }
213 }
214 
215 void VariablesToNtupleModule::terminate()
216 {
217  if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
218  B2INFO("Writing NTuple " << m_treeName);
219  TDirectory::TContext directoryGuard(m_file.get());
220  m_tree->write(m_file.get());
221 
222  const bool writeError = m_file->TestBit(TFile::kWriteError);
223  m_file.reset();
224  if (writeError) {
225  B2FATAL("A write error occurred while saving '" << m_fileName << "', please check if enough disk space is available.");
226  }
227  }
228 }
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
Base class for Modules.
Definition: Module.h:72
Class to store reconstructed particles.
Definition: Particle.h:74
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Module to calculate variables specified by the user for a given ParticleList and save them into a ROO...
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.
Definition: Module.h:650
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:133