CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/TopQuarkAnalysis/TopHitFit/src/Constraint_Intermed.cc

Go to the documentation of this file.
00001 //
00002 // $Id: Constraint_Intermed.cc,v 1.1 2011/05/26 09:46:59 mseidel Exp $
00003 //
00004 // File: src/Constraint_Intermed.cc
00005 // Purpose: Represent one side of a mass constraint equation.
00006 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
00007 //
00008 // CMSSW File      : src/Constraint_Intermed.cc
00009 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
00010 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
00011 //
00012 
00013 
00038 #include "TopQuarkAnalysis/TopHitFit/interface/Constraint_Intermed.h"
00039 #include "TopQuarkAnalysis/TopHitFit/interface/Fourvec_Event.h"
00040 #include <iostream>
00041 #include <cmath>
00042 #include <algorithm>
00043 #include <cstdlib>
00044 
00045 
00046 using std::auto_ptr;
00047 using std::ostream;
00048 using std::sqrt;
00049 using std::stable_sort;
00050 using std::find;
00051 using std::swap;
00052 using std::string;
00053 using std::vector;
00054 #ifndef __GNUC__
00055 using std::atoi;
00056 using std::atof;
00057 #endif
00058 
00059 
00060 namespace hitfit {
00061 
00062 
00063 //************************************************************************
00064 
00065 
00066 Constraint_Intermed_Constant::Constraint_Intermed_Constant (double constant)
00067 //
00068 // Purpose: Constructor.
00069 //
00070 // Inputs:
00071 //   constant -    The constant in the mass constraint half.
00072 //
00073   : _c2 (constant * constant / 2)
00074 {
00075 }
00076 
00077 
00078 Constraint_Intermed_Constant::Constraint_Intermed_Constant
00079  (const Constraint_Intermed_Constant& c)
00080 //
00081 // Purpose: Copy constructor.
00082 //
00083 // Inputs:
00084 //   c -           The instance to copy.
00085 //
00086    : _c2 (c._c2)
00087 {
00088 }
00089 
00090 
00091 bool Constraint_Intermed_Constant::has_labels (int ilabel, int jlabel) const
00092 //
00093 // Purpose: Return true if this guy references both labels ILABEL and JLABEL.
00094 //
00095 //          This version always returns false.
00096 //
00097 // Inputs:
00098 //   ilabel -      The first label to test.
00099 //   jlabel -      The second label to test.
00100 //
00101 // Returns:
00102 //   True if this guy references both labels ILABEL and JLABEL.
00103 //
00104 {
00105   return false;
00106 }
00107 
00108 
00109 double
00110 Constraint_Intermed_Constant::sum_mass_terms (const Fourvec_Event&/*ev*/) const
00111 //
00112 // Purpose: Evaluate this half of the mass constraint, using the data in EV.
00113 //          Return m^2/2.
00114 //
00115 // Inputs:
00116 //   ev -          The event for which the constraint should be evaluated.
00117 //
00118 // Returns:
00119 //   m^2/2.
00120 //
00121 {
00122   return _c2;
00123 }
00124 
00125 
00126 void Constraint_Intermed_Constant::print (std::ostream& s) const
00127 //
00128 // Purpose: Print out this object.
00129 //
00130 // Inputs:
00131 //   s -           The stream to which we should write.
00132 //
00133 {
00134   s << sqrt (2 * _c2);
00135 }
00136 
00137 
00138 auto_ptr<Constraint_Intermed> Constraint_Intermed_Constant::clone () const
00139 //
00140 // Purpose: Copy this object.
00141 //
00142 // Returns:
00143 //   A new copy of this object.
00144 //
00145 {
00146   return auto_ptr<Constraint_Intermed>
00147     (new Constraint_Intermed_Constant (*this));
00148 }
00149 
00150 
00151 //************************************************************************
00152 
00153 
00154 Constraint_Intermed_Labels::Constraint_Intermed_Labels
00155  (const std::vector<int>& labels)
00156 //
00157 // Purpose: Constructor.
00158 //
00159 // Inputs:
00160 //   labels -      The labels used by this half-constraint.
00161 //
00162   : _labels (labels)
00163 {
00164   // Sort them.
00165   stable_sort (_labels.begin(), _labels.end());
00166 }
00167 
00168 
00169 Constraint_Intermed_Labels::Constraint_Intermed_Labels
00170  (const Constraint_Intermed_Labels& c)
00171 //
00172 // Purpose: Copy constructor.
00173 //
00174 // Inputs:
00175 //   c -           The instance to copy.
00176 //
00177    : _labels (c._labels)
00178 {
00179 }
00180 
00181 
00182 bool Constraint_Intermed_Labels::has_labels (int ilabel, int jlabel) const
00183 //
00184 // Purpose: Return true if this guy references both labels ILABEL and JLABEL.
00185 //
00186 // Inputs:
00187 //   ilabel -      The first label to test.
00188 //   jlabel -      The second label to test.
00189 //
00190 // Returns:
00191 //   True if this guy references both labels ILABEL and JLABEL.
00192 //
00193 {
00194   if (ilabel > jlabel)
00195     swap (ilabel, jlabel);
00196 
00197   unsigned sz = _labels.size();
00198   unsigned i;
00199   for (i=0; i < sz; i++) {
00200     if (_labels[i] == ilabel)
00201       break;
00202   }
00203 
00204   if (i == sz)
00205     return false;
00206 
00207   for (; i < sz; i++) {
00208     if (_labels[i] == jlabel)
00209       break;
00210   }
00211 
00212   if (i == sz)
00213     return false;
00214 
00215   return true;
00216 }
00217 
00218 
00219 double
00220 Constraint_Intermed_Labels::sum_mass_terms (const Fourvec_Event& ev) const
00221 //
00222 // Purpose: Evaluate this half of the mass constraint, using the data in EV.
00223 //          Return m^2/2.
00224 //
00225 // Inputs:
00226 //   ev -          The event for which the constraint should be evaluated.
00227 //
00228 // Returns:
00229 //   m^2/2.
00230 //
00231 {
00232   int nobjs = ev.nobjs();
00233   double sum = 0;
00234   for (int i = 0; i < nobjs; i++) {
00235     const FE_Obj& o = ev.obj (i);
00236     if (has_label (o.label))
00237       sum += o.mass * o.mass / 2;
00238   }
00239 
00240   return sum;
00241 }
00242 
00243 
00244 void Constraint_Intermed_Labels::print (std::ostream& s) const
00245 //
00246 // Purpose: Print out this object.
00247 //
00248 // Inputs:
00249 //   s -           The stream to which we should write.
00250 //
00251 {
00252   s << "(";
00253   for (unsigned i = 0; i < _labels.size(); i++) {
00254     if (i > 0)
00255       s << "+";
00256     s << _labels[i];
00257   }
00258   s << ")";
00259 }
00260 
00261 
00262 bool Constraint_Intermed_Labels::has_label (int label) const
00263 //
00264 // Purpose: Helper function: Test to see if we use label LABEL.
00265 //
00266 // Inputs:
00267 //   label -       THe label for which to search.
00268 //
00269 // Returns:
00270 //   True if we use label LABEL.
00271 //
00272 {
00273   return find (_labels.begin(), _labels.end(), label) != _labels.end();
00274 }
00275 
00276 
00277 auto_ptr<Constraint_Intermed> Constraint_Intermed_Labels::clone () const
00278 //
00279 // Purpose: Copy this object.
00280 //
00281 // Returns:
00282 //   A new copy of this object.
00283 //
00284 {
00285   return auto_ptr<Constraint_Intermed> 
00286     (new Constraint_Intermed_Labels (*this));
00287 }
00288 
00289 
00290 //************************************************************************
00291 
00292 
00304 std::ostream& operator<< (std::ostream& s, const hitfit::Constraint_Intermed& ci)
00305 //
00306 // Purpose: Print the object to S.
00307 //
00308 // Inputs:
00309 //   s -           The stream to which to write.
00310 //   ci -          The object to write.
00311 //
00312 // Returns:
00313 //   The stream S.
00314 //
00315 {
00316   ci.print (s);
00317   return s;
00318 }
00319 
00320 
00321 auto_ptr<Constraint_Intermed> make_constraint_intermed (string s)
00322 //
00323 // Purpose: Parse the string S and return an appropriate
00324 //          Constraint_Intermed instance.
00325 //          Returns null if we can't interpret the string.
00326 //
00327 //          The string should either be a numeric constant like
00328 //
00329 //            80.2
00330 //
00331 //          or a list of integers in parens, like
00332 //
00333 //            (1 4 2)
00334 //
00335 //          Leading spaces are ignored, as is text in a leading < >
00336 //          construction.
00337 //
00338 // Inputs:
00339 //   s -           The string to parse.
00340 //
00341 // Returns:
00342 //   A new Constraint_Intermed instance, or null if we couldn't
00343 //   interpret the string.
00344 //
00345 {
00346   // Skip leading spaces, `=', '< ... >'.
00347   string::size_type i = 0;
00348   while (i < s.size() && s[i] == ' ')
00349     ++i;
00350   if (s[i] == '=')
00351     ++i;
00352   while (i < s.size() && s[i] == ' ')
00353     ++i;
00354   if (i < s.size() && s[i] == '<') {
00355     i = s.find ('>', i);
00356     if (i == string::npos)
00357       return auto_ptr<Constraint_Intermed> ();
00358     ++i;
00359   }
00360   while (i < s.size() && s[i] == ' ')
00361     ++i;
00362 
00363   // Fail if there's nothing left.
00364   if (i == s.size())
00365     return auto_ptr<Constraint_Intermed> ();
00366 
00367   if (s[i] == '(') {
00368     // List of labels.
00369     // Make a Constraint_Intermed_Labels instance.
00370     vector<int> labels;
00371     ++i;
00372     while (i < s.size()) {
00373       while (i < s.size() && s[i] == ' ')
00374         ++i;
00375       if (i < s.size() && s[i] == ')')
00376         break;
00377       if (i < s.size())
00378         labels.push_back (atoi (s.c_str() + i));
00379       while (i < s.size() && s[i] != ' ' && s[i] != ')')
00380         ++i;
00381     }
00382     return auto_ptr<Constraint_Intermed>
00383       (new Constraint_Intermed_Labels (labels));
00384   }
00385   else {
00386     // Make a Constraint_Intermed_Constant instance.
00387     return auto_ptr<Constraint_Intermed>
00388       (new Constraint_Intermed_Constant (atof (s.c_str() + i)));
00389   }
00390 }
00391 
00392 
00393 } // namespace hitfit
00394