Ascii2ROOT then drawing the variables

Hi,

I am creating a ROOT file from 2 different ascii files (each, a single column of text). However, when I try and draw the variables from the newly created root tree, using commands such as e.g.

T->Draw(“particle_energy>>h(5000,0,5000)”)

it seems to plot some kind of frequency distribution, which is not what I’m after. I would like to able to call the variables from the root tree and reproduce the 1D histograms (h1 and h2) that I declare in the code. My code is pasted below. At the moment the histograms h1 and h2 display what I want to see, but I’m not happy with the method I have used to create them since I really need to de able to display the spectra by playing with the variables in the root tree. Would really appreciate any ideas on how to rectify this. Cheers

#include <Riostream.h>
#include <fstream.h>
#include <stdio.h>
#include <TTree.h>
#include <TH1D.h>
#include <TH2D.h>

void reading(){

struct particles_t{

Double_t detector_ring;
Double_t particle_energy;

};

particles_t particles;

ifstream inputfile_1;
inputfile_1.open(“ring_hitpattern.txt”);

ifstream inputfile_2;
inputfile_2.open(“total_ring_pe.txt”);

TFile *f = new TFile(“new_spectra.root”,“RECREATE”);

TH1D *h1 = new TH1D(“h1”,“Rutherfords”,24,0,24);

TH1D *h2 = new TH1D(“h2”,“particle energy”,5000,0,5000);

TTree *tree = new TTree(“T”,“particle energy distribution”);
tree->Branch(“particle_energy”,&particles.particle_energy,“particle_energy/D”);
tree->Branch(“detector_ring”,&particles.detector_ring,“detector_ring/D”);

Int_t nlines = 0;
while(!inputfile_1.eof()){

inputfile_1 >> particles.detector_ring;

h1->Fill(nlines,particles.detector_ring);

tree->Fill();

nlines++;

}

while(!inputfile_2.eof()){

inputfile_2 >> particles.particle_energy;

h2->Fill(nlines,particles.particle_energy);

tree->Fill();

nlines++;

}

inputfile_1.close();
inputfile_2.close();

f->Write();
}

If you read the contents of each branch separatly, you should call TBranch::Fill and not TTree::Fill. I have modified your code accordingly. See lines marked with “//<=====”

Rene

[code]#include <Riostream.h>
#include <fstream.h>
#include <stdio.h>
#include <TTree.h>
#include <TH1D.h>
#include <TH2D.h>

void reading(){

struct particles_t{

Double_t detector_ring;
Double_t particle_energy;
};

particles_t particles;

ifstream inputfile_1;
inputfile_1.open(“ring_hitpattern.txt”);

ifstream inputfile_2;
inputfile_2.open(“total_ring_pe.txt”);

TFile *f = new TFile(“new_spectra.root”,“RECREATE”);
TH1D *h1 = new TH1D(“h1”,“Rutherfords”,24,0,24);
TH1D *h2 = new TH1D(“h2”,“particle energy”,5000,0,5000);

TTree *tree = new TTree(“T”,“particle energy distribution”);
TBranch *b1 = tree->Branch(“detector_ring”,&particles.detector_ring,“detector_ring/D”); //<======
TBranch *b2 = tree->Branch(“particle_energy”,&particles.particle_energy,“particle_energy/D”); //<======

Int_t nlines = 0;
while(!inputfile_1.eof()){
inputfile_1 >> particles.detector_ring;
h1->Fill(nlines,particles.detector_ring);
b1->Fill(); //<======
nlines++;
}
tree->SetEntries(nlines); //<======

while(!inputfile_2.eof()){
inputfile_2 >> particles.particle_energy;
h2->Fill(nlines,particles.particle_energy);
b2->Fill(); //<======
nlines++;
}

inputfile_1.close();
inputfile_2.close();

f->Write();
}[/code]