mcr/analysis/PeakFinder.C

00001 
00002 //                                             //
00003 // This macro is an example for using the      //
00004 //  most important facilities provided by the  //
00005 //  FIN_AN namespace:                          //
00006 //    (see $FROOTSYS/ANALYSIS/FIN_AN.h)        //
00007 //                                             //
00008 // Peak fitting is also supported by the       //
00009 //  TFndFitter class                           //
00010 //                                             //
00012 
00013 #include "Riostream.h"
00014 #include "TSystem.h"
00015 #include "TStyle.h"
00016 #include "TCanvas.h"
00017 #include "TH1F.h"
00018 #include "TRandom.h"
00019 #include "TThread.h"
00020 
00021 #include "FIN_AN.h"
00022 
00023 #include "HistogramCreators.C"
00024 using namespace FIN_AN;
00025 
00026 Int_t fNFoundPeaks = 0;// evaluated number of peaks (TSpectrum search is performed)
00027 
00028 Double_t fResolutionFactor = 1;
00029 // fResolutionFactor see the "FIN_AN" namespace
00030 //
00031 Float_t fPeakPos[FinAn_Nmaxpeaks];
00032 Float_t fPeakHeight[FinAn_Nmaxpeaks];
00033 
00034 
00035 //_____________________
00036 void FitPeak(TH1F *his,Int_t peak_id,Double_t sigma_init,Double_t sigma_maxerr){
00037   // this methods uses peaks already evaluated (by FIN_AN::FindPeaks)
00038 
00039   Printf("Now fitting peaks (id:%d)",peak_id);
00040   Double_t FitRange[2] = {0};
00041   FitRange[0] = fPeakPos[peak_id] - (3./fResolutionFactor)*sigma_init;
00042   FitRange[1] = fPeakPos[peak_id] + (3./fResolutionFactor)*sigma_init;
00043 
00044   TString curpknam = "TestPeak_"; curpknam+=peak_id+1;
00045   TF1 *cur_peak = new TF1(curpknam,FitFCN_GausYield,FitRange[0],FitRange[1],3);
00046 
00047   SetFitFCN_ParNames_GausYield(cur_peak);
00048 
00049   cur_peak->SetParameter(0,fPeakHeight[peak_id]*(sigma_init/fPeakPos[peak_id]));
00050   cur_peak->SetParameter(1,fPeakPos[peak_id]);
00051   cur_peak->SetParameter(2,sigma_init);
00052 
00053   cur_peak->SetParLimits(1,fPeakPos[peak_id]-sigma_init,fPeakPos[peak_id]+sigma_init);
00054   cur_peak->SetParLimits(2,
00055                          (sigma_init > sigma_maxerr) ? (sigma_init-sigma_maxerr) : 1e-10,
00056                          sigma_init+sigma_maxerr);
00057   
00058   Int_t fit_res = his->Fit(curpknam,"RQ+");
00059   if( fit_res != 0 ) cout << "Fit error: " << fit_res << endl;
00060   
00061   delete cur_peak;
00062   cur_peak = 0;
00063 
00064 }
00065 
00066 //_____________________
00067 void FitAllPeaks(TH1F *his,Double_t sigma_start,Double_t sigma_maxerr){
00068   
00069   for(Int_t pi=0;pi<fNFoundPeaks;pi++){
00070     FitPeak(his,pi,sigma_start,sigma_maxerr);
00071   }
00072   
00073 }
00074 
00075 //_____________________
00076 void PrintFittedPeaks(TH1F *his){
00077   
00078   TIter next(his->GetListOfFunctions());
00079   TObject *obj;
00080   
00081   TString strchk = "TestPeak";
00082   while ((obj = next())) {
00083     TString objnam = obj->GetName();
00084     objnam.Resize(8);
00085     if( strchk.CompareTo(objnam) ) continue;
00086   
00087     TF1 *cur_peak = (TF1 *)obj;
00088     // ---
00089     TString resmsg = "\nFit Results:\n";
00090     TString resline = "";
00091     for(Int_t i=0;i<3;i++){
00092       resline.Form("\t-> %s:\t%.4f\n",cur_peak->GetParName(i),cur_peak->GetParameter(i));
00093       resmsg+=resline;
00094     }
00095     Double_t Area = cur_peak->GetParameter(0) / his->GetBinWidth(1);
00096     Double_t Area_68 = Area * 0.683;
00097     Double_t Area_95 = Area * 0.9545;
00098     Double_t Area_99 = Area * 0.997;
00099     resline.Form("\t-> area:\t%.4f  (total)\n",Area);  resmsg+=resline;
00100     resline.Form("\t-> area:\t%.4f  (C.L: 68% [+- 1 sigma])\n",Area_68);  resmsg+=resline;
00101     resline.Form("\t-> area:\t%.4f  (C.L: 95% [+- 2 sigma])\n",Area_95);  resmsg+=resline;
00102     resline.Form("\t-> area:\t%.4f  (C.L: 99% [+- 3 sigma])\n",Area_99);  resmsg+=resline;
00103 
00104     
00105     Printf(resmsg.Data());
00106     // ---
00107   }
00108   
00109 }
00110 
00111 //_____________________
00112 void LoadPeaks(TSpectrum *TmpSpectrum){
00113   
00114   fNFoundPeaks = TmpSpectrum->GetNPeaks();
00115 
00116   Printf("Found %d candidate peaks to fit\n",fNFoundPeaks);
00117   for(Int_t pi=0;pi<fNFoundPeaks;pi++){
00118     // note: peaks are evaluated from right to left by TSpectrum.
00119     Int_t peakid = fNFoundPeaks - pi -1;
00120     fPeakPos[pi] = (*(TmpSpectrum->GetPositionX() + peakid));
00121     fPeakHeight[pi] = (*(TmpSpectrum->GetPositionY() + peakid));
00122   }
00123 }
00124 
00125 //_____________________
00126 void PrintFoundPeaks(){
00127   TString msg = "\nPeaks position: \n";
00128   TString linestr ="";
00129   for(Int_t pi=0;pi<fNFoundPeaks;pi++){
00130     linestr.Form("Peak number %d: position: %.4f ; height: %.4f \n",
00131                  pi+1,
00132                  fPeakPos[pi],
00133                  fPeakHeight[pi]
00134                  );
00135     msg+=linestr;
00136   }
00137   Printf(msg.Data());
00138 }
00139 
00143 
00144 void PeakFinder(){
00145   gStyle->SetOptStat(1001111);
00146 
00147   // --- peaks simulation
00148   Int_t n_simul_peaks=7;
00149   Double_t simul_sigma=0.01;
00150   Double_t sig_noise_ratio=2.;
00151      
00152   fResolutionFactor = 1; // must be >= 1 (1 means 3sigma)
00153   Bool_t eval_background = kTRUE;
00154   Int_t n_iter_background = 50;
00155 
00156   // --- peaks evaluation
00157   Int_t n_max_expected_peaks = 10;
00158   Double_t expected_sigma = 0.015;
00159   Double_t sigma_maxerr=0.005;
00160   Double_t peaks_threshold = 0.3;
00161 
00162   TH1F *start_his = BuildExampleHistogram(n_simul_peaks,simul_sigma,sig_noise_ratio,kTRUE);
00163   
00164   //  TH1F *start_his = new TH1F("starthis","starthis",3000,0,1);
00165   //   for(Int_t i=0;i<20000;i++)  start_his->Fill(gRandom->Gaus(.5,.02) );
00166   
00167   
00168   // --- evaluate background
00169   TH1F *fBckHis = 0;
00170   if(eval_background) fBckHis = EvalBackground(start_his,n_max_expected_peaks,n_iter_background);
00171 
00172   // --- check peaks before subtracting background
00173   LoadPeaks( FindPeaks(start_his,n_max_expected_peaks,expected_sigma,peaks_threshold,fResolutionFactor,"") );
00174   UpdateDisplay(start_his,fBckHis);
00175   PrintFoundPeaks();
00176   //  
00177 
00178   // --- subtract background (if required)
00179   TH1F *clean_his = (TH1F*)(start_his->Clone("CloneHis") );
00180   clean_his->SetTitle("Clean histogram (simple clone)");
00181   if(fBckHis){
00182     Printf("Now evaluating clean histogram (subtracting background)...");
00183     clean_his->SetName("CleanHis");
00184     clean_his->SetTitle("Clean histogram");
00185     clean_his->Add(fBckHis,-1);
00186     Printf("...clean histogram evaluated");
00187   }
00188   //  
00189 
00190   // --- check peaks after subtracting background
00191   LoadPeaks( FindPeaks(clean_his,n_max_expected_peaks,expected_sigma,peaks_threshold,fResolutionFactor,"") );
00192   UpdateDisplay(start_his,fBckHis,clean_his);
00193   
00194   // --- perform fit for every peak
00195   FitAllPeaks(clean_his,expected_sigma,sigma_maxerr);
00196   Printf("...peak fit completed... :)");
00197   PrintFittedPeaks(clean_his);
00198 }

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