Problem with root file

Hello,
I’m not sure this is a root problem or I’m just doing something wrong with c++… Anyhow I have a c++ program which read a tree and write many (thousends) of histogram in few files. The code I use is the following:

  ...
  int   nAlgo   = 5;
  int   nBinPt_ = 250;
  int   nBinEta = 50;

  TFile *myFile[nAlgo];
  TH1F* hDist_GJ[nAlgo][nBinEta][nBinPt_];

  for(int iAlg=0;iAlg<nAlgo;iAlg++) {

    myFile[iAlg] = new TFile(Form("histoFile_DR%03u_%d.root",drInt,iAlg),"RECREATE");

    for (Int_t i1=0; i1<nBinEta ; i1++)
      for (Int_t i2=0; i2<nBinPt_ ; i2++) {
        hDist_QJ[iAlg][i1][i2]= new TH1F(Form("hDist_QJ_%d_%d",i1,i2),"",200,0.5,1.5 );
      }
  }
...
  for(int iAlg=0;iAlg<nAlgo;iAlg++) {
    myFile[iAlg]->cd();
    cout << "Writing file for algo:" << iAlg << endl;
    myFile[iAlg]->Write();
  }

  cout << "End Writing and exiting" << endl;

  return 0;

}

The problem is that the program compile, run but it’s not exiting… I see on the standard output:

End Writing and exiting

and then the program stuck. If I ctrl^C, the program stops, the output file is correctly written and I can use it…

I can survive like this but I cannot use my program in a script because it’s not exiting…

So my question is: is the example code correct? If you like I can give you a code example to run

Thanks for helping.

Attilio

Please provide the shortest possible (but RUNNING) script reproducing this problem and indicate your version of ROOT.

Rene

This is an example which show the problem. This is an old problem for me… present with different root version… the one I’m using now is 5.14/00 but I have the same problem also with 5.18/00. To run the example is enough to untar, make and run the program.
Thanks

Attilio
example.gtar (146 KB)

In your loop writing histograms, clear the directory , otherwise it will take a lot of time at the end of the job when ROOT closes automatically files that have not yet been closed, ie

for(int iAlg=0;iAlg<nAlgo;iAlg++) { myFile[iAlg]->cd(); cout << "Writing file for algo:" << iAlg << endl; myFile[iAlg]->Write(); gDirectory->Clear(); //<====== }

Thanks! That solve the problem.

Attilio