#include "Minuit2/Minuit2Minimizer.h" #include "Math/Functor.h" #include "TFile.h" #include "TTree.h" #include "TROOT.h" #include using namespace std; TFile *tf = NULL; TTree *gentof = NULL; // define structure struct Events_t { // generated values Float_t dE_V; Float_t dE_H; Float_t t0_H; Float_t t0_V; Float_t x; Float_t y; Float_t tw_R; Float_t tw_L; Float_t tw_T; Float_t tw_B; Float_t t_offset; Float_t sigma_t; Float_t veff; Float_t lambda; Float_t Vmax; Float_t Vthr; // reconstructed values Float_t P_R; Float_t P_L; Float_t P_T; Float_t P_B; Float_t t_R; Float_t t_L; Float_t t_T; Float_t t_B; Float_t tcor_R; Float_t tcor_L; Float_t tcor_T; Float_t tcor_B; }; Events_t Events; double tof_chi2(const double *xx ){ Double_t walk0L = xx[0]; Double_t walk0R = xx[1]; Double_t walk0B = xx[2]; Double_t walk0T = xx[3]; Double_t walk1L = xx[4]; Double_t walk1R = xx[5]; Double_t walk1B = xx[6]; Double_t walk1T = xx[7]; Double_t chi2=0; /*char string[256]; Int_t jj; Double_t pi=3.14159;*/ Int_t nevents = 500; /*char filename[132]; sprintf(filename,"tof_tree.root"); printf ("chi2: Input filename = %s\n",filename); if (!tf) TFile *tf = new TFile(filename); gentof = (TTree*) tf->Get("gentof"); gentof->SetBranchAddress("Events",&Events.dE_V); gentof->Print();*/ Int_t nentries = gentof->GetEntries(); for (Int_t j=0; jGetEntry(j); Double_t tLc = Events.t_L - walk0L/(1+walk1L*sqrt(Events.P_L)); Double_t tRc = Events.t_R - walk0R/(1+walk1R*sqrt(Events.P_R)); Double_t tBc = Events.t_B - walk0B/(1+walk1B*sqrt(Events.P_B)); Double_t tTc = Events.t_T - walk0T/(1+walk1T*sqrt(Events.P_T)); Double_t t0H = 0.5*(tLc+tRc); Double_t t0V = 0.5*(tBc+tTc); // printf ("t0H =%f, t0V=%f\n",t0H,t0V); chi2 = chi2 + (t0H-t0V)*(t0H-t0V)/(Events. sigma_t*Events.sigma_t); } // printf ("chi2 = %f walk0L=%f, walk0R=%f, walk0B=%f, walk0T=%f \n\n",chi2,walk0L,walk0R,walk0B,walk0T); return chi2; } int tof_calibration_fit() { // read root tree char filename[132]; sprintf(filename,"tof_tree.root"); printf ("tof_calibration_fit: Input filename = %s\n",filename); tf = new TFile(filename); gentof = (TTree*) tf->Get("gentof"); gentof->SetBranchAddress("Events",&Events.dE_V); gentof->Print(); // Choose method upon creation between: // kMigrad, kSimplex, kCombined, // kScan, kFumili ROOT::Minuit2::Minuit2Minimizer min ( ROOT::Minuit2::kMigrad ); min.SetMaxFunctionCalls(1000000); min.SetMaxIterations(100000); min.SetTolerance(0.001); ROOT::Math::Functor f(&tof_chi2,8); double step[8] = {0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01}; double variable[8] = { 10,10,10,10,0.8,0.8,0.8,0.8}; min.SetFunction(f); // Set the free variables to be minimized! min.SetVariable(0,"walk0L",variable[0], step[0]); min.SetVariable(1,"walk0R",variable[1], step[1]); min.SetVariable(2,"walk0B",variable[2], step[2]); min.SetVariable(3,"walk0T",variable[3], step[3]); min.SetVariable(4,"walk1L",variable[4], step[4]); min.SetVariable(5,"walk1R",variable[5], step[5]); min.SetVariable(6,"walk1B",variable[6], step[6]); min.SetVariable(7,"walk1T",variable[7], step[7]); min.Minimize(); const double *xs = min.X(); cout << "Minimum: f(" << xs[0] << "," << xs[1] << "," << xs[2] << "," << xs[3] << "," << xs[4] << "," << xs[5] << "," << xs[6] << "," << xs[7] << "): " << tof_chi2(xs) << endl; delete tf; return 0; }