[Fwd: Re: A question about files]

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Feb 10 1998 - 10:23:50 MET



attached mail follows:


Chris Jillings wrote: > > Hi, > This question is simple, but has caused me hours of problems. > I want to read an ntuple and fill a histogram with data from the ntuple > and then write the histgram to another file. > > TH1F* h4 = new TH1F("h4","4 MeV Electrons",80,-1,1); > TFile* fout = new TFile("arHistos.root","NEW","AR hists",1); > TFile* ntp = new TFile("cjj_e4.root","READ"); > if(ntp->IsOpen()) > h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h4","Rfitt<600",); > Everything to here is fine. > > The first problem is that if I > ntp->Close(); > h4->Draw(); > the histogram is empty. The upper/lower bounds and title are correct > but there is no data in the histogram: I get a flat line at zero counts. > > I checked the browser and it turns out that the histogram is "part of" > the file cjj_e4.root (even though I opend it read only.) > > I thought I would work around the problem by opening a second file and > writing the histogram to it. That gives an error that the file cjj_e4 is > read-only. > > I realize that for just one file, I can keep it open for the duration of > my work, but I will need to take one histogram from each of 6 or 12 > files, and it seems inelagant to be forced to keep open that many files. > > Also, the histogram is created before any files are opened. When I fill > the histogram with the draw command, why doesn't the data get written to > something with global scope? (ie something that will exist in memory after > the file is closed) I don't understand why that wouldn't be automatically > so. When the statement: h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h4","Rfitt<600"); is executed, TTree::Draw searches for an histogram named "h4" in the current directory (file cjj_e4.root). It does not find an object named "h4" since this histogram has been created in a different directory (the one by default gROOT). A new histogram "h4" is created in this directory and correctly filled by TTree::Draw. However, when you close the file, all the objects in memory associated with this file are deleted. You are returned to the default directory and when you do h4.Draw(), you draw the original empty histogram! The solution to this problem is illustrated by the piece of code below where I show how to loop on a list of files containing the same Tree "h509". TH1F* h4 = new TH1F("h4","4 MeV Electrons",80,-1,1); TFile* fout = new TFile("arHistos.root","NEW","AR hists",1); char *files[] = {"file1.root","file2.root",...."file50.root"}; for (Int_t i=0;i<50;i++) { TFile *file = new TFile(files[i]); TTree *h509 = (TTree*)file->Get("h509"); gROOT->cd(); h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h4","Rfitt<600"); delete file; } // Now store accumulated histogram in output file fout->cd(); h4->Write(); Note that the same result can be obtained by using the TChain mechanism. Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:29 MET