Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree104_tree.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
  2. Using the automatic compiler interface
  3. ROOT::Detail::TRangeCast< T, true > TRangeDynCast
    TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
    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:
    #include "TROOT.h"
    #include "TFile.h"
    #include "TTree.h"
    #include "TBrowser.h"
    #include "TH2.h"
    #include "TRandom.h"
    {
    // create a Tree file tree104.root
    // create the file, the Tree and a few branches
    TFile f("tree104.root", "recreate");
    TTree t1("t1", "a simple Tree with simple variables");
    Float_t px, py, pz;
    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;
    ev = i;
    t1.Fill();
    }
    // save the Tree header. The file will be automatically closed
    // when going out of the function scope
    t1.Write();
    }
    {
    // read the Tree generated by tree1w and fill two histograms
    Float_t px, py, pz;
    // note that we create the TFile and TTree objects on the heap!
    // because we want to keep these objects alive when we leave this function.
    auto f = TFile::Open("tree104.root");
    auto t1 = f->Get<TTree>("t1");
    t1->SetBranchAddress("px", &px);
    t1->SetBranchAddress("py", &py);
    t1->SetBranchAddress("pz", &pz);
    t1->SetBranchAddress("random", &random);
    t1->SetBranchAddress("ev", &ev);
    // create two histograms
    auto hpx = new TH1F("hpx", "px distribution", 100, -3, 3);
    auto 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++) {
    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.
    // Allow to use the TTree after the end of the function.
    t1->ResetBranchAddresses();
    }
    {
    }
    #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:69
    int nentries
    #define gROOT
    Definition TROOT.h:414
    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 an on-disk file, usually with extension .root, that stores objects in a file-system-li...
    Definition TFile.h:131
    static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
    Create / open a file.
    Definition TFile.cxx:4131
    1-D histogram with a float per channel (see TH1 documentation)
    Definition TH1.h:877
    2-D histogram with a float per channel (see TH1 documentation)
    Definition TH2.h:307
    Double_t Rndm() override
    Machine independent random number generator.
    Definition TRandom.cxx:559
    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:507
    A TTree represents a columnar dataset.
    Definition TTree.h:84
    auto * t1
    Definition textangle.C:20
Author
Rene Brun

Definition in file tree104_tree.C.