Belle II Software development
SVDVariablesToStorageModule.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 <svd/modules/svdPerformance/SVDVariablesToStorageModule.h>
10#include <reconstruction/persistenceManager/PersistenceManagerFactory.h>
11
12#include <framework/datastore/StoreArray.h>
13#include <framework/core/ModuleParam.templateDetails.h>
14#include <tracking/dataobjects/RecoTrack.h>
15#include <mdst/dataobjects/Track.h>
16#include <mdst/dataobjects/TrackFitResult.h>
17#include <analysis/dataobjects/ParticleList.h>
18#include <mdst/dataobjects/HitPatternVXD.h>
19#include <analysis/VariableManager/Manager.h>
20namespace {
21 namespace VPM = Belle2::VariablePersistenceManager;
22
23 std::string getIndexedVariableName(const std::string& variableName, unsigned int iCluster)
24 {
25 return variableName + "(" + std::to_string(iCluster) + ")";
26 }
27
28 VPM::Variables createVariablesToStore(const std::vector<std::string>& variablesToNtuple)
29 {
30 VPM::Variables variables;
31 for (const auto& variableName : variablesToNtuple) {
32 const auto variableDataType = static_cast<VPM::VariableDataType>(Belle2::Variable::Manager::Instance().getVariable(
33 getIndexedVariableName(variableName, 0))->variabletype);
34 variables.push_back(VPM::TypedVariable(variableName, variableDataType));
35 }
36 return variables;
37 }
38
39 VPM::Variables createVariablesToStore(const std::vector<std::tuple<std::string, int, float, float>>& variablesToHistogram)
40 {
41 VPM::Variables variables;
42 for (const auto& [varName, nbins, minVal, maxVal] : variablesToHistogram) {
43 variables.push_back(VPM::BinnedVariable(varName, nbins, minVal, maxVal));
44 }
45 return variables;
46 }
47
48 std::vector<std::string> extractVariableNames(const std::vector<std::tuple<std::string, int, float, float>>& variablesToHistogram)
49 {
50 std::vector<std::string> variableNames;
51 std::transform(variablesToHistogram.begin(), variablesToHistogram.end(), std::back_inserter(variableNames),
52 [](const auto & variable) {
53 return std::get<0>(variable);
54 });
55 return variableNames;
56 }
57}
58
59namespace Belle2::SVD {
60
61 namespace VPM = Belle2::VariablePersistenceManager;
62
64 REG_MODULE(SVDVariablesToStorage);
65
67 {
68 addParam("outputFileName", m_fileName, "", m_fileName);
69 addParam("containerName", m_containerName, "", m_containerName);
70 addParam("particleListName", m_particleListName, "", m_particleListName);
71 addParam("variablesToNtuple", m_variablesToNtuple, "Variables to store in the ntuple.", m_variablesToNtuple);
72 addParam("variablesToHistogram", m_variablesToHistogram, "Variables to store in the histogram.", m_variablesToHistogram);
73 }
74
76 {
77 VPM::Variables variablesToStore;
78 if (not m_variablesToNtuple.empty() and not m_variablesToHistogram.empty()) {
79 B2FATAL("Cannot have both variablesToNtuple and variablesToHistogram set.");
80 } else if (not m_variablesToNtuple.empty()) {
82 variablesToStore = createVariablesToStore(m_variablesToNtuple);
83 persistenceManager = VPM::PersistenceManagerFactory::create("ntuple");
84 } else if (not m_variablesToHistogram.empty()) {
85 m_variableNames = extractVariableNames(m_variablesToHistogram);
86 variablesToStore = createVariablesToStore(m_variablesToHistogram);
87 persistenceManager = VPM::PersistenceManagerFactory::create("histogram");
88 }
89 persistenceManager->initialize(m_fileName, m_containerName, variablesToStore);
90 }
91
93 {
95 const auto ncandidates = particlelist->getListSize();
96 for (unsigned int iPart = 0; iPart < ncandidates; iPart++) {
97 const Particle* particle = particlelist->getParticle(iPart);
98
99 const auto trackFitResult = particle->getTrackFitResult();
100
101 if (!trackFitResult) {
102 continue;
103 }
104
105 const auto nSVDClusters = trackFitResult->getHitPatternVXD().getNSVDHits();
106
107 for (unsigned int iCluster = 0; iCluster < nSVDClusters; iCluster++) {
108
109 VPM::EvaluatedVariables evaluatedVariables{};
110 for (const auto& variableName : m_variableNames) {
111 const std::string indexedVariableName = getIndexedVariableName(variableName, iCluster);
112 const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(indexedVariableName);
113 const auto value = var->function(particle);
114 evaluatedVariables[variableName] = value;
115 }
116 persistenceManager->addEntry(evaluatedVariables);
117 }
118 }
119 }
120
125
126}
Module()
Constructor.
Definition Module.cc:30
Class to store reconstructed particles.
Definition Particle.h:76
std::string m_particleListName
The name of the particle list to use for analysis (if applicable).
std::vector< std::tuple< std::string, int, float, float > > m_variablesToHistogram
List of (variableName, nBins, lowBin, highBin) defining histograms.
virtual void initialize() override
Initializes the module.
std::vector< std::string > m_variableNames
List of variable names for reference.
std::unique_ptr< VPM::PersistenceManager > persistenceManager
Manages the persistence of data (e.g., writing to file).
virtual void event() override
Processes each event in the main event loop.
virtual void terminate() override
Performs finalization steps after all events have been processed.
std::string m_containerName
The name of the container storing SVD data (e.g., clusters).
std::string m_fileName
The output file name for storing results (e.g., root file).
std::vector< std::string > m_variablesToNtuple
List of variables to store in an ntuple.
SVDVariablesToStorageModule()
Constructs the SVDVariablesToStorageModule and sets default parameter values.
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition Manager.cc:58
static Manager & Instance()
get singleton instance.
Definition Manager.cc:26
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
VariableDataType variabletype
data type of variable
Definition Manager.h:132
A variable returning a floating-point value for a given Particle.
Definition Manager.h:145