Please read tips for efficient and successful posting and posting code
Please fill also the fields below. Note that root -b -q
will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug
from the ROOT prompt to pre-populate a topic.
ROOT Version: 6.28
Platform: Window 11
Compiler: Not Provided
Dear all,
I’m a newbie in ROOT. I have a ROOT file exported from Geant4 as shown in the below image.
I wrote a script to read data to plot histograms as the attached code. But it didn’t read the data. Can anyone help me, please?
void plot()
{
gROOT->Reset();
gROOT->SetStyle("Plain");
TFile* f = new TFile("output.root", "READ");
// list all Tree and Historgram
f->ls();
TTree* tree = (TTree*)f->Get("DetResults0;1");
tree->Print();
tree->Show(10);
double eSum;
double eProton;
double eAlpha;
double eBe8;
double eGamma;
double eB11;
double eB10;
double eNeutron;
double eOther;
int entries;
entries = tree->GetEntries();
cout << "Number of entries: " << entries << endl;
tree->SetBranchAddress("row_wise_branch.ESum", &eSum);
tree->SetBranchAddress("row_wise_branch.EProton", &eProton);
tree->SetBranchAddress("row_wise_branch.EAlpha", &eAlpha);
tree->SetBranchAddress("row_wise_branch.EBe8", &eBe8);
tree->SetBranchAddress("row_wise_branch.EGamma", &eGamma);
tree->SetBranchAddress("row_wise_branch.EB11", &eB11);
tree->SetBranchAddress("row_wise_branch.EB10", &eB10);
tree->SetBranchAddress("row_wise_branch.ENeutron", &eNeutron);
tree->SetBranchAddress("row_wise_branch.EOther", &eOther);
TH1D* Total_Hist = new TH1D("Total_Hist", "Total Energy Spectrum", 8000, 0., 8.);
TH1D* Proton_Hist = new TH1D("Proton_Hist", "Proton's Energy Spectrum", 8000, 0., 8.);
TH1D* Alpha_Hist = new TH1D("Alpha_Hist", "Alpha's Energy Spectrum", 8000, 0., 8.);
TH1D* Be8_Hist = new TH1D("Be8_Hist", "Be8's Energy Spectrum", 8000, 0., 8.);
TH1D* Gamma_Hits = new TH1D("Gamma_Hits", "Gamma's Energy Spectrum", 8000, 0., 8.);
TH1D* B11_Hits = new TH1D("B11_Hits", "B11's Energy Spectrum", 8000, 0., 8.);
TH1D* B10_Hist = new TH1D("B10_Hist", "B10's Energy Spectrum", 8000, 0., 8.);
TH1D* Neutron_Hist = new TH1D("Neutron_Hist", "Neutron's Energy Spectrum", 8000, 0., 8.);
TH1D* Others_Hist = new TH1D("Others_Hist", "Others Energy Spectrum", 8000, 0., 8.);
for(int i=0; i<entries; ++i)
{
tree->GetEntry(i);
// fill to histograms
Total_Hist->Fill(eSum);
Proton_Hist->Fill(eProton);
Alpha_Hist->Fill(eAlpha);
Be8_Hist->Fill(eBe8);
Gamma_Hits->Fill(eGamma);
B11_Hits->Fill(eB11);
B10_Hist->Fill(eB10);
Neutron_Hist->Fill(eNeutron);
Others_Hist->Fill(eOther);
}
}