source: trunk/MagicSoft/Mars/mhflux/MAlphaFitter.cc@ 5098

Last change on this file since 5098 was 5012, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.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): Thomas Bretz, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MAlphaFitter
28//
29// Create a single Alpha-Plot. The alpha-plot is fitted online. You can
30// check the result when it is filles in the MStatusDisplay
31// For more information see MHFalseSource::FitSignificance
32//
33// For convinience (fit) the output significance is stored in a
34// container in the parlisrt
35//
36// PRELIMINARY!
37//
38//////////////////////////////////////////////////////////////////////////////
39#include "MAlphaFitter.h"
40
41#include <TF1.h>
42#include <TH1.h>
43
44#include <TLatex.h>
45
46#include "MMath.h"
47
48#include "MLogManip.h"
49
50ClassImp(MAlphaFitter);
51
52using namespace std;
53
54// --------------------------------------------------------------------------
55//
56// This is a preliminary implementation of a alpha-fit procedure for
57// all possible source positions. It will be moved into its own
58// more powerfull class soon.
59//
60// The fit function is "gaus(0)+pol2(3)" which is equivalent to:
61// [0]*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2
62// or
63// A*exp(-0.5*((x-mu)/sigma)^2) + a + b*x + c*x^2
64//
65// Parameter [1] is fixed to 0 while the alpha peak should be
66// symmetric around alpha=0.
67//
68// Parameter [4] is fixed to 0 because the first derivative at
69// alpha=0 should be 0, too.
70//
71// In a first step the background is fitted between bgmin and bgmax,
72// while the parameters [0]=0 and [2]=1 are fixed.
73//
74// In a second step the signal region (alpha<sigmax) is fittet using
75// the whole function with parameters [1], [3], [4] and [5] fixed.
76//
77// The number of excess and background events are calculated as
78// s = int(hist, 0, 1.25*sigint)
79// b = int(pol2(3), 0, 1.25*sigint)
80//
81// The Significance is calculated using the Significance() member
82// function.
83//
84Bool_t MAlphaFitter::Fit(TH1D &h, Bool_t paint)
85{
86 Double_t sigint=fSigInt;
87 Double_t sigmax=fSigMax;
88 Double_t bgmin =fBgMin;
89 Double_t bgmax =fBgMax;
90
91 //TF1 fFunc("", Form("gaus(0) + pol%d(3)", fPolynomOrder), 0, 90);
92
93 //fFunc->SetParameters(fCoefficients.GetArray());
94
95 fFunc->FixParameter(1, 0);
96 fFunc->FixParameter(4, 0);
97 fFunc->SetParLimits(2, 0, 90);
98 fFunc->SetParLimits(3, -1, 1);
99
100 const Double_t alpha0 = h.GetBinContent(1);
101 const Double_t alphaw = h.GetXaxis()->GetBinWidth(1);
102
103 // Check for the regios which is not filled...
104 if (alpha0==0)
105 return kFALSE; //*fLog << warn << "Histogram empty." << endl;
106
107 // First fit a polynom in the off region
108 fFunc->FixParameter(0, 0);
109 fFunc->FixParameter(2, 1);
110 fFunc->ReleaseParameter(3);
111
112 for (int i=5; i<fFunc->GetNpar(); i++)
113 fFunc->ReleaseParameter(i);
114
115 // options : N do not store the function, do not draw
116 // I use integral of function in bin rather than value at bin center
117 // R use the range specified in the function range
118 // Q quiet mode
119 h.Fit(fFunc, "NQI", "", bgmin, bgmax);
120
121 fChiSqBg = fFunc->GetChisquare()/fFunc->GetNDF();
122
123 // ------------------------------------
124 if (paint)
125 {
126 fFunc->SetRange(0, 90);
127 fFunc->SetLineColor(kRed);
128 fFunc->SetLineWidth(2);
129 fFunc->Paint("same");
130 }
131 // ------------------------------------
132
133 fFunc->ReleaseParameter(0);
134 //func.ReleaseParameter(1);
135 fFunc->ReleaseParameter(2);
136 fFunc->FixParameter(3, fFunc->GetParameter(3));
137 for (int i=5; i<fFunc->GetNpar(); i++)
138 fFunc->FixParameter(i, fFunc->GetParameter(i));
139
140 // Do not allow signals smaller than the background
141 const Double_t A = alpha0-fFunc->GetParameter(3);
142 const Double_t dA = TMath::Abs(A);
143 fFunc->SetParLimits(0, -dA*4, dA*4);
144 fFunc->SetParLimits(2, 0, 90);
145
146 // Now fit a gaus in the on region on top of the polynom
147 fFunc->SetParameter(0, A);
148 fFunc->SetParameter(2, sigmax*0.75);
149
150 // options : N do not store the function, do not draw
151 // I use integral of function in bin rather than value at bin center
152 // R use the range specified in the function range
153 // Q quiet mode
154 h.Fit(fFunc, "NQI", "", 0, sigmax);
155
156 fChiSqSignal = fFunc->GetChisquare()/fFunc->GetNDF();
157 fCoefficients.Set(fFunc->GetNpar(), fFunc->GetParameters());
158
159 //const Bool_t ok = NDF>0 && chi2<2.5*NDF;
160
161 // ------------------------------------
162 if (paint)
163 {
164 fFunc->SetLineColor(kGreen);
165 fFunc->SetLineWidth(2);
166 fFunc->Paint("same");
167 }
168 // ------------------------------------
169 const Double_t s = fFunc->Integral(0, sigint)/alphaw;
170 fFunc->SetParameter(0, 0);
171 fFunc->SetParameter(2, 1);
172 const Double_t b = fFunc->Integral(0, sigint)/alphaw;
173 fSignificance = MMath::SignificanceLiMaSigned(s, b);
174
175 const Double_t uin = 1.25*sigint;
176 const Int_t bin = h.GetXaxis()->FindFixBin(uin);
177
178 fIntegralMax = h.GetBinLowEdge(bin+1);
179 fEventsBackground = fFunc->Integral(0, fIntegralMax)/alphaw;
180 fEventsSignal = h.Integral(0, bin);
181 fEventsExcess = fEventsSignal-fEventsBackground;
182 //fSignificance = MMath::SignificanceLiMaSigned(fEventsSignal, fEventsBackground);
183
184 return kTRUE;
185}
186
187void MAlphaFitter::PaintResult(Float_t x, Float_t y, Float_t size) const
188{
189 TLatex text(x, y, Form("\\sigma_{Li/Ma}=%.1f (\\alpha<%.1f\\circ) \\omega=%.1f\\circ E=%d (\\alpha<%.1f\\circ) (\\chi_{b}^{2}=%.1f \\chi_{s}^{2}=%.1f)",
190 fSignificance, fSigInt, GetGausSigma(),
191 (int)fEventsExcess, fIntegralMax,
192 fChiSqBg, fChiSqSignal));
193
194 text.SetBit(TLatex::kTextNDC);
195 text.SetTextSize(size);
196 text.Paint();
197}
Note: See TracBrowser for help on using the repository browser.