Plotting scatter plot from different tree in the same canvas

I’m trying to plot two scatter plot from two trees in one canvas, insipired by this tutorial ROOT: tutorials/tree/cernstaff.C File Reference, my code is:

TTree *TreeArr[NPlots];
    //Load from flashmatchAnaArOnly the tree FlashMatchTree
  fp->cd(FolderNames[0].c_str());
  gDirectory->GetObject(TreeNames[0].c_str(), TreeArr[0]);
    //Load from flashmatchAna10ppm the tree FlashMatchTree
  fp->cd(FolderNames[2].c_str());
  gDirectory->GetObject(TreeNames[0].c_str(), TreeArr[1]);
  THStack *plots = new THStack("plots", "plots");

  TH1 *first = new TH1F("first","first", 10000, -600.0, 600.0);
  TreeArr[0] -> Draw("SumPE:TrueX>>first");
  plots->Add(first);

  TH1 *second = new TH1F("second","second", 10000, -600.0, 600.0);
  TreeArr[1] -> Draw("SumPE:TrueX>>second");
  plots->Add(second);

  plots->Draw();

Which gives the error:



Warning in <TSelectorDraw::Begin>: Deleting old histogram with different dimensions

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f8d923e583a in __GI___wait4 (pid=26847, stat_loc=stat_loc
entry=0x7fffff276da8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1  0x00007f8d923e57fb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7fffff276da8, options=options
entry=0) at waitpid.c:38
#2  0x00007f8d923576ab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007f8d92a461ce in TUnixSystem::StackTrace() () from /home/username/root/lib/libCore.so.6.22
#4  0x00007f8d92a43055 in TUnixSystem::DispatchSignals(ESignals) () from /home/username/root/lib/libCore.so.6.22
#5  <signal handler called>
#6  0x00007f8d809fbd84 in THStack::Add(TH1*, char const*) () from /home/username/root/lib/libHist.so.6.22.00
#7  0x00007f8d7ea6ccf7 in ?? ()
#8  0x0000000000000002 in ?? ()
#9  0xfde9f6a073839000 in ?? ()
#10 0x000055a7b2bbeba8 in ?? ()
#11 0x00007f8d9239be1b in _int_free (av=0x7fffff279c40, p=0x0, have_lock=<optimized out>) at malloc.c:4575
#12 0x000055a7bd73ea00 in ?? ()
#13 0x00007fffff279a80 in ?? ()
#14 0x00007fffff279be0 in ?? ()
#15 0x000055a7bd5aaae0 in ?? ()
#16 0x0000000000000026 in ?? ()
#17 0x000055a7bd73b7d0 in ?? ()
#18 0x00007f8d8ecc53ba in std::_Function_handler<llvm::Expected<unsigned long> (), llvm::orc::RTDyldObjectLinkingLayer::ConcreteLinkedObject<std::shared_ptr<llvm::RuntimeDyld::MemoryManager>, std::shared_ptr<llvm::JITSymbolResolver>, llvm::orc::RTDyldObjectLinkingLayer::addObject(std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> >, std::shared_ptr<llvm::JITSymbolResolver>)::{lambda(std::_List_iterator<std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > >, llvm::RuntimeDyld&, std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> > const&, std::function<void ()>)#1}>::getSymbolMaterializer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /home/username/root/lib/libCling.so
#19 0x000055a7bd5970d0 in ?? ()
#20 0x00007f8d8ec499a0 in ?? () from /home/username/root/lib/libCling.so
#21 0x000055a7b29de2f0 in ?? ()
#22 0x000055a7bd73c810 in ?? ()
#23 0x000055a7bd736460 in ?? ()
#24 0x000055a7bbc3f8c0 in ?? ()
#25 0x000055a7bd700558 in ?? ()
#26 0x000055a7b29de2f0 in ?? ()
#27 0x000055a7bd5970d0 in ?? ()
#28 0x00007f8d8ec499a0 in ?? () from /home/username/root/lib/libCling.so
#29 0x00007f8d7ea6b26e in ?? ()
#30 0x00007f8d91df9380 in ?? () from /home/username/root/lib/libRIO.so
#31 0x000055a7bd5970d0 in ?? ()
#32 0x000055a7bd5970d0 in ?? ()
#33 0x000055a7bd5970d0 in ?? ()
#34 0x00007fffff279d50 in ?? ()
#35 0x00007f8d8ecee3a2 in cling::runtime::internal::setValueNoAlloc(void*, void*, void*, char) () from /home/username/root/lib/libCling.so
#36 0x00007f8d7ea6a06a in ?? ()
#37 0x000055a7b2aa7630 in ?? ()
#38 0x000000018ec4a0f0 in ?? ()
#39 0x00007f8d8ecee360 in ?? () from /home/username/root/lib/libCling.so
#40 0x00007fffff27a140 in ?? ()
#41 0x000055a7b308bba0 in ?? ()
#42 0x00007f8d8ecc3e14 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const () from /home/username/root/lib/libCling.so
#43 0x00007f8d8ec4bcc7 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/username/root/lib/libCling.so
#44 0x00007f8d8ec4d5cd in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/username/root/lib/libCling.so
#45 0x00007f8d8ec4d8e9 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/username/root/lib/libCling.so
#46 0x00007f8d8ed16a1d in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/username/root/lib/libCling.so
#47 0x00007f8d8eba6f0c in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/username/root/lib/libCling.so
#48 0x00007f8d8ebbda7b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/username/root/lib/libCling.so
#49 0x00007f8d928f303b in TApplication::ProcessLine(char const*, bool, int*) () from /home/username/root/lib/libCore.so.6.22
#50 0x00007f8d92c6c1fa in TRint::ProcessLineNr(char const*, char const*, int*) () from /home/username/root/lib/libRint.so.6.22
#51 0x00007f8d92c6c57a in TRint::HandleTermInput() () from /home/username/root/lib/libRint.so.6.22
#52 0x00007f8d92a42132 in TUnixSystem::CheckDescriptors() () from /home/username/root/lib/libCore.so.6.22
#53 0x00007f8d92a43df8 in TUnixSystem::DispatchOneEvent(bool) () from /home/username/root/lib/libCore.so.6.22
#54 0x00007f8d92957959 in TSystem::Run() () from /home/username/root/lib/libCore.so.6.22
#55 0x00007f8d928eff63 in TApplication::Run(bool) () from /home/username/root/lib/libCore.so.6.22
#56 0x00007f8d92c6dbaf in TRint::Run(bool) () from /home/username/root/lib/libRint.so.6.22
#57 0x000055a7b1a73180 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.
===========================================================
#6  0x00007f8d809fbd84 in THStack::Add(TH1*, char const*) () from /home/username/root/lib/libHist.so.6.22.00
#7  0x00007f8d7ea6ccf7 in ?? ()
#8  0x0000000000000002 in ?? ()
#9  0xfde9f6a073839000 in ?? ()
#10 0x000055a7b2bbeba8 in ?? ()
#11 0x00007f8d9239be1b in _int_free (av=0x7fffff279c40, p=0x0, have_lock=<optimized out>) at malloc.c:4575
#12 0x000055a7bd73ea00 in ?? ()
#13 0x00007fffff279a80 in ?? ()
#14 0x00007fffff279be0 in ?? ()
#15 0x000055a7bd5aaae0 in ?? ()
#16 0x0000000000000026 in ?? ()
#17 0x000055a7bd73b7d0 in ?? ()
#18 0x00007f8d8ecc53ba in std::_Function_handler<llvm::Expected<unsigned long> (), llvm::orc::RTDyldObjectLinkingLayer::ConcreteLinkedObject<std::shared_ptr<llvm::RuntimeDyld::MemoryManager>, std::shared_ptr<llvm::JITSymbolResolver>, llvm::orc::RTDyldObjectLinkingLayer::addObject(std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> >, std::shared_ptr<llvm::JITSymbolResolver>)::{lambda(std::_List_iterator<std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject, std::default_delete<llvm::orc::RTDyldObjectLinkingLayerBase::LinkedObject> > >, llvm::RuntimeDyld&, std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile> > const&, std::function<void ()>)#1}>::getSymbolMaterializer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /home/username/root/lib/libCling.so
#19 0x000055a7bd5970d0 in ?? ()
#20 0x00007f8d8ec499a0 in ?? () from /home/username/root/lib/libCling.so
#21 0x000055a7b29de2f0 in ?? ()
#22 0x000055a7bd73c810 in ?? ()
#23 0x000055a7bd736460 in ?? ()
#24 0x000055a7bbc3f8c0 in ?? ()
#25 0x000055a7bd700558 in ?? ()
#26 0x000055a7b29de2f0 in ?? ()
#27 0x000055a7bd5970d0 in ?? ()
#28 0x00007f8d8ec499a0 in ?? () from /home/username/root/lib/libCling.so
#29 0x00007f8d7ea6b26e in ?? ()
#30 0x00007f8d91df9380 in ?? () from /home/username/root/lib/libRIO.so
#31 0x000055a7bd5970d0 in ?? ()
#32 0x000055a7bd5970d0 in ?? ()
#33 0x000055a7bd5970d0 in ?? ()
#34 0x00007fffff279d50 in ?? ()
#35 0x00007f8d8ecee3a2 in cling::runtime::internal::setValueNoAlloc(void*, void*, void*, char) () from /home/username/root/lib/libCling.so
#36 0x00007f8d7ea6a06a in ?? ()
#37 0x000055a7b2aa7630 in ?? ()
#38 0x000000018ec4a0f0 in ?? ()
#39 0x00007f8d8ecee360 in ?? () from /home/username/root/lib/libCling.so
#40 0x00007fffff27a140 in ?? ()
#41 0x000055a7b308bba0 in ?? ()
#42 0x00007f8d8ecc3e14 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const () from /home/username/root/lib/libCling.so
#43 0x00007f8d8ec4bcc7 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/username/root/lib/libCling.so
#44 0x00007f8d8ec4d5cd in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) () from /home/username/root/lib/libCling.so
#45 0x00007f8d8ec4d8e9 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/username/root/lib/libCling.so
#46 0x00007f8d8ed16a1d in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/username/root/lib/libCling.so
#47 0x00007f8d8eba6f0c in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/username/root/lib/libCling.so
===========================================================

How do I do this?

that’s a 2-D histogram, not 1-D.

I changed them to a 2D graphs; I don’t get any error but it produces an empty canvas

  THStack *plots = new THStack("plots", "plots");

  printf("\n\n------------------------0----------------------- \n\n");

  auto *first = new TH2F("first","first", 10000, -600.0, 600.0, 10000, -600.0, 600.0);
  TreeArr[0] -> Draw("SumPE:TrueX>>first");
  plots->Add(first);

  printf("\n\n------------------------1----------------------- \n\n");

  auto *second = new TH2F("second","second", 10000, -600.0, 600.0, 10000, -600.0, 600.0);
  TreeArr[1] -> Draw("SumPE:TrueX>>second");
  plots->Add(second);

  printf("\n\n------------------------2----------------------- \n\n");

  plots->Draw();

What to you get when you do:

TreeArr[0] -> Draw("SumPE:TrueX>>first");

and

TreeArr[1] -> Draw("SumPE:TrueX>>second");

?

Once you have verified that the plots you get with these two commands are correct, the plot produced with the THStack should be fine also. At that point it will be better to use the option goff:

  THStack *plots = new THStack("plots", "plots");
  auto *first = new TH2F("first","first", 10000, -600.0, 600.0, 10000, -600.0, 600.0);
  TreeArr[0] -> Draw("SumPE:TrueX>>first", "","goff");
  plots->Add(first);
  auto *second = new TH2F("second","second", 10000, -600.0, 600.0, 10000, -600.0, 600.0);
  TreeArr[1] -> Draw("SumPE:TrueX>>second","","goff");
  plots->Add(second);
  plots->Draw();

According to the reference (ROOT: TTree Class Reference) I should get a TGraph, after modifying first and second as TGraph and plots as multigraph I still get an empty canvas.

The “>>” means “save it to a histogram”.

Right before creating the “THStack”, add:
gROOT->cd(); // newly created histograms should go here

1 Like