Belle II Software development
SegmentCreatorFacetAutomaton.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <tracking/trackFindingCDC/findlets/minimal/SegmentCreatorFacetAutomaton.h>
9
10#include <tracking/trackingUtilities/eventdata/segments/CDCSegment2D.h>
11#include <tracking/trackingUtilities/eventdata/segments/CDCFacetSegment.h>
12#include <tracking/trackingUtilities/eventdata/segments/CDCRLWireHitSegment.h>
13
14#include <tracking/trackingUtilities/eventdata/hits/CDCFacet.h>
15
16#include <cdc/topology/CDCWire.h>
17
18#include <tracking/trackingUtilities/utilities/WeightedRelation.h>
19#include <tracking/trackingUtilities/utilities/Functional.h>
20#include <tracking/trackingUtilities/utilities/VectorRange.h>
21#include <tracking/trackingUtilities/utilities/StringManipulation.h>
22#include <tracking/trackingUtilities/utilities/Algorithms.h>
23
24#include <framework/core/ModuleParamList.templateDetails.h>
25
26#include <algorithm>
27#include <functional>
28
29using namespace Belle2;
30using namespace TrackFindingCDC;
31using namespace TrackingUtilities;
32
33namespace {
34 void setRLWireHit(CDCRLWireHitTriple& rlWireHitTriple, int iRLWireHit, const CDCRLWireHit& rlWireHit)
35 {
36 if (iRLWireHit == 0) {
37 rlWireHitTriple.setStartRLWireHit(rlWireHit);
38 } else if (iRLWireHit == 1) {
39 rlWireHitTriple.setMiddleRLWireHit(rlWireHit);
40 } else if (iRLWireHit == 2) {
41 rlWireHitTriple.setEndRLWireHit(rlWireHit);
42 } else {
43 B2ASSERT("", false);
44 }
45 }
46}
47
49{
50 return "Constructs segments by extraction of facet paths in a cellular automaton.";
51}
52
53void SegmentCreatorFacetAutomaton::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
54{
55 moduleParamList->addParameter(prefixed(prefix, "searchReversed"),
57 "Switch to construct the reversed segment if it is available in the facet graph as well.",
59
60 moduleParamList->addParameter(prefixed(prefix, "searchAlias"),
62 "Switch to construct the alias segment if it is available in the facet graph as well.",
64
65 moduleParamList->addParameter(prefixed(prefix, "relaxSingleLayerSearch"),
67 "Switch to relax the alias and reverse search for segments contained in a single layer.",
69
70 moduleParamList->addParameter(prefixed(prefix, "allSingleAliases"),
72 "Switch to activate the write out of all available orientations of single facet segments.",
74}
75
77 const std::vector<CDCFacet>& inputFacets,
78 const std::vector<TrackingUtilities::WeightedRelation<const CDCFacet>>& inputFacetRelations,
79 std::vector<CDCSegment2D>& outputSegments)
80{
81 std::vector<ConstVectorRange<CDCFacet>> facetsByICluster =
82 adjacent_groupby(inputFacets.begin(), inputFacets.end(), std::mem_fn(&CDCFacet::getICluster));
83
84 for (const ConstVectorRange<CDCFacet>& facetsInCluster : facetsByICluster) {
85 if (facetsInCluster.empty()) continue;
86
87 B2ASSERT("Expect the facets to be sorted",
88 std::is_sorted(std::begin(facetsInCluster), std::end(facetsInCluster)));
89
90 // Obtain the facets as pointers
91 std::vector<const CDCFacet*> facetPtrsInCluster = as_pointers<const CDCFacet>(facetsInCluster);
92
93 // Cut out the chunk of relevant facet relations
94 const CDCFacet& firstFacet = facetsInCluster.front();
95 auto beginFacetRelationInCluster =
96 std::lower_bound(inputFacetRelations.begin(), inputFacetRelations.end(), &firstFacet);
97
98 const CDCFacet& lastFacet = facetsInCluster.back();
99 auto endFacetRelationInCluster =
100 std::upper_bound(inputFacetRelations.begin(), inputFacetRelations.end(), &lastFacet);
101
102 const int iCluster = firstFacet.getICluster();
103
104 std::vector<WeightedRelation<const CDCFacet>>
105 facetRelationsInCluster(beginFacetRelationInCluster,
106 endFacetRelationInCluster);
107
108 // Apply the cellular automaton in a multipass manner
109 m_facetPaths.clear();
110 m_cellularPathFinder.apply(facetPtrsInCluster, facetRelationsInCluster, m_facetPaths);
111
112 // Helper function to check if a given reverse or alias segment is
113 // also present in the graph of facets. Used in the search for
114 // aliasing segments.
115 auto getFacetPath = [&facetsInCluster,
116 &facetRelationsInCluster,
117 &iCluster](const CDCSegment2D & segment, bool checkRelations = true) {
118 CDCRLWireHitSegment rlWireHitSegment = segment.getRLWireHitSegment();
119 CDCFacetSegment aliasFacetSegment = CDCFacetSegment::create(rlWireHitSegment);
120 std::vector<const CDCFacet*> facetPath;
121 for (CDCRLWireHitTriple& rlWireHitTriple : aliasFacetSegment) {
122 // Do not forget to set the cluster id as it is a sorting criterion
123 rlWireHitTriple.setICluster(iCluster);
124
125 // Check whether the facet is a node in the graph
126 auto itFacet = std::lower_bound(facetsInCluster.begin(), facetsInCluster.end(), rlWireHitTriple);
127 if (itFacet == facetsInCluster.end()) break;
128 if (not(*itFacet == rlWireHitTriple)) break;
129 const CDCFacet* facet = &*itFacet;
130
131 // Check whether there is a relation to this new facet
132 if (not facetPath.empty() and checkRelations) {
133 const CDCFacet* fromFacet = facetPath.back();
134 auto relationsFromFacet = std::equal_range(facetRelationsInCluster.begin(),
135 facetRelationsInCluster.end(),
136 fromFacet);
137 if (std::count_if(relationsFromFacet.first, relationsFromFacet.second, Second() == facet) == 0) break;
138 }
139 facetPath.push_back(facet);
140 }
141 return facetPath;
142 };
143
144 // Reserve enough space to prevent reallocation and invalidated references
145 size_t additionalSpace = m_facetPaths.size();
146 if (m_param_searchReversed) additionalSpace *= 2;
147 if (m_param_searchAlias) additionalSpace *= 2;
148 outputSegments.reserve(outputSegments.size() + additionalSpace);
149
150 for (const std::vector<const CDCFacet*>& facetPath : m_facetPaths) {
151 // If path is only a single facet long - forward all viable orientations if requested
152 if (m_param_allSingleAliases and facetPath.size() == 1) {
153 const CDCFacet& originalSingleFacet = *facetPath.front();
154
155 int nSingleFacets = 0;
156
157 // Helper object to construct other single facet paths
158 std::vector<const CDCFacet*> singleFacetPath;
159 singleFacetPath.reserve(1);
160
161 std::array<int, 3> permIndices{0, 1, 2};
162 CDCRLWireHitTriple rlWireHitTriple = originalSingleFacet;
163
164 for (int iPerm = 0; iPerm < 6; ++iPerm) {
165 setRLWireHit(rlWireHitTriple, permIndices[0], originalSingleFacet.getStartRLWireHit());
166 setRLWireHit(rlWireHitTriple, permIndices[1], originalSingleFacet.getMiddleRLWireHit());
167 setRLWireHit(rlWireHitTriple, permIndices[2], originalSingleFacet.getEndRLWireHit());
168 std::next_permutation(permIndices.begin(), permIndices.end()); // Prepare for next round
169
170 for (ERightLeft startRLInfo : {ERightLeft::c_Left, ERightLeft::c_Right}) {
171 rlWireHitTriple.setStartRLInfo(startRLInfo);
172 for (ERightLeft middleRLInfo : {ERightLeft::c_Left, ERightLeft::c_Right}) {
173 rlWireHitTriple.setMiddleRLInfo(middleRLInfo);
174 for (ERightLeft endRLInfo : {ERightLeft::c_Left, ERightLeft::c_Right}) {
175 rlWireHitTriple.setEndRLInfo(endRLInfo);
176
177 auto itFacet = std::lower_bound(facetsInCluster.begin(),
178 facetsInCluster.end(),
179 rlWireHitTriple);
180
181 if (itFacet == facetsInCluster.end())continue;
182 if (not(*itFacet == rlWireHitTriple)) continue;
183
184 const CDCFacet* singleFacet = &*itFacet;
185 singleFacetPath.clear();
186 singleFacetPath.push_back(singleFacet);
187 outputSegments.push_back(CDCSegment2D::condense(singleFacetPath));
188 outputSegments.back()->setReverseFlag();
189 outputSegments.back()->setAliasFlag();
190 ++nSingleFacets;
191 }
192 }
193 }
194 }
195 B2ASSERT("At least one single facet added", nSingleFacets > 0);
196
197 // Skip the reset of the alias searches
198 continue;
199 }
200
201 outputSegments.reserve(outputSegments.size() + 4);
202 outputSegments.push_back(CDCSegment2D::condense(facetPath));
203 const CDCSegment2D* segment = &outputSegments.back();
204
205
206 // Check for the special situation where the segment is confined to one layer
207 // Relax the alias search a bit to better capture the situation
208 bool checkRelations = true;
210 auto differentILayer = [](const CDCRecoHit2D & lhs, const CDCRecoHit2D & rhs) {
211 return lhs.getWire().getILayer() != rhs.getWire().getILayer();
212 };
213 auto itLayerSwitch = std::adjacent_find(segment->begin(), segment->end(), differentILayer);
214 const bool onlyOneLayer = itLayerSwitch == segment->end();
215 checkRelations = not onlyOneLayer;
216 }
217
218 const CDCSegment2D* reverseSegment = nullptr;
220 std::vector<const CDCFacet*> reverseFacetPath = getFacetPath(segment->reversed(), checkRelations);
221 if (reverseFacetPath.size() == facetPath.size()) {
222 B2DEBUG(25, "Successful constructed REVERSE");
223 outputSegments.push_back(CDCSegment2D::condense(reverseFacetPath));
224 reverseSegment = &outputSegments.back();
225
226 (*segment)->setReverseFlag(true);
227 (*reverseSegment)->setReverseFlag(true);
228 }
229 }
230
231 if (not m_param_searchAlias) continue;
232
233 // Search for aliasing segment in the facet graph
234 int nRLSwitches = segment->getNRLSwitches();
235 if (nRLSwitches > 2) continue; // Segment is stable against aliases
236
237 const CDCSegment2D* aliasSegment = nullptr;
238 std::vector<const CDCFacet*> aliasFacetPath = getFacetPath(segment->getAlias(), checkRelations);
239 if (aliasFacetPath.size() == facetPath.size()) {
240 B2DEBUG(25, "Successful constructed alias");
241 outputSegments.push_back(CDCSegment2D::condense(aliasFacetPath));
242 aliasSegment = &outputSegments.back();
243
244 (*segment)->setAliasFlag(true);
245 (*aliasSegment)->setAliasFlag(true);
246 }
247
248 const CDCSegment2D* reverseAliasSegment = nullptr;
250 std::vector<const CDCFacet*> reverseAliasFacetPath =
251 getFacetPath(segment->reversed().getAlias(), checkRelations);
252 if (reverseAliasFacetPath.size() == facetPath.size()) {
253 B2DEBUG(25, "Successful constructed REVERSE alias");
254 outputSegments.push_back(CDCSegment2D::condense(reverseAliasFacetPath));
255 reverseAliasSegment = &outputSegments.back();
256 if (aliasSegment != nullptr) {
257 (*aliasSegment)->setReverseFlag(true);
258 (*reverseAliasSegment)->setReverseFlag(true);
259 }
260 }
261 }
262
263 if (reverseSegment != nullptr and reverseAliasSegment != nullptr) {
264 (*reverseSegment)->setAliasFlag(true);
265 (*reverseAliasSegment)->setAliasFlag(true);
266 }
267 }
268 }
269}
ILayer getILayer() const
Getter for the layer id within its superlayer Gives the layer id within its superlayer ranging from ...
Definition CDCWire.h:151
The Module parameter list class.
bool m_param_relaxSingleLayerSearch
Parameter : Switch to relax the alias and reverse search for segments contained in a single layer.
bool m_param_allSingleAliases
Paraneter : Switch to activate the write out of all available orientations of single facet segments.
bool m_param_searchReversed
Parameter : Switch to construct the reversed segment if it is available in the facet graph as well.
std::string getDescription() final
Short description of the findlet.
TrackingUtilities::MultipassCellularPathFinder< const TrackingUtilities::CDCFacet > m_cellularPathFinder
Instance of the cellular automaton path finder.
bool m_param_searchAlias
Parameter : Switch to construct the alias segment if it is available in the facet graph as well.
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
std::vector< TrackingUtilities::Path< const TrackingUtilities::CDCFacet > > m_facetPaths
Memory for the facet paths generated from the graph.
void apply(const std::vector< TrackingUtilities::CDCFacet > &inputFacets, const std::vector< TrackingUtilities::WeightedRelation< const TrackingUtilities::CDCFacet > > &inputFacetRelations, std::vector< TrackingUtilities::CDCSegment2D > &outputSegments) final
Main function of the segment finding by the cellular automaton.
A segment consisting of adjacent facets.
static CDCFacetSegment create(const CDCRLWireHitSegment &rlWireHitSegment)
Construct a train of facets from the given oriented wire hits.
Class representing a triple of neighboring oriented wire with additional trajectory information.
Definition CDCFacet.h:32
A segment consisting of two dimensional reconstructed hits.
Class representing a triple of neighboring wire hits.
void setICluster(int iCluster)
Setter for the cluster id.
void setEndRLInfo(const ERightLeft endRLInfo)
Setter for the right left passage information of the third oriented wire hit.
void setEndRLWireHit(const CDCRLWireHit &endRLWireHit)
Setter for the third oriented wire hit.
CDCRLWireHit & getStartRLWireHit()
Getter for the first oriented wire hit.
void setMiddleRLInfo(const ERightLeft middleRLInfo)
Setter for the right left passage information of the second oriented wire hit.
void setMiddleRLWireHit(const CDCRLWireHit &middleRLWireHit)
Setter for the second oriented wire hit.
int getICluster() const
Getter for the cluster id.
CDCRLWireHit & getEndRLWireHit()
Getter for the third oriented wire hit.
CDCRLWireHit & getMiddleRLWireHit()
Getter for the second oriented wire hit.
void setStartRLInfo(const ERightLeft startRLInfo)
Setter for the right left passage information of the first oriented wire hit.
void setStartRLWireHit(const CDCRLWireHit &startRLWireHit)
Setter for the first oriented wire hit.
Class representing an oriented hit wire including a hypotheses whether the causing track passes left ...
Class representing a two dimensional reconstructed hit in the central drift chamber.
const CDC::CDCWire & getWire() const
Getter for the wire the reconstructed hit associated to.
A reconstructed sequence of two dimensional hits in one super layer.
static CDCSegment2D condense(const CDCTangentSegment &tangentSegment)
Averages the reconstructed positions from hits that overlap in adjacent tangents in the given tangent...
Type for two related objects with a weight.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.