CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackerStablePhiSort.h
Go to the documentation of this file.
1 #ifndef TrackerStablePhiSort_H
2 #define TrackerStablePhiSort_H
3 // #include "FWCore/MessageLogger/interface/MessageLogger.h"
4 
5 #include<iostream>
6 
7 #include <utility>
8 #include <vector>
9 #include <algorithm>
10 
11 #include <cmath>
12 
13 #include<boost/bind.hpp>
14 
15 namespace details {
16  template<typename Object, typename Scalar>
17  struct PhiSortElement {
19 
20  template<typename Extractor>
21  static self
22  build(Object & o, Extractor const & extr) {
23  return self(&o, extr(o));
24  }
25 
27  : pointer(0) {}
28  PhiSortElement(Object * p, Scalar v):
29  pointer(p),
30  value(v) {}
31 
32  Object * pointer;
34 
35  bool operator<(self const & rh) const {
36  return value<rh.value;
37  }
38  Object const &
39  obj() const { return *pointer;}
40  };
41 
42 
43 }
44 
45 template<typename RandomAccessIterator, typename Extractor>
46 void TrackerStablePhiSort(RandomAccessIterator begin,
47  RandomAccessIterator end,
48  const Extractor& extr) {
49 
50  typedef typename Extractor::result_type Scalar;
52 
54 
55 
56 
57  std::vector<Element> tmpvec(end-begin);
58  std::transform(begin,end,tmpvec.begin(),
59  boost::bind(Element::template build<Extractor>,_1, boost::cref(extr))
60  );
61 
62  std::vector<Element> tmpcop(end-begin);
63 
64  std::sort(tmpvec.begin(), tmpvec.end());
65 
66  const unsigned int vecSize = tmpvec.size();
67 
68 
69 
70  // special tratment of the TEC modules of rings in petals near phi=0
71  // there are at most 5 modules, no other structure has less than ~10 elements to order in phi
72  // hence the special case in phi~0 if the size of the elements to order is <=5
73  const unsigned int nMaxModulesPerRing = 5;
74  bool phiZeroCase = true;
75  //
76  const double phiMin = M_PI_4;
77  const double phiMax = 2*M_PI-phiMin;
78  //
79  if( vecSize > nMaxModulesPerRing ) {
80  //stability check
81  // check if the last element is too near to zero --> probably it is zero
82  double tolerance = 0.000001;
83  if( fabs(tmpvec.back().value - 0) < tolerance // near 0
84  ||
85  fabs(tmpvec.back().value - 2*M_PI) < tolerance ) { // near 2pi
86  // move it to front
87  tmpvec.insert(tmpvec.begin(),tmpvec.back());
88  tmpvec.pop_back();
89  }
90  }
91  else {
92  // check if all the elements have phi<phiMin or phi>phiMax to be sure we are near phi~0 (angles are in [0,2pi) range)
93  // if a phi goes out from [0,phiMin]U[phiMax,2pi) it is not the case
94  // sorted. if first > phiMax all other will also...
95  typename std::vector<Element>::iterator p =
96  std::find_if(tmpvec.begin(),tmpvec.end(), boost::bind(&Element::value,_1) > phiMin);
97  phiZeroCase = !(p!=tmpvec.end() && (*p).value<phiMax);
98 
99  // go on if this is the petal phi~0 case, restricted to the case where all the |phi| are in range [0,phiMin]
100  if(phiZeroCase) {
101  // in this case the ordering must be: ('negative' values, >) and then ('positive' values, >) in (-pi,pi] mapping
102  // already sorted, just swap ranges
103  if(p!=tmpvec.end()) {
104  tmpvec.insert(tmpvec.begin(),p,tmpvec.end());
105  tmpvec.resize(vecSize);
106  }
107  }
108  }
109 
110  // overwrite the input range with the sorted values
111  // copy of input container not necessary, but tricky to avoid
112  std::vector<value_type> tmpvecy(vecSize);
113  std::transform(tmpvec.begin(),tmpvec.end(),tmpvecy.begin(),boost::bind(&Element::obj,_1));
114  std::copy(tmpvecy.begin(),tmpvecy.end(),begin);
115 
116 }
117 
118 #endif
119 
Object const & obj() const
double Scalar
Definition: Definitions.h:27
void TrackerStablePhiSort(RandomAccessIterator begin, RandomAccessIterator end, const Extractor &extr)
static self build(Object &o, Extractor const &extr)
PhiSortElement(Object *p, Scalar v)
#define end
Definition: vmac.h:38
Container::value_type value_type
#define M_PI
Definition: BFit3D.cc:3
bool operator<(self const &rh) const
#define begin
Definition: vmac.h:31
mathSSE::Vec4< T > v
def template
Definition: svgfig.py:520