source: trunk/MagicSoft/Mars/mcalib/MHGausEvents.cc@ 4382

Last change on this file since 4382 was 4382, checked in by gaug, 20 years ago
*** empty log message ***
File size: 27.7 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): Markus Gaug 02/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MHGausEvents
28//
29// A base class for events which are believed to follow a Gaussian distribution
30// with time, e.g. calibration events, observables containing white noise, ...
31//
32// MHGausEvents derives from MH, thus all features of MH can be used by a class
33// deriving from MHGausEvents, especially the filling functions.
34//
35// The central objects are:
36//
37// 1) The TH1F fHGausHist:
38// ====================
39//
40// It is created with a default name and title and resides in directory NULL.
41// - Any class deriving from MHGausEvents needs to apply a binning to fHGausHist
42// (e.g. by setting the variables fNbins, fFirst, fLast and calling the function
43// InitBins() or by directly calling fHGausHist.SetBins(..) )
44// - The histogram is filled with the functions FillHist() or FillHistAndArray().
45// - The histogram can be fitted with the function FitGaus(). This involves stripping
46// of all zeros at the lower and upper end of the histogram and re-binning to
47// a new number of bins, specified in the variable fBinsAfterStripping.
48// - The fit result's probability is compared to a reference probability fProbLimit
49// The NDF is compared to fNDFLimit and a check is made whether results are NaNs.
50// Accordingly, a flag IsGausFitOK() is set.
51// - One can repeat the fit within a given amount of sigmas from the previous mean
52// (specified by the variables fPickupLimit and fBlackoutLimit) with the function RepeatFit()
53// - One can completely skip the fitting to set mean, sigma and its errors directly
54// from the histograms with the function BypassFit()
55//
56// 2) The TArrayF fEvents:
57// ==========================
58//
59// It is created with 0 entries and not expanded unless FillArray() or FillHistAndArray()
60// are called.
61// - A first call to FillArray() or FillHistAndArray() initializes fEvents by default
62// to 512 entries.
63// - Any later call to FillArray() or FillHistAndArray() fills up the array.
64// Reaching the limit, the array is expanded by a factor 2.
65// - The array can be fourier-transformed into the array fPowerSpectrum.
66// Note that any FFT accepts only number of events which are a power of 2.
67// Thus, fEvents is cut to the next power of 2 smaller than its actual number of entries.
68// Be aware that you might lose information at the end of your analysis.
69// - Calling the function CreateFourierSpectrum() creates the array fPowerSpectrum
70// and its projection fHPowerProbability which in turn is fit to an exponential.
71// - The fit result's probability is compared to a referenc probability fProbLimit
72// and accordingly the flag IsExpFitOK() is set.
73// - The flag IsFourierSpectrumOK() is set accordingly to IsExpFitOK().
74// Later, a closer check will be installed.
75// - You can display all arrays by calls to: CreateGraphEvents() and
76// CreateGraphPowerSpectrum() and successive calls to the corresponding Getters.
77//
78// To see an example, have a look at: Draw()
79//
80//////////////////////////////////////////////////////////////////////////////
81#include "MHGausEvents.h"
82
83#include <TH1.h>
84#include <TF1.h>
85#include <TGraph.h>
86#include <TPad.h>
87#include <TVirtualPad.h>
88#include <TCanvas.h>
89#include <TStyle.h>
90
91#include "MFFT.h"
92#include "MArray.h"
93
94#include "MH.h"
95
96#include "MLog.h"
97#include "MLogManip.h"
98
99ClassImp(MHGausEvents);
100
101using namespace std;
102
103const Int_t MHGausEvents::fgBinsAfterStripping = 40;
104const Float_t MHGausEvents::fgBlackoutLimit = 5.;
105const Int_t MHGausEvents::fgNDFLimit = 2;
106const Float_t MHGausEvents::fgPickupLimit = 5.;
107const Float_t MHGausEvents::fgProbLimit = 0.001;
108const Int_t MHGausEvents::fgPowerProbabilityBins = 20;
109// --------------------------------------------------------------------------
110//
111// Default Constructor.
112// Sets:
113// - the default Probability Bins for fPowerProbabilityBins (fgPowerProbabilityBins)
114// - the default Probability Limit for fProbLimit (fgProbLimit)
115// - the default NDF Limit for fNDFLimit (fgNDFLimit)
116// - the default number for fPickupLimit (fgPickupLimit)
117// - the default number for fBlackoutLimit (fgBlackoutLimit)
118// - the default number of bins after stripping for fBinsAfterStipping (fgBinsAfterStipping)
119// - the default name of the fHGausHist ("HGausHist")
120// - the default title of the fHGausHist ("Histogram of Events with Gaussian Distribution")
121// - the default directory of the fHGausHist (NULL)
122// - the default for fNbins (100)
123// - the default for fFirst (0.)
124// - the default for fLast (100.)
125//
126// Initializes:
127// - fEvents to 0 entries
128// - fHGausHist()
129// - all other pointers to NULL
130// - all variables to 0.
131// - all flags to kFALSE
132//
133MHGausEvents::MHGausEvents(const char *name, const char *title)
134 : fEventFrequency(0), fHPowerProbability(NULL),
135 fPowerSpectrum(NULL),
136 fGraphEvents(NULL), fGraphPowerSpectrum(NULL),
137 fFGausFit(NULL), fFExpFit(NULL),
138 fFirst(0.), fLast(100.),
139 fNbins(100), fPixId(-1)
140{
141
142 fName = name ? name : "MHGausEvents";
143 fTitle = title ? title : "Events with expected Gaussian distributions";
144
145 Clear();
146
147 SetBinsAfterStripping();
148 SetBlackoutLimit();
149 SetNDFLimit();
150 SetPickupLimit();
151 SetPowerProbabilityBins();
152 SetProbLimit();
153
154 fHGausHist.SetName("HGausHist");
155 fHGausHist.SetTitle("Histogram of Events with Gaussian Distribution");
156 // important, other ROOT will not draw the axes:
157 fHGausHist.UseCurrentStyle();
158 fHGausHist.SetDirectory(NULL);
159 fHGausHist.GetYaxis()->CenterTitle();
160}
161
162
163
164// --------------------------------------------------------------------------
165//
166// Default Destructor.
167//
168// Deletes (if Pointer is not NULL):
169//
170// - fHPowerProbability
171// - fFGausFit
172// - fFExpFit
173// - fPowerSpectrum
174// - fGraphEvents
175// - fGraphPowerSpectrum
176//
177MHGausEvents::~MHGausEvents()
178{
179
180 // delete histograms
181// if (fHPowerProbability)
182// if (gROOT->FindObject(fHPowerProbability->GetName()))
183// delete fHPowerProbability;
184
185 // delete fits
186 if (fFGausFit)
187 delete fFGausFit;
188 if (fFExpFit)
189 delete fFExpFit;
190
191 // delete arrays
192 if (fPowerSpectrum)
193 delete fPowerSpectrum;
194
195 // delete graphs
196 if (fGraphEvents)
197 delete fGraphEvents;
198 if (fGraphPowerSpectrum)
199 delete fGraphPowerSpectrum;
200}
201
202// --------------------------------------------------------------------------
203//
204// Default Clear(), can be overloaded.
205//
206// Sets:
207// - all other pointers to NULL
208// - all variables to 0., except fPixId to -1 and keep fEventFrequency
209// - all flags to kFALSE
210//
211// Deletes (if not NULL):
212// - all pointers
213//
214void MHGausEvents::Clear(Option_t *o)
215{
216
217 SetGausFitOK ( kFALSE );
218 SetExpFitOK ( kFALSE );
219 SetFourierSpectrumOK( kFALSE );
220 SetExcluded ( kFALSE );
221
222 fMean = 0.;
223 fSigma = 0.;
224 fMeanErr = 0.;
225 fSigmaErr = 0.;
226 fProb = 0.;
227
228 fCurrentSize = 0;
229
230 if (fHPowerProbability)
231 {
232 if (gROOT->FindObject(fHPowerProbability->GetName()))
233 delete fHPowerProbability;
234 fHPowerProbability = NULL;
235 }
236
237 // delete fits
238 if (fFGausFit)
239 {
240 delete fFGausFit;
241 fFGausFit = NULL;
242 }
243
244 if (fFExpFit)
245 {
246 delete fFExpFit;
247 fFExpFit = NULL;
248 }
249
250 // delete arrays
251 if (fPowerSpectrum)
252 {
253 delete fPowerSpectrum;
254 fPowerSpectrum = NULL;
255 }
256
257 // delete graphs
258 if (fGraphEvents)
259 {
260 delete fGraphEvents;
261 fGraphEvents = NULL;
262 }
263
264 if (fGraphPowerSpectrum)
265 {
266 delete fGraphPowerSpectrum;
267 fGraphPowerSpectrum = NULL;
268 }
269}
270
271
272// -----------------------------------------------------------------------------
273//
274// Bypasses the Gauss fit by taking mean and RMS from the histogram
275//
276// Errors are determined in the following way:
277// MeanErr = RMS / Sqrt(entries)
278// SigmaErr = RMS / (2.*Sqrt(entries) )
279//
280void MHGausEvents::BypassFit()
281{
282
283 const Stat_t entries = fHGausHist.GetEntries();
284
285 if (entries <= 0.)
286 {
287 *fLog << warn << GetDescriptor()
288 << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl;
289 return;
290 }
291
292 fMean = fHGausHist.GetMean();
293 fMeanErr = fHGausHist.GetRMS() / TMath::Sqrt(entries);
294 fSigma = fHGausHist.GetRMS() ;
295 fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.;
296}
297
298
299
300// --------------------------------------------------------------------------
301//
302// - Set fPixId to id
303//
304// Add id to names and titles of:
305// - fHGausHist
306//
307void MHGausEvents::ChangeHistId(const Int_t id)
308{
309
310 fPixId = id;
311
312 fHGausHist.SetName( Form("%s%d", fHGausHist.GetName(), id));
313 fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
314
315 fName = Form("%s%d", fName.Data(), id);
316 fTitle = Form("%s%d", fTitle.Data(), id);
317
318}
319
320// -----------------------------------------------------------------------------
321//
322// Create the x-axis for the event graph
323//
324Float_t *MHGausEvents::CreateEventXaxis(Int_t n)
325{
326
327 Float_t *xaxis = new Float_t[n]; fHPowerProbability->SetDirectory(NULL);
328
329 if (fEventFrequency)
330 for (Int_t i=0;i<n;i++)
331 xaxis[i] = (Float_t)i/fEventFrequency;
332 else
333 for (Int_t i=0;i<n;i++)
334 xaxis[i] = (Float_t)i;
335
336 return xaxis;
337
338}
339
340
341// -------------------------------------------------------------------
342//
343// Create the fourier spectrum using the class MFFT.
344// The result is projected into a histogram and fitted by an exponential
345// fHPowerProbability->SetDirectory(NULL);
346void MHGausEvents::CreateFourierSpectrum()
347{
348
349 if (fFExpFit)
350 return;
351
352 if (fEvents.GetSize() < 8)
353 {
354 *fLog << warn << "Cannot create Fourier spectrum in pixel: " << fPixId
355 << ". Number of events smaller than 8 " << endl;
356 return;
357 }
358
359
360 //
361 // The number of entries HAS to be a potence of 2,
362 // so we can only cut out from the last potence of 2 to the rest.
363 // Another possibility would be to fill everything with
364 // zeros, but that gives a low frequency peak, which we would
365 // have to cut out later again.
366 //
367 // So, we have to live with the possibility that at the end
368 // of the calibration run, something has happened without noticing
369 // it...
370 //
371
372 // This cuts only the non-used zero's, but MFFT will later cut the rest
373 MArray::StripZeros(fEvents);
374
375 if (fEvents.GetSize() < 8)
376 {
377 /*
378 *fLog << warn << "Cannot create Fourier spectrum. " << endl;
379 *fLog << warn << "Number of events (after stripping of zeros) is smaller than 8 "
380 << "in pixel: " << fPixId << endl;
381 */
382 return;
383 }
384
385 MFFT fourier;
386
387 fPowerSpectrum = fourier.PowerSpectrumDensity(&fEvents);
388 fHPowerProbability = ProjectArray(*fPowerSpectrum, fPowerProbabilityBins,
389 Form("%s%s","PowerProbability",GetName()),
390 "Probability of Power occurrance");
391 fHPowerProbability->SetXTitle("P(f)");
392 fHPowerProbability->SetYTitle("Counts");
393 fHPowerProbability->GetYaxis()->CenterTitle();
394 fHPowerProbability->SetDirectory(NULL);
395 fHPowerProbability->SetBit(kCanDelete);
396 //
397 // First guesses for the fit (should be as close to reality as possible,
398 //
399 const Double_t xmax = fHPowerProbability->GetXaxis()->GetXmax();
400
401 fFExpFit = new TF1("FExpFit","exp([0]-[1]*x)",0.,xmax);
402
403 const Double_t slope_guess = (TMath::Log(fHPowerProbability->GetEntries())+1.)/xmax;
404 const Double_t offset_guess = slope_guess*xmax;
405
406 fFExpFit->SetParameters(offset_guess, slope_guess);
407 fFExpFit->SetParNames("Offset","Slope");
408 fFExpFit->SetParLimits(0,offset_guess/2.,2.*offset_guess);
409 fFExpFit->SetParLimits(1,slope_guess/1.5,1.5*slope_guess);
410 fFExpFit->SetRange(0.,xmax);
411
412 fHPowerProbability->Fit(fFExpFit,"RQL0");
413
414 if (GetExpProb() > fProbLimit)
415 SetExpFitOK(kTRUE);
416
417 //
418 // For the moment, this is the only check, later we can add more...
419 //
420 SetFourierSpectrumOK(IsExpFitOK());
421
422 return;
423}
424
425// ----------------------------------------------------------------------------------
426//
427// Create a graph to display the array fEvents
428// If the variable fEventFrequency is set, the x-axis is transformed into real time.
429//
430void MHGausEvents::CreateGraphEvents()
431{
432
433 MArray::StripZeros(fEvents);
434
435 const Int_t n = fEvents.GetSize();
436
437 fGraphEvents = new TGraph(n,CreateEventXaxis(n),fEvents.GetArray());
438 fGraphEvents->SetTitle("Evolution of Events with time");
439 fGraphEvents->GetXaxis()->SetTitle((fEventFrequency) ? "Time [s]" : "Event Nr.");
440 fGraphEvents->GetYaxis()->SetTitle(fHGausHist.GetXaxis()->GetTitle());
441 fGraphEvents->GetYaxis()->CenterTitle();
442}
443
444// ----------------------------------------------------------------------------------
445//
446// Create a graph to display the array fPowerSpectrum
447// If the variable fEventFrequency is set, the x-axis is transformed into real frequency.
448//
449void MHGausEvents::CreateGraphPowerSpectrum()
450{
451
452 MArray::StripZeros(*fPowerSpectrum);
453
454 const Int_t n = fPowerSpectrum->GetSize();
455
456 fGraphPowerSpectrum = new TGraph(n,CreatePSDXaxis(n),fPowerSpectrum->GetArray());
457 fGraphPowerSpectrum->SetTitle("Power Spectrum Density");
458 fGraphPowerSpectrum->GetXaxis()->SetTitle((fEventFrequency) ? "Frequency [Hz]" : "Frequency");
459 fGraphPowerSpectrum->GetYaxis()->SetTitle("P(f)");
460 fGraphPowerSpectrum->GetYaxis()->CenterTitle();
461
462}
463
464
465// -----------------------------------------------------------------------------
466//
467// Create the x-axis for the event graph
468//
469Float_t *MHGausEvents::CreatePSDXaxis(Int_t n)
470{
471
472 Float_t *xaxis = new Float_t[n];
473
474 if (fEventFrequency)
475 for (Int_t i=0;i<n;i++)
476 xaxis[i] = 0.5*(Float_t)i*fEventFrequency/n;
477 else
478 for (Int_t i=0;i<n;i++)
479 xaxis[i] = 0.5*(Float_t)i/n;
480
481 return xaxis;
482
483}
484
485// -----------------------------------------------------------------------------
486//
487// Default draw:
488//
489// The following options can be chosen:
490//
491// "EVENTS": displays a TGraph of the array fEvents
492// "FOURIER": display a TGraph of the fourier transform of fEvents
493// displays the projection of the fourier transform with the fit
494//
495// The following picture shows a typical outcome of call to Draw("fourierevents"):
496// - The first plot shows the distribution of the values with the Gauss fit
497// (which did not succeed, in this case, for obvious reasons)
498// - The second plot shows the TGraph with the events vs. time
499// - The third plot shows the fourier transform and a small peak at about 85 Hz.
500// - The fourth plot shows the projection of the fourier components and an exponential
501// fit, with the result that the observed deviation is still statistical with a
502// probability of 0.5%.
503//
504//Begin_Html
505/*
506<img src="images/MHGausEventsDraw.gif">
507*/
508//End_Html
509//
510void MHGausEvents::Draw(const Option_t *opt)
511{
512
513 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas(this,600, 900);
514
515 TString option(opt);
516 option.ToLower();
517
518 Int_t win = 1;
519
520 if (option.Contains("events"))
521 {
522 option.ReplaceAll("events","");
523 win += 1;
524 }
525 if (option.Contains("fourier"))
526 {
527 option.ReplaceAll("fourier","");
528 win += 2;
529 }
530
531 pad->SetTicks();
532 pad->SetBorderMode(0);
533 pad->Divide(1,win);
534 pad->cd(1);
535
536 if (!IsEmpty())
537 gPad->SetLogy();
538
539 fHGausHist.Draw(opt);
540
541 if (fFGausFit)
542 {
543 fFGausFit->SetLineColor(IsGausFitOK() ? kGreen : kRed);
544 fFGausFit->Draw("same");
545 }
546 switch (win)
547 {
548 case 2:
549 pad->cd(2);
550 DrawEvents();
551 break;
552 case 3:
553 pad->cd(2);
554 DrawPowerSpectrum(*pad,3);
555 break;
556 case 4:
557 pad->cd(2);
558 DrawEvents();
559 pad->cd(3);
560 DrawPowerSpectrum(*pad,4);
561 break;
562 }
563}
564
565void MHGausEvents::DrawEvents()
566{
567
568 if (!fGraphEvents)
569 CreateGraphEvents();
570
571 fGraphEvents->SetBit(kCanDelete);
572 fGraphEvents->SetTitle("Events with time");
573 fGraphEvents->Draw("AL");
574
575}
576
577
578void MHGausEvents::DrawPowerSpectrum(TVirtualPad &pad, Int_t i)
579{
580
581 if (fPowerSpectrum)
582 {
583 if (!fGraphPowerSpectrum)
584 CreateGraphPowerSpectrum();
585
586 fGraphPowerSpectrum->Draw("AL");
587 fGraphPowerSpectrum->SetBit(kCanDelete);
588 }
589
590 pad.cd(i);
591
592 if (fHPowerProbability && fHPowerProbability->GetEntries() > 0)
593 {
594 gPad->SetLogy();
595 fHPowerProbability->Draw();
596 if (fFExpFit)
597 {
598 fFExpFit->SetLineColor(IsExpFitOK() ? kGreen : kRed);
599 fFExpFit->Draw("same");
600 }
601 }
602}
603
604
605// --------------------------------------------------------------------------
606//
607// Fill fEvents with f
608// If size of fEvents is 0, initializes it to 512
609// If size of fEvents is smaller than fCurrentSize, double the size
610// Increase fCurrentSize by 1
611//
612void MHGausEvents::FillArray(const Float_t f)
613{
614 if (fEvents.GetSize() == 0)
615 fEvents.Set(512);
616
617 if (fCurrentSize >= fEvents.GetSize())
618 fEvents.Set(fEvents.GetSize()*2);
619
620 fEvents.AddAt(f,fCurrentSize++);
621}
622
623
624// --------------------------------------------------------------------------
625//
626// Fills fHGausHist with f
627// Returns kFALSE, if overflow or underflow occurred, else kTRUE
628//
629Bool_t MHGausEvents::FillHist(const Float_t f)
630{
631 return fHGausHist.Fill(f) > -1;
632}
633
634// --------------------------------------------------------------------------
635//
636// Executes:
637// - FillArray()
638// - FillHist()
639//
640Bool_t MHGausEvents::FillHistAndArray(const Float_t f)
641{
642
643 FillArray(f);
644 return FillHist(f);
645}
646
647// -------------------------------------------------------------------
648//
649// Fit fGausHist with a Gaussian after stripping zeros from both ends
650// and rebinned to the number of bins specified in fBinsAfterStripping
651//
652// The fit results are retrieved and stored in class-own variables.
653//
654// A flag IsGausFitOK() is set according to whether the fit probability
655// is smaller or bigger than fProbLimit, whether the NDF is bigger than
656// fNDFLimit and whether results are NaNs.
657//
658Bool_t MHGausEvents::FitGaus(Option_t *option, const Double_t xmin, const Double_t xmax)
659{
660
661 if (IsGausFitOK())
662 return kTRUE;
663
664 //
665 // First, strip the zeros from the edges which contain only zeros and rebin
666 // to about fBinsAfterStripping bins.
667 //
668 // (ATTENTION: The Chisquare method is more sensitive,
669 // the _less_ bins, you have!)
670 //
671 StripZeros(&fHGausHist,fBinsAfterStripping);
672
673 TAxis *axe = fHGausHist.GetXaxis();
674 //
675 // Get the fitting ranges
676 //
677 Axis_t rmin = ((xmin==0.) && (xmax==0.)) ? fHGausHist.GetBinCenter(axe->GetFirst()) : xmin;
678 Axis_t rmax = ((xmin==0.) && (xmax==0.)) ? fHGausHist.GetBinCenter(axe->GetLast()) : xmax;
679
680 //
681 // First guesses for the fit (should be as close to reality as possible,
682 //
683 const Stat_t entries = fHGausHist.Integral(axe->FindBin(rmin),axe->FindBin(rmax),"width");
684 const Double_t mu_guess = fHGausHist.GetBinCenter(fHGausHist.GetMaximumBin());
685 const Double_t sigma_guess = fHGausHist.GetRMS();
686 const Double_t area_guess = entries/TMath::Sqrt(TMath::TwoPi())/sigma_guess;
687
688 fFGausFit = new TF1("GausFit","gaus",rmin,rmax);
689
690 if (!fFGausFit)
691 {
692 *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit "
693 << "in pixel: " << fPixId << endl;
694 return kFALSE;
695 }
696
697 fFGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
698 fFGausFit->SetParNames("Area","#mu","#sigma");
699 fFGausFit->SetParLimits(0,0.,area_guess*25.);
700 fFGausFit->SetParLimits(1,rmin,rmax);
701 fFGausFit->SetParLimits(2,0.,rmax-rmin);
702 fFGausFit->SetRange(rmin,rmax);
703
704 fHGausHist.Fit(fFGausFit,option);
705
706
707 fMean = fFGausFit->GetParameter(1);
708 fSigma = fFGausFit->GetParameter(2);
709 fMeanErr = fFGausFit->GetParError(1);
710 fSigmaErr = fFGausFit->GetParError(2);
711 fProb = fFGausFit->GetProb();
712 //
713 // The fit result is accepted under condition:
714 // 1) The results are not nan's
715 // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
716 // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
717 //
718 if ( TMath::IsNaN(fMean)
719 || TMath::IsNaN(fMeanErr)
720 || TMath::IsNaN(fProb)
721 || TMath::IsNaN(fSigma)
722 || TMath::IsNaN(fSigmaErr)
723 || fFGausFit->GetNDF() < fNDFLimit
724 || fProb < fProbLimit )
725 return kFALSE;
726
727 SetGausFitOK(kTRUE);
728 return kTRUE;
729}
730
731// -------------------------------------------------------------------------------
732//
733// Return the number of "blackout" events, which are events with values higher
734// than fBlackoutLimit sigmas from the mean
735//
736//
737const Double_t MHGausEvents::GetBlackout() const
738{
739
740 if ((fMean == 0.) && (fSigma == 0.))
741 return -1.;
742
743 const Int_t first = fHGausHist.GetXaxis()->GetFirst();
744 const Int_t last = fHGausHist.GetXaxis()->FindBin(fMean-fBlackoutLimit*fSigma);
745
746 if (first >= last)
747 return 0.;
748
749 return fHGausHist.Integral(first, last, "width");
750}
751
752const Double_t MHGausEvents::GetChiSquare() const
753{
754 return ( fFGausFit ? fFGausFit->GetChisquare() : 0.);
755}
756
757
758const Double_t MHGausEvents::GetExpChiSquare() const
759{
760 return ( fFExpFit ? fFExpFit->GetChisquare() : 0.);
761}
762
763
764const Int_t MHGausEvents::GetExpNdf() const
765{
766 return ( fFExpFit ? fFExpFit->GetNDF() : 0);
767}
768
769
770const Double_t MHGausEvents::GetExpProb() const
771{
772 return ( fFExpFit ? fFExpFit->GetProb() : 0.);
773}
774
775
776const Int_t MHGausEvents::GetNdf() const
777{
778 return ( fFGausFit ? fFGausFit->GetNDF() : 0);
779}
780
781const Double_t MHGausEvents::GetOffset() const
782{
783 return ( fFExpFit ? fFExpFit->GetParameter(0) : 0.);
784}
785
786
787// -------------------------------------------------------------------------------
788//
789// Return the number of "pickup" events, which are events with values higher
790// than fPickupLimit sigmas from the mean
791//
792//
793const Double_t MHGausEvents::GetPickup() const
794{
795
796 if ((fMean == 0.) && (fSigma == 0.))
797 return -1.;
798
799 const Int_t first = fHGausHist.GetXaxis()->FindBin(fMean+fPickupLimit*fSigma);
800 const Int_t last = fHGausHist.GetXaxis()->GetLast();
801
802 if (first >= last)
803 return 0.;
804
805 return fHGausHist.Integral(first, last, "width");
806}
807
808
809const Double_t MHGausEvents::GetSlope() const
810{
811 return ( fFExpFit ? fFExpFit->GetParameter(1) : 0.);
812}
813
814// --------------------------------------------------------------------------
815//
816// Default InitBins, can be overloaded.
817//
818// Executes:
819// - fHGausHist.SetBins(fNbins,fFirst,fLast)
820//
821void MHGausEvents::InitBins()
822{
823 fHGausHist.SetBins(fNbins,fFirst,fLast);
824}
825
826const Bool_t MHGausEvents::IsEmpty() const
827{
828 return !(fHGausHist.GetEntries());
829}
830
831
832const Bool_t MHGausEvents::IsExcluded() const
833{
834 return TESTBIT(fFlags,kExcluded);
835}
836
837
838const Bool_t MHGausEvents::IsExpFitOK() const
839{
840 return TESTBIT(fFlags,kExpFitOK);
841}
842
843const Bool_t MHGausEvents::IsFourierSpectrumOK() const
844{
845 return TESTBIT(fFlags,kFourierSpectrumOK);
846}
847
848
849const Bool_t MHGausEvents::IsGausFitOK() const
850{
851 return TESTBIT(fFlags,kGausFitOK);
852}
853
854
855// -----------------------------------------------------------------------------------
856//
857// A default print
858//
859void MHGausEvents::Print(const Option_t *o) const
860{
861
862 *fLog << all << endl;
863 *fLog << all << "Results of the Gauss Fit in pixel: " << fPixId << endl;
864 *fLog << all << "Mean: " << GetMean() << endl;
865 *fLog << all << "Sigma: " << GetSigma() << endl;
866 *fLog << all << "Chisquare: " << GetChiSquare() << endl;
867 *fLog << all << "DoF: " << GetNdf() << endl;
868 *fLog << all << "Probability: " << GetProb() << endl;
869 *fLog << all << endl;
870
871}
872
873// --------------------------------------------------------------------------
874//
875// Re-normalize the results, has to be overloaded
876//
877void MHGausEvents::Renorm()
878{
879}
880
881// -----------------------------------------------------------------------------
882//
883// If flag IsGausFitOK() is set (histogram already successfully fitted),
884// returns kTRUE
885//
886// If both fMean and fSigma are still zero, call FitGaus()
887//
888// Repeats the Gauss fit in a smaller range, defined by:
889//
890// min = GetMean() - fBlackoutLimit * GetSigma();
891// max = GetMean() + fPickupLimit * GetSigma();
892//
893// The fit results are retrieved and stored in class-own variables.
894//
895// A flag IsGausFitOK() is set according to whether the fit probability
896// is smaller or bigger than fProbLimit, whether the NDF is bigger than
897// fNDFLimit and whether results are NaNs.
898//
899Bool_t MHGausEvents::RepeatFit(const Option_t *option)
900{
901
902 if (IsGausFitOK())
903 return kTRUE;
904
905 if ((fMean == 0.) && (fSigma == 0.))
906 return FitGaus();
907
908 //
909 // Get new fitting ranges
910 //
911 Axis_t rmin = fMean - fBlackoutLimit * fSigma;
912 Axis_t rmax = fMean + fPickupLimit * fSigma;
913
914 Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst());
915 Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ;
916
917 fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax);
918
919 fHGausHist.Fit(fFGausFit,option);
920
921 fMean = fFGausFit->GetParameter(1);
922 fSigma = fFGausFit->GetParameter(2);
923 fMeanErr = fFGausFit->GetParError(1) ;
924 fSigmaErr = fFGausFit->GetParError(2) ;
925 fProb = fFGausFit->GetProb() ;
926
927 //
928 // The fit result is accepted under condition:
929 // 1) The results are not nan's
930 // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
931 // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
932 //
933 if ( TMath::IsNaN ( fMean )
934 || TMath::IsNaN ( fMeanErr )
935 || TMath::IsNaN ( fProb )
936 || TMath::IsNaN ( fSigma )
937 || TMath::IsNaN ( fSigmaErr )
938 || fFGausFit->GetNDF() < fNDFLimit
939 || fProb < fProbLimit )
940 return kFALSE;
941
942 SetGausFitOK(kTRUE);
943 return kTRUE;
944
945}
946
947// --------------------------------------------------------------------------
948//
949// Default Reset(), can be overloaded.
950//
951// Executes:
952// - Clear()
953// - fHGausHist.Reset()
954// - fEvents.Set(0)
955//
956void MHGausEvents::Reset()
957{
958
959 Clear();
960 fHGausHist.Reset();
961 fEvents.Set(0);
962
963}
964
965// --------------------------------------------------------------------------
966//
967// Set Excluded bit from outside
968//
969void MHGausEvents::SetExcluded(const Bool_t b)
970{
971 b ? SETBIT(fFlags,kExcluded) : CLRBIT(fFlags,kExcluded);
972}
973
974
975// -------------------------------------------------------------------
976//
977// The flag setters are to be used ONLY for Monte-Carlo!!
978//
979void MHGausEvents::SetExpFitOK(const Bool_t b)
980{
981
982 b ? SETBIT(fFlags,kExpFitOK) : CLRBIT(fFlags,kExpFitOK);
983}
984
985// -------------------------------------------------------------------
986//
987// The flag setters are to be used ONLY for Monte-Carlo!!
988//
989void MHGausEvents::SetFourierSpectrumOK(const Bool_t b)
990{
991
992 b ? SETBIT(fFlags,kFourierSpectrumOK) : CLRBIT(fFlags,kFourierSpectrumOK);
993}
994
995
996// -------------------------------------------------------------------
997//
998// The flag setters are to be used ONLY for Monte-Carlo!!
999//
1000void MHGausEvents::SetGausFitOK(const Bool_t b)
1001{
1002 b ? SETBIT(fFlags,kGausFitOK) : CLRBIT(fFlags,kGausFitOK);
1003
1004}
1005
Note: See TracBrowser for help on using the repository browser.