Ignore:
Timestamp:
04/03/04 17:27:50 (21 years ago)
Author:
gaug
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mcalib/MHGausEvents.cc

    r3625 r3636  
    4040//     It is created with a default name and title and resides in directory NULL.
    4141//     - Any class deriving from MHGausEvents needs to apply a binning to fHGausHist
    42 //       (e.g. with the function TH1F::SetBins(..) )
     42//       (e.g. by setting the variables fNbins, fFirst, fLast and calling the function
     43//       InitBins() or by directly calling fHGausHist.SetBins(..) )
    4344//     - The histogram is filled with the functions FillHist() or FillHistAndArray().
    4445//     - The histogram can be fitted with the function FitGaus(). This involves stripping
     
    4849//       The NDF is compared to fNDFLimit and a check is made whether results are NaNs.
    4950//       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 variable fPickupLimit) 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()
    5055//
    5156//  2) The TArrayF fEvents:
     
    96101const Float_t  MHGausEvents::fgProbLimit            = 0.001;
    97102const Int_t    MHGausEvents::fgNDFLimit             = 2;
     103const Float_t  MHGausEvents::fgPickupLimit          = 5.;
    98104const Int_t    MHGausEvents::fgPowerProbabilityBins = 20;
    99105const Int_t    MHGausEvents::fgBinsAfterStripping   = 40;
     
    105111// - the default Probability Limit for fProbLimit  (fgProbLimit)
    106112// - the default NDF Limit for fNDFLimit  (fgNDFLimit)
     113// - the default number for fPickupLimit  (fgPickupLimit)
    107114// - the default number of bins after stripping for fBinsAfterStipping (fgBinsAfterStipping)
    108115// - the default name of the fHGausHist   ("HGausHist")
    109116// - the default title of the fHGausHist  ("Histogram of Events with Gaussian Distribution")
    110117// - the default directory of the fHGausHist (NULL)
     118// - the default for fNbins (100)
     119// - the default for fFirst (0.)
     120// - the default for fLast  (100.)
    111121//
    112122// Initializes:
     
    121131      fPowerSpectrum(NULL),
    122132      fGraphEvents(NULL), fGraphPowerSpectrum(NULL),
     133      fNbins(100), fFirst(0.), fLast(100.), fPixId(-1),
    123134      fHGausHist(), fEvents(0),
    124135      fFGausFit(NULL), fFExpFit(NULL)
     
    133144  SetProbLimit();
    134145  SetNDFLimit();
     146  SetPickupLimit();
    135147  SetBinsAfterStripping();
    136148
     
    183195// --------------------------------------------------------------------------
    184196//
     197// Default Clear(), can be overloaded.
     198//
    185199// Sets:
    186200// - all other pointers to NULL
    187 // - all variables to 0., except fEventFrequency
     201// - all variables to 0., except fPixId to -1 and keep fEventFrequency
    188202// - all flags to kFALSE
    189203//
     
    197211  SetExpFitOK         ( kFALSE );
    198212  SetFourierSpectrumOK( kFALSE );
     213  SetExcluded         ( kFALSE );
    199214
    200215  fMean              = 0.;
     
    205220 
    206221  fCurrentSize       = 0;
     222  fPixId             = -1;
    207223
    208224  if (fHPowerProbability)
     
    247263
    248264// --------------------------------------------------------------------------
     265//
     266// Default Reset(), can be overloaded.
    249267//
    250268// Executes:
     
    264282// --------------------------------------------------------------------------
    265283//
     284// Default InitBins, can be overloaded.
     285//
     286// Executes:
     287// - fHGausHist.SetBins(fNbins,fFirst,fLast)
     288//
     289void MHGausEvents::InitBins()
     290{
     291  fHGausHist.SetBins(fNbins,fFirst,fLast);
     292}
     293
     294// --------------------------------------------------------------------------
     295//
    266296// Executes:
    267297// - FillArray()
     
    303333}
    304334
     335// --------------------------------------------------------------------------
     336//
     337// Set Excluded bit from outside
     338//
     339void MHGausEvents::SetExcluded(const Bool_t b)
     340{
     341    b ? SETBIT(fFlags,kExcluded) : CLRBIT(fFlags,kExcluded);
     342}
     343
    305344
    306345const Double_t MHGausEvents::GetChiSquare()  const
     
    366405{
    367406  return TESTBIT(fFlags,kExpFitOK);
     407}
     408
     409const Bool_t MHGausEvents::IsExcluded() const
     410{
     411  return TESTBIT(fFlags,kExcluded);
    368412}
    369413
     
    409453  if (fFExpFit)
    410454    return;
     455
     456  if (fEvents.GetSize() < 8)
     457    {
     458      *fLog << warn << "Cannot create Fourier spectrum in pixel: " << fPixId
     459            << ". Number of events smaller than 8 " << endl;
     460      return;
     461    }
     462 
    411463
    412464  //
     
    424476  // This cuts only the non-used zero's, but MFFT will later cut the rest
    425477  MArray::StripZeros(fEvents);
     478
     479  if (fEvents.GetSize() < 8)
     480    {
     481      *fLog << warn << "Cannot create Fourier spectrum. " << endl;
     482      *fLog << warn << "Number of events (after stripping of zeros) is smaller than 8 "
     483            << "in pixel: " << fPixId << endl;
     484      return;
     485    }
    426486
    427487  MFFT fourier;
     
    506566  if (!fFGausFit)
    507567    {
    508     *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit" << endl;
     568    *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit "
     569          << "in pixel: " << fPixId << endl;
    509570    return kFALSE;
    510571    }
     
    528589  // The fit result is accepted under condition:
    529590  // 1) The results are not nan's
    530   // 2) The NDF is not smaller than fNDFLimit (5)
    531   // 3) The Probability is greater than fProbLimit (default 0.001 == 99.9%)
     591  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
     592  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
    532593  //
    533594  if (   TMath::IsNaN(fMean)
     
    545606
    546607// -----------------------------------------------------------------------------
     608//
     609// If flag IsGausFitOK() is set (histogram already successfully fitted),
     610// returns kTRUE
     611//
     612// If both fMean and fSigma are still zero, call FitGaus()
     613//
     614// Repeats the Gauss fit in a smaller range, defined by:
     615//
     616// min = GetMean() - fPickupLimit * GetSigma();
     617// max = GetMean() + fPickupLimit * GetSigma();
     618//
     619// The fit results are retrieved and stored in class-own variables. 
     620//
     621// A flag IsGausFitOK() is set according to whether the fit probability
     622// is smaller or bigger than fProbLimit, whether the NDF is bigger than
     623// fNDFLimit and whether results are NaNs.
     624//
     625Bool_t MHGausEvents::RepeatFit(const Option_t *option)
     626{
     627
     628  if (IsGausFitOK())
     629    return kTRUE;
     630
     631  if ((fMean == 0.) && (fSigma == 0.))
     632    return FitGaus();
     633
     634  //
     635  // Get new fitting ranges
     636  //
     637  Axis_t rmin = fMean - fPickupLimit * fSigma;
     638  Axis_t rmax = fMean + fPickupLimit * fSigma;
     639
     640  Axis_t hmin = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetFirst());
     641  Axis_t hmax = fHGausHist.GetBinCenter(fHGausHist.GetXaxis()->GetLast()) ;
     642
     643  fFGausFit->SetRange(hmin < rmin ? rmin : hmin , hmax > rmax ? rmax : hmax);
     644
     645  fHGausHist.Fit(fFGausFit,option);
     646
     647  fMean     = fFGausFit->GetParameter(1);
     648  fMeanErr  = fFGausFit->GetParameter(2);
     649  fSigma    = fFGausFit->GetParError(1) ;
     650  fSigmaErr = fFGausFit->GetParError(2) ;
     651  fProb     = fFGausFit->GetProb()      ;     
     652
     653  //
     654  // The fit result is accepted under condition:
     655  // 1) The results are not nan's
     656  // 2) The NDF is not smaller than fNDFLimit (default: fgNDFLimit)
     657  // 3) The Probability is greater than fProbLimit (default: fgProbLimit)
     658  //
     659  if (   TMath::IsNaN ( fMean     )
     660      || TMath::IsNaN ( fMeanErr  )
     661      || TMath::IsNaN ( fProb     )   
     662      || TMath::IsNaN ( fSigma    )
     663      || TMath::IsNaN ( fSigmaErr )
     664      || fFGausFit->GetNDF() < fNDFLimit
     665      || fProb < fProbLimit )
     666    return kFALSE;
     667 
     668  SetGausFitOK(kTRUE);
     669  return kTRUE;
     670
     671}
     672
     673// -----------------------------------------------------------------------------
    547674//
    548675// Bypasses the Gauss fit by taking mean and RMS from the histogram
     
    559686  if (entries <= 0.)
    560687    {
    561       *fLog << warn << GetDescriptor() << ": Cannot bypass fit. Number of entries smaller or equal 0" << endl;
     688      *fLog << warn << GetDescriptor()
     689            << ": Cannot bypass fit. Number of entries smaller or equal 0 in pixel: " << fPixId << endl;
    562690      return;
    563691    }
    564692 
    565   SetMean     ( fHGausHist.GetMean() );
    566   SetMeanErr  ( fHGausHist.GetRMS() / TMath::Sqrt(entries) );
    567   SetSigma    ( fHGausHist.GetRMS() );
    568   SetSigmaErr ( fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2. );
     693  fMean     = fHGausHist.GetMean();
     694  fMeanErr  = fHGausHist.GetRMS() / TMath::Sqrt(entries);
     695  fSigma    = fHGausHist.GetRMS() ;
     696  fSigmaErr = fHGausHist.GetRMS() / TMath::Sqrt(entries) / 2.;
    569697}
    570698
     
    577705 
    578706  *fLog << all                                                        << endl;
    579   *fLog << all << "Results of the Gauss Fit: "                        << endl;
     707  *fLog << all << "Results of the Gauss Fit in pixel: " << fPixId     << endl;
    580708  *fLog << all << "Mean: "             << GetMean()                   << endl;
    581709  *fLog << all << "Sigma: "            << GetSigma()                  << endl;
     
    585713  *fLog << all                                                        << endl;
    586714 
     715}
     716
     717// --------------------------------------------------------------------------
     718//
     719// - Set fPixId to id
     720//
     721// Add id to names and titles of:
     722// - fHGausHist
     723//
     724void MHGausEvents::ChangeHistId(Int_t id)
     725{
     726
     727  fPixId = id;
     728
     729  fHGausHist.SetName(  Form("%s%d", fHGausHist.GetName(),  id));
     730  fHGausHist.SetTitle( Form("%s%d", fHGausHist.GetTitle(), id));
     731
     732  fName  = Form("%s%d", fName.Data(),  id);
     733  fTitle = Form("%s%d", fTitle.Data(), id);
     734
    587735}
    588736
     
    766914}
    767915
    768 
     916const Double_t MHGausEvents::GetPickup() const
     917{
     918 
     919  if ((fMean == 0.) && (fSigma == 0.))
     920    return -1.;
     921
     922  const Int_t first = fHGausHist.GetXaxis()->FindBin(fMean+fPickupLimit*fSigma);
     923  const Int_t last  = fHGausHist.GetXaxis()->GetLast();
     924
     925  if (first >= last)
     926    return 0.;
     927 
     928  return fHGausHist.Integral(first, last, "width");
     929}
     930
     931
Note: See TracChangeset for help on using the changeset viewer.