CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Constraint_Intermed.cc
Go to the documentation of this file.
1 //
2 // $Id: Constraint_Intermed.cc,v 1.1 2011/05/26 09:46:59 mseidel Exp $
3 //
4 // File: src/Constraint_Intermed.cc
5 // Purpose: Represent one side of a mass constraint equation.
6 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
7 //
8 // CMSSW File : src/Constraint_Intermed.cc
9 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
10 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
11 //
12 
13 
40 #include <iostream>
41 #include <cmath>
42 #include <algorithm>
43 #include <cstdlib>
44 
45 
46 using std::auto_ptr;
47 using std::ostream;
48 using std::sqrt;
49 using std::stable_sort;
50 using std::find;
51 using std::swap;
52 using std::string;
53 using std::vector;
54 #ifndef __GNUC__
55 using std::atoi;
56 using std::atof;
57 #endif
58 
59 
60 namespace hitfit {
61 
62 
63 //************************************************************************
64 
65 
67 //
68 // Purpose: Constructor.
69 //
70 // Inputs:
71 // constant - The constant in the mass constraint half.
72 //
73  : _c2 (constant * constant / 2)
74 {
75 }
76 
77 
80 //
81 // Purpose: Copy constructor.
82 //
83 // Inputs:
84 // c - The instance to copy.
85 //
86  : _c2 (c._c2)
87 {
88 }
89 
90 
91 bool Constraint_Intermed_Constant::has_labels (int ilabel, int jlabel) const
92 //
93 // Purpose: Return true if this guy references both labels ILABEL and JLABEL.
94 //
95 // This version always returns false.
96 //
97 // Inputs:
98 // ilabel - The first label to test.
99 // jlabel - The second label to test.
100 //
101 // Returns:
102 // True if this guy references both labels ILABEL and JLABEL.
103 //
104 {
105  return false;
106 }
107 
108 
109 double
111 //
112 // Purpose: Evaluate this half of the mass constraint, using the data in EV.
113 // Return m^2/2.
114 //
115 // Inputs:
116 // ev - The event for which the constraint should be evaluated.
117 //
118 // Returns:
119 // m^2/2.
120 //
121 {
122  return _c2;
123 }
124 
125 
126 void Constraint_Intermed_Constant::print (std::ostream& s) const
127 //
128 // Purpose: Print out this object.
129 //
130 // Inputs:
131 // s - The stream to which we should write.
132 //
133 {
134  s << sqrt (2 * _c2);
135 }
136 
137 
138 auto_ptr<Constraint_Intermed> Constraint_Intermed_Constant::clone () const
139 //
140 // Purpose: Copy this object.
141 //
142 // Returns:
143 // A new copy of this object.
144 //
145 {
146  return auto_ptr<Constraint_Intermed>
147  (new Constraint_Intermed_Constant (*this));
148 }
149 
150 
151 //************************************************************************
152 
153 
155  (const std::vector<int>& labels)
156 //
157 // Purpose: Constructor.
158 //
159 // Inputs:
160 // labels - The labels used by this half-constraint.
161 //
162  : _labels (labels)
163 {
164  // Sort them.
165  stable_sort (_labels.begin(), _labels.end());
166 }
167 
168 
171 //
172 // Purpose: Copy constructor.
173 //
174 // Inputs:
175 // c - The instance to copy.
176 //
177  : _labels (c._labels)
178 {
179 }
180 
181 
182 bool Constraint_Intermed_Labels::has_labels (int ilabel, int jlabel) const
183 //
184 // Purpose: Return true if this guy references both labels ILABEL and JLABEL.
185 //
186 // Inputs:
187 // ilabel - The first label to test.
188 // jlabel - The second label to test.
189 //
190 // Returns:
191 // True if this guy references both labels ILABEL and JLABEL.
192 //
193 {
194  if (ilabel > jlabel)
195  swap (ilabel, jlabel);
196 
197  unsigned sz = _labels.size();
198  unsigned i;
199  for (i=0; i < sz; i++) {
200  if (_labels[i] == ilabel)
201  break;
202  }
203 
204  if (i == sz)
205  return false;
206 
207  for (; i < sz; i++) {
208  if (_labels[i] == jlabel)
209  break;
210  }
211 
212  if (i == sz)
213  return false;
214 
215  return true;
216 }
217 
218 
219 double
221 //
222 // Purpose: Evaluate this half of the mass constraint, using the data in EV.
223 // Return m^2/2.
224 //
225 // Inputs:
226 // ev - The event for which the constraint should be evaluated.
227 //
228 // Returns:
229 // m^2/2.
230 //
231 {
232  int nobjs = ev.nobjs();
233  double sum = 0;
234  for (int i = 0; i < nobjs; i++) {
235  const FE_Obj& o = ev.obj (i);
236  if (has_label (o.label))
237  sum += o.mass * o.mass / 2;
238  }
239 
240  return sum;
241 }
242 
243 
244 void Constraint_Intermed_Labels::print (std::ostream& s) const
245 //
246 // Purpose: Print out this object.
247 //
248 // Inputs:
249 // s - The stream to which we should write.
250 //
251 {
252  s << "(";
253  for (unsigned i = 0; i < _labels.size(); i++) {
254  if (i > 0)
255  s << "+";
256  s << _labels[i];
257  }
258  s << ")";
259 }
260 
261 
263 //
264 // Purpose: Helper function: Test to see if we use label LABEL.
265 //
266 // Inputs:
267 // label - THe label for which to search.
268 //
269 // Returns:
270 // True if we use label LABEL.
271 //
272 {
273  return find (_labels.begin(), _labels.end(), label) != _labels.end();
274 }
275 
276 
277 auto_ptr<Constraint_Intermed> Constraint_Intermed_Labels::clone () const
278 //
279 // Purpose: Copy this object.
280 //
281 // Returns:
282 // A new copy of this object.
283 //
284 {
285  return auto_ptr<Constraint_Intermed>
286  (new Constraint_Intermed_Labels (*this));
287 }
288 
289 
290 //************************************************************************
291 
292 
304 std::ostream& operator<< (std::ostream& s, const hitfit::Constraint_Intermed& ci)
305 //
306 // Purpose: Print the object to S.
307 //
308 // Inputs:
309 // s - The stream to which to write.
310 // ci - The object to write.
311 //
312 // Returns:
313 // The stream S.
314 //
315 {
316  ci.print (s);
317  return s;
318 }
319 
320 
321 auto_ptr<Constraint_Intermed> make_constraint_intermed (string s)
322 //
323 // Purpose: Parse the string S and return an appropriate
324 // Constraint_Intermed instance.
325 // Returns null if we can't interpret the string.
326 //
327 // The string should either be a numeric constant like
328 //
329 // 80.2
330 //
331 // or a list of integers in parens, like
332 //
333 // (1 4 2)
334 //
335 // Leading spaces are ignored, as is text in a leading < >
336 // construction.
337 //
338 // Inputs:
339 // s - The string to parse.
340 //
341 // Returns:
342 // A new Constraint_Intermed instance, or null if we couldn't
343 // interpret the string.
344 //
345 {
346  // Skip leading spaces, `=', '< ... >'.
347  string::size_type i = 0;
348  while (i < s.size() && s[i] == ' ')
349  ++i;
350  if (s[i] == '=')
351  ++i;
352  while (i < s.size() && s[i] == ' ')
353  ++i;
354  if (i < s.size() && s[i] == '<') {
355  i = s.find ('>', i);
356  if (i == string::npos)
357  return auto_ptr<Constraint_Intermed> ();
358  ++i;
359  }
360  while (i < s.size() && s[i] == ' ')
361  ++i;
362 
363  // Fail if there's nothing left.
364  if (i == s.size())
365  return auto_ptr<Constraint_Intermed> ();
366 
367  if (s[i] == '(') {
368  // List of labels.
369  // Make a Constraint_Intermed_Labels instance.
370  vector<int> labels;
371  ++i;
372  while (i < s.size()) {
373  while (i < s.size() && s[i] == ' ')
374  ++i;
375  if (i < s.size() && s[i] == ')')
376  break;
377  if (i < s.size())
378  labels.push_back (atoi (s.c_str() + i));
379  while (i < s.size() && s[i] != ' ' && s[i] != ')')
380  ++i;
381  }
382  return auto_ptr<Constraint_Intermed>
383  (new Constraint_Intermed_Labels (labels));
384  }
385  else {
386  // Make a Constraint_Intermed_Constant instance.
387  return auto_ptr<Constraint_Intermed>
388  (new Constraint_Intermed_Constant (atof (s.c_str() + i)));
389  }
390 }
391 
392 
393 } // namespace hitfit
394 
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
int i
Definition: DBlmapReader.cc:9
Concrete class for one side of mass constraint equation of the type: .
std::auto_ptr< Constraint_Intermed > make_constraint_intermed(std::string s)
Represent an event for kinematic fitting as a collection of four-momenta. Each object is represented ...
Concrete class for one side of mass constraint equation of the type: .
virtual void print(std::ostream &s) const =0
virtual double sum_mass_terms(const Fourvec_Event &ev) const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual void print(std::ostream &s) const
uint16_t size_type
Constraint_Intermed_Labels(const std::vector< int > &labels)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
T sqrt(T t)
Definition: SSEVec.h:46
virtual bool has_labels(int ilabel, int jlabel) const
virtual void print(std::ostream &s) const
string const
Definition: compareJSON.py:14
virtual std::auto_ptr< Constraint_Intermed > clone() const
std::ostream & operator<<(std::ostream &s, const Constraint_Intermed &ci)
Output stream operator, print the content of this Constraint_Intermed to an output stream...
Abstract base classes for describing one side of a mass constraint.
Represent a single object in a Fourvec_Event, this is just a dumb data container. Each object in a Fo...
Definition: Fourvec_Event.h:95
Represent one side of a mass constraint equation. Contains the abstract base class Constraint_Interme...
virtual double sum_mass_terms(const Fourvec_Event &ev) const
virtual std::auto_ptr< Constraint_Intermed > clone() const
virtual bool has_labels(int ilabel, int jlabel) const
Represent an event for kinematic fitting as a collection of four-momenta.