source: trunk/MagicSoft/Mars/mgui/MCamDisplay.cc@ 1326

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