CMS 3D CMS Logo

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