How to write data from a branch


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.14
Platform: Linux
Compiler: Not Provided


Hi,

I have a root file containing a tree with a branch “pTheta_emitted” which contains the 1D histogram of angular distribution of a particle from 0 to 180 deg. I want to write data in a text file in the form of (theta_min, theta_max, counts) from this branch for two degree interval upto the full 180 deg range, for example
theta_min theta_max counts
2 4 266
4 6 408

I have attached the root file. I would really appreciate any help.

proton_emitted_16MeV.root (590.4 KB)

{
  // root -b -l -q -e '.x this_macro.cxx' > this_macro.out.txt
  TFile *f = TFile::Open("proton_emitted_16MeV.root");
  TTree *t; f->GetObject("tree", t);
  TH1D *h = new TH1D("h", "h", 180, 0., 180.);
  t->Project("h", "pTheta_emitted");
  // h->DrawCopy();
  Int_t first = 0; // >= 0
  Int_t step = 2; // >= 1
  Int_t last = (h->GetNbinsX() - step); // <= (h->GetNbinsX() - step)
  for (Int_t i = first; i <= last; i += step) {
    Double_t sum = 0.;
    for (Int_t j = 0; j < step; j++) sum += h->GetBinContent(i + j + 1);
    std::cout << i << " " << (i + step) << " " << sum << std::endl;
  }
  delete f; // automatically deletes "t" and "h", too
}
1 Like

Thank you very much!