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

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