source: tags/Mars-V0.8.1/mimage/MHillas.cc

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