Problem in exporting data from array

Hi Rooters,
I’m new to ROOT and has a probabily stupid question.
There’re many root files and I want to export a branch data called “dth”, which is a 9X5 array(dth[9][5]).
I need the x (dth[0][0]) and chargx (dth[0][3]) data, y (dth[0][1]) and chargy (dth[0][4]) data in that array, and make a x-chargx , a y-chargy TH1D of them.
Then I have a problem. I’m not sure if I express clearly, so this is my macro:
(I am running Root 6.02/13 on Mac)

void ELECdth2(){
  TFile* m_f;
  Double_t x[1000],y[1000],chargx[1000],chargy[1000];
 
        TTree *dst = (TTree*)m_f->Get("dst");
        Double_t table[9][5];
        dst->SetBranchAddress("dth",table);

        Long64_t entry = dst->GetEntries();
        for(Int_t i=0; i<entry; i++){
          dst->GetEntries();
          x[i] = table[0][0];
          chargx[i] = table[0][3];
          y[i] = table[0][1];
          chargy[i] = table[0][4];
                }
        TH1D *histx1;
        TH1D *histy1;

        for(Int_t j=0;j<entry;j++){
          histx1->Fill(x[j],chargx[j]);
          histy1->Fill(y[j],chargy[j]);
        }
  TCanvas *canvas = new TCanvas("","",1000,800);
  canvas->Divide(2,9,0.01,0.01,0);
  canvas->cd(1);
  histx1->Draw();
  canvas->cd(2);
  histy1->Draw();


  m_f->Close();
  TFile* outF = new TFile("outFile.root", "RECREATE");
  canvas->Write();
  canvas->Print("ELECdth.pdf");
  outF->Close();
}

Then I got this:


 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003c7d0ac7be in waitpid () from /lib64/libc.so.6
#1  0x0000003c7d03e5c9 in do_system () from /lib64/libc.so.6
#2  0x00007f94f5d8406a in TUnixSystem::StackTrace() () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCore.so
#3  0x00007f94f5d8603c in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCore.so
#4  <signal handler called>
#5  0x00007f94f309028f in ?? ()
#6  0x00007ffe5ed951c0 in ?? ()
#7  0x00007f94f4ee4903 in llvm::FoldingSetNodeID::operator==(llvm::FoldingSetNodeIDRef) const () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#8  0x00007f94f4ee492a in llvm::FoldingSetNodeID::operator==(llvm::FoldingSetNodeID const&) const () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#9  0x00007f94f4ee5054 in llvm::FoldingSetImpl::FindNodeOrInsertPos(llvm::FoldingSetNodeID const&, void*&) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#10 0x00007f94f43e6fef in clang::NestedNameSpecifier::FindOrInsert(clang::ASTContext const&, clang::NestedNameSpecifier const&) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#11 0x0000000000000000 in ?? ()
===========================================================


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  0x00007f94f309028f in ?? ()
#6  0x00007ffe5ed951c0 in ?? ()
#7  0x00007f94f4ee4903 in llvm::FoldingSetNodeID::operator==(llvm::FoldingSetNodeIDRef) const () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#8  0x00007f94f4ee492a in llvm::FoldingSetNodeID::operator==(llvm::FoldingSetNodeID const&) const () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#9  0x00007f94f4ee5054 in llvm::FoldingSetImpl::FindNodeOrInsertPos(llvm::FoldingSetNodeID const&, void*&) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#10 0x00007f94f43e6fef in clang::NestedNameSpecifier::FindOrInsert(clang::ASTContext const&, clang::NestedNameSpecifier const&) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCling.so
#11 0x0000000000000000 in ?? ()
===========================================================

Not sure how to do next…
Any Reply helps.
Thanks.

Hi,

In the code snippet show here, the input file is never open; i.e. m_f is never initialized and thus the line
TTree *dst = (TTree*)m_f->Get("dst");
will crash.

Is that just a copy/paste ommision or the actual problem?

Cheers,
Philippe.

Dear Philippe,
Thanks for help. It’s an actual problem.
So I add some macro like the following:

std::vector<std::string> GetFileList(const char* fileName)
{
  std::vector<std::string> fileList;
  char infile[1000];
  sprintf(infile, "%s.txt" , fileName);
  std::string line;
  std::ifstream ifs(infile);
  while(std::getline(ifs, line))
    {
      fileList.push_back(line);
    }
  return fileList;
}


void ELECdth2(){
  std::vector<std::string> FileList = GetFileList("filelist");
  TFile* m_f;
  Double_t x[1000],y[1000],chargx[1000],chargy[1000];
  Long64_t entry;

  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open(FileList[iFile].c_str());
        TTree *dst = (TTree*)m_f->Get("dst");
        Double_t table[9][5];
        dst->SetBranchAddress("dth",table);

        entry = dst->GetEntries();
        for(Int_t i=0; i<entry; i++){
          dst->GetEntries();
          x[i] = table[0][0];
          chargx[i] = table[0][3];
          y[i] = table[0][1];
          chargy[i] = table[0][4];
                }
    }
        TH1D *histx1;
        TH1D *histy1;

        for(Int_t j=0;j<entry;j++){
          histx1->Fill(x[j],chargx[j]);
          histy1->Fill(y[j],chargy[j]);
        }


  TCanvas *canvas = new TCanvas("","",1000,800);
  canvas->Divide(2,9,0.01,0.01,0);
  canvas->cd(1);
  histx1->Draw();
  canvas->cd(2);
  histy1->Draw();


  m_f->Close();
  TFile* outF = new TFile("outFile.root", "RECREATE");
  canvas->Write();
  canvas->Print("ELECdth.pdf");
  outF->Close();
}
                        

, but still got something wrong:

Error in <TFile::TFile>: file YiAnalytics_MC.0000000.root  YiAnalytics_MC.0000017.root does not exist

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003c7d0ac7be in waitpid () from /lib64/libc.so.6
#1  0x0000003c7d03e5c9 in do_system () from /lib64/libc.so.6
#2  0x00007feaabbb606a in TUnixSystem::StackTrace() () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCore.so
#3  0x00007feaabbb803c in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/cms.cern.ch/slc6_amd64_gcc493/lcg/root/6.02.12-kpegke4/lib/libCore.so
#4  <signal handler called>
#5  0x00007feaa8ec2307 in ?? ()
#6  0x0000000000000000 in ?? ()
===========================================================


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  0x00007feaa8ec2307 in ?? ()
#6  0x0000000000000000 in ?? ()
===========================================================

I have totally 33 root files and it said that two of them can not be found.
After searching on website, I still can’t find out what’s wrong with it.

Hi,

You need to add error checking to your code:

 for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open(FileList[iFile].c_str());
        if (!m_f || m_f->IsZombie()) continue;
        TTree *dst = nullptr;
        m_f->GetObject("dst",dst);
        if (!dst) { 
           std::cerr << "Can not find the tree named dst in file named: " << FileList[iFile] << '\n';
           continue;
        }

Cheers,
Philippe.

You need to initialize your variables properly! histx1 und histy1 are used but not initialized. → before entering the loop do: TH1* histx1 = new TH1D(…);

Also:

This is a loop that does NOT read the tree! Instead of dst->GetEntries() you need to call dst->GetEntry(i) instead.

Also, why are you storing the values in the arrays x,y,chargex,chargey? You can fill them directly into your histogram. This also avoids problems for cases with more than 1000 entries (also make i Long64_t).

One more thing: when looping over files, don’t forget to “delete m_f” at the end of the loop body.

1 Like

okay, thanks to your help I find out the problem. Root files and my macro are in the different directory so none of them can’t be found.
But I don’t have permission to create a C file in where the root files are.
So should I put in the absolute path? But where should I put in? Or how can I change my macro?

Thanks for the advise!
And I’m wondering the second part you said…do you mean like this?

  TH1D *histx1 = new TH1D("histx1","",1000,0,1000);
  TH1D *histy1 = new TH1D("histx2","",1000,0,1000);

  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open(/data1/yuhsuan/data39/FileList[iFile].c_str());
        TTree *dst = (TTree*)m_f->Get("dst");
        Double_t table[9][5];
        dst->SetBranchAddress("dth",table);

        entry = dst->GetEntries();
        for(Int_t i=0; i<entry; i++){
          dst->GetEntry(i);
          histx1->Fill(table[0][0],table[0][3]);
          histy1->Fill(table[0][1],table[0][4]);
                }
    }

Do I change in the right way?

Did this resolve your problem?

No…it shows:

Error in <TFile::Open>: no url specified

and I have to interrupt program to get out of endless processing.
I don’t know where the problem is…

This line looks problematic:

Perhaps you want:

m_f = TFile::Open(("/data1/yuhsuan/data39/" + FileList[iFile]).c_str());

after I added…

SysError in <TFile::ReadBuffer>: error reading from file /data1/yuhsuan/data39/ (Is a directory)
Error in <TFile::Init>: /data1/yuhsuan/data39/ failed to read the file type data.

and I still have to interrupt program to get out.

so this is all my macro now…


std::vector<std::string> GetFileList(const char* fileName)
{ 
  std::vector<std::string> fileList;
  char infile[1000];
  sprintf(infile, "%s.txt" , fileName);
  std::string line;
  std::ifstream ifs(infile);
  while(std::getline(ifs, line))
    { 
      fileList.push_back(line);
    }
  return fileList;
}


void ELECdth2(){
  std::vector<std::string> FileList = GetFileList("filelist");
  TFile* m_f;
  Double_t x[1000],y[1000],chargx[1000],chargy[1000];
  Int_t entry;
  
for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open(FileList[iFile].c_str());
        if (!m_f || m_f->IsZombie()) continue;
        TTree *dst = nullptr;
        m_f->GetObject("dst",dst);
        if (!dst) { 
           std::cerr << "Can not find the tree named dst in file named: " << FileList[iFile] << '\n';
           continue;
        	}
    }

  TH1D *histx1 = new TH1D("histx1","",1000,0,1000);
  TH1D *histy1 = new TH1D("histy1","",1000,0,1000);	

  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open( FileList[iFile].c_str());
	TTree *dst = (TTree*)m_f->Get("dst");
	Double_t table[9][5];
       	dst->SetBranchAddress("dth",table);

	entry = dst->GetEntries();
	for(Int_t i=0; i<entry; i++){
	  dst->GetEntry(i);
	  histx1->Fill(table[0][0],table[0][3]);
	  histy1->Fill(table[0][1],table[0][4]);
		}
    }
	

  TCanvas *canvas = new TCanvas("","",1000,800);
  canvas->Divide(2,9,0.01,0.01,0);
  canvas->cd(1);
  histx1->Draw();
  canvas->cd(2);
  histy1->Draw();

  m_f->Close();
  TFile* outF = new TFile("outFile.root", "RECREATE");
  canvas->Write();
  canvas->Print("ELECdth.pdf");
  outF->Close();
}

which leads to

Error in <TFile::Open>: no url specified

Im losing my mind …

You should check your list of files. It looks as if one of the members of fileList is empty.

Also, you repeat the same block of code (for loop) without the error checking, try this instead:

std::vector<std::string> GetFileList(const char* fileName)
{ 
  std::vector<std::string> fileList;
  char infile[1000];
  sprintf(infile, "%s.txt" , fileName);
  std::string line;
  std::ifstream ifs(infile);
  while(std::getline(ifs, line))
    { 
      fileList.push_back(line);
    }
  return fileList;
}


void ELECdth2(){
  std::vector<std::string> FileList = GetFileList("filelist");
  TFile* m_f;
  Double_t x[1000],y[1000],chargx[1000],chargy[1000];
  Int_t entry;
  
  TH1D *histx1 = new TH1D("histx1","",1000,0,1000);
  TH1D *histy1 = new TH1D("histy1","",1000,0,1000);	

   for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
        m_f = TFile::Open(FileList[iFile].c_str());
        if (!m_f || m_f->IsZombie()) continue;
        TTree *dst = nullptr;
        m_f->GetObject("dst",dst);
        if (!dst) { 
           std::cerr << "Can not find the tree named dst in file named: " << FileList[iFile] << '\n';
           continue;
        }
   	Double_t table[9][5];
       	dst->SetBranchAddress("dth",table);

	entry = dst->GetEntries();
	for(Int_t i=0; i<entry; i++){
	  dst->GetEntry(i);
	  histx1->Fill(table[0][0],table[0][3]);
	  histy1->Fill(table[0][1],table[0][4]);
	}
        m_f->Close();
    }
	

  TCanvas *canvas = new TCanvas("","",1000,800);
  canvas->Divide(2,9,0.01,0.01,0);
  canvas->cd(1);
  histx1->Draw();
  canvas->cd(2);
  histy1->Draw();


  TFile* outF = new TFile("outFile.root", "RECREATE");
  canvas->Write();
  canvas->Print("ELECdth.pdf");
  outF->Close();
}

I also moved the m_f->close() statement. You may want to do error checking on the dst->SetBranchAddress as well.

I replace by your macro but still leads to same situation…

Error in <TFile::Open>: no url specified

(have checked the files and they are ok I think.)

Add the following before the TFile::Open

gSystem->Exec(("ls " + FileList[iFile]).c_str());
std::cout << "Attempting to open: '" << FileList[iFile] << "'\n";

And check if your file is indeed in the listing.

I think file problem is solved(every files shows YiAnalytics_MC.00000–.root
Attempting to open: ‘YiAnalytics_MC.00000–.root’), but then histo doesn’t show anything.

I think maybe the part of exporting data is wrong?

          histx1->Fill(table[0][0],table[0][3]);
          histy1->Fill(table[0][1],table[0][4]);

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