CMS 3D CMS Logo

Functions
trackerStablePhiSort.h File Reference
#include <vector>
#include <algorithm>
#include <cmath>

Go to the source code of this file.

Functions

template<class RandomAccessIterator , class Extractor >
void trackerStablePhiSort (RandomAccessIterator begin, RandomAccessIterator end, const Extractor &extr)
 

Function Documentation

template<class RandomAccessIterator , class Extractor >
void trackerStablePhiSort ( RandomAccessIterator  begin,
RandomAccessIterator  end,
const Extractor &  extr 
)

Definition at line 9 of file trackerStablePhiSort.h.

References funct::abs(), begin, constexpr, popcon2dropbox::copy(), M_PI, gen::n, AlCaHLTBitMon_ParallelJobs::p, AlignmentTrackSelector_cfi::phiMax, AlignmentTrackSelector_cfi::phiMin, tolerance, create_public_lumi_plots::transform, and genVertex_cff::x.

Referenced by mtdStablePhiSort(), CmsTrackerOTLayerBuilder::sortNS(), CmsTrackerRingBuilder::sortNS(), CmsTrackerPixelPhase2RingBuilder::sortNS(), CmsTrackerOTRingBuilder::sortNS(), CmsTrackerLayerBuilder::sortNS(), CmsTrackerWheelBuilder::sortNS(), and CmsTrackerDiskBuilder::sortNS().

10 {
11  using Scalar = decltype(extr(*begin));
13  using Element = std::pair<Scalar, Value*>;
14 
15  std::vector<Element> tmpvec(end-begin);
16  std::transform(begin,end,tmpvec.begin(), [&extr](Value& it){ return Element(extr(it), &it); });
17 
18  std::sort(tmpvec.begin(), tmpvec.end());
19 
20  // special tratment of the TEC modules of rings in petals near phi=0 there
21  // are at most 5 modules, no other structure has less than ~10 elements to
22  // order in phi hence the special case in phi~0 if the size of the elements
23  // to order is <=5
24  constexpr unsigned int nMaxModulesPerRing = 5;
25  constexpr double phiMin = M_PI_4;
26  constexpr double phiMax = 2 * M_PI - phiMin;
27  constexpr double tolerance = 0.000001;
28 
29  const unsigned int n = tmpvec.size();
30 
31  if( n > nMaxModulesPerRing ) {
32  // stability check
33  // check if the last element is too near to zero --> probably it is zero
34  if( std::abs(tmpvec.back().first - 0) < tolerance // near 0
35  || std::abs(tmpvec.back().first - 2*M_PI) < tolerance ) // near 2pi
36  {
37  // move it to front
38  tmpvec.insert(tmpvec.begin(),tmpvec.back());
39  tmpvec.pop_back();
40  }
41  } else {
42  // check if all the elements have phi<phiMin or phi>phiMax to be sure we
43  // are near phi~0 (angles are in [0,2pi) range) if a phi goes out from
44  // [0,phiMin]U[phiMax,2pi) it is not the case sorted. if first > phiMax all
45  // other will also...
46  auto p = std::find_if(tmpvec.begin(),tmpvec.end(), [&phiMin](auto const& x){ return x.first > phiMin; });
47 
48  // go on if this is the petal phi~0 case, restricted to the case where all
49  // the |phi| are in range [0,phiMin]
50  if(p == tmpvec.end() || p->first >= phiMax) {
51  // in this case the ordering must be: ('negative' values, >) and then
52  // ('positive' values, >) in (-pi,pi] mapping already sorted, just swap
53  // ranges
54  if(p!=tmpvec.end()) {
55  tmpvec.insert(tmpvec.begin(),p,tmpvec.end());
56  tmpvec.resize(n);
57  }
58  }
59  }
60 
61  // overwrite the input range with the sorted values
62  // copy of input container not necessary, but tricky to avoid
63  std::vector<Value> tmpvecy(n);
64  std::transform(tmpvec.begin(),tmpvec.end(),tmpvecy.begin(), [](auto& x){ return *x.second; });
65  std::copy(tmpvecy.begin(),tmpvecy.end(),begin);
66 }
def copy(args, dbName)
double Scalar
Definition: Definitions.h:27
const double tolerance
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
reco::JetExtendedAssociation::JetExtendedData Value
#define end
Definition: vmac.h:39
#define M_PI
#define begin
Definition: vmac.h:32
#define constexpr