CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CommonTools/Clustering1D/interface/MultiClusterizer1D.h

Go to the documentation of this file.
00001 #ifndef _MultiClusterizer1D_H_
00002 #define _MultiClusterizer1D_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 #include "CommonTools/Clustering1D/interface/FsmwClusterizer1D.h"
00009 
00010 #include <vector>
00011 #include <map>
00012 #include <cmath>
00013 #include <algorithm>
00014 
00019 template <class T>
00020 class MultiClusterizer1D : public Clusterizer1D<T>
00021 {
00022 public:
00023     //    typedef Cluster1D<T> Cluster1D;
00024     MultiClusterizer1D ( const Clusterizer1D<T> & single,
00025                       const WeightEstimator<T> & est = TrivialWeightEstimator<T>() );
00026     MultiClusterizer1D ( const MultiClusterizer1D & );
00027     ~MultiClusterizer1D();
00028 
00029     std::pair < std::vector < Cluster1D<T> >, std::vector < const T * > >
00030     operator() ( const std::vector< Cluster1D<T> > & ) const;
00031 
00032     virtual MultiClusterizer1D * clone() const;
00033 
00034 private:
00035     WeightEstimator<T> * theEstimator;
00036     Clusterizer1D<T> * theSingle;
00037 };
00038 
00039 /*
00040  *                              --- implementation ---
00041  *
00042  */
00043 
00044 /*
00045 namespace MultiClusterizer1DNameSpace
00046 {
00047 }*/
00048 
00049 template <class T>
00050 MultiClusterizer1D<T>::MultiClusterizer1D( const MultiClusterizer1D<T> & o ) 
00051     : theEstimator( o.theEstimator->clone() ), theSingle ( o.theSingle->clone() )
00052 {}
00053 
00054 template <class T>
00055 MultiClusterizer1D<T>::MultiClusterizer1D( const Clusterizer1D<T> & single
00056     , const WeightEstimator<T> & est ) 
00057     : theEstimator ( est.clone() ), theSingle ( single.clone() )
00058 {}
00059 
00060 template <class T>
00061 MultiClusterizer1D<T>::~MultiClusterizer1D()
00062 {
00063     delete theEstimator;
00064     delete theSingle;
00065 }
00066 
00067 template <class T>
00068 MultiClusterizer1D<T> * MultiClusterizer1D<T>::clone() const
00069 {
00070     return new MultiClusterizer1D<T>( *this );
00071 }
00072 
00073 template <class T>
00074 std::pair < std::vector< Cluster1D<T> >, std::vector< const T * > >
00075 MultiClusterizer1D<T>::operator() ( const std::vector < Cluster1D<T> > & ov ) const
00076 {
00077     using namespace Clusterizer1DCommons;
00078     // using namespace MultiClusterizer1DNameSpace;
00079     typedef Cluster1D<T> Cluster1D;
00080     std::vector < const T * > unusedtracks;
00081     switch ( ov.size() )
00082     {
00083     case 0:
00084        throw Clustering1DException("[MultiClusterizer1D] no values given" );
00085     case 1:
00086        std::pair < std::vector < Cluster1D >, std::vector < const T * > > ret ( ov, unusedtracks );
00087        return ret;
00088     };
00089 
00090     std::pair < std::vector< Cluster1D >, std::vector< const T * > > res;
00091 
00092     // works only with one track per cluster!!!
00093     std::map < const T *, Cluster1D > ass;
00094     std::vector < Cluster1D > cur;
00095 
00096     for ( typename std::vector< Cluster1D >::const_iterator i=ov.begin(); 
00097           i!=ov.end() ; ++i )
00098     {
00099       if ( i->tracks().size()==1 )
00100       {
00101         ass[ i->tracks()[0] ]=*i;
00102       }
00103       cur.push_back ( *i );
00104     }
00105 
00106     int ctr=0;
00107     try {
00108       while ( true )
00109       {
00110         std::pair < std::vector< Cluster1D >, std::vector< const T * > > tmp = (*theSingle)( cur );
00111 
00112         for ( typename std::vector< Cluster1D >::const_iterator i=tmp.first.begin(); 
00113               i!=tmp.first.end() ; ++i )
00114         {
00115           res.first.push_back ( *i );
00116         }
00117         res.second=tmp.second;
00118 
00119         cur.clear();
00120 
00121         for ( typename std::vector< const T * >::const_iterator 
00122               i=res.second.begin(); i!=res.second.end() ; ++i )
00123         {
00124           cur.push_back ( ass[*i] );
00125         }
00126         if ( ctr++ > 5 ) break;
00127         if ( cur.size() < 2 ) break;
00128       }
00129     } catch ( ... ) {};
00130 
00131     return res;
00132 }
00133 
00134 #endif