# Splitting THnSparse

**URL:** <https://root-forum.cern.ch/t/splitting-thnsparse/54431>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [April 10, 2023, 3:44pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431 "2023-04-10T15:44:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmclauchlin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@cmclauchlin](https://root-forum.cern.ch/u/cmclauchlin)\
**Post date:** [April 10, 2023, 3:44pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/1 "2023-04-10T15:44:17Z")

</div>

I have a 7-dimensional THnSparse histogram (hist\_7d) and want to be able to split it up into a 2d vector of 5-dimensional THnSparse (hist\_5d[i][j]) where the i and j indices correspond to specific bins in the first two axes of hist\_7d. To my knowledge I cannot simply project into the other 5 dimensions as it would integrate over all other dimensions, which I don’t want.

I’ve tried brute force looping over all the bins and effectively building brand new THnSparse histograms from this, but I’ve found results to be suspicious and I would love an internal way of splitting up THnSparse histograms like this. Is there a built in functionality for this?

_ROOT Version:_ 6.26.04  
_Platform:_ macOS Ventura 13.0  
_Compiler:_ g++

---

<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:** [April 10, 2023, 3:59pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/2 "2023-04-10T15:59:33Z")

</div>

See the “ **Projections** ” paragraph in the [THnSparse](https://root.cern/doc/master/classTHnSparse.html) class reference.

---

<div class="post-metadata">

**Author:** ![cmclauchlin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@cmclauchlin](https://root-forum.cern.ch/u/cmclauchlin)\
**Post date:** [April 10, 2023, 4:02pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/3 "2023-04-10T16:02:51Z")

</div>

> [@cmclauchlin](#):
>
> To my knowledge I cannot simply project into the other 5 dimensions as it would integrate over all other dimensions, which I don’t want.

I’ve looked at the Projections paragraph and the documentation for it for months and if one can use Projections to do what I’m talking about, then I don’t see it. Could you be a bit more specific as to how one could use Projections to do this?

---

<div class="post-metadata">

**Author:** ![cmclauchlin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@cmclauchlin](https://root-forum.cern.ch/u/cmclauchlin)\
**Post date:** [April 10, 2023, 4:14pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/4 "2023-04-10T16:14:00Z")

</div>

I think you’re referring to the part where it says:

> To only project parts of the histogram, call

```auto
THnSparse::GetAxis(12)->SetRange(from_bin, to_bin);

```

right?

It’s not clear to me how to use that or where I would incorporate it.  
What I’m familiar with would be

```auto
int bins = 5;
int which_bins[5] = {2,3,4,5,6};
for(int j=0; j<hist_7d->GetAxis(0)->GetNbins(); j++){
    for(int k=0; k<hist_7d->GetAxis(1)->GetNbins(); k++){
         hist_5d[j][k]=hist_7d->Projection(bins, which_bins,"E"); 
    }
}

```

So it’s not clear to me how I would use the prior? Would I do something like

```auto
int bins = 5;
int which_bins[5] = {2,3,4,5,6};
for(int j=0; j<hist_7d->GetAxis(0)->GetNbins(); j++){
    for(int k=0; k<hist_7d->GetAxis(1)->GetNbins(); k++){
         hist_6d[k]=hist_7d->GetAxis(1)->SetRange(k+1,k+1); 
         hist_5d[j][k] = hist_6d[k]->GetAxis(0)->SetRange(j+1,j+1);
    }
}

```

or am I misunderstanding?

---

<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:** [April 10, 2023, 4:26pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/5 "2023-04-10T16:26:17Z")

</div>

Try:

```cpp
hist_7d->GetAxis(0)->SetRange(j + 1, j + 1);
hist_7d->GetAxis(1)->SetRange(k + 1, k + 1);
hist_5d[j][k] = hist_7d->Projection(bins, which_bins, "E");

```

---

<div class="post-metadata">

**Author:** ![cmclauchlin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@cmclauchlin](https://root-forum.cern.ch/u/cmclauchlin)\
**Post date:** [April 10, 2023, 6:28pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/6 "2023-04-10T18:28:38Z")

</div>

I need to run a couple more tests to make sure it’s behaving as intended, but so far it looks like it’s working! To check comprehension: calling this hist\_7d-\>GetAxis(n)-\>SetRange(j,k) for different n’s, j’s, and k’s would all appropriately switch it to the designated bins and the underlying data and error in hist\_7d remains unchanged?  
So if I were to do

```auto
hist_7d->GetAxis(0)->SetRange(1, 1);
hist_7d->GetAxis(1)->SetRange(2, 2);
hist_5d[1][2] = hist_7d->Projection(bins, which_bins, "E");
hist_7d->GetAxis(0)->SetRange(3,3);
hist_7d->GetAxis(1)->SetRange(4,4);
hist_5d[3][4] = hist_7d->Projection(bins, which_bins, "E");

```

Then even though they’re sequential, the GetAxis and SetRange resets the expressed range in that axis for hist\_7d? And to get it back to “normal” I’d need to call

```auto
hist_7d->GetAxis(0)->SetRange(1,hist_7d->GetAxis(0)->GetNbins());
hist_7d->GetAxis(1)->SetRange(1, hist_7d->GetAxis(0)->GetNbins());

```

yes?

---

<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:** [April 10, 2023, 6:30pm UTC](https://root-forum.cern.ch/t/splitting-thnsparse/54431/7 "2023-04-10T18:30:07Z")

</div>

```cpp
hist_7d->GetAxis(0)->SetRange(); // () or (0, 0)
hist_7d->GetAxis(1)->SetRange(); // () or (0, 0);

```

BTW. See: [TAxis::SetRange](https://root.cern/doc/master/classTAxis.html#aed523b084d6b3f24f6b1128d7810e199)
