Writing TArray to file

Hi,

I want to store a TArrayD in a file, but it doesn’t work with array->Write(), because it is not derived from TObject.

The method for writing those objects mentioned in the User’s Guide (f->WriteObject(“array”, array)) does not work, too.

To write a useless Wrapper-Class which inherits from TObject is unsatisfying.

Is anybody experienced in this area? Code examples would be nice :wink:

Thank you in advance. Johannes Kissel.

[quote]The method for writing those objects mentioned in the User’s Guide (f->WriteObject(“array”, array)) does not work, too.[/quote]How does it not work?

Philippe

On page 164 of the User’s Guide v5.14 “Saving Objects to Disk”:

For objects that do not inherit from TObject use:

My simple code for testing:

[code] // fill a TArrayD
int len = mxGetDimensions(prhs[0])[1];
TArrayD numbers(len, mxGetPr(prhs[0]));

// save it to file
TFile f(“test.root”, “recreate”);
f.WriteObject(numbers, “numbers”);
f.Close();
[/code]
And the result:

OK, I’m a C++ and ROOT beginner, but I just don’t get it… :confused:

I think you should invoke the WriteObject method with a pointer to your TNumberD object.
That is,

  f.WriteObject(&numbers, "numbers"); 

I get a segmentation violation. Maybe I do something wrong…

TVectorT seems to be, what I’m looking for. It inherits from TObject :slight_smile:

Thanks for your help.

[quote]I get a segmentation violation. Maybe I do something wrong…[/quote]I can not reproduce this. The following should work:

[code] // fill a TArrayD
int len = mxGetDimensions(prhs[0])[1];
TArrayD numbers(len, mxGetPr(prhs[0]));

// save it to file
TFile f(“test.root”, “recreate”);
f.WriteObject(&numbers, “numbers”);
f.Close(); [/code]

Philippe

Maybe my problems are caused by compiling it not with .L test.cpp++

[quote]Maybe my problems are caused by compiling it not with .L test.cpp++[/quote]If you are not compiling with “.L test.cpp++”, how are you compiling?

Philippe

I need ROOT as data storage in a MATLAB-Environment. That's why...

I guess some day I'll get rid of all those C++-Problems :wink: But at the moment my project is in progress and I'm happy with TVectorT.

I need ROOT as data storage in a MATLAB-Environment. That’s why…

I guess some day I’ll get rid of all those C+±Problems :wink: But at the moment my project is in progress and I’m happy with TVectorT.

I just had a new look at the problem with the segmentation violation. I found out that this happens only when a file with not-TObject-derived-objects is opened in the object browser, whereas there’s no problem to load these objects in the CINT shell.

My error message:

*** Break *** segmentation violation Using host libthread_db library "/lib/libthread_db.so.1". Attaching to program: /proc/27663/exe, process 27663 [Thread debugging using libthread_db enabled] [New Thread -1224513840 (LWP 27663)] Failed to read a valid object file image from memory. 0xb7fd0410 in ?? () #1 0xbfc10ff8 in ?? () #2 0x00000000 in ?? ()

[size=75]Maybe this could be a bug in the browser…?[/size]

How can I determine an objects type if it’s not derived from TObject? I don’t know how to use the Class() method of TArray types.

Isn’t there any integer array type derived from TObject. I didn’t find one :unamused:

What’s the reason for so many base types? Is it historical?

Thank you for your answers!

You can get a tclassObject via:TClass::GetClass(classname)orTClass::GetClass(typeid<mytype>)
orTClass::GetClass(typeid<*mypointer>)

[quote]What’s the reason for so many base types? Is it historical? [/quote]Depends which one :slight_smile: Some are historical, some are needed for implementation reason, some are logical needed.

[quote]Isn’t there any integer array type derived from TObject. I didn’t find one[/quote]No there is not.

[quote]
I just had a new look at the problem with the segmentation violation. I found out that this happens only when a file with not-TObject-derived-objects is opened in the object browser, whereas there’s no problem to load these objects in the CINT shell.[/quote]

This problem has been solved in the CVS repository (5.17/03)

Cheers,
Philippe