CMS 3D CMS Logo

EgammaPCAHelper.cc
Go to the documentation of this file.
1 
3 
8 
10 
11 #include <algorithm>
12 #include <iostream>
13 #include <memory>
14 
15 using namespace hgcal;
16 
18  : // Thickness correction to dEdx weights
19  // (100um, 200um, 300um silicon)
20  // See RecoLocalCalo.HGCalRecProducers.HGCalRecHit_cfi
21  invThicknessCorrection_({1. / 1.132, 1. / 1.092, 1. / 1.084}),
22  pca_(new TPrincipal(3, "D")) {
23  hitMap_ = nullptr;
24  debug_ = false;
25 }
26 
27 void EGammaPCAHelper::setHitMap(const std::unordered_map<DetId, const HGCRecHit*>* hitMap) {
28  hitMap_ = hitMap;
29  pcaIteration_ = 0;
30 }
31 
33  recHitTools_ = recHitTools;
35 }
36 
38  theCluster_ = &cluster;
39  std::vector<std::pair<DetId, float>> result;
40  for (reco::HGCalMultiCluster::component_iterator it = cluster.begin(); it != cluster.end(); it++) {
41  const std::vector<std::pair<DetId, float>>& hf = (*it)->hitsAndFractions();
42  result.insert(result.end(), hf.begin(), hf.end());
43  }
45 }
46 
48  theCluster_ = &cluster;
49  storeRecHits(cluster.hitsAndFractions());
50 }
51 
52 void EGammaPCAHelper::storeRecHits(const std::vector<std::pair<DetId, float>>& hf) {
53  std::vector<double> pcavars;
54  pcavars.resize(3, 0.);
55  theSpots_.clear();
56  pcaIteration_ = 0;
57 
58  sigu_ = 0.;
59  sigv_ = 0.;
60  sigp_ = 0.;
61  sige_ = 0.;
62 
63  unsigned hfsize = hf.size();
64  if (debug_)
65  std::cout << "The seed cluster constains " << hfsize << " hits " << std::endl;
66 
67  if (hfsize == 0)
68  return;
69 
70  for (unsigned int j = 0; j < hfsize; j++) {
71  unsigned int layer = recHitTools_->getLayerWithOffset(hf[j].first);
72 
73  const DetId rh_detid = hf[j].first;
74  std::unordered_map<DetId, const HGCRecHit*>::const_iterator itcheck = hitMap_->find(rh_detid);
75  if (itcheck == hitMap_->end()) {
76  edm::LogWarning("EgammaPCAHelper") << " Big problem, unable to find a hit " << rh_detid.rawId() << " "
77  << rh_detid.det() << " " << HGCalDetId(rh_detid) << std::endl;
78  continue;
79  }
80  if (debug_) {
81  std::cout << "DetId " << rh_detid.rawId() << " " << layer << " " << itcheck->second->energy() << std::endl;
82  std::cout << " Hit " << itcheck->second << " " << itcheck->second->energy() << std::endl;
83  }
84  float fraction = hf[j].second;
85 
86  int thickIndex = recHitTools_->getSiThickIndex(rh_detid);
87  double mip = dEdXWeights_[layer] * 0.001; // convert in GeV
88  if (thickIndex > -1 and thickIndex < 3)
89  mip *= invThicknessCorrection_[thickIndex];
90 
91  pcavars[0] = recHitTools_->getPosition(rh_detid).x();
92  pcavars[1] = recHitTools_->getPosition(rh_detid).y();
93  pcavars[2] = recHitTools_->getPosition(rh_detid).z();
94  if (pcavars[2] == 0.)
95  edm::LogWarning("EgammaPCAHelper") << " Problem, hit with z =0 ";
96  else {
97  Spot mySpot(rh_detid, itcheck->second->energy(), pcavars, layer, fraction, mip);
98  theSpots_.push_back(mySpot);
99  }
100  }
101  if (debug_) {
102  std::cout << " Stored " << theSpots_.size() << " hits " << std::endl;
103  }
104 }
105 
106 void EGammaPCAHelper::computePCA(float radius, bool withHalo) {
107  // very important - to reset
108  pca_ = std::make_unique<TPrincipal>(3, "D");
109  bool initialCalculation = radius < 0;
110  if (debug_)
111  std::cout << " Initial calculation " << initialCalculation << std::endl;
112  if (initialCalculation && withHalo) {
113  edm::LogWarning("EGammaPCAHelper") << "Warning - in the first iteration, the halo hits are excluded " << std::endl;
114  withHalo = false;
115  }
116 
117  float radius2 = radius * radius;
118  if (!initialCalculation) {
119  math::XYZVector mainAxis(axis_);
120  mainAxis.unit();
121  math::XYZVector phiAxis(barycenter_.x(), barycenter_.y(), 0);
122  math::XYZVector udir(mainAxis.Cross(phiAxis));
123  udir = udir.unit();
126  Point(barycenter_ + udir),
127  Point(0, 0, 0),
128  Point(0., 0., 1.),
129  Point(1., 0., 0.));
130  }
131 
132  std::set<int> layers;
133  for (const auto& spot : theSpots_) {
134  if (spot.layer() > recHitTools_->lastLayerEE())
135  continue;
136  if (!withHalo && (!spot.isCore()))
137  continue;
138  if (initialCalculation) {
139  // initial calculation, take only core hits
140  if (!spot.isCore())
141  continue;
142  layers.insert(spot.layer());
143  for (int i = 0; i < spot.multiplicity(); ++i)
144  pca_->AddRow(spot.row());
145  } else {
146  // use a cylinder, include all hits
147  math::XYZPoint local = trans_(Point(spot.row()[0], spot.row()[1], spot.row()[2]));
148  if (local.Perp2() > radius2)
149  continue;
150  layers.insert(spot.layer());
151  for (int i = 0; i < spot.multiplicity(); ++i)
152  pca_->AddRow(spot.row());
153  }
154  }
155  if (debug_)
156  std::cout << " Nlayers " << layers.size() << std::endl;
157  if (layers.size() < 3) {
158  pcaIteration_ = -1;
159  return;
160  }
161  pca_->MakePrincipals();
162  ++pcaIteration_;
163  const TVectorD& means = *(pca_->GetMeanValues());
164  const TMatrixD& eigens = *(pca_->GetEigenVectors());
165 
167  axis_ = math::XYZVector(eigens(0, 0), eigens(1, 0), eigens(2, 0));
168  if (axis_.z() * barycenter_.z() < 0.0) {
169  axis_ = -1. * axis_;
170  }
171 }
172 
173 void EGammaPCAHelper::computeShowerWidth(float radius, bool withHalo) {
174  sigu_ = 0.;
175  sigv_ = 0.;
176  sigp_ = 0.;
177  sige_ = 0.;
178  double cyl_ene = 0.;
179 
180  float radius2 = radius * radius;
181  for (const auto& spot : theSpots_) {
182  Point globalPoint(spot.row()[0], spot.row()[1], spot.row()[2]);
183  math::XYZPoint local = trans_(globalPoint);
184  if (local.Perp2() > radius2)
185  continue;
186 
187  // Select halo hits or not
188  if (withHalo && spot.fraction() < 0)
189  continue;
190  if (!withHalo && !(spot.isCore()))
191  continue;
192 
193  sige_ += (globalPoint.eta() - theCluster_->eta()) * (globalPoint.eta() - theCluster_->eta()) * spot.energy();
194  sigp_ += deltaPhi(globalPoint.phi(), theCluster_->phi()) * deltaPhi(globalPoint.phi(), theCluster_->phi()) *
195  spot.energy();
196 
197  sigu_ += local.x() * local.x() * spot.energy();
198  sigv_ += local.y() * local.y() * spot.energy();
199  cyl_ene += spot.energy();
200  }
201 
202  if (cyl_ene > 0.) {
203  const double inv_cyl_ene = 1. / cyl_ene;
204  sigu_ = sigu_ * inv_cyl_ene;
205  sigv_ = sigv_ * inv_cyl_ene;
206  sigp_ = sigp_ * inv_cyl_ene;
207  sige_ = sige_ * inv_cyl_ene;
208  }
209  sigu_ = std::sqrt(sigu_);
210  sigv_ = std::sqrt(sigv_);
211  sigp_ = std::sqrt(sigp_);
212  sige_ = std::sqrt(sige_);
213 }
214 
216  if (pcaIteration_ == 0) {
217  if (debug_)
218  std::cout << " The PCA has not been run yet " << std::endl;
219  return false;
220  } else if (pcaIteration_ == 1) {
221  if (debug_)
222  std::cout << " The PCA has been run only once - careful " << std::endl;
223  return false;
224  } else if (pcaIteration_ == -1) {
225  if (debug_)
226  std::cout << " Not enough layers to perform PCA " << std::endl;
227  return false;
228  }
229  return true;
230 }
231 
233  theSpots_.clear();
234  pcaIteration_ = 0;
235  sigu_ = 0.;
236  sigv_ = 0.;
237  sigp_ = 0.;
238  sige_ = 0.;
239 }
240 
242  if (debug_)
243  checkIteration();
244  std::set<int> layers;
245  float radius2 = radius * radius;
246  std::vector<float> energyPerLayer(maxlayer_ + 1, 0.f);
247  math::XYZVector mainAxis(axis_);
248  mainAxis.unit();
249  math::XYZVector phiAxis(barycenter_.x(), barycenter_.y(), 0);
250  math::XYZVector udir(mainAxis.Cross(phiAxis));
251  udir = udir.unit();
254  Point(barycenter_ + udir),
255  Point(0, 0, 0),
256  Point(0., 0., 1.),
257  Point(1., 0., 0.));
258  float energyEE = 0.;
259  float energyFH = 0.;
260  float energyBH = 0.;
261 
262  for (const auto& spot : theSpots_) {
263  if (!withHalo && !spot.isCore())
264  continue;
265  math::XYZPoint local = trans_(Point(spot.row()[0], spot.row()[1], spot.row()[2]));
266  if (local.Perp2() > radius2)
267  continue;
268  energyPerLayer[spot.layer()] += spot.energy();
269  layers.insert(spot.layer());
270  if (spot.detId().det() == DetId::HGCalEE or spot.subdet() == HGCEE) {
271  energyEE += spot.energy();
272  } else if (spot.detId().det() == DetId::HGCalHSi or spot.subdet() == HGCHEF) {
273  energyFH += spot.energy();
274  } else if (spot.detId().det() == DetId::HGCalHSc or spot.subdet() == HGCHEB) {
275  energyBH += spot.energy();
276  }
277  }
278  return LongDeps(radius, energyPerLayer, energyEE, energyFH, energyBH, layers);
279 }
280 
282  unsigned nSpots = theSpots_.size();
283  float radius2 = radius * radius;
284  for (unsigned i = 0; i < nSpots; ++i) {
285  Spot spot(theSpots_[i]);
286  math::XYZPoint local = trans_(Point(spot.row()[0], spot.row()[1], spot.row()[2]));
287  if (local.Perp2() < radius2) {
288  std::cout << i << " " << theSpots_[i].detId().rawId() << " " << theSpots_[i].layer() << " "
289  << theSpots_[i].energy() << " " << theSpots_[i].isCore();
290  std::cout << " " << std::sqrt(local.Perp2()) << std::endl;
291  }
292  }
293 }
294 
296  unsigned int firstLayer = 0;
297  for (unsigned il = 1; il <= maxlayer_; ++il) {
298  if (ld.energyPerLayer()[il] > 0.) {
299  firstLayer = il;
300  break;
301  }
302  }
303  // Make dummy DetId to get abs(z) for layer
304  return recHitTools_->getPositionLayer(firstLayer).z();
305 }
306 
308  float& measuredDepth,
309  float& expectedDepth,
310  float& expectedSigma) {
311  expectedDepth = -999.;
312  expectedSigma = -999.;
313  measuredDepth = -999.;
314  if (!checkIteration())
315  return -999.;
316 
317  float z = findZFirstLayer(ld);
318  math::XYZVector dir = axis_.unit();
319  measuredDepth = std::abs((z - std::abs(barycenter_.z())) / dir.z());
320  return showerDepth_.getClusterDepthCompatibility(measuredDepth, ld.energyEE(), expectedDepth, expectedSigma);
321 }
void computeShowerWidth(float radius, bool withHalo=true)
void printHits(float radius) const
math::XYZPoint barycenter_
const std::vector< std::pair< DetId, float > > & hitsAndFractions() const
Definition: CaloCluster.h:209
void computePCA(float radius, bool withHalo=true)
LongDeps energyPerLayer(float radius, bool withHalo=true)
T z() const
Definition: PV3DBase.h:61
std::unique_ptr< TPrincipal > pca_
const double * row() const
Definition: Spot.h:24
const std::vector< float > & energyPerLayer() const
Definition: LongDeps.h:28
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
double phi() const
azimuthal angle of cluster centroid
Definition: CaloCluster.h:183
void storeRecHits(const reco::CaloCluster &theCluster)
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
GlobalPoint getPosition(const DetId &id) const
Definition: RecHitTools.cc:129
T sqrt(T t)
Definition: SSEVec.h:19
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float getClusterDepthCompatibility(float measuredDepth, float emEnergy, float &expectedDepth, float &expectedSigma) const
Definition: ShowerDepth.cc:6
double f[11][100]
std::vector< Spot > theSpots_
ROOT::Math::Transform3D Transform3D
component_iterator end() const
Definition: DetId.h:17
void setRecHitTools(const hgcal::RecHitTools *recHitTools)
float clusterDepthCompatibility(const LongDeps &, float &measuredDepth, float &expectedDepth, float &expectedSigma)
float findZFirstLayer(const LongDeps &) const
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
component_iterator begin() const
math::XYZVector axis_
GlobalPoint getPositionLayer(int layer, bool nose=false) const
Definition: RecHitTools.cc:141
unsigned int lastLayerBH() const
Definition: RecHitTools.h:78
Structure Point Contains parameters of Gaussian fits to DMRs.
std::vector< double > invThicknessCorrection_
void setHitMap(const std::unordered_map< DetId, const HGCRecHit *> *hitMap)
to set once per event
std::vector< double > dEdXWeights_
const reco::CaloCluster * theCluster_
double eta() const
pseudorapidity of cluster centroid
Definition: CaloCluster.h:180
const std::unordered_map< DetId, const HGCRecHit * > * hitMap_
Log< level::Warning, false > LogWarning
ROOT::Math::Transform3D::Point Point
int getSiThickIndex(const DetId &) const
Definition: RecHitTools.cc:205
const hgcal::RecHitTools * recHitTools_
unsigned int lastLayerEE(bool nose=false) const
Definition: RecHitTools.h:75
unsigned int getLayerWithOffset(const DetId &) const
Definition: RecHitTools.cc:365
float energyEE() const
Definition: LongDeps.h:25