# Problems with TH2D plotting

**URL:** <https://root-forum.cern.ch/t/problems-with-th2d-plotting/45976>\
**Category:** Newbie\
**Created:** [July 22, 2021, 3:37pm UTC](https://root-forum.cern.ch/t/problems-with-th2d-plotting/45976 "2021-07-22T15:37:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrramos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rrramos](https://root-forum.cern.ch/u/rrramos)\
**Post date:** [July 22, 2021, 3:37pm UTC](https://root-forum.cern.ch/t/problems-with-th2d-plotting/45976/1 "2021-07-22T15:37:10Z")

</div>

Hello everyone!!  
I need some help with TH2D plotting.

I have a TH2D histogram constructed from 2 branches (X and Y) of a TTree. What I want to do now is create a new TH2D but using the mean of Y in intervals of X. So, I want a new TH2D but now with the mean of Y vs X .Can I do it? If yes, how?

I tried to do this using the ProjectionY and calculate the mean but this is not working very well =/

this is what I tried to do

```auto
  TH2D *h2d_topo = new TH2D("h2d_topo","", 100,0,10, 1000,0,1000); 

  for(Long_t i=0; i<t3->GetEntries(); i++){
    t3->GetEntry(i);
    h2d_topo->Fill(XiPtTopo, fabs(DCATopo));
    }

  vector<TH1D *> hTopo(100);
  vector<Double_t> mean_topo(100);

  for(Int_t ii = 0; ii<100; ii++){
    hTopo[ii] = h2d_topo->ProjectionY(Form("h_%d",ii), ii, ii+1);
    mean_topo[ii] = hTopo[ii]->GetMean();
   // cout<<"media ii = " << mean_topo[ii] << endl;
  }

  TH2D *htest = new TH2D("htest", "", 100,0,10, 10, 0, 1000);

  for(Int_t ii = 0; ii<100; ii++){
    for(Long_t i=0; i<t3->GetEntries(); i++){
        htest->Fill(XiPtTopo, mean_topo[ii]);
    }  

```

Can someone help me?? I would be very grateful!!  
Thank you!

---

<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:** [July 22, 2021, 5:58pm UTC](https://root-forum.cern.ch/t/problems-with-th2d-plotting/45976/2 "2021-07-22T17:58:15Z")

</div>

How about: [TProfile](https://root.cern/doc/master/classTProfile.html)

---

<div class="post-metadata">

**Author:** ![rrramos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rrramos](https://root-forum.cern.ch/u/rrramos)\
**Post date:** [July 27, 2021, 12:10pm UTC](https://root-forum.cern.ch/t/problems-with-th2d-plotting/45976/3 "2021-07-27T12:10:22Z")

</div>

This solves my problem! Thank you very much!!!
