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): Wolfgang Wittek 10/2003 <mailto:wittek@mppmu.mpg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2003
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // M2dimFunction
|
---|
28 | //
|
---|
29 | // Storage Container for the parameters of the 2-dim function describing
|
---|
30 | // the shower image
|
---|
31 | //
|
---|
32 | //
|
---|
33 | /////////////////////////////////////////////////////////////////////////////
|
---|
34 | #include "M2dimFunction.h"
|
---|
35 |
|
---|
36 | #include <fstream>>
|
---|
37 |
|
---|
38 | #include "TMath.h"
|
---|
39 | #include "TVectorD.h"
|
---|
40 | #include "TMatrixD.h"
|
---|
41 |
|
---|
42 | #include "MLog.h"
|
---|
43 | #include "MLogManip.h"
|
---|
44 |
|
---|
45 | #include "MHillas.h"
|
---|
46 |
|
---|
47 | #include "MGeomCam.h"
|
---|
48 | #include "MGeomPix.h"
|
---|
49 |
|
---|
50 | #include "MCerPhotEvt.h"
|
---|
51 | #include "MCerPhotPix.h"
|
---|
52 |
|
---|
53 | ClassImp(M2dimFunction);
|
---|
54 |
|
---|
55 | using namespace std;
|
---|
56 |
|
---|
57 | // --------------------------------------------------------------------------
|
---|
58 | //
|
---|
59 | // TwodimFunction
|
---|
60 | //
|
---|
61 | // this function calculates for a given set of parameters
|
---|
62 | //
|
---|
63 | // - the log likelihood function L to be minimized
|
---|
64 | // - beta_k = -1/2 * dL/da_k (a kind of gradient of L)
|
---|
65 | // - alfa_kl = 1/2 * dL/(da_k da_l) (a kind of second derivative of L)
|
---|
66 | //
|
---|
67 | // (see Numerical recipes (2nd ed.), W.H.Press et al., p. 687)
|
---|
68 | //
|
---|
69 | // Note : this is not a member function of M2dimFunction;
|
---|
70 | // it will be called by the minimization class MMarquardt;
|
---|
71 | // the address of this function is passed to MMarquardt
|
---|
72 | // in M2dimFunction::Fit() by the call MMarquardt::Loop()
|
---|
73 | //
|
---|
74 |
|
---|
75 | Bool_t TwodimFunction(TVectord &a, TMatrixD &alfa, TVectord &beta, Double_t &L)
|
---|
76 | {
|
---|
77 | Int_t npar = a.GetNrows();
|
---|
78 | Int_t npixels =;
|
---|
79 |
|
---|
80 | TMatrixD dmuda (npar, npixels);
|
---|
81 | TVector dLdmu (npixels);
|
---|
82 | TVector d2Ldmu2(npixels);
|
---|
83 |
|
---|
84 | //------------------------------------------
|
---|
85 | // these are the parameters for which alfa, beta and L are calculated
|
---|
86 |
|
---|
87 | Double_t xbar = a(0);
|
---|
88 | Double_t ybar = a(1);
|
---|
89 | Double_t delta = a(2);
|
---|
90 | Double_t amp = a(3);
|
---|
91 | Double_t leng = a(4);
|
---|
92 | Double_t wid = a(5);
|
---|
93 | Double_t asy = a(6);
|
---|
94 |
|
---|
95 |
|
---|
96 | for (Int_t m=0; m<npar; m++)
|
---|
97 | {
|
---|
98 | beta(m) = 0.0;
|
---|
99 | for (Int_t n=0; n<=m; n++)
|
---|
100 | {
|
---|
101 | alfa(m,n) = 0.0;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | //------------------------------------------
|
---|
106 | // loop over the pixels
|
---|
107 | //
|
---|
108 | // quantities for each pixel :
|
---|
109 | //
|
---|
110 | // ar pixel area
|
---|
111 | // b quantum efficiency * geometrical acceptance of 1st dynode
|
---|
112 | // c = ar * b
|
---|
113 | // q measured number of photo electrons
|
---|
114 | // (after subtraction of the pedestal)
|
---|
115 | // S 'measured' number of photo electrons (including the NSB)
|
---|
116 | // mu fitted number of Cherenkov photons per area
|
---|
117 | // lambda average number of NSB photons per area
|
---|
118 | // (obtained from the pedestal fluctuations)
|
---|
119 | // sigma_el sigma of electronic noise
|
---|
120 | // F the probability of measuring S
|
---|
121 |
|
---|
122 |
|
---|
123 | L = 0.0;
|
---|
124 | Double_t Fexcessnoise2 = 1.2 * 1.2;
|
---|
125 | for (Int_t i=0; i<npixels; i++)
|
---|
126 | {
|
---|
127 | Double_t lambda =
|
---|
128 | Double_t sigma_el =
|
---|
129 |
|
---|
130 | Double_t S =
|
---|
131 | Double_t mu = 2-dim function evaluated for pixel i, at the position (x,y);
|
---|
132 |
|
---|
133 | Double_t F = 0.0;
|
---|
134 | Double_t dFdmu = 0.0;
|
---|
135 | Double_t dFdmu2 = 0.0;
|
---|
136 |
|
---|
137 | // range of n for which Poisson and Gaus are not too small
|
---|
138 | Int_t nmin =
|
---|
139 | Int_t nmax =
|
---|
140 |
|
---|
141 | for (Int_t n=nmin; n<=nmax; n++)
|
---|
142 | {
|
---|
143 | Double_t sigma_n = sqrt( n*(Fecessnoise2-1.0) + sigma_el*sigma_el );
|
---|
144 | Double_t probn = TMath::Poisson(n, c*(mu+lambda))
|
---|
145 | * TMath::Gaus(S, n, sigma_n, kTRUE);
|
---|
146 | Double_t brack = n/(mu+lambda)-c;
|
---|
147 |
|
---|
148 | F += probn;
|
---|
149 | dFdmu += probn * brack;
|
---|
150 | dFdmu2 += probn * (brack * brack - n/( (mu+lambda)*(mu+lambda) );
|
---|
151 | }
|
---|
152 |
|
---|
153 | // log-likelihood function
|
---|
154 | L -= 2.0 * log(F);
|
---|
155 |
|
---|
156 | // derivatives of log-likelihood function, up to factors of 2)
|
---|
157 | dLdmu(i) = dFdmu / F;
|
---|
158 | d2Ldmu2(i) = dFdmu*dFdmu / (F*F) - dFdmu2 / F;
|
---|
159 |
|
---|
160 |
|
---|
161 | // derivatives of 2-dim function mu
|
---|
162 | // - with respect to xbar, ybar and delta :
|
---|
163 | dmudu =
|
---|
164 | dmudv =
|
---|
165 | dmuda(0,i) = -dmudu * cos(delta) + dmudv * sin(delta);
|
---|
166 | dmuda(1,i) = -dmudu * sin(delta) - dmudv * cos(delta);
|
---|
167 | dmuda(2,i) = dmudu * ( -(x-xbar)*sin(delta) +(y-ybar)*cos(delta) )
|
---|
168 | + dmudv * ( -(x-xbar)*cos(delta) -(y-ybar)*sin(delta) );
|
---|
169 |
|
---|
170 | // - with respect to the other variables :
|
---|
171 | dmuda(3,i) =
|
---|
172 | dmuda(4,i) =
|
---|
173 | dmuda(5,i) =
|
---|
174 | dmuda(6,i) =
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | //------------------------------------------
|
---|
179 | // calculate alfa and beta
|
---|
180 | //
|
---|
181 | // sum over all pixels
|
---|
182 | for (Int_t i=0; i<npixels; i++)
|
---|
183 | {
|
---|
184 | for (Int_t m=0; m<npar; m++)
|
---|
185 | {
|
---|
186 | Double_t g = dmuda(m,i);
|
---|
187 | for (Int_t n=0; n<=m; n++)
|
---|
188 | {
|
---|
189 | alfa(m,n) += d2Ldmu2(i) * g * dmuda(n,i);
|
---|
190 | }
|
---|
191 | beta(m) += dLdmu(i) * g;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | return kTRUE;
|
---|
196 | }
|
---|
197 |
|
---|
198 | // --------------------------------------------------------------------------
|
---|
199 | //
|
---|
200 | // Default constructor.
|
---|
201 | //
|
---|
202 | M2dimFunction::M2dimFunction(const char *name, const char *title)
|
---|
203 | {
|
---|
204 | fName = name ? name : "M2dimFunction";
|
---|
205 | fTitle = title ? title : "Parameters of 2-dim function";
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | // --------------------------------------------------------------------------
|
---|
210 | //
|
---|
211 | // SetVinit
|
---|
212 | //
|
---|
213 | // set the initial values of the parameters
|
---|
214 | //
|
---|
215 | void M2dimFunction::SetVinit(MHillas *fhillas)
|
---|
216 | {
|
---|
217 | // get some initial values from the Hillas class
|
---|
218 |
|
---|
219 | if (fhillas)
|
---|
220 | {
|
---|
221 | fVinit(0) = fhillas->GetMeanX();
|
---|
222 | fVinit(1) = fhillas->GetMeanY();
|
---|
223 | fVinit(2) = fhillas->GetDelta();
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | fVinit(0) = ;
|
---|
228 | fVinit(1) = ;
|
---|
229 | fVinit(2) = ;
|
---|
230 | }
|
---|
231 |
|
---|
232 | fVinit(3) =
|
---|
233 | fVinit(4) =
|
---|
234 | fVinit(5) =
|
---|
235 | fVinit(6) =
|
---|
236 |
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 |
|
---|
242 | // --------------------------------------------------------------------------
|
---|
243 | //
|
---|
244 | // Fit of the 2-dim function to the shower image
|
---|
245 | //
|
---|
246 | void M2dimFunction::Fit()
|
---|
247 | {
|
---|
248 | fMarquardt.Loop(TwodimFunction, fVinit);
|
---|
249 |
|
---|
250 | SetReadyToSave();
|
---|
251 | }
|
---|
252 |
|
---|
253 | // --------------------------------------------------------------------------
|
---|
254 | //
|
---|
255 | void M2dimFunction::Print(Option_t *) const
|
---|
256 | {
|
---|
257 | *fLog << all;
|
---|
258 | *fLog << "Parameters of 2-dim function (" << GetName() << ")" << endl;
|
---|
259 | *fLog << " - fXbar = " << fXbar << endl;
|
---|
260 | *fLog << " - fYbar = " << fYbar << endl;
|
---|
261 | *fLog << " - fAmp = " << fAmp << endl;
|
---|
262 | *fLog << " - fMajor = " << fMajor << endl;
|
---|
263 | *fLog << " - fMinor = " << fMinor << endl;
|
---|
264 | *fLog << " - fAsym = " << fAsym << endl;
|
---|
265 | }
|
---|
266 | //============================================================================
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 |
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|