Creating multiple graphs with TMultigraph for N Graphs

Hello, kind of new user here so apologies if question is naive.

I’m Using root for creating a graph of random walks, so far a simple random walk works and I can create a graph for the code, my question comes to how do I scale it to show multiple number of walks using TMultiGraph()

I’m also quite rusty in C++ so if I have to make a for or while loop to declare the walker, that’s something that I don’t know how to do it.

Here’s the code I wrote for it and the graph it creates.

// Random Walk 
#import <time.h>
{
  g = new TGraph();


  int i, y=1;
  srand(time(NULL));
  double p = 0.5;
    for(int i=0; i<=100; i++){
      if (rand()%2<p){
	y +=1;
        }
    else{
      y +=-1;
    }      
    g->SetPoint(i,i,y);
  }
    g->Draw("AL*");
  }
  

Thanks in advance

ROOT Version: 6.24.02
Platform: Ubuntu
Compiler: C++


{
  TMultiGraph *mg = new TMultiGraph();
  srand(time(NULL));
  for (int j = 0; j < 8; j++) {
    TGraph *g = new TGraph();
    double p = 0.5;
    int y = 1;
    for(int i = 0; i <= 100; i++) {
      if (rand()%2 < p) y += 1; else y -= 1;
      g->SetPoint(i, i, y);
    }
    g->SetLineColor(j + 1); g->SetMarkerColor(j + 1);
    mg->Add(g);
  }
  mg->Draw("AL*");
}
1 Like