Belle II Software  release-05-01-25
AxialTrackCreatorHitHough.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingCDC/findlets/minimal/AxialTrackCreatorHitHough.h>
11 
12 #include <tracking/trackFindingCDC/hough/perigee/AxialLegendreLeafProcessor.h>
13 #include <tracking/trackFindingCDC/hough/perigee/AxialLegendreLeafProcessor.icc.h>
14 #include <tracking/trackFindingCDC/hough/perigee/StandardBinSpec.h>
15 
16 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
17 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
18 
19 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
20 
21 #include <framework/core/ModuleParamList.templateDetails.h>
22 
23 using namespace Belle2;
24 using namespace TrackFindingCDC;
25 
26 #if 0
27 namespace {
28  void saveBounds(std::vector<float> bounds, const std::string& fileName)
29  {
30  std::ofstream boundsFile;
31  boundsFile.open(fileName);
32  for (float bound : bounds) {
33  boundsFile << bound;
34  boundsFile << "\n";
35  }
36  boundsFile.close();
37  }
38 
39  std::vector<float> loadBounds(const std::string& fileName)
40  {
41  std::vector<float> bounds;
42  std::ifstream boundsFile;
43  std::string boundLine;
44  boundsFile.open(fileName);
45  if (boundsFile.is_open()) {
46  while (std::getline(boundsFile, boundLine)) {
47  float bound = stof(boundLine);
48  bounds.push_back(bound);
49  }
50  boundsFile.close();
51  } else {
52  B2ERROR("Could not read bounds file");
53  }
54  return bounds;
55  }
56 }
57 #endif
58 
60 {
61  return "Generates axial tracks from hits using several increasingly relaxed hough space search over phi0 and curvature.";
62 }
63 
65  const std::string& prefix)
66 {
67  // Parameters for the hough space
68  moduleParamList->addParameter(prefixed(prefix, "granularityLevel"),
70  "Level of divisions in the hough space.",
72 
73  moduleParamList->addParameter(prefixed(prefix, "sectorLevelSkip"),
75  "Number of levels to be skipped in the hough "
76  "space on the first level to form sectors",
78 
79  moduleParamList->addParameter(prefixed(prefix, "curvBounds"),
81  "Curvature bounds of the hough space. Either 2 or all discrete bounds",
83 
84  moduleParamList->addParameter(prefixed(prefix, "discretePhi0Width"),
86  "Width of the phi0 bins at the lowest level of the hough space.",
88 
89  moduleParamList->addParameter(prefixed(prefix, "discretePhi0Overlap"),
91  "Overlap of the phi0 bins at the lowest level of the hough space.",
93 
94  moduleParamList->addParameter(prefixed(prefix, "discreteCurvWidth"),
96  "Width of the curvature bins at the lowest level of the hough space.",
98 
99  moduleParamList->addParameter(prefixed(prefix, "discreteCurvOverlap"),
101  "Overlap of the curvature bins at the lowest level of the hough space.",
103 
104  // Relaxation schedule
105  moduleParamList->addParameter(prefixed(prefix, "relaxationSchedule"),
107  "Relaxation schedule for the leaf processor in the hough tree. "
108  "For content of the individual parameter maps consider the parameters of the "
109  "AxialLegendreLeafProcessor",
111 
112 }
113 
115 {
117 
118  // Construct the hough space
119  const long nPhi0Bins = std::pow(c_phi0Divisions, m_param_granularityLevel);
120  const Phi0BinsSpec phi0BinsSpec(nPhi0Bins,
123 
124  if (m_param_curvBounds.size() == 2) {
125  // If parameters are unchanged use the legendre default binning
126  if (m_param_discreteCurvOverlap == -1) {
128  std::array<float, 2> curvSpan({m_param_curvBounds[0], m_param_curvBounds[1]});
130  } else {
131  std::array<double, 2> curvBounds{{m_param_curvBounds.front(), m_param_curvBounds.back()}};
132  const long nCurvBins = std::pow(c_curvDivisions, m_param_granularityLevel);
133  const CurvBinsSpec curvBinsSpec(curvBounds.front(),
134  curvBounds.back(),
135  nCurvBins,
138  m_param_curvBounds = curvBinsSpec.constructArray();
139  }
140  }
141 
142  // Construct hough tree
144  m_houghTree = std::make_unique<SimpleRLTaggedWireHitPhi0CurvHough>(maxTreeLevel, m_curlCurv);
145  m_houghTree->setSectorLevelSkip(m_param_sectorLevelSkip);
146  m_houghTree->assignArray<DiscretePhi0>(phi0BinsSpec.constructArray(), phi0BinsSpec.getNOverlap());
148  m_houghTree->initialize();
149 }
150 
151 void AxialTrackCreatorHitHough::apply(const std::vector<const CDCWireHit*>& axialWireHits,
152  std::vector<CDCTrack>& tracks)
153 {
154  // Reset the mask flag and select only the untaken hits
155  std::vector<const CDCWireHit*> unusedAxialWireHits;
156  for (const CDCWireHit* wireHit : axialWireHits) {
157  (*wireHit)->setMaskedFlag(false);
158  if ((*wireHit)->hasTakenFlag()) continue;
159  unusedAxialWireHits.push_back(wireHit);
160  }
161 
162  // Setup the level processor and obtain its parameter list to be set.
163  using Node = typename SimpleRLTaggedWireHitPhi0CurvHough::Node;
165  AxialLegendreLeafProcessor<Node> leafProcessor(maxTreeLevel);
166  leafProcessor.setAxialWireHits(axialWireHits);
167  ModuleParamList moduleParamList;
168  const std::string prefix = "";
169  leafProcessor.exposeParameters(&moduleParamList, prefix);
170 
171  // Find tracks with increasingly relaxed conditions in the hough grid
172  m_houghTree->seed(std::move(unusedAxialWireHits));
173  for (const ParameterVariantMap& passParameters : m_param_relaxationSchedule) {
174  AssignParameterVisitor::update(&moduleParamList, passParameters);
175  leafProcessor.beginWalk();
176  m_houghTree->findUsing(leafProcessor);
177  }
178  m_houghTree->fell();
179 
180  // Write out tracks as return value
181  const std::vector<CDCTrack>& foundTracks = leafProcessor.getTracks();
182  tracks.insert(tracks.end(), foundTracks.begin(), foundTracks.end());
183 }
184 
186 {
187  m_houghTree->raze();
188  m_houghTree.reset();
190 }
191 
192 std::vector<float> AxialTrackCreatorHitHough::getDefaultCurvBounds(std::array<float, 2> curvSpan, int granularityLevel)
193 {
194  using BinSpan = std::array<float, 2>;
195  using BinSpans = std::vector<BinSpan>;
196  std::vector<BinSpans> binSpansByLevel(granularityLevel + 1);
197  binSpansByLevel[0].push_back(BinSpan({curvSpan[0], curvSpan[1]}));
198 
199  for (int level = 1; level <= granularityLevel; ++level) {
200  for (const BinSpan& binSpan : binSpansByLevel[level - 1]) {
201  const float subBinWidth = std::fabs(binSpan[1] - binSpan[0]) / 2;
202  const float middle = binSpan[0] + (binSpan[1] - binSpan[0]) / 2.0;
203 
204  // Expaning bins somewhat to have a overlap
205  // Assuming granularity level = 12
206  // For level 6 to 7 only expand 1 / 4, for higher levels expand 1 / 8.
207  // Never expand for curvatures lower than 0.005
208  // (copied from the legendre method. Works well, but some experimentation
209  // needs to be made to know why)
210  const float extension = [&]() {
211  if ((level + 7 <= granularityLevel)
212  // or (std::fabs(middle) <= 0.007)
213  or (std::fabs(middle) <= 0.005)) {
214  return 0.0;
215  } else if (level + 5 < granularityLevel) {
216  return subBinWidth / 4.0;
217  } else {
218  return subBinWidth / 8.0;
219  }
220  }();
221 
222  const float lower1 = binSpan[0] - extension;
223  const float upper1 = middle + extension;
224 
225  const float lower2 = middle - extension;
226  const float upper2 = binSpan[1] + extension;
227 
228  binSpansByLevel[level].push_back({lower1, upper1});
229  binSpansByLevel[level].push_back({lower2, upper2});
230  }
231  }
232 
233  // Return highest level as prepared bin bounds.
234  std::vector<float> result;
235 
236  for (BinSpan& binSpan : binSpansByLevel[granularityLevel]) {
237  result.push_back(binSpan[0]);
238  result.push_back(binSpan[1]);
239  }
240  return result;
241 }
Belle2::TrackFindingCDC::Phi0BinsSpec::getNOverlap
int getNOverlap() const
Getter for the overlap in discrete number of positions.
Definition: Phi0Rep.h:57
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
Definition: AxialTrackCreatorHitHough.cc:64
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::initialize
void initialize() final
Initialize the findlet before event processing.
Definition: AxialTrackCreatorHitHough.cc:114
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_curlCurv
const double m_curlCurv
Curvature below which particles from IP do not leave the CDC.
Definition: AxialTrackCreatorHitHough.h:70
Belle2::TrackFindingCDC::DiscreteValue
Representation for a discrete position in an array of discrete positions.
Definition: DiscreteValue.h:33
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_granularityLevel
int m_param_granularityLevel
Parameter: Level of divisions in the hough space.
Definition: AxialTrackCreatorHitHough.h:75
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_sectorLevelSkip
int m_param_sectorLevelSkip
Parameter: Number of levels to be skipped in the hough space on the first level to form sectors.
Definition: AxialTrackCreatorHitHough.h:78
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::getDefaultCurvBounds
static std::vector< float > getDefaultCurvBounds(std::array< float, 2 > curvSpan, int granularityLevel)
Get a series of parameters to be set for each pass over the rough hough space.
Definition: AxialTrackCreatorHitHough.cc:192
Belle2::TrackFindingCDC::AxialLegendreLeafProcessor::getTracks
const std::vector< CDCTrack > & getTracks() const
Getter for the tracks.
Definition: AxialLegendreLeafProcessor.h:123
Belle2::TrackFindingCDC::SimpleHitBasedHoughTree::Node
typename Super::Node Node
Type of the node in the hough tree.
Definition: SimpleHitBasedHoughTree.h:50
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::initialize
void initialize() override
Receive and dispatch signal before the start of the event processing.
Definition: CompositeProcessingSignalListener.cc:17
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::c_phi0Divisions
static const int c_phi0Divisions
Fixed parameter: Number of divisions in the phi0 direction.
Definition: AxialTrackCreatorHitHough.h:99
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_relaxationSchedule
std::vector< ParameterVariantMap > m_param_relaxationSchedule
Parameter: Relaxation schedule for the leaf processor in the hough tree.
Definition: AxialTrackCreatorHitHough.h:96
Belle2::TrackFindingCDC::AssignParameterVisitor::update
static void update(ModuleParamList *moduleParamList, const std::map< std::string, boost::variant< T... > > &valuesByName)
Transfer all the parameters from the map boost:variant values to the module parmeter list.
Definition: ParameterVariant.h:49
Belle2::TrackFindingCDC::CurvBinsSpec::constructArray
DiscreteCurv::Array constructArray() const
Constuct the array of discrete curv positions.
Definition: CurvRep.h:50
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::TrackFindingCDC::AxialLegendreLeafProcessor
Predicate class that is feed the nodes in a WeightedHoughTree walk It decides if a node should be fur...
Definition: AxialLegendreLeafProcessor.h:48
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Phi0BinsSpec::constructArray
DiscretePhi0::Array constructArray() const
Constuct the array of discrete phi0 positions.
Definition: Phi0Rep.cc:25
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_discreteCurvOverlap
int m_param_discreteCurvOverlap
Parameter: Overlap of the curvature bins at the highest level of the hough space.
Definition: AxialTrackCreatorHitHough.h:93
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::terminate
void terminate() final
Cleanup the findlet after event processing.
Definition: AxialTrackCreatorHitHough.cc:185
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::terminate
void terminate() override
Receive and dispatch Signal for termination of the event processing.
Definition: CompositeProcessingSignalListener.cc:49
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_discreteCurvWidth
int m_param_discreteCurvWidth
Parameter: Width of the curvature bins at the highest level of the hough space.
Definition: AxialTrackCreatorHitHough.h:90
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::c_curvDivisions
static const int c_curvDivisions
Fixed parameter: Number of divisions in the curv direction.
Definition: AxialTrackCreatorHitHough.h:102
Belle2::TrackFindingCDC::AxialLegendreLeafProcessor::setAxialWireHits
void setAxialWireHits(std::vector< const CDCWireHit * > axialWireHits)
Set the pool of all axial wire hits to be used in the postprocessing.
Definition: AxialLegendreLeafProcessor.h:136
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_curvBounds
std::vector< float > m_param_curvBounds
Parameter: hough bounds.
Definition: AxialTrackCreatorHitHough.h:81
Belle2::TrackFindingCDC::CurvBinsSpec
Strategy to construct discrete curv points from discrete overlap specifications.
Definition: CurvRep.h:33
Belle2::TrackFindingCDC::AxialLegendreLeafProcessor::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Expose the parameters as a module parameter list.
Definition: AxialLegendreLeafProcessor.icc.h:194
Belle2::TrackFindingCDC::AxialLegendreLeafProcessor::beginWalk
void beginWalk()
Function to notify the leaf processor about changes in parameters before a new walk.
Definition: AxialLegendreLeafProcessor.icc.h:235
Belle2::TrackFindingCDC::CDCWireHit
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:65
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_discretePhi0Width
int m_param_discretePhi0Width
Parameter: Width of the phi0 bins at the highest level of the hough space.
Definition: AxialTrackCreatorHitHough.h:84
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::apply
void apply(const std::vector< const CDCWireHit * > &axialWireHits, std::vector< CDCTrack > &tracks) final
Generates the tracks from the given segments into the output argument.
Definition: AxialTrackCreatorHitHough.cc:151
Belle2::TrackFindingCDC::Phi0BinsSpec
Strategy to construct discrete phi0 points from discrete overlap specifications.
Definition: Phi0Rep.h:30
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_param_discretePhi0Overlap
int m_param_discretePhi0Overlap
Parameter: Overlap of the phi0 bins at the highest level of the hough space.
Definition: AxialTrackCreatorHitHough.h:87
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::m_houghTree
std::unique_ptr< SimpleRLTaggedWireHitPhi0CurvHough > m_houghTree
The hough space tree search.
Definition: AxialTrackCreatorHitHough.h:110
Belle2::TrackFindingCDC::AxialTrackCreatorHitHough::getDescription
std::string getDescription() final
Short description of the findlet.
Definition: AxialTrackCreatorHitHough.cc:59