Branching from class

Hello experts,
I am trying to set the values of the branch and write it to a tree, following the tutorial root.cern.ch/root/html524/tutor … ee0.C.html

So I create a class, and import it via dictionary generation and all is good. The class is FOO, and has some public variables etc.

class FOO : Public TObject {
 public:
  Float_t foo_energy;
  FOO();
  virtual ~FOO();
  ClassDef(FOO,1);
  };

Then I write a program whose skeleton is below:

class analyzer {
public: 
   TFile *inFile;
   TTree *inTree;
   int nevents;
   analyzer();
   virtual ~analyzer();
   void Init(TFile *inFile);
   void Loop();
   void processALL();
   void processFOO(TLeaf *fooLeaf, FOO *foo);
};


/**********I think this is where the problem is************/
void analyzer::Loop()
{
 TFile *outFile = new TFile("fileWithFooTree.root","RECREATE");
 TTree *outTree = new TTree();
 FOO *foo = new FOO();
 outTree->Branch("foo",&foo);
 
 TLeaf *fooLeaf=(TLeaf*)inTree->GetLeaf("fooLeaf"); 
 
 nevents=inTree->GetNentries();
 for (int i=0;i<nevents;i++){
   if (i%1000==0 ) cout<<"--->Percent complete: "<< 100.0*(float)i/(float)nevents << "\r";
   processEvent(i);
   outTree->Fill();
 }
 outFile->Write();
 outFile->Close();
}

void analyzer::processALL(int eventNumber)
 {
 inTree->GetEntry(eventNumber);
 processFOO(fooLeaf, foo);
}

void analyzer::processFOO(TLeaf *fooLeaf, FOO* foo){
//assign the foo variables from the fooLeaf 
//i.e. read the leaf and do some stuff
}


int main(){
analyzer a = new analyzer();
a->Init();
a->Loop();
}

The tree is never written to file. The file is always empty. But when I cout the statements that detect the values of the foo variables, they are correctly set. Is there some subtlety that I am missing on writing events to a tree?

Many Thanks!


Cheers,
Philippe.

Cheers,
Philippe.

Instead of:
TTree *outTree = new TTree();
try:
TTree *outTree = new TTree(“outTree”, “my out tree”);
and instead of:
outFile->Write();
outFile->Close();
try:
outTree->Write();
delete outFile; // automatically deletes “outTree”, too
delete foo; //cleanup

These suggestions have got it! And improved the code cleanliness! Thank you!