source: trunk/MagicSoft/Mars/mtools/MagicShow.cc@ 1936

Last change on this file since 1936 was 1435, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 9.3 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 07/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MagicShow
28// ---------
29//
30// Tool to visualize Next Neighbours.
31//
32// Start the show by:
33// MagicShow show;
34//
35// Use the following keys:
36// -----------------------
37//
38// * Space:
39// Toggle between auto increment and manual increment
40//
41// * Right/Left:
42// Increment/Decrement pixel number by 1
43//
44// * Right/Left:
45// Increment/Decrement pixel number by 1
46//
47// * Up/Down:
48// Increment/Decrement pixel number by 10
49//
50// * PageUp/PageDown:
51// Increment/Decrement pixel number by 100
52//
53// * Home/End:
54// Jump to first/last pixel
55//
56////////////////////////////////////////////////////////////////////////////
57#include "MagicShow.h"
58
59#include <iostream.h>
60
61#include <KeySymbols.h>
62
63#include <TCanvas.h>
64#include <TInterpreter.h>
65
66#include "MHexagon.h"
67
68#include "MGeomPix.h"
69#include "MGeomCamCT1.h"
70#include "MGeomCamMagic.h"
71
72ClassImp(MagicShow);
73
74// ------------------------------------------------------------------------
75//
76// Free all onbects connected to a special camera geometry
77//
78void MagicShow::Free()
79{
80 if (!fGeomCam)
81 return;
82
83 fPixels->Delete();
84
85 delete fPixels;
86
87 delete fGeomCam;
88}
89
90// ------------------------------------------------------------------------
91//
92// Draw all pixels of the camera
93// (means apend all pixelobjects to the current pad)
94//
95void MagicShow::DrawHexagons()
96{
97 for (UInt_t i=0; i<fNumPixels; i++)
98 {
99 MHexagon &h = (*this)[i];
100
101 h.SetFillColor(kBackground);
102 h.Draw();
103 }
104}
105
106// ------------------------------------------------------------------------
107//
108// Change camera from Magic to CT1 and back
109//
110void MagicShow::ChangeCamera()
111{
112 static Bool_t ct1=kFALSE;
113
114 cout << "Change to " << (ct1?"Magic":"CT1") << endl;
115
116 if (ct1)
117 SetNewCamera(new MGeomCamMagic);
118 else
119 SetNewCamera(new MGeomCamCT1);
120
121 ct1 = !ct1;
122
123 DrawHexagons();
124}
125
126// ------------------------------------------------------------------------
127//
128// Reset/set all veriables needed for a new camera geometry
129//
130void MagicShow::SetNewCamera(MGeomCam *geom)
131{
132 Free();
133
134 //
135 // Reset the display geometry
136 //
137 fW=0;
138 fH=0;
139
140 //
141 // Set new camera
142 //
143 fGeomCam = geom;
144
145 //
146 // create the hexagons of the display
147 //
148 fNumPixels = fGeomCam->GetNumPixels();
149 fRange = fGeomCam->GetMaxRadius();
150
151 fNumPixel = fNumPixels-1;
152
153 //
154 // Construct all hexagons. Use new-operator with placement
155 //
156 fPixels = new TClonesArray("MHexagon", fNumPixels);
157
158 for (UInt_t i=0; i<fNumPixels; i++)
159 {
160 MHexagon &h = *new ((*fPixels)[i]) MHexagon((*fGeomCam)[i]);
161#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
162 h.SetBit(kNoContextMenu|kCannotPick);
163#endif
164 }
165}
166
167// ------------------------------------------------------------------------
168//
169// default constructor
170//
171MagicShow::MagicShow()
172 : fTimer(this, 250, kTRUE), fGeomCam(NULL), fNumPixel(-1), fAuto(kTRUE), fW(0), fH(0)
173{
174 SetNewCamera(new MGeomCamMagic);
175
176 memset(fText, 0, sizeof(fText));
177
178 //
179 // Make sure, that the object is destroyed when the canvas/pad is
180 // destroyed. Make also sure, that the interpreter doesn't try to
181 // delete it a second time.
182 //
183 SetBit(kCanDelete);
184 gInterpreter->DeleteGlobal(this);
185
186 Draw();
187
188 fTimer.TurnOn();
189}
190
191// ------------------------------------------------------------------------
192//
193// Destructor. Deletes TClonesArrays for hexagons and legend elements.
194//
195MagicShow::~MagicShow()
196{
197 Free();
198
199 for (int i=0; i<6; i++)
200 if (fText[i])
201 delete fText[i];
202
203 if (fDrawingPad->GetListOfPrimitives()->FindObject(this)==this)
204 {
205 fDrawingPad->RecursiveRemove(this);
206 delete fDrawingPad;
207 }
208}
209
210// ------------------------------------------------------------------------
211//
212// This is called at any time the canvas should get repainted.
213// Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
214// that the camera image doesn't get distorted by resizing the canvas.
215//
216void MagicShow::Paint(Option_t *opt)
217{
218 const UInt_t w = (UInt_t)(gPad->GetWw()*gPad->GetAbsWNDC());
219 const UInt_t h = (UInt_t)(gPad->GetWh()*gPad->GetAbsHNDC());
220
221 //
222 // Check for a change in width or height, and make sure, that the
223 // first call also sets the range
224 //
225 if (w*fH == h*fW && fW && fH)
226 return;
227
228 //
229 // Calculate aspect ratio (5/4=1.25 recommended)
230 //
231 const Double_t ratio = (Double_t)w/h;
232
233 Float_t x;
234 Float_t y;
235
236 if (ratio>1.0)
237 {
238 x = fRange*(ratio*2-1);
239 y = fRange;
240 }
241 else
242 {
243 x = fRange;
244 y = fRange/ratio;
245 }
246
247 fH = h;
248 fW = w;
249
250 //
251 // Set new range
252 //
253 fDrawingPad->Range(-fRange, -y, x, y);
254}
255
256// ------------------------------------------------------------------------
257//
258// Call this function to draw the camera layout into your canvas.
259// Setup a drawing canvas. Add this object and all child objects
260// (hexagons, etc) to the current pad. If no pad exists a new one is
261// created.
262//
263void MagicShow::Draw(Option_t *option)
264{
265 //
266 // if no canvas is yet existing to draw into, create a new one
267 //
268 /*TCanvas *c =*/ new TCanvas("MagicShow", "Magic Show Next Neighbours", 0, 0, 800, 800);
269 //c->ToggleEventStatus();
270
271 fDrawingPad = gPad;
272 fDrawingPad->SetBorderMode(0);
273 fDrawingPad->SetFillColor(22);
274
275 //
276 // Append this object, so that the aspect ratio is maintained
277 // (Paint-function is called)
278 //
279 AppendPad(option);
280
281 //
282 // Reset the game pad
283 //
284 DrawHexagons();
285
286 fShow.SetTextAlign(23); // centered/bottom
287#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
288 fShow.SetBit(kNoContextMenu|kCannotPick);
289#endif
290 fShow.Draw();
291}
292
293// ------------------------------------------------------------------------
294//
295// Update Status text
296//
297void MagicShow::Update()
298{
299 TString txt = "Pixels: ";
300 txt += fNumPixels;
301 txt += " Pixel: ";
302 txt += fNumPixel;
303
304 if (fAuto)
305 txt += " (auto)";
306
307 fShow.SetText(0, fRange, txt);
308}
309
310// ------------------------------------------------------------------------
311//
312// Execute a mouse event on the camera
313//
314void MagicShow::ExecuteEvent(Int_t event, Int_t keycode, Int_t keysym)
315{
316 if (event!=kKeyPress)
317 return;
318
319 switch (keysym)
320 {
321 case kKey_Space:
322 fAuto = !fAuto;
323 Update();
324 fDrawingPad->Update();
325 return;
326
327 case kKey_Right:
328 ChangePixel(+1);
329 return;
330
331 case kKey_Left:
332 ChangePixel(-1);
333 return;
334
335 case kKey_Up:
336 ChangePixel(+10);
337 return;
338
339 case kKey_Down:
340 ChangePixel(-10);
341 return;
342
343 case kKey_PageUp:
344 ChangePixel(+100);
345 return;
346
347 case kKey_PageDown:
348 ChangePixel(-100);
349 return;
350
351 case kKey_Home:
352 ChangePixel(-fNumPixel);
353 return;
354
355 case kKey_End:
356 ChangePixel(fNumPixels-fNumPixel-1);
357 return;
358 }
359}
360
361// ------------------------------------------------------------------------
362//
363// Change the shown pixel by add indices
364//
365void MagicShow::ChangePixel(Int_t add)
366{
367 MagicShow &This = *this;
368
369 const MGeomPix &pix1=(*fGeomCam)[fNumPixel];
370 This[fNumPixel].SetFillColor(kBackground);
371 for (int i=0; i<pix1.GetNumNeighbors(); i++)
372 {
373 This[pix1.GetNeighbor(i)].SetFillColor(kBackground);
374 if (!fText[i])
375 continue;
376
377 delete fText[i];
378 fText[i] = NULL;
379 }
380
381 fNumPixel += add;
382
383 if (fNumPixel>=fNumPixels)
384 fNumPixel = 0;
385 if (fNumPixel<0)
386 fNumPixel = fNumPixels-1;
387
388 const MGeomPix &pix2=(*fGeomCam)[fNumPixel];
389 This[fNumPixel].SetFillColor(kBlue);
390 for (int i=0; i<pix2.GetNumNeighbors(); i++)
391 {
392 Int_t idx = pix2.GetNeighbor(i);
393
394 This[idx].SetFillColor(kMagenta);
395
396 TString num;
397 num += idx;
398
399 fText[i] = new TText(This[idx].GetX(), This[idx].GetY(), num);
400 fText[i]->SetTextSize(0.3*This[idx].GetD()/fGeomCam->GetMaxRadius());
401 fText[i]->SetTextFont(122);
402 fText[i]->SetTextAlign(22); // centered/centered
403 fText[i]->Draw();
404 }
405
406 Update();
407
408 fDrawingPad->Update();
409}
410
411// ------------------------------------------------------------------------
412//
413// If automatic is switched on step one pixel forward
414//
415Bool_t MagicShow::HandleTimer(TTimer *timer)
416{
417 if (fAuto)
418 ChangePixel(+1);
419
420 return kTRUE;
421}
Note: See TracBrowser for help on using the repository browser.