Creating a TF1 with a lambda function crashes when plotting for the first time


ROOT Version: 6.18/04
Platform: 18.04
Compiler: cc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)


I’m defining a TF1 object using a lambda function in the following code:

#include "TF1.h"
#include "TGraph.h"
#include "Math/Functor.h"
#include "Math/IFunction.h"
#include "Math/Integrator.h"
#include "Math/WrappedFunction.h"

#include <vector>
#include <fstream>
#include <iostream>

using namespace std;

void test(){

	ROOT::Math::IntegratorOneDimOptions::SetDefaultIntegrator("AdaptiveSingular");
	//"Gauss", "GaussLegendre", "Adaptive", "AdaptiveSingular", "NonAdaptive"
    
    
    
    //Let's do the thresholds
    double E_low                = 0.001;
    double E_high               = 550.;
    double a                    = 1.e-5;
    double b                    = 1.75;
    double integral_tolerance   = 1.e-5;
    int    energy_points        = 1.e4;
    
    double x_alpha[energy_points], y_alpha[energy_points], p_vector[energy_points];
    
    TF1 *f_Porter_Thomas = new TF1("f_Porter_Thomas", "ROOT::Math::chisquared_pdf(x,1,0)",0,10);
    TF1 *f_alpha = new TF1("f_alpha", "[0]*pow(x,[1])" , E_low, E_high);
    f_alpha->SetParameters(a, b);
    
    for (int i=0; i<energy_points; ++i){
    	x_alpha[i]      = E_low+i*((E_high-E_low)/(energy_points));
    	y_alpha[i]      = f_alpha->Eval( x_alpha[i] );
    	p_vector[i]     = f_Porter_Thomas->Integral(0, y_alpha[i], integral_tolerance);
    }
    
    TGraph *g_p     = new TGraph(energy_points, x_alpha, p_vector);    
    TF1 *f_p   = new TF1("f_p"  ,[&](double*x_alpha, double *params){ return g_p->Eval(x_alpha[0]); }, E_low, E_high, 0);
    f_p->Draw("L");
    
}

I’m compiling the code in a root session using .L test.C++ and then executing using test(). The compiler doesn’t complain, however, when I execute if for the first time I get the following stack:

root [0] .L test_root_forum.C++
Info in <TUnixSystem::ACLiC>: creating shared library /mnt/macau-data/0/dicer/astamato/analysis/2020/Sm/149Sm/R_Matrix/cumulative_Gn0/./test_root_forum_C.so
Warning in cling::IncrementalParser::CheckABICompatibility():
  Possible C++ standard library mismatch, compiled with __GLIBCXX__ '20200808'
  Extraction of runtime standard library version was: '20210601'
root [1] 
root [1] test()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f8cee1aec3a in __GI___wait4 (pid=3688258, stat_loc=stat_loc
entry=0x7ffc9b5dd4a8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007f8cee1aebfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffc9b5dd4a8, options=options
entry=0) at waitpid.c:38
#2  0x00007f8cee11df67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007f8cee861aee in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x55915320d800) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:2119
#4  TUnixSystem::StackTrace (this=0x55915320d800) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:2413
#5  0x00007f8cee85e7cc in TUnixSystem::DispatchSignals (this=0x55915320d800, sig=kSigSegmentationViolation) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:3644
#6  <signal handler called>
#7  0x00007f8ce2c3a84c in ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl<double>, test()::{lambda(double*, double*)#1}>::operator()(double*, double*) () from /mnt/macau-data/0/dicer/astamato/analysis/2020/Sm/149Sm/R_Matrix/cumulative_Gn0/test_root_forum_C.so
#8  0x00007f8ce31ec590 in ROOT::Math::ParamFunctorTempl<double>::operator() (this=<optimized out>, p=<optimized out>, x=0x7ffc9b5e0818) at /home/daq/root/build/include/Math/ParamFunctor.h:362
#9  TF1::EvalPar (this=0x5591550488a0, x=0x7ffc9b5e0818, params=0x0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1382
#10 0x00007f8ce31f50a2 in TF1::DoCreateHistogram (this=0x5591550488a0, xmin=<optimized out>, xmax=<optimized out>, recreate=<optimized out>) at /home/daq/root/src/hist/hist/src/TF1.cxx:3004
#11 0x00007f8ce31f46b1 in TF1::Paint (this=0x5591550488a0, choptin=<optimized out>) at /home/daq/root/src/hist/hist/src/TF1.cxx:2860
#12 0x00007f8ce2e5479e in TPad::PaintModified (this=0x559154ff7d70) at /home/daq/root/build/include/TString.h:239
#13 0x00007f8ce2e1458c in TCanvas::Update (this=0x559154ff7d70) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:2323
#14 0x00007f8ceaad93b1 in TCling::UpdateAllCanvases () at /home/daq/root/src/core/metacling/src/TCling.cxx:6318
#15 0x00007f8ceaae3173 in TCling::ProcessLine (this=<optimized out>, line=0x7f8ced03dc3b "TRint::EndOfLineAction();", error=0x0) at /home/daq/root/src/core/metacling/src/TCling.cxx:2206
#16 0x00007f8ceaad5054 in TCling::ProcessLineSynch (error=0x0, line=0x7f8ced03dc3b "TRint::EndOfLineAction();", this=0x55915327fdf0) at /home/daq/root/src/core/metacling/src/TCling.cxx:3168
#17 TCling::EndOfLineAction (this=0x55915327fdf0) at /home/daq/root/src/core/metacling/src/TCling.cxx:2819
#18 0x00007f8cee9ef360 in TRint::HandleTermInput (this=0x55915326a940) at /home/daq/root/src/core/rint/src/TRint.cxx:642
#19 0x00007f8cee85da72 in TUnixSystem::CheckDescriptors (this=this
entry=0x55915320d800) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:1322
#20 0x00007f8cee85f6f8 in TUnixSystem::DispatchOneEvent (this=0x55915320d800, pendingOnly=<optimized out>) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:1077
#21 0x00007f8cee77ca89 in TSystem::InnerLoop (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#22 TSystem::Run (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#23 0x00007f8cee713553 in TApplication::Run (this=this
entry=0x55915326a940, retrn=retrn
entry=false) at /home/daq/root/src/core/base/src/TApplication.cxx:1183
#24 0x00007f8cee9f08e8 in TRint::Run (this=0x55915326a940, retrn=<optimized out>) at /home/daq/root/src/core/rint/src/TRint.cxx:460
#25 0x0000559152858180 in main (argc=<optimized out>, argv=0x7ffc9b5e32b8) at /home/daq/root/src/main/src/rmain.cxx:30
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#7  0x00007f8ce2c3a84c in ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl<double>, test()::{lambda(double*, double*)#1}>::operator()(double*, double*) () from /mnt/macau-data/0/dicer/astamato/analysis/2020/Sm/149Sm/R_Matrix/cumulative_Gn0/test_root_forum_C.so
#8  0x00007f8ce31ec590 in ROOT::Math::ParamFunctorTempl<double>::operator() (this=<optimized out>, p=<optimized out>, x=0x7ffc9b5e0818) at /home/daq/root/build/include/Math/ParamFunctor.h:362
#9  TF1::EvalPar (this=0x5591550488a0, x=0x7ffc9b5e0818, params=0x0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1382
#10 0x00007f8ce31f50a2 in TF1::DoCreateHistogram (this=0x5591550488a0, xmin=<optimized out>, xmax=<optimized out>, recreate=<optimized out>) at /home/daq/root/src/hist/hist/src/TF1.cxx:3004
#11 0x00007f8ce31f46b1 in TF1::Paint (this=0x5591550488a0, choptin=<optimized out>) at /home/daq/root/src/hist/hist/src/TF1.cxx:2860
#12 0x00007f8ce2e5479e in TPad::PaintModified (this=0x559154ff7d70) at /home/daq/root/build/include/TString.h:239
#13 0x00007f8ce2e1458c in TCanvas::Update (this=0x559154ff7d70) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:2323
#14 0x00007f8ceaad93b1 in TCling::UpdateAllCanvases () at /home/daq/root/src/core/metacling/src/TCling.cxx:6318
===========================================================


Root > test()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

A TCanvas pops up but it’s empty. If I re-execute, the function gets drawn. If however I click on the TCanvas I get a different stack of errors.


Root > test()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3] 
 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f8cee1aec3a in __GI___wait4 (pid=3688677, stat_loc=stat_loc
entry=0x7ffc9b5dd8e8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007f8cee1aebfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffc9b5dd8e8, options=options
entry=0) at waitpid.c:38
#2  0x00007f8cee11df67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007f8cee861aee in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x55915320d800) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:2119
#4  TUnixSystem::StackTrace (this=0x55915320d800) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:2413
#5  0x00007f8cee85e7cc in TUnixSystem::DispatchSignals (this=0x55915320d800, sig=kSigSegmentationViolation) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:3644
#6  <signal handler called>
#7  0x00007f8ce2c3a84c in ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl<double>, test()::{lambda(double*, double*)#1}>::operator()(double*, double*) () from /mnt/macau-data/0/dicer/astamato/analysis/2020/Sm/149Sm/R_Matrix/cumulative_Gn0/test_root_forum_C.so
#8  0x00007f8ce31ec590 in ROOT::Math::ParamFunctorTempl<double>::operator() (this=<optimized out>, p=<optimized out>, x=0x7ffc9b5e0c20) at /home/daq/root/build/include/Math/ParamFunctor.h:362
#9  TF1::EvalPar (this=0x5591558397a0, x=0x7ffc9b5e0c20, params=0x0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1382
#10 0x00007f8ce31efc6e in TF1::Eval (t=0, z=0, y=0, x=<optimized out>, this=0x5591558397a0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1343
#11 TF1::DistancetoPrimitive (this=0x5591558397a0, px=625, py=387) at /home/daq/root/src/hist/hist/src/TF1.cxx:1199
#12 0x00007f8ce2e31ebc in TPad::Pick (this=this
entry=0x5591552f3b20, px=px
entry=625, py=py
entry=387, pickobj=
0x7ffc9b5e0d08: 0x7f8ce2e91d40 <TPad::Pick(int, int, TObjLink*&)::dummyLink>) at /home/daq/root/src/graf2d/gpad/src/TPad.cxx:4541
#13 0x00007f8ce2e14efb in TCanvas::Pick (pickobj=
0x7ffc9b5e0d08: 0x7f8ce2e91d40 <TPad::Pick(int, int, TObjLink*&)::dummyLink>, py=387, px=625, this=0x5591552f3b20) at /home/daq/root/build/include/TCanvas.h:188
#14 TCanvas::Pick (this=0x5591552f3b20, px=625, py=387, prevSelObj=0x5591552f3b20) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:1494
#15 0x00007f8ce2e10ec6 in TCanvas::HandleInput (this=0x5591552f3b20, event=kMouseMotion, px=625, py=387) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:1190
#16 0x00007f8ce28018e1 in TRootCanvas::HandleContainerMotion (this=0x559155915410, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TRootCanvas.cxx:1915
#17 0x00007f8ce2724411 in TGFrame::HandleEvent (this=0x559155996650, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TGFrame.cxx:529
#18 0x00007f8ce26d1ade in TGClient::HandleEvent (this=0x559154ef8980, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TGClient.cxx:841
#19 0x00007f8ce26d2295 in TGClient::ProcessOneEvent (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:651
#20 TGClient::ProcessOneEvent (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:643
#21 0x00007f8ce26d22fb in TGClient::HandleInput (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:698
#22 0x00007f8cee85f190 in TUnixSystem::DispatchOneEvent (this=0x55915320d800, pendingOnly=<optimized out>) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:1067
#23 0x00007f8cee77ca89 in TSystem::InnerLoop (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#24 TSystem::Run (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#25 0x00007f8cee713553 in TApplication::Run (this=this
entry=0x55915326a940, retrn=retrn
entry=false) at /home/daq/root/src/core/base/src/TApplication.cxx:1183
#26 0x00007f8cee9f08e8 in TRint::Run (this=0x55915326a940, retrn=<optimized out>) at /home/daq/root/src/core/rint/src/TRint.cxx:460
#27 0x0000559152858180 in main (argc=<optimized out>, argv=0x7ffc9b5e32b8) at /home/daq/root/src/main/src/rmain.cxx:30
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#7  0x00007f8ce2c3a84c in ROOT::Math::ParamFunctorHandler<ROOT::Math::ParamFunctorTempl<double>, test()::{lambda(double*, double*)#1}>::operator()(double*, double*) () from /mnt/macau-data/0/dicer/astamato/analysis/2020/Sm/149Sm/R_Matrix/cumulative_Gn0/test_root_forum_C.so
#8  0x00007f8ce31ec590 in ROOT::Math::ParamFunctorTempl<double>::operator() (this=<optimized out>, p=<optimized out>, x=0x7ffc9b5e0c20) at /home/daq/root/build/include/Math/ParamFunctor.h:362
#9  TF1::EvalPar (this=0x5591558397a0, x=0x7ffc9b5e0c20, params=0x0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1382
#10 0x00007f8ce31efc6e in TF1::Eval (t=0, z=0, y=0, x=<optimized out>, this=0x5591558397a0) at /home/daq/root/src/hist/hist/src/TF1.cxx:1343
#11 TF1::DistancetoPrimitive (this=0x5591558397a0, px=625, py=387) at /home/daq/root/src/hist/hist/src/TF1.cxx:1199
#12 0x00007f8ce2e31ebc in TPad::Pick (this=this
entry=0x5591552f3b20, px=px
entry=625, py=py
entry=387, pickobj=
0x7ffc9b5e0d08: 0x7f8ce2e91d40 <TPad::Pick(int, int, TObjLink*&)::dummyLink>) at /home/daq/root/src/graf2d/gpad/src/TPad.cxx:4541
#13 0x00007f8ce2e14efb in TCanvas::Pick (pickobj=
0x7ffc9b5e0d08: 0x7f8ce2e91d40 <TPad::Pick(int, int, TObjLink*&)::dummyLink>, py=387, px=625, this=0x5591552f3b20) at /home/daq/root/build/include/TCanvas.h:188
#14 TCanvas::Pick (this=0x5591552f3b20, px=625, py=387, prevSelObj=0x5591552f3b20) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:1494
#15 0x00007f8ce2e10ec6 in TCanvas::HandleInput (this=0x5591552f3b20, event=kMouseMotion, px=625, py=387) at /home/daq/root/src/graf2d/gpad/src/TCanvas.cxx:1190
#16 0x00007f8ce28018e1 in TRootCanvas::HandleContainerMotion (this=0x559155915410, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TRootCanvas.cxx:1915
#17 0x00007f8ce2724411 in TGFrame::HandleEvent (this=0x559155996650, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TGFrame.cxx:529
#18 0x00007f8ce26d1ade in TGClient::HandleEvent (this=0x559154ef8980, event=0x7ffc9b5e0f10) at /home/daq/root/src/gui/gui/src/TGClient.cxx:841
#19 0x00007f8ce26d2295 in TGClient::ProcessOneEvent (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:651
#20 TGClient::ProcessOneEvent (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:643
#21 0x00007f8ce26d22fb in TGClient::HandleInput (this=0x559154ef8980) at /home/daq/root/src/gui/gui/src/TGClient.cxx:698
#22 0x00007f8cee85f190 in TUnixSystem::DispatchOneEvent (this=0x55915320d800, pendingOnly=<optimized out>) at /home/daq/root/src/core/unix/src/TUnixSystem.cxx:1067
#23 0x00007f8cee77ca89 in TSystem::InnerLoop (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#24 TSystem::Run (this=0x55915320d800) at /home/daq/root/src/core/base/src/TSystem.cxx:362
#25 0x00007f8cee713553 in TApplication::Run (this=this
entry=0x55915326a940, retrn=retrn
entry=false) at /home/daq/root/src/core/base/src/TApplication.cxx:1183
#26 0x00007f8cee9f08e8 in TRint::Run (this=0x55915326a940, retrn=<optimized out>) at /home/daq/root/src/core/rint/src/TRint.cxx:460
#27 0x0000559152858180 in main (argc=<optimized out>, argv=0x7ffc9b5e32b8) at /home/daq/root/src/main/src/rmain.cxx:30
===========================================================


Root > 
 *** Break *** segmentation violation

Any idea on what might be the issue?

Thanks in advance.

Dear @atha.stam ,

This warning already tells you that you there is a mismatch between the compiler that ROOT uses (i.e. that was used to build ROOT) and some other compiler / standard libraries in your environment. Practically, you should consider anything happening after this warning as invalid.

Make sure that you did not update something in your environment after installing ROOT. Also, if you downloaded the binaries from the website, that you are using the system compiler and not some other compiler. In general, I would suggest to use the conda installation for an easier installation process (I hope it works on ubuntu 18, but keep in mind that this is an ancient distro and you should really upgrade).

Cheers,
Vincenzo

Hello @vpadulan ,

That’s indeed a good point!
Unfortunately I can’t recompile or rebuild root because it lives in a cluster.
Do you really think that this will explain that behaviour?

Thanks for the input!

Dear @atha.stam ,

If the compiler was updated on the cluster nodes but ROOT was not recompiled afterwards, then that definitely could explain the issue you are seeing.

Cheers,
Vincenzo

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