Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JDrawDetector2D.cc File Reference

Auxiliary program to draw the footprint of detector(s). More...

#include <string>
#include <iostream>
#include <limits>
#include <vector>
#include "TROOT.h"
#include "TH2D.h"
#include "TApplication.h"
#include "TGraph.h"
#include "TEllipse.h"
#include "TCanvas.h"
#include "TMarker.h"
#include "TStyle.h"
#include "TLegend.h"
#include "TError.h"
#include "JLang/JSinglePointer.hh"
#include "JGizmo/JCanvas.hh"
#include "JGizmo/JStyle.hh"
#include "JGizmo/JMarkerAttributes.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JTripod.hh"
#include "JGeometry2D/JPosition2D.hh"
#include "JGeometry2D/JCircle2D.hh"
#include "Jeep/JeepToolkit.hh"
#include "Jeep/JContainer.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to draw the footprint of detector(s).

Author
mdejong

Definition in file JDrawDetector2D.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 38 of file JDrawDetector2D.cc.

39 {
40  using namespace std;
41  using namespace JPP;
42 
43  vector<string> detectorFile;
44  JContainer<JTripod> tripods;
45  string outputFile;
46  JCanvas canvas;
47  bool legend;
48  double markerSize;
49  bool batch;
50  int debug;
51 
52  try {
53 
54  JParser<> zap("Auxiliary program to draw the footprint of detector(s).");
55 
56  zap['w'] = make_field(canvas, "size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
57  zap['a'] = make_field(detectorFile, "detector file");
58  zap['T'] = make_field(tripods, "tripod data") = JPARSER::initialised();
59  zap['o'] = make_field(outputFile, "graphics output") = "";
60  zap['L'] = make_field(legend, "optional legend");
61  zap['S'] = make_field(markerSize, "marker size") = 1.0;
62  zap['B'] = make_field(batch, "batch processing");
63  zap['d'] = make_field(debug) = 1;
64 
65  zap(argc, argv);
66  }
67  catch(const exception &error) {
68  FATAL(error.what() << endl);
69  }
70 
71 
72  gROOT->SetBatch(batch);
73 
74  gErrorIgnoreLevel = kWarning;
75 
76  TApplication* tp = new TApplication("user", NULL, NULL);
77  TCanvas* cv = new TCanvas("detector", "", canvas.x, canvas.y);
78 
79  JSinglePointer<TStyle> gStyle(new JStyle("gplot", cv->GetWw(), cv->GetWh()));
80 
81  gROOT->SetStyle("gplot");
82  gROOT->ForceStyle();
83 
84  cv->SetFillStyle(4000);
85  cv->SetFillColor(kWhite);
86  cv->Divide(1, 1);
87  cv->cd(1);
88 
89  JMarkerAttributes::getInstance().setMarkerSize(markerSize);
90 
91 
92  int number_of_positions = 0;
93 
94  vector<TGraph*> buffer;
95 
96  JCircle2D circle;
97  JUTMPosition position;
98 
99  for (vector<string>::const_iterator file_name = detectorFile.begin(); file_name != detectorFile.end(); ++file_name) {
100 
102 
103  try {
104  load(*file_name, detector);
105  }
106  catch(const JException& error) {
107  FATAL(error);
108  }
109 
110  if (detector.empty()) {
111  ERROR("Empty detector." << endl);
112  }
113 
114  circle = JCircle2D(detector.begin(), detector.end());
115  position = detector.getUTMPosition();
116 
117  TGraph* graph = new TGraph(detector.size());
118 
119  graph->SetTitle(getFilename(*file_name).c_str());
120 
121  int n = 0;
122 
123  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i, ++n) {
124 
125  number_of_positions += 1;
126 
127  graph->GetX()[n] = i->getX();
128  graph->GetY()[n] = i->getY();
129  }
130 
131  dynamic_cast<TAttMarker&>(*graph) = *JGIZMO::JMarkerAttributes::getInstance().next();
132 
133  buffer.push_back(graph);
134  }
135 
136  if (!tripods.empty()) {
137 
138  if (number_of_positions == 0) {
139 
140  for (JContainer<JTripod>::const_iterator i = tripods.begin(); i != tripods.end(); ++i) {
141  position += *i;
142  }
143 
144  position /= tripods.size();
145  }
146 
147  vector<JPosition3D> zbuf;
148 
149  for (JContainer<JTripod>::iterator i = tripods.begin(); i != tripods.end(); ++i) {
150  zbuf.push_back(*i - position);
151  }
152 
153  circle = JCircle2D(zbuf.begin(), zbuf.end());
154 
155  TGraph* graph = new TGraph(zbuf.size());
156 
157  graph->SetTitle("tripod");
158 
159  for (size_t i = 0; i != zbuf.size(); ++i) {
160  graph->GetX()[i] = zbuf[i].getPosition().getX();
161  graph->GetY()[i] = zbuf[i].getPosition().getY();
162  }
163 
164  dynamic_cast<TAttMarker&>(*graph) = *JGIZMO::JMarkerAttributes::getInstance().next();
165 
166  buffer.push_back(graph);
167  }
168 
169  if (number_of_positions == 0 && tripods.empty()) {
170  FATAL("No detector elements." << endl);
171  }
172 
173 
174  DEBUG("Detector (x,y,R): " << circle.getX() << ' ' << circle.getY() << ' ' << circle.getRadius() << endl);
175 
176  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
177  for (int i = 0; i != (*graph)->GetN(); ++i) {
178  (*graph)->GetX()[i] -= circle.getX();
179  (*graph)->GetY()[i] -= circle.getY();
180  }
181  }
182 
183  circle.sub(circle.getPosition());
184 
185  Double_t R = circle.getRadius();
186 
187  if (R <= 1.0) {
188  R = 1.0;
189  }
190 
191  Double_t xmin = circle.getX() - R;
192  Double_t xmax = circle.getX() + R;
193  Double_t ymin = circle.getY() - R;
194  Double_t ymax = circle.getY() + R;
195 
196 
197  const Double_t dx = (xmax - xmin);
198  const Double_t dy = (ymax - ymin);
199 
200  xmin -= 0.1 * dx;
201  xmax += 0.1 * dx;
202  ymin -= 0.1 * dy;
203  ymax += 0.1 * dy;
204 
205 
206  cv->cd(1);
207 
208  TH2D h2("h2", "", 1, xmin, xmax, 1, ymin, ymax);
209 
210  h2.GetXaxis()->SetTitle("x [m]");
211  h2.GetYaxis()->SetTitle("y [m]");
212 
213  h2.GetXaxis()->CenterTitle(true);
214  h2.GetYaxis()->CenterTitle(true);
215 
216  h2.SetStats(kFALSE);
217  h2.Draw();
218 
219 
220  TEllipse ellipse(circle.getX(), circle.getY(), circle.getRadius());
221 
222  ellipse.Draw();
223 
224  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
225  (*graph)->Draw("P");
226  }
227 
228 
229  if (legend) {
230 
231  const Double_t x2 = gPad->GetX2() - gStyle->GetPadRightMargin();
232  const Double_t y2 = gPad->GetY2() - gStyle->GetPadTopMargin();
233 
234  size_t length = 10;
235  Double_t font_size = 0.03; // gStyle->GetStatFontSize();
236 
237  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
238  length = max(length, strlen((*graph)->GetTitle()));
239  }
240 
241  TLegend* legend = new TLegend(x2 - length * font_size * 0.65 - 0.02,
242  y2 - buffer.size() * font_size,
243  x2 - 0.01,
244  y2 - 0.01);
245 
246  //legend->SetFillStyle(4000);
247  legend->SetFillColor(0);
248  legend->SetBorderSize(0);
249  legend->SetTextSize(font_size);
250 
251  for (vector<TGraph*>::iterator graph = buffer.begin(); graph != buffer.end(); ++graph) {
252  legend->AddEntry(*graph, (*graph)->GetTitle(), "P");
253  }
254 
255  legend->Draw();
256  }
257 
258  cv->Update();
259 
260  if (outputFile != "") {
261  cv->SaveAs(outputFile.c_str());
262  }
263 
264  if (!batch) {
265  tp->Run();
266  }
267 }
Utility class to parse command line options.
Definition: JParser.hh:1493
General exception.
Definition: JException.hh:23
virtual const pointer_type & next()
Get next element.
Data structure for size of TCanvas.
Definition: JCanvas.hh:26
const JUTMPosition & getUTMPosition() const
Get UTM position.
Definition: JUTMPosition.hh:82
Data structure for circle in two dimensions.
Definition: JCircle2D.hh:29
Detector data structure.
Definition: JDetector.hh:80
std::vector< T >::const_iterator const_iterator
Definition: JContainer.hh:32
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:63
string outputFile
Data structure for UTM position.
Definition: JUTMPosition.hh:34
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 container for detector elements.
Definition: JContainer.hh:29
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
#define ERROR(A)
Definition: JMessage.hh:66
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
int debug
debug level
Definition: JSirene.cc:61
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:40
#define FATAL(A)
Definition: JMessage.hh:67
Wrapper class around ROOT TStyle.
Definition: JStyle.hh:20
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
std::vector< T >::iterator iterator
Definition: JContainer.hh:33
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
static JMarkerAttributes & getInstance()
Get reference to unique instance of this class object.