Search code examples
c++11root-framework

ROOT(CERN) calling method creates segfault even without trying the first line (seems like it's SetBranchAddress)


I try to read root file as usual using SetBranchAddress method, but when i run getHist() method it gives me segfault and even doesn't print "called getHist".

I use ROOT 6.10 and compile my code with gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5 using make.

#define nSec 110

using namespace std;

void getHist(TString filename, TH1D* h1Hnum)
{
  cout << "called getHist" << endl;

  // TChain *theChain1 = new TChain("T");

  TFile *file = new TFile(filename);
  TTree *theChain1 = (TTree*)file->Get("T");
  if (!theChain1) { std::cout << "Error: tree not found." << std::endl; delete file; return; }

  cout << filename << endl;
  // theChain1->Add(filename);

  Int_t EventID1;
  Int_t nPart1;
  Int_t nColl1;
  Int_t TrackID1[100000];
  Int_t ParentID1[100000];
  Int_t StationID1[100000];
  Double_t X1[100000];
  Double_t Y1[100000];
  Double_t Z1[100000];
  Double_t Momentum1[100000];
  Double_t Px1[100000];
  Double_t Py1[100000];
  Double_t Pz1[100000];
  Double_t Time1[100000];
  Double_t PdgID1[100000];

  theChain1->SetBranchAddress("EventID", &EventID1);
  theChain1->SetBranchAddress("nPart", &nPart1);
  theChain1->SetBranchAddress("nColl", &nColl1);
  theChain1->SetBranchAddress("TrackID", TrackID1);
  theChain1->SetBranchAddress("ParentID", ParentID1);
  theChain1->SetBranchAddress("StationID", StationID1);
  theChain1->SetBranchAddress("X", X1);
  theChain1->SetBranchAddress("Y", Y1);
  theChain1->SetBranchAddress("Z", Z1);
  theChain1->SetBranchAddress("Momentum", Momentum1);
  theChain1->SetBranchAddress("Px", Px1);
  theChain1->SetBranchAddress("Py", Py1);
  theChain1->SetBranchAddress("Pz", Pz1);
  theChain1->SetBranchAddress("Time", Time1);
  theChain1->SetBranchAddress("PdgID", PdgID1);

  Long_t nEv1 = theChain1->GetEntries();

  ////////// Loop 1 //////////
  for (Long_t j = 0; j <  nEv1; ++j) {
    theChain1->GetEntry(j);
    // h1nColl->Fill(nColl1);

    Int_t nPhot[nSec] = {0};
    Bool_t isChecked[nSec] = {false};

    for (Int_t i = 0; i < nPart1; ++i){
      if (StationID1[i] < 0) continue;
      if (isChecked[StationID1[i]]) continue;

      nPhot[StationID1[i]] ++;
      if (nPhot[StationID1[i]] > 20.){
        // h1Hnum->Fill(StationID[i]);
        isChecked[StationID1[i]] = true;
      }
    }

    Int_t numOfHits = 0;
    for (int i = 0; i < nSec; ++i)
      if (isChecked[i]) numOfHits++;

    h1Hnum->Fill(numOfHits);
  }

  cout << "returning hist" << endl;

}


int main(int argc, char** argv){

  #define NHIST 10

  TH1D *array[NHIST];

  cout << "start looping" << endl;

  // printHello();
  // getHist(filename);


  for (Int_t i = 0; i < NHIST; ++i) {
    TString filename = "newData_";
    filename += TString (std::to_string(i+1));
    filename += TString(".root");

    array[i] = new TH1D(TString("Hit Number"+std::to_string(i)),TString("HitNumber"+std::to_string(i)), nSec, -0.5, nSec-0.5);


    cout << "trying to get hist "<< filename << endl;

    getHist(filename, array[i]);

    cout << "trying to make new hist "  << endl;

    cout << "trying to set color" << endl;
    array[i]->SetLineColor(i);
    // delete htemp;
  }

  cout << "finish looping" << endl;

  TApplication *app = new TApplication("name", &argc, argv);
  cout << "TApp created" << endl;

  TCanvas *c = new TCanvas();

  array[0]->Draw();
  for (Int_t i = 1; i < NHIST; ++i) {
    array[i]->Draw("SAME");
  }
  // c->Show();
  // app->Run();
  std::cin;

  delete[] array;
  delete c;

  return 0;
}

It prints: start looping

trying to get hist newData_1.root
Segmentation fault (core dumped)

But if i comment all lines with SetBranchAdress that works and method getHist is called in loop. (actual output is to long but believe me, there's all thing that are printed by cout)


Solution

  • I fixed it just changing array definition to dynamic. Before:

    Int_t TrackID1[100000];
    

    After:

    Int_t *TrackID1 = new Int_t[100000];