# Problem with Writing and Reading Custom Class Objects from TTrees

**URL:** <https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490>\
**Category:** Newbie\
**Created:** [January 21, 2022, 4:02pm UTC](https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490 "2022-01-21T16:02:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://root-forum.cern.ch/u/andrew)\
**Post date:** [January 21, 2022, 4:02pm UTC](https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490/1 "2022-01-21T16:02:21Z")

</div>

Greetings,

I would like to write and read custom classes in _TTrees_, but I am facing an issue. I have created a custom class, illustrated below (Name of the files is _class.hpp_ and _class.cpp_, respectively)

**class.hpp**

```auto
#pragma once

class myclass : public TObject
{
        private:
                Double_t Charge;
                Double_t X;
                Double_t Y;
                Double_t Z;
        public:
                myclass();
                ~myclass();
                Double_t getX();
                Double_t getY();
                Double_t getZ();
                Double_t getCharge();
                void Set(Double_t, Double_t, Double_t, Double_t);
                ClassDef(myclass, 1)
};

```

**class.cpp**

```auto
include <TObject.h>
#include "class.hpp"
#include <TClass.h>

myclass::myclass(){}
myclass::~myclass(){}
void myclass::Set(Double_t _X, Double_t _Y, Double_t _Z, Double_t _charge)
{
        X = _X; Y = _Y; Z = _Z; Charge = _charge;
}
Double_t myclass::getX(){return X;}
Double_t myclass::getY(){return Y;}
Double_t myclass::getZ(){return Z;}
Double_t myclass::getCharge(){return Charge;}

```

I compile my class definitions via the command:

```auto
root -l
root [0] .L class.cpp+

```

which yields a _class\_cpp.so_ file, to which I add on the _rootlogon.C_ file in my working directory. Now, if I run the following macro, which is designed to test both the creation and the reading of a TTree with the class entries defined above,

```auto
#include <TFile.h>
#include <TTree.h>
#include "class.hpp"

int maker()
{
        // Making the .root file
        TFile* _file = new TFile("output.root", "NEW");
        TTree* _tree = new TTree("Tree", "Main");
        myclass Event;
        _tree->Branch("Event", &Event);
        // Creating a simple 'event', i.e. row in the main TTree
        Event.Set(1,1,1,1000);
        std::cout << Event.getX() << " " << Event.getY() << " " << Event.getZ() << " " << Event.getCharge() << std::endl;
        _tree->Fill();
        _file->cd();
        _tree->Write();
        _file->Close();

        // Reading the .root file created above.
        myclass* rEvent;
        TFile* _reader = TFile::Open("output.root", "READ");
        TTree* _rtree = (TTree*)_reader->FindObjectAny("Tree");
        _rtree->SetBranchAddress("Event", &rEvent);
        _rtree->GetEntry(1);
        std::cout << rEvent->getX() << " " << rEvent->getY() << " " << rEvent->getZ() << " " << rEvent->getCharge() << std::endl;
        return 0;
}

```

I get:

```auto
root [0] 
Processing maker.cpp...
1 1 1 1000
-3.60071e-305 6.80648e+38 -1.37406e+308 -2.55744e-254
(int) 0

```

which essentially means that even if my class definitions work correctly, the “writing” in the TTree seems to be faulty, and consequently, I do not get what I want when reading it. I would really appreciate any help on this issue.

Many Thanks,

Andy

**FURTHER THOUGHTS**  
Apparently, there is a [segment](https://root.cern.ch/root/htmldoc/guides/users-guide/InputOutput.html#streamers) of the ROOT Guide about this, that mentions “Streamers”, that, as far I understand, help with the “parsing” of a custom object, but I do not know how to use them. There is also [this](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/13) question on the FORUM on this, but the answers do not a straight answer to the problem.

* * *

\_ROOT Version: 6.24/06  
_Platform:_ Centos8  
_Compiler:_ g++ 11

* * *

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 21, 2022, 4:42pm UTC](https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490/2 "2022-01-21T16:42:20Z")

</div>

Use

```auto
        // Reading the .root file created above.
        myclass* rEvent = nullptr;

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [January 21, 2022, 4:56pm UTC](https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490/3 "2022-01-21T16:56:40Z")

</div>

Hi,  
also note that you don’t need to inherit from `TObject` and usage of `ClassDef` is not mandatory (although it provides a little performance optimization and other goodies).

Updated docs regarding I/O of custom classes with ROOT can be found at [I/O of custom classes - ROOT](https://root.cern/manual/io_custom_classes) .

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![andrew](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://root-forum.cern.ch/u/andrew)\
**Post date:** [January 25, 2022, 1:14pm UTC](https://root-forum.cern.ch/t/problem-with-writing-and-reading-custom-class-objects-from-ttrees/48490/4 "2022-01-25T13:14:59Z")

</div>

@pcanal @eguiraud Thank you both for your replies. Indeed, @pcanal’s answer solved the issue. Thanks also for the info @eguiraud.

Cheers,

Andy
