source: trunk/MagicSoft/Mars/mmuon/MMuonSearchPar.cc@ 7977

Last change on this file since 7977 was 7369, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 9.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): Keiichi Mase 10/2004 <mailto:mase@mppmu.mpg.de>
19! Author(s): Markus Meyer 10/2004 <mailto:meyer@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2005
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MMuonSearchPar
29//
30// Storage Container for muon
31//
32// This class is the container for muon parameters. The calculation
33// of Radius and center of the ring is done here.
34// Muons are searched by fitting the image with a circle.
35//
36// In order to use further information of muons such as the width of arcs,
37// the size of the fraction of the ring and the muons size, use the
38// infomation stored in
39//
40// MMuonCalibPar.
41//
42// The information will be available by using the task of
43//
44// MMuonCalibParCalc.
45//
46// Version 2:
47// ----------
48// + Float_t fTime; // Mean arrival time of core pixels
49// + Float_t fTimeRms; // Rms of arrival time of core pixels
50//
51// Input Containers:
52// MGeomCam
53// MHillas
54// MSignalCam
55//
56/////////////////////////////////////////////////////////////////////////////
57#include "MMuonSearchPar.h"
58
59#include <TMinuit.h>
60#include <TEllipse.h>
61
62#include "MLog.h"
63#include "MLogManip.h"
64
65#include "MHillas.h"
66
67#include "MGeomCam.h"
68#include "MGeomPix.h"
69
70#include "MSignalPix.h"
71#include "MSignalCam.h"
72
73using namespace std;
74
75ClassImp(MMuonSearchPar);
76
77// --------------------------------------------------------------------------
78//
79// Default constructor.
80//
81MMuonSearchPar::MMuonSearchPar(const char *name, const char *title)
82{
83 fName = name ? name : "MMuonSearchPar";
84 fTitle = title ? title : "Parameters to find Muons";
85}
86
87// --------------------------------------------------------------------------
88//
89void MMuonSearchPar::Reset()
90{
91 fRadius = -1;
92 fDeviation = -1;
93 fCenterX = 0;
94 fCenterY = 0;
95 fTime = 0;
96 fTimeRms = -1;
97}
98
99// --------------------------------------------------------------------------
100//
101// This is a wrapper function to have direct access to the data members
102// in the function calculating the minimization value.
103//
104void MMuonSearchPar::fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
105{
106 const MMuonSearchPar *optim = (MMuonSearchPar*)gMinuit->GetObjectFit();
107 f = optim->Fcn(par);
108}
109
110// --------------------------------------------------------------------------
111//
112// This function gives you the ring radius fitted best to the camera image
113// and its RMS for the input position.
114//
115Double_t MMuonSearchPar::Fcn(Double_t *par) const
116{
117 const Int_t entries = fSignal.GetSize();
118
119 Double_t meanr=0;
120 Double_t devr =0;
121 Double_t sums =0;
122
123 // It seems that the loop is easy enough for a compiler optimization.
124 // Using pointer arithmetics doesn't improve the speed of the fit.
125 for (Int_t i=0; i<entries; i++ )
126 {
127 Double_t tmp = TMath::Hypot(fX[i]-par[0], fY[i]-par[1]);
128
129 sums += fSignal[i];
130 meanr += fSignal[i] * tmp;
131 devr += fSignal[i] * tmp*tmp;
132 }
133
134 par[2] = meanr/sums;
135 par[3] = devr/sums - par[2]*par[2];
136
137 return par[3];
138}
139
140// --------------------------------------------------------------------------
141//
142// This function finds the center position of the circle which gives minimum
143// RMS of the fit, changing the center position of the circle.
144//
145void MMuonSearchPar::CalcMinimumDeviation(const MGeomCam &geom,
146 const MSignalCam &evt,
147 Double_t &x, Double_t &y,
148 Double_t &sigma, Double_t &radius)
149{
150 // ------- Make a temporaray copy of the signal ---------
151 // ------- and calculate arrival time parameters --------
152 const Int_t n = geom.GetNumPixels();
153
154 fSignal.Set(n);
155 fX.Set(n);
156 fY.Set(n);
157
158 Int_t p=0;
159 Int_t q=0;
160
161 Double_t mean=0;
162 Double_t sq =0;
163
164 for (int i=0; i<n; i++)
165 {
166 const MSignalPix &pix = evt[i];
167 if (!pix.IsPixelUsed())
168 continue;
169
170 fSignal[p] = pix.GetNumPhotons();
171
172 fX[p] = geom[i].GetX();
173 fY[p] = geom[i].GetY();
174 p++;
175
176 //timing
177 if (!pix.IsPixelCore())
178 continue;
179
180 mean += pix.GetArrivalTime();
181 sq += pix.GetArrivalTime()*pix.GetArrivalTime();
182 q++;
183 }
184
185 mean /= q;
186 sq /= q;
187
188 fTime = mean;
189 fTimeRms = TMath::Sqrt(sq-mean*mean);
190
191 fSignal.Set(p);
192
193
194 // ----------------- Setup and call minuit -------------------
195 const Float_t delta = 30.; // 3 mm (1/10 of an inner pixel size) Step to move.
196 const Double_t r = geom.GetMaxRadius()*2;
197
198 // Save gMinuit
199 TMinuit *minsave = gMinuit;
200
201 // Initialize TMinuit with 4 parameters
202 TMinuit minuit;
203 minuit.SetPrintLevel(-1); // Switch off printing
204 minuit.Command("set nowarn"); // Switch off warning
205 // Define Parameters
206 minuit.DefineParameter(0, "x", x, delta, -r, r);
207 minuit.DefineParameter(1, "y", y, delta, -r, r);
208 minuit.DefineParameter(2, "r", 0, 1, 0, 0);
209 minuit.DefineParameter(3, "sigma", 0, 1, 0, 0);
210 // Fix return parameters
211 minuit.FixParameter(2);
212 minuit.FixParameter(3);
213 // Setup Minuit for 'this' and use fit function fcn
214 minuit.SetObjectFit(this);
215 minuit.SetFCN(fcn);
216
217 // Perform Simplex minimization
218 Int_t err;
219 Double_t tmp[2] = { 0, 0 };
220 minuit.mnexcm("simplex", tmp, 2, err);
221
222 // Get resulting parameters
223 Double_t error;
224 minuit.GetParameter(0, x, error);
225 minuit.GetParameter(1, y, error);
226 minuit.GetParameter(2, radius, error);
227 minuit.GetParameter(3, sigma, error);
228
229 sigma = sigma>0 ? TMath::Sqrt(sigma) : 0;
230
231 gMinuit = minsave;
232}
233
234// --------------------------------------------------------------------------
235//
236// Calculation of muon parameters
237//
238void MMuonSearchPar::Calc(const MGeomCam &geom, const MSignalCam &evt,
239 const MHillas &hillas)
240{
241 Double_t x = hillas.GetMeanX();
242 Double_t y = hillas.GetMeanY();
243
244 // -------------------------------------------------
245 // Keiichi suggested trying to precalculate the Muon
246 // center a bit better, but it neither improves the
247 // fit result nor the speed
248 //
249 // const Float_t tmpr = 300.; // assume that the temporal cherenkov angle is 1 deg. (300 mm).
250 //
251 // const Double_t a = TMath::Tan(hillas.GetDelta());
252 //
253 // const Double_t dx = a/TMath::Sqrt(tmpr+a*a)/3.;
254 // const Double_t dy = -tmpr/TMath::Sqrt(1+a*a)/3.;
255 //
256 // Double_t par1[] = { x+dx, y+dy, 0, 0 };
257 // Double_t par2[] = { x-dx, y-dy, 0, 0 };
258 //
259 // const Double_t dev1 = MMuonSearchPar::Fcn(par1);
260 // const Double_t dev2 = MMuonSearchPar::Fcn(par2);
261 //
262 // if (dev1<dev2)
263 // {
264 // x += dx;
265 // y += dy;
266 // }
267 // else
268 // {
269 // x -= dx;
270 // y -= dy;
271 // }
272 // -------------------------------------------------
273
274 Double_t sigma, rad;
275
276 // find the best fit.
277 CalcMinimumDeviation(geom, evt, x, y, sigma, rad);
278
279 fCenterX = x;
280 fCenterY = y;
281 fRadius = rad;
282 fDeviation = sigma;
283
284 //SetReadyToSave();
285}
286
287void MMuonSearchPar::Print(Option_t *) const
288{
289 *fLog << all;
290 *fLog << GetDescriptor() << endl;
291 *fLog << " - Est. Radius [mm] = " << fRadius << endl;
292 *fLog << " - Deviation [mm] = " << fDeviation << endl;
293 *fLog << " - Center Pos. X [mm] = " << fCenterX << endl;
294 *fLog << " - Center Pos. Y [mm] = " << fCenterY << endl;
295}
296
297void MMuonSearchPar::Print(const MGeomCam &geom, Option_t *) const
298{
299 *fLog << all;
300 *fLog << GetDescriptor() << endl;
301 *fLog << " - Est. Radius [deg] = " << fRadius*geom.GetConvMm2Deg() << endl;
302 *fLog << " - Deviation [deg] = " << fDeviation*geom.GetConvMm2Deg() << endl;
303 *fLog << " - Center Pos. X [deg] = " << fCenterX*geom.GetConvMm2Deg() << endl;
304 *fLog << " - Center Pos. Y [deg] = " << fCenterY*geom.GetConvMm2Deg() << endl;
305}
306
307// --------------------------------------------------------------------------
308//
309// Paint the ellipse corresponding to the parameters
310//
311void MMuonSearchPar::Paint(Option_t *opt)
312{
313 if (fRadius<180 || fRadius>400 || fDeviation>45)
314 return;
315
316 TEllipse e1(fCenterX, fCenterY, fRadius-fDeviation, fRadius-fDeviation);
317 TEllipse e2(fCenterX, fCenterY, fRadius+fDeviation, fRadius+fDeviation);
318 e1.SetLineWidth(1);
319 e2.SetLineWidth(1);
320 e1.SetLineColor(kYellow);
321 e2.SetLineColor(kYellow);
322 e1.Paint();
323 e2.Paint();
324}
Note: See TracBrowser for help on using the repository browser.