Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:14

0001 #include "Geometry/TrackerNumberingBuilder/interface/trackerStablePhiSort.h"
0002 #include "FakeCPP.h"
0003 
0004 #include <cmath>
0005 #include <ctime>
0006 #include <vector>
0007 #include <algorithm>
0008 #include <random>
0009 #include <iostream>
0010 
0011 namespace {
0012 
0013   struct XY {
0014     XY(double ix=0.,double iy=0.):x(ix),y(iy){}
0015     double x;
0016     double y;
0017   };
0018   bool operator==(XY const & a, XY const & b) {
0019     return a.x==b.x&&a.y==b.y;
0020   }
0021 
0022   double getPhi(XY const & xy) {
0023     static const double pi2 = 2.*M_PI;
0024     return xy.y >= 0 ? std::atan2(xy.y,xy.x) : pi2-std::atan2(-xy.y,xy.x);
0025   }
0026 
0027   template<typename V>
0028   void printPhi(V const & v ) {
0029     for(auto const& x : v) std::cout << getPhi(x) << " ";
0030     std::cout << std::endl;
0031   }
0032 
0033 }
0034 
0035 int main()
0036 {
0037   cppUnit::Dump a;
0038 
0039   using Collection =  std::vector<std::vector<XY>>;
0040 
0041   Collection original;
0042   Collection shuffled;
0043   Collection sorted;
0044 
0045   auto fromRP = [](double phi){ return XY(2.*std::cos(phi), 2.*std::sin(phi)); };
0046 
0047   // test1
0048   {
0049     // ordered.... last element is almost 0 (but not quite)
0050     std::vector<double> phis {0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3., 3.1, 4.2, 5.2, 6.25, getPhi(XY(1.E5,-1.))};
0051     original.emplace_back(phis.size());
0052     std::transform(phis.begin(),phis.end(),original.back().begin(),fromRP);
0053   }  // test1
0054   {
0055     // ordered.... first element is almost 0 (is zero for the algo)
0056     std::vector<double> phis {getPhi(XY(1.E10,-1.)), 0.000001, 0.5, 1.0, 1.5, 2.0, 2.5, 3., 3.1, 4.2, 5.2, 6.25, };
0057     original.emplace_back(phis.size());
0058     std::transform(phis.begin(),phis.end(),original.back().begin(),fromRP);
0059   }
0060   // TEC tests???
0061   {
0062     // ordered....
0063     std::vector<double> phis {0.0, 0.5, 3., 5.2, 6.25};
0064     original.emplace_back(phis.size());
0065     std::transform(phis.begin(),phis.end(),original.back().begin(),fromRP);
0066   }
0067   {
0068     // ordered....
0069     std::vector<double> phis {-0.1683, -0.0561, +0.0000, +0.0561, +0.1683};
0070     original.emplace_back(phis.size());
0071     std::transform(phis.begin(),phis.end(),original.back().begin(),fromRP);
0072   }
0073 
0074 
0075   for(auto const& v : original) printPhi(v);
0076 
0077   // do the test
0078   shuffled.resize(original.size());
0079   sorted.resize(original.size());
0080   std::copy(original.begin(),original.end(),shuffled.begin());
0081   auto rng = std::default_random_engine {};
0082   for(auto& v : shuffled) std::shuffle(v.begin(), v.end(), rng);
0083 
0084   std::copy(shuffled.begin(),shuffled.end(),sorted.begin());
0085 
0086   CPPUNIT_ASSERT(original!=sorted);
0087 
0088   double before = std::clock();
0089   for(auto& v : sorted) trackerStablePhiSort(v.begin(), v.end(), getPhi);
0090 
0091   double after = std::clock();
0092   std::cout << "elapsed " << (after-before)*0.01 << std::endl;
0093 
0094   CPPUNIT_ASSERT(original==sorted);
0095 
0096   for(auto const& v : sorted) printPhi(v);
0097   std::cout << std::endl;
0098 
0099   return 0;
0100 }