// PropXYXY_t.cc // Test PropXYXY. #include "PropXYXY.h" #include "trfbase/PropStat.h" #include #include #include #include #include "objstream/StdObjStream.hpp" #include "SurfXYPlane.h" #include "trfbase/TrackVector.h" #include "trfbase/VTrack.h" #include "trfbase/ETrack.h" #include "trfutil/TRFMath.h" #include "trfutil/trfstream.h" #include "spacegeom/SpacePoint.h" #include "spacegeom/SpacePath.h" #ifndef DEFECT_NO_STDLIB_NAMESPACES using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; #endif using namespace trf; //********************************************************************** //Checker of correct z propagation namespace { void check_zero_propagation(const VTrack&trv0,const VTrack& trv,const PropStat& pstat); void check_derivatives(const Propagator& prop,const VTrack& trv0,const Surface& srf) ; void check_derivative(const Propagator& prop,const VTrack& trv0,const Surface& srf,int i,int j) ; } double check_dz(VTrack trv1, VTrack trv2) { double z1 = trv1.get_vector()(SurfXYPlane::IZ); double z2 = trv2.get_vector()(SurfXYPlane::IZ); double dzdu = trv1.get_vector()(SurfXYPlane::IDZDU); int sign_du = (trv1.is_forward())?1:-1; return (z2-z1)*dzdu*sign_du; } //********************************************************************** int main( ) { string ok_prefix = "PropXYXY (I): "; string error_prefix = "PropXYXY test (E): "; cout << ok_prefix << "-------- Testing component PropXYXY. --------" << endl; // Make sure assert is enabled. bool assert_flag = false; assert ( ( assert_flag = true, assert_flag ) ); if ( ! assert_flag ) { cerr << "Assert is disabled" << endl; return 1; } //******************************************************************** cout << trf_format; cout << ok_prefix << "Test constructor." << endl; PropXYXY prop(ObjDoublePtr(new ObjDouble(-2.0))); cout << prop << endl; //******************************************************************** // Here we propagate some tracks both forward and backward and then // each back to the original track. We check that the returned // track parameters match those of the original track. cout << ok_prefix << "Check reversibility." << endl; double u1[] ={10., 10., 10., 10., 10., 10.}; double u2[] ={10., 10., 10., 10., 10., 10.}; double phi1[]={ PI/2., PI/2., PI/2., PI/2., PI/2., PI/2.}; double phi2[]={5*PI/6., 5*PI/6., 5*PI/6., 5*PI/6., 5*PI/6., 5*PI/6.}; int sign_du[]={ 1, -1, -1, 1, 1, 1 }; double v[] ={ -2., 2., 40., 40., -2., -2. }; double z[] ={ 3., 3., 3., 3., 3., 3. }; double dvdu[]={-1.5, -1.5, 1.5, 1.5, -1.5, 0. }; double dzdu[]={ 2.3, -2.3, -2.3, 2.3, 0., 2.3 }; double qp[] ={ 0.05, -0.05, 0.05, -0.05, 0.05, 0.05 }; double maxdiff = 1.e-9; int ntrk = 6; int i; for ( i=0; i=0.); VTrack trv2b = trv1; pstat = prop.vec_dir_prop(trv2b,sxyp2,Propagator::BACKWARD); cout << pstat << endl; assert( pstat.backward() ); cout << " backward: " << trv2b << endl; assert(check_dz(trv1,trv2b)<=0.); VTrack trv2fb = trv2f; pstat = prop.vec_dir_prop(trv2fb,sxyp1,Propagator::BACKWARD_MOVE); cout << pstat << endl; assert( pstat.backward() ); cout << " f return: " << trv2fb << endl; assert(check_dz(trv2f,trv2fb)<=0.); VTrack trv2bf = trv2b; pstat = prop.vec_dir_prop(trv2bf,sxyp1,Propagator::FORWARD); cout << pstat << endl; assert( pstat.forward() ); cout << " b return: " << trv2bf << endl; assert(check_dz(trv2b,trv2bf)>=0.); double difff = sxyp1.vec_diff(trv2fb.get_vector(),trv1.get_vector()).amax(); double diffb = sxyp1.vec_diff(trv2bf.get_vector(),trv1.get_vector()).amax(); cout << "diffs: " << difff << ' ' << diffb << endl; assert( difff < maxdiff ); assert( diffb < maxdiff ); } //******************************************************************** // Repeat the above with errors. cout << ok_prefix << "Check reversibility with errors." << endl; double evv[] = { 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 }; double evz[] = { 0.01, -0.01, 0.01, -0.01, 0.01, -0.01 }; double ezz[] = { 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, }; double evdv[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double ezdv[] = { 0.04, -0.04, 0.04, -0.04, 0.04, -0.04, }; double edvdv[] = { 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 }; double evdz[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double edvdz[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double ezdz[] = { 0.04, -0.04, 0.04, -0.04, 0.04, -0.04 }; double edzdz[] = { 0.02, 0.02, 0.02, 0.02, 0.02, 0.02 }; double evqp[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double ezqp[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double edvqp[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double edzqp[] = { 0.004, -0.004, 0.004, -0.004, 0.004, -0.004 }; double eqpqp[] = { 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 }; maxdiff = 1.e-6; for ( i=0; ipure_equal(*psrf_to)); check_zero_propagation(trv0,trv,pstat); psrf_to = new SurfXYPlane(4.,PI/16.); trv = trv0; trv.set_forward(); pstat = prop0.vec_dir_prop(trv,*psrf_to,Propagator::NEAREST); assert( pstat.success() ); check_zero_propagation(trv0,trv,pstat); psrf_to = new SurfXYPlane(14.,PI/4.); trv = trv0; trv.set_forward(); pstat = prop0.vec_dir_prop(trv,*psrf_to,Propagator::NEAREST); assert( pstat.success() ); check_zero_propagation(trv0,trv,pstat); psrf_to = new SurfXYPlane(14.,PI/2.); trv = trv0; trv.set_forward(); pstat = prop0.vec_dir_prop(trv,*psrf_to,Propagator::NEAREST); assert( pstat.success() ); check_zero_propagation(trv0,trv,pstat); psrf_to = new SurfXYPlane(14.,PI); trv = trv0; trv.set_forward(); pstat = prop0.vec_dir_prop(trv,*psrf_to,Propagator::NEAREST); assert( pstat.success() ); check_zero_propagation(trv0,trv,pstat); psrf_to = new SurfXYPlane(14.,PI*5./4.); trv = trv0; trv.set_surface(SurfacePtr(new SurfXYPlane(14.,PI*7./4.))); trv.set_forward(); VTrack tmp = trv; VTrack der = trv; pstat = prop0.vec_dir_prop(trv,*psrf_to,Propagator::NEAREST); assert( pstat.success() ); check_zero_propagation(tmp,trv,pstat); check_derivatives(prop0,der,*psrf_to); } //******************************************************************** cout << ok_prefix << "Test cloning." << endl; assert( prop.new_propagator() != 0); //******************************************************************** cout << ok_prefix << "Test the field." << endl; assert( prop.get_bfield() == -2.0 ); //******************************************************************** ObjTable::register_type(); cout << ok_prefix << "Write object data." << endl; { ObjPtr pobj( new PropXYXY(ObjDoublePtr(new ObjDouble(12.3)))); ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("obj1",pobj); cout << mystream.str() << endl; assert( ObjTable::has_object_name("obj1") ); string::size_type pos = 0; assert( (pos=mystream.str().find( "obj1 ",pos)) != string::npos ); assert( (pos=mystream.str().find( "PropXYXY ",pos)) != string::npos ); assert( (pos=mystream.str().find( "bfield",pos)) != string::npos ); assert( (pos=mystream.str().find( "12.3 ",pos)) != string::npos ); } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ obj2 PropXYXY bfield=24.6 ]"; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "obj2" ); const PropXYXY* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == PropXYXY::get_static_type() ); assert( is_equal( pobj->get_bfield(), 24.6 ) ); } } //******************************************************************** /* { cout<<"===========================================\n"; PropXYXY p1(ObjDoublePtr(new ObjDouble(-2.))); PropXYXY p2(ObjDoublePtr(new ObjDouble(2.))); SurfXYPlane xy1(5.,0.); SurfXYPlane xy2(3.,0.); ETrack tre0(SurfacePtr(xy2.new_surface())); TrackVector vec;TrackError err; vec(0)=0.; vec(1)=0.; vec(2)=0.0; vec(3)=0.1; vec(4)=-0.1; err(0,0)=err(1,1)=err(2,2)=err(3,3)=err(4,4)=0.1; tre0.set_vector(vec); tre0.set_error(err); tre0.set_backward(); ETrack tre1(tre0),tre2(tre0); PropStat pstat=p1.err_dir_prop(tre1,xy1,Propagator::BACKWARD); assert(pstat.success()); pstat=p2.err_dir_prop(tre2,xy1,Propagator::BACKWARD); assert(pstat.success()); cerr< 1e-10 ) assert( fabs((dydx - didj)/didj) < 1e-4 ); else assert( fabs(dydx) < 1e-4 ); } }