Belle II Software  release-05-02-19
SubGraph.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 #pragma once
12 
13 #include <framework/logging/Logger.h>
14 #include <tracking/trackFindingVXD/sectorMapTools/RawDataCollectedMinMax.h>
15 #include <tracking/trackFindingVXD/sectorMapTools/MinMax.h>
16 #include <tracking/trackFindingVXD/sectorMapTools/SubGraphID.h>
17 #include <tracking/dataobjects/FullSecID.h>
18 
19 #include <string>
20 #include <vector>
21 #include <unordered_map>
22 #include <utility>
23 
24 
25 namespace Belle2 {
32  template<class FilterType> class SubGraph {
33  protected:
34  SubGraphID m_id;
36  std::unordered_map<FilterType, MinMax> m_minMaxValues;
38 
39  unsigned m_found;
41  std::unordered_map<FilterType, RawDataCollectedMinMax>* m_rawDataCollected;
44  void updateID(SubGraphID& newID)
45  { m_id = newID; }
46  public:
47 
49  bool operator==(const SubGraph<FilterType>& b) const
50  { return (m_id == b.m_id); }
51 
53  using Iterator = typename std::unordered_map<FilterType, MinMax>::iterator;
54 
56  SubGraph(SubGraphID& id, std::vector<FilterType>& fIDs) : m_id(id), m_found(1), m_rawDataCollected(nullptr)
57  {
58  for (auto& iD : fIDs) {
59  m_minMaxValues.insert({iD, MinMax()});
60  }
61  }
62 
64  ~SubGraph() { if (m_rawDataCollected != nullptr) delete m_rawDataCollected; }
65 
67  bool checkAndReplaceIfMinMax(FilterType fID, double value)
68  {
69  Iterator pos = m_minMaxValues.find(fID);
70  if (pos == m_minMaxValues.end()) {
71  B2WARNING("FilterID " << fID << " not known to this subgraph " << m_id.print() << " - nothing added!");
72  return false;
73  }
74  return pos->second.checkAndReplaceIfMinMax(value);
75  }
76 
77  void wasFound() { m_found++; }
79  unsigned getFound() const { return m_found; }
82  bool checkSharesTrunk(const SubGraph<FilterType>& b) const { return (m_id.checkSharesTrunk(b.m_id)); }
83 
85  std::string print() const
86  {
87  std::string out = "id: " + m_id.print() + " was found " + std::to_string(m_found) + " times. Filters with values collected:\n";
88  for (const auto& entry : m_minMaxValues) {
89  out += entry.first + " " + entry.second.print() + " _||_ ";
90  }
91  return out;
92  }
93 
95  const SubGraphID& getID() const { return m_id; }
96 
98  void prepareDataCollection(std::pair<double, double> quantiles)
99  {
100  if (m_rawDataCollected != nullptr) delete m_rawDataCollected;
101  m_rawDataCollected = new std::unordered_map<FilterType, RawDataCollectedMinMax>();
102 
103  for (const auto& entry : m_minMaxValues) {
104  m_rawDataCollected->insert({entry.first, RawDataCollectedMinMax(m_found, quantiles)});
105  }
106  }
107 
109  void addValue(FilterType fID, double value)
110  {
111  if (m_rawDataCollected == nullptr) { B2FATAL("SubGraph::addValue: attempt to add data before prepareDataCollection(...) was executed! Aborting illegal procedure."); }
112  auto pos = m_rawDataCollected->find(fID);
113  if (pos == m_rawDataCollected->end()) {
114  B2WARNING("addValue: FilterID " << fID << " not known to this subgraph " << m_id.print() << " - nothing added!");
115  return;
116  }
117  pos->second.add(value);
118  return;
119  }
120 
122  const std::unordered_map<FilterType, MinMax>& getFinalQuantileValues()
123  {
124  for (auto& entry : *m_rawDataCollected) {
125  std::pair<double, double> results = entry.second.getMinMax();
126  checkAndReplaceIfMinMax(entry.first, results.first);
127  checkAndReplaceIfMinMax(entry.first, results.second);
128  }
129 
130  std::string out;
131  for (auto& entry : m_minMaxValues) {
132  out += entry.first + " " + entry.second.print() + " ";
133  }
134  B2DEBUG(1, "SubGraph::getFinalQuantileValues: minMaxFound:\n" << out);
135  return m_minMaxValues;
136  }
137 
139  unsigned idCheckAndUpdate(const std::vector<unsigned>& ids)
140  {
141  unsigned nUpdated = 0;
142  for (unsigned id : ids) {
143 
144  if (m_id.hasElement(id)) {
145  m_id.updateID(id);
146  nUpdated++;
147  }
148  }
149  return nUpdated;
150  }
151 
153  std::vector<FullSecID> getSectorsOfSensor(VxdID& sensor)
154  {
155  std::vector<FullSecID> foundIDs;
156 
157  for (auto& sector : m_id) {
158  if (sensor != FullSecID(sector).getVxdID()) continue;
159  foundIDs.push_back(FullSecID(sector));
160  }
161  return foundIDs;
162  }
163  };
165 }
166 
Belle2::SubGraph::m_found
unsigned m_found
counts number of times this subgraph was found.
Definition: SubGraph.h:47
Belle2::SubGraph::prepareDataCollection
void prepareDataCollection(std::pair< double, double > quantiles)
takes care of being able to use the data collector. please use for quantiles [min,...
Definition: SubGraph.h:106
Belle2::MinMax
small class for storing min and max.
Definition: MinMax.h:33
Belle2::SubGraphID::hasElement
bool hasElement(unsigned b) const
checks if given raw secID is part of this, ignores subLayerID.
Definition: SubGraphID.h:142
Belle2::SubGraph::m_id
SubGraphID m_id
contains the IDs in the correct order (from outer to inner) as a unique identifier for this graph.
Definition: SubGraph.h:42
Belle2::SubGraph::checkSharesTrunk
bool checkSharesTrunk(const SubGraph< FilterType > &b) const
if both graphs have got the same IDs except the last one, they share a trunk.
Definition: SubGraph.h:90
Belle2::SubGraphID::updateID
bool updateID(unsigned newID)
if newID is element of this (ignoring subLayerID), oldID will be replaced by newID....
Definition: SubGraphID.h:181
Belle2::SubGraph::updateID
void updateID(SubGraphID &newID)
set newID for this subgraph.
Definition: SubGraph.h:52
Belle2::SubGraph::checkAndReplaceIfMinMax
bool checkAndReplaceIfMinMax(FilterType fID, double value)
add filter, if not added yet, checks value and replaces old ones if new one is better.
Definition: SubGraph.h:75
Belle2::Iterator
map< unsigned, const TOPSampleTimes * >::const_iterator Iterator
Iteratior for m_map.
Definition: TOPCalTimebase.cc:25
Belle2::SubGraph::~SubGraph
~SubGraph()
destructor
Definition: SubGraph.h:72
Belle2::SubGraphID::checkSharesTrunk
bool checkSharesTrunk(const SubGraphID &b) const
if both graphs have got the same IDs except the last one, they share a trunk.
Definition: SubGraphID.h:97
Belle2::SubGraph::SubGraph
SubGraph(SubGraphID &id, std::vector< FilterType > &fIDs)
constructor, mandatory iDChain musst at least contain one iD.
Definition: SubGraph.h:64
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SubGraph::m_rawDataCollected
std::unordered_map< FilterType, RawDataCollectedMinMax > * m_rawDataCollected
takes care of collecting the raw data.
Definition: SubGraph.h:49
Belle2::SubGraph
contains all relevant stuff needed for dealing with a subGraph.
Definition: SubGraph.h:40
Belle2::SubGraph::getSectorsOfSensor
std::vector< FullSecID > getSectorsOfSensor(VxdID &sensor)
returns vector containing all sectors for given sensor (if any) and empty vector if no sector of that...
Definition: SubGraph.h:161
Belle2::SubGraph::idCheckAndUpdate
unsigned idCheckAndUpdate(const std::vector< unsigned > &ids)
for given vector of ids check if any of them is part of subGraphID. If yes, update their SubLayerID....
Definition: SubGraph.h:147
Belle2::RawDataCollectedMinMax
takes care of collecting raw data and staying below RAM-threshold.
Definition: RawDataCollectedMinMax.h:37
Belle2::SubGraph::getID
const SubGraphID & getID() const
returns iD of this graph
Definition: SubGraph.h:103
Belle2::SubGraph::print
std::string print() const
"print" debugging information to a string
Definition: SubGraph.h:93
Belle2::SubGraph::addValue
void addValue(FilterType fID, double value)
adds value to rawDataCollector
Definition: SubGraph.h:117
Belle2::SubGraph::operator==
bool operator==(const SubGraph< FilterType > &b) const
overloaded '=='-operator
Definition: SubGraph.h:57
Belle2::SubGraph::getFinalQuantileValues
const std::unordered_map< FilterType, MinMax > & getFinalQuantileValues()
this deletes the old min and max values stored and replaces them with the quantiles to be found.
Definition: SubGraph.h:130
Belle2::SubGraphID::print
std::string print() const
returns string of entries.
Definition: SubGraphID.h:111
Belle2::SubGraphID
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
Definition: SubGraphID.h:34
Belle2::SubGraph::m_minMaxValues
std::unordered_map< FilterType, MinMax > m_minMaxValues
contains all min- and max-values found so far for all filters relevant for this Graph....
Definition: SubGraph.h:45