Re: fitting range, passing variables and scaling

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Nov 16 1998 - 14:15:06 MET


Alan Clayton Coleman wrote:
> 
> Hi ROOTers,
>     I am new to both C++ and ROOT. I have what hopefully are some
> simple questions and would request any assistance or direction. (i.e.,
> I can think of some painful ways to do the following)
> 
> 1) Is there a simple way to find the value of  the max and min bin
> with entries (bin # or "x"-value?) in a histogram?

Hi Alan,
You can find the maximum and minimum Y value of an histogram
via TH1::GetMaximum and TH1::GetMinimum.
We do not have  afunction returning the bin number corresponding
to the maximum or minimum. This could be added. Meanwhile,
you must loop yourself on the bins via TH1::GetBinContent.

Concerning your following questions, you will find in attachment
an example of a macro fitting a background + signal.
It also shows how to use a global variable.

Rene Brun

> 
> 2) It would be desirable to perform fits over the data in an iterative
> fashion, i.e., making differient cuts on a couple of the ntuple
> parameters and then fitting.  The fitting consists of a peak and
> background  function.  For single fits there are no problems as
> values required by the background function can be entered by hand.
> But, for multiple fits for different cut values it is not understood
> how these changing values can also be passed to the fitting functions.
> Is there a way to use global variables? In addition is there a way to
> define constant global values?  I.e.,  for  the latter it would be
> nice not to define the mass of a proton in every function.
> 
> 3) Initially the background function used gives only the shape (based
> on fitting parameters),  addition of an amplitude fit parameter in
> general distorts the meaning of amplitude. Is there an easy way to
> scale the background shape to 1 and  then multiply the amplitude fit
> parameter?
> 
>     Apologies for my ignorance and a great deal of thanks for any
> assistance.
> 
> --
> Alan Coleman
> colemana@cebaf.gov
> W&M Small Hall Rm 307
> (757) 221-3543
> TJNAF
> Bldg 16 Rm 14
> (757) 269-7541
> http://physics.wm.edu/~coleman/home.html
> "Lehrer sind Menschen, die uns helfen probleme zu loesen, die wir ohne sie nicht haetten."
> "Teachers are people who help us to solve problems that we would not have without them."
> 
>


// example of macro fitting background + signal
// -STEP 1: Generates theoretical function
// -STEP 2: Generates an histogram by sampling the function
// -STEP 3: estimates background parameters
// -STEP 4: estimates signal parameters
// -STEP 5: Combined fit of background + signal
//
   Double_t kTH = -0.5;

Double_t Background(Double_t *x, Double_t *par)
// The background function
{
   Double_t arg = 0;
   if (par[2]) arg = (x[0] - par[1])/par[2];

   Double_t val = par[0]*TMath::Exp(kTH*arg*arg)*x[0]*x[0];
   return val;
}

Double_t Signal(Double_t *x, Double_t *par)
// The signal function: a gaussian
{
   Double_t arg = 0;
   if (par[2]) arg = (x[0] - par[1])/par[2];

   Double_t sig = par[0]*TMath::Exp(-0.5*arg*arg);
   return sig;
}

Double_t Total(Double_t *x, Double_t *par)
// Combined background + signal
{
   Double_t tot = Background(x,par) + Signal(x,&par[3]);
   return tot;
}

void backsig()
{
   // the control function
   
   //STEP 1: Generates theoretical function
   Int_t npar = 6;
   Double_t params[6] = {100,3,1,350,6,0.5};
   TF1 *theory = new TF1("theory",Total,0,10,npar);
   theory->SetParameters(params);
   
   //STEP 2: Generates an histogram by sampling the theory function
   TH1F *Data = new TH1F("Data","Data sampled from theory",100,0,10);
   Data->FillRandom("theory",10000);
   
   //STEP 3: Estimates background parameters using a gaussian
   Data->Fit("gaus","q0");
   
   //STEP 4: Subtract estimated background to original data
   // Creates a temporary histogram and fit a gaussian
   TH1F *htemp = (TH1F*)Data->Clone();
   htemp->Reset();
   TF1 *eback = Data->GetFunction("gaus");
   for (Int_t bin=1;bin<=100;bin++) {
      Float_t x = Data->GetBinCenter(bin);
      Double_t fval = eback->Eval(x);
      Double_t diff = TMath::Abs(fval - Data->GetBinContent(bin));
      htemp->Fill(x,diff);
   }
   htemp->Fit("gaus","q0");
   TF1 *esig = htemp->GetFunction("gaus");
   
   //STEP 5: Fit background + signal
   eback->GetParameters(&params[0]);
   esig->GetParameters(&params[3]);
   Data->Fit("theory");
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:39 MET