source: trunk/MagicSoft/Mars/mhist/MHStarMap.cc@ 3340

Last change on this file since 3340 was 3340, checked in by wittek, 21 years ago
*** empty log message ***
File size: 12.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Wolfgang Wittek 02/2004 <mailto:wittek@mppmu.mpg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MHStarMap
29//
30// This class contains a 2-dimensional histogram. It should show some
31// kind of star map. The algorith which calculates the star map
32// from the Hillas parameters (Fill) can be enhanced.
33//
34// For a given a shower, a series of points along its main axis are filled
35// into the 2-dim histogram (x, y) of the camera plane.
36//
37// Before filling a point (x, y) into the histogram it is
38// - shifted by (xSrc, ySrc) (the expected source position)
39// - and rotated in order to compensate the rotation of the
40// sky image in the camera
41//
42//
43/////////////////////////////////////////////////////////////////////////////
44#include "MHStarMap.h"
45
46#include <TH2.h>
47#include <TStyle.h> // gStyle
48#include <TColor.h> // SetRGB
49#include <TCanvas.h> // TCanvas
50
51#include "MLog.h"
52#include "MLogManip.h"
53
54#include "MParList.h"
55
56#include "MGeomCam.h"
57#include "MHillas.h"
58#include "MBinning.h"
59#include "MMcEvt.hxx"
60#include "MSrcPosCam.h"
61#include "MObservatory.h"
62
63ClassImp(MHStarMap);
64
65using namespace std;
66
67// --------------------------------------------------------------------------
68//
69// Create the star map histogram
70//
71MHStarMap::MHStarMap(const char *name, const char *title)
72 : fMm2Deg(-1), fUseMmScale(kTRUE)
73{
74 //
75 // default constructor
76 // creates an a list of histograms for all pixels and both gain channels
77 //
78
79 //
80 // set the name and title of this object
81 //
82 fName = name ? name : "MHStarMap" ;
83 fTitle = title ? title : "Container for a Star Map" ;
84
85 //
86 // loop over all Pixels and create two histograms
87 // one for the Low and one for the High gain
88 // connect all the histogram with the container fHist
89 //
90 fStarMap = new TH2F("StarMap", " 2D Hillas Star Map ",
91 150, -300, 300,
92 150, -300, 300);
93
94 fStarMap->SetDirectory(NULL);
95
96 fStarMap->SetXTitle("x [mm]");
97 fStarMap->SetYTitle("y [mm]");
98 fStarMap->SetZTitle("Counts");
99}
100
101// --------------------------------------------------------------------------
102//
103// delete the histogram instance
104//
105MHStarMap::~MHStarMap()
106{
107 delete fStarMap;
108}
109
110// --------------------------------------------------------------------------
111//
112// Setup the Binning for the histograms automatically if the correct
113// instances of MBinning (with the names 'BinningCamera')
114// are found in the parameter list
115// Use this function if you want to set the conversion factor which
116// is used to convert the mm-scale in the camera plain into the deg-scale
117// used for histogram presentations. The conversion factor is part of
118// the camera geometry. Please create a corresponding MGeomCam container.
119//
120Bool_t MHStarMap::SetupFill(const MParList *plist)
121{
122 const MGeomCam *geom = (MGeomCam*)plist->FindObject("MGeomCam");
123 if (geom)
124 {
125 fMm2Deg = geom->GetConvMm2Deg();
126 fUseMmScale = kFALSE;
127
128 fStarMap->SetXTitle("x [\\circ]");
129 fStarMap->SetYTitle("y [\\circ]");
130 }
131
132 fMcEvt = (MMcEvt*)plist->FindObject("MMcEvt");
133 if (!fMcEvt)
134 {
135 *fLog << err << "MMcEvt not found... aborting." << endl;
136 return kFALSE;
137 }
138
139 fSrcPos = (MSrcPosCam*)plist->FindObject(AddSerialNumber("MSrcPosCam"));
140 if (!fSrcPos)
141 {
142 *fLog << err << "MSrcPosCam not found... aborting" << endl;
143 return kFALSE;
144 }
145
146
147 const MBinning *bins = (MBinning*)plist->FindObject("BinningStarMap");
148 if (!bins)
149 {
150 float r = geom ? geom->GetMaxRadius() : 600;
151 r *= 5./6;
152 if (!fUseMmScale)
153 r *= fMm2Deg;
154
155 MBinning b;
156 b.SetEdges(100, -r, r);
157 SetBinning(fStarMap, &b, &b);
158 }
159 else
160 SetBinning(fStarMap, bins, bins);
161
162 if (!geom)
163 {
164 *fLog << warn << "No Camera Geometry available. Using mm-scale for histograms." << endl;
165 return kTRUE;
166 }
167
168 *fLog << warn << "WARNING - Using MHStarMap doesn't take care of the Source Position!" << endl;
169
170 return kTRUE;
171}
172
173// --------------------------------------------------------------------------
174//
175// Get the observatory location "MObservatory" from parameter list
176//
177Bool_t MHStarMap::ReInit(MParList *pList)
178{
179 MObservatory *obs = (MObservatory*)pList->FindObject(AddSerialNumber("MObservatory"));
180 if (!obs)
181 {
182 *fLog << err << "MObservatory not found... aborting" << endl;
183 return kFALSE;
184 }
185
186 fSinLat = TMath::Sin(obs->GetPhi());
187 fCosLat = TMath::Cos(obs->GetPhi());
188
189 return kTRUE;
190}
191
192// --------------------------------------------------------------------------
193//
194// RotationAngle
195//
196// calculates the angle for the rotation of the sky image in the camera;
197// this angle is a function of the local coordinates (theta, phi) in rad.
198//
199// calculate rotation angle alpha of sky image in camera
200// (see TDAS 00-11, eqs. (18) and (20))
201//
202void MHStarMap::GetRotationAngle(Double_t &cosal, Double_t &sinal)
203{
204 const Double_t theta = fMcEvt->GetTelescopeTheta();
205 const Double_t phi = fMcEvt->GetTelescopePhi();
206
207 const Double_t sint = TMath::Sin(theta);
208 const Double_t cost = TMath::Cos(theta);
209
210 const Double_t sinp = TMath::Sin(phi);
211 const Double_t cosp = TMath::Cos(phi);
212
213 const Double_t v1 = sint*sinp;
214 const Double_t v2 = fCosLat*cost - fSinLat*sint*cosp;
215
216 const Double_t denom = 1./ TMath::Sqrt(v1*v1 + v2*v2);
217
218 cosal = (fSinLat * sint) + fCosLat * cost * cosp * denom;
219 sinal = (fCosLat * sinp) * denom;
220}
221
222// --------------------------------------------------------------------------
223//
224// Fill the four histograms with data from a MHillas-Container.
225// Be careful: Only call this with an object of type MHillas
226//
227Bool_t MHStarMap::Fill(const MParContainer *par, const Stat_t w)
228{
229 const MHillas &h = *(MHillas*)par;
230
231 const Float_t delta = h.GetDelta();
232
233 const Float_t m = TMath::Tan(delta);
234
235 const Float_t cosd = 1.0/TMath::Sqrt(m*m+1);
236 const Float_t sind = TMath::Sqrt(1-cosd*cosd);
237
238 Float_t t = h.GetMeanY() - m*h.GetMeanX();
239
240 Float_t xSource = fSrcPos->GetX();
241 Float_t ySource = fSrcPos->GetY();
242
243 if (!fUseMmScale)
244 {
245 t *= fMm2Deg;
246 xSource *= fMm2Deg;
247 ySource *= fMm2Deg;
248 }
249
250 // get step size ds along the main axis of the ellipse
251 const TAxis &axex = *fStarMap->GetXaxis();
252 const Float_t xmin = axex.GetXmin();
253 const Float_t xmax = axex.GetXmax();
254
255 // FIXME: Fixed number?
256 const Float_t ds = (xmax-xmin) / 200.0;
257
258 // Fill points along the main axis of the shower into the histogram;
259 // before filling
260 // - perform a translation by (xSource, ySource)
261 // - and perform a rotation to compensate the rotation of the
262 // sky image in the camera
263 Double_t cosal;
264 Double_t sinal;
265 GetRotationAngle(cosal, sinal);
266
267 if (m>-1 && m<1)
268 {
269 const Float_t dx = ds * cosd;
270
271 for (float x=xmin+dx/2; x<(xmax-xmin)+dx; x+=dx)
272 {
273 const Float_t xorig = x - xSource;
274 const Float_t yorig = m*x+t - ySource;
275
276 const Float_t xfill = cosal*xorig + sinal*yorig;
277 const Float_t yfill = -sinal*xorig + cosal*yorig;
278 fStarMap->Fill(xfill, yfill, w);
279 }
280 }
281 else
282 {
283 const TAxis &axey = *fStarMap->GetYaxis();
284 const Float_t ymin = axey.GetXmin();
285 const Float_t ymax = axey.GetXmax();
286
287 const float dy = ds * sind;
288
289 for (float y=ymin+dy/2; y<(ymax-ymin)+dy; y+=dy)
290 {
291 const Float_t xorig = (y-t)/m - xSource;
292 const Float_t yorig = y - ySource;
293
294 const Float_t xfill = cosal*xorig + sinal*yorig;
295 const Float_t yfill = -sinal*xorig + cosal*yorig;
296 fStarMap->Fill(xfill, yfill, w);
297 }
298 }
299
300 return kTRUE;
301}
302
303// --------------------------------------------------------------------------
304//
305// Use this function to setup your own conversion factor between degrees
306// and millimeters. The conversion factor should be the one calculated in
307// MGeomCam. Use this function with Caution: You could create wrong values
308// by setting up your own scale factor.
309//
310void MHStarMap::SetMm2Deg(Float_t mmdeg)
311{
312 if (mmdeg<0)
313 {
314 *fLog << warn << dbginf << "Warning - Conversion factor < 0 - nonsense. Ignored." << endl;
315 return;
316 }
317
318 if (fMm2Deg>=0)
319 *fLog << warn << dbginf << "Warning - Conversion factor already set. Overwriting" << endl;
320
321 fMm2Deg = mmdeg;
322}
323
324// --------------------------------------------------------------------------
325//
326// With this function you can convert the histogram ('on the fly') between
327// degrees and millimeters.
328//
329void MHStarMap::SetMmScale(Bool_t mmscale)
330{
331 if (fUseMmScale == mmscale)
332 return;
333
334 if (fMm2Deg<0)
335 {
336 *fLog << warn << dbginf << "Warning - Sorry, no conversion factor for conversion available." << endl;
337 return;
338 }
339
340 if (fUseMmScale)
341 {
342 fStarMap->SetXTitle("x [mm]");
343 fStarMap->SetYTitle("y [mm]");
344
345 fStarMap->Scale(1./fMm2Deg);
346 }
347 else
348 {
349 fStarMap->SetXTitle("x [\\circ]");
350 fStarMap->SetYTitle("y [\\circ]");
351
352 fStarMap->Scale(1./fMm2Deg);
353 }
354
355 fUseMmScale = mmscale;
356}
357
358// --------------------------------------------------------------------------
359//
360// Set the palette you wanna use:
361// - you could set the root "Pretty Palette Violet->Red" by
362// gStyle->SetPalette(1, 0), but in some cases this may look
363// confusing
364// - The maximum colors root allowes us to set by ourself
365// is 50 (idx: 51-100). This colors are set to a grayscaled
366// palette
367// - the number of contours must be two less than the number
368// of palette entries
369//
370void MHStarMap::PrepareDrawing() const
371{
372 const Int_t numg = 32; // number of gray scaled colors
373 const Int_t numw = 32; // number of white
374
375 Int_t palette[numg+numw];
376
377 //
378 // The first half of the colors are white.
379 // This is some kind of optical background supression
380 //
381 gROOT->GetColor(51)->SetRGB(1, 1, 1);
382
383 Int_t i;
384 for (i=0; i<numw; i++)
385 palette[i] = 51;
386
387 //
388 // now the (gray) scaled part is coming
389 //
390 for (;i<numw+numg; i++)
391 {
392 const Float_t gray = 1.0-(float)(i-numw)/(numg-1.0);
393
394 gROOT->GetColor(52+i)->SetRGB(gray, gray, gray);
395 palette[i] = 52+i;
396 }
397
398 //
399 // Set the palette and the number of contour levels
400 //
401 gStyle->SetPalette(numg+numw, palette);
402 fStarMap->SetContour(numg+numw-2);
403}
404
405// --------------------------------------------------------------------------
406//
407// Draw clones of the histograms, so that the object can be deleted
408// and the histogram is still visible in the canvas.
409// The cloned object is deleted together with the canvas if the canvas is
410// destroyed. If you want to handle destroying the canvas you can get a
411// pointer to it from this function
412//
413TObject *MHStarMap::DrawClone(Option_t *opt) const
414{
415 return MH::DrawClone(opt, 500, 500);
416}
417
418// --------------------------------------------------------------------------
419//
420// Creates a new canvas and draw the histogram into it.
421// Be careful: The histogram belongs to this object and won't get deleted
422// together with the canvas.
423//
424void MHStarMap::Draw(Option_t *)
425{
426 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this, 500, 500);
427 pad->SetBorderMode(0);
428
429 pad->Divide(1,1);
430
431 pad->cd(1);
432 gPad->SetBorderMode(0);
433
434 AppendPad("");
435}
436
437void MHStarMap::Paint(Option_t *opt)
438{
439 //
440 // Maintain aspect ratio
441 //
442 const float w = gPad->GetWw();
443 const float h = gPad->GetWh();
444
445 if (h<w)
446 gPad->SetPad((1.-h/w)/2, 0, (h/w+1)/2, 1);
447 else
448 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1)/2);
449
450 //
451 // Maintain colors
452 //
453 PrepareDrawing();
454
455 //
456 // Paint Histogram
457 //
458 fStarMap->Paint("colz");
459}
Note: See TracBrowser for help on using the repository browser.