Transfer object in memory and close file

Dear rooters
I have a file with several small tree objects, some histograms in the main directory and a few subdirectories with a rather large number of histograms in each of those. For reasons of efficiency, I would like to open this file, transfer all content in memory, close the file and work with just the copies of those objects (I developed an interactive GUI to test pixel detectors). Once the operator is done with all possibile tests and the histograms and trees contain fresh data, I would like to save the updated memory content in a new file.

As far as I could experiment, there is no way to close a file and keep the content mapped in memory, all pointers get cleared after file->Close(), so there is no way to update the content of histograms and trees.

Is this true or am I missing something? Is there some way to circumvent this? I tried to keep the file open, update the histograms content and do a
histogram->Write("",TObject::kOverwrite) (or even a global directory->Write("",TObject::kOverwrite)), but this takes forever (overwrite or delete objects on disk seems to be really slow). I suspect here also I’m missing something.

Help would be welcomed, thanks.

Simply call

Philippe

I tried it: here is a snippet of code

[code]#include
#include <TObject.h>

using namespace std ;

void test()
{
gROOT->Reset() ;

TFile * file = new TFile(“a.root”,“UPDATE”) ;

cout << “file->ReadAll() ;” << endl ;
file->ReadAll() ;

TObject obj = NULL ;
TIter next(gDirectory->GetListOfKeys());
while(obj = next())
{
((TH1
)obj)->SetDirectory(0);
}

file->Close() ;

gDirectory->ls() ;
gROOT->ls() ;

}[/code]

The result of gDirectory->ls() is always just a plain

I checked, of course, that after ReadAll the long list of histograms has been set in memory!

Please re-read carefully the User’s Guide chapter on Object Ownership.
To get the behavior you would like use

Philippe

Sorry, but it still doesn’t work (frankly the User Guide is rather criptyc…)

Here is the code and below the output I get:

[code]#include
#include <TObject.h>

using namespace std ;

void test()
{
gROOT->Reset() ;

TFile * file = new TFile(“a.root”) ;

gDirectory->ls() ;

cout << “file->ReadAll() ;” << endl ;
file->ReadAll() ;

TObject obj = NULL ;
TIter next(gDirectory->GetListOfKeys());
while(obj = next())
{
if( obj->InheritsFrom(TH1F::Class()))
{
((TH1F
)obj)->SetDirectory(gROOT);
}
}

file->Close() ;

gDirectory->ls() ;
gROOT->ls() ;
file->ls() ;

}[/code]

And here’s the output

[quote]root [0]
Processing test.cxx…
TFile** a.root
TFile* a.root
KEY: TTree treeTOP;1 treeTOP
KEY: TTree treeACL;1 treeACL
KEY: TTree treePTA;1 treePTA
KEY: TTree treeTBM;1 treeTBM
KEY: TTree treeChip;1 treeChip
KEY: TH2F Thr_Timing_Chip_0;1 Thr/Timing for chip: 0
KEY: TH2F Thr_Timing_Chip_1;1 Thr/Timing for chip: 1
KEY: TH2F Thr_Timing_Chip_2;1 Thr/Timing for chip: 2
KEY: TH2F Thr_Timing_Chip_3;1 Thr/Timing for chip: 3
KEY: TH2F Thr_Timing_Chip_4;1 Thr/Timing for chip: 4
KEY: TH2F Thr_Timing_Chip_5;1 Thr/Timing for chip: 5
KEY: TH2F Thr_Timing_Chip_6;1 Thr/Timing for chip: 6
KEY: TH2F Thr_Timing_Chip_7;1 Thr/Timing for chip: 7
KEY: TH2F Thr_Timing_Chip_8;1 Thr/Timing for chip: 8
KEY: TH2F Thr_Timing_Chip_9;1 Thr/Timing for chip: 9
KEY: TH1F SCurve_Chip_0_Row_0_Col_0;1 SCurve Chip: 0 Row: 0 Col: 0
KEY: TH1F SCurve_Chip_0_Row_0_Col_1;1 SCurve Chip: 0 Row: 0 Col: 1
KEY: TH1F SCurve_Chip_0_Row_0_Col_2;1 SCurve Chip: 0 Row: 0 Col: 2
KEY: TH1F SCurve_Chip_0_Row_0_Col_3;1 SCurve Chip: 0 Row: 0 Col: 3
KEY: TH1F SCurve_Chip_0_Row_0_Col_4;1 SCurve Chip: 0 Row: 0 Col: 4
KEY: TH1F SCurve_Chip_0_Row_0_Col_5;1 SCurve Chip: 0 Row: 0 Col: 5
KEY: TH1F SCurve_Chip_0_Row_0_Col_6;1 SCurve Chip: 0 Row: 0 Col: 6
KEY: TH1F SCurve_Chip_0_Row_0_Col_7;1 SCurve Chip: 0 Row: 0 Col: 7
KEY: TH1F SCurve_Chip_0_Row_0_Col_8;1 SCurve Chip: 0 Row: 0 Col: 8
KEY: TH1F SCurve_Chip_0_Row_0_Col_9;1 SCurve Chip: 0 Row: 0 Col: 9


KEY: TH1F SCurve_Chip_9_Row_79_Col_44;1 SCurve Chip: 9 Row: 79 Col: 44
KEY: TH1F SCurve_Chip_9_Row_79_Col_45;1 SCurve Chip: 9 Row: 79 Col: 45
KEY: TH1F SCurve_Chip_9_Row_79_Col_46;1 SCurve Chip: 9 Row: 79 Col: 46
KEY: TH1F SCurve_Chip_9_Row_79_Col_47;1 SCurve Chip: 9 Row: 79 Col: 47
KEY: TH1F SCurve_Chip_9_Row_79_Col_48;1 SCurve Chip: 9 Row: 79 Col: 48
KEY: TH1F SCurve_Chip_9_Row_79_Col_49;1 SCurve Chip: 9 Row: 79 Col: 49
KEY: TH1F SCurve_Chip_9_Row_79_Col_50;1 SCurve Chip: 9 Row: 79 Col: 50
KEY: TH1F SCurve_Chip_9_Row_79_Col_51;1 SCurve Chip: 9 Row: 79 Col: 51
file->ReadAll() ;
TROOT* Rint The ROOT of EVERYTHING
TROOT* Rint The ROOT of EVERYTHING
TFile** a.root
TFile* a.root
root [1] [/quote]

At this point I’m pretty confused…

Humm … I did not read your code carefully enough …

you never read the object from the file !!!

This should help:

while(obj = next()) { TKey *key = (TKey)obj; TObject *iobj = key->ReadObject(); if( iobj->InheritsFrom(TH1F::Class())) { ((TH1F*)iobj)->SetDirectory(gROOT); } }

Philippe.