Belle II Software  release-08-01-10
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/trackFindingCDC/findlets/base/Findlet.h>
11 
12 #include <tracking/trackFindingCDC/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 <tracking/trackFindingCDC/topology/CDCWire.h>
19 #include <tracking/trackFindingCDC/topology/CDCWireTopology.h>
20 
21 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
22 #include <framework/core/ModuleParamList.h>
23 #include <tracking/trackFindingCDC/numerics/Angle.h>
24 
25 
26 namespace Belle2 {
34  : public TrackFindingCDC::Findlet<CDCCKFState, const CDCCKFState,
35  const TrackFindingCDC::CDCWireHit* const > {
36 
39 
43  int icLayer;
45  double phi;
46  };
47 
48 
49  public:
50 
52  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) override
53  {
54  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalLayerJump"),
55  m_maximalLayerJump, "Maximal jump over N layers", m_maximalLayerJump);
56  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalLayerJumpBackwardSeed"),
58  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalDeltaPhi"),
59  m_maximalDeltaPhi, "Maximal distance in phi between wires for Z=0 plane", m_maximalDeltaPhi);
60  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "hitFindingDirection"),
61  m_param_writeOutDirectionAsString, "Start from innermost/outermost CDC layers", m_param_writeOutDirectionAsString);
62  }
63 
65  void beginEvent() override
66  {
68  m_wireHitCache.clear();
69 
70  // Determine direction of track building
72 
73  if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Forward) {
74  doForward = true;
75  } else if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Backward) {
76  doForward = false;
77  } else {
78  B2FATAL("CDCCKFStateCreator: No valid direction specified. Please use forward/backward.");
79  }
80  }
81 
83  void apply(std::vector<CDCCKFState>& nextStates, const CDCCKFPath& path,
84  const std::vector<const TrackFindingCDC::CDCWireHit*>& wireHits) override
85  {
86  // TODO: as we do not need any information on the current state (track state) of the path, we could in principle
87  // TODO: precalculate everything in here
88 
89  // Create cache over wirehits, if empty:
90  if (m_wireHitCache.empty()) {
91  const size_t nHits = wireHits.size();
92  m_wireHitCache.reserve(nHits);
93  for (auto hitPtr : wireHits) {
94  // to speed things up, don't consider background/taken hits at all (and not just in the loop below).
95  // I can't just remove them from the list, otherwise the relation to the wireHits is broken
96  // so set the layer index to a high number.
97  if (hitPtr->getAutomatonCell().hasBackgroundFlag() || hitPtr->getAutomatonCell().hasTakenFlag()) {
98  m_wireHitCache.push_back(CDCCKFWireHitCache{99999, 0.});
99  } else {
100  m_wireHitCache.push_back(CDCCKFWireHitCache{hitPtr->getWire().getICLayer(), hitPtr->getRefPos2D().phi()});
101  }
102  }
103  }
104 
105  // Cache last-on-the-path state info too:
106  const auto& lastState = path.back();
107  double lastPhi = 0;
108  double lastICLayer = -1;
109  if (lastState.isSeed()) {
110  if (doForward) {
111  lastICLayer = 0;
112  } else {
113  const auto& wireTopology = TrackFindingCDC::CDCWireTopology::getInstance();
114  const auto& wires = wireTopology.getWires();
115  const float maxForwardZ = wires.back().getForwardZ(); // 157.615
116  const float maxBackwardZ = wires.back().getBackwardZ(); // -72.0916
117 
118  const TrackFindingCDC::Vector3D seedPos(lastState.getSeed()->getPositionSeed());
119  const float seedPosZ = seedPos.z();
120 
121  if (seedPosZ < maxForwardZ && seedPosZ > maxBackwardZ) {
122  lastICLayer = 56;
123  } else {
124  // do straight extrapolation of seed momentum to CDC outer walls
125  TrackFindingCDC::Vector3D seedMomZOne(lastState.getSeed()->getMomentumSeed());
126  seedMomZOne = seedMomZOne / seedMomZOne.z();
127  // const float maxZ = seedPosZ > 0 ? maxForwardZ : maxBackwardZ;
128  // const TrackFindingCDC::Vector3D extrapolatedPos = seedPos - seedMom / seedMom.norm() * (seedPosZ - maxZ);
129 
130  // find closest iCLayer
131  float minDist = 99999;
132  for (const auto& wire : wires) {
133  const float maxZ = seedPosZ > 0 ? wire.getForwardZ() : wire.getBackwardZ();
134  const TrackFindingCDC::Vector3D extrapolatedPos = seedPos - seedMomZOne * (seedPosZ - maxZ);
135 
136  const auto distance = wire.getDistance(extrapolatedPos);
137  if (distance < minDist) {
138  minDist = distance;
139  lastICLayer = wire.getICLayer();
140  }
141  }
142  B2DEBUG(29, lastICLayer << " (d=" << minDist << ")");
143  }
144  }
145  } else {
146  lastPhi = lastState.getWireHit()->getRefPos2D().phi();
147  lastICLayer = lastState.getWireHit()->getWire().getICLayer();
148  }
149 
150  // Get sorted vector of wireHits on the path for faster search
151  std::vector<const TrackFindingCDC::CDCWireHit*> wireHitsOnPath;
152  for (auto const& state : path) {
153  if (! state.isSeed()) {
154  wireHitsOnPath.push_back(state.getWireHit());
155  }
156  }
157  std::sort(wireHitsOnPath.begin(), wireHitsOnPath.end());
158 
159  size_t nHits = wireHits.size();
160  for (size_t i = 0; i < nHits; i++) {
161  // adjust direction of loop (minimal speed gain)
162  int idx = doForward ? i : nHits - i - 1;
163 
164  const auto iCLayer = m_wireHitCache[idx].icLayer; // wireHit->getWire().getICLayer();
165  if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Backward && lastState.isSeed()) {
166  if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump_backwardSeed) {
167  continue;
168  }
169  } else if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump) {
170  continue;
171  }
172 
173  if (! lastState.isSeed()) {
174  double deltaPhi = TrackFindingCDC::AngleUtil::normalised(lastPhi - m_wireHitCache[idx].phi);
175  if (fabs(deltaPhi) > m_maximalDeltaPhi) {
176  continue;
177  }
178  }
179 
180  const TrackFindingCDC::CDCWireHit* wireHit = wireHits[idx];
181 
182  if (std::binary_search(wireHitsOnPath.begin(), wireHitsOnPath.end(), wireHit)) {
183  continue;
184  }
185 
186  nextStates.emplace_back(wireHit);
187  }
188  }
189 
190  private:
196  double m_maximalDeltaPhi = TMath::Pi() / 8;
198  std::string m_param_writeOutDirectionAsString = "forward";
200  TrackFindingCDC::EForwardBackward m_param_writeOutDirection = TrackFindingCDC::EForwardBackward::c_Unknown;
202  bool doForward = true;
203 
205  std::vector<CDCCKFWireHitCache> m_wireHitCache = {};
206 
207  };
209 }
Create CKF states, based on the current path. Perform some basic selection at this stage (based on ph...
void apply(std::vector< CDCCKFState > &nextStates, const CDCCKFPath &path, const std::vector< const TrackFindingCDC::CDCWireHit * > &wireHits) override
Main method of the findlet. Select + create states (output parameter nextStates) suitable for the inp...
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 beginEvent() override
Clear the wireHit cache.
TrackFindingCDC::EForwardBackward m_param_writeOutDirection
Direction parameter converted from the string parameters.
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.
The Module parameter list class.
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:55
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
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:33
double z() const
Getter for the z coordinate.
Definition: Vector3D.h:496
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
TrackFindingCDC::EForwardBackward fromString(const std::string &directionString)
Helper function to turn a direction string into a valid forward backward information.
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage 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