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

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