How Can I Optimize My ROOT Code for Array Initialization and Graphing?

In summary, the code provided is written in C++ and uses the ROOT scientific software framework. The goal of the conversation is to create two arrays, a and b, with specific values and then graph them in the form a=mb, with a slope of m=2. The code includes for loops to create the arrays and output them in a vertical list. The second part of the conversation involves creating a graph using the pre-built code in ROOT. There is a suggestion to use the float type instead of double, but it is not necessary in this case.
  • #1
RJLiberator
Gold Member
1,095
63

Homework Statement


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.

Homework Equations

The Attempt at a Solution



Code:
{
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.
 
Physics news on Phys.org
  • #2
As an update, I have had success creating the graph with the help of some pre-built code.

Code:
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
RJLiberator said:
C:
{
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;
}
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.
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.
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();
}

RJLiberator said:
My professor suggested using the "Float" command some how... I am not sure how this one works.
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.
 
  • Like
Likes RJLiberator
  • #4
What is ROOT? Your code appears to be C++.

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++!

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

Some advice: In your for loop, space things out -- it makes things easier for humans to read.
Ah, that clears up some minor confusion I've been having.

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.

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:
  • #5
RJLiberator said:
As an EDIT: I removed the "_t" part from int_t and double_t and the graph no longer showed when I ran it. :/
Did you change all three of these lines in your CurrentGraph() function?
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.
 
  • Like
Likes RJLiberator
  • #6
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
RJLiberator said:
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.
That would do it. All of the built-in types in C and C++ are lower-case: char, int, long, float, double, etc.

I'm surprised your code would even compile if you had Int instead of int.
 
  • Like
Likes RJLiberator
  • #8
@Mark44 is there anything you can tell me about the following command:

Code:
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:
  • #9
RJLiberator said:
@Mark44 is there anything you can tell me about the following command:

Code:
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?
Here's the context of your question.
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.
RJLiberator said:
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.
Yes.
 
  • Like
Likes RJLiberator
  • #10
RJLiberator said:
what does \n signify?
\n means only 'start a new line' for the printout.
 
Last edited:
  • Like
Likes RJLiberator

What is ROOT Code?

ROOT Code is a software framework used for data analysis, simulation, and visualization in high-energy physics research. It is written in C++ and provides a wide range of tools and libraries for data processing and analysis.

How can I get started with ROOT Code?

If you are a beginner, the best way to get started with ROOT Code is to go through the official tutorials and documentation provided on the ROOT website. They will help you understand the basic concepts and syntax of ROOT Code and give you hands-on experience on how to use it for data analysis.

What are some common mistakes made by beginners while coding in ROOT?

Some common mistakes made by beginners while coding in ROOT include not properly declaring variables, using incorrect data types, and not understanding the structure of the code. It is important to carefully read the documentation and practice writing code to avoid these mistakes.

Is ROOT Code only used in high-energy physics research?

No, while ROOT Code was originally developed for high-energy physics research, it is now used in various fields such as medical imaging, geoscience, and finance. It can be applied to any field that requires data analysis and visualization.

How can I get help with my ROOT Code if I get stuck?

If you get stuck while coding in ROOT, there are several resources you can turn to for help. The ROOT forum is a great place to ask questions and get support from the community. You can also refer to the official documentation and tutorials or reach out to experienced programmers for guidance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top