From d75030a1e6ba27fd55aa8c9e959213faa1cbbf59 Mon Sep 17 00:00:00 2001
From: Lorenzo Moneta <Lorenzo.Moneta@cern.ch>
Date: Wed, 8 Dec 2010 21:19:45 +0000
Subject: [PATCH] fix problem in setting print level and extra options for
 unuran and foam sampler classes

git-svn-id: http://root.cern.ch/svn/root/trunk@37419 27541ba8-7e3a-0410-8455-c3a389f83636
---
 math/foam/inc/TFoamSampler.h                |  2 +-
 math/foam/src/TFoamSampler.cxx              | 20 +++++++++----
 math/mathcore/inc/Math/DistSamplerOptions.h |  4 +--
 math/mathcore/src/DistSamplerOptions.cxx    |  4 +--
 math/unuran/inc/TUnuranSampler.h            |  3 +-
 math/unuran/src/TUnuran.cxx                 |  8 +++---
 math/unuran/src/TUnuranSampler.cxx          | 31 +++++++++++++++------
 7 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/math/foam/inc/TFoamSampler.h b/math/foam/inc/TFoamSampler.h
index cd15fb2f1e5..409cc29e17a 100644
--- a/math/foam/inc/TFoamSampler.h
+++ b/math/foam/inc/TFoamSampler.h
@@ -74,7 +74,7 @@ public:
    /**
       initialize the generators with the default options
    */
-   bool Init(const char * ); 
+   bool Init(const char * = ""); 
 
    /**
       initialize the generators with the fiven options
diff --git a/math/foam/src/TFoamSampler.cxx b/math/foam/src/TFoamSampler.cxx
index 2b741bc6fec..f7265897fe1 100644
--- a/math/foam/src/TFoamSampler.cxx
+++ b/math/foam/src/TFoamSampler.cxx
@@ -103,7 +103,9 @@ TFoamSampler::~TFoamSampler() {
 bool TFoamSampler::Init(const char *) { 
 
    // initialize using default options
-   ROOT::Math::DistSamplerOptions opt; 
+   ROOT::Math::DistSamplerOptions opt(0);
+   ROOT::Math::IOptions * foamOpt  = ROOT::Math::DistSamplerOptions::FindDefault("Foam"); 
+   if (foamOpt) opt.SetExtraOptions(*foamOpt); 
    return Init(opt);
 }
 
@@ -126,18 +128,26 @@ bool TFoamSampler::Init(const ROOT::Math::DistSamplerOptions & opt) {
    fFoamDist = new FoamDistribution(ParentPdf(),PdfRange());
 
    fFoam->SetRho(fFoamDist);
+   // set print level
+   fFoam->SetChat(opt.PrintLevel());
 
    // get extra options 
    ROOT::Math::IOptions * fopt = opt.ExtraOptions(); 
    if (fopt) { 
       int nval = 0; 
+      double fval = 0;
       if (fopt->GetIntValue("nCells", nval) ) fFoam->SetnCells(nval);
-      if (fopt->GetIntValue("nCell1D", nval) ) fFoam->SetnCells(nval);
-      if (fopt->GetIntValue("nCell2D", nval) ) fFoam->SetnCells(nval);
-      if (fopt->GetIntValue("nCell3D", nval) ) fFoam->SetnCells(nval);
-      if (fopt->GetIntValue("nCellND", nval) ) fFoam->SetnCells(nval);
+      if (fopt->GetIntValue("nCell1D", nval) && NDim() ==1) fFoam->SetnCells(nval);
+      if (fopt->GetIntValue("nCellND", nval) && NDim()  >1) fFoam->SetnCells(nval);
+      if (fopt->GetIntValue("nCell2D", nval) && NDim() ==2) fFoam->SetnCells(nval);
+      if (fopt->GetIntValue("nCell3D", nval) && NDim() ==3) fFoam->SetnCells(nval);
 
       if (fopt->GetIntValue("nSample", nval) ) fFoam->SetnSampl(nval);
+      if (fopt->GetIntValue("nBin", nval) ) fFoam->SetnBin(nval);
+      if (fopt->GetIntValue("OptDrive",nval) ) fFoam->SetOptDrive(nval);
+      if (fopt->GetIntValue("OptRej",nval) ) fFoam->SetOptRej(nval);
+      if (fopt->GetRealValue("MaxWtRej",fval) ) fFoam->SetMaxWtRej(fval);
+
 
       if (fopt->GetIntValue("chatLevel", nval) ) fFoam->SetChat(nval);
    }
diff --git a/math/mathcore/inc/Math/DistSamplerOptions.h b/math/mathcore/inc/Math/DistSamplerOptions.h
index 9e799dff014..e6bc216accb 100644
--- a/math/mathcore/inc/Math/DistSamplerOptions.h
+++ b/math/mathcore/inc/Math/DistSamplerOptions.h
@@ -58,11 +58,11 @@ public:
 
    // constructor using the default options 
    // pass optionally a pointer to the additional options
-   // otehrwise look if they exist for this default minimizer
+   // otherwise look if they exist for this default minimizer
    // and in that case they are copied in the constructed instance
    // constructor takes dimension since a different default algorithm
    // is used if the dimension is 1 or greater than 1 
-   DistSamplerOptions(int dim = 0, IOptions * extraOpts = 0);
+   DistSamplerOptions(int dim = 0);
 
    // destructor  
    ~DistSamplerOptions();
diff --git a/math/mathcore/src/DistSamplerOptions.cxx b/math/mathcore/src/DistSamplerOptions.cxx
index 27071bec9ee..6591bb68f92 100644
--- a/math/mathcore/src/DistSamplerOptions.cxx
+++ b/math/mathcore/src/DistSamplerOptions.cxx
@@ -59,9 +59,9 @@ const std::string & DistSamplerOptions::DefaultSampler()
 }
 
 
-DistSamplerOptions::DistSamplerOptions(int dim, IOptions * extraOpts): 
+DistSamplerOptions::DistSamplerOptions(int dim): 
    fLevel( Sampler::gDefaultPrintLevel),
-   fExtraOptions(extraOpts)
+   fExtraOptions(0)
 {
    // constructor using  the default options
 
diff --git a/math/unuran/inc/TUnuranSampler.h b/math/unuran/inc/TUnuranSampler.h
index f9886aa3c80..a551f02f133 100644
--- a/math/unuran/inc/TUnuranSampler.h
+++ b/math/unuran/inc/TUnuranSampler.h
@@ -100,7 +100,7 @@ public:
       Set the print level 
       (if level=-1 use default)
     */
-   void SetPrintLevel(int level);
+   void SetPrintLevel(int level) {fLevel = level;}
 
    /* 
       set the mode
@@ -169,6 +169,7 @@ private:
    bool                              fDiscrete;    // flag to indicate if the function is discrete
    bool                              fHasMode;     // flag to indicate if a mode is set
    bool                              fHasArea;     // flag to indicate if a area is set
+   int                               fLevel;       // debug level
    double                            fMode;        // mode of dist
    double                            fArea;        // area of dist
    const ROOT::Math::IGenFunction *  fFunc1D;      // 1D function pointer
diff --git a/math/unuran/src/TUnuran.cxx b/math/unuran/src/TUnuran.cxx
index 5bdab253935..bb6b7f71347 100644
--- a/math/unuran/src/TUnuran.cxx
+++ b/math/unuran/src/TUnuran.cxx
@@ -44,9 +44,9 @@ TUnuran::TUnuran(TRandom * r, unsigned int debugLevel) :
    if (fRng == 0) fRng = gRandom; 
    // set debug level at global level 
    // (should be in a static  initialization function of the library ? )
-   if ( debugLevel > 2) 
+   if ( debugLevel > 1) 
       unur_set_default_debug(UNUR_DEBUG_ALL);
-   else if (debugLevel > 1) 
+   else if (debugLevel == 1) 
       unur_set_default_debug(UNUR_DEBUG_INIT);
    else
       unur_set_default_debug(UNUR_DEBUG_OFF);
@@ -409,9 +409,9 @@ bool  TUnuran::SetLogLevel(unsigned int debugLevel)
 {
    if (fGen == 0) return false; 
    int ret = 0; 
-   if ( debugLevel > 2) 
+   if ( debugLevel > 1) 
       ret |= unur_chg_debug(fGen, UNUR_DEBUG_ALL);
-   else if (debugLevel > 1) 
+   else if (debugLevel == 1) 
       ret |= unur_chg_debug(fGen, UNUR_DEBUG_ALL);
    else
       ret |= unur_chg_debug(fGen, UNUR_DEBUG_OFF);
diff --git a/math/unuran/src/TUnuranSampler.cxx b/math/unuran/src/TUnuranSampler.cxx
index d910e329839..82a8404743d 100644
--- a/math/unuran/src/TUnuranSampler.cxx
+++ b/math/unuran/src/TUnuranSampler.cxx
@@ -37,7 +37,7 @@ TUnuranSampler::TUnuranSampler() : ROOT::Math::DistSampler(),
    fFunc1D(0),
    fUnuran(new TUnuran()  )
 {
-   fUnuran->SetLogLevel(ROOT::Math::DistSamplerOptions::DefaultPrintLevel());
+   fLevel = ROOT::Math::DistSamplerOptions::DefaultPrintLevel();
 }
 
 TUnuranSampler::~TUnuranSampler() {
@@ -53,6 +53,8 @@ bool TUnuranSampler::Init(const char * algo) {
       return false;
    }
 
+   if (fLevel < 0) fLevel =  ROOT::Math::DistSamplerOptions::DefaultPrintLevel();
+
    TString method(algo); 
    if (method.IsNull() ) { 
       if (NDim() == 1) method = ROOT::Math::DistSamplerOptions::DefaultAlgorithm1D();
@@ -60,22 +62,33 @@ bool TUnuranSampler::Init(const char * algo) {
    }
    method.ToUpper();
 
+   bool ret = false; 
    if (NDim() == 1) { 
        // check if distribution is discrete by 
       // using first string in the method name is "D"
-      if (method.First("D") == 0) return DoInitDiscrete1D(method);
-      return DoInit1D(method); 
+      if (method.First("D") == 0) { 
+         if (fLevel>1) Info("TUnuranSampler::Init","Initialize one-dim discrete distribution with method %s",method.Data());
+         ret =  DoInitDiscrete1D(method);
+      }
+      else {
+         if (fLevel>1) Info("TUnuranSampler::Init","Initialize one-dim continous distribution with method %s",method.Data());
+         ret =  DoInit1D(method); 
+      }
    }
    else { 
-      return DoInitND(method); 
+      if (fLevel>1) Info("TUnuranSampler::Init","Initialize multi-dim continous distribution with method %s",method.Data());
+      ret = DoInitND(method); 
    }
+   // set print level in UNURAN (must be done after having initialized) -
+   if (fLevel>0) { 
+      //fUnuran->SetLogLevel(fLevel); ( seems not to work  disable for the time being) 
+      if (ret) Info("TUnuranSampler::Init","Successfully initailized Unuran with method %s",method.Data() );
+      else Error("TUnuranSampler::Init","Failed to  initailize Unuran with method %s",method.Data() );
+      // seems not to work in UNURAN (cll only when level > 0 )
+   }
+   return ret; 
 }
 
-void TUnuranSampler::SetPrintLevel(int level ) {
-   // set print level 
-   if (level < 0) level =  ROOT::Math::DistSamplerOptions::DefaultPrintLevel();
-   fUnuran->SetLogLevel(level);
-} 
 
 bool TUnuranSampler::Init(const ROOT::Math::DistSamplerOptions & opt ) { 
    // default initialization with algorithm name
-- 
GitLab