void ChebyshevFitterTest::testFit() {
	ims::ChebyshevFitter fitter2(2);
	std::vector<double> a;
	std::vector<double> b;
	
	/* test identity transformation */
	a.push_back(-31.7);  b.push_back(-31.7);
	a.push_back(0.0);    b.push_back(0.0);
	a.push_back(2.0);    b.push_back(2.0);
	a.push_back(17.0);   b.push_back(17.0);
	a.push_back(22.0);   b.push_back(22.0);
	a.push_back(500.0);  b.push_back(500.0);
	a.push_back(2000.0); b.push_back(2000.0);
	a.push_back(3000.0); b.push_back(3000.0);
	
	std::auto_ptr<ims::PolynomialTransformation> pt1 = fitter2.fit(a.begin(), a.end(), b.begin(), b.end());
	
	std::vector<double>::const_iterator cit;
	for (cit = a.begin(); cit != a.end(); ++cit) {
		// TODO: it seems that CPPUNIT_ASSERT_DOUBLES_EQUAL does not fail if one of the doubles is NaN
		CPPUNIT_ASSERT_DOUBLES_EQUAL( *cit, pt1->transform(*cit), 1.0e-10 );
	}
	
	/* test a different transformation */
	// create a transformation
	ims::PolynomialTransformation pt_tmp(5);
	pt_tmp.setCoefficient(0, 3.7);
	pt_tmp.setCoefficient(1, 2.529);
	pt_tmp.setCoefficient(2, -19.3);
	pt_tmp.setCoefficient(3, 1409.7);
	pt_tmp.setCoefficient(4, 13.62);
	pt_tmp.setCoefficient(5, -7.0);
	
	// transform a to b using pt_tmp
	b.clear();
	//std::vector<double>::iterator it;
	for (cit = a.begin(); cit != a.end(); ++cit) {
		b.push_back(pt_tmp.transform(*cit));
	}
	
	// estimate 5th degree polynomial
	ims::ChebyshevFitter fitter5(5);
	std::auto_ptr<ims::PolynomialTransformation> pt2 = fitter5.fit(a.begin(), a.end(), b.begin(), b.end());
	
	// ... and compare to original
	for (size_t i = 0; i <= 5; ++i) {
		CPPUNIT_ASSERT( !isnan(pt2->getCoefficient(i)) );
		// leave 1.0e-5 in here, 1.0e-8 is too strict
		CPPUNIT_ASSERT_DOUBLES_EQUAL( pt_tmp.getCoefficient(i), pt2->getCoefficient(i), 1.0e-5 );
	}
}
Exemple #2
0
void Fit2(){
  ///  dz = zs-z = -s*vr *(z0-s*z)+s*dzt
  ///  dzs/dl = dz/dl +s*s*vr*dz/dl
  ///  d(dz/dl) = vr*dz/dl

  TLinearFitter fitter2(7, "hyp6");
  // parameters
  // 0 - P3   offset
  // 1 - P1   offset
  // 2 - vdr  factor
  //
  // 3 - side offset P3
  // 4 - side offset P1
  //
  // 5 - vdr difference -A side - C side
  // 6 - vdr gradient   -sin(alpha)*tl dependence 


  //  dz = zs-z = -s*vr *(z0-s*z)+s*dzt = [1] + [2]*(-s*(z0-s*z))+ [4]*s +[5]* (-s*s*(z0-s*z))      
  //  dz+= [6]*(-s*(z0-s*z))*sa
  //    
  //  d(dz/dl) = vr*dz/dl               = [0] + [2]*dz/dl        + [3]*s +[5]* s*dz/dl
  //   d(dz/dl)+= [6]*dz/dl*sa
  Double_t xxx[100];
  for (Int_t i=0;i<npoints;i++){
    for (Int_t jc =0;jc<10;jc++) xxx[jc]=0;
    // P1
    xxx[0] = 1;
    xxx[1] = -vside[i]*(250.0-vside[i]*vz[i]);
    xxx[3] = vside[i];
    xxx[4] = -vside[i]*vside[i]*(250.0-vside[i]*vz[i]);
    xxx[5] = -vside[i]*(250.0-vside[i]*vz[i])*vsa[i];
    fitter2.AddPoint(xxx,vdz[i],vez[i]);
    //P3
    for (Int_t jc =0;jc<10;jc++) xxx[jc]=0;
    xxx[1] = vtl[i];
    xxx[2] = vside[i];
    xxx[4] = vside[i]*vtl[i];
    xxx[5] = vtl[i]*vsa[i];
    fitter2.AddPoint(xxx,vdtl[i],vetl[i]);
  }
  fitter2.Eval();
  fitter2.GetParameters(fitParam2);  
  fitter2.GetErrors(errParam2);
  //  dz = zs-z = -s*vr *(z0-s*z)+s*dzt = [1] + [2]*(-s*(z0-s*z))+ [4]*s +[5]* (-s*s*(z0-s*z))      
  //  dz+= [6]*(-s*(z0-s*z))*sa
  //    
  //  d(dz/dl) = vr*dz/dl               = [0] + [2]*dz/dl        + [3]*s +[5]* s*dz/dl
  //   d(dz/dl)+= [6]*dz/dl*sa


  TString fstrP1f2 ="";
  TString fstrP3f2 ="";
  fstrP1f2+=fitParam2[1];fstrP1f2+="-side*(250.0-side*z)*(";fstrP1f2+=fitParam2[2];
  fstrP1f2+=")+side*("; fstrP1f2+=fitParam2[4];
  fstrP1f2+=")-side*side*(250.0-side*z)*("; fstrP1f2+=fitParam2[5];
  fstrP1f2+=")-side*(250.0-side*z)*sa*tl*(";fstrP1f2+=fitParam2[6];fstrP1f2+=")";
  //
  fstrP3f2+=fitParam2[0];fstrP3f2+="+tl*(";fstrP3f2+=fitParam2[2];
  fstrP3f2+=")+side*("; fstrP3f2+=fitParam2[3];
  fstrP3f2+=")+side*tl*("; fstrP3f2+=fitParam2[5];
  fstrP3f2+=")*tl*sa*("; fstrP3f2+=fitParam2[6];fstrP3f2+=")";
 
  //
  tree->SetAlias("fP1f2",fstrP1f2->Data());
  tree->SetAlias("fP3f2",fstrP3f2->Data());
  //
}