Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
tree123_clonesarray.C File Reference

Detailed Description

View in nbviewer Open in SWAN
How to write a TClonesArray to a TTree

The following tests can be run Interactive tests

Root > .x tree123_clonesarray.C //no-split interpreted
Root > .x tree123_clonesarray.C(1) //split interpreted
Root > .x tree123_clonesarray.C++ //no-split compiled
Root > .x tree123_clonesarray.C++(1) //split compiled

Batch tests: same as above but with no graphics

root -b -q tree123_clonesarray.C
root -b -q tree123_clonesarray.C++
root -b -q "tree123_clonesarray.C(1)"
root -b -q "tree123_clonesarray.C++(1)"
#include "TFile.h"
#include "TClonesArray.h"
#include "TH2.h"
#include "TLine.h"
#include "TTree.h"
#include "TBenchmark.h"
#include "TRandom.h"
void write_clonesarray(Int_t split)
{
// Generate a Tree with a TClonesArray
// The array can be split or not
TFile f("clonesarray.root", "recreate");
f.SetCompressionLevel(1); //try level 2 also
TTree T("T", "test clonesarray");
TClonesArray *arr = new TClonesArray("TLine");
TClonesArray &ar = *arr;
T.Branch("tcl", &arr, 256000, split);
// By default a TClonesArray is created with its BypassStreamer bit set.
// However, because TLine has a custom Streamer, this bit was reset
// by TTree::Branch above. We set again this bit because the current
// version of TLine uses the automatic Streamer.
// BypassingStreamer saves space and time.
for (Int_t ev=0; ev<10000; ev++) {
ar.Clear();
Int_t nlines = Int_t(gRandom->Gaus(50,10));
if(nlines < 0)
nlines = 1;
for (Int_t i=0;i<nlines;i++) {
Float_t x1 = gRandom->Rndm();
Float_t y1 = gRandom->Rndm();
Float_t x2 = gRandom->Rndm();
Float_t y2 = gRandom->Rndm();
new(ar[i]) TLine(x1, y1, x2, y2);
}
T.Fill();
}
T.Print();
T.Write();
}
void read_clonesarray()
{
// read file generated by write_clonesarray
// loop on all entries.
// histogram center of lines
auto f = new TFile("clonesarray.root");
auto T = f->Get<TTree>("T");
auto h2 = new TH2F("h2", "center of lines", 40, 0, 1, 40, 0, 1);
auto arr = new TClonesArray("TLine");
T->GetBranch("tcl")->SetAutoDelete(kFALSE);
T->SetBranchAddress("tcl", &arr);
Long64_t nentries = T->GetEntries();
for (Long64_t ev=0; ev<nentries; ev++) {
arr->Clear();
T->GetEntry(ev);
Int_t nlines = arr->GetEntriesFast();
for (Int_t i=0; i<nlines; i++) {
TLine *line = (TLine*)arr->At(i);
h2->Fill(0.5 * (line->GetX1() + line->GetX2()), 0.5 * (line->GetY1() + line->GetY2()));
}
}
h2->Draw("lego");
}
void tree123_clonesarray(Int_t split = 0)
{
gBenchmark->Start("clonesarray");
write_clonesarray(split);
read_clonesarray();
gBenchmark->Show("clonesarray");
}
#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
Author
Rene Brun

Definition in file tree123_clonesarray.C.