How to introduce data in a tree?

Hi,
I have an example how to create a tree but I want to know how to introduce data from notepad or an excel . The code is the following one:
#include “TROOT.h”
#include “TFile.h”
#include “TTree.h”
#include “TBrowser.h”
#include “TH2.h”
#include “TRandom.h”

void tree1w()
{
//create a Tree file tree1.root

 //create the file, the Tree and a few branches

TFile f(“tree1.root”,“recreate”);
TTree t1(“t1”,“a simple Tree with simple variables”);
Float_t px, py, pz;
Double_t random;
Int_t ev;
t1.Branch(“px”,&px,“px/F”);
t1.Branch(“py”,&py,“py/F”);
t1.Branch(“pz”,&pz,“pz/F”);
t1.Branch(“random”,&random,“random/D”);
t1.Branch(“ev”,&ev,“ev/I”);

 //fill the tree
 for (Int_t i=0;i<10000;i++) {
  gRandom->Rannor(px,py);
  pz = px*px + py*py;
  random = gRandom->Rndm();
   ev = i;
   t1.Fill();

}

//save the Tree header. The file will be automatically closed
//when going out of the function scope
t1.Write();
}

void tree1r()
{
//read the Tree generated by tree1w and fill two histograms

 //note that we use "new" to create the TFile and TTree objects !
 //because we want to keep these objects alive when we leave this function.
 TFile *f = new TFile("tree1.root");
 TTree *t1 = (TTree*)f->Get("t1");
Float_t px, py, pz;
 Double_t random;
 Int_t ev;
 t1->SetBranchAddress("px",&px);
 t1->SetBranchAddress("py",&py);
 t1->SetBranchAddress("pz",&pz);
 t1->SetBranchAddress("random",&random);
 t1->SetBranchAddress("ev",&ev);
 
//create two histograms
 TH1F *hpx   = new TH1F("hpx","px distribution",100,-3,3);
TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);

 //read all entries and fill the histograms
Int_t nentries = (Int_t)t1->GetEntries();
for (Int_t i=0;i<nentries;i++) {
  t1->GetEntry(i);
   hpx->Fill(px);
  hpxpy->Fill(px,py);

}

//we do not close the file. We want to keep the generated histograms
//we open a browser and the TreeViewer
if (gROOT->IsBatch()) return;
new TBrowser();
t1->StartViewer();
// in the browser, click on “ROOT Files”, then on “tree1.root”.
// you can click on the histogram icons in the right panel to draw them.
// in the TreeViewer, follow the instructions in the Help button.
}

void tree1() {
tree1w();
tree1r();
}

The next target of mine is that I want to extract data from this tree tu built a 2D Graph with error bar with the following code:
#ifndef ROOT_TGraph2DErrors
#define ROOT_TGraph2DErrors

#ifndef ROOT_TGraph2D
#include “TGraph2D.h”
#include <TROOT.h>
#endif

class TGraph2DErrors : public TGraph2D {

private:

 TGraph2DErrors(const TGraph2DErrors&); // Not implemented
 TGraph2DErrors& operator=(const TGraph2DErrors&); // Not implemented

protected:
Double_t *fEX; //[fNpoints] array of X errors
Double_t *fEY; //[fNpoints] array of Y errors
Double_t *fEZ; //[fNpoints] array of Z errors

public:
TGraph2DErrors();
TGraph2DErrors(Int_t n);
TGraph2DErrors(Int_t n, Double_t *x, Double_t *y, Double_t *z,
Double_t *ex=0, Double_t *ey=0, Double_t *ez=0, Option_t *option="");
virtual ~TGraph2DErrors();
Double_t GetErrorX(Int_t bin) const;
Double_t GetErrorY(Int_t bin) const;
Double_t GetErrorZ(Int_t bin) const;
Double_t *GetEX() const {return fEX;}
Double_t *GetEY() const {return fEY;}
Double_t *GetEZ() const {return fEZ;}
virtual void Set(Int_t n);
virtual void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z);
virtual void SetPointError(Int_t i, Double_t ex, Double_t ey, Double_t ez);

ClassDef(TGraph2DErrors,1)  //A 2D graph with error bars

};

#endif
Rgds
Bruno Santos

Look at the documentation of TTree::Draw at:
root.cern.ch/root/html/TTree.html#TTree:Draw
and in particular at the section that says:

  How to obtain more info from TTree::Draw
  ========================================

Once TTree::Draw has been called, it is possible to access useful
information still stored in the TTree object via the following
functions:
-GetSelectedRows() // return the number of entries accepted by the
//selection expression. In case where no
selection
//was specified, returns the number of entries
processed.
-GetV1() //returns a pointer to the float array of V1
-GetV2() //returns a pointer to the float array of V2
-GetV3() //returns a pointer to the float array of V3
-GetW() //returns a pointer to the double array of
Weights
//where weight equal the result of the selection
expression.
where V1,V2,V3 correspond to the expressions in
TTree::Draw(“V1:V2:V3”,selection);

Example:
Root > ntuple->Draw(“py:px”,“pz>4”);
Root > TGraph *gr = new TGraph(ntuple->GetSelectedRows(),
ntuple->GetV2(), ntuple->GetV1());
Root > gr->Draw(“ap”); //draw graph in current pad
creates a TGraph object with a number of points corresponding to the
number of entries selected by the expression “pz>4”, the x points of
the graph
being the px values of the Tree and the y points the py values.

Important note: By default TTree::Draw creates the arrays obtained
with GetV1, GetV2, GetV3, GetW with a length corresponding to the
parameter fEstimate. By default fEstimate=10000 and can be modified
via TTree::SetEstimate. A possible recipee is to do
tree->SetEstimate(tree->GetEntries());
You must call SetEstimate if the expected number of selected rows
is greater than 10000.

Cheers,
Philippe.

I have still some doubts . To create a branchdescriptor have you got to make a new code? Or you simply add to the tree ? If it is the last option I would like to Know how to do it . On other hand, how can I apply the code from the 2D graph with bar errors ?
Cheers,
Bruno Santos

[quote]To create a branchdescriptor have you got to make a new code? Or you simply add to the tree ?[/quote]I do not understand the question (and suggest you might want to re-read the chapter on TTree in the User’s guide) as you never ‘create’ a branchdescriptor … in some cases you give a description of the data to the branch you are creating …

[quote]On other hand, how can I apply the code from the 2D graph with bar errors ?[/quote] What did you try? In the example just replace TGraph with TGraphErrors … plus of course you will need to pass the errors.

Cheers,
Philippe.

I 'll read the links that you send me and I’ll try hard again. If I can’t do it I 'll ask for help.
Cheers
Bruno

I am sorry to bohter but I dont know how to create branchDescriptor .
If you can help me I would be very pleased.
Cheers,
Bruno

Hi,

It is explained in details in the chapter on TTree in the User’s Guide.
Alternatively you can look in the reference guide a couple down from this link.

However the simpliest by far is to use object and generate their dictionary via ACliC.

With the file myscript.C[code]// Start of myscript.C
struct MyClass {
Int_t fEventNumber;
Float_t fEnergy;
};

void createfile() {
MyClass event;
TFile *file = TFile::Open(“data.root”,“RECREATE”);
TTree *tree = new TTree(“tree”,“tree”);
tree->Branch(“event”,&event);

do {
    // set the values in event
    event.fEventNumber++;
    event.fEnergy = ....;

while( not done );

file->Write();
delete file;
}[/code]

Cheers,
Philippe.