The ROOT System A Data Access Analysis Framework

  • Slides: 63
Download presentation
The ROOT System A Data Access & Analysis Framework Input/Output ROOT courses 1

The ROOT System A Data Access & Analysis Framework Input/Output ROOT courses 1

R. Brun RT 2003 ROOT Input/Output 2

R. Brun RT 2003 ROOT Input/Output 2

ROOT + RDBMS Model ROOT files histograms Oracle My. SQL Event Store Calibrations Run/File

ROOT + RDBMS Model ROOT files histograms Oracle My. SQL Event Store Calibrations Run/File Catalog Trees Geometries R. Brun RT 2003 ROOT Input/Output 3

Object Persistency (in a nutshell) n n n Two I/O modes supported (Keys and

Object Persistency (in a nutshell) n n n Two I/O modes supported (Keys and Trees). Key access: simple object streaming mode. n A ROOT file is like a Unix directory tree n Very convenient for objects like histograms, geometries, mag. field, calibrations Trees n A generalization of ntuples to objects n Designed for storing events n split and no split modes n query processor Chains: Collections of files containing Trees ROOT files are self-describing Interfaces with RDBMS also available Access to remote files (RFIO, DCACHE, GRID) ROOT Input/Output 4 n R. Brun RT 2003

ROOT I/O : An Example Program Writing demoh. C TFile f(“example. root”, ”new”); TH

ROOT I/O : An Example Program Writing demoh. C TFile f(“example. root”, ”new”); TH 1 F h(“h”, ”My histogram”, 100, -3, 3); h. Fill. Random(“gaus”, 5000); h. Write(); Program Reading TFile f(“example. root”); demohr. C TH 1 F *h = (TH 1 F*)f. Get(“h”): h->Draw(); f. Map(); 20010831/171903 20010831/171941 20010831/171946 R. Brun RT 2003 At: 64 At: 154 At: 607 At: 2971 At: 3067 At: 3123 N=90 N=453 N=2364 N=96 N=56 N=1 TFile TH 1 F Streamer. Info Keys. List Free. Segments END CX = ROOT Input/Output 2. 09 3. 25 5

All what you need to known to navigate in a ROOT file R. Brun

All what you need to known to navigate in a ROOT file R. Brun RT 2003 ROOT Input/Output 6

ROOT files can be structured like a Unix file system R. Brun RT 2003

ROOT files can be structured like a Unix file system R. Brun RT 2003 ROOT Input/Output 7

A Root file pippa. root with two levels of directories R. Brun RT 2003

A Root file pippa. root with two levels of directories R. Brun RT 2003 ROOT Input/Output Objects in directory /pippa/DM/CJ eg: /pippa/DM/CJ/h 15 8

Browsing the file root [0] TFile f("demo 1. root") root [1] TBrowser b root

Browsing the file root [0] TFile f("demo 1. root") root [1] TBrowser b root [2] f. ls(); TFile* demo 1. root KEY: PSim. Hit hit 1; 1 KEY: PSim. Hit hit 2; 1 20011008/091050 At: 64 20011008/091050 At: 150 Address = 278 Nbytes = -27 20011008/091050 At: 305 20011008/091050 At: 445 20011008/091050 At: 585 20011008/091050 At: 1537 20011008/091050 At: 1601 root [3] f. Map(); root [4] hit 2. Dump(); the. Entry. Point. the. Vector. the. X the. Entry. Point. the. Vector. the. Y the. Entry. Point. the. Vector. the. Z the. Exit. Point. the. Vector. the. X the. Exit. Point. the. Vector. the. Y the. Exit. Point. the. Vector. the. Z the. Pabs the. Tof the. Energy. Loss the. Particle. Type the. Det. Unit. Id the. Track. Id f. Unique. ID f. Bits R. Brun RT 2003 ->874 a 01 c 1 2 3 ->874 a 030 10 20 30 41 1. 67 e-08 0. 00578 12 67 1234 0 50331648 N=86 TFile N=128 Keys. List =====G A P====== N=140 PSim. Hit N=952 Streamer. Info N=64 Free. Segments N=1 END CX = position momentum Time Of Flight Energy loss object unique identifier bit field status word ROOT Input/Output The description of all classes in a file is written in one single record when the file is closed Streamer. Info 9 2. 66

Root objects or any User Object can be stored in ROOT folders and browsed

Root objects or any User Object can be stored in ROOT folders and browsed R. Brun RT 2003 ROOT Input/Output 10

LAN/WAN files n n Files and Directories n a directory holds a list of

LAN/WAN files n n Files and Directories n a directory holds a list of named objects n a file may have a hierarchy of directories (a la Unix) n ROOT files are machine independent n built-in compression Local file Remote file access via a Web server Support for local, LAN and WAN files n TFile f 1("myfile. root") n TFile f 2("http: //pcbrun. cern. ch/Renefile. root") n TFile f 3("root: //cdfsga. fnal. gov/bigfile. root") n TFile f 4("rfio: //alice/run 678. root") R. Brun RT 2003 ROOT Input/Output Remote file access via the ROOT daemon Access to a file on a mass store hpps, castor, via RFIO 11

Streaming Objects ROOT courses 12

Streaming Objects ROOT courses 12

ROOT I/O -- Sequential/Flat Object in Object memory in memory TMap. File shared memory

ROOT I/O -- Sequential/Flat Object in Object memory in memory TMap. File shared memory Streamer Ob m ject em in or y Transient Object is serialized by the Streamer No need for transient/persistent classes http TWeb. File web server Objec t. Gram TBuff TRFIOFile RFIO daemon er sockets TNet. File rootd TFile R. Brun RT 2003 ROOT Input/Output 13

Old Streamers in 2. 25 (1999) class TAxis : public TNamed, public TAtt. Axis

Old Streamers in 2. 25 (1999) class TAxis : public TNamed, public TAtt. Axis { private: Int_t Float_t TArray. F Int_t TString Bool_t f. Nbins; f. Xmin; f. Xmax; f. Xbins; f. First; f. Last; f. Time. Format; f. Time. Display; rootcint R. Brun RT 2003 void TAxis: : Streamer(TBuffer &b) { UInt_t R__s, R__c; if (b. Is. Reading()) { Version_t v = b. Read. Version(&R__s, &R__c); TNamed: : Streamer(b); TAtt. Axis: : Streamer(b); b >> f. Nbins; b >> f. Xmin; b >> f. Xmax; f. Xbins. Streamer(b); if (v > 2) { b >> f. First; b >> f. Last; } if (v > 3) { b >> f. Time. Display; f. Time. Format. Streamer(b); } else { Set. Time. Format(); } b. Check. Byte. Count(R__s, R__c, TAxis: : Is. A()); } else { R__c = b. Write. Version(TAxis: : Is. A(), k. TRUE); TNamed: : Streamer(b); TAtt. Axis: : Streamer(b); b << f. Nbins; b << f. Xmin; b << f. Xmax; f. Xbins. Streamer(b); b << f. First; b << f. Last; b << f. Time. Display; f. Time. Format. Streamer(b); b. Set. Byte. Count(R__c, k. TRUE); } ROOT Input/Output 14 }

Problems with Old Streamers n n n Experience in several large experiments has shown

Problems with Old Streamers n n n Experience in several large experiments has shown that a system based only on automatic code generation with no support for schema evolution is not a long term solution. A huge maintenance problem. In a system with several hundred (thousand) classes and as many users, it is difficult to maintain coherent shared libs to support all possible combinations when accessing collections of old data sets. A few attempts (eg in STAR) to support automatic schema evolution seen as a progress, but not sufficient. We have seen a rapidly growing request for reading data sets without having the original classes. Backward compatibility (reading an old data set with new classes) is a must. Forward compatibility (reading a new data set with old classes) also a must. R. Brun RT 2003 ROOT Input/Output 15

The ROOT solution n n n Minimize reliance on generated code. Exploit the powerful

The ROOT solution n n n Minimize reliance on generated code. Exploit the powerful CINT Object Dictionary Make the process as automatic as possible and as simple as possible. Be as efficient as with the generated code. Self-describing data sets. Come with a solution that does not prevent the move to another language in the future. Back compatibility with the original system. n Like upgrading the engine in a running car Implementing all these features was a non trivial exercise and a lot of work R. Brun RT 2003 Thanks to our huge users base for providing many use cases and testing ROOT Input/Output 16

New Streamers in 3. 00 class TAxis : public TNamed, public TAtt. Axis {

New Streamers in 3. 00 class TAxis : public TNamed, public TAtt. Axis { private: Int_t Double_t TArray. D Char_t Int_t TString Bool_t TObject f. Nbins; f. Xmin; f. Xmax; f. Xbins; *f. Xlabels; //! f. First; f. Last; f. Time. Format; f. Time. Display; *f. Parent; //! void TAxis: : Streamer(TBuffer &b) { // Stream an object of class TAxis. if (b. Is. Reading()) TAxis: : Class()->Read. Buffer(b, this); else TAxis: : Class()->Write. Buffer(b, this); } rootcint R. Brun RT 2003 ROOT Input/Output 17

The ROOT Collection Classes All ROOT collections support Polymorphism -TCollection (abstract base class) -TSeq.

The ROOT Collection Classes All ROOT collections support Polymorphism -TCollection (abstract base class) -TSeq. Collection, TList, THash. List -TMap, TEx. Map Tbuffer b; -TObj. Array collection. Streamer(b); TClones. Array is a specialized collection for arrays of objects of the same class. It minimizes the overhead due to new/delete. Much more efficient than STL for I/O (see next slides) TRef. Array is an optimized collection for persistent reference pointers. New in 3. 02 See example Event R. Brun RT 2003 ROOT Input/Output 18

Self-describing files n n n Dictionary for persistent classes written to the file when

Self-describing files n n n Dictionary for persistent classes written to the file when closing the file. ROOT files can be read by foreign readers (eg Java. Root (Tony Johnson) Support for Backward and Forward compatibility Files created in 2003 must be readable in 2015 Classes (data objects) for all objects in a file can be regenerated via TFile: : Make. Project Root >TFile f(“demo. root”); Root > f. Make. Project(“dir”, ”*”, ”new++”); R. Brun RT 2003 ROOT Input/Output 19

Showing classes in a file TFile: : Show. Streamer. Info Root > f. Show.

Showing classes in a file TFile: : Show. Streamer. Info Root > f. Show. Streamer. Info() R. Brun RT 2003 ROOT Input/Output 20

TFile: : Make. Project Generate the classes header files Compile them make a shared

TFile: : Make. Project Generate the classes header files Compile them make a shared lib link the shared lib R. Brun RT 2003 ROOT Input/Output 21

TFile: : Make. Project All necessary header files are included Comments preserved Can do

TFile: : Make. Project All necessary header files are included Comments preserved Can do I/O Inspect Browse, etc R. Brun RT 2003 ROOT Input/Output 22

ROOT conventions/extentions class TGeo. Node : public TNamed, public TGeo. Att { protected: TGeo.

ROOT conventions/extentions class TGeo. Node : public TNamed, public TGeo. Att { protected: TGeo. Volume *f. Volume; // volume associated with this TGeo. Volume *f. Mother; // mother volume Int_t f. Number; // copy number Int_t f. Novlp; // number of overlaps Int_t *f. Overlaps; //[f. Novlp] list of indices for overlapping class TGeo. Manager : public TNamed { private : Double_t f. Step; Double_t f. Safety; Double_t f. Phimin; Double_t f. Phimax; TGeo. Node. Cache *f. Cache; TVirtual. Geo. Painter *f. Painter; TList *f. Matrices; TObj. Array *f. Nodes; TGeo. Volume *f. Master. Volume; TGeo. Volume *f. Current. Volume; TGeo. Node *f. Current. Node; R. Brun RT 2003 //! step to be done from current point //! safety radius from current point // lowest range for phi cut // highest range for phi cut //! cache for physical nodes //! current painter //-> list of local transformations //-> current branch of nodes // master volume //! current node ROOT Input/Output 23

Automatic Schema Evolution R. Brun RT 2003 ROOT Input/Output 24

Automatic Schema Evolution R. Brun RT 2003 ROOT Input/Output 24

Auto Schema Evolution (2) R. Brun RT 2003 ROOT Input/Output 25

Auto Schema Evolution (2) R. Brun RT 2003 ROOT Input/Output 25

Normal Streaming mode References using C++ pointers Only one copy of each object A

Normal Streaming mode References using C++ pointers Only one copy of each object A in the graph saved to buffer TBuffer b; A. Streamer(b) R. Brun RT 2003 ROOT Input/Output 26

Normal Streaming mode References using C++ pointers TBuffer b 1; A. Streamer(b 1) Objects

Normal Streaming mode References using C++ pointers TBuffer b 1; A. Streamer(b 1) Objects in red TBuffer b 2; B. Streamer(b 2) are in b 1 and b 2 C++ pointer B A R. Brun RT 2003 ROOT Input/Output 27

Normal Streaming mode References using TRef pointers TBuffer b 1; A. Streamer(b 1) Objects

Normal Streaming mode References using TRef pointers TBuffer b 1; A. Streamer(b 1) Objects in blue TBuffer b 2; are only in b 1 B. Streamer(b 2) C++ pointer A Bz B TRef z Set pointer to z with: TRef Bz = z; Get pointer to z with: z = Bz. Get. Object() R. Brun RT 2003 ROOT Input/Output 28

TRef example: Event. h class Event : public TObject { private: char int int

TRef example: Event. h class Event : public TObject { private: char int int float Event. Header TClones. Array TRef TH 1 F public: . . . TH 1 F R. Brun RT 2003 f. Type[20]; *f. Event. Name; f. Ntrack; f. Nseg; f. Nvertex; f. Measures[10]; f. Matrix[4][4]; *f. Closest. Distance; f. Evt. Hdr; *f. Tracks; *f. High. Pt; *f. Muons; f. Last. Track; f. Web. Histogram; *f. H; //event type //run+event number in character format //Number of tracks //Number of track segments //[f. Nvertex] //->array with all tracks //array of High Pt tracks only //array of Muon tracks only //reference pointer to last track //EXEC: Get. Web. Histogram //-> *Get. Histogram() const {return f. H; } *Get. Web. Histogram(Bool_t reload=k. FALSE) const { return (TH 1 F*)f. Web. Histogram. Get. Object(reload ); } ROOT Input/Output 29

TRef, TRef. Array n n Designed as light weight entities Assume large number of

TRef, TRef. Array n n Designed as light weight entities Assume large number of TRefs per event Very fast dereferencing (direct access tables) Cannot (not designed for) find an object in a file TLong. Ref, TUUID classes proposed for references with load on demand R. Brun RT 2003 ROOT Input/Output 30

TRef/TRef. Array goals n n TRef is perfect for referencing objects like hits, clusters,

TRef/TRef. Array goals n n TRef is perfect for referencing objects like hits, clusters, tracks that may be > 10000. You would not like to have the size of a TRef bigger than the size of its referenced object ! A TRef occupies in average 2. 5 bytes in the file There is no point in providing load on demand for one single hit, cluster or track. R. Brun RT 2003 ROOT Input/Output 31

A TRef use case Session 3 Session 1 Session 2 Myclass *obj 1; TFile

A TRef use case Session 3 Session 1 Session 2 Myclass *obj 1; TFile f 2(“f 2. root”); TRef r 1 = obj 1; TRef *r 1; TFile f 1(“f 1. root”, ”new”); r 1 = (TRef*)f 2. Get(“r 1”); obj 1 ->Write(“obj 1”); TFile f 3(“f 3. root”, ”new”); TFile f 2(“f 2. root”, ”new”); TRef r 2 = *r 1; r 1. Write(“r 1”); r 2. Write(“r 2”) obj 1 r 1 TFile f 3(“f 3. root”); TRef *r 2; r 2 = (TRef*)f 3. Get(“r 2”); r 2 ->Get. Object(); = 0 TFile f 1(“f 1. root”); Myclass *obj 1; obj 1 = (Myclass*)f 1. Get(“obj 1”); r 2 ->Get. Object(); r 2 --> obj 1 f 1. root R. Brun RT 2003 r 1 r 2 f 2. root f 3. root ROOT Input/Output 32

The ROOT System A Data Access & Analysis Framework Trees ROOT courses 33

The ROOT System A Data Access & Analysis Framework Trees ROOT courses 33

R. Brun RT 2003 ROOT Input/Output 34

R. Brun RT 2003 ROOT Input/Output 34

Memory <--> Tree Each Node is a branch in the Tree Memory 0 T.

Memory <--> Tree Each Node is a branch in the Tree Memory 0 T. Get. Entry(6) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 T. Fill() T R. Brun RT 2003 ROOT Input/Output 35

Tree Creation Example A few lines of code to create a Tree for structures

Tree Creation Example A few lines of code to create a Tree for structures that may be very complex R. Brun RT 2003 ROOT Input/Output 36

ROOT I/O - Split - multifile Object in Object memory in memory Streamer Object

ROOT I/O - Split - multifile Object in Object memory in memory Streamer Object in memory File 1 TAGs File 2 Tapes File 3 R. Brun RT 2003 ROOT Input/Output 37

Serial mode Split mode R. Brun RT 2003 ROOT Input/Output 38

Serial mode Split mode R. Brun RT 2003 ROOT Input/Output 38

Str uc tu re d ve sup esig ry po ne lar dt rt

Str uc tu re d ve sup esig ry po ne lar dt rt ge o DB s R. Brun RT 2003 ROOT Input/Output 39

8 leaves of branch Electrons A double-click to histogram the leaf 8 Branches of

8 leaves of branch Electrons A double-click to histogram the leaf 8 Branches of T R. Brun RT 2003 ROOT Input/Output 40

The Tree Viewer & Analyzer A very powerful class supporting complex cuts, event lists,

The Tree Viewer & Analyzer A very powerful class supporting complex cuts, event lists, 1 -d, 2 -d, 3 -d views parallelism R. Brun RT 2003 ROOT Input/Output 41

Tree Friends 01 23 45 67 89 10 11 12 13 14 15 16

Tree Friends 01 23 45 67 89 10 11 12 13 14 15 16 17 18 0 1 2 3 4 5 6 7 8 Entry # 8 01 23 45 67 89 10 11 12 13 14 15 16 17 18 Public User read Write R. Brun RT 2003 ROOT Input/Output 42

Tree Friends Analysis group protected Collaboration-wide public read user private Processing time independent of

Tree Friends Analysis group protected Collaboration-wide public read user private Processing time independent of the number of friends unlike table joins in RDBMS x Root > TFile f 1(“tree 1. root”); Root > tree. Add. Friend(“tree 2”, “tree 2. root”) Root > tree. Add. Friend(“tree 3”, “tree 3. root”); Root > tree. Draw(“x: a”, ”k<c”); Root > tree. Draw(“x: tree 2. x”, ”sqrt(p)<b”); R. Brun RT 2003 ROOT Input/Output 43

Chains of Trees n n A TChain is a collection of Trees. Same semantics

Chains of Trees n n A TChain is a collection of Trees. Same semantics for TChains and TTrees n root >. x h 1 chain. C n root > chain. Process(“h 1 analysis. C”) { //creates a TChain to be used by the h 1 analysis. C class //the symbol H 1 must point to a directory where the H 1 data sets //have been installed TChain chain("h 42"); chain. Add("$H 1/dstarmb. root"); chain. Add("$H 1/dstarp 1 a. root"); chain. Add("$H 1/dstarp 1 b. root"); chain. Add("$H 1/dstarp 2. root"); } R. Brun RT 2003 ROOT Input/Output 44

Ntuples and Trees n n n Ntuples n support PAW-like ntuples and functions n

Ntuples and Trees n n n Ntuples n support PAW-like ntuples and functions n PAW ntuples/histograms can be imported Trees n Extension of Ntuples for Objects n Collection of branches (branch has its own buffer) n Can input partial Event n Can have several Trees in parallel Chains = collections of Trees R. Brun RT 2003 ROOT Input/Output 45

Why Trees ? n n Any object deriving from TObject can be written to

Why Trees ? n n 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. R. Brun RT 2003 ROOT Input/Output 46

Why Trees ? n n n Trees have been designed to support very large

Why Trees ? n n n 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 RT 2003 ROOT Input/Output 47

Create a TTree Object A tree is a list of branches. The TTree Constructor:

Create a TTree Object A tree is a list of branches. The TTree Constructor: n n Tree Name (e. g. "my. Tree") Tree Title TTree *tree = new TTree("T", "A ROOT tree"); R. Brun RT 2003 ROOT Input/Output 48

Adding a Branch Many Branch constructors Only a few shown here n n n

Adding a Branch Many Branch constructors Only a few shown here n n n 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(); my. Tree->Branch(”e. Branch", "Event", &event, 64000, 1); R. Brun RT 2003 ROOT Input/Output 49

Splitting a Branch Setting the split level (default = 1) Split level = 0

Splitting a Branch Setting the split level (default = 1) Split level = 0 Split level = 1 Example: tree->Branch("Ev. Br", "Event", &ev, 64000, 0); R. Brun RT 2003 ROOT Input/Output 50

Adding Branches with a List of Variables n n Branch name Address: the address

Adding Branches with a List of Variables n n 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 Example TBranch *b = tree->Branch ("Ev_Branch", &event, "ntrack/I: nseg: nvtex: flag/i: temp/F"); R. Brun RT 2003 ROOT Input/Output 51

The Event class Event : public TObject { private: char Int_t UInt_t Float_t Event.

The Event class Event : public TObject { private: char Int_t UInt_t Float_t Event. Header TClones. Array TRef TH 1 F f. Type[20]; f. Ntrack; f. Nseg; f. Nvertex; f. Flag; f. Temperature; f. Measures[10]; f. Matrix[4][4]; *f. Closest. Distance; f. Evt. Hdr; *f. Tracks; *f. High. Pt; *f. Muons; f. Last. Track; *f. H; //event type //Number of tracks //Number of track segments //[f. Nvertex] //->array with all tracks //array of High Pt tracks only //array of Muon tracks only //reference pointer to last track //-> class Event. Header { private: Int_t R. Brun RT 2003 f. Evt. Num; f. Run; f. Date; See $ROOTSYS/test/Event. h ROOT Input/Output 52

The Track class Track : public TObject { private: Float_t Float_t Float_t Float_t Int_t

The Track class Track : public TObject { private: Float_t Float_t Float_t Float_t Int_t Short_t R. Brun RT 2003 f. Px; f. Py; f. Pz; f. Random; f. Mass 2; f. Bx; f. By; f. Mean. Charge; f. Xfirst; f. Xlast; f. Yfirst; f. Ylast; f. Zfirst; f. Zlast; f. Charge; f. Vertex[3]; f. Npoint; f. Valid; //X component of the momentum //Y component of the momentum //Z component of the momentum //A random track quantity //The mass square of this particle //X intercept at the vertex //Y intercept at the vertex //Mean charge deposition of all hits //X coordinate of the first point //X coordinate of the last point //Y coordinate of the first point //Y coordinate of the last point //Z coordinate of the first point //Z coordinate of the last point //Charge of this track //Track vertex position //Number of points for this track //Validity criterion ROOT Input/Output 53

Event Builder void Event: : Build(Int_t ev, Int_ntrack, Float_t ptmin) { Clear(); ………. .

Event Builder void Event: : Build(Int_t ev, Int_ntrack, Float_t ptmin) { Clear(); ………. . for (Int_t t = 0; t < ntrack; t++) Add. Track(random, ptmin); } Track *Event: : Add. Track(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. TClones. Array &tracks = *f. Tracks; Track *track = new(tracks[f. Ntrack++]) Track(random); //Save reference to last Track in the collection of Tracks f. Last. Track = track; //Save reference in f. High. Pt if track is a high Pt track if (track->Get. Pt() > ptmin) f. High. Pt->Add(track); //Save reference in f. Muons if track is a muon candidate if (track->Get. Mass 2() < 0. 11) f. Muons->Add(track); return track; } R. Brun RT 2003 ROOT Input/Output 54

Tree example Event (write) void demoe(int nevents) { //load shared lib with the Event

Tree example Event (write) void demoe(int nevents) { //load shared lib with the Event class g. System->Load("$ROOTSYS/test/lib. Event"); //create a new ROOT file TFile f("demoe. root", ”new"); All the examples can be executed with CINT or the compiler root >. x demoe. C++ //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(); T. Write(); //Print Tree statistics //Write Tree header to the file } R. Brun RT 2003 ROOT Input/Output 55

Tree example Event (read 1) void demoer() { //load shared lib with the Event

Tree example Event (read 1) void demoer() { //load shared lib with the Event class g. System->Load("$ROOTSYS/test/lib. Event"); //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->Set. Branch. Address("event", &event); //Loop on events and fill an histogram TH 1 F *h = new TH 1 F("hntrack", "Number of tracks", 100, 580, 620); int nevents = (int)T->Get. Entries(); for (int i=0; i<nevents; i++) { T->Get. Entry(i); Rebuild the full event h->Fill(event->Get. Ntrack()); in memory } h->Draw(); } R. Brun RT 2003 ROOT Input/Output 56

Tree example Event (read 2) void demoer 2() { //load shared lib with the

Tree example Event (read 2) void demoer 2() { //load shared lib with the Event class g. System->Load("$ROOTSYS/test/lib. Event"); //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->Set. Branch. Address("event", &event); Tbranch *bntrack = T->Get. Branch(“f. Ntrack”); //Loop on events and fill an histogram TH 1 F *h = new TH 1 F("hntrack", "Number of tracks", 100, 580, 620); int nevents = (int)T->Get. Entries(); for (int i=0; i<nevents; i++) { bntrack->Get. Entry(i); Read only h->Fill(event->Get. Ntrack()); one branch } Much faster ! h->Draw(); } R. Brun RT 2003 ROOT Input/Output 57

Tree example Event (read 3) void demoer 3() { //load shared lib with the

Tree example Event (read 3) void demoer 3() { //load shared lib with the Event class g. System->Load("$ROOTSYS/test/lib. Event"); //connect ROOT file TFile *f = new TFile("demoe. root"); //Read Tree header TTree *T = (TTree*)f->Get("T"); //Histogram number of tracks via the Tree. Player T->Draw(“event->Get. Ntrack()”); } R. Brun RT 2003 ROOT Input/Output 58

Writing CMS PSim. Hit in a Tree void demo 3() { //create a new

Writing CMS PSim. Hit in a Tree void demo 3() { //create a new ROOT file TFile f("demo 3. 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; PSim. Hit *hit = 0; TTree T("T", "CMS demo tree"); T. Branch("hit", "PSim. Hit", &hit, bufsize, split ); //Create hits in a loop and fill the Tree TRandom r; for (int i=0; i<50000; i++) { delete hit; Local 3 DPoint pentry(r. Gaus(0, 1), r. Gaus(0, 10)); Local 3 DPoint pexit (r. Gaus(0, 3), r. Gaus(50, 20)); float pabs = 100*r. Rndm(); float tof = r. Gaus(1 e-6, 1 e-8); float eloss= r. Landau(1 e-3, 1 e-7); int ptype = i%2; int det. Id = i%20; int track. Id= i%100; hit = new PSim. Hit(pentry, pexit, pabs, tof, eloss, ptype, det. Id, track. Id ); T. Fill(); } T. Print(); T. Write(); //Print Tree statistics //Write Tree header to the file } R. Brun RT 2003 ROOT Input/Output 59

Browsing the PSim. Hit Tree split = 0 *Tree : T : CMS demo

Browsing the PSim. Hit Tree split = 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 RT 2003 ROOT Input/Output 60

Browsing the PSim. Hit Tree split = 1 *************************************** *Tree : T : CMS

Browsing the PSim. Hit Tree split = 1 *************************************** *Tree : T : CMS demo tree * *Entries : 50000 : Total = 5258415 bytes File Size = 2021907 * * : : Tree compression factor = 2. 60 * *************************************** *Branch : hit * *Entries : 50000 : Branch. Element (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 : the. Entry. Point : * *Entries : 50000 : Total Size= 1704437 bytes File Size = 750090 * *Baskets : 119 : Basket Size= 16000 bytes Compression= 2. 27 * *. . . . . * *Br 2 : the. Exit. Point : * *Entries : 50000 : Total Size= 1704318 bytes File Size = 744721 * *Baskets : 119 : Basket Size= 16000 bytes Compression= 2. 29 * *. . . . . * *Br 3 : the. Pabs : * *Entries : 50000 : Total Size= 191988 bytes File Size = 170871 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 12 * *. . . . . * *Br 4 : the. Tof : * *Entries : 50000 : Total Size= 191976 bytes File Size = 145548 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 32 * *. . . . . * *Br 5 : the. Energy. Loss : * *Entries : 50000 : Total Size= 191964 bytes File Size = 122761 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 56 * *. . . . . * *Br 6 : the. Particle. Type : * *Entries : 50000 : Total Size= 191988 bytes File Size = 1860 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 103. 22 * *. . . . . * *Br 7 : the. Det. Unit. Id : * *Entries : 50000 : Total Size= 191952 bytes File Size = 2298 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 83. 53 * *. . . . . * *Br 8 : the. Track. Id : * *Entries : 50000 : Total Size= 191976 bytes File Size = 4179 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 45. 94 * *. . . . . * * R. Brun RT 2003 ROOT Input/Output 9 branches 61

Browsing the PSim. Hit Tree split = 99 *************************************** *Tree : T : CMS

Browsing the PSim. Hit Tree split = 99 *************************************** *Tree : T : CMS demo tree * *Entries : 50000 : Total = 2687592 bytes File Size = 1509041 * * : : Tree compression factor = 1. 78 * *************************************** *Branch : hit * *Entries : 50000 : Branch. Element (see below) * *. . . . . * *Br 0 : f. Unique. ID : * *Entries : 50000 : Total Size= 191964 bytes File Size = 1272 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 150. 92 * *. . . . . * *Br 1 : f. Bits : * *Entries : 50000 : Total Size= 191964 bytes File Size = 1260 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 152. 35 * *. . . . . * *Br 2 : the. Entry. Point : * *Entries : 50000 : Total Size= 0 bytes File Size = 0 * *Baskets : 0 : Basket Size= 16000 bytes Compression= 1. 00 * *. . . . . * *Br 3 : the. Entry. Point. the. Vector. the. X : * *Entries : 50000 : Total Size= 191952 bytes File Size = 177959 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 08 * *. . . . . * *Br 4 : the. Entry. Point. the. Vector. the. Y : * *Entries : 50000 : Total Size= 191952 bytes File Size = 177934 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 08 * *. . . . . * *Br 5 : the. Entry. Point. the. Vector. the. Z : * *Entries : 50000 : Total Size= 191952 bytes File Size = 178312 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 08 * *. . . . . * *Br 6 : the. Exit. Point : * *Entries : 50000 : Total Size= 0 bytes File Size = 0 * *Baskets : 0 : Basket Size= 16000 bytes Compression= 1. 00 * *. . . . . * *Br 7 : the. Exit. Point. the. Vector. the. X : * *Entries : 50000 : Total Size= 191988 bytes File Size = 178060 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 08 * *. . . . . * *Br 8 : the. Exit. Point. the. Vector. the. Y : * *Entries : 50000 : Total Size= 191988 bytes File Size = 178072 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 08 * *. . . . . * *Br 9 : the. Exit. Point. the. Vector. the. Z : * *Entries : 50000 : Total Size= 191988 bytes File Size = 168655 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 14 * *. . . . . * *Br 10 : the. Pabs : * *Entries : 50000 : Total Size= 191988 bytes File Size = 170871 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 12 * *. . . . . * *Br 11 : the. Tof : * *Entries : 50000 : Total Size= 191976 bytes File Size = 145548 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 32 * *. . . . . * *Br 12 : the. Energy. Loss : * *Entries : 50000 : Total Size= 191964 bytes File Size = 122761 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 1. 56 * *. . . . . * *Br 13 : the. Particle. Type : * *Entries : 50000 : Total Size= 191988 bytes File Size = 1860 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 103. 22 * *. . . . . * *Br 14 : the. Det. Unit. Id : * *Entries : 50000 : Total Size= 191952 bytes File Size = 2298 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 83. 53 * *. . . . . * *Br 15 : the. Track. Id : * *Entries : 50000 : Total Size= 191976 bytes File Size = 4179 * *Baskets : 12 : Basket Size= 16000 bytes Compression= 45. 94 * *. . . . . * R. Brun RT 2003 Double click produces this histogram 16 branches ROOT Input/Output 62

Collections of Hits n A more realistic Tree will have n A collection of

Collections of Hits n A more realistic Tree will have n A collection of Detectors n Each detector one or more collection of hits R. Brun RT 2003 ROOT Input/Output 63