// AddFitStarter_t.cpp // Test AddFitStarter. #include "AddFitStarter.h" #include #include #include #include #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" #include "trfbase/SurfTest.h" #include "trfbase/HitTest.h" #include "HTrack.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using namespace trf; //********************************************************************** ObjPtr create_HitTest2(const ObjData& data); class HitTest2 : public HitTest { public: // Return the type name. static TypeName get_type_name() { return "HitTest2"; } // Return the creator. static ObjCreator get_creator() { return create_HitTest2; } // Return the type. static Type get_static_type() { return get_creator(); } public: HitTest2(int ival) : HitTest(ival) { }; // Return the type. Type get_type() const { return get_static_type(); } // Write the object data. ObjData write_data() const { ObjData data("HitTest2"); data.add_bare_pointer("cluster",get_cluster()); data.add_int("ival",get_ival()); return data; } }; ObjPtr create_HitTest2(const ObjData& data) { assert( data.get_object_type() == "HitTest2" ); if ( data.get_object_type() != "HitTest2" ) return ObjPtr(0); int ival = data.get_int("ival"); ClusterPtr pclu; data.get_bare_pointer("cluster",pclu); HitTest* phit = new HitTest(ival); phit->set_parent_pointer(pclu); return ObjPtr(phit); } //********************************************************************** ObjPtr create_AddFitTest(const ObjData& data); class AddFitTest : public AddFitter { public: // Return the type name. static TypeName get_type_name() { return "AddFitTest"; } // Return the creator. static ObjCreator get_creator() { return create_AddFitTest; } // Return the type. static Type get_static_type() { return get_creator(); } private: int _param; void ostr(ostream& stream) const { cout << "AddFitTest with parameter " << _param; }; public: AddFitTest(int param) : _param(param) { }; // Return the type. Type get_type() const { return get_static_type(); } // Write the object data. ObjData write_data() const { ObjData data("AddFitTest"); data.add_int("param",_param); return data; } int fit(HTrack& trh) const { return _param; }; int add_hit(HTrack& trh, const HitPtr& phit) const { trh.add_hit(phit); return _param; }; }; ObjPtr create_AddFitTest(const ObjData& data) { assert( data.get_object_type() == "AddFitTest" ); int param = data.get_int("param"); return ObjPtr( new AddFitTest(param) ); } //********************************************************************** int main( ) { string component = "AddFitStarter"; 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; } //******************************************************************** cout << trf_format; cout << ok_prefix << "Test constructor." << endl; ObjTable::register_type(); ObjTable::register_type(); ObjTable::register_type(); ObjTable::register_type(); AddFitStarter starter(5); cout << starter << endl; //******************************************************************** cout << ok_prefix << "Register fitters." << endl; // We register the following: // 2 a // 3 b // 4 a a // 5 b a // 0 b b // 6 b a b // 7 b a b b // 8 b a b b b // 9 default (n > 5) AddFitterPtr pfit0(0); AddFitterPtr pfit1( new AddFitTest(1) ); AddFitterPtr pfit2( new AddFitTest(2) ); AddFitterPtr pfit3( new AddFitTest(3) ); AddFitterPtr pfit4( new AddFitTest(4) ); AddFitterPtr pfit5( new AddFitTest(5) ); AddFitterPtr pfit6( new AddFitTest(6) ); AddFitterPtr pfit7( new AddFitTest(7) ); AddFitterPtr pfit8( new AddFitTest(8) ); AddFitterPtr pfit9( new AddFitTest(9) ); HitPtr pahit1( new HitTest(1) ); HitPtr pbhit1( new HitTest2(2) ); cout << "HitTest type = " << pahit1->get_type() << endl; cout << "HitTest2 type = " << pbhit1->get_type() << endl; assert( pahit1->get_type() != pbhit1->get_type() ); assert( ! starter.register_fitter(pfit1,pahit1->get_type()) ); assert( starter.register_fitter(pfit2,pahit1->get_type()) ); assert( ! starter.register_fitter(pfit3,pbhit1->get_type()) ); starter.register_fitter(pfit4,pahit1->get_type(),pahit1->get_type()); starter.register_fitter(pfit5,pbhit1->get_type(),pahit1->get_type()); starter.register_fitter(pfit0,pbhit1->get_type(),pbhit1->get_type()); starter.register_fitter(pfit6,pbhit1->get_type(),pahit1->get_type(), pbhit1->get_type()); starter.register_fitter(pfit7,pbhit1->get_type(),pahit1->get_type(), pbhit1->get_type(),pbhit1->get_type()); starter.register_fitter(pfit8,pbhit1->get_type(),pahit1->get_type(), pbhit1->get_type(),pbhit1->get_type(), pbhit1->get_type()); cout << starter << endl; //******************************************************************** cout << ok_prefix << "Fit." << endl; ETrack tre( SurfacePtr(new SurfTest(10)) ); HTrack trh1(tre); // a assert( starter.add_hit(trh1,pahit1) == 2 ); assert( trh1.get_hits().size() == 1 ); // a b assert( starter.add_hit(trh1,pbhit1) == AddFitStarter::TYPES_NOT_FOUND ); assert( trh1.get_hits().size() == 1 ); // a a assert( starter.add_hit(trh1,pahit1) == 4 ); assert( trh1.get_hits().size() == 2 ); HTrack trh2(tre); // b assert( starter.add_hit(trh2,pbhit1) == 3 ); assert( trh2.get_hits().size() == 1 ); // b b assert( starter.add_hit(trh2,pbhit1) == AddFitStarter::TYPES_NOT_REGISTERED ); assert( trh2.get_hits().size() == 1 ); // b a assert( starter.add_hit(trh2,pahit1) == 5 ); assert( trh2.get_hits().size() == 2 ); // b a b assert( starter.add_hit(trh2,pbhit1) == 6 ); assert( trh2.get_hits().size() == 3 ); // b a b b assert( starter.add_hit(trh2,pbhit1) == 7 ); assert( trh2.get_hits().size() == 4 ); // b a b b b assert( starter.add_hit(trh2,pbhit1) == 8 ); assert( trh2.get_hits().size() == 5 ); // b a b b b b assert( starter.add_hit(trh2,pbhit1) == AddFitStarter::DEFAULT_NOT_REGISTERED ); assert( trh2.get_hits().size() == 5 ); starter.register_default_fitter(pfit9); cout << starter << endl; // b a b b b b (again) assert( starter.add_hit(trh2,pbhit1) == 9 ); assert( trh2.get_hits().size() == 6 ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; TrfObject::Type htype1 = HitTest::get_static_type(); TrfObject::Type htype2 = HitTest2::get_static_type(); { Ptr pfits( new AddFitStarter(3) ); pfits->register_default_fitter(pfit1); pfits->register_fitter( pfit1, htype1 ); pfits->register_fitter( pfit1, htype1, htype2 ); ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("fit1",pfit1); objstream.write_object("fits",pfits); cout << mystream.str() << endl; assert( ObjTable::has_object_name("fits") ); string::size_type pos = 0; assert( mystream.str().find("fits ",pos) != string::npos ); assert( mystream.str().find("AddFitStarter ",pos) != string::npos ); assert( mystream.str().find("max_hits",pos) != string::npos ); assert( mystream.str().find("3 ",pos) != string::npos ); assert( mystream.str().find("default_fitter",pos) != string::npos ); assert( mystream.str().find("fit1 ",pos) != string::npos ); #ifdef ObjData_supports_lists assert( mystream.str().find("fitters",pos) != string::npos ); assert( mystream.str().find("fit1 ",pos) != string::npos ); assert( mystream.str().find("fit1 ",pos) != string::npos ); assert( mystream.str().find("hit_types",pos) != string::npos ); assert( mystream.str().find("HitTest",pos) != string::npos ); ++pos; assert( mystream.str().find("HitTest",pos) != string::npos ); assert( mystream.str().find("HitTest2",pos) != string::npos ); #endif } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "\n[ afit1 AddFitTest param=1 ]"; istring += "\n[ afits AddFitStarter max_hits=3 default_fitter=@afit1 "; #ifdef ObjData_supports_lists istring += "\n fitters=@( afit1 afit1 )"; istring += "\n hit_types=string("; istring += "\n \"HitTest\" \":\""; istring += "\n \"HitTest\" \"HitTest2\" \":\" "; istring += "\n )"; #endif istring += "\n]"; cout << istring << endl; istringstream isstrm(istring); { StdObjStream obstr(isstrm); assert( obstr.read_object() == "afit1" ); // read add fitter AddFitterPtr pfit1; ObjTable::get_object("afit1",pfit1); assert( pfit1 != 0 ); assert( obstr.read_object() == "afits" ); // read add fit starter Ptr pfits; ObjTable::get_object("afits",pfits); assert( pfits != 0 ); cout << *pfits << endl; assert( pfits->get_type() == AddFitStarter::get_static_type() ); AddFitStarter::TypeList types; assert( pfits->get_fitter(types) == pfit1 ); #ifdef ObjData_supports_lists types.push_back(htype1); assert( pfits->get_fitter(types) == pfit1 ); types.push_back(htype2); assert( pfits->get_fitter(types) == pfit1 ); types.push_back(htype1); assert( pfits->get_fitter(types) == 0 ); #endif } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }