Jpp  15.0.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGetSmallestDistance2D.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <fstream>
5 #include <iomanip>
6 #include <vector>
7 
8 #include "TROOT.h"
9 #include "TRandom3.h"
10 #include "TH2D.h"
11 #include "TGraph.h"
12 #include "TMarker.h"
13 #include "TCanvas.h"
14 #include "TApplication.h"
15 
16 #include "JGeometry2D/JVector2D.hh"
18 
19 #include "Jeep/JPrint.hh"
20 #include "Jeep/JTimer.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  *
28  * Example program to find smallest distance between two points.
29  * \author mdejong
30  */
31 int main(int argc, char* argv[])
32 {
33  using namespace std;
34 
35  string inputFile;
36  string outputFile;
37  int numberOfPoints;
38  UInt_t seed;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Example program to find smallest distance between two points.");
44 
45  zap['f'] = make_field(inputFile) = "";
46  zap['o'] = make_field(outputFile) = "";
47  zap['n'] = make_field(numberOfPoints) = 0;
48  zap['S'] = make_field(seed) = 0;
49  zap['d'] = make_field(debug) = 0;
50 
51  zap(argc, argv);
52  }
53  catch(const exception &error) {
54  FATAL(error.what() << endl);
55  }
56 
57  gRandom->SetSeed(seed);
58 
59  using namespace JPP;
60 
61 
62  vector<JVector2D> buffer;
63 
64  typedef vector<JVector2D>::const_iterator const_iterator;
65 
66 
67  if (inputFile != "") {
68 
69  ifstream in(inputFile.c_str());
70 
71  for (double x, y; in >> x >> y; ) {
72  buffer.push_back(JVector2D(x,y));
73  }
74 
75  in.close();
76 
77  } else if (numberOfPoints > 1) {
78 
79  NOTICE("Seed: " << gRandom->GetSeed() << endl);
80 
81  for (int i = 0; i != numberOfPoints; ++i) {
82 
83  buffer.push_back(JVector2D(gRandom->Uniform(-1.0, +1.0),
84  gRandom->Uniform(-1.0, +1.0)));
85  }
86 
87  if (outputFile != "") {
88 
89  ofstream out(outputFile.c_str(), ios::out);
90 
91  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i)
92  out << setw(7) << i->getX() << ' '
93  << setw(7) << i->getY() << endl;
94 
95  out.close();
96  }
97  }
98 
99  if (buffer.size() < 2) {
100  FATAL("Not enough points." << endl);
101  }
102 
103  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
104  DEBUG(i->getX() << ' ' << i->getY() << endl);
105  }
106 
107 
108  {
109  JTimer timer("classic");
110 
111  timer.start();
112 
113  double dmin = numeric_limits<double>::max();
114 
115  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
116  for (const_iterator j = i; ++j != buffer.end(); ) {
117 
118  const double d = i->getDistance(*j);
119 
120  if (d < dmin) {
121  dmin = d;
122  }
123  }
124  }
125 
126  timer.stop();
127 
128  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
129  timer.print(cout);
130  }
131 
132 
133  JTimer timer("O(n log(n))");
134 
135  timer.start();
136 
137  const double dmin = getSmallestDistance2D(buffer.begin(), buffer.end());
138 
139  timer.stop();
140 
141  cout << "Minimal distance " << SCIENTIFIC(12,5) << dmin << endl;
142  timer.print(cout);
143 
144 
145  pair<const_iterator, const_iterator> result = JSmallestDistance2D::getPair(buffer.begin(), buffer.end(), dmin);
146 
147  cout << "pair "
148  << "(" << FIXED(7,5) << result.first ->getX() << "," << FIXED(7,5) << result.first ->getY() << ")" << ' '
149  << "(" << FIXED(7,5) << result.second->getX() << "," << FIXED(7,5) << result.second->getY() << ")" << endl;
150  cout << "Distance " << SCIENTIFIC(12,5) << result.first->getDistance(*result.second) << endl;
151 
152 
153  TApplication* tp = new TApplication("user", NULL, NULL);
154 
155  TCanvas cv("cv", "", 400, 400);
156 
157  cv.SetFillStyle(4000);
158  cv.SetFillColor(0);
159 
160  cv.Divide(1, 1);
161  cv.cd(1);
162 
163 
164  const Int_t MAX_BUFFER_SIZE = buffer.size() + 1;
165 
166  Double_t x[MAX_BUFFER_SIZE];
167  Double_t y[MAX_BUFFER_SIZE];
168 
169  int N = 0;
170 
171  for (const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++N) {
172  x[N] = i->getX();
173  y[N] = i->getY();
174  }
175 
176 
177  Double_t xmin = -1.1;
178  Double_t xmax = +1.1;
179  Double_t ymin = -1.1;
180  Double_t ymax = +1.1;
181 
182  TH2D h2("h2", "", 1000, xmin, xmax, 1000, ymin, ymax);
183 
184  h2.SetStats(kFALSE);
185  h2.Draw();
186 
187 
188  TGraph g(N, x, y);
189 
190  g.SetMarkerStyle(20);
191  g.SetMarkerColor(kBlack);
192  g.SetMarkerSize(0.7);
193  g.Draw("P");
194 
195  TMarker m1(result.first ->getX(), result.first ->getY(), 20);
196  TMarker m2(result.second->getX(), result.second->getY(), 20);
197 
198  m1.SetMarkerColor(kRed);
199  m2.SetMarkerColor(kRed);
200 
201  m1.SetMarkerSize(0.7);
202  m2.SetMarkerSize(0.7);
203 
204  m1.Draw();
205  m2.Draw();
206 
207  cv.Update();
208 
209  tp->Run();
210 }
211 
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
static const JSmallestDistance2D getSmallestDistance2D
Function object for smallest distance determination.
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
return result
Definition: JPolint.hh:727
#define NOTICE(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
int numberOfPoints
Definition: JResultPDF.cc:22
int j
Definition: JPolint.hh:666
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484