Ejemplo n.º 1
0
void fit(boot &A,boot &B,boot &C,bvec &X,bvec &Y)
{
  //copy X
  X_fit=new double[nens];
  for(int iens=0;iens<nens;iens++) X_fit[iens]=X[iens].med();
  Y_fit=new double[nens];
  err_Y_fit=new double[nens];
  
  TMinuit minu;
  minu.SetPrintLevel(-1);
  
  minu.DefineParameter(0,"A",0.0,0.0001,0,0);
  minu.DefineParameter(1,"B",0.0,0.0001,0,0);
  minu.DefineParameter(2,"C",0.0,0.0001,0,0);
  
  minu.SetFCN(chi2_wr);
  
  double C2;
  for(int iboot=0;iboot<nboot+1;iboot++)
    {
      if(iboot>0)
        minu.SetPrintLevel(-1);
      
      minu.DefineParameter(3,"a380",lat[0][iboot],0.0001,0,0);
      minu.DefineParameter(4,"a390",lat[1][iboot],0.0001,0,0);
      minu.DefineParameter(5,"a405",lat[2][iboot],0.0001,0,0);
      minu.DefineParameter(6,"a420",lat[3][iboot],0.0001,0,0);
      minu.FixParameter(3);
      minu.FixParameter(4);
      minu.FixParameter(5);
      minu.FixParameter(6);
      
      for(int iens=0;iens<nens;iens++)
        {
          Y_fit[iens]=Y.data[iens].data[iboot];
          err_Y_fit[iens]=Y.data[iens].err();
        }
      
      //minimize
      minu.Migrad();
      
      //get back parameters
      double dum;
      minu.GetParameter(0,A.data[iboot],dum);
      minu.GetParameter(1,B.data[iboot],dum);
      minu.GetParameter(2,C.data[iboot],dum);
      
      double lat_med[4]={lat[0].med(),lat[1].med(),lat[2].med(),lat[3].med()};
      if(iboot==0) C2=chi2(A.data[iboot],B[iboot],C[iboot],lat_med);
    }
  
  //calculate the chi2
  cout<<"A = ("<<A<<"), B=("<<B<<"), C=("<<C<<")"<<endl;
  cout<<"Chi2 = "<<C2<<" / "<<nens-3<<" = "<<C2/(nens-3)<<endl;
  
  delete[] X_fit;
  delete[] Y_fit;
  delete[] err_Y_fit;
}
//______________________________________________________________________________
vector< vector<double> > cSpline(int nPoints, int npar, vector <double> xData, vector <double> yData, vector <double> yErrorData, double stepSpline, double start, double step)
{
	//Populate the global variables//-> DONE IN THE MAIN
	xData_GLOB = xData;//-> DONE IN THE MAIN
	yData_GLOB = yData;//-> DONE IN THE MAIN
	yErrorData_GLOB = yErrorData;//-> DONE IN THE MAIN

	//Initialize Minuit
	vector<double> vstart, vstep;
	for(int i=0; i<npar; i++) 	//set starting values and step sizes for parameters
	{
		vstart.push_back(start);
		vstep.push_back(step);
	}

	TMinuit *myMinuit = new TMinuit(npar);  //initialize TMinuit with a maximum of npar (5) -> PASSED AS ARGUMENT
	myMinuit->SetFCN(fcn);//-> DONE IN THE MAIN
	myMinuit->SetPrintLevel(-1);//No output: -1, output:1//-> DONE IN THE MAIN

	double arglist[10];//-> DONE IN THE MAIN
	int ierflg = 0;//-> DONE IN THE MAIN
	arglist[0] = 1;//-> DONE IN THE MAIN
	myMinuit->mnexcm("SET ERR", arglist, 1, ierflg);//-> DONE IN THE MAIN

	for (int i = 0; i < npar; i++) {//-> DONE IN THE MAIN
		stringstream ss;//-> DONE IN THE MAIN
		ss<<"a"<<i;//-> DONE IN THE MAIN
		myMinuit->mnparm(i, ss.str().c_str(), vstart.at(i), vstep.at(i), 0, 0, ierflg);//-> DONE IN THE MAIN
	}//-> DONE IN THE MAIN

	//Perform the Minuit fit
	arglist[0] = 500;//-> DONE IN THE MAIN
	arglist[1] = 1.;//-> DONE IN THE MAIN
	myMinuit->mnexcm("MIGRAD", arglist, 2, ierflg); //minimization

	//Retrieve best-fit parameters
	vector< double > bestFitParams, e_bestFitParams;
	for(int i=0; i<npar; i++)
	{
		double par, epar;
		myMinuit->GetParameter(i, par, epar); //retrieve best fit parameters
		bestFitParams.push_back(par);
		e_bestFitParams.push_back(epar);
	}

	//Store the best-fit spline in a TGraph
	gsl_spline_init (spline_GLOB, &xData[0], &bestFitParams[0], xData.size()); //initialize the spline

	int nPointsSpline = int ((xData[nPoints-1]-xData[0])/stepSpline); //calculate the number of points of the spline

	vector< vector<double> > cSplineValues;
	cSplineValues.push_back( vector< double > ());//x
	cSplineValues.push_back( vector< double > ());//y
	for (int i= 0; i < nPointsSpline; i++){
		cSplineValues[0].push_back(xData[0]+i*stepSpline);
		cSplineValues[1].push_back(gsl_spline_eval (spline_GLOB, cSplineValues[0].back(), acc_GLOB));
	}

	return cSplineValues;
}
Ejemplo n.º 3
0
// --------------------------------- //
void make_lin_fit(double & slope, double & d_slope, double & offset, double & d_offset){
    TMinuit min;
    min.SetPrintLevel(-1);
    //min.SetPrintLevel(0);
    int err = min.DefineParameter(0, "slope", slope, d_slope, 0.05, 1.0);
    assert(err==0);
    err = min.DefineParameter(1, "offset", offset, d_offset, 0.001, 0.2);
    assert(err==0);
    min.SetFCN(chi2_linear);
    min.mnmigr();
    min.GetParameter(0, slope, d_slope);
    min.GetParameter(1, offset, d_offset);
}
Ejemplo n.º 4
0
void fit(boot &A,boot &B,boot &C,boot &D)
{
  //copy ml
  ml_fit=new double[nens];
  for(int iens=0;iens<nens;iens++) ml_fit[iens]=ml[iens].med();
  
  //alloc dM2Pi and dM2K
  dM2Pi_fit=new double[nens];
  err_dM2Pi_fit=new double[nens];
  dM2K_fit=new double[nens];
  err_dM2K_fit=new double[nens];
  
  TMinuit minu;
  minu.SetPrintLevel(-1);
  
  int npars=4;
  minu.DefineParameter(0,"A",0.0,0.0001,0,0);
  minu.DefineParameter(1,"B",0.0,0.0001,0,0);
  minu.DefineParameter(2,"C",0.0,0.0001,0,0);
  minu.DefineParameter(3,"D",0.0,0.0001,0,0);
  minu.SetFCN(chi2_wr);
  
  double C2;
  for(int iboot=0;iboot<nboot+1;iboot++)
    {
      if(iboot>0) minu.SetPrintLevel(-1);
      
      minu.DefineParameter(4,"a380",lat[0][iboot],0.0001,0,0);
      minu.DefineParameter(5,"a390",lat[1][iboot],0.0001,0,0);
      minu.DefineParameter(6,"a405",lat[2][iboot],0.0001,0,0);
      minu.DefineParameter(7,"a420",lat[3][iboot],0.0001,0,0);
      minu.FixParameter(4);
      minu.FixParameter(5);
      minu.FixParameter(6);
      minu.FixParameter(7);
      
      for(int iens=0;iens<nens;iens++)
        {
          dM2Pi_fit[iens]=dM2Pi.data[iens].data[iboot];
          err_dM2Pi_fit[iens]=dM2Pi.data[iens].err();
          dM2K_fit[iens]=dM2K.data[iens].data[iboot];
          err_dM2K_fit[iens]=dM2K.data[iens].err();
        }
      
      //minimize
      minu.Migrad();
      
      //get back parameters
      double dum;
      minu.GetParameter(0,A.data[iboot],dum);
      minu.GetParameter(1,B.data[iboot],dum);
      minu.GetParameter(2,C.data[iboot],dum);
      minu.GetParameter(3,D.data[iboot],dum);
      
      double lat_med[4]={lat[0].med(),lat[1].med(),lat[2].med(),lat[3].med()};
      if(iboot==nboot) C2=chi2(A.data[iboot],B[iboot],C[iboot],D[iboot],lat_med,true);
    }
  
  //calculate the chi2
  cout<<"A=("<<A<<"), B=("<<B<<"), C=("<<C<<"), D=("<<D<<")"<<endl;
  cout<<"Chi2 = "<<C2<<" / "<<2*nens-npars<<" = "<<C2/(2*nens-npars)<<endl;
  
  delete[] ml_fit;
  delete[] dM2Pi_fit;
  delete[] err_dM2Pi_fit;
  delete[] dM2K_fit;
  delete[] err_dM2K_fit;
}
Ejemplo n.º 5
0
void parab_fit(double ch2,jack &a,jack &b,jack &c,double *X,jvec Y,const char *path,bool fl_parab=1)
{
  ofstream out(path);
  out<<"@type xydy\n";
  
  double MX=0;
  //copy X
  nc_fit=Y.nel;
  X_fit=new double[nc_fit];
  for(int iel=0;iel<nc_fit;iel++)
    if(use[iel])
    {
      X_fit[iel]=X[iel];
      out<<X[iel]<<" "<<Y[iel]<<endl;
      if(X[iel]>MX) MX=X[iel];
    }
  Y_fit=new double[nc_fit];
  err_Y_fit=new double[nc_fit];
  
  TMinuit minu;
  minu.SetPrintLevel(0);
  
  int npars=3;
  minu.DefineParameter(0,"A",0.0,0.0001,0,0);
  minu.DefineParameter(1,"B",0.0,0.0001,0,0);
  minu.DefineParameter(2,"C",0.0,0.0001,0,0);
  if(!fl_parab)
    {
      minu.FixParameter(0);
      npars--;
    }
  minu.SetFCN(chi2_wr);
  
  for(int ijack=0;ijack<=njack;ijack++)
    {
      minu.DefineParameter(2,"C",fixed_A[ijack],0.0001,0,0);
      minu.FixParameter(2);
      if(ijack>0)
        minu.SetPrintLevel(-1);
      
      for(int iel=0;iel<nc_fit;iel++)
        {
          Y_fit[iel]=Y.data[iel].data[ijack];
          err_Y_fit[iel]=Y[iel].err();
        }
      
      //minimize
      minu.Migrad();
      
      //get back parameters
      double dum;
      minu.GetParameter(0,a.data[ijack],dum);
      minu.GetParameter(1,b.data[ijack],dum);
      minu.GetParameter(2,c.data[ijack],dum);
    }
  
  out<<"&\n@type xy\n";
  for(double t=0;t<MX;t+=MX/100)
    out<<t<<" "<<parab(a[njack],b[njack],c[njack],t)<<endl;
  
  out.close();
}
//_________________________________________________________________________________
void multipleSplinesWithHistogramsVb(int iEventLook = 163, int nEvents = 1000, int nPoints = 9, double seed = 231) 
{
	double lowerBound = 10; //bounds for random vector function
	double upperBound = 20;
	double lowerErrorBound = 1;
	double upperErrorBound = 2;

	//Load the data
	vector< vector<double> > xEvents, yEvents, yErrorEvents; //each of these is a vector of vectors (of randomized data points)
	for(int i = 0; i < nEvents; i++) 
	{
		vector <double> xData, yData, yErrorData; //temporary vectors that are only used to get random values from FillRand function
		FillRandVectors(nPoints, xData, yData, yErrorData, seed*(i+1), lowerBound, upperBound, lowerErrorBound, upperErrorBound); //populates random vectors for y values and y error vector
		xEvents.push_back(xData);
		yEvents.push_back(yData);
		yErrorEvents.push_back(yErrorData);
	}

	//Intialization of the variables
	const int npar = nPoints;
	const int orderSpline = 4;
	const int nbreak = npar+2-orderSpline;
	double stepSpline = 0.01;
	double xminBSplineWorkspace = 0;
	double xmaxBSplineWorkspace = 9;
	double startCSplineWorkspace = 15.;
	double stepCSplineWorkspace = 1.5;

	acc_GLOB = gsl_interp_accel_alloc ();
	spline_GLOB = gsl_spline_alloc (gsl_interp_cspline, nPoints);	
	bw_GLOB = gsl_bspline_alloc(orderSpline, nbreak);
	
	//B- and C-splines
	clock_t tbstart, tbstop, tcstart, tcstop;
	vector <double> timeb, timec;

	vector< vector<double> > xBSplineValues, yBSplineValues;
	vector< vector<double> > xCSplineValues, yCSplineValues; 

//Setup for the C-spline_________________________________________________________________________
	TMinuit *myMinuit = new TMinuit(npar);  //initialize TMinuit with a maximum of npar 
	myMinuit->SetFCN(fcn);
	myMinuit->SetPrintLevel(-1);//No output: -1, output:1

	double arglist[10];
	int ierflg = 0;
	arglist[0] = 1;
	myMinuit->mnexcm("SET ERR", arglist, 1, ierflg);

	//Initialize Minuit
	vector<double> vstart, vstep;
	for(int i=0; i<npar; i++) 	//set starting values and step sizes for parameters
	{
		vstart.push_back(startCSplineWorkspace);
		vstep.push_back(stepCSplineWorkspace);
	}

	for (int i = 0; i < npar; i++) {
		stringstream ss;
		ss<<"a"<<i;
		myMinuit->mnparm(i, ss.str().c_str(), vstart.at(i), vstep.at(i), 0, 0, ierflg);
	}

//Setup for the B-spline_________________________________________________________________________

	//Looping begins for the calculations of the B and C-splines for each event
	for(int i = 0; i < (int)xEvents.size(); i++)
	{
		//Populate the global variables
		xData_GLOB = xEvents.at(i);
		yData_GLOB = yEvents.at(i);
		yErrorData_GLOB = yErrorEvents.at(i);

		tbstart = clock();
		vector< vector<double> > bSplineValues = bSpline(nPoints, npar, xEvents.at(i), yEvents.at(i), yErrorEvents.at(i), stepSpline, xminBSplineWorkspace, xmaxBSplineWorkspace);
		tbstop = clock();
		timeb.push_back(((float)tbstop-(float)tbstart)/ (CLOCKS_PER_SEC/1000.) );		

		std::cout<<timeb.back()<<std::endl;

		xBSplineValues.push_back(bSplineValues.at(0));
		yBSplineValues.push_back(bSplineValues.at(1));

		tcstart = clock();
		vector< vector<double> > cSplineValues = cSpline(nPoints, npar, xEvents.at(i), stepSpline, myMinuit);
		tcstop = clock();
		timec.push_back(((float)tcstop-(float)tcstart)/ (CLOCKS_PER_SEC/1000.) );		

		xCSplineValues.push_back(cSplineValues.at(0));
		yCSplineValues.push_back(cSplineValues.at(1));
	}

	//Histograms______________________________________________________________________________________

	//Time
	int nbins = 100;
	double xlow = 0;
	double xup = 1.;

	TH1D *hTimeB = new TH1D("Time","Timing; time [ms]; Number of Events", nbins, xlow, xup); 
	hTimeB->SetStats(0);
	hTimeB->SetMarkerStyle(10);
	TH1D *hTimeC = new TH1D("TimeC","Timing; time [ms]; Number of Events", nbins, xlow, xup); 
	hTimeC->SetLineColor(kRed);
	hTimeC->SetMarkerStyle(10);
	hTimeC->SetStats(0);

	for(int i=0; i<(int)timec.size(); i++) 
	{
		hTimeB->Fill(timeb.at(i));
		hTimeC->Fill(timec.at(i));	
	}

	//Interpolation
	vector <double> interpB, interpC;
	for(int i = 0; i < (int)yEvents.size(); i++)
	{
		for(int j = 0; j < (int)yEvents[i].size(); j++)
		{
			int indexForB = binarySearch(xBSplineValues[i], xEvents[i][j]);
			int indexForC = binarySearch(xCSplineValues[i], xEvents[i][j]);

			interpB.push_back( (yEvents[i][j]-yBSplineValues[i][indexForB])/yErrorEvents[i][j] );
			interpC.push_back( (yEvents[i][j]-yCSplineValues[i][indexForC])/yErrorEvents[i][j] );
		}
	}	

	//Test graphs for splines
	TGraph *GCspline = new TGraph(xCSplineValues[iEventLook].size(), &xCSplineValues[iEventLook][0], &yCSplineValues[iEventLook][0]);
	GCspline->SetLineColor(kRed);
	TGraph *GBspline = new TGraph(xBSplineValues[iEventLook].size(), &xBSplineValues[iEventLook][0], &yBSplineValues[iEventLook][0]);
	TGraph *Gdata = new TGraph(xEvents[0].size(), &xEvents[iEventLook][0], &yEvents[iEventLook][0]);
		Gdata->SetMarkerStyle(20);

	int nbinsI = 101;
	double xlowI = -0.1;
	double xupI = 0.1;
	TH1D *hInterpB = new TH1D("Interp B","Interpolation; Distance between spline and data normalized by error; Number of Events", nbinsI, xlowI, xupI); 
	for(int i=0; i<(int)interpB.size(); i++) hInterpB->Fill(interpB.at(i));
	hInterpB->SetStats(0);

	TH1D *hInterpC = new TH1D("Interp C","Interpolation; Distance between spline and data normalized by error; Number of Events", nbinsI, xlowI, xupI); 
	for (int i=0; i<(int)interpC.size(); i++) hInterpC->Fill(interpC.at(i));
	hInterpC->SetLineColor(kGreen);
	hInterpC->SetStats(0);	

	//Draws______________________________________________________________________________________

	//Interpolation 
	TLegend *legInterp = new TLegend(0.9,0.70,0.75,0.85);
	legInterp->SetLineColor(kWhite); 
	legInterp->SetFillColor(kWhite);
	legInterp->SetMargin(0.3); 
	legInterp->AddEntry(hInterpB,"b-spline","l");
	legInterp->AddEntry(hInterpC,"c-spline","l");
	legInterp->SetTextSize(0.05);

	TCanvas *c1 = new TCanvas("c1", "Interpolation distance");
	c1->cd();
	hInterpB->Draw("");
	hInterpC->Draw("same");
	legInterp->Draw();

	//Time
	TLegend *legTime = new TLegend(0.9,0.70,0.75,0.85);
	legTime->SetLineColor(kWhite); 
	legTime->SetFillColor(kWhite);
	legTime->SetMargin(0.3); 
	legTime->AddEntry(hTimeB,"b-spline","l");
	legTime->AddEntry(hTimeC,"c-spline","l");
	legTime->SetTextSize(0.05);

	TCanvas *c2 = new TCanvas("c2", "Computation time");
	c2->cd();
	hTimeB->Draw();
//	hTimeC->Draw("same");
	legTime-> Draw();

	TCanvas *c3 = new TCanvas("c3", "Test splines");
	c3->cd();
	Gdata->Draw("ap");
	GCspline->Draw("samel");
	GBspline->Draw("samel");

	//Free the memory used
	gsl_spline_free (spline_GLOB); 
 	gsl_interp_accel_free (acc_GLOB);
	gsl_bspline_free(bw_GLOB);
}
void runAngleOpt() {

  gSystem->AddIncludePath("-I${ANITA_UTIL_INSTALL_DIR}/include");

  double startVal=0;               
  double stepSize=0.1;       
  double minVal=-2;           
  double maxVal=2;            
  Double_t p0 = 0;

  //Load libraries. Need to have ANITA_UTIL_INSTALL_DIR/lib and ROOTSYS/lib in the LD_LIBRARY_PATH               
  gSystem->Load("libfftw3.so");
  gSystem->Load("libMathMore.so");
  gSystem->Load("libPhysics.so");  
  gSystem->Load("libGeom.so");  
  gSystem->Load("libMinuit.so");  
  gSystem->Load("libRootFftwWrapper.so");         
  gSystem->Load("libAnitaEvent.so");      
  gSystem->Load("libAnitaCorrelator.so");

  AnitaGeomTool *fGeomTool = AnitaGeomTool::Instance();
  gSystem->CompileMacro("anglePlotterOpt.C","k");
  
  Double_t relDeltaOut=0;

  TMinuit *myMin = new TMinuit(1);
  myMin->SetObjectFit(anglePlotterOpt);
  myMin->SetFCN(iHateRoot);
  //setArray();

  for(int u = 0; u < 1; u++){

    int middle = 16;

      for(int y = 16; y <32; y++){

	int leftOpt, rightOpt;
	fGeomTool->getThetaPartners(middle,leftOpt,rightOpt); 


	myMin->DefineParameter(0, "antNum", middle, stepSize, minVal, maxVal);
	myMin->FixParameter(0);  
    
	myMin->DefineParameter(1, "deltaT", startVal, stepSize, minVal, maxVal);

  
	Double_t deltaT,deltaTErr;
  
	//*********MINUIT METHOD*******************
	myMin->SetPrintLevel(-1);
	myMin->Migrad();   
	myMin->GetParameter(1,deltaT,deltaTErr);

  

	setValue(rightOpt,deltaT);

	//	printArray();

	//	cout << middle << "  " << rightOpt << "  " << deltaT << endl;
  
	cout << "deltaTArrayMod[" << rightOpt << "] = " << deltaT << ";" << endl;

	middle = rightOpt;

      }

  }

  
  //   myMin->DeleteArrays();

  //   myMin->DeleteArrays();

  
}
Ejemplo n.º 8
0
//_________________________________________________________________________________
void integratedSplinesV4a(double seed = 231)
{
    //Load the data
    int nEvents = 1000; //number of times the data will be randomized
    int nPoints = 9;

    vector< vector<double> > xEvents, yEvents, yErrorEvents; //each of these is a vector of vectors (of randomized data points)
    for(int i = 0; i < nEvents; i++)
    {
        vector <double> xData, yData, yErrorData; //temporary vectors that are only used to get random values from FillRand function
        FillRandVectors(nPoints, xData, yData, yErrorData, seed*(i+1)); //populates random vectors for y values and y error vector
        xEvents.push_back(xData);
        yEvents.push_back(yData);
        yErrorEvents.push_back(yErrorData);
        //Populate the global variables
        xData_GLOB = xData;
        yData_GLOB = yData;
        yErrorData_GLOB = yErrorData;
    }

    //Intialization of the variables
    const int npar = nPoints;
    const int orderSpline = 4;
    const int nbreak = npar+2-orderSpline;
    double stepSpline = 0.01;
    double xminBSplineWorkspace = 0;
    double xmaxBSplineWorkspace = 9;
    double startCSplineWorkspace = 15.;
    double stepCSplineWorkspace = 1.5;

    acc_GLOB = gsl_interp_accel_alloc ();
    spline_GLOB = gsl_spline_alloc (gsl_interp_cspline, nPoints);
    gsl_bspline_workspace *bw = gsl_bspline_alloc(orderSpline, nbreak);

    //Setting up TMinuit for C-spline minimization
    TMinuit *myMinuit = new TMinuit(npar);  //initialize TMinuit with a maximum of npar (5)
    myMinuit->SetFCN(fcn);
    myMinuit->SetPrintLevel(-1);//No output: -1, output:1

    double arglist[10];
    int ierflg = 0;
    arglist[0] = 1;
    myMinuit->mnexcm("SET ERR", arglist, 1, ierflg);

    vector<double> vstart, vstep;
    for(int i=0; i < npar; i++) 	//set starting values and step sizes for parameters
    {
        vstart.push_back(startCSplineWorkspace);
        vstep.push_back(stepCSplineWorkspace);
    }

    for (int i = 0; i < npar; i++)
    {
        stringstream ss;
        ss<<"a"<<i;
        myMinuit->mnparm(i, ss.str().c_str(), vstart.at(i), vstep.at(i), 0, 0, ierflg);
    }

    //Perform the Minuit fit
    arglist[0] = 500;
    arglist[1] = 1.;

    //B and C spline loops__________________________________________________________________________
    clock_t tbstart, tbstop, tcstart, tcstop;
    vector <double> timeb, timec;

    vector< vector<double> > xBSplineValues, yBSplineValues;
    vector< vector<double> > xCSplineValues, yCSplineValues;
    for(int i = 0; i < (int)xEvents.size(); i++)
    {
        tbstart = clock();
        vector< vector<double> > bSplineValues = bSpline(nPoints, npar, xEvents.at(i), yEvents.at(i), yErrorEvents.at(i), stepSpline, xminBSplineWorkspace, xmaxBSplineWorkspace, bw);
        tbstop = clock();
        timeb.push_back(((float)tbstop-(float)tbstart)/ (CLOCKS_PER_SEC/1000.) );

        xBSplineValues.push_back(bSplineValues.at(0));
        yBSplineValues.push_back(bSplineValues.at(1));

        tcstart = clock();
        vector< vector<double> > cSplineValues = cSpline(nPoints, npar, xEvents.at(i), stepSpline, myMinuit, arglist, ierflg);
        tcstop = clock();
        timec.push_back(((float)tcstop-(float)tcstart)/ (CLOCKS_PER_SEC/1000.) );

        xCSplineValues.push_back(cSplineValues.at(0));
        yCSplineValues.push_back(cSplineValues.at(1));
    }

    //Histograms______________________________________________________________________________________

    //Time
    int nbins = 100;
    double xlow = 0;
    double xup = 5.;

    TH1D *hTimeB = new TH1D("Time","; time [ms]; number of runs", nbins, xlow, xup);
    hTimeB->SetStats(0);
    TH1D *hTimeC = new TH1D("TimeC","; time [ms]; number of runs", nbins, xlow, xup);
    hTimeC->SetLineColor(kRed);
    hTimeC->SetStats(0);

    for(int i=0; i<(int)timec.size(); i++)
    {
        hTimeB->Fill(timeb.at(i));
        hTimeC->Fill(timec.at(i));
    }

    //Interpolation distance -> Should FIND THE RIGHT J FOR SPLINE
    vector <double> interpB, interpC;
    for(int i = 0; i < (int)yEvents.size(); i++)
    {
        for(int j = 0; j < (int)yEvents[i].size(); j++)
        {
            double key = xEvents[i][j];
            int indexForB = binarySearch(xBSplineValues[i], key);
            int indexForC = binarySearch(xCSplineValues[i], key);
//			std::cout << "B: " << indexForB << " C: " << indexForC << endl;
            if(indexForB != -1)
                interpB.push_back( (yEvents[i][j]-yBSplineValues[i][indexForB])/yErrorEvents[i][indexForB] );
            if(indexForC != -1)
                interpC.push_back( (yEvents[i][j]-yCSplineValues[i][indexForC])/yErrorEvents[i][indexForC] );
        }
    }

    int nbinsI = 40;
    int xlowI = -4;
    int xupI = 4;
    TH1D *hInterpB = new TH1D("Interp B","; xAxis; yAxis", nbinsI, xlowI, xupI);
    for(int i=0; i<(int)interpB.size(); i++) hInterpB->Fill(interpB.at(i));
    hInterpB->SetStats(0);

    TH1D *hInterpC = new TH1D("Interp C","; xAxis; yAxis", nbinsI, xlowI, xupI);
    for (int i=0; i<(int)interpC.size(); i++) hInterpC->Fill(interpC.at(i));
    hInterpC->SetLineColor(kRed);
    hInterpC->SetStats(0);

    //Draws______________________________________________________________________________________

    //Interpolation distance
    TLegend *legInterp = new TLegend(0.75,0.70,0.4,0.85);
    legInterp->SetLineColor(kWhite);
    legInterp->SetFillColor(kWhite);
    legInterp->SetMargin(0.3);
    legInterp->AddEntry(hInterpB,"b-spline","l");
    legInterp->AddEntry(hInterpC,"c-spline","l");

    TCanvas *c1 = new TCanvas("c1", "Interpolation distance");
    c1->cd();
    hInterpB->Draw("");
    hInterpC->Draw("same");
    legInterp->Draw();

    //Time
    TLegend *legTime = new TLegend(0.75,0.70,0.4,0.85);
    legTime->SetLineColor(kWhite);
    legTime->SetFillColor(kWhite);
    legTime->SetMargin(0.3);
    legTime->AddEntry(hTimeB,"b-spline","l");
    legTime->AddEntry(hTimeC,"c-spline","l");

    TCanvas *c2 = new TCanvas("c2", "Computation time");
    c2->cd();
    hTimeB->Draw("");
    hTimeC->Draw("same");
    legTime-> Draw();

    //Free the memory for the spline
    gsl_spline_free (spline_GLOB); //frees the memory used by the spline
    gsl_interp_accel_free (acc_GLOB);
    gsl_bspline_free(bw);
}
void runTopRingOpt() {

  gSystem->AddIncludePath("-I${ANITA_UTIL_INSTALL_DIR}/include");

  double startVal=0;               
  double stepSize=0.1;       
  double minVal=-0.5;           
  double maxVal=0.5;            
  Double_t p0 = 0;

  //Load libraries. Need to have ANITA_UTIL_INSTALL_DIR/lib and ROOTSYS/lib in the LD_LIBRARY_PATH               
  gSystem->Load("libfftw3.so");
  gSystem->Load("libMathMore.so");
  gSystem->Load("libPhysics.so");  
  gSystem->Load("libGeom.so");  
  gSystem->Load("libMinuit.so");  
  gSystem->Load("libRootFftwWrapper.so");         
  gSystem->Load("libAnitaEvent.so");      
  gSystem->Load("libAnitaCorrelator.so");

  AnitaGeomTool *fGeomTool = AnitaGeomTool::Instance();
  gSystem->CompileMacro("topRingOpt.C","k");
  
  Double_t relDeltaOut=0;

  TMinuit *myMin = new TMinuit(150);
  myMin->SetObjectFit(topRingOpt);
  myMin->SetFCN(iHateRoot);
  //setArray();

  double startValDeltaT[16] ={0};
  double startValR[16] ={0};
  double startValPhi[16] ={0};

 //    startValDeltaT[0] =  -0.0519515;     startValR[0] =   -0.0101463;         startValPhi[0] =      -0.00473836 ;   
//     startValDeltaT[1] =     -0.0597062;   startValR[1] =      -0.02577;       startValPhi[1] =   0.00864501 ; 
//     startValDeltaT[2] =     -0.081435;     startValR[2] =     -0.000224044;    startValPhi[2] =   -0.000630649;
//     startValDeltaT[3] =     0.0118873;     startValR[3] =      0.019945;       startValPhi[3] =    0.014016;   
//     startValDeltaT[4] =      0.017917;      startValR[4] =     -0.00297559;    startValPhi[4] =    0.0224936 ;
//     startValDeltaT[5] =      0.0377119;     startValR[5] =     -0.014872;       startValPhi[5] =   0.0163349; 
//     startValDeltaT[6] =     -0.0426158;     startValR[6] =    -0.0562555;      startValPhi[6] =    0.0220065 ; 
//     startValDeltaT[7] =     -0.0221673;     startValR[7] =    -0.034104 ;      startValPhi[7] =    0.0158545 ; 
//     startValDeltaT[8] =      0.0263739;     startValR[8] =     0.00248804;      startValPhi[8] =    0.013246 ; 
//     startValDeltaT[9] =      -0.0938419;    startValR[9] =     -0.00344703;     startValPhi[9] =    -0.00718616; 
//     startValDeltaT[10] =      0.145264;      startValR[10] =     -0.0121874 ;     startValPhi[10] =     0.0156988 ;  
//     startValDeltaT[11] =      0.118105;      startValR[11] =     -0.0337033 ;      startValPhi[11] =   -0.00324182 ;
//     startValDeltaT[12] =      0.321805;      startValR[12] =     0.0134362  ;      startValPhi[12] =   -0.00190277 ;
//     startValDeltaT[13] =      0.0197693;     startValR[13] =     -0.000656063;      startValPhi[13] =   -0.0162318  ;
//     startValDeltaT[14] =      -0.115263;     startValR[14] =     0.0495637   ;      startValPhi[14] =   -0.0198119 ; 
//     startValDeltaT[15] =      -0.255707;   startValR[15] =     0.00189892  ;     startValPhi[15] =     0.0383932  ;
    


 for(int y = 0; y <16; y++){

   ofstream newfile("newSimonNumbers.txt");

   char name[30];
   sprintf(name,"r%d",y);
	myMin->DefineParameter(y, name, startValR[y], stepSize, minVal, maxVal);
   sprintf(name,"z%d",y);
	myMin->DefineParameter(y+16, name, startValDeltaT[y], stepSize, minVal, maxVal);
	   sprintf(name,"phi%d",y);
	myMin->DefineParameter(y+32, name, startValPhi[y], stepSize, minVal, maxVal);
 }


  
	Double_t deltaR[32],deltaRErr[32];
	Double_t deltaZ[32],deltaZErr[32];
	Double_t deltaPhi[32],deltaPhiErr[32];
  
	//*********MINUIT METHOD*******************
	myMin->SetPrintLevel(-1);
	myMin->Migrad();   

	for(int u = 0; u <16; u++){
	  myMin->GetParameter(u,deltaR[u],deltaRErr[u]);
	  //cout << "deltaR[" << u << "] = " << deltaR[u] ;
	
	
	  myMin->GetParameter(u+16,deltaZ[u],deltaZErr[u]);
	  //cout << " deltaZ[" << u << "] = " << deltaZ[u] ;
	
	  myMin->GetParameter(u+32,deltaPhi[u],deltaPhiErr[u]);
	  //cout << " deltaPhi[" << u << "] = " << deltaPhi[u] << ";" << endl;

	  newfile << u << "  " << deltaZ[u]<< "  " << deltaR[u]<< "  " << deltaPhi[u]<< "  " << 0 << endl;


	}


  
}
Ejemplo n.º 10
0
//______________________________________________________________________________
vector< vector<double> > cSpline(int nPoints, int npar, vector <double> xData, vector <double> yData, vector <double> yErrorData, double stepSpline =.01, double start = 10., double step = 0.01)
{
	//Populate the global variables
	xData_GLOB = xData;
	yData_GLOB = yData;
	yErrorData_GLOB = yErrorData;

	//Initialize Minuit
	vector<double> vstart, vstep;
	for(int i=0; i<npar; i++) 	//set starting values and step sizes for parameters
	{
		vstart.push_back(start);
		vstep.push_back(step);
	}

	TMinuit *myMinuit = new TMinuit(npar);  //initialize TMinuit with a maximum of npar (5)
	myMinuit->SetFCN(fcn);
	myMinuit->SetPrintLevel(-1);//No output: -1, output:1

	double arglist[10];
	int ierflg = 0;
	arglist[0] = 1;
	myMinuit->mnexcm("SET ERR", arglist, 1, ierflg);

	for (int i = 0; i < npar; i++) {
		stringstream ss;
		ss<<"a"<<i;
		myMinuit->mnparm(i, ss.str().c_str(), vstart.at(i), vstep.at(i), 0, 0, ierflg);
	}

	//Perform the Minuit fit
	arglist[0] = 500;
	arglist[1] = 1.;
	myMinuit->mnexcm("MIGRAD", arglist, 2, ierflg); //minimization

	//Retrieve best-fit parameters
	vector< double > bestFitParams, e_bestFitParams;
	for(int i=0; i<npar; i++)
	{
		double par, epar;
		myMinuit->GetParameter(i, par, epar); //retrieve best fit parameters
		bestFitParams.push_back(par);
		e_bestFitParams.push_back(epar);
	}

	//Store the best-fit spline in a TGraph
	gsl_spline_init (spline_GLOB, &xData[0], &bestFitParams[0], xData.size()); //initialize the spline

	int nPointsSpline = int ((xData[nPoints-1]-xData[0])/stepSpline); //calculate the number of points of the spline
	vector< double > xSpline, ySpline;
	for (int i= 0; i < nPointsSpline; i++){
		xSpline.push_back(xData[0]+i*stepSpline);
		ySpline.push_back(gsl_spline_eval (spline_GLOB, xSpline.back(), acc_GLOB));
	}

	//Construct a vector of vectors that will store the xSpline values and the ySpline values
	vector< vector<double> > cSplineValues;
	cSplineValues.push_back(xSpline);
	cSplineValues.push_back(ySpline);

	return cSplineValues;
}
Ejemplo n.º 11
0
void AverageMW()
{
   // include NuTeV?
   if( inclNuTeV ) nVals = 5;
   else            nVals = 4;

   // number of parameters
   const int npar = 1;
   
   // create minuit pointer
   TMinuit *minuit = new TMinuit( npar );
   
   // set FCN function
   minuit->SetFCN( &fcn );
 
   // -1=quiet, 0=normal, 1=verbose
   minuit->SetPrintLevel( 0 );

   // start values
   Double_t startVal = 80;
   Double_t fitStep  = 0.1;
   Double_t limitMin = 70;
   Double_t limitMax = 90;
   minuit->DefineParameter( 0, "MW", startVal, fitStep, limitMin, limitMax);

   // minimize with MIGRAD
   Int_t ierr = 0;
   Double_t args[2];
   args[0] = 0; // maximum function calls (0=default)
   args[1] = 0; // tolerance at minimum (0=default)
   minuit->mnexcm( "MIGrad", args, 2, ierr );
   if ( ierr != 0 )
      cout << "Warning: Maybe fit didn't converge!" << endl;

   // fit results
   Double_t fitval_MW[1], fiterr_MW[1]; 
   minuit->GetParameter( 0, fitval_MW[0], fiterr_MW[0] );

   cout << "\n\n*************************************************" << endl;
   cout << "          chi2Min = " << chi2Min  << endl;
   cout << "          n_dof   = " << nVals-1  << endl;
   cout << "          p-value = " << TMath::Prob(chi2Min, nVals-1)  << endl;
   cout << "          MW      = " << fitval_MW[0] << " +- " << fiterr_MW[0] << endl;
   cout << "*************************************************" << endl;

   // make plot
   gStyle->SetOptTitle(0);
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(0);
   TCanvas* c = new TCanvas( "c", "Mean of W boson mass", 0, 0, 850, 500 );
   c->SetGridy();
   TH2F *frame = new TH2F("frame", "Mean of W boson mass", 1, 80, 80.6, 5, 0, 5. );
   frame->SetLineColor(0);
   frame->SetTickLength(0,"Y");
   frame->SetXTitle("M_{W} [GeV]");  
   frame->GetYaxis()->CenterLabels( 1 );
   frame->GetYaxis()->SetNdivisions( frame->GetNbinsY()+10, 1 );
   frame->GetYaxis()->SetBinLabel( 1, "" ); // no labels
   frame->Draw();
   
   // make labels
   MakeLabels(frame);
   
   // draw fit value
   DrawFitValue( frame, fitval_MW[0], fiterr_MW[0] );
      
   // draw measurements
   for( Int_t i=0; i<5; i++ ){
      DrawMeasurement( frame, 4-i, val_MW[i], err_MW[i] ); 
   }

   // Redraw axis
   frame->Draw("sameaxis");
   
}
Ejemplo n.º 12
0
//_________________________________________________________________________________
void integratedSplinesV5(double seed = 231)
{
    //Load the data
    int nEvents = 1000; //number of times the data will be randomized
    int nPoints = 9;
    double lowerBound = 10; //bounds for random vector function
    double upperBound = 20;
    double lowerErrorBound = 1;
    double upperErrorBound = 2;

    vector< vector<double> > xEvents, yEvents, yErrorEvents; //each of these is a vector of vectors (of randomized data points)
    for(int i = 0; i < nEvents; i++)
    {
        vector <double> xData, yData, yErrorData; //temporary vectors that are only used to get random values from FillRand function
        FillRandVectors(nPoints, xData, yData, yErrorData, seed*(i+1), lowerBound, upperBound, lowerErrorBound, upperErrorBound); //populates random vectors for y values and y error vector
        xEvents.push_back(xData);
        yEvents.push_back(yData);
        yErrorEvents.push_back(yErrorData);
        //Populate the global variables
        xData_GLOB = xData;
        yData_GLOB = yData;
        yErrorData_GLOB = yErrorData;
    }

    //Intialization of the variables
    const int npar = nPoints;
    const int orderSpline = 4;
    const int nbreak = npar+2-orderSpline;
    double stepSpline = 0.01;
    double xminBSplineWorkspace = 0;
    double xmaxBSplineWorkspace = 9;
    double startCSplineWorkspace = 15.;
    double stepCSplineWorkspace = 1.5;

    acc_GLOB = gsl_interp_accel_alloc ();
    spline_GLOB = gsl_spline_alloc (gsl_interp_cspline, nPoints);
    gsl_bspline_workspace *bw = gsl_bspline_alloc(orderSpline, nbreak);

//Setup for the C-spline_________________________________________________________________________

    //Setting up TMinuit for C-spline minimization
    TMinuit *myMinuit = new TMinuit(npar);  //initialize TMinuit with a maximum of npar
    myMinuit->SetFCN(fcn);
    myMinuit->SetPrintLevel(-1); //No output: -1, output:1

    double arglist[10];
    int ierflg = 0;
    arglist[0] = 1;
    myMinuit->mnexcm("SET ERR", arglist, 1, ierflg);

    vector<double> vstart, vstep;
    for(int i=0; i < npar; i++) 	//set starting values and step sizes for parameters
    {
        vstart.push_back(startCSplineWorkspace);
        vstep.push_back(stepCSplineWorkspace);
    }

    for (int i = 0; i < npar; i++)
    {
        stringstream ss;
        ss<<"a"<<i;
        myMinuit->mnparm(i, ss.str().c_str(), vstart.at(i), vstep.at(i), 0, 0, ierflg);
    }

    //Perform the Minuit fit
    arglist[0] = 500;
    arglist[1] = 1.;

//Setup for the B-spline_________________________________________________________________________
    //Declare and allocate memory to compose data set of control points
    gsl_vector *xControl = gsl_vector_alloc(nPoints);
    gsl_vector *yControl = gsl_vector_alloc(nPoints);
    gsl_vector *w = gsl_vector_alloc(nPoints);
    gsl_vector *B = gsl_vector_alloc(npar);
    gsl_matrix *X = gsl_matrix_alloc(nPoints, npar);

    //Create a b spline workspace and allocate its memory
    gsl_bspline_knots_uniform(xminBSplineWorkspace, xmaxBSplineWorkspace, bw);
    //Set up the variables for the fit matrix
    for (int i = 0; i < nPoints; ++i)
    {
        gsl_bspline_eval(gsl_vector_get(xControl, i), B, bw); //Compute B_j(xi) for all j
        for (int j = 0; j < npar; ++j)	gsl_matrix_set(X, i, j, gsl_vector_get(B, j)); //Fill in row i of X
    }

    gsl_vector *c = gsl_vector_alloc(npar);
    gsl_multifit_linear_workspace *mw = gsl_multifit_linear_alloc(nPoints, npar);
    gsl_matrix *cov = gsl_matrix_alloc(npar, npar);

//B and C spline loops__________________________________________________________________________
    clock_t tbstart, tbstop, tcstart, tcstop;
    vector <double> timeb, timec;

    vector< vector<double> > xBSplineValues, yBSplineValues;
    vector< vector<double> > xCSplineValues, yCSplineValues;

    for(int i = 0; i < (int)xEvents.size(); i++)
    {
        //Populate gsl vectors with the appropriate data
        for (int j = 0; j < nPoints; j++)
        {
            double xi = xEvents[i][j];
            double yi = yEvents[i][j];
            double sigma = yErrorEvents[i][j];
            gsl_vector_set(xControl, j, xi);
            gsl_vector_set(yControl, j, yi);
            gsl_vector_set(w, j, 1.0/(sigma*sigma));
        }

        tbstart = clock();
        vector< vector<double> > bSplineValues = bSpline(xEvents.at(i),stepSpline, bw, yControl, B, X, c, mw, cov);
        tbstop = clock();
        timeb.push_back(((float)tbstop-(float)tbstart)/ (CLOCKS_PER_SEC/1000.) );

        xBSplineValues.push_back(bSplineValues.at(0));
        yBSplineValues.push_back(bSplineValues.at(1));

        tcstart = clock();
        vector< vector<double> > cSplineValues = cSpline(nPoints, npar, xEvents.at(i), stepSpline, myMinuit, arglist, ierflg);
        tcstop = clock();
        timec.push_back(((float)tcstop-(float)tcstart)/ (CLOCKS_PER_SEC/1000.) );

        xCSplineValues.push_back(cSplineValues.at(0));
        yCSplineValues.push_back(cSplineValues.at(1));
    }

    //Histograms______________________________________________________________________________________

    //Time
    int nbins = 100;
    double xlow = 0;
    double xup = 1.;

    TH1D *hTimeB = new TH1D("Time","Timing; time [ms]; Number of Events", nbins, xlow, xup);
    hTimeB->SetStats(0);
    TH1D *hTimeC = new TH1D("TimeC","Timing; time [ms]; Number of Events", nbins, xlow, xup);
    hTimeC->SetLineColor(kRed);
    hTimeC->SetStats(0);

    for(int i=0; i<(int)timec.size(); i++)
    {
        hTimeB->Fill(timeb.at(i));
        hTimeC->Fill(timec.at(i));
    }

    //Interpolation distance
    vector <double> interpB, interpC;
    for(int i = 0; i < (int)yEvents.size(); i++)
    {
        for(int j = 0; j < (int)yEvents[i].size(); j++)
        {
            double key = xEvents[i][j];
            int indexForB = binarySearch(xBSplineValues[i], key);
            int indexForC = binarySearch(xCSplineValues[i], key);
            if(indexForB != -1)
                interpB.push_back( (yEvents[i][j]-yBSplineValues[i][indexForB])/yErrorEvents[i][indexForB] );
            if(indexForC != -1)
                interpC.push_back( (yEvents[i][j]-yCSplineValues[i][indexForC])/yErrorEvents[i][indexForC] );
        }
    }

    int nbinsI = 40;
    int xlowI = -4;
    int xupI = 4;
    TH1D *hInterpB = new TH1D("Interp B","Interpolation; Distance between spline and data normalized by error; Number of Events", nbinsI, xlowI, xupI);
    for(int i=0; i<(int)interpB.size(); i++) hInterpB->Fill(interpB.at(i));
    hInterpB->SetStats(0);


    TH1D *hInterpC = new TH1D("Interp C","Interpolation; Distance between spline and data normalized by error; Number of Events", nbinsI, xlowI, xupI);
    for (int i=0; i<(int)interpC.size(); i++) hInterpC->Fill(interpC.at(i));
    hInterpC->SetLineColor(kRed);
    hInterpC->SetStats(0);

    //Draws______________________________________________________________________________________

    //Interpolation
    TLegend *legInterp = new TLegend(0.9,0.70,0.75,0.85);
    legInterp->SetLineColor(kWhite);
    legInterp->SetFillColor(kWhite);
    legInterp->SetMargin(0.3);
    legInterp->AddEntry(hInterpB,"b-spline","l");
    legInterp->AddEntry(hInterpC,"c-spline","l");
    legInterp->SetTextSize(0.05);

    TCanvas *c1 = new TCanvas("c1", "Interpolation distance");
    c1->cd();
    hInterpB->Draw("");
    hInterpC->Draw("same");
    legInterp->Draw();

    //Time
    TLegend *legTime = new TLegend(0.9,0.70,0.75,0.85);
    legTime->SetLineColor(kWhite);
    legTime->SetFillColor(kWhite);
    legTime->SetMargin(0.3);
    legTime->AddEntry(hTimeB,"b-spline","l");
    legTime->AddEntry(hTimeC,"c-spline","l");
    legTime->SetTextSize(0.05);

    TCanvas *c2 = new TCanvas("c2", "Computation time");
    c2->cd();
    hTimeB->Draw("");
    hTimeC->Draw("same");
    legTime-> Draw();

    //Free the memory____________________________________________________________________________
    gsl_spline_free (spline_GLOB);
    gsl_interp_accel_free (acc_GLOB);
    gsl_bspline_free(bw);
    gsl_vector_free(xControl);
    gsl_vector_free(yControl);
    gsl_vector_free(B);
    gsl_matrix_free(X);
    gsl_vector_free(c);
    gsl_multifit_linear_free(mw);
    gsl_matrix_free(cov);
}
Ejemplo n.º 13
0
int main(int argc, char *argv[])
{
  setenv("TZ","UTC",1);
  tzset();
  gROOT->SetStyle("Plain");

  static struct RecEvent RecEvent30;
  static struct RecEvent RecEvent17;
  static struct channel_profiles channel_profiles30;
  static struct channel_profiles channel_profiles17;
  static struct h3 h330;
  static struct h3 h317;

  //get pointer to data
  TChain *TRecEvent30 = new TChain ("TRecEvent");
  CreateTRecEventChain30(TRecEvent30);
  cout << "TRecEvent entries = " << TRecEvent30->GetEntries() << endl;
  CreateTRecEvent(TRecEvent30, &RecEvent30);
  CreateTGrande(TRecEvent30, &h330, false);
  
  TChain *TRecEvent17 = new TChain ("TRecEvent");
  CreateTRecEventChain17(TRecEvent17);
  cout << "TRecEvent entries = " << TRecEvent17->GetEntries() << endl;
  CreateTRecEvent(TRecEvent17, &RecEvent17);
  CreateTGrande(TRecEvent17, &h317, false);

  char cut[2048];
  sprintf(cut, "Eg>0");
  sprintf(cut, "Eg>8.5 && abs(Zenith1Third)+2*Zenith1Third_err>Zeg && abs(Zenith1Third)-2*Zenith1Third_err<Zeg && LDFSlope<0");
//  sprintf(cut, "Eg>6 && abs(Zenith1Third)+2*Zenith1Third_err>Zeg && abs(Zenith1Third)-2*Zenith1Third_err<Zeg && event_id!=2768428"); //slope!!
  cout << "cut = " << cut << endl;
  
  
  TRecEvent30->Draw(">>ListRecEvent30",cut);
  TEventList *EventList30 = (TEventList*) gDirectory->Get("ListRecEvent30");
  if(EventList30==NULL) cerr << "error - EventList30 has null entries" << endl;
  TRecEvent30->SetEventList(EventList30);
  cout << "30: events to analyse = " <<  EventList30->GetN() << endl;
  
  TRecEvent17->Draw(">>ListRecEvent17",cut);
  TEventList *EventList17 = (TEventList*) gDirectory->Get("ListRecEvent17");
  if(EventList17==NULL) cerr << "error - EventList17 has null entries" << endl;
  TRecEvent17->SetEventList(EventList17);
  cout << "17: events to analyse = " <<  EventList17->GetN() << endl;
  
  TChain *Tchannel_profile30 = new TChain("Tchannel_profile");
  int NoChannels30;
  Tchannel_profile30->AddFile("~/analysis/AnaFlag/AF_D30_run21-49_v28.root");
  CreateTchannel_profiles(Tchannel_profile30, &channel_profiles30);
  NoChannels30 = Tchannel_profile30->GetEntries();
  cout << "Tchannel_profile30 entries = " << NoChannels30 << endl;

  TChain *Tchannel_profile17 = new TChain("Tchannel_profile");
  int NoChannels17;
  Tchannel_profile17->AddFile("~/analysis/AnaFlag/AF_D17_run12-60_v28.root");
  CreateTchannel_profiles(Tchannel_profile17, &channel_profiles17);
  NoChannels17 = Tchannel_profile17->GetEntries();
  cout << "Tchannel_profile17 entries = " << NoChannels17 << endl;
  
  
  gEntries = 0;
  float errScale = 1;
  float errScale2 = 0.14; //0.0737; //0.048*1;
  
  bool bNS = false, bEW = false;
  //fill data pointers
  for(int i=0; i<EventList30->GetN(); i++){
    TRecEvent30->GetEntry(EventList30->GetEntry(i));
    bNS = false;
    bEW = false;
    for(int k=0; k<RecEvent30.DetCh; k++){
      for(int j=0; j<NoChannels30; j++){
        Tchannel_profile30->GetEntry(j);
	if((int)channel_profiles30.ch_id == RecEvent30.channel_id[k]){
	  if( strncmp(channel_profiles30.polarisation,"East",4) == 0 ){
	    bEW = true;
	    gFieldStrengthEW[gEntries]    = RecEvent30.FieldStrengthChannel[k];
	    gFieldStrengthEWErr[gEntries] = RecEvent30.ChannelErrBackground[k]*errScale + RecEvent30.FieldStrengthChannel[k]*errScale2;
	    gFieldStrengthAnt[gEntries]   = RecEvent30.FieldStrengthAntenna[k];
	    gDistanceShowerAxis[gEntries] = RecEvent30.DistanceShowerAxis[k];
	  }
	  else{
	    bNS = true;
	    gFieldStrengthNS[gEntries]    = RecEvent30.FieldStrengthChannel[k];
	    gFieldStrengthNSErr[gEntries] = RecEvent30.ChannelErrBackground[k]*errScale + RecEvent30.FieldStrengthChannel[k]*errScale2;
	    gFieldStrengthAnt[gEntries]   = RecEvent30.FieldStrengthAntenna[k];
	    gDistanceShowerAxis[gEntries] = RecEvent30.DistanceShowerAxis[k];
	  }
	  if(bNS && bEW){
	    gAzimuth[gEntries] = h330.Azg;
            while(gAzimuth[gEntries]>360) gAzimuth[gEntries] -= 360;
	    gBAngle[gEntries] = RecEvent30.BAngle;
            while(gBAngle[gEntries]>360) gBAngle[gEntries] -= 360;
	    gZenith[gEntries] = h330.Zeg;	    
	    gEg[gEntries] = h330.Eg;
	    
	   if(0){
              cout << gAzimuth[gEntries] << " -- " << gBAngle[gEntries] << endl;
              cout << channel_profiles30.polarisation << endl;
              cout << gFieldStrengthNS[gEntries] << " -- " << gFieldStrengthEW[gEntries] <<  " ----- " << gFieldStrengthNS[gEntries]/gFieldStrengthEW[gEntries] << endl;
	      cout << gFieldStrengthAnt[gEntries] << endl;
              cout << h330.Zeg << " -- " << RecEvent30.Zenith1Third << " -- " << RecEvent30.Zenith1Third_err << endl;
	      cout << RecEvent30.event_id << endl;
              cout << endl;
            }

	    gEntries++;
	    bNS = false;
	    bEW = false;
	  }
	  break;
	}
      }
    }
   }

  if(true)
  for(int i=0; i<EventList17->GetN(); i++){
    TRecEvent17->GetEntry(EventList17->GetEntry(i));
    bNS = false;
    bEW = false;
    for(int k=0; k<RecEvent17.DetCh; k++){
      for(int j=0; j<NoChannels17; j++){
        Tchannel_profile17->GetEntry(j);
	if((int)channel_profiles17.ch_id == RecEvent17.channel_id[k]){
	  if( strncmp(channel_profiles17.polarisation,"East",4) == 0 ){
	    bEW = true;
	    gFieldStrengthEW[gEntries]    = RecEvent17.FieldStrengthChannel[k];
	    gFieldStrengthEWErr[gEntries] = RecEvent17.ChannelErrBackground[k]*errScale + RecEvent17.FieldStrengthChannel[k]*errScale2;
	    gFieldStrengthAnt[gEntries]   = RecEvent17.FieldStrengthAntenna[k];
	    gDistanceShowerAxis[gEntries] = RecEvent17.DistanceShowerAxis[k];
	  }
	  else{
	    bNS = true;
	    gFieldStrengthNS[gEntries]    = RecEvent17.FieldStrengthChannel[k];
	    gFieldStrengthNSErr[gEntries] = RecEvent17.ChannelErrBackground[k]*errScale + RecEvent17.FieldStrengthChannel[k]*errScale2;
	    gFieldStrengthAnt[gEntries]   = RecEvent17.FieldStrengthAntenna[k];
	    gDistanceShowerAxis[gEntries] = RecEvent17.DistanceShowerAxis[k];
	  }
	  if(bNS && bEW){
	    gAzimuth[gEntries] = h317.Azg;
            while(gAzimuth[gEntries]>360) gAzimuth[gEntries] -= 360;
	    gBAngle[gEntries] = RecEvent17.BAngle;
            while(gBAngle[gEntries]>360) gBAngle[gEntries] -= 360;
    	    gZenith[gEntries] = h317.Zeg;
	    gEg[gEntries] = h317.Eg;

	    
	    
	   if(0){
	      cout << "index = " << i << endl;
              cout << gAzimuth[gEntries] << " -- " << gBAngle[gEntries] << endl;
              cout << channel_profiles17.polarisation << endl;
              cout << gFieldStrengthNS[gEntries] << " -- " << gFieldStrengthEW[gEntries] <<  " ----- " << gFieldStrengthNS[gEntries]/gFieldStrengthEW[gEntries] << endl;
	      cout << gFieldStrengthAnt[gEntries] << endl;
              cout << h317.Zeg << " -- " << RecEvent17.Zenith1Third << " -- " << RecEvent17.Zenith1Third_err << endl;
              cout << endl;
            }

	    gEntries++;
	    bNS = false;
	    bEW = false;
	  }
	  break;
	}
      }
    }
   }

   cout << gEntries << endl;
   if(NoChannels30+NoChannels17 > gMaxCh || gEntries > gMaxEvents) {
     cout << "constants limit reached" << endl;
     exit(1);
   }
 
   TMinuit *gMinuit = new TMinuit(10);
   gMinuit->SetPrintLevel(-1);
   Double_t arglist[10];
   Int_t ierflg = 0;
   int NoPar = 6;
   
   bool AntFit = false;
   
   if(AntFit){
   // Antenna Fit Settings
   //++++++++++++++++++++++
   cout << "Antenna Fit" << endl;
   cout << "+++++++++++" << endl;
   
   gMinuit->SetFCN(FcnParFS);
   
// no effect
  arglist[0] = 1;
  gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);

  arglist[0] = 1;
  gMinuit->mnexcm("SET STRATEGY", arglist, 1, ierflg);
  

   gMinuit->mnparm(0, "scale",  1., 0.01, 0, 0, ierflg);
   gMinuit->mnparm(1, "r0",    400., 1, 0, 0, ierflg);
   gMinuit->mnparm(2, "Epower",  1., 0.01, 0.8, 1.2, ierflg);
//   gMinuit->mnparm(2, "Epower",  1., 0.1, 0, 0, ierflg);
   gMinuit->mnparm(3, "Zphase",  0., 0.1, -TMath::Pi()*2, TMath::Pi()*2, ierflg);
   gMinuit->mnparm(4, "Bphase",  0., 0.1, -TMath::Pi()*2, TMath::Pi()*2, ierflg);
   gMinuit->mnparm(5, "offset",  2., 0.1, 0, 0, ierflg);
//   gMinuit->FixParameter(0);
//   gMinuit->FixParameter(2);

   gMinuit->FixParameter(3);
   gMinuit->FixParameter(4);
   gMinuit->FixParameter(5);
   }
   else{
   // Channel Fit Settings
   //+++++++++++++++++++++
   cout << "Channel Fit" << endl;
   cout << "+++++++++++" << endl;

   gMinuit->SetFCN(FcnParFSChannel);
   
// no effect
  arglist[0] = 1;
  gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);

  arglist[0] = 1;
  gMinuit->mnexcm("SET STRATEGY", arglist, 1, ierflg);
  
   gMinuit->mnparm(0, "scale",  1., 0.01, 0.1, 100, ierflg);
   gMinuit->mnparm(1, "r0",    400., 1, 100, 1000, ierflg);
   gMinuit->mnparm(2, "Epower",  1., 0.01, 0.8, 1.2, ierflg);
//   gMinuit->mnparm(2, "Epower",  1., 0.01, 0, 0, ierflg);
   gMinuit->mnparm(3, "Zphase",  0., 0.1, -TMath::Pi()*2, TMath::Pi()*2, ierflg);
   gMinuit->mnparm(4, "Bphase",  0., 0.1, -TMath::Pi()*2, TMath::Pi()*2, ierflg);
   gMinuit->mnparm(5, "offset",  2., 0.1, 0, 0, ierflg);
//   gMinuit->FixParameter(0);
//   gMinuit->FixParameter(2);

   gMinuit->FixParameter(3);
   gMinuit->FixParameter(4);
   gMinuit->FixParameter(5);
   }

   
   //scan par space
   for(int i=0; i<1; i++){
     gMinuit->mnexcm("SCA", arglist, 0, ierflg);
   }
   
// Now ready for minimization step
   arglist[0] = 50000; // steps of iteration
   arglist[1] = 1.; // tolerance
   gMinuit->mnexcm("MIGRAD", arglist ,2,ierflg); //MIGRAD
   cout << " MINIMIZE flag is " << ierflg << endl;
   if(ierflg!=0){
     cout << "+++++++++++++++++++++" << endl;
     cout << "+ ups.. check again +" << endl;
     cout << "+++++++++++++++++++++" << endl;
   }

   // MIGRAD: error optimiser
   if(ierflg==0 && 0){
     arglist[0] = 500000;
     gMinuit->mnexcm("MIGRAD", arglist ,1,ierflg);
     cout << " MIGRAD flag is " << ierflg << endl;
     if(ierflg!=0){
       cout << "+++++++++++++++++++++" << endl;
       cout << "+ ups.. check again +" << endl;
       cout << "+++++++++++++++++++++" << endl;
     }
   }

   // Minos: error optimiser
   if(ierflg==0 && 0){
     arglist[0] = 500000;
     gMinuit->mnexcm("MINOS", arglist ,1,ierflg);
     cout << " MINOS flag is " << ierflg << endl;
     if(ierflg!=0){
       cout << "+++++++++++++++++++++" << endl;
       cout << "+ ups.. check again +" << endl;
       cout << "+++++++++++++++++++++" << endl;
     }
   }

// Print results
   Double_t amin,edm,errdef;
   Int_t nvpar,nparx,icstat;
   gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
   gMinuit->mnprin(3,amin);
 
   cout << "prob = " << TMath::Prob(amin,gEntriesUsed-gMinuit->GetNumFreePars())*100 << " %" << endl;
   cout << "ndf = " << gEntriesUsed-gMinuit->GetNumFreePars() << endl;

   
   cout << "output" << endl;
//   gMinuit->mnexcm("SHO COV", arglist ,0,ierflg);
//   gMinuit->mnexcm("SHO COR", arglist ,0,ierflg);
//   gMinuit->mnexcm("SHO EIG", arglist ,0,ierflg);
   
   //get fit parameters
   double par[15], epar;
   for(int i=0; i<NoPar; i++){
     gMinuit->GetParameter(i,par[i],epar);
   }

   
   TFile *output = new TFile("FitParFieldStrength_out.root","RECREATE");

   int tcount = 0;
   float tmpBAngle, tmpZenith;
   float r0x[gMaxEvents], r0y[gMaxEvents];
   float bx[gMaxEvents], by[gMaxEvents];
   
   for(int i=0; i<gEntries; i++){
     if(gFieldStrengthAnt[i]>0 && gFieldStrengthNS[i]>0 && gFieldStrengthEW[i]>0){
       tmpBAngle = gBAngle[i] * TMath::Pi() / 180.;
       tmpZenith = gZenith[i] * TMath::Pi() / 180.;
       
       r0x[tcount] = -gDistanceShowerAxis[i];
       r0y[tcount] = (log( (gFieldStrengthAnt[i]/gEffBand) / ( par[0]*cos(tmpZenith+par[3])*(par[5]+sin(tmpBAngle+par[4]))*powf(10,par[2]*(gEg[i]-8))))  );
       //if(r0y[tcount] < -4 && r0x[tcount] > 50) cout << "E = " << gEg[i] << " --  i = " << i << endl;
       
       bx[tcount] = tmpBAngle;
       by[tcount] = gFieldStrengthAnt[i]/gEffBand / (par[0]*cos(tmpZenith+par[3])*exp(-gDistanceShowerAxis[i]/par[1])*powf(10,par[2]*(gEg[i]-8))) - par[5];
//       if(by[tcount]>1.5) cout << " E = " << gEg[i] << " -- i = " << i << endl;

       tcount++;
     }
   }
   TCanvas *can1 = new TCanvas("can1");
   can1->Divide(2,2);

   TGraph *gr0 = new TGraph(tcount,r0x, r0y);
   gr0->SetName("gr0");
   gr0->SetMarkerStyle(20);
   gr0->SetTitle("lateral distribution");

   TGraph *gb = new TGraph(tcount,bx, by);
   gb->SetName("gb");
   gb->SetMarkerStyle(20);
   gb->SetTitle("geomagnetic angle");
 
   can1->cd(1);
   gr0->Draw("ap");
   
   can1->cd(2);
   gb->Draw("ap");
 
 
   gr0->Write();
   gb->Write();
   can1->Write();
   output->Close();
   
   
   

}
Ejemplo n.º 14
0
void fit(boot &A,boot &B,boot &C,boot &D,bvec &X,bvec &Y)
{
  //copy X
  X_fit=new double[nens];
  for(int iens=0;iens<nens;iens++) X_fit[iens]=X[iens].med();
  Y_fit=new double[nens];
  err_Y_fit=new double[nens];
  
  TMinuit minu;
  minu.SetPrintLevel(-1);
  
  int npars=4;
  minu.DefineParameter(0,"A",0.0,0.0001,0,0);
  minu.DefineParameter(1,"B",0.0,0.0001,0,0);
  minu.DefineParameter(2,"C",0.0,0.0001,0,0);
  minu.DefineParameter(3,"D",0.0,0.0001,0,0);
  if(!include_a4)
    {
      minu.FixParameter(3);
      npars--;
    }
  if(!include_ml_term)
    {
      minu.FixParameter(1);
      npars--;
    }
  minu.SetFCN(chi2_wr);
  
  double C2;
  for(int iboot=0;iboot<nboot+1;iboot++)
    {
      if(iboot>0)
        minu.SetPrintLevel(-1);
      
      minu.DefineParameter(4,"a380",lat[0][iboot],0.0001,0,0);
      minu.DefineParameter(5,"a390",lat[1][iboot],0.0001,0,0);
      minu.DefineParameter(6,"a405",lat[2][iboot],0.0001,0,0);
      minu.DefineParameter(7,"a420",lat[3][iboot],0.0001,0,0);
      minu.FixParameter(4);
      minu.FixParameter(5);
      minu.FixParameter(6);
      minu.FixParameter(7);
      
      for(int iens=0;iens<nens;iens++)
        {
          Y_fit[iens]=Y.data[iens].data[iboot];
          err_Y_fit[iens]=Y.data[iens].err();
        }
      
      //minimize
      minu.Migrad();
      
      //get back parameters
      double dum;
      minu.GetParameter(0,A.data[iboot],dum);
      minu.GetParameter(1,B.data[iboot],dum);
      minu.GetParameter(2,C.data[iboot],dum);
      minu.GetParameter(3,D.data[iboot],dum);
      
      double lat_med[4]={lat[0].med(),lat[1].med(),lat[2].med(),lat[3].med()};
      if(iboot==nboot)
        {
          contr_flag=1;
          C2=chi2(A.data[iboot],B[iboot],C[iboot],D[iboot],lat_med);
          contr_flag=0;
        }
    }
  
  int ninc_ens=0;
  for(int iens=0;iens<nens;iens++)
    if(ibeta[iens]!=0 || include_380) ninc_ens++;
  
  //calculate the chi2
  cout<<"A=("<<A<<"), B=("<<B<<"), C=("<<C<<"), D=("<<D<<")"<<endl;
  cout<<"Chi2 = "<<C2<<" / "<<ninc_ens-npars<<" = "<<C2/(ninc_ens-npars)<<endl;
  
  delete[] X_fit;
  delete[] Y_fit;
  delete[] err_Y_fit;
}