Plotting two histograms from two different files/directories in one canvas

Hello Everyone,

I have a question on using TCanvas.
I have two histograms that I created in two separate directories from two different files.
More specifically, I have a file called hist1.c that I run in a directory called signal (that contains the data for my signal run that I am analyzing), and I also have another file called hist2.c that I run in a directory called background. hist1.c is my signal histogram and hist2.c is my background histogram. Is there a way to plot my signal histogram,hist1.c, against my background histogram,hist2.c. In other words, how can I plot hist1.c in the same canvas as hist2.c?

Thank you.

__
Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


I also wanted to show what I have attempted so far and the consistent error that I get. (I managed to get all my files in the same directory.)
First, I tried to follow this question that was successfully answered:
How do I get an object from a file?
When I wrote this:
selecthist.c (929 Bytes)

I get the error that:

#18 0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_8:1:
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selecthist.c:40:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
   df_zzz_backg->DrawClone();

But, I thought I followed the answer in the link, and I do have df_zzz_backg in my hist2.c file so I am not sure why I get this error. Any idea on how to fix this please?

Try (see if histograms exist):
rootls -l hist_xx_true1.root hist_xx_sig1.root

I type:

root -l hist_xx_true1.root
then root -1 hist_xx_sig1.root

I get:

root [0]
Attaching file hist_xx_true1.root as _file0...
(TFile *) 0x7fffbb7f9f50

then .q ( separately),
and get:

Attaching file hist_xx_sig1.root as _file0...
(TFile *) 0x7fffdb972df0

You can try: gFile->ls();

Ok, this is what I tried and this is my result (the error message is similar to what I see above the null pointer error:

$ root
   ------------------------------------------------------------
  | Welcome to ROOT 6.18/04                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for linuxx8664gcc on Sep 13 2019, 20:06:00           |
  | From tag , 11 September 2019                               |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] gFile->ls();
#0  0x00007f2754c34687 in __GI___waitpid (pid=4051, stat_loc=stat_loc
entry=0x7fffd88c7878, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f2754b9f067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f2755864823 in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x7fffd0065b80) at /home/cucip/root-6.18.04/core/unix/src/TUnixSystem.cxx:2106
#3  TUnixSystem::StackTrace (this=0x7fffd0065b80) at /home/cucip/root-6.18.04/core/unix/src/TUnixSystem.cxx:2400
#4  0x00007f274fff8d55 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/cucip/builddir/lib/libCling.so
#5  0x00007f274fff875b in cling_runtime_internal_throwIfInvalidPointer () from /home/cucip/builddir/lib/libCling.so
#6  0x00007f27560910b4 in ?? ()
#7  0x00007fffd09e3cb0 in ?? ()
#8  0x00007fffd00c7480 in ?? ()
#9  0x00007fffd0a1dc38 in ?? ()
#10 0x00007f274fff8710 in ?? () from /home/cucip/builddir/lib/libCling.so
#11 0x00007fffd011c440 in ?? ()
#12 0x0000000056091000 in ?? ()
#13 0x00007f275008eab0 in ?? () from /home/cucip/builddir/lib/libCling.so
#14 0x00007fffd88ca170 in ?? ()
#15 0x00007f2756091000 in ?? ()
#16 0x00007f27500680a0 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const () from /home/cucip/builddir/lib/libCling.so
#17 0x00007f274fffae77 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/cucip/builddir/lib/libCling.so
#18 0x00007f274fffc4df 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/cucip/builddir/lib/libCling.so
#19 0x00007f274fffc787 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) () from /home/cucip/builddir/lib/libCling.so
#20 0x00007f27500bf72d in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/cucip/builddir/lib/libCling.so
#21 0x00007f274ff7b68e in HandleInterpreterException (metaProcessor=0x7fffd06fa380, input_line=<optimized out>, compRes=
0x7fffd88ca15c: cling::Interpreter::kSuccess, result=result
entry=0x7fffd88ca170) at /home/cucip/root-6.18.04/core/metacling/src/TCling.cxx:2152
#22 0x00007f274ff8ff8d in TCling::ProcessLine (this=0x7fffd00c6c90, line=<optimized out>, error=0x7fffd88ca65c) at /home/cucip/root-6.18.04/core/metacling/src/TCling.cxx:2310
#23 0x00007f2755711ca2 in TApplication::ProcessLine (this=0x7fffd00b2900, line=<optimized out>, sync=<optimized out>, err=0x7fffd88ca65c) at /home/cucip/root-6.18.04/core/base/src/TApplication.cxx:1026
#24 0x00007f2755be1962 in TRint::ProcessLineNr (this=this
entry=0x7fffd00b2900, filestem=filestem
entry=0x7f2755bf39cd "ROOT_prompt_", line=0x7fffd88ca719 "gFile->ls();", error=0x7fffd88ca65c, error
entry=0x0) at /home/cucip/root-6.18.04/core/rint/src/TRint.cxx:746
#25 0x00007f2755be1cd4 in TRint::HandleTermInput (this=0x7fffd00b2900) at /home/cucip/root-6.18.04/core/rint/src/TRint.cxx:607
#26 0x00007f2755866600 in TUnixSystem::CheckDescriptors (this=this
entry=0x7fffd0065b80) at /home/cucip/root-6.18.04/core/unix/src/TUnixSystem.cxx:1309
#27 0x00007f2755867f98 in TUnixSystem::DispatchOneEvent (this=0x7fffd0065b80, pendingOnly=<optimized out>) at /home/cucip/root-6.18.04/core/unix/src/TUnixSystem.cxx:1064
#28 0x00007f275577cd81 in TSystem::InnerLoop (this=0x7fffd0065b80) at /home/cucip/root-6.18.04/core/base/src/TSystem.cxx:412
#29 TSystem::Run (this=0x7fffd0065b80) at /home/cucip/root-6.18.04/core/base/src/TSystem.cxx:362
#30 0x00007f275570fc7f in TApplication::Run (this=this
entry=0x7fffd00b2900, retrn=retrn
entry=false) at /home/cucip/root-6.18.04/core/base/src/TApplication.cxx:1178
#31 0x00007f2755be32d1 in TRint::Run (this=0x7fffd00b2900, retrn=<optimized out>) at /home/cucip/root-6.18.04/core/rint/src/TRint.cxx:460
#32 0x00007f2756200a0c in main (argc=<optimized out>, argv=0x7fffd88ccaa8) at /home/cucip/root-6.18.04/main/src/rmain.cxx:30
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
ROOT_prompt_0:1:1: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
gFile->ls();
^~~~~
/home/cucip/builddir/include/TFile.h:335:15: note: expanded from macro 'gFile'
#define gFile (TFile::CurrentFile())
              ^~~~~~~~~~~~~~~~~~~~~~
root [1]

If I type .ls in the root[1] terminal , I get nothing?
What does it mean? Is there a way to fix this?

root hist_xx_true1.root
root [0] _file0->ls()

But, how can I plot the two histograms into one canvas?

No matter how I run the code, I still get the error:

warning: null passed to a callee that requires a non-null argument [-Wnonnull]
df_zzz_backg->DrawCopy();

even though I can see my root files.

You haven’t executed the command line which I gave you.

I did and I get:

$ rootls -l hist_xx_true1.root hist_xx_sig1.root
hist_xx_sig1.root :
hist_xx_true1.root :

I also tried:

root -l hist_xx_true1.root hist_xx_sig1.root selecthist.c

but i get the same error about a null pointer as shown above

The “rootls” output clearly shows that both your files are completely empty.

I guess you need:

TFile *hfile1 = TFile::Open("hist_xx_sig1.root", "RECREATE");
df_xx_sig_4mu->Write();
delete hfile1;

@couet
could you give a hand?

Can you post your data files ?
Did I missed them ?

This cannot work… Where is df_zzz_backg defined?

Same here:

You are using a variable (df_xx_sig_4mu) before declaring it. This cannot work. Just look at the error messages.

I understand what the error is saying. However, I don’t understand why it is there if df_zzz_backg and df_xx_sig_4mu are defined inside my two .c files (which work fine and which I then wrote as a .root file). I look inside my directory and my two root files are filled, but when I write rootls as suggested by @Wile_E_Coyote, it shows that my root files are empty. So I am still not entirely sure where the problem is coming from. How can I define df_zzz_backg and df_xx_sig_4mu and have the .root files not “empty” so that my df_zzz_backg and df_xx_sig_4mu could be defined/read? Also, is there a certain way that I need to run my code so that both files can be opened/read at the same time?
@couet Which files do you need my two root files or .c files. My code to plot the two histograms is above in the forum?

You need to fix the macros which produce your ROOT files (e.g. AS SHOWN IN MY PREVIOUS POST) so that these files do not come out empty.

Ok, I understand now. I was writing your suggestion in the wrong place. Thank you very much!

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