AsymptoticCalculator crash after Initialization when called in custom function

Hello experts,
I am experiencing an issue in a code where I try to call the AsymptoticCalculator multiple times to test the expected limit for a variety of ranges for my main observable x.
The way I’m trying to do this is by using a loop that goes over the ranges I want to test it for and calls then a custom function sensitivity() that gives back the values I am interested in which then are stored in vectors before the loop moves on to the next range. Here is the loop I am using:

    vector<Double_t> resultvec;
    Double_t sen[10],esenm[10],esenp[10],bin[10];


    for(int num=1;num<=10;num++) {

        int counter = num*10;
        x.setRange(430-counter,430+counter);

        resultvec = sensitivity(counter, master, constr_tot_model, sample, sbmc, bmc, w);



        bin[num-1] = resultvec[0];
        sen[num-1] = resultvec[1];
        esenm[num-1] = resultvec[1] - resultvec[2];
        esenp[num-1] = resultvec[3] - resultvec[1];

    }

This is the sensitivity() function I am using:

vector <Double_t>
sensitivity(int counter, TChain* master, RooSimultaneous constr_tot_model, RooCategory sample,
            ModelConfig sbmc, ModelConfig bmc, RooWorkspace w) { //

    int elow = 430 - counter;
    int ehigh = 430 + counter;
    double binnumber = 2.*counter;

    //x.setRange(elow,ehigh);

    RooDataSet *data_one = new RooDataSet("data_one","data_one",RooArgSet(x));
    RooDataSet *data_two = new RooDataSet("data_two","data_two",RooArgSet(x));

    RooDataSet *data_three = new RooDataSet("data_three","data_three",RooArgSet(x));
    RooDataSet *data_four = new RooDataSet("data_four","data_four",RooArgSet(x));
    RooDataSet *data_five = new RooDataSet("data_five","data_five",RooArgSet(x));

    //a bit more complicated code that uses the TChain* master and fills the data_one etc by using x.setVal() and data_one->add(RooArgSet(x))
    //this works well outside of the loop, the code crashes later

    //bin data sets
    x.setBins(binnumber);
    RooDataHist* h_data_one = new RooDataHist("h_data_one", "binned version of data_one", RooArgSet(x),*data_one);
    RooDataHist* h_data_two = new RooDataHist("h_data_two", "binned version of data_two", RooArgSet(x),*data_two);
    RooDataHist* h_data_three = new RooDataHist("h_data_three", "binned version of data_three", RooArgSet(x),*data_three);
    RooDataHist* h_data_four = new RooDataHist("h_data_four", "binned version of data_four", RooArgSet(x),*data_four);
    RooDataHist* h_data_five = new RooDataHist("h_inv_data_coax", "binned version of data_five", RooArgSet(x),*data_five);

    //define combined data set
    RooDataHist combData("combData","combined data sample", x, Index(sample), Import("one",*h_data_one), Import("two",*h_data_two), Import("three",*h_data_three), Import("four",*h_data_four), Import("five",*h_data_five));
    RooDataHist binnedData = combData;

    w.import(binnedData);


    AsymptoticCalculator ac(binnedData, bmc, sbmc);
    ac.SetOneSided(true);
    ac.SetPrintLevel(-1);


    HypoTestInverter calc(ac);
    calc.SetConfidenceLevel(0.9);
    calc.UseCLs(true);
    calc.SetVerbose(false);


    ToyMCSampler *toymcs = (ToyMCSampler*)ac.GetTestStatSampler();//toymcs = calc.GetHypoTestCalculator()->GetTestStatSampler();

    ProfileLikelihoodTestStat profll(*sbmc.GetPdf());
    profll.SetOneSided(true);
    toymcs->SetTestStatistic(&profll);

    if (!sbmc.GetPdf()->canBeExtended()){
        toymcs->SetNEventsPerToy(1);
        cout << "can not be extended" << endl;
    }

    calc.SetFixedScan(30,0,0.2);
    HypoTestInverterResult* r = calc.GetInterval();
    expectedLimit = r->GetExpectedUpperLimit(0);

    cout << "Expected Limit: " << expectedLimit << endl;

    double median_half_life = 1./expectedLimit;
    double min_one_sigma = 1./r->GetExpectedUpperLimit(+1);
    double plu_one_sigma = 1./r->GetExpectedUpperLimit(-1);

    cout << median_half_life << endl;
    cout << min_one_sigma << endl;
    cout << plu_one_sigma << endl;

    cout << "Success" << endl;

    return {binnumber,median_half_life,min_one_sigma,plu_one_sigma};

}

The code crashes in the very first run of the loop when the AsymptoticCalculator is called. This is the error message I’m receiving:

[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset combData
[#0] PROGRESS:Eval -- AsymptoticCalculator::Initialize....
[#0] PROGRESS:Eval -- AsymptoticCalculator::Initialize - Find  best unconditional NLL on observed data

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f32fe73746c in waitpid () from /lib64/libc.so.6
#1  0x00007f32fe6b4f62 in do_system () from /lib64/libc.so.6
#2  0x00007f32ff73b8f9 in TUnixSystem::StackTrace (this=0x9df8e0) at /tmp/user-build-root-sN9Fat/root/core/unix/src/TUnixSystem.cxx:2324
#3  0x00007f32ff73d5fc in TUnixSystem::DispatchSignals (this=0x9df8e0, sig=kSigSegmentationViolation) at /tmp/user-build-root-sN9Fat/root/core/unix/src/TUnixSystem.cxx:3562
#4  <signal handler called>
#5  0x00007f32fea4f4fb in raise () from /lib64/libpthread.so.0
#6  0x00007f32e8e63b4d in softAbort () at /tmp/user-build-root-sN9Fat/root_build_linux-redhat-7/include/RooErrorHandler.h:30
#7  RooAbsOptTestStatistic::initSlave (this=this
entry=0x768de70, real=..., indata=..., projDeps=..., rangeName=rangeName
entry=0x0, addCoefRangeName=addCoefRangeName
entry=0x0) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsOptTestStatistic.cxx:264
#8  0x00007f32e8e646f1 in RooAbsOptTestStatistic::RooAbsOptTestStatistic (this=0x768de70, name=<optimized out>, title=<optimized out>, real=..., indata=..., projDeps=..., rangeName=0x0, addCoefRangeName=0x0, nCPU=1, interleave=RooFit::BulkPartition, verbose=false, splitCutRange=false) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsOptTestStatistic.cxx:136
#9  0x00007f32e8f5e9b6 in RooNLLVar::RooNLLVar (this=0x768de70, name=<optimized out>, title=<optimized out>, pdf=..., indata=..., projDeps=..., extended=true, rangeName=0x0, addCoefRangeName=0x0, nCPU=1, interleave=RooFit::BulkPartition, verbose=false, splitRange=false, cloneData=false, binnedL=false) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooNLLVar.cxx:155
#10 0x00007f32e8f5f288 in RooNLLVar::create (this=this
entry=0x770d9e0, name=0x4ed233c "one", title=0x4ed233c "one", pdf=..., adata=..., projDeps=..., rangeName=rangeName
entry=0x0, addCoefRangeName=addCoefRangeName
entry=0x0, nCPU=nCPU
entry=1, interleave=interleave
entry=RooFit::BulkPartition, verbose=verbose
entry=false, splitRange=splitRange
entry=false, binnedL=binnedL
entry=false) at /tmp/user-build-root-sN9Fat/root_build_linux-redhat-7/include/RooNLLVar.h:52
#11 0x00007f32e8e94196 in RooAbsTestStatistic::initSimMode (this=this
entry=0x770d9e0, simpdf=0x4d2beb0, data=<optimized out>, projDeps=0x51723a4, rangeName=0x0, addCoefRangeName=0x0) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsTestStatistic.cxx:565
#12 0x00007f32e8e94fc8 in RooAbsTestStatistic::initialize (this=0x770d9e0) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsTestStatistic.cxx:348
#13 0x00007f32e8e95409 in RooAbsTestStatistic::constOptimizeTestStatistic (this=this
entry=0x770d9e0, opcode=opcode
entry=RooAbsArg::Activate, doAlsoTrackingOpt=doAlsoTrackingOpt
entry=true) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsTestStatistic.cxx:412
#14 0x00007f32e8e65695 in RooAbsOptTestStatistic::constOptimizeTestStatistic (this=0x770d9e0, opcode=RooAbsArg::Activate, doAlsoTrackingOpt=true) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsOptTestStatistic.cxx:511
#15 0x00007f32e8e3890b in RooAbsArg::constOptimizeTestStatistic (this=<optimized out>, opcode=RooAbsArg::Activate, doAlsoTrackingOpt=<optimized out>) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooAbsArg.cxx:1743
#16 0x00007f32e8f3dcaa in RooMinimizer::optimizeConst (this=this
entry=0x7fff4c0ca9a0, flag=flag
entry=2) at /tmp/user-build-root-sN9Fat/root/roofit/roofitcore/src/RooMinimizer.cxx:615
#17 0x00007f32e849160c in RooStats::AsymptoticCalculator::EvaluateNLL (pdf=..., data=..., condObs=<optimized out>, poiSet=poiSet
entry=0x0) at /tmp/user-build-root-sN9Fat/root/roofit/roostats/src/AsymptoticCalculator.cxx:342
#18 0x00007f32e8495b32 in RooStats::AsymptoticCalculator::Initialize (this=this
entry=0x7fff4c0cb2c0) at /tmp/user-build-root-sN9Fat/root/roofit/roostats/src/AsymptoticCalculator.cxx:163
#19 0x00007f32e8496787 in RooStats::AsymptoticCalculator::AsymptoticCalculator (this=0x7fff4c0cb2c0, data=..., altModel=..., nullModel=..., nominalAsimov=<optimized out>) at /tmp/user-build-root-sN9Fat/root/roofit/roostats/src/AsymptoticCalculator.cxx:76
#20 0x00007f32ffd51e75 in ?? ()
#21 0x00007fff4c0cb8d8 in ?? ()
#22 0x00007fff4c0cb7e8 in ?? ()
#23 0x00007fff4c0cb6f8 in ?? ()
#24 0x00007fff4c0cb608 in ?? ()
#25 0x00007fff4c0cb518 in ?? ()
#26 0x00007fff4c0cc160 in ?? ()
#27 0x0000000000000000 in ?? ()
===========================================================

I have tried running the code without the separate custom function and loop and it worked well. On the other hand I have previously worked with a very similar code that changed the binning, not the range of the observable and that worked fine. I am a bit puzzled as to what might cause this problem. I would be happy if anyone could help! I’m also open to different ideas how to get the expected limit for multiple data ranges.

Best regards and many thanks in advance!

Hi @bibsession!

In your sensitivity function, you take all the arguments by value, which causes the RooFit objects to be copied which most likely messes up the computation graph and you’ll get this error.

If you take the arguments by reference like this, I think it should work:

vector<double> sensitivity(int counter,
                           TChain* master,
                           RooSimultaneous const& constr_tot_model,
                           RooCategory& sample,
                           ModelConfig const& sbmc,
                           ModelConfig const& bmc,
                           RooWorkspace& w);

And keep in mind to use a const-reference (const&) whenever possible. In your sensitivity function, you can do it for everything except for the RooCategory and the RooWorkspace.

I hope this fixes the problem and please let us know if you have any further questions!

Cheers,
Jonas

1 Like

Thank you, Jonas! I never would have thought of that on my own!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.