Segmentation fault when cloning tree and filling it

Hi! I am trying to shorten a large .root file by cloning its structure (with only some branches) and writing to it only the events for which a parameter numRecCandidates is >=1. It reads the structure well, then reaches the event passing the requirement, and then I get Segmentation fault error. The same problem was present when I tried to clone the entire tree. I would be very grateful if you could help me with this problem.

This is my code (excuse me, it contains some irrelevant pieces, but not that much):

import ROOT as r
import argparse

r.PyConfig.IgnoreCommandLineOptions = True

def analysis():
    parser = argparse.ArgumentParser(description='Script to makeAnalysis')
    parser.add_argument('-g', '--geofile', help='Geometry file to use.')
    parser.add_argument('-f', '--inputFile', help='Input file to use.')
    parser.add_argument('-F', '--inputFile2', help='Input file 2 to use.')
    parser.add_argument('-o', '--outputfile', default='muonDISstudy.root')
    parser.add_argument('-J', '--JobNum', type=int, help='Number of job.')
    parser.add_argument('-M', '--MuonsPerJob', type=int, help='MuonsPerJob.')
    parser.add_argument('-nDISPerMuon', '--nDIS', type=int, default=10000, help='Number of DIS per muon to generate')
    parser.add_argument('-scriptdir', '--scrdir', help='Script directory')

    args = parser.parse_args()

    # Open the input file
    f = r.TFile.Open(args.inputFile, 'READ')
    t1 = f.Get("cbmsim")
    if not t1:
        print("Error: Tree 'cbmsim' not found in the input file")
        return

    f2 = open(args.inputFile2)
    sigmadata = f2.readlines()

    # Open the output file
    o = r.TFile.Open(args.outputfile, 'RECREATE')
    t2 = t1.CloneTree(0)

    # Disable all branches and enable only the necessary ones
    t1.SetBranchStatus('*', 0)
    branches_to_keep = ['MCTrack', 'Particles']
    for branch in branches_to_keep:
        t1.SetBranchStatus(branch, 1)

    cross_sections_file = open(f'cross-sections_{args.JobNum}.txt', 'w')

    nDISperMuon = args.nDIS
    muonsPerJob = args.MuonsPerJob
    ev = 0

    for i in range(t1.GetEntries()):
        t1.GetEntry(i)
        evMuonNum = int(ev // nDISperMuon)
        evLastSimMuonNum = evMuonNum * nDISperMuon + nDISperMuon - 1
        TrueEvMuonNum = int(args.JobNum * muonsPerJob + evMuonNum)
        cross = float(sigmadata[evLastSimMuonNum])
        ev += 1

        numRecCandidates = len(t1.Particles)
        if numRecCandidates >= 1:
            cross_sections_file.write(f'{cross}\n')
            t2.Fill()

    cross_sections_file.close()
    o.cd()
    t2.Write()
    o.Close()
    f.Close()

if __name__ == '__main__':
    analysis()

This is the error:




===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f49148d89fa in wait4 () from /lib64/libc.so.6
#1  0x00007f491484b243 in do_system () from /lib64/libc.so.6
#2  0x00007f49142f939c in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x56432d2aa040) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:2104
#3  TUnixSystem::StackTrace (this=0x56432d2aa040) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:2395
#4  0x00007f4914a3cee3 in (anonymous namespace)::do_trace (sig=1) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/clingwrapper.cxx:182
#5  (anonymous namespace)::TExceptionHandlerImp::HandleException (this=<optimized out>, sig=1) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/clingwrapper.cxx:195
#6  0x00007f49142f6911 in TUnixSystem::DispatchSignals (this=0x56432d2aa040, sig=kSigSegmentationViolation) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:3610
#7  <signal handler called>
#8  0x00007f49142934cf in std::__atomic_base<TVirtualStreamerInfo*>::load (__m=std::memory_order_seq_cst, this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:820
#9  std::__atomic_base<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:694
#10 std::atomic<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/atomic:433
#11 TClass::GetStreamerInfo (this=0x0, version=version
entry=0, isTransient=isTransient
entry=false) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/meta/src/TClass.cxx:4587
#12 0x00007f4912ede374 in TBufferIO::ForceWriteInfoClones (this=0x56433cc91120, a=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/io/io/src/TBufferIO.cxx:343
#13 0x00007f491424961c in TClonesArray::Streamer (this=0x564333d73670, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/cont/src/TClonesArray.cxx:857
#14 0x00007f48f9c58844 in TBranch::FillLeavesImpl (this=0x564333d9db30, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:2438
#15 0x00007f48f9c5eb7e in TBranch::FillImpl (imtHelper=0x0, this=0x564333d9db30) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:893
#16 TBranch::FillImpl (this=0x564333d9db30, imtHelper=0x0) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:856
#17 0x00007f48f9cd8f48 in TTree::Fill (this=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TTree.cxx:4630
#18 0x00007f490b1f8024 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================


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.
===========================================================
#8  0x00007f49142934cf in std::__atomic_base<TVirtualStreamerInfo*>::load (__m=std::memory_order_seq_cst, this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:820
#9  std::__atomic_base<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:694
#10 std::atomic<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/atomic:433
#11 TClass::GetStreamerInfo (this=0x0, version=version
entry=0, isTransient=isTransient
entry=false) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/meta/src/TClass.cxx:4587
#12 0x00007f4912ede374 in TBufferIO::ForceWriteInfoClones (this=0x56433cc91120, a=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/io/io/src/TBufferIO.cxx:343
#13 0x00007f491424961c in TClonesArray::Streamer (this=0x564333d73670, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/cont/src/TClonesArray.cxx:857
#14 0x00007f48f9c58844 in TBranch::FillLeavesImpl (this=0x564333d9db30, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:2438
#15 0x00007f48f9c5eb7e in TBranch::FillImpl (imtHelper=0x0, this=0x564333d9db30) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:893
#16 TBranch::FillImpl (this=0x564333d9db30, imtHelper=0x0) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:856
#17 0x00007f48f9cd8f48 in TTree::Fill (this=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TTree.cxx:4630
#18 0x00007f490b1f8024 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================


 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f49148d89fa in wait4 () from /lib64/libc.so.6
#1  0x00007f491484b243 in do_system () from /lib64/libc.so.6
#2  0x00007f49142f939c in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x56432d2aa040) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:2104
#3  TUnixSystem::StackTrace (this=0x56432d2aa040) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:2395
#4  0x00007f4914a3cd63 in (anonymous namespace)::do_trace (sig=1) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/clingwrapper.cxx:182
#5  (anonymous namespace)::TExceptionHandlerImp::HandleException (this=<optimized out>, sig=1) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/clingwrapper.cxx:201
#6  0x00007f49142f6911 in TUnixSystem::DispatchSignals (this=0x56432d2aa040, sig=kSigSegmentationViolation) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/unix/src/TUnixSystem.cxx:3610
#7  <signal handler called>
#8  0x00007f49142934cf in std::__atomic_base<TVirtualStreamerInfo*>::load (__m=std::memory_order_seq_cst, this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:820
#9  std::__atomic_base<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:694
#10 std::atomic<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/atomic:433
#11 TClass::GetStreamerInfo (this=0x0, version=version
entry=0, isTransient=isTransient
entry=false) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/meta/src/TClass.cxx:4587
#12 0x00007f4912ede374 in TBufferIO::ForceWriteInfoClones (this=0x56433cc91120, a=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/io/io/src/TBufferIO.cxx:343
#13 0x00007f491424961c in TClonesArray::Streamer (this=0x564333d73670, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/cont/src/TClonesArray.cxx:857
#14 0x00007f48f9c58844 in TBranch::FillLeavesImpl (this=0x564333d9db30, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:2438
#15 0x00007f48f9c5eb7e in TBranch::FillImpl (imtHelper=0x0, this=0x564333d9db30) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:893
#16 TBranch::FillImpl (this=0x564333d9db30, imtHelper=0x0) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:856
#17 0x00007f48f9cd8f48 in TTree::Fill (this=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TTree.cxx:4630
#18 0x00007f490b1f8024 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================


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.
===========================================================
#8  0x00007f49142934cf in std::__atomic_base<TVirtualStreamerInfo*>::load (__m=std::memory_order_seq_cst, this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:820
#9  std::__atomic_base<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/bits/atomic_base.h:694
#10 std::atomic<TVirtualStreamerInfo*>::operator TVirtualStreamerInfo* (this=0x1d8) at /usr/include/c++/11/atomic:433
#11 TClass::GetStreamerInfo (this=0x0, version=version
entry=0, isTransient=isTransient
entry=false) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/meta/src/TClass.cxx:4587
#12 0x00007f4912ede374 in TBufferIO::ForceWriteInfoClones (this=0x56433cc91120, a=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/io/io/src/TBufferIO.cxx:343
#13 0x00007f491424961c in TClonesArray::Streamer (this=0x564333d73670, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/core/cont/src/TClonesArray.cxx:857
#14 0x00007f48f9c58844 in TBranch::FillLeavesImpl (this=0x564333d9db30, b=...) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:2438
#15 0x00007f48f9c5eb7e in TBranch::FillImpl (imtHelper=0x0, this=0x564333d9db30) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:893
#16 TBranch::FillImpl (this=0x564333d9db30, imtHelper=0x0) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TBranch.cxx:856
#17 0x00007f48f9cd8f48 in TTree::Fill (this=<optimized out>) at /cvmfs/ship.cern.ch/24.06/sw/SOURCES/ROOT/v6-26-16/v6-26-16/tree/tree/src/TTree.cxx:4630
#18 0x00007f490b1f8024 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================

For the given events, I may access these branches and extract the information from them. Could you please tell me what can be a reason?

Try with [quote=“Maksym_Ovchynnikov, post:1, topic:59751”]
branches_to_keep = ['MCTrack.*', 'Particles.*']
[/quote]
to also select the sub-branches.

Thanks! It works now, but some branches cannot be recorded. Say, I set

    branches_to_keep = ['MCTrack.*', 'Particles.*',"Digi_SBTHits.*","digiSBT2MC.*","strawtubesPoint.*","UpstreamTaggerPoint.*","FitTracks.*","goodTracks.*"]

Then, it returns

Error in <TTree::SetBranchStatus>: No branch name is matching wildcard -> Particles.*
Error in <TTree::SetBranchStatus>: No branch name is matching wildcard -> Digi_SBTHits.*
Error in <TTree::SetBranchStatus>: No branch name is matching wildcard -> digiSBT2MC.*
Error in <TTree::SetBranchStatus>: No branch name is matching wildcard -> FitTracks.*
Error in <TTree::SetBranchStatus>: No branch name is matching wildcard -> goodTracks.*

I.e., it finds some of the branches and cannot find the other. All of them seem to be present:

Tree structure and branches:
******************************************************************************
*Chain   :cbmsim    : ship.conical.muonDIS-TGeant4_1_rec.root                *
******************************************************************************
******************************************************************************
*Tree    :cbmsim    : /cbmroot_0                                             *
*Entries :     1570 : Total =      1248670374 bytes  File  Size =  524146856 *
*        :          : Tree compression factor =   2.38                       *
*Br    0 :MCTrack   : Int_t cbmroot_0.Stack.MCTrack_                         *
*Entries :     1570 : Total  Size=      30299 bytes  File Size  =       8441 *
*Baskets :       17 : Basket Size=      32000 bytes  Compression=   1.66     *
*............................................................................*
*Br    1 :MCTrack.fUniqueID : UInt_t fUniqueID[cbmroot_0.Stack.MCTrack_]     *
*Entries :     1570 : Total  Size=    5262719 bytes  File Size  =      32281 *
*Baskets :       27 : Basket Size=     579072 bytes  Compression= 163.00     *
*............................................................................*
...
*............................................................................*
*Br  215 :Digi_SBTHits : TClonesArray                                        *
*Entries :     1570 : Total  Size=    5053952 bytes  File Size  =    2010670 *
*Baskets :      167 : Basket Size=      32000 bytes  Compression=   2.51     *
*............................................................................*
*Br  217 :digiSBT2MC : vector<vector<int> >                                  *
*Entries :     1570 : Total  Size=   31614117 bytes  File Size  =   13338291 *
*Baskets :      768 : Basket Size=      32000 bytes  Compression=   2.37     *
*............................................................................*
*Br   86 :strawtubesPoint : Int_t cbmroot_0.strawtubes.strawtubesPoint_      *
*Entries :     1570 : Total  Size=      31706 bytes  File Size  =       7271 *
*Baskets :       17 : Basket Size=      32000 bytes  Compression=   1.94     *
*............................................................................*
*Br   87 :strawtubesPoint.fUniqueID :                                        *
*         | UInt_t fUniqueID[cbmroot_0.strawtubes.strawtubesPoint_]          *
*Entries :     1570 : Total  Size=     390226 bytes  File Size  =       8113 *
*Baskets :       22 : Basket Size=      29696 bytes  Compression=  47.97     *
*............................................................................*
...
*............................................................................*
*Br  156 :UpstreamTaggerPoint.fUniqueID :                                    *
*         | UInt_t fUniqueID[cbmroot_0.UpstreamTagger.UpstreamTaggerPoint_]  *
*Entries :     1570 : Total  Size=      11332 bytes  File Size  =       2395 *
*Baskets :       17 : Basket Size=       9680 bytes  Compression=   4.32     *
*............................................................................*
...
*............................................................................*
*Br  211 :FitTracks : TClonesArray                                           *
*Entries :     1570 : Total  Size=    3852140 bytes  File Size  =    1212271 *
*Baskets :      116 : Basket Size=      32000 bytes  Compression=   3.18     *
*............................................................................*
*Br  212 :goodTracks : vector<int>                                           *
*Entries :     1570 : Total  Size=      24660 bytes  File Size  =       4302 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   5.61     *
*............................................................................*
*Br  222 :Particles : TClonesArray                                           *
*Entries :     1570 : Total  Size=     138608 bytes  File Size  =      18270 *
*Baskets :        5 : Basket Size=      32000 bytes  Compression=   7.56     *

Could you please tell me how to deal with this? It looks like the problem is with the branches for which there are no . sub-branches. However, If I replace, say, Particles.* with just Particles, I immediately get the segmentation fault error. Also, it is present only if numRecCandidates >=1 (otherwise, it is a kind of empty):

for candidate in event.Particles:
		#define 3,4 Momentum Vector,vtarget
                RecVTX = r.TVector3()
                momentumRecParticle=r.TLorentzVector()
                vtarget=r.TVector3(0,0,ShipGeo.target.z0)
                #print("z of target = ",ShipGeo.target.z0)
                indexCandidate=event.Particles.index(candidate)

Sorry, I have a very limited knowledge in root.

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