tree1.C

Go to the documentation of this file.
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 // This example is a variant of hsimple.C but using a TTree instead
00009 // of a TNtuple. It shows :
00010 //   -how to fill a Tree with a few simple variables.
00011 //   -how to read this Tree
00012 //   -how to browse and analyze the Tree via the TBrowser and TTreeViewer
00013 // This example can be run in many different ways:
00014 //  way1:  .x tree1.C    using the CINT interpreter
00015 //  way2:  .x tree1.C++  using the automatic compiler interface
00016 //  way3:  .L tree1.C  or .L tree1.C++
00017 //          tree1()
00018 // One can also run the write and read parts in two separate sessions.
00019 // For example following one of the sessions above, one can start the session:
00020 //   .L tree1.C
00021 //   tree1r();
00022 //
00023 //  Author: Rene Brun
00024 
00025 void tree1w()
00026 {
00027    //create a Tree file tree1.root
00028    
00029    //create the file, the Tree and a few branches
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    //fill the tree
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   //save the Tree header. The file will be automatically closed
00051   //when going out of the function scope
00052   t1.Write();
00053 }
00054 
00055 void tree1r()
00056 {
00057    //read the Tree generated by tree1w and fill two histograms
00058    
00059    //note that we use "new" to create the TFile and TTree objects !
00060    //because we want to keep these objects alive when we leave this function.
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    //create two histograms
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    //read all entries and fill the histograms
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   //we do not close the file. We want to keep the generated histograms
00085   //we open a browser and the TreeViewer
00086   if (gROOT->IsBatch()) return;
00087   new TBrowser();
00088   t1->StartViewer();
00089   // in the browser, click on "ROOT Files", then on "tree1.root".
00090   //     you can click on the histogram icons in the right panel to draw them.
00091   // in the TreeViewer, follow the instructions in the Help button.
00092 }   
00093 
00094 void tree1() {
00095    tree1w();
00096    tree1r();
00097 }

Generated on Tue Jul 5 15:45:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1