Defining TBranch that hold TObject

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) ?

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


ROOT Version: 6.26.00 (conda)
Platform: Ubuntu20.04 (WSL)
Compiler: gcc 9


TTree → Create a TTree to store columnar data → Add a column holding objects

Hello @NgJunKai ,

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

/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

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