CMS 3D CMS Logo

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  typedef std::pair<float, const GlobalPoint*> MyPair;
13  typedef std::pair<float, float> FloatPair;
14  typedef std::pair<GlobalPoint, float> GlPtWt;
15  typedef std::pair<float, const GlPtWt*> MyPairWt;
16 
17  struct Sorter {
18  bool operator()(const MyPair& pair1, const MyPair& pair2) { return (pair1.first < pair2.first); };
19 
20  bool operator()(const FloatPair& pair1, const FloatPair& pair2) { return (pair1.first < pair2.first); };
21 
22  bool operator()(const MyPairWt& pair1, const MyPairWt& pair2) { return (pair1.first < pair2.first); };
23  };
24 
25  bool debug() { return false; }
26 
27  inline GlobalPoint& operator+=(GlobalPoint& a, const GlobalPoint& b) {
28  a = GlobalPoint(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
29  return a;
30  }
31  inline GlobalPoint& operator/=(GlobalPoint& a, float b) {
32  a = GlobalPoint(a.x() / b, a.y() / b, a.z() / b);
33  return a;
34  }
35 
36  GlobalPoint average(const std::vector<MyPair>& pairs, int nq) {
37  GlobalPoint location(0, 0, 0);
38  for (std::vector<MyPair>::const_iterator i = pairs.begin(); i != (pairs.begin() + nq); ++i)
39  location += *(i->second);
40  location /= nq;
41  return location;
42  }
43 
44  GlobalPoint average(const std::vector<MyPairWt>& pairs, int nq) {
45  GlobalPoint location(0, 0, 0);
46  for (std::vector<MyPairWt>::const_iterator i = pairs.begin(); i != (pairs.begin() + nq); ++i)
47  location += (i->second)->first;
48  location /= nq;
49  return location;
50  }
51 
52  typedef SMS::SMSType SMSType;
53 } // namespace
54 
55 SMS::SMS(SMSType tp, float q) : theType(tp), theRatio(q) {}
56 
57 GlobalPoint SMS::location(const std::vector<GlobalPoint>& data) const {
58  if (theType & Weighted) {
59  std::cout << "[SMS] warning: Weighted SMS was asked for, but data are "
60  << "weightless!" << std::endl;
61  };
62  int nobs = data.size();
63  int nq = (int)ceil(theRatio * nobs);
64  // cout << "nobs= " << nobs << " nq= " << nq << endl;
65 
66  // Compute distances
67  std::vector<MyPair> pairs;
68 
69  for (std::vector<GlobalPoint>::const_iterator i = data.begin(); i != data.end(); ++i) {
70  std::vector<float> D;
71  // Compute squared distances to all points
72  for (std::vector<GlobalPoint>::const_iterator j = data.begin(); j != data.end(); ++j) {
73  D.push_back((*j - *i).mag2());
74  }
75  // Find q-quantile in each row of the distance matrix
76  sort(D.begin(), D.end());
77  MyPair tmp(D[nq - 1], &(*i));
78  pairs.push_back(tmp);
79  };
80 
81  // Sort pairs by first element
82  sort(pairs.begin(), pairs.end(), Sorter());
83  if (!(theType & SMS::Interpolate) && !(theType & SMS::Iterate)) {
84  // we dont interpolate, we dont iterate, so we can stop right here.
85  // cout << "No interpolation, no iteration" << endl;
86  return *(pairs.begin()->second);
87  };
88 
89  // we dont iterate, or we dont have anything to iterate (anymore?)
90  // so we stop here
91 
92  // cout << "nobs= " << nobs << " nq= " << nq << endl;
93  if (!(theType & SMS::Iterate) || nq <= 2)
94  return average(pairs, nq);
95 
96  // we iterate (recursively)
97 
98  std::vector<GlobalPoint> data1;
99  std::vector<MyPair>::iterator j;
100 
101  for (j = pairs.begin(); j - pairs.begin() < nq; ++j)
102  data1.push_back(*(j->second));
103 
104  return this->location(data1);
105 }
106 
107 GlobalPoint SMS::location(const std::vector<GlPtWt>& wdata) const {
108  if (!(theType & Weighted)) {
109  std::vector<GlobalPoint> points;
110  for (std::vector<GlPtWt>::const_iterator i = wdata.begin(); i != wdata.end(); ++i) {
111  points.push_back(i->first);
112  };
113  if (debug()) {
114  std::cout << "[SMS] Unweighted SMS was asked for; ignoring the weights." << std::endl;
115  };
116  return location(points);
117  };
118  // int nobs=wdata.size();
119  // Sum of weights
120  float Sumw = 0;
121  std::vector<GlPtWt>::const_iterator i, j;
122  for (i = wdata.begin(); i != wdata.end(); ++i)
123  Sumw += i->second;
124 
125  // Compute pairwise distances
126  std::vector<MyPairWt> pairs;
127  for (i = wdata.begin(); i != wdata.end(); ++i) {
128  std::vector<FloatPair> D;
129  // Compute squared distances to all points
130  for (j = wdata.begin(); j != wdata.end(); ++j)
131  D.push_back(FloatPair((j->first - i->first).mag2(), j->second));
132  // Find weighted q-quantile in the distance vector
133  sort(D.begin(), D.end());
134  float sumw = 0;
135  std::vector<FloatPair>::const_iterator where;
136  for (where = D.begin(); where != D.end(); ++where) {
137  sumw += where->second;
138  // cout << sumw << endl;
139  if (sumw > Sumw * theRatio)
140  break;
141  }
142  MyPairWt tmp(where->first, &(*i));
143  pairs.push_back(tmp);
144  // cout << where->first << endl;
145  };
146 
147  // Sort pairs by first element
148  sort(pairs.begin(), pairs.end(), Sorter());
149 
150  // Find weighted q-quantile in the list of pairs
151  float sumw = 0;
152  int nq = 0;
153  std::vector<MyPairWt>::const_iterator k;
154  for (k = pairs.begin(); k != pairs.end(); ++k) {
155  sumw += k->second->second;
156  ++nq;
157  if (sumw > Sumw * theRatio)
158  break;
159  }
160 
161  // cout << "nobs= " << nobs << " nq= " << nq << endl;
162 
163  if (!(theType & SMS::Interpolate) && !(theType & SMS::Iterate)) {
164  // we dont interpolate, we dont iterate, so we can stop right here.
165  // cout << "No interpolation, no iteration" << endl;
166  return pairs.begin()->second->first;
167  };
168 
169  // we dont iterate, or we dont have anything to iterate (anymore?)
170  // so we stop here
171 
172  // cout << "nobs= " << nobs << " nq= " << nq << endl;
173  if (!(theType & SMS::Iterate) || nq <= 2)
174  return average(pairs, nq);
175 
176  // we iterate (recursively)
177 
178  std::vector<GlPtWt> wdata1;
179 
180  for (k = pairs.begin(); k - pairs.begin() < nq; ++k)
181  wdata1.push_back(*(k->second));
182 
183  return this->location(wdata1);
184 }
constexpr int32_t ceil(float num)
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
SMSType theType
Definition: SMS.h:31
location
10.6 INPUT and workflows
Definition: relval_nano.py:81
GlobalPoint location(const std::vector< GlobalPoint > &) const
Definition: SMS.cc:57
#define debug
Definition: HDRShower.cc:19
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:141
Basic3DVector & operator/=(T t)
Scaling by a scalar value (division)
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
double b
Definition: hdecay.h:120
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
double a
Definition: hdecay.h:121
SMS(SMSType tp=(SMSType)(Interpolate|Iterate|Weighted), float q=0.5)
Definition: SMS.cc:55
float theRatio
Definition: SMS.h:32
tmp
align.sh
Definition: createJobs.py:716
Basic3DVector & operator+=(const Basic3DVector< U > &p)
SMSType
Definition: SMS.h:18