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

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