Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:19

0001 //
0002 //
0003 // File: src/Vector_Resolution.cc
0004 // Purpose: Calculate resolutions in p, phi, eta.
0005 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
0006 //
0007 // CMSSW File      : src/Vector_Resolution.cc
0008 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
0009 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
0010 //
0011 
0012 /**
0013     @file Vector_Resolution.cc
0014 
0015     @brief Calculate and represent resolution for a vector
0016     of momentum \f$p\f$, pseudorapidity \f$\eta\f$, and azimuthal
0017     angle \f$\phi\f$. See the documentation for the header file
0018     Vector_Resolution.h for details.
0019 
0020     @author Scott Stuart Snyder <snyder@bnl.gov>
0021 
0022     @par Creation date:
0023     Jul 2000.
0024 
0025     @par Modification History:
0026     Apr 2009: Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>:
0027     Imported to CMSSW.<br>
0028     Nov 2009: Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>:
0029     Added doxygen tags for automatic generation of documentation.
0030 
0031     @par Terms of Usage:
0032     With consent for the original author (Scott Snyder).
0033 
0034  */
0035 
0036 #include "TopQuarkAnalysis/TopHitFit/interface/Vector_Resolution.h"
0037 #include "TopQuarkAnalysis/TopHitFit/interface/fourvec.h"
0038 #include <cmath>
0039 #include <ostream>
0040 #include <cctype>
0041 
0042 using std::isspace;
0043 using std::ostream;
0044 using std::string;
0045 
0046 namespace {
0047 
0048   /**
0049     @brief Helper function: extract the <i>i</i>-th slash-separated field
0050     from a string.
0051 
0052     @param s The string to analyze.
0053 
0054     @param i The field number, starting from 0.
0055  */
0056   string field(string s, int i)
0057   //
0058   // Purpose: Extract the Ith slash-separated field from S.
0059   //
0060   // Inputs:
0061   //   s -           The string to analyze.
0062   //   i -           Field number (starting with 0).
0063   //
0064   // Returns:
0065   //   Field I of S.
0066   //
0067   {
0068     string::size_type pos = 0;
0069     while (i > 0) {
0070       pos = s.find('/', pos);
0071       if (pos == string::npos)
0072         return "";
0073       ++pos;
0074       --i;
0075     }
0076 
0077     string::size_type pos2 = s.find('/', pos + 1);
0078     if (pos2 != string::npos)
0079       pos2 = pos2 - pos;
0080 
0081     while (pos < pos2 && isspace(s[pos]))
0082       ++pos;
0083 
0084     return s.substr(pos, pos2);
0085   }
0086 
0087 }  // unnamed namespace
0088 
0089 namespace hitfit {
0090 
0091   Vector_Resolution::Vector_Resolution()
0092       //
0093       // Purpose: Default constructor.  Create a vector resolution object with
0094       //          infinite precision.v
0095       //
0096       : _p_res("0,0,0"),
0097         _eta_res("0,0,0"),
0098         _phi_res("0,0,0"),
0099         _use_et(false)
0100 
0101   {}
0102 
0103   Vector_Resolution::Vector_Resolution(std::string s)
0104       //
0105       // Purpose: Constructor.
0106       //
0107       // Inputs:
0108       //   s -           String encoding the resolution parameters, as described
0109       //                 in the comments in the header.
0110       //
0111       : _p_res(field(s, 0)), _eta_res(field(s, 1)), _phi_res(field(s, 2)), _use_et(field(s, 3) == "et") {}
0112 
0113   Vector_Resolution::Vector_Resolution(const Resolution& p_res,
0114                                        const Resolution& eta_res,
0115                                        const Resolution& phi_res,
0116                                        bool use_et /*= false*/)
0117       //
0118       // Purpose: Constructor from individual resolution objects.
0119       //
0120       // Inputs:
0121       //   p_res -       Momentum resolution object.
0122       //   eta_res -     Eta resolution object.
0123       //   phi_res -     Phi resolution object.
0124       //   use_et -      If true, momentum resolution is based on pt, not p.
0125       //
0126       : _p_res(p_res), _eta_res(eta_res), _phi_res(phi_res), _use_et(use_et) {}
0127 
0128   const Resolution& Vector_Resolution::p_res() const
0129   //
0130   // Purpose: Return the momentum resolution object.
0131   //
0132   // Returns:
0133   //   The momentum resolution object.
0134   //
0135   {
0136     return _p_res;
0137   }
0138 
0139   const Resolution& Vector_Resolution::eta_res() const
0140   //
0141   // Purpose: Return the eta resolution object.
0142   //
0143   // Returns:
0144   //   The eta resolution object.
0145   //
0146   {
0147     return _eta_res;
0148   }
0149 
0150   const Resolution& Vector_Resolution::phi_res() const
0151   //
0152   // Purpose: Return the phi resolution object.
0153   //
0154   // Returns:
0155   //   The phi resolution object.
0156   //
0157   {
0158     return _phi_res;
0159   }
0160 
0161   bool Vector_Resolution::use_et() const
0162   //
0163   // Purpose: Return the use_et flag.
0164   //
0165   // Returns:
0166   //   The use_et flag.
0167   //
0168   {
0169     return _use_et;
0170   }
0171 
0172   namespace {
0173 
0174     /**
0175     @brief Helper function: calculate the uncertainty/resolution \f$\sigma\f$
0176     for the momentum of a four-momentum.
0177 
0178     @param v The four-momentum.
0179 
0180     @param res The resolution object.
0181 
0182     @param use_et If <b>TRUE</b> then use \f$p_{T}\f$ instead of \f$p\f$
0183     for momentum resolution.
0184  */
0185     double find_sigma(const Fourvec& v, const Resolution& res, bool use_et)
0186     //
0187     // Purpose: Helper for *_sigma functions below.
0188     //
0189     // Inputs:
0190     //   v -           The 4-vector.
0191     //   res -         The resolution object.
0192     //   use_et -      Use_et flag.
0193     //
0194     // Returns:
0195     //   The result of res.sigma() (not corrected for e/et difference).
0196     //
0197     {
0198       double ee = use_et ? v.perp() : v.e();  // ??? is perp() correct here?
0199       return res.sigma(ee);
0200     }
0201 
0202   }  // unnamed namespace
0203 
0204   double Vector_Resolution::p_sigma(const Fourvec& v) const
0205   //
0206   // Purpose: Calculate momentum resolution for 4-vector V.
0207   //
0208   // Inputs:
0209   //   v -           The 4-vector.
0210   //
0211   // Returns:
0212   //   The momentum resolution for 4-vector V.
0213   //
0214   {
0215     double sig = find_sigma(v, _p_res, _use_et);
0216     if (_use_et) {
0217       if (_p_res.inverse()) {
0218         sig *= v.perp() / v.e();
0219       } else {
0220         sig *= v.e() / v.perp();
0221       }
0222     }
0223     return sig;
0224   }
0225 
0226   double Vector_Resolution::eta_sigma(const Fourvec& v) const
0227   //
0228   // Purpose: Calculate eta resolution for 4-vector V.
0229   //
0230   // Inputs:
0231   //   v -           The 4-vector.
0232   //
0233   // Returns:
0234   //   The eta resolution for 4-vector V.
0235   //
0236   {
0237     return find_sigma(v, _eta_res, _use_et);
0238   }
0239 
0240   double Vector_Resolution::phi_sigma(const Fourvec& v) const
0241   //
0242   // Purpose: Calculate phi resolution for 4-vector V.
0243   //
0244   // Inputs:
0245   //   v -           The 4-vector.
0246   //
0247   // Returns:
0248   //   The phi resolution for 4-vector V.
0249   //
0250   {
0251     return find_sigma(v, _phi_res, _use_et);
0252   }
0253 
0254   namespace {
0255 
0256     /**
0257     @brief Helper function: smear the pseudorapidity of a four-momentum
0258 
0259     @param v The four-momentum to smear.
0260 
0261     @param ee The energy for \f$sigma\f$ calculation.
0262 
0263     @param res The resolution object, giving the amount of smearing.
0264 
0265     @param engine The underlying random number generator.
0266  */
0267     void smear_eta(Fourvec& v, double ee, const Resolution& res, CLHEP::HepRandomEngine& engine)
0268     //
0269     // Purpose: Smear the eta direction of V.
0270     //
0271     // Inputs:
0272     //   v -           The 4-vector to smear.
0273     //   ee -          Energy for sigma calculation.
0274     //   res -         The resolution object, giving the amount of smearing.
0275     //   engine -      The underlying RNG.
0276     //
0277     // Outputs:
0278     //   v -           The smeared 4-vector.
0279     //
0280     {
0281       double rot = res.pick(0, ee, engine);
0282       roteta(v, rot);
0283     }
0284 
0285     /**
0286     @brief Helper function: smear the azimuthal angle of a four-momentum
0287 
0288     @param v The four-momentum to smear.
0289 
0290     @param ee The energy for \f$sigma\f$ calculation.
0291 
0292     @param res The resolution object, giving the amount of smearing.
0293 
0294     @param engine The underlying random number generator.
0295  */
0296     void smear_phi(Fourvec& v, double ee, const Resolution& res, CLHEP::HepRandomEngine& engine)
0297     //
0298     // Purpose: Smear the phi direction of V.
0299     //
0300     // Inputs:
0301     //   v -           The 4-vector to smear.
0302     //   ee -          Energy for sigma calculation.
0303     //   res -         The resolution object, giving the amount of smearing.
0304     //   engine -      The underlying RNG.
0305     //
0306     // Outputs:
0307     //   v -           The smeared 4-vector.
0308     //
0309     {
0310       double rot = res.pick(0, ee, engine);
0311       v.rotateZ(rot);
0312     }
0313 
0314   }  // unnamed namespace
0315 
0316   void Vector_Resolution::smear(Fourvec& v, CLHEP::HepRandomEngine& engine, bool do_smear_dir /*= false*/) const
0317   //
0318   // Purpose: Smear a 4-vector according to the resolutions.
0319   //
0320   // Inputs:
0321   //   v -           The 4-vector to smear.
0322   //   engine -      The underlying RNG.
0323   //   do_smear_dir- If false, only smear the energy.
0324   //
0325   // Outputs:
0326   //   v -           The smeared 4-vector.
0327   //
0328   {
0329     double ee = _use_et ? v.perp() : v.e();  // ??? is perp() correct?
0330     v *= _p_res.pick(ee, ee, engine) / ee;
0331 
0332     if (do_smear_dir) {
0333       smear_eta(v, ee, _eta_res, engine);
0334       smear_phi(v, ee, _phi_res, engine);
0335     }
0336   }
0337 
0338   /**
0339     @brief Output stream operator, print the content of this Vector_Resolution
0340     object to an output stream.
0341 
0342     @param s The stream to which to write.
0343 
0344     @param r The instance of Vector_Resolution to be printed.
0345  */
0346   std::ostream& operator<<(std::ostream& s, const Vector_Resolution& r)
0347   //
0348   // Purpose: Dump this object to S.
0349   //
0350   // Inputs:
0351   //    s -          The stream to which to write.
0352   //    r -          The object to dump.
0353   //
0354   // Returns:
0355   //   The stream S.
0356   //
0357   {
0358     s << r._p_res << "/ " << r._eta_res << "/ " << r._phi_res;
0359     if (r._use_et)
0360       s << "/et";
0361     s << "\n";
0362     return s;
0363   }
0364 
0365 }  // namespace hitfit