Compilation error

Please help… error after compilation below the code… thanks :slight_smile:

//=====
#include "TF1.h"
#include "Math/WrappedTF1.h"
#include "Math/GaussIntegrator.h"
#include <TGraph.h>
#include <TMultiGraph.h>

void ScatteringRecoilFactor()
{
 double qt = 1.140;     // transfer momentum in GeV
 double mt = 1.0;       // mass of the target in GeV

 c1 = new TCanvas("c1", "Possible Low  Beam Energies", 800, 800);
 c1->Divide(2,2);

 c1->cd(1);
 // TF1 *f1 = new TF1("f1","(qt/2.0 + sqrt(qt*qt*x*x + 2.0*qt*mt)/(2.0*x)", -1.0, 1.0);
 TF1 *f1 = new TF1("f1","[0]/2.0 + sqrt([0]*[0]*x*x + 2.0*[0]*[1])/(2.0*x)", -1.0, 1.0);
 f1->SetParameter(0,qt);
 f1->SetParameter(1,mt);
 f1->SetLineColor(kBlue+2);
 f1->SetLineWidth(3);
 f1->SetTitle("Recoil Factor; Sine of Scattering Angle; Incident Beam Energy");
 f1->Draw();
 // gPad->SetLogy(1);

 c1->cd(2);
 TF1 *f2 = new TF1("f2","[0]/2.0 - sqrt([0]*[0]*x*x + 2.0*[0]*[1])/(2.0*x)", -1.0, 1.0);
 f2->SetParameter(0,qt);
 f2->SetParameter(1,mt);
 f2->SetLineColor(kBlue+2);
 f2->SetLineWidth(3);
 f2->SetTitle("Recoil Factor; Sine of Scattering Angle; Incident Beam Energy");
 f2->Draw();
 // gPad->SetLogy(1);

 c1->cd(3);
 TF1 *f3 = new TF1("f3","[0]/2.0 + sqrt([0]*[0]*x*x + 2.0*[0]*[1])/(2.0*x)", -1.0, 1.0);
 f3->SetParameter(0,qt);
 f3->SetParameter(1,mt);
 f3->SetLineColor(kBlue+2);
 f3->SetLineWidth(3);
 f3->SetTitle("Recoil Factor; Sine of Scattering Angle; Incident Beam Energy");
 f3->Draw();
 // gPad->SetLogy(1);
 c1->cd(4);
 TF1 *f4 = new TF1("f4","[0]/2.0 + sqrt([0]*[0]*x*x + 2.0*[0]*[1])/(2.0*x)", -1.0, 1.0);
 f4->SetParameter(0,qt);
 f4->SetParameter(1,mt);
 f4->SetLineColor(kBlue+2);
 f4->SetLineWidth(3);
 f4->SetTitle("Recoil Factor; Sine of Scattering Angle; Incident Beam Energy");
 f4->Draw();
 // gPad->SetLogy(1);
 f1->Draw("same");
 f2->Draw("same");
 f3->Draw("same");
 // gPad->SetLogy(1);
}
//=====

The error message:

Warning in <TCanvas::Constructor>: Deleting canvas with same name: c1

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f786063407a in __GI___waitpid (pid=5677, stat_loc=stat_loc
entry=0x7ffc2981ba40, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f78605acfbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f78615b33f4 in TUnixSystem::StackTrace() () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#3  0x00007f78615b5a5c in TUnixSystem::DispatchSignals(ESignals) () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#4  <signal handler called>
#5  0x0000000002121bf8 in ?? ()
#6  0x00007f784abc9d63 in FT_Done_Glyph () from /usr/lib/x86_64-linux-gnu/libfreetype.so.6
#7  0x00007f784af283d8 in TTF::LayoutGlyphs() () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#8  0x00007f784af28a8f in TTF::GetTextExtent(unsigned int&, unsigned int&, char*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#9  0x00007f784af44cf0 in TLatex::Analyse(double, double, TLatex::TextSpec_t, char const*, int) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#10 0x00007f784af46311 in TLatex::Anal1(TLatex::TextSpec_t, char const*, int) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#11 0x00007f784af464c2 in TLatex::FirstParse(double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#12 0x00007f784af46e8c in TLatex::PaintLatex1(double, double, double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#13 0x00007f784af4820f in TLatex::PaintLatex(double, double, double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#14 0x00007f784af10cf7 in TGaxis::PaintAxis(double, double, double, double, double&, double&, int&, char const*, double, bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#15 0x00007f7847f26e59 in THistPainter::PaintAxis(bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libHistPainter.so
#16 0x00007f7847f1e36d in THistPainter::Paint(char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libHistPainter.so
#17 0x00007f784b92996f in TF1::Paint(char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libHist.so
#18 0x00007f784b2414fb in TPad::PaintModified() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#19 0x00007f784b241434 in TPad::PaintModified() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#20 0x00007f784b26479d in TCanvas::Update() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#21 0x00007f785cd00219 in TCling::UpdateAllCanvases() () from /home/jaybee/ROOT/root-6.08.06/lib/libCling.so
#22 0x00007f785cd063a4 in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/jaybee/ROOT/root-6.08.06/lib/libCling.so
#23 0x00007f785ccf8e07 in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /home/jaybee/ROOT/root-6.08.06/lib/libCling.so
#24 0x00007f786110419b in TRint::HandleTermInput() () from /home/jaybee/ROOT/root-6.08.06/lib/libRint.so
#25 0x00007f78615b4f4c in TUnixSystem::CheckDescriptors() () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#26 0x00007f78615b638a in TUnixSystem::DispatchOneEvent(bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#27 0x00007f786153ba64 in TSystem::InnerLoop() () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#28 0x00007f786153a59f in TSystem::Run() () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#29 0x00007f786154a2df in TApplication::Run(bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libCore.so
#30 0x00007f7861105807 in TRint::Run(bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libRint.so
#31 0x000000000040106c in main ()
===========================================================


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.
===========================================================
#5  0x0000000002121bf8 in ?? ()
#6  0x00007f784abc9d63 in FT_Done_Glyph () from /usr/lib/x86_64-linux-gnu/libfreetype.so.6
#7  0x00007f784af283d8 in TTF::LayoutGlyphs() () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#8  0x00007f784af28a8f in TTF::GetTextExtent(unsigned int&, unsigned int&, char*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#9  0x00007f784af44cf0 in TLatex::Analyse(double, double, TLatex::TextSpec_t, char const*, int) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#10 0x00007f784af46311 in TLatex::Anal1(TLatex::TextSpec_t, char const*, int) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#11 0x00007f784af464c2 in TLatex::FirstParse(double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#12 0x00007f784af46e8c in TLatex::PaintLatex1(double, double, double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#13 0x00007f784af4820f in TLatex::PaintLatex(double, double, double, double, char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#14 0x00007f784af10cf7 in TGaxis::PaintAxis(double, double, double, double, double&, double&, int&, char const*, double, bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libGraf.so
#15 0x00007f7847f26e59 in THistPainter::PaintAxis(bool) () from /home/jaybee/ROOT/root-6.08.06/lib/libHistPainter.so
#16 0x00007f7847f1e36d in THistPainter::Paint(char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libHistPainter.so
#17 0x00007f784b92996f in TF1::Paint(char const*) () from /home/jaybee/ROOT/root-6.08.06/lib/libHist.so
#18 0x00007f784b2414fb in TPad::PaintModified() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#19 0x00007f784b241434 in TPad::PaintModified() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#20 0x00007f784b26479d in TCanvas::Update() () from /home/jaybee/ROOT/root-6.08.06/lib/libGpad.so
#21 0x00007f785cd00219 in TCling::UpdateAllCanvases() () from /home/jaybee/ROOT/root-6.08.06/lib/libCling.so
===========================================================

Well at the moment I can only guess, but unless c1 is a global varaible of your code declared somwhere else you should add a TCanvas* in front of its first declaration. I know that automatic variables are convinient, but it’s no safe way of programming.
FurthermoreI’m wondering what you need the includes except TF1.h for. Is there more code?
The error message tells that a segmentation violatio occurs when root attempts to draw one of the TF1 or more precisely when it attempts to draw text of the axes.
I guess this happens because it tries to access an object which has previously been deleted, my guess would be c1, because of the first line of the error message. Towards this: Why is c1 already existing before your macro runs? Is there a second macro or more code?

thanks :slight_smile: but I just had to transfer it in another folder… it worked :slight_smile:

best regards

but what does this mean? I used c2 instead of c1 and kept running the program.

root [2] .x ScatteringRecoilFactor.C
Warning in TCanvas::Constructor: Deleting canvas with same name: c2

As Triple_S said c1 has already existed before your macro runs, you probably have a second macro runs before this one. Although I can’t reproduce your result, I got this instead :

ScatteringRecoilFactor.cxx:6:2: error: use of undeclared identifier 'c1'
c1 = new TCanvas("c1", "Possible Low  Beam Energies", 800, 800);

From this I’d say that if it is a standalone code, above code line should be like this :

Tcanvas *c1 = new TCanvas("c1", "Possible Low  Beam Energies", 800, 800);

I hope I’ve answered your question.

thank you so much :slight_smile:

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