Belle II Software development
QuadTreeProcessor< AX, AY, AData > Class Template Referenceabstract

This abstract class serves as a base class for all implementations of track processors. More...

#include <QuadTreeProcessor.h>

Public Types

using Item = QuadTreeItem<AData>
 The QuadTree will only see items of this type.
 
using QuadTree = QuadTreeNode<AX, AY, Item>
 The used QuadTree.
 
using XSpan = typename QuadTree::XSpan
 This pair describes the span in X for a node.
 
using YSpan = typename QuadTree::YSpan
 This pair describes the span in Y for a node.
 
using XYSpans = std::pair<XSpan, YSpan>
 This pair of spans describes the span of a node.
 
using QuadTreeChildren = typename QuadTree::Children
 Alias for the QuadTree Children.
 
using CandidateReceiver = std::function<void(const std::vector<AData*>&, QuadTree*)>
 This lambda function can be used for postprocessing.
 

Public Member Functions

 QuadTreeProcessor (int lastLevel, int seedLevel, const XYSpans &xySpans, bool debugOutput=false)
 Constructor is very simple.
 
virtual ~QuadTreeProcessor ()
 Destructor deletes the quad tree.
 
void clear ()
 Delete all the QuadTreeItems in the tree and clear the tree.
 
void seed (const std::vector< AData * > &datas)
 Fill in the items in the given vector.
 
std::vector< AData * > getAssignedItems ()
 Get items that have been assigned to the seed level The returned elements are unique even if items are assigned multiple times.
 
void fill (const CandidateReceiver &candidateReceiver, int nHitsThreshold)
 Start filling the already created tree.
 
void fill (const CandidateReceiver &candidateReceiver, int nHitsThreshold, float yLimit)
 Fill vector of QuadTree instances with hits.
 
virtual void afterFillDebugHook (QuadTreeChildren &children)
 Override that function if you want to receive debug output whenever the children of a node are filled the first time Maybe you want to make some nice plots or statistics.
 
const std::map< std::pair< AX, AY >, std::vector< Item * > > & getDebugInformation () const
 Return the debug information if collected.
 

Protected Member Functions

virtual XYSpans createChild (QuadTree *node, int iX, int iY) const
 Implement that function if you want to provide a new processor.
 
virtual bool isInNode (QuadTree *node, AData *item) const =0
 Implement that function if you want to provide a new processor.
 
virtual bool isLeaf (QuadTree *node) const
 Function which checks if given node is leaf Implemented as virtual to keep possibility of changing lastLevel values depending on region is phase-space (i.e.
 
int getLastLevel () const
 Return the parameter last level.
 

Protected Attributes

std::unique_ptr< QuadTreem_quadTree
 The quad tree we work with.
 
std::deque< Itemm_items
 Storage space for the items that are referenced by the quad tree nodes.
 
std::vector< QuadTree * > m_seededTrees
 Vector of QuadTrees QuadTree instances (which are filled in the vector) cover the whole Legendre phase-space; each instance is processes independently.
 

Private Member Functions

void fillGivenTree (QuadTree *node, const CandidateReceiver &candidateReceiver, int nItemsThreshold, AY yLimit)
 Internal function to do the real quad tree search: fill the nodes, check which of the n*m bins we need to process further and go one level deeper.
 
void createChildren (QuadTree *node, QuadTreeChildren &m_children) const
 Creates the sub node of a given node.
 
void fillChildren (QuadTree *node, const std::vector< Item * > &items)
 This function is called by fillGivenTree and fills the items into the corresponding children.
 
void callResultFunction (QuadTree *node, const CandidateReceiver &candidateReceiver) const
 When a node is accepted as a result, we extract a vector with the items (back transformed to AData*) and pass it together with the result node to the candidate receiver function.
 

Private Attributes

int m_lastLevel
 The last level to be filled.
 
int m_seedLevel
 The first level to be filled, effectively skip forward to this higher granularity level.
 
bool m_debugOutput
 A flag to control the creation of the debug output.
 
std::map< std::pair< AX, AY >, std::vector< Item * > > m_debugOutputMap
 The calculated debug map.
 

Detailed Description

template<typename AX, typename AY, class AData>
class Belle2::TrackFindingCDC::QuadTreeProcessor< AX, AY, AData >

This abstract class serves as a base class for all implementations of track processors.

It provides some functions to create, fill, clear and postprocess a quad tree. If you want to use your own class as a quad tree item, you have to overload this processor. You have provide only the two functions isInNode and createChild.

Definition at line 40 of file QuadTreeProcessor.h.

Member Typedef Documentation

◆ CandidateReceiver

template<typename AX, typename AY, class AData>
using CandidateReceiver = std::function<void(const std::vector<AData*>&, QuadTree*)>

This lambda function can be used for postprocessing.

Definition at line 62 of file QuadTreeProcessor.h.

◆ Item

template<typename AX, typename AY, class AData>
using Item = QuadTreeItem<AData>

The QuadTree will only see items of this type.

Definition at line 44 of file QuadTreeProcessor.h.

◆ QuadTree

template<typename AX, typename AY, class AData>
using QuadTree = QuadTreeNode<AX, AY, Item>

The used QuadTree.

Definition at line 47 of file QuadTreeProcessor.h.

◆ QuadTreeChildren

template<typename AX, typename AY, class AData>
using QuadTreeChildren = typename QuadTree::Children

Alias for the QuadTree Children.

Definition at line 59 of file QuadTreeProcessor.h.

◆ XSpan

template<typename AX, typename AY, class AData>
using XSpan = typename QuadTree::XSpan

This pair describes the span in X for a node.

Definition at line 50 of file QuadTreeProcessor.h.

◆ XYSpans

template<typename AX, typename AY, class AData>
using XYSpans = std::pair<XSpan, YSpan>

This pair of spans describes the span of a node.

Definition at line 56 of file QuadTreeProcessor.h.

◆ YSpan

template<typename AX, typename AY, class AData>
using YSpan = typename QuadTree::YSpan

This pair describes the span in Y for a node.

Definition at line 53 of file QuadTreeProcessor.h.

Constructor & Destructor Documentation

◆ QuadTreeProcessor()

template<typename AX, typename AY, class AData>
QuadTreeProcessor ( int lastLevel,
int seedLevel,
const XYSpans & xySpans,
bool debugOutput = false )
inline

Constructor is very simple.

The QuadTree has to be constructed elsewhere.

Parameters
lastLeveldescribing the last search level for the quad tree creation
seedLevelfirst level to be filled, effectively skip forward to this higher granularity level
xySpanspair of spans describing the span of a node
debugOutputenable debug output

Definition at line 72 of file QuadTreeProcessor.h.

76 : m_quadTree{std::make_unique<QuadTree>(xySpans.first, xySpans.second, 0, nullptr)}
77 , m_lastLevel(lastLevel)
78 , m_seedLevel(seedLevel)
79 , m_debugOutput(debugOutput)
80 , m_debugOutputMap()
81 {
82 }

◆ ~QuadTreeProcessor()

template<typename AX, typename AY, class AData>
virtual ~QuadTreeProcessor ( )
inlinevirtual

Destructor deletes the quad tree.

Definition at line 87 of file QuadTreeProcessor.h.

88 {
89 clear();
90 }

Member Function Documentation

◆ afterFillDebugHook()

template<typename AX, typename AY, class AData>
virtual void afterFillDebugHook ( QuadTreeChildren & children)
inlinevirtual

Override that function if you want to receive debug output whenever the children of a node are filled the first time Maybe you want to make some nice plots or statistics.

Definition at line 359 of file QuadTreeProcessor.h.

360 {
361 if (not m_debugOutput) return;
362 for (QuadTree& childNode : children) {
363 if (childNode.getLevel() != getLastLevel()) continue; // Only write the lowest level
364 //m_debugOutputMap[ {childNode.getXMean(), childNode.getYMean()}] = childNode.getItems();
365 }
366 }

◆ callResultFunction()

template<typename AX, typename AY, class AData>
void callResultFunction ( QuadTree * node,
const CandidateReceiver & candidateReceiver ) const
inlineprivate

When a node is accepted as a result, we extract a vector with the items (back transformed to AData*) and pass it together with the result node to the candidate receiver function.

Definition at line 291 of file QuadTreeProcessor.h.

292 {
293 const std::vector<Item*>& foundItems = node->getItems();
294 std::vector<AData*> candidate;
295 candidate.reserve(foundItems.size());
296
297 for (Item* item : foundItems) {
298 item->setUsedFlag();
299 candidate.push_back(item->getPointer());
300 }
301
302 candidateReceiver(candidate, node);
303 }

◆ clear()

template<typename AX, typename AY, class AData>
void clear ( )
inline

Delete all the QuadTreeItems in the tree and clear the tree.

Definition at line 95 of file QuadTreeProcessor.h.

96 {
97 m_seededTrees.clear();
98 m_quadTree->clearChildren();
99 m_quadTree->clearItems();
100 m_items.clear();
101 }

◆ createChild()

template<typename AX, typename AY, class AData>
virtual XYSpans createChild ( QuadTree * node,
int iX,
int iY ) const
inlineprotectedvirtual

Implement that function if you want to provide a new processor.

It decides which node-spans the n * m children of the node should have. It is called when creating the nodes. The two indices iX and iY tell you where the new node will be created (as node.children[iX][iY]). You can check some information on the level or the x- or y-values by using the methods implemented for node.

Returns
a XYSpan pair of a x- and a y-span that the new child should have. If you don nt want to provide custom spans, just return XYSpans(XSpan(node->getXBinBound(iX), node->getXBinBound(iX + 1)), YSpan(node->getYBinBound(iY), node->getYBinBound(iY + 1)));

Definition at line 314 of file QuadTreeProcessor.h.

315 {
316 AX xMin = node->getXLowerBound(iX);
317 AX xMax = node->getXUpperBound(iX);
318 AY yMin = node->getYLowerBound(iY);
319 AY yMax = node->getYUpperBound(iY);
320 return XYSpans({xMin, xMax}, {yMin, yMax});
321 }

◆ createChildren()

template<typename AX, typename AY, class AData>
void createChildren ( QuadTree * node,
QuadTreeChildren & m_children ) const
inlineprivate

Creates the sub node of a given node.

This function is called by fillGivenTree. To calculate the spans of the children nodes the user-defined function createChiildWithParent is used.

Definition at line 252 of file QuadTreeProcessor.h.

253 {
254 for (int i = 0; i < node->getXNbins(); ++i) {
255 for (int j = 0; j < node->getYNbins(); ++j) {
256 const XYSpans& xySpans = createChild(node, i, j);
257 const XSpan& xSpan = xySpans.first;
258 const YSpan& ySpan = xySpans.second;
259 m_children.push_back(QuadTree(xSpan, ySpan, node->getLevel() + 1, node));
260 }
261 }
262 }

◆ fill() [1/2]

template<typename AX, typename AY, class AData>
void fill ( const CandidateReceiver & candidateReceiver,
int nHitsThreshold )
inline

Start filling the already created tree.

Parameters
candidateReceiverthe lambda function to call after a node was selected
nHitsThresholdthe threshold on the number of items

Definition at line 171 of file QuadTreeProcessor.h.

172 {
173 fill(candidateReceiver, nHitsThreshold, std::numeric_limits<AY>::max());
174 }

◆ fill() [2/2]

template<typename AX, typename AY, class AData>
void fill ( const CandidateReceiver & candidateReceiver,
int nHitsThreshold,
float yLimit )
inline

Fill vector of QuadTree instances with hits.

Parameters
candidateReceiverthe lambda function to call after a node was selected
nHitsThresholdthe threshold on the number of items
yLimitthe threshold in the rho (curvature) variable

Definition at line 182 of file QuadTreeProcessor.h.

183 {
184 std::vector<QuadTree*> quadTrees = m_seededTrees;
185 std::sort(quadTrees.begin(), quadTrees.end(), [](QuadTree * quadTree1, QuadTree * quadTree2) {
186 return quadTree1->getNItems() > quadTree2->getNItems();
187 });
188
189 for (QuadTree* tree : quadTrees) {
190 erase_remove_if(tree->getItems(), [](Item * hit) { return hit->isUsed(); });
191 fillGivenTree(tree, candidateReceiver, nHitsThreshold, yLimit);
192 }
193 }

◆ fillChildren()

template<typename AX, typename AY, class AData>
void fillChildren ( QuadTree * node,
const std::vector< Item * > & items )
inlineprivate

This function is called by fillGivenTree and fills the items into the corresponding children.

For this the user-defined method isInNode is called.

Definition at line 268 of file QuadTreeProcessor.h.

269 {
270 const size_t neededSize = 2 * items.size();
271 for (QuadTree& child : node->getChildren()) {
272 child.reserveItems(neededSize);
273 }
274
275 for (Item* item : items) {
276 if (item->isUsed()) continue;
277
278 for (QuadTree& child : node->getChildren()) {
279 if (isInNode(&child, item->getPointer())) {
280 child.insertItem(item);
281 }
282 }
283 }
284 afterFillDebugHook(node->getChildren());
285 }

◆ fillGivenTree()

template<typename AX, typename AY, class AData>
void fillGivenTree ( QuadTree * node,
const CandidateReceiver & candidateReceiver,
int nItemsThreshold,
AY yLimit )
inlineprivate

Internal function to do the real quad tree search: fill the nodes, check which of the n*m bins we need to process further and go one level deeper.

Definition at line 200 of file QuadTreeProcessor.h.

204 {
205 if (node->getNItems() < nItemsThreshold) {
206 return;
207 }
208
209 if ((node->getYMin() > yLimit) or (-node->getYMax() > yLimit)) {
210 return;
211 }
212
213 if (isLeaf(node)) {
214 callResultFunction(node, candidateReceiver);
215 return;
216 }
217
218 if (node->getChildren().empty()) {
219 this->createChildren(node, node->getChildren());
220 }
221
222 if (!node->checkFilled()) {
223 fillChildren(node, node->getItems());
224 node->setFilled();
225 }
226
227 std::vector<QuadTree*> children;
228 for (QuadTree& child : node->getChildren()) {
229 children.push_back(&child);
230 }
231 const auto compareNItems = [](const QuadTree * lhs, const QuadTree * rhs) {
232 return lhs->getNItems() < rhs->getNItems();
233 };
234
235 // Explicitly count down the children
236 const int nChildren = children.size();
237 for (int iChild = 0; iChild < nChildren; ++iChild) {
238 auto itHeaviestChild = std::max_element(children.begin(), children.end(), compareNItems);
239 QuadTree* heaviestChild = *itHeaviestChild;
240 children.erase(itHeaviestChild);
241 // After we have processed some children we need to get rid of the already used hits in all the children,
242 // because this can change the number of items drastically
243 erase_remove_if(heaviestChild->getItems(), [&](Item * hit) { return hit->isUsed(); });
244 this->fillGivenTree(heaviestChild, candidateReceiver, nItemsThreshold, yLimit);
245 }
246 }

◆ getAssignedItems()

template<typename AX, typename AY, class AData>
std::vector< AData * > getAssignedItems ( )
inline

Get items that have been assigned to the seed level The returned elements are unique even if items are assigned multiple times.

Definition at line 152 of file QuadTreeProcessor.h.

153 {
154 std::vector<const TrackingUtilities::CDCWireHit*> result;
155 for (QuadTree* seededTree : m_seededTrees) {
156 for (Item* item : seededTree->getItems()) {
157 result.push_back(item->getPointer());
158 }
159 }
160 std::sort(result.begin(), result.end());
161 result.erase(std::unique(result.begin(), result.end()), result.end());
162 return result;
163 }

◆ getDebugInformation()

template<typename AX, typename AY, class AData>
const std::map< std::pair< AX, AY >, std::vector< Item * > > & getDebugInformation ( ) const
inline

Return the debug information if collected.

Definition at line 371 of file QuadTreeProcessor.h.

372 {
373 return m_debugOutputMap;
374 }

◆ getLastLevel()

template<typename AX, typename AY, class AData>
int getLastLevel ( ) const
inlineprotected

Return the parameter last level.

Definition at line 349 of file QuadTreeProcessor.h.

350 {
351 return m_lastLevel;
352 }

◆ isInNode()

template<typename AX, typename AY, class AData>
virtual bool isInNode ( QuadTree * node,
AData * item ) const
protectedpure virtual

Implement that function if you want to provide a new processor.

It is called when filling the quad tree after creation. For every item in a node and every child node this function gets called and should decide, if the item should go into this child node or not.

Parameters
nodechild node
itemitem to be filled into the child node or not
Returns
true if this item belongs into this node.

◆ isLeaf()

template<typename AX, typename AY, class AData>
virtual bool isLeaf ( QuadTree * node) const
inlineprotectedvirtual

Function which checks if given node is leaf Implemented as virtual to keep possibility of changing lastLevel values depending on region is phase-space (i.e.

setting lastLevel as a function of Y-variable)

Definition at line 337 of file QuadTreeProcessor.h.

338 {
339 if (node->getLevel() >= m_lastLevel) {
340 return true;
341 } else {
342 return false;
343 }
344 }

◆ seed()

template<typename AX, typename AY, class AData>
void seed ( const std::vector< AData * > & datas)
inline

Fill in the items in the given vector.

They are transformed to QuadTreeItems internally.

Definition at line 106 of file QuadTreeProcessor.h.

107 {
108 // Create the items
109 for (AData* data : datas) {
110 m_items.emplace_back(data);
111 }
112
113 // Creating the seed level
114 long nSeedBins = pow(2, m_seedLevel);
115 m_seededTrees.reserve(nSeedBins * nSeedBins);
116
117 // Expand the first levels to the seed sectors
118 m_seededTrees.push_back(m_quadTree.get());
119 std::vector<QuadTree*> nextSeededTrees;
120
121 for (int level = 0; level < m_seedLevel; ++level) {
122 for (QuadTree* node : m_seededTrees) {
123 if (node->getChildren().empty()) {
124 this->createChildren(node, node->getChildren());
125 }
126 for (QuadTree& child : node->getChildren()) {
127 nextSeededTrees.push_back(&child);
128 }
129 }
130 std::swap(nextSeededTrees, m_seededTrees);
131 nextSeededTrees.clear();
132 }
133
134 // Fill the seed level with the items
135 for (QuadTree* seededTree : m_seededTrees) {
136 seededTree->reserveItems(m_items.size());
137
138 for (Item& item : m_items) {
139 if (item.isUsed()) continue;
140 if (isInNode(seededTree, item.getPointer())) {
141 seededTree->insertItem(&item);
142 }
143 }
144 }
145 }

Member Data Documentation

◆ m_debugOutput

template<typename AX, typename AY, class AData>
bool m_debugOutput
private

A flag to control the creation of the debug output.

Definition at line 398 of file QuadTreeProcessor.h.

◆ m_debugOutputMap

template<typename AX, typename AY, class AData>
std::map<std::pair<AX, AY>, std::vector<Item*> > m_debugOutputMap
private

The calculated debug map.

Definition at line 401 of file QuadTreeProcessor.h.

◆ m_items

template<typename AX, typename AY, class AData>
std::deque<Item> m_items
protected

Storage space for the items that are referenced by the quad tree nodes.

Definition at line 381 of file QuadTreeProcessor.h.

◆ m_lastLevel

template<typename AX, typename AY, class AData>
int m_lastLevel
private

The last level to be filled.

Definition at line 392 of file QuadTreeProcessor.h.

◆ m_quadTree

template<typename AX, typename AY, class AData>
std::unique_ptr<QuadTree> m_quadTree
protected

The quad tree we work with.

Definition at line 378 of file QuadTreeProcessor.h.

◆ m_seededTrees

template<typename AX, typename AY, class AData>
std::vector<QuadTree*> m_seededTrees
protected

Vector of QuadTrees QuadTree instances (which are filled in the vector) cover the whole Legendre phase-space; each instance is processes independently.

Definition at line 388 of file QuadTreeProcessor.h.

◆ m_seedLevel

template<typename AX, typename AY, class AData>
int m_seedLevel
private

The first level to be filled, effectively skip forward to this higher granularity level.

Definition at line 395 of file QuadTreeProcessor.h.


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