Cannot retrieve histogram from ROOT file

Hello,

Can someone help me to spot the mistake in a way how I retrieve the histogram?

I do following

TFile *f = TFile::Open("myFile.root");
TH1F *matched_plus = (TH1F*)f->Get("ChargeStudies/GenVisTau_pt; positive charge; matched");
std::cout << matched_plus->Integral() << std::endl;

And I get an error : warning: null passed to a callee that requires a non-null argument [-Wnonnull]

I thought that maybe there is a mistake in the directory itself, but when I do :
rootls myFile.root:"ChargeStudies/GenVisTau_pt; positive charge; matched" I get an output GenVisTau_pt; positive charge; matched which should mean that there is no issue with the naming.

Thanks in advance.

P.S. The histogram in the file is not empty and when I created it, I used RDataFrame and Histo1D method.

Most likely you meant:

TH1F *matched_plus = f->Get<TH1F*>("ChargeStudies/GenVisTau_pt");

If that still does not work, please send us the result of f->ls("")

I got this error:

error: cannot initialize a variable of type 'TH1F *' with an rvalue of type 'TH1F **'

And the output of f->ls() is following:

TFile**		myFile.root	
 TFile*		myFile.root	
  KEY: TDirectoryFile	ChargeStudies;1	ChargeStudies

There was a typo in my code, I meant:

TH1F *matched_plus = f->Get<TH1F>("ChargeStudies/GenVisTau_pt");

I still get exactly same error as described in the post :worried: :
warning: null passed to a callee that requires a non-null argument [-Wnonnull]

More detailed error:

#0  0x00007fee06f31a5a in wait4 () from /lib64/libc.so.6
#1  0x00007fee06eaf0bb in do_system () from /lib64/libc.so.6
#2  0x00007fee075891b4 in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.6.24
#3  0x00007fee017cc480 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /usr/lib64/root/libCling.so
#4  0x00007fee017bfcb9 in cling_runtime_internal_throwIfInvalidPointer () from /usr/lib64/root/libCling.so
#5  0x00007fee0726e1ba in ?? ()
#6  0x0000561000000007 in ?? ()
#7  0x00007fee017bfbf0 in ?? () from /usr/lib64/root/libCling.so
#8  0x00007fee017bfbf0 in ?? () from /usr/lib64/root/libCling.so
#9  0x00007fee017bfbf0 in ?? () from /usr/lib64/root/libCling.so
#10 0x00007fee05fb7870 in ?? () from /usr/lib64/root/libRIO.so
#11 0x0000000000000065 in ?? ()
#12 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.

Could you please attach myFile.root here?

The file is too big to be uploaded here, so I uploaded it to mega here .

Let me know if you have issues accessing it.

Thanks to this post (deleted now, so had to use the Wayback Machine), I think I figured out the solution. The following works for me:

        TFile *f = TFile::Open("myFile.root");
        TDirectoryFile* myDir = static_cast<TDirectoryFile*>(f->Get("ChargeStudies"));
        myDir->cd();
        TList *list = static_cast<TList*>(myDir->GetListOfKeys());
        TKey *key = static_cast<TKey*>(list->FindObject("GenVisTau_pt; positive charge; matched"));
        TObject *obj = static_cast<TKey*>(key)->ReadObj();
        TH1D *matched_plus = static_cast<TH1D*>(obj);
        std::cout << matched_plus->Integral() << std::endl;

1 Like

Wow, did not expect it to be so “complicated” in a way.
Thank you @yus ! You made my day :slight_smile:

No problem at all. Please try to avoid having spaces (and maybe semicolons, too) in the names of the histograms in the future :slight_smile: This is always a pain in the neck.

1 Like
TFile *f = TFile::Open("myFile.root");
TDirectoryFile* myDir = f->GetDirectory("ChargeStudies");
TH1D *matched_plus = myDir->Get<TH1D>("GenVisTau_pt; positive charge; matched");
std::cout << matched_plus->Integral() << std::endl;

might also work.

With testing:

TFile *f = TFile::Open("myFile.root");
TDirectoryFile* myDir = f->GetDirectory("ChargeStudies");
if (!myDir) {
    cerr << "Could not find ChargeStudies directory\n";
    f->ls();
    return;
}
TH1D *matched_plus = myDir->Get<TH1D>("GenVisTau_pt; positive charge; matched");
if (!matched_plus) {
    cerr << "Could not find 'GenVisTau_pt; positive charge; matched' \n";
    myDir->ls();
    return;
}
std::cout << matched_plus->Integral() << std::endl;

Except that indeed the semi colon is a special characters :frowning: , so indeed you might need to go through the long way.

Can you please have a look at this script and tell me if it is correct? I am trying to divide two histograms and plot the result with error bars, but as I noticed it does not matter if I put “E” option in the Draw because it anyway gives it with error bars and somehow when I add other options, like " L " it does not do anything…

TFile *f = TFile::Open("myFile.root");
	
    TDirectoryFile* myDir = static_cast<TDirectoryFile*>(f->Get("ChargeStudies"));
    myDir->cd();
    TList *list = static_cast<TList*>(myDir->GetListOfKeys());
   
    TKey *key1 = static_cast<TKey*>(list->FindObject("GenVisTau_pt; negative charge; matched"));
    TKey *key2 = static_cast<TKey*>(list->FindObject("GenVisTau_pt; negative charge; failed"));
    
    TObject *obj1 = static_cast<TKey*>(key1)->ReadObj();
    TObject *obj2 = static_cast<TKey*>(key2)->ReadObj();

    TH1D *matched_minus = static_cast<TH1D*>(obj3);
    TH1D *failed_minus = static_cast<TH1D*>(obj4);
    
    
    matched_minus->Rebin(50);
    failed_minus->Rebin(50); 
    
    matched_minus->Sumw2();
    failed_minus->Sumw2();

  
    TH1F * misId_prob_minus = (TH1F*)failed_minus->Clone("misId_prob_minus");
    misId_prob_minus->Divide(matched_minus);
    
    misId_prob_minus->SetLineColor(kRed);
    misId_prob_minus->SetMarkerStyle(21);
    misId_prob_minus->SetMarkerColor(kRed+2);
    misId_prob_minus->Draw("epl");

Thank you for looking into this. Unfortunately, like this it still does not work, it has this error :
error: cannot initialize a variable of type 'TDirectoryFile *' with an rvalue of type 'TDirectory *'
and if I change TDirectoryFile to TDirectory then the original error is there.
Solution suggested by @yus works well, for future I just will not use ; in the hist name anymore…

Try:

misId_prob_minus->Draw(""); // "" or "HIST P" (or "HIST")
misId_prob_minus->Draw("HIST L SAME"); // "HIST L SAME" (or "HIST SAME")