root for beginnerspetersen/teaching/stat2016/pythonrootintro/r… · objecttype* toto; you have to...

106
ROOT for beginners Second Day Programming Now this won't hurt a bit…

Upload: others

Post on 18-Jan-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

ROOT for beginners

Second DayProgramming

Now this won'thurt a bit…

Page 2: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Writing scripts

• Today:

•Creation and destruction of objects•Manipulating objects

•Finding the information - the user'sguide to the user's guide•Finding "lost" objects

•Writing functions•Analysis scripts

Page 3: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Creation and destruction of objects

Commands "new" & "delete"Object pointers

Page 4: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The "new" command

• Yesterday we began the day with:new TBrowser

which made a "ROOT object browser" appear.

Page 5: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The "new" command

• Yesterday we began the day with:new TBrowser

which made a "ROOT object browser" appear.

• If you typenew TCanvas

a canvas appears!But what is going on in thecommand window ???

Page 6: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The command window• The command window is a C++ interpreter!!• It displays the value of each function, expression

or command that you type - try e.g. 2+2…

Page 7: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The command window• The values displayed after a new command are

the type (class) & the address in memory of thecreated objects

address in memory of the "TCanvas"class object which is currentlydisplayed on your screen

Page 8: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Object pointers

• To use the object, you have to put its address in toa special variable, an object pointer (or pointer)

Declaration of an(object) pointer : ObjectType* toto;

Page 9: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

You have to declare thetype of object whoseaddress this will hold(object type = class name)

The '*' tells usthis is a pointer

Object pointers

• To use the object, you have to put its address in toa special variable, an object pointer (or pointer)

Declaration of an(object) pointer : ObjectType* toto;

Page 10: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Object pointers

• The value held by the object pointer is the addressof the object in memory

Initialisation of anobject pointer : toto = (ObjectType*)address ;

Page 11: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Initialise the pointer with theaddress of your object oftype (class) TCanvas

Object pointers

• The value held by the object pointer is the addressof the object in memory

Initialisation of anobject pointer : toto = (ObjectType*)address ;

Page 12: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Object destruction• The delete command frees the memory space

occupied by the objects• You must use it to destroy all the objects you

don't need any more or you will fill the memory!

Destroy an object: delete toto;

Page 13: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Object destruction• The delete command frees the memory space

occupied by the objects• You must use it to destroy all the objects you

don't need any more or you will fill the memory!

Destroy an object: delete toto; Executing this commandmakes the canvasdisappear!

The object (and itsgraphical representation)no longer exists, the spacein memory it occupied isfreed.

Page 14: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Constructors• Most of the time we will declare a pointer, create an

object, and initialise the pointer with the address of theobject in one single line !!

Object creation withdeclaration andinitialisation of pointer:

ObjectType* toto = new ObjetType;

Page 15: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

This is a special functioncalled a constructor.

The constructor defineshow all objects of a givenclass are made.

*a new canvasappears !

Constructors• Most of the time we will declare a pointer, create an

object, and initialise the pointer with the address of theobject in one single line !!

Object creation withdeclaration andinitialisation of pointer:

ObjectType* toto = new ObjetType;

Page 16: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Constructors

• Generally, constructors have arguments

ObjectType* toto = newObjectType(…);

Object creation withdeclaration andinitialisation of pointer:

Page 17: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

E.g. you can create acanvas and specify itsname, title, position andsize on screen

In the previous example, the canvas haddefault name "c1" and default title "c1"

Constructors

• Generally, constructors have arguments

ObjectType* toto = newObjectType(…);

Object creation withdeclaration andinitialisation of pointer:

Page 18: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Manipulating objects

Getting to grips with class methods

Page 19: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Interacting with objects• Graphically, we can interact with an object using

its context menu:

Example fromyesterday, when wedivided a canvas in 4

Page 20: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer: toto-> Method(arguments);

The object pointerwith the '->' operatordefine which objet weinteract with

Page 21: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

toto-> Method(arguments);

One of the methodsof the object's class.For example, one ofthe functions in thecontext menu.

All objects of the same classhave the same methods.

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer:

Page 22: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

canvas->Divide(2,2);

Interact with object "c1" using thepointer "canvas". We use its"Divide" method with nx=2 & ny=2.

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer:

Page 23: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The "c1" canvasdivides in to 4

canvas->Divide(2,2);

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer:

Page 24: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

another_canvas->Divide(10,1);

Interact with "c2" using pointer"another_canvas". We use c2's"Divide" method with nx=10 & ny=1.

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer:

Page 25: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Canvas 'c2' divides itself

another_canvas->Divide(10,1);

Interacting with objects• With an object pointer, we can also interact with

the object…

Interact with anobject using a pointer:

Page 26: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Interacting with objects• Other operations on canvases:

canvas->Clear();Clear the contents ofthe canvas, includingany divisions

Make a canvas (or apad) 'active', i.e. itsborder will becomeyellow and the nextobject to be drawnwill appear on thiscanvas

canvas->cd();

Page 27: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Example with a histogram

• You can create a 1-D spectrum in much the sameway as you create a canvas:

TH1F* histo = new TH1F("h1","My histo", 10, 0., 10.);Creating a 1-Dhistogram

ObjectType* toto = newObjectType(…);

TH1F ? Histogram in 1 dimensionof Floating-point values

N.B. The histogram does not appearautomatically on screen!

Page 28: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Example with a histogram

• Display and fill the histogram:

histo->Draw();Display ahistogram:

histo->Fill(3);Fill ahistogram:

The "3" correspondsto a X-axis value

Nothing seemsto happen ?

Page 29: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Example with a histogram

• Refresh the canvas:

canvas->Modified();Tell the canvasthat an objectit is displayinghas changed:

canvas->Update();Force thecanvas torefresh:

When using the command window, thisautomatically causes the canvas to refresh.

However, in a script/programme you also have toask for the canvas to redraw its objects!

Page 30: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Example with a histogram

• Starting to get bored ?

histo->Fill(1.43);histo->Fill(6.9); …histo->Fill(9, 2);canvas->Modified();

We could carry on like this until we finishfilling our histogram…

But we'd be happier if we wrote a loop……in a function……programmed in C++…!!!

Fill with a weight,i.e. the value '9'has occurredtwice

Don't look atme like that…

Page 31: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Creating objects without "new"

There is another way…'Temporary' vs. 'Permanent' objects

Page 32: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The other way…• There is another way to create and manipulate

objects…

ObjectType toto(…);Creating an objectwithout "new"

Page 33: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

ObjectType toto(…);

Creating anobject with "new"

ObjectType* toto_ptr = new ObjectType(…);

If the constructor hasarguments, put them here

The other way…• There is another way to create and manipulate

objects…

Creating an objectwithout "new"

Page 34: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

TCanvas can("c3","title",250,100,50,500);

TCanvas* can_ptr = new TCanvas("c3","title", …);Creating anobject with "new"

The other way…• There is another way to create and manipulate

objects…

Creating an objectwithout "new"

Page 35: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The other way…• The way we interact with the object isn't quite the

same as before…

toto. Methode(arguments);Interact with anobject createdwithout "new"

Page 36: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

toto. Methode(arguments);

Interact with anobject using a pointer: toto_ptr-> Methode(arguments);

In one case we use the'.' operator, in theother '->'

The other way…• The way we interact with the object isn't quite the

same as before…

Interact with anobject createdwithout "new"

Page 37: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

can.Divide(1,6);

can_ptr->Divide(1,6);Interact with anobject using a pointer:

The other way…• The way we interact with the object isn't quite the

same as before…

Interact with anobject createdwithout "new"

Page 38: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The other way…• We can also obtain the address in memory of an

object created in this way, and then use a pointer

ObjectType* toto_ptr = &toto;Initialise a pointerwith the address ofan existing object

Page 39: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

ObjectType* toto_ptr = &toto;

Operator returningthe address of theobject 'toto'

The other way…• We can also obtain the address in memory of an

object created in this way, and then use a pointer

Initialise a pointerwith the address ofan existing object

Page 40: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The other way…

toto_ptr ->Methode(arguments);Interact with anobject using a pointer

• The use of a pointer to interact with the object isidentical to the previous cases…

Page 41: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

can_ptr ->Clear();

The other way…

Interact with anobject using a pointer

• The use of a pointer to interact with the object isidentical to the previous cases…

Page 42: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The other way…

• What's the difference ? We don't need to deletethe object when we've finished with it…

Objects created this way areautomatically destroyed at the end ofthe code block* in which they arecreated.

Objects created with "new" are onlydestroyed by the user, with "delete".

*Function, loop,"if-else", etc.

Page 43: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Temporaryobjects

Permanentobjects

The other way…

• What's the difference ? We don't need to deletethe object when we've finished with it…

Objects created this way areautomatically destroyed at the end ofthe code block* in which they arecreated.

Objects created with "new" are onlydestroyed by the user, with "delete".

*Function, loop,"if-else", etc.

Page 44: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Getting information on classes

Where's the manual ?Where's the manual for the manual ?Do I have to learn it all by heart ?*

*NO!!!!

Page 45: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Getting to know your way around• How can I find out all the possible ways to interact with

an object ?• How do I find out all the methods of a class ?

1. command line completion with <TAB>

Very efficient, reduces to bareminimum the amount you have totype (and so reduces typingerrors…)

TIP 1:most methods which change anobject begin "Set…"TIP 2:most methods which give aninformation about an object begin"Get…"

<TAB>

<TAB>

Page 46: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Getting to know your way around• The best method: http://root.cern.ch

1. click on"Reference Guide"

2. Look at the ROOTweb site

Page 47: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

2. list of classes bycategory (histos,matrices, 3Dgeometry, etc.)

Getting to know your way around• The best method: http://root.cern.ch

Page 48: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

2. complete list ofall classes for eachversion of ROOT

Getting to know your way around• The best method: http://root.cern.ch

Page 49: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

2. keyword search

Getting to know your way around• The best method: http://root.cern.ch

Page 50: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

type "TCanvas"then <ENTER>/<RETURN>

Getting to know your way around• The best method: http://root.cern.ch

Page 51: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

searching using the nameof a class works best…

…but don't neglect theother responses whichcan be very interesting!!

Getting to know your way around• The best method: http://root.cern.ch

Page 52: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Presentation of a pagedocumenting a class

• All necessary information is here… as long asyou know where to look

Class name andname of its closestfamily relation(parent class)

Page 53: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Complete family treefor the class

Presentation of a pagedocumenting a class

• All necessary information is here… as long asyou know where to look

Page 54: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Complete family treeof the class

parents and grand-parents to the left

Presentation of a pagedocumenting a class

• All necessary information is here… as long asyou know where to look

Page 55: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Complete family treeof the class

parents and grand-parents to the left

children and grand-children to the right

Presentation of a pagedocumenting a class

• All necessary information is here… as long asyou know where to look

Page 56: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

(a bit further down)

complete list of classmethods

each method name is alink to an explanation ofthe method

Presentation of a pagedocumenting a class

• All necessary information is here… as long asyou know where to look

Page 57: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The class methods list• There are three types of method: "private",

"protected", and "public"

you cannot use the"private" or"protected" methods…

… so we'll onlylook at the"public" ones

Page 58: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The class methods list• The "public" methods list is always organised in

the same way:

First the constructorsof the class (methodswith the same name asthe class)

Page 59: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

A destructor"~ClassName()" called whenthe object is destroyed,e.g. by delete*

*the destructor cannot becalled directly i.e. toto->~ClassName();does not work. we use: delete toto;

The class methods list• The "public" methods list is always organised in

the same way:

Page 60: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

An alphabetical list of all ofthe methods of the class…?

Where is "Divide" ?

The class methods list• The "public" methods list is always organised in

the same way:

Page 61: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding ALL the methods of aclass

• If a method seems to be missing from a class, itmight be defined by its (grand-)parents…

The objects of a classinherit all thecharacteristics & know-how of their ancestors…

…whilst adding a fewparticularites of theirown.

Start with theparent class

Page 62: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Found it!Let's look at theexplanation…*

*…we'll explain thefunction declaration(variable types, defaultarguments etc.) later

Finding ALL the methods of aclass

• If a method seems to be missing from a class, itmight be defined by its (grand-)parents…

Page 63: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Here is the on-line HTMLdocumentation for method"Divide" of class "TPad",the parent class of"TCanvas".

As TCanvas does notdefine another 'Divide'method, this is the oneused by objects of classTCanvas.

Finding ALL the methods of aclass

• If a method seems to be missing from a class, itmight be defined by its (grand-)parents…

Page 64: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Lost & found

Retrieving lost objectsOr:

Why (most) objects have a name

Page 65: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects• ROOT keeps lists of objects, allowing to find

them easily

Double-click 'root'

Page 66: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• You can browse these lists using the TBrowser

Click on'Canvases'

Page 67: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• Each object has to have an unique name for us tofind it.

Here are our 2canvases, we recognisetheir names (and titles)

Page 68: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• You can interact with the objects using theircontext menu…

Right-click to openthe context menuof "c1"

Page 69: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Choose e.g."Divide", and youcan cut "c1" in 4,just likeyesterday…

Finding 'lost' objects

• You can interact with the objects using theircontext menu…

Page 70: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• …or if you knew the address of the object, youcould use a pointer:

gROOT->GetListOfCanvases()->FindObject("c1");

Page 71: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

gROOT->GetListOfCanvases()->FindObject("c1");

This global pointer containsthe address of the 'root'folder of the folder tree.

You can use it to access allthe other objects*

Finding 'lost' objects

• …or if you knew the address of the object, youcould use a pointer:

*gROOT points to an objectof the class TROOT.

Page 72: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

gROOT->GetListOfCanvases()->FindObject("c1");

Going down a level inthe tree structure, thisis the equivalent of the'Canvases' folder

Finding 'lost' objects

• …or if you knew the address of the object, youcould use a pointer:

Page 73: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

gROOT->GetListOfCanvases()->FindObject("c1");

In the list/folder ofall Canvases, we lookfor an object named"c1".

Finding 'lost' objects

• …or if you knew the address of the object, youcould use a pointer:

Page 74: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• If you aren't sure which folder to look in, you canrecursively search through all the folders:

gROOT->GetListOfCanvases()->FindObject("c1");

gROOT->FindObject("name");

This is the MAGIC FORMULA whichallows to find (nearly) any objectanywhere anytime.

You'll use it all the time!!

Page 75: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Finding 'lost' objects

• All you have to do now is put the address in theappropriate pointer and use it:

ClassName* toto = (ClassName*) gROOT->FindObject("nom");

We have to re-specify the classof the object wehope to find here

TCanvas* c1_ptr = (TCanvas*) gROOT->FindObject("c1");c1_ptr->Clear();

Example: we look forcanvas "c1" and wipe itclean:

Page 76: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Testing if an object exists

• You use the same function to know if an objectwith a given name exists or not:

Histogram "h1" exists.The function returns itsaddress.

Page 77: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

There is no histogramnamed "hh".

The function returns anaddress equal to ZERO!!

Testing if an object exists

• You use the same function to know if an objectwith a given name exists or not:

Page 78: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Testing if an object exists

• To be rigorous, you should always test the valueof a pointer before trying to use it…

What happens if you try to use apointer holding the address 0 ?

Page 79: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The interpreter is very kind……in a compiled programme thiswould give a "segmentationviolation" (OUCH!)

Testing if an object exists

• To be rigorous, you should always test the valueof a pointer before trying to use it…

Page 80: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Function programming

C++ for everybody…

Page 81: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

A function• Here is an example of a C++ function

HEADERS:a declaration for eachclass used in the function

Page 82: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

DECLARATION:type of value returned,name of function,arguments*

A function• Here is an example of a C++ function

*void = no returned value() = no arguments

Page 83: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

an 'if-else'block

A function• Here is an example of a C++ function

Page 84: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

A loop over 'x', fromx=0 to x=99,incrementing x by 1each time

A function• Here is an example of a C++ function

Page 85: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Clear out thecontents of thespectrum

gPad = pointer to the currentlyactive pad or canvas

A function• Here is an example of a C++ function

Page 86: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Compiling and using the function• To compile and load the definition of the function:

.L fillHisto.C+

Name of filecontaining thefunction definition

• To execute the function:

DrawGaussian()

Name of the function(don't forget the '()' !)

Page 87: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Why use 'new' to create thehistogram ?

• Let's see what happens if we don't use "new"

Temporary object,it only exists insidethis block (function)

We see the histogramfilling up…

… and thendisappear at theend of the function

Page 88: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Using functions with arguments• A function with arguments:

List of argumentswith their type

Use of the argumentsinside the function

Page 89: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Arguments with default values

e.g. defaultwidth for thegaussian

DrawGaussian() => amp=20, moy=50, large=10DrawGaussian(5) => amp=5, moy=50, large=10DrawGaussian(5,30) => amp=5, moy=30, large=10DrawGaussian(5,30,5) => amp=5, moy=30, large=5

Page 90: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Returning values/objects

• Functions can return ALL sorts of variables…

Gaussianfunction

We returnthe valueof 'f'

Type ofreturned value

Page 91: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

We return a pointerto a histogram object

Here we use thegaussian function

We return the value of 'h' i.e. theaddress of the histogram

Returning values/objects

• Functions can return ALL sorts of variables…

Page 92: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Returning values/objects

• Compiling and using the functions:We can use the gaussianfunction independently ofDrawGaussian4()

Execute the functionDrawGaussian4(), stock theaddress of the histogram in apointer and plot it…

Page 93: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Analysis example

I told you itwouldn't hurt…

Page 94: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Analysis example

Declaration of theI/O system ofROOT/C++

An example of an analysis script: we read the data from anASCII file basic.dat; generate a few histograms; and savethem in the file basic.root.http://caeinfo.in2p3.fr/root/Formation/en/Day2/basic.dat

this variable typemeans 'a string'

Object for readingan ASCII file

opening theASCII file

Page 95: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Analysis example

Declaring a fewworking variables

Creation of a newROOT file, if it existsalready we overwrite(recreate) it

Creation of a NtupleAll histograms, Ntuples, trees (Day 4)created after the creation of the filebelong to the file…

Page 96: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Analysis exampleA 'while' loop: keeps executing as long as thecondition is true i.e. as long as the file isreadable

we read three Double_tvalues from the file

Print the values we read onthe screenendl => carriage-return

Fill the histogram andthe ntuple with thevalues we read

Incrementthe numberof lines read

Page 97: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Analysis example

Close theASCII file

The file and all objects belonging to itare written to disk (f->Write())Deleting the TFile object (delete f)has the effect of closing the file.

WARNING:once the file has been closed, theobjects belonging to it no longer existin memory (only on disk)!

Page 98: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

• Compile and execute:

• Open the file and plot spectrum 'h1':

Perform the analysis and look atthe results

.L basic.C+basic()

TFile* fich = new TFile("basic.root")fich->ls()TH1F* histo = (TH1F*) fich->Get("h1")histo->Draw()Print the

contents ofthe file Copy the object 'h1'

into the memory.Return the address ofthis copy.

The last two lines in one:fich->Get("h1")->Draw()

Page 99: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Closing remarks

Some hints and tips for those darkmoments of the soul (when it's just you

and the C++ compiler)

Page 100: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Examples of functions/scripts• On the ROOT web site, under the heading

Tutorials, you can find lots of useful examples• Caution! if the file doesn't have a proper function

declaration then you can't compile it:

.x toto.CExecuting ascript withoutfull declaration

In this case, the code will beinterpreted, not compiled.

WARNING: we really don'trecommend you do this foryour own scripts!!

Page 101: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

The 'rootlogon.C' file

{gStyle->SetPalette(1);cout << "Salut " << gSystem->Getenv("USER") << "!" << endl;gSystem->Exec("date");}

• This script without a function declaration is executedautomatically when ROOT is launched from the samedirectory as the file

This way all your 2-Dhistograms will havenice colours (aah!)

Returns the value ofthe system environmentvariable 'USER'Executes the system

command 'date'

For more information, see classes TStyle & TSystem

Page 102: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Exercise

Another example of analysing data

Page 103: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Exercise(Episode 1: The ROOT menace)

http://caeinfo.in2p3.fr/root/Formation/en/Day2/exo_j2.data

• Write a script to analyse the data in ASCII file exo_j2.data .Each line of the file has 4 values corresponding to variablesx,y,z et e (How original!)x de -25 à 25, y de -25 à 25, z de -10 à 10, e de -500 à 2500

– Create and fill the following histograms:● 1-D: 'z' distribution (TH1F)● 2-D: 'y' vs 'x', 'z' vs 'x', 'z' vs 'y' (TH2F)● Profiles: <e> vs x, <e> vs y (TProfile)● 3-D: 'z' vs 'y' vs 'x', 'e' vs 'y' vs 'x' (TH3F)

– Save them in a ROOT file called exo_j2.root.

Page 104: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Exercise(Episode 2: The return of

exo_j2.root)• Open exo_j2.root.• Find the 3 most populated intervals of 'z'

– Keep a note of them!• Using the FitPanel:

– fit the TProfile "<e> vs x" with a polynomial and notethe values of the last two parameters, ex1 et ex2.

– fit the TProfile "<e> vs y" with a polynomial and notethe values of the last 3 parameters, ey1, ey2 et ey3.

• Close exo_j2.root.

Page 105: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Exercise(Part 3: The analysis strikes

back)• Write another script to analyse exo_j2.data.

– Create and fill the following:● 1-D: histogram (TH1F) of de=e-ex1*x-ex2*x*x-ey1*y-ey2*y*y-ey3*y*y*y

● 2-D: 'y' vs 'x' for each 'z' interval determined in Part 2(TH2F)

● 2-D Profiles: <z> vs y vs x, <e> vs y vs x (TProfile2D)– ADD them to file exo_j2.root.

Page 106: ROOT for beginnerspetersen/Teaching/Stat2016/PythonRootIntro/R… · ObjectType* toto; You have to declare the type of object whose address this will hold (object type = class name)

Exercise(Part 4: The final shot)

• Find the width of the de distribution by fittingwith a gaussian.

• Write a script to display, on the same canvas, thefollowing 4 figures:– 'y' vs 'x' for z < 1 with option col– 'y' vs 'x' for 3 < z < 5 with option box– <z> vs y vs x with option zcol– <e> vs y vs x with option surf1

• Save the picture in a ".gif" file