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

Last change on this file since 1228 was 1219, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 8.1 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 //
157 // if no canvas is yet existing to draw into, create a new one
158 //
159 if (!gPad)
160 fDrawingPad = new TCanvas("CamDisplay", "Magic Camera Display", 0, 0, 750, 600);
161 else
162 fDrawingPad = gPad;
163
164 fDrawingPad->SetBorderMode(0);
165
166 //
167 // Append this object, so that the aspect ratio is maintained
168 // (Paint-function is called)
169 //
170 AppendPad(option);
171
172 //
173 // Setup the correct environment
174 //
175 gStyle->SetPalette(1, 0);
176
177 gPad->SetFillColor(22);
178
179 //
180 // Draw all pixels of the camera
181 // (means apend all pixelobjects to the current pad)
182 //
183 for (UInt_t i=0; i<fNumPixels; i++)
184 (*this)[i].Draw();
185
186 //
187 // draw legend
188 //
189 const Float_t H = 0.9*fRange;
190 const Float_t h = 2./kItemsLegend;
191
192 const Float_t w = fRange/sqrt(fNumPixels);
193
194 for (Int_t i=0; i<kItemsLegend; i++)
195 {
196 TBox *box = GetBox(i);
197 box->SetX1(fRange);
198 box->SetX2(fRange+w);
199 box->SetY1(H*( i *h - 1.));
200 box->SetY2(H*((i+1)*h - 1.));
201 box->Draw();
202
203 TText *txt = GetText(i);
204 txt->SetX(fRange+1.5*w);
205 txt->SetY(H*((i+0.5)*h - 1.));
206 txt->Draw();
207 }
208
209 //fDrawingPad->SetEditable(kFALSE);
210}
211
212// ------------------------------------------------------------------------
213//
214// Call this function to draw the number of photo electron into the
215// camera.
216//
217void MCamDisplay::DrawPhotNum(const MCerPhotEvt *event)
218{
219 if (!fDrawingPad)
220 Draw();
221
222 fDrawingPad->cd();
223
224 //
225 // Reset pixel colors to default value
226 //
227 Reset();
228
229 //
230 // if the autoscale is true, set the values for the range for
231 // each event
232 //
233 if (fAutoScale)
234 {
235 fMinPhe = event->GetNumPhotonsMin(fGeomCam);
236 fMaxPhe = event->GetNumPhotonsMax(fGeomCam);
237
238 if (fMaxPhe < 20.)
239 fMaxPhe = 20.;
240
241 UpdateLegend();
242 }
243
244 //
245 // update the colors in the picture
246 //
247 const Int_t entries = event->GetNumPixels();
248
249 for (Int_t i=0; i<entries; i++)
250 {
251 const MCerPhotPix &pix = (*event)[i];
252
253 if (!pix.IsPixelUsed())
254 continue;
255
256 SetPixColor(pix, i);
257 }
258
259 //
260 // Update display physically
261 //
262 gPad->Modified();
263 gPad->Update();
264}
265
266// ------------------------------------------------------------------------
267//
268// reset the all pixel colors to a default value
269//
270void MCamDisplay::Reset()
271{
272 for (UInt_t i=0; i<fNumPixels; i++)
273 (*this)[i].SetFillColor(10);
274}
275
276// ------------------------------------------------------------------------
277//
278// Here we calculate the color index for the current value.
279// The color index is defined with the class TStyle and the
280// Color palette inside. We use the command gStyle->SetPalette(1,0)
281// for the display. So we have to convert the value "wert" into
282// a color index that fits the color palette.
283// The range of the color palette is defined by the values fMinPhe
284// and fMaxRange. Between this values we have 50 color index, starting
285// with 0 up to 49.
286//
287Int_t MCamDisplay::GetColor(Float_t val)
288{
289 //
290 // first treat the over- and under-flows
291 //
292 const Int_t maxcolidx = 49;
293
294 if (val >= fMaxPhe)
295 return gStyle->GetColorPalette(maxcolidx);
296
297 if (val <= fMinPhe)
298 return gStyle->GetColorPalette(0);
299
300 //
301 // calculate the color index
302 //
303 const Float_t ratio = (val-fMinPhe) / (fMaxPhe-fMinPhe);
304 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
305
306 return gStyle->GetColorPalette(colidx);
307}
308
309// ------------------------------------------------------------------------
310//
311// change the text on the legend according to the range of the
312// Display
313//
314void MCamDisplay::UpdateLegend()
315{
316 char text[10];
317
318 for (Int_t i=0; i<kItemsLegend; i++)
319 {
320 const Float_t val = fMinPhe + (Float_t)i/kItemsLegend * (fMaxPhe-fMinPhe) ;
321
322 sprintf(text, "%5.1f", val);
323
324 TText &txt = *GetText(i);
325
326 txt.SetText(txt.GetX(), txt.GetY(), text);
327 }
328}
329
330// ------------------------------------------------------------------------
331//
332// Save primitive as a C++ statement(s) on output stream out
333//
334void MCamDisplay::SavePrimitive(ofstream &out, Option_t *opt)
335{
336 if (!gROOT->ClassSaved(TCanvas::Class()))
337 fDrawingPad->SavePrimitive(out, opt);
338
339 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
340 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
341}
Note: See TracBrowser for help on using the repository browser.