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"
{
// 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");
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.
arr->BypassStreamer();
for (Int_t ev=0; ev<10000; ev++) {
ar.Clear();
if(nlines < 0)
nlines = 1;
for (Int_t i=0;i<nlines;i++) {
new(ar[i]) TLine(x1, y1, x2, y2);
}
T.Fill();
}
T.Print();
T.Write();
}
{
// 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 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);
GetY2()));
}
}
h2->Draw("lego");
}
void tree123_clonesarray(Int_t split = 0)
{
gBenchmark->Start("clonesarray");
gBenchmark->Show("clonesarray");
}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
long long Long64_t
Definition RtypesCore.h:69
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
int nentries
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
An array of clone (identical) objects.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
Use the TLine constructor to create a simple line.
Definition TLine.h:22
Double_t GetY1() const
Definition TLine.h:52
Double_t GetX2() const
Definition TLine.h:51
void Print(Option_t *option="") const override
Dump this line with its attributes.
Definition TLine.cxx:419
Double_t GetX1() const
Definition TLine.h:50
Double_t GetY2() const
Definition TLine.h:53
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:275
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
A TTree represents a columnar dataset.
Definition TTree.h:84
TLine * line
Author
Rene Brun

Definition in file tree123_clonesarray.C.