Belle II Software development
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 {
46 protected:
48
49 std::vector<std::string> m_PARAMrootFileNames;
50
52 std::vector<std::string> m_PARAMmapNames;
53
56
59 // ///////////////////////////////////////////////////////////////////////////////// member variables END:
60 public:
61
64
65
68
69
71 std::vector<std::string> getRootFiles(std::string mapName);
72
73
75 std::unique_ptr<TChain> createTreeChain(const SectorMapConfig& configuration, const std::string& nHitString);
76
77
81 template<class ValueType> std::vector<BranchInterface<ValueType>> getBranches(
82 std::unique_ptr<TChain>& chain,
83 const std::vector<std::string>& branchNames);
84
85
87 std::string prepareNHitSpecificStuff(
88 unsigned nHits,
89 const SectorMapConfig& config,
90 std::vector<std::string>& secBranchNames,
91 std::vector<std::string>& filterBranchNames);
92
93
95 std::vector<unsigned> getSecIDs(std::vector<BranchInterface<unsigned>>& secBranches, Long64_t entry)
96 {
97 std::vector<unsigned> iDs;
98 for (BranchInterface<unsigned>& branch : secBranches) {
99 branch.update(entry);
100 iDs.push_back(branch.value);
101 }
102 return iDs;
103 }
104
105
107 template <class FilterType> void trainGraph(
108 SectorGraph<FilterType>& mainGraph,
109 std::unique_ptr<TChain>& chain,
110 std::vector<BranchInterface<unsigned>>& sectorBranches,
111 std::vector<BranchInterface<double>>& filterBranches);
112
113
115 template <class FilterType> SectorGraph<FilterType> buildGraph(
116 std::unique_ptr<TChain>& chain,
117 std::vector<BranchInterface<unsigned>>& sectorBranches,
118 std::vector<BranchInterface<double>>& filterBranches);
119
120
122 void printData(
123 std::unique_ptr<TChain>& chain,
124 std::vector<BranchInterface<unsigned>>& sectorBranches,
125 std::vector<BranchInterface<double>>& filterBranches);
126
127
129 void printVXDTFFilters(const VXDTFFilters<SpacePoint>& filters, std::string configName,
130 unsigned int nHitCombinations, bool print2File);
131
132
133
135 // TODO: remove
136 // std::vector<VxdID> getCompatibleVxdIDs(const SectorMapConfig& config);
137
138
140 template <class FilterType> void getSegmentFilters(
141 const SectorMapConfig& config,
142 SectorGraph<FilterType>& mainGraph,
143 VXDTFFilters<SpacePoint>* xHitFilters,
144 int nSecChainLength);
145
146
148 template <class FilterType> void add2HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
149 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
150
151
153 template <class FilterType> void add3HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
154 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
155
156
158 template <class FilterType> void add4HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
159 SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
160
161
165 template <class FilterType> unsigned updateFilterSubLayerIDs(SectorGraph<FilterType>& mainGraph,
166 VXDTFFilters<SpacePoint>& segFilters);
167
168
171 unsigned secChainLength)
172 {
173 B2INFO("processSectorCombinations: training map " << config.secMapName << " with secChainLength: " << secChainLength);
174
175 // branch-names sorted from outer to inner:
176 std::vector<std::string> secBranchNames;
177 // filter names are different for different secChainLengths:
178 std::vector<std::string> filterBranchNames;
179 std::string nHit = prepareNHitSpecificStuff(secChainLength, config, secBranchNames, filterBranchNames);
180
181 // contains the raw data
182 std::unique_ptr<TChain> chain = createTreeChain(config, nHit);
183 if (chain->GetEntries() == 0) {
184 B2WARNING("raw data for map " << config.secMapName << " with " << nHit << " is empty! skipping");
185 return;
186 }
187
188 // prepare links to branches branches:
189 std::vector<BranchInterface<unsigned>> sectorBranches = getBranches<unsigned>(chain, secBranchNames);
190 std::vector<BranchInterface<double>> filterBranches = getBranches<double>(chain, filterBranchNames);
191
192 // for debugging: print data for crosschecks:
193 printData(chain, sectorBranches, filterBranches);
194
195 // create graph containing all sector-chains occurred in the training sample
196 SectorGraph<std::string> mainGraph = buildGraph<std::string>(chain, sectorBranches, filterBranches);
197
198 // use rareness-threshold to find sector-combinations which are very rare and remove them:
199 unsigned nKilled = mainGraph.pruneGraph(config.rarenessThreshold);
200
201 B2INFO("processSectorCombinations: nKilled after graph-pruning: " << nKilled);
202
203 // get the raw data and determine the cuts for the filters/selectionVariable
204 for (auto& subgraph : mainGraph) {
205 subgraph.second.prepareDataCollection(config.quantiles); // TODO small-sample-case!
206 }
207
208 // Get the absolute threshold (nfound) from the relative threshold
209 int absThreshold = mainGraph.getAbsThreshold(m_RelThreshold);
210
211 // Prune the sector map
212 nKilled += mainGraph.pruneGraphBeforeTraining(absThreshold);
213
214 B2INFO("processSectorCombinations: nKilled before the training: " << nKilled);
215
216 trainGraph(mainGraph, chain, sectorBranches, filterBranches);
218
226 // checks for sectors which have inner neighbour and updates the sublayerID of the sensors.
227 // TODO: check if all FullSecIDs are updated and not only those in the corresponding graphs as this may cause problems in a later step
228 mainGraph.updateSubLayerIDs();
229
230 B2INFO("processSectorCombinations: training finished.\n" << mainGraph.print(m_PARAMprintFullGraphs););
231
232 getSegmentFilters(config, mainGraph, xHitFilters, secChainLength);
233
234 if (xHitFilters->size() == 0) {
235 // thou shall not delete the filters!
236 // delete xHitFilters;
237 B2FATAL("processSectorCombinations: an empty VXDTFFilters was returned, training data did not work!");
238 }
239
240 return;
241
242 }
243
244
245
248 void initialize() override
249 {
251 B2INFO("RawSecMapMerger::initialize():");
252
253 // loop over all the setups in the filtersContainer:
254 for (auto& setup : filtersContainer.getAllSetups()) {
255
256 // TODO: remove the config from all the following functions as it is contained in the filters!
257 auto config = setup.second->getConfig();
258 B2INFO("RawSecMapMerger::initialize(): loading mapName: " << config.secMapName);
259
260 VXDTFFilters<SpacePoint>* xHitFilters = setup.second;
261
262 B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 2-hit-combinations:\n\n");
263 processSectorCombinations(config, xHitFilters, 2);
264
265 // for debugging:
266 printVXDTFFilters(*xHitFilters, config.secMapName, 2, true);
267
268
269 // catching case of empty xHitFilters:
270 // TODO: check if that causes problems as these are the filters from bootstrapping
271 // this statement is useless as the filters from bootstrapping have already sectors added so the size is !=0
272 if (xHitFilters->size() == 0) {
273 B2FATAL("This should not happen!");
274 }
275 B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 3-hit-combinations:\n\n");
276 processSectorCombinations(config, xHitFilters, 3);
277
278 /* // return; // TODO WARNING DEBUG we do not want to run more than one run yet! */
279
280 }
281 }
282
288 bool good(const std::vector< unsigned>& ids);
289 };
290
292}
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 occurrences.
Definition: SectorGraph.h:207
int getAbsThreshold(int relThreshold)
Get the absolute threshold (# nfound) given a relative threshold.
Definition: SectorGraph.h:168
unsigned pruneGraph(double rarenessCut)
returns removed occurrences.
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.