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

Last change on this file since 5456 was 5114, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.4 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 sigmax=fSigMax;
87 Double_t bgmin =fBgMin;
88 Double_t bgmax =fBgMax;
89
90 //*fLog << inf << "Fit: " << sigmax << " " << fSigInt << " " << bgmin << " " << bgmax << endl;
91
92 //TF1 fFunc("", Form("gaus(0) + pol%d(3)", fPolynomOrder), 0, 90);
93
94 //fFunc->SetParameters(fCoefficients.GetArray());
95
96 fFunc->FixParameter(1, 0);
97 fFunc->FixParameter(4, 0);
98 fFunc->SetParLimits(2, 0, 90);
99 fFunc->SetParLimits(3, -1, 1);
100
101 const Double_t alpha0 = h.GetBinContent(1);
102 const Double_t alphaw = h.GetXaxis()->GetBinWidth(1);
103
104 // Check for the regios which is not filled...
105 if (alpha0==0)
106 return kFALSE; //*fLog << warn << "Histogram empty." << endl;
107
108 // First fit a polynom in the off region
109 fFunc->FixParameter(0, 0);
110 fFunc->FixParameter(2, 1);
111 fFunc->ReleaseParameter(3);
112
113 for (int i=5; i<fFunc->GetNpar(); i++)
114 fFunc->ReleaseParameter(i);
115
116 // options : N do not store the function, do not draw
117 // I use integral of function in bin rather than value at bin center
118 // R use the range specified in the function range
119 // Q quiet mode
120 h.Fit(fFunc, "NQI", "", bgmin, bgmax);
121
122 fChiSqBg = fFunc->GetChisquare()/fFunc->GetNDF();
123
124 // ------------------------------------
125 if (paint)
126 {
127 fFunc->SetRange(0, 90);
128 fFunc->SetLineColor(kRed);
129 fFunc->SetLineWidth(2);
130 fFunc->Paint("same");
131 }
132 // ------------------------------------
133
134 fFunc->ReleaseParameter(0);
135 //func.ReleaseParameter(1);
136 fFunc->ReleaseParameter(2);
137 fFunc->FixParameter(3, fFunc->GetParameter(3));
138 for (int i=5; i<fFunc->GetNpar(); i++)
139 fFunc->FixParameter(i, fFunc->GetParameter(i));
140
141 // Do not allow signals smaller than the background
142 const Double_t A = alpha0-fFunc->GetParameter(3);
143 const Double_t dA = TMath::Abs(A);
144 fFunc->SetParLimits(0, -dA*4, dA*4);
145 fFunc->SetParLimits(2, 0, 90);
146
147 // Now fit a gaus in the on region on top of the polynom
148 fFunc->SetParameter(0, A);
149 fFunc->SetParameter(2, sigmax*0.75);
150
151 // options : N do not store the function, do not draw
152 // I use integral of function in bin rather than value at bin center
153 // R use the range specified in the function range
154 // Q quiet mode
155 h.Fit(fFunc, "NQI", "", 0, sigmax);
156
157 fChiSqSignal = fFunc->GetChisquare()/fFunc->GetNDF();
158 fCoefficients.Set(fFunc->GetNpar(), fFunc->GetParameters());
159
160 //const Bool_t ok = NDF>0 && chi2<2.5*NDF;
161
162 // ------------------------------------
163 if (paint)
164 {
165 fFunc->SetLineColor(kGreen);
166 fFunc->SetLineWidth(2);
167 fFunc->Paint("same");
168 }
169 // ------------------------------------
170
171 //const Double_t s = fFunc->Integral(0, fSigInt)/alphaw;
172 fFunc->SetParameter(0, 0);
173 fFunc->SetParameter(2, 1);
174 //const Double_t b = fFunc->Integral(0, fSigInt)/alphaw;
175 //fSignificance = MMath::SignificanceLiMaSigned(s, b);
176
177
178 const Int_t bin = h.GetXaxis()->FindFixBin(fSigInt*0.999);
179
180 fIntegralMax = h.GetBinLowEdge(bin+1);
181 fEventsBackground = fFunc->Integral(0, fIntegralMax)/alphaw;
182 fEventsSignal = h.Integral(0, bin);
183 fEventsExcess = fEventsSignal-fEventsBackground;
184 fSignificance = MMath::SignificanceLiMaSigned(fEventsSignal, fEventsBackground);
185
186 if (fEventsExcess<0)
187 fEventsExcess=0;
188
189 return kTRUE;
190}
191
192void MAlphaFitter::PaintResult(Float_t x, Float_t y, Float_t size) const
193{
194 TLatex text(x, y, Form("\\sigma_{Li/Ma}=%.1f \\omega=%.1f\\circ E=%d (\\alpha<%.1f\\circ) (\\chi_{b}^{2}=%.1f \\chi_{s}^{2}=%.1f)",
195 fSignificance, GetGausSigma(),
196 (int)fEventsExcess, fIntegralMax,
197 fChiSqBg, fChiSqSignal));
198
199 text.SetBit(TLatex::kTextNDC);
200 text.SetTextSize(size);
201 text.Paint();
202}
203
204void MAlphaFitter::Copy(TObject &o) const
205{
206 MAlphaFitter &f = static_cast<MAlphaFitter&>(o);
207
208 f.fSigInt = fSigInt;
209 f.fSigMax = fSigMax;
210 f.fBgMin = fBgMin;
211 f.fBgMax = fBgMax;
212 f.fPolynomOrder = fPolynomOrder;
213 f.fCoefficients.Set(fCoefficients.GetSize());
214 f.fCoefficients.Reset();
215
216 TF1 *fcn = f.fFunc;
217 f.fFunc = new TF1(*fFunc);
218 gROOT->GetListOfFunctions()->Remove(f.fFunc);
219 f.fFunc->SetName("Dummy");
220 delete fcn;
221}
222
223void MAlphaFitter::Print(Option_t *o) const
224{
225 *fLog << inf << GetDescriptor() << ": Fitting..." << endl;
226 *fLog << " ...background from " << fBgMin << " to " << fBgMax << endl;
227 *fLog << " ...signal to " << fSigMax << " (integrate into bin at " << fSigInt << ")" << endl;
228 *fLog << " ...polynom order " << fPolynomOrder << endl;
229}
Note: See TracBrowser for help on using the repository browser.