# Defining TBranch that hold TObject

**URL:** <https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675>\
**Category:** ROOT\
**Tags:** root\
**Created:** [February 18, 2023, 8:12am UTC](https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675 "2023-02-18T08:12:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NgJunKai](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ngjunkai/32/30046_2.png) [@NgJunKai](https://root-forum.cern.ch/u/NgJunKai)\
**Post date:** [February 18, 2023, 8:12am UTC](https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675/1 "2023-02-18T08:12:56Z")

</div>

Can anyone explain why I got segmentation fault with the `("ROCCurve","TH1D",ROCCurve);` line. Doesnt it the way to define a branch that stores TObject (If the `TString` line is working) ?

```auto
void test(){
    TString Options;
	double TrainTime;
	double ROCArea;
	TH1D* ROCCurve;
    
    //Build Result Tree
	TTree ResultsTree("t","tree");
	
    ResultsTree.Branch("Options","TString",&Options);
    ResultsTree.Branch("ROCCurve","TH1D",ROCCurve);
    ResultsTree.Branch("TrainTime",&TrainTime,"TrainTime/D");
    ResultsTree.Branch("ROCArea",&ROCArea,"ROCArea/D");
    
    Options = TString("hhh");
    TrainTime = 2.0;
    ROCArea = 4.0;
    *ROCCurve = new TH1D("jj","kk",50,0,100);
    // for(int i=0;i<10;i++){ROCCurve->Fill(i*100);};
    
    ResultsTree.Fill();

	//End
	TFile ResultsFile("test.root","recreate");
	ResultsTree.Write();
	ResultsFile.Close();
}

```

n.b.: I didnt get the point even though there is a similar topic [Add a branch holding object](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093)

* * *

_ROOT Version:_ 6.26.00 (conda)  
_Platform:_ Ubuntu20.04 (WSL)  
_Compiler:_ gcc 9

* * *

---

<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:** [February 18, 2023, 10:48am UTC](https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675/2 "2023-02-18T10:48:32Z")

</div>

[TTree → Create a TTree to store columnar data → Add a column holding objects](https://root.cern/doc/master/classTTree.html#addingacolumnofobjs)

---

<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:** [February 21, 2023, 2:39pm UTC](https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675/3 "2023-02-21T14:39:53Z")

</div>

Hello @NgJunKai ,

at least with the code you posted the outcome is not a segmentation fault but a compilation error:

```auto
/tmp root -l test.C
root [0]
Processing test.C...
In file included from input_line_8:1:
/tmp/test.C:18:15: error: no viable overloaded '='
    *ROCCurve = new TH1D("jj","kk",50,0,100);
    ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/blue/ROOT/master/cmake-build-foo/include/TH1.h:629:10: note: candidate function not viable: no known conversion from 'TH1D *' to 'const TH1D' for 1st argument; dereference the argument with *
   TH1D& operator=(const TH1D &h1);
         ^

```

which should be self-descriptive.

Also note that if you want to write a single histogram out into a ROOT file you don’t need to go through a TTree. You can simply write the histogram to the TFile.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 7, 2023, 2:40pm UTC](https://root-forum.cern.ch/t/defining-tbranch-that-hold-tobject/53675/4 "2023-03-07T14:40:21Z")

</div>

Closed on March 7, 2023, 2:40pm UTC

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