Belle II Software release-09-00-00
RawSecMapMergerModule.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
9
10#pragma once
11
12#include <framework/core/Module.h>
13
14#include <tracking/trackFindingVXD/filterMap/filterFramework/Shortcuts.h>
15#include <tracking/trackFindingVXD/environment/VXDTFFilters.h>
16#include <tracking/dataobjects/SectorMapConfig.h>
17#include <tracking/trackFindingVXD/filterMap/map/FiltersContainer.h>
18
19#include <tracking/trackFindingVXD/sectorMapTools/BranchInterface.h>
20#include <tracking/trackFindingVXD/sectorMapTools/SectorGraph.h>
21#include <tracking/trackFindingVXD/sectorMapTools/SubGraph.h>
22#include <tracking/spacePointCreation/SpacePoint.h>
23
24// stl:
25#include <string>
26#include <vector>
27#include <unordered_map>
28#include <utility> // std::pair
29#include <memory> // std::unique_ptr
30
31#include <TChain.h>
32
33namespace Belle2 {
49 protected:
51
52 std::vector<std::string> m_PARAMrootFileNames;
53
55 std::vector<std::string> m_PARAMmapNames;
56
59
62 // ///////////////////////////////////////////////////////////////////////////////// member variables END:
63 public:
64
67
68
71
72
74 std::vector<std::string> getRootFiles(std::string mapName);
75
76
78 std::unique_ptr<TChain> createTreeChain(const SectorMapConfig& configuration, const std::string& nHitString);
79
80
84 template<class ValueType> std::vector<BranchInterface<ValueType>> getBranches(
85 std::unique_ptr<TChain>& chain,
86 const std::vector<std::string>& branchNames);
87
88
90 std::string prepareNHitSpecificStuff(
91 unsigned nHits,
92 const SectorMapConfig& config,
93 std::vector<std::string>& secBranchNames,
94 std::vector<std::string>& filterBranchNames);
95
96
98 std::vector<unsigned> getSecIDs(std::vector<BranchInterface<unsigned>>& secBranches, Long64_t entry)
99 {
100 std::vector<unsigned> iDs;
101 for (BranchInterface<unsigned>& branch : secBranches) {
102 branch.update(entry);
103 iDs.push_back(branch.value);
104 }
105 return iDs;
106 }
107
108
110 template <class FilterType> void trainGraph(
111 SectorGraph<FilterType>& mainGraph,
112 std::unique_ptr<TChain>& chain,
113 std::vector<BranchInterface<unsigned>>& sectorBranches,
114 std::vector<BranchInterface<double>>& filterBranches);
115
116
118 template <class FilterType> SectorGraph<FilterType> buildGraph(
119 std::unique_ptr<TChain>& chain,
120 std::vector<BranchInterface<unsigned>>& sectorBranches,
121 std::vector<BranchInterface<double>>& filterBranches);
122
123
125 void printData(
126 std::unique_ptr<TChain>& chain,
127 std::vector<BranchInterface<unsigned>>& sectorBranches,
128 std::vector<BranchInterface<double>>& filterBranches);
129
130
132 void printVXDTFFilters(const VXDTFFilters<SpacePoint>& filters, std::string configName,
133 unsigned int nHitCombinations, bool print2File);
134
135
136
138 // TODO: remove
139 // std::vector<VxdID> getCompatibleVxdIDs(const SectorMapConfig& config);
140
141
143 template <class FilterType> void getSegmentFilters(
144 const SectorMapConfig& config,
145 SectorGraph<FilterType>& mainGraph,
146 VXDTFFilters<SpacePoint>* xHitFilters,
147 int nSecChainLength);
148
149
151 template <class FilterType> void add2HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
152 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
153
154
156 template <class FilterType> void add3HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
157 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
158
159
161 template <class FilterType> void add4HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
162 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
163
164
168 template <class FilterType> unsigned updateFilterSubLayerIDs(SectorGraph<FilterType>& mainGraph,
169 VXDTFFilters<SpacePoint>& segFilters);
170
171
174 unsigned secChainLength)
175 {
176 B2INFO("processSectorCombinations: training map " << config.secMapName << " with secChainLength: " << secChainLength);
177
178 // branch-names sorted from outer to inner:
179 std::vector<std::string> secBranchNames;
180 // filter names are different for different secChainLengths:
181 std::vector<std::string> filterBranchNames;
182 std::string nHit = prepareNHitSpecificStuff(secChainLength, config, secBranchNames, filterBranchNames);
183
184 // contains the raw data
185 std::unique_ptr<TChain> chain = createTreeChain(config, nHit);
186 if (chain->GetEntries() == 0) {
187 B2WARNING("raw data for map " << config.secMapName << " with " << nHit << " is empty! skipping");
188 return;
189 }
190
191 // prepare links to branches branches:
192 std::vector<BranchInterface<unsigned>> sectorBranches = getBranches<unsigned>(chain, secBranchNames);
193 std::vector<BranchInterface<double>> filterBranches = getBranches<double>(chain, filterBranchNames);
194
195 // for debugging: print data for crosschecks:
196 printData(chain, sectorBranches, filterBranches);
197
198 // create graph containing all sector-chains occured in the training sample
199 SectorGraph<std::string> mainGraph = buildGraph<std::string>(chain, sectorBranches, filterBranches);
200
201 // use rareness-threshold to find sector-combinations which are very rare and remove them:
202 unsigned nKilled = mainGraph.pruneGraph(config.rarenessThreshold);
203
204 B2INFO("processSectorCombinations: nKilled after graph-pruning: " << nKilled);
205
206 // get the raw data and determine the cuts for the filters/selectionVariable
207 for (auto& subgraph : mainGraph) {
208 subgraph.second.prepareDataCollection(config.quantiles); // TODO small-sample-case!
209 }
210
211 // Get the absolute threshold (nfound) from the relative threshold
212 int absThreshold = mainGraph.getAbsThreshold(m_RelThreshold);
213
214 // Prune the sector map
215 nKilled += mainGraph.pruneGraphBeforeTraining(absThreshold);
216
217 B2INFO("processSectorCombinations: nKilled before the training: " << nKilled);
218
219 trainGraph(mainGraph, chain, sectorBranches, filterBranches);
221
229 // checks for sectors which have inner neighbour and updates the sublayerID of the sensors.
230 // TODO: check if all FullSecIDs are updated and not only those in the corresponding graphs as this may cause problems in a later step
231 mainGraph.updateSubLayerIDs();
232
233 B2INFO("processSectorCombinations: training finished.\n" << mainGraph.print(m_PARAMprintFullGraphs););
234
235 getSegmentFilters(config, mainGraph, xHitFilters, secChainLength);
236
237 if (xHitFilters->size() == 0) {
238 // thou shall not delete the filters!
239 // delete xHitFilters;
240 B2FATAL("processSectorCombinations: an empty VXDTFFilters was returned, training data did not work!");
241 }
242
243 return;
244
245 }
246
247
248
251 void initialize() override
252 {
254 B2INFO("RawSecMapMerger::initialize():");
255
256 // loop over all the setups in the filtersContainer:
257 for (auto& setup : filtersContainer.getAllSetups()) {
258
259 // TODO: remove the config from all the following functions as it is contained in the filters!
260 auto config = setup.second->getConfig();
261 B2INFO("RawSecMapMerger::initialize(): loading mapName: " << config.secMapName);
262
263 VXDTFFilters<SpacePoint>* xHitFilters = setup.second;
264
265 B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 2-hit-combinations:\n\n");
266 processSectorCombinations(config, xHitFilters, 2);
267
268 // for debugging:
269 printVXDTFFilters(*xHitFilters, config.secMapName, 2, true);
270
271
272 // catching case of empty xHitFilters:
273 // TODO: check if that causes problems as these are the filters from bootstrapping
274 // this statement is useless as the filters from bootstrapping have already sectors added so the size is !=0
275 if (xHitFilters->size() == 0) {
276 B2FATAL("This should not happen!");
277 }
278 B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 3-hit-combinations:\n\n");
279 processSectorCombinations(config, xHitFilters, 3);
280
281 /* // return; // TODO WARNING DEBUG we do not want to run more than one run yet! */
282
283 }
284 }
285
291 bool good(const std::vector< unsigned>& ids);
292 };
293
295}
This class contains everything needed by the VXDTF that is not going to change during a RUN,...
static FiltersContainer & getInstance()
one and only way to access the singleton object
const setupNameToFilters_t & getAllSetups(void)
returns all the available setups.
Base class for Modules.
Definition: Module.h:72
The RawSecMapMergerModule.
unsigned updateFilterSubLayerIDs(SectorGraph< FilterType > &mainGraph, VXDTFFilters< SpacePoint > &segFilters)
updates the sublayer ID of the FullSecIDs used in the VXDTFFilters with the one used during the train...
void add3HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
SectorGraph< FilterType > buildGraph(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > &sectorBranches, std::vector< BranchInterface< double > > &filterBranches)
build graph with secChains found in TChain.
void initialize() override
Initializes the Module.
bool m_PARAMprintFullGraphs
If true, the full trained graphs will be printed to screen.
std::vector< BranchInterface< ValueType > > getBranches(std::unique_ptr< TChain > &chain, const std::vector< std::string > &branchNames)
for given chain and names of branches: this function returns their pointers to the branch and the con...
void printVXDTFFilters(const VXDTFFilters< SpacePoint > &filters, std::string configName, unsigned int nHitCombinations, bool print2File)
for debugging purposes: print VXDTFFilters into a file of name of the sectorMapConfig.
std::unique_ptr< TChain > createTreeChain(const SectorMapConfig &configuration, const std::string &nHitString)
bundle all relevant files to a TChain
std::vector< std::string > m_PARAMrootFileNames
///////////////////////////////////////////////////////////////////////////////// member variables of...
std::vector< std::string > m_PARAMmapNames
contains names of sectorMaps to be loaded.
void printData(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > &sectorBranches, std::vector< BranchInterface< double > > &filterBranches)
for debugging: print data for crosschecks.
std::vector< std::string > getRootFiles(std::string mapName)
returns all names of root-files fitting given parameter mapName
int m_RelThreshold
Relative threshold for pruning the sector maps (in %).
std::vector< unsigned > getSecIDs(std::vector< BranchInterface< unsigned > > &secBranches, Long64_t entry)
returns secIDs of current entry in the secBranches.
void add4HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
RawSecMapMergerModule()
Constructor of the module.
void add2HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
void getSegmentFilters(const SectorMapConfig &config, SectorGraph< FilterType > &mainGraph, VXDTFFilters< SpacePoint > *xHitFilters, int nSecChainLength)
returns all VxdIDs (sensors) compatible with given configData.
void processSectorCombinations(const SectorMapConfig &config, VXDTFFilters< SpacePoint > *xHitFilters, unsigned secChainLength)
does everything needed for given chainLength of sectors (e.g.: 2 -> twoHitFilters)
void trainGraph(SectorGraph< FilterType > &mainGraph, std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > &sectorBranches, std::vector< BranchInterface< double > > &filterBranches)
fill the graphs with raw data fitting to their filters respectively.
std::string prepareNHitSpecificStuff(unsigned nHits, const SectorMapConfig &config, std::vector< std::string > &secBranchNames, std::vector< std::string > &filterBranchNames)
sets everything which is hit-dependent.
bool good(const std::vector< unsigned > &ids)
check that the vector of FullSecIDs
~RawSecMapMergerModule()
Destructor of the module.
contains all subgraphs.
Definition: SectorGraph.h:31
void updateSubLayerIDs()
finds sectors having inner sectors in same layer and update them in the subGraph-ID.
Definition: SectorGraph.h:245
std::string print(bool fullPrint=true) const
returns a string giving an overview of the graph.
Definition: SectorGraph.h:76
unsigned pruneGraphBeforeTraining(int absThreshold)
returns removed occurances.
Definition: SectorGraph.h:207
int getAbsThreshold(int relThreshold)
Get the absolute treshold (# nfound) given a relative threshold.
Definition: SectorGraph.h:168
unsigned pruneGraph(double rarenessCut)
returns removed occurances.
Definition: SectorGraph.h:89
contains all relevant stuff needed for dealing with a subGraph.
Definition: SubGraph.h:30
Class that contains all the static sectors to which the filters are attached.
Definition: VXDTFFilters.h:63
unsigned size() const
returns number of compact secIDs stored for this filter-container.
Definition: VXDTFFilters.h:289
Abstract base class for different kinds of events.
simple struct for interfacing the Branch.
simple struct containing all the configuration data needed for the SecMapTrainer.