CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Jet.cc
Go to the documentation of this file.
1 //
2 //
3 
8 
9 using namespace pat;
10 
12 Jet::Jet() :
13  PATObject<reco::Jet>(reco::Jet()),
14  embeddedCaloTowers_(false),
15  embeddedPFCandidates_(false),
16  jetCharge_(0.)
17 {
18 }
19 
21 Jet::Jet(const reco::Jet & aJet) :
22  PATObject<reco::Jet>(aJet),
23  embeddedCaloTowers_(false),
24  embeddedPFCandidates_(false),
25  jetCharge_(0.0)
26 {
27  tryImportSpecific(aJet);
28 }
29 
31 Jet::Jet(const edm::Ptr<reco::Jet> & aJetRef) :
32  PATObject<reco::Jet>(aJetRef),
33  embeddedCaloTowers_(false),
34  embeddedPFCandidates_(false),
35  jetCharge_(0.0)
36 {
37  tryImportSpecific(*aJetRef);
38 }
39 
41 Jet::Jet(const edm::RefToBase<reco::Jet> & aJetRef) :
42  PATObject<reco::Jet>(aJetRef),
43  embeddedCaloTowers_(false),
44  embeddedPFCandidates_(false),
45  jetCharge_(0.0)
46 {
47  tryImportSpecific(*aJetRef);
48 }
49 
50 std::ostream&
51 reco::operator<<(std::ostream& out, const pat::Jet& obj)
52 {
53  if(!out) return out;
54 
55  out << "\tpat::Jet: ";
56  out << std::setiosflags(std::ios::right);
57  out << std::setiosflags(std::ios::fixed);
58  out << std::setprecision(3);
59  out << " E/pT/eta/phi "
60  << obj.energy()<<"/"
61  << obj.pt()<<"/"
62  << obj.eta()<<"/"
63  << obj.phi();
64  return out;
65 }
66 
68 void Jet::tryImportSpecific(const reco::Jet& source)
69 {
70  const std::type_info & type = typeid(source);
71  if( type == typeid(reco::CaloJet) ){
72  specificCalo_.push_back( (static_cast<const reco::CaloJet&>(source)).getSpecific() );
73  } else if( type == typeid(reco::JPTJet) ){
74  reco::JPTJet const & jptJet = static_cast<reco::JPTJet const &>(source);
75  specificJPT_.push_back( jptJet.getSpecific() );
76  reco::CaloJet const * caloJet = 0;
77  if ( jptJet.getCaloJetRef().isNonnull() && jptJet.getCaloJetRef().isAvailable() ) {
78  caloJet = dynamic_cast<reco::CaloJet const *>( jptJet.getCaloJetRef().get() );
79  }
80  if ( caloJet != 0 ) {
81  specificCalo_.push_back( caloJet->getSpecific() );
82  }
83  else {
84  edm::LogWarning("OptionalProductNotFound") << " in pat::Jet, Attempted to add Calo Specifics to JPT Jets, but failed."
85  << " Jet ID for JPT Jets will not be available for you." << std::endl;
86  }
87  } else if( type == typeid(reco::PFJet) ){
88  specificPF_.push_back( (static_cast<const reco::PFJet&>(source)).getSpecific() );
89  }
90 }
91 
93 Jet::~Jet() {
94 }
95 
97 
98 CaloTowerPtr Jet::getCaloConstituent (unsigned fIndex) const {
99  if (embeddedCaloTowers_) {
100  // Refactorized PAT access
101  if ( caloTowersFwdPtr_.size() > 0 ) {
102  return (fIndex < caloTowersFwdPtr_.size() ?
103  caloTowersFwdPtr_[fIndex].ptr() : CaloTowerPtr());
104  }
105  // Compatibility PAT access
106  else {
107  if ( caloTowers_.size() > 0 ) {
108  return (fIndex < caloTowers_.size() ?
109  CaloTowerPtr(&caloTowers_, fIndex) : CaloTowerPtr());
110 
111  }
112  }
113  }
114  // Non-embedded access
115  else {
116  Constituent dau = daughterPtr (fIndex);
117  const CaloTower* caloTower = dynamic_cast <const CaloTower*> (dau.get());
118  if (caloTower != 0) {
119  return CaloTowerPtr(dau.id(), caloTower, dau.key() );
120  }
121  else {
122  throw cms::Exception("Invalid Constituent") << "CaloJet constituent is not of CaloTower type";
123  }
124 
125  }
126 
127  return CaloTowerPtr ();
128 }
129 
130 
131 
132 std::vector<CaloTowerPtr> const & Jet::getCaloConstituents () const {
133  if ( !caloTowersTemp_.isSet() || caloTowers_.size() > 0 ) cacheCaloTowers();
134  return *caloTowersTemp_;
135 }
136 
137 
139 
140 reco::PFCandidatePtr Jet::getPFConstituent (unsigned fIndex) const {
141  if (embeddedPFCandidates_) {
142  // Refactorized PAT access
143  if ( pfCandidatesFwdPtr_.size() > 0 ) {
144  return (fIndex < pfCandidatesFwdPtr_.size() ?
145  pfCandidatesFwdPtr_[fIndex].ptr() : reco::PFCandidatePtr());
146  }
147  // Compatibility PAT access
148  else {
149  if ( pfCandidates_.size() > 0 ) {
150  return (fIndex < pfCandidates_.size() ?
151  reco::PFCandidatePtr(&pfCandidates_, fIndex) : reco::PFCandidatePtr());
152 
153  }
154  }
155  }
156  // Non-embedded access
157  else {
158  Constituent dau = daughterPtr (fIndex);
159  const reco::PFCandidate* pfCandidate = dynamic_cast <const reco::PFCandidate*> (dau.get());
160  if (pfCandidate) {
161  return reco::PFCandidatePtr(dau.id(), pfCandidate, dau.key() );
162  }
163  else {
164  throw cms::Exception("Invalid Constituent") << "PFJet constituent is not of PFCandidate type";
165  }
166 
167  }
168 
169  return reco::PFCandidatePtr ();
170 }
171 
172 std::vector<reco::PFCandidatePtr> const & Jet::getPFConstituents () const {
173  if ( !pfCandidatesTemp_.isSet() || pfCandidates_.size() > 0 ) cachePFCandidates();
174  return *pfCandidatesTemp_;
175 }
176 
177 const reco::Candidate * Jet::daughter(size_t i) const {
178  if (isCaloJet() || isJPTJet() ) {
179  if ( embeddedCaloTowers_ ) {
180  if ( caloTowersFwdPtr_.size() > 0 ) return caloTowersFwdPtr_[i].get();
181  else if ( caloTowers_.size() > 0 ) return &caloTowers_[i];
182  else return reco::Jet::daughter(i);
183  }
184  }
185  if (isPFJet()) {
186  if ( embeddedPFCandidates_ ) {
187  if ( pfCandidatesFwdPtr_.size() > 0 ) return pfCandidatesFwdPtr_[i].get();
188  else if ( pfCandidates_.size() > 0 ) return &pfCandidates_[i];
189  else return reco::Jet::daughter(i);
190  }
191  }
192  return reco::Jet::daughter(i);
193 }
194 
195 size_t Jet::numberOfDaughters() const {
196  if (isCaloJet() || isJPTJet()) {
197  if ( embeddedCaloTowers_ ) {
198  if ( caloTowersFwdPtr_.size() > 0 ) return caloTowersFwdPtr_.size();
199  else if ( caloTowers_.size() > 0 ) return caloTowers_.size();
200  else return reco::Jet::numberOfDaughters();
201  }
202  }
203  if (isPFJet()) {
204  if ( embeddedPFCandidates_ ) {
205  if ( pfCandidatesFwdPtr_.size() > 0 ) return pfCandidatesFwdPtr_.size();
206  else if ( pfCandidates_.size() > 0 ) return pfCandidates_.size();
207  else return reco::Jet::numberOfDaughters();
208  }
209  }
211 }
212 
214 const reco::GenJet * Jet::genJet() const {
215  if (genJet_.size()) return &(genJet_.front());
216  else if ( genJetRef_.size() ) return genJetRef_[0].get();
217  else return genJetFwdRef_.get();
218 }
219 
221 int Jet::partonFlavour() const {
222  return jetFlavourInfo_.getPartonFlavour();
223 }
224 
226 int Jet::hadronFlavour() const {
227  return jetFlavourInfo_.getHadronFlavour();
228 }
229 
231 const reco::JetFlavourInfo & Jet::jetFlavourInfo() const {
232  return jetFlavourInfo_;
233 }
234 
236 
237 // initialize the jet to a given JEC level during creation starting from Uncorrected
238 void Jet::initializeJEC(unsigned int level, const JetCorrFactors::Flavor& flavor, unsigned int set)
239 {
240  currentJECSet(set);
241  currentJECLevel(level);
242  currentJECFlavor(flavor);
243  setP4(jec_[set].correction(level, flavor)*p4());
244 }
245 
247 int Jet::jecSet(const std::string& set) const
248 {
249  for(std::vector<pat::JetCorrFactors>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor)
250  if( corrFactor->jecSet()==set ){ return (corrFactor-jec_.begin()); }
251  return -1;
252 }
253 
255 const std::vector<std::string> Jet::availableJECSets() const
256 {
257  std::vector<std::string> sets;
258  for(std::vector<pat::JetCorrFactors>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor)
259  sets.push_back(corrFactor->jecSet());
260  return sets;
261 }
262 
263 const std::vector<std::string> Jet::availableJECLevels(const int& set) const
264 {
265  return set>=0 ? jec_.at(set).correctionLabels() : std::vector<std::string>();
266 }
267 
270 float Jet::jecFactor(const std::string& level, const std::string& flavor, const std::string& set) const
271 {
272  for(unsigned int idx=0; idx<jec_.size(); ++idx){
273  if(set.empty() || jec_.at(idx).jecSet()==set){
274  if(jec_[idx].jecLevel(level)>=0){
275  return jecFactor(jec_[idx].jecLevel(level), jec_[idx].jecFlavor(flavor), idx);
276  }
277  else{
278  throw cms::Exception("InvalidRequest") << "This JEC level " << level << " does not exist. \n";
279  }
280  }
281  }
282  throw cms::Exception("InvalidRequest") << "This jet does not carry any jet energy correction factor information \n"
283  << "for a jet energy correction set with label " << set << "\n";
284 }
285 
288 float Jet::jecFactor(const unsigned int& level, const JetCorrFactors::Flavor& flavor, const unsigned int& set) const
289 {
290  if(!jecSetsAvailable()){
291  throw cms::Exception("InvalidRequest") << "This jet does not carry any jet energy correction factor information \n";
292  }
293  if(!jecSetAvailable(set)){
294  throw cms::Exception("InvalidRequest") << "This jet does not carry any jet energy correction factor information \n"
295  << "for a jet energy correction set with index " << set << "\n";
296  }
297  return jec_.at(set).correction(level, flavor)/jec_.at(currentJECSet_).correction(currentJECLevel_, currentJECFlavor_);
298 }
299 
302 Jet Jet::correctedJet(const std::string& level, const std::string& flavor, const std::string& set) const
303 {
304  // rescale p4 of the jet; the update of current values is
305  // done within the called jecFactor function
306  for(unsigned int idx=0; idx<jec_.size(); ++idx){
307  if(set.empty() || jec_.at(idx).jecSet()==set){
308  if(jec_[idx].jecLevel(level)>=0){
309  return correctedJet(jec_[idx].jecLevel(level), jec_[idx].jecFlavor(flavor), idx);
310  }
311  else{
312  throw cms::Exception("InvalidRequest") << "This JEC level " << level << " does not exist. \n";
313  }
314  }
315  }
316  throw cms::Exception("InvalidRequest") << "This JEC set " << set << " does not exist. \n";
317 }
318 
321 Jet Jet::correctedJet(const unsigned int& level, const JetCorrFactors::Flavor& flavor, const unsigned int& set) const
322 {
323  Jet correctedJet(*this);
324  //rescale p4 of the jet
325  correctedJet.setP4(jecFactor(level, flavor, set)*p4());
326  // update current level, flavor and set
327  correctedJet.currentJECSet(set); correctedJet.currentJECLevel(level); correctedJet.currentJECFlavor(flavor);
328  return correctedJet;
329 }
330 
331 
333 
334 const std::vector<std::pair<std::string, float> > & Jet::getPairDiscri() const {
335  return pairDiscriVector_;
336 }
337 
339 float Jet::bDiscriminator(const std::string & aLabel) const {
340  float discriminator = -1000.;
341  const std::string & theLabel = ((aLabel == "" || aLabel == "default")) ? "trackCountingHighEffBJetTags" : aLabel;
342  for(unsigned int i=0; i!=pairDiscriVector_.size(); i++){
343  if(pairDiscriVector_[i].first == theLabel){
344  discriminator = pairDiscriVector_[i].second;
345  }
346  }
347  return discriminator;
348 }
349 
350 const reco::BaseTagInfo * Jet::tagInfo(const std::string &label) const {
351  std::vector<std::string>::const_iterator it = std::find(tagInfoLabels_.begin(), tagInfoLabels_.end(), label);
352  if (it != tagInfoLabels_.end()) {
353  if ( tagInfosFwdPtr_.size() > 0 ) return tagInfosFwdPtr_[it - tagInfoLabels_.begin()].get();
354  else if ( tagInfos_.size() > 0 ) return & tagInfos_[it - tagInfoLabels_.begin()];
355  return 0;
356  }
357  return 0;
358 }
359 
360 
361 const reco::CandIPTagInfo *
362 Jet::tagInfoCandIP(const std::string &label) const {
363  return tagInfoByTypeOrLabel<reco::CandIPTagInfo>(label);
364 }
365 
366 const reco::TrackIPTagInfo *
367 Jet::tagInfoTrackIP(const std::string &label) const {
368  return tagInfoByTypeOrLabel<reco::TrackIPTagInfo>(label);
369 }
370 
372 Jet::tagInfoCandSoftLepton(const std::string &label) const {
373  return tagInfoByTypeOrLabel<reco::CandSoftLeptonTagInfo>(label);
374 }
375 
377 Jet::tagInfoSoftLepton(const std::string &label) const {
378  return tagInfoByTypeOrLabel<reco::SoftLeptonTagInfo>(label);
379 }
380 
382 Jet::tagInfoCandSecondaryVertex(const std::string &label) const {
383  return tagInfoByTypeOrLabel<reco::CandSecondaryVertexTagInfo>(label);
384 }
385 
387 Jet::tagInfoSecondaryVertex(const std::string &label) const {
388  return tagInfoByTypeOrLabel<reco::SecondaryVertexTagInfo>(label);
389 }
390 
391 void
392 Jet::addTagInfo(const std::string &label,
394  std::string::size_type idx = label.find("TagInfos");
395  if (idx == std::string::npos) {
396  tagInfoLabels_.push_back(label);
397  } else {
398  tagInfoLabels_.push_back(label.substr(0,idx));
399  }
400  tagInfosFwdPtr_.push_back(info);
401 }
402 
403 
404 
406 float Jet::jetCharge() const {
407  return jetCharge_;
408 }
409 
411 const reco::TrackRefVector & Jet::associatedTracks() const {
412  return associatedTracks_;
413 }
414 
416 void Jet::setAssociatedTracks(const reco::TrackRefVector &tracks) {
417  associatedTracks_ = tracks;
418 }
419 
421 void Jet::setCaloTowers(const CaloTowerFwdPtrCollection & caloTowers) {
422  caloTowersFwdPtr_.reserve(caloTowers.size());
423  for(auto const& tower : caloTowers) {
424  caloTowersFwdPtr_.push_back( tower );
425  }
426  embeddedCaloTowers_ = true;
427  caloTowersTemp_.reset();
428 }
429 
430 
432 void Jet::setPFCandidates(const PFCandidateFwdPtrCollection & pfCandidates) {
433  pfCandidatesFwdPtr_.reserve(pfCandidates.size());
434  for(auto const& cand : pfCandidates) {
435  pfCandidatesFwdPtr_.push_back(cand);
436  }
437  embeddedPFCandidates_ = true;
438  pfCandidatesTemp_.reset();
439 }
440 
441 
443 void Jet::setGenJetRef(const edm::FwdRef<reco::GenJetCollection> & gj)
444 {
445  genJetFwdRef_ = gj;
446 }
447 
448 
449 
451 void Jet::setPartonFlavour(int partonFl) {
452  jetFlavourInfo_.setPartonFlavour(partonFl);
453 }
454 
456 void Jet::setHadronFlavour(int hadronFl) {
457  jetFlavourInfo_.setHadronFlavour(hadronFl);
458 }
459 
461 void Jet::setJetFlavourInfo(const reco::JetFlavourInfo & jetFlavourInfo) {
462  jetFlavourInfo_ = jetFlavourInfo;
463 }
464 
466 void Jet::addBDiscriminatorPair(const std::pair<std::string, float> & thePair) {
467  pairDiscriVector_.push_back(thePair);
468 }
469 
471 void Jet::setJetCharge(float jetCharge) {
472  jetCharge_ = jetCharge;
473 }
474 
475 
476 
478 void Jet::cacheCaloTowers() const {
479  // Clear the cache
480  // Here is where we've embedded constituents
481  std::unique_ptr<std::vector<CaloTowerPtr>> caloTowersTemp{ new std::vector<CaloTowerPtr>{}};
482  if ( embeddedCaloTowers_ ) {
483  // Refactorized PAT access
484  if ( caloTowersFwdPtr_.size() > 0 ) {
485  caloTowersTemp->reserve(caloTowersFwdPtr_.size());
486  for ( CaloTowerFwdPtrVector::const_iterator ibegin=caloTowersFwdPtr_.begin(),
487  iend = caloTowersFwdPtr_.end(),
488  icalo = ibegin;
489  icalo != iend; ++icalo ) {
490  caloTowersTemp->emplace_back( icalo->ptr() );
491  }
492  }
493  // Compatibility access
494  else if ( caloTowers_.size() > 0 ) {
495  caloTowersTemp->reserve(caloTowers_.size());
496  for ( CaloTowerCollection::const_iterator ibegin=caloTowers_.begin(),
497  iend = caloTowers_.end(),
498  icalo = ibegin;
499  icalo != iend; ++icalo ) {
500  caloTowersTemp->emplace_back( &caloTowers_, icalo - ibegin );
501  }
502  }
503  }
504  // Non-embedded access
505  else {
506  const auto nDaughters = numberOfDaughters();
507  caloTowersTemp->reserve(nDaughters);
508  for ( unsigned fIndex = 0; fIndex < nDaughters; ++fIndex ) {
509  Constituent const & dau = daughterPtr (fIndex);
510  const CaloTower* caloTower = dynamic_cast <const CaloTower*> (dau.get());
511  if (caloTower) {
512  caloTowersTemp->emplace_back( dau.id(), caloTower,dau.key() );
513  }
514  else {
515  throw cms::Exception("Invalid Constituent") << "CaloJet constituent is not of CaloTower type";
516  }
517  }
518  }
519  caloTowersTemp_.set(std::move(caloTowersTemp));
520 }
521 
523 void Jet::cachePFCandidates() const {
524 
525  std::unique_ptr<std::vector<reco::PFCandidatePtr>> pfCandidatesTemp{ new std::vector<reco::PFCandidatePtr>{}};
526  // Here is where we've embedded constituents
527  if ( embeddedPFCandidates_ ) {
528  // Refactorized PAT access
529  if ( pfCandidatesFwdPtr_.size() > 0 ) {
530  pfCandidatesTemp->reserve(pfCandidatesFwdPtr_.size());
531  for ( PFCandidateFwdPtrCollection::const_iterator ibegin=pfCandidatesFwdPtr_.begin(),
532  iend = pfCandidatesFwdPtr_.end(),
533  ipf = ibegin;
534  ipf != iend; ++ipf ) {
535  pfCandidatesTemp->emplace_back( ipf->ptr() );
536  }
537  }
538  // Compatibility access
539  else if ( pfCandidates_.size() > 0 ) {
540  pfCandidatesTemp->reserve(pfCandidates_.size());
541  for ( reco::PFCandidateCollection::const_iterator ibegin=pfCandidates_.begin(),
542  iend = pfCandidates_.end(),
543  ipf = ibegin;
544  ipf != iend; ++ipf ) {
545  pfCandidatesTemp->emplace_back( &pfCandidates_, ipf - ibegin );
546  }
547  }
548  }
549  // Non-embedded access
550  else {
551  const auto nDaughters = numberOfDaughters();
552  pfCandidatesTemp->reserve(nDaughters);
553  for ( unsigned fIndex = 0; fIndex < nDaughters; ++fIndex ) {
554  Constituent const & dau = daughterPtr (fIndex);
555  const reco::PFCandidate* pfCandidate = dynamic_cast <const reco::PFCandidate*> (dau.get());
556  if (pfCandidate) {
557  pfCandidatesTemp->emplace_back( dau.id(), pfCandidate,dau.key() );
558  }
559  else {
560  throw cms::Exception("Invalid Constituent") << "PFJet constituent is not of PFCandidate type";
561  }
562  }
563  }
564  // Set the cache
565  pfCandidatesTemp_.set(std::move(pfCandidatesTemp));
566 }
type
Definition: HCALResponse.h:21
value_type const * get() const
Definition: RefToBase.h:213
Jet()
default constructor
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
Jets made from CaloTowers.
Definition: CaloJet.h:29
virtual float pt() const
transverse momentum
bool isAvailable() const
Definition: RefToBase.h:113
const Specific & getSpecific() const
block accessors
Definition: JPTJet.h:133
virtual float phi() const
momentum azimuthal angle
Base class for all types of Jets.
Definition: Jet.h:20
std::vector< CaloTower >::const_iterator const_iterator
virtual void setP4(const LorentzVector &p4)
set 4-momentum
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:280
const edm::RefToBase< reco::Jet > & getCaloJetRef() const
Definition: JPTJet.h:130
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
uint16_t size_type
Jets made from PFObjects.
Definition: PFJet.h:21
std::vector< PFCandidatePtr > pfCandidates(const PFJet &jet, int particleId, bool sort=true)
virtual size_t numberOfDaughters() const
number of daughters
virtual double energy() const
energy
virtual size_t numberOfDaughters() const
number of daughters
virtual const Candidate * daughter(size_type) const
return daughter at a given position (throws an exception)
std::ostream & operator<<(std::ostream &, BeamSpot beam)
Definition: BeamSpot.cc:71
virtual float eta() const
momentum pseudorapidity
Jets made from CaloJets corrected for ZSP and tracks.
Definition: JPTJet.h:29
edm::Ptr< CaloTower > CaloTowerPtr
Definition: CaloTowerFwd.h:12
Class storing the jet flavour information.
T get() const
get a component
Jets made from MC generator particles.
Definition: GenJet.h:24
bool first
Definition: L1TdeRCT.cc:75
Container::value_type value_type
tuple out
Definition: dbtoconf.py:99
tuple tracks
Definition: testEve_cfg.py:39
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
std::vector< edm::FwdPtr< CaloTower > > CaloTowerFwdPtrCollection
Definition: Jet.h:73
std::vector< edm::FwdPtr< reco::PFCandidate > > PFCandidateFwdPtrCollection
Definition: Jet.h:72
Analysis-level calorimeter jet class.
Definition: Jet.h:76
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
Jet()
Definition: Jet.h:16
Templated PAT object container.
Definition: PATObject.h:41
tuple level
Definition: testEve_cfg.py:34
volatile std::atomic< bool > shutdown_flag false
edm::Ptr< PFCandidate > PFCandidatePtr
persistent Ptr to a PFCandidate
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
~Jet()
Definition: Jet.cc:24
static std::string const source
Definition: EdmProvDump.cc:43
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
const Specific & getSpecific() const
Definition: CaloJet.h:149