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 numberOfStripLayers = stripLayersWithMeasurement(), numberOfPixelLayers = pixelLayersWithMeasurement();
175  int numberOfPixelHits = this->numberOfPixelHits();
176  int numberOfHits = this->numberOfHits();
177 
178  int ndof = numberOfHits+numberOfPixelHits-5;
179  reco::HitPattern hp, hpExpIn;
180  int i=0;
181  LostInnerHits innerLost = lostInnerHits();
182 
183  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);
184 
185  // add hits to match the number of laters and validHitInFirstPixelBarrelLayer
186  if(innerLost == validHitInFirstPixelBarrelLayer){
187  // first we add one hit on the first barrel layer
188  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::valid);
189  // then to encode the number of layers, we add more hits on distinct layers (B2, B3, B4, F1, ...)
190  for(i++; i<numberOfPixelLayers; i++) {
191  if (i <= 3) {
192  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, i+1, 0, TrackingRecHit::valid);
193  } else {
194  track->appendTrackerHitPattern(PixelSubdetector::PixelEndcap, i-3, 0, TrackingRecHit::valid);
195  }
196  }
197  } else {
198  // to encode the information on the layers, we add one valid hits per layer but skipping PXB1
199  for(;i<numberOfPixelLayers; i++) {
200  if (i <= 2 ) {
201  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, i+2, 0, TrackingRecHit::valid);
202  } else {
203  track->appendTrackerHitPattern(PixelSubdetector::PixelEndcap, i-3, 0, TrackingRecHit::valid);
204  }
205  }
206  }
207  // add extra hits (overlaps, etc), all on the first layer with a hit - to avoid increasing the layer count
208  for(;i<numberOfPixelHits; i++) {
209  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, (innerLost == validHitInFirstPixelBarrelLayer ? 1 : 2), 0, TrackingRecHit::valid);
210  }
211  // now start adding strip layers, putting one hit on each layer so that the hitPattern.stripLayersWithMeasurement works.
212  // we don't know what the layers where, so we just start with TIB (4 layers), then TOB (6 layers), then TEC (9)
213  // and then TID(3), so that we can get a number of valid strip layers up to 4+6+9+3
214  for(int sl = 0; sl < numberOfStripLayers; ++sl, ++i) {
215  if (sl < 4) track->appendTrackerHitPattern(StripSubdetector::TIB, sl +1, 1, TrackingRecHit::valid);
216  else if (sl < 4+6) track->appendTrackerHitPattern(StripSubdetector::TOB, (sl- 4)+1, 1, TrackingRecHit::valid);
217  else if (sl < 10+9) track->appendTrackerHitPattern(StripSubdetector::TEC, (sl-10)+1, 1, TrackingRecHit::valid);
218  else if (sl < 19+3) track->appendTrackerHitPattern(StripSubdetector::TID, (sl-13)+1, 1, TrackingRecHit::valid);
219  else break; // wtf?
220  }
221  // finally we account for extra strip hits beyond the one-per-layer added above. we put them all on TIB1,
222  // to avoid incrementing the number of layersWithMeasurement.
223  for(;i<numberOfHits;i++) {
224  track->appendTrackerHitPattern(StripSubdetector::TIB, 1, 1, TrackingRecHit::valid);
225  }
226 
227  switch (innerLost) {
228  case validHitInFirstPixelBarrelLayer:
229  break;
230  case noLostInnerHits:
231  break;
232  case oneLostInnerHit:
233  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::missing_inner);
234  break;
235  case moreLostInnerHits:
236  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 1, 0, TrackingRecHit::missing_inner);
237  track->appendTrackerHitPattern(PixelSubdetector::PixelBarrel, 2, 0, TrackingRecHit::missing_inner);
238  break;
239  };
240 
241  if (trackHighPurity()) track->setQuality(reco::TrackBase::highPurity);
242 
243  reco::Track* expected = nullptr;
244  if( track_.compare_exchange_strong(expected,track.get()) ) {
245  track.release();
246  }
247 
248 }
249 
251 
253  throw cms::Exception("Invalid Reference")
254  << "this Candidate has no master clone reference."
255  << "Can't call masterClone() method.\n";
256 }
257 
259  return false;
260 }
261 
263  return false;
264 }
265 
266 
268  throw cms::Exception("Invalid Reference")
269  << "this Candidate has no master clone ptr."
270  << "Can't call masterClonePtr() method.\n";
271 }
272 
274  return 0;
275 }
276 
278  return 0;
279 }
280 
282  return p4() == o.p4() && vertex() == o.vertex() && charge() == o.charge();
283 // return p4() == o.p4() && charge() == o.charge();
284 }
285 
287  return 0;
288 }
289 
291  return 0;
292 }
293 
296  << "This Candidate type does not implement daughter(std::string). "
297  << "Please use CompositeCandidate or NamedCompositeCandidate.\n";
298 }
299 
302  << "This Candidate type does not implement daughter(std::string). "
303  << "Please use CompositeCandidate or NamedCompositeCandidate.\n";
304 }
305 
306 
307 
309  return 0;
310 }
311 
313  return 0;
314 }
315 
317  return 0;
318 }
319 
321  return 0;
322 }
323 
326  << "reco::ConcreteCandidate does not implement vertex covariant matrix.\n";
327 }
328 
331  << "reco::ConcreteCandidate does not implement vertex covariant matrix.\n";
332 }
333 
334 
335 bool pat::PackedCandidate::longLived() const {return false;}
336 
337 bool pat::PackedCandidate::massConstraint() const {return false;}
338 
339 // puppiweight
340 void pat::PackedCandidate::setPuppiWeight(float p, float p_nolep) {
341  // Set both weights at once to avoid misconfigured weights if called in the wrong order
342  packedPuppiweight_ = pack8logClosed((p-0.5)*2,-2,0,64);
343  packedPuppiweightNoLepDiff_ = pack8logClosed((p_nolep-0.5)*2,-2,0,64) - packedPuppiweight_;
344 }
345 
346 float pat::PackedCandidate::puppiWeight() const { return unpack8logClosed(packedPuppiweight_,-2,0,64)/2. + 0.5;}
347 
348 float pat::PackedCandidate::puppiWeightNoLep() const { return unpack8logClosed(packedPuppiweightNoLepDiff_+packedPuppiweight_,-2,0,64)/2. + 0.5;}
349 
351  hcalFraction_ = 100*p;
352 }
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:55
int8_t pack8log(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:27
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:14
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:42
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:66
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