00001 #include "TROOT.h"
00002 #include "TFile.h"
00003 #include "TTree.h"
00004 #include "TBrowser.h"
00005 #include "TH2.h"
00006 #include "TRandom.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 void tree1w()
00026 {
00027
00028
00029
00030 TFile f("tree1.root","recreate");
00031 TTree t1("t1","a simple Tree with simple variables");
00032 Float_t px, py, pz;
00033 Double_t random;
00034 Int_t ev;
00035 t1.Branch("px",&px,"px/F");
00036 t1.Branch("py",&py,"py/F");
00037 t1.Branch("pz",&pz,"pz/F");
00038 t1.Branch("random",&random,"random/D");
00039 t1.Branch("ev",&ev,"ev/I");
00040
00041
00042 for (Int_t i=0;i<10000;i++) {
00043 gRandom->Rannor(px,py);
00044 pz = px*px + py*py;
00045 random = gRandom->Rndm();
00046 ev = i;
00047 t1.Fill();
00048 }
00049
00050
00051
00052 t1.Write();
00053 }
00054
00055 void tree1r()
00056 {
00057
00058
00059
00060
00061 TFile *f = new TFile("tree1.root");
00062 TTree *t1 = (TTree*)f->Get("t1");
00063 Float_t px, py, pz;
00064 Double_t random;
00065 Int_t ev;
00066 t1->SetBranchAddress("px",&px);
00067 t1->SetBranchAddress("py",&py);
00068 t1->SetBranchAddress("pz",&pz);
00069 t1->SetBranchAddress("random",&random);
00070 t1->SetBranchAddress("ev",&ev);
00071
00072
00073 TH1F *hpx = new TH1F("hpx","px distribution",100,-3,3);
00074 TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);
00075
00076
00077 Int_t nentries = (Int_t)t1->GetEntries();
00078 for (Int_t i=0;i<nentries;i++) {
00079 t1->GetEntry(i);
00080 hpx->Fill(px);
00081 hpxpy->Fill(px,py);
00082 }
00083
00084
00085
00086 if (gROOT->IsBatch()) return;
00087 new TBrowser();
00088 t1->StartViewer();
00089
00090
00091
00092 }
00093
00094 void tree1() {
00095 tree1w();
00096 tree1r();
00097 }