Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 //
0003 // File: src/Fit_Result_Vec.cc
0004 // Purpose: Hold a set of Fit_Result structures.
0005 // Created: Jul, 2000, sss, based on run 1 mass analysis code.
0006 //
0007 // CMSSW File      : src/Fit_Result_Vec.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 Fit_Result_Vec.cc
0014 
0015     @brief Hold a list of pointers to a set of
0016     Fit_Result objects, resulting from different jet permutation with
0017     some consistent selection.  See the documentation for the header file
0018     Fit_Result_Vec.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 from the original author (Scott Snyder).
0033 
0034  */
0035 
0036 #include "TopQuarkAnalysis/TopHitFit/interface/Fit_Result_Vec.h"
0037 #include "TopQuarkAnalysis/TopHitFit/interface/Fit_Result.h"
0038 #include <cassert>
0039 #include <ostream>
0040 #include <algorithm>
0041 
0042 using std::lower_bound;
0043 using std::ostream;
0044 using std::vector;
0045 
0046 namespace hitfit {
0047 
0048   Fit_Result_Vec::Fit_Result_Vec(std::vector<std::shared_ptr<Fit_Result>>::size_type max_len)
0049       //
0050       // Purpose Constructor.
0051       //
0052       // Inputs:
0053       //   max_len -     The maximum length of the vector.
0054       //
0055       : _max_len(max_len) {
0056     assert(max_len > 0);
0057     _v.reserve(max_len + 1);
0058   }
0059 
0060   Fit_Result_Vec::Fit_Result_Vec(const Fit_Result_Vec& vec)
0061       //
0062       // Purpose: Copy constructor.
0063       //
0064       // Inputs:
0065       //   vec -         The vector to copy.
0066       //
0067       : _v(vec._v), _max_len(vec._max_len) {}
0068 
0069   Fit_Result_Vec::~Fit_Result_Vec()
0070   //
0071   // Purpose: Destructor.
0072   //
0073   {}
0074 
0075   Fit_Result_Vec& Fit_Result_Vec::operator=(const Fit_Result_Vec& vec)
0076   //
0077   // Purpose: Assignment.
0078   //
0079   // Inputs:
0080   //   vec -         The vector to copy.
0081   //
0082   // Returns:
0083   //   This object.
0084   //
0085   {
0086     _v = vec._v;
0087     _max_len = vec._max_len;
0088     return *this;
0089   }
0090 
0091   std::vector<std::shared_ptr<Fit_Result>>::size_type Fit_Result_Vec::size() const
0092   //
0093   // Purpose: Get back the number of results in the vector.
0094   //
0095   // Returns:
0096   //   The number of results in the vector.
0097   //
0098   {
0099     return _v.size();
0100   }
0101 
0102   const Fit_Result& Fit_Result_Vec::operator[](std::vector<std::shared_ptr<Fit_Result>>::size_type i) const
0103   //
0104   // Purpose: Get back the Ith result in the vector.
0105   //
0106   // Inputs:
0107   //   i -           The index of the desired result.
0108   //
0109   // Returns:
0110   //   The Ith result.
0111   //
0112   {
0113     assert(i < _v.size());
0114     return *_v[i];
0115   }
0116 
0117   namespace {
0118 
0119     struct Compare_Fitresptr
0120     //
0121     // Purpose: Helper for push().
0122     //
0123     {
0124       bool operator()(std::shared_ptr<const Fit_Result> a, std::shared_ptr<const Fit_Result> b) const {
0125         return *a < *b;
0126       }
0127     };
0128 
0129   }  // unnamed namespace
0130 
0131   void Fit_Result_Vec::push(std::shared_ptr<Fit_Result> res)
0132   //
0133   // Purpose: Add a new result to the vector.
0134   //
0135   // Inputs:
0136   //   res -         The result to add.
0137   //
0138   {
0139     // Find where to add it.
0140     vector<std::shared_ptr<Fit_Result>>::iterator it = lower_bound(_v.begin(), _v.end(), res, Compare_Fitresptr());
0141 
0142     // Insert it.
0143     _v.insert(it, res);
0144 
0145     // Knock off the guy at the end if we've exceeded our maximum size.
0146     if (_v.size() > _max_len) {
0147       _v.erase(_v.end() - 1);
0148     }
0149   }
0150 
0151   /**
0152     @brief Output stream operator, print the content of this
0153     Fit_Result_Vec to an output stream.
0154 
0155     @param s The output stream to which to write.
0156 
0157     @param resvec The instance of Fit_Result_Vec to be printed.
0158 */
0159   std::ostream& operator<<(std::ostream& s, const Fit_Result_Vec& resvec)
0160   //
0161   // Purpose: Print the object to S.
0162   //
0163   // Inputs:
0164   //   s -           The stream to which to write.
0165   //   resvec -      The object to write.
0166   //
0167   // Returns:
0168   //   The stream S.
0169   //
0170   {
0171     for (std::vector<std::shared_ptr<Fit_Result>>::size_type i = 0; i < resvec._v.size(); i++)
0172       s << "Entry " << i << "\n" << *resvec._v[i];
0173     return s;
0174   }
0175 
0176 }  // namespace hitfit