*** Break *** segmentation violation , *** Break *** bus error

hi, im new to root___
ive trying out this code, a simple mu vs events, but keeps on failing with --break segmentation–

//=============================
//6oct20 audiya
//read and analyze root file
//=============================
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TVector3.h>
#include <TLorentzVector.h>
#include "TMath.h"
#include "TChain.h"
#include "TBranch.h"
#include "TFile.h"
#include "TTree.h"
#include "TH1.h"
#include "stdio.h"
#include <iostream>
#include "TClonesArray.h"
#include "TObject.h"

using namespace std;

void analyzetree () { //.C file name
   
    //load root file
   // TFile *f = new TFile("/Users/applestudio/Desktop/sample28sept/10/10.root");
     
    //get tree from file
    //TTree *t = (TTree*) f->Get("Delphes/Muon");
    
    //Get the tree (called nameoftree) from file
     TChain *fChain = new TChain("Delphes");
    
    //Load root file
    fChain->Add("/Users/applestudio/Desktop/sample28sept/10/10.root");
    

// set address to variable
    // Fixed size dimensions of array or collections stored in the TTree if any.
    static constexpr Int_t kMaxMuon = 4;
    
//declaration of leaf type
    Int_t           Muon_;
    UInt_t          Muon_fUniqueID[kMaxMuon];   //[Muon_]
    UInt_t          Muon_fBits[kMaxMuon];   //[Muon_]
    Float_t         Muon_PT[kMaxMuon];   //[Muon_]
    Float_t         Muon_Eta[kMaxMuon];   //[Muon_]
    Float_t         Muon_Phi[kMaxMuon];   //[Muon_]
    Float_t         Muon_T[kMaxMuon];   //[Muon_]
    Int_t           Muon_Charge[kMaxMuon];   //[Muon_]
    TRef            Muon_Particle[kMaxMuon];
    Float_t         Muon_IsolationVar[kMaxMuon];   //[Muon_]
    Float_t         Muon_IsolationVarRhoCorr[kMaxMuon];   //[Muon_]
    Float_t         Muon_SumPtCharged[kMaxMuon];   //[Muon_]
    Float_t         Muon_SumPtNeutral[kMaxMuon];   //[Muon_]
    Float_t         Muon_SumPtChargedPU[kMaxMuon];   //[Muon_]
    Float_t         Muon_SumPt[kMaxMuon];   //[Muon_]
    Float_t         Muon_D0[kMaxMuon];   //[Muon_]
    Float_t         Muon_DZ[kMaxMuon];   //[Muon_]
    Float_t         Muon_ErrorD0[kMaxMuon];   //[Muon_]
    Float_t         Muon_ErrorDZ[kMaxMuon];   //[Muon_]
    Int_t           Muon_size;

  // List of branches
  //------muons------
  TBranch        *b_Muon_;   //!
    TBranch        *b_Muon_fUniqueID;   //!
    TBranch        *b_Muon_fBits;   //!
    TBranch        *b_Muon_PT;   //!
    TBranch        *b_Muon_Eta;   //!
    TBranch        *b_Muon_Phi;   //!
    TBranch        *b_Muon_T;   //!
    TBranch        *b_Muon_Charge;   //!
    TBranch        *b_Muon_Particle;   //!
    TBranch        *b_Muon_IsolationVar;   //!
    TBranch        *b_Muon_IsolationVarRhoCorr;   //!
    TBranch        *b_Muon_SumPtCharged;   //!
    TBranch        *b_Muon_SumPtNeutral;   //!
    TBranch        *b_Muon_SumPtChargedPU;   //!
    TBranch        *b_Muon_SumPt;   //!
    TBranch        *b_Muon_D0;   //!
    TBranch        *b_Muon_DZ;   //!
    TBranch        *b_Muon_ErrorD0;   //!
    TBranch        *b_Muon_ErrorDZ;   //!
    TBranch        *b_Muon_size;   //!
  

//set Branch address
//------muon------
  fChain->SetBranchAddress("Muon", &Muon_, &b_Muon_);
  fChain->SetBranchAddress("Muon.fUniqueID", Muon_fUniqueID, &b_Muon_fUniqueID);
  fChain->SetBranchAddress("Muon.fBits", Muon_fBits, &b_Muon_fBits);
  fChain->SetBranchAddress("Muon.PT", Muon_PT, &b_Muon_PT);
  fChain->SetBranchAddress("Muon.Eta", Muon_Eta, &b_Muon_Eta);
  fChain->SetBranchAddress("Muon.Phi", Muon_Phi, &b_Muon_Phi);
  fChain->SetBranchAddress("Muon.T", Muon_T, &b_Muon_T);
  fChain->SetBranchAddress("Muon.Charge", Muon_Charge, &b_Muon_Charge);
  fChain->SetBranchAddress("Muon.Particle", Muon_Particle, &b_Muon_Particle);
  fChain->SetBranchAddress("Muon.IsolationVar", Muon_IsolationVar, &b_Muon_IsolationVar);
  fChain->SetBranchAddress("Muon.IsolationVarRhoCorr", Muon_IsolationVarRhoCorr, &b_Muon_IsolationVarRhoCorr);
  fChain->SetBranchAddress("Muon.SumPtCharged", Muon_SumPtCharged, &b_Muon_SumPtCharged);
  fChain->SetBranchAddress("Muon.SumPtNeutral", Muon_SumPtNeutral, &b_Muon_SumPtNeutral);
  fChain->SetBranchAddress("Muon.SumPtChargedPU", Muon_SumPtChargedPU, &b_Muon_SumPtChargedPU);
  fChain->SetBranchAddress("Muon.SumPt", Muon_SumPt, &b_Muon_SumPt);
  fChain->SetBranchAddress("Muon.D0", Muon_D0, &b_Muon_D0);
  fChain->SetBranchAddress("Muon.DZ", Muon_DZ, &b_Muon_DZ);
  fChain->SetBranchAddress("Muon.ErrorD0", Muon_ErrorD0, &b_Muon_ErrorD0);
  fChain->SetBranchAddress("Muon.ErrorDZ", Muon_ErrorDZ, &b_Muon_ErrorDZ);
  fChain->SetBranchAddress("Muon_size", &Muon_size, &b_Muon_size);


    //Declare histogram
    TH1F *muPt = new TH1F("muPt", "p_{T #mu}[GeV]", 160, 0., 800.);
    TH1F *muEta = new TH1F("muEta", "#eta_{#mu}", 160, -3, 3.);
    TH1F *muPhi = new TH1F("muPhi", "#phi_{#mu}", 100, -5., 5.);
    
    //loop over 100 event je (cuba) (klo nentries sume skli takot lama) in the tree
    //Long64_t nentries = t->GetEntries();
   // for(int i =0; i < nentries; i++)
    Int_t nentries = fChain->GetEntries();
    for (int i = 0; i < nentries ; i++)
    {
        //t->GetEntry(i);
        fChain->GetEntry(i);
        
        // Fill histogram
        muPt->Fill(Muon_PT[i]);
        muEta->Fill(Muon_Eta[i]);
        muPhi->Fill(Muon_Phi[i]);
    }
    // Dram histogram
    TCanvas *c1 = new TCanvas("c1", "muEtaPtPhi", 400, 200);
    c1->Divide(2,2);
    
    c1->cd(1);
    muPt->GetXaxis()->SetTitle("Transverse Momentum (Gev/c)");
    muPt->GetYaxis()->SetTitle("Events");
    muPt->Draw();
    
    c1->cd(2);
    muEta->GetXaxis()->SetTitle("Psedo-rapidity");
    muEta->GetYaxis()->SetTitle("Events");
    muEta->Draw();
    
    c1->cd(3);
    muPhi->GetXaxis()->SetTitle("Azimuthal angle");
    muPhi->GetYaxis()->SetTitle("Events");
    muPhi->Draw();
}

*** Break *** segmentation violation

[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[/usr/lib/system/libsystem_c.dylib] _vsnprintf (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TBranch::GetEntry(long long, int) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TBranchElement::GetEntry(long long, int) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TTree::GetEntry(long long, int) (no debug info)

[] (no debug info)

[] (no debug info)

[] (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libRint.so] TRint::Run(bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/bin/root.exe] main (no debug info)

[/usr/lib/system/libdyld.dylib] start (no debug info)

i also tried to plot others such as Z mass but failing with–break bus-- error

//==========================
//6 october 2020 audiya Zmass
//==========================

#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TVector3.h>
#include <TLorentzVector.h>
#include "TMath.h"
#include "TChain.h"
#include "TBranch.h"
#include "TFile.h"
#include "TTree.h"
#include "TH1.h"
#include "stdio.h"
#include <iostream>
#include "TClonesArray.h"
#include "TObject.h"

using namespace std;

void Zmass() {
    // load file
    TChain *fChain = new TChain("Delphes");
    
    //Load root file
    fChain->Add("/Users/applestudio/Desktop/sample28sept/10/10.root");
    
    //load root file
      // TFile *f = new TFile("/Users/applestudio/Desktop/sample28sept/10/10.root");
        
       //get tree from file
       //TTree *t = (TTree*) f->Get("Delphes/Muon");
    
    // set address to variable
        // Fixed size dimensions of array or collections stored in the TTree if any.
        static constexpr Int_t kMaxMuon = 4;
        
    //declaration of leaf type
        Int_t           Muon_;
        UInt_t          Muon_fUniqueID[kMaxMuon];   //[Muon_]
        UInt_t          Muon_fBits[kMaxMuon];   //[Muon_]
        Float_t         Muon_PT[kMaxMuon];   //[Muon_]
        Float_t         Muon_Eta[kMaxMuon];   //[Muon_]
        Float_t         Muon_Phi[kMaxMuon];   //[Muon_]
        Float_t         Muon_T[kMaxMuon];   //[Muon_]
        Int_t           Muon_Charge[kMaxMuon];   //[Muon_]
        TRef            Muon_Particle[kMaxMuon];
        Float_t         Muon_IsolationVar[kMaxMuon];   //[Muon_]
        Float_t         Muon_IsolationVarRhoCorr[kMaxMuon];   //[Muon_]
        Float_t         Muon_SumPtCharged[kMaxMuon];   //[Muon_]
        Float_t         Muon_SumPtNeutral[kMaxMuon];   //[Muon_]
        Float_t         Muon_SumPtChargedPU[kMaxMuon];   //[Muon_]
        Float_t         Muon_SumPt[kMaxMuon];   //[Muon_]
        Float_t         Muon_D0[kMaxMuon];   //[Muon_]
        Float_t         Muon_DZ[kMaxMuon];   //[Muon_]
        Float_t         Muon_ErrorD0[kMaxMuon];   //[Muon_]
        Float_t         Muon_ErrorDZ[kMaxMuon];   //[Muon_]
        Int_t           Muon_size;

      // List of branches
      //------muons------
      TBranch        *b_Muon_;   //!
        TBranch        *b_Muon_fUniqueID;   //!
        TBranch        *b_Muon_fBits;   //!
        TBranch        *b_Muon_PT;   //!
        TBranch        *b_Muon_Eta;   //!
        TBranch        *b_Muon_Phi;   //!
        TBranch        *b_Muon_T;   //!
        TBranch        *b_Muon_Charge;   //!
        TBranch        *b_Muon_Particle;   //!
        TBranch        *b_Muon_IsolationVar;   //!
        TBranch        *b_Muon_IsolationVarRhoCorr;   //!
        TBranch        *b_Muon_SumPtCharged;   //!
        TBranch        *b_Muon_SumPtNeutral;   //!
        TBranch        *b_Muon_SumPtChargedPU;   //!
        TBranch        *b_Muon_SumPt;   //!
        TBranch        *b_Muon_D0;   //!
        TBranch        *b_Muon_DZ;   //!
        TBranch        *b_Muon_ErrorD0;   //!
        TBranch        *b_Muon_ErrorDZ;   //!
        TBranch        *b_Muon_size;   //!
      

    //set Branch address
    //------muon------
      fChain->SetBranchAddress("Muon", &Muon_, &b_Muon_);
      fChain->SetBranchAddress("Muon.fUniqueID", Muon_fUniqueID, &b_Muon_fUniqueID);
      fChain->SetBranchAddress("Muon.fBits", Muon_fBits, &b_Muon_fBits);
      fChain->SetBranchAddress("Muon.PT", Muon_PT, &b_Muon_PT);
      fChain->SetBranchAddress("Muon.Eta", Muon_Eta, &b_Muon_Eta);
      fChain->SetBranchAddress("Muon.Phi", Muon_Phi, &b_Muon_Phi);
      fChain->SetBranchAddress("Muon.T", Muon_T, &b_Muon_T);
      fChain->SetBranchAddress("Muon.Charge", Muon_Charge, &b_Muon_Charge);
      fChain->SetBranchAddress("Muon.Particle", Muon_Particle, &b_Muon_Particle);
      fChain->SetBranchAddress("Muon.IsolationVar", Muon_IsolationVar, &b_Muon_IsolationVar);
      fChain->SetBranchAddress("Muon.IsolationVarRhoCorr", Muon_IsolationVarRhoCorr, &b_Muon_IsolationVarRhoCorr);
      fChain->SetBranchAddress("Muon.SumPtCharged", Muon_SumPtCharged, &b_Muon_SumPtCharged);
      fChain->SetBranchAddress("Muon.SumPtNeutral", Muon_SumPtNeutral, &b_Muon_SumPtNeutral);
      fChain->SetBranchAddress("Muon.SumPtChargedPU", Muon_SumPtChargedPU, &b_Muon_SumPtChargedPU);
      fChain->SetBranchAddress("Muon.SumPt", Muon_SumPt, &b_Muon_SumPt);
      fChain->SetBranchAddress("Muon.D0", Muon_D0, &b_Muon_D0);
      fChain->SetBranchAddress("Muon.DZ", Muon_DZ, &b_Muon_DZ);
      fChain->SetBranchAddress("Muon.ErrorD0", Muon_ErrorD0, &b_Muon_ErrorD0);
      fChain->SetBranchAddress("Muon.ErrorDZ", Muon_ErrorDZ, &b_Muon_ErrorDZ);
      fChain->SetBranchAddress("Muon_size", &Muon_size, &b_Muon_size);

    //create local variable for the branches set as default (0,0,0,0)
    
    TLorentzVector M1(0,0,0,0);
    TLorentzVector M2(0,0,0,0);
    
    //declare Histograms
    TH1F *Zmass = new TH1F("muZmass", "Zmass", 50, 80., 100.);
    
    //get enteries
    Int_t nentries = fChain->GetEntries();
    for (int i=0; i<nentries; i++)
    {
           //t->GetEntry(i);
           fChain->GetEntry(i);
        
        for (int m=0; m<Muon_ ; m++)
        {
            //aplly cuts
            if (fabs(Muon_PT[m]<20)) continue;
            if (Muon_Eta[2]<-2)continue;
            
            if (m==0) M1.SetPtEtaPhiM(Muon_PT[m],Muon_Eta[m],Muon_Phi[m], 105.7);
            if (m==1) M2.SetPtEtaPhiM(Muon_PT[m],Muon_Eta[m],Muon_Phi[m], 105.7);
            
                                      double ZM = (M1+M2).M();
                                      Zmass->Fill(ZM);
        
                
            }
        }
    
                                      Zmass->GetXaxis()->SetTitle("Mass Gev/c^2");
                                      Zmass->GetYaxis()->SetTitle("Events");
                                      Zmass->Draw();
                            
}

*** Break *** bus error

[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[/usr/lib/system/libsystem_c.dylib] _vsnprintf (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TBranch::GetEntry(long long, int) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TBranchElement::GetEntry(long long, int) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libTree.so] TTree::GetEntry(long long, int) (no debug info)

[] (no debug info)

[] (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)

[/usr/local/Cellar/root/6.22.02/lib/root/libRint.so] TRint::Run(bool) (no debug info)

[/usr/local/Cellar/root/6.22.02/bin/root.exe] main (no debug info)

[/usr/lib/system/libdyld.dylib] start (no debug info)

i assume that its related,

any suggestions on what im missing, thanks
Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.23
Platform: Mac os catalina 10.15.6
Compiler: Apple clang version 11.0.3 (clang-1103.0.32.29)


Hi @odylalalaaa ,
please check our guide on posting code on the forum and amend your post accordingly.

The error is due to a segmentation fault, which typically indicates there is a bug in your code, e.g. accessing a nullptr or similar. If you could run the code on lxplus using one of the releases with debug symbols the stacktraces might contain more useful information.

Compiling the code with debug symbols and running it through a debugger such as lldb might also help.

Also, to verify that the file can be read correctly in general: can you read and visualize all those branches via TBrowser?

Cheers,
Enrico

There may be other errors, but one that seems evident is that you declare arrays of size 4, i.e.

    static constexpr Int_t kMaxMuon = 4;
    ...
    Float_t         Muon_PT[kMaxMuon];

and then you have a loop over all the entries in fChain

    for (int i = 0; i < nentries ; i++)
    {
        //t->GetEntry(i);
        fChain->GetEntry(i);
        
        // Fill histogram
        muPt->Fill(Muon_PT[i]);
        muEta->Fill(Muon_Eta[i]);
        muPhi->Fill(Muon_Phi[i]);
    }

Unless your chain/tree only has 4 events or less, the index i will go outside those arrays, resulting in a segfault. So you need to understand what you are actually trying to access. I guess those variables don’t need to be arrays, or maybe you need to use an index independent from i, but check your tree and make sure.

1 Like

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