[ROOT] Re: Poisson fitting

From: Stanislav Nesterov (qleap@pnpi.spb.ru)
Date: Tue Jan 28 2003 - 08:12:11 MET


dennis@physics.msuiit.edu.ph wrote:

>Hi rooters!
>
>How can i use a user-defined function such as Poisson for fitting a
>histogram? I tried using myfit.C of the tutorials but encountered
>problems. I hope someone could give an example. This would be a great
>help.
>
>Thanks
>
>
>Dennis Arogancia
>MSU-ILigan City
>
>
>  
>
Hi Dennis,

It is very simple: exactly like in tutorial examples you should create 
wrapper function and put it in TF1 object.

See example below (or attachment) for Poisson distribution:

Double_t 
func(Double_t*x,Double_t*par)                                         
{                                                                              
  Double_t res=0., 
xx=x[0];                                                    
  if (xx<=0) return 
0;                                                         
  // Poisson 
distribution                                                      
  // par[1] - distribution 
parameter                                           
  return 
par[0]*TMath::Power(par[1],xx)/TMath::Gamma(xx+1)/TMath::Exp(par[1]); 
}                                                                              
                                                                               
void 
poisson()                                                                 
{                                                                              
   TF1 pois("pois",func,0,10,2); // x in [0;10], 2 
parameters                  
   
pois->SetParName(0,"Const");                                                
   
pois->SetParName(1,"#mu");                                                  
   // Create histogram with poisson 
distribution                               
   TH1F*testhi = new TH1F("testhi","Poisson 
distribution",100,0,5);            
   
pois->SetParameter(0,3.75654);                                              
   
pois->SetParameter(1,2.95437);                                              
   
testhi->FillRandom("pois",20000);                                           
                                                                               
   // Fitting 
it                                                               
   pois->SetParameter(0,1);  // not the best 
shots                             
                            // 'cause we fill with 20000 
events                
                            // so constant will be in 1000 times 
bigger        
   pois->SetParameter(1,1);  // 
:)                                             
   TCanvas *cc = new 
TCanvas("cc","Canvas",600,600);                           
   
gStyle->SetOptStat(0);                                                      
   gStyle->SetOptFit(111);
  
 testhi->Fit("pois");                                                        
   
testhi->Draw();                                                             
}

                                                Best reagards,
                                                                   
 Stanislav.  


Double_t func(Double_t*x,Double_t*par)
{
  Double_t res=0., xx=x[0];
  if (xx<=0) return 0;
  // Poisson distribution
  // par[1] - distribution parameter 
  return par[0]*TMath::Power(par[1],xx)/TMath::Gamma(xx+1)/TMath::Exp(par[1]);
}

void poisson()
{
   TF1 pois("pois",func,0,10,2); // x in [0;10], 2 parameters
   pois->SetParName(0,"Const");
   pois->SetParName(1,"#mu");
   // Create histogram with poisson distribution
   TH1F*testhi = new TH1F("testhi","Poisson distribution",100,0,5);
   pois->SetParameter(0,3.75654);
   pois->SetParameter(1,2.95437);
   testhi->FillRandom("pois",20000);
   
   // Fitting it 
   pois->SetParameter(0,1);  // not the best shots 
			    // 'cause we fill with 20000 events
			    // so constant will be in 1000 times bigger 
   pois->SetParameter(1,1);  // :)
   TCanvas *cc = new TCanvas("cc","Canvas",600,600);
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(111);
   testhi->Fit("pois");
   testhi->Draw();
}



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:08 MET