# Using a matrix

Hello rooters

I wrote a small toy montecarlo in order to estimate a solid angle. Everything works fine. But if I increase the number of simulation points to more than 10’000000 (takes 3min calculation time) root crashes and I always get the same error message:

Error: malloc failed for ptdir[anzahl]

Ptdir is a matrix: Float_t ptdir[anzahl];
Anzahl denotes the number of simulation points (rows) and 6 is the number of columns (3 for particle coordinates and 3 for particle direction) of the matrix ptdir.

How can I use more than these 10’000000 simulation points (I don’t want to take 10’000000 simulation points and then make a loop)

Thanks a lot for your help…

Cheers, Christian

Hi,

You need to allocate that memory on the heap instead of the stack.
So either use an stl container (for std::vector) or new you array:

```Float_t *m = new Float_t[large_number]; std::vector<Float_t> m;```Or look at the matrix package (in particular if your matrix is sparse).
Cheers,
Philippe

I’m sorry that it took me so long to reply…

Unfortunately I still have the same problem I also have another question:
How can I set the user range in x-direction?
More precisely: Let’s say I have data with x-values in the interval [3,10].
If I want to zoom in, for example from 5 to 7 then I can simply use

graph->GetXaxis()->SetRangeUser(5,7);

But how do I do it, if I want the Canvas to reach from let’s say 0 to 15?
SetRangeUser only works to zoom in. In y-direction the command:

graph->SetMinimum(…);
graph->SetMaximum(…);

works fine even if I want to have a Canvas reaching from 0 to 15 with data in the range [3,10].

``````{
Int_t n=20;
Double_t x[n],y[n];
for (Int_t i=0; i < n; i++) {
x[i]=i*0.1;
y[i]=10*sin(x[i]+0.2);
}
TGraph *gr1 = new TGraph (n,x,y);
TAxis *axis = gr1->GetXaxis();

axis->SetLimits(0.,5.);                 // along X
gr1->GetHistogram()->SetMaximum(20.);   // along
gr1->GetHistogram()->SetMinimum(-20.);  //   Y

gr1->Draw("AC*");
}``````

Thanks a lot, works fine…

Cheers

Christian

I found the reason why root crashes when I increase the number of simulation points (i.e. the size of my matrix).

Matrix is float (32bit=8byte);
The dimension is 6 columns times 1E8 rows.

Therefore the size of the matrix is 4.8E9 bytes which is 4.8 gigabyte. But my laptop only possesses 2giga ram…