# Minuit2 gives -nan results

**URL:** <https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367>\
**Category:** ROOT\
**Created:** [March 20, 2018, 7:28am UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367 "2018-03-20T07:28:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhiyhep](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhiyhep/32/16095_2.png) [@Zhiyhep](https://root-forum.cern.ch/u/Zhiyhep)\
**Post date:** [March 20, 2018, 7:28am UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367/1 "2018-03-20T07:28:49Z")

</div>

Dear ROOT experts:

I am trying to minimize an object function like this:

f(lambda) = Σi=1~n(2ln(Σj=1~mLij\*lambda[j])+(fX2[i]+fY2[i])/(Σj=1~mLij\*lambda[j])).

Where lambda is a 1000 dimension array and L, fX2, fY2 are parameters stored in TTree.

I defined a class named Likelihood\_FCN with reloaded operator() as this:

```auto
double Likelihood_FCN::operator()(const double* lambda)
{
double result = 0;
int nentry = t_ml->GetEntries();
for(int i =0; i < nentry; i++)
{
 t_ml->GetEntry(i);
 double temp = 0;
 for(int j=0; j < fNVoxel; j++)
 {
   int index = fIndex[j];
   temp+=fLength[j]*lambda[index];
   std::cout << i<<": Length, lambda: " << fLength[j] << "," << lambda[index] << std::endl;
 }
 //std::cout<<"temp: "<<temp <<std::endl;
 result+=2*TMath::Log(temp)+(fScatt_Ang_X2+fScatt_Ang_Y2)/temp;
}
std::cout << result << std::endl;
return result;
}

```

And the minimizer is defined as below:

```auto
 ROOT::Math::Minimizer* minim = ROOT::Math::Factory::CreateMinimizer("Minuit2","Migrad");
 minim->SetMaxFunctionCalls(10000);
 minim->SetMaxIterations(10000);
 minim->SetTolerance(1);
 minim->SetPrintLevel(3);
 Likelihood_FCN lf = Likelihood_FCN("PoCA_Result.root","ML_Info","PoCA");
 int nvariable = lf.GetNVariable();
 ROOT::Math::Functor f(lf, nvariable);
 double *init_value = lf.Init();
 lf(init_value);
 minim->SetFunction(f);
 for(int i=0;i<nvariable;i++)
 {
   const string name = "x" + std::to_string(i);
   minim->SetLowerLimitedVariable(i,name,init_value[i],0.1,2.3);
 }
 minim->Minimize();

```

Segmentation fault occurs when I set the minimization strategy other than “Combined”.  
The minimization result is an array of -nan value.

Besides, the evaluations of the FCN during minimization are also -nan, which should not  
occur unless all members of lambda is 0.

Could you give some advice on the -nan result ?

The related files are here:  
[MLEM.tar.gz](https://root-forum.cern.ch/uploads/default/original/2X/5/5865005b5c218fb40bad944da506b9735a40ad90.gz) (123.4 KB)

Best,  
Zhi Yu

* * *

ROOT Version (6.10/02):  
Platform, compiler (e.g. Ubuntu 14.04, gcc4.8):

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 20, 2018, 7:46am UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367/2 "2018-03-20T07:46:02Z")

</div>

Try something like (note: some [Valgrind](http://valgrind.org/) functionality requires that the source code, that you want to analyse, is compiled with debug symbols):

```auto
valgrind --tool=memcheck --leak-check=full [--show-reachable=yes] [--track-origins=yes] [--num-callers=50] [--vgdb=full] --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -q 'MLEM.cc++g'
valgrind --tool=exp-sgcheck [--num-callers=50] [--vgdb=full] --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -q 'MLEM.cc++g'

```

and especially carefully study messages that appear in the beginning of the output.  
(Note: the `--show-reachable=yes` option will give you too many warnings, I believe.)

BTW. The above [Valgrind](http://valgrind.org) commands include some `[something_optional]` options (if you want to use any of these options, REMOVE the “square brackets”).

See also:

> [@AddressSanitizer memory error detector](https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046):
>
> The default [valgrind’s “memcheck” memory error detector](http://valgrind.org/docs/manual/mc-manual.html) will not find various {stack,global}-buffer overflow bugs. [Valgrind](http://valgrind.org/) does provide a dedicated complementary [“exp-sgcheck” experimental stack and global array overrun detector](http://valgrind.org/docs/manual/sg-manual.html) but, for the time being, [it does not work, as soon as ROOT related libraries are used](https://root-forum.cern.ch/t/valgrind-tool-exp-sgcheck/21044) [and it seems that it is possible to use it](https://root-forum.cern.ch/t/valgrind-tool-exp-sgcheck/21044/2). On the other hand, gcc 4.8 and newer compilers provide their own [AddressSanitizer (a.k.a. ASan) fast memory error detector](https://github.com/google/sanitizers/wiki/AddressSanitizer) (note: you may n…

---

<div class="post-metadata">

**Author:** ![Zhiyhep](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhiyhep/32/16095_2.png) [@Zhiyhep](https://root-forum.cern.ch/u/Zhiyhep)\
**Post date:** [March 20, 2018, 2:17pm UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367/3 "2018-03-20T14:17:10Z")

</div>

Hi,

Thanks for your suggestion. I have no experience on valgrind, so i spent some time familiarizing the tool.

When I use the memcheck tool (after include some header files), I got an error message saying that : ‘to\_string’ is not a member of ‘std’.  
The full output message is as below

```auto
$: sudo valgrind --tool=memcheck --leak-check=full --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -a 'MLEM.cc++g'
==9462== Memcheck, a memory error detector
==9462== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9462== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==9462== Command: /my_cache/ROOT6.10.02/bin/root.exe -l -a MLEM.cc++g
==9462== 
root [0] 
Processing MLEM.cc++g...
Info in <TUnixSystem::ACLiC>: creating shared library /home/cihenp/g4proj/MuImag/build/analysis/./MLEM_cc.so
In file included from /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.h:34:0,
                 from /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.cxx:17:
/home/cihenp/g4proj/MuImag/build/analysis/./MLEM.cc: In function ‘int MLEM()’:
/home/cihenp/g4proj/MuImag/build/analysis/./MLEM.cc:39:31: error: ‘to_string’ is not a member of ‘std’
     const string name = "x" + std::to_string(i);
                               ^
g++-4.8: error: /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.o: no such file or directory
Error in <ACLiC>: Compilation failed!
Error: Function MLEM() is not defined in current scope :0:
***Interpreter error recovered***

```

How could this error occur ?

Best,  
Zhi Yu

---

<div class="post-metadata">

**Author:** ![Zhiyhep](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhiyhep/32/16095_2.png) [@Zhiyhep](https://root-forum.cern.ch/u/Zhiyhep)\
**Post date:** [March 20, 2018, 2:25pm UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367/4 "2018-03-20T14:25:03Z")

</div>

And thanks for pointing out a potential memory error.

I find that I defined a dynamic array in the Likelihood\_FCN::Init() method.  
I redefine the array as a private member of the class and the minimizer seems to be giving reasonable results now, at least the -nan value has disappeared.

Best,

Zhi Yu

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 3, 2018, 2:25pm UTC](https://root-forum.cern.ch/t/minuit2-gives-nan-results/28367/5 "2018-04-03T14:25:10Z")

</div>

Closed on April 3, 2018, 2:25pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
