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 }
mps_fire.i
i
Definition: mps_fire.py:428
HLT_FULL_cff.points
points
Definition: HLT_FULL_cff.py:21469
gather_cfg.cout
cout
Definition: gather_cfg.py:144
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
PDRates.average
average
Definition: PDRates.py:138
debug
#define debug
Definition: HDRShower.cc:19
SMS.h
reco::ceil
constexpr int32_t ceil(float num)
Definition: constexpr_cmath.h:7
SMS::theRatio
float theRatio
Definition: SMS.h:32
SMS::Weighted
Definition: SMS.h:18
SMS::Interpolate
Definition: SMS.h:18
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
dqmdumpme.k
k
Definition: dqmdumpme.py:60
Point3DBase< float, GlobalTag >
b
double b
Definition: hdecay.h:118
cmsswSequenceInfo.tp
tp
Definition: cmsswSequenceInfo.py:17
a
double a
Definition: hdecay.h:119
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
SMS::theType
SMSType theType
Definition: SMS.h:31
createfilelist.int
int
Definition: createfilelist.py:10
mag2
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Definition: Basic3DVectorLD.h:124
submitPVResolutionJobs.q
q
Definition: submitPVResolutionJobs.py:84
cms::cuda::for
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
Definition: HistoContainer.h:15
SMS::Iterate
Definition: SMS.h:18
funct::D
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:141
std
Definition: JetResolutionObject.h:76
operator/=
Basic3DVector & operator/=(T t)
Scaling by a scalar value (division)
Definition: Basic3DVectorLD.h:203
SMS::location
GlobalPoint location(const std::vector< GlobalPoint > &) const
Definition: SMS.cc:57
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
SMS::SMSType
SMSType
Definition: SMS.h:18
EcalCondDBWriter_cfi.location
location
Definition: EcalCondDBWriter_cfi.py:63
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
operator+=
Basic3DVector & operator+=(const Basic3DVector< U > &p)
Definition: Basic3DVectorLD.h:174
SMS::SMS
SMS(SMSType tp=(SMSType)(Interpolate|Iterate|Weighted), float q=0.5)
Definition: SMS.cc:55