Crash while running a cpp file

I am trying to run the following code,

#include <iostream>
#include<fstream>
#include<string>
#include "TObject.h"
#include <cmath>
#include "TFile.h"
#include "TTree.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TF1.h"
#include "TLorentzVector.h"
#include "TGraphErrors.h"
#include <TCanvas.h>
#include <TStyle.h>

void cpfile()
{

        TFile *f = new TFile("Merged_File_Run1695831790_NANO.root");// combinedfile.root : combination of 4 root files.

        TTree *Tout = (TTree*)f->Get("Events");

        TFile *fout = new TFile("finalpfile1.root","RECREATE",0);
        auto newtree = std::make_unique<TTree>("Events", "Events");

        const int N_HGC = 454;


        UInt_t          run;
   UInt_t          luminosityBlock;
   ULong64_t       event;
   UInt_t          bunchCrossing;
   Int_t           nHGCCM;
   Int_t           nHGC;
   //UShort_t        HGC_adc[N_HGC];
   UShort_t        HGC_adc[N_HGC];
   Float_t        HGC_adcFlt[N_HGC];
   Float_t         HGC_x[N_HGC];   //[nHGC]
   Float_t         HGC_y[N_HGC];
   Float_t         HGC_energy[N_HGC];
   //UChar_t         HGCCM_captureBlock;


        // List of branches
        TBranch        *b_run;   //!
   TBranch        *b_luminosityBlock;   //!
   TBranch        *b_event;   //!
   TBranch        *b_bunchCrossing;   //!
   TBranch        *b_nHGCCM;
   TBranch        *b_nHGC;
   TBranch        *b_HGC_adc;
   TBranch        *b_HGC_x;   //!
   TBranch        *b_HGC_y;
   TBranch        *b_HGC_energy;
   //TBranch        *b_HGCCM_captureBlock;








   Tout->SetBranchAddress("run", &run, &b_run);
   Tout->SetBranchAddress("luminosityBlock", &luminosityBlock, &b_luminosityBlock);
   Tout->SetBranchAddress("event", &event, &b_event);
   Tout->SetBranchAddress("bunchCrossing", &bunchCrossing, &b_bunchCrossing);
   Tout->SetBranchAddress("nHGCCM", &nHGCCM, &b_nHGCCM);
   Tout->SetBranchAddress("nHGC", &nHGC, &b_nHGC);
   Tout->SetBranchAddress("HGC_adc", HGC_adc, &b_HGC_adc);
   Tout->SetBranchAddress("HGC_x", HGC_x, &b_HGC_x);
   Tout->SetBranchAddress("HGC_y", HGC_y, &b_HGC_y);
   Tout->SetBranchAddress("HGC_energy", HGC_energy, &b_HGC_energy);
   //Tout->SetBranchAddress("HGCCM_captureBlock", &HGCCM_captureBlock, &b_HGCCM_captureBlock);


    newtree->Branch("run", &run);
    newtree->Branch("luminosityBlock", &luminosityBlock);
    newtree->Branch("event", &event);
    newtree->Branch("bunchCrossing", &bunchCrossing);
    newtree->Branch("nHGCCM", &nHGCCM);
    newtree->Branch("nHGC", &nHGC);
    newtree->Branch("HGC_adc", HGC_adcFlt,"HGC_adc[nHGC]");
    newtree->Branch("HGC_x", HGC_x,"HGC_x[nHGC]");
    newtree->Branch("HGC_y", HGC_y,"HGC_Y[nHGC]");
    newtree->Branch("HGC_energy", HGC_energy , "HGC_energy[nHGC]");
    //newtree->Branch("HGCCM_captureBlock",&HGCCM_captureBlock);



   int nevt;
        nevt=Tout->GetEntries();

        for (int i = 0; i < nevt; i++)
        {
                Tout->GetEntry(i);
                for (int j = 0; j < N_HGC; j++){
                    HGC_adcFlt[j] = float(HGC_adc[j]);
                }
                newtree->Fill();
    }

    fout->cd();
    newtree->Write();


   fout->Write();
   fout->Close();
}

int main(){
    cpfile();
    }

I compiled it using

(py38-env) :Old_Method_Files$ g++ -o cpf cpfile.cpp -pthread -std=c++17 -m64 -I/home/cms-iith/miniconda3/envs/py38-env/include `root-config --libs` `root-config --cflags`

And tried to run it using,

(py38-env) :Old_Method_Files$ ./cpf
Warning in <TClass::Init>: no dictionary for class edm::Hash<1> is available
Warning in <TClass::Init>: no dictionary for class edm::ProcessHistory is available
Warning in <TClass::Init>: no dictionary for class edm::ProcessConfiguration is available
Warning in <TClass::Init>: no dictionary for class edm::ParameterSetBlob is available
Warning in <TClass::Init>: no dictionary for class pair<edm::Hash<1>,edm::ParameterSetBlob> is available

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000743da11107a7 in __GI___wait4 (pid=3691756, stat_loc=stat_loc
entry=0x7ffd5fc70dd8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1  0x0000743da11108eb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffd5fc70dd8, options=options
entry=0) at ./posix/waitpid.c:38
#2  0x0000743da10585ab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x0000743da2cb1d6d in TUnixSystem::StackTrace() () from /home/cms-iith/miniconda3/envs/py38-env/lib/libCore.so.6.26
#4  0x0000743da2caf077 in TUnixSystem::DispatchSignals(ESignals) () from /home/cms-iith/miniconda3/envs/py38-env/lib/libCore.so.6.26
#5  <signal handler called>
#6  0x0000625920e0feda in std::default_delete<TTree>::operator()(TTree*) const ()
#7  0x0000625920e0f9b8 in std::unique_ptr<TTree, std::default_delete<TTree> >::~unique_ptr() ()
#8  0x0000625920e0f5d2 in cpfile() ()
#9  0x0000625920e0f62f 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.
===========================================================
#6  0x0000625920e0feda in std::default_delete<TTree>::operator()(TTree*) const ()
#7  0x0000625920e0f9b8 in std::unique_ptr<TTree, std::default_delete<TTree> >::~unique_ptr() ()
#8  0x0000625920e0f5d2 in cpfile() ()
#9  0x0000625920e0f62f in main ()
===========================================================

Is there something wrong with the way I am compiling it?
The codes are correct and have worked using the same commands, for the one who wrote these codes. However, I am unable to do to the same.

Tried to run it in debug mode as well,

(py38-env) :Old_Method_Files$ gdb ./cpf
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./cpf...
(No debugging symbols found in ./cpf)
(gdb) run
Starting program: /home/cms-iith/HGCAL_Calibration/Pratyush_Codes/Old_Method_Files/cpf

This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
Downloading separate debug info for system-supplied DSO at 0x7ffff7fc3000
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Detaching after vfork from child process 3693902]
[Detaching after vfork from child process 3693904]
[Detaching after vfork from child process 3693906]
[Detaching after vfork from child process 3693908]
[Detaching after vfork from child process 3693922]
[Detaching after vfork from child process 3693930]
[Detaching after vfork from child process 3693932]
Warning in <TClass::Init>: no dictionary for class edm::Hash<1> is available
Warning in <TClass::Init>: no dictionary for class edm::ProcessHistory is available
Warning in <TClass::Init>: no dictionary for class edm::ProcessConfiguration is available
Warning in <TClass::Init>: no dictionary for class edm::ParameterSetBlob is available
Warning in <TClass::Init>: no dictionary for class pair<edm::Hash<1>,edm::ParameterSetBlob> is available
[Detaching after vfork from child process 3693934]

Program received signal SIGSEGV, Segmentation fault.
0x0000555555558eda in std::default_delete<TTree>::operator()(TTree*) const ()
(gdb) backtrace
#0  0x0000555555558eda in std::default_delete<TTree>::operator()(TTree*) const ()
#1  0x00005555555589b8 in std::unique_ptr<TTree, std::default_delete<TTree> >::~unique_ptr() ()
#2  0x00005555555585d2 in cpfile() ()
#3  0x000055555555862f in main ()
(gdb)

Hello @Swasti,

for compiling you code, you can simply run
g++ -o cpf cpfile.cpp `root-config --libs --cflags`
as compilation flags and include paths of ROOT are automatically passed.

It’s a bit difficult to find out what’s happening here. Can you dig and figure out which instruction (code line) of your code is triggering the segmentation fault? You could e.g. use a debugger or just comment out some regions of your code.

Cheers,
Monica

1 Like

Dear @Swasti ,

I strongly suggest you follow @mdessole instructions which represent a good way to debug your code in general and make sure the real culprit of the issue is found.

On top of that, by quickly skimming through your code, I see that you are mixing some patterns and you are not following all the proper C++ guidelines. In the line where you create the new TTree you are making use of the make_unique functionality, which is a good more-modern approach. Unfortunately there are many other places in your code where you use the new operator without deleting the corresponding object, this should be avoided at all costs. Make sure you either declare your variable on the stack or you only create std::unique_ptr to wrap your heap-allocated objects. After that, I also see you are calling fout->Close at the end of your script, which is redundant and error-prone in this case (I suspect this is potentially leading to a double-delete of your new TTree which may be the cause of the segfault). Try to clean your code with these suggestions and rerun.

Cheers,
Vincenzo

1 Like

This helped!

These are the changes that I made!

auto f = std::make_unique<TFile>("Merged_File_Run1695831790_NANO.root");
...
.
.
auto fout = std::make_unique<TFile>("finalpfile1.root", "RECREATE", 0);

.
.
.
removed fout->Close();

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