# Loop over histograms, referring to file with %d

**URL:** <https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [March 16, 2020, 10:32am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426 "2020-03-16T10:32:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 10:32am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/1 "2020-03-16T10:32:37Z")

</div>

ROOT Version: 6.18/04  
Platform: Linux

I am trying to loop over different histograms in a rootfile and draw these histograms on the same plot. I have tried:  
‘’’  
void Function();  
auto f = new TFile (“output.root”);  
std::vectorstd::string Cut = {“L1\_F10”, “L1\_F30”};  
int n = Cut.Size();  
TH1D _h[n];  
for (unsigned i = 0; i \< n; i++) {  
h[i] = (TH1D_)f-\>Get(“L1\_F%d”);  
if (i ==0) h[i]-\>Draw();  
else h[i]-\>Draw(“same”);  
‘’’

I get the error saying that “null passed to a callee that required a non-null argument [-Wnonnull]: h[i]-\>Draw()”

So something is wrong with how I refer to my file (i.e. the %d), which maybe has something to do with the fact that my vector contains strings, rather than integers, but I am not sure how to fix this.

---

<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:** [March 16, 2020, 10:35am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/2 "2020-03-16T10:35:16Z")

</div>

Instead of: `h[i] = (TH1D*)f->Get("L1_F%d");` try:  
`f->GetObject(TString::Format("L1_F%d", i), h[i]);`

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 10:40am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/3 "2020-03-16T10:40:53Z")

</div>

Thanks for your help, but I get the same error ☹

---

<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:** [March 16, 2020, 11:00am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/4 "2020-03-16T11:00:28Z")

</div>

Try: `f->ls();` and see if “TH1D” histograms with such names (“L1\_F0”, “L1\_F1”, …) exist.

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 11:39am UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/5 "2020-03-16T11:39:55Z")

</div>

They exist… Because if I replace L1\_F%d by L1\_F10 or L1\_F30 it works

---

<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:** [March 16, 2020, 12:00pm UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/6 "2020-03-16T12:00:03Z")

</div>

Try: `f->ls();`  
Right after “GetObject” add: `if (!h[i]) continue; // just a precaution`

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 16, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/loop-over-histograms-referring-to-file-with-d/38426/7 "2020-03-16T12:12:50Z")

</div>

I changed some things here and there and it works now! Thanks for you help!
