Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDrawDetector2D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <limits>
4 #include <vector>
5 
6 #include "TROOT.h"
7 #include "TH2D.h"
8 #include "TApplication.h"
9 #include "TGraph.h"
10 #include "TEllipse.h"
11 #include "TCanvas.h"
12 #include "TMarker.h"
13 #include "TStyle.h"
14 #include "TLegend.h"
15 #include "TError.h"
16 
17 #include "JLang/JSinglePointer.hh"
18 #include "JROOT/JCanvas.hh"
19 #include "JROOT/JStyle.hh"
21 #include "JDetector/JDetector.hh"
23 #include "JDetector/JTripod.hh"
25 #include "JGeometry2D/JCircle2D.hh"
26 
27 #include "Jeep/JeepToolkit.hh"
28 #include "Jeep/JContainer.hh"
29 #include "Jeep/JParser.hh"
30 #include "Jeep/JMessage.hh"
31 
32 
33 /**
34  * \file
35  * Auxiliary program to draw the footprint of detector(s).
36  * \author mdejong
37  */
38 int main(int argc, char**argv)
39 {
40  using namespace std;
41  using namespace JPP;
42 
43  typedef JContainer< vector<JTripod> > tripods_container;
44 
45  vector<string> detectorFile;
46  tripods_container tripods;
47  string outputFile;
48  JCanvas canvas;
49  bool legend;
50  double markerSize;
51  bool batch;
52  int debug;
53 
54  try {
55 
56  JParser<> zap("Auxiliary program to draw the footprint of detector(s).");
57 
58  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
59  zap['a'] = make_field(detectorFile, "detector file") = JPARSER::initialised();
60  zap['T'] = make_field(tripods, "tripod data") = JPARSER::initialised();
61  zap['o'] = make_field(outputFile, "graphics output") = "";
62  zap['L'] = make_field(legend, "optional legend");
63  zap['S'] = make_field(markerSize, "marker size") = 1.0;
64  zap['B'] = make_field(batch, "batch processing");
65  zap['d'] = make_field(debug) = 1;
66 
67  zap(argc, argv);
68  }
69  catch(const exception &error) {
70  FATAL(error.what() << endl);
71  }
72 
73 
74  if (detectorFile.empty() && tripods.empty()) {
75  FATAL("No detector elements." << endl);
76  }
77 
78 
79  gROOT->SetBatch(batch);
80 
81  gErrorIgnoreLevel = kWarning;
82 
83  TApplication* tp = new TApplication("user", NULL, NULL);
84  TCanvas* cv = new TCanvas("detector", "", canvas.x, canvas.y);
85 
86  JSinglePointer<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
87 
88  gROOT->SetStyle("gplot");
89  gROOT->ForceStyle();
90 
91  cv->SetFillStyle(4000);
92  cv->SetFillColor(kWhite);
93  cv->Divide(1, 1);
94  cv->cd(1);
95 
96  JMarkerAttributes::getInstance().setMarkerSize(markerSize);
97 
98 
99  int number_of_positions = 0;
100 
101  vector<TGraph*> buffer;
102 
103  JCircle2D circle;
104  JUTMPosition position;
105 
106  for (vector<string>::const_iterator file_name = detectorFile.begin(); file_name != detectorFile.end(); ++file_name) {
107 
109 
110  try {
111  load(*file_name, detector);
112  }
113  catch(const JException& error) {
114  FATAL(error);
115  }
116 
117  if (detector.empty()) {
118  ERROR("Empty detector." << endl);
119  }
120 
121  circle = JCircle2D(detector.begin(), detector.end());
122  position = detector.getUTMPosition();
123 
124  TGraph* graph = new TGraph(detector.size());
125 
126  graph->SetTitle(getFilename(*file_name).c_str());
127 
128  int n = 0;
129 
130  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i, ++n) {
131 
132  number_of_positions += 1;
133 
134  graph->GetX()[n] = i->getX();
135  graph->GetY()[n] = i->getY();
136  }
137 
138  dynamic_cast<TAttMarker&>(*graph) = *JMarkerAttributes::getInstance().next();
139 
140  buffer.push_back(graph);
141  }
142 
143  if (!tripods.empty()) {
144 
145  if (number_of_positions == 0) {
146 
147  for (tripods_container::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
148  position += *i;
149  }
150 
151  position /= tripods.size();
152  }
153 
154  vector<JPosition3D> zbuf;
155 
156  for (tripods_container::iterator i = tripods.begin(); i != tripods.end(); ++i) {
157  zbuf.push_back(*i - position);
158  }
159 
160  circle = JCircle2D(zbuf.begin(), zbuf.end());
161 
162  TGraph* graph = new TGraph(zbuf.size());
163 
164  graph->SetTitle("tripod");
165 
166  for (size_t i = 0; i != zbuf.size(); ++i) {
167  graph->GetX()[i] = zbuf[i].getPosition().getX();
168  graph->GetY()[i] = zbuf[i].getPosition().getY();
169  }
170 
171  dynamic_cast<TAttMarker&>(*graph) = *JMarkerAttributes::getInstance().next();
172 
173  buffer.push_back(graph);
174  }
175 
176  if (number_of_positions == 0 && tripods.empty()) {
177  FATAL("No detector elements." << endl);
178  }
179 
180 
181  DEBUG("Detector (x,y,R): " << FIXED(12,3) << circle.getX() << ' ' << FIXED(12,3) << circle.getY() << ' ' << FIXED(9,3) << circle.getRadius() << endl);
182 
183  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
184  for (int i = 0; i != (*graph)->GetN(); ++i) {
185  (*graph)->GetX()[i] -= circle.getX();
186  (*graph)->GetY()[i] -= circle.getY();
187  }
188  }
189 
190  circle.sub(circle.getPosition());
191 
192  Double_t R = circle.getRadius();
193 
194  if (R <= 1.0) {
195  R = 1.0;
196  }
197 
198  Double_t xmin = circle.getX() - R;
199  Double_t xmax = circle.getX() + R;
200  Double_t ymin = circle.getY() - R;
201  Double_t ymax = circle.getY() + R;
202 
203 
204  const Double_t dx = (xmax - xmin);
205  const Double_t dy = (ymax - ymin);
206 
207  xmin -= 0.1 * dx;
208  xmax += 0.1 * dx;
209  ymin -= 0.1 * dy;
210  ymax += 0.1 * dy;
211 
212 
213  cv->cd(1);
214 
215  TH2D h2("h2", "", 1, xmin, xmax, 1, ymin, ymax);
216 
217  h2.GetXaxis()->SetTitle("x [m]");
218  h2.GetYaxis()->SetTitle("y [m]");
219 
220  h2.GetXaxis()->CenterTitle(true);
221  h2.GetYaxis()->CenterTitle(true);
222 
223  h2.SetStats(kFALSE);
224  h2.Draw();
225 
226 
227  TEllipse ellipse(circle.getX(), circle.getY(), circle.getRadius());
228 
229  ellipse.Draw();
230 
231  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
232  (*graph)->Draw("P");
233  }
234 
235 
236  if (legend) {
237 
238  const Double_t x2 = gPad->GetX2() - gStyle->GetPadRightMargin();
239  const Double_t y2 = gPad->GetY2() - gStyle->GetPadTopMargin();
240 
241  size_t length = 10;
242  Double_t font_size = 0.03; // gStyle->GetStatFontSize();
243 
244  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
245  length = max(length, strlen((*graph)->GetTitle()));
246  }
247 
248  TLegend* legend = new TLegend(x2 - length * font_size * 0.65 - 0.02,
249  y2 - buffer.size() * font_size,
250  x2 - 0.01,
251  y2 - 0.01);
252 
253  //legend->SetFillStyle(4000);
254  legend->SetFillColor(0);
255  legend->SetBorderSize(0);
256  legend->SetTextSize(font_size);
257 
258  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
259  legend->AddEntry(*graph, (*graph)->GetTitle(), "P");
260  }
261 
262  legend->Draw();
263  }
264 
265  cv->Update();
266 
267  if (outputFile != "") {
268  cv->SaveAs(outputFile.c_str());
269  }
270 
271  if (!batch) {
272  tp->Run();
273  }
274 }
Utility class to parse command line options.
Definition: JParser.hh:1500
General exception.
Definition: JException.hh:23
const JUTMPosition & getUTMPosition() const
Get UTM position.
Definition: JUTMPosition.hh:84
Data structure for circle in two dimensions.
Definition: JCircle2D.hh:30
Detector data structure.
Definition: JDetector.hh:80
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
string outputFile
Data structure for detector geometry and calibration.
Data structure for UTM position.
Definition: JUTMPosition.hh:36
The template JSinglePointer class can be used to hold a pointer to an object.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:39
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Auxiliary methods for handling file names, type names and environment.
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:63
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
Utility class to parse command line options.
alias put_queue eval echo n
Definition: qlib.csh:19
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:88
do set_variable DETECTOR_TXT $WORKDIR detector
Data structure for tripod.
Wrapper class around ROOT TStyle.
Definition: JStyle.hh:20
Container I/O.
Data structure for size of TCanvas.
Definition: JCanvas.hh:26
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15