test
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 // Jet.cc
2 // Fedor Ratnikov, UMd
3 
4 #include <sstream>
5 #include <cmath>
6 
9 
10 //Own header file
12 
13 
14 
15 using namespace reco;
16 
17 namespace {
18  // approximate simple CALO geometry
19  // abstract baseclass for geometry.
20 
21  class CaloPoint {
22  public:
23  static const double depth; // one for all relative depth of the reference point between ECAL begin and HCAL end
24  static const double R_BARREL;
25  static const double R_BARREL2;
26  static const double Z_ENDCAP; // 1/2(EEz+HEz)
27  static const double R_FORWARD; // eta=3
28  static const double R_FORWARD2;
29  static const double Z_FORWARD;
30  static const double Z_BIG;
31  };
32 
33  const double CaloPoint::depth = 0.1; // one for all relative depth of the reference point between ECAL begin and HCAL end
34  const double CaloPoint::R_BARREL = (1.-depth)*143.+depth*407.;
35  const double CaloPoint::R_BARREL2 = R_BARREL * R_BARREL;
36  const double CaloPoint::Z_ENDCAP = (1.-depth)*320.+depth*568.; // 1/2(EEz+HEz)
37  const double CaloPoint::R_FORWARD = Z_ENDCAP / std::sqrt (std::cosh(3.)*std::cosh(3.) -1.); // eta=3
38  const double CaloPoint::R_FORWARD2 = R_FORWARD * R_FORWARD;
39  const double CaloPoint::Z_FORWARD = 1100.+depth*165.;
40  const double CaloPoint::Z_BIG = 1.e5;
41 
42  //old zvertex only implementation:
43  class CaloPointZ: private CaloPoint{
44  public:
45  CaloPointZ (double fZ, double fEta){
46 
47  static const double ETA_MAX = 5.2;
48 
49  if (fZ > Z_ENDCAP) fZ = Z_ENDCAP-1.;
50  if (fZ < -Z_ENDCAP) fZ = -Z_ENDCAP+1; // sanity check
51 
52  double tanThetaAbs = std::sqrt (std::cosh(fEta)*std::cosh(fEta) - 1.);
53  double tanTheta = fEta >= 0 ? tanThetaAbs : -tanThetaAbs;
54 
55  double rEndcap = tanTheta == 0 ? 1.e10 :
56  fEta > 0 ? (Z_ENDCAP - fZ) / tanTheta : (-Z_ENDCAP - fZ) / tanTheta;
57  if (rEndcap > R_BARREL) { // barrel
58  mR = R_BARREL;
59  mZ = fZ + R_BARREL * tanTheta;
60  }
61  else {
62  double zRef = Z_BIG; // very forward;
63  if (rEndcap > R_FORWARD) zRef = Z_ENDCAP; // endcap
64  else if (std::fabs (fEta) < ETA_MAX) zRef = Z_FORWARD; // forward
65 
66  mZ = fEta > 0 ? zRef : -zRef;
67  mR = std::fabs ((mZ - fZ) / tanTheta);
68  }
69  }
70  double etaReference (double fZ) {
71  Jet::Point p (r(), 0., z() - fZ);
72  return p.eta();
73  }
74 
75  double thetaReference (double fZ) {
76  Jet::Point p (r(), 0., z() - fZ);
77  return p.theta();
78  }
79 
80  double z() const {return mZ;}
81  double r() const {return mR;}
82  private:
83  CaloPointZ(){};
84  double mZ;
85  double mR;
86  };
87 
88  //new implementation to derive CaloPoint for free 3d vertex.
89  //code provided thanks to Christophe Saout
90  template<typename Point>
91  class CaloPoint3D : private CaloPoint {
92  public:
93  template<typename Vector, typename Point2>
94  CaloPoint3D(const Point2 &vertex, const Vector &dir)
95  {
96  // note: no sanity checks here, make sure vertex is inside the detector!
97 
98  // check if positive or negative (or none) endcap should be tested
99  int side = dir.z() < -1e-9 ? -1 : dir.z() > 1e-9 ? +1 : 0;
100 
101  double dirR = dir.Rho();
102 
103  // normalized direction in x-y plane
104  double dirUnit[2] = { dir.x() / dirR, dir.y() / dirR };
105 
106  // rotate the vertex into a coordinate system where dir lies along x
107 
108  // vtxLong is the longitudinal coordinate of the vertex wrt/ dir
109  double vtxLong = dirUnit[0] * vertex.x() + dirUnit[1] * vertex.y();
110 
111  // tIP is the (signed) transverse impact parameter
112  double tIP = dirUnit[0] * vertex.y() - dirUnit[1] * vertex.x();
113 
114  // r and z coordinate
115  double r, z;
116 
117  if (side) {
118  double slope = dirR / dir.z();
119 
120  // check extrapolation to endcap
121  r = vtxLong + slope * (side * Z_ENDCAP - vertex.z());
122  double r2 = sqr(r) + sqr(tIP);
123 
124  if (r2 < R_FORWARD2) {
125  // we are in the forward calorimeter, recompute
126  r = vtxLong + slope * (side * Z_FORWARD - vertex.z());
127  z = side * Z_FORWARD;
128  } else if (r2 < R_BARREL2) {
129  // we are in the endcap
130  z = side * Z_ENDCAP;
131  } else {
132  // we are in the barrel, do the intersection below
133  side = 0;
134  }
135  }
136 
137  if (!side) {
138  // we are in the barrel
139  double slope = dir.z() / dirR;
140  r = std::sqrt(R_BARREL2 - sqr(tIP));
141  z = vertex.z() + slope * (r - vtxLong);
142  }
143 
144  // rotate (r, tIP, z) back into original x-y coordinate system
145  point = Point(dirUnit[0] * r - dirUnit[1] * tIP,
146  dirUnit[1] * r + dirUnit[0] * tIP,
147  z);
148  }
149 
150  const Point &caloPoint() const { return point; }
151 
152  private:
153  template<typename T>
154  static inline T sqr(const T &value) { return value * value; }
155 
156  Point point;
157  };
158 
159 }
160 
161 Jet::Jet (const LorentzVector& fP4,
162  const Point& fVertex,
163  const Constituents& fConstituents)
164  : CompositePtrCandidate (0, fP4, fVertex),
165  mJetArea (0),
166  mPileupEnergy (0),
167  mPassNumber (0)
168 {
169  for (unsigned i = 0; i < fConstituents.size (); i++) {
170  addDaughter (fConstituents [i]);
171  }
172 }
173 
174 Jet::Jet (const LorentzVector& fP4,
175  const Point& fVertex)
176  : CompositePtrCandidate (0, fP4, fVertex),
177  mJetArea (0),
178  mPileupEnergy (0),
179  mPassNumber (0)
180 {}
181 
184  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
185  double phiRef = phi();
186  double sumw = 0;
187  double sumEta = 0;
188  double sumEta2 = 0;
189  double sumPhi = 0;
190  double sumPhi2 = 0;
191  double sumEtaPhi = 0;
192  int i = towers.size();
193  while (--i >= 0) {
194  double eta = towers[i]->eta();
195  double phi = deltaPhi (towers[i]->phi(), phiRef);
196  double weight = towers[i]->et();
197  sumw += weight;
198  sumEta += eta * weight;
199  sumEta2 += eta * eta * weight;
200  sumPhi += phi * weight;
201  sumPhi2 += phi * phi * weight;
202  sumEtaPhi += eta * phi * weight;
203  }
205  if (sumw > 0) {
206  result.etaMean = sumEta / sumw;
207  result.phiMean = deltaPhi (phiRef + sumPhi, 0.);
208  result.etaEtaMoment = (sumEta2 - sumEta * sumEta / sumw) / sumw;
209  result.phiPhiMoment = (sumPhi2 - sumPhi * sumPhi / sumw) / sumw;
210  result.etaPhiMoment = (sumEtaPhi - sumEta * sumPhi / sumw) / sumw;
211  }
212  else {
213  result.etaMean = 0;
214  result.phiMean = 0;
215  result.etaEtaMoment = 0;
216  result.phiPhiMoment = 0;
217  result.etaPhiMoment = 0;
218  }
219  return result;
220 }
221 
223 float Jet::etaetaMoment () const {
224  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
225  double sumw = 0;
226  double sum = 0;
227  double sum2 = 0;
228  int i = towers.size();
229  while (--i >= 0) {
230  double value = towers[i]->eta();
231  double weight = towers[i]->et();
232  sumw += weight;
233  sum += value * weight;
234  sum2 += value * value * weight;
235  }
236  return sumw > 0 ? (sum2 - sum*sum/sumw ) / sumw : 0;
237 }
238 
240 float Jet::phiphiMoment () const {
241  double phiRef = phi();
242  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
243  double sumw = 0;
244  double sum = 0;
245  double sum2 = 0;
246  int i = towers.size();
247  while (--i >= 0) {
248  double value = deltaPhi (towers[i]->phi(), phiRef);
249  double weight = towers[i]->et();
250  sumw += weight;
251  sum += value * weight;
252  sum2 += value * value * weight;
253  }
254  return sumw > 0 ? (sum2 - sum*sum/sumw ) / sumw : 0;
255 }
256 
258 float Jet::etaphiMoment () const {
259  double phiRef = phi();
260  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
261  double sumw = 0;
262  double sumA = 0;
263  double sumB = 0;
264  double sumAB = 0;
265  int i = towers.size();
266  while (--i >= 0) {
267  double valueA = towers[i]->eta();
268  double valueB = deltaPhi (towers[i]->phi(), phiRef);
269  double weight = towers[i]->et();
270  sumw += weight;
271  sumA += valueA * weight;
272  sumB += valueB * weight;
273  sumAB += valueA * valueB * weight;
274  }
275  return sumw > 0 ? (sumAB - sumA*sumB/sumw ) / sumw : 0;
276 }
277 
279 float Jet::etInAnnulus (float fRmin, float fRmax) const {
280  float result = 0;
281  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
282  int i = towers.size ();
283  while (--i >= 0) {
284  double r = deltaR (*this, *(towers[i]));
285  if (r >= fRmin && r < fRmax) result += towers[i]->et ();
286  }
287  return result;
288 }
289 
291 int Jet::nCarrying (float fFraction) const {
292  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
293  if (fFraction >= 1) return towers.size();
294  double totalEt = 0;
295  for (unsigned i = 0; i < towers.size(); ++i) totalEt += towers[i]->et();
296  double fractionEnergy = totalEt * fFraction;
297  unsigned result = 0;
298  for (; result < towers.size(); ++result) {
299  fractionEnergy -= towers[result]->et();
300  if (fractionEnergy <= 0) return result+1;
301  }
302  return 0;
303 }
304 
306 float Jet::maxDistance () const {
307  float result = 0;
308  std::vector<const Candidate*> towers = getJetConstituentsQuick ();
309  for (unsigned i = 0; i < towers.size(); ++i) {
310  float dR = deltaR (*(towers[i]), *this);
311  if (dR > result) result = dR;
312  }
313  return result;
314 }
315 
317 // kept for backwards compatibility, use detector/physicsP4 instead!
318 float Jet::physicsEta (float fZVertex, float fDetectorEta) {
319  CaloPointZ refPoint (0., fDetectorEta);
320  return refPoint.etaReference (fZVertex);
321 }
322 
324 // kept for backwards compatibility, use detector/physicsP4 instead!
325 float Jet::detectorEta (float fZVertex, float fPhysicsEta) {
326  CaloPointZ refPoint (fZVertex, fPhysicsEta);
327  return refPoint.etaReference (0.);
328 }
329 
330 Candidate::LorentzVector Jet::physicsP4 (const Candidate::Point &newVertex, const Candidate &inParticle,const Candidate::Point &oldVertex) {
331  CaloPoint3D<Point> caloPoint(oldVertex,inParticle.momentum()); // Jet position in Calo.
332  Vector physicsDir = caloPoint.caloPoint() - newVertex;
333  double p = inParticle.momentum().r();
334  Vector p3 = p * physicsDir.unit();
335  LorentzVector returnVector(p3.x(), p3.y(), p3.z(), inParticle.energy());
336  return returnVector;
337 }
338 
340  CaloPoint3D<Point> caloPoint(vertex,inParticle.momentum()); // Jet position in Calo.
341  static const Point np(0,0,0);
342  Vector detectorDir = caloPoint.caloPoint() - np;
343  double p = inParticle.momentum().r();
344  Vector p3 = p * detectorDir.unit();
345  LorentzVector returnVector(p3.x(), p3.y(), p3.z(), inParticle.energy());
346  return returnVector;
347 }
348 
349 
352  for (unsigned i = 0; i < numberOfDaughters(); i++) {
353  result.push_back (daughterPtr (i));
354  }
355  return result;
356 }
357 
358 std::vector<const Candidate*> Jet::getJetConstituentsQuick () const {
359  std::vector<const Candidate*> result;
360  int nDaughters = numberOfDaughters();
361  for (int i = 0; i < nDaughters; ++i) {
362  result.push_back (daughter (i));
363  }
364  return result;
365 }
366 
367 
368 
370 
371  Jet::Constituents constituents = this->getJetConstituents();
372 
373  float sum_pt2 = 0.;
374  float sum_pt = 0.;
375 
376  for( unsigned iConst=0; iConst<constituents.size(); ++iConst ) {
377 
378  float pt = constituents[iConst]->p4().Pt();
379  float pt2 = pt*pt;
380 
381  sum_pt += pt;
382  sum_pt2 += pt2;
383 
384  } //for constituents
385 
386  float ptD_value = (sum_pt>0.) ? sqrt( sum_pt2 / (sum_pt*sum_pt) ) : 0.;
387 
388  return ptD_value;
389 
390 } //constituentPtDistribution
391 
392 
393 
395 
396  Jet::Constituents constituents = this->getJetConstituents();
397 
398 
399  float sum_pt2 = 0.;
400  float sum_pt2deltaR2 = 0.;
401 
402  for( unsigned iConst=0; iConst<constituents.size(); ++iConst ) {
403 
404  LorentzVector thisConstituent = constituents[iConst]->p4();
405 
406  float pt = thisConstituent.Pt();
407  float pt2 = pt*pt;
408  double dR = deltaR (*this, *(constituents[iConst]));
409  float pt2deltaR2 = pt*pt*dR*dR;
410 
411  sum_pt2 += pt2;
412  sum_pt2deltaR2 += pt2deltaR2;
413 
414  } //for constituents
415 
416  float rmsCand_value = (sum_pt2>0.) ? sum_pt2deltaR2/sum_pt2 : 0.;
417 
418  return rmsCand_value;
419 
420 } //constituentEtaPhiSpread
421 
422 
423 
425  std::ostringstream out;
426  out << "Jet p/px/py/pz/pt: " << p() << '/' << px () << '/' << py() << '/' << pz() << '/' << pt() << std::endl
427  << " eta/phi: " << eta () << '/' << phi () << std::endl
428  << " # of constituents: " << nConstituents () << std::endl;
429  out << " Constituents:" << std::endl;
430  for (unsigned index = 0; index < numberOfDaughters(); index++) {
431  Constituent constituent = daughterPtr (index); // deref
432  if (constituent.isNonnull()) {
433  out << " #" << index << " p/pt/eta/phi: "
434  << constituent->p() << '/' << constituent->pt() << '/' << constituent->eta() << '/' << constituent->phi()
435  << " productId/index: " << constituent.id() << '/' << constituent.key() << std::endl;
436  }
437  else {
438  out << " #" << index << " constituent is not available in the event" << std::endl;
439  }
440  }
441  return out.str ();
442 }
443 
444 void Jet::scaleEnergy (double fScale) {
445  setP4 (p4() * fScale);
446 }
447 
448 bool Jet::isJet() const {
449  return true;
450 }
int i
Definition: DBlmapReader.cc:9
bool isJet() const
Definition: Jet.cc:448
virtual double p() const
magnitude of momentum vector
float etInAnnulus(float fRmin, float fRmax) const
ET in annulus between rmin and rmax around jet direction.
Definition: Jet.cc:279
virtual double energy() const =0
energy
math::XYZVector Vector
point in the space
Definition: Candidate.h:47
virtual float pt() const
transverse momentum
static float detectorEta(float fZVertex, float fPhysicsEta)
static function to convert physics eta to detector eta
Definition: Jet.cc:325
virtual double et() const
transverse energy
CandidatePtr daughterPtr(size_type i) const
reference to daughter at given position
virtual float phi() const
momentum azimuthal angle
static const double slope[3]
int nCarrying(float fFraction) const
return # of constituent carrying fraction of energy
Definition: Jet.cc:291
virtual void setP4(const LorentzVector &p4)
set 4-momentum
std::vector< Constituent > Constituents
Definition: Jet.h:23
virtual void scaleEnergy(double fScale)
scale energy of the jet
Definition: Jet.cc:444
double deltaR(const T1 &t1, const T2 &t2)
Definition: deltaR.h:48
EtaPhiMoments etaPhiStatistics() const
eta-phi statistics, ET weighted
Definition: Jet.cc:183
std::pair< double, double > Point
Definition: CaloEllipse.h:18
float float float z
float etaphiMoment() const
eta-phi second moment, ET weighted
Definition: Jet.cc:258
float maxDistance() const
maximum distance from jet to constituent
Definition: Jet.cc:306
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:152
virtual Vector momentum() const =0
spatial momentum vector
virtual size_t numberOfDaughters() const
number of daughters
record to store eta-phi first and second moments
Definition: Jet.h:26
int np
Definition: AMPTWrapper.h:33
fixed size vector
Definition: Vector.h:31
virtual float eta() const
momentum pseudorapidity
T sqrt(T t)
Definition: SSEVec.h:48
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:164
tuple result
Definition: query.py:137
math::XYZPoint Point
float constituentEtaPhiSpread() const
Definition: Jet.cc:394
tuple out
Definition: dbtoconf.py:99
double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:12
float etaetaMoment() const
eta-eta second moment, ET weighted
Definition: Jet.cc:223
key_type key() const
Definition: Ptr.h:169
unsigned int index
index type
Definition: Candidate.h:55
float constituentPtDistribution() const
Definition: Jet.cc:369
virtual double px() const
x coordinate of momentum vector
virtual std::vector< const reco::Candidate * > getJetConstituentsQuick() const
quick list of constituents
Definition: Jet.cc:358
float phiphiMoment() const
phi-phi second moment, ET weighted
Definition: Jet.cc:240
virtual double pz() const
z coordinate of momentum vector
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:41
static Candidate::LorentzVector detectorP4(const Candidate::Point &vertex, const Candidate &inParticle)
Definition: Jet.cc:339
static Candidate::LorentzVector physicsP4(const Candidate::Point &newVertex, const Candidate &inParticle, const Candidate::Point &oldVertex=Candidate::Point(0, 0, 0))
Definition: Jet.cc:330
virtual int nConstituents() const
of constituents
Definition: Jet.h:65
Square< F >::type sqr(const F &f)
Definition: Square.h:13
void addDaughter(const CandidatePtr &)
add a daughter via a reference
Jet()
Default constructor.
Definition: Jet.h:36
math::XYZPoint Point
point in the space
Definition: Candidate.h:45
dbl *** dir
Definition: mlp_gen.cc:35
int weight
Definition: histoStyle.py:50
virtual std::string print() const
Print object.
Definition: Jet.cc:424
long double T
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
static float physicsEta(float fZVertex, float fDetectorEta)
static function to convert detector eta to physics eta
Definition: Jet.cc:318
virtual double py() const
y coordinate of momentum vector
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
math::XYZPoint Point
point in the space
Definition: LeafCandidate.h:32
double p3[4]
Definition: TauolaWrapper.h:91
virtual Constituents getJetConstituents() const
list of constituents
Definition: Jet.cc:350