Loop over histograms, referring to file with %d

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.

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

Thanks for your help, but I get the same error :frowning:

Try: f->ls(); and see if “TH1D” histograms with such names (“L1_F0”, “L1_F1”, …) exist.

They exist… Because if I replace L1_F%d by L1_F10 or L1_F30 it works

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

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