Histogram array projection


I apologize for my trivial question, and maybe it’s more related to C++, but I was hoping someone could help me,
I have a root file where I have stored several TH2D histograms. I have successfully extracted 15 of them as TH1D. I need to make a loop where I can call them individually by using the iterator and then project all 15 of them onto the X-axis and draw them. Can you please give me some advice/example on how to do this? I have tried this:

for(Int_t i = 0; i <= 15; i++)
TString name = Form(“Temperature_%d" , i);
TH1D *hpx = Temperature _%d->ProjectionX(“hpx");
But it doesn’t work, and I assume it is totally wrong. Any help would be deeply appreciated. Thanks!

TFile *f = ... ;
TH2D *h2 = (TH2D*)f->Get(Form( ... ));
TH1D *h = h2->ProjectionX( ... );
1 Like

Be careful, this is 16, not 15!

Thank you for your helpful answer. It worked out. But when I try to draw all 15 projections on the same canvas, root only draws the first projection or the code crashes. Do you maybe have some advice on how to fix this? Thanks.

You’re right. Thank you for noticing.