CMS 3D CMS Logo

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