CMS 3D CMS Logo

HGCalShowerShapeHelper.cc
Go to the documentation of this file.
2 
5 
7  std::shared_ptr<const hgcal::RecHitTools> recHitTools,
8  std::shared_ptr<const std::unordered_map<uint32_t, const reco::PFRecHit *>> pfRecHitPtrMap,
9  const std::vector<std::pair<DetId, float>> &hitsAndFracs,
10  const double rawEnergy,
11  const double minHitE,
12  const double minHitET,
13  const int minLayer,
14  const int maxLayer,
15  const DetId::Detector subDet)
16  : recHitTools_(recHitTools),
17  pfRecHitPtrMap_(pfRecHitPtrMap),
18  rawEnergy_(rawEnergy),
19  minHitE_(minHitE),
20  minHitET_(minHitET),
21  minHitET2_(minHitET * minHitET),
22  minLayer_(minLayer),
23  maxLayer_(maxLayer <= 0 ? recHitTools_->lastLayerEE() : maxLayer),
24  nLayer_(maxLayer_ - minLayer_ + 1),
25  subDet_(subDet) {
26  assert(nLayer_ > 0);
27  setFilteredHitsAndFractions(hitsAndFracs);
29 }
30 
32  return recHitTools_->getSiThickIndex(detId) == 0 ? kHDWaferCellSize_ : kLDWaferCellSize_;
33 }
34 
35 double HGCalShowerShapeHelper::ShowerShapeCalc::getRvar(double cylinderR, bool useFractions, bool useCellSize) const {
36  if (hitsAndFracs_.empty()) {
37  return 0.0;
38  }
39 
40  if (rawEnergy_ <= 0.0) {
41  edm::LogWarning("HGCalShowerShapeHelper")
42  << "Encountered negative or zero energy for HGCal R-variable denominator: " << rawEnergy_ << std::endl;
43  }
44 
45  double cylinderR2 = cylinderR * cylinderR;
46 
47  double rVar = 0.0;
48 
49  auto hitEnergyIter = useFractions ? hitEnergiesWithFracs_.begin() : hitEnergies_.begin();
50 
51  hitEnergyIter--;
52 
53  for (const auto &hnf : hitsAndFracs_) {
54  hitEnergyIter++;
55 
56  DetId hitId = hnf.first;
57 
58  int hitLayer = recHitTools_->getLayer(hitId) - 1;
59 
60  const auto &hitPos = recHitTools_->getPosition(hitId);
61  ROOT::Math::XYZVector hitXYZ(hitPos.x(), hitPos.y(), hitPos.z());
62 
63  auto distXYZ = hitXYZ - layerCentroids_[hitLayer];
64 
65  double r2 = distXYZ.x() * distXYZ.x() + distXYZ.y() * distXYZ.y();
66 
67  // Including the cell size seems to make the variable less sensitive to the HD/LD transition region
68  if (useCellSize) {
69  if (std::sqrt(r2) > cylinderR + getCellSize(hitId)) {
70  continue;
71  }
72  }
73 
74  else if (r2 > cylinderR2) {
75  continue;
76  }
77 
78  rVar += *hitEnergyIter;
79  }
80 
81  rVar /= rawEnergy_;
82 
83  return rVar;
84 }
85 
87  bool useFractions) const {
88  if (hitsAndFracs_.empty()) {
89  return ShowerWidths();
90  }
91 
92  double cylinderR2 = cylinderR * cylinderR;
93 
94  TMatrixD covMat(3, 3);
95 
96  double dxdx = 0.0;
97  double dydy = 0.0;
98  double dzdz = 0.0;
99 
100  double dxdy = 0.0;
101  double dydz = 0.0;
102  double dzdx = 0.0;
103 
104  double totalW = 0.0;
105 
106  auto hitEnergyIter = useFractions ? hitEnergiesWithFracs_.begin() : hitEnergies_.begin();
107 
108  int nHit = 0;
109  hitEnergyIter--;
110 
111  for (const auto &hnf : hitsAndFracs_) {
112  hitEnergyIter++;
113 
114  DetId hitId = hnf.first;
115 
116  const auto &hitPos = recHitTools_->getPosition(hitId);
117  ROOT::Math::XYZVector hitXYZ(hitPos.x(), hitPos.y(), hitPos.z());
118 
119  int hitLayer = recHitTools_->getLayer(hitId) - 1;
120 
121  ROOT::Math::XYZVector radXYZ = hitXYZ - layerCentroids_[hitLayer];
122 
123  double r2 = radXYZ.x() * radXYZ.x() + radXYZ.y() * radXYZ.y();
124 
125  if (r2 > cylinderR2) {
126  continue;
127  }
128 
129  ROOT::Math::XYZVector dXYZ = hitXYZ - centroid_;
130 
131  double weight = *hitEnergyIter;
132  totalW += weight;
133 
134  dxdx += weight * dXYZ.x() * dXYZ.x();
135  dydy += weight * dXYZ.y() * dXYZ.y();
136  dzdz += weight * dXYZ.z() * dXYZ.z();
137 
138  dxdy += weight * dXYZ.x() * dXYZ.y();
139  dydz += weight * dXYZ.y() * dXYZ.z();
140  dzdx += weight * dXYZ.z() * dXYZ.x();
141 
142  nHit++;
143  }
144 
145  if (!totalW || nHit < 2) {
146  return ShowerWidths();
147  }
148 
149  dxdx /= totalW;
150  dydy /= totalW;
151  dzdz /= totalW;
152 
153  dxdy /= totalW;
154  dydz /= totalW;
155  dzdx /= totalW;
156 
157  covMat(0, 0) = dxdx;
158  covMat(1, 1) = dydy;
159  covMat(2, 2) = dzdz;
160 
161  covMat(0, 1) = covMat(1, 0) = dxdy;
162  covMat(0, 2) = covMat(2, 0) = dzdx;
163  covMat(1, 2) = covMat(2, 1) = dydz;
164 
165  if (!covMat.Sum()) {
166  return ShowerWidths();
167  }
168 
169  // Get eigen values and vectors
170  TVectorD eigVals(3);
171  TMatrixD eigVecMat(3, 3);
172 
173  eigVecMat = covMat.EigenVectors(eigVals);
174 
175  ShowerWidths returnWidths;
176 
177  returnWidths.sigma2xx = dxdx;
178  returnWidths.sigma2yy = dydy;
179  returnWidths.sigma2zz = dzdz;
180 
181  returnWidths.sigma2xy = dxdy;
182  returnWidths.sigma2yz = dydz;
183  returnWidths.sigma2zx = dzdx;
184 
185  returnWidths.sigma2uu = eigVals(1);
186  returnWidths.sigma2vv = eigVals(2);
187  returnWidths.sigma2ww = eigVals(0);
188 
189  return returnWidths;
190 }
191 
193  bool useFractions) const {
194  std::vector<double> sortedEnergies(nrHits, 0.);
195  const auto &hits = useFractions ? hitEnergiesWithFracs_ : hitEnergies_;
196  std::partial_sort_copy(
197  hits.begin(), hits.end(), sortedEnergies.begin(), sortedEnergies.end(), std::greater<double>());
198  return sortedEnergies;
199 }
200 
202  const std::vector<std::pair<DetId, float>> &hitsAndFracs) {
203  hitsAndFracs_.clear();
204  hitEnergies_.clear();
205  hitEnergiesWithFracs_.clear();
206 
207  for (const auto &hnf : hitsAndFracs) {
208  DetId hitId = hnf.first;
209  float hitEfrac = hnf.second;
210 
211  int hitLayer = recHitTools_->getLayer(hitId);
212 
213  if (hitLayer > nLayer_) {
214  continue;
215  }
216 
217  if (hitId.det() != subDet_) {
218  continue;
219  }
220  auto hitIt = pfRecHitPtrMap_->find(hitId.rawId());
221  if (hitIt == pfRecHitPtrMap_->end()) {
222  continue;
223  }
224 
225  const reco::PFRecHit &recHit = *hitIt->second;
226 
227  if (recHit.energy() < minHitE_) {
228  continue;
229  }
230 
231  if (recHit.pt2() < minHitET2_) {
232  continue;
233  }
234 
235  // Fill the vectors
236  hitsAndFracs_.push_back(hnf);
237  hitEnergies_.push_back(recHit.energy());
238  hitEnergiesWithFracs_.push_back(recHit.energy() * hitEfrac);
239  }
240 }
241 
243  layerEnergies_.clear();
244  layerEnergies_.resize(nLayer_);
245 
246  layerCentroids_.clear();
247  layerCentroids_.resize(nLayer_);
248 
249  centroid_.SetXYZ(0, 0, 0);
250 
251  int iHit = -1;
252  double totalW = 0.0;
253 
254  // Compute the centroid per layer
255  for (const auto &hnf : hitsAndFracs_) {
256  iHit++;
257 
258  DetId hitId = hnf.first;
259 
260  double weight = hitEnergies_[iHit];
261  totalW += weight;
262 
263  const auto &hitPos = recHitTools_->getPosition(hitId);
264  ROOT::Math::XYZVector hitXYZ(hitPos.x(), hitPos.y(), hitPos.z());
265 
266  centroid_ += weight * hitXYZ;
267 
268  int hitLayer = recHitTools_->getLayer(hitId) - 1;
269 
270  layerEnergies_[hitLayer] += weight;
271  layerCentroids_[hitLayer] += weight * hitXYZ;
272  }
273 
274  int iLayer = -1;
275 
276  for (auto &centroid : layerCentroids_) {
277  iLayer++;
278 
279  if (layerEnergies_[iLayer]) {
280  centroid /= layerEnergies_[iLayer];
281  }
282  }
283 
284  if (totalW) {
285  centroid_ /= totalW;
286  }
287 }
288 
290  : recHitTools_(std::make_shared<hgcal::RecHitTools>()),
291  pfRecHitPtrMap_(std::make_shared<std::unordered_map<uint32_t, const reco::PFRecHit *>>()) {}
292 
294  : recHitTools_(std::make_shared<hgcal::RecHitTools>()),
295  pfRecHitPtrMap_(std::make_shared<std::unordered_map<uint32_t, const reco::PFRecHit *>>()) {
296  setTokens(sumes);
297 }
298 
300  recHitTools_->setGeometry(iSetup.getData(caloGeometryToken_));
301 }
302 
303 void HGCalShowerShapeHelper::initPerEvent(const std::vector<reco::PFRecHit> &pfRecHits) {
304  setPFRecHitPtrMap(pfRecHits);
305 }
306 
307 void HGCalShowerShapeHelper::initPerEvent(const edm::EventSetup &iSetup, const std::vector<reco::PFRecHit> &pfRecHits) {
308  initPerSetup(iSetup);
309  initPerEvent(pfRecHits);
310 }
311 
313  const std::vector<std::pair<DetId, float>> &hitsAndFracs,
314  double rawEnergy,
315  double minHitE,
316  double minHitET,
317  int minLayer,
318  int maxLayer,
319  DetId::Detector subDet) const {
320  return ShowerShapeCalc(
321  recHitTools_, pfRecHitPtrMap_, hitsAndFracs, rawEnergy, minHitE, minHitET, minLayer, maxLayer, subDet);
322 }
323 
324 void HGCalShowerShapeHelper::setPFRecHitPtrMap(const std::vector<reco::PFRecHit> &recHits) {
325  pfRecHitPtrMap_->clear();
326 
327  for (const auto &recHit : recHits) {
328  (*pfRecHitPtrMap_)[recHit.detId()] = &recHit;
329  }
330 }
HGCalShowerShapeHelper::HGCalShowerShapeHelper
HGCalShowerShapeHelper()
Definition: HGCalShowerShapeHelper.cc:289
HGCalShowerShapeHelper::ShowerShapeCalc::nLayer_
int nLayer_
Definition: HGCalShowerShapeHelper.h:123
HGCalShowerShapeHelper::recHitTools_
std::shared_ptr< hgcal::RecHitTools > recHitTools_
Definition: HGCalShowerShapeHelper.h:172
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
HGCalShowerShapeHelper::ShowerWidths::sigma2yy
double sigma2yy
Definition: HGCalShowerShapeHelper.h:65
HGCalShowerShapeHelper::ShowerShapeCalc
Definition: HGCalShowerShapeHelper.h:88
mps_merge.weight
weight
Definition: mps_merge.py:88
DetId::det
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
HGCalShowerShapeHelper::ShowerShapeCalc::getRvar
double getRvar(double cylinderR, bool useFractions=true, bool useCellSize=true) const
Definition: HGCalShowerShapeHelper.cc:35
HGCalShowerShapeHelper::ShowerWidths::sigma2uu
double sigma2uu
Definition: HGCalShowerShapeHelper.h:72
cms::cuda::assert
assert(be >=bs)
HGCalShowerShapeHelper::caloGeometryToken_
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > caloGeometryToken_
Definition: HGCalShowerShapeHelper.h:171
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
watchdog.const
const
Definition: watchdog.py:83
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
HGCalShowerShapeHelper::setTokens
void setTokens(edm::ConsumesCollector consumesCollector)
Definition: HGCalShowerShapeHelper.h:144
rpcPointValidation_cfi.recHit
recHit
Definition: rpcPointValidation_cfi.py:7
hgcal
Definition: EgammaPCAHelper.h:31
HGCalShowerShapeHelper::setPFRecHitPtrMap
void setPFRecHitPtrMap(const std::vector< reco::PFRecHit > &recHits)
Definition: HGCalShowerShapeHelper.cc:324
DetId
Definition: DetId.h:17
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
HGCalShowerShapeHelper::ShowerShapeCalc::setFilteredHitsAndFractions
void setFilteredHitsAndFractions(const std::vector< std::pair< DetId, float > > &hitsAndFracs)
Definition: HGCalShowerShapeHelper.cc:201
HGCalShowerShapeHelper::ShowerWidths::sigma2vv
double sigma2vv
Definition: HGCalShowerShapeHelper.h:73
HGCalShowerShapeHelper::ShowerWidths::sigma2zz
double sigma2zz
Definition: HGCalShowerShapeHelper.h:66
HGCalShowerShapeHelper::ShowerShapeCalc::getEnergyHighestHits
std::vector< double > getEnergyHighestHits(unsigned int nrHits, bool useFractions=true) const
Definition: HGCalShowerShapeHelper.cc:192
HGCalShowerShapeHelper.h
HGCalShowerShapeHelper::ShowerWidths
Definition: HGCalShowerShapeHelper.h:63
HGCalShowerShapeHelper::ShowerWidths::sigma2yz
double sigma2yz
Definition: HGCalShowerShapeHelper.h:69
HGCalShowerShapeHelper::pfRecHitPtrMap_
std::shared_ptr< std::unordered_map< uint32_t, const reco::PFRecHit * > > pfRecHitPtrMap_
Definition: HGCalShowerShapeHelper.h:173
FastTrackerRecHitMaskProducer_cfi.recHits
recHits
Definition: FastTrackerRecHitMaskProducer_cfi.py:8
HGCalShowerShapeHelper::createCalc
HGCalShowerShapeHelper::ShowerShapeCalc createCalc(const std::vector< std::pair< DetId, float > > &hitsAndFracs, double rawEnergy, double minHitE=0, double minHitET=0, int minLayer=1, int maxLayer=-1, DetId::Detector subDet=DetId::HGCalEE) const
HLT_FULL_cff.minLayer
minLayer
Definition: HLT_FULL_cff.py:52986
diffTwoXMLs.r2
r2
Definition: diffTwoXMLs.py:73
BeamSpotPI::dydz
Definition: BeamSpotPayloadInspectorHelper.h:38
HGCalShowerShapeHelper::ShowerWidths::sigma2xy
double sigma2xy
Definition: HGCalShowerShapeHelper.h:68
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
DetId::Detector
Detector
Definition: DetId.h:24
edm::EventSetup
Definition: EventSetup.h:58
HGCalShowerShapeHelper::ShowerWidths::sigma2ww
double sigma2ww
Definition: HGCalShowerShapeHelper.h:74
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:127
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
HGCalShowerShapeHelper::ShowerShapeCalc::ShowerShapeCalc
ShowerShapeCalc(std::shared_ptr< const hgcal::RecHitTools > recHitTools, std::shared_ptr< const std::unordered_map< uint32_t, const reco::PFRecHit * > > pfRecHitPtrMap, const std::vector< std::pair< DetId, float > > &hitsAndFracs, const double rawEnergy, const double minHitE=0, const double minHitET=0, const int minLayer=1, const int maxLayer=-1, DetId::Detector subDet=DetId::HGCalEE)
Definition: HGCalShowerShapeHelper.cc:6
HGCalShowerShapeHelper::kHDWaferCellSize_
static const double kHDWaferCellSize_
Definition: HGCalShowerShapeHelper.h:61
ROOT::Math::XYZVector
Transform3DPJ::Vector XYZVector
Definition: Transform3DPJ.cc:34
reco::PFRecHit
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
HGCalShowerShapeHelper::ShowerShapeCalc::getCellSize
double getCellSize(DetId detId) const
Definition: HGCalShowerShapeHelper.cc:31
HGCalShowerShapeHelper::ShowerWidths::sigma2zx
double sigma2zx
Definition: HGCalShowerShapeHelper.h:70
HGCalShowerShapeHelper::ShowerWidths::sigma2xx
double sigma2xx
Definition: HGCalShowerShapeHelper.h:64
HGCalShowerShapeHelper::initPerEvent
void initPerEvent(const std::vector< reco::PFRecHit > &recHits)
Definition: HGCalShowerShapeHelper.cc:303
HGCalShowerShapeHelper::ShowerShapeCalc::setLayerWiseInfo
void setLayerWiseInfo()
Definition: HGCalShowerShapeHelper.cc:242
HGCalShowerShapeHelper::kLDWaferCellSize_
static const double kLDWaferCellSize_
Definition: HGCalShowerShapeHelper.h:60
HGCalShowerShapeHelper::ShowerShapeCalc::getPCAWidths
ShowerWidths getPCAWidths(double cylinderR, bool useFractions=false) const
Definition: HGCalShowerShapeHelper.cc:86
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
weight
Definition: weight.py:1
HGCalShowerShapeHelper::initPerSetup
void initPerSetup(const edm::EventSetup &iSetup)
Definition: HGCalShowerShapeHelper.cc:299