Multi-dimensional adaptive integration

Hi,

I didn’t find any documentation about the usage of multi-dimensional adaptive integration in ROOT. I looked at the source code and the tutorial mathmoreIntegration.C and came up with the following approach. Is it OK or should I use something else? It calculates the volume of a sphere with radius R=2 in spherical coordinates.

#include "TMath.h"
#include "Math/WrappedFunction.h"
#include "Math/IntegratorMultiDim.h"
#include <iostream>

Double_t func(const Double_t *x){ 
    return x[0]*x[0]*TMath::Sin(x[1]);
}

void multiDimIntegration()
{
    Double_t R = 2.0;  // sphere radius
    // numeric integration
    ROOT::Math::WrappedMultiFunction<> f1(func,3);
    ROOT::Math::IntegratorMultiDim ig(f1);
    Double_t xmin[3] = {0.0, 0.0, 0.0};
    Double_t xmax[3] = {R, TMath::Pi(), 2*TMath::Pi()};
    Double_t volume1 = ig.Integral(xmin, xmax);
    // analytic formula
    Double_t volume2 = 4.0/3*TMath::Pi()*R*R*R;
    
    std::cout << scientific;
    std::cout << "volume1 = " << volume1 << std::endl;
    std::cout << "volume2 = " << volume2 << std::endl;
}

The compiled version works OK, the interpreted version crashes:

root [] .x multiDimIntegration.C++ // works OK
Info in <ACLiC>: unmodified script has already been compiled and loaded
Info in <ACLiC>: it will be regenerated and reloaded!
Info in <TUnixSystem::ACLiC>: creating shared library /home/malusek/work/root/./multiDimIntegration_C.so
volume1 = 3.351032e+01
volume2 = 3.351032e+01

root [] .x multiDimIntegration.C  // crashes
Syntax error /lw/root/5.22.00/include/Math/WrappedFunction.h:159:
Error: Symbol FreeMultiFunctionPtrfFunc is not defined in current scope  /lw/root/5.22.00/include/Math/WrappedFunction.h:183:
Warning: Error occurred during reading source files
Warning: Error occurred during dictionary source generation
!!!Removing /home/malusek/work/root/AutoDict_ROOT__Math__WrappedMultiFunction_FreeMultiFunctionPtr__cxx_ACLiC_dict.cxx /home/malusek/work/root/AutoDict_ROOT__Math__WrappedMultiFunction_FreeMultiFunctionPtr__cxx_ACLiC_dict.h !!!
Error: /lw/root/5.22.00/bin/rootcint: error loading headers...
Error in <ACLiC>: Dictionary generation failed!
Syntax error Math/WrappedFunction.h:159:
Error: abstract class object 'WrappedMultiFunction<FreeMultiFunctionPtr> f1' declared multiDimIntegration.C:14:

 *** Break *** segmentation violation
Using host libthread_db library "/lib/libthread_db.so.1".
Attaching to program: /proc/10171/exe, process 10171
[Thread debugging using libthread_db enabled]
[New Thread 0xb6c156d0 (LWP 10171)]
0xffffe410 in __kernel_vsyscall ()
#1  0xb6cd7123 in waitpid () from /lib/libc.so.6
#2  0xb6c7fb5b in ?? () from /lib/libc.so.6
#3  0xb6d8950d in system () from /lib/libpthread.so.0
#4  0xb79e57bd in TUnixSystem::Exec () from /lw/root/5.22.00/lib/libCore.so
#5  0xb79ec453 in TUnixSystem::StackTrace ()
   from /lw/root/5.22.00/lib/libCore.so
#6  0xb79ea0f6 in TUnixSystem::DispatchSignals ()
   from /lw/root/5.22.00/lib/libCore.so
#7  0xb79ea1dd in SigHandler () from /lw/root/5.22.00/lib/libCore.so
#8  0xb79e2fdd in sighandler () from /lw/root/5.22.00/lib/libCore.so
#9  <signal handler called>
#10 0x083da2d2 in ?? ()
#11 0x083d7fd8 in ?? ()
#12 0x6f722f77 in ?? ()
#13 0x083cf150 in ?? ()
#14 0xb7246eab in G__defined_templateclass ()
   from /lw/root/5.22.00/lib/libCint.so
#15 0xb70f8d95 in Cint::G__ExceptionWrapper ()
   from /lw/root/5.22.00/lib/libCint.so
#16 0xb71b2816 in G__execute_call () from /lw/root/5.22.00/lib/libCint.so
#17 0xb71b3990 in G__call_cppfunc () from /lw/root/5.22.00/lib/libCint.so
#18 0xb7187091 in G__interpret_func () from /lw/root/5.22.00/lib/libCint.so
#19 0xb71757e7 in G__getfunction () from /lw/root/5.22.00/lib/libCint.so
#20 0xb71962f0 in G__basedestructor () from /lw/root/5.22.00/lib/libCint.so
#21 0xb7176f31 in G__getfunction () from /lw/root/5.22.00/lib/libCint.so
#22 0xb7229307 in G__destroy_upto_vararray ()
   from /lw/root/5.22.00/lib/libCint.so
#23 0xb722973d in G__destroy_upto () from /lw/root/5.22.00/lib/libCint.so
#24 0xb718a7df in G__interpret_func () from /lw/root/5.22.00/lib/libCint.so
#25 0xb7175932 in G__getfunction () from /lw/root/5.22.00/lib/libCint.so
#26 0xb7149a0b in G__getitem () from /lw/root/5.22.00/lib/libCint.so
#27 0xb715074d in G__getexpr () from /lw/root/5.22.00/lib/libCint.so
#28 0xb715c402 in G__calc_internal () from /lw/root/5.22.00/lib/libCint.so
#29 0xb71f0bb1 in G__process_cmd () from /lw/root/5.22.00/lib/libCint.so
#30 0xb79d33a0 in TCint::ProcessLine () from /lw/root/5.22.00/lib/libCore.so
#31 0xb79d305f in TCint::ProcessLineSynch ()
   from /lw/root/5.22.00/lib/libCore.so
#32 0xb78fc984 in TApplication::ExecuteFile ()
   from /lw/root/5.22.00/lib/libCore.so
#33 0xb78fcf54 in TApplication::ProcessFile ()
   from /lw/root/5.22.00/lib/libCore.so
#34 0xb78fa164 in TApplication::ProcessLine ()
   from /lw/root/5.22.00/lib/libCore.so
#35 0xb6edeae1 in TRint::HandleTermInput ()
   from /lw/root/5.22.00/lib/libRint.so
#36 0xb6edce75 in TTermInputHandler::Notify ()
   from /lw/root/5.22.00/lib/libRint.so
#37 0xb6edf594 in TTermInputHandler::ReadNotify ()
   from /lw/root/5.22.00/lib/libRint.so
#38 0xb79e9dd5 in TUnixSystem::CheckDescriptors ()
   from /lw/root/5.22.00/lib/libCore.so
#39 0xb79ea8af in TUnixSystem::DispatchOneEvent ()
   from /lw/root/5.22.00/lib/libCore.so
#40 0xb7959771 in TSystem::InnerLoop () from /lw/root/5.22.00/lib/libCore.so
#41 0xb795cef1 in TSystem::Run () from /lw/root/5.22.00/lib/libCore.so
#42 0xb78f8a07 in TApplication::Run () from /lw/root/5.22.00/lib/libCore.so
#43 0xb6edf090 in TRint::Run () from /lw/root/5.22.00/lib/libRint.so
#44 0x08048ef5 in main ()
The program is running.  Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/10171/exe, process 10171
Root > Function multiDimIntegration() busy flag cleared

Platform: ROOT 5.22/00 on openSUSE 10.3 (x86).

Hi,

just replace the line

ROOT::Math::WrappedMultiFunction<> f1(func,3); 

with

ROOT::Math::Functor f1(func,3);

and your macro will work in interpreted mode. In the previous case CINT had some problem understanding the template class.

Regards

Lorenzo

Hi,

Thanks for your reply. It works with CINT but it doesn’t work with ACLiC now. How can I fix it so that it works with both?

#ifndef __CINT__
#include "TMath.h"
#include "Math/Functor.h"
#include "Math/IntegratorMultiDim.h"
#include <iostream>
#endif

Double_t func(const Double_t *x){ 
    return x[0]*x[0]*TMath::Sin(x[1]);
}

void multiDimIntegration()
{
    Double_t R = 2.0;  // sphere radius
    // numeric integration
    ROOT::Math::Functor f1(func,3); 
    ROOT::Math::IntegratorMultiDim ig(f1);
    Double_t xmin[3] = {0.0, 0.0, 0.0};
    Double_t xmax[3] = {R, TMath::Pi(), 2*TMath::Pi()};
    Double_t volume1 = ig.Integral(xmin, xmax);
    // analytic formula
    Double_t volume2 = 4.0/3*TMath::Pi()*R*R*R;
    
    std::cout << scientific;
    std::cout << "volume1 = " << volume1 << std::endl;
    std::cout << "volume2 = " << volume2 << std::endl;
}
root [] .x multiDimIntegration.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/malusek/work/root/./multiDimIntegration_C.so
/lw/root/5.22.00/include/Math/Functor.h: In instantiation of ‘ROOT::Math::FunctorHandler<ROOT::Math::Functor, Double_t ()(const Double_t*)>’:
/lw/root/5.22.00/include/Math/Functor.h:370:   instantiated from ‘ROOT::Math::Functor::Functor(const Func&, unsigned int) [with Func = Double_t ()(const Double_t*)]’
/home/malusek/work/root/./multiDimIntegration.C:16:   instantiated from here
/lw/root/5.22.00/include/Math/Functor.h:99: error: field ‘ROOT::Math::FunctorHandler<ROOT::Math::Functor, Double_t ()(const Double_t*)>::fFunc’ invalidly declared function type
g++: /home/malusek/work/root/./multiDimIntegration_C_ACLiC_dict.o: není souborem ani adresářem
Error in <ACLiC>: Compilation failed!
Error: Function multiDimIntegration() is not defined in current scope  :0:
*** Interpreter error recovered ***
1 Like

Sorry for the previous post, the correct syntax working in C++ and CINT is:

 ROOT::Math::Functor f1(&func,3);

Lorenzo

It works now. Thank you for your help.

It would be nice to have simple examples available in the reference documentation, e.g. in http://root.cern.ch/root/html522/ROOT__Math__IntegratorMultiDim.html. Has anybody thought about it? It takes much less time to adapt a simple example than to start from scratch by studying the source code.

1 Like

I agree with your point. We are in did working now on improving the documentation and examples.
Thank you for your feedback

Best Regards
Lorenzo

1 Like