source: trunk/MagicSoft/Mars/manalysis/MHillas.cc@ 972

Last change on this file since 972 was 961, checked in by tbretz, 23 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 8.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): Harald Kornmayer 1/2001 (harald@mppmu.mpg.de)
19! Author(s): Thomas Bretz 12/2000 (tbretz@uni-sw.gwdg.de)
20!
21! Copyright: MAGIC Software Development, 2000-2001
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27// //
28// MHillas //
29// //
30// Storage Container for the Hillas parameter //
31// //
32// FIXME: - Here everybody should find an explanation of the parameters //
33// - using boooleans for fIsPixelUsed, fIsPixelCore, ... is rather //
34// slow because you have to loop over all pixels in any loop. //
35// There could be a huge speed improvement using Hash Tables //
36// (linked lists, see THashTable and THashList, too) //
37// //
38/////////////////////////////////////////////////////////////////////////////
39#include "MHillas.h"
40
41#include <math.h>
42#include <fstream.h>
43
44#include <TEllipse.h>
45
46#include "MCerPhotEvt.h"
47#include "MCerPhotPix.h"
48#include "MGeomCam.h"
49
50#include "MLog.h"
51
52ClassImp(MHillas);
53
54// --------------------------------------------------------------------------
55//
56// Default constructor.
57//
58MHillas::MHillas(const char *name, const char *title) : fEllipse(NULL)
59{
60 *fName = name ? name : "MHillas";
61 *fTitle = title ? title : "Storage container for Hillas parameter of one event";
62
63 Reset();
64 // FIXME: (intelligent) initialization of values missing
65}
66
67// --------------------------------------------------------------------------
68//
69// Destructor. Deletes the TEllipse if one exists.
70//
71MHillas::~MHillas()
72{
73 Clear();
74}
75
76void MHillas::Reset()
77{
78 fAlpha = 0;
79 fTheta = 0;
80 fWidth = 0;
81 fLength = 0;
82 fSize = 0;
83 fDist = 0;
84
85 Clear();
86}
87
88// --------------------------------------------------------------------------
89//
90// Print the hillas Parameters to *fLog
91//
92void MHillas::Print(Option_t *)
93{
94 *fLog << "Hillas Parameter:" << endl;
95 *fLog << " - Alpha = " << fabs(fAlpha) << endl;
96 *fLog << " - Width = " << fWidth << endl;
97 *fLog << " - Length = " << fLength << endl;
98 *fLog << " - Size = " << fSize << endl;
99 *fLog << " - Dist = " << fDist << endl;
100}
101
102// --------------------------------------------------------------------------
103//
104// call the Paint function of the Ellipse if a TEllipse exists
105//
106void MHillas::Paint(Option_t *)
107{
108 if (!fEllipse)
109 return;
110
111 fEllipse->Paint();
112}
113
114// --------------------------------------------------------------------------
115//
116// Instead of adding MHillas itself to the Pad
117// (s. AppendPad in TObject) we create an ellipse,
118// which is added to the Pad by it's Draw function
119// You can remove it by deleting the Ellipse Object
120// (s. Clear() )
121//
122void MHillas::Draw(Option_t *)
123{
124 Clear();
125
126 fEllipse = new TEllipse(cos(fTheta)*fDist, sin(fTheta)*fDist,
127 fLength, fWidth,
128 0, 360, fTheta*kRad2Deg+fAlpha-180);
129
130 fEllipse->SetLineWidth(2);
131 fEllipse->Draw();
132
133 /*
134 This is from TH1
135 TString opt = option;
136 opt.ToLower();
137 if (gPad && !opt.Contains("same")) {
138 //the following statement is necessary in case one attempts to draw
139 //a temporary histogram already in the current pad
140 if (TestBit(kCanDelete)) gPad->GetListOfPrimitives()->Remove(this);
141 gPad->Clear();
142 }
143 AppendPad(opt.Data());
144 */
145}
146
147// --------------------------------------------------------------------------
148//
149// If a TEllipse object exists it is deleted
150//
151void MHillas::Clear(Option_t *)
152{
153 if (!fEllipse)
154 return;
155
156 delete fEllipse;
157
158 fEllipse = NULL;
159}
160
161// --------------------------------------------------------------------------
162//
163// Calculate the Hillas parameters from a cerenkov photon event
164// (The calcualtion is some kind of two dimentional statistics)
165//
166// FIXME: MHillas::Calc is rather slow at the moment because it loops
167// unnecessarily over all pixels in all its loops (s.MImgCleanStd)
168// The speed could be improved very much by using Hash-Tables
169// (linked lists, see THashTable and THashList, too)
170//
171Bool_t MHillas::Calc(const MGeomCam &geom, const MCerPhotEvt &evt)
172{
173 const UInt_t nevt = evt.GetNumPixels();
174
175 //
176 // sanity check
177 //
178 if (nevt <= 2)
179 return kFALSE;
180
181 //
182 // calculate mean valu of pixels
183 //
184 float xmean =0;
185 float ymean =0;
186
187 fSize = 0;
188
189 //
190 // FIXME! npix should be retrieved from MCerPhotEvt
191 //
192 UShort_t npix=0;
193 for (UInt_t i=0; i<nevt; i++)
194 {
195 const MCerPhotPix &pix = evt[i];
196
197 if (!pix.IsPixelUsed())
198 continue;
199
200 const MGeomPix &gpix = geom[pix.GetPixId()];
201
202 const float nphot = pix.GetNumPhotons();
203
204 fSize += nphot;
205 xmean += nphot * gpix.GetX(); // [mm]
206 ymean += nphot * gpix.GetY(); // [mm]
207
208 npix++;
209 }
210
211 //
212 // sanity check
213 //
214 if (fSize==0 || npix<=2)
215 return kFALSE;
216
217 xmean /= fSize; // [mm]
218 ymean /= fSize; // [mm]
219
220 //
221 // calculate sdev
222 //
223 float sigmaxx=0;
224 float sigmaxy=0;
225 float sigmayy=0;
226
227 for (UInt_t i=0; i<nevt; i++)
228 {
229 const MCerPhotPix &pix = evt[i];
230
231 if (!pix.IsPixelUsed())
232 continue;
233
234 const MGeomPix &gpix = geom[pix.GetPixId()];
235
236 const float dx = gpix.GetX() - xmean;
237 const float dy = gpix.GetY() - ymean;
238
239 const float nphot = pix.GetNumPhotons();
240
241 sigmaxx += nphot * dx*dx; // [mm^2]
242 sigmaxy += nphot * dx*dy; // [mm^2]
243 sigmayy += nphot * dy*dy; // [mm^2]
244 }
245
246 //
247 // check for orientation
248 //
249 const float theta = atan(sigmaxy/(sigmaxx-sigmayy)*2)/2;
250
251 float c = cos(theta); // [1]
252 float s = sin(theta); // [1]
253
254 //
255 // calculate the length of the two axis
256 //
257 float axis1 = 2.0*c*s*sigmaxy + c*c*sigmaxx + s*s*sigmayy; // [mm^2]
258 float axis2 = -2.0*c*s*sigmaxy + s*s*sigmaxx + c*c*sigmayy; // [mm^2]
259
260 axis1 /= fSize; // [mm^2]
261 axis2 /= fSize; // [mm^2]
262
263 //
264 // check for numerical negatives
265 // (very small number can get negative by chance)
266 //
267 if (axis1 < 0) axis1=0;
268 if (axis2 < 0) axis2=0;
269
270 //
271 // calculate the main Hillas parameters
272 //
273 // fLength, fWidth describes the two axis of the ellipse
274 // fAlpha is the angle between the length-axis and the center
275 // of the camera
276 // fDist is the distance between the center of the camera and the
277 // denter of the ellipse
278 //
279 const int rotation = axis1<axis2;
280
281 fLength = rotation ? sqrt(axis2) : sqrt(axis1); // [mm]
282 fWidth = rotation ? sqrt(axis1) : sqrt(axis2); // [mm]
283
284 const float a = c*xmean + s*ymean;
285 const float b = c*ymean - s*xmean;
286
287 fAlpha = rotation ? atan(a/b) : atan(-b/a); // [rad]
288 fAlpha *= kRad2Deg; // [deg]
289
290 fDist = sqrt(xmean*xmean + ymean*ymean); // [mm]
291
292 fTheta = atan(ymean/xmean); // [rad]
293 if (xmean<0) fTheta += kPI; // [rad]
294
295 SetReadyToSave();
296
297 return kTRUE;
298}
299
300void MHillas::AsciiRead(ifstream &fin)
301{
302 fin >> fAlpha;
303 fin >> fTheta;
304 fin >> fWidth;
305 fin >> fLength;
306 fin >> fSize;
307 fin >> fDist;
308}
309
310void MHillas::AsciiWrite(ofstream &fout) const
311{
312 fout << fAlpha << " ";
313 fout << fTheta << " ";
314 fout << fWidth << " ";
315 fout << fLength << " ";
316 fout << fSize << " ";
317 fout << fDist << endl;
318}
Note: See TracBrowser for help on using the repository browser.