38 std::atomic<int> JetIDHelper::sanity_checks_left_{100};
43 useRecHits_ =
pset.getParameter<
bool>(
"useRecHits");
59 hcal_topo_token_ = iC.esConsumes();
66 fSubDetector1_ = -1.0;
67 fSubDetector2_ = -1.0;
68 fSubDetector3_ = -1.0;
69 fSubDetector4_ = -1.0;
70 restrictedEMF_ = -1.0;
73 approximatefHPD_ = -1.0;
74 approximatefRBX_ = -1.0;
76 fEB_ = fEE_ = fHB_ = fHE_ = fHO_ = fLong_ = fShort_ = -1.0;
77 fLS_ = fHFOOT_ = -1.0;
89 "If using RecHits to calculate the precise jet ID variables that need them, " 90 "their sources need to be specified");
107 if (E_Had + E_EM > 0)
108 restrictedEMF_ = E_EM / (E_EM + E_Had);
110 cout <<
"jet pT: " <<
jet.pt() <<
", eT: " <<
jet.et() <<
", E: " <<
jet.energy() <<
" rEMF: " << restrictedEMF_
116 vector<subtower> subtowers, Ecal_subtowers, Hcal_subtowers, HO_subtowers;
117 vector<double> HPD_energies, RBX_energies;
120 event,
jet, subtowers, Ecal_subtowers, Hcal_subtowers, HO_subtowers, HPD_energies, RBX_energies, iDbg);
123 for (
unsigned int i = 0;
i < subtowers.size(); ++
i)
124 cout <<
" " << subtowers[
i].E <<
"," << subtowers[
i].Nhit;
126 for (
unsigned int i = 0;
i < Ecal_subtowers.size(); ++
i)
127 cout <<
" " << Ecal_subtowers[
i].E <<
"," << Ecal_subtowers[
i].Nhit;
129 for (
unsigned int i = 0;
i < Hcal_subtowers.size(); ++
i)
130 cout <<
" " << Hcal_subtowers[
i].E <<
"," << Hcal_subtowers[
i].Nhit;
132 for (
unsigned int i = 0;
i < HO_subtowers.size(); ++
i)
133 cout <<
" " << HO_subtowers[
i].E <<
"," << HO_subtowers[
i].Nhit;
135 for (
unsigned int i = 0;
i < HPD_energies.size(); ++
i)
136 cout <<
" " << HPD_energies[
i];
138 for (
unsigned int i = 0;
i < RBX_energies.size(); ++
i)
139 cout <<
" " << RBX_energies[
i];
144 hitsInN90_ = hitsInNCarrying(0.9, subtowers);
145 nHCALTowers_ = Hcal_subtowers.size();
146 vector<subtower>::const_iterator it;
147 it = find_if(Ecal_subtowers.begin(), Ecal_subtowers.end(),
hasNonPositiveE);
148 nECALTowers_ = it - Ecal_subtowers.begin();
151 double max_HPD_energy = 0., max_RBX_energy = 0.;
152 std::vector<double>::const_iterator it_max_HPD_energy = std::max_element(HPD_energies.begin(), HPD_energies.end());
153 std::vector<double>::const_iterator it_max_RBX_energy = std::max_element(RBX_energies.begin(), RBX_energies.end());
154 if (it_max_HPD_energy != HPD_energies.end()) {
155 max_HPD_energy = *it_max_HPD_energy;
157 if (it_max_RBX_energy != RBX_energies.end()) {
158 max_RBX_energy = *it_max_RBX_energy;
160 if (
jet.energy() > 0) {
161 if (!HPD_energies.empty())
162 approximatefHPD_ = max_HPD_energy /
jet.energy();
163 if (!RBX_energies.empty())
164 approximatefRBX_ = max_HPD_energy /
jet.energy();
171 vector<double> energies, subdet_energies, Ecal_energies, Hcal_energies, HO_energies;
172 double LS_bad_energy, HF_OOT_energy;
173 classifyJetComponents(
event,
188 n90Hits_ = nCarrying(0.9, energies);
191 if (
jet.energy() > 0) {
192 if (!HPD_energies.empty())
193 fHPD_ = max_HPD_energy /
jet.energy();
194 if (!RBX_energies.empty())
195 fRBX_ = max_RBX_energy /
jet.energy();
196 if (!subdet_energies.empty())
197 fSubDetector1_ = subdet_energies.at(0) /
jet.energy();
198 if (subdet_energies.size() > 1)
199 fSubDetector2_ = subdet_energies.at(1) /
jet.energy();
200 if (subdet_energies.size() > 2)
201 fSubDetector3_ = subdet_energies.at(2) /
jet.energy();
202 if (subdet_energies.size() > 3)
203 fSubDetector4_ = subdet_energies.at(3) /
jet.energy();
204 fLS_ = LS_bad_energy /
jet.energy();
205 fHFOOT_ = HF_OOT_energy /
jet.energy();
207 if (iDbg > 0 && sanity_checks_left_.load(std::memory_order_acquire) > 0) {
208 --sanity_checks_left_;
209 double EH_sum = accumulate(Ecal_energies.begin(), Ecal_energies.end(), 0.);
210 EH_sum = accumulate(Hcal_energies.begin(), Hcal_energies.end(), EH_sum);
211 double EHO_sum = accumulate(HO_energies.begin(), HO_energies.end(), EH_sum);
212 if (
jet.energy() > 0.001 &&
abs(EH_sum /
jet.energy() - 1) > 0.01 &&
abs(EHO_sum /
jet.energy() - 1) > 0.01)
214 <<
") does not match the total energy in the recHits (" << EHO_sum
215 <<
", or without HO: " << EH_sum <<
") . Are these the right recHits? " 216 <<
"Were jet energy corrections mistakenly applied before jet ID? A bug?";
218 cout <<
"Sanity check - E: " <<
jet.energy() <<
" =? EH_sum: " << EH_sum <<
" / EHO_sum: " << EHO_sum << endl;
223 cout <<
"DBG - fHPD: " << fHPD_ <<
", fRBX: " << fRBX_ <<
", nh90: " << n90Hits_ <<
", fLS: " << fLS_
224 <<
", fHFOOT: " << fHFOOT_ << endl;
225 cout <<
" -~fHPD: " << approximatefHPD_ <<
", ~fRBX: " << approximatefRBX_ <<
", hits in n90: " << hitsInN90_
227 cout <<
" - nHCALTowers: " << nHCALTowers_ <<
", nECALTowers: " << nECALTowers_
228 <<
"; subdet fractions: " << fSubDetector1_ <<
", " << fSubDetector2_ <<
", " << fSubDetector3_ <<
", " 229 << fSubDetector4_ << endl;
236 for (
unsigned int i = 0;
i < descending_energies.size(); ++
i)
237 totalE += descending_energies[
i];
240 unsigned int NC = descending_energies.size();
243 for (
unsigned int i = descending_energies.size();
i > 0; --
i) {
244 runningE += descending_energies[
i - 1];
245 if (runningE < (1 -
fraction) * totalE)
252 const std::vector<subtower> &descending_towers) {
254 for (
unsigned int i = 0;
i < descending_towers.size(); ++
i)
255 totalE += descending_towers[
i].E;
261 for (
unsigned int i = descending_towers.size();
i > 0; --
i) {
262 runningE += descending_towers[
i - 1].E;
263 if (runningE >= (1 -
fraction) * totalE)
264 NH += descending_towers[
i - 1].Nhit;
272 vector<double> &energies,
273 vector<double> &subdet_energies,
274 vector<double> &Ecal_energies,
275 vector<double> &Hcal_energies,
276 vector<double> &HO_energies,
277 vector<double> &HPD_energies,
278 vector<double> &RBX_energies,
279 double &LS_bad_energy,
280 double &HF_OOT_energy,
283 subdet_energies.clear();
284 Ecal_energies.clear();
285 Hcal_energies.clear();
287 HPD_energies.clear();
288 RBX_energies.clear();
289 LS_bad_energy = HF_OOT_energy = 0.;
293 std::map<int, double> HPD_energy_map, RBX_energy_map;
294 vector<double> EB_energies, EE_energies, HB_energies, HE_energies, short_energies, long_energies;
301 event.getByToken(input_HBHERecHits_token_,
HBHERecHits);
302 event.getByToken(input_HORecHits_token_, HORecHits);
303 event.getByToken(input_HFRecHits_token_,
HFRecHits);
304 event.getByToken(input_EBRecHits_token_,
EBRecHits);
305 event.getByToken(input_EERecHits_token_,
EERecHits);
307 cout <<
"# of rechits found - HBHE: " <<
HBHERecHits->size() <<
", HO: " << HORecHits->
size()
310 vector<CaloTowerPtr>
towers =
jet.getCaloConstituents();
313 cout <<
"In classifyJetComponents. # of towers found: " <<
nTowers << endl;
315 for (
int iTower = 0; iTower <
nTowers; iTower++) {
320 cout <<
"tower #" << iTower <<
" has " <<
nCells <<
" cells. " 321 <<
"It's at iEta: " <<
tower->ieta() <<
", iPhi: " <<
tower->iphi() << endl;
323 const vector<DetId> &cellIDs =
tower->constituents();
325 for (
int iCell = 0; iCell <
nCells; ++iCell) {
333 if (theRecHit == HORecHits->
end()) {
334 edm::LogWarning(
"UnexpectedEventContents") <<
"Can't find the HO recHit with ID: " << HcalID;
337 hitE = theRecHit->energy();
338 HO_energies.push_back(hitE);
343 edm::LogWarning(
"UnexpectedEventContents") <<
"Can't find the HF recHit with ID: " << HcalID;
346 hitE = theRecHit->energy();
348 cout <<
"hit #" << iCell <<
" is HF , E: " << hitE <<
" iEta: " << theRecHit->id().ieta()
349 <<
", depth: " << theRecHit->id().depth() <<
", iPhi: " << theRecHit->id().iphi();
351 if (HcalID.
depth() == 1)
352 long_energies.push_back(hitE);
354 short_energies.push_back(hitE);
356 uint32_t
flags = theRecHit->flags();
358 LS_bad_energy += hitE;
362 HF_OOT_energy += hitE;
363 if (iDbg > 4 &&
flags)
364 cout <<
"flags: " <<
flags <<
" -> LS_bad_energy: " << LS_bad_energy <<
", HF_OOT_energy: " << HF_OOT_energy
371 edm::LogWarning(
"UnexpectedEventContents") <<
"Can't find the HBHE recHit with ID: " << HcalID;
374 hitE = theRecHit->energy();
375 int iEta = theRecHit->id().ieta();
376 int depth = theRecHit->id().depth();
378 int hitIPhi = theRecHit->id().iphi();
380 cout <<
"hit #" << iCell <<
" is HBHE, E: " << hitE <<
" iEta: " <<
iEta <<
", depth: " <<
depth 381 <<
", iPhi: " << theRecHit->id().iphi() <<
" -> " <<
region;
387 int iRBX = 100 *
region + ((hitIPhi + 1) % 72) / 4;
390 if ((0
x1 & hitIPhi) == 0) {
391 edm::LogError(
"CodeAssumptionsViolated") <<
"Bug?! Jet ID code assumes no even iPhi recHits at HE edges";
394 bool oddnessIEta = HBHE_oddness(
iEta,
depth);
395 bool upperIPhi = ((hitIPhi % 4) == 1 || (hitIPhi % 4) == 2);
399 if (upperIPhi != oddnessIEta)
406 HPD_energy_map[iHPD] += hitE;
407 RBX_energy_map[iRBX] += hitE;
409 cout <<
" --> H[" << iHPD <<
"]=" << HPD_energy_map[iHPD] <<
", R[" << iRBX <<
"]=" << RBX_energy_map[iRBX];
414 HB_energies.push_back(hitE);
416 HE_energies.push_back(hitE);
420 edm::LogWarning(
"UnexpectedEventContents") <<
"HCal hitE==0? (or unknown subdetector?)";
425 int EcalNum = cellIDs[iCell].subdetId();
428 EBDetId EcalID = cellIDs[iCell];
431 edm::LogWarning(
"UnexpectedEventContents") <<
"Can't find the EB recHit with ID: " << EcalID;
434 hitE = theRecHit->energy();
435 EB_energies.push_back(hitE);
436 }
else if (EcalNum == 2) {
437 EEDetId EcalID = cellIDs[iCell];
440 edm::LogWarning(
"UnexpectedEventContents") <<
"Can't find the EE recHit with ID: " << EcalID;
443 hitE = theRecHit->energy();
444 EE_energies.push_back(hitE);
447 edm::LogWarning(
"UnexpectedEventContents") <<
"ECal hitE==0? (or unknown subdetector?)";
449 cout <<
"EcalNum: " << EcalNum <<
" hitE: " << hitE << endl;
463 Hcal_energies.insert(Hcal_energies.end(), HB_energies.begin(), HB_energies.end());
464 Hcal_energies.insert(Hcal_energies.end(), HE_energies.begin(), HE_energies.end());
465 Hcal_energies.insert(Hcal_energies.end(), short_energies.begin(), short_energies.end());
466 Hcal_energies.insert(Hcal_energies.end(), long_energies.begin(), long_energies.end());
467 Ecal_energies.insert(Ecal_energies.end(), EB_energies.begin(), EB_energies.end());
468 Ecal_energies.insert(Ecal_energies.end(), EE_energies.begin(), EE_energies.end());
476 HPD_energy_map.begin(), HPD_energy_map.end(), std::inserter(HPD_energies, HPD_energies.end()),
select2nd);
480 RBX_energy_map.begin(), RBX_energy_map.end(), std::inserter(RBX_energies, RBX_energies.end()),
select2nd);
484 energies.insert(energies.end(), Hcal_energies.begin(), Hcal_energies.end());
485 energies.insert(energies.end(), Ecal_energies.begin(), Ecal_energies.end());
486 energies.insert(energies.end(), HO_energies.begin(), HO_energies.end());
487 std::sort(energies.begin(), energies.end(), greater<double>());
490 fEB_ = std::accumulate(EB_energies.begin(), EB_energies.end(), 0.);
491 fEE_ = std::accumulate(EE_energies.begin(), EE_energies.end(), 0.);
492 fHB_ = std::accumulate(HB_energies.begin(), HB_energies.end(), 0.);
493 fHE_ = std::accumulate(HE_energies.begin(), HE_energies.end(), 0.);
494 fHO_ = std::accumulate(HO_energies.begin(), HO_energies.end(), 0.);
495 fShort_ = std::accumulate(short_energies.begin(), short_energies.end(), 0.);
496 fLong_ = std::accumulate(long_energies.begin(), long_energies.end(), 0.);
497 subdet_energies.push_back(fEB_);
498 subdet_energies.push_back(fEE_);
499 subdet_energies.push_back(fHB_);
500 subdet_energies.push_back(fHE_);
501 subdet_energies.push_back(fHO_);
502 subdet_energies.push_back(fShort_);
503 subdet_energies.push_back(fLong_);
504 std::sort(subdet_energies.begin(), subdet_energies.end(), greater<double>());
505 if (
jet.energy() > 0) {
506 fEB_ /=
jet.energy();
507 fEE_ /=
jet.energy();
508 fHB_ /=
jet.energy();
509 fHE_ /=
jet.energy();
510 fHO_ /=
jet.energy();
511 fShort_ /=
jet.energy();
512 fLong_ /=
jet.energy();
514 if (fEB_ > 0 || fEE_ > 0 || fHB_ > 0 || fHE_ > 0 || fHO_ > 0 || fShort_ > 0 || fLong_ > 0)
515 edm::LogError(
"UnexpectedEventContents") <<
"Jet ID Helper found energy in subdetectors and jet E <= 0";
521 vector<subtower> &subtowers,
522 vector<subtower> &Ecal_subtowers,
523 vector<subtower> &Hcal_subtowers,
524 vector<subtower> &HO_subtowers,
525 vector<double> &HPD_energies,
526 vector<double> &RBX_energies,
529 Ecal_subtowers.clear();
530 Hcal_subtowers.clear();
531 HO_subtowers.clear();
532 HPD_energies.clear();
533 RBX_energies.clear();
535 std::map<int, double> HPD_energy_map, RBX_energy_map;
537 vector<CaloTowerPtr>
towers =
jet.getCaloConstituents();
540 cout <<
"classifyJetTowers started. # of towers found: " <<
nTowers << endl;
542 for (
int iTower = 0; iTower <
nTowers; iTower++) {
545 int nEM = 0, nHad = 0, nHO = 0;
546 const vector<DetId> &cellIDs =
tower->constituents();
547 int nCells = cellIDs.size();
549 cout <<
"tower #" << iTower <<
" has " <<
nCells <<
" cells. " 550 <<
"It's at iEta: " <<
tower->ieta() <<
", iPhi: " <<
tower->iphi() << endl;
552 for (
int iCell = 0; iCell <
nCells; ++iCell) {
567 double E_em =
tower->emEnergy();
569 Ecal_subtowers.push_back(
subtower(E_em, nEM));
571 double E_HO =
tower->outerEnergy();
573 HO_subtowers.push_back(
subtower(E_HO, nHO));
575 double E_had =
tower->hadEnergy();
577 Hcal_subtowers.push_back(
subtower(E_had, nHad));
582 int iPhi =
tower->iphi();
584 cout <<
"tower has E_had: " << E_had <<
" iEta: " <<
iEta <<
", iPhi: " << iPhi <<
" -> " << reg;
586 if (reg == HEneg || reg == HBneg || reg == HBpos || reg == HEpos) {
587 int oddnessIEta = HBHE_oddness(
iEta);
591 int iHPD = 100 * reg;
592 int iRBX = 100 * reg + ((iPhi + 1) % 72) / 4;
594 if ((reg == HEneg || reg == HEpos) &&
std::abs(
iEta) >= 21) {
595 if ((0
x1 & iPhi) == 0) {
596 edm::LogError(
"CodeAssumptionsViolated") <<
"Bug?! Jet ID code assumes no even iPhi recHits at HE edges";
599 bool boolOddnessIEta = oddnessIEta;
600 bool upperIPhi = ((iPhi % 4) == 1 || (iPhi % 4) == 2);
604 if (upperIPhi != boolOddnessIEta)
611 HPD_energy_map[iHPD] += E_had;
612 RBX_energy_map[iRBX] += E_had;
614 cout <<
" --> H[" << iHPD <<
"]=" << HPD_energy_map[iHPD] <<
", R[" << iRBX <<
"]=" << RBX_energy_map[iRBX];
625 HPD_energy_map.begin(), HPD_energy_map.end(), std::inserter(HPD_energies, HPD_energies.end()),
select2nd);
629 RBX_energy_map.begin(), RBX_energy_map.end(), std::inserter(RBX_energies, RBX_energies.end()),
select2nd);
633 subtowers.insert(subtowers.end(), Hcal_subtowers.begin(), Hcal_subtowers.end());
634 subtowers.insert(subtowers.end(), Ecal_subtowers.begin(), Ecal_subtowers.end());
635 subtowers.insert(subtowers.end(), HO_subtowers.begin(), HO_subtowers.end());
643 int ae = TMath::Abs(
iEta);
644 if (ae == 29 &&
depth == 1)
663 int ae = TMath::Abs(
iEta);
671 return unknown_region;
673 return unknown_region;
bool mergedDepth29(HcalDetId id) const
void setComment(std::string const &value)
ParameterDescriptionNode * ifValue(ParameterDescription< T > const &switchParameter, std::unique_ptr< ParameterDescriptionCases< T >> cases)
bool subtower_has_greater_E(reco::helper::JetIDHelper::subtower i, reco::helper::JetIDHelper::subtower j)
void classifyJetComponents(const edm::Event &event, const edm::EventSetup &setup, const reco::CaloJet &jet, std::vector< double > &energies, std::vector< double > &subdet_energies, std::vector< double > &Ecal_energies, std::vector< double > &Hcal_energies, std::vector< double > &HO_energies, std::vector< double > &HPD_energies, std::vector< double > &RBX_energies, double &LS_bad_energy, double &HF_OOT_energy, const int iDbg=0)
Jets made from CaloTowers.
std::vector< T >::const_iterator const_iterator
unsigned int nCarrying(double fraction, const std::vector< double > &descending_energies)
caConstants::TupleMultiplicity const CAHitNtupletGeneratorKernelsGPU::HitToTuple const cms::cuda::AtomicPairCounter GPUCACell const *__restrict__ uint32_t const *__restrict__ nCells
Log< level::Error, false > LogError
void calculate(const edm::Event &event, const edm::EventSetup &setup, const reco::CaloJet &jet, const int iDbg=0)
bool hasNonPositiveE(reco::helper::JetIDHelper::subtower x)
Region HBHE_region(uint32_t)
constexpr HcalSubdetector subdet() const
get the subdetector
Container::value_type value_type
Abs< T >::type abs(const T &t)
static double select2nd(std::map< int, double >::value_type const &pair)
void fillDescription(edm::ParameterSetDescription &iDesc)
void classifyJetTowers(const edm::Event &event, const reco::CaloJet &jet, std::vector< subtower > &subtowers, std::vector< subtower > &Ecal_subtowers, std::vector< subtower > &Hcal_subtowers, std::vector< subtower > &HO_subtowers, std::vector< double > &HPD_energies, std::vector< double > &RBX_energies, const int iDbg=0)
const_iterator end() const
int HBHE_oddness(int iEta, int depth)
iterator find(key_type k)
Log< level::Warning, false > LogWarning
unsigned int hitsInNCarrying(double fraction, const std::vector< subtower > &descending_towers)
constexpr int depth() const
get the tower depth