SND@LHC Software
Loading...
Searching...
No Matches
sndShowerTools.cxx File Reference
#include "sndShowerTools.h"
#include <vector>
#include <algorithm>
#include <utility>
#include "sndConfiguration.h"
#include "sndScifiPlane.h"
#include "sndUSPlane.h"
#include "TMatrixD.h"
#include "TMatrixDSym.h"
#include "TMatrixDSymEigen.h"
#include "TVectorD.h"
#include "TDecompChol.h"
#include "Math/Vector3D.h"
#include "Math/Point3D.h"
Include dependency graph for sndShowerTools.cxx:

Go to the source code of this file.

Functions

std::pair< double, double > PCA (const std::vector< double > &u, const std::vector< double > &z)
 

Function Documentation

◆ PCA()

std::pair< double, double > PCA ( const std::vector< double > &  u,
const std::vector< double > &  z 
)

Definition at line 152 of file sndShowerTools.cxx.

153{
154 // Need at least 3 measurements
155 assert(u.size() == z.size() && u.size() >= 2);
156 const size_t n_samples = u.size();
157 const int n_variables = 2;
158
159 // Find the mean values
160 double mean_u = 0.0, mean_z = 0.0;
161 for (size_t i = 0; i < n_samples; ++i)
162 {
163 mean_u += u[i];
164 mean_z += z[i];
165 }
166 mean_u /= n_samples;
167 mean_z /= n_samples;
168
169 // Find the covariance components
170 double cov_uu = 0.0, cov_zz = 0.0, cov_uz = 0.0;
171 for (size_t i = 0; i < n_samples; ++i)
172 {
173 double du = u[i] - mean_u;
174 double dz = z[i] - mean_z;
175 cov_uu += du * du;
176 cov_zz += dz * dz;
177 cov_uz += du * dz;
178 }
179
180 const double denom = (n_samples > 1) ? (n_samples - 1.0) : 1.0;
181 cov_uu /= denom;
182 cov_zz /= denom;
183 cov_uz /= denom;
184
185 // Fill the covariance matrix
186 TMatrixDSym cov_matrix(n_variables);
187 cov_matrix[0][0] = cov_uu;
188 cov_matrix[0][1] = cov_uz;
189 cov_matrix[1][0] = cov_uz;
190 cov_matrix[1][1] = cov_zz;
191
192 // Find the eigenvalues and eigenvectors. For now we only need the former.
193 // Perhaps the eigenvectors could be used in the future, so left that part in comments.
194 TMatrixDSymEigen eig_decomp(cov_matrix);
195 TVectorD evals = eig_decomp.GetEigenValues();
196 //TMatrixD evecs = eig_decomp.GetEigenVectors();
197
198 // Make sure the order is descending, swap if needed
199 if (evals[0] < evals[1])
200 {
201 std::swap(evals[0], evals[1]);
202 //evecs[0] = eig_decomp.GetEigenVectors()[1];
203 //evecs[1] = eig_decomp.GetEigenVectors()[0];
204 }
205
206 return {evals[0], evals[1]};
207}
int i
Definition ShipAna.py:86