Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:04

0001 #ifndef _OutermostClusterizer1D_H_
0002 #define _OutermostClusterizer1D_H_
0003 
0004 #include "CommonTools/Clustering1D/interface/Clusterizer1D.h"
0005 #include "CommonTools/Clustering1D/interface/Clusterizer1DCommons.h"
0006 #include "CommonTools/Clustering1D/interface/TrivialWeightEstimator.h"
0007 #include "CommonTools/Clustering1D/interface/Clustering1DException.h"
0008 #include "CommonTools/Clustering1D/interface/Cluster1DMerger.h"
0009 
0010 #include <vector>
0011 #include <cmath>
0012 #include <algorithm>
0013 
0014 /** 
0015  *   Produces two clusters for each end of the 1d data points.
0016  *   It then puts 50 % of the points in each cluster.
0017  */
0018 
0019 template <class T>
0020 class OutermostClusterizer1D : public Clusterizer1D<T> {
0021 public:
0022   /** \param fraction fraction of values that will be considered to be 'in'.
0023      */
0024   OutermostClusterizer1D(const WeightEstimator<T>& est = TrivialWeightEstimator<T>());
0025   OutermostClusterizer1D(const OutermostClusterizer1D&);
0026   ~OutermostClusterizer1D() override;
0027 
0028   std::pair<std::vector<Cluster1D<T> >, std::vector<const T*> > operator()(
0029       const std::vector<Cluster1D<T> >&) const override;
0030 
0031   OutermostClusterizer1D* clone() const override;
0032 
0033 private:
0034   WeightEstimator<T>* theEstimator;
0035 };
0036 
0037 /*
0038  *                              --- implementation ---
0039  *
0040  */
0041 
0042 template <class T>
0043 OutermostClusterizer1D<T>::OutermostClusterizer1D(const OutermostClusterizer1D<T>& o)
0044     : theEstimator(o.theEstimator->clone()) {}
0045 
0046 template <class T>
0047 OutermostClusterizer1D<T>::OutermostClusterizer1D(const WeightEstimator<T>& est) : theEstimator(est.clone()) {}
0048 
0049 template <class T>
0050 OutermostClusterizer1D<T>::~OutermostClusterizer1D() {
0051   delete theEstimator;
0052 }
0053 
0054 template <class T>
0055 OutermostClusterizer1D<T>* OutermostClusterizer1D<T>::clone() const {
0056   return new OutermostClusterizer1D<T>(*this);
0057 }
0058 
0059 template <class T>
0060 std::pair<std::vector<Cluster1D<T> >, std::vector<const T*> > OutermostClusterizer1D<T>::operator()(
0061     const std::vector<Cluster1D<T> >& ov) const {
0062   using namespace Clusterizer1DCommons;
0063   typedef Cluster1D<T> Cluster1D;
0064   std::vector<const T*> unusedtracks;
0065 
0066   switch (ov.size()) {
0067     case 0:
0068       throw Clustering1DException("[OutermostClusterizer1D] no values given");
0069     case 1: {
0070       std::pair<std::vector<Cluster1D>, std::vector<const T*> > ret(ov, unusedtracks);
0071       return ret;
0072     };
0073     case 2: {
0074       std::pair<std::vector<Cluster1D>, std::vector<const T*> > ret(ov, unusedtracks);
0075       return ret;
0076     };
0077   };
0078 
0079   std::vector<Cluster1D> v = ov;
0080   sort(v.begin(), v.end(), ComparePairs<T>());
0081   std::vector<Cluster1D> sols;
0082   int sze = v.size() / 2;
0083   Cluster1D tmp = v[0];
0084   Cluster1DMerger<T> merger(*theEstimator);
0085   // merge the inner half to the primary cluster
0086   for (typename std::vector<Cluster1D>::const_iterator i = v.begin() + 1; i != v.begin() + sze; ++i) {
0087     tmp = merger(tmp, *i);
0088   }
0089   sols.push_back(tmp);
0090   tmp = v[sze];
0091   for (typename std::vector<Cluster1D>::const_iterator i = v.begin() + sze + 1; i != v.end(); ++i) {
0092     tmp = merger(tmp, *i);
0093   }
0094   sols.push_back(tmp);
0095 
0096   std::pair<std::vector<Cluster1D>, std::vector<const T*> > ret(sols, unusedtracks);
0097   return ret;
0098 }
0099 
0100 #endif