Problem when reading entries from tree into matrix

Hi

i have a tree with one branch like this…

Double_t signal[N_SEG][200];

From this tree i need to load a number of entries (N) into matrices. It should be one 200xN matrix for each segment (N_SEG).

What i tried is the following (i will try to reproduce the error in an executable snippet, this is just to show what i want to do):

void MyTree::LoadToMatrices(){
    TMatrixD* A[this->N_SEG];
    for (int seg=0;seg<this->N_SEG;seg++){A[seg] = new TMatrixD(200,this->N);}    
    Long64_t nentries = this->fChain->GetEntriesFast();
    if (nentries < this->N){cout << "Error: nentries too small !!" << endl;return 0;}
    Long64_t nbytes = 0, nb = 0;
    for (Long64_t jentry=0; jentry<this->N;jentry++) {
        Long64_t ientry = LoadTree(jentry);
        if (ientry < 0) break;
        cout << jentry << endl;
        nb = fChain->GetEntry(jentry);   nbytes += nb;
        for (int seg=0;seg<N_SEG;seg++){for (int t=0;t<200;t++){
                A[seg](t,jentry) = this->signal[seg][t];
        }}
    }
}

when i run this i get the following output:

0
1
Error: Symbol #include is not defined in current scope  MyTree.C(225)
Error: Symbol exception is not defined in current scope  MyTree.C(225)
Syntax Error: #include <exception> MyTree.C(225)
Error: Symbol G__exception is not defined in current scope  MyTree.C(225)
Error: type G__exception not defined FILE:MyTree.C LINE:225
*** Interpreter error recovered ***
root [2]

this error message i get quite often. It doenst really help to identify whats wrong with the code and i tried different alternatives to create and fill the matrices but nothing worked :frowning:
what seems a bit strange to me is that the loop is executed once without any problems but the second time it crashes…
any help would be appreciated

Here is a function that reproduces the same error…

void TestMatrixCop(){
    const int N_sig = 10;
    const int N_seg = 2;
    const int N_bin = 5;
    // init signal
    Double_t signal[N_sig][N_seg][N_bin];
    for (int sig=0;sig<N_sig;sig++){for (int seg=0;seg<N_seg;seg++){
            for (int bin=0;bin<N_bin;bin++){signal[sig][seg][bin] = sig+seg+bin;}
    }}
    // init matrices
    TMatrixD* A[N_seg];
    for (int seg=0;seg<N_seg;seg++){A[seg] = new TMatrixD(N_bin,N_sig);}
    // copy values
    for (int sig =0;sig<N_sig;sig++){
        cout << sig << endl;
        for (int seg=0;seg<N_seg;seg++){
            for (int bin=0;bin<N_bin;bin++){
                A[seg](bin,sig) = signal[sig][seg][bin];                
            }
        }
    }
    cout << "DONE" << endl;
}[/code]
when i run it i get this output...
[code]root [1] TestMatrixCop()
0
1
Error: Symbol #include is not defined in current scope  C:\[...]
Error: Symbol exception is not defined in current scope  C:\[...]
Syntax Error: #include <exception> C:\[...]
Error: Symbol G__exception is not defined in current scope  C:\[...]
Error: type G__exception not defined FILE:C:\[...]
*** Interpreter error recovered ***

again the first iteration of the outer loop seems to be ok. Thus i assume accessing the matrix elements via

A[seg](bin,sig) is correct. So it might be the way i put the matrices in the array…however even when i fill the matrices in reverse order, A[N_seg-seg-1](bin,sig) = signal[sig][seg][bin]; , the output is the same. Only one matrix is filled and then it crashes.

after searching the forum i found these two posts
[url]TMatrix oddity
[url]Array of TMatrix

changing in the above code the access to the matrix elements to

fixes the problem :smiley:

I didnt take into account that my array is filled with pointers but not objects…
however I dont understand why the first iteration of the outer loop worked :question:
and still i wonder wheter there is an easier/more efficient way to place the data into a TMatrix

ps: actually this alone didnt fix my original problem. After putting the *, suddenly the loop crashed in the line

when i removed the line where i copy the values from the array to the matrix it worked again :question:
However i was lucky to find out that the trick mentioned in one of the above codes does the job.
Using

                TMatrixD &M = (*A[seg]);
                M(t,jentry) = this->signal[seg][t];

finally works :smiley:
maybe compiling the code instead of using the interpreter could prevent me from doing such mistakes / running in such problems but i prefer to have code that runs both ways if possible.