CMS 3D CMS Logo

Trackster.h
Go to the documentation of this file.
1 // Author: Felice Pantaleo - felice.pantaleo@cern.ch
2 // Date: 09/2018
3 
4 #ifndef DataFormats_HGCalReco_Trackster_h
5 #define DataFormats_HGCalReco_Trackster_h
6 
7 #include <array>
8 #include <vector>
11 
12 #include <Eigen/Core>
13 
14 // A Trackster is a Direct Acyclic Graph created when
15 // pattern recognition algorithms connect hits or
16 // layer clusters together in a 3D object.
17 
18 namespace ticl {
19  class Trackster {
20  public:
22 
23  enum IterationIndex { TRKEM = 0, EM, TRKHAD, HAD, MIP, SIM, SIM_CP };
24 
25  // types considered by the particle identification
26  enum class ParticleType {
27  photon = 0,
28  electron,
29  muon,
33  ambiguous,
34  unknown,
35  };
36 
37  enum class PCAOrdering { ascending = 0, descending };
38 
40  : barycenter_({0.f, 0.f, 0.f}),
42  raw_energy_(0.f),
43  boundTime_(0.f),
44  time_(0.f),
45  timeError_(-1.f),
47  raw_pt_(0.f),
48  raw_em_pt_(0.f),
49  raw_em_energy_(0.f),
50  seedIndex_(-1),
51  eigenvalues_{},
52  sigmas_{},
53  sigmasPCA_{},
54  iterationIndex_(0) {}
55 
57  std::vector<unsigned int> &vertices() { return vertices_; }
58  std::vector<float> &vertex_multiplicity() { return vertex_multiplicity_; }
59  std::vector<std::array<unsigned int, 2> > &edges() { return edges_; }
60  inline void setSeed(edm::ProductID pid, int index) {
61  seedID_ = pid;
62  seedIndex_ = index;
63  }
64  inline void setTimeAndError(float t, float tError) {
65  time_ = t;
66  timeError_ = tError;
67  }
68  inline void setRegressedEnergy(float value) { regressed_energy_ = value; }
69  inline void setRawEnergy(float value) { raw_energy_ = value; }
70  inline void addToRawEnergy(float value) { raw_energy_ += value; }
71  inline void setRawEmEnergy(float value) { raw_em_energy_ = value; }
72  inline void addToRawEmEnergy(float value) { raw_em_energy_ += value; }
73  inline void setRawPt(float value) { raw_pt_ = value; }
74  inline void setRawEmPt(float value) { raw_em_pt_ = value; }
76  inline void setTrackIdx(int index) { track_idx_ = index; }
77  int trackIdx() const { return track_idx_; }
78  inline bool isHadronic(float th = 0.5f) const {
80  }
81  inline void mergeTracksters(const Trackster &other) {
82  *this += other;
83 
84  //remove duplicates
87  }
88 
89  inline void mergeTracksters(const std::vector<Trackster> &others) {
90  for (auto &other : others) {
91  *this += other;
92  }
93 
94  //remove duplicates
97  }
100  Eigen::Vector3f const &sigmas,
101  Eigen::Vector3f const &sigmasEigen,
102  size_t pcadimension,
103  PCAOrdering order) {
104  int original_index = 0;
105  for (size_t i = 0; i < pcadimension; ++i) {
106  sigmas_[i] = std::sqrt(sigmas[i]);
107  // Reverse the order, since Eigen gives back the eigevalues in
108  // **increasing order** while we store them in **descreasing order**.
109  original_index = i;
111  original_index = pcadimension - i - 1;
112  eigenvalues_[i] = (float)eigenvalues[original_index];
114  eigenvectors(0, original_index), eigenvectors(1, original_index), eigenvectors(2, original_index));
115  sigmasPCA_[i] = std::sqrt(sigmasEigen[original_index]);
116  }
117  original_index = 0;
119  original_index = pcadimension - 1;
120  if (eigenvectors_[0].z() * barycenter_.z() < 0.0) {
122  eigenvectors(0, original_index), eigenvectors(1, original_index), eigenvectors(2, original_index));
123  }
124 
125  // Now also update the pt part of the Trackster, using the PCA as direction
126  raw_pt_ = std::sqrt((eigenvectors_[0].Unit() * raw_energy_).perp2());
128  }
130  for (auto &p : id_probabilities_) {
131  p = 0.f;
132  }
133  }
134  inline void setProbabilities(float *probs) {
135  for (float &p : id_probabilities_) {
136  p = *(probs++);
137  }
138  }
140 
141  inline void setBoundaryTime(float t) { boundTime_ = t; };
142 
144  inline const std::vector<unsigned int> &vertices() const { return vertices_; }
145  inline const unsigned int vertices(int index) const { return vertices_[index]; }
146  inline const std::vector<float> &vertex_multiplicity() const { return vertex_multiplicity_; }
147  inline const float vertex_multiplicity(int index) const { return vertex_multiplicity_[index]; }
148  inline const std::vector<std::array<unsigned int, 2> > &edges() const { return edges_; }
149  inline const edm::ProductID &seedID() const { return seedID_; }
150  inline const int seedIndex() const { return seedIndex_; }
151  inline const float time() const { return time_; }
152  inline const float timeError() const { return timeError_; }
153  inline const float regressed_energy() const { return regressed_energy_; }
154  inline const float raw_energy() const { return raw_energy_; }
155  inline const float raw_em_energy() const { return raw_em_energy_; }
156  inline const float raw_pt() const { return raw_pt_; }
157  inline const float raw_em_pt() const { return raw_em_pt_; }
158  inline const float boundaryTime() const { return boundTime_; };
159  inline const Vector &barycenter() const { return barycenter_; }
160  inline const std::array<float, 3> &eigenvalues() const { return eigenvalues_; }
161  inline const std::array<Vector, 3> &eigenvectors() const { return eigenvectors_; }
162  inline const Vector &eigenvectors(int index) const { return eigenvectors_[index]; }
163  inline const std::array<float, 3> &sigmas() const { return sigmas_; }
164  inline const std::array<float, 3> &sigmasPCA() const { return sigmasPCA_; }
165  inline const std::array<float, 8> &id_probabilities() const { return id_probabilities_; }
166  inline const float id_probabilities(int index) const { return id_probabilities_[index]; }
167 
168  // convenience method to return the ID probability for a certain particle type
169  inline float id_probability(ParticleType type) const {
170  // probabilities are stored in the same order as defined in the ParticleType enum
171  return id_probabilities_[(int)type];
172  }
173 
174  private:
177  float raw_energy_;
178  // -99, -1 if not available. ns units otherwise
179  float boundTime_;
180  float time_;
181  float timeError_;
182 
183  // trackster ID probabilities
184  std::array<float, 8> id_probabilities_;
185 
186  // The vertices of the DAG are the indices of the
187  // 2d objects in the global collection
188  std::vector<unsigned int> vertices_;
189  std::vector<float> vertex_multiplicity_;
190  float raw_pt_;
191  float raw_em_pt_;
193 
194  // Product ID of the seeding collection used to create the Trackster.
195  // For GlobalSeeding the ProductID is set to 0. For track-based seeding
196  // this is the ProductID of the track-collection used to create the
197  // seeding-regions.
199 
200  // For Global Seeding the index is fixed to one. For track-based seeding,
201  // the index is the index of the track originating the seeding region that
202  // created the trackster. For track-based seeding the pointer to the track
203  // can be cooked using the previous ProductID and this index.
205  int track_idx_ = -1;
206 
207  std::array<Vector, 3> eigenvectors_;
208  std::array<float, 3> eigenvalues_;
209  std::array<float, 3> sigmas_;
210  std::array<float, 3> sigmasPCA_;
211 
212  // The edges connect two vertices together in a directed doublet
213  // ATTENTION: order matters!
214  // A doublet generator should create edges in which:
215  // the first element is on the inner layer and
216  // the outer element is on the outer layer.
217  std::vector<std::array<unsigned int, 2> > edges_;
218 
219  // TICL iteration producing the trackster
221  inline void removeDuplicates() {
222  auto vtx_sorted{vertices_};
223  std::sort(std::begin(vtx_sorted), std::end(vtx_sorted));
224  for (unsigned int iLC = 1; iLC < vtx_sorted.size(); ++iLC) {
225  if (vtx_sorted[iLC] == vtx_sorted[iLC - 1]) {
226  // Clean up duplicate LCs
227  const auto lcIdx = vtx_sorted[iLC];
228  const auto firstEl = std::find(vertices_.begin(), vertices_.end(), lcIdx);
229  const auto firstPos = std::distance(std::begin(vertices_), firstEl);
230  auto iDup = std::find(std::next(firstEl), vertices_.end(), lcIdx);
231  while (iDup != vertices_.end()) {
232  vertex_multiplicity_.erase(vertex_multiplicity_.begin() + std::distance(std::begin(vertices_), iDup));
233  vertices_.erase(iDup);
234  vertex_multiplicity_[firstPos] -= 1;
235  iDup = std::find(std::next(firstEl), vertices_.end(), lcIdx);
236  };
237  }
238  }
239  }
240  inline void operator+=(const Trackster &other) {
241  // use getters on other
242  raw_energy_ += other.raw_energy();
243  raw_em_energy_ += other.raw_em_energy();
244  raw_pt_ += other.raw_pt();
245  raw_em_pt_ += other.raw_em_pt();
246  // add vertices and multiplicities
247  std::copy(std::begin(other.vertices()), std::end(other.vertices()), std::back_inserter(vertices_));
248  std::copy(std::begin(other.vertex_multiplicity()),
249  std::end(other.vertex_multiplicity()),
250  std::back_inserter(vertex_multiplicity_));
251  }
252  };
253 
254  typedef std::vector<Trackster> TracksterCollection;
255 } // namespace ticl
256 #endif
void setSeed(edm::ProductID pid, int index)
Definition: Trackster.h:60
float timeError_
Definition: Trackster.h:181
void setRawPt(float value)
Definition: Trackster.h:73
const float raw_pt() const
Definition: Trackster.h:156
const float regressed_energy() const
Definition: Trackster.h:153
void setTrackIdx(int index)
Definition: Trackster.h:76
void setRegressedEnergy(float value)
Definition: Trackster.h:68
const std::array< float, 3 > & sigmasPCA() const
Definition: Trackster.h:164
const std::vector< unsigned int > & vertices() const
Definition: Trackster.h:144
math::XYZVectorF Vector
Definition: Trackster.h:21
const Trackster::IterationIndex ticlIteration() const
Definition: Trackster.h:143
edm::ProductID seedID_
Definition: Trackster.h:198
const Vector & barycenter() const
Definition: Trackster.h:159
std::vector< unsigned int > vertices_
Definition: Trackster.h:188
void fillPCAVariables(Eigen::Vector3f const &eigenvalues, Eigen::Matrix3f const &eigenvectors, Eigen::Vector3f const &sigmas, Eigen::Vector3f const &sigmasEigen, size_t pcadimension, PCAOrdering order)
Definition: Trackster.h:98
const std::vector< float > & vertex_multiplicity() const
Definition: Trackster.h:146
void setRawEnergy(float value)
Definition: Trackster.h:69
int trackIdx() const
Definition: Trackster.h:77
uint8_t iterationIndex_
Definition: Trackster.h:220
bool isHadronic(float th=0.5f) const
Definition: Trackster.h:78
void addToRawEnergy(float value)
Definition: Trackster.h:70
void setProbabilities(float *probs)
Definition: Trackster.h:134
std::vector< std::array< unsigned int, 2 > > & edges()
Definition: Trackster.h:59
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::array< Vector, 3 > eigenvectors_
Definition: Trackster.h:207
float raw_em_pt_
Definition: Trackster.h:191
std::array< float, 3 > sigmas_
Definition: Trackster.h:209
const float timeError() const
Definition: Trackster.h:152
Vector barycenter_
Definition: Trackster.h:175
T perp2() const
Squared magnitude of transverse component.
const float raw_em_energy() const
Definition: Trackster.h:155
const std::array< Vector, 3 > & eigenvectors() const
Definition: Trackster.h:161
void removeDuplicates()
Definition: Trackster.h:221
const std::vector< std::array< unsigned int, 2 > > & edges() const
Definition: Trackster.h:148
void mergeTracksters(const Trackster &other)
Definition: Trackster.h:81
float boundTime_
Definition: Trackster.h:179
const float raw_energy() const
Definition: Trackster.h:154
T sqrt(T t)
Definition: SSEVec.h:23
std::vector< std::array< unsigned int, 2 > > edges_
Definition: Trackster.h:217
void operator+=(const Trackster &other)
Definition: Trackster.h:240
double f[11][100]
const Vector & eigenvectors(int index) const
Definition: Trackster.h:162
void zeroProbabilities()
Definition: Trackster.h:129
Definition: value.py:1
Eigen::Matrix3f Matrix3f
Definition: FitUtils.h:51
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< float > > XYZVectorF
spatial vector with cartesian internal representation
Definition: Vector3D.h:16
float raw_energy_
Definition: Trackster.h:177
const std::array< float, 3 > & sigmas() const
Definition: Trackster.h:163
float id_probability(ParticleType type) const
Definition: Trackster.h:169
const float id_probabilities(int index) const
Definition: Trackster.h:166
std::vector< unsigned int > & vertices()
Definition: Trackster.h:57
const unsigned int vertices(int index) const
Definition: Trackster.h:145
const float boundaryTime() const
Definition: Trackster.h:158
void setRawEmPt(float value)
Definition: Trackster.h:74
std::vector< float > vertex_multiplicity_
Definition: Trackster.h:189
const edm::ProductID & seedID() const
Definition: Trackster.h:149
void mergeTracksters(const std::vector< Trackster > &others)
Definition: Trackster.h:89
std::vector< float > & vertex_multiplicity()
Definition: Trackster.h:58
std::array< float, 8 > id_probabilities_
Definition: Trackster.h:184
void setRawEmEnergy(float value)
Definition: Trackster.h:71
const int seedIndex() const
Definition: Trackster.h:150
float regressed_energy_
Definition: Trackster.h:176
const float time() const
Definition: Trackster.h:151
Definition: Common.h:10
void addToRawEmEnergy(float value)
Definition: Trackster.h:72
std::vector< Trackster > TracksterCollection
Definition: Trackster.h:254
void setTimeAndError(float t, float tError)
Definition: Trackster.h:64
const float vertex_multiplicity(int index) const
Definition: Trackster.h:147
std::array< float, 3 > sigmasPCA_
Definition: Trackster.h:210
void setIteration(const Trackster::IterationIndex i)
Definition: Trackster.h:56
const float raw_em_pt() const
Definition: Trackster.h:157
void setBoundaryTime(float t)
Definition: Trackster.h:141
Eigen::Vector3f Vector3f
Definition: FitUtils.h:52
void setIdProbability(ParticleType type, float value)
Definition: Trackster.h:139
std::array< float, 3 > eigenvalues_
Definition: Trackster.h:208
const std::array< float, 8 > & id_probabilities() const
Definition: Trackster.h:165
const std::array< float, 3 > & eigenvalues() const
Definition: Trackster.h:160
float raw_em_energy_
Definition: Trackster.h:192
void setBarycenter(Vector value)
Definition: Trackster.h:75