Solutions for ROOT Exercise Session A

Session A

 

Question A1:

root [] f1.Derivative(2)

      (Double_t)(-4.35393825829554215e-01)

 

Question A2:

root [] f1.Integral(0,3)

      (Double_t)1.84865252799946833e+00

 

Question A3:

root [] f1.Eval(1.23456789)

      (Double_t)7.64644644211939339e-01

 

Question A4:

ACP is a concatenation of several TGraph::Draw options.

·           'A':  Axis are drawn around the graph

·           'C':  A smooth Curve is drawn

·           'P':  Idem with the current marker

Question A5:

Add or change the following lines:

 

   // add two points

   const Int_t n = 22;

  

   Double_t x[n], y[n];

   for (Int_t i=0;i<n-2;i++) {

     x[i] = i*0.1;

     y[i] = 10*sin(x[i]+0.2);

     printf(" i %i %f %f \n",i,x[i],y[i]);

   }

  

   x[20] = 2.5;

   y[20] = 6;

   x[21] = 3;

   y[21] = 4;

 

   // Insert these lines after giving the axis a title.

   // Center the title by getting the x and y axis first and

   // calling the CenterTitle method of the TAxis class

   gr->GetXaxis()->CenterTitle();

   gr->GetYaxis()->CenterTitle();

 

Question A6: 

At the end of the script  (e.g., before drawing the TPaveText), add the following code:

Double_t *gx = gr->GetX();

Double_t *gy = gr->GetY();

TArrow *arrow1 = new TArrow(1,5,gx[5],gy[5],0.03,"|>");

arrow1->Draw();

TArrow *arrow2 = new TArrow(1,5,gx[21],gy[21],0.03,"|>");

arrow2->Draw();

 

Question A7:

Use the context menu for the graph:

1.       Right click on the graph

2.       To set the line thickness and color: select the SetLineAttributes option

3.       To set the marker style and color: select the SetMarkerAttributes option

4.       To set the back ground of the canvas right click on the canvas to bring up the context menu

5.       Select SetFillAttributes and use the panel to change the color.

6.       To zoom, left click and hold on the x-axis (the cursor changes to a hand) at 0.5. Drag the cursor to the right until 3.5 and let go.

Question A8:

1.       Select the Edit from the canvas Edit menu.

2.       Click on the Arrow in the Editor menu

3.       Left-click in the frame at 3,0 and hold

4.       Drag to 2,5 and let go.

Question A9:

You can add the following statements at the end of the script:

// fitting graph with polynomial function of degree 2 to 7

char function[8];

for (Int_t i=2;i<=7;i++) {

   sprintf(function,"pol%d",i);

   gr->Fit(function,"q+");

   TF1 *f1 = gr->GetFunction(function);

   f1->SetLineColor(i);

}

 

Question A10:

The lfits points to a TList of TObjects. TList is a polymorphic container that may contain any object derived from TObject. The return type of the function TList::FindObject returns the lowest common denominator TObject*. One can find the class name of the returned object with:

TObject *object = lfits->FindObject("pol4");

object->ClassName();

 

Question A11:

Locate the history file: $HOME/.root_hist

Question A12:

In a named script, like in normal C++, all objects created on the stack in the scope of the function are automatically deleted when exiting from the function.  If an object is deleted and it is in one or more pad(s), it is automatically removed from the list of objects in the pad.