CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SMS.cc
Go to the documentation of this file.
1 #ifdef PROJECT_NAME
3 #else
4 #include <vector>
5 #include <algorithm>
7 using namespace std;
8 #endif
9 
10 namespace {
11 
12 
13  typedef std::pair < float, const GlobalPoint * > MyPair;
14  typedef std::pair < float, float > FloatPair;
15  typedef std::pair < GlobalPoint, float > GlPtWt;
16  typedef std::pair < float, const GlPtWt * > MyPairWt;
17 
18  struct Sorter
19  {
20  bool operator() ( const MyPair & pair1, const MyPair & pair2 )
21  {
22  return ( pair1.first < pair2.first );
23  };
24 
25  bool operator() ( const FloatPair & pair1, const FloatPair & pair2 )
26  {
27  return ( pair1.first < pair2.first );
28  };
29 
30  bool operator() ( const MyPairWt & pair1, const MyPairWt & pair2 )
31  {
32  return ( pair1.first < pair2.first );
33  };
34  };
35 
36  bool debug()
37  {
38  return false;
39  }
40 
41  inline GlobalPoint & operator += ( GlobalPoint & a, const GlobalPoint & b )
42  {
43  a = GlobalPoint ( a.x() + b.x(), a.y() + b.y(), a.z() + b.z() );
44  return a;
45  }
46  inline GlobalPoint & operator /= ( GlobalPoint & a, float b )
47  {
48  a = GlobalPoint ( a.x() / b, a.y() / b, a.z() / b );
49  return a;
50  }
51 
52  GlobalPoint average ( const std::vector < MyPair > & pairs, int nq )
53  {
54  GlobalPoint location(0,0,0);
55  for ( std::vector< MyPair >::const_iterator i=pairs.begin(); i!=( pairs.begin() + nq ); ++i )
56  location+=*( i->second );
57  location/=nq;
58  return location;
59  }
60 
61  GlobalPoint average ( const std::vector < MyPairWt > & pairs, int nq )
62  {
63  GlobalPoint location(0,0,0);
64  for ( std::vector< MyPairWt >::const_iterator i=pairs.begin(); i!=( pairs.begin() + nq ); ++i )
65  location+=(i->second)->first;
66  location/=nq;
67  return location;
68  }
69 
70  typedef SMS::SMSType SMSType;
71 }
72 
73 SMS::SMS ( SMSType tp , float q ) : theType(tp) , theRatio(q) {}
74 
75 
76 GlobalPoint SMS::location ( const std::vector<GlobalPoint> & data ) const
77 {
78  if ( theType & Weighted )
79  {
80  std::cout << "[SMS] warning: Weighted SMS was asked for, but data are "
81  << "weightless!" << std::endl;
82  };
83  int nobs=data.size();
84  int nq=(int) ceil( theRatio*nobs);
85  // cout << "nobs= " << nobs << " nq= " << nq << endl;
86 
87  // Compute distances
88  std::vector<MyPair> pairs;
89 
90  for ( std::vector< GlobalPoint >::const_iterator i=data.begin(); i!=data.end() ; ++i )
91  {
92  std::vector < float > D;
93  // Compute squared distances to all points
94  for ( std::vector< GlobalPoint >::const_iterator j=data.begin(); j!=data.end() ; ++j )
95  { D.push_back ( (*j - *i).mag2() ); }
96  // Find q-quantile in each row of the distance matrix
97  sort( D.begin(), D.end() );
98  MyPair tmp ( D[nq-1], &(*i) );
99  pairs.push_back ( tmp );
100  };
101 
102  // Sort pairs by first element
103  sort( pairs.begin(), pairs.end(), Sorter() );
104  if ( !(theType & SMS::Interpolate) &&
105  !(theType & SMS::Iterate) )
106  {
107  // we dont interpolate, we dont iterate, so we can stop right here.
108  // cout << "No interpolation, no iteration" << endl;
109  return *(pairs.begin()->second);
110  };
111 
112  // we dont iterate, or we dont have anything to iterate (anymore?)
113  // so we stop here
114 
115  // cout << "nobs= " << nobs << " nq= " << nq << endl;
116  if (!(theType & SMS::Iterate) || nq<=2)
117  return average ( pairs, nq );
118 
119  // we iterate (recursively)
120 
121  std::vector < GlobalPoint > data1;
122  std::vector<MyPair>::iterator j;
123 
124  for ( j=pairs.begin(); j-pairs.begin()<nq; ++j)
125  data1.push_back(*(j->second));
126 
127  return this->location( data1 );
128 
129 }
130 
131 
132 GlobalPoint SMS::location ( const std::vector < GlPtWt > & wdata ) const
133 {
134  if ( !(theType & Weighted) )
135  {
136  std::vector < GlobalPoint > points;
137  for ( std::vector< GlPtWt >::const_iterator i=wdata.begin();
138  i!=wdata.end() ; ++i )
139  {
140  points.push_back ( i->first );
141  };
142  if ( debug() )
143  {
144  std::cout << "[SMS] Unweighted SMS was asked for; ignoring the weights."
145  << std::endl;
146  };
147  return location ( points );
148  };
149  // int nobs=wdata.size();
150  // Sum of weights
151  float Sumw=0;
152  std::vector< GlPtWt >::const_iterator i,j;
153  for ( i=wdata.begin() ; i!=wdata.end() ; ++i)
154  Sumw+=i->second;
155 
156  // Compute pairwise distances
157  std::vector <MyPairWt> pairs;
158  for ( i=wdata.begin(); i!=wdata.end() ; ++i )
159  {
160  std::vector < FloatPair > D;
161  // Compute squared distances to all points
162  for ( j=wdata.begin(); j!=wdata.end() ; ++j )
163  D.push_back ( FloatPair( (j->first - i->first).mag2() , j->second ) ) ;
164  // Find weighted q-quantile in the distance vector
165  sort( D.begin(), D.end() );
166  float sumw=0;
167  std::vector< FloatPair >::const_iterator where;
168  for ( where=D.begin(); where!=D.end(); ++where )
169  {
170  sumw+=where->second;
171  // cout << sumw << endl;
172  if (sumw>Sumw*theRatio) break;
173  }
174  MyPairWt tmp ( where->first, &(*i) );
175  pairs.push_back ( tmp );
176  // cout << where->first << endl;
177  };
178 
179  // Sort pairs by first element
180  sort( pairs.begin(), pairs.end(), Sorter() );
181 
182  // Find weighted q-quantile in the list of pairs
183  float sumw=0;
184  int nq=0;
185  std::vector < MyPairWt >::const_iterator k;
186  for (k=pairs.begin(); k!=pairs.end(); ++k )
187  {
188  sumw+=k->second->second;
189  ++nq;
190  if (sumw>Sumw*theRatio) break;
191  }
192 
193  // cout << "nobs= " << nobs << " nq= " << nq << endl;
194 
195  if ( !(theType & SMS::Interpolate) &&
196  !(theType & SMS::Iterate) )
197  {
198  // we dont interpolate, we dont iterate, so we can stop right here.
199  // cout << "No interpolation, no iteration" << endl;
200  return pairs.begin()->second->first;
201  };
202 
203 
204 
205 
206  // we dont iterate, or we dont have anything to iterate (anymore?)
207  // so we stop here
208 
209  // cout << "nobs= " << nobs << " nq= " << nq << endl;
210  if (!(theType & SMS::Iterate) || nq<=2) return average ( pairs, nq );
211 
212  // we iterate (recursively)
213 
214  std::vector<GlPtWt> wdata1;
215 
216  for ( k=pairs.begin(); k-pairs.begin()<nq; ++k)
217  wdata1.push_back(*(k->second));
218 
219  return this->location( wdata1 );
220 
221 }
int i
Definition: DBlmapReader.cc:9
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:62
SMSType theType
Definition: SMS.h:35
T z() const
Definition: PV3DBase.h:63
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
int j
Definition: DBlmapReader.cc:9
int k[5][pyjets_maxn]
Basic3DVector & operator/=(T t)
Scaling by a scalar value (division)
double b
Definition: hdecay.h:120
int average
Definition: PDRates.py:137
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
double a
Definition: hdecay.h:121
SMS(SMSType tp=(SMSType)(Interpolate|Iterate|Weighted), float q=0.5)
Definition: SMS.cc:73
tuple cout
Definition: gather_cfg.py:121
float theRatio
Definition: SMS.h:36
#define debug
Definition: MEtoEDMFormat.h:34
T x() const
Definition: PV3DBase.h:61
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150
GlobalPoint location(const std::vector< GlobalPoint > &) const
Definition: SMS.cc:76
Basic3DVector & operator+=(const Basic3DVector< U > &p)
SMSType
Definition: SMS.h:19