Histogram Subtraction

Pls how can I subtract a histogram in one file from another histogram in a different file. Also how to have two or more histogram from different file on one histogram

TFile  *f1 = new TFile( "f1.root");
TH1F *h1 = (TH1F*)f1->Get("h1");
TFile  *f2 = new TFile( "f2.root");
TH1F *h2 = (TH1F*)f2->Get("h2");
h1->Add(h2,-1);

Thanks Couet,
I have 8 histogram in each file. for example R37.root has 8 histogram name energy_cal_L1 to energy_cal_L8 and R40.root has 8 histogram name energy_cal_L1 to energy_cal_L8.
Are you saying I shld do something like this

TFile *R37 = new TFile("R37.root");
TH1F *energy_cal_L1 = (TH1F*)R37->Get("energy_cal_L1);
TFile *R40 = new TFile("R40.root");
TH1F *energy_cal_L1 = (TH1F*)R40->Get("energy_cal_L1);
energy_cal_L1->Add(energy_cal_L1,-1);

How will I know whether it is subtracting energy_cal_L1 from R37 from R40 or vice versa

Define different variables for energy_cal_L1 coming from R37 and energy_cal_L1 coming from R40

Ok thanks, I will try that

TFile *R37 = new TFile("R37.root");
TH1F *energy_cal_L1_R37 = (TH1F*)R37->Get("energy_cal_L1");
TFile *R40 = new TFile("R40.root");
TH1F *energy_cal_L1_R40 = (TH1F*)R40->Get("energy_cal_L1");
energy_cal_L1_R37->Add(energy_cal_L1_R40,-1);

Bellow is the error I received.

root [0] new TBrowser
(TBrowser *) 0x12ff760
root [1] TFile *R40 = new TFile("R40.root");
Error in <TFile::TFile>: file R40.root does not exist
root [2] TH1F *energy_cal_L1_R40 = (TH1F*)R40->Get("energy_cal_L1");
root [3] TFile *R37 = new TFile("R37.root");
Error in <TFile::TFile>: file R37.root does not exist
root [4] TH1F *energy_cal_L1_R37 = (TH1F*)R37->Get("energy_cal_L1");
root [5] energy_cal_L1_R37->Add(energy_cal_L1_R40,-1)
#0  0x00007f89078ad4ca in __GI___waitpid (pid=10619, stat_loc=stat_loc
entry=0x7ffd9e931bf0, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f8907826fbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f8908417064 in TUnixSystem::StackTrace() () from /home/munirat/root/lib/libCore.so
#3  0x00007f89041f5235 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/munirat/root/lib/libCling.so
#4  0x00007f89041f4bd8 in cling_runtime_internal_throwIfInvalidPointer () from /home/munirat/root/lib/libCling.so
#5  0x00007f8908b6505d in ?? ()
#6  0x0000000000991ab0 in ?? ()
#7  0x5a44a40bce70e100 in ?? ()
#8  0x00007ffd9e934068 in ?? ()
#9  0x00007ffd9e934370 in ?? ()
#10 0xbff0000000000000 in ?? ()
#11 0x000000000098aa70 in ?? ()
#12 0x00007ffd9e934370 in ?? ()
#13 0x00007ffd9e934370 in ?? ()
#14 0x00007ffd9e934020 in ?? ()
#15 0x00007f89041f67cf in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/munirat/root/lib/libCling.so
#16 0x00007f89041f822d 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/munirat/root/lib/libCling.so
#17 0x00007f89041f84e0 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**) () from /home/munirat/root/lib/libCling.so
#18 0x00007f890428171e in cling::MetaProcessor::process(char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/munirat/root/lib/libCling.so
#19 0x00007f8904188486 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/munirat/root/lib/libCling.so
#20 0x00007f890419946a in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/munirat/root/lib/libCling.so
#21 0x00007f890834a3d6 in TApplication::ProcessLine(char const*, bool, int*) () from /home/munirat/root/lib/libCore.so
#22 0x00007f89087815ee in TRint::ProcessLineNr(char const*, char const*, int*) () from /home/munirat/root/lib/libRint.so
#23 0x00007f8908781975 in TRint::HandleTermInput() () from /home/munirat/root/lib/libRint.so
#24 0x00007f8908418bbc in TUnixSystem::CheckDescriptors() () from /home/munirat/root/lib/libCore.so
#25 0x00007f8908419ffa in TUnixSystem::DispatchOneEvent(bool) () from /home/munirat/root/lib/libCore.so
#26 0x00007f89083201c4 in TSystem::InnerLoop() () from /home/munirat/root/lib/libCore.so
#27 0x00007f890831ecff in TSystem::Run() () from /home/munirat/root/lib/libCore.so
#28 0x00007f890834790f in TApplication::Run(bool) () from /home/munirat/root/lib/libCore.so
#29 0x00007f8908783057 in TRint::Run(bool) () from /home/munirat/root/lib/libRint.so
#30 0x00000000004010dc in main ()
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_5:1:1: warning: null passed to a callee that requires a non-null
      argument [-Wnonnull]
energy_cal_L1_R37->Add(energy_cal_L1_R40,-1)
^~~~~~~~~~~~~~~~~
root [1] TFile *R40 = new TFile("R40.root");
Error in <TFile::TFile>: file R40.root does not exist

At least in this example you got the filename wrong (and/or the location thereof), then you try to retrieve information from those files … resulting in nullptr dereferencing …

Cheers,
Philippe.

Thanks for your response. that is the filename the histogram is in but cant figure out the problem

Very likely the file R40.root is not in the directory where you started ROOT. Check first where this file is exactly and access it with its full path name if you run ROOT from an other directory or by clicking on some icon.

OK thanks. Checking that

Thanks couet. I did check the full path name and use and below is the error I get.

root [3] TH1F *energy_cal_L1_0x33a3e30 =(TH1F*)0x33a3e30->Get("energy_cal_L1");
root [4] TH1F *energy_cal_L1_0x371eee0 =(TH1F*)0x371eee0->Get("energy_cal_L1");
root [5] energy_cal_L1_0x33a3e30->Add(energy_cal_L1_0x371eee0,-1);
ene#0  0x00007f15bde844ca in __GI___waitpid (pid=3736, stat_loc=stat_loc
entry=0x7ffd09522b40, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007f15bddfdfbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007f15be9ee064 in TUnixSystem::StackTrace() () from /home/munirat/root/lib/libCore.so
#3  0x00007f15ba7cc235 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/munirat/root/lib/libCling.so
#4  0x00007f15ba7cbbd8 in cling_runtime_internal_throwIfInvalidPointer () from /home/munirat/root/lib/libCling.so
#5  0x00007f15bf13805d in ?? ()
#6  0x00000000016f1ab0 in ?? ()
#7  0xe976f53d461f1c00 in ?? ()
#8  0x00007ffd09524fb8 in ?? ()
#9  0x00007ffd095252c0 in ?? ()
#10 0xbff0000000000000 in ?? ()
#11 0x00000000016eaa70 in ?? ()
#12 0x00007ffd095252c0 in ?? ()
#13 0x00007ffd095252c0 in ?? ()
#14 0x00007ffd09524f70 in ?? ()
#15 0x00007f15ba7cd7cf in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/munirat/root/lib/libCling.so
#16 0x00007f15ba7cf22d 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/munirat/root/lib/libCling.so
#17 0x00007f15ba7cf4e0 in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**) () from /home/munirat/root/lib/libCling.so
#18 0x00007f15ba85871e in cling::MetaProcessor::process(char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/munirat/root/lib/libCling.so
#19 0x00007f15ba75f486 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/munirat/root/lib/libCling.so
#20 0x00007f15ba77046a in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/munirat/root/lib/libCling.so
#21 0x00007f15be9213d6 in TApplication::ProcessLine(char const*, bool, int*) () from /home/munirat/root/lib/libCore.so
#22 0x00007f15bed585ee in TRint::ProcessLineNr(char const*, char const*, int*) () from /home/munirat/root/lib/libRint.so
#23 0x00007f15bed58975 in TRint::HandleTermInput() () from /home/munirat/root/lib/libRint.so
#24 0x00007f15be9efbbc in TUnixSystem::CheckDescriptors() () from /home/munirat/root/lib/libCore.so
#25 0x00007f15be9f0ffa in TUnixSystem::DispatchOneEvent(bool) () from /home/munirat/root/lib/libCore.so
#26 0x00007f15be8f71c4 in TSystem::InnerLoop() () from /home/munirat/root/lib/libCore.so
#27 0x00007f15be8f5cff in TSystem::Run() () from /home/munirat/root/lib/libCore.so
#28 0x00007f15be91e90f in TApplication::Run(bool) () from /home/munirat/root/lib/libCore.so
#29 0x00007f15bed5a057 in TRint::Run(bool) () from /home/munirat/root/lib/libRint.so
#30 0x00000000004010dc in main ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
root [3] TH1F *energy_cal_L1_0x33a3e30 =(TH1F*)0x33a3e30->Get("energy_cal_L1");
root [4] TH1F *energy_cal_L1_0x371eee0 =(TH1F*)0x371eee0->Get("energy_cal_L1");

Why use the bare value of the pointers?
Try

TFile *R40 = new TFile( fullpathname_to_R40 );
if (!R40 || R40->IsZombie) Fatal("script","R40.root not found");
TFile *R37 = new TFile( fullpathname_to_R37 );
if (!R37 || R37->IsZombie) Fatal("script","R37.root not found");
TH1F *energy_cal_L1_R40 = nullptr;
R40->GetObject("energy_cal_L1",energy_cal_L1_R40);
if (energy_cal_L1_R40 == nullptr) Fatal("script","energy_cal_L1_R40 does not exist or is not of the right type);
TH1F *energy_cal_L1_R37 = nullptr;
R37->GetObject("energy_cal_L1",energy_cal_L1_R37);
if (energy_cal_L1_R37 == nullptr) Fatal("script","energy_cal_L1_R37 does not exist or is not of the right type);

You may also want to look at the result of

R40->ls();

Cheers,
Philippe.

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