fitLinear2.C

Go to the documentation of this file.
00001 #include "TLinearFitter.h"
00002 #include "TF1.h"
00003 #include "TRandom.h"
00004 
00005 void fitLinear2()
00006 {
00007    //Fit a 5d hyperplane by n points, using the linear fitter directly
00008 
00009    //This macro shows some features of the TLinearFitter class
00010    //A 5-d hyperplane is fit, a constant term is assumed in the hyperplane
00011    //equation (y = a0 + a1*x0 + a2*x1 + a3*x2 + a4*x3 + a5*x4)
00012    //Author: Anna Kreshuk
00013    
00014    Int_t n=100;
00015    Int_t i;
00016    TRandom rand;
00017    TLinearFitter *lf=new TLinearFitter(5);
00018 
00019    //The predefined "hypN" functions are the fastest to fit
00020    lf->SetFormula("hyp5"); 
00021 
00022    Double_t *x=new Double_t[n*10*5];
00023    Double_t *y=new Double_t[n*10];
00024    Double_t *e=new Double_t[n*10];
00025 
00026    //Create the points and put them into the fitter
00027    for (i=0; i<n; i++){
00028       x[0 + i*5] = rand.Uniform(-10, 10);
00029       x[1 + i*5] = rand.Uniform(-10, 10);
00030       x[2 + i*5] = rand.Uniform(-10, 10);                 
00031       x[3 + i*5] = rand.Uniform(-10, 10);
00032       x[4 + i*5] = rand.Uniform(-10, 10);
00033       e[i] = 0.01;
00034       y[i] = 4*x[0+i*5] + x[1+i*5] + 2*x[2+i*5] + 3*x[3+i*5] + 0.2*x[4+i*5]  + rand.Gaus()*e[i];
00035    }
00036 
00037    //To avoid copying the data into the fitter, the following function can be used:
00038    lf->AssignData(n, 5, x, y, e);
00039    //A different way to put the points into the fitter would be to use
00040    //the AddPoint function for each point. This way the points are copied and stored
00041    //inside the fitter
00042 
00043    //Perform the fitting and look at the results
00044    lf->Eval();
00045    TVectorD params;
00046    TVectorD errors;
00047    lf->GetParameters(params);
00048    lf->GetErrors(errors);
00049    for (Int_t i=0; i<6; i++)
00050       printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
00051    Double_t chisquare=lf->GetChisquare();
00052    printf("chisquare=%f\n", chisquare);
00053 
00054 
00055    //Now suppose you want to add some more points and see if the parameters will change
00056    for (i=n; i<n*2; i++) {
00057       x[0+i*5] = rand.Uniform(-10, 10);
00058       x[1+i*5] = rand.Uniform(-10, 10);
00059       x[2+i*5] = rand.Uniform(-10, 10);           
00060       x[3+i*5] = rand.Uniform(-10, 10);
00061       x[4+i*5] = rand.Uniform(-10, 10);
00062       e[i] = 0.01;
00063       y[i] = 4*x[0+i*5] + x[1+i*5] + 2*x[2+i*5] + 3*x[3+i*5] + 0.2*x[4+i*5]  + rand.Gaus()*e[i];
00064    }
00065 
00066    //Assign the data the same way as before
00067    lf->AssignData(n*2, 5, x, y, e);   
00068    lf->Eval();
00069    lf->GetParameters(params);
00070    lf->GetErrors(errors);
00071    printf("\nMore Points:\n");
00072    for (Int_t i=0; i<6; i++)
00073       printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
00074    chisquare=lf->GetChisquare();
00075    printf("chisquare=%.15f\n", chisquare);
00076 
00077 
00078    //Suppose, you are not satisfied with the result and want to try a different formula
00079    //Without a constant:
00080    //Since the AssignData() function was used, you don't have to add all points to the fitter again
00081    lf->SetFormula("x0++x1++x2++x3++x4");
00082 
00083    lf->Eval();
00084    lf->GetParameters(params);
00085    lf->GetErrors(errors);
00086    printf("\nWithout Constant\n");
00087    for (Int_t i=0; i<5; i++)
00088      printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
00089    chisquare=lf->GetChisquare();
00090    printf("chisquare=%f\n", chisquare);
00091 
00092    //Now suppose that you want to fix the value of one of the parameters
00093    //Let's fix the first parameter at 4:
00094    lf->SetFormula("hyp5");
00095    lf->FixParameter(1, 4);
00096    lf->Eval();
00097    lf->GetParameters(params);
00098    lf->GetErrors(errors);
00099    printf("\nFixed Constant:\n");
00100    for (i=0; i<6; i++)
00101       printf("par[%d]=%f+-%f\n", i, params(i), errors(i));
00102    chisquare=lf->GetChisquare();
00103    printf("chisquare=%.15f\n", chisquare);
00104    
00105    //The fixed parameters can then be released by the ReleaseParameter method
00106    delete lf;
00107 
00108 }
00109 

Generated on Tue Jul 5 15:44:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1