Belle II Software  release-05-02-19
CDCCKFStateCreator.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun, Simon Kurz *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/findlets/base/Findlet.h>
13 
14 #include <tracking/trackFindingCDC/numerics/EForwardBackward.h>
15 #include <tracking/ckf/general/utilities/SearchDirection.h>
16 
17 #include <tracking/ckf/cdc/entities/CDCCKFState.h>
18 #include <tracking/ckf/cdc/entities/CDCCKFPath.h>
19 
20 #include <tracking/trackFindingCDC/topology/CDCWire.h>
21 #include <tracking/trackFindingCDC/topology/CDCWireTopology.h>
22 
23 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
24 #include <framework/core/ModuleParamList.h>
25 #include <tracking/trackFindingCDC/numerics/Angle.h>
26 
27 
28 namespace Belle2 {
34  class CDCCKFStateCreator
36  : public TrackFindingCDC::Findlet<CDCCKFState, const CDCCKFState,
37  const TrackFindingCDC::CDCWireHit* const > {
38 
40  using Super = TrackFindingCDC::Findlet<CDCCKFState, const CDCCKFState, const TrackFindingCDC::CDCWireHit* const>;
41 
45  int icLayer;
47  double phi;
48  };
49 
50 
51  public:
52 
54  void exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix) override
55  {
56  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalLayerJump"),
57  m_maximalLayerJump, "Maximal jump over N layers", m_maximalLayerJump);
58  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalLayerJumpBackwardSeed"),
60  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalDeltaPhi"),
61  m_maximalDeltaPhi, "Maximal distance in phi between wires for Z=0 plane", m_maximalDeltaPhi);
62  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "hitFindingDirection"),
63  m_param_writeOutDirectionAsString, "Start from innermost/outermost CDC layers", m_param_writeOutDirectionAsString);
64  }
65 
67  void beginEvent() override
68  {
70  m_wireHitCache.clear();
71 
72  // Determine direction of track building
74 
75  if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Forward) {
76  doForward = true;
77  } else if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Backward) {
78  doForward = false;
79  } else {
80  B2FATAL("CDCCKFStateCreator: No valid direction specified. Please use forward/backward.");
81  }
82  }
83 
85  void apply(std::vector<CDCCKFState>& nextStates, const CDCCKFPath& path,
86  const std::vector<const TrackFindingCDC::CDCWireHit*>& wireHits) override
87  {
88  // TODO: as we do not need any information on the current state (track state) of the path, we could in principle
89  // TODO: precalculate everything in here
90 
91  // Create cache over wirehits, if empty:
92  if (m_wireHitCache.empty()) {
93  const size_t nHits = wireHits.size();
94  m_wireHitCache.reserve(nHits);
95  for (auto hitPtr : wireHits) {
96  // to speed things up, don't consider background/taken hits at all (and not just in the loop below).
97  // I can't just remove them from the list, otherwise the relation to the wireHits is broken
98  // so set the layer index to a high number.
99  if (hitPtr->getAutomatonCell().hasBackgroundFlag() || hitPtr->getAutomatonCell().hasTakenFlag()) {
100  m_wireHitCache.push_back(CDCCKFWireHitCache{99999, 0.});
101  } else {
102  m_wireHitCache.push_back(CDCCKFWireHitCache{hitPtr->getWire().getICLayer(), hitPtr->getRefPos2D().phi()});
103  }
104  }
105  }
106 
107  // Cache last-on-the-path state info too:
108  const auto& lastState = path.back();
109  double lastPhi = 0;
110  double lastICLayer = -1;
111  if (lastState.isSeed()) {
112  if (doForward) {
113  lastICLayer = 0;
114  } else {
115  const auto& wireTopology = TrackFindingCDC::CDCWireTopology::getInstance();
116  const auto& wires = wireTopology.getWires();
117  const float maxForwardZ = wires.back().getForwardZ(); // 157.615
118  const float maxBackwardZ = wires.back().getBackwardZ(); // -72.0916
119 
120  const TrackFindingCDC::Vector3D seedPos(lastState.getSeed()->getPositionSeed());
121  const float seedPosZ = seedPos.z();
122 
123  if (seedPosZ < maxForwardZ && seedPosZ > maxBackwardZ) {
124  lastICLayer = 56;
125  } else {
126  // do straight extrapolation of seed momentum to CDC outer walls
127  TrackFindingCDC::Vector3D seedMomZOne(lastState.getSeed()->getMomentumSeed());
128  seedMomZOne = seedMomZOne / seedMomZOne.z();
129  // const float maxZ = seedPosZ > 0 ? maxForwardZ : maxBackwardZ;
130  // const TrackFindingCDC::Vector3D extrapolatedPos = seedPos - seedMom / seedMom.norm() * (seedPosZ - maxZ);
131 
132  // find closest iCLayer
133  float minDist = 99999;
134  for (const auto& wire : wires) {
135  const float maxZ = seedPosZ > 0 ? wire.getForwardZ() : wire.getBackwardZ();
136  const TrackFindingCDC::Vector3D extrapolatedPos = seedPos - seedMomZOne * (seedPosZ - maxZ);
137 
138  const auto distance = wire.getDistance(extrapolatedPos);
139  if (distance < minDist) {
140  minDist = distance;
141  lastICLayer = wire.getICLayer();
142  }
143  }
144  B2DEBUG(100, lastICLayer << " (d=" << minDist << ")");
145  }
146  }
147  } else {
148  lastPhi = lastState.getWireHit()->getRefPos2D().phi();
149  lastICLayer = lastState.getWireHit()->getWire().getICLayer();
150  }
151 
152  // Get sorted vector of wireHits on the path for faster search
153  std::vector<const TrackFindingCDC::CDCWireHit*> wireHitsOnPath;
154  for (auto const& state : path) {
155  if (! state.isSeed()) {
156  wireHitsOnPath.push_back(state.getWireHit());
157  }
158  }
159  std::sort(wireHitsOnPath.begin(), wireHitsOnPath.end());
160 
161  size_t nHits = wireHits.size();
162  for (size_t i = 0; i < nHits; i++) {
163  // adjust direction of loop (minimal speed gain)
164  int idx = doForward ? i : nHits - i - 1;
165 
166  const auto iCLayer = m_wireHitCache[idx].icLayer; // wireHit->getWire().getICLayer();
167  if (m_param_writeOutDirection == TrackFindingCDC::EForwardBackward::c_Backward && lastState.isSeed()) {
168  if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump_backwardSeed) {
169  continue;
170  }
171  } else if (std::abs(lastICLayer - iCLayer) > m_maximalLayerJump) {
172  continue;
173  }
174 
175  if (! lastState.isSeed()) {
176  double deltaPhi = TrackFindingCDC::AngleUtil::normalised(lastPhi - m_wireHitCache[idx].phi);
177  if (fabs(deltaPhi) > m_maximalDeltaPhi) {
178  continue;
179  }
180  }
181 
182  const TrackFindingCDC::CDCWireHit* wireHit = wireHits[idx];
183 
184  if (std::binary_search(wireHitsOnPath.begin(), wireHitsOnPath.end(), wireHit)) {
185  continue;
186  }
187 
188  nextStates.emplace_back(wireHit);
189  }
190  }
191 
192  private:
194  int m_maximalLayerJump = 2;
198  double m_maximalDeltaPhi = TMath::Pi() / 8;
200  std::string m_param_writeOutDirectionAsString = "forward";
202  TrackFindingCDC::EForwardBackward m_param_writeOutDirection = TrackFindingCDC::EForwardBackward::c_Unknown;
204  bool doForward = true;
205 
207  std::vector<CDCCKFWireHitCache> m_wireHitCache = {};
208 
209  };
211 }
Belle2::CDCCKFStateCreator::m_param_writeOutDirectionAsString
std::string m_param_writeOutDirectionAsString
Parameter for the direction in which the tracks are built.
Definition: CDCCKFStateCreator.h:208
Belle2::fromString
TrackFindingCDC::EForwardBackward fromString(const std::string &directionString)
Helper function to turn a direction string into a valid forward backward information.
Definition: SearchDirection.h:46
Belle2::CDCCKFStateCreator::beginEvent
void beginEvent() override
Clear the wireHit cache.
Definition: CDCCKFStateCreator.h:75
Belle2::Vector3D
HepGeom::Vector3D< double > Vector3D
3D Vector
Definition: Cell.h:35
Belle2::CDCCKFStateCreator::m_maximalLayerJump_backwardSeed
int m_maximalLayerJump_backwardSeed
Maximum allowed step over layers (if outside->in CKF) for first step after seed (e....
Definition: CDCCKFStateCreator.h:204
Belle2::TrackFindingCDC::CDCWireTopology::getInstance
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
Definition: CDCWireTopology.cc:22
Belle2::CDCCKFStateCreator::m_param_writeOutDirection
TrackFindingCDC::EForwardBackward m_param_writeOutDirection
Direction parameter converted from the string parameters.
Definition: CDCCKFStateCreator.h:210
Belle2::CDCCKFStateCreator::doForward
bool doForward
Direction parameter converted to boolean for convenience.
Definition: CDCCKFStateCreator.h:212
Belle2::TrackFindingCDC::NForwardBackward::EForwardBackward
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage information.
Definition: EForwardBackward.h:35
Belle2::CDCCKFStateCreator::CDCCKFWireHitCache::phi
double phi
azimuthal coordinate
Definition: CDCCKFStateCreator.h:55
Belle2::CDCCKFStateCreator::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) override
Expose the parameters of the sub findlets.
Definition: CDCCKFStateCreator.h:62
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2::CDCCKFStateCreator::CDCCKFWireHitCache
Store basic wire info for faster access.
Definition: CDCCKFStateCreator.h:51
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CDCCKFStateCreator::Super
TrackFindingCDC::Findlet< CDCCKFState, const CDCCKFState, const TrackFindingCDC::CDCWireHit *const > Super
Parent class.
Definition: CDCCKFStateCreator.h:48
Belle2::CDCCKFStateCreator::m_maximalDeltaPhi
double m_maximalDeltaPhi
Maximal distance in phi between the path last hit/seed and the candidate hit.
Definition: CDCCKFStateCreator.h:206
Belle2::CDCCKFPath
std::vector< CDCCKFState > CDCCKFPath
Shortcut for the collection of CDC CKF-algorithm states.
Definition: CDCCKFPath.h:29
Belle2::CDCCKFStateCreator::m_maximalLayerJump
int m_maximalLayerJump
Maximum allowed step over layers.
Definition: CDCCKFStateCreator.h:202
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::beginEvent
void beginEvent() override
Receive and dispatch signal for the start of a new event.
Definition: CompositeProcessingSignalListener.cc:33
Belle2::CDCCKFStateCreator::apply
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...
Definition: CDCCKFStateCreator.h:93
Belle2::CDCCKFStateCreator::m_wireHitCache
std::vector< CDCCKFWireHitCache > m_wireHitCache
Cache to store frequently used information.
Definition: CDCCKFStateCreator.h:215
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::AngleUtil::normalised
static double normalised(const double angle)
Normalise an angle to lie in the range from [-pi, pi].
Definition: Angle.h:43
Belle2::CDCCKFStateCreator::CDCCKFWireHitCache::icLayer
int icLayer
layer index
Definition: CDCCKFStateCreator.h:53