// ClusterFindCyl_t.cpp // Test component ClusterFindCyl. #include "ClusterFindCyl.h" #include #include #include #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" #include "trfbase/ETrack.h" #include "SurfCylinder.h" #include "HitCylPhi.h" #include "HitCylPhiZ.h" #include "trfutil/TRFMath.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using namespace trf; //********************************************************************** int main( ) { string component = "ClusterFindCyl"; 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(); cout << trf_format; cout << ok_prefix << "Enable tuple." << endl; assert( ! ClusterFindCyl::tuple_is_enabled() ); ClusterFindCyl::enable_tuple(); assert( ClusterFindCyl::tuple_is_enabled() ); ClusterFindCyl::disable_tuple(); assert( ! ClusterFindCyl::tuple_is_enabled() ); ClusterFindCyl::enable_tuple(); assert( ClusterFindCyl::tuple_is_enabled() ); //******************************************************************** cout << ok_prefix << "Test constructor." << endl; double r1 = 10.0; ClusterFindCyl find1(r1, false); cout << find1 << endl; //******************************************************************** cout << ok_prefix << "Check the surface." << endl; SurfCylinder srf1(r1); assert( srf1 == find1.get_surface() ); //******************************************************************** cout << ok_prefix << "Add phi clusters to finder." << endl; ClusterPtr pclu11 = new ClusCylPhi(r1,1.23,0.02); ClusterPtr pclu12 = new ClusCylPhi(r1,5.29,0.02); ClusterPtr pclu13 = new ClusCylPhi(r1,2.38+TWOPI,0.02); ClusterPtr pclu14 = new ClusCylPhi(r1,4.17,0.02); ClusterPtr pclu15 = new ClusCylPhi(r1,2.34,0.02); find1.add_cluster(pclu11); find1.add_cluster(pclu12); find1.add_cluster(pclu13); find1.add_cluster(pclu14); int stat = find1.add_cluster(pclu15); cout << find1 << endl; assert( ! stat ); assert( find1.get_clusters().size() == 5 ); //******************************************************************** cout << ok_prefix << "Find nearby clusters." << endl; TrackVector vec; vec(0) = 2.36; TrackError err; err(0,0) = 0.0001; err(1,1) = 0.01; err(2,2) = 0.01; err(3,3) = 0.01; err(4,4) = 0.01; ETrack tre1( SurfacePtr(srf1.new_pure_surface()), vec, err ); cout << tre1 << endl; ClusterList nearby_clusters = find1.get_clusters(tre1); assert( nearby_clusters.size() == 5 ); cout << *nearby_clusters.front() << endl; cout << *nearby_clusters.back() << endl; assert( *nearby_clusters.front() == *pclu11 ); assert( *nearby_clusters.back() == *pclu12 ); //******************************************************************** cout << ok_prefix << "Drop clusters." << endl; find1.drop_clusters(); assert( find1.get_clusters().size() == 0 ); assert( find1.get_clusters(tre1).size() == 0 ); //******************************************************************** cout << ok_prefix << "Find a single cluster." << endl; find1.add_cluster(pclu13); assert( find1.get_clusters().size() == 1 ); assert( find1.get_clusters(tre1).size() == 1 ); //******************************************************************** cout << ok_prefix << "Construct a phiz finder." << endl; double r2 = 20.0; bool stereo2 = true; ClusterFindCyl find2(r2,stereo2); cout << find2 << endl; //******************************************************************** cout << ok_prefix << "Add phiz clusters to finder." << endl; ClusterPtr pclu21 = new ClusCylPhiZ(r2,1.23,0.02,stereo2); ClusterPtr pclu22 = new ClusCylPhiZ(r2,5.29,0.02,stereo2); ClusterPtr pclu23 = new ClusCylPhiZ(r2,2.38-TWOPI,0.02,stereo2); ClusterPtr pclu24 = new ClusCylPhiZ(r2,4.17,0.02,stereo2); ClusterPtr pclu25 = new ClusCylPhiZ(r2,2.34,0.02,stereo2); find2.add_cluster(pclu21); find2.add_cluster(pclu22); find2.add_cluster(pclu23); find2.add_cluster(pclu24); stat = find2.add_cluster(pclu25); assert( ! stat ); assert( find2.get_clusters().size() == 5 ); //******************************************************************** cout << ok_prefix << "Find nearby phiz clusters." << endl; SurfCylinder srf2(r2); vec(1) = 35.0; vec(0) = 2.36 - 0.01*vec(1); ETrack tre2( SurfacePtr(srf2.new_pure_surface()), vec, err ); cout << tre2 << endl; nearby_clusters = find2.get_clusters(tre2); assert( nearby_clusters.size() == 5 ); cout << *nearby_clusters.front() << endl; cout << *nearby_clusters.back() << endl; assert( *nearby_clusters.front() == *pclu21 ); assert( *nearby_clusters.back() == *pclu22 ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; { ObjPtr pobj( new ClusterFindCyl(30,0.012,1) ); 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("ClusterFindCyl ",pos)) != string::npos ); assert( (pos=mystream.str().find("radius",pos)) != string::npos ); assert( (pos=mystream.str().find("30. ",pos)) != string::npos ); assert( (pos=mystream.str().find("stereo",pos)) != string::npos ); assert( (pos=mystream.str().find("true ",pos)) != string::npos ); assert( (pos=mystream.str().find("tuple_id",pos)) != string::npos ); assert( (pos=mystream.str().find("1 ",pos)) != string::npos ); } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ obj2 ClusterFindCyl"; istring += "\n radius=60."; istring += "\n stereo=true"; istring += "\n tuple_id=2"; istring += "\n]"; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "obj2" ); const ClusterFindCyl* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == ClusterFindCyl::get_static_type() ); assert( pobj->get_radius() == 60.0 ); assert( pobj->get_stereo() == true ); assert( pobj->get_tuple_id() == 2 ); } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }