Trees and objects


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi. I want to create a tree with a branch collecting events (1 event for each “step”). I found a tutorial (tree4.C) where one object event is created and then updated to create all the events (I think):


Are there functions in ROOT to create a branch with no adress (no"&event") and then in the for loop put objects in the branch to obtain the same result without enabling users to change private members of the class with Setters? Something like

for(int i.....){
event *ev=new event(....); 
//link ev to branch to save it there
delete ev;}

Thanks.

TFile *file = TFile::Open("...", "recreate");
TTree *tree = new TTree(...);
event *ev = 0;
tree->Branch("my_events", &ev);
for(int i.....) {
  delete ev; // just a precaution
  ev = new event(...); 
  // ev->SetWhateverYouWant(...);
  tree->Fill();
  delete ev; ev = 0;
}
tree->Write();
delete file; // automatically deletes tree, too

Thanks. I will try. I thought deleting objects would cause brach to be “floating” and save random memory :see_no_evil: :see_no_evil:

I thought deleting objects would cause brach to be “floating” and save random memory

Indeed, in between the statement:

delete ev;

and the statement:

ev = ...

The TTree Technically points to deleted memory.

Thanks @pcanal. I will pay attention to the order of statements to properly fill my trees.