# Change type of member of a class stored in a tree

**URL:** <https://root-forum.cern.ch/t/change-type-of-member-of-a-class-stored-in-a-tree/18039>\
**Category:** ROOT\
**Created:** [August 11, 2014, 7:39am UTC](https://root-forum.cern.ch/t/change-type-of-member-of-a-class-stored-in-a-tree/18039 "2014-08-11T07:39:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![suhl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@suhl](https://root-forum.cern.ch/u/suhl)\
**Post date:** [August 11, 2014, 7:39am UTC](https://root-forum.cern.ch/t/change-type-of-member-of-a-class-stored-in-a-tree/18039/1 "2014-08-11T07:39:24Z")

</div>

Hi,

suppose I have some class A, and I want to change the type of one of its members. How can I make sure that old versions of this class can still be read? Consider the following example of class A with a member variable content of type std::vector.

```auto
class A : public TObject {
    public:
        std::vector<TComplex> content;
    ClassDef(A, 1);
};
```

Now I want to change the type of content to std::vector\<std::complex \>:

```auto
class A : public TObject {
    public:
        std::vector<std::complex<double> > content;
    ClassDef(A, 2);
};
```

As expected without any further modifications, if an old version of this class is loaded content is empty. The error messages I get are:

```auto
Warning in <TStreamerInfo::BuildOld>: Cannot convert A::content from type:vector<TComplex> to type:vector<complex<double> >, skip element
Error in <TBranchElement::SetAddress>: For content can not convert vector<complex<double> > into vector<complex<double> >
```

But, to my understanding, I should in principle be able to write some code to convert content by putting something along this line in my LinkDef.h:

```auto
#pragma read sourceClass="A" \
             source="std::vector<TComplex> content" \
             version="[1]" \
             targetClass="A" \
             target="content" \
             include="iostream, TComplex.h" \
             code="{ std::cout << "hello world" << std::endl; }"
```

Obviously the printing of “hello world” would need to be replaced by some proper conversion, however, “hello world” is not printed when I try to read an old version. But at least one of the error message from above vanishes, the one remaining is:

```auto
Error in <TBranchElement::SetAddress>: For content can not convert vector<complex<double> > into vector<complex<double> >
```

Which additional steps to I need to take to make this work?

In the attached archive you will find the code I used for the example above, A\_v1.h and A\_v2.h contain the two versions of the definition of class A, A\_v1LinkDef.h and A\_v2LinkDef.h are the corresponding LinkDef files. write.cxx is a simple program writing a single entry with an object of class A (version 1) to a tree, read\_v1.cxx successfully reads the object back using version 1 of class A. read\_v2.cxx should do the same with version 2, but does not work (yet(?)).

Cheers,  
Sebastian  
[test.tgz](https://root-forum.cern.ch/uploads/default/original/2X/f/f8111a3fb839be03867ab74cc765e07cdb6fee0b.tgz) (1.23 KB)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 15, 2014, 2:06pm UTC](https://root-forum.cern.ch/t/change-type-of-member-of-a-class-stored-in-a-tree/18039/2 "2014-08-15T14:06:27Z")

</div>

Hi Sebastian,

thanks for providing such a complete example.

in cases analogous to the one you describe, i.e. transformations of collection of objects, the rule is to be written for the types of the contained objects. For example, a class which has a data member which is a vector in version 1 and a vector **in version 2 will require a rule for the transformation of A into B.**

Now, coming to your specific case, the usage of std::complex is not yet supported. Indeed ROOT does not yet provide a custom streamer for it. The custom streamer is needed in this case since the layout of this class can be implementation dependent. I would not reccomend to persistify std::complex instances for the moment.

As a side note, looking at your code I saw you have datamodel classes which inherit from TObject without needing it. Probably you are already aware of this optimisation: [root.cern.ch/drupal/content/addi … t-classdef](http://root.cern.ch/drupal/content/adding-your-class-root-classdef) last paragraph.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 15, 2014, 3:04pm UTC](https://root-forum.cern.ch/t/change-type-of-member-of-a-class-stored-in-a-tree/18039/3 "2014-08-15T15:04:17Z")

</div>

The “[Adding Your Class to ROOT: ClassDef](http://root.cern.ch/drupal/content/adding-your-class-root-classdef)” web page contains a bit not-up-to-date informations.  
See:

> [@Simple way to create and merge shared libraries?](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/6):
>
> [quote]as far as I remember, you need “ClassDef” / “ClassImp” macros for a class only if you want to use ROOT I/O on its objects. So, if you do NOT need it, you can remove them (even if the class inherits from TObject).[/quote]Please note that neither ClassDef nor ClassImp is needed to to ROOT I/O. All you need for ROOT I/O is to generate the dictionary for the class. Note that ClassDef provides an optimizations, object with a ClassDef will be smaller on file and will be read slightly faster. …

> [@Adding a class: hand holding requested](https://root-forum.cern.ch/t/adding-a-class-hand-holding-requested/13893/10):
>
> I’d just change “.cxx” into “.cc” directly here … and learn the “-p” flag … you will often need it … rootcint -f [dictionary.cc](http://dictionary.cc) -c -p TestClass.hh Root\_LinkDef.h Also, as you plan to use “std:: vector”, maybe it would be good to: #include in your “TestClass.hh” file. One more thing. If you ever use “ClassDef”, remember that it must be the last line in your class definition AND it must be PUBLIC: class MyClass { public: // something protected: // something else private: // yet somethi…

See also: [ROOT User’s Guide → Adding a Class](http://root.cern.ch/root/html534/guides/users-guide/AddingaClass.html)
