File indexing completed on 2024-09-07 04:37:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <iostream>
0019 #include <memory>
0020
0021
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/one/EDProducer.h"
0024
0025 #include "FWCore/Framework/interface/Event.h"
0026 #include "FWCore/Framework/interface/EventSetup.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030
0031 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
0032 #include "DataFormats/METReco/interface/HcalCaloFlagLabels.h"
0033 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0034 #include "Geometry/Records/interface/HcalRecNumberingRecord.h"
0035
0036 #include "CondFormats/HcalObjects/interface/HcalChannelStatus.h"
0037 #include "CondFormats/HcalObjects/interface/HcalChannelQuality.h"
0038 #include "CondFormats/HcalObjects/interface/HcalCondObjectContainer.h"
0039 #include "CondFormats/DataRecord/interface/HcalChannelQualityRcd.h"
0040
0041 using namespace std;
0042 using namespace edm;
0043
0044
0045
0046
0047
0048 class HcalRecHitReflagger : public edm::one::EDProducer<> {
0049 public:
0050 explicit HcalRecHitReflagger(const edm::ParameterSet&);
0051 ~HcalRecHitReflagger();
0052
0053 private:
0054 void beginJob() override;
0055 void produce(edm::Event&, const edm::EventSetup&) override;
0056 void endJob() override;
0057
0058
0059 double GetThreshold(const int base, const std::vector<double>& params);
0060 double GetThreshold(const double base, const std::vector<double>& params);
0061
0062
0063 bool CheckS9S1(const HFRecHit& hf);
0064
0065
0066 bool CheckPET(const HFRecHit& hf);
0067
0068
0069 double GetS9S1value(const HFRecHit& hf);
0070 double GetPETvalue(const HFRecHit& hf);
0071 double GetSlope(const int ieta, const std::vector<double>& params);
0072
0073
0074 const edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> tokTopo_;
0075 const edm::ESGetToken<HcalChannelQuality, HcalChannelQualityRcd> tokChan_;
0076 const HcalTopology* topo;
0077 edm::InputTag hfInputLabel_;
0078 edm::EDGetTokenT<HFRecHitCollection> tok_hf_;
0079 int hfFlagBit_;
0080
0081
0082 bool hfBitAlwaysOn_;
0083 bool hfBitAlwaysOff_;
0084 bool hf_Algo3test_;
0085 bool hf_Algo2test_;
0086 bool hf_Algo3_AND_Algo2_;
0087 bool hf_Algo3_OR_Algo2_;
0088
0089 std::vector<double> S9S1_EnergyThreshLong_;
0090 std::vector<double> S9S1_ETThreshLong_;
0091 std::vector<double> S9S1_EnergyThreshShort_;
0092 std::vector<double> S9S1_ETThreshShort_;
0093 std::vector<double> S9S1_optimumslope_;
0094 std::vector<double> PET_EnergyThreshLong_;
0095 std::vector<double> PET_ETThreshLong_;
0096 std::vector<double> PET_EnergyThreshShort_;
0097 std::vector<double> PET_ETThreshShort_;
0098 std::vector<double> PET_ratiocut_;
0099 int debug_;
0100
0101
0102 std::map<HcalDetId, unsigned int> badstatusmap;
0103
0104 edm::Handle<HFRecHitCollection> hfRecHits;
0105 };
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 HcalRecHitReflagger::HcalRecHitReflagger(const edm::ParameterSet& ps)
0119 : tokTopo_(esConsumes<HcalTopology, HcalRecNumberingRecord>()),
0120 tokChan_(esConsumes<HcalChannelQuality, HcalChannelQualityRcd>(edm::ESInputTag("", "withTopo"))) {
0121
0122 produces<HFRecHitCollection>();
0123
0124 hfInputLabel_ = ps.getUntrackedParameter<InputTag>("hfInputLabel", edm::InputTag("hfreco"));
0125 tok_hf_ = consumes<HFRecHitCollection>(hfInputLabel_);
0126 hfFlagBit_ = ps.getUntrackedParameter<int>("hfFlagBit", HcalCaloFlagLabels::UserDefinedBit0);
0127 debug_ = ps.getUntrackedParameter<int>("debug", 0);
0128
0129
0130 hfBitAlwaysOn_ = ps.getUntrackedParameter<bool>("hfBitAlwaysOn", false);
0131 hfBitAlwaysOff_ = ps.getUntrackedParameter<bool>("hfBitAlwaysOff", false);
0132
0133
0134 hf_Algo3test_ = ps.getUntrackedParameter<bool>("hf_Algo3test", false);
0135 hf_Algo2test_ = ps.getUntrackedParameter<bool>("hf_Algo2test", false);
0136 hf_Algo3_AND_Algo2_ = ps.getUntrackedParameter<bool>("hf_Algo3_AND_Algo2", false);
0137 hf_Algo3_OR_Algo2_ = ps.getUntrackedParameter<bool>("hf_Algo3_OR_Algo2", false);
0138
0139
0140 const edm::ParameterSet& S9S1 = ps.getParameter<edm::ParameterSet>("hf_S9S1_params");
0141 S9S1_EnergyThreshLong_ = S9S1.getUntrackedParameter<std::vector<double> >("S9S1_EnergyThreshLong");
0142 S9S1_ETThreshLong_ = S9S1.getUntrackedParameter<std::vector<double> >("S9S1_ETThreshLong");
0143 S9S1_EnergyThreshShort_ = S9S1.getUntrackedParameter<std::vector<double> >("S9S1_EnergyThreshShort");
0144 S9S1_ETThreshShort_ = S9S1.getUntrackedParameter<std::vector<double> >("S9S1_ETThreshShort");
0145 S9S1_optimumslope_ = S9S1.getParameter<std::vector<double> >("S9S1_optimumslope");
0146
0147 const edm::ParameterSet& PET = ps.getParameter<edm::ParameterSet>("hf_PET_params");
0148 PET_EnergyThreshLong_ = PET.getUntrackedParameter<std::vector<double> >("PET_EnergyThreshLong");
0149 PET_ETThreshLong_ = PET.getUntrackedParameter<std::vector<double> >("PET_ETThreshLong");
0150 PET_EnergyThreshShort_ = PET.getUntrackedParameter<std::vector<double> >("PET_EnergyThreshShort");
0151 PET_ETThreshShort_ = PET.getUntrackedParameter<std::vector<double> >("PET_ETThreshShort");
0152 PET_ratiocut_ = PET.getParameter<std::vector<double> >("PET_ratiocut");
0153 }
0154
0155 HcalRecHitReflagger::~HcalRecHitReflagger() {
0156
0157
0158 }
0159
0160
0161
0162
0163
0164 void HcalRecHitReflagger::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0165
0166 if (!iEvent.getByToken(tok_hf_, hfRecHits)) {
0167 if (debug_ > 0)
0168 std::cout << "Unable to find HFRecHitCollection with label '" << hfInputLabel_ << "'" << std::endl;
0169 return;
0170 }
0171
0172
0173 topo = &iSetup.getData(tokTopo_);
0174
0175
0176 const HcalChannelQuality& chanquality_ = iSetup.getData(tokChan_);
0177
0178 std::vector<DetId> mydetids = chanquality_.getAllChannels();
0179 for (std::vector<DetId>::const_iterator i = mydetids.begin(); i != mydetids.end(); ++i) {
0180 if (i->det() != DetId::Hcal)
0181 continue;
0182 HcalDetId id = HcalDetId(*i);
0183 int status = (chanquality_.getValues(id))->getValue();
0184 if ((status & (1 << HcalChannelStatus::HcalCellDead)) == 0)
0185 continue;
0186 badstatusmap[id] = status;
0187 }
0188
0189
0190 auto pOut = std::make_unique<HFRecHitCollection>();
0191
0192
0193 for (HFRecHitCollection::const_iterator recHit = hfRecHits->begin(); recHit != hfRecHits->end(); ++recHit) {
0194 HFRecHit newhit = (HFRecHit)(*recHit);
0195
0196 if (hfBitAlwaysOn_)
0197 newhit.setFlagField(1, hfFlagBit_);
0198
0199
0200 else if (hfBitAlwaysOff_)
0201 newhit.setFlagField(0, hfFlagBit_);
0202
0203 else {
0204
0205 bool Algo2bool = false;
0206 bool Algo3bool = false;
0207
0208 HcalDetId id(newhit.detid().rawId());
0209 int depth = newhit.id().depth();
0210 int ieta = newhit.id().ieta();
0211
0212
0213
0214 if (depth == 2)
0215 {
0216 Algo2bool = CheckPET(newhit);
0217 Algo3bool = Algo2bool;
0218 } else if (depth == 1)
0219 {
0220 Algo2bool = CheckPET(newhit);
0221 abs(ieta) == 29 ? Algo3bool = Algo2bool : Algo3bool = CheckS9S1(newhit);
0222 }
0223
0224 int flagvalue = -1;
0225
0226
0227 if (hf_Algo3_AND_Algo2_ == true)
0228 (Algo2bool && Algo3bool) == true ? flagvalue = 1 : flagvalue = 0;
0229 else if (hf_Algo3_OR_Algo2_ == true)
0230 (Algo2bool || Algo3bool) == true ? flagvalue = 1 : flagvalue = 0;
0231 else if (hf_Algo3test_ == true)
0232 Algo3bool == true ? flagvalue = 1 : flagvalue = 0;
0233 else if (hf_Algo2test_ == true)
0234 Algo2bool == true ? flagvalue = 1 : flagvalue = 0;
0235
0236
0237 if (flagvalue != -1)
0238 newhit.setFlagField(flagvalue, hfFlagBit_);
0239 }
0240
0241
0242 pOut->push_back(newhit);
0243 }
0244
0245
0246 iEvent.put(std::move(pOut));
0247 }
0248
0249
0250 void HcalRecHitReflagger::beginJob() {}
0251
0252
0253 void HcalRecHitReflagger::endJob() {}
0254
0255 bool HcalRecHitReflagger::CheckPET(const HFRecHit& hf) {
0256
0257
0258
0259
0260
0261 HcalDetId id(hf.detid().rawId());
0262 int iphi = id.iphi();
0263 int ieta = id.ieta();
0264 double energy = hf.energy();
0265 int depth = id.depth();
0266 std::pair<double, double> etas = topo->etaRange(HcalForward, abs(ieta));
0267 double fEta = 0.5 * (etas.first + etas.second);
0268 double ET = energy / cosh(fEta);
0269 double threshold = 0;
0270
0271 if (debug_ > 0)
0272 std::cout << "<RechitReflagger::CheckPET> energy = " << energy << " ieta = " << ieta << std::endl;
0273
0274 if (depth == 1)
0275 {
0276
0277 threshold = GetThreshold(ieta, PET_EnergyThreshLong_);
0278 if (energy < threshold)
0279 return false;
0280 threshold = GetThreshold(ieta, PET_ETThreshLong_);
0281 if (ET < threshold)
0282 return false;
0283
0284
0285
0286 double PETcut = GetThreshold(energy, PET_ratiocut_);
0287 double PETvalue = GetPETvalue(hf);
0288 if (debug_ > 1)
0289 std::cout << " <HcalRecHitReflagger::CheckPET> ieta = " << ieta
0290 << " energy threshold = " << GetThreshold(ieta, PET_EnergyThreshLong_)
0291 << " ET threshold = " << GetThreshold(ieta, PET_ETThreshLong_) << " ENERGY = " << energy
0292 << " PET threshold = " << PETcut << std::endl;
0293
0294 if (PETvalue > PETcut) {
0295 if (debug_ > 2)
0296 std::cout << "***** FOUND CHANNEL FAILING PET CUT! CHANNEL = " << ieta << ", " << iphi << ", " << depth
0297 << std::endl;
0298 return true;
0299 } else
0300 return false;
0301 }
0302
0303 else if (depth == 2)
0304 {
0305 threshold = GetThreshold(ieta, PET_EnergyThreshShort_);
0306 if (energy < threshold)
0307 return false;
0308 threshold = GetThreshold(ieta, PET_ETThreshShort_);
0309 if (ET < threshold)
0310 return false;
0311
0312 double PETcut = GetThreshold(energy, PET_ratiocut_);
0313 double PETvalue = GetPETvalue(hf);
0314 if (debug_ > 1)
0315 std::cout << " <HcalRecHitReflagger::CheckPET> ieta = " << ieta
0316 << " energy threshold = " << GetThreshold(ieta, PET_EnergyThreshShort_)
0317 << " ET threshold = " << GetThreshold(ieta, PET_ETThreshShort_) << " ENERGY = " << energy
0318 << " PET threshold = " << PETcut << endl;
0319
0320 if (PETvalue > PETcut) {
0321 if (debug_ > 2)
0322 std::cout << "***** FOUND CHANNEL FAILING PET CUT! CHANNEL = " << ieta << ", " << iphi << ", " << depth
0323 << std::endl;
0324
0325 return true;
0326 } else
0327 return false;
0328 }
0329 return false;
0330 }
0331
0332 bool HcalRecHitReflagger::CheckS9S1(const HFRecHit& hf) {
0333
0334
0335 HcalDetId id(hf.detid().rawId());
0336 int iphi = id.iphi();
0337 int ieta = id.ieta();
0338 double energy = hf.energy();
0339 int depth = id.depth();
0340 std::pair<double, double> etas = topo->etaRange(HcalForward, ieta);
0341 double fEta = 0.5 * (etas.first + etas.second);
0342 double ET = energy / cosh(fEta);
0343 double threshold = 0;
0344
0345
0346 double S9S1slope = S9S1_optimumslope_[abs(ieta) - 29];
0347 double S9S1value = GetS9S1value(hf);
0348
0349 if (debug_ > 0)
0350 std::cout << "<RechitReflagger::CheckS9S1> energy = " << energy << " ieta = " << ieta
0351 << " S9S1slope = " << S9S1slope << " S9S1value = " << S9S1value << std::endl;
0352
0353 if (depth == 1)
0354 {
0355
0356 threshold = GetThreshold(ieta, S9S1_EnergyThreshLong_);
0357 if (energy < threshold)
0358 return false;
0359 threshold = GetThreshold(ieta, S9S1_ETThreshLong_);
0360 if (ET < threshold)
0361 return false;
0362
0363
0364 double S9S1cut = -1. * S9S1slope * log(GetThreshold(ieta, PET_EnergyThreshLong_)) + S9S1slope * (log(energy));
0365
0366 if (S9S1value < S9S1cut) {
0367 if (debug_ > 0)
0368 std::cout << "***** FOUND CHANNEL FAILING S9S1 CUT! CHANNEL = " << ieta << ", " << iphi << ", " << depth
0369 << std::endl;
0370 return true;
0371 } else
0372 return false;
0373 }
0374
0375
0376 else if (depth == 2)
0377 {
0378
0379 threshold = GetThreshold(ieta, S9S1_EnergyThreshShort_);
0380 if (energy < threshold)
0381 return false;
0382 threshold = GetThreshold(ieta, S9S1_ETThreshShort_);
0383 if (ET < threshold)
0384 return false;
0385
0386
0387 double S9S1cut = -1. * S9S1slope * log(GetThreshold(ieta, PET_EnergyThreshShort_)) + S9S1slope * (log(energy));
0388
0389 if (S9S1value < S9S1cut) {
0390 if (debug_ > 0)
0391 std::cout << "***** FOUND CHANNEL FAILING S9S1 CUT! CHANNEL = " << ieta << ", " << iphi << ", " << depth
0392 << std::endl;
0393 return true;
0394 } else
0395 return false;
0396 }
0397 return false;
0398 }
0399
0400 double HcalRecHitReflagger::GetS9S1value(const HFRecHit& hf) {
0401
0402
0403
0404 HcalDetId id(hf.detid().rawId());
0405 int iphi = id.iphi();
0406 int ieta = id.ieta();
0407 double energy = hf.energy();
0408 int depth = id.depth();
0409 if (debug_ > 0)
0410 std::cout << "\t<HcalRecHitReflagger::GetS9S1value> Channel = (" << iphi << ", " << ieta << ", " << depth
0411 << ") : Energy = " << energy << std::endl;
0412
0413 double S9S1 = 0;
0414
0415 int testphi = -99;
0416
0417
0418 for (int d = 1; d <= 2; ++d)
0419 {
0420 for (int i = ieta - 1; i <= ieta + 1; ++i)
0421 {
0422 testphi = iphi;
0423
0424
0425 if (abs(ieta) == 39 && abs(i) > 39 && testphi % 4 == 1)
0426 testphi -= 2;
0427 while (testphi < 0)
0428 testphi += 72;
0429 if (i == ieta && d == depth)
0430 continue;
0431
0432 HcalDetId neighbor(HcalForward, i, testphi, d);
0433 HFRecHitCollection::const_iterator neigh = hfRecHits->find(neighbor);
0434 if (neigh != hfRecHits->end())
0435 S9S1 += neigh->energy();
0436 }
0437 }
0438
0439
0440
0441 int phiseg = 2;
0442 if (abs(ieta) > 39)
0443 phiseg = 4;
0444 for (int d = 1; d <= 2; ++d) {
0445 for (int i = iphi - phiseg; i <= iphi + phiseg; i += phiseg) {
0446 if (i == iphi)
0447 continue;
0448 testphi = i;
0449
0450 while (testphi < 0)
0451 testphi += 72;
0452 while (testphi > 72)
0453 testphi -= 72;
0454
0455 HcalDetId neighbor(HcalForward, ieta, testphi, d);
0456 HFRecHitCollection::const_iterator neigh = hfRecHits->find(neighbor);
0457 if (neigh != hfRecHits->end())
0458 S9S1 += neigh->energy();
0459 }
0460 }
0461
0462 if (abs(ieta) == 40)
0463 {
0464 for (int d = 1; d <= 2; ++d)
0465 {
0466 HcalDetId neighbor(HcalForward, 39 * abs(ieta) / ieta, (iphi + 2) % 72, d);
0467 HFRecHitCollection::const_iterator neigh = hfRecHits->find(neighbor);
0468 if (neigh != hfRecHits->end())
0469 S9S1 += neigh->energy();
0470 }
0471 }
0472
0473 S9S1 /= energy;
0474 return S9S1;
0475 }
0476
0477 double HcalRecHitReflagger::GetPETvalue(const HFRecHit& hf) {
0478 HcalDetId id(hf.detid().rawId());
0479 int iphi = id.iphi();
0480 int ieta = id.ieta();
0481 int depth = id.depth();
0482 double energy = hf.energy();
0483 if (debug_ > 0)
0484 std::cout << "\t<HcalRecHitReflagger::GetPETvalue> Channel = (" << iphi << ", " << ieta << ", " << depth
0485 << ") : Energy = " << energy << std::endl;
0486
0487 HcalDetId pId(HcalForward, ieta, iphi, 3 - depth);
0488
0489 if (badstatusmap.find(pId) != badstatusmap.end())
0490 return 0;
0491
0492 double epartner = 0;
0493
0494 HFRecHitCollection::const_iterator part = hfRecHits->find(pId);
0495 if (part != hfRecHits->end())
0496 epartner = part->energy();
0497 return 1. * (energy - epartner) / (energy + epartner);
0498 }
0499
0500 double HcalRecHitReflagger::GetThreshold(const int base, const std::vector<double>& params) {
0501 double thresh = 0;
0502 for (unsigned int i = 0; i < params.size(); ++i) {
0503 thresh += params[i] * pow(fabs(base), (int)i);
0504 }
0505 return thresh;
0506 }
0507
0508 double HcalRecHitReflagger::GetThreshold(const double base, const std::vector<double>& params) {
0509 double thresh = 0;
0510 for (unsigned int i = 0; i < params.size(); ++i) {
0511 thresh += params[i] * pow(fabs(base), (int)i);
0512 }
0513 return thresh;
0514 }
0515
0516 double HcalRecHitReflagger::GetSlope(const int ieta, const std::vector<double>& params) {
0517 double slope = 0;
0518 if (abs(ieta) == 40 && params.size() > 0)
0519 slope = params[0];
0520 else if (abs(ieta) == 41 && params.size() > 1)
0521 slope = params[1];
0522 else if (params.size() > 2) {
0523 for (unsigned int i = 2; i < params.size(); ++i)
0524 slope += params[i] * pow(static_cast<double>(abs(ieta)), static_cast<int>(i) - 2);
0525 }
0526 if (debug_ > 1)
0527 std::cout << "<HcalRecHitReflagger::GetSlope> ieta = " << ieta << " slope = " << slope << std::endl;
0528 return slope;
0529 }
0530
0531
0532 DEFINE_FWK_MODULE(HcalRecHitReflagger);