Skewed Normal Distribution Code is Crashing

Hi everyone,

I am an undergraduate physics researcher. I am trying to make a skewed normal distribution curve to appear on a canvas pad with a histogram for my job. The problem is that my code keeps not working or crashing.

I started off with a “void maincode()” method. I tried making my skewed normal distribution function to appear on a canvas while having my histogram code commented out, but I kept getting the error “Warning in TPad::ResizePad: Inf/NaN propagated to the pad. Check drawn objects.
Warning in TPad::ResizePad: c1_1 height changed from 0 to 10.” At this point, I separated the histogram and function onto two different canvas pads. I recommented in my histogram code, which made my histogram appear on the top canvas pad, but there was nothing at all where the bottom canvas pad was supposed to be.

I then decided to change my method “void maincode()” to “int main()” after seeing many people online use “int main()” for their main code. This made my whole program crash with the following errors:

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007fe5f411812a in wait4 () from /usr/lib64/libc.so.6
#1  0x00007fe5f406198b in do_system () from /usr/lib64/libc.so.6
#2  0x00007fe5f4af18ec in TUnixSystem::StackTrace() () from /opt/root_v6-26-00/lib/libCore.so
#3  0x00007fe5f4aeefe5 in TUnixSystem::DispatchSignals(ESignals) () from /opt/root_v6-26-00/lib/libCore.so
#4  <signal handler called>
#5  0x00007fe5f40bc58b in __strcmp_avx2 () from /usr/lib64/libc.so.6
#6  0x000000000040131e in handle_notebook_option(int, char**) ()
#7  0x0000000000401186 in main ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007fe5f40bc58b in __strcmp_avx2 () from /usr/lib64/libc.so.6
#6  0x000000000040131e in handle_notebook_option(int, char**) ()
#7  0x0000000000401186 in main ()
===========================================================

I’m not really sure what is wrong with my code. I figure it might be something to do with how I define my skewed normal distribution in “MyMethod” and pass it to my TF1, “graph_function,” or possibly how my computer is setup to run ROOT CERN.

Here is my most updated code:

#include "TF1.h"
#include "TMath.h"

Double_t MyMethod(Double_t *x, Double_t *par)
{

        Double_t exponent = ((-((x[0] - par[2])/par[3]) - par[0]) * (-((x[0] - par[2])/par[3]) - par[0])) / (2 * par[1] * par[1]);
        Double_t erf_top_limit = par[4] * (x[0] - par[2]) / (par[3] * TMath::Sqrt(2));
        Double_t function = (1 / (par[3] * par[1] * TMath::Sqrt(2 * TMath::Pi()))) * TMath::Exp(exponent) * (1 + TMath::Erf(erf_top_limit));

        return function;

}
int main()
{
        //Normal Histogram Stuff
        //gStyle->SetOptTitle(1);
        //gStyle->SetOptStat(0);
        TCanvas* c1 = new TCanvas("c1", "", 20, 20, 1000, 1000);
        c1->Divide(1,2);
        c1->cd(1);

        TString filename = "~/Vincent/G4CATS/Out/B4_200MeV.root";
        TFile *f = TFile::Open(filename);

        TH1F* h1 = new TH1F("Histogram Statistics", "", 300, 180, 210);

        TTreeReader r1("B4", f);
        TTreeReaderValue<Double_t> Ecore(r1, "Ecore");
        TTreeReaderValue<Double_t> Eann1(r1, "Eann1");
        TTreeReaderValue<Double_t> Eann2(r1, "Eann2");
        TTreeReaderValue<Double_t> Eann3(r1, "Eann3");
        TTreeReaderValue<Double_t> Eann4(r1, "Eann4");
        TTreeReaderValue<Double_t> Eann5(r1, "Eann5");
        TTreeReaderValue<Double_t> Eann6(r1, "Eann6");

        while (r1.Next())
        {
                h1->Fill(*Ecore + *Eann1 + *Eann2 + *Eann3 + *Eann4 + *Eann5 + *Eann6);
        }

        h1->GetXaxis()->SetTitle("Energy (MeV)");
        h1->GetYaxis()->SetTitle("Counts");
        h1->SetTitle("Energy Recorded by G4 CATS sim -- 200MeV Photon Beam");
        h1->Draw();
        //Histogram Stats
        Double_t BinWithMostCounts = h1->GetMaximumBin();
        Double_t MaxYValue = h1->GetBinContent(BinWithMostCounts);
        Double_t CenterPeak = h1->GetBinCenter(BinWithMostCounts);
        Double_t StdDev = h1->GetStdDev();

        c1->cd(2);

        //180 and 210 are the x-axis limits for the graph. 5 means there are 5 parameters being passed to the TF1
        TF1 *graph_function = new TF1("graph_function", MyMethod, 180, 210, 5);

        //0 is the mean; 1 is the standard deviation; 2 is E; 3 is w; 4 is a (the shape parameter)
        graph_function->SetParameter(0, CenterPeak);
        graph_function->SetParameter(1, StdDev);
        graph_function->SetParameter(2, 0);
        graph_function->SetParameter(3, 1);
        graph_function->SetParameter(4, -10);
        graph_function->Draw("SAME");

        return 0;

}

Note that I have indented everything that needs to be indented, but for some reason, this textbox is not showing that. I indented everything except for the two methods, and I double indented the line inside the “while” loop.

Thank you for your help in advance!

-Laura

Hi @ldhubbert,

thank you for your question.

So firstly I would check again your skewed normal distribution definition, maybe this post could be useful for you: Clarifying a user defined function (skewed gaussian) (you can make use of the existing ROOT “gauss” function to make it simpler and avoid potential mistakes).

Secondly, it might also be easier to write ROOT macros instead of the C++ application that you need to compile as well, see: A ROOT Guide For Beginners.

Cheers,
Marta

Hi @mczurylo ,

I would prefer to keep my equation written out in full, since I might end up modifying it a bit down the road.

Also, my file is written in C++ code and accessible in ROOT (since it has a .C extension.) I named it “testskew2.C.”

Sincerely,
Laura

Hi everyone!

Turns out, I typed in my skewed Gaussian formula incorrectly in the code. My Double_t exponent in MyMethod should be as follows:
Double_t exponent = (-1 * (((x[0] - par[2])/par[3]) - par[0]) * (((x[0] - par[2])/par[3]) - par[0])) / (2 *par[1] * par[1]);

It’s strange that such a little typo with the negative signs could cause my code to crash. Thanks everyone for your contributions :slight_smile:

1 Like

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