CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CommonUsefulStuff.h
Go to the documentation of this file.
1 #ifndef CommonUsefulStuff_h
2 #define CommonUsefulStuff_h
3 
4 
12 
21 
24 
26 
29 
33 
34 #include "TROOT.h"
35 #include "TFile.h"
36 #include "TTree.h"
37 #include "TH1F.h"
38 #include "TString.h"
39 
40 /* getDist functions by Jim:
41 /uscms/home/jhirsch/IsoTracks_314/CMSSW_3_1_4/src/JetMETCorrections/IsolatedParticles/interface/FindCaloHit.icc
42 */
43 
44 
46 {
47  int iphihitm;
48  int ietahitm;
49  int depthhit;
50  float hitenergy;
51  float dr;
54  depthhit(0),hitenergy(-100),dr(0){}
55 };
56 
57 
58 inline double getDistInPlaneSimple(const GlobalPoint caloPoint,
59  const GlobalPoint rechitPoint)
60 {
61 
62  // Simplified version of getDistInPlane
63  // Assume track direction is origin -> point of hcal intersection
64 
65  const GlobalVector caloIntersectVector(caloPoint.x(),
66  caloPoint.y(),
67  caloPoint.z());
68 
69  const GlobalVector caloIntersectUnitVector = caloIntersectVector.unit();
70 
71  const GlobalVector rechitVector(rechitPoint.x(),
72  rechitPoint.y(),
73  rechitPoint.z());
74 
75  const GlobalVector rechitUnitVector = rechitVector.unit();
76  double dotprod = caloIntersectUnitVector.dot(rechitUnitVector);
77  double rechitdist = caloIntersectVector.mag()/dotprod;
78 
79 
80  const GlobalVector effectiveRechitVector = rechitdist*rechitUnitVector;
81  const GlobalPoint effectiveRechitPoint(effectiveRechitVector.x(),
82  effectiveRechitVector.y(),
83  effectiveRechitVector.z());
84 
85 
86  GlobalVector distance_vector = effectiveRechitPoint-caloPoint;
87 
88  if (dotprod > 0.)
89  {
90  return distance_vector.mag();
91  }
92  else
93  {
94  return 999999.;
95 
96  }
97 
98 }
99 
100 inline double getDistInPlaneTrackDir(const GlobalPoint caloPoint,
101  const GlobalVector caloVector,
102  const GlobalPoint rechitPoint)
103 {
104 
105  // Simplified version of getDistInPlane : no cone "within" Hcal, but
106  // don't assume track direction is origin -> point of hcal
107  // intersection.
108  const GlobalVector caloIntersectVector(caloPoint.x(),
109  caloPoint.y(),
110  caloPoint.z()); //p
111 
112  const GlobalVector caloUnitVector = caloVector.unit();
113  const GlobalVector rechitVector(rechitPoint.x(),
114  rechitPoint.y(),
115  rechitPoint.z());
116  const GlobalVector rechitUnitVector = rechitVector.unit();
117  double dotprod_denominator = caloUnitVector.dot(rechitUnitVector);
118  double dotprod_numerator = caloUnitVector.dot(caloIntersectVector);
119  double rechitdist = dotprod_numerator/dotprod_denominator;
120  const GlobalVector effectiveRechitVector = rechitdist*rechitUnitVector;
121  const GlobalPoint effectiveRechitPoint(effectiveRechitVector.x(),
122  effectiveRechitVector.y(),
123  effectiveRechitVector.z());
124  GlobalVector distance_vector = effectiveRechitPoint-caloPoint;
125  if (dotprod_denominator > 0. && dotprod_numerator > 0.)
126  {
127 
128  return distance_vector.mag();
129  }
130  else
131  {
132  return 999999.;
133  }
134 }
135 
136 
137 
138 inline double getDistInPlane(const GlobalVector trackDirection,
139  const GlobalPoint caloPoint,
140  const GlobalPoint rechitPoint,
141  double coneHeight)
142 {
143 
144  // The iso track candidate hits the Calo (Ecal or Hcal) at "caloPoint"
145  // with direction "trackDirection".
146 
147  // "rechitPoint" is the position of the rechit. We only care about
148  // the direction of the rechit.
149 
150  // Consider the rechitPoint as characterized by angles theta and phi
151  // wrt the origin which points at the calo cell of the rechit. In
152  // some sense the distance along the line theta/phi is arbitrary. A
153  // simplified choice might be to put the rechit at the surface of
154  // the Hcal. Our approach is to see whether/where this line
155  // intersects a cone of height "coneHeight" with vertex at caloPoint
156  // aligned with trackDirection.
157  // To that end, this function returns the distance between the
158  // center of the base of the cone and the intersection of the rechit
159  // line and the plane that contains the base of the cone. This
160  // distance is compared with the radius of the cone outside this
161  // function.
162 
163 
164  // Make vector of length cone height along track direction
165  const GlobalVector heightVector = trackDirection*coneHeight;
166 
167  // Make vector from origin to point where iso track intersects the
168  // calorimeter.
169  const GlobalVector caloIntersectVector(caloPoint.x(),
170  caloPoint.y(),
171  caloPoint.z());
172 
173  // Make vector from origin to point in center of base of cone
174  const GlobalVector coneBaseVector = heightVector+caloIntersectVector;
175 
176 // Make point in center of base of cone
177  const GlobalPoint coneBasePoint(coneBaseVector.x(),
178  coneBaseVector.y(),
179  coneBaseVector.z());
180 
181  // Make unit vector pointing at rechit.
182  const GlobalVector rechitVector(rechitPoint.x(),
183  rechitPoint.y(),
184  rechitPoint.z());
185  const GlobalVector rechitDirection = rechitVector.unit();
186 
187  // Find distance "r" along "rechit line" (with angles theta2 and
188  // phi2) where line intersects plane defined by base of cone.
189 
190  // Definition plane of that contains base of cone:
191  // trackDirection.x() (x - coneBaseVector.x()) +
192  // trackDirection.y() (y - coneBaseVector.y()) +
193  // trackDirection.z() (z - coneBaseVector.z()) = 0
194 
195  // Point P_{rh} where rechit line intersects plane:
196  // (rechitdist sin(theta2) cos(phi2),
197  // rechitdist sin(theta2) cos(phi2),
198  // rechitdist cos(theta2))
199 
200  // Substitute P_{rh} into equation for plane and solve for rechitdist.
201  // rechitDist turns out to be the ratio of dot products:
202 
203  double rechitdist = trackDirection.dot(coneBaseVector)/trackDirection.dot(rechitDirection);
204 
205  // Now find distance between point at center of cone base and point
206  // where the "rechit line" intersects the plane defined by the base
207  // of the cone; i.e. the effectiveRecHitPoint.
208  const GlobalVector effectiveRechitVector = rechitdist*rechitDirection;
209  const GlobalPoint effectiveRechitPoint(effectiveRechitVector.x(),
210  effectiveRechitVector.y(),
211  effectiveRechitVector.z());
212 
213 
214  GlobalVector distance_vector = effectiveRechitPoint-coneBasePoint;
215  return distance_vector.mag();
216 }
217 
218 
219 /* Function to calculate Ecal Energy in Cone (given in cm) */
220 inline double ecalEnergyInCone(const GlobalPoint center, double radius, const EcalRecHitCollection ecalCol, const CaloGeometry *geo)
221 {
222  double eECALcone = 0;
223  std::vector<int> usedHitsEcal;
224  usedHitsEcal.clear();
225 
226  for (std::vector<EcalRecHit>::const_iterator ehit=ecalCol.begin(); ehit!=ecalCol.end(); ehit++)
227  {
228  //This is a precaution for the case when hitCollection contains duplicats.
229  bool hitIsUsed=false;
230  int hitHashedIndex=-10000;
231  if (ehit->id().subdetId()==EcalBarrel)
232  {
233  EBDetId did(ehit->id());
234  hitHashedIndex=did.hashedIndex();
235  }
236  if (ehit->id().subdetId()==EcalEndcap)
237  {
238  EEDetId did(ehit->id());
239  hitHashedIndex=did.hashedIndex();
240  }
241  for (uint32_t i=0; i<usedHitsEcal.size(); i++)
242  {
243  if (usedHitsEcal[i]==hitHashedIndex) hitIsUsed=true;
244  }
245  if (hitIsUsed) continue;
246  usedHitsEcal.push_back(hitHashedIndex);
247  // -----------------------------------------------
248 
249  GlobalPoint pos = geo->getPosition((*ehit).detid());
250 
251  if (getDistInPlaneSimple(center,pos) < radius)
252  {
253  eECALcone += ehit->energy();
254  }
255  }
256  return eECALcone;
257 }
258 
259 /* This is another version of ecalEnergy calculation using the getDistInPlaneTrackDir() */
260 inline double ecalEnergyInCone(const GlobalVector trackMom, const GlobalPoint center, double radius, const EcalRecHitCollection ecalCol, const CaloGeometry *geo)
261 {
262 
263  double eECALcone = 0;
264  std::vector<int> usedHitsEcal;
265  usedHitsEcal.clear();
266  for (std::vector<EcalRecHit>::const_iterator ehit=ecalCol.begin(); ehit!=ecalCol.end(); ehit++)
267  {
268  //This is a precaution for the case when hitCollection contains duplicats.
269  bool hitIsUsed=false;
270  int hitHashedIndex=-10000;
271  if (ehit->id().subdetId()==EcalBarrel)
272  {
273  EBDetId did(ehit->id());
274  hitHashedIndex=did.hashedIndex();
275  }
276  if (ehit->id().subdetId()==EcalEndcap)
277  {
278  EEDetId did(ehit->id());
279  hitHashedIndex=did.hashedIndex();
280  }
281  for (uint32_t i=0; i<usedHitsEcal.size(); i++)
282  {
283  if (usedHitsEcal[i]==hitHashedIndex) hitIsUsed=true;
284  }
285  if (hitIsUsed) continue;
286  usedHitsEcal.push_back(hitHashedIndex);
287  // -----------------------------------------------
288 
289  GlobalPoint pos = geo->getPosition((*ehit).detid());
290 
291  if (getDistInPlaneTrackDir(center, trackMom ,pos) < radius)
292  {
293  eECALcone += ehit->energy();
294  }
295  }
296  return eECALcone;
297 }
298 
299 
300 
301 #endif
302 
int i
Definition: DBlmapReader.cc:9
int hashedIndex() const
get a compact index for arrays
Definition: EBDetId.h:86
GlobalPoint posMax
double ecalEnergyInCone(const GlobalPoint center, double radius, const EcalRecHitCollection ecalCol, const CaloGeometry *geo)
double getDistInPlaneTrackDir(const GlobalPoint caloPoint, const GlobalVector caloVector, const GlobalPoint rechitPoint)
T y() const
Definition: PV3DBase.h:63
PreciseFloatType< T, U >::Type dot(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:107
T mag() const
Definition: PV3DBase.h:67
double getDistInPlane(const GlobalVector trackDirection, const GlobalPoint caloPoint, const GlobalPoint rechitPoint, double coneHeight)
T z() const
Definition: PV3DBase.h:64
const GlobalPoint & getPosition(const DetId &id) const
Get the position of a given detector id.
Definition: CaloGeometry.cc:68
const_iterator end() const
Vector3DBase unit() const
Definition: Vector3DBase.h:57
int hashedIndex() const
Definition: EEDetId.h:182
double getDistInPlaneSimple(const GlobalPoint caloPoint, const GlobalPoint rechitPoint)
Definition: ConeDefinition.h:9
T x() const
Definition: PV3DBase.h:62
const_iterator begin() const