source: tags/Mars-V0.9.4/mtools/MagicShow.cc

Last change on this file was 2173, 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>
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
169// ------------------------------------------------------------------------
170//
171// default constructor
172//
173MagicShow::MagicShow()
174 : fTimer(this, 250, kTRUE), fGeomCam(NULL), fNumPixel(-1), fAuto(kTRUE), fW(0), fH(0)
175{
176 SetNewCamera(new MGeomCamMagic);
177
178 memset(fText, 0, sizeof(fText));
179
180 //
181 // Make sure, that the object is destroyed when the canvas/pad is
182 // destroyed. Make also sure, that the interpreter doesn't try to
183 // delete it a second time.
184 //
185 SetBit(kCanDelete);
186 gInterpreter->DeleteGlobal(this);
187
188 Draw();
189
190 fTimer.TurnOn();
191}
192
193// ------------------------------------------------------------------------
194//
195// Destructor. Deletes TClonesArrays for hexagons and legend elements.
196//
197MagicShow::~MagicShow()
198{
199 Free();
200
201 for (int i=0; i<6; i++)
202 if (fText[i])
203 delete fText[i];
204
205 if (fDrawingPad->GetListOfPrimitives()->FindObject(this)==this)
206 {
207 fDrawingPad->RecursiveRemove(this);
208 delete fDrawingPad;
209 }
210}
211
212// ------------------------------------------------------------------------
213//
214// This is called at any time the canvas should get repainted.
215// Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
216// that the camera image doesn't get distorted by resizing the canvas.
217//
218void MagicShow::Paint(Option_t *opt)
219{
220 const UInt_t w = (UInt_t)(gPad->GetWw()*gPad->GetAbsWNDC());
221 const UInt_t h = (UInt_t)(gPad->GetWh()*gPad->GetAbsHNDC());
222
223 //
224 // Check for a change in width or height, and make sure, that the
225 // first call also sets the range
226 //
227 if (w*fH == h*fW && fW && fH)
228 return;
229
230 //
231 // Calculate aspect ratio (5/4=1.25 recommended)
232 //
233 const Double_t ratio = (Double_t)w/h;
234
235 Float_t x;
236 Float_t y;
237
238 if (ratio>1.0)
239 {
240 x = fRange*(ratio*2-1);
241 y = fRange;
242 }
243 else
244 {
245 x = fRange;
246 y = fRange/ratio;
247 }
248
249 fH = h;
250 fW = w;
251
252 //
253 // Set new range
254 //
255 fDrawingPad->Range(-fRange, -y, x, y);
256}
257
258// ------------------------------------------------------------------------
259//
260// Call this function to draw the camera layout into your canvas.
261// Setup a drawing canvas. Add this object and all child objects
262// (hexagons, etc) to the current pad. If no pad exists a new one is
263// created.
264//
265void MagicShow::Draw(Option_t *option)
266{
267 //
268 // if no canvas is yet existing to draw into, create a new one
269 //
270 /*TCanvas *c =*/ new TCanvas("MagicShow", "Magic Show Next Neighbours", 0, 0, 800, 800);
271 //c->ToggleEventStatus();
272
273 fDrawingPad = gPad;
274 fDrawingPad->SetBorderMode(0);
275 fDrawingPad->SetFillColor(22);
276
277 //
278 // Append this object, so that the aspect ratio is maintained
279 // (Paint-function is called)
280 //
281 AppendPad(option);
282
283 //
284 // Reset the game pad
285 //
286 DrawHexagons();
287
288 fShow.SetTextAlign(23); // centered/bottom
289#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
290 fShow.SetBit(kNoContextMenu|kCannotPick);
291#endif
292 fShow.Draw();
293}
294
295// ------------------------------------------------------------------------
296//
297// Update Status text
298//
299void MagicShow::Update()
300{
301 TString txt = "Pixels: ";
302 txt += fNumPixels;
303 txt += " Pixel: ";
304 txt += fNumPixel;
305
306 if (fAuto)
307 txt += " (auto)";
308
309 fShow.SetText(0, fRange, txt);
310}
311
312// ------------------------------------------------------------------------
313//
314// Execute a mouse event on the camera
315//
316void MagicShow::ExecuteEvent(Int_t event, Int_t keycode, Int_t keysym)
317{
318 if (event!=kKeyPress)
319 return;
320
321 switch (keysym)
322 {
323 case kKey_Space:
324 fAuto = !fAuto;
325 Update();
326 fDrawingPad->Update();
327 return;
328
329 case kKey_Right:
330 ChangePixel(+1);
331 return;
332
333 case kKey_Left:
334 ChangePixel(-1);
335 return;
336
337 case kKey_Up:
338 ChangePixel(+10);
339 return;
340
341 case kKey_Down:
342 ChangePixel(-10);
343 return;
344
345 case kKey_PageUp:
346 ChangePixel(+100);
347 return;
348
349 case kKey_PageDown:
350 ChangePixel(-100);
351 return;
352
353 case kKey_Home:
354 ChangePixel(-fNumPixel);
355 return;
356
357 case kKey_End:
358 ChangePixel(fNumPixels-fNumPixel-1);
359 return;
360 }
361}
362
363// ------------------------------------------------------------------------
364//
365// Change the shown pixel by add indices
366//
367void MagicShow::ChangePixel(Int_t add)
368{
369 MagicShow &This = *this;
370
371 const MGeomPix &pix1=(*fGeomCam)[fNumPixel];
372 This[fNumPixel].SetFillColor(kBackground);
373 for (int i=0; i<pix1.GetNumNeighbors(); i++)
374 {
375 This[pix1.GetNeighbor(i)].SetFillColor(kBackground);
376 if (!fText[i])
377 continue;
378
379 delete fText[i];
380 fText[i] = NULL;
381 }
382
383 fNumPixel += add;
384
385 if (fNumPixel>=fNumPixels)
386 fNumPixel = 0;
387 if (fNumPixel<0)
388 fNumPixel = fNumPixels-1;
389
390 const MGeomPix &pix2=(*fGeomCam)[fNumPixel];
391 This[fNumPixel].SetFillColor(kBlue);
392 for (int i=0; i<pix2.GetNumNeighbors(); i++)
393 {
394 Int_t idx = pix2.GetNeighbor(i);
395
396 This[idx].SetFillColor(kMagenta);
397
398 TString num;
399 num += idx;
400
401 fText[i] = new TText(This[idx].GetX(), This[idx].GetY(), num);
402 fText[i]->SetTextSize(0.3*This[idx].GetD()/fGeomCam->GetMaxRadius());
403 fText[i]->SetTextFont(122);
404 fText[i]->SetTextAlign(22); // centered/centered
405 fText[i]->Draw();
406 }
407
408 Update();
409
410 fDrawingPad->Update();
411}
412
413// ------------------------------------------------------------------------
414//
415// If automatic is switched on step one pixel forward
416//
417Bool_t MagicShow::HandleTimer(TTimer *timer)
418{
419 if (fAuto)
420 ChangePixel(+1);
421
422 return kTRUE;
423}
Note: See TracBrowser for help on using the repository browser.