Belle II Software prerelease-11-00-00a
CDCCKFStateCreator.h
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#pragma once
9
10#include <tracking/trackingUtilities/findlets/base/Findlet.h>
11
12#include <tracking/trackingUtilities/numerics/EForwardBackward.h>
13#include <tracking/ckf/general/utilities/SearchDirection.h>
14
15#include <tracking/ckf/cdc/entities/CDCCKFState.h>
16#include <tracking/ckf/cdc/entities/CDCCKFPath.h>
17
18#include <cdc/topology/CDCWire.h>
19#include <cdc/topology/CDCWireTopology.h>
20
21#include <tracking/trackingUtilities/utilities/StringManipulation.h>
22#include <framework/core/ModuleParamList.h>
23#include <tracking/trackingUtilities/numerics/Angle.h>
24
25
26namespace Belle2 {
31
34 : public TrackingUtilities::Findlet<CDCCKFState, const CDCCKFState,
35 const TrackingUtilities::CDCWireHit* const > {
36
39
45 double phi;
46 };
47
48
49 public:
50
55 void setMaximalLayerJump(int maximalLayerJump) { m_maximalLayerJump = maximalLayerJump; }
56
61 void setMaximalLayerJumpBackwardSeed(int maximalLayerJumpBackwardSeed)
62 {
63 m_maximalLayerJump_backwardSeed = maximalLayerJumpBackwardSeed;
64 }
65
70 void setMaximalDeltaPhi(double maximalDeltaPhi) { m_maximalDeltaPhi = maximalDeltaPhi; }
71
76 void setHitFindingDirection(const std::string& hitFindingDirection)
77 {
78 m_param_writeOutDirectionAsString = hitFindingDirection;
79 }
80
82 void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) override
83 {
84 moduleParamList->addParameter(TrackingUtilities::prefixed(prefix, "maximalLayerJump"),
85 m_maximalLayerJump, "Maximal jump over N layers", m_maximalLayerJump);
86 moduleParamList->addParameter(TrackingUtilities::prefixed(prefix, "maximalLayerJumpBackwardSeed"),
88 moduleParamList->addParameter(TrackingUtilities::prefixed(prefix, "maximalDeltaPhi"),
89 m_maximalDeltaPhi, "Maximal distance in phi between wires for Z=0 plane", m_maximalDeltaPhi);
90 moduleParamList->addParameter(TrackingUtilities::prefixed(prefix, "hitFindingDirection"),
91 m_param_writeOutDirectionAsString, "Start from innermost/outermost CDC layers", m_param_writeOutDirectionAsString);
92 }
93
95 void beginEvent() override
96 {
98 m_wireHitCache.clear();
99
100 // Determine direction of track building
102
103 if (m_param_writeOutDirection == TrackingUtilities::EForwardBackward::c_Forward) {
104 doForward = true;
105 } else if (m_param_writeOutDirection == TrackingUtilities::EForwardBackward::c_Backward) {
106 doForward = false;
107 } else {
108 B2FATAL("CDCCKFStateCreator: No valid direction specified. Please use forward/backward.");
109 }
110 }
111
113 void apply(std::vector<CDCCKFState>& nextStates, const CDCCKFPath& path,
114 const std::vector<const TrackingUtilities::CDCWireHit*>& wireHits) override
115 {
116 // TODO: as we do not need any information on the current state (track state) of the path, we could in principle
117 // TODO: precalculate everything in here
118
119 // Create cache over wirehits, if empty:
120 if (m_wireHitCache.empty()) {
121 const size_t nHits = wireHits.size();
122 m_wireHitCache.reserve(nHits);
123 for (auto hitPtr : wireHits) {
124 // to speed things up, don't consider background/taken hits at all (and not just in the loop below).
125 // I can't just remove them from the list, otherwise the relation to the wireHits is broken
126 // so set the layer index to a high number.
127 if (hitPtr->getAutomatonCell().hasBackgroundFlag() || hitPtr->getAutomatonCell().hasTakenFlag()) {
128 m_wireHitCache.push_back(CDCCKFWireHitCache{99999, 0.});
129 } else {
130 m_wireHitCache.push_back(CDCCKFWireHitCache{hitPtr->getWire().getICLayer(), hitPtr->getRefPos2D().phi()});
131 }
132 }
133 }
134
135 // Cache last-on-the-path state info too:
136 const auto& lastState = path.back();
137 double lastPhi = 0;
138 double lastICLayer = -1;
139 if (lastState.isSeed()) {
140 if (doForward) {
141 lastICLayer = 0;
142 } else {
143 const auto& wireTopology = CDC::CDCWireTopology::getInstance();
144 const auto& wires = wireTopology.getWires();
145 const float maxForwardZ = wires.back().getForwardZ(); // 157.615
146 const float maxBackwardZ = wires.back().getBackwardZ(); // -72.0916
147
148 const TrackingUtilities::Vector3D seedPos(lastState.getSeed()->getPositionSeed());
149 const float seedPosZ = seedPos.z();
150
151 if (seedPosZ < maxForwardZ && seedPosZ > maxBackwardZ) {
152 lastICLayer = 56;
153 } else {
154 // do straight extrapolation of seed momentum to CDC outer walls
155 TrackingUtilities::Vector3D seedMomZOne(lastState.getSeed()->getMomentumSeed());
156 seedMomZOne = seedMomZOne / seedMomZOne.z();
157 // const float maxZ = seedPosZ > 0 ? maxForwardZ : maxBackwardZ;
158 // const TrackingUtilities::Vector3D extrapolatedPos = seedPos - seedMom / seedMom.norm() * (seedPosZ - maxZ);
159
160 // find closest iCLayer
161 float minDist = 99999;
162 for (const auto& wire : wires) {
163 const float maxZ = seedPosZ > 0 ? wire.getForwardZ() : wire.getBackwardZ();
164 const TrackingUtilities::Vector3D extrapolatedPos = seedPos - seedMomZOne * (seedPosZ - maxZ);
165
166 const auto distance = wire.getDistance(extrapolatedPos);
167 if (distance < minDist) {
168 minDist = distance;
169 lastICLayer = wire.getICLayer();
170 }
171 }
172 B2DEBUG(29, lastICLayer << " (d=" << minDist << ")");
173 }
174 }
175 } else {
176 lastPhi = lastState.getWireHit()->getRefPos2D().phi();
177 lastICLayer = lastState.getWireHit()->getWire().getICLayer();
178 }
179
180 // Get sorted vector of wireHits on the path for faster search
181 std::vector<const TrackingUtilities::CDCWireHit*> wireHitsOnPath;
182 for (auto const& state : path) {
183 if (! state.isSeed()) {
184 wireHitsOnPath.push_back(state.getWireHit());
185 }
186 }
187 std::sort(wireHitsOnPath.begin(), wireHitsOnPath.end());
188
189 size_t nHits = wireHits.size();
190 for (size_t i = 0; i < nHits; i++) {
191 // adjust direction of loop (minimal speed gain)
192 int idx = doForward ? i : nHits - i - 1;
193
194 const auto iCLayer = m_wireHitCache[idx].icLayer; // wireHit->getWire().getICLayer();
195 if (m_param_writeOutDirection == TrackingUtilities::EForwardBackward::c_Backward && lastState.isSeed()) {
196 if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump_backwardSeed) {
197 continue;
198 }
199 } else if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump) {
200 continue;
201 }
202
203 if (! lastState.isSeed()) {
204 double deltaPhi = TrackingUtilities::AngleUtil::normalised(lastPhi - m_wireHitCache[idx].phi);
205 if (fabs(deltaPhi) > m_maximalDeltaPhi) {
206 continue;
207 }
208 }
209
210 const TrackingUtilities::CDCWireHit* wireHit = wireHits[idx];
211
212 if (std::binary_search(wireHitsOnPath.begin(), wireHitsOnPath.end(), wireHit)) {
213 continue;
214 }
215
216 nextStates.emplace_back(wireHit);
217 }
218 }
219
220 private:
226 double m_maximalDeltaPhi = TMath::Pi() / 8;
228 std::string m_param_writeOutDirectionAsString = "forward";
230 TrackingUtilities::EForwardBackward m_param_writeOutDirection = TrackingUtilities::EForwardBackward::c_Unknown;
232 bool doForward = true;
233
235 std::vector<CDCCKFWireHitCache> m_wireHitCache = {};
236
237 };
238
239}
Create CKF states, based on the current path. Perform some basic selection at this stage (based on ph...
TrackingUtilities::Findlet< CDCCKFState, const CDCCKFState, const TrackingUtilities::CDCWireHit *const > Super
Parent class.
TrackingUtilities::EForwardBackward m_param_writeOutDirection
Direction parameter converted from the string parameters.
void setMaximalLayerJump(int maximalLayerJump)
Set maximal layer jump for state creation.
double m_maximalDeltaPhi
Maximal distance in phi between the path last hit/seed and the candidate hit.
int m_maximalLayerJump_backwardSeed
Maximum allowed step over layers (if outside->in CKF) for first step after seed (e....
int m_maximalLayerJump
Maximum allowed step over layers.
bool doForward
Direction parameter converted to boolean for convenience.
std::vector< CDCCKFWireHitCache > m_wireHitCache
Cache to store frequently used information.
void setMaximalDeltaPhi(double maximalDeltaPhi)
Set maximal delta phi for state creation.
void setMaximalLayerJumpBackwardSeed(int maximalLayerJumpBackwardSeed)
Set maximal layer jump for backward seed tracks.
void beginEvent() override
Clear the wireHit cache.
void apply(std::vector< CDCCKFState > &nextStates, const CDCCKFPath &path, const std::vector< const TrackingUtilities::CDCWireHit * > &wireHits) override
Main method of the findlet. Select + create states (output parameter nextStates) suitable for the inp...
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the parameters of the sub findlets.
std::string m_param_writeOutDirectionAsString
Parameter for the direction in which the tracks are built.
void setHitFindingDirection(const std::string &hitFindingDirection)
Set hit finding direction.
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
The Module parameter list class.
Class representing a hit wire in the central drift chamber.
Definition CDCWireHit.h:58
void beginEvent() override
Receive and dispatch signal for the start of a new event.
Interface for a minimal algorithm part that wants to expose some parameters to a module.
Definition Findlet.h:26
A three dimensional vector.
Definition Vector3D.h:34
double z() const
Getter for the z coordinate.
Definition Vector3D.h:497
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
std::vector< CDCCKFState > CDCCKFPath
Shortcut for the collection of CDC CKF-algorithm states.
Definition CDCCKFPath.h:19
TrackingUtilities::EForwardBackward fromString(const std::string &directionString)
Helper function to turn a direction string into a valid forward backward information.
Abstract base class for different kinds of events.
Store basic wire info for faster access.
static double normalised(const double angle)
Normalise an angle to lie in the range from [-pi, pi].
Definition Angle.h:33