Belle II Software development
SectorGraph< FilterType > Class Template Reference

contains all subgraphs. More...

#include <SectorGraph.h>

Public Types

using Iterator = typename std::unordered_map< SubGraphID, SubGraph< FilterType > >::iterator
 for better readability.
 

Public Member Functions

 SectorGraph (const std::vector< FilterType > &fIDs)
 constructor expects filterIDs.
 
Iterator find (SubGraphID idChain)
 find entry.
 
Iterator begin ()
 returns begin of subGraphs.
 
Iterator end ()
 returns end of subGraphs.
 
unsigned size () const
 returns number of collected subgraphs so far.
 
unsigned long nFoundTotal () const
 returns number of occurances for all subGraphs found together.
 
Iterator add (SubGraphID &newID)
 add new subgraph if not added already.
 
std::string print (bool fullPrint=true) const
 returns a string giving an overview of the graph.
 
unsigned pruneGraph (double rarenessCut)
 returns removed occurances.
 
int getAbsThreshold (int relThreshold)
 Get the absolute treshold (# nfound) given a relative threshold.
 
unsigned pruneGraphBeforeTraining (int absThreshold)
 returns removed occurances.
 
void output_nfound ()
 Output in a txt file id & nfound of subgraphs.
 
void updateSubLayerIDs ()
 finds sectors having inner sectors in same layer and update them in the subGraph-ID.
 
std::vector< FullSecIDgetAllFullSecIDsOfSensor (VxdID sensor)
 returns a Vector containing all FullSecIDs found for given sensor.
 
const std::vector< FilterType > & getFilterTypes () const
 returns a const reference to the filterTypes stored in this graph
 

Protected Attributes

std::unordered_map< SubGraphID, SubGraph< FilterType > > m_subgraphs
 contains all subgraphs.
 
std::vector< FilterType > m_filterIDs
 ids of all filterTypes to be stored by subGraphs.
 

Detailed Description

template<class FilterType>
class Belle2::SectorGraph< FilterType >

contains all subgraphs.

The ids used for the subgraphs have to be sorted from outer to inner sectors.

Definition at line 31 of file SectorGraph.h.

Member Typedef Documentation

◆ Iterator

using Iterator = typename std::unordered_map<SubGraphID, SubGraph<FilterType> >::iterator

for better readability.

Definition at line 43 of file SectorGraph.h.

Constructor & Destructor Documentation

◆ SectorGraph()

SectorGraph ( const std::vector< FilterType > &  fIDs)
inlineexplicit

constructor expects filterIDs.

Definition at line 39 of file SectorGraph.h.

39 : m_filterIDs(fIDs)
40 { if (m_filterIDs.empty()) { B2FATAL("SectorGraph-constructor: passed filterIDs are empty, this is an illegal usage of this class!"); } }
std::vector< FilterType > m_filterIDs
ids of all filterTypes to be stored by subGraphs.
Definition: SectorGraph.h:35

Member Function Documentation

◆ add()

Iterator add ( SubGraphID newID)
inline

add new subgraph if not added already.

Definition at line 66 of file SectorGraph.h.

67 {
68 if (m_subgraphs.find(newID) != end())
69 { B2WARNING("SectorGraph::add: given ID " << newID.print() << " is already in graph, not added again..."); return end(); }
70 std::pair<Iterator, bool> pos = m_subgraphs.insert({newID, SubGraph<FilterType>(newID, m_filterIDs)});
71 B2DEBUG(20, "SectorGraph::add: new subgraph added: " << pos.first->second.print());
72 return pos.first;
73 }
Iterator end()
returns end of subGraphs.
Definition: SectorGraph.h:52
std::unordered_map< SubGraphID, SubGraph< FilterType > > m_subgraphs
contains all subgraphs.
Definition: SectorGraph.h:33

◆ begin()

Iterator begin ( )
inline

returns begin of subGraphs.

Definition at line 49 of file SectorGraph.h.

49{ return m_subgraphs.begin(); }

◆ end()

Iterator end ( )
inline

returns end of subGraphs.

Definition at line 52 of file SectorGraph.h.

52{ return m_subgraphs.end(); }

◆ find()

Iterator find ( SubGraphID  idChain)
inline

find entry.

returns end() if not found.

Definition at line 46 of file SectorGraph.h.

46{ return m_subgraphs.find(idChain); }

◆ getAbsThreshold()

int getAbsThreshold ( int  relThreshold)
inline

Get the absolute treshold (# nfound) given a relative threshold.

Definition at line 168 of file SectorGraph.h.

169 {
170 if (relThreshold == 0) {return 0;}
171 B2INFO("Relative threshold : " << relThreshold << " %");
172 int xmax = 100000;
173
174 TH1D* h_nfound = new TH1D("h", "# times that subgraphs were found n_found times", xmax, 0, xmax);
175
176 for (auto& subGraphEntry : m_subgraphs) {
177 SubGraph<FilterType>& graph = subGraphEntry.second;
178 h_nfound->Fill(graph.getFound());
179 }
180
181 if (h_nfound->GetEntries() == 0) {
182 B2ERROR("nfound histogram empty.");
183 delete h_nfound;
184 return 0;
185 }
186
187 TH1* hc_nfound = h_nfound->GetCumulative();
188 hc_nfound->Scale(1 / h_nfound->GetEntries());
189
190 for (int nfound = 1; nfound < hc_nfound->GetNbinsX(); nfound++) {
191 if (hc_nfound->GetBinContent(nfound) > relThreshold / 100.) {
192 B2INFO("Absolute threshold : remove every graph with nfound < " << nfound);
193 delete h_nfound;
194 delete hc_nfound;
195 return nfound;
196 }
197 }
198
199 // In case no value for nfound is found
200 B2ERROR("No nfound value found.");
201 delete h_nfound;
202 delete hc_nfound;
203 return 0;
204 }

◆ getAllFullSecIDsOfSensor()

std::vector< FullSecID > getAllFullSecIDsOfSensor ( VxdID  sensor)
inline

returns a Vector containing all FullSecIDs found for given sensor.

Definition at line 300 of file SectorGraph.h.

301 {
302 std::vector<FullSecID> foundIDs;
303
304 for (auto& subGraph : m_subgraphs) {
305 std::vector<FullSecID> sectorsFound = subGraph.second.getSectorsOfSensor(sensor);
306 if (sectorsFound.empty()) continue;
307 foundIDs.insert(foundIDs.end(), sectorsFound.begin(), sectorsFound.end());
308 }
309 B2DEBUG(20, "getAllFullSecIDsOfSensor: VxdID " << sensor << " has " << foundIDs.size() << " sectors in this graph");
310 return foundIDs;
311 }

◆ getFilterTypes()

const std::vector< FilterType > & getFilterTypes ( ) const
inline

returns a const reference to the filterTypes stored in this graph

Definition at line 314 of file SectorGraph.h.

314{ return m_filterIDs; }

◆ nFoundTotal()

unsigned long nFoundTotal ( ) const
inline

returns number of occurances for all subGraphs found together.

Definition at line 58 of file SectorGraph.h.

59 {
60 unsigned long nFound = 0;
61 for (auto& pack : m_subgraphs) { nFound += pack.second.getFound(); }
62 return nFound;
63 }

◆ output_nfound()

void output_nfound ( )
inline

Output in a txt file id & nfound of subgraphs.

Definition at line 231 of file SectorGraph.h.

232 {
233 std::ofstream out;
234 out.open("output_nfound.txt");
235 for (auto& subGraphEntry : m_subgraphs) {
236 SubGraph<FilterType>& graph = subGraphEntry.second;
237 out << graph.print() << std::endl;
238 }
239 out.close();
240 }

◆ print()

std::string print ( bool  fullPrint = true) const
inline

returns a string giving an overview of the graph.

Definition at line 76 of file SectorGraph.h.

77 {
78 unsigned nSubgraphs = m_subgraphs.size();
79 std::string out = "graph has got " + std::to_string(nSubgraphs) + " entries:\n";
80 out += "now printing " + (fullPrint ? std::string("full") : std::string("short version of the")) + " graph:\n";
81 for (const auto& entry : m_subgraphs) {
82 if (!fullPrint and nSubgraphs % 100 != 0) continue; // printing only 100 subgraphs of mainGraph for the short version.
83 out += entry.second.print() + "\n";
84 }
85 return out;
86 }
STL namespace.

◆ pruneGraph()

unsigned pruneGraph ( double  rarenessCut)
inline

returns removed occurances.

vector of trunks (= outer sector(s)):

Definition at line 89 of file SectorGraph.h.

90 {
91 //sanity checks:
92 if (rarenessCut < 0 or rarenessCut >= 1)
93 { B2WARNING("pruneGraph: rarenessCut is rubbish: " << rarenessCut << ", stopping prune-process."); return 0; }
94 if (rarenessCut == 0) { B2DEBUG(20, "pruneGraph: nothing to be done, stopping prune-process."); return 0; }
95
97 // .first counts total number of occurances of branches of this trunk (= outer sector(s))
98 // .second is a vector of pointers to the subgraphs of that trunk.
99 std::vector< std::pair<unsigned, std::vector<SubGraph<FilterType>*> >> trunks;
100
101 // find those sharing a trunk (trunkTotal) and cluster them in these trunks:
102 for (auto& subGraphEntry : m_subgraphs) {
103 SubGraph<FilterType>& graph = subGraphEntry.second;
104 bool found = false;
105 for (auto& trunk : trunks) {
106 if (graph.checkSharesTrunk(*(trunk.second.at(0)))) {
107 trunk.first += graph.getFound();
108 trunk.second.push_back(&graph);
109 found = true;
110 continue;
111 }
112
113 if (found) continue;
114 // this should really be avoided but it in this case it seems to be on purpose (push_back to container that is iterated)
115 // tell cppcheck it is fine..
116 // cppcheck-suppress invalidContainerLoop
117 trunks.push_back({graph.getFound(), {&graph} });
118 }
119 }
120
121 unsigned long nFoundB4 = nFoundTotal(), nKilled = 0;
122 unsigned sizeb4 = size();
123 B2DEBUG(20, "pruneGraph - before pruning: graph of size " << sizeb4 << " has " << trunks.size() << " trunks with " << nFoundB4 <<
124 " total found.");
125
126 // collect subGraphs (=branches) to be deleted:
127 std::vector<SubGraph<FilterType>*> deadBranches;
128 for (auto& trunk : trunks) {
129 double trunkCut = rarenessCut * double(trunk.first);
130
131 // sort branches of trunk by rareness (rarest one first):
132 std::sort(trunk.second.begin(),
133 trunk.second.end(),
134 [](const SubGraph<FilterType>* a, const SubGraph<FilterType>* b)
135 -> bool { return a->getFound() < b->getFound(); });
136
137 // skip trunk, if there are no rare branches.
138 auto pos = trunk.second.begin();
139 if (double((**pos).getFound()) >= trunkCut) continue;
140
141 // collect branches to be killed starting with the smallest one and stop when reaching those which have been slipping above the threshold in the process:
142 while (pos != trunk.second.end()) {
143 // mark the most rarest entry:
144 deadBranches.push_back(*pos);
145 trunkCut -= double((**pos).getFound()) * rarenessCut;
146
147 // reached the point when all graphs left are now good enough for us: -> stopping loop.
148 if (double((**pos).getFound()) >= trunkCut) break;
149 ++pos;
150 }
151 } // looping over trunks
152
153 if (deadBranches.empty()) { B2DEBUG(20, "pruneGraph: no rare branches found - stopping pruning process."); return 0; }
154
155 // kill selected ones:
156 for (auto* graph : deadBranches) {
157 nKilled += graph->getFound();
158 m_subgraphs.erase(graph->getID());
159 }
160
161 B2DEBUG(20, "pruneGraph - after pruning graph with size (before/after " << sizeb4 << "/" << size() <<
162 ") and nFound (before/after/killed " << nFoundB4 << "/" << nFoundTotal() << "/" << nKilled);
163
164 return nKilled;
165 }
unsigned size() const
returns number of collected subgraphs so far.
Definition: SectorGraph.h:55
unsigned long nFoundTotal() const
returns number of occurances for all subGraphs found together.
Definition: SectorGraph.h:58

◆ pruneGraphBeforeTraining()

unsigned pruneGraphBeforeTraining ( int  absThreshold)
inline

returns removed occurances.

Definition at line 207 of file SectorGraph.h.

208 {
209 if (absThreshold == 0) {return 0;}
210
211 int killed = 0;
212
213 std::vector<SubGraph<FilterType>*> deadBranches;
214
215 for (auto& subGraphEntry : m_subgraphs) {
216 SubGraph<FilterType>& graph = subGraphEntry.second;
217 if (int(graph.getFound()) <= absThreshold) {
218 deadBranches.push_back(&graph);
219 }
220 }
221
222 for (auto& graph : deadBranches) {
223 m_subgraphs.erase(graph->getID());
224 killed ++;
225 }
226
227 return killed;
228 }

◆ size()

unsigned size ( ) const
inline

returns number of collected subgraphs so far.

Definition at line 55 of file SectorGraph.h.

55{ return m_subgraphs.size(); }

◆ updateSubLayerIDs()

void updateSubLayerIDs ( )
inline

finds sectors having inner sectors in same layer and update them in the subGraph-ID.

Definition at line 245 of file SectorGraph.h.

246 {
247 unsigned nUpdated = 0, // counts sectors which shall be updated
248 nFound = 0, // counts sectors which were found (without double entry removal)
249 nGraphsUpdated = 0; // counts graphs which were updated
250
251 // collects the secIDs which have got inner sectors on same sensor:
252 std::vector<unsigned> idsFound;
253 std::string idsPrinted;
254
255 // collect all SecIDs where SubLayer has to be updated.
256 for (auto& subGraphEntry : m_subgraphs) {
257 SubGraphID graphID = subGraphEntry.second.getID();
258 std::vector<unsigned> found = graphID.hasSharedLayer();
259 if (found.empty()) continue;
260 idsFound.insert(idsFound.end(), found.begin(), found.end());
261 }
262 for (unsigned id : idsFound) { idsPrinted += FullSecID(id).getFullSecString() + " "; }
263 B2DEBUG(20, "updateSubLayerIDs: before unique of found ids, following IDs are recorded: \n" << idsPrinted);
264 nFound += idsFound.size();
265 std::sort(idsFound.begin(), idsFound.end());
266 idsFound.erase(std::unique(idsFound.begin(), idsFound.end()), idsFound.end());
267 nUpdated += idsFound.size();
268
269 idsPrinted = "";
270 for (unsigned id : idsFound) { idsPrinted += FullSecID(id).getFullSecString() + " "; }
271 B2DEBUG(20, "updateSubLayerIDs: before updating Subgraphs, following IDs have to be updated: \n" << idsPrinted);
272
273
274 // update all subGraphIDs where subLayerID has to be increased:
275 for (auto& subGraphEntry : m_subgraphs) {
276 SubGraph<FilterType>& graph = subGraphEntry.second;
277 unsigned nSecsUpdated = graph.idCheckAndUpdate(idsFound);
278 if (nSecsUpdated == 0) {
279 B2DEBUG(25, "updateSubLayerIDs: was _not_ updated: " << graph.getID().print());
280 continue;
281 }
282 nGraphsUpdated++;
283 B2DEBUG(25, "updateSubLayerIDs: was updated " << nSecsUpdated << " times: " << graph.getID().print());
284 }
285
286 B2DEBUG(20, "updateSubLayerIDs: nSectors found/updated: " << nFound << "/" << nUpdated << ", nSubgraphs updated: " <<
287 nGraphsUpdated);
288
289 // create new map of Subgraphs with updated LayerIDs
290 // subGraph: copy with updated iD.
291 // SubGraphID: a.isElementOf(SubGraphID& b) <- checks if given sectorPack ( >= 1 sector) is part of a, ignores subLayerID.
292 // SubGraphID: a.replaceElement(SubGraphID& b) <- checks if given sectorPack ( >= 1 sector) is part of a, ignores subLayerID. if yes, replaces element(s). WARNING only works with complete replacement of SubGraphID (entries are const).
293 // SubGraphID: a.areTheSameSectors(SubGraphID& b) <- checks if sectors are identical (while ignoring the subLayerID)
294 // SubGraphID: a.sharesLayer() <- returns IDs of entries being inner friend of sectors on same layer.
295
296 // return new map of Subgraphs.
297 }

Member Data Documentation

◆ m_filterIDs

std::vector<FilterType> m_filterIDs
protected

ids of all filterTypes to be stored by subGraphs.

Definition at line 35 of file SectorGraph.h.

◆ m_subgraphs

std::unordered_map<SubGraphID, SubGraph<FilterType> > m_subgraphs
protected

contains all subgraphs.

Definition at line 33 of file SectorGraph.h.


The documentation for this class was generated from the following file: