# Presentation: 081613

## Trees: New DevelopmentsFolders and Tasks

- ROOT Workshop 2001
- June 13 FNAL
- René Brun
- CERN

- http://root.cern.ch

- CERN School of Computing

## Trees before 3.01

- TTree
- TBranch
- TBranchObject
- TBranchClones
- TChain

- Many limitations in split mode
- Only simple classes in TClonesArray
- Original classes required for TBranchObject

- CERN School of Computing

- 17 - 30 Sept. 2000

## Trees in version 3.01

- TTree
- TBranch
- TBranchObject
- TBranchClones
- TBranchElement
- TChain
- TFriendElement

- This new class replaces
- TBranchObject & TBranchClones
- Many more cases supported

- Tree Friends
- A fundamental addition

- CERN School of Computing

- 17 - 30 Sept. 2000

## Self-describing files

- Dictionary for persistent classes written to the file.
- ROOT files can be read by foreign readers (JAS)
- Support for Backward and Forward compatibility
- Files created in 2001 must be readable in 2015
- Classes (data objects) for all objects in a file can be regenerated via TFile::MakeProject

- Root >TFile f(“demo.root”);
- Root > f.MakeProject(“dir”,”*”,”new++”);

- CERN School of Computing

- 17 - 30 Sept. 2000

##     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");**
**TTree T2(“T2”,”/Event/Tracks”);**

- CERN School of Computing

- 17 - 30 Sept. 2000

##          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);**

- CERN School of Computing

- 17 - 30 Sept. 2000

## Splitting a Branch

- Setting the split level (default = 1)

- Split level = 0

- Split level = 1

- Example:
- int splitlevel = 2;
- tree->Branch("EvBr","Event",&ev,64000,splitlevel);

- CERN School of Computing

- 17 - 30 Sept. 2000

## 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

- Example
**TBranch *b = tree->Branch ("Ev_Branch",&event,**
**	****"ntrack/I:nseg:nvtex:flag/i:temp/F");**

- CERN School of Computing

- 17 - 30 Sept. 2000

## Adding Branches with a TClonesArray

- Branch name
- Address of a pointer to a TClonesArray
- Buffer size
- Split level (default = 1)

- Example:
- tree->Branch( ”tracks",&Track,64000,1);
- tree->Branch( ”tracks",”TClonesArray”,&Track,64000,1);

- CERN School of Computing

- 17 - 30 Sept. 2000

## 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”)
- root > chain.Process(“h1analysis.C+”)

- void h1chain() {
-    ***//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");
- }

- h1chain.C

- CERN School of Computing

- 17 - 30 Sept. 2000

## 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 time
- independent of the
- number of friends
- unlike table joins
- in RDBMS

- CERN School of Computing

- 17 - 30 Sept. 2000

## Trees and Folders

- A complete Tree can be generated automatically from an existing Folder structure
- One Branch can be generated from a Folder
- A Folder structure can be automatically rebuilt from a Tree file

- CERN School of Computing

- 17 - 30 Sept. 2000

## The Tree Viewer & Analyzer

- A very powerful class
- supporting
- complex cuts,
- event lists,
- 1-d,2-d, 3-d views
- parallelism

- CERN School of Computing

- 17 - 30 Sept. 2000

## New TTreeFormula

- The class TTreeFormula has been extended to process the new TBranchElement class. Thanks Philippe Canal.
- Can access data members in an object hierarchy in split and/or non-split modes
- support multiple levels of calls to member functions in split or/and no-split modes.
- Support complex classes in TClonesArray
- Examples

- CERN School of Computing

- 17 - 30 Sept. 2000

## New TTreeFormula

- Event *event = 0;
- TTree T(“T”);
- T.Branch(“event”,”Event”,&event,32000,2);

- CERN School of Computing

- 17 - 30 Sept. 2000

## New TTreeFormula

- CERN School of Computing

- 17 - 30 Sept. 2000

## New TTreeFormula

- CERN School of Computing

- 17 - 30 Sept. 2000

## New TTreeFormula

- CERN School of Computing

- 17 - 30 Sept. 2000

## read/query Trees without the classes

- CERN School of Computing

- 17 - 30 Sept. 2000

## read/query Trees without the classes

- CERN School of Computing

- 17 - 30 Sept. 2000

## Automatic Code Generators

- Data sets can be analyzed by the same classes used to store the data.
- However, one must be able to read the data without these original classes. The classes may not be available after some time.
- Root provides two utilities to generate a class skeleton to read the data, still preserving the attribute names, types and the structure.
- TTree::MakeClass
- TTree::MakeSelector

- This point is important.
- You can always analyze
- a data set even if you have lost
- the class(es) that generated
- this data set

- CERN School of Computing

- 17 - 30 Sept. 2000

## TTree::MakeClass

- tree.MakeClass(“myClass”); generates two files: myClass.h and myClass.C
- myClass.h contains the class declaration and member functions code that is selection invariant.
- myClass.C contains an example of empty loop where one can insert the analysis code
- Usage:
- root > .L myClass.C    or  .L myClass.C++
- root > myClass xx;
- root > xx.Loop();

- Use the interpreter

- Use the native compiler
- The file myClass.C
- is automatically compiled
- and linked !!

- CERN School of Computing

- 17 - 30 Sept. 2000

## TTree::MakeSelector

- tree.MakeSelector(“myClass”); generates two files: myClass.h and myClass.C that can work in a parallel system like PROOF. The event loop is not under user control.
- myClass.h contains the class declaration and member functions code that is selection invariant.
- myClass.C contains the skeleton of 4 functions: Begin, ProcessCut, ProcessFill, Terminate.
- Usage:
- root > tree.Process(“myClass.C”);
- root > chain.Process(“myClass.C++”);

- Macro is
- automatically
- compiled
- and linked

- CERN School of Computing

- 17 - 30 Sept. 2000

## Folders: in a nutshell

- The class TFolder has been in ROOT since quite some time.
- A Folder structure can be used as a white board facility to minimize dependencies between classes via the Folder naming scheme
- User classes/collections in Folders facilitate the documentation and inspection.
- Trees can be generated automatically from Folders.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Some analogy with the past

- At the beginning of computing, communication via Subroutine arguments: No global state
- Labelled Common Blocks
- ZEBRA/BOS solved at least 2 problems
- Dynamic structures
- Communication between modules only via the ZEBRA store. Eg, banks from a simulation program could be read in a reconstruction program
- Experience with a large variety of C++ applications indicates a “common-block like” approach.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Why Folders ?

- This diagram shows a system without folders. The objects have pointers to each other to access each other's data.
- Pointers are an efficient way to
- share data between classes. However, a direct pointer creates a direct coupling between classes.
- This design can become a very tangled web of dependencies in a system with a large number of classes.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Why Folders ?

**In the  diagram below, a reference to the data is in the folder and the consumers refer to the folder rather than each other to access the data. **
**The naming and search service provided by the ROOT folders hierarchy provides **
**an alternative. It loosely couples the classes and greatly enhances I/O operations. **
**In this way, folders separate the data from the algorithms and greatly **
**improve the modularity of an application by minimizing the class dependencies**.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Posting Data to a Folder (Producer)

- No changes required in user class structure.
- Build a folder structure with:
- TFolder::AddFolder(TFolder *)
- Post objects or collections to a Folder with:
- TFolder::Add(TObject*)
- A TFolder can contain other folders or any TObject descendents. In general, users will not post a single object to a folder, they will store a collection or multiple collections in a folder. For example, to add an array to a folder:
- TObjArray *array;
-  run_mc->Add(array);

- CERN School of Computing

- 17 - 30 Sept. 2000

## Reading Data from a Folder (Consumer) 

**One can search for a folder or an**** ****object in a folder using the ****TROOT::FindObjectAny**** method. ****FindObjectAny**** analyzes the string passed as its argument and searches in **
**the hierarchy until it finds an object or folder matching the name. **
**With ****FindObjectAny****, you can give the full path name, or the name of the folder. **
**If only the name of the folder is given, it will return the first instance of that name.**

**conf = (TFolder*)gROOT->FindObjectAny("/aliroot/Run/Configuration");**** **
** ****or **
**conf = (TFolder*)gROOT->FindObjectAny("Configuration");**

**A string-based search is time consuming. If the retrieved object is used frequently or inside a loop, you should save a pointer to the object as a class data member. **
**Use the naming service only in the initialization of the consumer class.**

- CERN School of Computing

- 17 - 30 Sept. 2000

## Example: Alice folders

- A ROOT Tree can be automatically generated from the folder, eg:
- TTree T(“T”,”/Event”);
- T.Fill();
- T.Show();

- This statement
- generates a Tree
- with 325 branches

- CERN School of Computing

- 17 - 30 Sept. 2000

## Tasks

**In the same way that Folders can be used to organize the data, one can use the class ****TTask**** to organize a hierarchy of algorithms.**
**Tasks can be organized into a hierarchical tree of tasks and displayed  in the browser. The ****TTask**** class is the base class from which the subtasks are derived. To give a task functionality, you need to subclass the ****TTask ****class and override the ****Exec**** method.  **
**Each ****TTask**** derived class may contain other ****TTasks**** that can be executed recursively, such that a complex program can be dynamically built and executed by invoking the services of the top level task or one of its subtasks.**

-  **TTask *run      = new MyRun("run","Process one run");**
**  ****TTask *event  = new MyEvent("event","Process one event");**

**Use the ****TTask::Add**** method to add a subtask to an existing ****TTask****.**
**To execute a ****TTask****, you call the ****ExecuteTask**** ****method. **
**ExecuteTask**** will recursively call:    **
**  ****-****TTask::Exec**** method of the derived class    **
**  ****-****TTask::ExecuteTasks**** to execute for each task the list of its subtasks**.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Execute and Debug Tasks 

- If the top level task is added to the list of ROOT browse-able objects,
- the tree of tasks can be visualized by the ROOT browser.
- To add it to the browser, get the list of browse-able objects first and
- add it to the collection.

- gROOT->GetListOfBrowsables()->Add(aliroot,"aliroot");

- The browser can be used to start a task, set break points at the beginning
- of a task or when the task has completed. At a breakpoint, data structures
- generated by the execution up this point may be inspected asynchronously
- and then the execution can be resumed by selecting the "Continue" function
- of a task.
- A Task may be active or inactive (controlled by TTask::SetActive).
- When a task is inactive, its sub tasks are not executed.
- A Task tree may be made persistent, saving the status of all the tasks.

- CERN School of Computing

- 17 - 30 Sept. 2000

## Tasks

- CERN School of Computing

- 17 - 30 Sept. 2000

## Folders/Tasks Summary

- Folders minimize coupling between classes
- Folders are browsable. Good for documentation and code reviews.
- Trees generated from folders

- Tasks are browsable. Good for documentation and code reviews.
- Tasks help in understanding a program hierarchy
- Tasks encourage common behaviors

- CERN School of Computing

- 17 - 30 Sept. 2000