Get values from projection

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

 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);

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!


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() :slight_smile:

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() instead:

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

I hope this helps!


1 Like