Crash due to uncleaned variables?

Hi guys,

sorry for still newbie questions but, I think, I will never be friend of this software.
However, I made a code that keeps a of root file, extract values from it and, after making stuff, it write them on another root file (I implement this in a bash file to keep a series of root file with same structure). I do this:

#include <TH1.h>
#include <TH1F.h>
#include <TH1D.h>
#include <TH2.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TH1F.h>
#include <TFile.h>
#include <TObject.h>
#include <TMath.h>
#include <TTree.h>
#include <TBranch.h>
#include <TLeaf.h>

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;


 // OUTPUT-Definitions                                                                                                                                                                                                                                                                                                  
  TFile *histoFile = new TFile(argv[1],"RECREATE");
  TTree *t2 = new TTree("t2","");

  // declaration of some variable                                                                                                                                                 
  int Nsel0=0, Nsel1=0, Nsel2=0, Nsel3=0;

  //Input file definition and its variable extraction                                                                                                                             
  TFile *fopen=TFile::Open(argv[2]);
  TTree *myt1=(TTree*)fopen->Get("t1");
  int accepted = 0;
  unsigned long int nentries = myt1->GetEntries(); 

 myt1->SetBranchAddress("variable",&variable);
***** stuff *****

for(unsigned long int i=0; i<nentries; i++){
  *** stuff ***
  t2->Branch("Variable",&Variable,"Variable/D");
  t2->Fill();
}
t2-> Write();

histoFile->cd();
histoFile->Write();
histoFile->Close();

This worked but gave an Error saying that the the argv[2] file is not writable… even if the output came out in the wirte way.
I did not know the nature of the error, so I have tried to change the definition of the input file in:

TFile fopen(argv[2]);
TTree *myt1=(TTree*)fopen.Get("t1");

This did not work so I came back to the previous stage but it gave me this segmentation fault

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f796e97c60c in waitpid () from /lib64/libc.so.6
#1  0x00007f796e8f9f62 in do_system () from /lib64/libc.so.6
#2  0x00007f79738f7619 in TUnixSystem::StackTrace() () from /software/centos-7-x86_64/root/5.34.38/lib/libCore.so
#3  0x00007f79738f923c in TUnixSystem::DispatchSignals(ESignals) () from /software/centos-7-x86_64/root/5.34.38/lib/libCore.so
#4  <signal handler called>
#5  main (argc=<optimized out>, argv=0x7ffeab7ef918) at /folder/code.cc:76
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  main (argc=<optimized out>, argv=0x7ffeab7ef918) at /folder/code.cc:76
===========================================================
`

The strange thing is that before it worked and after coming back to the same code, it did not work anymore. So, I guess there should be some cleaning operation (I tried to look around and this is the only suggestion that I got) that I have to do but don’t know what.
Putting some flags around I found that the error is in

unsigned long int nentries = myt1->GetEntries();

but don’t know why, since before it worked.

Sorry again for these newbie questions.

Thanks in advance for the reply.

ROOT Version: 5.37-38
Platform: Unix CentOS 7.9.2009
Compiler: CINT 5.18


Attach the full “code.cc” file.

Thanks for the reply.

This is the code.cc

#include <TH1.h>
#include <TH1F.h>
#include <TH1D.h>
#include <TH2.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TH1F.h>
#include <TFile.h>
#include <TObject.h>
#include <TMath.h>
#include <TTree.h>
#include <TBranch.h>
#include <TLeaf.h>

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

const double PIm=TMath::Pi()/2;
const double conv=90./PIm; 

void usage(){
     cerr <<  "\n  Usage: code.o <Outputfilename> <InputName> InitialZenith InitialAzimuth FinalZenith FinalAzimuth  Zenith+ Azimuth+ Zenith- Azimuth- \n"

}

int main(int argc, char **argv)
{
  if ( argc != 11 )
    {
      usage();
      exit(1);
    } 
  
  double zenini,aziini,zenfin,azifin,zenplus,aziplus,zenmin,azimin;

  zenini=atof(argv[3]);
  aziini=atof(argv[4]);
  zenfin=atof(argv[5]);
  azifin=atof(argv[6]);
  zenplus=atof(argv[7]);
  aziplus=atof(argv[8]);
  zenmin=atof(argv[9]);
  azimin=atof(argv[10]);

  // OUTPUT-Definitions
  TFile *histoFile = new TFile(argv[1],"RECREATE");
  TTree *t2 = new TTree("t2","");
 
  // declaration of some variable
  int Nsel0=0, Nsel1=0, Nsel2=0, Nsel3=0;   

  //Input file definition and its variable extraction    
  TFile *fopen=TFile::Open(argv[2]);
  TTree *myt1=(TTree*)fopen->Get("t1");
 
  int accepted = 0;
  unsigned long int nentries = myt1->GetEntries();
  cout << "Reading file "<< argv[2] <<" with "<<nentries<<" events\n"<<endl;

  //########## Definition of the branches in t1 ##########
  int ntot, nsel, run, Tcount, a_nhit, a_nlin, frame, scan; 
  double E_nu, time, zen_nu, azi_nu, Dec_nu, RA_nu, w2, w3, RunDurationYear, quality;
  double a_lambda, a_zen, a_azi, a_dec, a_ra, a_beta, a_dEdX, a_TrackLength, a_EstMuEnANN;
  
  myt1->SetBranchAddress("ntot",&ntot);
  myt1->SetBranchAddress("nsel",&nsel);
  myt1->SetBranchAddress("run",&run);
  myt1->SetBranchAddress("E_nu",&E_nu);
  myt1->SetBranchAddress("time",&time);
  myt1->SetBranchAddress("zen_nu",&zen_nu);
  myt1->SetBranchAddress("azi_nu",&azi_nu);
  myt1->SetBranchAddress("Dec_nu",&Dec_nu);
  myt1->SetBranchAddress("RA_nu",&RA_nu);
  myt1->SetBranchAddress("Tcount",&Tcount);
  myt1->SetBranchAddress("w2",&w2);
  myt1->SetBranchAddress("w3",&w3);
  myt1->SetBranchAddress("a_lambda",&a_lambda);
  myt1->SetBranchAddress("a_zen",&a_zen);
  myt1->SetBranchAddress("a_azi",&a_azi);
  myt1->SetBranchAddress("a_dec",&a_dec);
  myt1->SetBranchAddress("a_ra",&a_ra);
  myt1->SetBranchAddress("a_beta",&a_beta);
  myt1->SetBranchAddress("a_nhit",&a_nhit);
  myt1->SetBranchAddress("a_nlin",&a_nlin);
  myt1->SetBranchAddress("a_dEdX",&a_dEdX);
  myt1->SetBranchAddress("a_TrackLength",&a_TrackLength);
  myt1->SetBranchAddress("a_EstMuEnANN",&a_EstMuEnANN);
  myt1->SetBranchAddress("frame",&frame);
  myt1->SetBranchAddress("quality",&quality);
  myt1->SetBranchAddress("scan",&scan);
  myt1->SetBranchAddress("RunDurationYear",&RunDurationYear);
 
  ofstream output;
  output.open("/bin/temp.txt");

  ofstream outputB;
  outputB.open("/bin/tempB.txt");

  ofstream outputSel0;
  outputSel0.open("/bin/tempNSEL0.txt");

  ofstream outputSel1;
  outputSel1.open("/bin/tempNSEL1.txt");

  ofstream outputSel2;
  outputSel2.open("/bin/tempNSEL2.txt");

  ofstream outputSel3;
  outputSel3.open("/bin/tempNSEL3.txt");

  for(int i=0;i<nentries;i++){
     myt1->GetEntry(i);
     
     double zen_nuB=0., azi_nuB=0.;
     zen_nuB=zen_nu*conv;
     azi_nuB=azi_nu*conv;
      

     if(((zenfin<zen_nuB)&&(zen_nuB<zenini))||((zenini<zen_nuB)&&(zen_nuB<zenfin))){
       Nsel0++;
       outputSel0<<"RUN: "<< run <<" nentries: "<< nentries << " nsel: "<< nsel << " Nsel0: "<< Nsel0 <<" zen_nu(deg): "<< zen_nuB <<" E_nu(GeV): "<< E_nu <<"\n";
       if(((azifin<azi_nuB)&&(azi_nuB<aziini))||((aziini<azi_nuB)&&(azi_nuB<azifin))){
         Nsel1++;                    
         outputSel1<<"RUN: "<< run <<" nentries: "<< nentries << " nsel: "<< nsel << " Nsel1: "<< Nsel1 <<" zen_nu(deg): "<< zen_nuB <<" azi_nu(deg): "<<azi_nuB <<" E_nu(GeV):\
 "<< E_nu <<"\n";
         if((zenmin<zen_nuB)&&(zen_nuB<zenplus)){
           Nsel2++;
           outputSel2<<"RUN: "<< run <<" nentries: "<< nentries << " nsel: "<< nsel << " Nsel2: "<< Nsel2 <<" zen_nu(deg): "<< zen_nuB <<" azi_nu(deg): "<<azi_nuB <<" E_nu(GeV): "<< E_nu <<"\n"; 
           if((azimin<azi_nuB)&&(azi_nuB<aziplus)){
             Nsel3++;    
             cout <<"Nsel3: "<<Nsel3 <<" azimin: "<<azimin << " aziplus: "<<aziplus <<" azi_nuB: "<<azi_nuB<<endl;
             outputSel3<<"RUN: "<< run <<" nentries: "<< nentries << "nsel: "<< nsel << " Nsel3: "<< Nsel3 <<" zen_nu(deg): "<<zen_nuB <<" azi_nu(deg): "<<azi_nuB <<" E_nu (GeV): "<< E_nu <<"\n";

             accepted=1;

	     // declaration of some variable 
	     double E_Nu, Zen_Nu, Azi_Nu, Time, W2, W3, RA_Nu, Dec_Nu;
	     double A_Lambda, A_Zen, A_Azi, A_Beta, A_dEdX, A_TraLen, A_EstMuEnANN, A_Dec, A_RA;
	     int Run, Scan, A_Nhit, A_Nlin, TCount, Frame, Ntot;
	     Double_t Quality=-1, RuDuYe=-1;

	     // MC
	     // Adding the branch
	     t2->Branch("E_Nu",&E_Nu,"E_Nu/D");
	     t2->Branch("Time",&Time,"Time/D");
	     t2->Branch("Zen_Nu",&Zen_Nu,"Zen_Nu/D");
	     t2->Branch("Azi_Nu",&Azi_Nu,"Azi_Nu/D");
	     t2->Branch("Dec_Nu",&Dec_Nu,"Dec_Nu/D");
	     t2->Branch("RA_Nu",&RA_Nu,"RA_Nu/D");
	     t2->Branch("Run",&Run,"Run/I");
	     t2->Branch("TCount",&TCount,"TCount/I");
	     t2->Branch("W2", &W2, "W2/D");
	     t2->Branch("W3", &W3, "W3/D");
	     t2->Branch("A_Lambda", &A_Lambda, "A_Lambda/D");
	     t2->Branch("A_Zen", &A_Zen, "A_Zen/D");
	     t2->Branch("A_Azi", &A_Azi, "A_Azi/D");
	     t2->Branch("A_Dec", &A_Dec, "A_Dec/D");
	     t2->Branch("A_RA", &A_RA, "A_RA/D");
	     t2->Branch("A_Beta", &A_Beta, "A_Beta/D");
	     t2->Branch("A_Nhit", &A_Nhit, "A_Nhit/I");
	     t2->Branch("A_Nlin", &A_Nlin, "A_Nlin/I");
	     t2->Branch("A_dEdX", &A_dEdX, "A_dEdX/D");
	     t2->Branch("A_TrackLength", &A_TraLen, "A_TrackLength/D");
	     t2->Branch("A_EstMuEnANN", &A_EstMuEnANN, "A_EstMuEnANN/D");
	     t2->Branch("Frame", &Frame, "Frame/I");
	     t2->Branch("Quality", &Quality, "Quality/D");
	     t2->Branch("Scan", &Scan, "Scan/I");
	     t2->Branch("RunDurationYear", &RuDuYe, "RunDurationYear/D");
	     t2->Branch("Ntot", &Ntot,"Ntot/I");
	     t2->Branch("Nsel0",&Nsel0,"Nsel0/I");
	     t2->Branch("Nsel1",&Nsel1,"Nsel1/I");      
	     t2->Branch("Nsel2",&Nsel2,"Nsel2/I");   
	     t2->Branch("Nsel3",&Nsel3,"Nsel3/I"); 

	     E_Nu=E_nu;
             Zen_Nu=zen_nuB; 
             Azi_Nu=azi_nuB;
             Time=time; 
             W2=w2;
             W3=w3;
             RA_Nu=RA_nu; 
             Dec_Nu=Dec_nu;
	     A_Lambda=a_lambda;
             A_Zen=a_zen; 
             A_Azi=a_azi; 
             A_Beta=a_beta;
             A_dEdX=a_dEdX;
             A_TraLen=a_TrackLength;
             A_EstMuEnANN=a_EstMuEnANN;
             A_Dec=a_dec;
             A_RA=a_ra;
	     Run=run; 
             Scan=scan; 
             A_Nhit=a_nhit;
             A_Nlin=a_nlin;
             TCount=Tcount;
             Frame=frame;
             Ntot=ntot;
	     Quality=quality; 
             RuDuYe=RunDurationYear;
             
             t2->Fill();
	   }
	 }
       }
     }
  }
   
  if(accepted==1)
    {
      cout <<"accepted: "<< accepted <<endl;
     t2->Write();
    }else{
    outputB<<"RUN: "<<run <<" "<<-1<<"\n";    
  }

  output <<"run: " << run << " ntot: "<< ntot <<" nentries: "<< nentries <<" Nsel0: "<< Nsel0 << " Nsel1: " << Nsel1 <<" Nsel2: "<< Nsel2 <<" Nsel3: "<< Nsel3 <<"\n"; 
  //save histograms
  histoFile->cd();
  histoFile->Write();
  histoFile->Close();

  output.close();
  outputB.close();
  outputSel0.close();
  outputSel1.close();
  outputSel2.close();
  outputSel3.close();

  cout<< "\ntotal number of events (ntot): "<< ntot << "\n"
      << "selected events in the (zenith) RUN range(Nsel0) : "<< Nsel0 <<"\n"
      << "selected events in the (zenith,azimuth) RUN range(Nsel1) : "<< Nsel1 <<"\n"
      << "selected events in the RUN range, between (zen-;zen+) (Nsel2): "<< Nsel2 <<"\n"
      << "selected events in the RUN range, between (zen-;zen+) and (azi-;azi+) (Nsel3): "<< Nsel3 <<"\n"
      <<endl;
  
}

I had the previous error (argv[2] not writable ) only inside the NSel3 condition.
As I explained, after I changed very less the code and it didn’t work. So I cambe back to the previous stage (the one in this code) and gives me the segmentation fault.

All “t2->Branch(...);” should be moved outside (before) of the “for” loop.

BTW. In the “usage”, a “;” is missing after the “cerr” line.

I did it before but after I changed and put them into the third condition since I want to write on the ouput root file only if the third condition is satisfied. So was to save space (or I thought it did).
However, it continue to give me same segmentation fault at same point (myt1->GetEntries() ).

It’s because I deleted a few useless parts in that function when I copied the code here.

Try:

  TFile *histoFile = TFile::Open(argv[1], "RECREATE");
  if ((!histoFile) || histoFile->IsZombie()) { delete histoFile; return 1; }

and then:

  TFile *fopen = TFile::Open(argv[2]);
  if ((!fopen) || fopen->IsZombie()) { delete fopen; delete histoFile; return 2; }
  TTree *myt1; fopen->GetObject("t1", myt1);
  if (!myt1) { delete fopen; delete histoFile; return 3; }

BTW. Maybe you also need to change the “fopen” name (there exists a standard function with this name).

Ok, with all of these changes it seems that the segmentation fault and related errors are not occurring anymore. So, as far as I understood there were Zombie files or not opend one (don’t know why).
However, it’s still present the error that I got in the beginning inside the third condition, when it writes on the output.root file, of which I don’t understand it’s nature:

Error in <TFile::WriteTObject>: Directory /folder/input.root is not writable

I mean, I don’t understand why it gives problem to the input “directory”

histoFile->cd(); t2->Write();

1 Like