Dear ROOTtalk Here is a simple beginner's question, to which everytime I think I have found the answer something happens to prove me wrong yet again. In all simple examples, when one wants to create some objects (e.g. histograms) and write them in a file, one first opens the file, then creates the histograms, then writes them to the file. However, if one is forced to open the file after having created the objects, things get tricky (if one has not understood what one is doing, which is one's case). The actual problem I have is that I want to create some TEventList objects in an analysis class derived from TSelector. The TEventLists are created in Begin(), filled in ProcessFill(), and should be written to a file which is opened, written and closed in Terminate(). If I do root [0] TH1F *h = new TH1F("h","h",100,0,1) root [1] gROOT->FindObject("h") (const class TObject*)0x8948790 root [2] TFile *f = new TFile("savehisto.root","recreate") root [3] gROOT->FindObject("h") (const class TObject*)0x0 I see that opening the file makes the current directory become the file, which is empty, and so I do not retrieve my histogram. However, if I now do root [7] gROOT->cd() (Bool_t)1 root [8] gROOT->FindObject("h") (const class TObject*)0x8948790 I'm back in business ! (Strange though: doesn't gROOT->cd() mean "set the current directory to be the one pointed to by gROOT" - but gROOT already points to the current directory, the file in this case ?!?!?) So now I do root [9] gROOT->FindObject("h")->Write() Error in <TH1F::Write>: No file open (Int_t)0 and realise that I can't have my file and fill it, or have my histo and write it. So I end up doing : root [7] gROOT->cd() (Bool_t)1 root [10] TH1F* h1=(TH1F*)gROOT->FindObject("h") root [11] f->cd() (Bool_t)1 root [12] h1->Write() (Int_t)208 I'm sure that there is a simpler, clearer way to do this, but this is what I managed with my limited knowledge. As I said, I didn't find an example because all the examples I have seen are of the simple type "open file - create objects - write objects". Coming back to the case in hand, I could of course keep track of all the pointers to my TEventLists (there are quite a lot of them), i.e. by putting them in a TList and then using TList::Write(), but this seems a little pedestrian and I thought the better way of doing it, in the true ROOT spirit (whatever that is) would be to use gROOT->FindObject(...)..... Can you please help ? Thanks a lot John PS. I thought "SetDirectory(0)" might help, but not the way I use it: root [0] TH1F *h = new TH1F("h","h",100,0,1) root [1] h->SetDirectory(0) root [2] TFile *f = new TFile("savehisto.root","recreate") root [3] gROOT->FindObject("h") (const class TObject*)0x0 root [4] gROOT->Get("h") (class TObject*)0x0 root [5] gDirectory->Get("h") (class TObject*)0x0 -- ganil logo <http://www.ganil.fr> John D. Frankland <mailto:frankland@ganil.fr> Beam Coordinator GANIL B.P. 55027 14076 CAEN Cedex 05 *tel:* +33 (0)231454628 *fax:* +33 (0)231454665
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:13 MET