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

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