# Help on Method/s to create Tree from multi-fold gamma-ray coincidence data

**URL:** <https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704>\
**Category:** ROOT\
**Created:** [July 9, 2018, 7:06am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704 "2018-07-09T07:06:20Z")\
**Posts on this page:** 12\
**Page:** 4

<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:** [August 11, 2018, 7:08am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/67 "2018-08-11T07:08:10Z")

</div>

I think you can easily reduce the amount of the required RAM by (up to) a factor 6, if you explicitly sort your gammas:

```auto
#include <algorithm>
// ...
      for(int j = 0; j < (No_Clover - 2); j++) {
        for(int k = (j + 1); k < (No_Clover - 1); k++) { // (k > j)
          for(int l = (k + 1); l < No_Clover; l++) { // (l > k)
            // ...
                        // Double_t x[3] = {(Double_T_t)Eclab[j], (Double_T_t)Eclab[k], (Double_T_t)Eclab[l]};
                        // Double_t x[3] = {(Eclab[j] + 1.e-6), (Eclab[k] + 1.e-6), (Eclab[l] + 1.e-6)};
                        Double_t x[3] = {(Eclab[j] + 0.5), (Eclab[k] + 0.5), (Eclab[l] + 0.5)};
                        std::sort(x, x + 3); // sort in an increasing order
                        if (his3D->GetNbins() <= MaxNbins) his3D->Fill(x);
                        // ...

```

When you do not sort your gammas, each triple of energies appears 6 times in your 3D histogram so, there are 6 “peaks” in different areas of the 3D histogram (where 6 is the number of permutations, of course).

If you sort them (in an increasing order), you will get 1 “peak” only (always energy\_gamma\_1 \<= energy\_gamma\_2 \<= energy\_gamma\_3).

This makes your 3D histogram “totally unsymmetric” but, I think you can take it into account when projecting / analysing it later (well, I’m afraid that this idea requires some detailed thoughtful examination so, you should discuss it with your colleagues).

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 11, 2018, 11:49am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/68 "2018-08-11T11:49:57Z")

</div>

I am open to make thorough checks using the procedure that you have mentioned. However, I don’t to how to analyze such “unsymmetric” cube. Can you please tell me the possible analysis method/s?

If I have to make such a “totally unsymmetric” cube, then probably, [TH3F](https://root.cern.ch/doc/master/classTH3F.html) class will also work. Then, I can make use of [TSpectrum3](https://root.cern.ch/doc/v612/classTSpectrum3.html) for subtracting 3D-background from the 3D-histogram, which I guess not possible in [THnSparse](https://root.cern.ch/root/html524/THnSparse.html).

BTW, I am able to successfully generate 3D THnSparse with lower dimensions (1000) with full data set.

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 11, 2018, 3:47pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/69 "2018-08-11T15:47:14Z")

</div>

Is this same as scaling or using weightage while filling the histogram?  
i.e. his3D-\>Fill(x,0.1666666);

---

<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:** [August 12, 2018, 9:08am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/70 "2018-08-12T09:08:13Z")

</div>

Maybe this works:

```auto
#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 *option = "") { // THnSparse::Projection option
  if (!h) return 0; // just a precaution
  TAxis *a0 = h->GetAxis(0);
  TAxis *a1 = h->GetAxis(1);
  TAxis *a2 = h->GetAxis(2);
  a0->SetRange(a0->FindFixBin( low1 ), a0->FindFixBin( up1 ));
  a1->SetRange(a1->FindFixBin( low2 ), a1->FindFixBin( up2 ));
  a2->SetRange(-1, -1);
  TH1D *h1 = h->Projection(2, option);
  h1->SetName(TString::Format("%s_Project1D", h->GetName()));
  a0->SetRange(a0->FindFixBin( low2 ), a0->FindFixBin( up2 ));
  a1->SetRange(a1->FindFixBin( low1 ), a1->FindFixBin( up1 ));
  TH1D *hi = h->Projection(2, option);
  h1->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(a0->FindFixBin( low1 ), a0->FindFixBin( up1 ));
  a1->SetRange(-1, -1);
  a2->SetRange(a2->FindFixBin( low2 ), a2->FindFixBin( up2 ));
  hi = h->Projection(1, option);
  h1->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(a0->FindFixBin( low2 ), a0->FindFixBin( up2 ));
  a2->SetRange(a2->FindFixBin( low1 ), a2->FindFixBin( up1 ));
  hi = h->Projection(1, option);
  h1->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(-1, -1);
  a1->SetRange(a1->FindFixBin( low1 ), a1->FindFixBin( up1 ));
  a2->SetRange(a2->FindFixBin( low2 ), a2->FindFixBin( up2 ));
  hi = h->Projection(0, option);
  h1->Add(hi);
  delete hi; // no longer needed
  a1->SetRange(a1->FindFixBin( low2 ), a1->FindFixBin( up2 ));
  a2->SetRange(a2->FindFixBin( low1 ), a2->FindFixBin( up1 ));
  hi = h->Projection(0, option);
  h1->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(-1, -1);
  a1->SetRange(-1, -1);
  a2->SetRange(-1, -1);
  return h1;
}

// Usage example: TH2D *h2 = Project2D(his3D, 449.5, 452.5);
// Note: in principle, you should set low <= up
TH2D *Project2D(THnSparse *h, // the ("totally unsymmetric") "cubic" histogram
                Double_t low, Double_t up, // the "window"
                Option_t *option = "") { // THnSparse::Projection option
  if (!h) return 0; // just a precaution
  TAxis *a0 = h->GetAxis(0);
  TAxis *a1 = h->GetAxis(1);
  TAxis *a2 = h->GetAxis(2);
  a0->SetRange(a0->FindFixBin( low ), a0->FindFixBin( up ));
  a1->SetRange(-1, -1);
  a2->SetRange(-1, -1);
  TH2D *h2 = h->Projection(1, 2, option);
  h2->SetName(TString::Format("%s_Project2D", h->GetName()));
  TH2D *hi = h->Projection(2, 1, option);
  h2->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(-1, -1);
  a1->SetRange(a1->FindFixBin( low ), a1->FindFixBin( up ));
  a2->SetRange(-1, -1);
  hi = h->Projection(0, 2, option);
  h2->Add(hi);
  delete hi; // no longer needed
  hi = h->Projection(2, 0, option);
  h2->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(-1, -1);
  a1->SetRange(-1, -1);
  a2->SetRange(a2->FindFixBin( low ), a2->FindFixBin( up ));
  hi = h->Projection(0, 1, option);
  h2->Add(hi);
  delete hi; // no longer needed
  hi = h->Projection(1, 0, option);
  h2->Add(hi);
  delete hi; // no longer needed
  a0->SetRange(-1, -1);
  a1->SetRange(-1, -1);
  a2->SetRange(-1, -1);
  return h2;
}

```

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 13, 2018, 11:02am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/71 "2018-08-13T11:02:16Z")

</div>

Please give me some time to test this. Will get back to you with the outcome after testing.

In the meantime, can you please answer my last question?

You have mentioned in the post 61 of this thread that we can reduce the RAM usage by a factor of 6 by creating an “asymmetric” 3D-histogram. Is it the same as using

`his3D->Fill(x,0.1666666); // 1/6 = 0.16666666`

instead of

`his3D->Fill(x);`

---

<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:** [August 13, 2018, 11:04am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/72 "2018-08-13T11:04:55Z")

</div>

[Just try it.](https://en.wikipedia.org/wiki/Learning-by-doing)

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 14, 2018, 10:04am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/73 "2018-08-14T10:04:20Z")

</div>

I tried it, and it failed to work 😢

Now, I am testing the last method. Will get back to you with results.

BTW, do you know whether @pcanal is working on/has time to resolve the issue?

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 15, 2018, 12:28pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/74 "2018-08-15T12:28:56Z")

</div>

I have tested the “unsymmetric” cubic histogram and the corresponding projection method that you have suggested. The preliminary observations indicate that everything is **working perfectly as expected**. I will do more thorough check in the weekend.

As I had mentioned earlier, I have to create several projection1D using different combination of gamma-ray pairs. For this, I am using the macro “Sparse\_project.C” (attached). I use the following sequence to generate two 1D-Projections:

```auto
root [0] TFile *f = TFile::Open("sparse3D_asym_all_4k.root");
root [1] .ls
TFile** sparse3D_asym_all_4k.root	
 TFile* sparse3D_asym_all_4k.root	
  KEY: THnSparseT<TArrayF>	sparse3D;1	sparse3D
root [2] .L Sparse_project.C 
root [3] Project1D(sparse3D,449.5,453.5,668.5,672.5);
root [4] .ls
TFile** sparse3D_asym_all_4k.root	
 TFile* sparse3D_asym_all_4k.root	
  OBJ: TH1D	sparse3D_Project1D	sparse3D projection EclabZ : 0 at: 0xec6d440
  KEY: THnSparseT<TArrayF>	sparse3D;1	sparse3D
root [5] sparse3D_Project1D->Draw();
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
root [6] sparse3D_Project1D->SetName("his_451_670");
root [7] his_451_670->Draw();
root [8] Project1D(sparse3D,289.5,293.5,698.5,702.5);
root [9] .ls
TFile** sparse3D_asym_all_4k.root	
 TFile* sparse3D_asym_all_4k.root	
  OBJ: TH1D	his_451_670	sparse3D projection EclabZ : 0 at: 0xec6d440
  OBJ: TH1D	sparse3D_Project1D	sparse3D projection EclabZ : 0 at: 0xf129740
  KEY: THnSparseT<TArrayF>	sparse3D;1	sparse3D
root [10] sparse3D_Project1D->Draw();
root [11] sparse3D_Project1D->SetName("his_291_700");
root [12] his_291_700->Draw();
root [13] his_451_670->Draw();
root [14]

```

However, there is **a problem**. Both the spectra are identical. As you can see, I am using entirely different sets of gamma rays to generate two 1D-histograms. Am I doing anything wrong?

Also, I feel that it would be ideal to mention only gamma-ray energies while projecting, along with a variable “width parameter” which will internally set the “window” for the gamma rays.

[Sparse\_project.C](https://root-forum.cern.ch/uploads/default/original/3X/4/6/46456ed0d9eead52dc0568e74dee00ff314e4056.C) (3.4 KB)

[gen\_Sparse3D.cxx](https://root-forum.cern.ch/uploads/default/original/3X/6/7/67d6bbd057bfc26c33fe81216b78f8e39d7dbd52.cxx) (6.9 KB)

---

<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:** [August 15, 2018, 1:43pm UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/75 "2018-08-15T13:43:34Z")

</div>

Try (first you need to `root [0] .L Sparse_project.C`, of course):

```auto
{
  TFile *f = TFile::Open("sparse3D_asym_all_4k.root");
  THnSparse *h; f->GetObject("sparse3D", h);
  Double_t g1 = 451.5;
  Double_t g2 = 670.5;
  Double_t w = 2.0;
  TH1D *h1 = Project1D(h, g1 - w, g1 + w, g2 - w, g2 + w);
  h1->SetName(TString::Format("his_%d_%d", Int_t(g1), Int_t(g2)));
  h1->Print();
  g1 = 291.5;
  g2 = 700.5;
  TH1D *h2 = Project1D(h, g1 - w, g1 + w, g2 - w, g2 + w);
  h2->SetName(TString::Format("his_%d_%d", Int_t(g1), Int_t(g2)));
  h2->Print();
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 2);
  c->cd(1);
  h1->Draw();
  c->cd(2);
  h2->Draw();
  c->cd(0);
  // do we get exactly the same output as above?
  h1->Print();
  h2->Print();
}

```

---

<div class="post-metadata">

**Author:** ![ajaydeo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@ajaydeo](https://root-forum.cern.ch/u/ajaydeo)\
**Post date:** [August 16, 2018, 10:06am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/76 "2018-08-16T10:06:13Z")

</div>

It worked, thank you. The two 1D-histograms are no longer identical.

Please check the attached modified version of the macro which I used for 1D- and 2D- projections of THnSparse.

We know that we have [TSpectrum2](https://root.cern.ch/doc/v608/classTSpectrum2.html) and [TSpectrum3](https://root.cern.ch/doc/v608/classTSpectrum3.html) classes available for background generation of TH2 and TH3 histograms, respectively.

Are there any equivalent “background generation” methods/procedures available for THnSparse Class?

[Sparse\_project.C](https://root-forum.cern.ch/uploads/default/original/3X/6/8/68a78d84372bf0b4ef249b1005f1bdc92bd37e73.C) (3.7 KB)

---

<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:** [August 16, 2018, 10:47am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/77 "2018-08-16T10:47:36Z")

</div>

You don’t need to care if “eg\_low \<= eg\_hi”.  
Use “SetName” once per histogram (just modify the first occurrence).

---

<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:** [August 30, 2018, 10:47am UTC](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704/78 "2018-08-30T10:47:37Z")

</div>

Closed on August 30, 2018, 10:47am UTC

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

[Previous page](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704.md?page=3)
