CMS 3D CMS Logo

MtvClusterizer1D.h
Go to the documentation of this file.
1 #ifndef _MtvClusterizer1D_H_
2 #define _MtvClusterizer1D_H_
3 
8 
9 #include <vector>
10 #include <cmath>
11 #include <algorithm>
12 
16 template <class T>
18 {
19 public:
23 
24  std::pair < std::vector < Cluster1D<T> >, std::vector < const T * > >
25  operator() ( const std::vector< Cluster1D<T> > & ) const;
26 
27  virtual MtvClusterizer1D * clone() const;
28 
29 private:
32 };
33 
34 /*
35  * --- implementation ---
36  *
37  */
38 
39 template <class T>
42 {}
43 
44 
45 template <class T>
47  const WeightEstimator<T> & est ) : theEstimator ( est.clone() )
48 {}
49 
50 
51 template <class T>
53 {
54  delete theEstimator;
55 }
56 
57 template <class T>
59 {
60  return new MtvClusterizer1D<T> ( * this );
61 }
62 
63 template <class T>
64 std::pair < std::vector < Cluster1D<T> >, std::vector < const T * > >
65 MtvClusterizer1D<T>::operator() ( const std::vector < Cluster1D<T> > & ov ) const
66 {
67  typedef Cluster1D<T> Cluster1D;
68  using namespace Clusterizer1DCommons;
69  std::vector < const T * > unusedtracks;
70  switch ( ov.size() )
71  {
72  case 0:
73  throw Clustering1DException("[MtvClusterizer1D] no values given" );
74  case 1:
75  std::pair < std::vector < Cluster1D >, std::vector < const T * > > ret ( ov, unusedtracks );
76  return ret;
77  };
78  std::vector < Cluster1D > v = ov;
79  sort ( v.begin(), v.end(), ComparePairs<T>() );
80  std::vector < Cluster1D > sols;
81  std::vector < const T * > trks;
82 
83  typename std::vector< Cluster1D >::iterator cur = v.begin();
84  typename std::vector< Cluster1D >::iterator end = (v.end() - 1 );
85  double cur_min = cur->weight() + ( cur+1 )->weight();
86 
87  for ( typename std::vector< Cluster1D >::iterator i=v.begin();
88  i!=end ; ++i )
89  {
90  double cur_val = i->weight() + ( i+1 )->weight();
91  if ( cur_val > cur_min )
92  {
93  cur_min = cur_val;
94  cur = i;
95  };
96  };
97 
98  double weight = ( cur->weight() + (cur+1)->weight() );
99  double est = ( cur->weight() * cur->position().value() +
100  (cur+1)->weight() * (cur+1)->position().value()) / weight;
101  double sigma = sqrt ( square ( cur->position().value() - est ) +
102  square ( (cur+1)->position().value() - est ) );
103  double err=0.;
104  int inliers=0;
105 
106  for ( typename std::vector< Cluster1D >::iterator i=v.begin();
107  i!=v.end() ; ++i )
108  {
109  if ( fabs ( i->position().value() - est ) < 3 * sigma )
110  {
111  // all within 3 sigma are 'in'
112  add
113  ( i->tracks(), trks );
114  err+= square ( i->position().value() - est );
115  inliers++;
116  }
117  else
118  {
119  add
120  ( i->tracks(), unusedtracks );
121  };
122  };
123  err /= ( inliers - 1 ); // the algo definitely produces 2 or more inliers
124  err = sqrt ( err );
125 
126  sols.push_back ( Cluster1D ( Measurement1D ( est,err ), trks, weight ) );
127  std::pair < std::vector < Cluster1D >, std::vector < const T * > > ret ( sols, unusedtracks );
128  return ret;
129 }
130 
131 #endif
MtvClusterizer1D(const WeightEstimator< T > &est=TrivialWeightEstimator< T >())
virtual MtvClusterizer1D * clone() const
Definition: weight.py:1
T sqrt(T t)
Definition: SSEVec.h:18
#define end
Definition: vmac.h:37
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
std::pair< std::vector< Cluster1D< T > >, std::vector< const T * > > operator()(const std::vector< Cluster1D< T > > &) const
static double square(double x)
static int position[264][3]
Definition: ReadPGInfo.cc:509
WeightEstimator< T > * theEstimator