コード例 #1
0
ファイル: GEMAlignment.C プロジェクト: TomLiu2016/CSCUCLA
  void solve(TString outFileName){
    ROOT::Math::Minimizer* min = ROOT::Math::Factory::CreateMinimizer("Minuit2", "MIGRAD");
    min->SetMaxFunctionCalls(1000000);
    min->SetTolerance(0.001);
    min->SetPrintLevel(10);

        ROOT::Math::Functor f(&totalDist,4);
    double step[4] = {0.01,0.01,.01,.01};
    double variable[4] = { 0,0,50,0};
    min->SetFunction(f);
    min->SetVariable(0,"x"  ,variable[0], step[0]);
    min->SetVariable(1,"y"  ,variable[1], step[1]);
    min->SetVariable(2,"z"  ,variable[2], step[2]);
    min->SetVariable(3,"phi",variable[3], step[3]);

       // do the minimization
       min->Minimize();

       const double *xs = min->X();
       std::cout << "Minimum: f(" << xs[0] << "," << xs[1]<< "," << xs[2]<< "," << xs[3] << "): "
                 << min->MinValue()  << std::endl;


       setZ=xs[2];

       ROOT::Math::Minimizer* min2 = ROOT::Math::Factory::CreateMinimizer("Minuit2", "MIGRAD");
       min2->SetMaxFunctionCalls(1000000);
       min2->SetTolerance(0.001);
       min2->SetPrintLevel(10);

           ROOT::Math::Functor f2(&totalDistWithoutZ,3);
       double step2[3] = {0.01,0.01,.01};
       double variable2[3] = { xs[0],xs[1],xs[3]};
       min2->SetFunction(f2);
       min2->SetVariable(0,"x"  ,variable2[0], step2[0]);
       min2->SetVariable(1,"y"  ,variable2[1], step2[1]);
       min2->SetVariable(2,"phi",variable2[2], step2[2]);

          // do the minimization
          min2->Minimize();

          const double *xs2 = min2->X();
          std::cout << "Minimum: f(" << xs2[0] << "," << xs2[1]<< "," << xs2[2] << "): "
                    << min2->MinValue()  << std::endl;




//       // expected minimum is 0
//       if ( min->MinValue()  < 1.E-4  && f(xs) < 1.E-4)
//          std::cout << "   converged to the right minimum" << std::endl;
//       else {
//          std::cout << "   failed to converge !!!" << std::endl;
//          Error("NumericalMinimization","fail to converge");
//       }
  }
コード例 #2
0
ファイル: bootstrap.c プロジェクト: SFUNUSC/FileConvTools
void find_chisqMin()
{
  chisq=0.;
  for(int i=0; i<numSpectra; i++)
    {
      // for more information see minimizer class documentation
      // https://root.cern.ch/root/html/ROOT__Math__Minimizer.html
      char minName[132] = "Minuit";
      char algoName[132] = ""; // default conjugate gradient type method
      ROOT::Math::Minimizer* min = ROOT::Math::Factory::CreateMinimizer(minName, algoName);
  
      // set tolerance , etc...
      min->SetMaxFunctionCalls(1000000); // for Minuit
      min->SetMaxIterations(10000);      // for GSL 
      min->SetTolerance(0.001);
      min->SetPrintLevel(0);             // set to 1 for more info
      
      // communication to the likelihood ratio function
      // (via global parameters)
      for(int j=0; j<S32K; j++)
	{
	  expCurrent[j] = (double)expHist[spectrum[i]][j];
	  simCurrent[j] = (double)dOutHist[spectrum[i]][j];
	}
      spCurrent = i;
      
      // create funciton wrapper for minmizer
      // a IMultiGenFunction type 
      ROOT::Math::Functor lr(&lrchisq,3); 
      
      // behaves best when these are small
      // starting point    
      double variable[3] = {0.001,0.001,0.001};
      // step size
      double step[3] = {0.0001,0.0001,0.0001};
      
      min->SetFunction(lr);
      
      // Set pars for minimization
      min->SetVariable(0,"a0",variable[0],step[0]);
      min->SetVariable(1,"a1",variable[1],step[1]);
      min->SetVariable(2,"a2",variable[2],step[2]);
      
      // do the minimization
      min->Minimize(); 
      
      // grab parameters from minimum
      const double *xs = min->X();

      // print results
      /* std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "," << xs[2] << "): " */
      /* 		<< min->MinValue()  << std::endl; */

      // assuming 3 parameters
      // save pars
      for(int j=0;j<3;j++)
	aFinal[j][i] = xs[j];

      // add to total chisq
      chisq += min->MinValue();
    }
}
コード例 #3
0
/* Main part of macro */
void fitData( TGraphErrors *gdata , TF1* ffit , double *startval )
{
  cout << "\n***** fitData.C: Starting fitting procedure... *****" << endl;

  g_data_global = gdata;
  f_fit_global = ffit;

  if ( g_data_global == NULL )
  {
    cout << "fitData.C: Did no find any data to fit." << endl;
    return NULL;
  }

  const char * minName = "Minuit2";
  const char *algoName = "Migrad";

  // create minimizer giving a name and a name (optionally) for the specific
  // algorithm
  // possible choices are:
  //     minName                  algoName
  // Minuit /Minuit2             Migrad, Simplex,Combined,Scan  (default is Migrad)
  //  Minuit2                     Fumili2
  //  Fumili
  //  GSLMultiMin                ConjugateFR, ConjugatePR, BFGS,
  //                              BFGS2, SteepestDescent
  //  GSLMultiFit
  //   GSLSimAn
  //   Genetic
  ROOT::Math::Minimizer* min =
  ROOT::Math::Factory::CreateMinimizer(minName, algoName);

  /* Alternatice constructor for Minuit */
  //ROOT::Minuit2::Minuit2Minimizer *min = new ROOT::Minuit2::Minuit2Minimizer();

  //TMinuit *min = new TMinuit();

  // set tolerance , etc...
  min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
  min->SetMaxIterations(10000);  // for GSL
  min->SetTolerance(0.001);
  min->SetPrintLevel(1);

  // create funciton wrapper for minmizer
  // a IMultiGenFunction type
  ROOT::Math::Functor f(&Chi2_log,3);
  double step[3] = {0.1,0.1,10};

  // starting point
  double variable[3] = { startval[0],startval[1],startval[2]};

  // set function to minimize (this is the Chi2 function)
  min->SetFunction(f);

  // Set the free variables to be minimized!
  min->SetVariable(0,"x",variable[0], step[0]);
  min->SetVariable(1,"y",variable[1], step[1]);
  min->SetVariable(2,"z",variable[2], step[2]);

  // do the minimization
  min->Minimize();

  /* set function parameters to best fit results */
  const double *xs = min->X();

  f_fit_global->SetParameter(0, xs[0]);
  f_fit_global->SetParameter(1, xs[1]);
  f_fit_global->SetParameter(2, xs[2]);

  /* Print summaries of minimization process */
  std::cout << "Minimum: f(" << xs[0] << ", " << xs[1] << ", " << xs[2] << "): "
  << min->MinValue()  << std::endl;

  // expected minimum is 0
  if ( min->MinValue()  < 1.E-4  && f(xs) < 1.E-4)
  std::cout << "Minimizer " << minName << " - " << algoName
  << "   converged to the right minimum" << std::endl;
  else {
    std::cout << "Minimizer " << minName << " - " << algoName
    << "   failed to converge !!!" << std::endl;
    Error("NumericalMinimization","fail to converge");
  }

  /* Get degrees of freedom NDF */
  unsigned ndf = 0;

  /* get range of function used for fit */
  double fit_min, fit_max;
  f_fit_global->GetRange(fit_min,fit_max);

  /* count data points in range of function */
  for ( int p = 0; p < g_data_global->GetN(); p++ )
  {
    if ( g_data_global->GetX()[p] >= fit_min && g_data_global->GetX()[p] <= fit_max )
    {
      ndf++;
    }
  }
  ndf -= 3; // 3 parameters in fit

  cout << "Degrees of freedom: " << ndf << " --> chi2 / NDF = " << min->MinValue() / ndf << endl;

  /* Get error covariance matrix from minimizer */
  TMatrixD matrix0(3,3);
  for ( unsigned i = 0; i < 3; i++ )
  {
    for ( unsigned j = 0; j < 3; j++ )
    {
      matrix0[i][j] = min->CovMatrix(i,j);
    }
  }
  matrix0.Print();

  /* Get minos error */
  double par0_minos_error_up = 0;
  double par0_minos_error_low = 0;
  double par1_minos_error_up = 0;
  double par1_minos_error_low = 0;
  double par2_minos_error_up = 0;
  double par2_minos_error_low = 0;

  min->GetMinosError( 0, par0_minos_error_low, par0_minos_error_up );
  min->GetMinosError( 1, par1_minos_error_low, par1_minos_error_up );
  min->GetMinosError( 2, par2_minos_error_low, par2_minos_error_up );

  cout << "Minos uncertainty parameter 0: up = " << par0_minos_error_up << ", low = " << par0_minos_error_low << endl;
  cout << "Minos uncertainty parameter 1: up = " << par1_minos_error_up << ", low = " << par1_minos_error_low << endl;
  cout << "Minos uncertainty parameter 2: up = " << par2_minos_error_up << ", low = " << par2_minos_error_low << endl;

  /* Plot chi2 */
  /* sigmas symmetric from covariance matrix */
  //double sigmas[6] = {sqrt(matrix0[0][0]), sqrt(matrix0[0][0]), sqrt(matrix0[1][1]), sqrt(matrix0[1][1]), sqrt(matrix0[2][2]), sqrt(matrix0[2][2])};
  /* sigmas asymmetric from minos */
  double sigmas[6] = {par0_minos_error_low, par0_minos_error_up, par1_minos_error_low, par1_minos_error_up, par2_minos_error_low, par2_minos_error_up};
  plotChi2( g_data_global, f_fit_global, 3, sigmas );

  /* Draw contour plots */
  TGraph2D* gcontour3D = plotContour( min , 3 , ndf );

  /* Find error boundaries */
  TF1* flow = (TF1*)f_fit_global->Clone("flow");
  TF1* fup = (TF1*)f_fit_global->Clone("fup");
  flow->SetRange(0,100000);
  flow->SetLineColor(kBlue);
  flow->SetLineStyle(2);
  fup->SetRange(0,100000);
  fup->SetLineColor(kBlue);
  fup->SetLineStyle(2);
  findErrorBounds( f_fit_global, flow, fup, gcontour3D );

  /* plot residuals data w.r.t. fit */
  plotResiduals( g_data_global, f_fit_global, flow, fup );

  /* Draw data, best fit, and 1-sigma band */
  TCanvas *cfit = new TCanvas();
  g_data_global->Draw("AP");
  //f_fit_global->SetRange(0,4000);
  f_fit_global->Draw("same");
  flow->Draw("same");
  fup->Draw("same");

  cfit->Print("new_FitResult.png");

  /* Fit Extrapolation */
  double t_extrapolate_months = 6;
  double t_extrapolate_s = t_extrapolate_months * 30 * 24 * 60 * 60;
  cout << "Extrapolation to " << t_extrapolate_months
  << " months: " << ffit->Eval( t_extrapolate_s )
  << " , up: " << fup->Eval( t_extrapolate_s ) - ffit->Eval( t_extrapolate_s )
  << " , low: " << flow->Eval( t_extrapolate_s ) - ffit->Eval( t_extrapolate_s )
  << endl;


  return f_fit_global;
}
コード例 #4
0
ファイル: NumericalMinimization.C プロジェクト: Y--/root
int NumericalMinimization(const char * minName = "Minuit2",
                          const char *algoName = "" ,
                          int randomSeed = -1)
{
   // create minimizer giving a name and a name (optionally) for the specific
   // algorithm
   // possible choices are:
   //     minName                  algoName
   // Minuit /Minuit2             Migrad, Simplex,Combined,Scan  (default is Migrad)
   //  Minuit2                     Fumili2
   //  Fumili
   //  GSLMultiMin                ConjugateFR, ConjugatePR, BFGS,
   //                              BFGS2, SteepestDescent
   //  GSLMultiFit
   //   GSLSimAn
   //   Genetic
   ROOT::Math::Minimizer* minimum =
      ROOT::Math::Factory::CreateMinimizer(minName, algoName);

   // set tolerance , etc...
   minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
   minimum->SetMaxIterations(10000);  // for GSL
   minimum->SetTolerance(0.001);
   minimum->SetPrintLevel(1);

   // create function wrapper for minimizer
   // a IMultiGenFunction type
   ROOT::Math::Functor f(&RosenBrock,2);
   double step[2] = {0.01,0.01};
   // starting point

   double variable[2] = { -1.,1.2};
   if (randomSeed >= 0) {
      TRandom2 r(randomSeed);
      variable[0] = r.Uniform(-20,20);
      variable[1] = r.Uniform(-20,20);
   }

   minimum->SetFunction(f);

   // Set the free variables to be minimized !
   minimum->SetVariable(0,"x",variable[0], step[0]);
   minimum->SetVariable(1,"y",variable[1], step[1]);

   // do the minimization
   minimum->Minimize();

   const double *xs = minimum->X();
   std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
             << minimum->MinValue()  << std::endl;

   // expected minimum is 0
   if ( minimum->MinValue()  < 1.E-4  && f(xs) < 1.E-4)
      std::cout << "Minimizer " << minName << " - " << algoName
                << "   converged to the right minimum" << std::endl;
   else {
      std::cout << "Minimizer " << minName << " - " << algoName
                << "   failed to converge !!!" << std::endl;
      Error("NumericalMinimization","fail to converge");
   }

   return 0;
}