Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JDrawDetector2D.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <limits>
4#include <vector>
5#include <map>
6#include <set>
7#include <memory>
8
9#include "TROOT.h"
10#include "TH2D.h"
11#include "TApplication.h"
12#include "TGraph.h"
13#include "TEllipse.h"
14#include "TCanvas.h"
15#include "TRootCanvas.h"
16#include "TMarker.h"
17#include "TText.h"
18#include "TStyle.h"
19#include "TLegend.h"
20#include "TError.h"
21
22#include "JROOT/JCanvas.hh"
23#include "JROOT/JStyle.hh"
25#include "JROOT/JLegend.hh"
28#include "JDetector/JTripod.hh"
32
33#include "Jeep/JeepToolkit.hh"
34#include "Jeep/JContainer.hh"
35#include "Jeep/JProperties.hh"
36#include "Jeep/JParser.hh"
37#include "Jeep/JMessage.hh"
38
39namespace {
40
42
43 /**
44 * Marker with text.
45 */
46 struct JPoint_t :
47 public JPosition2D
48 {
49 /**
50 * Constructor.
51 *
52 * \param title title
53 * \param pos position
54 * \param marker marker attributes
55 * \param text text attributes
56 * \param offset text ofset
57 */
58 JPoint_t(const std::string& title,
59 const JPosition2D& pos,
60 const TAttMarker& marker,
61 const TAttText& text,
62 const double offset) :
63 JPosition2D(pos),
64 offset(offset * cos(text.GetTextAngle()),
65 offset * sin(text.GetTextAngle()))
66 {
67 static_cast<TAttMarker&>(this->marker) = marker;
68 static_cast<TAttText&> (this->text) = text;
69
70 this->text.SetTitle(title.c_str());
71 this->text.SetTextAngle(0.0); // overwrite text attributes
72 this->text.SetTextColor(marker.GetMarkerColor()); //
73 }
74
75 /**
76 * Add offset.
77 *
78 * \param x x-position
79 * \param y y-position
80 */
81 void add(const double x, const double y)
82 {
83 static_cast<JPosition2D&>(*this).add(JPosition2D(x,y));
84
85 configure();
86 }
87
88 /**
89 * Subtract offset.
90 *
91 * \param x x-position
92 * \param y y-position
93 */
94 void sub(const double x, const double y)
95 {
96 static_cast<JPosition2D&>(*this).sub(JPosition2D(x,y));
97
98 configure();
99 }
100
101 /**
102 * Draw.
103 */
104 void Draw()
105 {
106 marker.Draw();
107 text .Draw();
108 }
109
110 /**
111 * Configure.
112 */
113 void configure()
114 {
115 this->marker.SetX(this->getX());
116 this->marker.SetY(this->getY());
117 this->text .SetX(this->getX() + offset.getX());
118 this->text .SetY(this->getY() + offset.getY());
119 }
120
121 TMarker marker;
122 TText text;
123
124 private:
125 JPosition2D offset;
126 };
127
128
129 /**
130 * Container of markers.
131 */
132 struct JGraph_t :
133 public std::vector<JPoint_t>
134 {
135 /**
136 * Add offset.
137 *
138 * \param x x-position
139 * \param y y-position
140 */
141 void add(const double x, const double y)
142 {
143 for (iterator i = begin(); i != end(); ++i) {
144 i->add(x, y);
145 }
146 }
147
148 /**
149 * Subtract offset.
150 *
151 * \param x x-position
152 * \param y y-position
153 */
154 void sub(const double x, const double y)
155 {
156 for (iterator i = begin(); i != end(); ++i) {
157 i->sub(x, y);
158 }
159 }
160
161 /**
162 * Draw.
163 */
164 void Draw()
165 {
166 for (iterator i = begin(); i != end(); ++i) {
167 i->Draw();
168 }
169 }
170 };
171}
172
173
174/**
175 * \file
176 * Auxiliary program to draw the footprint of detector(s).
177 * \author mdejong
178 */
179int main(int argc, char**argv)
180{
181 using namespace std;
182 using namespace JPP;
183
186
187 vector<string> detectorFile;
188 vector<string> tripodsFile;
189 vector<string> transmittersFile;
190 string outputFile;
191 JCanvas canvas;
192 JCircle2D focus;
193 JStyle::JParameters parameters;
194 string legend;
195 double markerSize;
196 double textSize;
197 bool drawCircle;
198 bool all;
199 bool batch;
200 int debug;
201
202 try {
203
204 JProperties properties = parameters.getProperties();
205
206 properties["focus"] = focus;
207
208 JParser<> zap("Auxiliary program to draw the footprint of detector(s).");
209
210 zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
211 zap['@'] = make_field(properties, "properties") = JPARSER::initialised();
212 zap['a'] = make_field(detectorFile, "detector file") = JPARSER::initialised();
213 zap['T'] = make_field(tripodsFile, "tripod file") = JPARSER::initialised();
214 zap['Y'] = make_field(transmittersFile, "transmitter file") = JPARSER::initialised();
215 zap['o'] = make_field(outputFile, "graphics output") = "";
216 zap['L'] = make_field(legend, "position legend e.g. TR") = "", "TL", "TR", "BR", "BL";
217 zap['S'] = make_field(markerSize, "marker size") = 1.0;
218 zap['s'] = make_field(textSize, "text size") = 0.02;
219 zap['C'] = make_field(drawCircle, "draw smallest enclosing cicrle");
220 zap['A'] = make_field(all, "draw all modules");
221 zap['B'] = make_field(batch, "batch processing");
222 zap['d'] = make_field(debug) = 1;
223
224 zap(argc, argv);
225 }
226 catch(const exception &error) {
227 FATAL(error.what() << endl);
228 }
229
230
231 if (detectorFile.empty() && tripodsFile.empty()) {
232 FATAL("No detector elements." << endl);
233 }
234
235
236 gROOT->SetBatch(batch);
237
238 gErrorIgnoreLevel = kWarning;
239
240 TApplication* tp = new TApplication("user", NULL, NULL);
241 TCanvas* cv = new TCanvas("detector", "", canvas.x, canvas.y);
242
243 if (!batch) {
244 ((TRootCanvas *) cv->GetCanvasImp())->Connect("CloseWindow()", "TApplication", tp, "Terminate()");
245 }
246
247 unique_ptr<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
248
249 gROOT->SetStyle("gplot");
250 gROOT->ForceStyle();
251
252 cv->SetFillStyle(4000);
253 cv->SetFillColor(kWhite);
254 cv->Divide(1, 1);
255 cv->cd(1);
256
257 JMarkerAttributes::getInstance().setMarkerSize(markerSize);
258
259 vector<TAttText> text_attributes = {
260 TAttText(kHAlignLeft + kVAlignBottom, 0.25*PI, kBlack, 62, textSize),
261 TAttText(kHAlignRight + kVAlignBottom, 0.75*PI, kBlack, 62, textSize),
262 TAttText(kHAlignRight + kVAlignTop, 1.25*PI, kBlack, 62, textSize),
263 TAttText(kHAlignLeft + kVAlignTop, 1.75*PI, kBlack, 62, textSize)
264 };
265
266 map<string, JGraph_t> data; // graphics data
267 JUTMPosition position; // UTM position
268 double offset = 0.0; // text offset
269
270 for (size_t i = 0; i != detectorFile.size(); ++i) {
271
273
274 try {
275 load(detectorFile[i], detector);
276 }
277 catch(const JException& error) {
278 FATAL(error);
279 }
280
281 position = detector.getUTMPosition();
282
283 const TAttMarker& marker = *JMarkerAttributes::getInstance().next();
284 const TAttText& text = text_attributes[(0+i)%text_attributes.size()];
285 vector<JPoint_t>& buffer = data[getFilename(detectorFile[i])];
286
287 if (offset == 0.0) {
288 offset = 3.0 * textSize * JCircle2D(detector.begin(), detector.end()).getRadius();
289 }
290
291 set<int> counter;
292
293 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
294
295 if (module->getFloor() == 0) {
296
297 buffer.push_back(JPoint_t(MAKE_STRING(module->getString()),
298 JPosition2D(module->getX(), module->getY()),
299 marker,
300 text,
301 offset));
302
303 counter.insert(module->getString());
304 }
305 }
306
307 for (JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
308
309 const bool status = (counter.count(module->getString()) == 0);
310
311 if (status || all) {
312
313 buffer.push_back(JPoint_t((status ? MAKE_STRING(module->getString()) : ""),
314 JPosition2D(module->getX(), module->getY()),
315 marker,
316 text,
317 offset));
318
319 counter.insert(module->getString());
320 }
321 }
322 }
323
324 for (size_t i = 0; i != tripodsFile.size(); ++i) {
325
326 tripods_container tripods;
327
328 tripods.load(tripodsFile[i].c_str());
329
330 const TAttMarker& marker = *JMarkerAttributes::getInstance().next();
331 const TAttText& text = text_attributes[(0+i)%text_attributes.size()];
332 vector<JPoint_t>& buffer = data[getFilename(tripodsFile[i])];
333
334 if (offset == 0.0) {
335 offset = 3.0 * textSize * JCircle2D(tripods.begin(), tripods.end()).getRadius();
336 }
337
338 for (tripods_container::iterator i = tripods.begin(); i != tripods.end(); ++i) {
339
340 buffer.push_back(JPoint_t(MAKE_STRING(i->getID()),
341 JPosition2D(i->getUTMEast() - position.getUTMEast(), i->getUTMNorth() - position.getUTMNorth()),
342 marker,
343 text,
344 offset));
345 }
346 }
347
348 if (!transmittersFile.empty()) {
349
350 if (transmittersFile.size() == detectorFile.size()) {
351
352 for (size_t i = 0; i != transmittersFile.size(); ++i) {
353
354 transmitters_container transmitters;
355
356 transmitters.load(transmittersFile[i].c_str());
357
359
360 load(detectorFile[i], detector);
361
362 const TAttMarker& marker = *JMarkerAttributes::getInstance().next();
363 const TAttText& text = text_attributes[(2+i)%text_attributes.size()];
364 vector<JPoint_t>& buffer = data[getFilename(transmittersFile[i])];
365
366 if (offset == 0.0) {
367 offset = 3.0 * textSize * JCircle2D(transmitters.begin(), transmitters.end()).getRadius();
368 }
369
370 for (transmitters_container::iterator i = transmitters.begin(); i != transmitters.end(); ++i) {
371
372 const JPosition3D pos = detector.getModule(i->getLocation()).getPosition();
373
374 buffer.push_back(JPoint_t(MAKE_STRING(i->getID()),
375 JPosition2D(pos.getX() + i->getX(), pos.getY() + i->getY()),
376 marker,
377 text,
378 offset));
379 }
380 }
381
382 } else {
383
384 FATAL("Tranmitter files and detector files should match one-to-one." << endl);
385 }
386 }
387
388 vector<JPosition2D> buffer;
389
390 for (map<string, JGraph_t>::iterator i = data.begin(); i != data.end(); ++i) {
391 copy(i->second.begin(), i->second.end(), back_inserter(buffer));
392 }
393
394 JCircle2D circle(buffer.begin(), buffer.end()); // enclosing circle
395
396 if (focus.getRadius() > 0.0) {
397 circle = focus;
398 }
399
400 NOTICE("focus = " << FIXED(12,3) << circle.getX() << ' ' << FIXED(12,3) << circle.getY() << ' ' << FIXED(9,3) << circle.getRadius() << endl);
401
402 // center
403
404 for (map<string, JGraph_t>::iterator i = data.begin(); i != data.end(); ++i) {
405 i->second.sub(circle.getX(), circle.getY());
406 }
407
408 circle.sub(circle.getPosition());
409
410
411 // draw
412
413 cv->cd(1);
414
415 const Double_t xmin = circle.getX() - 1.15 * circle.getRadius();
416 const Double_t xmax = circle.getX() + 1.15 * circle.getRadius();
417 const Double_t ymin = circle.getY() - 1.15 * circle.getRadius();
418 const Double_t ymax = circle.getY() + 1.15 * circle.getRadius();
419
420 TH2D h2("h2", "", 200, xmin, xmax, 200, ymin, ymax);
421
422 h2.GetXaxis()->SetTitle("x [m]");
423 h2.GetYaxis()->SetTitle("y [m]");
424
425 h2.GetXaxis()->CenterTitle(true);
426 h2.GetYaxis()->CenterTitle(true);
427
428 h2.SetStats(kFALSE);
429 h2.Draw("AXIS");
430
431
432 TEllipse ellipse(circle.getX(), circle.getY(), circle.getRadius());
433
434 if (drawCircle) {
435 ellipse.Draw();
436 }
437
438 for (map<string, JGraph_t>::iterator i = data.begin(); i != data.end(); ++i) {
439 i->second.Draw();
440 }
441
442 if (legend != "") {
443
444 Ssiz_t height = data.size();
445 Ssiz_t width = 1;
446
447 for (map<string, JGraph_t>::const_iterator i = data.begin(); i != data.end(); ++i) {
448 width = max(width, (Ssiz_t) i->first.size());
449 }
450
451 TLegend* lg = getLegend(width, height, legend);
452
453 lg->SetTextSize(textSize);
454
455 for (map<string, JGraph_t>::const_iterator i = data.begin(); i != data.end(); ++i) {
456 if (!i->second.empty()) {
457 lg->AddEntry(&i->second[0].marker, i->first.c_str(), "P");
458 }
459 }
460
461 lg->Draw();
462 }
463
464 cv->Update();
465
466 if (outputFile != "") {
467 cv->SaveAs(outputFile.c_str());
468 }
469
470 if (!batch) {
471 tp->Run();
472 }
473}
Container I/O.
string outputFile
Data structure for detector geometry and calibration.
int main(int argc, char **argv)
General purpose messaging.
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
Utility class to parse parameter values.
Data structure for transmitter.
Data structure for tripod.
Auxiliary methods for handling file names, type names and environment.
Detector data structure.
Definition JDetector.hh:96
Utility class to parse parameter values.
Data structure for circle in two dimensions.
Definition JCircle2D.hh:35
double getRadius() const
Get radius.
Definition JCircle2D.hh:144
Data structure for position in two dimensions.
const JPosition2D & getPosition() const
Get position.
double getY() const
Get y position.
Definition JVector2D.hh:74
double getX() const
Get x position.
Definition JVector2D.hh:63
JVector2D & sub(const JVector2D &vector)
Subtract vector.
Definition JVector2D.hh:115
Data structure for position in three dimensions.
double getY() const
Get y position.
Definition JVector3D.hh:104
double getX() const
Get x position.
Definition JVector3D.hh:94
General exception.
Definition JException.hh:24
Utility class to parse command line options.
Definition JParser.hh:1698
Data structure for size of TCanvas.
Definition JCanvas.hh:26
int y
number of pixels in Y
Definition JCanvas.hh:99
int x
number of pixels in X
Definition JCanvas.hh:98
Wrapper class around ROOT TStyle.
Definition JStyle.hh:24
Data structure for UTM position.
double getUTMNorth() const
Get UTM north.
double getUTMEast() const
Get UTM east.
char text[TEXT_SIZE]
Definition elog.cc:72
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Detector file.
Definition JHead.hh:227
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:42
void load(const char *file_name)
Load from input file.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
JProperties getProperties()
Get properties of this class.
Definition JStyle.hh:44