CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CommonTools/Clustering1D/interface/MtvClusterizer1D.h

Go to the documentation of this file.
00001 #ifndef _MtvClusterizer1D_H_
00002 #define _MtvClusterizer1D_H_
00003 
00004 #include "CommonTools/Clustering1D/interface/Clusterizer1D.h"
00005 #include "CommonTools/Clustering1D/interface/Clusterizer1DCommons.h"
00006 #include "CommonTools/Clustering1D/interface/TrivialWeightEstimator.h"
00007 #include "CommonTools/Clustering1D/interface/Clustering1DException.h"
00008 
00009 #include <vector>
00010 #include <cmath>
00011 #include <algorithm>
00012 
00016 template <class T>
00017 class MtvClusterizer1D : public Clusterizer1D<T>
00018 {
00019 public:
00020     MtvClusterizer1D ( const WeightEstimator<T> & est = TrivialWeightEstimator<T>() );
00021     MtvClusterizer1D ( const MtvClusterizer1D & );
00022     ~MtvClusterizer1D();
00023 
00024     std::pair < std::vector < Cluster1D<T> >, std::vector < const T * > >
00025     operator() ( const std::vector< Cluster1D<T> > & ) const;
00026 
00027     virtual MtvClusterizer1D * clone() const;
00028 
00029 private:
00030     WeightEstimator<T> * theEstimator;
00031     float theErrorStretchFactor;
00032 };
00033 
00034 /*
00035  *                              --- implementation ---
00036  *
00037  */
00038 
00039 template <class T>
00040 MtvClusterizer1D<T>::MtvClusterizer1D(
00041     const MtvClusterizer1D<T> & o ) : theEstimator ( o.theEstimator->clone() )
00042 {}
00043 
00044 
00045 template <class T>
00046 MtvClusterizer1D<T>::MtvClusterizer1D(
00047     const WeightEstimator<T> & est ) : theEstimator ( est.clone() )
00048 {}
00049 
00050 
00051 template <class T>
00052 MtvClusterizer1D<T>::~MtvClusterizer1D()
00053 {
00054     delete theEstimator;
00055 }
00056 
00057 template <class T>
00058 MtvClusterizer1D<T> * MtvClusterizer1D<T>::clone() const
00059 {
00060     return new MtvClusterizer1D<T> ( * this );
00061 }
00062 
00063 template <class T>
00064 std::pair < std::vector < Cluster1D<T> >, std::vector < const T * > >
00065 MtvClusterizer1D<T>::operator() ( const std::vector < Cluster1D<T> > & ov ) const
00066 {
00067     typedef Cluster1D<T> Cluster1D;
00068     using namespace Clusterizer1DCommons;
00069     std::vector < const T * > unusedtracks;
00070     switch ( ov.size() )
00071     {
00072     case 0:
00073         throw Clustering1DException("[MtvClusterizer1D] no values given" );
00074     case 1:
00075         std::pair < std::vector < Cluster1D >, std::vector < const T * > > ret ( ov, unusedtracks );
00076         return ret;
00077     };
00078     std::vector < Cluster1D > v = ov;
00079     sort ( v.begin(), v.end(), ComparePairs<T>() );
00080     std::vector < Cluster1D > sols;
00081     std::vector < const T * > trks;
00082 
00083     typename std::vector< Cluster1D >::iterator cur = v.begin();
00084     typename std::vector< Cluster1D >::iterator   end = (v.end() - 1 );
00085     double cur_min = cur->weight() + ( cur+1 )->weight();
00086 
00087     for ( typename std::vector< Cluster1D >::iterator i=v.begin();
00088             i!=end ; ++i )
00089     {
00090         double cur_val = i->weight() + ( i+1 )->weight();
00091         if ( cur_val > cur_min )
00092         {
00093             cur_min = cur_val;
00094             cur = i;
00095         };
00096     };
00097 
00098     double weight = ( cur->weight() + (cur+1)->weight() );
00099     double est = ( cur->weight() * cur->position().value() +
00100                    (cur+1)->weight() * (cur+1)->position().value()) / weight;
00101     double sigma = sqrt ( square ( cur->position().value() - est ) +
00102                           square ( (cur+1)->position().value() - est ) );
00103     double err=0.;
00104     int inliers=0;
00105 
00106     for ( typename std::vector< Cluster1D >::iterator i=v.begin();
00107             i!=v.end() ; ++i )
00108     {
00109         if ( fabs ( i->position().value() - est ) < 3 * sigma )
00110         {
00111             // all within 3 sigma are 'in'
00112             add
00113                 ( i->tracks(), trks );
00114             err+= square ( i->position().value() - est );
00115             inliers++;
00116         }
00117         else
00118         {
00119             add
00120                 ( i->tracks(), unusedtracks );
00121         };
00122     };
00123     err /= ( inliers - 1 ); // the algo definitely produces 2 or more inliers
00124     err = sqrt ( err );
00125 
00126     sols.push_back ( Cluster1D ( Measurement1D ( est,err ), trks, weight ) );
00127     std::pair < std::vector < Cluster1D >, std::vector < const T * > > ret ( sols, unusedtracks );
00128     return ret;
00129 }
00130 
00131 #endif