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

Last change on this file since 9203 was 8755, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 9.7 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>
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
74using namespace std;
75
76// ------------------------------------------------------------------------
77//
78// Free all onbects connected to a special camera geometry
79//
80void MagicShow::Free()
81{
82 if (!fGeomCam)
83 return;
84
85 fPixels->Delete();
86
87 delete fPixels;
88
89 delete fGeomCam;
90}
91
92// ------------------------------------------------------------------------
93//
94// Draw all pixels of the camera
95// (means apend all pixelobjects to the current pad)
96//
97void MagicShow::DrawHexagons()
98{
99 for (UInt_t i=0; i<fNumPixels; i++)
100 {
101 MHexagon &h = (*this)[i];
102
103 h.SetFillColor(kBackground);
104 h.Draw();
105 }
106}
107
108// ------------------------------------------------------------------------
109//
110// Change camera from Magic to CT1 and back
111//
112void MagicShow::ChangeCamera()
113{
114 static Bool_t ct1=kFALSE;
115
116 cout << "Change to " << (ct1?"Magic":"CT1") << endl;
117
118 if (ct1)
119 SetNewCamera(new MGeomCamMagic);
120 else
121 SetNewCamera(new MGeomCamCT1);
122
123 ct1 = !ct1;
124
125 DrawHexagons();
126}
127
128// ------------------------------------------------------------------------
129//
130// Reset/set all veriables needed for a new camera geometry
131//
132void MagicShow::SetNewCamera(MGeomCam *geom)
133{
134 Free();
135
136 //
137 // Reset the display geometry
138 //
139 fW=0;
140 fH=0;
141
142 //
143 // Set new camera
144 //
145 fGeomCam = geom;
146
147 //
148 // create the hexagons of the display
149 //
150 fNumPixels = fGeomCam->GetNumPixels();
151 fRange = fGeomCam->GetMaxRadius();
152
153 fNumPixel = fNumPixels-1;
154
155 //
156 // Construct all hexagons. Use new-operator with placement
157 //
158 fPixels = new TClonesArray("MHexagon", fNumPixels);
159
160 for (UInt_t i=0; i<fNumPixels; i++)
161 {
162 MHexagon &h = *new ((*fPixels)[i]) MHexagon((*fGeomCam)[i]);
163#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
164 h.SetBit(kNoContextMenu|kCannotPick);
165#endif
166 }
167}
168
169void MagicShow::Init()
170{
171 memset(fText, 0, sizeof(fText));
172
173 //
174 // Make sure, that the object is destroyed when the canvas/pad is
175 // destroyed. Make also sure, that the interpreter doesn't try to
176 // delete it a second time.
177 //
178 SetBit(kCanDelete);
179 gInterpreter->DeleteGlobal(this);
180
181 Draw();
182
183 fTimer.TurnOn();
184}
185
186// ------------------------------------------------------------------------
187//
188// default constructor
189//
190MagicShow::MagicShow()
191 : fTimer(this, 250, kTRUE), fGeomCam(NULL), fNumPixel(-1), fAuto(kTRUE), fW(0), fH(0)
192{
193 SetNewCamera(new MGeomCamMagic);
194 Init();
195}
196
197// ------------------------------------------------------------------------
198//
199// default constructor
200//
201MagicShow::MagicShow(const MGeomCam &geom)
202 : fTimer(this, 250, kTRUE), fGeomCam(NULL), fNumPixel(-1), fAuto(kTRUE), fW(0), fH(0)
203{
204 SetNewCamera(static_cast<MGeomCam*>(geom.Clone()));
205 Init();
206}
207
208// ------------------------------------------------------------------------
209//
210// Destructor. Deletes TClonesArrays for hexagons and legend elements.
211//
212MagicShow::~MagicShow()
213{
214 Free();
215
216 for (int i=0; i<6; i++)
217 if (fText[i])
218 delete fText[i];
219
220 if (fDrawingPad->GetListOfPrimitives()->FindObject(this)==this)
221 {
222 fDrawingPad->RecursiveRemove(this);
223 delete fDrawingPad;
224 }
225}
226
227// ------------------------------------------------------------------------
228//
229// This is called at any time the canvas should get repainted.
230// Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
231// that the camera image doesn't get distorted by resizing the canvas.
232//
233void MagicShow::Paint(Option_t *opt)
234{
235 const UInt_t w = (UInt_t)(gPad->GetWw()*gPad->GetAbsWNDC());
236 const UInt_t h = (UInt_t)(gPad->GetWh()*gPad->GetAbsHNDC());
237
238 //
239 // Check for a change in width or height, and make sure, that the
240 // first call also sets the range
241 //
242 if (w*fH == h*fW && fW && fH)
243 return;
244
245 //
246 // Calculate aspect ratio (5/4=1.25 recommended)
247 //
248 const Double_t ratio = (Double_t)w/h;
249
250 Float_t x;
251 Float_t y;
252
253 if (ratio>1.0)
254 {
255 x = fRange*(ratio*2-1);
256 y = fRange;
257 }
258 else
259 {
260 x = fRange;
261 y = fRange/ratio;
262 }
263
264 fH = h;
265 fW = w;
266
267 //
268 // Set new range
269 //
270 fDrawingPad->Range(-fRange, -y, x, y);
271}
272
273// ------------------------------------------------------------------------
274//
275// Call this function to draw the camera layout into your canvas.
276// Setup a drawing canvas. Add this object and all child objects
277// (hexagons, etc) to the current pad. If no pad exists a new one is
278// created.
279//
280void MagicShow::Draw(Option_t *option)
281{
282 //
283 // if no canvas is yet existing to draw into, create a new one
284 //
285 /*TCanvas *c =*/ new TCanvas("MagicShow", "Magic Show Next Neighbours", 0, 0, 800, 800);
286 //c->ToggleEventStatus();
287
288 fDrawingPad = gPad;
289 fDrawingPad->SetBorderMode(0);
290 fDrawingPad->SetFillColor(22);
291
292 //
293 // Append this object, so that the aspect ratio is maintained
294 // (Paint-function is called)
295 //
296 AppendPad(option);
297
298 //
299 // Reset the game pad
300 //
301 DrawHexagons();
302
303 fShow.SetTextAlign(23); // centered/bottom
304#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
305 fShow.SetBit(kNoContextMenu|kCannotPick);
306#endif
307 fShow.Draw();
308}
309
310// ------------------------------------------------------------------------
311//
312// Update Status text
313//
314void MagicShow::Update()
315{
316 TString txt = "Pixels: ";
317 txt += fNumPixels;
318 txt += " Pixel: ";
319 txt += fNumPixel;
320
321 if (fAuto)
322 txt += " (auto)";
323
324 fShow.SetText(0, fRange, txt);
325}
326
327// ------------------------------------------------------------------------
328//
329// Execute a mouse event on the camera
330//
331void MagicShow::ExecuteEvent(Int_t event, Int_t keycode, Int_t keysym)
332{
333 if (event!=kKeyPress)
334 return;
335
336 switch (keysym)
337 {
338 case kKey_Space:
339 fAuto = !fAuto;
340 Update();
341 fDrawingPad->Update();
342 return;
343
344 case kKey_Right:
345 ChangePixel(+1);
346 return;
347
348 case kKey_Left:
349 ChangePixel(-1);
350 return;
351
352 case kKey_Up:
353 ChangePixel(+10);
354 return;
355
356 case kKey_Down:
357 ChangePixel(-10);
358 return;
359
360 case kKey_PageUp:
361 ChangePixel(+100);
362 return;
363
364 case kKey_PageDown:
365 ChangePixel(-100);
366 return;
367
368 case kKey_Home:
369 ChangePixel(-fNumPixel);
370 return;
371
372 case kKey_End:
373 ChangePixel(fNumPixels-fNumPixel-1);
374 return;
375 }
376}
377
378// ------------------------------------------------------------------------
379//
380// Change the shown pixel by add indices
381//
382void MagicShow::ChangePixel(Int_t add)
383{
384 MagicShow &This = *this;
385
386 const MGeomPix &pix1=(*fGeomCam)[fNumPixel];
387 This[fNumPixel].SetFillColor(kBackground);
388 for (int i=0; i<pix1.GetNumNeighbors(); i++)
389 {
390 This[pix1.GetNeighbor(i)].SetFillColor(kBackground);
391 if (!fText[i])
392 continue;
393
394 delete fText[i];
395 fText[i] = NULL;
396 }
397
398 fNumPixel += add;
399
400 if (fNumPixel>=fNumPixels)
401 fNumPixel = 0;
402 if (fNumPixel<0)
403 fNumPixel = fNumPixels-1;
404
405 const MGeomPix &pix2=(*fGeomCam)[fNumPixel];
406 This[fNumPixel].SetFillColor(kBlue);
407 for (int i=0; i<pix2.GetNumNeighbors(); i++)
408 {
409 Int_t idx = pix2.GetNeighbor(i);
410
411 This[idx].SetFillColor(kMagenta);
412
413 TString num;
414 num += idx;
415
416 fText[i] = new TText(This[idx].GetX(), This[idx].GetY(), num);
417 fText[i]->SetTextSize(0.3*This[idx].GetD()/fGeomCam->GetMaxRadius());
418 fText[i]->SetTextFont(122);
419 fText[i]->SetTextAlign(22); // centered/centered
420 fText[i]->Draw();
421 }
422
423 Update();
424
425 fDrawingPad->Update();
426}
427
428// ------------------------------------------------------------------------
429//
430// If automatic is switched on step one pixel forward
431//
432Bool_t MagicShow::HandleTimer(TTimer *timer)
433{
434 if (fAuto)
435 ChangePixel(+1);
436
437 return kTRUE;
438}
Note: See TracBrowser for help on using the repository browser.