Segmentation error when I try to draw a histogram

I get a segmentation error when I run the code below. My aim is to draw a histogram that is under a root file.

void histo2()
TFile *f1 = TFile::Open("file05.root", "READ");
TH1F* h1 = (TH1F*)f1->Get("h_mcvtx_muon");


root [0]

Processing histo2.cpp...

*** Break *** bus error

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

[<unknown binary>] (no debug info)

[<unknown binary>] (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] 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)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)

[/usr/local/Cellar/root/6.24.04/lib/root/] TRint::Run(bool) (no debug info)

[/usr/local/Cellar/root/6.24.04/bin/root.exe] main (no debug info)

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

Root >

file05.root (10.7 KB)
histo2.cpp (161 Bytes)


there is something peculiar with the histogram stored in that file of yours. You should really consult with the person who generated file05.root: ask them how they did it exactly.

rootls gives:

% rootls -l file05.root
unique_ptr<TH1F,default_delete<TH1F> >  Oct 21 18:00 2021 h_mcvtx_muon;2 "object title" [current cycle]
unique_ptr<TH1F,default_delete<TH1F> >  Oct 21 18:00 2021 h_mcvtx_muon;1 "object title" [backup cycle]

@Axel Try (it first generates an error that it does not need a dictionary and then it generates an error that this dictionary does not exist):

[...]$ root -q -e 'gInterpreter->GenerateDictionary("unique_ptr<TH1F>", "TH1.h;memory");' file05.root

Error: It is not necessary to explicitly select class unique_ptr<TH1F,default_delete<TH1F> >. I/O is supported for it transparently.
Error in <ACLiC>: Executing '/.../v6-24-00-patches-cxx17/bin/rootcling -v0 "--lib-list-prefix=/tmp/AutoDict_unique_ptr_TH1F__cxx_ACLiC_map" -f "/tmp/AutoDict_unique_ptr_TH1F__cxx_ACLiC_dict.cxx" -I$ROOTSYS/include -I"/.../v6-24-00-patches-cxx17/etc/" -I"/.../v6-24-00-patches-cxx17/etc//cling" -I"/.../v6-24-00-patches-cxx17/include/" -I"/.../v6-24-00-patches-cxx17/include" -D__ACLIC__  "/tmp/AutoDict_unique_ptr_TH1F_.cxx" "/tmp/AutoDict_unique_ptr_TH1F__cxx_ACLiC_linkdef.h"' failed!
Attaching file file05.root as _file0...
Warning in <TStreamerInfo::Build>: unique_ptr<TH1F,default_delete<TH1F> >: __uniq_ptr_impl<TH1F,default_delete<TH1F> > has no streamer or dictionary, data member "_M_t" will not be saved
Warning in <TClass::Init>: no dictionary for class __compressed_pair<TH1F*,default_delete<TH1F> > is available
Warning in <TClass::Init>: no dictionary for class __compressed_pair_elem<TH1F*,0,false> is available
Warning in <TClass::Init>: no dictionary for class __compressed_pair_elem<default_delete<TH1F>,1,true> is available
(TFile *) 0x55f5d0927720

But I am able to see the histogram from TBrowser. I am getting this error when I run histo2.cpp

Thank you for your help! Unfortunately, It didn’t work.

is the problem. As you showed above, h_mcvtx_muon is of type unique_ptr<TH1F>. So you’ll have to do something along these lines:

std::unique_ptr<TH1F> *h1Ptr = nullptr;
f1->GetObject("h_mcvtx_muon", h1Ptr);

Does that work better? It’s fairly awkward to use due to the “double pointerness”.

In general it’s (very!) rare to see people writing unique_ptr to a TFile; it’s more common to write the pointee directly. But the StreamerInfo warnings are still a bug. I have created Unnecessary (?) warnings reading `unique_ptr` · Issue #9188 · root-project/root · GitHub for that.

Thank you for your help!

It worked when I commented out “->Draw” part. How can I draw the histogram? Because it gave this error when I include ht1Ptr->Draw("hist"): error: no member named ‘Draw’ in 'std::__1::unique_ptr<TH1F, std::__1::default_delete >'

Try: (*h1Ptr)->Draw("hist");

It worked, thank you!

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