root courses1 the root system a data access & analysis framework 4-5-6 february 2003 ren é...
TRANSCRIPT
ROOT courses 1
The ROOT SystemA Data Access & Analysis Framework
4-5-6 February 2003René Brun/EP
http://root.cern.ch
Trees
R.Brun LCG ROOT corses 2
R.Brun LCG ROOT corses 3
Memory <--> TreeEach Node is a branch in the Tree
0123456789101112131415161718
T.Fill()
T.GetEntry(6)
T
Memory
R.Brun LCG ROOT corses 4
Tree Creation Example
A few lines of codeto create a Tree
for structuresthat may be
very complex
R.Brun LCG ROOT corses 5
8 Branches of T
8 leaves of branchElectrons
A double-clickto histogram
the leaf
R.Brun LCG ROOT corses 6
The Tree Viewer & Analyzer
A very powerful classsupporting
complex cuts,event lists,
1-d,2-d, 3-d viewsparallelism
R.Brun LCG ROOT corses 7
Tree Friends
0123456789101112131415161718
0123456789101112131415161718
0123456789101112131415161718
Public
read
Public
read
User
Write
Entry # 8
R.Brun LCG ROOT corses 8
Tree Friends
Root > TFile f1(“tree1.root”);
Root > tree.AddFriend(“tree2”,“tree2.root”)
Root > tree.AddFriend(“tree3”,“tree3.root”);
Root > tree.Draw(“x:a”,”k<c”);
Root > tree.Draw(“x:tree2.x”,”sqrt(p)<b”);
x
Processing timeindependent of thenumber of friendsunlike table joins
in RDBMS
Collaboration-widepublic read
Analysis groupprotected
userprivate
R.Brun LCG ROOT corses 9
Chains of Trees A TChain is a collection of Trees. Same semantics for TChains and TTrees
root > .x h1chain.C root > chain.Process(“h1analysis.C”)
{ //creates a TChain to be used by the h1analysis.C class //the symbol H1 must point to a directory where the H1 data sets //have been installed TChain chain("h42"); chain.Add("$H1/dstarmb.root"); chain.Add("$H1/dstarp1a.root"); chain.Add("$H1/dstarp1b.root"); chain.Add("$H1/dstarp2.root");}
R.Brun LCG ROOT corses 10
Ntuples and Trees
Ntuples support PAW-like ntuples and functions PAW ntuples/histograms can be imported
Trees Extension of Ntuples for Objects Collection of branches (branch has its own
buffer) Can input partial Event Can have several Trees in parallel
Chains = collections of Trees
R.Brun LCG ROOT corses 11
Why Trees ? Any object deriving from TObject can be written to a file
with an associated key with object.Write() However each key has an overhead in the directory
structure in memory (about 60 bytes). Object.Write is very convenient for objects like histograms, detector objects, calibrations, but not for event objects.but not for event objects.
R.Brun LCG ROOT corses 12
Why Trees ?
Trees have been designed to support very large collections of objects. The overhead in memory is in general less than 4 bytes per entry.
Trees allow direct and random access to any entry (sequential access is the best)
Trees have branches and leaves. One can read a subset of all branches. This can speed-up considerably the data analysis processes.
R.Brun LCG ROOT corses 13
Adding a Branch
Branch name Class name Address of the pointer to the Object
(descendant of TObject) Buffer size (default = 32,000) Split level (default = 1)
Event *event = new Event();myTree->Branch(”eBranch","Event",&event,64000,1);
Many Branch constructorsOnly a fewshown here
R.Brun LCG ROOT corses 14
Splitting a Branch
Setting the split level (default = 1)
Split level = 0 Split level = 1
Example:
tree->Branch("EvBr","Event",&ev,64000,0);
R.Brun LCG ROOT corses 15
Adding Branches with a List of Variables
Branch name Address: the address of the first
item of a structure. Leaflist: all variable names and
types Order the variables according to
their size
ExampleTBranch *b = tree->Branch ("Ev_Branch",&event,
"ntrack/I:nseg:nvtex:flag/i:temp/F");
R.Brun LCG ROOT corses 16
Why Trees ? PAW ntuples are a special case of Trees. Trees are designed to work with complex event
objects. High level functions like TTree::Draw loop on all
entries with selection expressions. Trees can be browsed via TBrowser Trees can be analized via TTreeViewer
The PROOF system is designed to process chains
of Trees in parallel in a GRID environment
R.Brun LCG ROOT corses 17
Create a TTree Object
A tree is a list of branches. The TTree Constructor:
Tree Name (e.g. "myTree") Tree Title
TTree *tree = new TTree("T","A ROOT tree");
R.Brun LCG ROOT corses 18
ROOT I/O - Split - multifile
Object in
memory
Object in
memory
Object in
memory
Object in
memory
Object in
memory
Streamer
File1
File2
File3
Object in memory
TAGs
Tapes
R.Brun LCG ROOT corses 19
Serial mode
Split mode
R.Brun LCG ROOT corses 20
Structure designed to
supportvery large DBs
R.Brun LCG ROOT corses 21
The Event class
class Event : public TObject {
private: char fType[20]; //event type Int_t fNtrack; //Number of tracks Int_t fNseg; //Number of track segments Int_t fNvertex; UInt_t fFlag; Float_t fTemperature; Int_t fMeasures[10]; Float_t fMatrix[4][4]; Float_t *fClosestDistance; //[fNvertex] EventHeader fEvtHdr; TClonesArray *fTracks; //->array with all tracks TRefArray *fHighPt; //array of High Pt tracks only TRefArray *fMuons; //array of Muon tracks only TRef fLastTrack; //reference pointer to last track TH1F *fH; //->
class EventHeader {
private: Int_t fEvtNum; Int_t fRun; Int_t fDate;
See $ROOTSYS/test/Event.h
R.Brun LCG ROOT corses 22
The Track class
class Track : public TObject {
private: Float_t fPx; //X component of the momentum Float_t fPy; //Y component of the momentum Float_t fPz; //Z component of the momentum Float_t fRandom; //A random track quantity Float_t fMass2; //The mass square of this particle Float_t fBx; //X intercept at the vertex Float_t fBy; //Y intercept at the vertex Float_t fMeanCharge; //Mean charge deposition of all hits Float_t fXfirst; //X coordinate of the first point Float_t fXlast; //X coordinate of the last point Float_t fYfirst; //Y coordinate of the first point Float_t fYlast; //Y coordinate of the last point Float_t fZfirst; //Z coordinate of the first point Float_t fZlast; //Z coordinate of the last point Float_t fCharge; //Charge of this track Float_t fVertex[3]; //Track vertex position Int_t fNpoint; //Number of points for this track Short_t fValid; //Validity criterion
R.Brun LCG ROOT corses 23
Event Builder
void Event::Build(Int_t ev, Int_ntrack, Float_t ptmin) { Clear(); ……….. for (Int_t t = 0; t < ntrack; t++) AddTrack(random,ptmin);}
Track *Event::AddTrack(Float_t random, Float_t ptmin){ // Add a new track to the list of tracks for this event. // To avoid calling the very time consuming operator new for each track, // the standard but not well know C++ operator "new with placement" // is called. If tracks[i] is 0, a new Track object will be created // otherwise the previous Track[i] will be overwritten.
TClonesArray &tracks = *fTracks; Track *track = new(tracks[fNtrack++]) Track(random); //Save reference to last Track in the collection of Tracks fLastTrack = track; //Save reference in fHighPt if track is a high Pt track if (track->GetPt() > ptmin) fHighPt->Add(track); //Save reference in fMuons if track is a muon candidate if (track->GetMass2() < 0.11) fMuons->Add(track); return track;}
R.Brun LCG ROOT corses 24
Tree example Event (write)void demoe(int nevents) { //load shared lib with the Event class gSystem->Load("$ROOTSYS/test/libEvent"); //create a new ROOT file TFile f("demoe.root",”new"); //Create a ROOT Tree with one single top level branch int split = 99; //try also split=1 and split=0 int bufsize = 16000; Event *event = new Event; TTree T("T","Event demo tree"); T.Branch("event","Event",&event,bufsize,split); //Build Event in a loop and fill the Tree for (int i=0;i<nevents;i++) { event->Build(i); T.Fill(); } T.Print(); //Print Tree statistics T.Write(); //Write Tree header to the file
}
All the examples can be executedwith CINTor the compiler
root > .x demoe.Croot > .x demoe.C++
R.Brun LCG ROOT corses 25
Tree example Event (read 1)
void demoer() { //load shared lib with the Event class gSystem->Load("$ROOTSYS/test/libEvent"); //connect ROOT file TFile *f = new TFile("demoe.root"); //Read Tree header and set top branch address Event *event = 0; TTree *T = (TTree*)f->Get("T"); T->SetBranchAddress("event",&event); //Loop on events and fill an histogram TH1F *h = new TH1F("hntrack","Number of tracks",100,580,620); int nevents = (int)T->GetEntries(); for (int i=0;i<nevents;i++) { T->GetEntry(i); h->Fill(event->GetNtrack()); } h->Draw();
}
Rebuild the full eventin memory
R.Brun LCG ROOT corses 26
Tree example Event (read 2)
void demoer2() { //load shared lib with the Event class gSystem->Load("$ROOTSYS/test/libEvent"); //connect ROOT file TFile *f = new TFile("demoe.root"); //Read Tree header and set top branch address Event *event = 0; TTree *T = (TTree*)f->Get("T"); T->SetBranchAddress("event",&event); Tbranch *bntrack = T->GetBranch(“fNtrack”); //Loop on events and fill an histogram TH1F *h = new TH1F("hntrack","Number of tracks",100,580,620); int nevents = (int)T->GetEntries(); for (int i=0;i<nevents;i++) { bntrack->GetEntry(i); h->Fill(event->GetNtrack()); } h->Draw();
}
Read onlyone branch
Much faster !
R.Brun LCG ROOT corses 27
Tree example Event (read 3)
void demoer3() { //load shared lib with the Event class gSystem->Load("$ROOTSYS/test/libEvent"); //connect ROOT file TFile *f = new TFile("demoe.root"); //Read Tree header TTree *T = (TTree*)f->Get("T"); //Histogram number of tracks via the TreePlayer T->Draw(“event->GetNtrack()”);
}
R.Brun LCG ROOT corses 28
Writing CMS PSimHit in a Tree
void demo3() { //create a new ROOT file TFile f("demo3.root","recreate"); //Create a ROOT Tree with one single top level branch int split = 99; //you can try split=1 and split=0 int bufsize = 16000; PSimHit *hit = 0; TTree T("T","CMS demo tree"); T.Branch("hit","PSimHit",&hit,bufsize,split); //Create hits in a loop and fill the Tree TRandom r; for (int i=0;i<50000;i++) { delete hit; Local3DPoint pentry(r.Gaus(0,1), r.Gaus(0,1), r.Gaus(0,10)); Local3DPoint pexit (r.Gaus(0,3), r.Gaus(0,3), r.Gaus(50,20)); float pabs = 100*r.Rndm(); float tof = r.Gaus(1e-6,1e-8); float eloss= r.Landau(1e-3,1e-7); int ptype = i%2; int detId = i%20; int trackId= i%100; hit = new PSimHit(pentry,pexit,pabs,tof,eloss,ptype,detId,trackId); T.Fill(); } T.Print(); //Print Tree statistics T.Write(); //Write Tree header to the file}
R.Brun LCG ROOT corses 29
Browsing the PSimHit Treesplit = 0
*Tree :T : CMS demo tree **Entries : 50000 : Total = 4703775 bytes File Size = 2207143 ** : : Tree compression factor = 2.13 ********************************************************************************Br 0 :hit : **Entries : 50000 : Total Size= 4703775 bytes File Size = 2207143 **Baskets : 295 : Basket Size= 16000 bytes Compression= 2.13 **............................................................................*
1 branch only
R.Brun LCG ROOT corses 30
Browsing the PSimHit Treesplit = 1
*******************************************************************************Tree :T : CMS demo tree **Entries : 50000 : Total = 5258415 bytes File Size = 2021907 ** : : Tree compression factor = 2.60 ********************************************************************************Branch :hit **Entries : 50000 : BranchElement (see below) **............................................................................**Br 0 :TObject : **Entries : 50000 : Total Size= 697816 bytes File Size = 79579 **Baskets : 56 : Basket Size= 16000 bytes Compression= 8.77 **............................................................................**Br 1 :theEntryPoint : **Entries : 50000 : Total Size= 1704437 bytes File Size = 750090 **Baskets : 119 : Basket Size= 16000 bytes Compression= 2.27 **............................................................................**Br 2 :theExitPoint : **Entries : 50000 : Total Size= 1704318 bytes File Size = 744721 **Baskets : 119 : Basket Size= 16000 bytes Compression= 2.29 **............................................................................**Br 3 :thePabs : **Entries : 50000 : Total Size= 191988 bytes File Size = 170871 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.12 **............................................................................**Br 4 :theTof : **Entries : 50000 : Total Size= 191976 bytes File Size = 145548 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.32 **............................................................................**Br 5 :theEnergyLoss : **Entries : 50000 : Total Size= 191964 bytes File Size = 122761 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.56 **............................................................................**Br 6 :theParticleType : **Entries : 50000 : Total Size= 191988 bytes File Size = 1860 **Baskets : 12 : Basket Size= 16000 bytes Compression= 103.22 **............................................................................**Br 7 :theDetUnitId : **Entries : 50000 : Total Size= 191952 bytes File Size = 2298 **Baskets : 12 : Basket Size= 16000 bytes Compression= 83.53 **............................................................................**Br 8 :theTrackId : **Entries : 50000 : Total Size= 191976 bytes File Size = 4179 **Baskets : 12 : Basket Size= 16000 bytes Compression= 45.94 **............................................................................*
9 branches
R.Brun LCG ROOT corses 31
Browsing the PSimHit Treesplit = 99
*******************************************************************************Tree :T : CMS demo tree *
*Entries : 50000 : Total = 2687592 bytes File Size = 1509041 ** : : Tree compression factor = 1.78 ********************************************************************************Branch :hit **Entries : 50000 : BranchElement (see below) **............................................................................**Br 0 :fUniqueID : **Entries : 50000 : Total Size= 191964 bytes File Size = 1272 **Baskets : 12 : Basket Size= 16000 bytes Compression= 150.92 **............................................................................**Br 1 :fBits : **Entries : 50000 : Total Size= 191964 bytes File Size = 1260 **Baskets : 12 : Basket Size= 16000 bytes Compression= 152.35 **............................................................................**Br 2 :theEntryPoint : **Entries : 50000 : Total Size= 0 bytes File Size = 0 **Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 **............................................................................**Br 3 :theEntryPoint.theVector.theX : **Entries : 50000 : Total Size= 191952 bytes File Size = 177959 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.08 **............................................................................**Br 4 :theEntryPoint.theVector.theY : **Entries : 50000 : Total Size= 191952 bytes File Size = 177934 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.08 **............................................................................**Br 5 :theEntryPoint.theVector.theZ : **Entries : 50000 : Total Size= 191952 bytes File Size = 178312 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.08 **............................................................................**Br 6 :theExitPoint : **Entries : 50000 : Total Size= 0 bytes File Size = 0 **Baskets : 0 : Basket Size= 16000 bytes Compression= 1.00 **............................................................................**Br 7 :theExitPoint.theVector.theX : **Entries : 50000 : Total Size= 191988 bytes File Size = 178060 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.08 **............................................................................**Br 8 :theExitPoint.theVector.theY : **Entries : 50000 : Total Size= 191988 bytes File Size = 178072 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.08 **............................................................................**Br 9 :theExitPoint.theVector.theZ : **Entries : 50000 : Total Size= 191988 bytes File Size = 168655 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.14 **............................................................................**Br 10 :thePabs : **Entries : 50000 : Total Size= 191988 bytes File Size = 170871 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.12 **............................................................................**Br 11 :theTof : **Entries : 50000 : Total Size= 191976 bytes File Size = 145548 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.32 **............................................................................**Br 12 :theEnergyLoss : **Entries : 50000 : Total Size= 191964 bytes File Size = 122761 **Baskets : 12 : Basket Size= 16000 bytes Compression= 1.56 **............................................................................**Br 13 :theParticleType : **Entries : 50000 : Total Size= 191988 bytes File Size = 1860 **Baskets : 12 : Basket Size= 16000 bytes Compression= 103.22 **............................................................................**Br 14 :theDetUnitId : **Entries : 50000 : Total Size= 191952 bytes File Size = 2298 **Baskets : 12 : Basket Size= 16000 bytes Compression= 83.53 *
*............................................................................**Br 15 :theTrackId : **Entries : 50000 : Total Size= 191976 bytes File Size = 4179 **Baskets : 12 : Basket Size= 16000 bytes Compression= 45.94 **............................................................................* 16 branches
Double clickproduces
this histogram
R.Brun LCG ROOT corses 32
Collections of Hits
A more realistic Tree will have A collection of Detectors Each detector one or more collection of
hits
R.Brun LCG ROOT corses 33
36 branchesin Tree T
19 leaves in branch fDele
R.Brun LCG ROOT corses 34
8 Branches of T
8 leaves of branchElectrons A double-click
to histogramthe leaf
R.Brun LCG ROOT corses 35
The Tree Viewer & Analyzer
A very powerful classsupporting
complex cuts,event lists,
1-d,2-d, 3-d viewsparallelism
R.Brun LCG ROOT corses 36
Chains
Scenario:
Perform an analysis using multiple ROOT files. All files are of the same structure and have the same tree.
Chainsare collections of
chains or files
Chains can be builtautomatically by quering
the run/file catalog
R.Brun LCG ROOT corses 37
The “No Shared Library” case
There are many applications for which it does not make sense to read data without the code of the corresponding classes.
In true OO, you want to exploit Data Hiding and rely on the functional interface.
However, there are also cases where the functional interface is not necessary (PAW ntuples).
It is nice to be able to browse any type of file without any code. May be you cannot do much, but it gives some confidence that you can always read your data sets.
We have seen a religious debate on this subject. Our conclusion was that we had to support these two
modes of operation. Support for the “No Shared Lib case” is non trivial
R.Brun LCG ROOT corses 38
read/query Trees without the classes
R.Brun LCG ROOT corses 39
TFile::MakeProjectGenerate the classes
header filesCompile them
make a shared liblink the shared lib
R.Brun LCG ROOT corses 40
TFile::MakeProject
All necessaryheader filesare included
Commentspreserved
Can do I/OInspect
Browse,etc