# Histogram array projection

**URL:** <https://root-forum.cern.ch/t/histogram-array-projection/51975>\
**Category:** Newbie\
**Created:** [October 15, 2022, 5:09am UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975 "2022-10-15T05:09:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 15, 2022, 5:09am UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975/1 "2022-10-15T05:09:00Z")

</div>

Hello,

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!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 15, 2022, 10:01am UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975/2 "2022-10-15T10:01:34Z")

</div>

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

```

---

<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:** [October 15, 2022, 12:31pm UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975/3 "2022-10-15T12:31:48Z")

</div>

> [@alex14](#):
>
> ```c++
> for(Int_t i = 0; i <= 15; i++)
> 
> ```

Be careful, this is 16, not 15!

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 15, 2022, 2:50pm UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975/4 "2022-10-15T14:50:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [October 15, 2022, 2:50pm UTC](https://root-forum.cern.ch/t/histogram-array-projection/51975/5 "2022-10-15T14:50:24Z")

</div>

You’re right. Thank you for noticing.
