CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFCandidate.cc
Go to the documentation of this file.
2 //#include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
3 
11 
13 
14 #include <ostream>
15 #include <iomanip>
16 
17 using namespace reco;
18 using namespace std;
19 
20 
21 
22 const float PFCandidate::bigMva_ = -999.;
23 
24 
26 
27 
28 
29 
31  ecalERatio_(1.),
32  hcalERatio_(1.),
33  hoERatio_(1.),
34  rawEcalEnergy_(0.),
35  rawHcalEnergy_(0.),
36  rawHoEnergy_(0.),
37  ps1Energy_(0.),
38  ps2Energy_(0.),
39  flags_(0),
40  deltaP_(0.),
41  vertexType_(kCandVertex),
42  mva_e_pi_(bigMva_),
43  mva_e_mu_(bigMva_),
44  mva_pi_mu_(bigMva_),
45  mva_nothing_gamma_(bigMva_),
46  mva_nothing_nh_(bigMva_),
47  mva_gamma_nh_(bigMva_),
48  getter_(0),storedRefsBitPattern_(0)
49 {
50 
52  refsInfo_.reserve(3);
53 }
54 
55 
57  *this = *sourcePtr;
58  sourcePtr_ = sourcePtr;
59 }
60 
61 
63  const LorentzVector & p4,
64  ParticleType partId ) :
65 
66  CompositeCandidate(charge, p4),
67  ecalERatio_(1.),
68  hcalERatio_(1.),
69  hoERatio_(1.),
70  rawEcalEnergy_(0.),
71  rawHcalEnergy_(0.),
72  rawHoEnergy_(0.),
73  ps1Energy_(0.),
74  ps2Energy_(0.),
75  flags_(0),
76  deltaP_(0.),
77  vertexType_(kCandVertex),
78  mva_e_pi_(bigMva_),
79  mva_e_mu_(bigMva_),
80  mva_pi_mu_(bigMva_),
81  mva_nothing_gamma_(bigMva_),
82  mva_nothing_nh_(bigMva_),
83  mva_gamma_nh_(bigMva_),
84  getter_(0),storedRefsBitPattern_(0)
85 {
86  refsInfo_.reserve(3);
88  elementsStorage_.reserve(10);
89 
90  // proceed with various consistency checks
91 
92  // charged candidate: track ref and charge must be non null
93  if( partId == h ||
94  partId == e ||
95  partId == mu ) {
96 
97  if( charge == 0 ) {
98  string err;
99  err+="Attempt to construct a charged PFCandidate with a zero charge";
100  throw cms::Exception("InconsistentValue",
101  err.c_str() );
102  }
103  }
104  else {
105  if( charge ) {
106  string err;
107  err += "Attempt to construct a neutral PFCandidate ";
108  err += "with a non-zero charge";
109  throw cms::Exception("InconsistentValue",
110  err.c_str() );
111  }
112  }
113  setPdgId( translateTypeToPdgId( partId ) );
114 }
115 
117 
119  return new PFCandidate( * this );
120 }
121 
122 
124  unsigned elementIndex ) {
125  //elementsInBlocks_.push_back( make_pair(blockref.key(), elementIndex) );
126  if (blocksStorage_.size()==0)
127  blocksStorage_ =Blocks(blockref.id());
128  blocksStorage_.push_back(blockref);
129  elementsStorage_.push_back(elementIndex);
130 }
131 
132 
133 
135  switch (std::abs(pdgid)) {
136  case 211: return h;
137  case 11: return e;
138  case 13: return mu;
139  case 22: return gamma;
140  case 130: return h0;
141  case 1: return h_HF;
142  case 2: return egamma_HF;
143  case 0: return X;
144  default: return X;
145  }
146 }
147 
149 
150  int thecharge = charge();
151 
152  switch( type ) {
153  case h: return thecharge*211; // pi+
154  case e: return thecharge*(-11);
155  case mu: return thecharge*(-13);
156  case gamma: return 22;
157  case h0: return 130; // K_L0
158  case h_HF: return 1; // dummy pdg code
159  case egamma_HF: return 2; // dummy pdg code
160  case X:
161  default: return 0;
162  }
163 }
164 
165 
167  setPdgId( translateTypeToPdgId( type ) );
168 }
169 
170 
171 bool PFCandidate::overlap(const reco::Candidate & other) const {
172  CandidatePtr myPtr = sourceCandidatePtr(0);
173  if (myPtr.isNull()) return false;
174  for (size_t i = 0, n = other.numberOfSourceCandidatePtrs(); i < n; ++i) {
175  CandidatePtr otherPtr = other.sourceCandidatePtr(i);
176  if ((otherPtr == myPtr) ||
177  (sourcePtr_.isNonnull() && otherPtr.isNonnull() && sourcePtr_->overlap(*otherPtr))) {
178  return true;
179  }
180  }
181  return false;
182 }
183 
184 
185 
186 
187 
188 void PFCandidate::rescaleMomentum( double rescaleFactor ) {
189  LorentzVector rescaledp4 = p4();
190  rescaledp4 *= rescaleFactor;
191  setP4( rescaledp4 );
192 }
193 
194 
195 void PFCandidate::setFlag(Flags theFlag, bool value) {
196 
197  if(value)
198  flags_ = flags_ | (1<<theFlag);
199  else
200  flags_ = flags_ ^ (1<<theFlag);
201 }
202 
203 
204 
205 bool PFCandidate::flag(Flags theFlag) const {
206 
207  return (flags_>>theFlag) & 1;
208 }
209 
210 
211 
212 
213 ostream& reco::operator<<(ostream& out,
214  const PFCandidate& c ) {
215 
216  if(!out) return out;
217 
218  out<<"\tPFCandidate type: "<<c.particleId();
219  out<<setiosflags(ios::right);
220  out<<setiosflags(ios::fixed);
221  out<<setprecision(3);
222  out<<" E/pT/eta/phi "
223  <<c.energy()<<"/"
224  <<c.pt()<<"/"
225  <<c.eta()<<"/"
226  <<c.phi();
227  if( c.flag( PFCandidate::T_FROM_DISP ) ) out<<", T_FROM_DISP" << endl;
228  else if( c.flag( PFCandidate::T_TO_DISP ) ) out<<", T_TO_DISP" << endl;
229  else if( c.flag( PFCandidate::T_FROM_GAMMACONV ) ) out<<", T_FROM_GAMMACONV" << endl;
230  else if( c.flag( PFCandidate::GAMMA_TO_GAMMACONV ) ) out<<", GAMMA_TO_GAMMACONV" << endl;
231 
232  out<<", blocks/iele: ";
233 
235  for(unsigned i=0; i<eleInBlocks.size(); i++) {
236  PFBlockRef blockRef = eleInBlocks[i].first;
237  unsigned indexInBlock = eleInBlocks[i].second;
238 
239  out<<"("<<blockRef.key()<<"|"<<indexInBlock<<"), ";
240  }
241 
242  out<<" source:"<<c.sourcePtr_.id()<<"/"<<c.sourcePtr_.key();
243 
244 // PFBlockRef blockRef = c.block();
245 // int blockid = blockRef.key();
246 // const edm::OwnVector< reco::PFBlockElement >& elements = c.elements();
247 // out<< "\t# of elements " << elements.size()
248 // <<" from block " << blockid << endl;
249 
250 // // print each element in turn
251 
252 // for(unsigned ie=0; ie<elements.size(); ie++) {
253 // out<<"\t"<< elements[ie] <<endl;
254 // }
255 
256  // Improved printout for electrons if PFCandidateElectronExtra is available
258  out << std::endl << *(c.electronExtraRef()) ;
259  }
260  out<<resetiosflags(ios::right|ios::fixed);
261  return out;
262 }
263 
264 static unsigned long long bitPackRefInfo(const edm::RefCore& iCore, size_t iIndex){
265  unsigned long long bitPack = iIndex;
266  bitPack |= static_cast<unsigned long long>(iCore.id().productIndex())<<32;
267  bitPack |= static_cast<unsigned long long>(iCore.id().processIndex())<<48;
268  return bitPack;
269 }
270 
271 void PFCandidate::storeRefInfo(unsigned int iMask, unsigned int iBit, bool iIsValid,
272  const edm::RefCore& iCore, size_t iKey,
273  const edm::EDProductGetter* iGetter) {
274 
275  size_t index = s_refsBefore[storedRefsBitPattern_ & iMask];
276  if ( 0 == getter_) {
277  getter_ = iGetter;
278  }
279 
280  if(iIsValid) {
281  if(0 == (storedRefsBitPattern_ & iBit) ) {
282  refsInfo_.insert(refsInfo_.begin()+index, bitPackRefInfo(iCore,iKey));
283  if (iGetter==0)
284  refsCollectionCache_.insert(refsCollectionCache_.begin()+index,(void*)iCore.productPtr());
285  else
287  } else {
288  assert(refsInfo_.size()>index);
289  *(refsInfo_.begin()+index)=bitPackRefInfo(iCore,iKey);
290  if (iGetter==0)
291  *(refsCollectionCache_.begin()+index)=(void*)iCore.productPtr();
292  else
293  *(refsCollectionCache_.begin()+index)=0;
294 
295  }
296  storedRefsBitPattern_ |= iBit;
297  } else{
298  if( storedRefsBitPattern_ & iBit) {
299  refsInfo_.erase(refsInfo_.begin()+index);
301  storedRefsBitPattern_ ^= iBit;
302  }
303  }
304 
305 }
306 
307 bool PFCandidate::getRefInfo(unsigned int iMask, unsigned int iBit,
308  edm::ProductID& oProductID, size_t& oIndex, size_t& aIndex) const {
309 
310  if( 0 == (iBit & storedRefsBitPattern_) ) {
311  return false;
312  }
313  aIndex = s_refsBefore[storedRefsBitPattern_ & iMask];
314  unsigned long long bitPacked = refsInfo_[aIndex];
315  oIndex = bitPacked & 0xFFFFFFFFULL; //low 32 bits are the index
316  unsigned short productIndex = (bitPacked & 0x0000FFFF00000000ULL)>>32;
317  unsigned short processIndex = (bitPacked & 0xFFFF000000000000ULL)>>48;
318  oProductID = edm::ProductID(processIndex,productIndex);
319  return true;
320 }
321 
323  if(!charge()) {
324  string err;
325  err += "PFCandidate::setTrackRef: this is a neutral candidate! ";
326  err += "particleId_=";
327  char num[4];
328  sprintf( num, "%d", particleId());
329  err += num;
330 
331  throw cms::Exception("InconsistentReference",
332  err.c_str() );
333  }
334 
336  iRef.refCore(), iRef.key(),iRef.productGetter());
337 }
338 
340 
341 
343  if( trackRef() != iRef->track() ) {
344  string err;
345  err += "PFCandidate::setMuonRef: inconsistent track references!";
346 
347  throw cms::Exception("InconsistentReference",
348  err.c_str() );
349  }
350 
352  iRef.refCore(), iRef.key(),iRef.productGetter());
353 }
354 
356 
357 
360 // Removed by F. Beaudette. Would like to be able to save the GsfTrackRef even for charged pions
361 // if( particleId() != e ) {
362 // string err;
363 // err += "PFCandidate::setGsfTrackRef: this is not an electron ! particleId_=";
364 // char num[4];
365 // sprintf( num, "%d", particleId());
366 // err += num;
367 //
368 // throw cms::Exception("InconsistentReference",
369 // err.c_str() );
370 // }
371 
373  iRef.refCore(), iRef.key(),iRef.productGetter());
374 }
375 
377 
378 
381 
382  if( particleId() != h ) {
383  string err;
384  err += "PFCandidate::setDisplacedVertexRef: this is not a hadron! particleId_=";
385  char num[4];
386  sprintf( num, "%d", particleId());
387  err += num;
388 
389  throw cms::Exception("InconsistentReference",
390  err.c_str() );
391  }
392  else if( !flag( T_FROM_DISP ) && !flag( T_TO_DISP ) ) {
393  string err;
394  err += "PFCandidate::setDisplacedVertexRef: particule flag is neither T_FROM_DISP nor T_TO_DISP";
395 
396  throw cms::Exception("InconsistentReference",
397  err.c_str() );
398  }
399 
400 
401  if (type == T_TO_DISP && flag( T_TO_DISP ))
403  iRef.isNonnull(),
404  iRef.refCore(), iRef.key(),iRef.productGetter());
405  else if (type == T_FROM_DISP && flag( T_FROM_DISP ))
407  iRef.isNonnull(),
408  iRef.refCore(), iRef.key(),iRef.productGetter());
409  else if ( (type == T_FROM_DISP && !flag( T_FROM_DISP ))
410  ||
411  (type == T_TO_DISP && !flag( T_TO_DISP )) ){
412  string err;
413  err += "PFCandidate::setDisplacedVertexRef: particule flag is not switched on";
414 
415  throw cms::Exception("InconsistentReference",
416  err.c_str() );
417  }
418 
419 }
420 
421 
422 
423 
425  if (type == T_TO_DISP) {
427  }
428  else if (type == T_FROM_DISP) {
430  }
432 }
433 
436  if( particleId() != gamma ) {
437  string err;
438  err += "PFCandidate::setConversionRef: this is not a (converted) photon ! particleId_=";
439  char num[4];
440  sprintf( num, "%d", particleId());
441  err += num;
442 
443  throw cms::Exception("InconsistentReference",
444  err.c_str() );
445  }
446  else if( !flag( GAMMA_TO_GAMMACONV ) ) {
447  string err;
448  err += "PFCandidate::setConversionRef: particule flag is not GAMMA_TO_GAMMACONV";
449 
450  throw cms::Exception("InconsistentReference",
451  err.c_str() );
452  }
453 
455  iRef.refCore(), iRef.key(),iRef.productGetter());
456 }
457 
458 
461 }
462 
466  iRef.refCore(), iRef.key(),iRef.productGetter());
467 }
468 
471 }
472 
476  iRef.refCore(), iRef.key(),iRef.productGetter());
477 }
478 
481 }
482 
486  iRef.refCore(), iRef.key(),iRef.productGetter());
487 }
488 
491 }
492 
493 
496 }
497 
500 }
501 
504 }
505 
507  if( particleId() != gamma && particleId() != e) {
508  string err;
509  err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
510  char num[4];
511  sprintf( num, "%d", particleId());
512  err += num;
513 
514  throw cms::Exception("InconsistentReference", err.c_str() );
515  }
516 
518  iRef.refCore(), iRef.key(),iRef.productGetter());
519 
520 }
521 
523  if( particleId() != gamma && particleId() != e) {
524  string err;
525  err += "PFCandidate::setSuperClusterRef: this is not an electron neither a photon ! particleId_=";
526  char num[4];
527  sprintf( num, "%d", particleId());
528  err += num;
529 
530  throw cms::Exception("InconsistentReference", err.c_str() );
531  }
532 
534  iRef.refCore(), iRef.key(),iRef.productGetter());
535 
536 }
537 
540  iRef.refCore(), iRef.key(),iRef.productGetter());
541 }
542 
543 
544 
545 
547  switch (vertexType_) {
548  case kCandVertex:
549  return vertex_;
550  break;
551  case kTrkVertex:
552  return trackRef()->vertex();
553  break;
554  case kComMuonVertex:
555  return muonRef()->combinedMuon()->vertex();
556  break;
557  case kSAMuonVertex:
558  return muonRef()->standAloneMuon()->vertex();
559  break;
560  case kTrkMuonVertex:
561  return muonRef()->track()->vertex();
562  break;
563  case kGSFVertex:
564  return gsfTrackRef()->vertex();
565  break;
566  }
567  return vertex_;
568 }
type
Definition: HCALResponse.h:22
int Charge
electric charge type
Definition: Candidate.h:36
int i
Definition: DBlmapReader.cc:9
ParticleType
particle types
Definition: PFCandidate.h:38
const edm::EDProductGetter * getter_
Definition: PFCandidate.h:459
void setPFPhotonExtraRef(const reco::PFCandidatePhotonExtraRef &ref)
set the PF Photon Extra Ref
Definition: PFCandidate.cc:538
void setGsfElectronRef(const reco::GsfElectronRef &ref)
set GsfElectronRef
Definition: PFCandidate.cc:474
void setFlag(Flags theFlag, bool value)
set a given flag
Definition: PFCandidate.cc:195
bool getRefInfo(unsigned int iMask, unsigned int iBit, edm::ProductID &oProdID, size_t &oIndex, size_t &aIndex) const
Definition: PFCandidate.cc:307
virtual bool overlap(const Candidate &) const
Polymorphic overlap.
Definition: PFCandidate.cc:171
virtual PFCandidate * clone() const
return a clone
Definition: PFCandidate.cc:118
unsigned flags_
all flags, packed (ecal regional, hcal regional, tracking)
Definition: PFCandidate.h:422
virtual CandidatePtr sourceCandidatePtr(size_type i) const
Definition: Candidate.h:169
static const float bigMva_
Definition: PFCandidate.h:355
#define abs(x)
Definition: mlp_lapack.h:159
ProcessIndex productIndex() const
Definition: ProductID.h:37
virtual void setP4(const LorentzVector &p4)
set 4-momentum
edm::RefVector< reco::PFBlockCollection > Blocks
Definition: PFCandidate.h:339
reco::PFCandidatePhotonExtraRef photonExtraRef() const
return a reference to the photon extra
Definition: PFCandidate.cc:498
bool isAvailable() const
Definition: Ref.h:276
Point vertex_
vertex position
double charge(const std::vector< uint8_t > &Ampls)
virtual double eta() const
momentum pseudorapidity
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:337
const ElementsInBlocks & elementsInBlocks() const
Definition: PFCandidate.h:342
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:339
reco::PFDisplacedVertexRef displacedVertexRef(Flags type) const
Definition: PFCandidate.cc:424
CandidatePtr sourceCandidatePtr(size_type i) const
Definition: PFCandidate.h:117
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
std::vector< unsigned long long > refsInfo_
Definition: PFCandidate.h:461
virtual double energy() const
energy
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
static unsigned long long bitPackRefInfo(const edm::RefCore &iCore, size_t iIndex)
Definition: PFCandidate.cc:264
void setDisplacedVertexRef(const reco::PFDisplacedVertexRef &ref, Flags flag)
set displaced vertex reference
Definition: PFCandidate.cc:380
std::ostream & operator<<(std::ostream &, BeamSpot beam)
Definition: BeamSpot.cc:72
void addElementInBlock(const reco::PFBlockRef &blockref, unsigned elementIndex)
add an element to the current PFCandidate
Definition: PFCandidate.cc:123
void setPFElectronExtraRef(const reco::PFCandidateElectronExtraRef &ref)
set the PF Electron Extra Ref
Definition: PFCandidate.cc:484
void setParticleType(ParticleType type)
set Particle Type
Definition: PFCandidate.cc:166
double p4[4]
Definition: TauolaWrapper.h:92
reco::PhotonRef photonRef() const
return a reference to the corresponding Photon if any
Definition: PFCandidate.cc:494
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:164
bool flag(Flags theFlag) const
return a given flag
Definition: PFCandidate.cc:205
void const * productPtr() const
Definition: RefCore.h:32
virtual int charge() const
electric charge
unsigned short storedRefsBitPattern_
Definition: PFCandidate.h:460
int translateTypeToPdgId(ParticleType type) const
Definition: PFCandidate.cc:148
reco::GsfElectronRef gsfElectronRef() const
return a reference to the corresponding GsfElectron if any
Definition: PFCandidate.cc:479
virtual const Point & vertex() const
vertex position
Definition: PFCandidate.cc:546
RefCore const & refCore() const
Definition: Ref.h:281
unsigned int index
Definition: LeafCandidate.h:33
void reserve(size_type n)
Reserve space for RefVector.
Definition: RefVector.h:95
void rescaleMomentum(double rescaleFactor)
particle momentum *= rescaleFactor
Definition: PFCandidate.cc:188
virtual ~PFCandidate()
destructor
Definition: PFCandidate.cc:116
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:355
tuple out
Definition: dbtoconf.py:99
static const unsigned int s_refsBefore[]
Definition: CountBits.h:14
key_type key() const
Definition: Ptr.h:169
unsigned int index
index type
Definition: Candidate.h:52
Block of elements.
reco::PFCandidateElectronExtraRef electronExtraRef() const
return a reference to the electron extra
Definition: PFCandidate.cc:489
void setGsfTrackRef(const reco::GsfTrackRef &ref)
set gsftrack reference
Definition: PFCandidate.cc:359
PFCandidate()
default constructor
Definition: PFCandidate.cc:30
virtual double pt() const
transverse momentum
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
void setPhotonRef(const reco::PhotonRef &phRef)
set ref to the corresponding reco::Photon if any
Definition: PFCandidate.cc:506
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void setConversionRef(const reco::ConversionRef &ref)
set ref to original reco conversion
Definition: PFCandidate.cc:435
long long int num
Definition: procUtils.cc:71
edm::Ref< PFDisplacedVertexCollection > PFDisplacedVertexRef
persistent reference to a PFDisplacedVertex objects
ParticleType translatePdgIdToType(int pdgid) const
Definition: PFCandidate.cc:134
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:38
ProductID id() const
Definition: RefCore.h:29
PFVertexType vertexType_
Definition: PFCandidate.h:427
PFCandidatePtr sourcePtr_
reference to the source PFCandidate, if any
Definition: PFCandidate.h:394
bool isNull() const
Checks for null.
Definition: Ptr.h:148
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:33
reco::VertexCompositeCandidateRef v0Ref() const
return a reference to the original conversion
Definition: PFCandidate.cc:469
void setMuonRef(const reco::MuonRef &ref)
set muon reference
Definition: PFCandidate.cc:342
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:376
virtual void setPdgId(int pdgId)
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:64
size_type size() const
Size of the RefVector.
Definition: RefVector.h:89
void setSuperClusterRef(const reco::SuperClusterRef &scRef)
Definition: PFCandidate.cc:522
ProductID id() const
Accessor for product ID.
Definition: Ref.h:256
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: Ref.h:259
void setV0Ref(const reco::VertexCompositeCandidateRef &ref)
set ref to original reco conversion
Definition: PFCandidate.cc:464
virtual ParticleType particleId() const
Definition: PFCandidate.h:324
void setTrackRef(const reco::TrackRef &ref)
set track reference
Definition: PFCandidate.cc:322
ProcessIndex processIndex() const
Definition: ProductID.h:36
virtual size_t numberOfSourceCandidatePtrs() const =0
virtual double phi() const
momentum azimuthal angle
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
Elements elementsStorage_
Definition: PFCandidate.h:390
reco::ConversionRef conversionRef() const
return a reference to the original conversion
Definition: PFCandidate.cc:459
std::vector< const void * > refsCollectionCache_
Definition: PFCandidate.h:462
reco::SuperClusterRef superClusterRef() const
return a reference to the corresponding SuperCluster if any
Definition: PFCandidate.cc:502
void storeRefInfo(unsigned int iMask, unsigned int iBit, bool iIsValid, const edm::RefCore &iCore, size_t iKey, const edm::EDProductGetter *)
Definition: PFCandidate.cc:271
#define GETREF(_class_, _mask_, _bit_)
Definition: CountBits.h:4145