How can read data from ROOT files


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

Hi,

Welcome to the ROOT community!
I think the quickest is that you share the file with us, if possible.

Cheers,
D

Thank you so much.
Here is my root file.
output.root.txt (1.4 MB)

Hi,

The file seems OK, except that the tree DetResults0 seems empty.
Example for two histos (you can easily add the rest of them!):

ROOT::RDataFrame rdf("DetResults1", "output.root");
auto hESum = rdf.Histo1D("row_wise_branch.ESum");
auto hEProton = rdf.Histo1D("row_wise_branch.EProton");
// here your other histos
TCanvas c1;
hESum->Draw();
TCanvas c2;
hEProton->Draw();

I hope this helps!

Cheers,
D

Dear @Danilo
Thank you for your help. But it doesn’t work. I copied your code to my script and tried to plot the histogram. There is nothing that appeared.

Hi,

What do you mean by nothing happened? That will show 2 canvases with two histograms.

D

@hungbt1908

You may try replacing

TCanvas c1; hESum->Draw(); with
auto c1 = new TCanvas(); hESum->DrawClone();

and

TCanvas c2; hEProton->Draw(); with
auto c2 = new TCanvas(); hEProton->DrawClone();

Ajay

Thanks, @ajaydeo and @Danilo.
I have read and plotted NTuple data.
I have one more question. I would like to read row by row the NTuple data and fill them into a histogram.
How can I do it?

I have an example to read NTuple as shown below but it only worked for the newer Geant4 version.

    TFile* f = new TFile("output.root", "READ");
    TTree* tree = (TTree*)f->Get("DetResults0;1");

    double eSum;
    double eProton;
    double eAlpha;
    double eBe8;
	double eB11;
	double eB10;
	double eNeutron;
    double eGamma;
    double Other;

    tree->SetBranchAddress("ESum", &eSum);
    tree->SetBranchAddress("EProton", &eProton);
    tree->SetBranchAddress("EAlpha", &eAlpha);
    tree->SetBranchAddress("EBe8", &eBe8);
    tree->SetBranchAddress("EGamma", &eGamma);
	tree->SetBranchAddress("EB11", &eB11);
    tree->SetBranchAddress("EB10", &eB10);
    tree->SetBranchAddress("ENeutron", &eNeutron);
    tree->SetBranchAddress("EOther", &eOther);

    int entries;
    entries = tree->GetEntries();

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

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