Belle II Software development
UnionRecordingFilter.icc.h
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#pragma once
9
10#include <tracking/trackFindingCDC/filters/base/UnionRecordingFilter.dcl.h>
11
12#include <tracking/trackFindingCDC/filters/base/RecordingFilter.icc.h>
13
14#include <tracking/trackFindingCDC/filters/base/FilterVarSet.icc.h>
15#include <tracking/trackFindingCDC/filters/base/FilterVarSet.dcl.h>
16
17#include <tracking/trackFindingCDC/varsets/UnionVarSet.h>
18
19#include <framework/core/ModuleParamList.templateDetails.h>
20#include <tracking/trackFindingCDC/utilities/StringManipulation.h>
21
22#include <vector>
23#include <string>
24#include <memory>
25
26namespace Belle2 {
31 namespace TrackFindingCDC {
32
33 /* UnionRecording<> */
34 template <class AFilter>
35 UnionRecording<AFilter>::UnionRecording(std::unique_ptr<AFilterFactory> filterFactory,
36 const std::string& defaultRootFileName,
37 const std::string& defaultTreeName)
38 : Super(std::make_unique<UnionVarSet<Object>>(), defaultRootFileName, defaultTreeName)
39 , m_filterFactory(std::move(filterFactory))
40 {
41 }
42
43 template <class AFilter>
45
46 template <class AFilter>
47 void UnionRecording<AFilter>::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
48 {
49 Super::exposeParameters(moduleParamList, prefix);
50
51 moduleParamList->addParameter(prefixed(prefix, "varSets"),
52 m_param_varSetNames,
53 "List of names refering to concrete variable sets."
54 "Valid names: " +
55 join(", ", this->getValidVarSetNames()),
56 m_param_varSetNames);
57
58 moduleParamList->addParameter(prefixed(prefix, "skim"),
59 m_param_skim,
60 "Filter name which object must pass to be recorded."
61 "Valid names: " +
62 join(", ", this->getValidFilterNames()),
63 m_param_skim);
64 }
65
66 template <class AFilter>
68 {
70 if (m_param_skim != "") {
71 std::unique_ptr<AFilter> skimFilter = m_filterFactory->create(m_param_skim);
72 this->setSkimFilter(std::move(skimFilter));
73 }
74
75 auto multiVarSet = std::make_unique<UnionVarSet<Object>>();
76
78 for (std::string name : getVarSetNames()) {
79 std::unique_ptr<AVarSet> varSet = createVarSet(name);
80 if (varSet) {
81 multiVarSet->push_back(std::move(varSet));
82 } else {
83 B2WARNING("Could not create a variable set from name " << name);
84 }
85 }
86 this->setVarSet(std::move(multiVarSet));
87 Super::initialize();
88 }
89
90 template <class AFilter>
91 std::vector<std::string> UnionRecording<AFilter>::getValidVarSetNames() const
92 {
93 // Get all filter names and make a var set name for each.
94 std::vector<std::string> varSetNames;
95 std::vector<std::string> filterNames = getValidFilterNames();
96 for (const std::string& filterName : filterNames) {
97 std::string varSetName = "filter(" + filterName + ")";
98 varSetNames.push_back(varSetName);
99 }
100 return varSetNames;
101 }
102
103 template <class AFilter>
104 std::vector<std::string> UnionRecording<AFilter>::getValidFilterNames() const
105 {
106 // Get all filter names and make a var set name for each.
107 std::map<std::string, std::string> filterNamesAndDescriptions =
108 m_filterFactory->getValidFilterNamesAndDescriptions();
109
110 std::vector<std::string> filterNames;
111 filterNames.reserve(filterNamesAndDescriptions.size());
112 for (const auto& filterNameAndDescription :
113 filterNamesAndDescriptions) {
114 const std::string& filterName = filterNameAndDescription.first;
115 filterNames.push_back(filterName);
116 }
117 return filterNames;
118 }
119
120 template <class AFilter>
121 auto UnionRecording<AFilter>::createVarSet(const std::string& name) const
122 -> std::unique_ptr<AVarSet> {
123 if (name.find("filter(") == 0 and name.rfind(")") == name.size() - 1)
124 {
125 B2INFO("Detected filter name");
126 std::string filterName = name.substr(7, name.size() - 8);
127 B2INFO("filterName = " << filterName);
128 std::unique_ptr<AFilter> filter = m_filterFactory->create(filterName);
129 if (not filter) {
130 B2WARNING("Could not construct filter for name " << filterName);
131 return std::unique_ptr<AVarSet>(nullptr);
132 } else {
133 AVarSet* filterVarSet = new FilterVarSet<AFilter>(filterName, std::move(filter));
134 return std::unique_ptr<AVarSet>(filterVarSet);
135 }
136 } else {
137 return std::unique_ptr<AVarSet>(nullptr);
138 }
139 }
140
141 template <class AFilter>
142 const std::vector<std::string>& UnionRecording<AFilter>::getVarSetNames() const
143 {
144 return m_param_varSetNames;
145 }
146
147 /* UnionRecordingFilter<> */
148 template <class AFilterFactory>
150 const std::string& defaultRootFileName,
151 const std::string& defaultTreeName)
152 : Super(std::make_unique<AFilterFactory>(), defaultRootFileName, defaultTreeName)
153 {
154 }
155
156 template <class AFilterFactory>
158 }
160}
The Module parameter list class.
Class to compute floating point variables from a filter response which can be recorded as a flat TNtu...
Filter adapter to make a filter work on a set of variables and record the observed instances on invok...
UnionRecordingFilter(const std::string &defaultRootFileName="records.root", const std::string &defaultTreeName="records")
Constructor of the filter.
A filter that records variables form given objects.
virtual std::vector< std::string > getValidFilterNames() const
Getter for the names of valid filters.
void initialize() override
Initialize the recorder before event processing.
UnionRecording(std::unique_ptr< AFilterFactory > filterFactory, const std::string &defaultRootFileName="records.root", const std::string &defaultTreeName="records")
Constructor of the filter.
const std::vector< std::string > & getVarSetNames() const
Splits the comma separated variable names list into a vector of names.
typename AFilter::Object Object
Type of the object to be analysed.
virtual std::unique_ptr< AVarSet > createVarSet(const std::string &name) const
Create a variable set for the given name.
virtual std::vector< std::string > getValidVarSetNames() const
Getter for the list of valid names of concret variable sets.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the set of parameters of the filter to the module parameter list.
Class that accomodates many variable sets and presents them as on set of variables.
Definition: UnionVarSet.h:27
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double > > &runs, double cut, std::map< ExpRun, std::pair< double, double > > &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
Definition: Splitter.cc:38
Abstract base class for different kinds of events.
STL namespace.