LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
View2D.cxx
Go to the documentation of this file.
1 #include <algorithm>
11 
12 #include "TGraph.h"
13 #include "TPad.h"
14 
15 namespace evdb{
16 
17  //......................................................................
18  class TBoxClipped: public TBox
19  {
20  public:
21  TBoxClipped(double a, double b, double c, double d) : TBox(a, b, c, d){}
22  virtual void Paint(Option_t* option)
23  {
24  const double ux1 = gPad->GetUxmin(), uy1 = gPad->GetUymin();
25  const double ux2 = gPad->GetUxmax(), uy2 = gPad->GetUymax();
26  // Completely outside frame
27  if(!gPad->GetLogx()){
28  if(fX1 < ux1 && fX2 < ux1) return;
29  if(fX1 > ux2 && fX2 > ux2) return;
30  }
31  if(!gPad->GetLogy()){
32  if(fY1 < uy1 && fY2 < uy1) return;
33  if(fY1 > uy2 && fY2 > uy2) return;
34  }
35 
36  // Store the parameters for restoration later
37  const double x1 = fX1, y1 = fY1, x2 = fX2, y2 = fY2;
38 
39  // Clip corners to avoid painting outside of the frame (TBox doesn't do
40  // this by default)
41  if(!gPad->GetLogx()){
42  if(fX1 < ux1) fX1 = ux1;
43  if(fX2 > ux2) fX2 = ux2;
44  }
45  if(!gPad->GetLogy()){
46  if(fY1 < uy1) fY1 = uy1;
47  if(fY2 > uy2) fY2 = uy2;
48  }
49 
50  TBox::Paint(option);
51 
52  // Put the real parameters back
53  fX1 = x1; fX2 = x2; fY1 = y1; fY2 = y2;
54 
55  // NB: clipped boxes overdraw axis lines, caller might want to call
56  // Draw("axis same") on their containing histogram.
57  }
58  };
59 
60  // All of these static lists are "leaked" when the application ends. But that's
61  // OK: they were serving a useful purpose right up until that moment, and ROOT
62  // object destruction takes an age, so the event display actually shuts down
63  // much faster this way.
64  std::list<TMarker*> View2D::fgMarkerL;
65  std::list<TPolyMarker*> View2D::fgPolyMarkerL;
66  std::list<TLine*> View2D::fgLineL;
67  std::list<TPolyLine*> View2D::fgPolyLineL;
68  std::list<TArc*> View2D::fgArcL;
69  std::list<TBox*> View2D::fgBoxL;
70  std::list<TText*> View2D::fgTextL;
71  std::list<TLatex*> View2D::fgLatexL;
72 
73  //......................................................................
74 
76  {
77  }
78 
79  //......................................................................
80 
82  {
83  // Make sure to return all our objects to where they came from
84  Clear();
85  }
86 
87  //......................................................................
88 
89  void View2D::Draw()
90  {
91  // Want to clip all of our objects inside the axis frame. Note, TBox doesn't
92  // obey this flag, have to use TBoxClipped to do it by hand. Unfortunately
93  // we have to change global state, and we can't just put it back at the end
94  // of the function, because this has to be set at Paint() time.
95  gPad->SetBit(TGraph::kClipFrame, true);
96 
97  for_each(fArcL.begin(), fArcL.end(), draw_tobject());
98  for_each(fBoxL.begin(), fBoxL.end(), draw_tobject());
99  for_each(fPolyLineL.begin(), fPolyLineL.end(), draw_tobject());
100  for_each(fLineL.begin(), fLineL.end(), draw_tobject());
101  for_each(fMarkerL.begin(), fMarkerL.end(), draw_tobject());
102  for_each(fPolyMarkerL.begin(),fPolyMarkerL.end(),draw_tobject());
103  for_each(fTextL.begin(), fTextL.end(), draw_tobject());
104  for_each(fLatexL.begin(), fLatexL.end(), draw_tobject());
105  }
106 
107  //......................................................................
108 
110  {
111  // Empty each of our lists, appending them back onto the static ones
112  fgMarkerL.splice(fgMarkerL.end(), fMarkerL);
113  fgArcL.splice(fgArcL.end(), fArcL);
114  fgBoxL.splice(fgBoxL.end(), fBoxL);
115  fgPolyLineL.splice(fgPolyLineL.end(), fPolyLineL);
116  fgLineL.splice(fgLineL.end(), fLineL);
117  fgPolyMarkerL.splice(fgPolyMarkerL.end(), fPolyMarkerL);
118  fgTextL.splice(fgTextL.end(), fTextL);
119  fgLatexL.splice(fgLatexL.end(), fLatexL);
120  }
121 
122  //......................................................................
123 
124  TMarker& View2D::AddMarker(double x, double y, int c, int st, double sz)
125  {
126  // Each "Add" function follows this same pattern. If there are no cached
127  // objects of the right type we make a new one as instructed. If there are
128  // some in the cache, we take possession of one and reset it to the state
129  // this new caller wants.
130 
131  TMarker* m = 0;
132  if(fgMarkerL.empty()){
133  m = new TMarker(x,y,st);
134  m->SetBit(kCanDelete,kFALSE);
135  m->SetMarkerColor(c);
136  m->SetMarkerSize(sz);
137  }
138  else{
139  m = fgMarkerL.back();
140  fgMarkerL.pop_back();
141 
142  m->SetX(x);
143  m->SetY(y);
144  m->SetMarkerSize(sz);
145  m->SetMarkerColor(c);
146  m->SetMarkerStyle(st);
147  }
148 
149  // In either case, we have to remember we have it so that we can give it back
150  // when we're done with it.
151  fMarkerL.push_back(m);
152  return *m;
153  }
154 
155  //......................................................................
156 
157  TPolyMarker& View2D::AddPolyMarker(int n, int c, int st, double sz)
158  {
159  TPolyMarker* pm = 0;
160  if(fgPolyMarkerL.empty()){
161  pm = new TPolyMarker(n);
162  pm->SetBit(kCanDelete,kFALSE);
163  pm->SetMarkerColor(c);
164  pm->SetMarkerStyle(st);
165  pm->SetMarkerSize(sz);
166  }
167  else {
168  pm = fgPolyMarkerL.back();
169  fgPolyMarkerL.pop_back();
170 
171  // The first call to SetPolyMarker with the 0
172  // deletes the current set of points before trying
173  // to make a new set
174  pm->SetPolyMarker(0);
175  pm->SetPolyMarker(n);
176  pm->SetMarkerColor(c);
177  pm->SetMarkerSize(sz);
178  pm->SetMarkerStyle(st);
179  }
180 
181  fPolyMarkerL.push_back(pm);
182  return *pm;
183  }
184 
185  //......................................................................
186 
187  TLine& View2D::AddLine(double x1, double y1, double x2, double y2)
188  {
189  TLine* ln = 0;
190  if(fgLineL.empty()){
191  ln = new TLine(x1,y1,x2,y2);
192  ln->SetBit(kCanDelete,kFALSE);
193  }
194  else {
195  ln = fgLineL.back();
196  fgLineL.pop_back();
197 
198  ln->SetX1(x1);
199  ln->SetY1(y1);
200  ln->SetX2(x2);
201  ln->SetY2(y2);
202  }
203 
204  fLineL.push_back(ln);
205  return *ln;
206  }
207 
208  //......................................................................
209 
210  TPolyLine& View2D::AddPolyLine(int n, int c, int w, int s)
211  {
212  TPolyLine* pl = 0;
213  if(fgPolyLineL.empty()){
214  pl = new TPolyLine(n);
215  pl->SetBit(kCanDelete,kFALSE);
216  pl->SetLineColor(c);
217  pl->SetLineWidth(w);
218  pl->SetLineStyle(s);
219  }
220  else {
221  pl = fgPolyLineL.back();
222  fgPolyLineL.pop_back();
223 
224  pl->SetPolyLine(0);
225  pl->SetPolyLine(n); // reset elements in PolyLine
226  pl->SetOption("");
227  pl->SetLineColor(c);
228  pl->SetLineWidth(w);
229  pl->SetLineStyle(s);
230  }
231 
232  fPolyLineL.push_back(pl);
233  return *pl;
234  }
235 
236  //......................................................................
237 
238  TArc& View2D::AddArc(double x, double y, double r, double p1, double p2)
239  {
240  TArc* a = 0;
241  if(fgArcL.empty()){
242  a = new TArc(x,y,r,p1,p2);
243  a->SetBit(kCanDelete,kFALSE);
244  }
245  else {
246  a = fgArcL.back();
247  fgArcL.pop_back();
248 
249  a->SetX1(x);
250  a->SetY1(y);
251  a->SetR1(r);
252  a->SetR2(r);
253  a->SetPhimin(p1);
254  a->SetPhimax(p2);
255  }
256 
257  fArcL.push_back(a);
258  return *a;
259  }
260 
261  //......................................................................
262 
263  TBox& View2D::AddBox(double x1, double y1, double x2, double y2)
264  {
265  TBox* b = 0;
266  if(fgBoxL.empty()){
267  b = new TBoxClipped(x1,y1,x2,y2);
268  b->SetBit(kCanDelete,kFALSE);
269  }
270  else {
271  b = fgBoxL.back();
272  fgBoxL.pop_back();
273 
274  b->SetX1(x1);
275  b->SetY1(y1);
276  b->SetX2(x2);
277  b->SetY2(y2);
278  }
279 
280  fBoxL.push_back(b);
281  return *b;
282  }
283 
284  //......................................................................
285 
286  TText& View2D::AddText(double x, double y, const char* text)
287  {
288  TText* itxt = 0;
289  if(fgTextL.empty()) {
290  itxt = new TText(x,y,text);
291  itxt->SetBit(kCanDelete,kFALSE);
292  }
293  else {
294  itxt = fgTextL.back();
295  fgTextL.pop_back();
296 
297  itxt->SetText(x,y,text);
298  itxt->SetTextAngle(0);
299  itxt->SetTextAlign(11);
300  }
301 
302  fTextL.push_back(itxt);
303  return *itxt;
304  }
305 
306  //......................................................................
307 
308  TLatex& View2D::AddLatex(double x, double y, const char* text)
309  {
310  TLatex* itxt = 0;
311  if(fgLatexL.empty()){
312  itxt = new TLatex(x,y,text);
313  itxt->SetBit(kCanDelete,kFALSE);
314  }
315  else {
316  itxt = fgLatexL.back();
317  fgLatexL.pop_back();
318 
319  itxt->SetText(x,y,text);
320  itxt->SetTextAngle(0);
321  itxt->SetTextAlign(11);
322  }
323 
324  fLatexL.push_back(itxt);
325  return *itxt;
326  }
327 
328 }//namespace
Float_t x
Definition: compare.C:6
Float_t s
Definition: plot.C:23
static std::list< TBox * > fgBoxL
Definition: View2D.h:48
Float_t y1[n_points_granero]
Definition: compare.C:5
TPolyLine & AddPolyLine(int n, int c, int w, int s)
Definition: View2D.cxx:210
Float_t x1[n_points_granero]
Definition: compare.C:5
TLine & AddLine(double x1, double y1, double x2, double y2)
Definition: View2D.cxx:187
Float_t y
Definition: compare.C:6
A collection of drawable 2-D objects.
Manage all things related to colors for the event display.
virtual void Paint(Option_t *option)
Definition: View2D.cxx:22
void Clear()
Definition: View2D.cxx:109
static std::list< TPolyMarker * > fgPolyMarkerL
Definition: View2D.h:44
Float_t y2[n_points_geant4]
Definition: compare.C:26
static std::list< TPolyLine * > fgPolyLineL
Definition: View2D.h:46
TBox & AddBox(double x1, double y1, double x2, double y2)
Definition: View2D.cxx:263
void Draw()
Definition: View2D.cxx:89
TPolyMarker & AddPolyMarker(int n, int c, int st, double sz)
Definition: View2D.cxx:157
TArc & AddArc(double x, double t, double r, double a=0., double b=360.)
Definition: View2D.cxx:238
TText & AddText(double x, double y, const char *text)
Definition: View2D.cxx:286
Float_t d
Definition: plot.C:237
TLatex & AddLatex(double x, double y, const char *text)
Definition: View2D.cxx:308
static std::list< TLatex * > fgLatexL
Definition: View2D.h:50
static std::list< TMarker * > fgMarkerL
Definition: View2D.h:43
static std::list< TLine * > fgLineL
Definition: View2D.h:45
Char_t n[5]
static std::list< TArc * > fgArcL
Definition: View2D.h:47
Float_t x2[n_points_geant4]
Definition: compare.C:26
static std::list< TText * > fgTextL
Definition: View2D.h:49
TMarker & AddMarker(double x, double y, int c, int st, double sz)
Definition: View2D.cxx:124
Float_t w
Definition: plot.C:23
TBoxClipped(double a, double b, double c, double d)
Definition: View2D.cxx:21