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

Last change on this file since 2567 was 2468, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 9.5 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 <fstream>
63
64#include <TArrayF.h>
65#include <TEllipse.h>
66
67#include "MGeomPix.h"
68#include "MGeomCam.h"
69
70#include "MCerPhotPix.h"
71#include "MCerPhotEvt.h"
72
73#include "MLog.h"
74#include "MLogManip.h"
75
76ClassImp(MHillas);
77
78using namespace std;
79
80// --------------------------------------------------------------------------
81//
82// Default constructor.
83//
84MHillas::MHillas(const char *name, const char *title)
85{
86 fName = name ? name : "MHillas";
87 fTitle = title ? title : "Storage container for image parameters of one event";
88
89 Reset();
90}
91
92// --------------------------------------------------------------------------
93//
94// Destructor. Deletes the TEllipse if one exists.
95//
96MHillas::~MHillas()
97{
98}
99
100// --------------------------------------------------------------------------
101//
102// Initializes the values with defaults. For the default values see the
103// source code.
104//
105void MHillas::Reset()
106{
107 fLength = -1;
108 fWidth = -1;
109 fDelta = 0;
110
111 fSize = -1;
112 fMeanX = 0;
113 fMeanY = 0;
114}
115
116// --------------------------------------------------------------------------
117//
118// Print the hillas Parameters to *fLog
119//
120void MHillas::Print(Option_t *) const
121{
122 Double_t atg = atan2(fMeanY, fMeanX)*kRad2Deg;
123
124 if (atg<0)
125 atg += 180;
126
127 *fLog << all;
128 *fLog << "Basic Image Parameters (" << GetName() << ")" << endl;
129 *fLog << " - Length [mm] = " << fLength << endl;
130 *fLog << " - Width [mm] = " << fWidth << endl;
131 *fLog << " - Delta [deg] = " << fDelta*kRad2Deg << endl;
132 *fLog << " - Size [1] = " << fSize << " #CherPhot" << endl;
133 *fLog << " - Meanx [mm] = " << fMeanX << endl;
134 *fLog << " - Meany [mm] = " << fMeanY << endl;
135 *fLog << " - atg(y/x) [deg] = " << atg << endl;
136}
137
138// --------------------------------------------------------------------------
139//
140// Paint the ellipse corresponding to the parameters
141//
142void MHillas::Paint(Option_t *opt)
143{
144 if (fLength<0 || fWidth<0)
145 return;
146
147 TEllipse e(fMeanX, fMeanY, fLength, fWidth, 0, 360, fDelta*kRad2Deg+180);
148 e.SetLineWidth(2);
149 e.Paint();
150}
151
152// --------------------------------------------------------------------------
153//
154// Calculate the image parameters from a Cherenkov photon event
155// assuming Cher.photons/pixel and pixel coordinates are given
156// In case you don't call Calc from within an eventloop make sure, that
157// you call the Reset member function before.
158// Returns:
159// 0 no error
160// 1 number of pixels < 3
161// 2 size==0
162// 3 number of used pixel < 3
163// 4 CorrXY == 0
164//
165Int_t MHillas::Calc(const MGeomCam &geom, const MCerPhotEvt &evt)
166{
167 const UInt_t npixevt = evt.GetNumPixels();
168
169 //
170 // sanity check
171 //
172 if (npixevt < 3)
173 return 1;
174
175 //
176 // calculate mean value of pixel coordinates and fSize
177 // -----------------------------------------------------
178 //
179 // Because this are only simple sums of roughly 600 values
180 // with an accuracy less than three digits there should not
181 // be any need to use double precision (Double_t) for the
182 // calculation of fMeanX, fMeanY and fSize.
183 //
184 fMeanX = 0;
185 fMeanY = 0;
186 fSize = 0;
187
188 Int_t numused = 0;
189
190 for (UInt_t i=0; i<npixevt; i++)
191 {
192 const MCerPhotPix &pix = evt[i];
193
194 if (!pix.IsPixelUsed())
195 continue;
196
197 const MGeomPix &gpix = geom[pix.GetPixId()];
198
199 const Float_t nphot = pix.GetNumPhotons();
200
201 fSize += nphot; // [counter]
202 fMeanX += nphot * gpix.GetX(); // [mm]
203 fMeanY += nphot * gpix.GetY(); // [mm]
204
205 numused++;
206 }
207
208 //
209 // sanity checks
210 //
211 if (fSize==0)
212 return 2;
213
214 fMeanX /= fSize; // [mm]
215 fMeanY /= fSize; // [mm]
216
217 if (numused<3)
218 return 3;
219
220 //
221 // calculate 2nd moments
222 // ---------------------
223 //
224 // To make sure that the more complicated sum is evaluated as
225 // accurate as possible (because it is needed for more
226 // complicated calculations (see below) we calculate it using
227 // double prcision.
228 //
229 Double_t corrxx=0; // [m^2]
230 Double_t corrxy=0; // [m^2]
231 Double_t corryy=0; // [m^2]
232
233 for (UInt_t i=0; i<npixevt; i++)
234 {
235 const MCerPhotPix &pix = evt[i];
236
237 if (!pix.IsPixelUsed())
238 continue;
239
240 const MGeomPix &gpix = geom[pix.GetPixId()];
241
242 const Float_t dx = gpix.GetX() - fMeanX; // [mm]
243 const Float_t dy = gpix.GetY() - fMeanY; // [mm]
244
245 const Float_t nphot = pix.GetNumPhotons(); // [#phot]
246
247 corrxx += nphot * dx*dx; // [mm^2]
248 corrxy += nphot * dx*dy; // [mm^2]
249 corryy += nphot * dy*dy; // [mm^2]
250 }
251
252 //
253 // If corrxy=0 (which should happen not very often, because fSize>0)
254 // we cannot calculate Length and Width.
255 // In reallity it is almost impossible to have a distribution of
256 // cerenkov photons in the used pixels which is exactly symmetric
257 // along one of the axis.
258 // It seems to be less than 0.1% of all events.
259 //
260 if (corrxy==0)
261 return 4;
262
263 //
264 // calculate the basic Hillas parameters: orientation and size of axes
265 // -------------------------------------------------------------------
266 //
267 // fDelta is the angle between the major axis of the ellipse and
268 // the x axis. It is independent of the position of the ellipse
269 // in the camera it has values between -pi/2 and pi/2 degrees
270 //
271 // Rem: I tested replacing sqrt() by hypot() but they exactly
272 // consume the same amount of time
273 //
274 const Double_t d0 = corryy - corrxx;
275 const Double_t d1 = corrxy*2;
276 const Double_t d2 = d0 + sqrt(d0*d0 + d1*d1);
277 const Double_t tand = d2 / d1;
278 const Double_t tand2 = tand*tand;
279
280 fDelta = atan(tand);
281
282 const Double_t s2 = tand2+1;
283 const Double_t s = sqrt(s2);
284
285 fCosDelta = 1.0/s; // need these in derived classes
286 fSinDelta = tand/s; // like MHillasExt
287
288 Double_t axis1 = (tand2*corryy + d2 + corrxx)/s2/fSize;
289 Double_t axis2 = (tand2*corrxx - d2 + corryy)/s2/fSize;
290
291 //
292 // fLength^2 is the second moment along the major axis of the ellipse
293 // fWidth^2 is the second moment along the minor axis of the ellipse
294 //
295 // From the algorithm we get: fWidth <= fLength is always true
296 //
297 // very small numbers can get negative by rounding
298 //
299 fLength = axis1<0 ? 0 : sqrt(axis1); // [mm]
300 fWidth = axis2<0 ? 0 : sqrt(axis2); // [mm]
301
302 SetReadyToSave();
303
304 return 0;
305}
306
307// --------------------------------------------------------------------------
308//
309// This function is ment for special usage, please never try to set
310// values via this function
311//
312void MHillas::Set(const TArrayF &arr)
313{
314 if (arr.GetSize() != 6)
315 return;
316
317 fLength = arr.At(0); // [mm] major axis of ellipse
318 fWidth = arr.At(1); // [mm] minor axis of ellipse
319 fDelta = arr.At(2); // [rad] angle of major axis with x-axis
320 fSize = arr.At(3); // [#CerPhot] sum of content of all pixels (number of Cherenkov photons)
321 fMeanX = arr.At(4); // [mm] x-coordinate of center of ellipse
322 fMeanY = arr.At(5); // [mm] y-coordinate of center of ellipse
323}
Note: See TracBrowser for help on using the repository browser.