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