ROOT logo

From $ROOTSYS/tutorials/tree/tree0.C

// Author: Heiko.Scheit@mpi-hd.mpg.de
//
//  simple Event class example
//
// execute as: .x tree0.C++
//
//  You have to copy it first to a directory where you have write access!
//  Note that .x tree0.C cannot work with this example
//  
//
///////////////////////////////
//  Effect of ClassDef() and ClassImp() macros
//===============================================
//
// After running this macro create an instance of Det and Event
//
//   Det d;
//   Event e;
//
// now you can see the effect of the  ClassDef() and ClassImp() macros.
// (for the Det class these commands are commented!)
// For instance 'e' now knows who it is:
//
//   cout<<e.Class_Name()<<endl;
//
// whereas d does not.
//
// The methods that are added by the ClassDef()/Imp() marcro can be listed with
// .class
//   .class Event
//   .class Det
///////////////////

#include <TRandom.h>
#include <TTree.h>
#include <TCanvas.h>
#include <TStyle.h>

#include <Riostream.h>

//class Det  : public TObject  {
class Det {  // each detector gives an energy and time signal 
public:
  Double_t e; //energy
  Double_t t; //time

//  ClassDef(Det,1)
};

//ClassImp(Det)

//class Event { //TObject is not required by this example
class Event : public TObject {
public:
  
  Det a; // say there are two detectors (a and b) in the experiment
  Det b;
  ClassDef(Event,1)
};

ClassImp(Event)

void tree0() {
  // create a TTree   
  TTree *tree = new TTree("tree","treelibrated tree");
  Event *e = new Event;
  
  // create a branch with energy 
  tree->Branch("event",&e);
  
  // fill some events with random numbers
  Int_t nevent=10000;
  for (Int_t iev=0;iev<nevent;iev++) {
    if (iev%1000==0) cout<<"Processing event "<<iev<<"..."<<endl;
    
    Float_t ea,eb;
    gRandom->Rannor(ea,eb); // the two energies follow a gaus distribution
    e->a.e=ea;
    e->b.e=eb;
    e->a.t=gRandom->Rndm();  // random
    e->b.t=e->a.t + gRandom->Gaus(0.,.1);  // identical to a.t but a gaussian
					   // 'resolution' was added with sigma .1

    tree->Fill();  // fill the tree with the current event
  }  

  // start the viewer
  // here you can investigate the structure of your Event class
  tree->StartViewer();

  //gROOT->SetStyle("Plain");   // uncomment to set a different style
  gStyle->SetPalette(1);        // use precomputed color palette 1

  // now draw some tree variables 
  TCanvas *c1 = new TCanvas();
  c1->Divide(2,2);
  c1->cd(1); 
  tree->Draw("a.e");  //energy of det a
  tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same");  // same but with condition on energy b; scaled by 3
  c1->cd(2); 
  tree->Draw("b.e:a.e","","colz");        // one energy against the other
  c1->cd(3); 
  tree->Draw("b.t","","e");    // time of b with errorbars
  tree->Draw("a.t","","same"); // overlay time of detector a
  c1->cd(4);
  tree->Draw("b.t:a.t");       // plot time b again time a

  cout<<endl;
  cout<<"You can now examine the structure of your tree in the TreeViewer"<<endl;
  cout<<endl;
}

 tree0.C:1
 tree0.C:2
 tree0.C:3
 tree0.C:4
 tree0.C:5
 tree0.C:6
 tree0.C:7
 tree0.C:8
 tree0.C:9
 tree0.C:10
 tree0.C:11
 tree0.C:12
 tree0.C:13
 tree0.C:14
 tree0.C:15
 tree0.C:16
 tree0.C:17
 tree0.C:18
 tree0.C:19
 tree0.C:20
 tree0.C:21
 tree0.C:22
 tree0.C:23
 tree0.C:24
 tree0.C:25
 tree0.C:26
 tree0.C:27
 tree0.C:28
 tree0.C:29
 tree0.C:30
 tree0.C:31
 tree0.C:32
 tree0.C:33
 tree0.C:34
 tree0.C:35
 tree0.C:36
 tree0.C:37
 tree0.C:38
 tree0.C:39
 tree0.C:40
 tree0.C:41
 tree0.C:42
 tree0.C:43
 tree0.C:44
 tree0.C:45
 tree0.C:46
 tree0.C:47
 tree0.C:48
 tree0.C:49
 tree0.C:50
 tree0.C:51
 tree0.C:52
 tree0.C:53
 tree0.C:54
 tree0.C:55
 tree0.C:56
 tree0.C:57
 tree0.C:58
 tree0.C:59
 tree0.C:60
 tree0.C:61
 tree0.C:62
 tree0.C:63
 tree0.C:64
 tree0.C:65
 tree0.C:66
 tree0.C:67
 tree0.C:68
 tree0.C:69
 tree0.C:70
 tree0.C:71
 tree0.C:72
 tree0.C:73
 tree0.C:74
 tree0.C:75
 tree0.C:76
 tree0.C:77
 tree0.C:78
 tree0.C:79
 tree0.C:80
 tree0.C:81
 tree0.C:82
 tree0.C:83
 tree0.C:84
 tree0.C:85
 tree0.C:86
 tree0.C:87
 tree0.C:88
 tree0.C:89
 tree0.C:90
 tree0.C:91
 tree0.C:92
 tree0.C:93
 tree0.C:94
 tree0.C:95
 tree0.C:96
 tree0.C:97
 tree0.C:98
 tree0.C:99
 tree0.C:100
 tree0.C:101
 tree0.C:102
 tree0.C:103
 tree0.C:104
 tree0.C:105
 tree0.C:106
 tree0.C:107
 tree0.C:108
 tree0.C:109
 tree0.C:110
 tree0.C:111
 tree0.C:112
 tree0.C:113