source: trunk/MagicSoft/Mars/mhflux/MAlphaFitter.h@ 9347

Last change on this file since 9347 was 9302, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 7.9 KB
Line 
1#ifndef MARS_MAlphaFitter
2#define MARS_MAlphaFitter
3
4#ifndef MARS_MParContainer
5#include "MParContainer.h"
6#endif
7
8#ifndef ROOT_TArrayD
9#include <TArrayD.h>
10#endif
11
12class TF1;
13class TH1D;
14class TH3D;
15
16class MAlphaFitter : public MParContainer
17{
18public:
19 enum ScaleMode_t {
20 kNone, // No scaling
21 kEntries, // scale by the number of entries in on and off
22 kIntegral, // scale by the integral in on and off
23 kOffRegion, // scale by the integral between fScaleMin, fScaleMax in on and off
24 kBackground, // scale by the integral between fBgMin, fBgMax in on and off
25 kLeastSquare, // not yet implemented
26 kUserScale // scale by fixed factor set by SetScaleUser
27 };
28 enum Strategy_t {
29 kSignificance,
30 kSignificanceChi2,
31 kSignificanceLogExcess,
32 kSignificanceExcess,
33 kExcess,
34 kGaussSigma,
35 kWeakSource,
36 kWeakSourceLogExcess
37 };
38 enum SignalFunc_t {
39 kGauss, kThetaSq
40 };
41
42private:
43 // Fitting Setup
44 Float_t fSigInt; // minimum of range to fit the signal
45 Float_t fSigMax; // maximum of range to fit the signal
46 Float_t fBgMin; // minimum of range to fit the background
47 Float_t fBgMax; // minimum of range to fit the background
48 Float_t fScaleMin; // minimum of range to determin the scale factor of the background
49 Float_t fScaleMax; // maximum of range to determin the scale factor of the background
50 Int_t fPolynomOrder; // order of polyom to be fitted to the background
51 Bool_t fFitBackground; // Backround fit: yes/no
52 SignalFunc_t fSignalFunc; // Type of signal function
53 // Result
54 Double_t fSignificance; // significance of an unknown signal (Li/Ma 17)
55 Double_t fErrorExcess; // Simple error propagation
56 Double_t fEventsExcess; // calculated number of excess events (signal-bg)
57 Double_t fEventsSignal; // calculated number of signal events
58 Double_t fEventsBackground; // calculated number of bg events (fScaleFactor already applied)
59
60 Double_t fChiSqSignal; // Reduced (chi^2/NDF) chisq of signal fit
61 Double_t fChiSqBg; // Reduced (chi^2/NDF) chisq of bg fit
62 Double_t fIntegralMax; // Calculated bin border to which it was integrated
63 Double_t fScaleFactor; // Scale factor determined for off-data
64
65 TArrayD fCoefficients; // Fit result
66 TArrayD fErrors; // Fit errors
67
68 // Function
69 TF1 *fFunc; // fit function (gauss + polynom)
70
71 // Scaling setup
72 ScaleMode_t fScaleMode; // scaling mode
73 Double_t fScaleUser; // user scale factor
74
75 // Minimization strategy
76 Strategy_t fStrategy; // How to calc minimization value
77
78 Double_t DoOffFit(const TH1D &hon, const TH1D &hof, Bool_t paint);
79 Bool_t FitOff(TH1D &h, Int_t paint);
80 void FitResult(const TH1D &h);
81
82public:
83 // Implementing the function yourself is only about 5% faster
84 MAlphaFitter(const char *name=0, const char *title=0);
85 MAlphaFitter(const MAlphaFitter &f) : fFunc(0)
86 {
87 f.Copy(*this);
88 }
89 ~MAlphaFitter();
90
91 // TObject
92 void Clear(Option_t *o="");
93 void Print(Option_t *o="") const; //*MENU*
94 void Copy(TObject &o) const;
95
96 // Setter
97 void SetScaleUser(Float_t scale) { fScaleUser = scale; fScaleMode=kUserScale; }
98 void SetScaleMode(ScaleMode_t mode) { fScaleMode = mode; }
99 void SetMinimizationStrategy(Strategy_t mode) { fStrategy = mode; }
100 void SetSignalIntegralMax(Float_t s) { fSigInt = s; }
101 void SetSignalFitMax(Float_t s) { fSigMax = s; }
102 void SetBackgroundFitMin(Float_t s) { fBgMin = s; }
103 void SetBackgroundFitMax(Float_t s) { fBgMax = s; }
104 void SetScaleMin(Float_t s) { fScaleMin = s; }
105 void SetScaleMax(Float_t s) { fScaleMax = s; }
106 void SetPolynomOrder(Int_t s)
107 {
108 if (s==fPolynomOrder)
109 return;
110
111 fPolynomOrder = s;
112
113 SetSignalFunction(fSignalFunc);
114 }
115 void SetSignalFunction(SignalFunc_t func);
116
117 void EnableBackgroundFit(Bool_t b=kTRUE) { fFitBackground=b; }
118
119 void FixScale()
120 {
121 fScaleMode = kUserScale;
122 fScaleUser = fScaleFactor;
123 }
124
125 // Getter
126 Double_t GetSignalIntegralMax() const { return fSigInt; }
127
128 Double_t GetEventsExcess() const { return fEventsExcess; }
129 Double_t GetEventsSignal() const { return fEventsSignal; }
130 Double_t GetEventsBackground() const { return fEventsBackground; }
131
132 Double_t GetSignificance() const { return fSignificance; }
133 Double_t GetErrorExcess() const { return fErrorExcess; }
134 Double_t GetChiSqSignal() const { return fChiSqSignal; }
135 Double_t GetChiSqBg() const { return fChiSqBg; }
136 Double_t GetScaleFactor() const { return fScaleFactor; }
137 Double_t GetMinimizationValue() const;
138
139 ScaleMode_t GetScaleMode() const { return fScaleMode; }
140
141 Double_t GetGausSigma() const { return fCoefficients[2]; }
142 Double_t GetGausMu() const { return fCoefficients[1]; }
143 Double_t GetGausA() const { return fCoefficients[0]; }
144 Double_t GetCoefficient(Int_t i) const { return fCoefficients[i]; }
145 const TArrayD &GetCoefficients() const { return fCoefficients; }
146 const TArrayD &GetErrors() const { return fErrors; }
147 Double_t Eval(Double_t d) const;
148
149 Double_t CalcUpperLimit() const;
150
151 // Interface to fit
152 Bool_t Fit(TH1D &h, Bool_t paint=kFALSE);
153 Bool_t Fit(const TH1D &on, const TH1D &off, Double_t alpha, Bool_t paint=kFALSE);
154 Bool_t Fit(TH1D &on, TH1D *off, Double_t alpha, Bool_t paint=kFALSE)
155 {
156 return off ? Fit(on, *off, alpha, paint) : Fit(on, paint);
157 }
158 Bool_t Fit(TH1D &on, TH1D *off, Bool_t paint=kFALSE)
159 {
160 return off ? Fit(on, *off, 1, paint) : Fit(on, paint);
161 }
162 Bool_t ScaleAndFit(TH1D &on, TH1D *off, Bool_t paint=kFALSE)
163 {
164 const Double_t alpha = off ? Scale(*off, on) : 1;
165 return off ? Fit(on, *off, alpha, paint) : Fit(on, paint);
166 }
167
168 Bool_t FitAlpha(const TH3D &h, Bool_t paint=kFALSE);
169 Bool_t FitEnergy(const TH3D &h, UInt_t bin, Bool_t paint=kFALSE);
170 Bool_t FitTheta(const TH3D &h, UInt_t bin, Bool_t paint=kFALSE);
171 //Bool_t FitTime(const TH3D &h, UInt_t bin, Bool_t paint=kFALSE);
172
173 Bool_t FitAlpha(const TH3D &on, const TH3D &off, Bool_t paint=kFALSE);
174 Bool_t FitEnergy(const TH3D &on, const TH3D &off, UInt_t bin, Bool_t paint=kFALSE);
175 Bool_t FitTheta(const TH3D &on, const TH3D &off, UInt_t bin, Bool_t paint=kFALSE);
176 //Bool_t FitTime(const TH3D &on, const TH3D &off, UInt_t bin, Bool_t paint=kFALSE);
177
178 Bool_t FitAlpha(const TH3D &on, const TH3D *off, Bool_t paint=kFALSE)
179 {
180 return off ? FitAlpha(on, *off, paint) : FitAlpha(on, paint);
181 }
182 Bool_t FitEnergy(const TH3D &on, const TH3D *off, UInt_t bin, Bool_t paint=kFALSE)
183 {
184 return off ? FitEnergy(on, *off, bin, paint) : FitEnergy(on, bin, paint);
185 }
186 Bool_t FitTheta(const TH3D &on, const TH3D *off, UInt_t bin, Bool_t paint=kFALSE)
187 {
188 return off ? FitTheta(on, *off, bin, paint) : FitTheta(on, bin, paint);
189 }/*
190 Bool_t FitTime(const TH3D &on, const TH3D *off, UInt_t bin, Bool_t paint=kFALSE)
191 {
192 return off ? FitTime(on, *off, bin, paint) : FitTime(on, bin, paint);
193 }*/
194
195 Double_t Scale(TH1D &off, const TH1D &on) const;
196
197 Bool_t ApplyScaling(const TH3D &hon, TH3D &hof, UInt_t bin) const;
198 Bool_t ApplyScaling(const TH3D &hon, TH3D &hof) const;
199
200 // Interface to result
201 void PaintResult(Float_t x=0.04, Float_t y=0.94, Float_t size=0.035, Bool_t draw=kFALSE) const;
202 void DrawResult(Float_t x=0.04, Float_t y=0.94, Float_t size=0.035) const { PaintResult(x, y, size, kTRUE); }
203
204 // MTask
205 Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print=kFALSE);
206
207 ClassDef(MAlphaFitter, 4)
208};
209
210#endif
Note: See TracBrowser for help on using the repository browser.