vector<TGraph*> problem

Hi,

I have problem creating a vector containing TGraphs* and use a for loop to create multiple graphs. I think it is because when I loop, I overwrite some quantities, and TGraph doesn’t like it. What should I do ?

Here’s a simpler version but contains the essence of my problem:

vector<TGraph*> graph;

for(int j = 0; j != 3; j++){
const int n = 20;
double x[n], y[n];
for (int i=0; i<n; i++) {
x[i] = i0.1;
y[i] = 10
sin(x[i]+0.2);
}
graph.push_back(new TGraph(n,x,y));
}

TCanvas* c = new TCanvas(“c”,"",150,10,990,660);
c->cd();
graph[0]->Draw(“AC*”);

upon executing, ROOT gives this error:

Error: C++ exception caught _vector.h(150)
void(0)

If I compile basically the same code it works:

test.C:

#include <vector>
#include "TGraph.h"
#include "TCanvas.h"

void test()
{

   vector< TGraph* > graph;

   for (int outerLoop = 0; outerLoop != 3; ++outerLoop)
   {
      const int num = 20;
      double xArr[num], yArr[num];
      for (int innerLoop = 0; innerLoop < num; ++innerLoop) 
      {
         xArr[innerLoop] = innerLoop*0.1;
         yArr[innerLoop] = 10 * sin( xArr [innerLoop] + 0.2 );
      }
      graph.push_back( new TGraph (num, xArr, yArr) );
   }
   
   TCanvas* c = new TCanvas("c","",150,10,990,660);
   graph[0]->Draw("AC*"); 
}

where

root [0] .L test.C+
Info in <TUnixSystem::ACLiC>: creating shared library /cdf/home/cplager/./test_C.so
In file included from /cdf/home/cplager/./test_C_ACLiC_dict.h:33,
                 from /cdf/home/cplager/./test_C_ACLiC_dict.cxx:16:
/cdf/home/cplager/./test.C: In function `void test()':
/cdf/home/cplager/./test.C:22: warning: unused variable 'c'
root [1] test()

My guess is you’re running into a Cint limitation.

Cheers,
Charles

[quote=“cplager”]If I compile basically the same code it works:

test.C:

#include <vector>
#include "TGraph.h"
#include "TCanvas.h"

void test()
{

   vector< TGraph* > graph;

   for (int outerLoop = 0; outerLoop != 3; ++outerLoop)
   {
      const int num = 20;
      double xArr[num], yArr[num];
      for (int innerLoop = 0; innerLoop < num; ++innerLoop) 
      {
         xArr[innerLoop] = innerLoop*0.1;
         yArr[innerLoop] = 10 * sin( xArr [innerLoop] + 0.2 );
      }
      graph.push_back( new TGraph (num, xArr, yArr) );
   }
   
   TCanvas* c = new TCanvas("c","",150,10,990,660);
   graph[0]->Draw("AC*"); 
}

where

root [0] .L test.C+
Info in <TUnixSystem::ACLiC>: creating shared library /cdf/home/cplager/./test_C.so
In file included from /cdf/home/cplager/./test_C_ACLiC_dict.h:33,
                 from /cdf/home/cplager/./test_C_ACLiC_dict.cxx:16:
/cdf/home/cplager/./test.C: In function `void test()':
/cdf/home/cplager/./test.C:22: warning: unused variable 'c'
root [1] test()

My guess is you’re running into a Cint limitation.

Cheers,
Charles[/quote]

I compiled this code of yours, and still the same errors. What is this Cint limitation and how do I go around it ? Has this something to do with the version of ROOT I am using ?

[quote]I compiled this code of yours, and still the same errors. [/quote]This is very surprising. The error you mentioned “Error: C++ exception caught _vector.h(150)” can only happen if there is an interpreted vector involved … which could not possibly happen if you compile (using ACLiC) the code snippet Charles provided. Can you copy/paste the session you ran that lead to the problem (when compiling)?

[quote]What is this Cint limitation and how do I go around it ? Has this something to do with the version of ROOT I am using ?[/quote]The problem is that CINT can not (yet) properly fully interpret vector of pointer to object (The interpreter can only ‘use’ compiled instance of those type of vector).

Cheers,
Philippe.

[quote=“pcanal”][quote]I compiled this code of yours, and still the same errors. [/quote]This is very surprising. The error you mentioned “Error: C++ exception caught _vector.h(150)” can only happen if there is an interpreted vector involved … which could not possibly happen if you compile (using ACLiC) the code snippet Charles provided. Can you copy/paste the session you ran that lead to the problem (when compiling)?

[quote]What is this Cint limitation and how do I go around it ? Has this something to do with the version of ROOT I am using ?[/quote]The problem is that CINT can not (yet) properly fully interpret vector of pointer to object (The interpreter can only ‘use’ compiled instance of those type of vector).

Cheers,
Philippe.[/quote]

It works now. I didn’t know that ACLiC refers to that plus (+) symbol after you do .L test… +
I omitted it previously.