How i can take two histograms from root-file and make its in one picture?

Hello, dear All!

I have a problem:

How i can take two histograms from root-file and make its in one picture?

it is my code:

void itog_allGuass()
{
// Float_t PMT2;
Int_t PMT2;

    TFile* file1 = TFile::Open("351mm_12.root","READ");   
   	TTree* Hits = (TTree*)file1->Get("Hits");
    Hits -> SetBranchAddress("PMT2", &PMT2); 
    TH2I *h = (TH2I*)file1->Get("PMT2");   
    h->Draw();
}

but i have mistake:

andy@andy$ root itog_allGuass.C

root [0] 
Processing itog_allGuass.C...
#0  0x00007fbff3f8d337 in __GI___waitpid (pid=29249, stat_loc=stat_loc
entry=0x7fff6a545ba8, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007fbff3ef8047 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007fbff4ba6bff in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x56467eec8740) at /opt/root/root-6.09.02-source/core/unix/src/TUnixSystem.cxx:2119
#3  TUnixSystem::StackTrace (this=0x56467eec8740) at /opt/root/root-6.09.02-source/core/unix/src/TUnixSystem.cxx:2413
#4  0x00007fbfefd37ff5 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /opt/root/root-6.09.02-build/lib/libCling.so
#5  0x00007fbfefd37a88 in cling_runtime_internal_throwIfInvalidPointer () from /opt/root/root-6.09.02-build/lib/libCling.so
#6  0x00007fbff52f2316 in ?? ()
#7  0x00007fbf00000000 in ?? ()
#8  0x9d59dc20b9863700 in ?? ()
#9  0x0000000000000002 in ?? ()
#10 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:
/opt/geant4/Дима/gc/reasult/4/itog_allGuass.C:11:9: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
        h->Draw()
---------------------------------------------------------

Why my histogram is null?
I can open my root-file and see my histogram(PMT2) using TBrowser!
Help me!

Can you provide the content of your file, using file1->ls() like for example:

root [0] TFile *file1 = TFile::Open("hsimple.root")
(TFile *) 0xe789d80
root [1] file1->ls()
TFile**         hsimple.root    Demo ROOT file with histograms
 TFile*         hsimple.root    Demo ROOT file with histograms
  KEY: TH1F     hpx;1   This is the px distribution
  KEY: TH2F     hpxpy;1 py vs px
  KEY: TProfile hprof;1 Profile of pz versus px
  KEY: TNtuple  ntuple;1        Demo ntuple
root [2]

root [0] TFile file1 = TFile::Open(“351mm_12.root”)
(TFile ) 0x5624031a8a60
root [1] file1->ls()
TFile
351mm_12.root
TFile* 351mm_12.root
KEY: TTree Hits;1 Hits
KEY: TTree Scoring;1 Scoring
KEY: TTree rates;1 rates
KEY: TTree pherates;1 pherates
KEY: TTree Photoelectrons;1 Photoelectrons
KEY: TTree Photoelectrons_allPoints;1 Photoelectrons_allPoints

OK, so there is no TH2I in your file, so TH2I *h = (TH2I*)file1->Get("PMT2"); cannot work. You must create and fill an histogram from the Hits TTree. Probably something like:

void itog_allGuass()
{
    TFile *file1 = TFile::Open("351mm_12.root","READ");   
    TTree *Hits = (TTree *)file1->Get("Hits");
    if (Hits)
        Hits->Draw("PMT2");
}

And if that doesn’t work, please provide your 351mm_12.root file.

1 Like

And a better solution, using RDataFrame:

#include <TCanvas.h>
#include <ROOT/RDataFrame.hxx>

void itog_allGuass()
{
    TCanvas *c1 = new TCanvas("c1", "c1");
    ROOT::RDataFrame df{"Hits", "351mm_12.root"};
    auto hist1 = df.Histo1D("PMT1");
    hist1->DrawClone();
    /// draw another histogram in the same canvas
    auto hist2 = df.Histo1D("PMT2");
    hist2->DrawClone("SAME");
}
1 Like

It is all right - it is work!

thank you very much for your attention and answer!

1 Like

tell me, please:

error: no type named ‘RDataFrame’ in namespace ‘ROOT’

which library i can include in my code for multiply draw histogram?

Link against libROOTDataFrame and add

#include <ROOT/RDataFrame.hxx>

in your source code (I just modified the example above)

fatal error: ‘ROOT/RDataFrame.hxx’ file not found #include <ROOT/RDataFrame.hxx>

and where i can take this library?

How do you compile your code?

yes, my code in this multiply case.

i use this command: $ root itog_allGuass.C

P.S. My ROOT is 6.09/02

OK, so this is too old. With your version of ROOT, try this way:

void itog_allGuass()
{
    TCanvas *c1 = new TCanvas("c1", "c1");
    TFile *file1 = TFile::Open("351mm_12.root","READ");   
    TTree *Hits = (TTree *)file1->Get("Hits");
    if (Hits) {
        Hits->Draw("PMT1");
        Hits->Draw("PMT2", nullptr, "SAME");
    }
}

no, no, excuse me: i have two different files, in each of it are tree1 and PMT2, and these PMT2(from two files) i need draw on one canvas/picture.

this is my problem.

in ROOT 6.28/12 it is all right - work!

void itog_allGuass()
{
    TCanvas *c1 = new TCanvas("c1", "c1");
    TH1::AddDirectory(false);
    TFile *file1 = TFile::Open("351mm_12.root","READ");   
    TTree *Hits1 = (TTree *)file1->Get("Hits");
    if (Hits1) {
        Hits1->Draw("PMT1");
    }
    TFile *file2 = TFile::Open("351mm_13.root","READ");   
    TTree *Hits2 = (TTree *)file2->Get("Hits");
    if (Hits2) {
        Hits2->Draw("PMT1", nullptr, "SAME");
    }
}

It is all right!

Thank you VERY MUCH!

1 Like