Vector out of range

I am runnig a program that loops ~100,000 times. but it is going through this for loop once and then is returning
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
It compiles fine.
lines comented out were previous problem sections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
for (int i=1;i<=ph_n; i++)
{
    float ph=ph_pt->at(i);
    //TLorentzVector pht;
    //      pht.SetPtEtaPhiE(ph,ph_eta->at(i),ph_phi->at(i),ph_E->at(i));

    //if(PassPhTight(i)==false){continue;}
    float phphi=ph_phi->at(i);
    float pheta=ph_eta->at(i);
    float ph_px=ph*cos(phphi);
    float ph_py=ph*sin(phphi);
    float ph_pz=ph*sinh(pheta);
    float phE=ph_E->at(i);
    float ph_theta=2*atan(exp(-1*pheta));
    //cout <<"b"<<endl;
    for(int j=1;j<el_n;j++)
    {
      float el1=el_pt->at(j);
      //cout <<"e" <<endl;
      //if (el1<15000){continue;}
      //cout <<"g" <<endl;
      //if (PassElTightPP(j)==false){continue;}
      //cout <<"f" <<endl;
      int el1c=el_charge->at(j);

      //cout <<"i" <<endl;
      //ut <<"a" <<endl;
      //TLorentzVector el1t;
      //     el1t.SetPtEtaPhiE(el1,el_eta->at(j),el_phi->at(j),el_E->at(j));
     float e1eta=el_eta->at(j);
     for (int l=0; l<=el_n && l!=j; l++){
        //cout <<"c" <<endl;
        float el2=el_pt->at(l);
       //if (el2<15000){continue;}
       //if (PassElTightPP(j)==false){continue;}
       int el2c=el_charge->at(l);
       //cout <<"a" <<endl;
       if (el1c!=(-1)*el2c){continue;}
       //cout <<"a" <<endl;
       // TLorentzVector el2t;
      //      el2t.SetPtEtaPhiE(el2,el_eta->at(l),el_phi->at(l),el_E->at(l));
      float e2eta=el_eta->at(l);
      //cout <<e2eta <<endl;
      //TLorentzVector elst;
      //      elst=el1t+el2t;
      //float ph_theta=2*arctan(exp(-1*ph_eta));
      float z_theta=2*atan(exp(-1*e1eta+e2eta));
      //cout <<el1 <<endl;
      float seperation=abs(ph_theta-z_theta);
      //cout <<el2 <<endl;
      //if (seperation<=17*PI/18){continue;}
      cout <<el2/el1 <<endl;
     }
   }
   file2 <<phE <<endl;
   //cout <<"a" <<endl;
   file2 <<ph_px <<endl;
   //cout <<"b" <<endl;
   file2 <<ph_py <<endl;
   //cout <<"c" <<endl;
   file2 <<ph_pz <<endl;
   cout <<"seperation" <<endl;
}
Last edited on
Well the problem is pretty clear. You're accessing outside of the bounds of your vector.

This raises a huge red flag for me:

for (int i=1;i<=ph_n; i++)

I don't know what ph_n is, but if it's the size of your vector, this is wrong.

Remember that indexes always start at zero, not at 1. So if your array has 5 elements... then element #5 is out of bounds (only elements 0 - 4 are valid).

Typical for loops look like so:
for (int i=0;i<ph_n; i++)

Note how I start at zero and do < ph_n instead of <= ph_n
Yep, I realized right after I posted that that was the answer thank you though.
Topic archived. No new replies allowed.