26 #include "CLHEP/Matrix/SymMatrix.h" 27 #include "CLHEP/Matrix/Matrix.h" 28 #include "CLHEP/Matrix/Vector.h" 32 #define NITER 1 // number of iterations 34 #define NLOOP 1000000 // number of time the test is repeted 38 template <
unsigned int NDIM1,
unsigned int NDIM2>
53 int test_clhep_kalman();
63 iret |= test_clhep_kalman();
78 { TestRunner<N1,N2> tr; if (tr.run()) return -1; } 83 #ifndef RUN_ALL_POINTS 160 for(
unsigned int i = start; i < len+start; ++i)
161 v[i] = r.
Rndm() + offset;
166 for(
unsigned int i = start; i < first+start; ++i)
167 for(
unsigned int j = start; j < second+start; ++j)
168 m(i,j) = r.
Rndm() + offset;
173 for(
unsigned int i = start; i < first+start; ++i) {
174 for(
unsigned int j = i; j < first+start; ++j) {
176 m(i,j) = r.
Rndm() + offset;
180 m(i,i) = r.
Rndm() + 3*offset;
191 int pr = std::cout.precision(4);
192 std::cout << std::setw(12) << s <<
"\t" <<
" Real time = " << time.
RealTime() <<
"\t(sec)\tCPU time = " 195 std::cout.precision(pr);
200 double refTime2[4] = { 393.81, 462.16, 785.50,10000 };
202 #define NMAX1 9 // matrix storese results from 2 to 10 203 #define NMAX2 7 // results from 2 to 8 208 typedef std::map<std::string, double>
a;
210 typedef std::map< std::string, M > ResultTable;
219 void Set(
const std::string &
name,
int dim1,
int dim2 ) {
224 if (fResult1.find(name) == fResult1.end() )
225 fResult1.insert(ResultTable::value_type(name, M() ) );
226 if (fResult2.find(name) == fResult2.end() )
227 fResult2.insert(ResultTable::value_type(name, M() ) );
231 std::string
name()
const {
return fName; }
233 void report(
double rt,
double ct) {
234 fResult1[fName](fDim1-2,fDim2-2) = rt;
235 fResult2[fName](fDim1-2,fDim2-2) = ct;
238 double smallSum(
const M &
m,
int cut = 6) {
241 for (
int i = 0; i<cut-1; ++i)
242 for (
int j = 0; j<cut-1; ++j)
248 double largeSum(
const M & m,
int cut = 6) {
251 for (
int i = 0; i<M::kRows; ++i)
252 for (
int j = 0; j<M::kCols; ++j)
253 if ( i > cut-2 || j > cut-2)
259 void print(std::ostream & os) {
260 std::map<std::string,double>
r1;
261 std::map<std::string,double>
r2;
262 os <<
"Real time results " << std::endl;
263 for (ResultTable::iterator itr = fResult1.begin(); itr != fResult1.end(); ++itr) {
264 std::string
type = itr->first;
265 os <<
" Results for " << type << std::endl;
266 os <<
"\n" << itr->second << std::endl << std::endl;
267 r1[
type] = smallSum(itr->second);
268 r2[
type] = largeSum(itr->second);
269 os <<
"\nTotal for N1,N2 <= 6 : " << r1[
type] << std::endl;
270 os <<
"\nTotal for N1,N2 > 6 : " << r2[
type] << std::endl;
272 os <<
"\n\nCPU time results " << std::endl;
273 for (ResultTable::iterator itr = fResult2.begin(); itr != fResult2.end(); ++itr) {
274 os <<
" Results for " << itr->first << std::endl;
275 os <<
"\n" << itr->second << std::endl << std::endl;
276 os <<
"\nTotal for N1,N2 <= 6 : " << smallSum(itr->second) << std::endl;
277 os <<
"\nTotal for N1,N2 > 6 : " << largeSum(itr->second) << std::endl;
281 os <<
"\n\n****************************************************************************\n";
282 os <<
"Root version: " <<
gROOT->GetVersion() <<
" " 283 <<
gROOT->GetVersionDate() <<
"/" <<
gROOT->GetVersionTime() << std::endl;
284 os <<
"****************************************************************************\n";
285 os <<
"\n\t ROOTMARKS for N1,N2 <= 6 \n\n";
287 os.setf(std::ios::right,std::ios::adjustfield);
288 for (std::map<std::string,double>::iterator i = r1.begin(); i != r1.end(); ++i) {
289 std::string
type = i->first;
290 os << std::setw(12) << type <<
"\t=\t" <<
refTime1[j]*800/r1[
type] << std::endl;
293 os <<
"\n\t ROOTMARKS for N1,N2 > 6 \n\n";
295 for (std::map<std::string,double>::iterator i = r1.begin(); i != r1.end(); ++i) {
296 std::string
type = i->first;
297 os << std::setw(12) << type <<
"\t=\t" <<
refTime2[j]*800/r2[
type] << std::endl;
303 void save(
const std::string & fileName) {
308 for (ResultTable::iterator itr = fResult1.begin(); itr != fResult1.end(); ++itr) {
309 int ret =
file.WriteObject(&(itr->second),(itr->first).c_str() );
310 if (ret ==0) std::cerr <<
"==> Error saving results in ROOT file " << fileName << std::endl;
314 for (ResultTable::iterator itr = fResult2.begin(); itr != fResult2.end(); ++itr) {
315 std::string typeName = itr->first +
"_2";
316 int ret =
file.WriteObject(&(itr->second), typeName.c_str() );
317 if (ret ==0) std::cerr <<
"==> Error saving results in ROOT file " << fileName << std::endl;
328 ResultTable fResult1;
329 ResultTable fResult2;
345 TestTimer(TimeReport &
r ) :
348 fName = fRep->name();
351 TestTimer(
double & t,
const std::string & s =
"") : fName(s), fTime(&t), fRep(0)
359 if (fRep) fRep->report( fWatch.RealTime(), fWatch.CpuTime() );
360 if (fTime) *fTime += fWatch.RealTime();
375 template <
unsigned int NDIM1,
unsigned int NDIM2>
398 std::cout <<
"****************************************************************************\n";
399 std::cout <<
"\t\tSMatrix kalman test " << first <<
" x " << second << std::endl;
400 std::cout <<
"****************************************************************************\n";
407 double x2sum = 0, c2sum = 0;
408 for (
int k = 0; k <
npass; k++) {
432 for(
int i = 0; i < second; i++)
436 std::cout <<
"pass " << k << std::endl;
438 std::cout <<
" K0 = " << K0 << std::endl;
439 std::cout <<
" H = " << K0 << std::endl;
440 std::cout <<
" Cp = " << Cp << std::endl;
441 std::cout <<
" V = " << V << std::endl;
442 std::cout <<
" m = " << m << std::endl;
443 std::cout <<
" xp = " << xp << std::endl;
449 double x2 = 0,
c2 = 0;
450 TestTimer t(gReporter);
469 Rinv = V; Rinv += H * tmp;
471 bool test = Rinv.Invert();
473 std::cout<<
"inversion failed" <<std::endl;
474 std::cout << Rinv << std::endl;
482 x2 =
Dot(vtmp, Rinv*vtmp);
486 std::cout <<
" Rinv =\n " << Rinv << std::endl;
487 std::cout <<
" C =\n " << C << std::endl;
495 for (
unsigned int i=0; i<
NDIM2; ++i)
496 for (
unsigned int j=0; j<
NDIM2; ++j)
504 double d = std::abs(x2sum-fX2sum);
505 if ( d > 1.
E-6 * fX2sum ) {
506 std::cout <<
"ERROR: difference found in x2sum = " << x2sum <<
"\tref = " << fX2sum <<
507 "\tdiff = " << d << std::endl;
510 d = std::abs(c2sum-fC2sum);
511 if ( d > 1.
E-6 * fC2sum ) {
512 std::cout <<
"ERROR: difference found in c2sum = " << c2sum <<
"\tref = " << fC2sum <<
513 "\tdiff = " << d << std::endl;
520 template <
unsigned int NDIM1,
unsigned int NDIM2>
545 std::cout <<
"****************************************************************************\n";
546 std::cout <<
"\t\tSMatrix_Sym kalman test " << first <<
" x " << second << std::endl;
547 std::cout <<
"****************************************************************************\n";
554 double x2sum = 0, c2sum = 0;
555 for (
int k = 0; k <
npass; k++) {
579 for(
int i = 0; i < second; i++)
583 std::cout <<
"pass " << k << std::endl;
585 std::cout <<
" K0 = " << K0 << std::endl;
586 std::cout <<
" H = " << K0 << std::endl;
587 std::cout <<
" Cp = " << Cp << std::endl;
588 std::cout <<
" V = " << V << std::endl;
589 std::cout <<
" m = " << m << std::endl;
590 std::cout <<
" xp = " << xp << std::endl;
596 double x2 = 0,
c2 = 0;
597 TestTimer t(gReporter);
600 MnSymMatrixNN RinvSym;
606 #define OPTIMIZED_SMATRIX_SYM 607 #ifdef OPTIMIZED_SMATRIX_SYM 615 #ifdef OPTIMIZED_SMATRIX_SYM 624 bool test = RinvSym.Invert();
626 std::cout<<
"inversion failed" <<std::endl;
627 std::cout << RinvSym << std::endl;
644 bool test = RinvSym.Invert();
646 std::cout<<
"inversion failed" <<std::endl;
647 std::cout << RinvSym << std::endl;
660 for (
unsigned int i=0; i<
NDIM2; ++i)
661 for (
unsigned int j=0; j<
NDIM2; ++j)
670 if (x2sum == 0 || c2sum == 0) {
671 std::cout <<
"WARNING: x2sum = " << x2sum <<
"\tc2sum = " << c2sum << std::endl;
682 template <
unsigned int NDIM1,
unsigned int NDIM2>
699 std::cout <<
"****************************************************************************\n";
700 std::cout <<
"\t\tTMatrix Kalman test " << first <<
" x " << second << std::endl;
701 std::cout <<
"****************************************************************************\n";
705 double x2sum = 0,c2sum = 0;
707 for (
int k = 0; k <
npass; k++)
710 MnMatrix
H(first,second);
711 MnMatrix K0(second,first);
712 MnMatrix Cp(second,second);
713 MnMatrix V(first,first);
726 MnMatrix
I(second,second);
727 for (
int i = 0; i < second; i++)
728 for(
int j = 0; j <second; j++) {
730 if (i==j)
I(i,i) = 1.0;
739 double x2 = 0,
c2 = 0;
750 TestTimer t(gReporter);
753 tmp1 =
m;
Add(tmp1,-1.0,H,xp);
754 x = xp;
Add(x,+1.0,K0,tmp1);
767 std::cout <<
" Rinv =\n "; Rinv.
Print();
768 std::cout <<
" RinvSym =\n "; RinvSym.
Print();
769 std::cout <<
" C =\n "; C.
Print();
776 for (
unsigned int i=0; i<
NDIM2; ++i)
777 for (
unsigned int j=0; j<
NDIM2; ++j)
790 double d = std::abs(x2sum-fX2sum);
791 if ( d > 1.
E-6 * fX2sum ) {
792 std::cout <<
"ERROR: difference found in x2sum = " << x2sum <<
"\tref = " << fX2sum <<
793 "\tdiff = " << d << std::endl;
796 d = std::abs(c2sum-fC2sum);
797 if ( d > 1.
E-6 * fC2sum ) {
798 std::cout <<
"ERROR: difference found in c2sum = " << c2sum <<
"\tref = " << fC2sum <<
799 "\tdiff = " << d << std::endl;
811 template <
unsigned int NDIM1,
unsigned int NDIM2>
812 int TestRunner<NDIM1,NDIM2>::test_clhep_kalman() {
817 typedef HepSymMatrix MnSymMatrix;
818 typedef HepMatrix MnMatrix;
819 typedef HepVector MnVector;
830 std::cout <<
"****************************************************************************\n";
831 std::cout <<
" CLHEP Kalman test " << first <<
" x " << second << std::endl;
832 std::cout <<
"****************************************************************************\n";
836 double x2sum = 0,c2sum = 0;
838 for (
int k = 0; k <
npass; k++)
842 MnMatrix
H(first,second);
843 MnMatrix K0(second,first);
844 MnMatrix Cp(second,second);
845 MnMatrix V(first,first);
857 MnSymMatrix
I(second,1);
860 double x2 = 0,
c2 = 0;
862 MnMatrix Rinv(first,first);
863 MnSymMatrix RinvSym(first);
864 MnMatrix
K(second,first);
865 MnSymMatrix
C(second);
866 MnVector vtmp1(first);
867 MnMatrix tmp(second,first);
869 TestTimer t(gReporter);
879 Rinv = V; Rinv += H * tmp;
880 RinvSym.assign(Rinv);
881 RinvSym.invert(ifail);
882 if (ifail !=0) { std::cout <<
"Error inverting Rinv" << std::endl;
break; }
886 C.assign( (I-K*H)*Cp );
887 x2= RinvSym.similarity(vtmp1);
888 if(ifail!=0) { std::cout <<
"Error inverting Rinv" << std::endl;
break; }
893 for (
unsigned int i=1; i<=
NDIM2; ++i)
894 for (
unsigned int j=1; j<=
NDIM2; ++j)
905 double d = std::abs(x2sum-fX2sum);
906 if ( d > 1.
E-6 * fX2sum ) {
907 std::cout <<
"ERROR: difference found in x2sum = " << x2sum <<
"\tref = " << fX2sum <<
908 "\tdiff = " << d << std::endl;
911 d = std::abs(c2sum-fC2sum);
912 if ( d > 1.
E-6 * fC2sum ) {
913 std::cout <<
"ERROR: difference found in c2sum = " << c2sum <<
"\tref = " << fC2sum <<
914 "\tdiff = " << d << std::endl;
923 int main(
int argc,
char *argv[]) {
927 std::cout <<
"\nERROR - stressKalman FAILED - exit!" << std::endl ;
931 gReporter.print(std::cout);
932 std::string fname =
"kalman";
934 std::string platf(argv[1]);
935 fname = fname +
"_" + platf;
938 gReporter.save(fname+
".root");
T Dot(const SVector< T, D > &lhs, const SVector< T, D > &rhs)
Vector dot product.
T Similarity(const SMatrix< T, D, D, R > &lhs, const SVector< T, D > &rhs)
Similarity Vector - Matrix Product: v^T * A * v returning a scalar value of type T ...
int test_tmatrix_kalman()
static long int sum(long int i)
void fillRandomSym(TRandom &r, M &m, unsigned int first, unsigned int start=0, double offset=1)
Random number generator class based on M.
int test_smatrix_kalman()
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
TMatrixTSym< Element > & Use(Int_t row_lwb, Int_t row_upb, Element *data)
virtual const Element * GetMatrixArray() const
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
int main(int argc, char *argv[])
Expr< TransposeOp< SMatrix< T, D1, D2, R >, T, D1, D2 >, T, D2, D1, typename TranspPolicy< T, D1, D2, R >::RepType > Transpose(const SMatrix< T, D1, D2, R > &rhs)
Matrix Transpose B(i,j) = A(j,i) returning a matrix expression.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
void fillRandomVec(TRandom &r, V &v, unsigned int len, unsigned int start=0, double offset=1)
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void Minus(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix summation. Create a matrix C such that C = A - B.
static const double x2[5]
SMatrix: a generic fixed size D1 x D2 Matrix class.
This is the base class for the ROOT Random number generators.
void MultT(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B^T.
virtual Double_t Rndm()
Machine independent random number generator.
R__EXTERN TSystem * gSystem
unsigned int r1[N_CITIES]
int test_smatrix_sym_kalman()
void run(bool only_compile=false)
static void Evaluate(SMatrix< T, D, D, MatRepSym< T, D > > &lhs, const Expr< A, T, D, D, R > &rhs)
assign a symmetric matrix from an expression
void printTime(TStopwatch &time, std::string s)
Double_t Dot(const TGLVector3 &v1, const TGLVector3 &v2)
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, const THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
TMatrixTSym< Element > & InvertFast(Double_t *det=0)
Invert the matrix and calculate its determinant.
TMatrixTSym< Element > & Similarity(const TMatrixT< Element > &n)
Calculate B * (*this) * B^T , final matrix will be (nrowsb x nrowsb) This is a similarity transform w...
void Print(Option_t *name="") const
Print the matrix as a table of elements.
void Plus(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix summation. Create a matrix C such that C = A + B.
void fillRandomMat(TRandom &r, M &m, unsigned int first, unsigned int second, unsigned int start=0, double offset=1)
SMatrix< T, D2, D2, MatRepSym< T, D2 > > SimilarityT(const SMatrix< T, D1, D2, R > &lhs, const SMatrix< T, D1, D1, MatRepSym< T, D1 > > &rhs)
Transpose Similarity Matrix Product : B = U^T * A * U for A symmetric returning a symmetric matrix ex...
void Mult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B.
unsigned int r2[N_CITIES]
SVector: a generic fixed size Vector class.