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

Last change on this file since 9525 was 9525, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 29.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-2008
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// Version 2:
37// ----------
38// + Double_t fSignificanceExc; // significance of a known excess
39//
40// Version 3:
41// ----------
42// + TArrayD fErrors; // errors of coefficients
43//
44// Version 4:
45// ----------
46// + Double_t fErrorExcess;
47// - Double_t fSignificanceExc;
48//
49//
50//////////////////////////////////////////////////////////////////////////////
51#include "MAlphaFitter.h"
52
53#include <TF1.h>
54#include <TH1.h>
55#include <TH3.h>
56
57#include <TRandom.h>
58#include <TFeldmanCousins.h>
59
60#include <TLine.h>
61#include <TLatex.h>
62#include <TVirtualPad.h>
63
64#include "MMath.h"
65#include "MString.h"
66
67#include "MLogManip.h"
68
69ClassImp(MAlphaFitter);
70
71using namespace std;
72
73// --------------------------------------------------------------------------
74//
75// Default constructor.
76//
77MAlphaFitter::MAlphaFitter(const char *name, const char *title) : fSigInt(15),
78 fSigMax(75), fBgMin(45), fBgMax(85), fScaleMin(40), fScaleMax(80),
79 fPolynomOrder(2), fFitBackground(kTRUE), fFunc(0),
80 fScaleMode(kOffRegion), fScaleUser(1), fStrategy(kSignificance)
81{
82 fName = name ? name : "MAlphaFitter";
83 fTitle = title ? title : "Fit alpha";
84
85 SetSignalFunction(kGauss);
86
87 Clear();
88}
89
90// --------------------------------------------------------------------------
91//
92// Destructor
93//
94MAlphaFitter::~MAlphaFitter()
95{
96 delete fFunc;
97}
98
99// --------------------------------------------------------------------------
100//
101// Re-initializes fFunc either according to SignalFunc_t
102//
103void MAlphaFitter::SetSignalFunction(SignalFunc_t func)
104{
105 if (gROOT->GetListOfFunctions()->FindObject(""))
106 {
107 gLog << err << "MAlphaFitter::SetSignalFunction -- '' found!" << endl;
108 return;
109 }
110
111 delete fFunc;
112 fFunc = 0;
113
114 switch (func)
115 {
116 case kGauss:
117 fFunc=new TF1("", MString::Format("gaus(0) + pol%d(3)", fPolynomOrder).Data());
118 break;
119 case kThetaSq:
120 if (fPolynomOrder>0)
121 fPolynomOrder = 1;
122 fFunc=new TF1("", "[0]*exp(-0.5*((sqrt(x)-[1])/[2])^2) + expo(3)");
123 break;
124 }
125
126 fSignalFunc=func;
127
128 fFunc->SetName("Dummy");
129 gROOT->GetListOfFunctions()->Remove(fFunc);
130
131 fCoefficients.Set(3+fPolynomOrder+1);
132 fCoefficients.Reset();
133
134 fErrors.Set(3+fPolynomOrder+1);
135 fErrors.Reset();
136}
137
138// --------------------------------------------------------------------------
139//
140// Reset variables which belong to results. Reset the arrays.
141//
142void MAlphaFitter::Clear(Option_t *o)
143{
144 fSignificance=0;
145 fErrorExcess=0;
146 fEventsExcess=0;
147 fEventsSignal=0;
148 fEventsBackground=0;
149
150 fChiSqSignal=0;
151 fChiSqBg=0;
152 fIntegralMax=0;
153 fScaleFactor=1;
154
155 fCoefficients.Reset();
156 fErrors.Reset();
157}
158
159// --------------------------------------------------------------------------
160//
161// Returns fFunc->Eval(d) or 0 if fFunc==NULL
162//
163Double_t MAlphaFitter::Eval(Double_t d) const
164{
165 return fFunc ? fFunc->Eval(d) : 0;
166}
167
168// --------------------------------------------------------------------------
169//
170// This function implementes the fit to the off-data as used in Fit()
171//
172Bool_t MAlphaFitter::FitOff(TH1D &h, Int_t paint)
173{
174 if (h.GetEntries()==0)
175 return kFALSE;
176
177 // First fit a polynom in the off region
178 fFunc->FixParameter(0, 0);
179 fFunc->FixParameter(1, 0);
180 fFunc->FixParameter(2, 1);
181 fFunc->ReleaseParameter(3);
182 if (fPolynomOrder!=1)
183 fFunc->FixParameter(4, 0);
184
185 for (int i=5; i<fFunc->GetNpar(); i++)
186 if (fFitBackground)
187 fFunc->ReleaseParameter(i);
188 else
189 fFunc->SetParameter(i, 0);
190
191 if (!fFitBackground)
192 return kTRUE;
193
194 if (fSignalFunc==kThetaSq)
195 {
196 const Double_t sum = h.Integral(1, 3)/3;
197 const Double_t a = sum<=1 ? 0 : TMath::Log(sum);
198 const Double_t b = -1.7;
199
200 // Do a best-guess
201 fFunc->SetParameter(3, a);
202 fFunc->SetParameter(4, b);
203 }
204
205 // options : N do not store the function, do not draw
206 // I use integral of function in bin rather than value at bin center
207 // R use the range specified in the function range
208 // Q quiet mode
209 // E Perform better Errors estimation using Minos technique
210 h.Fit(fFunc, "NQI", "", fBgMin, fBgMax);
211 fChiSqBg = fFunc->GetChisquare()/fFunc->GetNDF();
212
213 fCoefficients.Set(fFunc->GetNpar(), fFunc->GetParameters());
214 fErrors.Set(fFunc->GetNpar());
215 for (int i=3; i<fFunc->GetNpar(); i++)
216 fErrors[i] = fFunc->GetParError(i);
217
218 // ------------------------------------
219
220 if (paint)
221 {
222 if (paint==2)
223 {
224 fFunc->SetLineColor(kBlack);
225 fFunc->SetLineWidth(1);
226 }
227 else
228 {
229 fFunc->SetRange(0, 90);
230 fFunc->SetLineColor(kRed);
231 fFunc->SetLineWidth(2);
232 }
233 fFunc->Paint("same");
234 }
235
236 return kTRUE;
237}
238
239// --------------------------------------------------------------------------
240//
241// Calculate the result of the fit and set the corresponding data members
242//
243void MAlphaFitter::FitResult(const TH1D &h)
244{
245 const Double_t alphaw = h.GetXaxis()->GetBinWidth(1);
246
247 const Int_t bin = h.GetXaxis()->FindFixBin(fSigInt*0.999);
248
249 fIntegralMax = h.GetBinLowEdge(bin+1);
250 fEventsBackground = fFunc->Integral(0, fIntegralMax)/alphaw;
251 fEventsSignal = h.Integral(1, bin);
252 fEventsExcess = fEventsSignal-fEventsBackground;
253 fSignificance = MMath::SignificanceLiMaSigned(fEventsSignal, fEventsBackground);
254 fErrorExcess = MMath::ErrorExc(fEventsSignal, fEventsBackground);
255
256 // !Finitite includes IsNaN
257 if (!TMath::Finite(fSignificance))
258 fSignificance=0;
259}
260
261// --------------------------------------------------------------------------
262//
263// This is a preliminary implementation of a alpha-fit procedure for
264// all possible source positions. It will be moved into its own
265// more powerfull class soon.
266//
267// The fit function is "gaus(0)+pol2(3)" which is equivalent to:
268// [0]*exp(-0.5*((x-[1])/[2])^2) + [3] + [4]*x + [5]*x^2
269// or
270// A*exp(-0.5*((x-mu)/sigma)^2) + a + b*x + c*x^2
271//
272// Parameter [1] is fixed to 0 while the alpha peak should be
273// symmetric around alpha=0.
274//
275// Parameter [4] is fixed to 0 because the first derivative at
276// alpha=0 should be 0, too.
277//
278// In a first step the background is fitted between bgmin and bgmax,
279// while the parameters [0]=0 and [2]=1 are fixed.
280//
281// In a second step the signal region (alpha<sigmax) is fittet using
282// the whole function with parameters [1], [3], [4] and [5] fixed.
283//
284// The number of excess and background events are calculated as
285// s = int(hist, 0, 1.25*sigint)
286// b = int(pol2(3), 0, 1.25*sigint)
287//
288// The Significance is calculated using the Significance() member
289// function.
290//
291Bool_t MAlphaFitter::Fit(TH1D &h, Bool_t paint)
292{
293 Clear();
294
295 // Check for the region which is not filled...
296 // if (alpha0==0)
297 // return kFALSE;
298
299 // Perform fit to the off-data
300 if (!FitOff(h, paint))
301 return kFALSE;
302
303 fFunc->ReleaseParameter(0); // It is also released by SetParLimits later on
304 //func.ReleaseParameter(1); // It is also released by SetParLimits later on
305 fFunc->ReleaseParameter(2);
306 for (int i=3; i<fFunc->GetNpar(); i++)
307 fFunc->FixParameter(i, fFunc->GetParameter(i));
308
309
310 // Do not allow signals smaller than the background
311 const Double_t alpha0 = h.GetBinContent(1);
312 const Double_t s = fSignalFunc==kGauss ? fFunc->GetParameter(3) : TMath::Exp(fFunc->GetParameter(3));
313 const Double_t A = alpha0-s;
314 //const Double_t dA = TMath::Abs(A);
315 //fFunc->SetParLimits(0, -dA*4, dA*4); // SetParLimits also releases the parameter
316 fFunc->SetParLimits(2, 0, 90); // SetParLimits also releases the parameter
317
318 // Now fit a gaus in the on region on top of the polynom
319 fFunc->SetParameter(0, A);
320 fFunc->SetParameter(2, fSigMax*0.75);
321
322 // options : N do not store the function, do not draw
323 // I use integral of function in bin rather than value at bin center
324 // R use the range specified in the function range
325 // Q quiet mode
326 // E Perform better Errors estimation using Minos technique
327 h.Fit(fFunc, "NQI", "", 0, fSigMax);
328
329 fChiSqSignal = fFunc->GetChisquare()/fFunc->GetNDF();
330 fCoefficients.Set(fFunc->GetNpar(), fFunc->GetParameters());
331 for (int i=0; i<3; i++)
332 fErrors[i] = fFunc->GetParError(i);
333 //const Bool_t ok = NDF>0 && chi2<2.5*NDF;
334
335 // ------------------------------------
336 if (paint)
337 {
338 fFunc->SetLineColor(kGreen);
339 fFunc->SetLineWidth(2);
340 fFunc->Paint("same");
341 }
342 // ------------------------------------
343
344 //const Double_t s = fFunc->Integral(0, fSigInt)/alphaw;
345 fFunc->SetParameter(0, 0);
346 fFunc->SetParameter(2, 1);
347 //const Double_t b = fFunc->Integral(0, fSigInt)/alphaw;
348 //fSignificance = MMath::SignificanceLiMaSigned(s, b);
349
350 // Calculate the fit result and set the corresponding data members
351 FitResult(h);
352
353 return kTRUE;
354}
355
356Double_t MAlphaFitter::DoOffFit(const TH1D &hon, const TH1D &hof, Bool_t paint)
357{
358 if (fSignalFunc!=kThetaSq)
359 return 0;
360
361 // ----------------------------------------------------------------------------
362
363 const Int_t bin = hon.GetXaxis()->FindFixBin(fSigInt*0.999);
364
365 MAlphaFitter fit(*this);
366 fit.EnableBackgroundFit();
367 fit.SetBackgroundFitMin(0);
368
369 // produce a histogram containing the off-samples from on-source and
370 // off-source in the off-source region and the on-data in the source-region
371 TH1D h(hof);
372 h.SetDirectory(0);
373 h.Add(&hon);
374
375 h.Scale(0.5);
376 for (int i=1; i<=bin+3; i++)
377 {
378 h.SetBinContent(i, hof.GetBinContent(i));
379 h.SetBinError( i, hof.GetBinError(i));
380 }
381
382 // Now fit the off-data
383 if (!fit.FitOff(h, paint?2:0)) // FIXME: Show fit!
384 return -1;
385
386 // Calculate fit-result
387 fit.FitResult(h);
388
389 // Do a gaussian error propagation to calculated the error of
390 // the background estimated from the fit
391 const Double_t ea = fit.fErrors[3];
392 const Double_t eb = fit.fErrors[4];
393 const Double_t a = fit.fCoefficients[3];
394 const Double_t b = fit.fCoefficients[4];
395
396 const Double_t t = fIntegralMax;
397
398 const Double_t ex = TMath::Exp(t*b);
399 const Double_t eab = TMath::Exp(a)/b;
400
401 const Double_t eA = ex-1;
402 const Double_t eB = t*ex - eA/b;
403
404 const Double_t w = h.GetXaxis()->GetBinWidth(1);
405
406 // Error of estimated background
407 const Double_t er = TMath::Abs(eab)*TMath::Hypot(eA*ea, eB*eb)/w;
408
409 // Calculate arbitrary scale factor from propagated error from the
410 // condition: sqrt(alpha*background) = est.background/est.error
411 // const Double_t bg = hof.Integral(1, bin);
412 // const Double_t sc = bg * er*er / (fit2.GetEventsBackground()*fit2.GetEventsBackground());
413 // Assuming that bg and fit2.GetEventsBackground() are rather identical:
414 const Double_t sc = er*er / fit.fEventsBackground;
415
416
417 /*
418 cout << MMath::SignificanceLiMaSigned(hon.Integral(1, bin), fit.GetEventsBackground()/sc, sc) << " ";
419 cout << sc << " " << fit.GetEventsBackground() << " ";
420 cout << fit.fChiSqBg << endl;
421 */
422 return sc;
423}
424
425Bool_t MAlphaFitter::Fit(const TH1D &hon, const TH1D &hof, Double_t alpha, Bool_t paint)
426{
427 TH1D h(hon);
428 h.SetDirectory(0);
429 h.Add(&hof, -1); // substracts also number of entries!
430 h.SetEntries(hon.GetEntries());
431
432 MAlphaFitter fit(*this);
433 fit.SetPolynomOrder(0);
434 if (alpha<=0 || !fit.Fit(h, paint))
435 return kFALSE;
436
437 fChiSqSignal = fit.fChiSqSignal;
438 fChiSqBg = fit.fChiSqBg;
439 fCoefficients = fit.fCoefficients;
440 fErrors = fit.fErrors;
441
442 // ----------------------------------------------------------------------------
443
444 const Double_t scale = DoOffFit(hon, hof, paint);
445 if (scale<0)
446 return kFALSE;
447
448 // ----------------------------------------------------------------------------
449
450 const Int_t bin = hon.GetXaxis()->FindFixBin(fSigInt*0.999);
451
452 fIntegralMax = hon.GetBinLowEdge(bin+1);
453 fEventsBackground = hof.Integral(1, bin);
454 fEventsSignal = hon.Integral(1, bin);
455 fEventsExcess = fEventsSignal-fEventsBackground;
456 fScaleFactor = alpha;
457 fSignificance = MMath::SignificanceLiMaSigned(fEventsSignal, fEventsBackground/alpha, alpha);
458 fErrorExcess = MMath::ErrorExc(fEventsSignal, fEventsBackground/alpha, alpha);
459
460 // !Finitite includes IsNaN
461 if (!TMath::Finite(fSignificance))
462 fSignificance=0;
463
464 return kTRUE;
465}
466
467// --------------------------------------------------------------------------
468//
469// Calculate the upper limit for fEventsSignal number of observed events
470// and fEventsBackground number of background events.
471//
472// Therefor TFeldmanCousin is used.
473//
474// The Feldman-Cousins method as described in PRD V57 #7, p3873-3889
475//
476Double_t MAlphaFitter::CalcUpperLimit() const
477{
478 // get a FeldmanCousins calculation object with the default limits
479 // of calculating a 90% CL with the minimum signal value scanned
480 // = 0.0 and the maximum signal value scanned of 50.0
481 TFeldmanCousins f;
482 f.SetMuStep(0.05);
483 f.SetMuMax(100);
484 f.SetMuMin(0);
485 f.SetCL(90);
486
487 return f.CalculateUpperLimit(fEventsSignal, fEventsBackground);
488}
489
490void MAlphaFitter::PaintResult(Float_t x, Float_t y, Float_t size, Bool_t draw) const
491{
492 const Double_t w = GetGausSigma();
493 const Double_t m = fIntegralMax;
494
495 const Int_t l1 = w<=0 ? 0 : (Int_t)TMath::Ceil(-TMath::Log10(w));
496 const Int_t l2 = m<=0 ? 0 : (Int_t)TMath::Ceil(-TMath::Log10(m));
497 const TString fmt = MString::Format("\\sigma_{L/M}=%%.1f \\omega=%%.%df\\circ E=%%d B=%%d x<%%.%df \\tilde\\chi_{b}=%%.1f \\tilde\\chi_{s}=%%.1f c=%%.1f f=%%.2f",
498 l1<1?1:l1+1, l2<1?1:l2+1);
499 const TString txt = MString::Format(fmt.Data(), fSignificance, w, (int)fEventsExcess,
500 (int)fEventsBackground, m, fChiSqBg, fChiSqSignal,
501 fCoefficients[3], fScaleFactor);
502
503 // This is totaly weired but the only way to get both options
504 // working with this nonsense implementation of TLatex
505 TLatex text(x, y, txt);
506 text.SetBit(TLatex::kTextNDC);
507 text.SetTextSize(size);
508 if (draw)
509 text.DrawLatex(x, y, txt);
510 else
511 text.Paint();
512
513 TLine line;
514 line.SetLineColor(14);
515 if (draw)
516 line.DrawLine(m, gPad->GetUymin(), m, gPad->GetUymax());
517 else
518 line.PaintLine(m, gPad->GetUymin(), m, gPad->GetUymax());
519}
520
521void MAlphaFitter::Copy(TObject &o) const
522{
523 MAlphaFitter &f = static_cast<MAlphaFitter&>(o);
524
525 // Setup
526 f.fSigInt = fSigInt;
527 f.fSigMax = fSigMax;
528 f.fBgMin = fBgMin;
529 f.fBgMax = fBgMax;
530 f.fScaleMin = fScaleMin;
531 f.fScaleMax = fScaleMax;
532 f.fPolynomOrder = fPolynomOrder;
533 f.fFitBackground= fFitBackground;
534 f.fSignalFunc = fSignalFunc;
535 f.fScaleMode = fScaleMode;
536 f.fScaleUser = fScaleUser;
537 f.fStrategy = fStrategy;
538 f.fCoefficients.Set(fCoefficients.GetSize());
539 f.fCoefficients.Reset();
540 f.fErrors.Set(fCoefficients.GetSize());
541 f.fErrors.Reset();
542
543 // Result
544 f.fSignificance = fSignificance;
545 f.fErrorExcess = fErrorExcess;
546 f.fEventsExcess = fEventsExcess;
547 f.fEventsSignal = fEventsSignal;
548 f.fEventsBackground = fEventsBackground;
549 f.fChiSqSignal = fChiSqSignal;
550 f.fChiSqBg = fChiSqBg;
551 f.fIntegralMax = fIntegralMax;
552 f.fScaleFactor = fScaleFactor;
553
554 // Function
555 delete f.fFunc;
556
557 f.fFunc = new TF1(*fFunc);
558 f.fFunc->SetName("Dummy");
559 gROOT->GetListOfFunctions()->Remove(f.fFunc);
560}
561
562void MAlphaFitter::Print(Option_t *o) const
563{
564 *fLog << GetDescriptor() << ": Fitting..." << endl;
565 *fLog << " ...signal to " << fSigMax << " (integrate into bin at " << fSigInt << ")" << endl;
566 *fLog << " ...signal function: ";
567 switch (fSignalFunc)
568 {
569 case kGauss: *fLog << "gauss(x)/pol" << fPolynomOrder; break;
570 case kThetaSq: *fLog << "gauss(sqrt(x))/expo"; break;
571 }
572 *fLog << endl;
573 if (!fFitBackground)
574 *fLog << " ...no background." << endl;
575 else
576 {
577 *fLog << " ...background from " << fBgMin << " to " << fBgMax << endl;
578 *fLog << " ...polynom order " << fPolynomOrder << endl;
579 *fLog << " ...scale mode: ";
580 switch (fScaleMode)
581 {
582 case kNone: *fLog << "none."; break;
583 case kEntries: *fLog << "entries."; break;
584 case kIntegral: *fLog << "integral."; break;
585 case kOffRegion: *fLog << "off region (integral between " << fScaleMin << " and " << fScaleMax << ")"; break;
586 case kBackground: *fLog << "background (integral between " << fBgMin << " and " << fBgMax << ")"; break;
587 case kLeastSquare: *fLog << "least square (N/A)"; break;
588 case kUserScale: *fLog << "user def (" << fScaleUser << ")"; break;
589 }
590 *fLog << endl;
591 }
592
593 if (TString(o).Contains("result"))
594 {
595 *fLog << "Result:" << endl;
596 *fLog << " - Significance (Li/Ma) " << fSignificance << endl;
597 *fLog << " - Excess Events " << fEventsExcess << endl;
598 *fLog << " - Signal Events " << fEventsSignal << endl;
599 *fLog << " - Background Events " << fEventsBackground << endl;
600 *fLog << " - E/sqrt(B>=Alpha) " << fEventsExcess/TMath::Sqrt(TMath::Max(fEventsBackground,fScaleFactor)) << endl;
601 *fLog << " - Chi^2/ndf (Signal) " << fChiSqSignal << endl;
602 *fLog << " - Chi^2/ndf (Background) " << fChiSqBg << endl;
603 *fLog << " - Signal integrated up to " << fIntegralMax << "°" << endl;
604 *fLog << " - Off Scale Alpha (Off) " << fScaleFactor << endl;
605 }
606}
607
608Bool_t MAlphaFitter::FitEnergy(const TH3D &hon, UInt_t bin, Bool_t paint)
609{
610 const TString name(MString::Format("TempAlphaEnergy%06d", gRandom->Integer(1000000)));
611
612 TH1D *h = hon.ProjectionZ(name, 0, hon.GetNbinsX()+1, bin, bin, "E");
613 h->SetDirectory(0);
614
615 const Bool_t rc = Fit(*h, paint);
616
617 delete h;
618
619 return rc;
620}
621
622Bool_t MAlphaFitter::FitTheta(const TH3D &hon, UInt_t bin, Bool_t paint)
623{
624 const TString name(MString::Format("TempAlphaTheta%06d", gRandom->Integer(1000000)));
625
626 TH1D *h = hon.ProjectionZ(name, bin, bin, 0, hon.GetNbinsY()+1, "E");
627 h->SetDirectory(0);
628
629 const Bool_t rc = Fit(*h, paint);
630
631 delete h;
632
633 return rc;
634}
635/*
636Bool_t MAlphaFitter::FitTime(const TH3D &hon, UInt_t bin, Bool_t paint)
637{
638 const TString name(Form("TempAlphaTime%06d", gRandom->Integer(1000000)));
639
640 hon.GetZaxis()->SetRange(bin,bin);
641 TH1D *h = (TH1D*)hon.Project3D("ye");
642 hon.GetZaxis()->SetRange(-1,-1);
643
644 h->SetDirectory(0);
645
646 const Bool_t rc = Fit(*h, paint);
647 delete h;
648 return rc;
649}
650*/
651Bool_t MAlphaFitter::FitAlpha(const TH3D &hon, Bool_t paint)
652{
653 const TString name(MString::Format("TempAlpha%06d", gRandom->Integer(1000000)));
654
655 TH1D *h = hon.ProjectionZ(name, 0, hon.GetNbinsX()+1, 0, hon.GetNbinsY()+1, "E");
656 h->SetDirectory(0);
657
658 const Bool_t rc = Fit(*h, paint);
659
660 delete h;
661
662 return rc;
663}
664
665Bool_t MAlphaFitter::FitEnergy(const TH3D &hon, const TH3D &hof, UInt_t bin, Bool_t paint)
666{
667 const TString name1(MString::Format("TempAlpha%06d_on", gRandom->Integer(1000000)));
668 const TString name0(MString::Format("TempAlpha%06d_off", gRandom->Integer(1000000)));
669
670 TH1D *h1 = hon.ProjectionZ(name1, 0, hon.GetNbinsX()+1, bin, bin, "E");
671 h1->SetDirectory(0);
672
673 TH1D *h0 = hof.ProjectionZ(name0, 0, hof.GetNbinsX()+1, bin, bin, "E");
674 h0->SetDirectory(0);
675
676 const Bool_t rc = ScaleAndFit(*h1, h0, paint);
677
678 delete h0;
679 delete h1;
680
681 return rc;
682}
683
684Bool_t MAlphaFitter::FitTheta(const TH3D &hon, const TH3D &hof, UInt_t bin, Bool_t paint)
685{
686 const TString name1(MString::Format("TempAlpha%06d_on", gRandom->Integer(1000000)));
687 const TString name0(MString::Format("TempAlpha%06d_off", gRandom->Integer(1000000)));
688
689 TH1D *h1 = hon.ProjectionZ(name1, bin, bin, 0, hon.GetNbinsY()+1, "E");
690 h1->SetDirectory(0);
691
692 TH1D *h0 = hof.ProjectionZ(name0, bin, bin, 0, hof.GetNbinsY()+1, "E");
693 h0->SetDirectory(0);
694
695 const Bool_t rc = ScaleAndFit(*h1, h0, paint);
696
697 delete h0;
698 delete h1;
699
700 return rc;
701}
702/*
703Bool_t MAlphaFitter::FitTime(const TH3D &hon, const TH3D &hof, UInt_t bin, Bool_t paint)
704{
705 const TString name1(Form("TempAlphaTime%06d_on", gRandom->Integer(1000000)));
706 const TString name0(Form("TempAlphaTime%06d_off", gRandom->Integer(1000000)));
707
708 hon.GetZaxis()->SetRange(bin,bin);
709 TH1D *h1 = (TH1D*)hon.Project3D("ye");
710 hon.GetZaxis()->SetRange(-1,-1);
711 h1->SetDirectory(0);
712
713 hof.GetZaxis()->SetRange(bin,bin);
714 TH1D *h0 = (TH1D*)hof.Project3D("ye");
715 hof.GetZaxis()->SetRange(-1,-1);
716 h0->SetDirectory(0);
717
718 const Bool_t rc = ScaleAndFit(*h1, h0, paint);
719
720 delete h0;
721 delete h1;
722
723 return rc;
724}
725*/
726
727Bool_t MAlphaFitter::FitAlpha(const TH3D &hon, const TH3D &hof, Bool_t paint)
728{
729 const TString name1(MString::Format("TempAlpha%06d_on", gRandom->Integer(1000000)));
730 const TString name0(MString::Format("TempAlpha%06d_off", gRandom->Integer(1000000)));
731
732 TH1D *h1 = hon.ProjectionZ(name1, 0, hon.GetNbinsX()+1, 0, hon.GetNbinsY()+1, "E");
733 h1->SetDirectory(0);
734
735 TH1D *h0 = hof.ProjectionZ(name0, 0, hof.GetNbinsX()+1, 0, hof.GetNbinsY()+1, "E");
736 h0->SetDirectory(0);
737
738 const Bool_t rc = ScaleAndFit(*h1, h0, paint);
739
740 delete h0;
741 delete h1;
742
743 return rc;
744}
745
746Bool_t MAlphaFitter::ApplyScaling(const TH3D &hon, TH3D &hof, UInt_t bin) const
747{
748 const TString name1(MString::Format("TempAlpha%06d_on", gRandom->Integer(1000000)));
749 const TString name0(MString::Format("TempAlpha%06d_off", gRandom->Integer(1000000)));
750
751 TH1D *h1 = hon.ProjectionZ(name1, 0, hon.GetNbinsX()+1, bin, bin, "E");
752 h1->SetDirectory(0);
753
754 TH1D *h0 = hof.ProjectionZ(name0, 0, hof.GetNbinsX()+1, bin, bin, "E");
755 h0->SetDirectory(0);
756
757 const Double_t scale = Scale(*h0, *h1);
758
759 delete h0;
760 delete h1;
761
762 for (int x=0; x<=hof.GetNbinsX()+1; x++)
763 for (int z=0; z<=hof.GetNbinsZ()+1; z++)
764 {
765 hof.SetBinContent(x, bin, z, hof.GetBinContent(x, bin, z)*scale);
766 hof.SetBinError( x, bin, z, hof.GetBinError( x, bin, z)*scale);
767 }
768
769 return scale>0;
770}
771
772Bool_t MAlphaFitter::ApplyScaling(const TH3D &hon, TH3D &hof) const
773{
774 for (int y=0; y<=hof.GetNbinsY()+1; y++)
775 ApplyScaling(hon, hof, y);
776
777 return kTRUE;
778}
779
780Double_t MAlphaFitter::Scale(TH1D &of, const TH1D &on) const
781{
782 Float_t scaleon = 1;
783 Float_t scaleof = 1;
784 switch (fScaleMode)
785 {
786 case kNone:
787 return 1;
788
789 case kEntries:
790 scaleon = on.GetEntries();
791 scaleof = of.GetEntries();
792 break;
793
794 case kIntegral:
795 scaleon = on.Integral();
796 scaleof = of.Integral();
797 break;
798
799 case kOffRegion:
800 {
801 const Int_t min = on.GetXaxis()->FindFixBin(fScaleMin);
802 const Int_t max = on.GetXaxis()->FindFixBin(fScaleMax);
803 scaleon = on.Integral(min, max);
804 scaleof = of.Integral(min, max);
805 }
806 break;
807
808 case kBackground:
809 {
810 const Int_t min = on.GetXaxis()->FindFixBin(fBgMin);
811 const Int_t max = on.GetXaxis()->FindFixBin(fBgMax);
812 scaleon = on.Integral(min, max);
813 scaleof = of.Integral(min, max);
814 }
815 break;
816
817 case kUserScale:
818 scaleon = fScaleUser;
819 break;
820
821 // This is just to make some compiler happy
822 default:
823 return 1;
824 }
825
826 if (scaleof!=0)
827 {
828 of.Scale(scaleon/scaleof);
829 return scaleon/scaleof;
830 }
831 else
832 {
833 of.Reset();
834 return 0;
835 }
836}
837
838Double_t MAlphaFitter::GetMinimizationValue() const
839{
840 switch (fStrategy)
841 {
842 case kSignificance:
843 return -GetSignificance();
844 case kSignificanceChi2:
845 return -GetSignificance()/GetChiSqSignal();
846 case kSignificanceLogExcess:
847 if (GetEventsExcess()<1)
848 return 0;
849 return -GetSignificance()*TMath::Log10(GetEventsExcess());
850 case kSignificanceSqrtExcess:
851 if (GetEventsExcess()<1)
852 return 0;
853 return -GetSignificance()*TMath::Sqrt(GetEventsExcess());
854 case kSignificanceExcess:
855 return -GetSignificance()*GetEventsExcess();
856 case kExcess:
857 return -GetEventsExcess();
858 case kGaussSigma:
859 return GetGausSigma();
860 case kWeakSource:
861 if (GetEventsExcess()<1)
862 return 0;
863 return -GetEventsExcess()/TMath::Sqrt(TMath::Max(GetEventsBackground(), GetEventsBackground()));
864 case kWeakSourceLogExcess:
865 if (GetEventsExcess()<1)
866 return 0;
867 return -GetEventsExcess()/TMath::Sqrt(TMath::Max(GetEventsBackground(), GetEventsBackground()))*TMath::Log10(GetEventsExcess());
868 }
869 return 0;
870}
871
872Int_t MAlphaFitter::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
873{
874 Bool_t rc = kFALSE;
875
876 //void SetScaleUser(Float_t scale) { fScaleUser = scale; fScaleMode=kUserScale; }
877 //void SetScaleMode(ScaleMode_t mode) { fScaleMode = mode; }
878
879 if (IsEnvDefined(env, prefix, "SignalIntegralMax", print))
880 {
881 SetSignalIntegralMax(GetEnvValue(env, prefix, "SignalIntegralMax", fSigInt));
882 rc = kTRUE;
883 }
884 if (IsEnvDefined(env, prefix, "SignalFitMax", print))
885 {
886 SetSignalIntegralMax(GetEnvValue(env, prefix, "SignalFitMax", fSigMax));
887 rc = kTRUE;
888 }
889 if (IsEnvDefined(env, prefix, "BackgroundFitMax", print))
890 {
891 SetBackgroundFitMax(GetEnvValue(env, prefix, "BackgroundFitMax", fBgMax));
892 rc = kTRUE;
893 }
894 if (IsEnvDefined(env, prefix, "BackgroundFitMin", print))
895 {
896 SetBackgroundFitMin(GetEnvValue(env, prefix, "BackgroundFitMin", fBgMin));
897 rc = kTRUE;
898 }
899 if (IsEnvDefined(env, prefix, "ScaleMin", print))
900 {
901 SetScaleMin(GetEnvValue(env, prefix, "ScaleMin", fScaleMin));
902 rc = kTRUE;
903 }
904 if (IsEnvDefined(env, prefix, "ScaleMax", print))
905 {
906 SetScaleMax(GetEnvValue(env, prefix, "ScaleMax", fScaleMax));
907 rc = kTRUE;
908 }
909 if (IsEnvDefined(env, prefix, "PolynomOrder", print))
910 {
911 SetPolynomOrder(GetEnvValue(env, prefix, "PolynomOrder", fPolynomOrder));
912 rc = kTRUE;
913 }
914
915 if (IsEnvDefined(env, prefix, "MinimizationStrategy", print))
916 {
917 TString txt = GetEnvValue(env, prefix, "MinimizationStrategy", "");
918 txt = txt.Strip(TString::kBoth);
919 txt.ToLower();
920 if (txt==(TString)"significance")
921 fStrategy = kSignificance;
922 if (txt==(TString)"significancechi2")
923 fStrategy = kSignificanceChi2;
924 if (txt==(TString)"significanceexcess")
925 fStrategy = kSignificanceExcess;
926 if (txt==(TString)"excess")
927 fStrategy = kExcess;
928 if (txt==(TString)"gausssigma" || txt==(TString)"gaussigma")
929 fStrategy = kGaussSigma;
930 if (txt==(TString)"weaksource")
931 fStrategy = kWeakSource;
932 rc = kTRUE;
933 }
934 if (IsEnvDefined(env, prefix, "Scale", print))
935 {
936 fScaleUser = GetEnvValue(env, prefix, "Scale", fScaleUser);
937 rc = kTRUE;
938 }
939 if (IsEnvDefined(env, prefix, "ScaleMode", print))
940 {
941 TString txt = GetEnvValue(env, prefix, "ScaleMode", "");
942 txt = txt.Strip(TString::kBoth);
943 txt.ToLower();
944 if (txt==(TString)"none")
945 fScaleMode = kNone;
946 if (txt==(TString)"entries")
947 fScaleMode = kEntries;
948 if (txt==(TString)"integral")
949 fScaleMode = kIntegral;
950 if (txt==(TString)"offregion")
951 fScaleMode = kOffRegion;
952 if (txt==(TString)"background")
953 fScaleMode = kBackground;
954 if (txt==(TString)"leastsquare")
955 fScaleMode = kLeastSquare;
956 if (txt==(TString)"userscale")
957 fScaleMode = kUserScale;
958 if (txt==(TString)"fixed")
959 FixScale();
960 rc = kTRUE;
961 }
962 if (IsEnvDefined(env, prefix, "SignalFunction", print))
963 {
964 TString txt = GetEnvValue(env, prefix, "SignalFunction", "");
965 txt = txt.Strip(TString::kBoth);
966 txt.ToLower();
967 if (txt==(TString)"gauss" || txt==(TString)"gaus")
968 SetSignalFunction(kGauss);
969 if (txt==(TString)"thetasq")
970 SetSignalFunction(kThetaSq);
971 rc = kTRUE;
972 }
973
974 return rc;
975}
Note: See TracBrowser for help on using the repository browser.