Break Segmentation


ROOT Version: v6-13-02
Platform: macosx64
Compiler: Not Provided


Dear All,

I am trying to create a histogram from a periodic
function

root [3] h = new **TH1F** ("h","h",100,-20,20);

root [4] for **(** i=0; i<h->GetNbinsX(); i++ **)**


*** Break *** segmentation violation

[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[<unknown binary>] (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitScalarConversion(llvm::Value*, clang::QualType, clang::QualType, clang::SourceLocation) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitForStmt(clang::ForStmt const&, llvm::ArrayRef<clang::Attr const*>) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::CodeGen::FunctionArgList&, clang::Stmt const*) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (no debug info)

[/Users/sn/root/lib/libCling.so] clang::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) (no debug info)

[/Users/sn/root/lib/libCling.so] cling::DeclCollector::HandleTopLevelDecl(clang::DeclGroupRef) (no debug info)

[/Users/sn/root/lib/libCling.so] cling::IncrementalParser::ParseInternal(llvm::StringRef) (no debug info)

[/Users/sn/root/lib/libCling.so] cling::IncrementalParser::Compile(llvm::StringRef, cling::CompilationOptions const&) (no debug info)

[/Users/sn/root/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)

[/Users/sn/root/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)

[/Users/sn/root/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)

[/Users/sn/root/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)

[/Users/sn/root/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)

[/Users/sn/root/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)

[/Users/sn/root/lib/libRint.so] TRint::HandleTermInput() (no debug info)

[/Users/sn/root/lib/libCore.so] TUnixSystem::CheckDescriptors() (no debug info)

[/Users/sn/root/lib/libCore.so] TUnixSystem::DispatchOneEvent(bool) (no debug info)

[/Users/sn/root/lib/libCore.so] TSystem::InnerLoop() (no debug info)

[/Users/sn/root/lib/libCore.so] TSystem::Run() (no debug info)

[/Users/sn/root/lib/libCore.so] TApplication::Run(bool) (no debug info)

[/Users/sn/root/lib/libRint.so] TRint::Run(bool) (no debug info)

[/Users/sn/root/bin/root.exe] main (no debug info)

[/usr/lib/system/libdyld.dylib] start (no debug info)

Root > h->SetBinContent(i,f->Eval(h->GetBinCenter(i)));

Can someone kindly assist me with correcting this error, please?

Thanks All

You need to define “i” in the “for” loop, and if you write “SetBinContent” in a separate line, you need brackets (alternatively, write SetBinContent in the same line as “for”, if it is the only command inside the loop). Supposing you already defined “f” before this:

root [4] for (Int_t i=0; iGetNbinsX(); i++ ) h->SetBinContent(i,f->Eval(h->GetBinCenter(i)));
or:
root [4] for (Int_t i=0; iGetNbinsX(); i++ ) {
root (cont’ed, cancel with .@) [5]h->SetBinContent(i,f->Eval(h->GetBinCenter(i)));
root (cont’ed, cancel with .@) [6] (any other lines inside the loop, with “;”, brackets after the last one) }

EDIT: By the way, you probably want to start filling the histogram from bin = 1, not zero, check here:
https://root.cern.ch/root/htmldoc/guides/users-guide/Histograms.html#bin-numbering
so either start the loop from i=1, or use h->SetBinContent(i+1,…)

Yes, “f” was defined before. Thank you so much for your response.

The problem is the missing declaration of i as int i, e.g. in for (int i=0;

That’s a bug, thanks for reporting! I’ll let you know what happens to it!

Axel

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

I opened a bug report, finally: https://sft.its.cern.ch/jira/browse/ROOT-10309