Segfault when trying to create TH2D contour

I’m trying to create a TH2D contour from an exclusion fit. Since our normal plotting macros segfaulted I decided to investigate and plot it by hand. What I do is the following:

[code]gbesjes@athena:/tmp $ root -l ZL2012__SRCtight_Gluino_Stop_charm_Output_fixSigXSecNominal_hypotest__1_harvest_list.root
root [0]
Attaching file ZL2012__SRCtight_Gluino_Stop_charm_Output_fixSigXSecNominal_hypotest__1_harvest_list.root as _file0…
root [1] .ls
TFile** ZL2012__SRCtight_Gluino_Stop_charm_Output_fixSigXSecNominal_hypotest__1_harvest_list.root
TFile* ZL2012__SRCtight_Gluino_Stop_charm_Output_fixSigXSecNominal_hypotest__1_harvest_list.root
KEY: TH2D hclPmin2;1 Observed CLsplusb
KEY: TH2D sigp1;1 One-sided significance of CLsplusb
KEY: TH2D sigp1clsf;2 One-sided significalce of observed CLs
KEY: TH2D sigp1clsf;1 Observed CLs
KEY: TH2D sigp1expclsf;1 One-sided significalce of expected CLs
KEY: TH2D sigclsu1s;1 One-sided significalce of expected CLs (+1 sigma)
KEY: TH2D sigclsd1s;1 One-sided significalce of expected CLs (-1 sigma)
KEY: TH2D upperLimit;1 upperlimit
KEY: TH2D xsec;1 xsec
KEY: TH2D excludedXsec;1 excludedXsec
root [2] sigp1->Draw(“CONTLIST”)
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [3] gPad->Update()
root [4] TObjArray *contours = gROOT->GetListOfSpecials()->FindObject(“contours”)
root [5] TList list = (TList) contours->At(0)
root [6] list->GetSize()
(const Int_t)1
root [7] TGraph gr1 = (TGraph) list->First()
root [8] gr1->Draw()
root [9] gr1->Draw()

*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

Thread 1 (process 40611):
#0 0x00007fff8fed86ac in wait4 ()
#1 0x00007fff980d903a in system ()
#2 0x00000001080f2b86 in TUnixSystem::StackTrace ()
#3 0x00000001080f0917 in TUnixSystem::DispatchSignals ()
#4
#5 0x0000000108304537 in G__G__Base2_11_0_15 ()
#6 0x00000001088fb35b in Cint::G__ExceptionWrapper ()
#7 0x00000001089aab3a in G__execute_call ()
#8 0x00000001089ab11b in G__call_cppfunc ()
#9 0x000000010897f205 in G__interpret_func ()
#10 0x0000000108968750 in G__getfunction ()
#11 0x0000000108a6b2e9 in G__getstructmem ()
#12 0x0000000108a61b63 in G__getvariable ()
#13 0x000000010895d7c2 in G__getitem ()
#14 0x000000010895ca1a in G__getexpr ()
#15 0x00000001089dce31 in G__exec_statement ()
#16 0x000000010893f2e8 in G__exec_tempfile_core ()
#17 0x000000010893ee99 in G__exec_tempfile_fp ()
#18 0x00000001089e8b97 in G__process_cmd ()
#19 0x00000001080b8dd2 in TCint::ProcessLine ()
#20 0x0000000108021d97 in TApplication::ProcessLine ()
#21 0x0000000109027f1c in TRint::HandleTermInput ()
#22 0x00000001090265c7 in TTermInputHandler::Notify ()
#23 0x000000010902878d in TTermInputHandler::ReadNotify ()
#24 0x00000001080efdce in TUnixSystem::CheckDescriptors ()
#25 0x00000001080ef625 in TUnixSystem::DispatchOneEvent ()
#26 0x0000000108079c6d in TSystem::InnerLoop ()
#27 0x0000000108079a9e in TSystem::Run ()
#28 0x0000000108022894 in TApplication::Run ()
#29 0x00000001090278ca in TRint::Run ()
#30 0x0000000108015a3f in main ()

The crash is most likely caused by a problem in your script.
Try to compile it (.L myscript.C+g) and fix any errors.
If that does not help then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.[/code]

The initial drawing of the contour in [2] goes perfectly fine. I see 20 nicely coloured contours, of which I’m just interested in one, since the aim is to create an exclusion plot. (Our full macro calls h->SetContourLevel( 0, signif ); to set the 0th contour to 95%, but that is not the cause of the problem). The first drawing call in [8] does nothing, at [9] the segmentation fault occurs.

What I’m trying to draw is an exclusion line in SUSY space, so I initially used the rather well-tested scripts used in the ATLAS SUSY group. The only difference is that here I have run over a different SUSY grid, but that makes no difference here: only the x- and y-axis have a different interpretation but that’s not what causes this problem. (When plotting with the CONTZLIST option they nicely match what the input is.)

The TH2D in question is made from an ascii file:

0.001916 0.001916 0.008588 -1 -1 0 0.033182 -1 -1.000000 -1.000000 0.153639 0.005511 0.458768 0.000798 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1000.000000 200.000000 0.008950 0.008950 0.041962 -1 -1 0 0.115812 -1 -1.000000 -1.000000 0.336911 0.031811 0.680948 0.007768 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1000.000000 300.000000 0.076030 0.076030 0.379754 -1 -1 0 0.554242 -1 -1.000000 -1.000000 0.782768 0.351441 0.941950 0.210069 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1000.000000 500.000000 0.105116 0.105116 0.808193 -1 -1 0 0.899071 -1 -1.000000 -1.000000 0.961215 0.818795 0.992046 0.734805 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1000.000000 700.000000 0.004946 0.004946 0.033767 -1 -1 0 0.126536 -1 -1.000000 -1.000000 0.355125 0.036162 0.697437 0.009206 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1100.000000 200.000000 0.009315 0.009315 0.061070 -1 -1 0 0.184434 -1 -1.000000 -1.000000 0.441850 0.062882 0.766901 0.019278 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1100.000000 300.000000 0.044268 0.044268 0.286137 -1 -1 0 0.492254 -1 -1.000000 -1.000000 0.740437 0.288852 0.926533 0.158583 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1100.000000 500.000000 0.080038 0.080038 0.776258 -1 -1 0 0.888048 -1 -1.000000 -1.000000 0.956673 0.800364 0.991049 0.709715 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1100.000000 700.000000 0.085437 0.085437 0.926086 -1 -1 0 0.966171 -1 -1.000000 -1.000000 0.987544 0.936689 0.997555 0.903528 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1100.000000 900.000000 0.009894 0.009894 0.103142 -1 -1 0 0.305256 -1 -1.000000 -1.000000 0.582326 0.135023 0.854606 0.054603 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1200.000000 200.000000 0.014635 0.014635 0.141737 -1 -1 0 0.359345 -1 -1.000000 -1.000000 0.633781 0.174237 0.880718 0.077770 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1200.000000 300.000000 0.049378 0.049378 0.372678 -1 -1 0 0.591793 -1 -1.000000 -1.000000 0.806555 0.392296 0.949983 0.246263 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1200.000000 500.000000 0.095648 0.095648 0.722576 -1 -1 0 0.848133 -1 -1.000000 -1.000000 0.939684 0.735737 0.987194 0.624515 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1200.000000 700.000000 0.129789 0.129789 0.959396 -1 -1 0 0.979557 -1 -1.000000 -1.000000 0.992536 0.961420 0.998548 0.940726 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1200.000000 900.000000 0.022609 0.022609 0.282435 -1 -1 0 0.549913 -1 -1.000000 -1.000000 0.779940 0.346876 0.940966 0.206149 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1300.000000 200.000000 0.022908 0.022908 0.290407 -1 -1 0 0.558913 -1 -1.000000 -1.000000 0.785799 0.356401 0.942999 0.214356 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1300.000000 300.000000 0.035208 0.035208 0.427461 -1 -1 0 0.674566 -1 -1.000000 -1.000000 0.854679 0.490497 0.964911 0.341212 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1300.000000 500.000000 0.093933 0.093933 0.950180 -1 -1 0 0.976998 -1 -1.000000 -1.000000 0.991588 0.956660 0.998360 0.933518 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1300.000000 900.000000 0.171468 0.171468 0.992418 -1 -1 0 0.995902 -1 -1.000000 -1.000000 0.998519 0.992187 0.999715 0.987873 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 1100.000000 0.154605 0.154605 0.235990 -1 -1 0 0.156748 -1 -1.000000 -1.000000 0.402534 0.049440 0.737127 0.013962 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 200.000000 0.037023 0.037023 0.485008 -1 -1 0 0.721717 -1 -1.000000 -1.000000 0.879733 0.551657 0.972008 0.405816 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 300.000000 0.041544 0.041544 0.589007 -1 -1 0 0.793935 -1 -1.000000 -1.000000 0.915183 0.653099 0.981289 0.521900 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 500.000000 0.062497 0.062497 0.745442 -1 -1 0 0.877263 -1 -1.000000 -1.000000 0.952167 0.782576 0.990046 0.685835 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 700.000000 0.171284 0.171284 0.957066 -1 -1 0 0.976228 -1 -1.000000 -1.000000 0.991303 0.955231 0.998304 0.931360 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 1400.000000 900.000000 0.024992 0.024992 0.041476 -1 -1 0 0.026413 -1 -1.000000 -1.000000 0.132190 0.004039 0.422517 0.000537 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 700.000000 200.000000 0.008020 0.008020 0.021618 -1 -1 0 0.037637 -1 -1.000000 -1.000000 0.166806 0.006550 0.479509 0.000995 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 700.000000 300.000000 0.500000 0.500000 0.816669 -1 -1 0 0.775513 -1 -1.000000 -1.000000 0.906457 0.626304 0.979086 0.490169 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 700.000000 500.000000 0.001973 0.001973 0.004451 -1 -1 0 0.006150 -1 -1.000000 -1.000000 0.048681 0.000581 0.235095 0.000047 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 800.000000 200.000000 0.017635 0.017635 0.051817 -1 -1 0 0.090325 -1 -1.000000 -1.000000 0.289941 0.022271 0.634759 0.004858 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 800.000000 300.000000 0.071855 0.071855 0.350438 -1 -1 0 0.523231 -1 -1.000000 -1.000000 0.762095 0.319384 0.934605 0.183087 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 800.000000 500.000000 0.001927 0.001927 0.006133 -1 -1 0 0.016133 -1 -1.000000 -1.000000 0.094939 0.002078 0.350379 0.000232 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 900.000000 200.000000 0.008564 0.008564 0.026216 -1 -1 0 0.053015 -1 -1.000000 -1.000000 0.207936 0.010519 0.538238 0.001830 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 900.000000 300.000000 0.144810 0.144810 0.502795 -1 -1 0 0.617252 -1 -1.000000 -1.000000 0.821959 0.421290 0.954951 0.273145 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -999007. -999007. 900.000000 500.000000
Which is interpreted into a tree as

p0:p1:CLs:mode:nexp:seed:CLsexp:fID:sigma0:sigma1:clsu1s:clsd1s:clsu2s:clsd2s:upperLimit:upperLimitEstimatedError:expectedUpperLimit:expectedUpperLimitPlus1Sig:expectedUpperLimitPlus2Sig:expectedUpperLimitMinus1Sig:expectedUpperLimitMinus2Sig:xsec:excludedXsec:m0:m12 as is our default.

Any idea how I might go about solving this problem?

[size=75]If it is of interest, the following is the relevant snippet in our plotting code:

cout << "==> Will dumb histogram: " << h->GetName() << " into a graph" <<endl; h->SetContour( 1 ); //h->GetXaxis()->SetRangeUser(250,1200); h->GetXaxis()->SetRangeUser(xmin,xmax); //h->GetYaxis()->SetRangeUser(2,50); double pval = CombinationGlob::cl_percent[1]; double signif = TMath::NormQuantile(1-pval); h->SetContourLevel( 0, signif ); h->Draw("CONT LIST"); h->SetDirectory(0); gPad->Update(); TObjArray *contours = gROOT->GetListOfSpecials()->FindObject("contours"); Int_t ncontours = contours->GetSize(); //cout << "Found " << ncontours << " contours " << endl; TList *list = (TList*)contours->At(0); gr = (TGraph*)list->First(); gr->SetName(hist->GetName())
This pointer gr is then returned, after which gr_contour_1su = (TGraph*) ContourGraph( hist0_1su ,xmin,1200)->Clone() segfaults - since there is no graph to clone.
[/size]

Try: TGraph *gr1 = (TGraph*) list->First()->Clone(); gr1->Draw("ALP");

[quote=“Wile E. Coyote”]Try: TGraph *gr1 = (TGraph*) list->First()->Clone(); gr1->Draw("ALP");[/quote]

That indeed solves the problem in interactive mode, thanks! Now I can continue investigating why the macro fails at certain times.

After: gr->SetName(hist->GetName()) ROOT will have two completely different memory resident objects ("gr and “hist”) with the same name.
You’re asking for trouble, I believe.
Try something like: gr->SetName(TString::Format("gr_%s", hist->GetName())); or: gr = (TGraph*)list->First()->Clone(TString::Format("gr_%s", hist->GetName()));