Logo ROOT  
Reference Guide
No Matches
tree4.C File Reference

Detailed Description

View in nbviewer Open in SWAN This example writes a tree with objects of the class Event.

It is a simplified version of $ROOTSYS/test/MainEvent.cxx to write the tree, and $ROOTSYS/test/eventb.C It shows:

This example can be run in many different ways:

  • way1 using the Cling interpreter:
    .x tree4.C
  • way2 using the Cling interpreter:
    .L tree4.C
  • way3 using ACLIC:
    .L ../test/libEvent.so
    .x tree4.C++
    Definition test.py:1
    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 tree4.C
    #include "TFile.h"
    #include "TTree.h"
    #include "TBrowser.h"
    #include "TH2.h"
    #include "TRandom.h"
    #include "TClassTable.h"
    #include "TSystem.h"
    #include "TROOT.h"
    #include "../test/Event.h"
    void tree4w()
    //create a Tree file tree4.root
    TFile f("tree4.root","RECREATE");
    // Create a ROOT Tree
    TTree t4("t4","A Tree with Events");
    // Create a pointer to an Event object
    Event *event = new Event();
    // Create two branches, split one.
    t4.Branch("event_split", &event,16000,99);
    t4.Branch("event_not_split", &event,16000,0);
    // a local variable for the event type
    char etype[20];
    // Fill the tree
    for (Int_t ev = 0; ev <100; ev++) {
    Float_t sigmat, sigmas;
    Int_t ntrack = Int_t(600 + 600 *sigmat/120.);
    Float_t random = gRandom->Rndm(1);
    event->SetHeader(ev, 200, 960312, random);
    for(UChar_t m = 0; m < 10; m++) {
    event->SetMeasure(m, Int_t(gRandom->Gaus(m,m+1)));
    // fill the matrix
    for(UChar_t i0 = 0; i0 < 4; i0++) {
    for(UChar_t i1 = 0; i1 < 4; i1++) {
    // Create and fill the Track objects
    for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);
    // Fill the tree
    // Clear the event before reloading it
    // Write the file header
    // Print the tree contents
    void tree4r()
    // read the tree generated with tree4w
    //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("tree4.root");
    TTree *t4 = (TTree*)f->Get("t4");
    // create a pointer to an event object. This will be used
    // to read the branch values.
    Event *event = new Event();
    // get two branches and set the branch address
    TBranch *bntrack = t4->GetBranch("fNtrack");
    TBranch *branch = t4->GetBranch("event_split");
    Long64_t nevent = t4->GetEntries();
    Int_t nselected = 0;
    Int_t nb = 0;
    for (Long64_t i=0;i<nevent;i++) {
    //read branch "fNtrack"only
    //reject events with more than 587 tracks
    if (event->GetNtrack() > 587)continue;
    //read complete accepted event in memory
    nb += t4->GetEntry(i);
    //print the first accepted event
    if (nselected == 1) t4->Show();
    //clear tracks array
    if (gROOT->IsBatch()) return;
    new TBrowser();
    void tree4() {
    Event::Reset(); // Allow for re-run this script by cleaning static variables.
    Event::Reset(); // Allow for re-run this script by cleaning static variables.
    #define f(i)
    Definition RSha256.hxx:104
    unsigned char UChar_t
    Definition RtypesCore.h:38
    long long Long64_t
    Definition RtypesCore.h:73
    Definition Rtypes.h:472
    #define gROOT
    Definition TROOT.h:406
    R__EXTERN TRandom * gRandom
    Definition TRandom.h:62
    A TTree is a list of TBranches.
    Definition TBranch.h:89
    virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
    Read all leaves of entry and return total number of bytes read.
    Definition TBranch.cxx:1652
    virtual void SetAddress(void *add)
    Set address of this branch.
    Definition TBranch.cxx:2620
    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
    virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
    Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
    Definition TRandom.cxx:274
    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
    virtual Double_t Rndm()
    Machine independent random number generator.
    Definition TRandom.cxx:552
    A TTree represents a columnar dataset.
    Definition TTree.h:79
    virtual TBranch * GetBranch(const char *name)
    Return pointer to the branch with the given name in this tree or its friends.
    Definition TTree.cxx:5275
    virtual void Show(Long64_t entry=-1, Int_t lenmax=20)
    Print values of all active leaves for entry.
    Definition TTree.cxx:9318
    virtual Long64_t GetEntries() const
    Definition TTree.h:460
    virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
    Read all branches of entry and return total number of bytes read.
    Definition TTree.cxx:5618
    virtual void StartViewer()
    Start the TTreeViewer on this tree.
    Definition TTree.cxx:9392
    auto * m
    Definition textangle.C:8
Rene Brun

Definition in file tree4.C.