Error saving a tree into a root file

Hello,

I am a new root user and I am trying to store in a rootfile (“doesnotwork.root”) some events selected from another rootfile (whose path is “FILENAME”).
I can create the output one and make root write the tree (t3) into it, because when I try to open the rootfile via Tbrowser I find my variables (“e” and “time”), but if I try to see an histogramme of the contents of one of these two variables I get this error so many times:
Error in TFile::ReadBuffer: error reading all requested bytes from file doesnotwork.root, got 0 of 1919

I’ve tried everything I know, someone please help me out.

Thanks
Guido

 // --------------------------------------------------------------------------
  // Open rootfile and get # of events
  // --------------------------------------------------------------------------
  TFile *f=new TFile("doesnotwork.root","RECREATE"); // there I will write the result of the selection
  TFile *rootfile=new TFile(FILENAME,"UPDATE"); // from here I load data
  if (!(rootfile->IsOpen())){ 
    printf("I had trouble opening the rootfile.\n");
    printf("I'm sorry, but I have to terminate here.\n");
    exit(EXIT_FAILURE);
  }
  // go to folder
  gDirectory->cd("PROD2NTU");
  // go to tree
  TTree *alberello;
  gDirectory->GetObject("h1;1",alberello);
  NEVENTI = alberello->GetEntries();
  cout << "In " << FILENAME << " sono registrati " << NEVENTI << "eventi." << endl;
  gDirectory->pwd();

  // some code ….

///*--------------------------------------------------------------- needs debugging
  // make a new TTree to organize data of interesting tracks
  TTree *t3=new TTree("t3","A nice TTree");
  Float_t e,time;
  TBranch *br1 = t3->Branch("e",&e,"e/F");
  TBranch *br2 = t3->Branch("time",&time,"time/F");
//*/

  for(i=0;i<NEVENTI;i++){
    alberello->GetEntry(i);
    // some other code... 
    	if(crystal[evento.cry[k]]==1){ // if track hits the crystal which gave a signal add the signal to histogramme 
	  //* ------------------------------------- needs debugging
	  e=(Float_t)evento.e[k];
	  time=(Float_t)evento.time[k];
	  t3->Fill();
	  // cout << "e vale " << e << " time vale " << time << endl; // DEBUG
	  //*/
	  if(evento.time[k]>1500 && evento.time[k]<2500){ // cut on TDC counts 
	    hADC->Fill(evento.e[k]);
	    hTDC->Fill(evento.time[k]);
	  }
  }
}

  padup->cd();
  hADC->Draw();
  hADC->Write();
  paddown->cd();
  hTDC->Draw();
  hTDC->Write();
  ///* ------------------------------------------------------- needs debugging
  f->cd();
  gDirectory->pwd();  
  t3->Write();
  f->Close();
  //*/

Try to add:
f->cd();
right before:
TTree *t3=new TTree(“t3”,“A nice TTree”);
Also, instead of “f->Close();” you can simply use "delete f;"
BTW. Why do you open the “rootfile” in the “UPDATE” mode if you just retrieve objects from it?

First of all thank you very much for your reply.

About what you suggested, I just tried with

[quote]Try to add:
f->cd();
right before:
TTree *t3=new TTree(“t3”,“A nice TTree”);[/quote]

and it works!
I had already tried “delete f;”, but it still didn’t work

Actually I open my source rootfile in UPDATE mode because I need to attach a couple of histogrammes, but that’s not essential. So, to me, the problem is solved. But if I wanted to add those histogrammes also, I wouldn’t know how to do… Anyway thank you again! =D>

Guido

Try: // ... TDirectory *oldDir = gDirectory; // save the current directory f->cd(); // switch to "f" for "t3" creation TTree *t3 = new TTree("t3", "A nice TTree"); gDirectory = oldDir; // restore the saved directory // ...