Logo ROOT  
Reference Guide
tree1.C File Reference

Detailed Description

View in nbviewer Open in SWAN This example is a variant of hsimple.C but using a TTree instead of a TNtuple.

It shows:

  • how to fill a Tree with a few simple variables.
  • how to read this Tree
  • how to browse and analyze the Tree via the TBrowser and TTreeViewer This example can be run in many different ways:
  1. Using the Cling interpreter
    .x tree1.C
  2. Using the automatic compiler interface
    .x tree1.C++
  3. .L tree1.C or .L tree1.C++
    One can also run the write and read parts in two separate sessions. For example following one of the sessions above, one can start the session:
    .L tree1.C
    #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;
    //fill the tree
    for (Int_t i=0;i<10000;i++) {
    pz = px*px + py*py;
    random = gRandom->Rndm();
    ev = i;
    //save the Tree header. The file will be automatically closed
    //when going out of the function scope
    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;
    //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
    Long64_t nentries = t1->GetEntries();
    for (Long64_t i=0;i<nentries;i++) {
    //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();
    // 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.
    // Allow to use the TTree after the end of the function.
    void tree1() {
    #define f(i)
    Definition: RSha256.hxx:104
    int Int_t
    Definition: RtypesCore.h:45
    float Float_t
    Definition: RtypesCore.h:57
    double Double_t
    Definition: RtypesCore.h:59
    long long Long64_t
    Definition: RtypesCore.h:80
    int nentries
    Definition: THbookFile.cxx:91
    #define gROOT
    Definition: TROOT.h:404
    R__EXTERN TRandom * gRandom
    Definition: TRandom.h:62
    Using a TBrowser one can browse all ROOT objects.
    Definition: TBrowser.h:37
    A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
    Definition: TFile.h:54
    1-D histogram with a float per channel (see TH1 documentation)}
    Definition: TH1.h:574
    virtual Int_t Fill(Double_t x)
    Increment bin with abscissa X by 1.
    Definition: TH1.cxx:3348
    2-D histogram with a float per channel (see TH1 documentation)}
    Definition: TH2.h:257
    Int_t Fill(Double_t) override
    Invalid Fill method.
    Definition: TH2.cxx:358
    Double_t Rndm() override
    Machine independent random number generator.
    Definition: TRandom.cxx:552
    virtual void Rannor(Float_t &a, Float_t &b)
    Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
    Definition: TRandom.cxx:500
    A TTree represents a columnar dataset.
    Definition: TTree.h:79
    auto * t1
    Definition: textangle.C:20
Rene Brun

Definition in file tree1.C.