# 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:** 1\
**Showing post:** 70

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

```

---

_[View the full topic](https://root-forum.cern.ch/t/help-on-method-s-to-create-tree-from-multi-fold-gamma-ray-coincidence-data/29704)._
