# Storing/Reading custom class

**URL:** <https://root-forum.cern.ch/t/storing-reading-custom-class/13663>\
**Category:** ROOT\
**Created:** [January 24, 2012, 2:34pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663 "2012-01-24T14:34:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrislaz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/chrislaz/32/11772_2.png) [@chrislaz](https://root-forum.cern.ch/u/chrislaz)\
**Post date:** [January 24, 2012, 2:34pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/1 "2012-01-24T14:34:28Z")

</div>

Hi, I’ve written a custom class “Particle” like:

```auto
class Particle : public TObject {
  public:  
    Particle();
    ~Particle();
    Double_t x;
    Double_t y;
    Double_t z;
    ClassDef(Particle,1)
};
```

then, I create a TTree and store several values:

```auto
  TTree *tree = new TTree("target","data");
  Particle *p = new Particle;
  tree->Branch("particle",&p);
(...)
  tree->Fill();
```

Now I want to read the information from the rootfile. I tried doing MakeClass on the resulting file and instead of giving me access to the “Particle” objects, I get access to the variables inside:

```auto
   TBranch *b_particle_x; //!
   TBranch *b_particle_y; //!
   TBranch *b_particle_z; //!
```

if I try something like:

```auto
fChain->SetBranchAddress("particle", &particle );
```

The information returned on the “particle” object is incorrect. Which would be the correct way to retrieve the actual object?

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [January 24, 2012, 6:43pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/2 "2012-01-24T18:43:51Z")

</div>

tree-\>Branch(“particle”, &p, 32000, 0);

Search for “splitlevel” in [http://root.cern.ch/root/html/TTree.html](http://root.cern.ch/root/html/TTree.html)

---

<div class="post-metadata">

**Author:** ![chrislaz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/chrislaz/32/11772_2.png) [@chrislaz](https://root-forum.cern.ch/u/chrislaz)\
**Post date:** [January 25, 2012, 8:27am UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/3 "2012-01-25T08:27:20Z")

</div>

Exactly what I needed! Many thanks!

---

<div class="post-metadata">

**Author:** ![bpnuke](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d9b06d/32.png) [@bpnuke](https://root-forum.cern.ch/u/bpnuke)\
**Post date:** [January 3, 2020, 12:48am UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/4 "2020-01-03T00:48:18Z")

</div>

I have a custom class that includes a TTree, kListData (see attached). I’ve stashed this class inside a folder inside my ROOT GIT repo and modified the CMakeList.txt files to build this class as part of ROOT.

The objective of the parsing function call is to selectively take data from one root tree and store it in another as a vector. The plan is to use the vector to make the class expandable for reconstructing coincident events later. The class successfully compiles and parses files but when I try to kListData.Scan() the data inside the tree ROOT just hangs.

Could someone provide me some pointers on how to use a TTree inside a custom class?

[TListSpectrum.h](https://root-forum.cern.ch/uploads/short-url/qcKLUbS7DXxQSAvS9VlFqBw4vTx.h) (2.5 KB) [TListSpectrum.cpp](https://root-forum.cern.ch/uploads/short-url/28azeCZCZ8AFze1Tok3viyb6SQx.cpp) (7.5 KB)

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [January 6, 2020, 9:04am UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/5 "2020-01-06T09:04:52Z")

</div>

Hi,

@pcanal can surely help. But please clarify one thing: You should be able to use a TTree inside a custom class in the same way that you can use a Tree in a macro etc. Is the problem that you want to save the custom class with a tree inside into another tree?

---

<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 6, 2020, 7:30pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/6 "2020-01-06T19:30:42Z")

</div>

Most likely the `kListData TTree object` gets attached to some (random/arbitrary) TFile outside of TListSpectrum and is deleted when that file is deleted/closed.

If this is the case and you need/want a in-memory tree then use

```auto
TListSpectrum::TListSpectrum() : TNamed("ListSpectrum", "Unnamed Rawdata File"), kListData("kListData","kListData")
{
	kChannels = 0;
	kSampleNorm = 1.0;
	kStartofCounting = *(new TTimeStamp());
	kGeometry = "{EMPTY}";
	kSampleUnit = "sample";
	
        kListData.SetDirectory(nullptr);
	
	kEnergyCalibration = {0.0, 0.0, 0.0}; // this should be in TListSpectrumCal
}

```

If you mean for it to belong to a file, I recommend making it more explicit:

```auto
TListSpectrum::TListSpectrum(TFile *file) : TNamed("ListSpectrum", "Unnamed Rawdata File"), kListData("kListData","kListData")
{
	kChannels = 0;
	kSampleNorm = 1.0;
	kStartofCounting = *(new TTimeStamp());
	kGeometry = "{EMPTY}";
	kSampleUnit = "sample";
	
        kListData.SetDirectory(file);
	
	kEnergyCalibration = {0.0, 0.0, 0.0}; // this should be in TListSpectrumCal
}

```

and make sure the TFile is open until you delete the TListSpectrum object.

---

<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 6, 2020, 7:35pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/7 "2020-01-06T19:35:40Z")

</div>

As a side note since `kStartofCounting` is a `TTimeStamp` object, the constructor will of TListSpectrum will implicitly call the default constructor for it and thus:

> [@pcanal](#):
>
> kStartofCounting = \*(new TTimeStamp());

is redundant … and is a memory leak (the object create by `new TTimeStamp` is never deleted.

---

<div class="post-metadata">

**Author:** ![bpnuke](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d9b06d/32.png) [@bpnuke](https://root-forum.cern.ch/u/bpnuke)\
**Post date:** [January 9, 2020, 7:23pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/8 "2020-01-09T19:23:15Z")

</div>

I want to save the custom class as itself inside a TFile much like you would declare TFile X then TTree Y and as you fill it populates the TFile with the data. I’d like to declare TFile X then TListSpectrum Y and as it is populated with the data it fills the TFile with the data.

---

<div class="post-metadata">

**Author:** ![bpnuke](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d9b06d/32.png) [@bpnuke](https://root-forum.cern.ch/u/bpnuke)\
**Post date:** [January 9, 2020, 7:27pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/9 "2020-01-09T19:27:45Z")

</div>

Yes, thank you, good eye.

---

<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 9, 2020, 7:36pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/10 "2020-01-09T19:36:58Z")

</div>

What is the exact sequence of code that leads to the crash?

---

<div class="post-metadata">

**Author:** ![bpnuke](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d9b06d/32.png) [@bpnuke](https://root-forum.cern.ch/u/bpnuke)\
**Post date:** [January 9, 2020, 8:06pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/11 "2020-01-09T20:06:18Z")

</div>

I’m definitely doing something silly and ROOT is doing exactly what ROOT is supposed to do. The crash is the traditional user error of creating the TTree without a resident TFile.

1. I populate a huge tree (~2-6Gb)
2. I create a TFile pointer using the “recreate” option
3. I ask the TListSpectrum (TNamed object) to Write itself.
4. It grabs the TFile pointer and begins preparations to dump an object that far exceeds the buffer ROOT defined buffer limit and crashes.

My question isn’t so much why is ROOT crashing but rather how best to write a TListSpectrum object to a TFile for storage purposes. Alternatively, I’ve thought about just writing the private ROOT objects that makeup TListSpectrum to the TFile by overloading the TNamed Write call but that wouldn’t have fixed the issue identified above where I was trying to write large memory-resident TTrees to a TFile. I have modified the constructors in TListSpectrum as you suggested and the TTree is now getting stored correctly, thank you so much for your help!

The idea is to use this class structure to store binary list-mode data files in an archive and incorporate functionality that makes it easier for spectroscopists to analyze and re-analyze the data using canned analysis utilities associated with two separate classes TListSpectrumCal and TListSpectrumAnalysis.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [January 10, 2020, 9:26am UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/12 "2020-01-10T09:26:09Z")

</div>

I’m just guessing here, but give this a thought:  
Could it be that ROOT doesn’t serialise an object larger than 1 Gb in a single I/O operation?  
If TListSpectrum contains a tree, and you write the tree manually, you might get around the 1 Gb limit, and therefore it works.

---

<div class="post-metadata">

**Author:** ![bpnuke](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/d9b06d/32.png) [@bpnuke](https://root-forum.cern.ch/u/bpnuke)\
**Post date:** [January 13, 2020, 6:44pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/13 "2020-01-13T18:44:53Z")

</div>

The way we have it working right now is just to instantiate a TFile and write the TTree separate from the object. ROOT was crashing for me if I filled the TTree object from binary then ask it to write the TTree to a TFile using the TTree::Write() call. I believe the correct method of filling large (\>1Gb) TTrees is to instantiate a TFile then the TTree such that the TDirectory the TTree is linked to is a real file. As you fill the tree it writes to file such that you don’t end up trying to write a multi-Gb TTree to a file.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [January 14, 2020, 12:59pm UTC](https://root-forum.cern.ch/t/storing-reading-custom-class/13663/14 "2020-01-14T12:59:18Z")

</div>

That sounds correct.
