Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef _MultiClusterizer1D_H_
0002 #define _MultiClusterizer1D_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/FsmwClusterizer1D.h"
0009 
0010 #include <vector>
0011 #include <map>
0012 #include <cmath>
0013 #include <algorithm>
0014 
0015 /** 
0016  *  A clusterizer that uses a "single" clusterizer iteratively ...
0017  */
0018 
0019 template <class T>
0020 class MultiClusterizer1D : public Clusterizer1D<T> {
0021 public:
0022   //    typedef Cluster1D<T> Cluster1D;
0023   MultiClusterizer1D(const Clusterizer1D<T> &single, const WeightEstimator<T> &est = TrivialWeightEstimator<T>());
0024   MultiClusterizer1D(const MultiClusterizer1D &);
0025   ~MultiClusterizer1D();
0026 
0027   std::pair<std::vector<Cluster1D<T> >, std::vector<const T *> > operator()(const std::vector<Cluster1D<T> > &) const;
0028 
0029   virtual MultiClusterizer1D *clone() const;
0030 
0031 private:
0032   WeightEstimator<T> *theEstimator;
0033   Clusterizer1D<T> *theSingle;
0034 };
0035 
0036 /*
0037  *                              --- implementation ---
0038  *
0039  */
0040 
0041 /*
0042 namespace MultiClusterizer1DNameSpace
0043 {
0044 }*/
0045 
0046 template <class T>
0047 MultiClusterizer1D<T>::MultiClusterizer1D(const MultiClusterizer1D<T> &o)
0048     : theEstimator(o.theEstimator->clone()), theSingle(o.theSingle->clone()) {}
0049 
0050 template <class T>
0051 MultiClusterizer1D<T>::MultiClusterizer1D(const Clusterizer1D<T> &single, const WeightEstimator<T> &est)
0052     : theEstimator(est.clone()), theSingle(single.clone()) {}
0053 
0054 template <class T>
0055 MultiClusterizer1D<T>::~MultiClusterizer1D() {
0056   delete theEstimator;
0057   delete theSingle;
0058 }
0059 
0060 template <class T>
0061 MultiClusterizer1D<T> *MultiClusterizer1D<T>::clone() const {
0062   return new MultiClusterizer1D<T>(*this);
0063 }
0064 
0065 template <class T>
0066 std::pair<std::vector<Cluster1D<T> >, std::vector<const T *> > MultiClusterizer1D<T>::operator()(
0067     const std::vector<Cluster1D<T> > &ov) const {
0068   using namespace Clusterizer1DCommons;
0069   // using namespace MultiClusterizer1DNameSpace;
0070   typedef Cluster1D<T> Cluster1D;
0071   std::vector<const T *> unusedtracks;
0072   switch (ov.size()) {
0073     case 0:
0074       throw Clustering1DException("[MultiClusterizer1D] no values given");
0075     case 1:
0076       std::pair<std::vector<Cluster1D>, std::vector<const T *> > ret(ov, unusedtracks);
0077       return ret;
0078   };
0079 
0080   std::pair<std::vector<Cluster1D>, std::vector<const T *> > res;
0081 
0082   // works only with one track per cluster!!!
0083   std::map<const T *, Cluster1D> ass;
0084   std::vector<Cluster1D> cur;
0085 
0086   for (typename std::vector<Cluster1D>::const_iterator i = ov.begin(); i != ov.end(); ++i) {
0087     if (i->tracks().size() == 1) {
0088       ass[i->tracks()[0]] = *i;
0089     }
0090     cur.push_back(*i);
0091   }
0092 
0093   int ctr = 0;
0094   try {
0095     while (true) {
0096       std::pair<std::vector<Cluster1D>, std::vector<const T *> > tmp = (*theSingle)(cur);
0097 
0098       for (typename std::vector<Cluster1D>::const_iterator i = tmp.first.begin(); i != tmp.first.end(); ++i) {
0099         res.first.push_back(*i);
0100       }
0101       res.second = tmp.second;
0102 
0103       cur.clear();
0104 
0105       for (typename std::vector<const T *>::const_iterator i = res.second.begin(); i != res.second.end(); ++i) {
0106         cur.push_back(ass[*i]);
0107       }
0108       if (ctr++ > 5)
0109         break;
0110       if (cur.size() < 2)
0111         break;
0112     }
0113   } catch (...) {
0114   };
0115 
0116   return res;
0117 }
0118 
0119 #endif