File indexing completed on 2024-04-06 11:59:04
0001 #include <iostream>
0002 #include <fstream>
0003 #include <algorithm>
0004 #include <numeric>
0005
0006 #include "TString.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "Calibration/HcalCalibAlgos/interface/hcalCalibUtils.h"
0009
0010
0011 #include "Calibration/HcalCalibAlgos/interface/CommonUsefulStuff.h"
0012 #include "DataFormats/DetId/interface/DetId.h"
0013 #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
0014
0015 void sumDepths(std::vector<TCell>& selectCells) {
0016
0017
0018
0019
0020 if (selectCells.empty())
0021 return;
0022
0023 std::vector<TCell> selectCellsDepth1;
0024 std::vector<TCell> selectCellsHighDepth;
0025
0026
0027
0028
0029
0030
0031
0032
0033 for (std::vector<TCell>::iterator i_it = selectCells.begin(); i_it != selectCells.end(); ++i_it) {
0034 if (HcalDetId(i_it->id()).depth() == 1) {
0035 selectCellsDepth1.push_back(*i_it);
0036 } else {
0037 selectCellsHighDepth.push_back(*i_it);
0038 }
0039 }
0040
0041
0042
0043 for (std::vector<TCell>::iterator i_it2 = selectCellsHighDepth.begin(); i_it2 != selectCellsHighDepth.end();
0044 ++i_it2) {
0045
0046 if (HcalDetId(i_it2->id()).ietaAbs() < 15 && HcalDetId(i_it2->id()).depth() > 1) {
0047 edm::LogWarning("HcalCalib") << "ERROR!!! there are no HB cells with depth>1 for iEta<15!\n"
0048 << "Check the input data...\nHCalDetId: " << HcalDetId(i_it2->id());
0049 return;
0050 }
0051
0052 bool foundDepthOne = false;
0053 for (std::vector<TCell>::iterator i_it = selectCellsDepth1.begin(); i_it != selectCellsDepth1.end(); ++i_it) {
0054 if (HcalDetId(i_it->id()).ieta() == HcalDetId(i_it2->id()).ieta() &&
0055 HcalDetId(i_it->id()).iphi() == HcalDetId(i_it2->id()).iphi())
0056 foundDepthOne = true;
0057 continue;
0058 }
0059 if (!foundDepthOne) {
0060
0061 UInt_t newId;
0062 if (abs(HcalDetId(i_it2->id()).ieta()) == 16)
0063 newId = HcalDetId(HcalBarrel, HcalDetId(i_it2->id()).ieta(), HcalDetId(i_it2->id()).iphi(), 1);
0064 else
0065 newId =
0066 HcalDetId(HcalDetId(i_it2->id()).subdet(), HcalDetId(i_it2->id()).ieta(), HcalDetId(i_it2->id()).iphi(), 1);
0067
0068 selectCellsDepth1.push_back(TCell(newId, 0.0));
0069 }
0070 }
0071
0072 for (std::vector<TCell>::iterator i_it = selectCellsDepth1.begin(); i_it != selectCellsDepth1.end(); ++i_it) {
0073 for (std::vector<TCell>::iterator i_it2 = selectCellsHighDepth.begin(); i_it2 != selectCellsHighDepth.end();
0074 ++i_it2) {
0075 if (HcalDetId(i_it->id()).ieta() == HcalDetId(i_it2->id()).ieta() &&
0076 HcalDetId(i_it->id()).iphi() == HcalDetId(i_it2->id()).iphi()) {
0077 i_it->SetE(i_it->e() + i_it2->e());
0078 i_it2->SetE(0.0);
0079 }
0080 }
0081 }
0082
0083
0084 selectCells = selectCellsDepth1;
0085
0086 return;
0087 }
0088
0089 void combinePhi(std::vector<TCell>& selectCells) {
0090
0091
0092
0093 if (selectCells.empty())
0094 return;
0095
0096
0097
0098
0099 std::vector<TCell> combinedCells;
0100
0101 std::map<UInt_t, std::vector<Float_t> > etaSliceE;
0102
0103
0104 std::vector<TCell>::iterator i_it = selectCells.begin();
0105 for (; i_it != selectCells.end(); ++i_it) {
0106 DetId id = HcalDetId(i_it->id());
0107 UInt_t thisKey = HcalDetId(HcalDetId(id).subdet(), HcalDetId(id).ieta(), 1, HcalDetId(id).depth());
0108 etaSliceE[thisKey].push_back(i_it->e());
0109 }
0110
0111 std::map<UInt_t, std::vector<Float_t> >::iterator m_it = etaSliceE.begin();
0112 for (; m_it != etaSliceE.end(); ++m_it) {
0113 combinedCells.push_back(TCell(m_it->first, accumulate(m_it->second.begin(), m_it->second.end(), 0.0)));
0114 }
0115
0116
0117 selectCells = combinedCells;
0118 }
0119
0120 void combinePhi(std::vector<TCell>& selectCells, std::vector<TCell>& combinedCells) {
0121
0122
0123
0124 if (selectCells.empty())
0125 return;
0126
0127 std::map<UInt_t, std::vector<Float_t> > etaSliceE;
0128
0129
0130 std::vector<TCell>::iterator i_it = selectCells.begin();
0131 for (; i_it != selectCells.end(); ++i_it) {
0132 DetId id = HcalDetId(i_it->id());
0133 UInt_t thisKey = HcalDetId(HcalDetId(id).subdet(), HcalDetId(id).ieta(), 1, HcalDetId(id).depth());
0134 etaSliceE[thisKey].push_back(i_it->e());
0135 }
0136
0137 std::map<UInt_t, std::vector<Float_t> >::iterator m_it = etaSliceE.begin();
0138 for (; m_it != etaSliceE.end(); ++m_it) {
0139 combinedCells.push_back(TCell(m_it->first, accumulate(m_it->second.begin(), m_it->second.end(), 0.0)));
0140 }
0141 }
0142
0143 void getIEtaIPhiForHighestE(std::vector<TCell>& selectCells, Int_t& iEtaMostE, UInt_t& iPhiMostE) {
0144 std::vector<TCell> summedDepthsCells = selectCells;
0145
0146 sumDepths(summedDepthsCells);
0147 std::vector<TCell>::iterator highCell = summedDepthsCells.begin();
0148
0149
0150
0151 Float_t highE = -999;
0152
0153 for (std::vector<TCell>::iterator it = summedDepthsCells.begin(); it != summedDepthsCells.end(); ++it) {
0154 if (highE < it->e()) {
0155 highCell = it;
0156 highE = it->e();
0157 }
0158 }
0159
0160 iEtaMostE = HcalDetId(highCell->id()).ieta();
0161 iPhiMostE = HcalDetId(highCell->id()).iphi();
0162
0163 return;
0164 }
0165
0166
0167
0168
0169
0170
0171
0172 void filterCells3x3(std::vector<TCell>& selectCells, Int_t iEtaMaxE, UInt_t iPhiMaxE) {
0173 std::vector<TCell> filteredCells;
0174
0175 Int_t dEta, dPhi;
0176
0177 for (std::vector<TCell>::iterator it = selectCells.begin(); it != selectCells.end(); ++it) {
0178 Bool_t passDEta = false;
0179 Bool_t passDPhi = false;
0180
0181 dEta = HcalDetId(it->id()).ieta() - iEtaMaxE;
0182 dPhi = HcalDetId(it->id()).iphi() - iPhiMaxE;
0183
0184 if (dPhi > 36)
0185 dPhi -= 72;
0186 if (dPhi < -36)
0187 dPhi += 72;
0188
0189 if (abs(dEta) <= 1 || (iEtaMaxE * HcalDetId(it->id()).ieta() == -1))
0190 passDEta = true;
0191
0192 if (abs(iEtaMaxE) <= 20) {
0193 if (abs(HcalDetId(it->id()).ieta()) <= 20) {
0194 if (abs(dPhi) <= 1)
0195 passDPhi = true;
0196 } else {
0197
0198 if (iPhiMaxE % 2 == 0) {
0199 if (abs(dPhi) <= 1)
0200 passDPhi = true;
0201 } else {
0202 if (dPhi == -2 || dPhi == 0)
0203 passDPhi = true;
0204 }
0205 }
0206
0207 }
0208
0209 else {
0210 if (abs(HcalDetId(it->id()).ieta()) <= 20) {
0211 if (abs(dPhi) <= 1 || dPhi == 2)
0212 passDPhi = true;
0213 } else {
0214 if (abs(dPhi) <= 2)
0215 passDPhi = true;
0216 }
0217 }
0218
0219 if (passDEta && passDPhi)
0220 filteredCells.push_back(*it);
0221 }
0222
0223 selectCells = filteredCells;
0224
0225 return;
0226 }
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 void filterCells5x5(std::vector<TCell>& selectCells, Int_t iEtaMaxE, UInt_t iPhiMaxE) {
0237 std::vector<TCell> filteredCells;
0238
0239 Int_t dEta, dPhi;
0240
0241 for (std::vector<TCell>::iterator it = selectCells.begin(); it != selectCells.end(); ++it) {
0242 dEta = HcalDetId(it->id()).ieta() - iEtaMaxE;
0243 dPhi = HcalDetId(it->id()).iphi() - iPhiMaxE;
0244
0245 if (dPhi > 36)
0246 dPhi -= 72;
0247 if (dPhi < -36)
0248 dPhi += 72;
0249
0250 bool passDPhi = (abs(dPhi) < 3);
0251
0252 bool passDEta = (abs(dEta) < 3 || (iEtaMaxE * HcalDetId(it->id()).ieta() == -2));
0253
0254
0255 if (passDPhi && passDEta)
0256 filteredCells.push_back(*it);
0257 }
0258
0259 selectCells = filteredCells;
0260
0261 return;
0262 }
0263
0264
0265
0266
0267 void sumSmallDepths(std::vector<TCell>& selectCells) {
0268 if (selectCells.empty())
0269 return;
0270
0271 std::vector<TCell> newCells;
0272 std::vector<TCell> manipulatedCells;
0273
0274 for (std::vector<TCell>::iterator i_it = selectCells.begin(); i_it != selectCells.end(); ++i_it) {
0275 if ((HcalDetId(i_it->id()).ietaAbs() == 15 && HcalDetId(i_it->id()).depth() <= 2) ||
0276 (HcalDetId(i_it->id()).ietaAbs() == 16 && HcalDetId(i_it->id()).depth() <= 2)) {
0277 manipulatedCells.push_back(*i_it);
0278 } else {
0279 newCells.push_back(*i_it);
0280 }
0281 }
0282
0283
0284
0285
0286 if (manipulatedCells.empty()) {
0287 newCells.clear();
0288 return;
0289 }
0290
0291
0292
0293
0294 std::vector<UInt_t> dummyIds;
0295 std::vector<TCell> createdCells;
0296
0297 for (std::vector<TCell>::iterator i_it = manipulatedCells.begin(); i_it != manipulatedCells.end(); ++i_it) {
0298 UInt_t dummyId =
0299 HcalDetId(HcalDetId(i_it->id()).subdet(), HcalDetId(i_it->id()).ieta(), HcalDetId(i_it->id()).iphi(), 1);
0300 if (find(dummyIds.begin(), dummyIds.end(), dummyId) == dummyIds.end()) {
0301 dummyIds.push_back(dummyId);
0302 createdCells.push_back(TCell(dummyId, 0.0));
0303 }
0304 }
0305
0306 for (std::vector<TCell>::iterator i_it = createdCells.begin(); i_it != createdCells.end(); ++i_it) {
0307 for (std::vector<TCell>::iterator i_it2 = manipulatedCells.begin(); i_it2 != manipulatedCells.end(); ++i_it2) {
0308 if (HcalDetId(i_it->id()).ieta() == HcalDetId(i_it2->id()).ieta() &&
0309 HcalDetId(i_it->id()).iphi() == HcalDetId(i_it2->id()).iphi() && HcalDetId(i_it2->id()).depth() <= 2) {
0310 i_it->SetE(i_it->e() + i_it2->e());
0311 }
0312 }
0313 }
0314
0315 for (std::vector<TCell>::iterator i_it = createdCells.begin(); i_it != createdCells.end(); ++i_it) {
0316 newCells.push_back(*i_it);
0317 }
0318
0319
0320 selectCells = newCells;
0321
0322 return;
0323 }
0324
0325 void filterCellsInCone(std::vector<TCell>& selectCells,
0326 const GlobalPoint hitPositionHcal,
0327 Float_t maxConeDist,
0328 const CaloGeometry* theCaloGeometry) {
0329 std::vector<TCell> filteredCells;
0330
0331 for (std::vector<TCell>::iterator it = selectCells.begin(); it != selectCells.end(); ++it) {
0332 GlobalPoint recHitPoint;
0333 DetId id = it->id();
0334 if (id.det() == DetId::Hcal) {
0335 recHitPoint = (static_cast<const HcalGeometry*>(theCaloGeometry->getSubdetectorGeometry(id)))->getPosition(id);
0336 } else {
0337 recHitPoint = GlobalPoint(theCaloGeometry->getPosition(id));
0338 }
0339
0340 if (getDistInPlaneSimple(hitPositionHcal, recHitPoint) <= maxConeDist)
0341 filteredCells.push_back(*it);
0342 }
0343
0344 selectCells = filteredCells;
0345
0346 return;
0347 }
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392