Using TVectord for data analysis

Hi there!
I only have a question.I’ve recived this error while using the TVectorD
Error in <TVectorT::operator()>: Request index(20) outside vector range of 0 - 20
Error in <TVectorT::operator()>: Request index(20) outside vector range of 0 - 20
Error in <TVectorT::operator()>: Request index(20) outside vector range of 0 - 20
Error in <TVectorT::operator()>: Request index(20) outside vector range of 0 - 20
Also I’m new in root, there is a method to add values in a TVectorD while reading a file? I used this method, but I know that it’s not functional at all.
Hope it’s not a waste of your time.
This is my code:

 TCanvas *c1 = new TCanvas("c1", "Canvas", 200, 10, 2000, 900); //creo il canvas sul quale mettere il grafico (TGraph)
    c1->SetGrid();
 



    vector<double> err_p_fabio;
    vector<double> err_q_fabio;
    vector<double> p_fabio;
    vector<double> q_fabio;
    ifstream fab("../File/coniugati_fabio.txt");
    double f;
    int i = 1;
    while (fab >> f)
    {
        if (i == 1)
            p_fabio.push_back(f);
        if (i == 2)
            q_fabio.push_back(f);
        if (i == 3)
            err_p_fabio.push_back(f);
        if (i == 4)
        {
            err_q_fabio.push_back(f);
            i = 0;
        }
        i++;
    }
    TVectorD x(p_fabio.size());
    for (int q = 0; q <= p_fabio.size(); q++)
    {
        x(q) = p_fabio[q];
    }

    TVectorD y(q_fabio.size());
    for (int w = 0; w <= err_q_fabio.size(); w++)
    {
        y(w) = q_fabio[w];
    }
    TVectorD err_x(err_p_fabio.size());
    for (int q = 0; q <= err_p_fabio.size(); q++)
    {
        err_x(q) = err_p_fabio[q];
    }

    TVectorD err_y(err_q_fabio.size());
    for (int w = 0; w <= err_q_fabio.size(); w++)
    {
        err_y(w) = err_q_fabio[w];
    }

    TGraphErrors *fabio = new TGraphErrors(x, y, err_x, err_y);
    fabio->Draw("AP");

Hi Fabio,

I’m no TVector expert, but I’m pretty sure you have to replace your loop conditions

q <= p_fabio.size()
w <= err_q_fabio.size()
q <= err_p_fabio.size()
w <= err_q_fabio.size()

with

q < p_fabio.size()
w < err_q_fabio.size()
q < err_p_fabio.size()
w < err_q_fabio.size()

2 Likes

Hi,
@yus correctly pointed out the source of the error messages.

Anyways, from the docs, the simplest method is probably:

TVectorD y(q_fabio.size(), q_fabio.data()); // this copies q_fabio into y

Cheers,
Enrico

1 Like

Thank you so much @eguiraud . I put your advice in my program. Coluld I ask you a question? Can I insert elements in a TVectorD dynamically, like push_back () for vector in C ++?

It’s possible but awkward:

v.ResizeTo(n+1);
v[n]= value; 

However, you should just use std::vectors if you need to perform these kind of operations, and convert them to TVectorDs when you need to perform matrix/vector mathematical operations or when you need a TVectorD to pass to some ROOT API.

Cheers,
Enrico

1 Like

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