ROOT(CERN) 调用方法即使不尝试第一行也会创建段错误(好像是 SetBranchAddress)
ROOT(CERN) calling method creates segfault even without trying the first line (seems like it's SetBranchAddress)
我尝试像往常一样使用 SetBranchAddress
方法读取根文件,但是当我使用 运行 getHist()
方法时,它给了我段错误甚至不打印 "called getHist" .
我使用 ROOT 6.10 并使用 make
.
使用 gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5 编译我的代码
#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;
}
它打印:
开始循环
trying to get hist newData_1.root
Segmentation fault (core dumped)
但是如果我用 SetBranchAdress
注释所有行,并且方法 getHist
在循环中被调用。
(实际输出很长,但相信我,cout 打印了所有东西)
我修复它只是将数组定义更改为动态。
之前:
Int_t TrackID1[100000];
之后:
Int_t *TrackID1 = new Int_t[100000];
我尝试像往常一样使用 SetBranchAddress
方法读取根文件,但是当我使用 运行 getHist()
方法时,它给了我段错误甚至不打印 "called getHist" .
我使用 ROOT 6.10 并使用 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;
}
它打印: 开始循环
trying to get hist newData_1.root
Segmentation fault (core dumped)
但是如果我用 SetBranchAdress
注释所有行,并且方法 getHist
在循环中被调用。
(实际输出很长,但相信我,cout 打印了所有东西)
我修复它只是将数组定义更改为动态。 之前:
Int_t TrackID1[100000];
之后:
Int_t *TrackID1 = new Int_t[100000];