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

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