Current directory (root) is not associated with a file

Hi,

I’m trying to dump on a .root file some histograms created and filled in a program, but I’ve got this error:
Error in TROOT::WriteTObject: The current directory (root) is not associated with a file. The object (lambda_1) has not been written.

What does this mean? Could someone help me fixing the problem?
I’m adding the significant lines of code…

Sara

// useful histograms
TH1F * hlambda[30];
int iterNum = 10;

int main()
{
// init histograms
for(int i=0; i<iterNum; i++)
{
string name = “lambda_”;
name += i +‘0’;
hlambda[i] = new TH1F(name.c_str(),“lambda values”,1000,0.,0.02);
}
[…]
if(dump_root)
{
// *** Draw histograms just for sanity check and save them to file
fout = new TFile(“EM.root”,“recreate”);
for(int i=0; i<iterNum; i++)
{
hlambda[i]->Write();
fout->Close();
}
}
return 0;
}

Sara,

Create the TFile before your histograms or alternatively do

hlambda[i]->SetDirectory(fout); in a loop after having created the file

Rene

Hi Brun,
I’ve tried to fix including both your advices, but it doesn’t vwork… what am I doing wrong?
Thanks
Sara

PS this is the new code:
// global variables
TFile * fout;

// useful histograms
TH1F * hlambda[10];

int main()
{
fout = new TFile(“EM.root”,“recreate”);

// init histograms
for(int i=0; i<iterNum; i++)
{
string name = “lambda_”;
name += i +‘0’;
hlambda[i] = new TH1F(name.c_str(),“lambda values”,1000,0.,0.02);
hlambda[i]->SetDirectory(fout);
}
[…]
if(dump_root)
{
// *** Draw histograms just for sanity check and save them to file
for(int i=0; i<iterNum; i++)
{
hlambda[i]->Write();
fout->Close();
}
}
return 0;
}

Could you post the shortest possible RUNNING script reproducing this problem?

Rene

I compile this code along with some classes I’m using, and link to ROOT libraries with the makefile in attachment. Then I just execute the executable “runEM”. No script is involved.
Let me know if you need more details.
Regards
Sara
Makefile.txt (1.31 KB)

Sara,

I do not want a Makefile. I need the shortest piece of R U N N I N G code reproducing the problem
Response time is proportional to the square of the number of lines of code ::slight_smile:

Rene

Sorry for misunderstanding… here is the shortest piece of code, it does reproduce the error, but it gives segmentation fault too in the end… argh!
Let me know if you could help me…
Sara
Makefile.txt (1.31 KB)
test.c (1.18 KB)

Uhm! you should not close teh output file in the loop !!
Do:

if(dump_root) { // *** Draw histograms just for sanity check and save them to file for(int i=0; i<iterNum; i++) { hlambda[i]->Write(); } fout->Close(); }

Thanks, and sorry for the silly error…
Sara