Error in <TRint::HandleTermInput()>: std::length_error caught: vector::_M_default_append

ROOT Version: 6.24/06
Platform: CentOS Stream
Compiler: GCC

Hi, everyone,

I am a newbie of ROOT.
Recently, when I read my own data, I encountered the following problem

root [1] .L Ntuple6.C
root [2] Ntuple6 t
(Ntuple6 &) @0x7fbf6da2f020
root [3] t.Write()
************---------open*****
Warning in <TBasket::ReadBasketBuffers>: basket:Pz has fNevBuf=3 but fEntryOffset=0, pos=206960, len=810, fNbytes=810, fObjlen=738, trying to repair
Error in <TRint::HandleTermInput()>: std::length_error caught: vector::_M_default_append
root [4]

In my data file there are several Ntuples with the same structure. But this problem doesn’t always arise. In the same file, reading one of the Ntuple, the result is ok, while reading the other one gives an error. for example,

root [1] .L Ntuple1.C
root [2] Ntuple1 t
(Ntuple1 &) @0x3d80800
root [3] t.Write()
************---------open*****
************---------complete*****
************---------close*****
root [4]

I will be very glad if someone answers this question, thanks !!!
(Since new user cannot add links, I’m sorry I can’t upload the data files and source files so that you can reproduce my problem)

First, welcome to the ROOT Forum! Then, it would be more useful to have some code we can run to reproduce the issue, but maybe @pcanal can give some precisions about that particular error message

my *.C and *.h file is generated by command MakeClass();
I added a read and write function (Write()) in it. I have tried using the MakeSelector method to generate the header and source files, unfortunately the error still appears when reading the Ntuple6.

this is my code.
I’m sorry I can’t provide the ROOT-data file as newbies can’t upload files


#ifndef Ntuple6_h
#define Ntuple6_h

#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
//Standard c++ header
#include <fstream>
#include <iostream>
using namespace std;
// Header file for the classes stored in the TTree if any.
#include "vector"

class Ntuple6 {
public :
   TTree          *fChain;   //!pointer to the analyzed TTree or TChain
   Int_t           fCurrent; //!current Tree number in a TChain
 fstream file1;
// Fixed size dimensions of array or collections stored in the TTree if any.

   // Declaration of leaf types
   Int_t           EventID;
   vector<double>  *Px;
   vector<double>  *Py;
   vector<double>  *Pz;

   // List of branches
   TBranch        *b_EventID;   //!
   TBranch        *b_Px;   //!
   TBranch        *b_Py;   //!
   TBranch        *b_Pz;   //!

   Ntuple6(TTree *tree=0);
   virtual ~Ntuple6();
   virtual Int_t    Cut(Long64_t entry);
   virtual Int_t    GetEntry(Long64_t entry);
   virtual Long64_t LoadTree(Long64_t entry);
   virtual void     Init(TTree *tree);
   virtual void     Loop();
   virtual Bool_t   Notify();
   virtual void     Show(Long64_t entry = -1);
   //
   virtual Long64_t GetEntries();
   virtual void     Write();
};

#endif

#ifdef Ntuple6_cxx
Ntuple6::Ntuple6(TTree *tree) : fChain(0) 
{
// if parameter tree is not specified (or zero), connect the file
// used to generate this class and read the Tree.
   if (tree == 0) {
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("GGCM08.root");
      if (!f || !f->IsOpen()) {
         f = new TFile("GGCM08.root");
      }
      f->GetObject("Ntuple6",tree);

   }
   Init(tree);
}

Ntuple6::~Ntuple6()
{
   if (!fChain) return;
   delete fChain->GetCurrentFile();
}

Int_t Ntuple6::GetEntry(Long64_t entry)
{
// Read contents of entry.
   if (!fChain) return 0;
   return fChain->GetEntry(entry);
}
Long64_t Ntuple6::LoadTree(Long64_t entry)
{
// Set the environment to read one entry
   if (!fChain) return -5;
   Long64_t centry = fChain->LoadTree(entry);
   if (centry < 0) return centry;
   if (fChain->GetTreeNumber() != fCurrent) {
      fCurrent = fChain->GetTreeNumber();
      Notify();
   }
   return centry;
}

//
Long64_t Ntuple6::GetEntries()
{
   Long64_t Nb;
   if (!fChain) return 0;
   Nb = fChain->GetEntriesFast();
   return Nb;
}
void Ntuple6::Init(TTree *tree)
{
   // The Init() function is called when the selector needs to initialize
   // a new tree or chain. Typically here the branch addresses and branch
   // pointers of the tree will be set.
   // It is normally not necessary to make changes to the generated
   // code, but the routine can be extended by the user if needed.
   // Init() will be called many times when running on PROOF
   // (once per file to be processed).

   // Set object pointer
   Px = 0;
   Py = 0;
   Pz = 0;
   // Set branch addresses and branch pointers
   if (!tree) return;
   fChain = tree;
   fCurrent = -1;
   fChain->SetMakeClass(1);

   fChain->SetBranchAddress("EventID", &EventID, &b_EventID);
   fChain->SetBranchAddress("Px", &Px, &b_Px);
   fChain->SetBranchAddress("Py", &Py, &b_Py);
   fChain->SetBranchAddress("Pz", &Pz, &b_Pz);
   Notify();
}

void Ntuple6::Write()
{
   //write content of entry to .txt file.
   // If entry is not specified, write current entry
   if (!fChain) return;
       // 
   file1.open("Ntuple6.txt");
   cout<<"************---------open*****"<<endl;
   Long64_t nentries = fChain->GetEntriesFast();

   file1<<"EventID"<<"\t"<<"Px"<<"\t"<<"Py"<<"\t"<<"Pz"<<"\t"<<"Rxy"<<"\t"<<"x"<<"\t"<<"y"<<"\t"<<"z"<<"\t"<<"Rxyz"<<"\t"<<"Theta"<<"\t"<<"Phi"<<"\t"<<"LengthOfTrajectory"<<endl;
   for (Long64_t jentry=0; jentry<nentries;jentry++) 
    {
      fChain->GetEntry(jentry);
      file1<<EventID<<endl;
   double Length = 0;
      Int_t NbofPoint = Px->size();
      for(Int_t iPoint = 0; iPoint<NbofPoint;iPoint++)
        {

            if(iPoint==0)
               {
              double x,y,z;
              x = Px->at(iPoint);
              y = Py->at(iPoint);
              z = Pz->at(iPoint);
              file1<<"    "<<iPoint<<"\t"<<x<<"\t"<<y<<"\t"<<z<<endl;
               }
            else
               {
              double x0,y0,z0,x1,y1,z1,Rxy,x,y,z,Rxyz,Theta,Phi;
              x0 = Px->at(iPoint-1);
              y0 = Py->at(iPoint-1);
              z0 = Pz->at(iPoint-1);
              x1 = Px->at(iPoint);
              y1 = Py->at(iPoint);
              z1 = Pz->at(iPoint);
              Rxy = std::sqrt(x1*x1+y1*y1);
              x = x1-x0;
              y = y1-y0;
              z = z1-z0;
              Rxyz = std::sqrt(x*x+y*y+z*z);
              Length+=Rxyz;
              Theta = std::acos(z/Rxyz);
              Phi = std::atan(y/x);
              file1<<"    "<<iPoint<<"\t"<<x1<<"\t"<<y1<<"\t"<<z1<<"\t"<<Rxy<<"\t"<<x<<"\t"<<y<<"\t"<<z<<"\t"<<Rxyz<<"\t"<<Theta<<"\t"<<Phi;
              if(iPoint==(NbofPoint-1))  { file1<<"\t"<<Length<<endl; }
                else{file1<<endl;}
               }
        }
    }
   cout<<"************---------complete*****"<<endl;
   file1.close();
   cout<<"************---------close*****"<<endl;
}
Bool_t Ntuple6::Notify()
{
   // The Notify() function is called when a new file is opened. This
   // can be either for a new TTree in a TChain or when when a new TTree
   // is started when using PROOF. It is normally not necessary to make changes
   // to the generated code, but the routine can be extended by the
   // user if needed. The return value is currently not used.

   return kTRUE;
}

void Ntuple6::Show(Long64_t entry)
{
// Print contents of entry.
// If entry is not specified, print current entry
   if (!fChain) return;
   fChain->Show(entry);
}
Int_t Ntuple6::Cut(Long64_t entry)
{
// This function may be called from Loop.
// returns  1 if entry is accepted.
// returns -1 otherwise.
   return 1;
}
#endif // #ifdef Ntuple6_cxx

#define Ntuple6_cxx
#include "Ntuple6.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

void Ntuple6::Loop()
{
//   In a ROOT session, you can do:
//      root> .L Ntuple6.C
//      root> Ntuple6 t
//      root> t.GetEntry(12); // Fill t data members with entry number 12
//      root> t.Show();       // Show values of entry 12
//      root> t.Show(16);     // Read and show values of entry 16
//      root> t.Loop();       // Loop on all entries
//

//     This is the loop skeleton where:
//    jentry is the global entry number in the chain
//    ientry is the entry number in the current Tree
//  Note that the argument to GetEntry must be:
//    jentry for TChain::GetEntry
//    ientry for TTree::GetEntry and TBranch::GetEntry
//
//       To read only selected branches, Insert statements like:
// METHOD1:
//    fChain->SetBranchStatus("*",0);  // disable all branches
//    fChain->SetBranchStatus("branchname",1);  // activate branchname
// METHOD2: replace line
//    fChain->GetEntry(jentry);       //read all branches
//by  b_branchname->GetEntry(ientry); //read only this branch
   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();

   Long64_t nbytes = 0, nb = 0;
   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      // if (Cut(ientry) < 0) continue;
   }
}

In your “Write” routine, use:

if (LoadTree(jentry) < 0) break;
fChain->GetEntry(jentry);

hi, Wile_E_Coyote, thanks for your reply!

I added, and got :

Warning in <TBasket::ReadBasketBuffers>: basket:Pz has fNevBuf=3 but fEntryOffset=0, pos=206960, len=810, fNbytes=810, fObjlen=738, trying to repair
Error in <TRint::HandleTermInput()>: std::length_error caught: vector<T> too long

I’ve tried printing to the screen to see where the problem is.
I have four data members
Int_t EventID;
vector *Px;
vector *Py;
vector *Pz;
When an error occurs in a Entry(jentry), the EventID can be printed, and others not.

Those error using indicate that the file is corrupted. You can check whether the file is file by executing (without your code):

auto f = TFIle::Open(filename);
auto tree = f->Get<TTree>("Ntuple6");
for(Long64_t e = 0; e < tree->GetEntriesFast; ++e)
   tree->GetEntry(e);

If this works correctly and since you mentioned "But this problem doesn’t always arise. ", I then recommend running your example under valgrind:

valgrind --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -b -l -q -e '.L Ntuple6.C+' -e 'Ntuple6 t; t.Write();'