source: trunk/MagicSoft/Mars/mimage/MHillas.cc@ 7177

Last change on this file since 7177 was 7177, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 12.2 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@uni-sw.gwdg.de>
19! Author(s): Harald Kornmayer 1/2001
20! Author(s): Rudolf Bock 10/2001 <mailto:Rudolf.Bock@cern.ch>
21! Author(s): Wolfgang Wittek 6/2002 <mailto:wittek@mppmu.mpg.de>
22!
23! Copyright: MAGIC Software Development, 2000-2003
24!
25!
26\* ======================================================================== */
27
28/////////////////////////////////////////////////////////////////////////////
29//
30// MHillas
31//
32// Storage Container for image parameters
33//
34// basic image parameters
35//
36// Version 1:
37// ----------
38// fLength [mm] major axis of ellipse
39// fWidth [mm] minor axis
40// fDelta [rad] angle of major axis with x-axis
41// by definition the major axis is pointing into
42// the hemisphere x>0, thus -pi/2 < delta < pi/2
43// fSize [#CerPhot] total sum of pixels
44// fMeanX [mm] x of center of ellipse
45// fMeanY [mm] y of center of ellipse
46//
47// Version 2:
48// ----------
49// fNumCorePixels number of pixels called core
50// fNumUsedPixels number of pixels which survived the cleaning
51//
52// Version 3:
53// ----------
54// fNumCorePixels moved to MNewImagePar
55// fNumUsedPixels moved to MNewImagePar
56// fCosDelta added
57// fSinDelte added
58//
59/////////////////////////////////////////////////////////////////////////////
60#include "MHillas.h"
61
62#include <TArrayF.h>
63
64#include <TLine.h>
65#include <TEllipse.h>
66#include <TVector2.h>
67
68#include "MGeomPix.h"
69#include "MGeomCam.h"
70
71#include "MSignalPix.h"
72#include "MSignalCam.h"
73
74#include "MLog.h"
75#include "MLogManip.h"
76
77ClassImp(MHillas);
78
79using namespace std;
80
81// --------------------------------------------------------------------------
82//
83// Default constructor.
84//
85MHillas::MHillas(const char *name, const char *title)
86{
87 fName = name ? name : "MHillas";
88 fTitle = title ? title : "Storage container for image parameters of one event";
89
90 Reset();
91}
92
93// --------------------------------------------------------------------------
94//
95// Initializes the values with defaults. For the default values see the
96// source code.
97//
98void MHillas::Reset()
99{
100 fLength = -1;
101 fWidth = -1;
102 fDelta = 0;
103
104 fSize = -1;
105 fMeanX = 0;
106 fMeanY = 0;
107}
108
109// --------------------------------------------------------------------------
110//
111// return the mean position as TVector2(meanx, meany)
112//
113TVector2 MHillas::GetMean() const
114{
115 return TVector2(fMeanX, fMeanY);
116}
117
118// --------------------------------------------------------------------------
119//
120// Analytical estimation of length of border line:
121// U = pi*(a+b - sqrt(a*b))
122//
123// GetBorderLine() [mm]
124//
125Double_t MHillas::GetBorderLine() const
126{
127 const Double_t a = fWidth+fLength;
128 const Double_t s = fWidth*fLength;
129
130 return TMath::Pi()*(1.5*a - TMath::Sqrt(s));
131}
132
133// --------------------------------------------------------------------------
134//
135// Analytical estimation of length of border line:
136// A = pi*a*b
137//
138// GetArea() [mm^2]
139//
140Double_t MHillas::GetArea() const
141{
142 return TMath::Pi()*fWidth*fLength;
143}
144
145// --------------------------------------------------------------------------
146//
147// Print the hillas Parameters to *fLog
148//
149void MHillas::Print(Option_t *) const
150{
151 Double_t atg = atan2(fMeanY, fMeanX)*kRad2Deg;
152
153 if (atg<0)
154 atg += 180;
155
156 *fLog << all;
157 *fLog << GetDescriptor() << endl;
158 *fLog << " - Length [mm] = " << fLength << endl;
159 *fLog << " - Width [mm] = " << fWidth << endl;
160 *fLog << " - Delta [deg] = " << fDelta*kRad2Deg << endl;
161 *fLog << " - Size [phe] = " << fSize << endl;
162 *fLog << " - Meanx [mm] = " << fMeanX << endl;
163 *fLog << " - Meany [mm] = " << fMeanY << endl;
164 *fLog << " - atg(y/x) [deg] = " << atg << endl;
165}
166
167// --------------------------------------------------------------------------
168//
169// Print the hillas Parameters to *fLog, depending on the geometry in
170// units of deg
171//
172void MHillas::Print(const MGeomCam &geom) const
173{
174 Double_t atg = atan2(fMeanY, fMeanX)*kRad2Deg;
175
176 if (atg<0)
177 atg += 180;
178
179 *fLog << all;
180 *fLog << GetDescriptor() << endl;
181 *fLog << " - Length [deg] = " << fLength*geom.GetConvMm2Deg() << endl;
182 *fLog << " - Width [deg] = " << fWidth *geom.GetConvMm2Deg() << endl;
183 *fLog << " - Delta [deg] = " << fDelta*kRad2Deg << endl;
184 *fLog << " - Size [phe] = " << fSize << endl;
185 *fLog << " - Meanx [deg] = " << fMeanX *geom.GetConvMm2Deg() << endl;
186 *fLog << " - Meany [deg] = " << fMeanY *geom.GetConvMm2Deg() << endl;
187 *fLog << " - atg(y/x) [deg] = " << atg << endl;
188}
189
190// --------------------------------------------------------------------------
191//
192// Paint the ellipse corresponding to the parameters
193//
194void MHillas::Paint(Option_t *opt)
195{
196 static const Float_t sOffsetW=40.0;
197 static const Float_t sOffsetL=266.0;
198
199 if (fLength<0 || fWidth<0)
200 return;
201
202 const Float_t l = fLength+sOffsetL;
203 const Float_t w = fWidth +sOffsetW;
204
205 TLine line;
206 line.SetLineWidth(1);
207 line.SetLineColor(kRed);
208
209 // Length line
210 line.PaintLine(-l*fCosDelta+fMeanX, -l*fSinDelta+fMeanY,
211 +l*fCosDelta+fMeanX, +l*fSinDelta+fMeanY);
212
213
214 // Width line
215 line.PaintLine(+w*fSinDelta+fMeanX, -w*fCosDelta+fMeanY,
216 -w*fSinDelta+fMeanX, +w*fCosDelta+fMeanY);
217
218 // Rough estimate for disp
219 const Double_t p = 362*(1-fWidth/fLength);
220
221 // Disp line
222 line.PaintLine( p*fCosDelta+20*fSinDelta+fMeanX, p*fSinDelta-20*fCosDelta+fMeanY,
223 p*fCosDelta-20*fSinDelta+fMeanX, p*fSinDelta+20*fCosDelta+fMeanY);
224 line.PaintLine(-p*fCosDelta+20*fSinDelta+fMeanX, -p*fSinDelta-20*fCosDelta+fMeanY,
225 -p*fCosDelta-20*fSinDelta+fMeanX, -p*fSinDelta+20*fCosDelta+fMeanY);
226
227 TEllipse e(fMeanX, fMeanY, fLength, fWidth, 0, 360, fDelta*kRad2Deg+180);
228 e.SetLineWidth(2);
229 e.SetLineColor(kGreen);
230 e.Paint();
231}
232
233// --------------------------------------------------------------------------
234//
235// Calculate the image parameters from a Cherenkov photon event
236// assuming Cher.photons/pixel and pixel coordinates are given
237// In case you don't call Calc from within an eventloop make sure, that
238// you call the Reset member function before.
239// Returns:
240// 0 no error
241// 1 number of pixels in event == 0 (special MC events)
242// 2 size==0
243// 3 number of used pixel < 3
244// 4 CorrXY == 0
245//
246Int_t MHillas::Calc(const MGeomCam &geom, const MSignalCam &evt, Int_t island)
247{
248 const UInt_t numpix = evt.GetNumPixels();
249
250 //
251 // sanity check 1 (special MC events)
252 //
253 if (numpix==0)
254 return 1;
255
256 //
257 // calculate mean value of pixel coordinates and fSize
258 // -----------------------------------------------------
259 //
260 // Because this are only simple sums of roughly 600 values
261 // with an accuracy less than three digits there should not
262 // be any need to use double precision (Double_t) for the
263 // calculation of fMeanX, fMeanY and fSize.
264 //
265 fMeanX = 0;
266 fMeanY = 0;
267 fSize = 0;
268
269 UInt_t numused = 0;
270
271 for (UInt_t i=0; i<numpix; i++)
272 {
273 MSignalPix &pix = evt[i];
274 if (!pix.IsPixelUsed())
275 continue;
276
277 if (island>=0 && pix.GetIdxIsland()!=island)
278 continue;
279
280 const MGeomPix &gpix = geom[i];
281
282 const Float_t nphot = pix.GetNumPhotons();
283
284 fSize += nphot; // [counter]
285 fMeanX += nphot * gpix.GetX(); // [mm]
286 fMeanY += nphot * gpix.GetY(); // [mm]
287
288 numused++;
289 }
290
291 //
292 // sanity check 2
293 //
294 if (fSize==0)
295 return 2;
296
297 fMeanX /= fSize; // [mm]
298 fMeanY /= fSize; // [mm]
299
300 //
301 // sanity check 3
302 //
303 if (numused<3)
304 return 3;
305
306 //
307 // calculate 2nd moments
308 // ---------------------
309 //
310 // To make sure that the more complicated sum is evaluated as
311 // accurate as possible (because it is needed for more
312 // complicated calculations (see below) we calculate it using
313 // double prcision.
314 //
315 Double_t corrxx=0; // [m^2]
316 Double_t corrxy=0; // [m^2]
317 Double_t corryy=0; // [m^2]
318
319 for (UInt_t i=0; i<numpix; i++)
320 {
321 const MSignalPix &pix = evt[i];
322 if (!pix.IsPixelUsed())
323 continue;
324
325 if (island>=0 && pix.GetIdxIsland()!=island)
326 continue;
327
328 const MGeomPix &gpix = geom[i];
329
330 const Float_t dx = gpix.GetX() - fMeanX; // [mm]
331 const Float_t dy = gpix.GetY() - fMeanY; // [mm]
332
333 const Float_t nphot = pix.GetNumPhotons(); // [#phot]
334
335 corrxx += nphot * dx*dx; // [mm^2]
336 corrxy += nphot * dx*dy; // [mm^2]
337 corryy += nphot * dy*dy; // [mm^2]
338 }
339
340 //
341 // calculate the basic Hillas parameters: orientation and size of axes
342 // -------------------------------------------------------------------
343 //
344 // fDelta is the angle between the major axis of the ellipse and
345 // the x axis. It is independent of the position of the ellipse
346 // in the camera it has values between -pi/2 and pi/2 degrees
347 //
348 // Rem: I tested replacing sqrt() by hypot() but they exactly
349 // consume the same amount of time
350 //
351 const Double_t d0 = corryy - corrxx;
352 const Double_t d1 = corrxy*2;
353 const Double_t d2 = TMath::Sqrt(d0*d0 + d1*d1) + d0; // [0
354
355 const Double_t tand = d2==0 ? 0 : d2 / d1; // Force 0 also if d1==0
356 const Double_t tand2 = tand*tand;
357
358 const Double_t s2 = tand2+1;
359 const Double_t s = TMath::Sqrt(s2);
360
361 // Set default for the case in which the image is symmetric on the y-axis
362 fDelta = TMath::Pi()/2;
363 fCosDelta = 0;
364 fSinDelta = 1;
365
366 Double_t axis1 = corryy;
367 Double_t axis2 = corrxx;
368
369 // This are all cases in which the image is not symmetric on the y-axis
370 if (d1!=0 || d2==0)
371 {
372 fDelta = TMath::ATan(tand);
373
374 fCosDelta = 1.0 /s; // need these in derived classes
375 fSinDelta = tand/s; // like MHillasExt
376
377 axis1 = (tand2*corryy + d2 + corrxx)/s2;
378 axis2 = (tand2*corrxx - d2 + corryy)/s2;
379 }
380
381 //
382 // fLength^2 is the second moment along the major axis of the distribution
383 // fWidth^2 is the second moment along the minor axis of the distribution
384 //
385 // From the algorithm we get: fWidth <= fLength is always true
386 //
387 // very small numbers can get negative by rounding
388 //
389 fLength = axis1<0 ? 0 : TMath::Sqrt(axis1/fSize); // [mm]
390 fWidth = axis2<0 ? 0 : TMath::Sqrt(axis2/fSize); // [mm]
391
392 SetReadyToSave();
393
394 return 0;
395}
396
397// --------------------------------------------------------------------------
398//
399// This function is ment for special usage, please never try to set
400// values via this function
401//
402void MHillas::Set(const TArrayF &arr)
403{
404 if (arr.GetSize() != 6)
405 return;
406
407 fLength = arr.At(0); // [mm] major axis of ellipse
408 fWidth = arr.At(1); // [mm] minor axis of ellipse
409 fDelta = arr.At(2); // [rad] angle of major axis with x-axis
410 fSize = arr.At(3); // [#CerPhot] sum of content of all pixels (number of Cherenkov photons)
411 fMeanX = arr.At(4); // [mm] x-coordinate of center of ellipse
412 fMeanY = arr.At(5); // [mm] y-coordinate of center of ellipse
413}
Note: See TracBrowser for help on using the repository browser.