# Projection in THnSparse

**URL:** <https://root-forum.cern.ch/t/projection-in-thnsparse/39025>\
**Category:** ROOT\
**Created:** [April 22, 2020, 6:59am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025 "2020-04-22T06:59:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [April 22, 2020, 6:59am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/1 "2020-04-22T06:59:35Z")

</div>

Hello,  
I am new in ROOT. I am analyzing gamma-gamma coincidence data. I needed to build a 3D triple gamma coincident histogram. For that I have used THnSparse and got gamma energies along three axes that I generated by using THnSparse. Now I need the projections. I can get the full projection along any of my axes, and see 1D or 2D histograms.  
However, I want to make a gate in my first axis (for a bin range, e.g., 260 to 280) and project onto the 2D histogram made by the second and the third axes. Can I do this by using a ROOT command line? Can anybody help me with this how can I see gated histogram made by THnSparse?

Thank You.  
Becka

---

<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 22, 2020, 7:58am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/2 "2020-04-22T07:58:36Z")

</div>

Some time ago, I wrote various source code (which deals with a creation of a 3D [THnSparse](https://root.cern/doc/master/classTHnSparse.html) and then its 1D and 2D projections) for someone else which you can reuse:

> [@Help on Method/s to create Tree from multi-fold gamma-ray coincidence data](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/70):
>
> Maybe this works: #include "TH1.h" #include "TH2.h" #include "THnSparse.h" #include "TAxis.h" #include "TString.h" // Usage example: TH1D \*h1 = Project1D(his3D, 449.5, 452.5, 668.5, 671.5); // Note: in principle, you should set low1 \<= up1 and low2 \<= up2 TH1D \*Project1D(THnSparse \*h, // the ("totally unsymmetric") "cubic" histogram Double\_t low1, Double\_t up1, // the first "window" Double\_t low2, Double\_t up2, // the second "window" Option\_t \*op…

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [April 23, 2020, 5:52am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/3 "2020-04-23T05:52:25Z")

</div>

Hi, Thank you so much, this post is very helpful indeed. But I am a bit confused about the gated projection. I understand the following,

```auto
THnSparse *h;
   TH1D *h1 = h->Projection(2, option);

```

However, I failed to understand the following, I have taken it from the link you suggested,

```auto
TH1D *Project1D(THnSparse *h, Double_t low1, Double_t up1, Double_t low2, Double_t up2, Option_t *option = "") 

```

Does it mean I am taking gates from the first (ranging from Double\_t low1 to Double\_t up1) and the second (Double\_t low2 to Double\_t up2) axes and projecting onto the third one? If this is so, can’t I take gates from the second and the third axes and project onto the first one? Also, can you tell me what does ‘option’ do?

Thank you,  
Becka

---

<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 23, 2020, 7:54am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/4 "2020-04-23T07:54:08Z")

</div>

The “gates” are applied to all possible axes combinations and these “partial” projections are summed.

For the “option”, see [THnBase::Projection](https://root.cern/doc/master/classTHnBase.html#a9b524f67ae6fdb4c18d57d7437975ab5) (forwarded from [THnSparse::Projection](https://root.cern/doc/master/classTHnSparse.html#a788487e2992ea9214379ed7a2de4b0b7)).

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [April 23, 2020, 8:35am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/5 "2020-04-23T08:35:21Z")

</div>

I see. But is there a way if I want to make a gate from the first axis and project onto the 2D histogram made by the second and the third axes? I mean can I have the control on from which axis I want to make a gate?

---

<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 23, 2020, 8:44am UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/6 "2020-04-23T08:44:54Z")

</div>

And what prevents you from doing it the way you want it?  
You do not have to use my full macros.  
You can e.g. use only parts of their source codes which suit your needs.

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [April 23, 2020, 3:18pm UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/7 "2020-04-23T15:18:01Z")

</div>

I assume I could not explain it properly. What I understood from your second reply that if I apply the following on a cubic histogram,

```auto
TH2D *Project2D(THnSparse *h, Double_t low, Double_t up, Option_t *option = "")

```

It will make gates on all three axes and project into the 2D histograms made by the rest two and finally all the partial projections will be summed up. However, I do not want the summed up histogram, I need to see the individual 2D histogram while gated and projected from the remaining axis. I am not sure if it is clear, or perhaps I am missing something as I said I am a new ROOT user.

---

<div class="post-metadata">

**Author:** ![Sbecka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@Sbecka](https://root-forum.cern.ch/u/Sbecka)\
**Post date:** [April 27, 2020, 9:49pm UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/8 "2020-04-27T21:49:00Z")

</div>

I figured out what I was missing, thank you so much! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 11, 2020, 9:49pm UTC](https://root-forum.cern.ch/t/projection-in-thnsparse/39025/9 "2020-05-11T21:49:05Z")

</div>

Closed on May 11, 2020, 9:49pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
