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