// Hit_t.cpp // Test component Hit. #include "Hit.h" #include "HitTest.h" #include #include #include #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using trf::SurfacePtr; using trf::trf_format; //********************************************************************** int main( ) { string component = "Hit"; 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 constructors." << endl; ObjTable::register_type(); ObjTable::register_type(); ObjTable::register_type(); Ptr pstest1(new SurfTest(1)); const SurfTest& stest = *pstest1; Ptr pclu1(new ClusterTest(stest,3)); const Cluster& ct1 = *pclu1; cout << ct1 << endl; //******************************************************************** cout << ok_prefix << "Test surface." << endl; cout << stest << endl; const Surface& srfchk = ct1.get_surface(); cout << srfchk << endl; assert( stest.pure_equal(srfchk) ); //******************************************************************** cout << ok_prefix << "Fetch hits." << endl; ETrack tre( SurfacePtr(stest.new_pure_surface()) ); HitList hits; Ptr ct1ptr(pclu1); hits = ct1.predict(tre); HitList::iterator ihit; for( ihit=hits.begin(); ihit!=hits.end(); ++ihit ) cout << **ihit << endl; assert( hits.size() == 3 ); //******************************************************************** cout << ok_prefix << "Check hit size." << endl; const Hit& hit0 = *hits.front(); cout << "size = " << hit0.size() << endl; assert( hit0.size() == 2 ); //******************************************************************** cout << ok_prefix << "Check hit." << endl; const ClusterPtr pclus = hit0.get_cluster(); cout << ct1 << endl; cout << *pclus << endl; assert( &ct1 == pclus ); //******************************************************************** cout << ok_prefix << "Check hit surface." << endl; cout << ct1.get_surface(); cout << hit0.get_surface(); assert( &hit0.get_surface() == &ct1.get_surface() ); assert( hit0.get_surface() == ct1.get_surface() ); //******************************************************************** cout << ok_prefix << "Check hit data." << endl; cout << hit0.measured_vector() << endl; assert( hit0.measured_vector()(1) == 2.0 ); cout << hit0.measured_error() << endl; assert( hit0.measured_error()(1,1) == 4.0 ); cout << hit0.predicted_vector() << endl; assert( hit0.predicted_vector()(1) == 4.0 ); cout << hit0.predicted_error() << endl; assert( hit0.predicted_error()(1,1) == 6.0 ); cout << hit0.dhit_dtrack() << endl; assert( hit0.dhit_dtrack()(1,4) == 14.0 ); cout << hit0.difference_vector() << endl; assert( hit0.difference_vector().amax() == 2.0 ); //******************************************************************** cout << ok_prefix << "Test types." << endl; cout << ct1.get_type() << endl; assert( ct1.get_type() != 0 ); ClusterPtr pclu2 = new ClusterTest(stest,2); const Cluster& ct2 = *pclu2; cout << ct2.get_type() << endl; assert( ct1.get_type() == ct2.get_type() ); cout << pclu1->get_type() << endl; assert( ct1.get_type() == pclu1->get_type() ); cout << hit0.get_type() << endl; assert( hit0.get_type() != 0 ); assert( hit0.get_type() != ct1.get_type() ); ihit = hits.begin(); const Hit* phit = (*++ihit).pointer(); cout << phit->get_type() << endl; assert( hit0.get_type() == phit->get_type() ); //******************************************************************** cout << ok_prefix << "Test equalities." << endl; // hit assert( ct1 == ct1 ); assert( ! ( ct1 != ct1 ) ); assert( ! ( ct1 == ct2 ) ); assert( ct1 != ct2 ); assert( *pclu1 == ct1 ); assert( ct1 == *pclu1 ); assert( *pclu1 != ct2 ); assert( ct2 != *pclu1 ); // hit const ClusterPtr ct2ptr(pclu2); HitList hits2 = ct2.predict(tre); assert( hits2.size() == 2 ); const Hit& hit2 = *hits2.front(); assert( hit0 == hit0 ); assert( ! ( hit0 != hit0 ) ); assert( hit0 != hit2 ); assert( ! ( hit0 == hit2 ) ); //******************************************************************** cout << ok_prefix << "Test predicting with cluster pointer." << endl; HitList hits3 = pclu2->predict(tre); assert( hits3.size() == 2 ); const Hit& hit3 = *hits2.front(); assert( hit2 == hit3 ); //******************************************************************** { const McIdList& oids = pclu1->get_mc_ids(); assert( oids.size() == 0 ); } { const McIdList& oids = hit0.get_mc_ids(); assert( oids.size() == 0 ); } //******************************************************************** cout << ok_prefix << "Test output object stream." << endl; { ostringstream osstrm; StdObjStream objstrm(osstrm); assert( osstrm.str() == "" ); objstrm.write_object("stest1",pstest1); objstrm.write_object("clus1",pclu1); assert( osstrm.str() != "" ); HitList::const_iterator ihit = hits.begin(); objstrm.write_object("hit0",**ihit++); objstrm.write_object("hit1",**ihit++); cout << osstrm.str() << endl; } //******************************************************************** cout << ok_prefix << "Test input object stream." << endl; { string str; str += "[ stest2 SurfTest x=2. ]\n"; str += "[ clus5 ClusterTest surface=*stest1 npred=5 ]\n"; str += "[ hit51 HitTest cluster=*clus5 ival=51 ]\n"; str += "[ hit52 HitTest cluster=*clus5 ival=51 ]\n"; istringstream isstrm(str); StdObjStream objstrm(isstrm); // Read objects. assert( objstrm.read_object() == "stest2" ); assert( objstrm.read_object() == "clus5" ); assert( objstrm.read_object() == "hit51" ); assert( objstrm.read_object() == "hit52" ); // Fetch surface. Ptr pstest2; ObjTable::get_object("stest2",pstest2); cout << *pstest2 << endl; assert( *pstest2 == SurfTest(2) ); // Fetch cluster. ClusterPtr pclu; ObjTable::get_object("clus5",pclu); cout << *pclu << endl; assert( *pclu == ClusterTest(*pstest2,5) ); // Fetch hit. ConstHitPtr phit; ObjTable::get_object("hit51",phit); cout << *phit << endl; HitTest hit(51); hit.set_parent_pointer(pclu); assert( *phit == hit ); } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }