ROOT Mathematical Libraries

  • Upload
    lauren

  • View
    32

  • Download
    4

Embed Size (px)

DESCRIPTION

ROOT Mathematical Libraries. Lorenzo Moneta CERN/PH-SFT. Root Math Work Package. Main responsibilities for this work package: Basic mathematical functions Numerical algorithms Random numbers Linear algebra Physics and geometry vectors (3D and 4D) Fitting and minimization - PowerPoint PPT Presentation

Citation preview

  • ROOT Mathematical Libraries Lorenzo Moneta

    CERN/PH-SFT

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Root Math Work PackageMain responsibilities for this work package: Basic mathematical functions Numerical algorithmsRandom numbersLinear algebra Physics and geometry vectors (3D and 4D)Fitting and minimizationHistograms (math and statistical part)Statistics (confidence levels, multivariate analysis)People contributing: A. Kreshuk, L. Moneta (CERN)E. Offermann J. Leydold, Vienna (Unuran)M. Fischler, J. Marraffino, W. Brown, FNALothers: W. Verkerke (RooFit), TMVA team, etc..

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    OutlineNew core math library merge Math in libCore with current libMathCore numerical algorithmsfunctor classes (changes in TF1)Recent developments of ROOT Math Libraries : Random numbers improvementsPhysics and vector package (GenVector)SMatrix package MathMore Fitting and Minimizationplans for new fitting classesfitting GUI (new fit panel)Other recent developments: Histogram comparison (Chi2 test)FFT , Unuran, TMVAFuture plans

    LCG AA Internal Review, CERN, September 18, 2006 Lorenzo Moneta, CERN/PH-SFT

    Current Status of ROOT Math Libsno dependencywith dependency not yet released

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    New Structure of ROOT Math Libsno dependencywith dependency not yet released

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    New Core Math LibraryRestructuring of ROOT libraries: new core Math lib with Math classes from libCore:TRandom classes, TComplex , TMath (excluding TMathBase) Classes from current version of MathCore basic mathematical and statistical functions remove duplications with TMathphysics vector ( 3D and LorentzVector + Rotation and Boost classes )Implementation of basic numerical algorithms from TF1 : derivation, numerical integration, Brent minimization Interfaces for functions and algorithms needed for using different implementations (with plug-in manager)Functor classes use of C++ callable objects in TF1 and in the algorithms

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Library sizeEstimation for the size of the library (on Linux)Dictionary for physics vectors will be in a separate library ?move them in libPhysics with TLorentzVector ?

    ~ 1Mb

    Classes/Functionssize of Library (KB)size of Library and Dictionary (KB)TMath109240TRandom, 1,2,355150TComplex470ROOT::Math functions16150Physics Vector116(~2000)Numerical algorithms100300Total for libMath400900 (~3MB)

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Math DictionariesTemplate classes (like GenVector and SMatrix) the dictionary is provided for the most used types double, float and Double32_tall types of 3D and Lorentz Vectors (12 in total)for all the main vector operationsfor SVector and SMatrix classes up to dimension 7dictionary is the dominant part of the library : 2 Mb on Linux of a 2.3 Mb library for current MathCore libSMatrix ( ~ 3 Mb) contains only the dictionary. do not use if you dont need I/O or to run interactively

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    MathMore LibraryC++ interface to GSL algorithms and functionsrequires a GSL (version >= 1.8) already installedNumerical algorithms for 1D functions: Numerical Derivation central evaluation (5 points rule) and forward/backward Numerical Integration adaptive integration for finite and infinite intervalsRoot Finders bracketing and polishing algorithms using derivativesMinimization Golden section and Brent algorithmInterpolation linear, polynomial, cubic and Akima splineChebyshev polynomials (for function approximation)Random Numbers generation and distributions

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Numerical AlgorithmsCollect in the new MathCore basic numerical algorithms present in various places in ROOT start with algorithms from TF1 (derivation, integration, etc..) provide a coherent interface for these algorithmsmaintain the current methods in TF1 for user convenience and backward compatibilitydesign based on classes already developed in MathMore Derivator, Integrator, RootFinder, etc..provides a basic implementation using code from TF1use plug-in manager for alternative implementation (from GSL)Algorithms could be used directly (with-out the need of creating a TF1 objects) user would need to provide a C++ callable object (functor)

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Example: IntegrationEntry point is a Integrator class which will be in MathCorecan be used directly of via TF1Integrator class internally uses the chosen implementation: Gauss Integrator : simple algorithm from MathCore (used now in TF1::Integral)GSL Integrator based on QAGS (from MathMore)

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Functor classUse a Functor class to wrap any C++ callable object which has a right signatureCan work with: free C functionsC++ classes (or structs) implementing operator() member function of a classThe requirement is that they must have the right signature Example: a function of type : double f ( double )

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Examples of using FunctorsFree Functions Classes implementing operator() Member functionsdouble func(double x) { ............}

    Functor f1(func);f1(x); // evaluate functor at value xstruct MyFunction { double operator()(double x) {.....}};

    Functor f2( MyFunction() );f2(x); // evaluate functor at value xclass MyClass { ...... double Eval(double x) {.....}};

    MyClass * p = new MyClass(); Functor f3( p, &MyClass::Eval );f3(x); // evaluate functor at value x

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Advantages of Functor classesThey are very convenient for users easier to defining a functor than implementing a class following an abstract interfaceHave value semantics (like shared pointers)Very flexibleusers can customize the callable objects using its statethis is not possible when using a free functionExample of usage in C++: STL algorithm are based on similar conceptvery powerful and flexibleboost::function (will be in next C++ standard)

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Functor Usage in TF1Use a Functor class in TF1 instead of using a pointer to a free function (fFunction) Functor class with needed signature(ROOT::Math::ParamFunctor)Use this class as a data member of TF1 Have template constructors for callable objects and for member functions// template constructors from general callable objecttemplate TF1(const char * name, Func f, Double_t xmin, Double_t xmax, Int_t npar){.....fFunction = ROOT::Math::ParamFunctor(f);}// template constructors from a member functiontemplate TF1(const char * name, const PtrObj & p, MemFn fn, Double_t xmin,Double_t xmax..{.....fFunction = ROOT::Math::ParamFunctor(p,memFn);}

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Example of Functor Usage working example: possible now with the current TF1 only by using globals objects

    double MyFunc (double *x, double *p ) { return TMath::Gaus(x[0],p[0],p[1] );}

    struct MyDerivFunc { MyDerivFunc(TF1 * f): fFunc(f) {} double operator()(double *x,double *) const { return fFunc->Derivative(*x); } TF1 * fFunc; };struct MyIntegFunc { MyIntegFunc(TF1 * f): fFunc(f) {} double operator()(double *x,double *) const { double a = fFunc->GetXmin(); return fFunc->Integral(a, *x); } TF1 * fFunc; };

    void testTF1Functor() {

    double xmin = -10; double xmax = 10; TF1 * f1 = new TF1("f1",MyFunc,xmin,xmax,2); f1->SetParameters(0.,1.); f1->SetMaximum(3); f1->SetMinimum(-1); f1->Draw();

    // create derivatives function using // the parameter of the parent function TF1 * f2 = new TF1("f2",MyDerivFunc(f1), xmin, xmax, 0);

    f2->SetLineColor(kBlue); f2->Draw("same");

    // create integral function TF1 * f3 = new TF1("f3",MyIntegFunc(f1), xmin, xmax, 0);

    f3->SetLineColor(kRed); f3->Draw("same");}

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Functors in ROOTDifferent signatures can be easily matched using some Functor adaptersusing something similar to std::bind2nd functions taking two parameters can be adapted to function with one parameterone could project a multidimensional function in a 1D function Numerical Algorithm (Integration, Root Finder classes, etc..) will work also accepting a Functor use a template method relying on the defined functor signatureBetter to define also in TF1 an operator() for TF1::Eval it would avoid need of an additional adapterExpect to make the TF1 changes for the June release

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Evaluation of Mathematical FunctionsSpecial Functions: use interface proposed to the C++ standard: double cyl_bessel_i (double nu, double x); Statistical Functions: Probability density functions (pdf)Cumulative distributions (lower tail and upper tail)Inverse of cumulative distributions (quantiles)Coherent naming scheme. Example for chi2 :chisquared_pdfchisquared_cdf, chisquared_cdf_c,chisquared_quantile, chisquare_quantile_c

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Mathematical FunctionsLarge variety of new functions complementing what is in TMathmajority are currently in MathMore (implemented using GSL)of general good quality ( tested with Mathematica and Nag)Implement directly in MathCore some of the most used functionsincomplete beta and gamma functions remove duplication with TMath

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Improvements in Pseudo-Random Number GeneratorsMersenne-Twister generator (TRandom3) is now used for gRandomfast and excellent pseudo-random quality very long period, ~106000replace obsolete TRandom2 with TausWorthe generator from LEcuyer (fast and good quality, but not so long period: 1026)add RanLux generator (TRandom1) use a better linear congruential for TRandomold one had seeding problems and a not uniform coverageneed to maintain for backward compatibility a generator based on a state of only 32 bits (very short period : 231 ~ 109) must NOT be used in any statistics application we should rename classes: more meaningful names and use M-T for the base class

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Further TRandom ImprovementsNew faster algorithm for generating Gaussian numberacceptance-complement ratio method (W.Hrmann,G.Derflinger) from UNU.RANfaster than polar method (Box-Muller) which requires 2 numbers + sin, sqrt and log calculationsone of the fastest existing methodsNew Poisson algorithm previous one was buggy for large mu valuesexact algorithm (rejection from a Lorentzian distribution)if constant not very efficient better using algorithm from UNU.RANImprove performances of TRandom::Landau

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Random Number Performancesgenerators:

    Gauss random numbersns/callTests run on lxplus slc4 dual-core Intel 64

  • ns/call

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Random Generator PerformancesPerformances on lxplus (slc4_i64_gcc34) running testrandom.C Distribution nanoseconds/call TRandom TRandom1 TRandom2 TRandom3Rndm.............. 5.500 98.100 6.200 9.200Gaus(old algo.)... 172.700 421.700 195.400 182.700Gaus(new algo.)... 30.700 160.900 35.400 41.900Rannor............ 116.400 215.800 125.800 130.000Landau............ 22.000 108.100 23.100 27.000Poisson(10)....... 146.700 1161.300 162.400 238.500GausTF1........... 109.700 191.100 96.600 102.100LandauTF1......... 93.400 199.800 110.100 97.500GausUNURAN........ 39.900 129.400 46.400 42.600PoissonUNURAN(10). 79.800 293.600 88.700 99.400

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    Physics and Geometry VectorsClasses for 3D and 4D vectors with their operations and transformations (rotations)functionality as in CLHEP Vector and Geometry packages Work done in collaboration with Fermilab computing group (M. Fischler, W. Brown and J. Marraffino)Main features of the new classes:generic scalar contained type i.e. single or double precisiongeneric coordinate system concepti.e. cartesian, polar and cylindricalUsed now by CMS and LHCb Classes do not inherit from TObject and cannot be used in ROOT collections Plan in the future to re-implement TLorentzVector using new classes

    ROOT 2007 Users Workhop, CERN, March 26-28, 2007 Lorenzo Moneta, CERN/PH-SFT

    SMatrix PackagePackage initially developed by T. Glebe for HeraB Matrix and vector classes of arbitrary type For fixed (not dynamic) matrix and vector sizes :size must be knows at compile timeSMatrix< double, 2 , 5>SVector< double, 5 >Complementary and NOT a replacement of TMatrixOptimized for small matrix sizes (dim