Code crash by putting in function

hi, my code is as below:

TH1D* h = new TH1D("h","h",1000,0,400);
TFile *f = new TFile("/home/sima/ElMuDATA.root_tree.root","READ");
TTree *myTree;
gDirectory->GetObject("tree",myTree);

//myTree->Print();
std::vector<double> *pt_Leptons;
std::vector<double> *pt_bJets;
TBranch *b;
TBranch *bb;
myTree->SetBranchAddress("pt_Leptons", &pt_Leptons, &b);
//myTree->SetBranchAddress("pt_bJets", &pt_bJets, &bb);
//Long64_t n = myTree->GetEntries();

for (Long64_t i=1;i < 100 ;i++) {
    myTree->GetEntry(i);
}

it works but when I put it in function to create a ā€œ.Cā€ file it crashed because of " myTree->GetEntry(i); ".

void f(){
TH1D* h = new TH1D("h","h",1000,0,400);
TFile *f = new TFile("/home/sima/ElMuDATA.root_tree.root","READ");
TTree *myTree;
gDirectory->GetObject("tree",myTree);

//myTree->Print();
std::vector<double> *pt_Leptons;
std::vector<double> *pt_bJets;
TBranch *b;
TBranch *bb;
myTree->SetBranchAddress("pt_Leptons", &pt_Leptons, &b);
//myTree->SetBranchAddress("pt_bJets", &pt_bJets, &bb);
//Long64_t n = myTree->GetEntries();

for (Long64_t i=1;i < 100 ;i++) {
    myTree->GetEntry(i);
}
    
}

this is the crash output:

There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f84a4a97687 in __GI___waitpid (pid=6294, stat_loc=stat_loc
entry=0x7fffbab95268, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f84a4a02067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f84a5639ed3 in TUnixSystem::StackTrace() () from /home/sima/Products/root/lib/libCore.so.6.22
#3  0x00007f84a563c9c5 in TUnixSystem::DispatchSignals(ESignals) () from /home/sima/Products/root/lib/libCore.so.6.22
#4  <signal handler called>
#5  0x00007f84a322df73 in TBufferFile::ReadFastArray(double*, int) () from /home/sima/Products/root/lib/libRIO.so
#6  0x00007f84a33821bd in int TStreamerInfoActions::VectorLooper::ReadCollectionBasicType<double>(TBuffer&, void*, TStreamerInfoActions::TConfiguration const*) () from /home/sima/Products/root/lib/libRIO.so
#7  0x00007f84a322e574 in TBufferFile::ApplySequence(TStreamerInfoActions::TActionSequence const&, void*) [clone .localalias.106] () from /home/sima/Products/root/lib/libRIO.so
#8  0x00007f8490aa377f in TBranchElement::ReadLeavesMember(TBuffer&) () from /home/sima/Products/root/lib/libTree.so.6.22.00
#9  0x00007f8490a99db5 in TBranch::GetEntry(long long, int) () from /home/sima/Products/root/lib/libTree.so.6.22.00
#10 0x00007f8490aab209 in TBranchElement::GetEntry(long long, int) () from /home/sima/Products/root/lib/libTree.so.6.22.00
#11 0x00007f8490b115b8 in TTree::GetEntry(long long, int) () from /home/sima/Products/root/lib/libTree.so.6.22.00
#12 0x00007f84a5e8a4ee in ?? ()
#13 0x0000000000000000 in ?? ()
===========================================================


does anyone know how can I solve this problem?

ROOT Version: 6.22
Platform: Not Provided
Compiler: Not Provided


How do you execute the .C file ?

root [0] .x f.C

?

root f.C and .x f.C. both of them.

We do not have the ROOT file you are using. We cannot run your macro to try it.
Looking at it, it looks ok.

this is the root file link : https://ufile.io/6inlq68z
the root file is about 17 Meg.

Try this one (it works for me):

void f(){
   TH1D* h = new TH1D("h","h",1000,0,400);
   TFile *f = new TFile("ElMuDATA.root_tree.root","READ");
   TTree *myTree;
   gDirectory->GetObject("tree",myTree);

   std::vector<double> *pt_Leptons=0;
   std::vector<double> *pt_bJets=0;
   TBranch *b;
   TBranch *bb;
   myTree->SetBranchAddress("pt_Leptons", &pt_Leptons, &b);

   for (Long64_t i=1;i < 100 ;i++) {
       myTree->GetEntry(i);
   }
}

I only initialized the pointers.

Thank you very much.

1 Like

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