CMS 3D CMS Logo

Cluster3DPCACalculator.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include <unordered_map>
6 
7 #include "vdt/vdtMath.h"
8 
12 
14 
15 #include "TPrincipal.h"
16 
18  public:
21  updateTiming_(conf.getParameter<bool>("updateTiming")),
22  pca_(new TPrincipal(3,"D")){
23  }
26 
29 
30  private:
31  const bool updateTiming_;
32  std::unique_ptr<TPrincipal> pca_;
33 
35  math::XYZVector& );
36 
38 };
39 
42  "Cluster3DPCACalculator");
43 
46  pca_.reset(new TPrincipal(3,"D"));
48 }
49 
52  for( reco::PFCluster& cluster : clusters ) {
53  pca_.reset(new TPrincipal(3,"D"));
55  }
56 }
57 
60  if( !cluster.seed() ) {
61  throw cms::Exception("ClusterWithNoSeed")
62  << " Found a cluster with no seed: " << cluster;
63  }
64  double cl_energy = 0;
65  double max_e = 0.0;
66  double avg_time = 0.0;
67  double time_norm = 0.0;
68  PFLayer::Layer max_e_layer = PFLayer::NONE;
69  reco::PFRecHitRef refseed;
70  double pcavars[3];
71 
72  for( const reco::PFRecHitFraction& rhf : cluster.recHitFractions() ) {
73  const reco::PFRecHitRef& refhit = rhf.recHitRef();
74  double rh_energy = refhit->energy();
75  double rh_time = refhit->time();
76  cl_energy += rh_energy * rhf.fraction();
77  if( rh_time > 0.0 ) { // time == -1 means no measurement
78  // all times are offset by one nanosecond in digitizer
79  // remove that here so all times of flight
80  // are with respect to (0,0,0)
81  avg_time += (rh_time - 1.0);
82  time_norm += 1.0;
83  }
84  if( rh_energy > max_e ) {
85  max_e = rh_energy;
86  max_e_layer = rhf.recHitRef()->layer();
87  }
88  if( refhit->detId() == cluster.seed() ) refseed = refhit;
89  const double rh_fraction = rhf.fraction();
90  rh_energy = refhit->energy()*rh_fraction;
91  if( edm::isNotFinite(rh_energy) ) {
92 //temporarily changed exception to warning
93 // throw cms::Exception("PFClusterAlgo")
94  edm::LogWarning("PFClusterAlgo")
95  <<"rechit " << refhit->detId() << " has a NaN energy... "
96  << "The input of the particle flow clustering seems to be corrupted.";
97  continue;
98  }
99  pcavars[0] = refhit->position().x();
100  pcavars[1] = refhit->position().y();
101  pcavars[2] = refhit->position().z();
102  int nhit = int( rh_energy*100 ); // put rec_hit energy in units of 10 MeV
103 
104  for( int i = 0; i < nhit; ++i ) {
105  pca_->AddRow(pcavars);
106  }
107 
108  }
109  cluster.setEnergy(cl_energy);
110  cluster.setLayer(max_e_layer);
111  // calculate the position
112 
113  pca_->MakePrincipals();
114  const TVectorD& means = *(pca_->GetMeanValues());
115  const TMatrixD& eigens = *(pca_->GetEigenVectors());
116 
117  math::XYZPoint barycenter(means[0],means[1],means[2]);
118  math::XYZVector axis(eigens(0,0),eigens(1,0),eigens(2,0));
119 
120  if( time_norm > 0.0 ) {
121  avg_time = avg_time/time_norm;
122  } else {
124  }
125 
126  if( axis.z()*barycenter.z() < 0.0 ) {
127  axis = math::XYZVector(-eigens(0,0),-eigens(1,0),-eigens(2,0));
128  }
129 
130  if (updateTiming_) {
131  cluster.setTime(avg_time);
132  }
133  cluster.setPosition(barycenter);
134  cluster.calculatePositionREP();
135 
136 }
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:112
Cluster3DPCACalculator & operator=(const Cluster3DPCACalculator &)=delete
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:117
void setTime(float time, float timeError=0)
Definition: PFCluster.h:92
void showerParameters(const reco::PFCluster &, math::XYZPoint &, math::XYZVector &)
Fraction of a PFRecHit (rechits can be shared between several PFCluster&#39;s)
void setEnergy(double energy)
Definition: CaloCluster.h:113
void calculatePositionREP()
computes posrep_ once and for all
Definition: PFCluster.h:100
Cluster3DPCACalculator(const edm::ParameterSet &conf)
T min(T a, T b)
Definition: MathUtil.h:58
void calculateAndSetPositionActual(reco::PFCluster &)
Layer
layer definition
Definition: PFLayer.h:31
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:207
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
void calculateAndSetPositions(reco::PFClusterCollection &) override
std::unique_ptr< TPrincipal > pca_
void calculateAndSetPosition(reco::PFCluster &) override
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:72
#define DEFINE_EDM_PLUGIN(factory, type, name)