Appending to a TTree, or creating if it does not exist

Dear all

I’m working on a monte carlo simulation in native C++ that is intended to be run multiple times, writing the final state of the system to a ROOT TFile, then upon the next run resuming from this saved state. I have succeeded in getting this to work. Each saved state is saved in a new TNtupleD with different name.

In addition to the raw state of the system, I have some summary statistics and information I would like calculated at the end of each time the program is run, saved into a TTree.

Currently, I am running

each time the program is executed. While this does work, each time the simulation is run, it creates a new TTree in the TFile with an incremented cycle number. This causes problems when I want to analyse the output or use hadd to merge files.

I want to use something along the lines of

to open the Tree, then append to the end, but the first time it is run, this fails.

Can’t seem to find much information on what I would think is a quite common thing to want to do. I’m very new when it comes to ROOT and not that exprerienced in C++ so it’s quite possible I’m just thinking in the wrong way.

I have also tried to do something along the lines of

        TTree* ResultsTree;

	if (runtimeslice == 0 ){
		ResultsTree = new TTree("starucn_summary","Summary of STARucn output");
	}
	else{
		ResultsTree = (TTree*)(fCore->RootFile)->Get("starucn_summary");
	}


     [some more stuff]

	ResultsTree->Branch("RunID",runidcstr,"RunID/C");
	ResultsTree->Branch("Runsection",&runsection,"Runsection/I");
        [and so on...]

	ResultsTree->Fill();
        ResultsTree->Write("", TObject::kOverwrite)

but when this is run with runtimeslice != 0, it just writes a row of zeroes to the file.

If anyone can show me a better way to do this, or show me where I am going wrong I would be very grateful.

Thanks in Advance,
Nick

Hi,

you can check the existence of the TTree and create it if it doesn’t exist, see Check if Object exists in TFile.

Also, to append data to an existing TTree, maybe this is a useful link: root.cern.ch/root/roottalk/roottalk02/5226.html.

Thank you!

I read the second link and did that and it worked! It turns out that the program was writing zeroes for the second and later runs because I did ResultsTree->Branch("RunID",runidcstr,"RunID/C");

rather than TBranch* tmp; tmp = ResultsTree->GetBranch("RunID"); tmp->SetAddress(runidcstr);

In case anybody stumbles upon this in the future, my code looks something like


TTree* ResultsTree;

if (runtimeslice == 0 ){
		ResultsTree = new TTree("starucn_summary","Summary of STARucn output");
	}
	else{
		ResultsTree = (TTree*)(fCore->RootFile)->Get("starucn_summary");
	}


[do stuff]

	if (runtimeslice == 0) {
		//Create new branches in the Tree
		ResultsTree->Branch("RunID",runidcstr,"RunID/C");
        ...
}

else {
		//set branch addresses
		TBranch* tmp;
		tmp =  ResultsTree->GetBranch("RunID");
		tmp->SetAddress(runidcstr);
}
ResultsTree->Fill();

Apologies about the horribe indentation…