# Help me with my ROOT Code [level: Beginner]

1. Jan 13, 2016

### RJLiberator

1. The problem statement, all variables and given/known data
The goal is to create two arrays that have the values
a = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

And then graph them so that a=mb
slope m thus being 2.

I am working on the first part before I graph them.

2. Relevant equations

3. The attempt at a solution

Code (Text):

{
int a[10];
for(int n=0; n<10; n++)
{
a[n] = 2+2*n;
}
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl;
int b[10];
for(int r=0; r<10; r++)
{
b[r] = r+1;
}
cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<" "<<b[5]<<" "<<b[6]<<" "<<b[7]<<" "<<b[8]<<" "<<b[9]<<endl;
}

My question:
a) Is there anything I can do to make the code shorter?
b) My professor wants the output to be in the form a[10] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 } instead of vertical list. That's why I had to do the cout<<....<<endl. Is there anyway to make this output more easier than simply listing all the a[1]<<a[2]... etc.?
c) If I've done everything correctly here, how do I go from having these 2 arrays to inserting them into a simple xy graph? Any pointers to get me started there? I'm looking through tutorials now.

2. Jan 13, 2016

### RJLiberator

As an update, I have had success creating the graph with the help of some pre-built code.

Code (Text):

void CurrentGraph() {

TCanvas *c1 = new TCanvas("c1","V=IR",200,10,700,500);

c1->SetFillColor(42);
c1->SetGrid();

const Int_t n = 10;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i+1;
y[i] = 2*x[i];
printf(" i %i %f %f \n",i,x[i],y[i]);
}
TGraph *gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("I = V/r");
gr->GetXaxis()->SetTitle("Resistance");
gr->GetYaxis()->SetTitle("Voltage");
gr->Draw("ACP");

c1->Update();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);
c1->Modified();
}

With that being stated, is there any better way to do what I am trying to do?

My professor suggested using the "Float" command some how... I am not sure how this one works.

3. Jan 14, 2016

### Staff: Mentor

What is ROOT? Your code appears to be C++.

Here is your code from post 1, indented for better readability, and incorporating your output into the two for loops.
Code (C):

{
int a[10];
for(int n=0; n<10; n++)
{
a[n] = 2+2*n;
cout << a[n] << " ";
}
cout << endl;
// cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl;

int b[10];
for(int r=0; r<10; r++)
{
b[r] = r+1;
cout << b[r] << " ";
}
cout << endl;
// cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<" "<<b[5]<<" "<<b[6]<<" "<<b[7]<<" "<<b[8]<<" "<<b[9]<<endl;
}

There is a slight difference between what you did and what I have in the code just above: after the last array item is printed, my code will add one space and then the end-of-line character.

In your code in post 2, why did you switch from int to Int_t? I don't seen any advantage in using Double_t instead of double, either.
Some advice: In your for loop, space things out -- it makes things easier for humans to read.
The loop should look like this:
for (Int_t i = 0; i < n; i++)
Notice the extra spaces I added.
Here's your code from post 2. My only change is the indentation of the for loop.
Code (C):
void CurrentGraph() {

TCanvas *c1 = new TCanvas("c1","V=IR",200,10,700,500);

c1->SetFillColor(42);
c1->SetGrid();

const Int_t n = 10;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++)
{
x[i] = i+1;
y[i] = 2*x[i];
printf(" i %i %f %f \n",i,x[i],y[i]);
}
TGraph *gr = new TGraph(n,x,y);
gr->SetLineColor(2);
gr->SetLineWidth(4);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(21);
gr->SetTitle("I = V/r");
gr->GetXaxis()->SetTitle("Resistance");
gr->GetYaxis()->SetTitle("Voltage");
gr->Draw("ACP");

c1->Update();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);
c1->Modified();
}
float is a type, not a command. Your code from post 2 declares your arrays to be of type double, which is another floating point type. The float type is generally 4 bytes, while the double type is generally 8 bytes, so can store larger (or smaller) numbers with higher precision. Double_t, which you are using, is probably just an alias for double, so in your code, I don't see any reason to use float instead.

4. Jan 14, 2016

### RJLiberator

Root is a modular scientific software framework. It provides all the functionalities needed to deal with big data processing, statistical analysis, visualisation and storage. It is mainly written in C++ but integrated with other languages such as Python and R.
https://root.cern.ch/
My course in Computational and Mathematical physics is working with ROOT. So, basically C++!

Ignorance on my part. So, essentially int = int_t and it would be simpler to use int. Not sure at all why the code was using int_t then. I will try it out with just int.

Ah, that clears up some minor confusion I've been having.

That makes it very, very clear to me and those words have helped me understand what I am doing a lot.
I thank you sir. I am going to try out some things, and perhaps report back.

As always, Mark44, you are truly greatness. :)

As an EDIT: I removed the "_t" part from int_t and double_t and the graph no longer showed when I ran it. :/

Last edited: Jan 14, 2016
5. Jan 14, 2016

### Staff: Mentor

Did you change all three of these lines in your CurrentGraph() function?
Code (C):
const Int_t n = 10;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++)
I don't have any idea why changing Double_T and Int_t to double and int would cause the problem you said.

6. Jan 14, 2016

### RJLiberator

Yes, I change all three lines.
The program runs, but when it comes up, it appears that the background color that I set is covering over everything.
It is as if the graph part of it is hidden behind the background when I make the three changes.

EDIT: Got it.
The problem was that When doing Int_t the Int part was capitalized.
So I had to change Int_t to int instead of Int. Same thing with double.
:) Cheers.

7. Jan 14, 2016

### Staff: Mentor

That would do it. All of the built-in types in C and C++ are lower-case: char, int, long, float, double, etc.

8. Jan 14, 2016

### RJLiberator

@Mark44 is there anything you can tell me about the following command:

Code (Text):
printf(" i %i %f %f \n",i,x[i],y[i]);
I understand that this command prints the values out into the command terminal.

But why does %i go from 0 to 10 and then the first %f list the array from 1 to 10 and then the next %f list the array that goes 2, 4, 6, 8, etc. And what does \n signify?

Then why do we need another i after that before x[ i ], y[ i ]?

My only thought is that the are connected in the order they come. The first %f goes with x[ i ] and similarly the second %f goes with y[ i ] based on the way the code was written.

Last edited by a moderator: Jan 14, 2016
9. Jan 14, 2016

### Staff: Mentor

Here's the context of your question.
Code (C):
const Int_t n = 10;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++)
{
x[i] = i+1;
y[i] = 2*x[i];
printf(" i %i %f %f \n",i,x[i],y[i]);
}
%i is not a variable, so it doesn't take on values. In the printf() call, the first argument is the control string. It consists of literals that will be printed (such as i and \n) and conversion specifiers (%i and %f). The 2nd, 3rd, and 4th arguments are expressions whose values will be displayed based on the conversion specifiers in the control string, in the same order.

So it's not %i that goes from 0 to 10 -- the loop control variable goes from 0 through 9. Each iteration of the loop causes another element of each array to be set, and causes the printf() function to be called (don't call it a "command" -- you'll seem like a newbie).

The first time through the loop, x[0] gets set to 1, y[0] gets set to 2, and printf() prints this:
i 0 1.0 2.0
and moves the cursor to the next line, as a result of the \n (newline) character.
The next iteration of the loop prints the next value of i (1) and the two array values, and so on, until the loop finishes.
Yes.

10. Jan 14, 2016

### rootone

\n means only 'start a new line' for the printout.

Last edited: Jan 14, 2016