Belle II Software  release-05-02-19
RawSecMapMergerModule.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler (jakob.lettenbichler@oeaw.ac.at) *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 
12 #pragma once
13 
14 #include <framework/core/Module.h>
15 
16 #include <tracking/trackFindingVXD/filterMap/filterFramework/Shortcuts.h>
17 #include <tracking/trackFindingVXD/environment/VXDTFFilters.h>
18 #include <tracking/dataobjects/SectorMapConfig.h>
19 #include <tracking/trackFindingVXD/filterMap/map/FiltersContainer.h>
20 
21 #include <tracking/trackFindingVXD/sectorMapTools/BranchInterface.h>
22 #include <tracking/trackFindingVXD/sectorMapTools/SectorGraph.h>
23 #include <tracking/trackFindingVXD/sectorMapTools/SubGraph.h>
24 #include <tracking/spacePointCreation/SpacePoint.h>
25 
26 // stl:
27 #include <string>
28 #include <vector>
29 #include <unordered_map>
30 #include <utility> // std::pair
31 #include <memory> // std::unique_ptr
32 
33 #include <TChain.h>
34 
35 namespace Belle2 {
50  class RawSecMapMergerModule : public Module {
51  protected:
53 
54  std::vector<std::string> m_PARAMrootFileNames;
55 
57  std::vector<std::string> m_PARAMmapNames;
58 
61  // ///////////////////////////////////////////////////////////////////////////////// member variables END:
62  public:
63 
66 
67 
70 
71 
73  std::vector<std::string> getRootFiles(std::string mapName);
74 
75 
77  std::unique_ptr<TChain> createTreeChain(const SectorMapConfig& configuration, const std::string& nHitString);
78 
79 
83  template<class ValueType> std::vector<BranchInterface<ValueType>> getBranches(
84  std::unique_ptr<TChain>& chain,
85  const std::vector<std::string>& branchNames);
86 
87 
89  std::string prepareNHitSpecificStuff(
90  unsigned nHits,
91  const SectorMapConfig& config,
92  std::vector<std::string>& secBranchNames,
93  std::vector<std::string>& filterBranchNames);
94 
95 
97  std::vector<unsigned> getSecIDs(std::vector<BranchInterface<unsigned>>& secBranches, Long64_t entry)
98  {
99  std::vector<unsigned> iDs;
100  for (BranchInterface<unsigned>& branch : secBranches) {
101  branch.update(entry);
102  iDs.push_back(branch.value);
103  }
104  return iDs;
105  }
106 
107 
109  template <class FilterType> void trainGraph(
110  SectorGraph<FilterType>& mainGraph,
111  std::unique_ptr<TChain>& chain,
112  std::vector<BranchInterface<unsigned>>& sectorBranches,
113  std::vector<BranchInterface<double>>& filterBranches);
114 
115 
117  template <class FilterType> SectorGraph<FilterType> buildGraph(
118  std::unique_ptr<TChain>& chain,
119  std::vector<BranchInterface<unsigned>>& sectorBranches,
120  std::vector<BranchInterface<double>>& filterBranches);
121 
122 
124  void printData(
125  std::unique_ptr<TChain>& chain,
126  std::vector<BranchInterface<unsigned>>& sectorBranches,
127  std::vector<BranchInterface<double>>& filterBranches);
128 
129 
131  void printVXDTFFilters(const VXDTFFilters<SpacePoint>& filters, std::string configName,
132  unsigned int nHitCombinations, bool print2File);
133 
134 
135 
137  // TODO: remove
138  // std::vector<VxdID> getCompatibleVxdIDs(const SectorMapConfig& config);
139 
140 
142  template <class FilterType> void getSegmentFilters(
143  const SectorMapConfig& config,
144  SectorGraph<FilterType>& mainGraph,
145  VXDTFFilters<SpacePoint>* xHitFilters,
146  int nSecChainLength);
147 
148 
150  template <class FilterType> void add2HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
151  SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
152 
153 
155  template <class FilterType> void add3HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
156  SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
157 
158 
160  template <class FilterType> void add4HitFilters(VXDTFFilters<SpacePoint>& filterContainer,
161  SubGraph<FilterType>& subGraph, const SectorMapConfig& config);
162 
163 
167  template <class FilterType> unsigned updateFilterSubLayerIDs(SectorGraph<FilterType>& mainGraph,
168  VXDTFFilters<SpacePoint>& segFilters);
169 
170 
172  void processSectorCombinations(const SectorMapConfig& config, VXDTFFilters<SpacePoint>* xHitFilters,
173  unsigned secChainLength)
174  {
175  B2INFO("processSectorCombinations: training map " << config.secMapName << " with secChainLength: " << secChainLength);
176 
177  // branch-names sorted from outer to inner:
178  std::vector<std::string> secBranchNames;
179  // filter names are different for different secChainLengths:
180  std::vector<std::string> filterBranchNames;
181  std::string nHit = prepareNHitSpecificStuff(secChainLength, config, secBranchNames, filterBranchNames);
182 
183  // contains the raw data
184  std::unique_ptr<TChain> chain = createTreeChain(config, nHit);
185  if (chain->GetEntries() == 0) {
186  B2WARNING("raw data for map " << config.secMapName << " with " << nHit << " is empty! skipping");
187  return;
188  }
189 
190  // prepare links to branches branches:
191  std::vector<BranchInterface<unsigned>> sectorBranches = getBranches<unsigned>(chain, secBranchNames);
192  std::vector<BranchInterface<double>> filterBranches = getBranches<double>(chain, filterBranchNames);
193 
194  // for debugging: print data for crosschecks:
195  printData(chain, sectorBranches, filterBranches);
196 
197  // create graph containing all sector-chains occured in the training sample
198  SectorGraph<std::string> mainGraph = buildGraph<std::string>(chain, sectorBranches, filterBranches);
199 
200  // use rareness-threshold to find sector-combinations which are very rare and remove them:
201  unsigned nKilled = mainGraph.pruneGraph(config.rarenessThreshold);
202 
203  B2INFO("processSectorCombinations: nKilled after graph-pruning: " << nKilled);
204 
205  // get the raw data and determine the cuts for the filters/selectionVariable
206  for (auto& subgraph : mainGraph) {
207  subgraph.second.prepareDataCollection(config.quantiles); // TODO small-sample-case!
208  }
209 
210  trainGraph(mainGraph, chain, sectorBranches, filterBranches);
212 
220  // checks for sectors which have inner neighbour and updates the sublayerID of the sensors.
221  // TODO: check if all FullSecIDs are updated and not only those in the corresponding graphs as this may cause problems in a later step
222  mainGraph.updateSubLayerIDs();
223 
224  B2INFO("processSectorCombinations: training finished.\n" << mainGraph.print(m_PARAMprintFullGraphs););
225 
226  getSegmentFilters(config, mainGraph, xHitFilters, secChainLength);
227 
228  if (xHitFilters->size() == 0) {
229  // thou shall not delete the filters!
230  // delete xHitFilters;
231  B2FATAL("processSectorCombinations: an empty VXDTFFilters was returned, training data did not work!");
232  }
233 
234  return;
235 
236  }
237 
238 
239 
242  void initialize() override
243  {
244  FiltersContainer<SpacePoint>& filtersContainer = Belle2::FiltersContainer<SpacePoint>::getInstance();
245  B2INFO("RawSecMapMerger::initialize():");
246 
247  // loop over all the setups in the filtersContainer:
248  for (auto& setup : filtersContainer.getAllSetups()) {
249 
250  // TODO: remove the config from all the following functions as it is contained in the filters!
251  auto config = setup.second->getConfig();
252  B2INFO("RawSecMapMerger::initialize(): loading mapName: " << config.secMapName);
253 
254  VXDTFFilters<SpacePoint>* xHitFilters = setup.second;
255 
256  B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 2-hit-combinations:\n\n");
257  processSectorCombinations(config, xHitFilters, 2);
258 
259  // for debugging:
260  printVXDTFFilters(*xHitFilters, config.secMapName, 2, true);
261 
262 
263  // catching case of empty xHitFilters:
264  // TODO: check if that causes problems as these are the filters from bootstrapping
265  // this statement is useless as the filters from bootstrapping have already sectors added so the size is !=0
266  if (xHitFilters->size() == 0) {
267  B2FATAL("This should not happen!");
268  }
269  B2INFO("\n\nRawSecMapMerger::initialize(): for mapName " << config.secMapName << ": process 3-hit-combinations:\n\n");
270  processSectorCombinations(config, xHitFilters, 3);
271 
272  /* // return; // TODO WARNING DEBUG we do not want to run more than one run yet! */
273 
274  }
275  }
276 
282  bool good(const std::vector< unsigned>& ids);
283  };
284 
286 }
Belle2::RawSecMapMergerModule::add4HitFilters
void add4HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
Definition: RawSecMapMergerModule.cc:530
setup
Belle2::VXDTFFilters::size
unsigned size() const
returns number of compact secIDs stored for this filter-container.
Definition: VXDTFFilters.h:299
Belle2::SectorGraph
contains all subgraphs.
Definition: SectorGraph.h:38
Belle2::VXDTFFilters
Class that contains all the static sectors to which the filters are attached.
Definition: VXDTFFilters.h:75
Belle2::RawSecMapMergerModule::RawSecMapMergerModule
RawSecMapMergerModule()
Constructor of the module.
Definition: RawSecMapMergerModule.cc:29
Belle2::RawSecMapMergerModule::createTreeChain
std::unique_ptr< TChain > createTreeChain(const SectorMapConfig &configuration, const std::string &nHitString)
bundle all relevant files to a TChain
Definition: RawSecMapMergerModule.cc:70
Belle2::RawSecMapMergerModule::getSegmentFilters
void getSegmentFilters(const SectorMapConfig &config, SectorGraph< FilterType > &mainGraph, VXDTFFilters< SpacePoint > *xHitFilters, int nSecChainLength)
returns all VxdIDs (sensors) compatible with given configData.
Definition: RawSecMapMergerModule.cc:380
Belle2::RawSecMapMergerModule::good
bool good(const std::vector< unsigned > &ids)
check that the vector of FullSecIDs
Definition: RawSecMapMergerModule.cc:234
Belle2::RawSecMapMergerModule::trainGraph
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.
Definition: RawSecMapMergerModule.cc:138
Belle2::RawSecMapMergerModule::m_PARAMmapNames
std::vector< std::string > m_PARAMmapNames
contains names of sectorMaps to be loaded.
Definition: RawSecMapMergerModule.h:65
Belle2::RawSecMapMergerModule::prepareNHitSpecificStuff
std::string prepareNHitSpecificStuff(unsigned nHits, const SectorMapConfig &config, std::vector< std::string > &secBranchNames, std::vector< std::string > &filterBranchNames)
sets everything which is hit-dependent.
Definition: RawSecMapMergerModule.cc:106
Belle2::RawSecMapMergerModule::processSectorCombinations
void processSectorCombinations(const SectorMapConfig &config, VXDTFFilters< SpacePoint > *xHitFilters, unsigned secChainLength)
does everything needed for given chainLength of sectors (e.g.
Definition: RawSecMapMergerModule.h:180
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RawSecMapMergerModule::updateFilterSubLayerIDs
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...
Definition: RawSecMapMergerModule.cc:340
Belle2::BranchInterface
simple struct for interfacing the Branch.
Definition: BranchInterface.h:32
Belle2::RootIOUtilities::filterBranches
std::set< std::string > filterBranches(const std::set< std::string > &branchesToFilter, const std::vector< std::string > &branches, const std::vector< std::string > &excludeBranches, int durability, bool quiet=false)
Given a list of input branches and lists of branches to include/exclude, returns a list of branches t...
Definition: RootIOUtilities.cc:25
Belle2::SectorGraph::pruneGraph
unsigned pruneGraph(double rarenessCut)
returns removed occurances.
Definition: SectorGraph.h:96
Belle2::SubGraph
contains all relevant stuff needed for dealing with a subGraph.
Definition: SubGraph.h:40
Belle2::RawSecMapMergerModule::printData
void printData(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned >> &sectorBranches, std::vector< BranchInterface< double >> &filterBranches)
for debugging: print data for crosschecks.
Definition: RawSecMapMergerModule.cc:263
Belle2::RawSecMapMergerModule::initialize
void initialize() override
Initializes the Module.
Definition: RawSecMapMergerModule.h:250
Belle2::RawSecMapMergerModule::buildGraph
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.
Definition: RawSecMapMergerModule.cc:177
Belle2::RawSecMapMergerModule::add3HitFilters
void add3HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
Definition: RawSecMapMergerModule.cc:475
Belle2::FiltersContainer::getInstance
static FiltersContainer & getInstance()
one and only way to access the singleton object
Definition: FiltersContainer.h:64
Belle2::RawSecMapMergerModule::getBranches
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...
Definition: RawSecMapMergerModule.cc:85
Belle2::RawSecMapMergerModule::m_PARAMprintFullGraphs
bool m_PARAMprintFullGraphs
If true, the full trained graphs will be printed to screen.
Definition: RawSecMapMergerModule.h:68
Belle2::RawSecMapMergerModule::printVXDTFFilters
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.
Definition: RawSecMapMergerModule.cc:305
Belle2::SectorMapConfig
simple struct containing all the configuration data needed for the SecMapTrainer.
Definition: SectorMapConfig.h:36
Belle2::RawSecMapMergerModule::m_PARAMrootFileNames
std::vector< std::string > m_PARAMrootFileNames
///////////////////////////////////////////////////////////////////////////////// member variables of...
Definition: RawSecMapMergerModule.h:62
Belle2::RawSecMapMergerModule::add2HitFilters
void add2HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
Definition: RawSecMapMergerModule.cc:423
Belle2::RawSecMapMergerModule::~RawSecMapMergerModule
~RawSecMapMergerModule()
Destructor of the module.
Definition: RawSecMapMergerModule.h:77
Belle2::RawSecMapMergerModule::getRootFiles
std::vector< std::string > getRootFiles(std::string mapName)
returns all names of root-files fitting given parameter mapName
Definition: RawSecMapMergerModule.cc:50
Belle2::RawSecMapMergerModule::getSecIDs
std::vector< unsigned > getSecIDs(std::vector< BranchInterface< unsigned >> &secBranches, Long64_t entry)
returns secIDs of current entry in the secBranches.
Definition: RawSecMapMergerModule.h:105