Problem in plotting several histograms together

Hi everybody,

I am a newbie here so please excuse me for the banality of my issue.
I have some histograms stored in 2 root files (here attached), and I wrote this very simple macro to plot them in the same canvas

{
   TFile f1("L21M500.root");
   f1.ls();
   f1.cd("d_L21M500;1"); 
   gDirectory->ls();
   TH1D *h1=(TH1D*) f1.Get("histo_PTe1");   
   
   TFile f2("tt_0PU.root");
   f2.ls();
   f2.cd("d_tt4p;1"); 
   gDirectory->ls();
   TH1D *h2=(TH1D*) f2.Get("PT_e1");
   
   h1->SetLineColor(kRed);
   h2->SetLineColor(kGreen);
   
   h1->Draw("l");
   h2->Draw("same");
   
   gPad->BuildLegend();
}

Anyway, when running the macro I got some errors:

[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling_runtime_internal_throwIfInvalidPointer (no debug info)
[<unknown binary>] (no debug info)
[<unknown binary>] (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalExecutor::runStaticInitializersOnce(cling::Transaction const&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::Interpreter::executeTransaction(cling::Transaction&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalParser::commitTransaction(llvm::PointerIntPair<cling::Transaction*, 2u, cling::IncrementalParser::EParseResult, llvm::PointerLikeTypeTraits<cling::Transaction*>, llvm::PointerIntPairInfo<cling::Transaction*, 2u, llvm::PointerLikeTypeTraits<cling::Transaction*> > >&, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::IncrementalParser::Compile(llvm::StringRef, cling::CompilationOptions const&) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/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)
[/usr/local/Cellar/root/6.10.08/lib/root/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)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/usr/local/Cellar/root/6.10.08/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/usr/local/Cellar/root/6.10.08/bin/root.exe] main (no debug info)
[/usr/lib/system/libdyld.dylib] start (no debug info)
libc++abi.dylib: terminating with uncaught exception of type cling::InvalidDerefException: Trying to dereference null pointer or trying to call routine taking non-null arguments

I add that even if I use ROOT 6.10/08 I got the same with other versions in lxplus.

Can anyone please help me to fix this problem?

Thanks a lot.
Best,

Elvin

tt_0PU.root (21.9 KB)
L21M500.root (18.2 KB)

Try:

// ...
TH1D *h1; gDirectory->GetObject("histo_PTe1", h1);
// ...
TH1D *h2; gDirectory->GetObject("PT_e1", h2);
// ...
1 Like

Thanks a lot!!
Cheers,
Elvin

Hi again,

what should I change in the particular case in which the root files have histograms with same names? (For example, if they are both called “histo_PTe1”)
I tried by adding

TH1D *d1= (TH1D*) h1->Clone();

but it seems not to be working.

Thanks,
Elvin

You should be able to retrieve both histograms from both files.

{
  gStyle->SetOptStat(0);
  
  TFile f1("L21M500.root");
  TH1D *h1; f1.GetObject("d_L21M500/histo_PTe1", h1);
  // h1->SetName("first_histo_PTe1");
  // h1->SetTitle("first PT_e1");
  h1->SetLineColor(kRed);
  
  TFile f2("L21M500.root");
  TH1D *h2; f2.GetObject("d_L21M500/histo_PTe1", h2);
  // h2->SetName("second_histo_PTe1");
  h2->SetTitle("second PT_e1");
  h2->SetLineColor(kGreen);
#if 1 /* 0 or 1 */
  h2->Scale(0.5);
#endif /* 0 or 1 */
  
  h1->Draw("l");
  h2->Draw("same");
  
  gPad->BuildLegend();
}

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