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

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