ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcl.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// How to write a TClonesArray to a TTree
4 ///
5 /// The following tests can be run
6 /// Interactive tests
7 /// ~~~
8 /// Root > .x tcl.C //no-split interpreted
9 /// Root > .x tcl.C(1) //split interpreted
10 /// Root > .x tcl.C++ //no-split compiled
11 /// Root > .x tcl.C++(1) //split compiled
12 /// ~~~
13 /// Batch tests: same as above but with no graphics
14 /// ~~~{.bash}
15 /// root -b -q tcl.C
16 /// root -b -q tcl.C++
17 /// root -b -q "tcl.C(1)"
18 /// root -b -q "tcl.C++(1)"
19 /// ~~~
20 /// \macro_code
21 ///
22 /// \author Rene Brun
23 
24 #include "TFile.h"
25 #include "TClonesArray.h"
26 #include "TH2.h"
27 #include "TLine.h"
28 #include "TTree.h"
29 #include "TBenchmark.h"
30 #include "TRandom.h"
31 
32 void tclwrite(Int_t split)
33 {
34 // Generate a Tree with a TClonesArray
35 // The array can be split or not
36  TFile f("tcl.root","recreate");
37  f.SetCompressionLevel(1); //try level 2 also
38  TTree T("T","test tcl");
39  TClonesArray *arr = new TClonesArray("TLine");
40  TClonesArray &ar = *arr;
41  T.Branch("tcl",&arr,256000,split);
42  //By default a TClonesArray is created with its BypassStreamer bit set.
43  //However, because TLine has a custom Streamer, this bit was reset
44  //by TTree::Branch above. We set again this bit because the current
45  //version of TLine uses the automatic Streamer.
46  //BypassingStreamer saves space and time.
47  arr->BypassStreamer();
48  for (Int_t ev=0;ev<10000;ev++) {
49  ar.Clear();
50  Int_t nlines = Int_t(gRandom->Gaus(50,10));
51  if(nlines < 0) nlines = 1;
52  for (Int_t i=0;i<nlines;i++) {
53  Float_t x1 = gRandom->Rndm();
54  Float_t y1 = gRandom->Rndm();
55  Float_t x2 = gRandom->Rndm();
56  Float_t y2 = gRandom->Rndm();
57  new(ar[i]) TLine(x1,y1,x2,y2);
58  }
59  T.Fill();
60  }
61  T.Print();
62  T.Write();
63 }
64 
65 void tclread()
66 {
67 // read file generated by tclwrite
68 // loop on all entries.
69 // histogram center of lines
70  TFile *f = new TFile("tcl.root");
71  TTree *T = (TTree*)f->Get("T");
72  TH2F *h2 = new TH2F("h2","center of lines",40,0,1,40,0,1);
73 
74  TClonesArray *arr = new TClonesArray("TLine");
75  T->GetBranch("tcl")->SetAutoDelete(kFALSE);
76  T->SetBranchAddress("tcl",&arr);
77  Long64_t nentries = T->GetEntries();
78  for (Long64_t ev=0;ev<nentries;ev++) {
79  arr->Clear();
80  T->GetEntry(ev);
81  Int_t nlines = arr->GetEntriesFast();
82  for (Int_t i=0;i<nlines;i++) {
83  TLine *line = (TLine*)arr->At(i);
84  h2->Fill(0.5*(line->GetX1()+line->GetX2()), 0.5*(line->GetY1()+line->GetY2()));
85  }
86  }
87  h2->Draw("lego");
88 }
89 
90 void tcl(Int_t split=0)
91 {
92  gBenchmark->Start("tcl");
93  tclwrite(split);
94  tclread();
95  gBenchmark->Show("tcl");
96 }
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
Double_t GetX1() const
Definition: TLine.h:67
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:235
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
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:5144
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
TFile * f
void BypassStreamer(Bool_t bypass=kTRUE)
When the kBypassStreamer bit is set, the automatically generated Streamer can call directly TClass::W...
TTree * T
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
virtual void SetCompressionLevel(Int_t level=1)
See comments for function SetCompressionSettings.
Definition: TFile.cxx:2131
Double_t GetY2() const
Definition: TLine.h:70
virtual void Clear(Option_t *option="")
Clear the clones array.
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:4803
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:8780
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
A simple line.
Definition: TLine.h:41
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Double_t GetX2() const
Definition: TLine.h:68
int nentries
Definition: THbookFile.cxx:89
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1623
An array of clone (identical) objects.
Definition: TClonesArray.h:32
Double_t GetY1() const
Definition: TLine.h:69
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
virtual void SetAutoDelete(Bool_t autodel=kTRUE)
Set the automatic delete bit.
Definition: TBranch.cxx:2083