multidimfit.C

Go to the documentation of this file.
00001 //Multi-Dimensional Parametrisation and Fitting
00002 //Authors: Rene Brun, Christian Holm Christensen
00003    
00004 #include "Riostream.h"
00005 #include "TROOT.h"
00006 #include "TApplication.h"
00007 #include "TCanvas.h"
00008 #include "TH1.h"
00009 #include "TSystem.h"
00010 #include "TBrowser.h"
00011 #include "TFile.h"
00012 #include "TRandom.h"
00013 #include "TMultiDimFit.h"
00014 #include "TVectorD.h"
00015 #include "TMath.h"
00016 
00017 //____________________________________________________________________
00018 void makeData(Double_t* x, Double_t& d, Double_t& e) 
00019 {
00020   // Make data points 
00021   Double_t upp[5] = { 10, 10, 10, 10,  1 };
00022   Double_t low[5] = {  0,  0,  0,  0, .1 };
00023   for (int i = 0; i < 4; i++) 
00024     x[i] = (upp[i] - low[i]) * gRandom->Rndm() + low[i]; 
00025   
00026   d = x[0] * TMath::Sqrt(x[1] * x[1] + x[2] * x[2] + x[3] * x[3]);
00027   
00028   e = gRandom->Gaus(upp[4],low[4]);
00029 }
00030 
00031 //____________________________________________________________________
00032 int CompareResults(TMultiDimFit *fit)
00033 { 
00034    //Compare results with reference run
00035    
00036    // the right coefficients
00037   double GoodCoeffs[] = {
00038   -4.37056,
00039   43.1468,
00040   13.432,
00041   13.4632,
00042   13.3964,
00043   13.328,
00044   13.3016,
00045   13.3519,
00046   4.49724,
00047   4.63876,
00048   4.89036,
00049   -3.69982,
00050   -3.98618,
00051   -3.86195,
00052   4.36054,
00053   -4.02597,
00054   4.57037,
00055   4.69845,
00056   2.83819,
00057   -3.48855,
00058   -3.97612
00059 };
00060 
00061 // Good Powers
00062   int GoodPower[] = {
00063   1,  1,  1,  1,
00064   2,  1,  1,  1,
00065   1,  1,  1,  2,
00066   1,  1,  2,  1,
00067   1,  2,  1,  1,
00068   2,  2,  1,  1,
00069   2,  1,  1,  2,
00070   2,  1,  2,  1,
00071   1,  1,  1,  3,
00072   1,  3,  1,  1,
00073   1,  1,  5,  1,
00074   1,  1,  2,  2,
00075   1,  2,  1,  2,
00076   1,  2,  2,  1,
00077   2,  1,  1,  3,
00078   2,  2,  1,  2,
00079   2,  1,  3,  1,
00080   2,  3,  1,  1,
00081   1,  2,  2,  2,
00082   2,  1,  2,  2,
00083   2,  2,  2,  1
00084 };
00085 
00086   Int_t nc = fit->GetNCoefficients();
00087   Int_t nv = fit->GetNVariables();
00088   const Int_t *powers = fit->GetPowers();
00089   const Int_t *pindex = fit->GetPowerIndex();
00090   if (nc != 21) return 1;
00091   const TVectorD *coeffs = fit->GetCoefficients();
00092   int k = 0;
00093   for (Int_t i=0;i<nc;i++) {
00094      if (TMath::Abs((*coeffs)[i] - GoodCoeffs[i]) > 5e-5) return 2;
00095      for (Int_t j=0;j<nv;j++) {
00096         if (powers[pindex[i]*nv+j] != GoodPower[k]) return 3;
00097         k++;
00098      }
00099   }
00100   
00101   // now test the result of the generated function
00102   gROOT->ProcessLine(".L MDF.C");
00103   Double_t x[]    = {5,5,5,5};
00104   Double_t refMDF = 43.98;
00105   Double_t rMDF   = MDF(x);
00106   if (TMath::Abs(rMDF -refMDF) > 1e-2) return 4;
00107   return 0;     
00108 }
00109 
00110 //____________________________________________________________________
00111 Int_t multidimfit() 
00112 {
00113 
00114   cout << "*************************************************" << endl; 
00115   cout << "*             Multidimensional Fit              *" << endl;
00116   cout << "*                                               *" << endl;
00117   cout << "* By Christian Holm <cholm@nbi.dk> 14/10/00     *" << endl;
00118   cout << "*************************************************" << endl; 
00119   cout << endl;
00120 
00121   // Initialize global TRannom object. 
00122   gRandom = new TRandom();
00123 
00124   // Open output file 
00125   TFile* output = new TFile("mdf.root", "RECREATE");
00126   
00127   // Global data parameters 
00128   Int_t nVars       = 4;
00129   Int_t nData       = 500;
00130   Double_t x[4];
00131 
00132   // make fit object and set parameters on it. 
00133   TMultiDimFit* fit = new TMultiDimFit(nVars, TMultiDimFit::kMonomials,"v");
00134 
00135   Int_t mPowers[]   = { 6 , 6, 6, 6 };
00136   fit->SetMaxPowers(mPowers);
00137   fit->SetMaxFunctions(1000);
00138   fit->SetMaxStudy(1000);
00139   fit->SetMaxTerms(30);
00140   fit->SetPowerLimit(1);
00141   fit->SetMinAngle(10);
00142   fit->SetMaxAngle(10);
00143   fit->SetMinRelativeError(.01);
00144 
00145   // variables to hold the temporary input data 
00146   Double_t d;
00147   Double_t e;
00148   
00149   // Print out the start parameters
00150   fit->Print("p");
00151 
00152   // Create training sample 
00153   Int_t i;
00154   for (i = 0; i < nData ; i++) {
00155 
00156     // Make some data 
00157     makeData(x,d,e);
00158 
00159     // Add the row to the fit object
00160     fit->AddRow(x,d,e);
00161   }
00162 
00163   // Print out the statistics
00164   fit->Print("s");
00165 
00166   // Book histograms 
00167   fit->MakeHistograms();
00168 
00169   // Find the parameterization 
00170   fit->FindParameterization();
00171 
00172   // Print coefficents 
00173   fit->Print("rc");
00174 
00175   // Get the min and max of variables from the training sample, used
00176   // for cuts in test sample. 
00177   Double_t *xMax = new Double_t[nVars];
00178   Double_t *xMin = new Double_t[nVars];
00179   for (i = 0; i < nVars; i++) {
00180     xMax[i] = (*fit->GetMaxVariables())(i);
00181     xMin[i] = (*fit->GetMinVariables())(i);
00182   }
00183 
00184   nData = fit->GetNCoefficients() * 100;
00185   Int_t j;
00186 
00187   // Create test sample 
00188   for (i = 0; i < nData ; i++) {
00189     // Make some data 
00190     makeData(x,d,e);
00191 
00192     for (j = 0; j < nVars; j++) 
00193       if (x[j] < xMin[j] || x[j] > xMax[j])
00194         break;
00195 
00196     // If we get through the loop above, all variables are in range 
00197     if (j == nVars)  
00198       // Add the row to the fit object
00199       fit->AddTestRow(x,d,e);
00200     else
00201       i--;
00202   }
00203   //delete gRandom;
00204 
00205   // Test the parameterizatio and coefficents using the test sample. 
00206   fit->Fit();
00207 
00208   // Print result 
00209   fit->Print("fc");
00210 
00211   // Write code to file 
00212   fit->MakeCode();
00213 
00214   // Write histograms to disk, and close file 
00215   output->Write();
00216   output->Close();
00217   delete output;
00218   
00219   // Compare results with reference run
00220   Int_t compare = CompareResults(fit);
00221   if (!compare) {
00222      printf("\nmultidimfit ..............................................  OK\n");
00223   } else {
00224      printf("\nmultidimfit ..............................................  fails case %d\n",compare);
00225   }
00226 
00227   // We're done 
00228   delete fit;
00229   return compare;
00230 }

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