Error in the returning a vector from the function

Hi,

This is the program result:

root [0] 
Processing Lorentz_Transformation.C...
In file included from input_line_8:1:
/home/andre/Dropbox/Doutorado/FPMC/Lorentz_Transformation.C:118:3: error: assigning to 'Double_t *' (aka 'double *') from incompatible type 'Double_t' (aka 'double')
v = BOOST(px[i],py[i],pz[i],E1[i]);
  ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I am using the root 6.

The structure of the program:

#include "READT.C"
#include "WRITET.C"

//Definition of function
Double_t BOOST(Double_t , Double_t , Double_t , Double_t);

//Lorentz Transformation ...
Double_t BOOST(Double_t px, Double_t py, Double_t pz, Double_t E1){
...
...
...
Double_t *vector = (Double_t*)calloc(4,sizeof(Double_t));   

After the Lorentz Transformation

vector[0] = P0;
vector[1] = P1;
vector[2] = P2;
vector[3] = P3;

return *vector;
}


void Lorentz_Transformation(){

#define N 200000

Definition of the variables...
Double_t px[N], py[N], pz[N], E1[N], E1T[N], pxT[N], pyT[N], pzT[N], x1[N], x2[N], x3[N];

                                                                        
int lNpts = READT("file_p_E.txt",x1,x2,x3,px,py,pz,E1);

cout<<"lNpts = "<<lNpts<<endl;


Double_t  *v;


for(long i=0; i<lNpts; i++){

v = BOOST(px[i],py[i],pz[i],E1[i]);

E1T[i] = v[0];
pxT[i] = v[1];
pyT[i] = v[2];          
pzT[i] = v[3];
       
        free(v);

}

WRITET(pxT,pyT,pzT,E1T,lNpts,"Dados_Transfor_LAB.txt"); 
}

Can someone help me?
Thanks.

Cheers,
Andre

Before we start: ROOT already provides the TLorentzVector class, probably you should use that!

Your question is about returning a vector from a function. Yet in your program you are not using vector but instead are allocating memory to store 4 doubles, i.e. you have a pointer here!

To return a vector, simply change the return type of BOOST to return a vector:

std::vector<Double_t> BOOST(...) {
   ...
   return {P0, P1, P2, P3};
}

I this case you could also return a std::array<Double_t, 4> (because it has always 4 elements).

Do not work with malloc/free in C++ (that’s C without ++). Use new/delete instead. But also try to avoid new&delete! (except for ROOT classes). Use container classes instead - this avoids memory leaks and simplifies code!

Your code doesn’t work because you a returning a Double_t (i.e. ONE double value), not a pointer to double. So you could make BOOST return a Double_t* and then remove the * in the return *vector; line. But I suggest you use any of the alternatives instead.

Hi, behrenhoff

Thanks for the answer quickly! I’m going to use your tip (much better to use TLorentzVector class).

Now with the TLorentzVector the problem is gone and the program is
more simple!

Cheers,
Andre

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