42 std::atomic<int> JetIDHelper::sanity_checks_left_{100};
79 approximatefHPD_ = -1.0;
80 approximatefRBX_ = -1.0;
82 fEB_ = fEE_ = fHB_ = fHE_ = fHO_ = fLong_ = fShort_ = -1.0;
83 fLS_ = fHFOOT_ = -1.0;
95 )->
setComment(
"If using RecHits to calculate the precise jet ID variables that need them, " 96 "their sources need to be specified");
115 if( E_Had + E_EM > 0 ) restrictedEMF_ = E_EM / ( E_EM + E_Had );
116 if( iDbg > 1 )
cout<<
"jet pT: "<<jet.
pt()<<
", eT: "<<jet.
et()<<
", E: " 117 <<jet.
energy()<<
" rEMF: "<<restrictedEMF_<<endl;
122 vector<subtower> subtowers, Ecal_subtowers, Hcal_subtowers, HO_subtowers;
123 vector<double> HPD_energies, RBX_energies;
125 classifyJetTowers( event, jet,
126 subtowers, Ecal_subtowers, Hcal_subtowers, HO_subtowers,
127 HPD_energies, RBX_energies, iDbg );
130 for (
unsigned int i=0;
i<subtowers.size(); ++
i)
cout<<
" "<<subtowers[
i].E<<
","<<subtowers[
i].Nhit;
132 for (
unsigned int i=0;
i<Ecal_subtowers.size(); ++
i)
cout<<
" "<<Ecal_subtowers[
i].E<<
","<<Ecal_subtowers[
i].Nhit;
134 for (
unsigned int i=0;
i<Hcal_subtowers.size(); ++
i)
cout<<
" "<<Hcal_subtowers[
i].E<<
","<<Hcal_subtowers[
i].Nhit;
136 for (
unsigned int i=0;
i<HO_subtowers.size(); ++
i)
cout<<
" "<<HO_subtowers[
i].E<<
","<<HO_subtowers[
i].Nhit;
138 for (
unsigned int i=0;
i<HPD_energies.size(); ++
i)
cout<<
" "<<HPD_energies[
i];
140 for (
unsigned int i=0;
i<RBX_energies.size(); ++
i)
cout<<
" "<<RBX_energies[
i];
145 hitsInN90_ = hitsInNCarrying( 0.9, subtowers );
146 nHCALTowers_ = Hcal_subtowers.size();
147 vector<subtower>::const_iterator it;
148 it = find_if( Ecal_subtowers.begin(), Ecal_subtowers.end(),
hasNonPositiveE );
149 nECALTowers_ = it - Ecal_subtowers.begin();
152 double max_HPD_energy = 0., max_RBX_energy = 0.;
153 std::vector<double>::const_iterator it_max_HPD_energy = std::max_element( HPD_energies.begin(), HPD_energies.end() );
154 std::vector<double>::const_iterator it_max_RBX_energy = std::max_element( RBX_energies.begin(), RBX_energies.end() );
155 if ( it_max_HPD_energy != HPD_energies.end() ) {
156 max_HPD_energy = *it_max_HPD_energy;
158 if ( it_max_RBX_energy != RBX_energies.end() ) {
159 max_RBX_energy = *it_max_RBX_energy;
162 if( !HPD_energies.empty() ) approximatefHPD_ = max_HPD_energy / jet.
energy();
163 if( !RBX_energies.empty() ) approximatefRBX_ = max_HPD_energy / jet.
energy();
170 vector<double> energies, subdet_energies, Ecal_energies, Hcal_energies, HO_energies;
171 double LS_bad_energy, HF_OOT_energy;
172 classifyJetComponents( event, setup, jet,
173 energies, subdet_energies, Ecal_energies, Hcal_energies, HO_energies,
174 HPD_energies, RBX_energies, LS_bad_energy, HF_OOT_energy, iDbg );
177 n90Hits_ = nCarrying( 0.9, energies );
181 if( !HPD_energies.empty() ) fHPD_ = max_HPD_energy / jet.
energy();
182 if( !RBX_energies.empty() ) fRBX_ = max_RBX_energy / jet.
energy();
183 if( !subdet_energies.empty() ) fSubDetector1_ = subdet_energies.at( 0 ) / jet.
energy();
184 if( subdet_energies.size() > 1 ) fSubDetector2_ = subdet_energies.at( 1 ) / jet.
energy();
185 if( subdet_energies.size() > 2 ) fSubDetector3_ = subdet_energies.at( 2 ) / jet.
energy();
186 if( subdet_energies.size() > 3 ) fSubDetector4_ = subdet_energies.at( 3 ) / jet.
energy();
187 fLS_ = LS_bad_energy / jet.
energy();
188 fHFOOT_ = HF_OOT_energy / jet.
energy();
190 if( iDbg > 0 && sanity_checks_left_.load(std::memory_order_acquire) > 0 ) {
191 --sanity_checks_left_;
192 double EH_sum = accumulate( Ecal_energies.begin(), Ecal_energies.end(), 0. );
193 EH_sum = accumulate( Hcal_energies.begin(), Hcal_energies.end(), EH_sum );
194 double EHO_sum = accumulate( HO_energies.begin(), HO_energies.end(), EH_sum );
197 <<
") does not match the total energy in the recHits ("<<EHO_sum
198 <<
", or without HO: "<<EH_sum<<
") . Are these the right recHits? " 199 <<
"Were jet energy corrections mistakenly applied before jet ID? A bug?";
200 if( iDbg > 1 )
cout<<
"Sanity check - E: "<<jet.
energy()<<
" =? EH_sum: "<<EH_sum<<
" / EHO_sum: "<<EHO_sum<<endl;
205 cout<<
"DBG - fHPD: "<<fHPD_<<
", fRBX: "<<fRBX_<<
", nh90: "<<n90Hits_<<
", fLS: "<<fLS_<<
", fHFOOT: "<<fHFOOT_<<endl;
206 cout<<
" -~fHPD: "<<approximatefHPD_<<
", ~fRBX: "<<approximatefRBX_
207 <<
", hits in n90: "<<hitsInN90_<<endl;
208 cout<<
" - nHCALTowers: "<<nHCALTowers_<<
", nECALTowers: "<<nECALTowers_
209 <<
"; subdet fractions: "<<fSubDetector1_<<
", "<<fSubDetector2_<<
", "<<fSubDetector3_<<
", "<<fSubDetector4_<<endl;
218 for(
unsigned int i = 0;
i < descending_energies.size(); ++
i ) totalE += descending_energies[
i ];
221 unsigned int NC = descending_energies.size();
224 for(
unsigned int i = descending_energies.size();
i > 0; --
i ) {
225 runningE += descending_energies[
i-1 ];
226 if (runningE < ( 1-fraction ) * totalE) NC =
i-1;
235 for(
unsigned int i = 0;
i < descending_towers.size(); ++
i ) totalE += descending_towers[
i ].E;
241 for(
unsigned int i = descending_towers.size();
i > 0; --
i ) {
242 runningE += descending_towers[
i-1 ].E;
243 if (runningE >= ( 1-fraction ) * totalE) NH += descending_towers[
i-1 ].Nhit;
250 vector< double > &energies,
251 vector< double > &subdet_energies,
252 vector< double > &Ecal_energies,
253 vector< double > &Hcal_energies,
254 vector< double > &HO_energies,
255 vector< double > &HPD_energies,
256 vector< double > &RBX_energies,
257 double& LS_bad_energy,
double& HF_OOT_energy,
const int iDbg )
259 energies.clear(); subdet_energies.clear(); Ecal_energies.clear(); Hcal_energies.clear(); HO_energies.clear();
260 HPD_energies.clear(); RBX_energies.clear();
261 LS_bad_energy = HF_OOT_energy = 0.;
266 std::map< int, double > HPD_energy_map, RBX_energy_map;
267 vector< double > EB_energies, EE_energies, HB_energies, HE_energies, short_energies, long_energies;
274 event.getByToken(input_HBHERecHits_token_, HBHERecHits );
275 event.getByToken(input_HORecHits_token_, HORecHits );
276 event.getByToken(input_HFRecHits_token_, HFRecHits );
277 event.getByToken(input_EBRecHits_token_, EBRecHits );
278 event.getByToken(input_EERecHits_token_, EERecHits );
279 if( iDbg > 2 )
cout<<
"# of rechits found - HBHE: "<<HBHERecHits->
size()
280 <<
", HO: "<<HORecHits->
size()<<
", HF: "<<HFRecHits->
size()
281 <<
", EB: "<<EBRecHits->
size()<<
", EE: "<<EERecHits->
size()<<endl;
285 if( iDbg > 9 )
cout<<
"In classifyJetComponents. # of towers found: "<<nTowers<<endl;
287 for(
int iTower = 0; iTower <
nTowers ; iTower++ ) {
292 if( iDbg )
cout<<
"tower #"<<iTower<<
" has "<<nCells<<
" cells. " 293 <<
"It's at iEta: "<<tower->
ieta()<<
", iPhi: "<<tower->
iphi()<<endl;
297 for(
int iCell = 0; iCell < nCells; ++iCell ) {
305 if (theRecHit == HORecHits->
end()) {
306 edm::LogWarning(
"UnexpectedEventContents")<<
"Can't find the HO recHit with ID: "<<HcalID;
309 hitE = theRecHit->energy();
310 HO_energies.push_back( hitE );
315 if( theRecHit == HFRecHits->
end() ) {
316 edm::LogWarning(
"UnexpectedEventContents")<<
"Can't find the HF recHit with ID: "<<HcalID;
319 hitE = theRecHit->energy();
321 <<
"hit #"<<iCell<<
" is HF , E: "<<hitE<<
" iEta: "<<theRecHit->id().ieta()
322 <<
", depth: "<<theRecHit->id().depth()<<
", iPhi: " 323 <<theRecHit->id().iphi();
325 if( HcalID.
depth() == 1 )
326 long_energies.push_back( hitE );
328 short_energies.push_back( hitE );
330 uint32_t
flags = theRecHit->flags();
337 if( iDbg>4 && flags )
cout<<
"flags: "<<flags
338 <<
" -> LS_bad_energy: "<<LS_bad_energy<<
", HF_OOT_energy: "<<HF_OOT_energy<<endl;
343 if( theRecHit == HBHERecHits->
end() ) {
344 edm::LogWarning(
"UnexpectedEventContents")<<
"Can't find the HBHE recHit with ID: "<<HcalID;
347 hitE = theRecHit->energy();
348 int iEta = theRecHit->id().
ieta();
349 int depth = theRecHit->id().depth();
350 Region region = HBHE_region( theRecHit->id().rawId() );
351 int hitIPhi = theRecHit->id().iphi();
352 if( iDbg>3 )
cout<<
"hit #"<<iCell<<
" is HBHE, E: "<<hitE<<
" iEta: "<<iEta
353 <<
", depth: "<<depth<<
", iPhi: "<<theRecHit->id().iphi()
359 int iHPD = 100 * region;
360 int iRBX = 100 * region + ((hitIPhi + 1) % 72) / 4;
363 if( (0
x1 & hitIPhi) == 0 ) {
364 edm::LogError(
"CodeAssumptionsViolated")<<
"Bug?! Jet ID code assumes no even iPhi recHits at HE edges";
367 bool oddnessIEta = HBHE_oddness( iEta, depth );
368 bool upperIPhi = (( hitIPhi%4 ) == 1 || ( hitIPhi%4 ) == 2);
372 if( upperIPhi != oddnessIEta ) ++hitIPhi;
378 HPD_energy_map[ iHPD ] += hitE;
379 RBX_energy_map[ iRBX ] += hitE;
380 if( iDbg > 5 )
cout<<
" --> H["<<iHPD<<
"]="<<HPD_energy_map[iHPD]
381 <<
", R["<<iRBX<<
"]="<<RBX_energy_map[iRBX];
382 if( iDbg > 1 )
cout<<endl;
384 if( region == HBneg || region == HBpos )
385 HB_energies.push_back( hitE );
387 HE_energies.push_back( hitE );
390 if( hitE == 0 )
edm::LogWarning(
"UnexpectedEventContents")<<
"HCal hitE==0? (or unknown subdetector?)";
396 int EcalNum = cellIDs[iCell].subdetId();
399 EBDetId EcalID = cellIDs[iCell];
402 <<
"Can't find the EB recHit with ID: "<<EcalID;
continue;}
403 hitE = theRecHit->energy();
404 EB_energies.push_back( hitE );
405 }
else if( EcalNum == 2 ){
406 EEDetId EcalID = cellIDs[iCell];
409 <<
"Can't find the EE recHit with ID: "<<EcalID;
continue;}
410 hitE = theRecHit->energy();
411 EE_energies.push_back( hitE );
413 if( hitE == 0 )
edm::LogWarning(
"UnexpectedEventContents")<<
"ECal hitE==0? (or unknown subdetector?)";
414 if( iDbg > 6 )
cout<<
"EcalNum: "<<EcalNum<<
" hitE: "<<hitE<<endl;
428 Hcal_energies.insert( Hcal_energies.end(), HB_energies.begin(), HB_energies.end() );
429 Hcal_energies.insert( Hcal_energies.end(), HE_energies.begin(), HE_energies.end() );
430 Hcal_energies.insert( Hcal_energies.end(), short_energies.begin(), short_energies.end() );
431 Hcal_energies.insert( Hcal_energies.end(), long_energies.begin(), long_energies.end() );
432 Ecal_energies.insert( Ecal_energies.end(), EB_energies.begin(), EB_energies.end() );
433 Ecal_energies.insert( Ecal_energies.end(), EE_energies.begin(), EE_energies.end() );
441 std::inserter (HPD_energies, HPD_energies.end()),
select2nd );
445 std::inserter (RBX_energies, RBX_energies.end()),
select2nd );
449 energies.insert( energies.end(), Hcal_energies.begin(), Hcal_energies.end() );
450 energies.insert( energies.end(), Ecal_energies.begin(), Ecal_energies.end() );
451 energies.insert( energies.end(), HO_energies.begin(), HO_energies.end() );
452 std::sort( energies.begin(), energies.end(), greater<double>() );
455 fEB_ = std::accumulate( EB_energies.begin(), EB_energies.end(), 0. );
456 fEE_ = std::accumulate( EE_energies.begin(), EE_energies.end(), 0. );
457 fHB_ = std::accumulate( HB_energies.begin(), HB_energies.end(), 0. );
458 fHE_ = std::accumulate( HE_energies.begin(), HE_energies.end(), 0. );
459 fHO_ = std::accumulate( HO_energies.begin(), HO_energies.end(), 0. );
460 fShort_ = std::accumulate( short_energies.begin(), short_energies.end(), 0. );
461 fLong_ = std::accumulate( long_energies.begin(), long_energies.end(), 0. );
462 subdet_energies.push_back( fEB_ );
463 subdet_energies.push_back( fEE_ );
464 subdet_energies.push_back( fHB_ );
465 subdet_energies.push_back( fHE_ );
466 subdet_energies.push_back( fHO_ );
467 subdet_energies.push_back( fShort_ );
468 subdet_energies.push_back( fLong_ );
469 std::sort( subdet_energies.begin(), subdet_energies.end(), greater<double>() );
479 if( fEB_ > 0 || fEE_ > 0 || fHB_ > 0 || fHE_ > 0 || fHO_ > 0 || fShort_ > 0 || fLong_ > 0 )
480 edm::LogError(
"UnexpectedEventContents")<<
"Jet ID Helper found energy in subdetectors and jet E <= 0";
485 vector< subtower > &subtowers,
486 vector< subtower > &Ecal_subtowers,
487 vector< subtower > &Hcal_subtowers,
488 vector< subtower > &HO_subtowers,
489 vector< double > &HPD_energies,
490 vector< double > &RBX_energies,
493 subtowers.clear(); Ecal_subtowers.clear(); Hcal_subtowers.clear(); HO_subtowers.clear();
494 HPD_energies.clear(); RBX_energies.clear();
496 std::map< int, double > HPD_energy_map, RBX_energy_map;
500 if( iDbg > 9 )
cout<<
"classifyJetTowers started. # of towers found: "<<nTowers<<endl;
502 for(
int iTower = 0; iTower <
nTowers ; iTower++ ) {
506 int nEM = 0, nHad = 0, nHO = 0;
508 int nCells = cellIDs.size();
509 if( iDbg )
cout<<
"tower #"<<iTower<<
" has "<<nCells<<
" cells. " 510 <<
"It's at iEta: "<<tower->
ieta()<<
", iPhi: "<<tower->
iphi()<<endl;
512 for(
int iCell = 0; iCell < nCells; ++iCell ) {
528 if( E_em != 0 ) Ecal_subtowers.push_back(
subtower( E_em, nEM ) );
531 if( E_HO != 0 ) HO_subtowers.push_back(
subtower( E_HO, nHO ) );
535 Hcal_subtowers.push_back(
subtower( E_had, nHad ) );
538 int iEta = tower->
ieta();
539 Region reg = region( iEta );
540 int iPhi = tower->
iphi();
541 if( iDbg>3 )
cout<<
"tower has E_had: "<<E_had<<
" iEta: "<<iEta
542 <<
", iPhi: "<<iPhi<<
" -> "<<reg;
544 if( reg == HEneg || reg == HBneg || reg == HBpos || reg == HEpos ) {
545 int oddnessIEta = HBHE_oddness( iEta );
546 if( oddnessIEta < 0 )
break;
548 int iHPD = 100 * reg;
549 int iRBX = 100 * reg + ((iPhi + 1) % 72) / 4;
551 if(( reg == HEneg || reg == HEpos ) &&
std::abs( iEta ) >= 21 ) {
552 if( (0
x1 & iPhi) == 0 ) {
554 "Bug?! Jet ID code assumes no even iPhi recHits at HE edges";
557 bool boolOddnessIEta = oddnessIEta;
558 bool upperIPhi = (( iPhi%4 ) == 1 || ( iPhi%4 ) == 2);
562 if( upperIPhi != boolOddnessIEta ) ++iPhi;
568 HPD_energy_map[ iHPD ] += E_had;
569 RBX_energy_map[ iRBX ] += E_had;
570 if( iDbg > 5 )
cout<<
" --> H["<<iHPD<<
"]="<<HPD_energy_map[iHPD]
571 <<
", R["<<iRBX<<
"]="<<RBX_energy_map[iRBX];
582 std::inserter (HPD_energies, HPD_energies.end()),
select2nd );
586 std::inserter (RBX_energies, RBX_energies.end()),
select2nd );
590 subtowers.insert( subtowers.end(), Hcal_subtowers.begin(), Hcal_subtowers.end() );
591 subtowers.insert( subtowers.end(), Ecal_subtowers.begin(), Ecal_subtowers.end() );
592 subtowers.insert( subtowers.end(), HO_subtowers.begin(), HO_subtowers.end() );
602 if( ae == 29 && depth == 1 ) ae += 1;
611 if(
id.ieta() < 0 )
return HEneg;
614 if(
id.subdet() ==
HcalBarrel &&
id.ieta() < 0)
return HBneg;
621 if( ae == 29 )
return -1;
627 if( iEta == 16 || iEta == -16 )
return unknown_region;
628 if( iEta == 29 || iEta == -29 )
return unknown_region;
629 if( iEta <= -30 )
return HFneg;
630 if( iEta >= 30 )
return HFpos;
631 if( iEta <= -17 )
return HEneg;
632 if( iEta >= 17 )
return HEpos;
633 if( iEta < 0 )
return HBneg;
float hadEnergyInHE() const
float emEnergyInEE() const
T getParameter(std::string const &) const
void setComment(std::string const &value)
bool subtower_has_greater_E(reco::helper::JetIDHelper::subtower i, reco::helper::JetIDHelper::subtower j)
bool mergedDepth29(HcalDetId id) const
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.
size_t constituentsSize() const
HcalSubdetector subdet() const
get the subdetector
std::vector< HORecHit >::const_iterator const_iterator
def setup(process, global_tag, zero_tesla=False)
float emEnergyInHF() const
std::vector< Variable::Flags > flags
double pt() const final
transverse momentum
unsigned int nCarrying(double fraction, const std::vector< double > &descending_energies)
virtual std::vector< CaloTowerPtr > getCaloConstituents() const
get all constituents
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)
float hadEnergyInHO() const
int depth() const
get the tower depth
Container::value_type value_type
double et() const final
transverse energy
float emEnergyInEB() const
ParameterDescriptionNode * ifValue(ParameterDescription< T > const &switchParameter, std::unique_ptr< ParameterDescriptionCases< T > > cases)
int ieta() const
get the cell ieta
double energy() const final
energy
Abs< T >::type abs(const T &t)
static double select2nd(std::map< int, double >::value_type const &pair)
void fillDescription(edm::ParameterSetDescription &iDesc)
const std::vector< DetId > & constituents() const
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)
float hadEnergyInHB() const
float hadEnergyInHF() const
double outerEnergy() const
unsigned int hitsInNCarrying(double fraction, const std::vector< subtower > &descending_towers)