[ROOT] x error bars and fitting

From: Nicolas de Sereville (deserevi@csnsm.in2p3.fr)
Date: Fri Sep 13 2002 - 13:35:08 MEST


Hi Rooters,

I found a strange behavior while fitting TGraphErrors objects. When I fit
my data with a predefined function (let's say "pol1") I can see that the
fit / chisquare is different if I switch on/off the error bars along x
(ex). So in this case it behaves as I expect.

If now I use my user defined function, I can't see anymore this feature
(see macro at the end. I also attached the file used for defining my
function). I guess I am doing something wrong but I cannot figure out what
it is.

any help is welcome, thanks

Nicolas

P.S : I am using root 3.02/5 on RH7.1, and I see the same behavior with
root 3.00/06 on HP-UX


*************
macro chisq.C
*************

Graph *GR;
Int_t DIM;
Double_t *XX, *YY;

void chisq()
{
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1);

   // on dessine un canvas
   TCanvas *c1 = new TCanvas("c1", "c1");
   c1->SetGrid();

   // on dessine les axes
   TH2F *h2 = new TH2F("h2", "test fit", 300, 2.65, 2.76, 1000, 0, 100);
   h2->SetXTitle("Energie (MeV)");
   h2->Draw();

   // on creer les donnees
   Double_t x[4]        = {2.693, 2.7, 2.7, 2.708};
   Double_t ex[4]       = {0.002, 0.002, 0.002, 0.002};
   Double_t y[4]        = {64.02073, 61.66537, 64.23304, 33.05851};
   Double_t ey[4]       = {2.75809, 1.91538 ,1.89935, 1.26583};

   // on remplit un TGraphErrors
   TGraphErrors *data = new TGraphErrors(4, x, y, ex, ey);
//   TGraphErrors *data = new TGraphErrors(4, x, y, 0, ey);
   data->SetMarkerStyle(22);
   data->SetMarkerColor(kRed);
   data->SetLineColor(kRed);
   data->Draw("P");

   // on lit le fichier contenant la fonction a fitter
   GR = read("reson_n02.out");
   DIM = GR->GetN();
   XX = GR->GetX();
   YY = GR->GetY();

   // on definit la fonction du fit
   TF1 *fitf = new TF1("fitf", fitf, XX[0], XX[DIM-1], 1);
   fitf->SetParameter(0, 1.1);
   fitf->SetParName(0, "N");

   // on fait le fit
   data->Fit("fitf", "RV+");
//   data->Fit("pol1");
}



TGraph* read(TString name)
{
   // on construit le nom du fichier a lire
   TString path = "/home/astronuc/deserevi/";
   path += name;

   // ouverture du fichier de donnees
   ifstream fich;
   fich.open(path);
   if ( !fich )
     cout << "Probleme dans l'ouverture du fichier : " << path << endl;

   // saute les 'comm' premieres lignes de commentaires
   Int_t comm = 0;
   Char_t line[256];
   for ( Int_t i = 0; i < comm; i++ ) fich.getline(line, sizeof(line));

//   TGraphErrors *gre = new TGraphErrors();
   TGraph *gre = new TGraph();
   Int_t nlines = 0;
   Float_t x, y;
   // lecture du fichier de donnees
   while ( fich >> x >> y ) gre->SetPoint(nlines++, x, y);

   return gre;
}


Double_t fitf(Double_t *x, Double_t *par)
{
   Int_t index = TMath::BinarySearch(DIM, XX, x[0]);

   // interpolation lineaire
   Double_t fitval = ((YY[index] - YY[index+1]) * x[0]
                      + (XX[index]*YY[index+1] - XX[index+1]*YY[index]))
                      / (XX[index] - XX[index+1]);
   return par[0] * fitval;
}

-- 
:-------------------------------:
:     Nicolas DE SEREVILLE      :
:         C.S.N.S.M.            :
:        Batiment 104           :
:     91405 ORSAY Campus        :
: Tel : (+ 33) (0)1 69 15 48 57 :
: Fax : (+ 33) (0)1 69 15 50 08 :
:-------------------------------:





This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:09 MET