11#include <framework/logging/Logger.h>
12#include <tracking/trackFindingVXD/sectorMapTools/SubGraphID.h>
13#include <tracking/trackFindingVXD/sectorMapTools/SubGraph.h>
19#include <unordered_map>
33 std::unordered_map<SubGraphID, SubGraph<FilterType>>
m_subgraphs;
40 {
if (
m_filterIDs.empty()) { B2FATAL(
"SectorGraph-constructor: passed filterIDs are empty, this is an illegal usage of this class!"); } }
43 using Iterator =
typename std::unordered_map<SubGraphID, SubGraph<FilterType>>::iterator;
60 unsigned long nFound = 0;
61 for (
auto& pack :
m_subgraphs) { nFound += pack.second.getFound(); }
69 { B2WARNING(
"SectorGraph::add: given ID " << newID.
print() <<
" is already in graph, not added again...");
return end(); }
71 B2DEBUG(20,
"SectorGraph::add: new subgraph added: " << pos.first->second.print());
76 std::string
print(
bool fullPrint =
true)
const
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";
82 if (!fullPrint and nSubgraphs % 100 != 0)
continue;
83 out += entry.second.print() +
"\n";
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; }
99 std::vector< std::pair<unsigned, std::vector<SubGraph<FilterType>*> >> trunks;
105 for (
auto& trunk : trunks) {
108 trunk.second.push_back(&graph);
117 trunks.push_back({graph.
getFound(), {&graph} });
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 <<
127 std::vector<SubGraph<FilterType>*> deadBranches;
128 for (
auto& trunk : trunks) {
129 double trunkCut = rarenessCut * double(trunk.first);
132 std::sort(trunk.second.begin(),
135 ->
bool { return a->getFound() < b->getFound(); });
138 auto pos = trunk.second.begin();
139 if (
double((**pos).getFound()) >= trunkCut)
continue;
142 while (pos != trunk.second.end()) {
144 deadBranches.push_back(*pos);
145 trunkCut -= double((**pos).getFound()) * rarenessCut;
148 if (
double((**pos).getFound()) >= trunkCut)
break;
153 if (deadBranches.empty()) { B2DEBUG(20,
"pruneGraph: no rare branches found - stopping pruning process.");
return 0; }
156 for (
auto* graph : deadBranches) {
157 nKilled += graph->getFound();
161 B2DEBUG(20,
"pruneGraph - after pruning graph with size (before/after " << sizeb4 <<
"/" <<
size() <<
162 ") and nFound (before/after/killed " << nFoundB4 <<
"/" <<
nFoundTotal() <<
"/" << nKilled);
170 if (relThreshold == 0) {
return 0;}
171 B2INFO(
"Relative threshold : " << relThreshold <<
" %");
174 TH1D* h_nfound =
new TH1D(
"h",
"# times that subgraphs were found n_found times", xmax, 0, xmax);
181 if (h_nfound->GetEntries() == 0) {
182 B2ERROR(
"nfound histogram empty.");
187 TH1* hc_nfound = h_nfound->GetCumulative();
188 hc_nfound->Scale(1 / h_nfound->GetEntries());
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);
200 B2ERROR(
"No nfound value found.");
209 if (absThreshold == 0) {
return 0;}
213 std::vector<SubGraph<FilterType>*> deadBranches;
217 if (
int(graph.
getFound()) <= absThreshold) {
218 deadBranches.push_back(&graph);
222 for (
auto& graph : deadBranches) {
234 out.open(
"output_nfound.txt");
237 out << graph.
print() << std::endl;
247 unsigned nUpdated = 0,
252 std::vector<unsigned> idsFound;
253 std::string idsPrinted;
257 SubGraphID graphID = subGraphEntry.second.getID();
259 if (found.empty())
continue;
260 idsFound.insert(idsFound.end(), found.begin(), found.end());
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();
271 B2DEBUG(20,
"updateSubLayerIDs: before updating Subgraphs, following IDs have to be updated: \n" << idsPrinted);
278 if (nSecsUpdated == 0) {
279 B2DEBUG(25,
"updateSubLayerIDs: was _not_ updated: " << graph.
getID().
print());
283 B2DEBUG(25,
"updateSubLayerIDs: was updated " << nSecsUpdated <<
" times: " << graph.
getID().
print());
286 B2DEBUG(20,
"updateSubLayerIDs: nSectors found/updated: " << nFound <<
"/" << nUpdated <<
", nSubgraphs updated: " <<
302 std::vector<FullSecID> foundIDs;
305 std::vector<FullSecID> sectorsFound = subGraph.second.getSectorsOfSensor(sensor);
306 if (sectorsFound.empty())
continue;
307 foundIDs.insert(foundIDs.end(), sectorsFound.begin(), sectorsFound.end());
309 B2DEBUG(20,
"getAllFullSecIDsOfSensor: VxdID " << sensor <<
" has " << foundIDs.size() <<
" sectors in this graph");
Class to identify a sector inside of the VXD.
std::string getFullSecString() const
returns the FullSecID coded as string compatible to secIDs stored in the xml-sectormaps
unsigned size() const
returns number of collected subgraphs so far.
std::vector< FullSecID > getAllFullSecIDsOfSensor(VxdID sensor)
returns a Vector containing all FullSecIDs found for given sensor.
SectorGraph(const std::vector< FilterType > &fIDs)
constructor expects filterIDs.
Iterator begin()
returns begin of subGraphs.
Iterator find(SubGraphID idChain)
find entry.
Iterator add(SubGraphID &newID)
add new subgraph if not added already.
void updateSubLayerIDs()
finds sectors having inner sectors in same layer and update them in the subGraph-ID.
std::string print(bool fullPrint=true) const
returns a string giving an overview of the graph.
std::vector< FilterType > m_filterIDs
ids of all filterTypes to be stored by subGraphs.
Iterator end()
returns end of subGraphs.
typename std::unordered_map< SubGraphID, SubGraph< FilterType > >::iterator Iterator
for better readability.
const std::vector< FilterType > & getFilterTypes() const
returns a const reference to the filterTypes stored in this graph
unsigned pruneGraphBeforeTraining(int absThreshold)
returns removed occurrences.
int getAbsThreshold(int relThreshold)
Get the absolute threshold (# nfound) given a relative threshold.
void output_nfound()
Output in a txt file id & nfound of subgraphs.
std::unordered_map< SubGraphID, SubGraph< FilterType > > m_subgraphs
contains all subgraphs.
unsigned pruneGraph(double rarenessCut)
returns removed occurrences.
unsigned long nFoundTotal() const
returns number of occurrences for all subGraphs found together.
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
std::vector< unsigned > hasSharedLayer() const
returns raw IDs of entries being outer friend of sectors on same layer.
std::string print() const
returns string of entries.
contains all relevant stuff needed for dealing with a subGraph.
bool checkSharesTrunk(const SubGraph< FilterType > &b) const
if both graphs have got the same IDs except the last one, they share a trunk.
unsigned getFound() const
returns found-counter.
const SubGraphID & getID() const
returns iD of this graph
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....
std::string print() const
"print" debugging information to a string
Class to uniquely identify a any structure of the PXD and SVD.
Abstract base class for different kinds of events.