*** Break *** segmentation violation when fill TH3

Hello everyone,

Now I write a program that can select some signal to fill histogarm, TH1, TH2, and TH3, but this program can’t normally work when TH3 is filled. TH1 and TH2 are filled successfully, but when TH3 is filled, an error will be reported. I add some input message to confirm my viewpoint.

I will show a part of code and the error sended from runing program in the below of help post. You can see completed code and a simple data file in Github

_ROOT Version:6.28/06
_Platform:WSL2 Ubuntu22.04
_Compiler:g++

// tips 
// typedef TH1F YTH1;
// typedef TH2F YTH2;
// typedef TH3I YTH3;
bool YMatrix::CreateHistogram(YTH1 *phistogram, const TString &pshname)
{
    phistogram = new YTH1(("h1" + pshname).Data(), (pshname + "_projection").Data(), EBINNUMBER, EMIN, EMAX);
    if (phistogram != nullptr)
    {
        cout << "Create h1: " << phistogram << "\t" << phistogram->GetName() << endl;
        phistogram->Fill(0.0);
        return true;
    }
    else
    {
        cerr << "Can't create " << phistogram->Class_Name() << "named " << pshname << " histogram!" << endl;
        return false;
    }
}
bool YMatrix::CreateHistogram(YTH2 *phistogram, const TString &pshname)
{
    phistogram = new YTH2(("h2" + pshname).Data(), (pshname + "_2matrix").Data(), EBINNUMBER, EMIN, EMAX, EBINNUMBER, EMIN, EMAX);
    if (phistogram != nullptr)
    {
        cout << "Create h2: " << phistogram << "\t" << phistogram->GetName() << endl;
        phistogram->Fill(0.0, 0.0);
        return true;
    }
    else
    {
        cerr << "Can't create " << phistogram->Class_Name() << "named " << pshname << " histogram!" << endl;
        return false;
    }
}
bool YMatrix::CreateHistogram(YTH3 *phistogram, const TString &pshname)
{
    phistogram = new YTH3(("h3" + pshname).Data(), (pshname + "_3matrix").Data(), EBINNUMBER, EMIN, EMAX, EBINNUMBER, EMIN, EMAX, EBINNUMBER, EMIN, EMAX);
    if (phistogram != nullptr)
    {
        cout << "Create h3: " << phistogram << "\t" << phistogram->GetName() << endl;
        phistogram->Fill(0.0, 0.0, 0.0);
        return true;
    }
    else
    {
        cerr << "Can't create " << phistogram->Class_Name() << "named " << pshname << " histogram!" << endl;
        return false;
    }
}
Process file from begining file  to end file!
run file number from 1081 to 1081
Read file: /mnt/g/GC_event/event_hit_W1000_1081_addback_doppler.root
Output file: /mnt/g/GC_matrix/matrix_2PAM_2PM_3PAM_1081_1081_CSIW40_GECSIWminus70_200_GEW70_AMWminus150_minus100_100_150.root:/
Create h1: 0x558f8a272630       h1alpha0_proton0_total4PAM
Create h1: 0x558f8b4cd120       h1alpha0_proton0_total4AM
Create h1: 0x558f8b521cb0       h1alpha0_proton0_total4PM
Create h2: 0x558f8b69a3a0       h2alpha0_proton0_total4PAM
Create h2: 0x558f8b3ef330       h2alpha0_proton0_total4AM
Create h2: 0x558f8b6a0030       h2alpha0_proton0_total4PM
Create h3: 0x558f8b528710       h3alpha0_proton0_total4PAM

 *** Break *** segmentation violation
 Generating stack trace...
 0x0000558f87cd2e8d in YMatrix::CreateHistogram(TH3I*, TString const&) + 0x19d from ./YMATRIX
 0x0000558f87cd3d23 in YMatrix::CreateHistogram() + 0xcc3 from ./YMATRIX
 0x0000558f87cd9a41 in YMatrix::Init() + 0x5a1 from ./YMATRIX
 0x0000558f87cd9ccc in YMatrix::Process() + 0x2c from ./YMATRIX
 0x0000558f87cd1d40 in main + 0x110 from ./YMATRIX
 0x00007fbb7b718d90 in <unknown> from /lib/x86_64-linux-gnu/libc.so.6
 0x00007fbb7b718e40 in __libc_start_main + 0x80 from /lib/x86_64-linux-gnu/libc.so.6
 0x0000558f87cd1e95 in _start + 0x25 from ./YMATRIX

Best Regard
Mala

Hi,

You would need to compile with debug symbols and possibly run gdb to understand where the crash is happening. What is YTH3 ? it is a typedef to a TH3I ?

Lorenzo

Thank you for your reply!

Yes, YTH3 is a typedef to a TH3I,. The purpose of using typedef is to easily change TH3 class. But I am a newbie using c++ and ROOT :hot_face:.

I don’t know what debugging method you are talking about. I just using some std::cout to mark the code that has been run to completion, therefore the sentences like Create h1: address   histogram name will be printed on the screen, as well h2 and h3.

  • In a creating histogram loop, PAM, AM, and PM, you can see this word at the end of histogram name, will be created.
  • In the print message, I can know that h1 and h2 have been created, and memory allocated. After h3…PAM allocate memory, however, the TH3::Fill(Double_t x, Double_t y, Double_t z) function does not functioned. :thinking:

Mala

Hi,
Can you please post a minimal running code causing the crash? The code you have showed is missing something to run it.

Lorenzo

In github_identifyPartical+sortTimeStamp+dopplerCorrection/matrixv1/, I uploaded all the code and a part of original data file. In matrexv1 directory, there has a file named YMatrix.cc. In the file you can see the running logic. Line 217 of file YMatrix.cc shows how to use the above function.

This is the main code structure Line 217.

// tips
// ALPHA is alpha
// PTC is particle
// MAX is maximum number
// MIN is minimum number
// shggaptname is belong to Class::TString, as well shggatname, shggptname, shggapname
// apt is alpha, proton, total
// at is alpha, total
// pt is proton, total
// ap is alpha, proton
// In another using the histogram, using TObject::FindObject(const chat* name).
for (short i = 0; i < (ALPHAPTCMAX - ALPHAPTCMIN + 1); i++)
{
        for (short j = 0; j < (PROTONPTCMAX - PROTONPTCMIN + 1); j++)
        {
            for (short k = 0; k < (TOTALPTCMAX - TOTALPTCMIN + 1); k++)
            {
                if ((i + j) > k)
                    continue;

                shggaptname = TString::Format("alpha%hd_proton%hd_total%hd", (i + ALPHAPTCMIN), (j + PROTONPTCMIN), (k + TOTALPTCMIN));
                shggatname = TString::Format("alpha%hd_total%hd", (i + ALPHAPTCMIN), (k + TOTALPTCMIN));
                shggptname = TString::Format("proton%hd_total%hd", (j + PROTONPTCMIN), (k + TOTALPTCMIN));
                shggapname = TString::Format("alpha%hd_proton%hd", (i + ALPHAPTCMIN), (j + PROTONPTCMIN));
                if (bapt[i][j][k])
                {
                    bapt[i][j][k] = false;
                    CreateHistogram(h1ggaptPAM, shggaptname + "PAM");
                    CreateHistogram(h1ggaptAM, shggaptname + "AM");
                    CreateHistogram(h1ggaptPM, shggaptname + "PM");
                    h1ggaptPM = (YTH1 *)gROOT->FindObject(("h1" + shggaptname + "PM").Data());
                    if (h1ggaptPM != nullptr)
                        h1ggaptPM->Sumw2(1);
                    else
                        cerr << "Can't find h1" << shggaptname << "PM" << endl;
                    h1ggaptPM = nullptr;
#ifdef GEGE
                    CreateHistogram(h2ggaptPAM, shggaptname + "PAM");
#ifdef GEGEAM
                    CreateHistogram(h2ggaptAM, shggaptname + "AM");
                    CreateHistogram(h2ggaptPM, shggaptname + "PM");
                    h2ggaptPM = (YTH2 *)gROOT->FindObject(("h2" + shggaptname + "PM").Data());
                    if (h2ggaptPM != nullptr)
                        h2ggaptPM->Sumw2(1);
                    else
                        cerr << "Can't find h2" << shggaptname << "PM" << endl;
                    h2ggaptPM = nullptr;
#endif
#endif
#ifdef CUBEMATRIX
                    CreateHistogram(h3ggaptPAM, shggaptname + "PAM");
#ifdef CUBEMATRIXAM
                    CreateHistogram(h3ggaptAM, shggaptname + "AM");
                    CreateHistogram(h3ggaptPM, shggaptname + "PM");
                    h3ggaptPM = (YTH3 *)gROOT->FindObject(("h3" + shggaptname + "PM").Data());
                    if (h3ggaptPM != nullptr)
                        h3ggaptPM->Sumw2(1);
                    else
                        cerr << "Can't find h3" << shggaptname << "PM" << endl;
                    h3ggaptPM = nullptr;
#endif
#endif
                }

...
        }
    }
}

By the way, this code requires make before it can run. In the .cpp file that only contains the code bool YMatrix::CreateHistogram(YTH3 *phistogram, const TString &pshname) that causes the crash, the .cpp runs fine. But when I use make to turn into an application, it runs into problems. I don’t kown why cause the condition.

Mala

It is possible for you to reduce the code to the minimal, only the part where the histogram it is created and filled? If this works fine, then likely the crash is caused by something else (e.g. memory overwritten). In this case I would recommend you to try to use valgrind

Yes, in .cpp file the code is simplified to only create TH3 and fill histogram.

Thank you for your suggestion.I should learn how to use valgrind and then check if the memory overwritten is causing the crash issue. If you have some good tutorials, that would be the best. Thank you for providing them to me. :rofl:

Mala

Each of your TH3I histograms will request/need at least (EBINNUMBER + 2) * (EBINNUMBER + 2) * (EBINNUMBER + 2) * 4 bytes of RAM (and calling Sumw2(1) will immediately double this amount).

BTW. You probably want: bool YMatrix::CreateHistogram(YTH... *&phistogram, const TString &pshname)

YMatrix.txt (408.8 KB) [Input file from valgrind ]
I have used Valgrind to check my application, and I maybe get the error of memory! I attach it below the help post, but I don’t kown how to fix it. Thanks again everyone! :smiling_face_with_three_hearts:

Warning: set address range perms: large range [0x2b2ae040, 0x432de060) (undefined)
==56621== Warning: set address range perms: large range [0x59c99040, 0x71cc9060) (undefined)
==56621== Warning: set address range perms: large range [0x71cca040, 0x89cfa060) (undefined)
==56621== Warning: set address range perms: large range [0x89cfb040, 0xa1d2b060) (undefined)
==56621== Invalid read of size 4
==56621==    at 0x54DA402: TH3I::AddBinContent(int) (in /home/ys/ROOT/root/lib/libHist.so.6.28.06)
==56621==    by 0x54DEB6B: TH3::Fill(double, double, double) (in /home/ys/ROOT/root/lib/libHist.so.6.28.06)
==56621==    by 0x10D756: YMatrix::FillHistogram(bool const&, double const&, double const&, short const&, short const&, short const&, TString const&, double const&) (YMatrix.cc:559)
==56621==    by 0x111533: YMatrix::Process() (YMatrix.cc:1037)
==56621==    by 0x10AB7F: main (main.cc:33)
==56621==  Address 0xfffffffe43851d64 is not stack'd, malloc'd or (recently) free'd

Mala

Maybe you want to recommend me to use template to create the three kinds histogram. I used it in the frist version. However, I encountered many, many error, so I abandoned this method. :hot_face:

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