forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stressMathCore.cxx
1597 lines (1269 loc) · 45.5 KB
/
stressMathCore.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/test:$Id$
// Author: Lorenzo Moneta 06/2005
///////////////////////////////////////////////////////////////////////////////////
//
// MathCore Benchmark test suite
// ==============================
//
// This program performs tests :
// - mathematical functions in particular the statistical functions by estimating
// pdf, cdf and quantiles. cdf are estimated directly and compared with calculated integral from pdf
// - physics vectors (2D, 3D and 4D) including I/O for every type and for both double and Double32_t
// - SMatrix and SVectors including I/O for double and Double32_t types
// - I/O of complex objects which dictionary has been generated using CINT (default) or Reflex
// TrackD and TrackD32 which contain physics vectors of double and Double32_t
// TrackErrD and TrackErrD32 which contain physics vectors and an SMatrix of double and Double32_t
// VecTrackD which contains an std::vector<TrackD>
//
//
// the program cun run only in compiled mode.
// To run outside ROOT do:
//
// > cd $ROOTSYS/test
// > make stressMathMore
// > ./stressMathMore
//
// to run using REflex set before compiling the environment variable useReflex.
//
// > export useReflex=1
// > make stressMathMore
// > ./stressMathMore
//
// to run inside ROOT using ACliC
// for using CINT you need first to have the library libTrackMathCoreDict.so
// (type: make libTrackMathCoreDict.so to make it)
//
// root> gSystem->Load("libMathCore");
// root> gSystem->Load("libTree");
// root> gSystem->Load("libHist");
// root> .x stressMathCore.cxx+
//
// for using Reflex dictionaries you need first to have the library libTrackMathCoreRflx.so
// (type: make libTrackMathCoreRflx.so to make it)
//
// root> gSystem->Load("libMathCore");
// root> gSystem->Load("libTree");
// root> gSystem->Load("libHist");
// root> gSystem->Load("libReflex");
// root> gSystem->SetIncludePath("-DUSE_REFLEX");
// root> .x stressMathCore.cxx+
//
//
#ifndef __CINT__
#include "Math/DistFuncMathCore.h"
//#define USE_MATHMORE
#ifdef USE_MATHMORE
#include "Math/DistFuncMathMore.h"
#endif
#include "Math/IParamFunction.h"
#include "Math/Integrator.h"
#include "Math/IntegratorOptions.h"
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
#include "TBenchmark.h"
#include "TROOT.h"
#include "TRandom3.h"
#include "TSystem.h"
#include "TTree.h"
#include "TFile.h"
#include "TF1.h"
#include "TMath.h"
#include "Math/Vector2D.h"
#include "Math/Vector3D.h"
#include "Math/Vector4D.h"
#include "Math/VectorUtil.h"
#include "Math/SVector.h"
#include "Math/SMatrix.h"
R__ADD_INCLUDE_PATH($ROOTSYS/test)
#include "TrackMathCore.h"
#include "Math/GenVector/RotationZ.h" // Workaround to autoload libGenVector ROOT-7056
using namespace ROOT::Math;
#endif
//#define DEBUG
bool debug = true; // print out reason of test failures
bool debugTime = false; // print out separate timings for vectors
bool removeFiles = true; // remove Output root files
void PrintTest(std::string name) {
std::cout << std::left << std::setw(40) << name;
}
void PrintStatus(int iret) {
if (iret == 0)
std::cout <<"\t\t................ OK" << std::endl;
else
std::cout <<"\t\t............ FAILED " << std::endl;
}
int compare( std::string name, double v1, double v2, double scale = 2.0) {
// ntest = ntest + 1;
//std::cout << std::setw(50) << std::left << name << ":\t";
// numerical double limit for epsilon
double eps = scale* std::numeric_limits<double>::epsilon();
int iret = 0;
double delta = v2 - v1;
double d = 0;
if (delta < 0 ) delta = - delta;
if (v1 == 0 || v2 == 0) {
if (delta > eps ) {
iret = 1;
}
}
// skip case v1 or v2 is infinity
else {
d = v1;
if ( v1 < 0) d = -d;
// add also case when delta is small by default
if ( delta/d > eps && delta > eps )
iret = 1;
}
if (iret) {
if (debug) {
int pr = std::cout.precision (18);
std::cout << "\nDiscrepancy in " << name.c_str() << "() :\n " << v1 << " != " << v2 << " discr = " << int(delta/d/eps)
<< " (Allowed discrepancy is " << eps << ")\n\n";
std::cout.precision (pr);
//nfail = nfail + 1;
}
}
//else
// std::cout <<".";
return iret;
}
#ifndef __CINT__
// trait class for distinguishing the number of parameters for the various functions
template<class Func, unsigned int NPAR>
struct Evaluator {
static double F(Func f, double x, const double * ) {
return f(x);
}
};
template<class Func>
struct Evaluator<Func, 1> {
static double F(Func f, double x, const double * p) {
return f(x,p[0]);
}
};
template<class Func>
struct Evaluator<Func, 2> {
static double F(Func f, double x, const double * p) {
return f(x,p[0],p[1]);
}
};
template<class Func>
struct Evaluator<Func, 3> {
static double F(Func f, double x, const double * p) {
return f(x,p[0],p[1],p[2]);
}
};
// global test variable
int NFuncTest = 100;
// statistical function class
// template on the number of parameters
template<class Func, class FuncQ, int NPAR, int NPARQ=NPAR-1>
class StatFunction : public ROOT::Math::IParamFunction {
public:
StatFunction(Func pdf, Func cdf, FuncQ quant) : fPdf(pdf), fCdf(cdf), fQuant(quant)
{
fScale1 = 1.0E6; //scale for cdf test (integral)
fScale2 = 10; //scale for quantile test
for(int i = 0; i< NPAR; ++i) fParams[i]=0;
}
unsigned int NPar() const { return NPAR; }
const double * Parameters() const { return fParams; }
ROOT::Math::IGenFunction * Clone() const { return new StatFunction(fPdf,fCdf,fQuant); }
void SetParameters(const double * p) { std::copy(p,p+NPAR,fParams); }
void SetParameters(double p0) { *fParams = p0; }
void SetParameters(double p0, double p1) { *fParams = p0; *(fParams+1) = p1; }
void SetParameters(double p0, double p1, double p2) { *fParams = p0; *(fParams+1) = p1; *(fParams+2) = p2; }
static void SetNTest(int n) { NFuncTest = n; }
double Cdf(double x) const {
return Evaluator<Func,NPAR>::F(fCdf,x, fParams);
}
double Quantile(double x) const {
double z = Evaluator<FuncQ,NPARQ>::F(fQuant,x, fParams);
if ((NPAR - NPARQ) == 1)
z += fParams[NPAR-1]; // adjust the offset
return z;
}
// test cumulative function
int Test(double x1, double x2, double xl = 1, double xu = 0, bool cumul = false);
void ScaleTol1(double s) { fScale1 *= s; }
void ScaleTol2(double s) { fScale2 *= s; }
private:
double DoEvalPar(double x, const double * ) const {
// implement explicitly using cached parameter values
return Evaluator<Func,NPAR>::F(fPdf,x, fParams);
}
Func fPdf;
Func fCdf;
FuncQ fQuant;
double fParams[NPAR];
double fScale1;
double fScale2;
};
// test cdf at value f
template<class F1, class F2, int N1, int N2>
int StatFunction<F1,F2,N1,N2>::Test(double xmin, double xmax, double xlow, double xup, bool c) {
int iret = 0;
// scan all values from xmin to xmax
double dx = (xmax-xmin)/NFuncTest;
#ifdef USE_MATHMORE
for (int i = 0; i < NFuncTest; ++i) {
double v1 = xmin + dx*i; // value used for testing
double q1 = Cdf(v1);
//std::cout << "v1 " << v1 << " pdf " << (*this)(v1) << " cdf " << q1 << " quantile " << Quantile(q1) << std::endl;
// calculate integral of pdf
Integrator ig(IntegrationOneDim::kADAPTIVESINGULAR, 1.E-12,1.E-12,100000);
ig.SetFunction(*this);
double q2 = 0;
if (!c) {
// lower intergal (cdf)
if (xlow >= xup || xlow > xmin)
q2 = ig.IntegralLow(v1);
else
q2 = ig.Integral(xlow,v1);
// use a larger scale (integral error is 10-9)
iret |= compare("test _cdf", q1, q2, fScale1 );
// test the quantile
double v2 = Quantile(q1);
iret |= compare("test _quantile", v1, v2, fScale2 );
}
else {
// upper integral (cdf_c)
if (xlow >= xup || xup < xmax)
q2 = ig.IntegralUp(v1);
else
q2 = ig.Integral(v1,xup);
iret |= compare("test _cdf_c", q1, q2, fScale1);
double v2 = Quantile(q1);
iret |= compare("test _quantile_c", v1, v2, fScale2 );
}
if (iret) {
std::cout << "Failed test for x = " << v1 << " p = ";
for (int j = 0; j < N1; ++j) std::cout << fParams[j] << "\t";
std::cout << std::endl;
break;
}
}
#else
// use TF1 for the integral
// std::cout << "xlow-xuo " << xlow << " " << xup << std::endl;
// std::cout << "xmin-xmax " << xmin << " " << xmax << std::endl;
double x1,x2 = 0;
if (xlow >= xup) {
x1 = -100; x2 = 100;
}
else if (xup < xmax) {
x1 = xlow; x2 = 100;
}
else {
x1=xlow; x2 = xup;
}
//std::cout << "x1-x2 " << x1 << " " << x2 << std::endl;
TF1 * f = new TF1("ftemp",ParamFunctor(*this),x1,x2,0);
//ROOT::Math::IntegratorOneDimOptions::SetDefaultIntegrator("Gauss");
for (int i = 0; i < NFuncTest; ++i) {
double v1 = xmin + dx*i; // value used for testing
double q1 = Cdf(v1);
double q2 = 0;
if (!c) {
q2 = f->Integral(x1,v1,1.E-12);
// use a larger scale (integral error is 10-9)
iret |= compare("test _cdf", q1, q2, fScale1 );
// test the quantile
double v2 = Quantile(q1);
iret |= compare("test _quantile", v1, v2, fScale2 );
}
else {
// upper integral (cdf_c)
q2 = f->Integral(v1,x2,1.E-12);
iret |= compare("test _cdf_c", q1, q2, fScale1);
double v2 = Quantile(q1);
iret |= compare("test _quantile_c", v1, v2, fScale2 );
}
// if (!c) std::cout << "i = " << i << " x1 = " << x1 << " v = " << v1 << " pdf " << (*this)(v1) << " cdf " << q1 << " Int(x1,v)= " << q2 << std::endl;
// else
// std::cout << "i = " << i << " x2 = " << x2 << " v = " << v1 << " pdf " << (*this)(v1) << " cdf_c " << q1 << " Int(v,x2)= " << q2 << std::endl;
}
delete f;
#endif
if (c || iret != 0) PrintStatus(iret);
return iret;
}
// typedef defining the functions
typedef double ( * F0) ( double);
typedef double ( * F1) ( double, double);
typedef double ( * F2) ( double, double, double);
typedef double ( * F3) ( double, double, double, double);
typedef StatFunction<F2,F2,2,2> Dist_beta;
typedef StatFunction<F2,F1,2> Dist_breitwigner;
typedef StatFunction<F2,F1,2> Dist_chisquared;
typedef StatFunction<F3,F2,3> Dist_fdistribution;
typedef StatFunction<F3,F2,3> Dist_gamma;
typedef StatFunction<F2,F1,2> Dist_gaussian;
typedef StatFunction<F3,F2,3> Dist_lognormal;
typedef StatFunction<F2,F1,2> Dist_tdistribution;
typedef StatFunction<F2,F1,2> Dist_exponential;
typedef StatFunction<F2,F1,2> Dist_landau;
typedef StatFunction<F3,F2,3> Dist_uniform;
#define CREATE_DIST(name) Dist_ ##name dist( name ## _pdf, name ## _cdf, name ##_quantile );
#define CREATE_DIST_C(name) Dist_ ##name distc( name ## _pdf, name ## _cdf_c, name ##_quantile_c );
// #define CREATE_DIST_C(name) Dist_ ##name distc( name ## _pdf, name ## _cdf_c, 0 );
// #else
// #endif
template<class Distribution>
int TestDist(Distribution & d, double x1, double x2) {
int ir = 0;
ir |= d.Test(x1,x2);
return ir;
}
int testStatFunctions(int nfunc = 100 ) {
// test statistical functions
int iret = 0;
NFuncTest = nfunc;
{
PrintTest("Beta distribution");
CREATE_DIST(beta);
dist.SetParameters( 2, 2);
iret |= dist.Test(0.01,0.99,0.,1.);
CREATE_DIST_C(beta);
distc.SetParameters( 2, 2);
iret |= distc.Test(0.01,0.99,0.,1.,true);
}
{
PrintTest("Gamma distribution");
#ifdef USE_MATHMORE // gamma_quantile is in mathmore
CREATE_DIST(gamma);
dist.SetParameters( 2, 1);
iret |= dist.Test(0.05,5, 0.,TMath::Infinity());
#endif
CREATE_DIST_C(gamma);
distc.SetParameters( 2, 1);
iret |= distc.Test(0.05,5, 0.,TMath::Infinity(),true);
}
{
PrintTest("Chisquare distribution");
#ifdef USE_MATHMORE
CREATE_DIST(chisquared);
dist.SetParameters( 10, 0);
dist.ScaleTol2(10);
iret |= dist.Test(0.05,30, 0.,1.);
#endif
CREATE_DIST_C(chisquared);
distc.SetParameters( 10, 0);
distc.ScaleTol2(10000000); // t.b.c.
iret |= distc.Test(0.05,30, 0.,TMath::Infinity(),true);
}
{
PrintTest("Normal distribution ");
CREATE_DIST(gaussian);
dist.SetParameters( 2, 1);
dist.ScaleTol2(100);
iret |= dist.Test(-3,5);
CREATE_DIST_C(gaussian);
distc.SetParameters( 1, 0);
distc.ScaleTol2(100);
iret |= distc.Test(-3,5,1,0,true);
}
{
PrintTest("BreitWigner distribution ");
CREATE_DIST(breitwigner);
dist.SetParameters( 1);
dist.ScaleTol1(1E8);
dist.ScaleTol2(10);
iret |= dist.Test(-5,5);
CREATE_DIST_C(breitwigner);
distc.SetParameters( 1);
distc.ScaleTol1(1E8);
distc.ScaleTol2(10);
iret |= distc.Test(-5,5,1,0,true);
}
{
PrintTest("F distribution ");
CREATE_DIST(fdistribution);
dist.SetParameters( 5, 4);
dist.ScaleTol1(1000000);
dist.ScaleTol2(10);
// if enlarge scale test fails
iret |= dist.Test(0.05,5,0,1);
CREATE_DIST_C(fdistribution);
distc.SetParameters( 5, 4);
distc.ScaleTol1(100000000);
distc.ScaleTol2(10);
// if enlarge scale test fails
iret |= distc.Test(0.05,5,0,TMath::Infinity(),true);
}
#ifdef USE_MATHMORE // wait t quantile is in mathcore
{
PrintTest("t distribution ");
CREATE_DIST(tdistribution);
dist.SetParameters( 10 );
// dist.ScaleTol1(1000);
dist.ScaleTol2(5000);
iret |= dist.Test(-10,10);
CREATE_DIST_C(tdistribution);
distc.SetParameters( 10 );
distc.ScaleTol2(10000); // t.b.c.
iret |= distc.Test(-10,10,1,0,true);
}
#endif
{
PrintTest("lognormal distribution");
CREATE_DIST(lognormal);
dist.SetParameters(1,1 );
dist.ScaleTol1(1000);
iret |= dist.Test(0.01,5,0,1);
CREATE_DIST_C(lognormal);
distc.SetParameters(1,1 );
distc.ScaleTol1(1000);
distc.ScaleTol2(1000000); // t.b.c.
iret |= distc.Test(0.01,5,0,TMath::Infinity(),true);
}
{
PrintTest("Exponential distribution");
CREATE_DIST(exponential);
dist.SetParameters( 2);
dist.ScaleTol2(100);
iret |= dist.Test(0.,5.,0.,1.);
CREATE_DIST_C(exponential);
distc.SetParameters( 2);
distc.ScaleTol2(100);
iret |= distc.Test(0.,5.,0.,1.,true);
}
{
PrintTest("Landau distribution");
CREATE_DIST(landau);
dist.SetParameters( 2);
// Landau is not very precise (put prec at 10-6)
// as indicated in Landau paper (
dist.ScaleTol1(10000);
dist.ScaleTol2(1.E10);
iret |= dist.Test(-1,10,-TMath::Infinity(),TMath::Infinity());
CREATE_DIST_C(landau);
distc.SetParameters( 2);
distc.ScaleTol1(1000);
distc.ScaleTol2(1E10);
iret |= distc.Test(-1,10,-TMath::Infinity(), TMath::Infinity(),true);
}
{
PrintTest("Uniform distribution");
CREATE_DIST(uniform);
dist.SetParameters( 1, 2);
iret |= dist.Test(1.,2.,1.,2.);
CREATE_DIST_C(uniform);
distc.SetParameters( 1, 2);
iret |= distc.Test(1.,2.,1.,2.,true);
}
return iret;
}
//*******************************************************************************************************************
// GenVector tests
//*******************************************************************************************************************
// trait for getting vector name
template<class V>
struct VecType {
static std::string name() { return "MathCoreVector";}
};
template<>
struct VecType<XYVector> {
static std::string name() { return "XYVector";}
static std::string name32() { return "ROOT::Math::DisplacementVector2D<ROOT::Math::Cartesian2D<Double32_t> >";}
};
template<>
struct VecType<Polar2DVector> {
static std::string name() { return "Polar2DVector";}
};
template<>
struct VecType<XYZVector> {
static std::string name() { return "XYZVector";}
static std::string name32() { return "ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<Double32_t> >";}
};
template<>
struct VecType<Polar3DVector> {
static std::string name() { return "Polar3DVector";}
};
template<>
struct VecType<RhoEtaPhiVector> {
static std::string name() { return "RhoEtaPhiVector";}
};
template<>
struct VecType<RhoZPhiVector> {
static std::string name() { return "RhoZPhiVector";}
};
template<>
struct VecType<PxPyPzEVector> {
static std::string name() { return "PxPyPzEVector";}
static std::string name32() { return "ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<Double32_t> >";}
};
template<>
struct VecType<PtEtaPhiEVector> {
static std::string name() { return "PtEtaPhiEVector";}
};
template<>
struct VecType<PtEtaPhiMVector> {
static std::string name() { return "PtEtaPhiMVector";}
};
template<>
struct VecType<PxPyPzMVector> {
static std::string name() { return "PxPyPzMVector";}
};
template<>
struct VecType<SVector<double,3> > {
static std::string name() { return "SVector3";}
static std::string name32() { return "ROOT::Math::SVector<Double32_t,3>";}
};
template<>
struct VecType<SVector<double,4> > {
static std::string name() { return "SVector4";}
static std::string name32() { return "ROOT::Math::SVector<Double32_t,4>";}
};
template<>
struct VecType<TrackD> {
static std::string name() { return "TrackD";}
};
template<>
struct VecType<TrackD32> {
static std::string name() { return "TrackD32";}
};
template<>
struct VecType<TrackErrD> {
static std::string name() { return "TrackErrD";}
};
template<>
struct VecType<TrackErrD32> {
static std::string name() { return "TrackErrD32";}
};
template<>
struct VecType<VecTrack<TrackD> > {
static std::string name() { return "VecTrackD";}
};
template<>
struct VecType<VecTrack<TrackErrD> > {
static std::string name() { return "VecTrackErrD";}
};
// generic (2 dim)
template<class V, int Dim>
struct VecOp {
template<class It>
static V Create(It &x, It &y, It & , It& ) { return V(*x++,*y++); }
template<class It>
static void Set(V & v, It &x, It &y, It &, It&) { v.SetXY(*x++,*y++); }
static double Add(const V & v) { return v.X() + v.Y(); }
static double Delta(const V & v1, const V & v2) { double d = ROOT::Math::VectorUtil::DeltaPhi(v1,v2); return d*d; } // is v2-v1
};
// specialized for 3D
template<class V>
struct VecOp<V,3> {
template<class It>
static V Create(It &x, It& y, It& z , It& ) { return V(*x++,*y++,*z++); }
template<class It>
static void Set(V & v, It & x, It &y, It &z, It&) { v.SetXYZ(*x++,*y++,*z++); }
static V Create(double x, double y, double z , double ) { return V(x,y,z); }
static void Set(V & v, double x, double y, double z, double) { v.SetXYZ(x,y,z); }
static double Add(const V & v) { return v.X() + v.Y() + v.Z(); }
static double Delta(const V & v1, const V & v2) { return ROOT::Math::VectorUtil::DeltaR2(v1,v2); }
};
// specialized for 4D
template<class V>
struct VecOp<V,4> {
template<class It>
static V Create(It &x, It &y, It &z , It &t ) { return V(*x++,*y++,*z++,*t++);}
template<class It>
static void Set(V & v, It & x, It &y, It &z, It &t) { v.SetXYZT(*x++,*y++,*z++,*t++); }
static double Add(const V & v) { return v.X() + v.Y() + v.Z() + v.E(); }
static double Delta(const V & v1, const V & v2) {
return ROOT::Math::VectorUtil::DeltaR2(v1,v2) + ROOT::Math::VectorUtil::InvariantMass(v1,v2); }
};
// specialized for SVector<3>
template<>
struct VecOp<SVector<double,3>,3> {
typedef SVector<double,3> V;
template<class It>
static V Create(It &x, It &y, It &z , It & ) { return V(*x++,*y++,*z++);}
static double Add(const V & v) { return v(0) + v(1) + v(2); }
};
// specialized for SVector<4>
template<>
struct VecOp<SVector<double,4>,4> {
typedef SVector<double,4> V;
template<class It>
static V Create(It &x, It &y, It &z , It &t ) { return V(*x++,*y++,*z++,*t++);}
static double Add(const V & v) { return v(0) + v(1) + v(2) + v(3); }
};
// internal structure to measure the time
TStopwatch gTimer;
double gTotTime;
struct Timer {
Timer() {
gTimer.Start();
}
~Timer() {
gTimer.Stop();
gTotTime += Time();
if (debugTime) printTime();
}
void printTime( std::string s = "") {
int pr = std::cout.precision(8);
std::cout << s << "\t" << " time = " << Time() << "\t(sec)\t"
// << time.CpuTime()
<< std::endl;
std::cout.precision(pr);
}
double Time() { return gTimer.RealTime(); } // use real time
TStopwatch gTimer;
double gTotTime;
};
template<int Dim>
class VectorTest {
private:
// global data variables
std::vector<double> dataX;
std::vector<double> dataY;
std::vector<double> dataZ;
std::vector<double> dataE;
int nGen;
int n2Loop ;
double fSum; // total sum of x,y,z,t (for testing first addition)
public:
VectorTest(int n1, int n2=0) :
nGen(n1),
n2Loop(n2)
{
gTotTime = 0;
}
double TotalTime() const { return gTotTime; } // use real time
double Sum() const { return fSum; }
int check(std::string name, double s1, double s2, double scale=1) {
int iret = 0;
PrintTest(name);
iret |= compare(name,s1,s2,scale);
PrintStatus(iret);
return iret;
}
void print(std::string name) {
PrintTest(name);
std::cout <<"\t\t..............\n";
}
void genData() {
// generate for all 4 d data
TRandom3 r(111); // use a fixed seed to be able to reproduce tests
fSum = 0;
for (int i = 0; i < nGen ; ++ i) {
// generate a 4D vector and stores only the interested dimensions
double phi = r.Rndm()*3.1415926535897931;
double eta = r.Uniform(-5.,5.);
double pt = r.Exp(10.);
double m = r.Uniform(0,10.);
if ( i%50 == 0 )
m = r.BreitWigner(1.,0.01);
double E = sqrt( m*m + pt*pt*std::cosh(eta)*std::cosh(eta) );
// fill vectors
PtEtaPhiEVector q( pt, eta, phi, E);
dataX.push_back( q.x() );
dataY.push_back( q.y() );
fSum += q.x() + q.y();
if (Dim >= 3) {
dataZ.push_back( q.z() );
fSum += q.z();
}
if (Dim >=4 ) {
dataE.push_back( q.t() );
fSum += q.t();
}
}
assert( int(dataX.size()) == nGen);
assert( int(dataY.size()) == nGen);
if (Dim >= 3) assert( int(dataZ.size()) == nGen);
if (Dim >=4 ) assert( int(dataE.size()) == nGen);
// // // dataZ.resize(nGen);
// // // dataE.resize(nGen);
}
// gen data for a Ndim matrix or vector
void genDataN() {
// generate for all 4 d data
TRandom3 r(111); // use a fixed seed to be able to reproduce tests
fSum = 0;
dataX.reserve(nGen*Dim);
for (int i = 0; i < nGen*Dim ; ++ i) {
// generate random data between [0,1]
double x = r.Rndm();
fSum += x;
dataX.push_back( x );
}
}
typedef std::vector<double>::const_iterator DataIt;
// test methods
template <class V>
void testCreate( std::vector<V > & dataV) {
Timer tim;
DataIt x = dataX.begin();
DataIt y = dataY.begin();
DataIt z = dataZ.begin();
DataIt t = dataE.begin();
while (x != dataX.end() ) {
dataV.push_back(VecOp<V,Dim>::Create(x,y,z,t) );
assert(int(dataV.size()) <= nGen);
}
}
template <class V>
void testCreateAndSet( std::vector<V > & dataV) {
Timer tim;
DataIt x = dataX.begin();
DataIt y = dataY.begin();
DataIt z = dataZ.begin();
DataIt t = dataE.begin();
while (x != dataX.end() ) {
V v;
VecOp<V,Dim>::Set( v, x,y,z,t);
dataV.push_back(v);
assert(int(dataV.size()) <= nGen);
}
}
template <class V>
double testAddition( const std::vector<V > & dataV) {
V v0;
Timer t;
for (int i = 0; i < nGen; ++i) {
v0 += dataV[i];
}
return VecOp<V,Dim>::Add(v0);
}
template <class V>
double testOperations( const std::vector<V > & dataV) {
double tot = 0;
Timer t;
for (int i = 0; i < nGen-1; ++i) {
const V & v1 = dataV[i];
const V & v2 = dataV[i+1];
double a = v1.R();
double b = v2.mag2(); // mag2 is defined for all dimensions;
double c = 1./v1.Dot(v2);
V v3 = c * ( v1/a + v2/b );
tot += VecOp<V,Dim>::Add(v3);
}
return tot;
}
// mantain loop in gen otherwise is proportional to N**@
template <class V>
double testDelta( const std::vector<V > & dataV) {
double tot = 0;
Timer t;
for (int i = 0; i < nGen-1; ++i) {
const V & v1 = dataV[i];
const V & v2 = dataV[i+1];
tot += VecOp<V,Dim>::Delta(v1,v2);
}
return tot;
}
// template <class V>
// double testDotProduct( const std::vector<V *> & dataV) {
// //unsigned int n = std::min(n2Loop, dataV.size() );
// double tot = 0;
// V v0 = *(dataV[0]);
// Timer t;
// for (unsigned int i = 0; i < nGen-1; ++i) {
// V & v1 = *(dataV[i]);
// tot += v0.Dot(v1);
// }
// return tot;
// }
template <class V1, class V2>
void testConversion( std::vector<V1 > & dataV1, std::vector<V2 > & dataV2) {
Timer t;
for (int i = 0; i < nGen; ++i) {
dataV2.push_back( V2( dataV1[i] ) );
}
}
// rotation
template <class V, class R>
double testRotation( std::vector<V > & dataV ) {
double sum = 0;
double rotAngle = 1;
Timer t;
for (unsigned int i = 0; i < nGen; ++i) {
V & v1 = dataV[i];
V v2 = v1;
v2.Rotate(rotAngle);
sum += VecOp<V,Dim>::Add(v2);
}
return sum;
}
template<class V>
double testWrite(const std::vector<V> & dataV, std::string typeName="", bool compress = false) {
std::string fname = VecType<V>::name() + ".root";
// replace < character with _
TFile file(fname.c_str(),"RECREATE","",compress);
// create tree
std::string tree_name="Tree with" + VecType<V>::name();
TTree tree("VectorTree",tree_name.c_str());
V *v1 = new V();
//std::cout << "typeID written : " << typeid(*v1).name() << std::endl;
// need to add namespace to full type name
if (typeName == "") {
typeName = "ROOT::Math::" + VecType<V>::name();
}
//std::cout << typeName << std::endl;
TBranch * br = tree.Branch("Vector_branch",typeName.c_str(),&v1);
if (br == 0) {
std::cout << "Error creating branch for" << typeName << "\n\t typeid is "
<< typeid(*v1).name() << std::endl;
return -1;
}
Timer timer;
for (int i = 0; i < nGen; ++i) {
*v1 = dataV[i];
tree.Fill();
}
#ifdef DEBUG
tree.Print(); // debug
#endif
file.Write();
file.Close();
return file.GetSize();
}
template<class V>
int testRead(std::vector<V> & dataV) {
dataV.clear();
dataV.reserve(nGen);
std::string fname = VecType<V>::name() + ".root";
TFile f1(fname.c_str());
if (f1.IsZombie() ) {
std::cout << " Error opening file " << fname << std::endl;
return -1;
}
// create tree
TTree *tree = dynamic_cast<TTree*>(f1.Get("VectorTree"));