Segmentation Violation, TGraph with SetPoint

Hi everyone, I’m new to Root and, following a tutorial, I was trying to run this macro to plot data that is generated by root.

the program is

# {
#   g = new TGraph ();
#    
#   for (i=0; i<10; i++);
#     {
#          g->SetPoint(i,i,i*i-4*i+7);
#      }
#  g->SetMarkerStyle (22);
#  g->Draw("ALC");
# }

the output is:

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[<unknown binary>] (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitScalarConversion(llvm::Value*, clang::QualType, clang::QualType, clang::SourceLocation) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitForStmt(clang::ForStmt const&, llvm::ArrayRef<clang::Attr const*>) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::CodeGen::FunctionArgList&, clang::Stmt const*) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (no debug info)
[/Roots/install/lib/libCling.so] clang::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) (no debug info)
[/Roots/install/lib/libCling.so] cling::DeclCollector::HandleTopLevelDecl(clang::DeclGroupRef) (no debug info)
[/Roots/install/lib/libCling.so] cling::IncrementalParser::ParseInternal(llvm::StringRef) (no debug info)
[/Roots/install/lib/libCling.so] cling::IncrementalParser::Compile(llvm::StringRef, cling::CompilationOptions const&) (no debug info)
[/Roots/install/lib/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/Roots/install/lib/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/Roots/install/lib/libCling.so] cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) (no debug info)
[/Roots/install/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) /roots/root/core/metacling/src/TCling.cxx:2030
[/Roots/install/lib/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) /roots/root/core/metacling/src/TCling.cxx:2883
[/Roots/install/lib/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) /roots/root/core/base/src/TApplication.cxx:1137
[/Roots/install/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) /roots/root/core/rint/src/TRint.cxx:756
[/Roots/install/lib/libRint.so] TRint::HandleTermInput() /roots/root/core/rint/src/TRint.cxx:603
[/Roots/install/lib/libCore.so] TUnixSystem::CheckDescriptors() /roots/root/core/unix/src/TUnixSystem.cxx:1323
[/Roots/install/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) /roots/root/core/macosx/src/TMacOSXSystem.mm:378
[/Roots/install/lib/libCore.so] TSystem::InnerLoop() /roots/root/core/base/src/TSystem.cxx:411
[/Roots/install/lib/libCore.so] TSystem::Run() /roots/root/core/base/src/TSystem.cxx:361
[/Roots/install/lib/libCore.so] TApplication::Run(bool) /roots/root/core/base/src/TApplication.cxx:1154
[/Roots/install/lib/libRint.so] TRint::Run(bool) /roots/root/core/rint/src/TRint.cxx:458
[/Roots/install/bin/./root.exe] main /roots/root/main/src/rmain.cxx:32
[/usr/lib/system/libdyld.dylib] start (no debug info)
[<unknown binary>] (no debug info)

Is anyone able to help me? I’m running root on macOS 10.13
Root version is 6.10/08

TGraph *g = new TGraph();
for (int i = 0; i < 10; i++)

Hi Mike,

there are two little mistakes in your code. Here you can find the correct version of the macro:

{
  auto g = new TGraph();

  for (int i = 0; i < 10; i++) {
    g->SetPoint(i, i, i * i - 4 * i + 7);
  }
  g->SetMarkerStyle(22);
  g->Draw("ALC");
}

I take note of the fact that a crash in this case is less than optimal. Arguably the best behaviour would be the interpreter erroring out nicely.

Cheers,
Danilo

1 Like
{
  auto g = new TGraph();

  for (int i = 0; i &lt; 10; i++) {
    g-&gt;SetPoint(i, i, i * i - 4 * i + 7);
  }
  g-&gt;SetMarkerStyle(22);
  g-&gt;Draw("ALC");
}

That worked! Thank you very much Danilo

For what I could understand it’s crucial that I declare the variable “i” to be an int type otherwise root will look for every number between 0 and 10 which is an impossible task, am I right?

Also, what’s the “auto” before “g” in the second line of the script doing? I noticed that root is still able to generate a canvas even if I neglet to specify it in the Macro.

Thank you for your time,
Michael

Yes it is. In C++ programming all the variables need to be declared.

In the past version of C++, to declare a pointer to a class (a TGraph in your case) you would have wrote:

TGraph  *g = new TGraph();

As you can see in a such declaration the class name was mentioned twice, it was therefore a bit redondant … That’s why the latest incarnation of C++ allows some automatic finding of the class type ( auto)… and you can write:

auto g = new TGraph();

Note also that the * before g signaling a pointer can be also omitted as the new keyword on the right side of the equal sign make it obvious it is a pointer.

Yes that’s a special case ROOT implements to ease the production of plots. But that’s not standard C++.

I hope my explanations are clear. May be @dpiparo and/or @Wile_E_Coyote will want to add something.

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