ANALYSIS/TFndFitter.cxx

00001 // @(#)fROOT/ANALYSYS:$Name:  $:$Id: TFndFitter.cxx,v 1.11 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 11/05/07
00003 
00005 //                                                  //
00006 //                    TFndFitter                    //
00007 //                                                  //
00008 //  This class provides a general-purposes          //
00009 //  fit-environment, based on the FIN_AN namespace  //
00010 //                                                  //
00012 
00013 #include "TFndFitter.h"
00014 #include "TROOT.h"
00015 
00016 ClassImp(TFndFitter)
00017 
00018 //_______________________
00019 TFndFitter::TFndFitter():
00020   fCurHist(),fCurSpec(),
00021   fResolutionFactor(1.)
00022 {
00023   
00024 }
00025 
00026 //_______________________
00027 TFndFitter::~TFndFitter(){
00028   //   delete fCurHist;
00029   //   delete fCurSpec;
00030 }
00031 
00032 //_____________________
00033 Int_t TFndFitter::FitAllPeaks_GausYield(Double_t sigma_start,Double_t sigma_maxerr){
00034   // return value
00035   //             0: ok
00036   //            -1: at least one error
00037   
00038   Int_t retres = 0;
00039 
00040   Int_t n_found_peaks = fCurSpec->GetNPeaks();
00041   Printf("Found %d candidate peaks to fit\n",n_found_peaks);
00042 
00043   for(Int_t peak_num=1;peak_num<=n_found_peaks;peak_num++){
00044     Int_t fres = FitSinglePeak_GausYield(peak_num,sigma_start,sigma_maxerr);
00045     if (fres != 0) retres = -1;
00046   }
00047   
00048   return retres;
00049 }
00050 
00051 //_____________________
00052 Int_t TFndFitter::FitSinglePeak_GausYield(Int_t peak_num,Double_t sigma_init,Double_t sigma_maxerr){
00053   // this methods uses peaks already evaluated (by FIN_AN::FindPeaks)
00054   // return value (fit_result)
00055   //             0: ok
00056   //           !=0: see root-documentation  
00057   //          -999: error within this method
00058 
00059   if(!fCurSpec){
00060     Warning("FitSinglePeak","No spectrum selected: nothing will happen...");
00061     return -999;
00062   }
00063   
00064   Int_t n_found_peaks = fCurSpec->GetNPeaks();
00065   
00066   if(n_found_peaks==0){
00067     Info("FitSinglePeak","No peak found in current spectrum.");
00068     return -999;
00069   }
00070 
00071   if(peak_num > n_found_peaks){
00072     gROOT->Warning("FitSinglePeak","Peak number %d not available.",peak_num);
00073     return -999;
00074   }
00075   
00076   // note: peaks are evaluated from right to left by TSpectrum.
00077   Float_t peak_pos = (*(fCurSpec->GetPositionX() + peak_num - 1));
00078   Float_t peak_height = (*(fCurSpec->GetPositionY() + peak_num - 1 ));
00079   
00080 
00081   //  Printf("Now fitting peak number %d",peak_num);
00082   Double_t FitRange[2] = {0};
00083   FitRange[0] = peak_pos - (3./fResolutionFactor)*sigma_init;
00084   FitRange[1] = peak_pos + (3./fResolutionFactor)*sigma_init;
00085 
00086   TString curpknam = "FndPeak_GausYield"; curpknam+=peak_num;
00087   TF1 *cur_peak = new TF1(curpknam,FitFCN_GausYield,FitRange[0],FitRange[1],3);
00088 
00089   SetFitFCN_ParNames_GausYield(cur_peak);
00090 
00091   cur_peak->SetParameter(0,peak_height * (sigma_init/peak_pos));
00092   cur_peak->SetParameter(1,peak_pos);
00093   cur_peak->SetParameter(2,sigma_init);
00094 
00095   cur_peak->SetParLimits(1,peak_pos - sigma_init,peak_pos + sigma_init);
00096   cur_peak->SetParLimits(2,
00097                          (sigma_init > sigma_maxerr) ? (sigma_init-sigma_maxerr) : 1e-10,
00098                          sigma_init+sigma_maxerr);
00099   
00100   Int_t fit_res = fCurHist->Fit(curpknam,"RQ+");
00101   if( fit_res != 0 ) Printf("Fit error: %d",fit_res);
00102   
00103   delete cur_peak;
00104   cur_peak = 0;
00105 
00106   return fit_res;
00107 }
00108 
00109 //_____________________
00110 void TFndFitter::PrintFittedPeaks_GausYield(){
00111   
00112   if(!fCurHist){
00113     Warning("PrintFittedPeaks_GausYield","No histogram selected: nothing will happen...");
00114     return;
00115   }
00116 
00117   TIter next(fCurHist->GetListOfFunctions());
00118   TObject *obj;
00119   
00120   while ((obj = next())) {
00121     TString objnam = obj->GetName();
00122     if( objnam.BeginsWith("FndPeak_GausYield") == kFALSE) continue;
00123   
00124     TF1 *cur_peak = (TF1 *)obj;
00125     // ---
00126     TString resmsg = "";
00127     TString resline = "";
00128     resline.Form("\nFit Results (peak name: \"%s\"):\n",cur_peak->GetName());
00129     resmsg+=resline;
00130     
00131     for(Int_t i=0;i<3;i++){
00132       resline.Form("\t-> %s:\t%.4f\n",cur_peak->GetParName(i),cur_peak->GetParameter(i));
00133       resmsg+=resline;
00134     }
00135     Double_t Area = cur_peak->GetParameter(0) / fCurHist->GetBinWidth(1);
00136     Double_t Area_68 = Area * 0.683;
00137     Double_t Area_95 = Area * 0.9545;
00138     Double_t Area_99 = Area * 0.997;
00139     resline.Form("\t-> area:\t%.4f  (total)\n",Area);  resmsg+=resline;
00140     resline.Form("\t-> area:\t%.4f  (C.L: 68% [+- 1 sigma])\n",Area_68);  resmsg+=resline;
00141     resline.Form("\t-> area:\t%.4f  (C.L: 95% [+- 2 sigma])\n",Area_95);  resmsg+=resline;
00142     resline.Form("\t-> area:\t%.4f  (C.L: 99% [+- 3 sigma])\n",Area_99);  resmsg+=resline;
00143 
00144     Printf(resmsg.Data());
00145     // ---
00146   }
00147   
00148 }
00149 
00150 //_____________________
00151 TF1 *TFndFitter::LocateFittedPeak_GausYield(Double_t low_lim,Double_t high_lim){
00152   // 
00153 
00154   if(!fCurHist){
00155     Warning("LocateFittedPeaks_GausYield","No histogram selected: nothing will happen...");
00156     return 0;
00157   }
00158 
00159   TIter next(fCurHist->GetListOfFunctions());
00160   TObject *obj;
00161  
00162   TF1 *cur_peak = 0;
00163   while ((obj = next())) {
00164     TString objnam = obj->GetName();
00165     if( objnam.BeginsWith("FndPeak_GausYield") == kFALSE) continue;
00166     
00167     cur_peak = (TF1 *)obj;
00168     Double_t cur_avg = cur_peak->GetParameter(1);
00169     if(cur_avg >= low_lim && cur_avg <= high_lim){
00170       //Printf("LocateFittedPeak_GausYield: peak found at pos: %.4f ",cur_avg);
00171       return cur_peak;
00172     }    
00173   }
00174 
00175   Printf("LocateFittedPeak_GausYield: peak not found in range [%.4f ; %.4f]",low_lim,high_lim);
00176   return 0;
00177 }

Generated on Tue Oct 16 15:40:46 2007 by  doxygen 1.5.2