CMS 3D CMS Logo

CTPPSTimingTrackRecognition.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * This is a part of CTPPS offline software.
4  * Authors:
5  * Laurent Forthomme (laurent.forthomme@cern.ch)
6  * Nicola Minafra (nicola.minafra@cern.ch)
7  * Mateusz Szpyrka (mateusz.szpyrka@cern.ch)
8  *
9  ****************************************************************************/
10 
11 #ifndef RecoPPS_Local_CTPPSTimingTrackRecognition
12 #define RecoPPS_Local_CTPPSTimingTrackRecognition
13 
16 
18 
20 
21 #include <vector>
22 #include <unordered_map>
23 #include <algorithm>
24 #include <cmath>
25 
30 template <typename TRACK_TYPE, typename HIT_TYPE>
32 public:
34  : threshold_(iConfig.getParameter<double>("threshold")),
35  thresholdFromMaximum_(iConfig.getParameter<double>("thresholdFromMaximum")),
36  resolution_(iConfig.getParameter<double>("resolution")),
37  sigma_(iConfig.getParameter<double>("sigma")),
38  tolerance_(iConfig.getParameter<double>("tolerance")),
39  pixelEfficiencyFunction_(iConfig.getParameter<std::string>("pixelEfficiencyFunction")) {
41  throw cms::Exception("CTPPSTimingTrackRecognition")
42  << "Invalid number of parameters to the pixel efficiency function! "
44  }
45  virtual ~CTPPSTimingTrackRecognition() = default;
46 
47  //--- class API
48 
50  inline virtual void clear() { hitVectorMap_.clear(); }
52  virtual void addHit(const HIT_TYPE& recHit) = 0;
55 
56 protected:
57  // Algorithm parameters:
58  const float threshold_;
59  const float thresholdFromMaximum_;
60  const float resolution_;
61  const float sigma_;
62  const float tolerance_;
64 
65  typedef std::vector<TRACK_TYPE> TrackVector;
66  typedef std::vector<HIT_TYPE> HitVector;
67  typedef std::unordered_map<int, HitVector> HitVectorMap;
68 
71 
76  };
78  struct SpatialRange {
79  float xBegin, xEnd;
80  float yBegin, yEnd;
81  float zBegin, zEnd;
82  };
83 
94  const DimensionParameters& param,
95  float (*getHitCenter)(const HIT_TYPE&),
96  float (*getHitRangeWidth)(const HIT_TYPE&),
97  void (*setTrackCenter)(TRACK_TYPE&, float),
98  void (*setTrackSigma)(TRACK_TYPE&, float),
100 
111  bool timeEval(const HitVector& hits, float& meanTime, float& timeSigma) const;
112 };
113 
114 /****************************************************************************
115  * Implementation
116  ****************************************************************************/
117 
118 template <class TRACK_TYPE, class HIT_TYPE>
120  const HitVector& hits,
121  const DimensionParameters& param,
122  float (*getHitCenter)(const HIT_TYPE&),
123  float (*getHitRangeWidth)(const HIT_TYPE&),
124  void (*setTrackCenter)(TRACK_TYPE&, float),
125  void (*setTrackSigma)(TRACK_TYPE&, float),
126  TrackVector& result) {
127  const float invResolution = 1. / resolution_;
128  const float profileRangeMargin = sigma_ * 3.;
129  const float profileRangeBegin = param.rangeBegin - profileRangeMargin;
130  const float profileRangeEnd = param.rangeEnd + profileRangeMargin;
131 
132  std::vector<float> hitProfile((profileRangeEnd - profileRangeBegin) * invResolution + 1, 0.);
133  // extra component to make sure that the profile drops below the threshold at range's end
134  *hitProfile.rbegin() = -1.f;
135 
136  // Creates hit profile
137  for (auto const& hit : hits) {
138  const float center = getHitCenter(hit), rangeWidth = getHitRangeWidth(hit);
139  std::vector<double> params{center, rangeWidth, sigma_};
140  for (unsigned int i = 0; i < hitProfile.size(); ++i)
141  hitProfile[i] +=
142  pixelEfficiencyFunction_.evaluate(std::vector<double>{profileRangeBegin + i * resolution_}, params);
143  }
144 
145  bool underThreshold = true;
146  float rangeMaximum = -1.0f;
147  bool trackRangeFound = false;
148  int trackRangeBegin = 0, trackRangeEnd = 0;
149 
150  // Searches for tracks in the hit profile
151  for (unsigned int i = 0; i < hitProfile.size(); i++) {
152  if (hitProfile[i] > rangeMaximum)
153  rangeMaximum = hitProfile[i];
154 
155  // Going above the threshold
156  if (underThreshold && hitProfile[i] > threshold_) {
157  underThreshold = false;
158  trackRangeBegin = i;
159  rangeMaximum = hitProfile[i];
160  }
161 
162  // Going under the threshold
163  else if (!underThreshold && hitProfile[i] <= threshold_) {
164  underThreshold = true;
165  trackRangeEnd = i;
166  trackRangeFound = true;
167  }
168 
169  // Finds all tracks within the track range
170  if (trackRangeFound) {
171  float trackThreshold = rangeMaximum - thresholdFromMaximum_;
172  int trackBegin;
173  bool underTrackThreshold = true;
174 
175  for (int j = trackRangeBegin; j <= trackRangeEnd; j++) {
176  if (underTrackThreshold && hitProfile[j] > trackThreshold) {
177  underTrackThreshold = false;
178  trackBegin = j;
179  } else if (!underTrackThreshold && hitProfile[j] <= trackThreshold) {
180  underTrackThreshold = true;
181  TRACK_TYPE track;
182  float leftMargin = profileRangeBegin + resolution_ * trackBegin;
183  float rightMargin = profileRangeBegin + resolution_ * j;
184  setTrackCenter(track, 0.5f * (leftMargin + rightMargin));
185  setTrackSigma(track, 0.5f * (rightMargin - leftMargin));
186  result.push_back(track);
187  }
188  }
189  trackRangeFound = false;
190  }
191  }
192 }
193 
194 template <class TRACK_TYPE, class HIT_TYPE>
197  bool initialized = false;
198  SpatialRange result = {};
199 
200  for (const auto& hit : hits) {
201  const float xBegin = hit.x() - 0.5f * hit.xWidth(), xEnd = hit.x() + 0.5f * hit.xWidth();
202  const float yBegin = hit.y() - 0.5f * hit.yWidth(), yEnd = hit.y() + 0.5f * hit.yWidth();
203  const float zBegin = hit.z() - 0.5f * hit.zWidth(), zEnd = hit.z() + 0.5f * hit.zWidth();
204 
205  if (!initialized) {
206  result.xBegin = xBegin;
207  result.xEnd = xEnd;
208  result.yBegin = yBegin;
209  result.yEnd = yEnd;
210  result.zBegin = zBegin;
211  result.zEnd = zEnd;
212  initialized = true;
213  continue;
214  }
215  result.xBegin = std::min(result.xBegin, xBegin);
216  result.xEnd = std::max(result.xEnd, xEnd);
217  result.yBegin = std::min(result.yBegin, yBegin);
218  result.yEnd = std::max(result.yEnd, yEnd);
219  result.zBegin = std::min(result.zBegin, zBegin);
220  result.zEnd = std::max(result.zEnd, zEnd);
221  }
222 
223  return result;
224 }
225 
226 template <class TRACK_TYPE, class HIT_TYPE>
228  float& mean_time,
229  float& time_sigma) const {
230  float mean_num = 0.f, mean_denom = 0.f;
231  bool valid_hits = false;
232  for (const auto& hit : hits) {
233  if (hit.tPrecision() <= 0.)
234  continue;
235  const float weight = std::pow(hit.tPrecision(), -2);
236  mean_num += weight * hit.time();
237  mean_denom += weight;
238  valid_hits = true; // at least one valid hit to account for
239  }
240  mean_time = valid_hits ? (mean_num / mean_denom) : 0.f;
241  time_sigma = valid_hits ? std::sqrt(1.f / mean_denom) : 0.f;
242  return valid_hits;
243 }
244 
245 #endif
SpatialRange getHitSpatialRange(const HitVector &hits)
double evaluate(V const &iVariables, P const &iParameters) const
Structure representing a 3D range in space.
void producePartialTracks(const HitVector &hits, const DimensionParameters &param, float(*getHitCenter)(const HIT_TYPE &), float(*getHitRangeWidth)(const HIT_TYPE &), void(*setTrackCenter)(TRACK_TYPE &, float), void(*setTrackSigma)(TRACK_TYPE &, float), TrackVector &result)
virtual ~CTPPSTimingTrackRecognition()=default
Definition: weight.py:1
constexpr int pow(int x)
Definition: conifer.h:24
virtual void clear()
Reset internal state of a class instance.
CTPPSTimingTrackRecognition(const edm::ParameterSet &iConfig)
std::unordered_map< int, HitVector > HitVectorMap
T sqrt(T t)
Definition: SSEVec.h:19
double f[11][100]
unsigned int numberOfParameters() const
reco::FormulaEvaluator pixelEfficiencyFunction_
std::vector< TRACK_TYPE > TrackVector
HitVectorMap hitVectorMap_
RecHit vectors that should be processed separately while reconstructing tracks.
virtual void addHit(const HIT_TYPE &recHit)=0
Add new hit to the set from which the tracks are reconstructed.
bool timeEval(const HitVector &hits, float &meanTime, float &timeSigma) const
virtual int produceTracks(edm::DetSet< TRACK_TYPE > &tracks)=0
Produce a collection of tracks, given its hits collection.