Segmentation violation occurred when using SetPtEtaPhiM

Hi experts,

I am new to ROOT and I just encountered a cryptic segmentation violation when I use SetPtEtaPhiM to assign values from TTree to a Lorentz vector of class TLorentzVector. I have trimmed down my code to only include the problematic command. Below are the source code and the corresponding output.

#define ZSelection_cxx
#include "ZSelection.h"
#include "GoodLumiBlock.h"
#include <TStyle.h>
#include <TCanvas.h>
#include <vector>
#include <TLorentzVector.h>
#include <TH1.h>
#include <TH2.h>
#include "TMath.h"
#include <iostream>

static int doMC = 0;

void ZSelection::Loop()
{

   if (fChain == 0) return;cout<<__LINE__<<endl;

   fChain->LoadTree(0);cout<<__LINE__<<endl;

   TFile *WPreSelection = new TFile("ZSelection.root","recreate");cout<<__LINE__<<endl;
   TTree *Preselection_Tree = (TTree *)fChain->GetTree()->CloneTree(0);cout<<__LINE__<<endl;

   Long64_t nentries = fChain->GetEntries();cout<<__LINE__<<endl;
   printf("\nTotalEntries=%d\n\n",(int)nentries);cout<<__LINE__<<endl;

   for (Long64_t jentry=0; jentry<nentries;jentry++) {

      fChain->GetEntry(jentry);cout<<__LINE__<<endl;

      TLorentzVector L_mu;cout<<__LINE__<<endl;
      float m_mass = 105.66;cout<<__LINE__<<endl;
      L_mu.SetPtEtaPhiM((*mu_staco_pt)[1],(*mu_staco_eta)[1],(*mu_staco_phi) [1],m_mass);cout<<__LINE__<<endl;  // This is line 49.

      }
}
Processing run_WWAnalysis.C(2,"input_list.txt")...
Info in <TUnixSystem::ACLiC>: creating shared library /tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection_C.so
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.h:14233: warning: unused parameter 'entry'
/tmp/hccheng/mytest/splitted_list0002-1315215933/./GoodLumiBlock.h:6: warning: unused parameter 'bcid'
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.C: In member function 'virtual void ZSelection::Loop()':
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.C:29: warning: unused variable 'WPreSelection'
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.C:30: warning: unused variable 'Preselection_Tree'
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.C: At global scope:
/tmp/hccheng/mytest/splitted_list0002-1315215933/./ZSelection.C:14: warning: 'doMC' defined but not used
Warning: Automatic variable PrintLevel is allocated run_WWAnalysis.C:60:
20
Warning in <TClass::TClass>: no dictionary for class AttributeListLayout is available
Warning in <TClass::TClass>: no dictionary for class pair<string,string> is available
27
29
30
37

TotalEntries=40982

38
46
48
49

 *** Break *** segmentation violation
 Generating stack trace...
 0x00002ae42c78732d in Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) + 0x6d from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c8384d0 in G__execute_call + 0x60 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c83a1ea in G__call_cppfunc + 0x28a from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c8133b7 in G__interpret_func + 0x15d7 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c802c22 in G__getfunction + 0x2072 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c8ed8c3 in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) + 0x683 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c8e3285 in G__getvariable + 0x9d5 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c7d4d8c in G__getitem + 0x9c from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c7e02e0 in G__getexpr + 0xa310 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c86754c in G__exec_statement + 0x79cc from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c8146ef in G__interpret_func + 0x290f from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c802d8e in G__getfunction + 0x21de from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c7d4ef4 in G__getitem + 0x204 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c7e02e0 in G__getexpr + 0xa310 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c7e799d in G__calc_internal + 0x2dd from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42c87595b in G__process_cmd + 0x409b from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCint.so
 0x00002ae42bfc51fd in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x4ed from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCore.so
 0x00002ae42bfc4ce3 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0xb3 from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCore.so
 0x00002ae42bf2bfca in TApplication::ExecuteFile(char const*, int*, bool) + 0x59a from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCore.so
 0x00002ae42bf2953b in TApplication::ProcessLine(char const*, bool, int*) + 0x71b from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libCore.so
 0x00002ae42d4f61ab in TRint::Run(bool) + 0x19b from /afs/atlas.umich.edu/opt/root_v5.28.00a/lib/libRint.so
 0x000000000040100d in main + 0x4d from /afs/atlas.umich.edu/opt/root_v5.28.00a/bin/root.exe
 0x000000361961d994 in __libc_start_main + 0xf4 from /lib64/libc.so.6
 0x0000000000400ed9 in __gxx_personality_v0 + 0x69 from /afs/atlas.umich.edu/opt/root_v5.28.00a/bin/root.exe
Function run_WWAnalysis() busy flag cleared

I have spent a couple days of trial and error as well as looking for possible solution on the internet but I still cannot fix it. I would really appreciate it if any expert can give me a helping hand. Thank you so much in advance.

Problem solved. Some of the events contain zero muon and that’s what caused the seg fault as the mu_staco_XX[1] do not exist in the tree in those cases. Thank you anyway.