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

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 *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;
}
1 Like