I need to open file, get histogram to memory and close file.
What is wrong with the below code?
I would expect that Get() copy object to memory so the file following Close() changes nothing.
Instead if one opens file as a pointer using new TFile() and does not close it the same code works fine.
Note that use of commented lines does not solve the problem
The output is:
$ root t.C
*******************************************
* *
* W E L C O M E to R O O T *
* *
* Version 5.34/25 12 January 2015 *
* *
* You are welcome to visit our Web site *
* http://root.cern.ch *
* *
*******************************************
ROOT 5.34/25 (v5-34-25@v5-34-25, Jan 12 2015, 15:29:06 on linuxx8664gcc)
root [0]
Processing t.C...
TFile** mean.root
TFile* mean.root
KEY: TH2F h2;1 h2
KEY: TH2F hmean;1 hmean
*** Break *** segmentation violation
Generating stack trace...
root [0] TFile f("hsimple.root");
root [1] TH2F *h2 = (TH2F*)f.Get("hpxpy");
root [2] h2->SetDirectory(0); // to decouple it from the open file directory
root [3] f.Close()
root [4] h2->Draw()
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
Especially strange that when I create completely new object with Clone() function this object is still depends somehow from the file directory even if the file is not writable.
I agree it might be more intuitive. May be you can encapsulate the Get and SetDirectory in one macro so you do not need to type two lines each time you want to do that.
TFile f("mean.root");
at the end of the function, the object f is automatically deleted, and also all objects associated to this file.
By calling SetDirectory(0) you dissociate the histogram from the file, and then it is your responsibility to manage it (eg avoiding a memory leak)
I’m mistaken or 2 or 3 major root versions ago the behavior was different and the objects like histograms were kept in memory after the file closing?
I see the point about memory leak, but to do for each object SetDirectory(0) brr… I’m sure that you already got an infinity of similar questions.
The TDirectory / TFile association exists only for a few types, and can only be removed for a subset of those. See the users guide, chapter “Object Ownership”.
This has mostly historical reasons; it’s impossible to change this now fundamentally or we break everyones’ code
Is it possible to move an arbitrary TObject to any TDirectory? I would like to be able to retain TGraph in memory after closing the file from which it was retreived.