Root crashes while filling out a 2D array in a nested FOR loop

Hi experts

So I’m using a macro (attached here) to grab the start and end points of particle tracks and then take the dot product of the two kinds of tracks (MC and Reco). I want to take the dot product of every track in the MCTracktree with every track in the RecoTracktree. The issue I’m having is that while calculating the x-component of the dot product, I choose to fill out a 2-D array which goes over the start and end points of both MC and Reco tracks and when I try to fill out the array Root crashes.
The portion of code that is of interest is here:

    Int_t nentries = (Int_t)t1->GetEntries();
    Int_t mentries = (Int_t)t2->GetEntries();
    
    Double_t xprod[nentries][mentries],yprod[nentries][mentries],zprod[nentries][mentries];
    int counter1=0;
    int counter2=0;
    for (Int_t i=0;i<nentries;i++) {
        b_muontrack_startX->GetEntry(i);
        b_muontrack_endX->GetEntry(i);
      
        counter1++;
        cout<<"Counter1: "<<counter1<<endl;
          for (Int_t j=0;j<mentries;j++) {
                    counter2++;
              cout<<"Counter2: "<<counter2<<endl;
              b_recotrack_startX->GetEntry(j);
              b_recotrack_endX->GetEntry(j);
           
              xprod[i][j]=(muontrack_endX-muontrack_startX)*(recotrack_endX-recotrack_startX);
           //  cout <<"xprod is: "<<xprod[i][j]<<endl;
             }
    }

As soon as I comment out the line :

xprod[i][j]=(muontrack_endX-muontrack_startX)*(recotrack_endX-recotrack_startX);
             cout <<"xprod is: "<<xprod[i][j]<<endl;

it works fine.
I have no idea why root is crashing. The way it is crashing is like this:

-bash-4.1$ root -l
root [0] .L readTTree.C 
root [1] readTTree()
-bash-4.1$ 

And everytime I choose to comment out the above line which I mentioned, the nested FOR loop does what it’s supposed to do:

Counter1: 1
Counter2: 1
Counter2: 2
Counter2: 3
Counter2: 4
Counter2: 5
Counter2: 6
Counter2: 7
Counter2: 8
Counter2: 9
Counter2: 10
Counter2: 11
Counter2: 12
Counter2: 13
Counter2: 14
Counter2: 15
Counter2: 16
Counter2: 17
Counter2: 18
Counter2: 19
Counter2: 20
Counter2: 21
Counter2: 22
Counter2: 23
Counter2: 24
Counter2: 25
Counter2: 26
Counter2: 27
Counter2: 28
Counter2: 29
Counter2: 30
Counter2: 31
Counter2: 32
Counter2: 33
Counter2: 34
Counter2: 35
Counter2: 36
Counter2: 37
Counter2: 38
Counter2: 39
Counter2: 40
Counter2: 41
Counter2: 42
Counter2: 43
Counter2: 44
Counter2: 45
Counter2: 46
Counter2: 47
Counter2: 48
Counter2: 49
Counter2: 50
Counter2: 51
Counter2: 52
Counter2: 53
Counter2: 54
Counter2: 55
Counter2: 56
Counter2: 57
Counter2: 58
Counter2: 59
Counter2: 60

and blah blah blah...

Anybody know what am I doing wrong here?
Any help is appreciated. Let me know if you need more info.
Thanks !
readTTree.C (6.8 KB)

Just an update. I replaced the line:

Int_t nentries = (Int_t)t1->GetEntries();
    Int_t mentries = (Int_t)t2->GetEntries();
    
    Double_t xprod[nentries][mentries],yprod[nentries][mentries],zprod[nentries][mentries];

with:

// Int_t nentries = (Int_t)t1->GetEntries();
//  Int_t mentries = (Int_t)t2->GetEntries(); 
    Int_t nentries = 4597, mentries = 5002;
    Double_t xprod[4597][5002],yprod[4597][5002],zprod[4597][5002],dotprod[4597][5002];

where the number allocated to nentries and mentries are the actual entry sizes.
And it worked !

Could somebody tell me why I can’t declare an array with a variable ?
And ultimately I need to declare the array with the variables and not with actual numbers, so how do I go on doing that?

Thanks !

Thank you @Wile_E_Coyote. It was very simple after all.
Following the link here : http://www.cplusplus.com/forum/articles/7459/
I made the following changes and it worked :

Int_t nentries = (Int_t)t1->GetEntries();
Int_t mentries = (Int_t)t2->GetEntries();

vector<vector<Double_t>> xprod;

for (Int_t i=0;i<nentries;i++) {

   xprod[i].resize(mentries);

   b_muontrack_startX->GetEntry(i);
   b_muontrack_endX->GetEntry(i);

  for (Int_t j=0;j<mentries;j++) {
      b_recotrack_startX->GetEntry(j);
      b_recotrack_endX->GetEntry(j);

      xprod[i][j]=(muontrack_endX-muontrack_startX)*(recotrack_endX-recotrack_startX);
  }
}


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.