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

Last change on this file since 984 was 981, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.1 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 = (UInt_t)(gPad->GetWw()*gPad->GetAbsWNDC());
91 const UInt_t h = (UInt_t)(gPad->GetWh()*gPad->GetAbsHNDC());
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 fDrawingPad = gPad;
144
145 //
146 // Append this object, so that the aspect ratio is maintained
147 // (Paint-function is called)
148 //
149 AppendPad(option);
150
151 //
152 // Setup the correct environment
153 //
154 gStyle->SetPalette(1, 0);
155
156 gPad->SetFillColor(22);
157
158 //
159 // Draw all pixels of the camera
160 // (means apend all pixelobjects to the current pad)
161 //
162 for (UInt_t i=0; i<fNumPixels; i++)
163 (*this)[i].Draw();
164
165 //
166 // draw legend
167 //
168 const Float_t H = 0.9*fRange;
169 const Float_t h = 2./kItemsLegend;
170
171 const Float_t w = fRange/sqrt(fNumPixels);
172
173 for (Int_t i=0; i<kItemsLegend; i++)
174 {
175 TBox *box = GetBox(i);
176 box->SetX1(fRange);
177 box->SetX2(fRange+w);
178 box->SetY1(H*( i *h - 1.));
179 box->SetY2(H*((i+1)*h - 1.));
180 box->Draw();
181
182 TText *txt = GetText(i);
183 txt->SetX(fRange+1.5*w);
184 txt->SetY(H*((i+0.5)*h - 1.));
185 txt->Draw();
186 }
187}
188
189// ------------------------------------------------------------------------
190//
191// Call this function to draw the number of photo electron into the
192// camera.
193//
194void MCamDisplay::DrawPhotNum(const MCerPhotEvt *event)
195{
196 if (!fDrawingPad)
197 Draw();
198
199 fDrawingPad->cd();
200
201 //
202 // Reset pixel colors to default value
203 //
204 Reset();
205
206 //
207 // if the autoscale is true, set the values for the range for
208 // each event
209 //
210 if (fAutoScale)
211 {
212 fMinPhe = event->GetNumPhotonsMin();
213 fMaxPhe = event->GetNumPhotonsMax();
214
215 if (fMaxPhe < 20.)
216 fMaxPhe = 20.;
217
218 UpdateLegend();
219 }
220
221 //
222 // update the colors in the picture
223 //
224 const Int_t entries = event->GetNumPixels();
225
226 for (Int_t i=0; i<entries; i++)
227 {
228 const MCerPhotPix &pix = (*event)[i];
229
230 if (!pix.IsPixelUsed())
231 continue;
232
233 SetPixColor(pix);
234 }
235
236 //
237 // Update display physically
238 //
239 gPad->Modified();
240 gPad->Update();
241}
242
243// ------------------------------------------------------------------------
244//
245// reset the all pixel colors to a default value
246//
247void MCamDisplay::Reset()
248{
249 for (UInt_t i=0; i<fNumPixels; i++)
250 (*this)[i].SetFillColor(10);
251}
252
253// ------------------------------------------------------------------------
254//
255// Here we calculate the color index for the current value.
256// The color index is defined with the class TStyle and the
257// Color palette inside. We use the command gStyle->SetPalette(1,0)
258// for the display. So we have to convert the value "wert" into
259// a color index that fits the color palette.
260// The range of the color palette is defined by the values fMinPhe
261// and fMaxRange. Between this values we have 50 color index, starting
262// with 0 up to 49.
263//
264Int_t MCamDisplay::GetColor(Float_t val)
265{
266 //
267 // first treat the over- and under-flows
268 //
269 const Float_t maxcolidx = 49.0;
270
271 if (val >= fMaxPhe)
272 return gStyle->GetColorPalette(maxcolidx);
273
274 if (val <= fMinPhe)
275 return gStyle->GetColorPalette(0);
276
277 //
278 // calculate the color index
279 //
280 const Float_t ratio = (val-fMinPhe) / (fMaxPhe-fMinPhe);
281 const Int_t colidx = (Int_t)(maxcolidx*ratio + .5);
282
283 return gStyle->GetColorPalette(colidx);
284}
285
286// ------------------------------------------------------------------------
287//
288// change the text on the legend according to the range of the
289// Display
290//
291void MCamDisplay::UpdateLegend()
292{
293 char text[10];
294
295 for (Int_t i=0; i<kItemsLegend; i++)
296 {
297 const Float_t val = fMinPhe + (Float_t)i/kItemsLegend * (fMaxPhe-fMinPhe) ;
298
299 sprintf(text, "%5.1f", val);
300
301 TText &txt = *GetText(i);
302
303 txt.SetText(txt.GetX(), txt.GetY(), text);
304 }
305}
Note: See TracBrowser for help on using the repository browser.