# Get values from projection

**URL:** <https://root-forum.cern.ch/t/get-values-from-projection/51536>\
**Category:** Newbie\
**Created:** [September 11, 2022, 5:27pm UTC](https://root-forum.cern.ch/t/get-values-from-projection/51536 "2022-09-11T17:27:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ailema](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ailema/32/24027_2.png) [@Ailema](https://root-forum.cern.ch/u/Ailema)\
**Post date:** [September 11, 2022, 5:27pm UTC](https://root-forum.cern.ch/t/get-values-from-projection/51536/1 "2022-09-11T17:27:03Z")

</div>

Dear expert,  
I have a THnSparse with 4 dimension and I want to get the values from one of them.  
What I have done:

```c++
 THnSparse *sparse = (THnSparse *)list->FindObject("sparse"); 
TH1F *proj = (TH1F *)sparse->Projection(1);
int entries = proj->GetEntries();
Double_t value;
TH1F *hist;
for(int i(1); i<=entries; i++) {
    value = proj->GetBinContent(i);
    hist->Fill(value);
}

```

However, hist does not retrieve the same values as the projection.  
I am not sure how to achieve this, or what I did wrong.

Any help is more than appreciated!

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [September 11, 2022, 7:53pm UTC](https://root-forum.cern.ch/t/get-values-from-projection/51536/2 "2022-09-11T19:53:41Z")

</div>

Hi!

I have no clue how this code works for you, because the `TH1F *hist` pointer is uninitialized and then you are dereferencing it in `hist->Fill()` 🙂

Anyway, I think in principle what you do wrong is this: in your new histogram, you want to set the bin content exactly like in the projection histogram. With `Fill()`, you are only increasing the bin count by one in the bin that the value falls into, which is absolutely not what you want because the value is a bin count itself.

You should use [TH1::SetBinContent()](https://root.cern.ch/doc/master/classTH1.html#a9599e2dae40c574fd9d8970aaf638ee6) instead:

```c++
    for(int i(1); i<=entries; i++)
    {
        value = proj->GetBinContent(i);
        hist->SetBinContent(i, value);
    }

```

I hope this helps!

Jonas
