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 05/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | ! Author(s): Harald Kornmayer 1/2001
|
---|
20 | !
|
---|
21 | ! Copyright: MAGIC Software Development, 2000-2002
|
---|
22 | !
|
---|
23 | !
|
---|
24 | \* ======================================================================== */
|
---|
25 |
|
---|
26 | /////////////////////////////////////////////////////////////////////////////
|
---|
27 | //
|
---|
28 | // MCamDisplay
|
---|
29 | //
|
---|
30 | // Camera Display
|
---|
31 | //
|
---|
32 | ////////////////////////////////////////////////////////////////////////////
|
---|
33 | #include "MCamDisplay.h"
|
---|
34 |
|
---|
35 | #include <math.h>
|
---|
36 | #include <fstream.h>
|
---|
37 |
|
---|
38 | #include <TClonesArray.h>
|
---|
39 | #include <TCanvas.h>
|
---|
40 | #include <TStyle.h>
|
---|
41 | #include <TBox.h>
|
---|
42 | #include <TText.h>
|
---|
43 |
|
---|
44 | #include "MHexagon.h"
|
---|
45 |
|
---|
46 | #include "MGeomPix.h"
|
---|
47 | #include "MGeomCam.h"
|
---|
48 |
|
---|
49 | #include "MCerPhotPix.h"
|
---|
50 | #include "MCerPhotEvt.h"
|
---|
51 |
|
---|
52 | #define kItemsLegend 25 // see SetPalette(1,0)
|
---|
53 |
|
---|
54 | ClassImp(MCamDisplay);
|
---|
55 |
|
---|
56 | // ------------------------------------------------------------------------
|
---|
57 | //
|
---|
58 | // default constructor
|
---|
59 | //
|
---|
60 | MCamDisplay::MCamDisplay(MGeomCam *geom)
|
---|
61 | : fAutoScale(kTRUE), fMinPhe(-2), fMaxPhe(50), fW(0), fH(0), fDrawingPad(NULL), fIsAllocated(kFALSE)
|
---|
62 | {
|
---|
63 | fGeomCam = (MGeomCam*)geom; // FIXME: Clone doesn't work! (MGeomCam*)geom->Clone();
|
---|
64 |
|
---|
65 | //
|
---|
66 | // create the hexagons of the display
|
---|
67 | //
|
---|
68 | fNumPixels = geom->GetNumPixels();
|
---|
69 | fRange = geom->GetMaxRadius();
|
---|
70 |
|
---|
71 | //
|
---|
72 | // Construct all hexagons. Use new-operator with placement
|
---|
73 | //
|
---|
74 |
|
---|
75 | // root 3.02
|
---|
76 | // * base/inc/TObject.h:
|
---|
77 | // register BIT(8) as kNoContextMenu. If an object has this bit set it will
|
---|
78 | // not get an automatic context menu when clicked with the right mouse button.
|
---|
79 |
|
---|
80 | fPixels = new TClonesArray("MHexagon", fNumPixels);
|
---|
81 | for (UInt_t i=0; i<fNumPixels; i++)
|
---|
82 | new ((*fPixels)[i]) MHexagon((*geom)[i]);
|
---|
83 |
|
---|
84 | //
|
---|
85 | // set the color palette for the TBox elements
|
---|
86 | //
|
---|
87 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
|
---|
88 | gStyle->SetPalette(1, 0);
|
---|
89 | #else
|
---|
90 | gStyle->SetPalette(51, 0);
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | //
|
---|
94 | // set up the Legend
|
---|
95 | //
|
---|
96 | fLegend = new TClonesArray("TBox", kItemsLegend);
|
---|
97 | fLegText = new TClonesArray("TText", kItemsLegend);
|
---|
98 |
|
---|
99 | for (Int_t i = 0; i<kItemsLegend; i++)
|
---|
100 | {
|
---|
101 | TBox *newbox = new ((*fLegend)[i]) TBox;
|
---|
102 | TText *newtxt = new ((*fLegText)[i]) TText;
|
---|
103 |
|
---|
104 | const Float_t lvl = 50. / kItemsLegend * i;
|
---|
105 |
|
---|
106 | newbox->SetFillColor(GetColor(lvl));
|
---|
107 |
|
---|
108 | newtxt->SetTextSize(0.025);
|
---|
109 | newtxt->SetTextAlign(12);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | // ------------------------------------------------------------------------
|
---|
114 | //
|
---|
115 | // Destructor. Deletes TClonesArrays for hexagons and legend elements.
|
---|
116 | //
|
---|
117 | MCamDisplay::~MCamDisplay()
|
---|
118 | {
|
---|
119 | fPixels->Delete();
|
---|
120 | fLegend->Delete();
|
---|
121 | fLegText->Delete();
|
---|
122 |
|
---|
123 | delete fPixels;
|
---|
124 | delete fLegend;
|
---|
125 | delete fLegText;
|
---|
126 |
|
---|
127 | // delete fGeomCam;
|
---|
128 |
|
---|
129 | if (fIsAllocated)
|
---|
130 | delete fDrawingPad;
|
---|
131 | }
|
---|
132 |
|
---|
133 | inline void MCamDisplay::SetPixColor(const MCerPhotPix &pix, const Int_t i)
|
---|
134 | {
|
---|
135 | //
|
---|
136 | // Fixme: Divide pnum by the (real) area of the pixel
|
---|
137 | //
|
---|
138 | const Float_t ratio = (*fGeomCam)[0].GetA()/(*fGeomCam)[i].GetA();
|
---|
139 | const Float_t pnum = ratio*pix.GetNumPhotons();
|
---|
140 |
|
---|
141 | (*this)[pix.GetPixId()].SetFillColor(GetColor(pnum));
|
---|
142 | }
|
---|
143 |
|
---|
144 | // ------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | // This is called at any time the canvas should get repainted.
|
---|
147 | // Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
|
---|
148 | // that the camera image doesn't get distorted by resizing the canvas.
|
---|
149 | //
|
---|
150 | void MCamDisplay::Paint(Option_t *opt)
|
---|
151 | {
|
---|
152 | const UInt_t w = (UInt_t)(gPad->GetWw()*gPad->GetAbsWNDC());
|
---|
153 | const UInt_t h = (UInt_t)(gPad->GetWh()*gPad->GetAbsHNDC());
|
---|
154 |
|
---|
155 | //
|
---|
156 | // Check for a change in width or height, and make sure, that the
|
---|
157 | // first call also sets the range
|
---|
158 | //
|
---|
159 | if (w*fH == h*fW && fW && fH)
|
---|
160 | return;
|
---|
161 |
|
---|
162 | //
|
---|
163 | // Calculate aspect ratio (5/4=1.25 recommended)
|
---|
164 | //
|
---|
165 | const Double_t ratio = (Double_t)w/h;
|
---|
166 |
|
---|
167 | Float_t x;
|
---|
168 | Float_t y;
|
---|
169 |
|
---|
170 | if (ratio>1.25)
|
---|
171 | {
|
---|
172 | x = (ratio*2-1)*fRange;
|
---|
173 | y = fRange;
|
---|
174 | }
|
---|
175 | else
|
---|
176 | {
|
---|
177 | x = fRange*1.5;
|
---|
178 | y = fRange*1.25/ratio;
|
---|
179 | }
|
---|
180 |
|
---|
181 | fH = h;
|
---|
182 | fW = w;
|
---|
183 |
|
---|
184 | //
|
---|
185 | // Set new range
|
---|
186 | //
|
---|
187 | gPad->Range(-fRange, -y, x, y);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // ------------------------------------------------------------------------
|
---|
191 | //
|
---|
192 | // Call this function to draw the camera layout into your canvas.
|
---|
193 | // Setup a drawing canvas. Add this object and all child objects
|
---|
194 | // (hexagons, etc) to the current pad. If no pad exists a new one is
|
---|
195 | // created.
|
---|
196 | //
|
---|
197 | void MCamDisplay::Draw(Option_t *option)
|
---|
198 | {
|
---|
199 | // root 3.02:
|
---|
200 | // gPad->SetFixedAspectRatio()
|
---|
201 |
|
---|
202 | if (fDrawingPad)
|
---|
203 | return;
|
---|
204 |
|
---|
205 | //
|
---|
206 | // if no canvas is yet existing to draw into, create a new one
|
---|
207 | //
|
---|
208 | if (!gPad)
|
---|
209 | {
|
---|
210 | fDrawingPad = new TCanvas("CamDisplay", "Magic Camera Display", 0, 0, 750, 600);
|
---|
211 | fIsAllocated = kTRUE;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | fDrawingPad = gPad;
|
---|
216 | fIsAllocated = kFALSE;
|
---|
217 | }
|
---|
218 |
|
---|
219 | fDrawingPad->SetBorderMode(0);
|
---|
220 |
|
---|
221 | //
|
---|
222 | // Append this object, so that the aspect ratio is maintained
|
---|
223 | // (Paint-function is called)
|
---|
224 | //
|
---|
225 | AppendPad(option);
|
---|
226 |
|
---|
227 | //
|
---|
228 | // Setup the correct environment
|
---|
229 | //
|
---|
230 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
|
---|
231 | gStyle->SetPalette(1, 0);
|
---|
232 | #else
|
---|
233 | gStyle->SetPalette(51, 0);
|
---|
234 | #endif
|
---|
235 |
|
---|
236 | gPad->SetFillColor(22);
|
---|
237 |
|
---|
238 | //
|
---|
239 | // Draw all pixels of the camera
|
---|
240 | // (means apend all pixelobjects to the current pad)
|
---|
241 | //
|
---|
242 | for (UInt_t i=0; i<fNumPixels; i++)
|
---|
243 | (*this)[i].Draw();
|
---|
244 |
|
---|
245 | //
|
---|
246 | // draw legend
|
---|
247 | //
|
---|
248 | const Float_t H = 0.9*fRange;
|
---|
249 | const Float_t h = 2./kItemsLegend;
|
---|
250 |
|
---|
251 | const Float_t w = fRange/sqrt(fNumPixels);
|
---|
252 |
|
---|
253 | for (Int_t i=0; i<kItemsLegend; i++)
|
---|
254 | {
|
---|
255 | TBox *box = GetBox(i);
|
---|
256 | box->SetX1(fRange);
|
---|
257 | box->SetX2(fRange+w);
|
---|
258 | box->SetY1(H*( i *h - 1.));
|
---|
259 | box->SetY2(H*((i+1)*h - 1.));
|
---|
260 | box->Draw();
|
---|
261 |
|
---|
262 | TText *txt = GetText(i);
|
---|
263 | txt->SetX(fRange+1.5*w);
|
---|
264 | txt->SetY(H*((i+0.5)*h - 1.));
|
---|
265 | txt->Draw();
|
---|
266 | }
|
---|
267 |
|
---|
268 | //fDrawingPad->SetEditable(kFALSE);
|
---|
269 | }
|
---|
270 |
|
---|
271 | void MCamDisplay::DrawPixelNumbers()
|
---|
272 | {
|
---|
273 | if (!fDrawingPad)
|
---|
274 | Draw();
|
---|
275 |
|
---|
276 | fDrawingPad->cd();
|
---|
277 |
|
---|
278 | TText txt;
|
---|
279 | txt.SetTextFont(122);
|
---|
280 | txt.SetTextAlign(22); // centered/centered
|
---|
281 |
|
---|
282 | for (UInt_t i=0; i<fNumPixels; i++)
|
---|
283 | {
|
---|
284 | TString num;
|
---|
285 | num += i;
|
---|
286 |
|
---|
287 | const MHexagon &h = (MHexagon&)*(*fPixels)[i];
|
---|
288 | TText *nt = txt.DrawText(h.GetX(), h.GetY(), num);
|
---|
289 | nt->SetTextSize(0.0005*h.GetD());
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | // ------------------------------------------------------------------------
|
---|
294 | //
|
---|
295 | // Call this function to draw the number of photo electron into the
|
---|
296 | // camera.
|
---|
297 | //
|
---|
298 | void MCamDisplay::DrawPhotNum(const MCerPhotEvt *event)
|
---|
299 | {
|
---|
300 | if (!event)
|
---|
301 | return;
|
---|
302 |
|
---|
303 | if (!fDrawingPad)
|
---|
304 | Draw();
|
---|
305 |
|
---|
306 | fDrawingPad->cd();
|
---|
307 |
|
---|
308 | //
|
---|
309 | // Reset pixel colors to default value
|
---|
310 | //
|
---|
311 | Reset();
|
---|
312 |
|
---|
313 | //
|
---|
314 | // if the autoscale is true, set the values for the range for
|
---|
315 | // each event
|
---|
316 | //
|
---|
317 | if (fAutoScale)
|
---|
318 | {
|
---|
319 | fMinPhe = event->GetNumPhotonsMin(fGeomCam);
|
---|
320 | fMaxPhe = event->GetNumPhotonsMax(fGeomCam);
|
---|
321 |
|
---|
322 | if (fMaxPhe < 20.)
|
---|
323 | fMaxPhe = 20.;
|
---|
324 |
|
---|
325 | UpdateLegend();
|
---|
326 | }
|
---|
327 |
|
---|
328 | //
|
---|
329 | // update the colors in the picture
|
---|
330 | //
|
---|
331 | const Int_t entries = event->GetNumPixels();
|
---|
332 |
|
---|
333 | for (Int_t i=0; i<entries; i++)
|
---|
334 | {
|
---|
335 | const MCerPhotPix &pix = (*event)[i];
|
---|
336 |
|
---|
337 | if (!pix.IsPixelUsed())
|
---|
338 | continue;
|
---|
339 |
|
---|
340 | SetPixColor(pix, i);
|
---|
341 | }
|
---|
342 |
|
---|
343 | //
|
---|
344 | // Update display physically
|
---|
345 | //
|
---|
346 | gPad->Modified();
|
---|
347 | gPad->Update();
|
---|
348 | }
|
---|
349 |
|
---|
350 | // ------------------------------------------------------------------------
|
---|
351 | //
|
---|
352 | // reset the all pixel colors to a default value
|
---|
353 | //
|
---|
354 | void MCamDisplay::Reset()
|
---|
355 | {
|
---|
356 | for (UInt_t i=0; i<fNumPixels; i++)
|
---|
357 | (*this)[i].SetFillColor(10);
|
---|
358 | }
|
---|
359 |
|
---|
360 | // ------------------------------------------------------------------------
|
---|
361 | //
|
---|
362 | // Here we calculate the color index for the current value.
|
---|
363 | // The color index is defined with the class TStyle and the
|
---|
364 | // Color palette inside. We use the command gStyle->SetPalette(1,0)
|
---|
365 | // for the display. So we have to convert the value "wert" into
|
---|
366 | // a color index that fits the color palette.
|
---|
367 | // The range of the color palette is defined by the values fMinPhe
|
---|
368 | // and fMaxRange. Between this values we have 50 color index, starting
|
---|
369 | // with 0 up to 49.
|
---|
370 | //
|
---|
371 | Int_t MCamDisplay::GetColor(Float_t val)
|
---|
372 | {
|
---|
373 | //
|
---|
374 | // first treat the over- and under-flows
|
---|
375 | //
|
---|
376 | const Int_t maxcolidx = 49;
|
---|
377 |
|
---|
378 | if (val >= fMaxPhe)
|
---|
379 | return gStyle->GetColorPalette(maxcolidx);
|
---|
380 |
|
---|
381 | if (val <= fMinPhe)
|
---|
382 | return gStyle->GetColorPalette(0);
|
---|
383 |
|
---|
384 | //
|
---|
385 | // calculate the color index
|
---|
386 | //
|
---|
387 | const Float_t ratio = (val-fMinPhe) / (fMaxPhe-fMinPhe);
|
---|
388 | const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
|
---|
389 |
|
---|
390 | return gStyle->GetColorPalette(colidx);
|
---|
391 | }
|
---|
392 |
|
---|
393 | // ------------------------------------------------------------------------
|
---|
394 | //
|
---|
395 | // change the text on the legend according to the range of the
|
---|
396 | // Display
|
---|
397 | //
|
---|
398 | void MCamDisplay::UpdateLegend()
|
---|
399 | {
|
---|
400 | char text[10];
|
---|
401 |
|
---|
402 | for (Int_t i=0; i<kItemsLegend; i++)
|
---|
403 | {
|
---|
404 | const Float_t val = fMinPhe + (Float_t)i/kItemsLegend * (fMaxPhe-fMinPhe) ;
|
---|
405 |
|
---|
406 | sprintf(text, "%5.1f", val);
|
---|
407 |
|
---|
408 | TText &txt = *GetText(i);
|
---|
409 |
|
---|
410 | txt.SetText(txt.GetX(), txt.GetY(), text);
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | // ------------------------------------------------------------------------
|
---|
415 | //
|
---|
416 | // Save primitive as a C++ statement(s) on output stream out
|
---|
417 | //
|
---|
418 | void MCamDisplay::SavePrimitive(ofstream &out, Option_t *opt)
|
---|
419 | {
|
---|
420 | if (!gROOT->ClassSaved(TCanvas::Class()))
|
---|
421 | fDrawingPad->SavePrimitive(out, opt);
|
---|
422 |
|
---|
423 | out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
|
---|
424 | out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
|
---|
425 | }
|
---|