ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtHitCluster.cxx
Go to the documentation of this file.
1 #include "AtHitCluster.h"
2 
3 #include <FairLogger.h>
4 
5 #include <Math/Point3D.h> // for Cartesian3D, operator<<, operator-
6 #include <Math/Vector3D.h> // for DisplacementVector3D, operator<<, operator*
7 #include <Rtypes.h>
8 #include <TError.h> // for Error
9 #include <TMatrixTUtils.h> // for TMatrixTRow
10 
11 #include <array> // for array
12 
14 
15 constexpr auto scale = 1;
16 
17 std::unique_ptr<AtHit> AtHitCluster::Clone()
18 {
19  return std::make_unique<AtHitCluster>(*this);
20 }
21 
22 AtHitCluster::AtHitCluster() : AtHit(-1, -1, {0, 0, 0}, 0)
23 {
24  fCovMatrix.Zero();
25  fCovNumerator.Zero();
26  fPositionVariance.SetCoordinates(0, 0, 0);
27 }
28 
32 void AtHitCluster::SetCovMatrix(int i, int j, double val)
33 {
34  fCovMatrix(i, j) = val;
35  fCovMatrix(j, i) = val;
36 }
37 
52 void AtHitCluster::AddHit(const AtHit &hit)
53 {
54  LOG(debug) << "Adding hit " << hit.GetPosition() << " " << hit.GetCharge();
56  updatePosition(hit);
57  updateCovariance(hit);
58 }
59 
67 {
68  // Get array representations of the positions
69  std::array<double, 3> hitPos{}, oldPos{}, pos{};
70 
71  hit.GetPosition().GetCoordinates(hitPos.begin());
72  fPositionChargeOld.GetCoordinates(oldPos.begin());
73  fPositionCharge.GetCoordinates(pos.begin());
74 
75  // Update off diagonal elements of the covariance matrix
76  for (int i = 0; i < 3; ++i)
77  for (int j = 0; j < i; ++j) {
78  fCovNumerator[i][j] += hit.GetCharge() * (hitPos[i] - pos[i]) * (hitPos[j] - oldPos[j]);
79  fCovNumerator[j][i] = fCovNumerator[i][j];
80  if (fCharge != 1)
81  fCovMatrix[i][j] = fCovNumerator[i][j] / (fCharge - 1);
82  else
83  fCovMatrix[i][j] = fCovNumerator[i][j] / (fCharge);
84 
85  fCovMatrix[j][i] = fCovMatrix[i][j];
86  }
87 
88  std::array<double, 3> weight{}, totalWeight{}, totalWeight2{};
89  fPositionOld.GetCoordinates(oldPos.begin());
90  fPosition.GetCoordinates(pos.begin());
91  fWeight.GetCoordinates(weight.begin());
92  fTotalWeight.GetCoordinates(totalWeight.begin());
93  fTotalWeight2.GetCoordinates(totalWeight2.begin());
94 
95  // Update on diagonal elements of the covariance matrix
96  for (int i = 0; i < 3; ++i) {
97  fCovNumerator(i, i) += weight[i] * (hitPos[i] - pos[i]) * (hitPos[i] - oldPos[i]);
98  LOG(debug) << "Numerator_" << i << ": " << fCovNumerator(i, i);
99  if (fClusterSize == 0)
100  fCovMatrix(i, i) = 1 / weight[i] * scale;
101  else
102  fCovMatrix(i, i) = fCovNumerator(i, i) / (totalWeight[i] - totalWeight2[i] / totalWeight[i]);
103  }
104 
105  fPositionVariance.SetCoordinates(fCovMatrix(0, 0), fCovMatrix(1, 1), fCovMatrix(2, 2));
106 }
107 
114 {
116  auto wRatio = ElementDiv(fWeight, fTotalWeight); // Ratio between this weight and the total weight
117  fPosition += ElementMult(wRatio, hit.GetPosition() - fPosition);
118  LOG(debug) << "Position: " << fPosition;
119 
122  LOG(debug) << "Position charge: " << fPositionCharge;
123 }
124 
130 {
131 
132  // Update the weights
133  auto wRel = ElementInvert(hit.GetPositionVariance());
134  auto wRel2 = ElementMult(wRel, wRel);
135  fWeight = hit.GetCharge() * wRel * scale;
136  auto w2 = hit.GetCharge() * wRel2 * scale;
138  fTotalWeight2 += w2;
139  fCharge += hit.GetCharge();
140  fClusterSize++;
141 
142  LOG(debug) << "w: " << fWeight;
143  LOG(debug) << "W: " << fTotalWeight;
144  LOG(debug) << "W2: " << fTotalWeight2;
145  LOG(debug) << "Q: " << fCharge;
146  LOG(debug) << "Denom: " << fTotalWeight - ElementDiv(fTotalWeight2, fTotalWeight);
147 }
148 
150 {
151  SetCovMatrix(0, 0, vec.X());
152  SetCovMatrix(1, 1, vec.Y());
153  SetCovMatrix(2, 2, vec.Z());
155 }
AtHitCluster::ElementMult
XYZVector ElementMult(const A &a, const B &b)
Definition: AtHitCluster.h:83
AtHitCluster::fTotalWeight2
XYZVector fTotalWeight2
Definition: AtHitCluster.h:49
AtHitCluster::fWeight
XYZVector fWeight
Definition: AtHitCluster.h:50
AtHitCluster::updateWeightsAndCharge
void updateWeightsAndCharge(const AtHit &hit)
Definition: AtHitCluster.cxx:129
scale
constexpr auto scale
Definition: AtHitCluster.cxx:15
AtHitCluster::AtHitCluster
AtHitCluster()
Definition: AtHitCluster.cxx:22
AtHit::fPositionVariance
XYZVector fPositionVariance
Definition: AtHit.h:37
AtHitCluster
: Class representing a cluster of hits that arise from the same deposition of charge in space....
Definition: AtHitCluster.h:37
AtHitCluster.h
AtHit::XYZVector
ROOT::Math::XYZVector XYZVector
Definition: AtHit.h:30
AtHitCluster::fCovNumerator
TMatrixDSym fCovNumerator
Definition: AtHitCluster.h:45
AtHitCluster::fPositionCharge
XYZPoint fPositionCharge
Definition: AtHitCluster.h:52
AtHitCluster::SetCovMatrix
void SetCovMatrix(TMatrixDSym matrix)
Definition: AtHitCluster.h:65
ClassImp
ClassImp(AtHitCluster)
AtHitCluster::updateCovariance
void updateCovariance(const AtHit &hit)
Definition: AtHitCluster.cxx:66
AtHitCluster::fPositionOld
XYZVector fPositionOld
Definition: AtHitCluster.h:54
AtHitCluster::updatePosition
void updatePosition(const AtHit &hit)
Definition: AtHitCluster.cxx:113
AtHit::fPosition
XYZPoint fPosition
Definition: AtHit.h:36
AtHit::fCharge
Double_t fCharge
Definition: AtHit.h:31
AtHitCluster::ElementDiv
XYZVector ElementDiv(const A &a, const B &b)
Definition: AtHitCluster.h:88
AtHitCluster::fTotalWeight
XYZVector fTotalWeight
Definition: AtHitCluster.h:48
AtHit::GetPosition
const XYZPoint & GetPosition() const
Definition: AtHit.h:79
AtHitCluster::AddHit
virtual void AddHit(const AtHit &hit)
Add hit to cluster.
Definition: AtHitCluster.cxx:52
AtHitCluster::fCovMatrix
TMatrixDSym fCovMatrix
Definition: AtHitCluster.h:42
AtHit::SetPositionVariance
virtual void SetPositionVariance(const XYZVector &vec)
Definition: AtHit.h:66
AtHitCluster::SetPositionVariance
virtual void SetPositionVariance(const XYZVector &vec) override
Definition: AtHitCluster.cxx:149
AtHitCluster::Clone
virtual std::unique_ptr< AtHit > Clone() override
Definition: AtHitCluster.cxx:17
AtHit::GetPositionVariance
const XYZVector & GetPositionVariance() const
Definition: AtHit.h:80
AtHitCluster::fPositionChargeOld
XYZPoint fPositionChargeOld
Definition: AtHitCluster.h:53
AtHitCluster::ElementInvert
T ElementInvert(const T &b)
Definition: AtHitCluster.h:93
AtHitCluster::fClusterSize
Int_t fClusterSize
Definition: AtHitCluster.h:57
AtHit::GetCharge
Double_t GetCharge() const
Definition: AtHit.h:82
AtHit
Point in space with charge.
Definition: AtHit.h:27