// AddFitCyl_Phi_Phi_t.cpp // Test AddFitCyl_Phi_Phi. #include "AddFitCyl_Phi_Phi.h" #include #include #include #include #include #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" #include "trfutil/trfstream.h" #include "trfutil/TRFMath.h" #include "trfbase/HitTest.h" #include "trfbase/PropDirectedTest.h" #include "trffit/HTrack.h" #include "trffit/AddFitterPtr.h" #include "HitCylPhi.h" #include "AddFitterTest.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; #ifndef DEFECT_CMATH_NOT_STD using std::fabs; #endif using namespace trf; // Assign track parameter indices. enum { IPHI = SurfCylinder::IPHI, IZ = SurfCylinder::IZ, IALF = SurfCylinder::IALF, ITLM = SurfCylinder::ITLM, IQPT = SurfCylinder::IQPT }; //********************************************************************** int main( ) { string component = "AddFitCyl_Phi_Phi"; string ok_prefix = component + " (I): "; string error_prefix = component + " test (E): "; cout << ok_prefix << "-------- Testing component " + component + ". --------" << 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; } //******************************************************************** ObjTable::register_type(); ObjTable::register_type(); ObjTable::register_type(); cout << trf_format; cout << ok_prefix << "Enable tuple." << endl; assert( ! AddFitCyl_Phi_Phi::tuple_is_enabled() ); AddFitCyl_Phi_Phi::enable_tuple(); assert( AddFitCyl_Phi_Phi::tuple_is_enabled() ); AddFitCyl_Phi_Phi::disable_tuple(); assert( ! AddFitCyl_Phi_Phi::tuple_is_enabled() ); AddFitCyl_Phi_Phi::enable_tuple(); assert( AddFitCyl_Phi_Phi::tuple_is_enabled() ); //******************************************************************** cout << ok_prefix << "Test constructor." << endl; AddFitTest ftest(1); AddFitTest2 ftest2; AddFitCyl_Phi_Phi fit(ObjDoublePtr(new ObjDouble(-2.0)),ftest2,1.0); cout << fit << endl; //******************************************************************** cout << ok_prefix << "Build 1st hit and track." << endl; double r1 = 20.0; ETrack tre( SurfacePtr(new SurfCylinder(r1)) ); HTrack trh(tre); double phi1 = 1.234; double dphi = 0.00567; ClusterPtr pclus1 = new ClusCylPhi(r1,phi1,dphi); HitList hits1 = pclus1->predict(tre); HitPtr phit1 = hits1.front(); cout << *phit1 << endl; cout << trh << endl; //******************************************************************** cout << ok_prefix << "Check 1st hit fails." << endl; int stat = fit.add_hit(trh,phit1); cout << stat << endl; assert( stat ); assert( trh.get_hits().size() == 0 ); //******************************************************************** cout << ok_prefix << "Add first hit with dummy fitter." << endl; stat = ftest.add_hit(trh,phit1); assert( stat == 0 ); assert( trh.get_hits().size() == 1 ); cout << "Track phi - hit phi: " << trh.get_track().get_vector()(IPHI) - phi1 << endl; assert( trh.get_track().get_vector()(IPHI) == phi1 ); //******************************************************************** cout << ok_prefix << "Try to add 2nd hit of wrong type." << endl; HitPtr phitw( new HitTest(1) ); stat = fit.add_hit(trh,phitw); cout << stat << endl; assert( stat ); assert( stat == AddFitCyl_Phi_Phi::WRONG_TYPE ); assert( trh.get_hits().size() == 1 ); //******************************************************************** cout << ok_prefix << "Add second hit." << endl; double r2 = 10.0; double phi2 = 1.235; ClusterPtr pclus2 = new ClusCylPhi(r2,phi2,dphi); HitList hits2 = pclus2->predict(tre); HitPtr phit2 = hits2.front(); cout << *phit2 << endl; stat = fit.add_hit(trh,phit2); cout << trh << endl; cout << stat << endl; assert( stat == 0 ); assert( trh.get_hits().size() == 2 ); cout << "Track phi - hit phi: " << trh.get_track().get_vector()(0) - phi2 << endl; double close = 1.e-8; assert( fabs( trh.get_track().get_vector()(IPHI) - phi2 ) < close ); // Check that sin(phi2) - sin(phi1) = 0.5*(r2-r1)*C = sin(alf) double alf = trh.get_track().get_vector()(IALF); double qpt = trh.get_track().get_vector()(IQPT); double crv = BFAC*2.0*qpt; double alf_check1 = asin(0.5*r2*crv); double alf_check2 = phi2 - phi1 + asin(0.5*r1*crv); cout << alf << " " << alf_check1 << " " << alf_check2 << endl; assert( fabs(alf-alf_check1) < close ); assert( fabs(alf-alf_check2) < close ); //******************************************************************** cout << ok_prefix << "Try to add third hit." << endl; stat = fit.add_hit(trh,phit1); cout << stat << endl; assert( stat ); assert( stat == AddFitCyl_Phi_Phi::WRONG_HIT_COUNT ); assert( trh.get_hits().size() == 2 ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; AddFitterPtr pfit( new AddFitTest(3) ); PropagatorPtr pprop( new PropTest ); { ObjPtr pobj( new AddFitCyl_Phi_Phi(ObjDoublePtr(new ObjDouble(-2.1)), *pfit,.5,pprop) ); ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("myfit",pfit); objstream.write_object("myprop",pprop); 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("AddFitCyl_Phi_Phi ",pos)) != string::npos ); assert( (pos=mystream.str().find("bfield",pos)) != string::npos ); assert( (pos=mystream.str().find("2.1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("ptmin",pos)) != string::npos ); assert( (pos=mystream.str().find(".5 ",pos)) != string::npos ); assert( (pos=mystream.str().find("fitter",pos)) != string::npos ); assert( (pos=mystream.str().find("myfit ",pos)) != string::npos ); assert( (pos=mystream.str().find("propagator",pos)) != string::npos ); assert( (pos=mystream.str().find("myprop ",pos)) != string::npos ); } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ obj2 AddFitCyl_Phi_Phi"; istring += "\n bfield=2.2"; istring += "\n ptmin=0.6"; istring += "\n fitter=*myfit"; istring += "\n propagator=@myprop"; istring += "\n]"; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "obj2" ); const AddFitCyl_Phi_Phi* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == AddFitCyl_Phi_Phi::get_static_type() ); assert( is_equal(pobj->get_bfield(), 2.2) ); assert( is_equal(pobj->get_ptmin(), 0.6) ); assert( &pobj->get_fitter() == pfit ); assert( pobj->get_propagator() == pprop ); } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }