# Cannot retrieve histogram from ROOT file

**URL:** <https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566>\
**Category:** Newbie\
**Created:** [September 14, 2022, 11:56am UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566 "2022-09-14T11:56:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 11:56am UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/1 "2022-09-14T11:56:37Z")

</div>

Hello,

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

I do following

```auto
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.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 14, 2022, 12:18pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/2 "2022-09-14T12:18:55Z")

</div>

Most likely you meant:

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

```

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

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 12:26pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/3 "2022-09-14T12:26:54Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 14, 2022, 12:53pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/4 "2022-09-14T12:53:25Z")

</div>

There was a typo in my code, I meant:

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

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 1:26pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/5 "2022-09-14T13:26:44Z")

</div>

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

More detailed error:

```auto
#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.

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 14, 2022, 1:46pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/6 "2022-09-14T13:46:12Z")

</div>

Could you please attach `myFile.root` here?

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 1:53pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/7 "2022-09-14T13:53:22Z")

</div>

The file is too big to be uploaded here, so I uploaded it to mega [here](https://mega.nz/file/vj5iGDIZ#Mc7gxxHRtI9h_IEDT4NextyC0umimi1V0oRyUXpWU9c) .

Let me know if you have issues accessing it.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 14, 2022, 3:02pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/8 "2022-09-14T15:02:22Z")

</div>

Thanks to [this post](https://web.archive.org/web/20171120163834/https://root.cern.ch/how/how-draw-object-special-characters-its-name) (deleted now, so had to use the Wayback Machine), I think I figured out the solution. The following works for me:

```c++
        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;

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 3:22pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/9 "2022-09-14T15:22:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 14, 2022, 3:24pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/10 "2022-09-14T15:24:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 14, 2022, 4:05pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/11 "2022-09-14T16:05:34Z")

</div>

```auto
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:

```auto
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 ☹ , so indeed you might need to go through the long way.

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 5:05pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/12 "2022-09-14T17:05:32Z")

</div>

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…

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![beg1nner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@beg1nner](https://root-forum.cern.ch/u/beg1nner)\
**Post date:** [September 14, 2022, 5:09pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/13 "2022-09-14T17:09:22Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 14, 2022, 5:14pm UTC](https://root-forum.cern.ch/t/cannot-retrieve-histogram-from-root-file/51566/14 "2022-09-14T17:14:19Z")

</div>

Try:

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

```
