Problem with TFile

Hi all.
I have written a function which is loaded to the CINT interpreter (".L"), given below. This function creates “fake” muSR data, and works properly when called (from a simple script called create_test.cxx). However, the CINT is stuck after calling the function, forcing me to kill and restart ROOT root every time I want to use it. Namely, I get:

[quote] root [0] .x create_test.cxx
Calculating muSR histograms with 12800 channels, 0.000976565 microsec per channel
TCanvas::MakeDefCanvas: created default TCanvas with name c1
Statistics are : Left - 1.06988e+07 Right - 9.30119e+06 Total - 2e+07
Starting output…
Files for test fakeprl2me created.

*** Break *** segmentation violation

[/quote]

I noticed that commenting out the last part of the function, (which creates a root file with is used for control if the output is OK, see code), makes the problem disappear. That is, it is the TFile object with is responsible for the fault. I looked in the ROOT talk and docus and there I found that I should just delete the variable. As you see in the code, Although I delete (and close) it, it still get stuck. Anybody knows how can I resolve this?

Many Thanks in Advance from Switzerland on a sunny sunday :unamused:
Moshe Dubman.

[code]void Calculate_muSR_histograms(TString strNum, TH1F* pB, Double_t TotalStatistics, Double_t A0_l=0.3,Double_t alpha = 1,Double_t phasel=0,Double_t phaser=180, Double_t BckpctL = 0, Double_t BckpctR = 0, Double_t dt=0.001, Int_t channels = 10000, Bool_t statisticalerrors = kTRUE)
{
Double_t N0[2] = { 1000, 1000/alpha };
Double_t B[2] = { N0[0]*BckpctL, N0[1]*BckpctL };

if (strNum=="") { strNum=pB->GetName(); cout << "default name " << pB->GetName() << " assigned." << endl; }

const TString detectors_str[2] = { "left", "right" };
const Double_t muonlifetime = 2.197078;

char dumbuf[100];
TH1F* ACalc[2];
TH1F *histcalc[2], *temp[2];
TF1* singlemuon = new TF1("thismuon","[0]*cos(8.51586494060339455e+02*x*[1]+[2]*pi/180)",0,14);
singlemuon->SetParNames("pBcoeff", "Bloc", "Phase");
singlemuon->SetNpx(100000);

pB->Scale(1000);

cout << "Calculating muSR histograms with " << channels << " channels, " << dt << " microsec per channel" << endl;

////// CALCULATE HISTOGRAMS

for (Int_t det=0; det<=1;det++)
{
ACalc[det] = new TH1F("A"+strNum+"_"+detectors_str[det],"A"+strNum+"_"+detectors_str[det], channels, 0,channels*dt);
temp[det] = new TH1F("temp"+strNum+"_"+detectors_str[det],"temp"+strNum+"_"+detectors_str[det], channels, 0,channels*dt);
if (det==0)
	singlemuon->SetParameter("Phase", phasel);
	else
    singlemuon->SetParameter("Phase", phaser);

	for (Int_t Bbin=1;Bbin<=pB->GetNbinsX();Bbin++)
	{
		if (pB->GetBinContent(Bbin)>0)
			{
			singlemuon->SetParameter("Bloc", pB->GetBinCenter(Bbin));
			singlemuon->SetParameter("pBcoeff", pB->GetBinContent(Bbin));
			ACalc[det]->Add(singlemuon);
			}
	}
	ACalc[det]->Scale(A0_l/pB->Integral());

	for (Int_t hbin=1;hbin<=temp[det]->GetNbinsX();hbin++)
	{
		Double_t xpos = temp[det]->GetBinCenter(hbin);
		Double_t asym = ACalc[det]->GetBinContent(hbin);
		Double_t histval = N0[det]*exp(-xpos/muonlifetime)*(1+asym)+B[det];
		temp[det]->SetBinContent(hbin,histval);
	}
}

temp[0]->Draw();
temp[1]->Draw("SAME");

	Double_t sumnow = temp[0]->Integral()+temp[1]->Integral();
	temp[0]->Scale(TotalStatistics/sumnow);
	temp[1]->Scale(TotalStatistics/sumnow);
	cout << "Statistics are : Left - " << temp[0]->Integral() << " Right - " << temp[1]->Integral() << " Total - " << temp[0]->Integral()+temp[1]->Integral() << endl;



for (Int_t det=0; det<=1;det++)
{
	histcalc[det] = new TH1F("N_"+detectors_str[det],"N_"+detectors_str[det], channels, 0,channels*dt);
	if (statisticalerrors)
				{
				TRandom rndg;
				for (Int_t rbin=1;rbin<=temp[det]->GetNbinsX();rbin++)
						{
						Double_t yvaltemp = temp[det]->GetBinContent(rbin);
						histcalc[det]->SetBinContent(rbin,rndg.Gaus(yvaltemp,TMath::Sqrt(yvaltemp)));
						histcalc[det]->SetBinError(rbin,TMath::Sqrt(yvaltemp));
						}

				}
				else
				{
				histcalc[det] = (TH1F*) temp[det]->Clone("temp_"+detectors_str[det]);
				for (Int_t rbin=1;rbin<=histcalc[det]->GetNbinsX();rbin++)
                        histcalc[det]->SetBinError(rbin,0);
				}
}



/////////////////////////////////////// OUTPUT - HEADER
Printf("Starting output.....");
ofstream myfile;
myfile.open (strNum+".nemu");
myfile << "- created by maxent test (Dec 2006/MD) -" << endl;
myfile << "NEMU_Run:            test"+strNum << endl;
myfile << "WKM_Run:             "+strNum << endl;
myfile << "Date:                Sat Oct 21 19:53:10 2006, Sat Oct 21 22:03:03 2006" << endl;
myfile << "Title:               " << pB->GetTitle() << endl;
myfile << "Statistics:          " << endl;
myfile << "Title3:              title3" << endl;
myfile << "Title4:              title4" << endl;
myfile << "TOF(M3S1):           nocut" << endl;
myfile << "Groups:              4" << endl;
myfile << "Channels:            " << histcalc[0]->GetNbinsX() << endl;
myfile << "Resolution:          " << (histcalc[0]->GetBinLowEdge(histcalc[0]->GetNbinsX()+1)-histcalc[0]->GetBinLowEdge(1))/histcalc[0]->GetNbinsX() << endl;
myfile << endl;

/////////////////////////////////////// OUTPUT - Block 1 (left detector)
Int_t rowpos=0; Double_t tval;
for (Int_t ii=1; ii<=histcalc[0]->GetNbinsX();ii++)
{
tval = histcalc[0]->GetBinContent(ii);
Double_t esroni = tval-(Int_t)tval;
if (esroni>0.5) tval=tval+1;
sprintf(dumbuf, "%i", tval);
TString binstr=dumbuf;
binstr.Prepend(' ',8-binstr.Length());
myfile << binstr;
if (++rowpos==10) { rowpos=0; myfile << endl; }
}
myfile << endl;

/////////////////////////////////////// OUTPUT - Block 2 (up detector)
rowpos=0;
for (Int_t ii=1; ii<=histcalc[0]->GetNbinsX();ii++)
{
tval = histcalc[0]->GetBinContent(ii);
Double_t esroni = tval-(Int_t)tval;
if (esroni>0.5) tval=tval+1;
sprintf(dumbuf, "%i", 0);
TString binstr=dumbuf;
binstr.Prepend(' ',8-binstr.Length());
myfile << binstr;
if (++rowpos==10) { rowpos=0; myfile << endl; }
}
myfile << endl;

/////////////////////////////////////// OUTPUT - Block 3 (right detector)
rowpos=0;
for (Int_t ii=1; ii<=histcalc[1]->GetNbinsX();ii++)
{
tval = histcalc[1]->GetBinContent(ii);
Double_t esroni = tval-(Int_t)tval;
if (esroni>0.5) tval=tval+1;
sprintf(dumbuf, "%i", tval);
TString binstr=dumbuf;
binstr.Prepend(' ',8-binstr.Length());
myfile << binstr;
if (++rowpos==10) { rowpos=0; myfile << endl; }
}
myfile << endl;

/////////////////////////////////////// OUTPUT - Block 4 (down detector)
rowpos=0;
for (Int_t ii=1; ii<=histcalc[1]->GetNbinsX();ii++)
{
tval = histcalc[1]->GetBinContent(ii);
Double_t esroni = tval-(Int_t)tval;
if (esroni>0.5) tval=tval+1;
sprintf(dumbuf, "%i", 0);
TString binstr=dumbuf;
binstr.Prepend(' ',8-binstr.Length());
myfile << binstr;
if (++rowpos==10) { rowpos=0; myfile << endl; }
}
myfile << endl;
myfile.close();

///// START AREA TO COMMENT OUT
TFile* f = new TFile();
f->Open(strNum+".root", “recreate”);
pB->Scale(1/pB->Integral(“width”));
pB->Write(“pB”);
histcalc[0]->Write(“N_l”);
histcalc[1]->Write(“N_r”);
ACalc[0]->Write(“A_l”);
ACalc[1]->Write(“A_r”);
sprintf(dumbuf, “A0_l = %f , alpha = %f , phase_l = %f , phaser = %f”, A0_l, alpha, phasel, phaser);
TObjString j(dumbuf);
j.Write(“Sample”);
sprintf(dumbuf, “N0_l = %f , N0_r = %f , B_l = %f , B_r = %f, TotalStat = %f”, N0[0], N0[1], B[0], B[1], TotalStatistics);
TObjString kstr(dumbuf);
kstr.Write(“Apparatus”);
f->Close()
delete f;
///// END AREA TO COMMENT OUT
cout << “Files for test " << strNum << " created.” << endl;
}
[/code]

replace the problematic end by:

TFile *f = TFile::Open(strNum+".root", "recreate"); pB->Scale(1/pB->Integral("width")); pB->Write("pB"); histcalc[0]->Write("N_l"); histcalc[1]->Write("N_r"); ACalc[0]->Write("A_l"); ACalc[1]->Write("A_r"); sprintf(dumbuf, "A0_l = %f , alpha = %f , phase_l = %f , phaser = %f", A0_l, alpha, phasel, phaser); TObjString j(dumbuf); j.Write("Sample"); sprintf(dumbuf, "N0_l = %f , N0_r = %f , B_l = %f , B_r = %f, TotalStat = %f", N0[0], N0[1], B[0], B[1], TotalStatistics); TObjString kstr(dumbuf); kstr.Write("Apparatus"); // f->Close() delete f;

Rene

Hi. Thanks.
As I saw in the root talk archieves, using the delete command “includes” also a Close command, but I tried it as well.
If I remove the f->Close(); line, it still doesn’t work… same behaviour.

(actually I added it manually after pasting in my code… this is why the “;” is missing :slight_smile:)…

my main change was with TFile::Open, not f->Close
Did you really apply my changes? If they do not work, send a complete example that we can run ourselves.

Rene