CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PackedCandidate.cc
Go to the documentation of this file.
6 
8 using namespace logintpack;
9 
10 void pat::PackedCandidate::pack(bool unpackAfterwards) {
11  packedPt_ = MiniFloatConverter::float32to16(p4_.load()->Pt());
12  packedEta_ = int16_t(std::round(p4_.load()->Eta()/6.0f*std::numeric_limits<int16_t>::max()));
13  packedPhi_ = int16_t(std::round(p4_.load()->Phi()/3.2f*std::numeric_limits<int16_t>::max()));
14  packedM_ = MiniFloatConverter::float32to16(p4_.load()->M());
15  if (unpackAfterwards) {
16  delete p4_.exchange(nullptr);
17  delete p4c_.exchange(nullptr);
18  unpack(); // force the values to match with the packed ones
19  }
20 }
21 
22 void pat::PackedCandidate::packVtx(bool unpackAfterwards) {
23  reco::VertexRef pvRef = vertexRef();
24  Point pv = pvRef.isNonnull() ? pvRef->position() : Point();
25  float dxPV = vertex_.load()->X() - pv.X(), dyPV = vertex_.load()->Y() - pv.Y(); //, rPV = std::hypot(dxPV, dyPV);
26  float s = std::sin(float(p4_.load()->Phi())+dphi_), c = std::cos(float(p4_.load()->Phi()+dphi_)); // not the fastest option, but we're in reduced precision already, so let's avoid more roundoffs
27  dxy_ = - dxPV * s + dyPV * c;
28  // if we want to go back to the full x,y,z we need to store also
29  // float dl = dxPV * c + dyPV * s;
30  // float xRec = - dxy_ * s + dl * c, yRec = dxy_ * c + dl * s;
31  float pzpt = p4_.load()->Pz()/p4_.load()->Pt();
32  dz_ = vertex_.load()->Z() - pv.Z() - (dxPV*c + dyPV*s) * pzpt;
33  packedDxy_ = MiniFloatConverter::float32to16(dxy_*100);
34  packedDz_ = pvRef.isNonnull() ? MiniFloatConverter::float32to16(dz_*100) : int16_t(std::round(dz_/40.f*std::numeric_limits<int16_t>::max()));
35  packedDPhi_ = int16_t(std::round(dphi_/3.2f*std::numeric_limits<int16_t>::max()));
36  packedCovarianceDxyDxy_ = MiniFloatConverter::float32to16(dxydxy_*10000.);
37  packedCovarianceDxyDz_ = MiniFloatConverter::float32to16(dxydz_*10000.);
38  packedCovarianceDzDz_ = MiniFloatConverter::float32to16(dzdz_*10000.);
39 // packedCovarianceDxyDxy_ = pack8log(dxydxy_,-15,-1); // MiniFloatConverter::float32to16(dxydxy_*10000.);
40 // packedCovarianceDxyDz_ = pack8log(dxydz_,-20,-1); //MiniFloatConverter::float32to16(dxydz_*10000.);
41 // packedCovarianceDzDz_ = pack8log(dzdz_,-13,-1); //MiniFloatConverter::float32to16(dzdz_*10000.);
42 
43  packedCovarianceDptDpt_ = pack8logCeil(dptdpt_,-15,0);
44  packedCovarianceDetaDeta_ = pack8logCeil(detadeta_,-20,-5);
45  packedCovarianceDphiDphi_ = pack8logCeil(dphidphi_,-15,0);
46  packedCovarianceDphiDxy_ = pack8log(dphidxy_,-17,-4); // MiniFloatConverter::float32to16(dphidxy_*10000.);
47  packedCovarianceDlambdaDz_ = pack8log(dlambdadz_,-17,-4); // MiniFloatConverter::float32to16(dlambdadz_*10000.);
48 
49  /*packedCovarianceDptDpt_ = pack8logCeil(dptdpt_,-15,5,32);
50  packedCovarianceDetaDeta_ = pack8logCeil(detadeta_,-20,0,32);
51  packedCovarianceDphiDphi_ = pack8logCeil(dphidphi_,-15,5,32);
52  packedCovarianceDphiDxy_ = pack8log(dphidxy_,-17,-4); // MiniFloatConverter::float32to16(dphidxy_*10000.);
53  packedCovarianceDlambdaDz_ = pack8log(dlambdadz_,-17,-4); // MiniFloatConverter::float32to16(dlambdadz_*10000.);
54 
55 */
56 /* packedCovarianceDphiDxy_ = MiniFloatConverter::float32to16(dphidxy_*10000.);
57  packedCovarianceDlambdaDz_ = MiniFloatConverter::float32to16(dlambdadz_*10000.);
58  packedCovarianceDetaDeta_ = MiniFloatConverter::float32to16(detadeta_*10000.);
59  packedCovarianceDphiDphi_ = MiniFloatConverter::float32to16(dphidphi_*10000.);
60  packedCovarianceDptDpt_ = MiniFloatConverter::float32to16(dptdpt_*10000.);
61 */
62 // packedCovarianceDphiDxy_ = pack8log(dphidxy_,-17,-4); // MiniFloatConverter::float32to16(dphidxy_*10000.);
63 // packedCovarianceDlambdaDz_ = pack8log(dlambdadz_,-17,-4); // MiniFloatConverter::float32to16(dlambdadz_*10000.);
64 
65  if (unpackAfterwards) {
66  delete vertex_.exchange(nullptr);
67  unpackVtx();
68  }
69 }
70 
72  float pt = MiniFloatConverter::float16to32(packedPt_);
73  double shift = (pt<1. ? 0.1*pt : 0.1/pt); // shift particle phi to break degeneracies in angular separations
74  double sign = ( ( int(pt*10) % 2 == 0 ) ? 1 : -1 ); // introduce a pseudo-random sign of the shift
75  double phi = int16_t(packedPhi_)*3.2f/std::numeric_limits<int16_t>::max() + sign*shift*3.2/std::numeric_limits<int16_t>::max();
76  auto p4 = std::make_unique<PolarLorentzVector>(pt,
77  int16_t(packedEta_)*6.0f/std::numeric_limits<int16_t>::max(),
78  phi,
80  auto p4c = std::make_unique<LorentzVector>( *p4 );
81  PolarLorentzVector* expectp4= nullptr;
82  if( p4_.compare_exchange_strong(expectp4,p4.get()) ) {
83  p4.release();
84  }
85 
86  //p4c_ works as the guard for unpacking so it
87  // must be set last
88  LorentzVector* expectp4c = nullptr;
89  if(p4c_.compare_exchange_strong(expectp4c, p4c.get()) ) {
90  p4c.release();
91  }
92 }
94  reco::VertexRef pvRef = vertexRef();
95  dphi_ = int16_t(packedDPhi_)*3.2f/std::numeric_limits<int16_t>::max(),
96  dxy_ = MiniFloatConverter::float16to32(packedDxy_)/100.;
97  dz_ = pvRef.isNonnull() ? MiniFloatConverter::float16to32(packedDz_)/100. : int16_t(packedDz_)*40.f/std::numeric_limits<int16_t>::max();
98  Point pv = pvRef.isNonnull() ? pvRef->position() : Point();
99  float phi = p4_.load()->Phi()+dphi_, s = std::sin(phi), c = std::cos(phi);
100  auto vertex = std::make_unique<Point>(pv.X() - dxy_ * s,
101  pv.Y() + dxy_ * c,
102  pv.Z() + dz_ ); // for our choice of using the PCA to the PV, by definition the remaining term -(dx*cos(phi) + dy*sin(phi))*(pz/pt) is zero
103 // dxydxy_ = unpack8log(packedCovarianceDxyDxy_,-15,-1);
104 // dxydz_ = unpack8log(packedCovarianceDxyDz_,-20,-1);
105 // dzdz_ = unpack8log(packedCovarianceDzDz_,-13,-1);
106  dphidxy_ = unpack8log(packedCovarianceDphiDxy_,-17,-4);
107  dlambdadz_ = unpack8log(packedCovarianceDlambdaDz_,-17,-4);
108  dptdpt_ = unpack8log(packedCovarianceDptDpt_,-15,0);
109  detadeta_ = unpack8log(packedCovarianceDetaDeta_,-20,-5);
110  dphidphi_ = unpack8log(packedCovarianceDphiDphi_,-15,0);
111 /*
112  dphidxy_ = unpack8log(packedCovarianceDphiDxy_,-17,-4);
113  dlambdadz_ = unpack8log(packedCovarianceDlambdaDz_,-17,-4);
114  dptdpt_ = unpack8log(packedCovarianceDptDpt_,-15,5,32);
115  detadeta_ = unpack8log(packedCovarianceDetaDeta_,-20,0,32);
116  dphidphi_ = unpack8log(packedCovarianceDphiDphi_,-15,5,32);
117 */
118 
119 /* dphidxy_ = MiniFloatConverter::float16to32(packedCovarianceDphiDxy_)/10000.;
120  dlambdadz_ = MiniFloatConverter::float16to32(packedCovarianceDlambdaDz_)/10000.;
121  dptdpt_ = MiniFloatConverter::float16to32(packedCovarianceDptDpt_)/10000.;
122  detadeta_ = MiniFloatConverter::float16to32(packedCovarianceDetaDeta_)/10000.;
123  dphidphi_ = MiniFloatConverter::float16to32(packedCovarianceDphiDphi_)/10000.;
124 */
125  dxydxy_ = MiniFloatConverter::float16to32(packedCovarianceDxyDxy_)/10000.;
126  dxydz_ =MiniFloatConverter::float16to32(packedCovarianceDxyDz_)/10000.;
127  dzdz_ =MiniFloatConverter::float16to32(packedCovarianceDzDz_)/10000.;
128 /* dphidxy_ = MiniFloatConverter::float16to32(packedCovarianceDphiDxy_)/10000.;
129  dlambdadz_ =MiniFloatConverter::float16to32(packedCovarianceDlambdaDz_)/10000.;
130 */
131  Point* expected = nullptr;
132  if( vertex_.compare_exchange_strong(expected,vertex.get()) ) {
133  vertex.release();
134  }
135 }
136 
138  delete p4_.load();
139  delete p4c_.load();
140  delete vertex_.load();
141  delete track_.load();
142 }
143 
144 
145 float pat::PackedCandidate::dxy(const Point &p) const {
146  maybeUnpackBoth();
147  const float phi = float(p4_.load()->Phi())+dphi_;
148  return -(vertex_.load()->X()-p.X()) * std::sin(phi) + (vertex_.load()->Y()-p.Y()) * std::cos(phi);
149 }
150 float pat::PackedCandidate::dz(const Point &p) const {
151  maybeUnpackBoth();
152  const float phi = float(p4_.load()->Phi())+dphi_;
153  return (vertex_.load()->Z()-p.Z()) - ((vertex_.load()->X()-p.X()) * std::cos(phi) + (vertex_.load()->Y()-p.Y()) * std::sin(phi)) * p4_.load()->Pz()/p4_.load()->Pt();
154 }
155 
157  maybeUnpackBoth();
159 // m(0,0)=0.5e-4/pt()/pt(); //TODO: tune
160 // m(1,1)=6e-6; //TODO: tune
161 // m(2,2)=1.5e-5/pt()/pt(); //TODO: tune
162  m(0,0)=dptdpt_/pt()/pt(); //TODO: tune
163  m(1,1)=detadeta_; //TODO: tune
164  m(2,2)=dphidphi_/pt()/pt(); //TODO: tune
165  m(2,3)=dphidxy_;
166  m(3,2)=dphidxy_;
167  m(4,1)=dlambdadz_;
168  m(1,4)=dlambdadz_;
169  m(3,3)=dxydxy_;
170  m(3,4)=dxydz_;
171  m(4,3)=dxydz_;
172  m(4,4)=dzdz_;
173  math::RhoEtaPhiVector p3(p4_.load()->pt(),p4_.load()->eta(),phiAtVtx());
174  int numberOfPixelHits = packedHits_ & trackPixelHitsMask ;
175  int numberOfHits = (packedHits_>>trackStripHitsShift) + numberOfPixelHits;
176 
177  int ndof = numberOfHits+numberOfPixelHits-5;
178  reco::HitPattern hp, hpExpIn;
179  int i=0;
180  LostInnerHits innerLost = lostInnerHits();
181 
182  auto track = std::make_unique<reco::Track>(normalizedChi2_*ndof,ndof,*vertex_,math::XYZVector(p3.x(),p3.y(),p3.z()),charge(),m,reco::TrackBase::undefAlgorithm,reco::TrackBase::loose);
183 
184  if(innerLost == validHitInFirstPixelBarrelLayer){
185  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::valid);
186  i++;
187  }
188  for(;i<numberOfPixelHits; i++) {
189  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, i > 1 ? 3 : 2, 0, TrackingRecHit::valid);
190  }
191 
192  for(;i<numberOfHits;i++) {
193  track->appendTrackerHitPattern(StripSubdetector::TIB, 1, 1, TrackingRecHit::valid);
194  }
195 
196  switch (innerLost) {
197  case validHitInFirstPixelBarrelLayer:
198  break;
199  case noLostInnerHits:
200  break;
201  case oneLostInnerHit:
202  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::missing_inner);
203  break;
204  case moreLostInnerHits:
205  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::missing_inner);
206  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 2, 0, TrackingRecHit::missing_inner);
207  break;
208  };
209 
210  if (trackHighPurity()) track->setQuality(reco::TrackBase::highPurity);
211 
212  reco::Track* expected = nullptr;
213  if( track_.compare_exchange_strong(expected,track.get()) ) {
214  track.release();
215  }
216 
217 }
218 
220 
222  throw cms::Exception("Invalid Reference")
223  << "this Candidate has no master clone reference."
224  << "Can't call masterClone() method.\n";
225 }
226 
228  return false;
229 }
230 
232  return false;
233 }
234 
235 
237  throw cms::Exception("Invalid Reference")
238  << "this Candidate has no master clone ptr."
239  << "Can't call masterClonePtr() method.\n";
240 }
241 
243  return 0;
244 }
245 
247  return 0;
248 }
249 
251  return p4() == o.p4() && vertex() == o.vertex() && charge() == o.charge();
252 // return p4() == o.p4() && charge() == o.charge();
253 }
254 
256  return 0;
257 }
258 
260  return 0;
261 }
262 
265  << "This Candidate type does not implement daughter(std::string). "
266  << "Please use CompositeCandidate or NamedCompositeCandidate.\n";
267 }
268 
271  << "This Candidate type does not implement daughter(std::string). "
272  << "Please use CompositeCandidate or NamedCompositeCandidate.\n";
273 }
274 
275 
276 
278  return 0;
279 }
280 
282  return 0;
283 }
284 
286  return 0;
287 }
288 
290  return 0;
291 }
292 
295  << "reco::ConcreteCandidate does not implement vertex covariant matrix.\n";
296 }
297 
300  << "reco::ConcreteCandidate does not implement vertex covariant matrix.\n";
301 }
302 
303 
304 bool pat::PackedCandidate::longLived() const {return false;}
305 
306 bool pat::PackedCandidate::massConstraint() const {return false;}
307 
308 // puppiweight
309 void pat::PackedCandidate::setPuppiWeight(float p, float p_nolep) {
310  // Set both weights at once to avoid misconfigured weights if called in the wrong order
311  packedPuppiweight_ = pack8logClosed((p-0.5)*2,-2,0,64);
312  packedPuppiweightNoLepDiff_ = pack8logClosed((p_nolep-0.5)*2,-2,0,64) - packedPuppiweight_;
313 }
314 
315 float pat::PackedCandidate::puppiWeight() const { return unpack8logClosed(packedPuppiweight_,-2,0,64)/2. + 0.5;}
316 
317 float pat::PackedCandidate::puppiWeightNoLep() const { return unpack8logClosed(packedPuppiweightNoLepDiff_+packedPuppiweight_,-2,0,64)/2. + 0.5;}
318 
320  hcalFraction_ = 100*p;
321 }
float puppiWeight() const
Set both weights at once (with option for only full PUPPI)
virtual float dz(size_t ipv=0) const
dz with respect to the PV[ipv]
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
virtual size_t numberOfMothers() const
number of mothers
virtual bool hasMasterClonePtr() const
float puppiWeightNoLep() const
Weight from full PUPPI.
void setPuppiWeight(float p, float p_nolep=0.0)
size_t size_type
Definition: Candidate.h:30
double unpack8log(int8_t i, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:52
int8_t pack8log(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:25
virtual void fillVertexCovariance(CovarianceMatrix &v) const
fill SMatrix
double sign(double x)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
virtual double vertexNdof() const
virtual bool overlap(const reco::Candidate &) const
check overlap with another Candidate
std::pair< double, double > Point
Definition: CaloEllipse.h:18
void unpackVtx() const
RhoEtaPhiVectorD RhoEtaPhiVector
spatial vector with cylindrical internal representation using pseudorapidity
Definition: Vector3D.h:32
static float float16to32(uint16_t h)
Definition: libminifloat.h:10
int8_t pack8logCeil(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:13
virtual const reco::Candidate * daughter(size_type) const
return daughter at a given position (throws an exception)
int8_t pack8logClosed(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:39
void setHcalFraction(float p)
Weight from PUPPI removing leptons.
susybsm::HSCParticleRefProd hp
Definition: classes.h:27
double p4[4]
Definition: TauolaWrapper.h:92
static uint16_t float32to16(float x)
Definition: libminifloat.h:15
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
virtual const reco::CandidateBaseRef & masterClone() const
int j
Definition: DBlmapReader.cc:9
double f[11][100]
virtual const Point & vertex() const =0
vertex position
virtual double vertexNormalizedChi2() const
chi-squared divided by n.d.o.f.
virtual int charge() const =0
electric charge
virtual const reco::CandidatePtr & masterClonePtr() const
LostInnerHits
Enumerator specifying the.
virtual bool massConstraint() const
do mass constraint?
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
Geom::Phi< T > phi() const
virtual size_t numberOfDaughters() const
number of daughters
CovarianceMatrix vertexCovariance() const
return SMatrix
double unpack8logClosed(int8_t i, double lmin, double lmax, uint8_t base=128)
reverse of pack8logClosed
Definition: liblogintpack.h:62
virtual bool longLived() const
is long lived?
virtual const reco::Candidate * mother(size_type) const
return mother at a given position (throws an exception)
void packVtx(bool unpackAfterwards=true)
static unsigned int const shift
math::XYZPoint Point
point in the space
Definition: Candidate.h:41
void pack(bool unpackAfterwards=true)
virtual float dxy() const
dxy with respect to the PV ref
virtual double vertexChi2() const
chi-squares
virtual ~PackedCandidate()
destructor
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77
virtual bool hasMasterClone() const
double p3[4]
Definition: TauolaWrapper.h:91
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Candidate.h:39