Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
tree123_clonesarray.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook
4/// How to write a TClonesArray to a TTree
5///
6/// The following tests can be run
7/// Interactive tests
8/// ~~~
9/// Root > .x tree123_clonesarray.C //no-split interpreted
10/// Root > .x tree123_clonesarray.C(1) //split interpreted
11/// Root > .x tree123_clonesarray.C++ //no-split compiled
12/// Root > .x tree123_clonesarray.C++(1) //split compiled
13/// ~~~
14/// Batch tests: same as above but with no graphics
15/// ~~~{.bash}
16/// root -b -q tree123_clonesarray.C
17/// root -b -q tree123_clonesarray.C++
18/// root -b -q "tree123_clonesarray.C(1)"
19/// root -b -q "tree123_clonesarray.C++(1)"
20/// ~~~
21/// \macro_code
22///
23/// \author Rene Brun
24
25#include "TFile.h"
26#include "TClonesArray.h"
27#include "TH2.h"
28#include "TLine.h"
29#include "TTree.h"
30#include "TBenchmark.h"
31#include "TRandom.h"
32
33void write_clonesarray(Int_t split)
34{
35 // Generate a Tree with a TClonesArray
36 // The array can be split or not
37 TFile f("clonesarray.root", "recreate");
38 f.SetCompressionLevel(1); //try level 2 also
39 TTree T("T", "test clonesarray");
40 TClonesArray *arr = new TClonesArray("TLine");
41 TClonesArray &ar = *arr;
42 T.Branch("tcl", &arr, 256000, split);
43 // By default a TClonesArray is created with its BypassStreamer bit set.
44 // However, because TLine has a custom Streamer, this bit was reset
45 // by TTree::Branch above. We set again this bit because the current
46 // version of TLine uses the automatic Streamer.
47 // BypassingStreamer saves space and time.
48 arr->BypassStreamer();
49 for (Int_t ev=0; ev<10000; ev++) {
50 ar.Clear();
51 Int_t nlines = Int_t(gRandom->Gaus(50,10));
52 if(nlines < 0)
53 nlines = 1;
54 for (Int_t i=0;i<nlines;i++) {
55 Float_t x1 = gRandom->Rndm();
56 Float_t y1 = gRandom->Rndm();
57 Float_t x2 = gRandom->Rndm();
58 Float_t y2 = gRandom->Rndm();
59 new(ar[i]) TLine(x1, y1, x2, y2);
60 }
61 T.Fill();
62 }
63 T.Print();
64 T.Write();
65}
66
67void read_clonesarray()
68{
69 // read file generated by write_clonesarray
70 // loop on all entries.
71 // histogram center of lines
72 auto f = new TFile("clonesarray.root");
73 auto T = f->Get<TTree>("T");
74 auto h2 = new TH2F("h2", "center of lines", 40, 0, 1, 40, 0, 1);
75
76 auto arr = new TClonesArray("TLine");
77 T->GetBranch("tcl")->SetAutoDelete(kFALSE);
78 T->SetBranchAddress("tcl", &arr);
79 Long64_t nentries = T->GetEntries();
80 for (Long64_t ev=0; ev<nentries; ev++) {
81 arr->Clear();
82 T->GetEntry(ev);
83 Int_t nlines = arr->GetEntriesFast();
84 for (Int_t i=0; i<nlines; i++) {
85 TLine *line = (TLine*)arr->At(i);
86 h2->Fill(0.5 * (line->GetX1() + line->GetX2()), 0.5 * (line->GetY1() + line->GetY2()));
87 }
88 }
89 h2->Draw("lego");
90}
91
92void tree123_clonesarray(Int_t split = 0)
93{
94 gBenchmark->Start("clonesarray");
95 write_clonesarray(split);
96 read_clonesarray();
97 gBenchmark->Show("clonesarray");
98}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
externTBenchmark * gBenchmark
Definition TBenchmark.h:59
int nentries
externTRandom * gRandom
Definition TRandom.h:62
An array of clone (identical) objects.
void BypassStreamer(Bool_t bypass=kTRUE)
When the kBypassStreamer bit is set, the automatically generated Streamer can call directly TClass::W...
void Clear(Option_t *option="") override
Clear the clones array.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
Use the TLine constructor to create a simple line.
Definition TLine.h:22
Int_t GetEntriesFast() const
Definition TObjArray.h:58
TObject * At(Int_t idx) const override
Definition TObjArray.h:170
A TTree represents a columnar dataset.
Definition TTree.h:89
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4653
void Draw(Option_t *opt) override
Default Draw method for all objects.
Definition TTree.h:478
TLine * line