Segmentation Fault in HybridCalculatorOriginal

Hello,

I’ve encountered a problem when trying to incorporate HybridCalculatorOriginal into my ROOT script. The script’s basic structure is a for loop that will select a region of my data set, fit a signal and background PDF to it, call HybridCalculatorOriginal to determine significance, and then move to another region of the data set to repeat the process. Specifically:

for(int i = 0; i<max; i++)
{

   //Code before HCO: Set range of fit, create signal and background PDFs, fit, etc etc...

	//HybridCalculatorOriginal
	significance_HCO = 0;
	RooExtendPdf bkg_ext_pdf("bkg_ext_pdf","",bkg,nbkg);												//PDF for CLs testing
	HybridCalculatorOriginal myHybridCalc(data, model, bkg_ext_pdf,0,0);
	myHybridCalc.UseNuisance(false);
	myHybridCalc.SetTestStatistic(1);
	myHybridCalc.SetNumberOfToys(2000);																	//default is 1000
	HybridResult* myHybridResult = myHybridCalc.GetHypoTest();
	HybridPlot* myHybridPlot = myHybridResult->GetPlot("myHybridPlot","Results with H.C.O.",100);		//Plot
	TCanvas* canvas2 = new TCanvas("canvas2", "Second canvas", 1200, 1000);
	canvas2->cd(1);
	myHybridPlot->Draw();
	canvas2->SaveAs(strip+TString("ecm_")+strip2+TString("A0mass_myHybridPlot.png"));
	significance_HCO = myHybridResult->Significance();
	cout<<"signal_events = "<<signal_events<<endl;
	cout<<"significance_HCO = "<<significance_HCO<<endl;

   //Code after HCO

}

The code compiles and it even runs for a few iterations, but then the program will crash with a segmentation violation. If I lower the value of the argument in SetNumberOfToys it will run for more iterations, but it will still crash. One other thing I’ve noticed: after the 0th iteration of the code, the messages from HybridCalculatorOriginal include a warning about a potential memory leak. I’ve included one of the later iterations (before a crash) below, and one of a crash following it.

Test statistics has been evaluated for data
HybridCalculatorOriginal: run 2000 toy-MC experiments
with test statistics index: 1
....... toy number 0 / 2000
....... toy number 500 / 2000
....... toy number 1000 / 2000
....... toy number 1500 / 2000
Warning in <TFile::Append>: Replacing existing TH1: SB_model (Potential memory leak).
Warning in <TFile::Append>: Replacing existing TH1: B_model (Potential memory leak).
signal_events = -5
significance_HCO = 0.239136
Test statistics has been evaluated for data
HybridCalculatorOriginal: run 2000 toy-MC experiments
with test statistics index: 1
....... toy number 0 / 2000
....... toy number 500 / 2000
....... toy number 1000 / 2000
....... toy number 1500 / 2000

 *** Break *** segmentation violation

I’ve seen the same segmentation fault on two different machines, in cases where I run my code inside of the CINT, and in cases where I compile and run it from the shell. Any advice would be appreciated, and I’ll be happy to provide more information if that will help.

Thank you,

J. Rorie

In your “for” loop, after “canvas2->SaveAs”, try to add:
delete canvas2;
Also, the line:
canvas2->cd(1);
is a bit wrong as you do not create any sub-pads (so there’s no “1”, the only existing is “0”).
Maybe you should “delete myHybridPlot;”, too.

[quote=“Pepe Le Pew”]In your “for” loop, after “canvas2->SaveAs”, try to add:
delete canvas2;
Also, the line:
canvas2->cd(1);
is a bit wrong as you do not create any sub-pads (so there’s no “1”, the only existing is “0”).
Maybe you should “delete myHybridPlot;”, too.[/quote]

It is a bit early to say anything definitive, but your suggestion to explicitly delete myHybridPlot seems to have eliminated the memory leak errors. Thank you!

Fingers crossed that it eliminates the crash as well.

I really think you should explicitly “delete canvas2;” (before you “delete myHybridPlot;”) in your “for” loop.
In principle, you should get many warnings saying “Deleting canvas with same name: canvas2”, so ROOT will try to delete it “automatically”.
However, I remember cases in which this led to “segmentation violations”. As soon as I “manually” deleted the “old” canvas before creating the “new” one (with the same name), everything became fine.
So, now I tend to write: TCanvas *MyCanvas = ((TCanvas *)(gROOT->GetListOfMyCanvases()->FindObject("MyCanvas"))); if (MyCanvas) delete MyCanvas; // make sure you delete it, if it already exists MyCanvas = new TCanvas("MyCanvas", "MyCanvas", 800, 600);

I’m sorry, I wasn’t clear. I applied all of your suggestions; it was the deletion of myHybridPlot that eliminated the error in output.

Unfortunately, those suggestions did not remedy the problem. The program still crashes with a segmentation fault while running toy-MC. I should note that when I remove the HybridCalculatorOriginal section the code executes to completion.

One last note: out of desperation I’ve tried creating a HybridCalculatorOriginal object on the heap and then explicitly deleting it. That also failed. The code rewrite for this looks like:

	//HybridCalculatorOriginal
	significance_HCO = 0;
	RooExtendPdf bkg_ext_pdf("bkg_ext_pdf","",bkg,nbkg);												//PDF for CLs testing
	//HybridCalculatorOriginal myHybridCalc(data, model, bkg_ext_pdf,0,0);
	HybridCalculatorOriginal * myHybridCalc = new HybridCalculatorOriginal(data, model, bkg_ext_pdf,0,0);
	myHybridCalc->UseNuisance(false);
	myHybridCalc->SetTestStatistic(1);
	myHybridCalc->SetNumberOfToys(2000);																	//default is 1000
	HybridResult* myHybridResult = myHybridCalc->GetHypoTest();
	HybridPlot* myHybridPlot = myHybridResult->GetPlot("myHybridPlot","Results with H.C.O.",100);		//Plot
	TCanvas* canvas2 = new TCanvas("canvas2", "Second canvas", 1200, 1000);
	//canvas2->cd(1);
	myHybridPlot->Draw();
	canvas2->SaveAs(strip+TString("ecm_")+strip2+TString("A0mass_myHybridPlot.png"));
	significance_HCO = myHybridResult->Significance();
	cout<<"signal_events = "<<signal_events<<endl;
	cout<<"significance_HCO = "<<significance_HCO<<endl;
	delete canvas2;
	delete myHybridPlot;
	delete myHybridResult;
	delete myHybridCalc;

Thanks

Are you sure that for all possible “i” (0 … max-1) the “bkg”, “nbkg”, “data” and “model” are o.k.?

I think you need to report your ROOT version, your Operating System (version) and your compiler (version).

[quote=“Pepe Le Pew”]Are you sure that for all possible “i” (0 … max-1) the “bkg”, “nbkg”, “data” and “model” are o.k.?

I think you need to report your ROOT version, your Operating System (version) and your compiler (version).[/quote]

Yes. If I remove the HCO section, the code exits normally.

The versions for the system I’m currently running on:
ROOT ver: 5.33/01
Operating System: OS X 10.8
Compiler: gcc version 4.2.1

I’ve also seen the same results on OS X 10.7 and on RedHat Linux.

Thanks for your assistance.

Hi,

Have you tried using the new HybridCalculator class instead of HybridCalculatorOriginal ? The “original” class is probably going to be deprecated, since all the functionality is provided by the new class.

Best Regards

Lorenzo

[quote=“moneta”]Hi,

Have you tried using the new HybridCalculator class instead of HybridCalculatorOriginal ? The “original” class is probably going to be deprecated, since all the functionality is provided by the new class.

Best Regards

Lorenzo[/quote]

I have not; thank you for the suggestion. Hopefully that will solve my problem!

Thanks,

J. Rorie