Saving TVectorD to file

Hello, i have problems trying to export a TVectorD in a log file.
I made a routine that runs the PCA with TPrincipal (still trying to understand how it works), and i created a vector that gets the eigenvalues:

const TVectorD* eigen_values;
eigen_values=principal->GetEigenValues();

Now i want to create a file with the eigenvalues, so I tried with a TFile:

    TFile *lambda = new TFile("lambda.root", "RECREATE");
    eigen_values->Write();
    lambda->Close();

THe file lambda.root is full of strange symbols and gedit says that it has problems. I can’t obtain a proper result like the one that i would obtain with a Print();.

You cannot open ROOT files with a text editor. It’s a binary file format. If you want to save to a text file, just print each component into the usual FILE* or std::ostream.

For more information, see https://root.cern.ch/input-and-output.

Now i tried to create a FILE*, but i don’t have any better result because i can’t recursively read a TVectorD and print it into a file directly, neither I can first assign it to a different array and then print it to a file.

Double_t tmp[col]; 
const TVectorD *eigen_values;
FILE *lambda;

lambda=fopen("lambda.log", "w");
if(lambda == NULL){ //if file does not exist, create it
      freopen("lambda.log", "wb", lambda);
}
for(y=0; y<col; y++){
      tmp[y]=eigen_values[y];
      fprintf(lambda,"%d %lf\n",y, tmp[y]);
}
fclose(lambda);

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