How to rewrite TTree?

I have tree (say) tree1. I created Class by tree1->MakeClass("MyProcess"). In Loop() I have:

TFile *out = new TFile( filename, "RECREATE" );  
  
  TBranch *nb1 = fChain->Branch( "dev1", &dev1, "dev1/D" );
  TBranch *nb2 = fChain->Branch( "dev2", &dev2, "dev2/D" );
  TBranch *nb3 = fChain->Branch( "pi0pi0", &pi0pi0, "pi0pi0[2]/D" );
  TBranch *nb4 = fChain->Branch( "pi0omega", &pi0omega, "pi0omega[2]/D" );
  TBranch *nb5 = fChain->Branch( "omega", &omega, "omega[2]/D" );
  TBranch *nb6 = fChain->Branch( "mpair", &mpair, "mpair[10]/F" );
  TBranch *nb7 = fChain->Branch( "mtriple", &mtriple, "mtriple[10]/F" );

you can see I want to add new branches to tree1. Then

...
     fChain->Fill();
  }
  fChain->Write();
  out->Close();
}

It seems like code works until fChain->Write() line. After that Segmentation violation appears.
How to write new tree in file properly?

As in your previous topic, move the TFile declaration in the Selector definition, at the same location than the TChain is declared, e.g.:

     TFile *out;
     TChain *fChain;

And then change you code as:

  out = new TFile( filename, "RECREATE" );
  
  TBranch *nb1 = fChain->Branch( "dev1", &dev1, "dev1/D" );
  ...
  

Cheers, Bertrand.

I think segmentation violation was caused by my typo in code. But now after adding TFile *out
in MyProcess.h the same error appears:

Error in <TTree::Fill>: Failed filling branch:Selected.mpair, nbytes=-1, entry=35458
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:
    TTree *T = new TTree(...)
    TFile *f = new TFile(...)
 you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)
Error in <TTree::Fill>: Failed filling branch:Selected.mtriple, nbytes=-1, entry=35458

This is what in MyProcess.h:

 public :
     TFile          *out;
     TTree          *fChain;   //!pointer to the analyzed TTree or TChain
     Int_t           fCurrent; //!current Tree number in a TChain
 ...
MyProcess::MyProcess(TTree *tree) : out(0), fChain(0)

And in MyProcess.C:

...
 out = new TFile( filename, "RECREATE" );
  
    TBranch *nb1 = fChain->Branch( "dev1", &dev1, "dev1/D" );
    TBranch *nb2 = fChain->Branch( "dev2", &dev2, "dev2/D" );
...

And where do you create your TTree (fChain = new TTree(...))?

As a side note, fChain is a TSelector is usually for the input. For the output you likely need to create a different data member (so that, at least, to avoid confusion).

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