Esempio n. 1
0
//____________________________________________________________________
Int_t multidimfit(bool doFit = true)
{

  cout << "*************************************************" << endl;
  cout << "*             Multidimensional Fit              *" << endl;
  cout << "*                                               *" << endl;
  cout << "* By Christian Holm <*****@*****.**> 14/10/00     *" << endl;
  cout << "*************************************************" << endl;
  cout << endl;

  // Initialize global TRannom object.
  gRandom = new TRandom();

  // Open output file
  TFile* output = new TFile("mdf.root", "RECREATE");

  // Global data parameters
  Int_t nVars       = 4;
  Int_t nData       = 500;
  Double_t x[4];

  // make fit object and set parameters on it.
  TMultiDimFit* fit = new TMultiDimFit(nVars, TMultiDimFit::kMonomials,"v");

  Int_t mPowers[]   = { 6 , 6, 6, 6 };
  fit->SetMaxPowers(mPowers);
  fit->SetMaxFunctions(1000);
  fit->SetMaxStudy(1000);
  fit->SetMaxTerms(30);
  fit->SetPowerLimit(1);
  fit->SetMinAngle(10);
  fit->SetMaxAngle(10);
  fit->SetMinRelativeError(.01);

  // variables to hold the temporary input data
  Double_t d;
  Double_t e;

  // Print out the start parameters
  fit->Print("p");

  printf("======================================\n");

  // Create training sample
  Int_t i;
  for (i = 0; i < nData ; i++) {

    // Make some data
    makeData(x,d,e);

    // Add the row to the fit object
    fit->AddRow(x,d,e);
  }

  // Print out the statistics
  fit->Print("s");

  // Book histograms
  fit->MakeHistograms();

  // Find the parameterization
  fit->FindParameterization();

  // Print coefficents
  fit->Print("rc");

  // Get the min and max of variables from the training sample, used
  // for cuts in test sample.
  Double_t *xMax = new Double_t[nVars];
  Double_t *xMin = new Double_t[nVars];
  for (i = 0; i < nVars; i++) {
    xMax[i] = (*fit->GetMaxVariables())(i);
    xMin[i] = (*fit->GetMinVariables())(i);
  }

  nData = fit->GetNCoefficients() * 100;
  Int_t j;

  // Create test sample
  for (i = 0; i < nData ; i++) {
    // Make some data
    makeData(x,d,e);

    for (j = 0; j < nVars; j++)
      if (x[j] < xMin[j] || x[j] > xMax[j])
    break;

    // If we get through the loop above, all variables are in range
    if (j == nVars)
      // Add the row to the fit object
      fit->AddTestRow(x,d,e);
    else
      i--;
  }
  //delete gRandom;

  // Test the parameterizatio and coefficents using the test sample.
  if (doFit)
     fit->Fit("M");

  // Print result
  fit->Print("fc v");

  // Write code to file
  fit->MakeCode();

  // Write histograms to disk, and close file
  output->Write();
  output->Close();
  delete output;

  // Compare results with reference run
  Int_t compare = CompareResults(fit, doFit);
  if (!compare) {
     printf("\nmultidimfit ..............................................  OK\n");
  } else {
     printf("\nmultidimfit ..............................................  fails case %d\n",compare);
  }

  // We're done
  delete fit;
  return compare;
}
void AngularAcceptance(string filename)
{
  bool imposesymmetry = true;
  // Input
  TFile* file = TFile::Open(filename.c_str());
  TTree* tree = (TTree*)file->Get("DecayTree");
  int n = tree->GetEntries();
  double x[3],d;
  tree->SetBranchAddress("Phi_angle", &x[0]);
  tree->SetBranchAddress("cos_theta1",&x[1]);
  tree->SetBranchAddress("cos_theta2",&x[2]);
  // Configure and make the 3D histogram
  int nbinsx = 10;
  double xlow = imposesymmetry ? 0 : -TMath::Pi();
  double xup  = +TMath::Pi();
  double xrange = xup - xlow;
  int nbinsy = 10;
  double ylow = imposesymmetry ? 0 : -1;
  double yup  = +1;
  double yrange = yup - ylow;
  int nbinsz = 20;
  double zlow = imposesymmetry ? 0 : -1;
  double zup  = +1;
  double zrange = zup - zlow;
  // For TH3::Interpolate() to work properly, the points have to be within the centres of the edge bins
  TH3D* hist = new TH3D("hist","",nbinsx,xlow-(xrange/(nbinsx)),xup+(xrange/(nbinsx))  // Phi range
                                 ,nbinsy,ylow-(yrange/(nbinsy)),yup+(yrange/(nbinsy))  // cos_theta1 range
                                 ,nbinsz,zlow-(zrange/(nbinsz)),zup+(zrange/(nbinsz)));// cos_theta2 range
  // Fill the histogram
  cout << "Filling a 3D histogram with " << n << " events." << endl;
  for(int i = 0; i < n; i++)
  {
    tree->GetEntry(i);
    imposesymmetry ?
      hist->Fill(TMath::Abs(x[0]),TMath::Abs(x[1]),TMath::Abs(x[2]))
      :
      hist->Fill(x[0],x[1],x[2]);
  }
  // Output
  TFile* outfile = TFile::Open("AngAcc.root", "RECREATE");
  TMultiDimFit* fit = new TMultiDimFit(3, TMultiDimFit::kLegendre,"KV");
  // Configuration
  int maxpowers[3] = {2,2,6};
  fit->SetMaxPowers(maxpowers);
  fit->SetMaxFunctions(100000);
  fit->SetMaxStudy(1000000);
  fit->SetMaxTerms(30);
  fit->SetPowerLimit(1);
  fit->SetMinAngle(10);
  fit->SetMaxAngle(10);
  fit->SetMinRelativeError(.01);
  // Event loop
  for(int i = 0; i < n; i++)
  {
    tree->GetEntry(i);
    d = imposesymmetry ?
      hist->Interpolate(TMath::Abs(x[0]),TMath::Abs(x[1]),TMath::Abs(x[2]))
      :
      hist->Interpolate(x[0],x[1],x[2]);
    (i < n/2) ?
      fit->AddRow(x,d) // Training sample
      :
      fit->AddTestRow(x,d); // Test sample
  }
  // Print starting parameters
  fit->Print("p");
  // Print out the statistics
  fit->Print("s");
  // Book histograms
  fit->MakeHistograms();
  // Find the parameterization
  fit->FindParameterization();
  // Print coefficents
  fit->Print("rc");
  // Do the fit
  fit->Fit("M");
  // Print result
  fit->Print("fc");
  // Write code to file
  fit->MakeMethod("AutoAngAcc");
  hist->Write();
  outfile->Write();
  outfile->Close();
}