10#include <tracking/trackFindingCDC/legendre/quadtree/QuadTreeNode.h>
11#include <tracking/trackFindingCDC/legendre/quadtree/QuadTreeItem.h>
13#include <tracking/trackFindingCDC/utilities/Algorithms.h>
28 namespace TrackFindingCDC {
36 template<
typename AX,
typename AY,
class AData>
72 bool debugOutput =
false)
103 void seed(
const std::vector<AData*>& datas)
106 for (AData* data : datas) {
116 std::vector<QuadTree*> nextSeededTrees;
118 for (
int level = 0; level <
m_seedLevel; ++level) {
120 if (node->getChildren().empty()) {
123 for (
QuadTree& child : node->getChildren()) {
124 nextSeededTrees.push_back(&child);
128 nextSeededTrees.clear();
133 seededTree->reserveItems(
m_items.size());
136 if (item.isUsed())
continue;
137 if (
isInNode(seededTree, item.getPointer())) {
138 seededTree->insertItem(&item);
151 std::vector<const CDCWireHit*> result;
153 for (
Item* item : seededTree->getItems()) {
154 result.push_back(item->getPointer());
157 std::sort(result.begin(), result.end());
158 result.erase(std::unique(result.begin(), result.end()), result.end());
170 fill(candidateReceiver, nHitsThreshold, std::numeric_limits<AY>::max());
182 std::sort(quadTrees.begin(), quadTrees.end(), [](
QuadTree * quadTree1,
QuadTree * quadTree2) {
183 return quadTree1->getNItems() > quadTree2->getNItems();
187 erase_remove_if(tree->getItems(), [](
Item * hit) { return hit->isUsed(); });
188 fillGivenTree(tree, candidateReceiver, nHitsThreshold, yLimit);
202 if (node->
getNItems() < nItemsThreshold) {
224 std::vector<QuadTree*> children;
226 children.push_back(&child);
229 return lhs->
getNItems() < rhs->getNItems();
233 const int nChildren = children.size();
234 for (
int iChild = 0; iChild < nChildren; ++iChild) {
235 auto itHeaviestChild = std::max_element(children.begin(), children.end(), compareNItems);
236 QuadTree* heaviestChild = *itHeaviestChild;
237 children.erase(itHeaviestChild);
240 erase_remove_if(heaviestChild->
getItems(), [&](
Item * hit) { return hit->isUsed(); });
241 this->
fillGivenTree(heaviestChild, candidateReceiver, nItemsThreshold, yLimit);
251 for (
int i = 0; i < node->
getXNbins(); ++i) {
252 for (
int j = 0; j < node->
getYNbins(); ++j) {
254 const XSpan& xSpan = xySpans.first;
255 const YSpan& ySpan = xySpans.second;
267 const size_t neededSize = 2 * items.size();
269 child.reserveItems(neededSize);
272 for (
Item* item : items) {
273 if (item->isUsed())
continue;
276 if (
isInNode(&child, item->getPointer())) {
277 child.insertItem(item);
290 const std::vector<Item*>& foundItems = node->
getItems();
291 std::vector<AData*> candidate;
292 candidate.reserve(foundItems.size());
294 for (
Item* item : foundItems) {
296 candidate.push_back(item->getPointer());
299 candidateReceiver(candidate, node);
317 return XYSpans({xMin, xMax}, {yMin, yMax});
359 for (
QuadTree& childNode : children) {
This class serves as a wrapper around all things that should go into a QuadTree.
Class which holds quadtree structure.
Children & getChildren()
Returns the children structure of this node.
std::vector< AItem * > & getItems()
Get items from node.
std::vector< This > Children
Type of the child node structure for this node.
AY getYMax() const
Get maximal "r" value of the node.
std::array< AY, 2 > YSpan
Type for a span in the Y direction that is covered by the tree.
void setFilled()
Set status of node to "filled" (children nodes has been filled)
AY getYMin() const
Get minimal "r" value of the node.
int getLevel() const
Returns level of the node in tree (i.e., how much ancestors the node has)
AX getXLowerBound(int iBin) const
Get lower "Theta" value of given bin.
AY getYUpperBound(int iBin) const
Get upper "r" value of given bin.
std::array< AX, 2 > XSpan
Type for a span in the X direction that is covered by the tree.
AY getYLowerBound(int iBin) const
Get lower "r" value of given bin.
bool checkFilled() const
Check whether node has been processed, i.e.
int getNItems() const
Check if the node passes threshold on number of hits.
constexpr int getXNbins() const
Get number of bins in "Theta" direction.
constexpr int getYNbins() const
Get number of bins in "r" direction.
AX getXUpperBound(int iBin) const
Get upper "Theta" value of given bin.
This abstract class serves as a base class for all implementations of track processors.
void fill(const CandidateReceiver &candidateReceiver, int nHitsThreshold, float yLimit)
Fill vector of QuadTree instances with hits.
QuadTreeNode< AX, AY, Item > QuadTree
The used QuadTree.
typename QuadTree::Children QuadTreeChildren
Alias for the QuadTree Children.
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.
int m_lastLevel
The last level to be filled.
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 nee...
virtual ~QuadTreeProcessor()
Destructor deletes the quad tree.
const std::map< std::pair< AX, AY >, std::vector< Item * > > & getDebugInformation() const
Return the debug information if collected.
std::pair< XSpan, YSpan > XYSpans
This pair of spans describes the span of a node.
QuadTreeProcessor(int lastLevel, int seedLevel, const XYSpans &xySpans, bool debugOutput=false)
Constructor is very simple.
std::vector< QuadTree * > m_seededTrees
Vector of QuadTrees QuadTree instances (which are filled in the vector) cover the whole Legendre phas...
bool m_debugOutput
A flag to control the creation of the debug output.
int getLastLevel() const
Return the parameter last level.
virtual XYSpans createChild(QuadTree *node, int iX, int iY) const
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 la...
typename QuadTree::XSpan XSpan
This pair describes the span in X for a node.
std::deque< Item > m_items
Storage space for the items that are referenced by the quad tree nodes.
std::vector< AData * > getAssignedItems()
Get items that have been assigned to the seed level The returned elements are unique even if items ar...
typename QuadTree::YSpan YSpan
This pair describes the span in Y for a node.
void fill(const CandidateReceiver &candidateReceiver, int nHitsThreshold)
Start filling the already created tree.
void clear()
Delete all the QuadTreeItems in the tree and clear the tree.
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*) ...
int m_seedLevel
The first level to be filled, effectively skip forward to this higher granularity level.
void seed(const std::vector< AData * > &datas)
Fill in the items in the given vector.
std::function< void(const std::vector< AData * > &, QuadTree *)> CandidateReceiver
This lambda function can be used for postprocessing.
virtual bool isInNode(QuadTree *node, AData *item) const =0
Implement that function if you want to provide a new processor.
std::map< std::pair< AX, AY >, std::vector< Item * > > m_debugOutputMap
The calculated debug map.
virtual void afterFillDebugHook(QuadTreeChildren &children)
Override that function if you want to receive debug output whenever the children of a node are filled...
std::unique_ptr< QuadTree > m_quadTree
The quad tree we work with.
Abstract base class for different kinds of events.