# Fitting and integration (Windows Build Problem)

Dear ROOTers,

The function I want to fit is the integral over three-dim intedgrand.

F(x,y,z) = Int[ f(r,s,t;,x,y,z) ]drdsdt

What I do: first I calculate the integral F(x,y,z), and than I fit F to the data points.

If I’m not mistaken (I’ve seen it in some posts), there is a possibility to perform the fit and the integral calculation at the same time (to reduce the running time).

If it is correct, I’ll appreciate any links or examples on this subject.

Thanks,

Andrey.

Exploring the Roottalk forum, I found very interesting post of Moneta, where he suggested how to overcome the problem of fitting 3D function, here I copy-pasted his code:

void exampleFit3D() {

const int n = 1000;
double x[n], y[n], z[n], v[n];
double ev = 0.1;

// generate the data
TRandom2 r;
for (int i = 0; i < n; ++i) {
x[i] = r.Uniform(0,10);
y[i] = r.Uniform(0,10);
z[i] = r.Uniform(0,10);
v[i] = sin(x[i] ) + cos(y[i]) + z[i] + r.Gaus(0,ev);
}

// fill the fit data
ROOT::Fit::BinData data(n,3);
double xx[3];
for(int i = 0; i < n; ++i) {
xx[0] = x[i];
xx[1] = y[i];
xx[2] = z[i];
}

TF3 * f3 = new TF3(“f3”,"[0] * sin(x) + [1] * cos(y) + [2] * z",0,10,0,10,0,10);
f3->SetParameters(2,2,2);
ROOT::Fit::Fitter fitter;
ROOT::Math::WrappedMultiTF1 wf(*f3,3);
fitter.SetFunction(wf);
bool ret = fitter.Fit(data);
if (ret) {
ROOT::Fit::FitResult & res = fitter.Result();
res.Print(std::cout);
}
else
cout << “Fit Failed” << endl;
}

Trying to run the code, compiler returned the error:

" error C2440: ‘initializing’ : cannot convert from ‘const ROOT::Fit::FitResult’ to ‘ROOT::Fit::FitResult &’ "

I use MVS C++ 2008 express edition, Wndows 7 and 5.24 ROOT version.

I run my code on Visual Studio using the integrated ROOT libraries.

I would appreciate any suggestions,

Thanks,

Andrey.

Replace`ROOT::Fit::FitResult & res = fitter.Result();`with`ROOT::Fit::FitResult res( fitter.Result() ); `
Philippe.

[quote=“pcanal”]Replace`ROOT::Fit::FitResult & res = fitter.Result();`with`ROOT::Fit::FitResult res( fitter.Result() ); `
Philippe.[/quote]

Great!! Tank you Philippe! Worked fine for me, but…

…but there is a new problem: debagger shows a problem (the arrow points to the problem line):

"…
bool ret = fitter.Fit(data);
if (ret) {
ROOT::Fit::FitResult res( fitter.Result() );
res.Print(std::cout);
—> }
else
cout << “Fit Failed” << endl;

The message is:
" Unhandled exception at 0x75de9617 in root_example_new.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002a6ea8"

Probably the code has some discrepancy while runnig under VS C++.

Sorry for bothering with such a simple problem.

[quote]Probably the code has some discrepancy while runnig under VS C++.[/quote]Did you make sure to compile your code with the same options, including debug vs release as the ROOT libraries that you are using?

Philippe.

[quote]Did you make sure to compile your code with the same options, including debug vs release as the ROOT libraries that you are using?

Philippe.[/quote]

Dear Philippe,

It’s not very clear for me what does it mean. I downloaded MSI version for VS++9 and installed. Afterwards I worte a path in Tool/Options/Project and Solution/VC++ Directories and put, “C:\root\lib”, “C:\root\bin” and “C:\root\include”

and also in: “Project/Properties/Linker/Input” I included all “…*.lib” libraries

It works very well and I can compile everything I want (up to this code I posted in above).

Do I need to to something else?

A.K.

Philippe.

[quote=“pcanal”]The MSI version is available in both Release and Debug? Which one did you download?

Philippe.[/quote]

A.K.

Hi,

If you are using the release version of ROOT, you have to compile your application in release mode, using the [color=#BF0000]/MD[/color] compiler flag (to use the [color=#BF0000]Multi-threaded DLL[/color] runtime library)

And if you want to compile and use your application in debug mode, you have to use the debug version of ROOT, and use the [color=#BF0000]/MDd[/color] compiler flag ([color=#BF0000]Multi-threaded Debug DLL[/color] runtime library)

Mixing different runtime libraries (debug/release) is not allowed with recent versions of MS Visual C++

Cheers, Bertrand.

Dear Bertrand,

[quote=“bellenot”]Hi,
you have to compile your application in release mode, using the [color=#BF0000]/MD[/color] compiler flag (to use the [color=#BF0000]Multi-threaded DLL[/color] runtime library)

And if you want to compile and use your application in debug mode, you have to use the debug version of ROOT, and use the [color=#BF0000]/MDd[/color] compiler flag ([color=#BF0000]Multi-threaded Debug DLL[/color] runtime library)[/quote]

Isn’t it strange, that I still can compile and run my code on VC++ only by specifying the directories (Linker) and path to installed ROOT lib’s?

Thank you for helping me,

Andrey.

Hi Andrey,

I assume you are working with a Visual Studio Project/Solution (sln). In this case, the settings I was talking about are accessible in the project properties dialog (see attachment as example)
If you are working with nmake makefiles, just take a look at the \$(ROOTSYS)/test/Makefile.win32

There is no problem linking with incorrect versions of the libraries. The problems appear only at run time.

Cheers, Bertrand.

Dear Bertrand,

I followed your instructions and used the /MD compiler flag (BTW, by default it was set to /MDd mode).

But, during the building process, the following error occurred:

"error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function “public: double const & __thiscall std::_Vector_const_iterator<double,class std::allocator >::operator*(void)const " (??D?\$_Vector_const_iterator@NV?\$allocator@N@std@@@std@@QBEABNXZ)”

running the same code, but using “/MDd” mode, reveals no error.

Any help?

Thank you,

Andrey.

Looks like a mix of debug & release code in your project… Make sure you are building your application in [color=#BF0000]Release[/color] mode. This is probably not the case, since the option was set to /MDd…

Bertrand.

So, if I understand this correctly, I should pay attention to correct matching of the versions.

In order to solve this problem I will re-install a new ROOT Release version 5.27 (just to make sure I use indeed the Release version), will set \MD option and that’s it.

The content of my code remains untouched?

Dear All,

Nothing helps. I re-installed the Release of version (MSI), set Code Generation to /MD mode and still get the following error:

"
example_new.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: double const & __thiscall std::_Vector_const_iterator<double,class std::allocator >::operator*(void)const " (??D?\$_Vector_const_iterator@NV?\$allocator@N@std@@@std@@QBEABNXZ)
"

(I build the code (“solution”) and use “start without debagging” option to run the program)

If I change to /MDd option, the code makes no problem)

Thanks,

Andrey.

Andrey,

You still didn’t tell me if you were using a project/solution or a nmake makefile, but if you are using a project/solution, as I told you before, make sure you build in Release mode (see attached screenshot). If it still doesn’t work for you, I’m afraid you will have to post your project (or any project reproducing the problem), to be able to go further (as we’re going nowhere like this)…

Best regards,
Bertrand.

Dear Bertrand,

I do not know how to express my deep appreciation to you, you’re genius!

You pointed to the correct place I always missed (how to build in Release/Debag mode (the encircled bar, in red).

Now it works perfectly!

P.S. In order to save your time, explaining the obvious things, may be it is worth making a very brief tutorial “for dummies” how to install ROOT and what to do in order to run it properly. Looking for the solution to my problem, I found not a few simillar questions on this forum, regarding the same problem (mixing versions and run).

At any case, I would like to thank you and others, who helped me.

Best regards,

Andrey.