3 #include "TPrincipal.h"
11 const std::vector<reco::CaloCluster> &layerClusters,
14 LogDebug(
"TrackstersPCA_Eigen") <<
"------- Eigen -------" << std::endl;
16 for (
auto &trackster : tracksters) {
17 Eigen::Vector3d
point;
19 Eigen::Vector3d barycenter;
20 barycenter << 0., 0., 0.;
29 trackster.setRawEnergy(0.
f);
30 trackster.setRawEmEnergy(0.
f);
31 trackster.setRawPt(0.
f);
32 trackster.setRawEmPt(0.
f);
34 size_t N = trackster.vertices().size();
36 float weights2_sum = 0.f;
37 Eigen::Vector3d sigmas;
39 Eigen::Vector3d sigmasEigen;
40 sigmasEigen << 0., 0., 0.;
41 Eigen::Matrix3d covM = Eigen::Matrix3d::Zero();
43 for (
size_t i = 0;
i <
N; ++
i) {
44 auto fraction = 1.f / trackster.vertex_multiplicity(
i);
45 trackster.addToRawEnergy(layerClusters[trackster.vertices(
i)].energy() *
fraction);
46 if (
std::abs(layerClusters[trackster.vertices(
i)].z()) <= z_limit_em)
47 trackster.addToRawEmEnergy(layerClusters[trackster.vertices(
i)].energy() *
fraction);
52 fillPoint(layerClusters[trackster.vertices(
i)],
weight);
53 for (
size_t j = 0;
j < 3; ++
j)
56 if (energyWeight && trackster.raw_energy())
57 barycenter /= trackster.raw_energy();
62 for (
size_t i = 0;
i <
N; ++
i) {
63 fillPoint(layerClusters[trackster.vertices(
i)]);
64 if (energyWeight && trackster.raw_energy())
66 (layerClusters[trackster.vertices(
i)].energy() / trackster.vertex_multiplicity(
i)) / trackster.raw_energy();
68 for (
size_t x = 0;
x < 3; ++
x)
69 for (
size_t y = 0;
y <=
x; ++
y) {
71 covM(
y,
x) = covM(
x,
y);
74 covM *= 1. / (1. - weights2_sum);
77 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d>::RealVectorType eigenvalues_fromEigen;
78 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d>::EigenvectorsType eigenvectors_fromEigen;
79 Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigensolver(covM);
80 if (eigensolver.info() != Eigen::Success) {
81 eigenvalues_fromEigen = eigenvalues_fromEigen.Zero();
82 eigenvectors_fromEigen = eigenvectors_fromEigen.Zero();
84 eigenvalues_fromEigen = eigensolver.eigenvalues();
85 eigenvectors_fromEigen = eigensolver.eigenvectors();
89 for (
size_t i = 0;
i <
N; ++
i) {
90 fillPoint(layerClusters[trackster.vertices(
i)]);
91 sigmas +=
weight * (
point - barycenter).cwiseAbs2();
92 Eigen::Vector3d point_transformed = eigenvectors_fromEigen * (
point - barycenter);
93 if (energyWeight && trackster.raw_energy())
95 (layerClusters[trackster.vertices(
i)].energy() / trackster.vertex_multiplicity(
i)) / trackster.raw_energy();
96 sigmasEigen +=
weight * (point_transformed.cwiseAbs2());
98 sigmas /= (1. - weights2_sum);
99 sigmasEigen /= (1. - weights2_sum);
103 trackster.fillPCAVariables(
106 LogDebug(
"TrackstersPCA") <<
"Use energy weighting: " << energyWeight << std::endl;
107 LogDebug(
"TrackstersPCA") <<
"\nTrackster characteristics: " << std::endl;
108 LogDebug(
"TrackstersPCA") <<
"Size: " <<
N << std::endl;
109 LogDebug(
"TrackstersPCA") <<
"Energy: " << trackster.raw_energy() << std::endl;
110 LogDebug(
"TrackstersPCA") <<
"raw_pt: " << trackster.raw_pt() << std::endl;
111 LogDebug(
"TrackstersPCA") <<
"Means: " << barycenter[0] <<
", " << barycenter[1] <<
", " << barycenter[2]
113 LogDebug(
"TrackstersPCA") <<
"EigenValues from Eigen/Tr(cov): " << eigenvalues_fromEigen[2] / covM.trace() <<
", "
114 << eigenvalues_fromEigen[1] / covM.trace() <<
", "
115 << eigenvalues_fromEigen[0] / covM.trace() << std::endl;
116 LogDebug(
"TrackstersPCA") <<
"EigenValues from Eigen: " << eigenvalues_fromEigen[2] <<
", "
117 << eigenvalues_fromEigen[1] <<
", " << eigenvalues_fromEigen[0] << std::endl;
118 LogDebug(
"TrackstersPCA") <<
"EigenVector 3 from Eigen: " << eigenvectors_fromEigen(0, 2) <<
", "
119 << eigenvectors_fromEigen(1, 2) <<
", " << eigenvectors_fromEigen(2, 2) << std::endl;
120 LogDebug(
"TrackstersPCA") <<
"EigenVector 2 from Eigen: " << eigenvectors_fromEigen(0, 1) <<
", "
121 << eigenvectors_fromEigen(1, 1) <<
", " << eigenvectors_fromEigen(2, 1) << std::endl;
122 LogDebug(
"TrackstersPCA") <<
"EigenVector 1 from Eigen: " << eigenvectors_fromEigen(0, 0) <<
", "
123 << eigenvectors_fromEigen(1, 0) <<
", " << eigenvectors_fromEigen(2, 0) << std::endl;
124 LogDebug(
"TrackstersPCA") <<
"Original sigmas: " << sigmas[0] <<
", " << sigmas[1] <<
", " << sigmas[2]
126 LogDebug(
"TrackstersPCA") <<
"SigmasEigen in PCA space: " << sigmasEigen[2] <<
", " << sigmasEigen[1] <<
", "
127 << sigmasEigen[0] << std::endl;
128 LogDebug(
"TrackstersPCA") <<
"covM: \n" << covM << std::endl;