Segmentation violating from GetEntry

I have not used MakeClass for this macro. I just have a root file (I have checked it is not empty) and I want to make a new root file with histograms that have the weights applied. I have written the following:

void applyCuts(){

TFile *_file0 = TFile::Open("unweighted.root");

 TFile hfile("weighted.root","RECREATE");
float yStar, mjj, weight;
vector<float> *jet_eta, *jet_pt;
TTree *t1 = (TTree*)_file0->Get("outTree");
   t1->SetBranchAddress("mjj", &mjj);
   t1->SetBranchAddress("weight", &weight);
   t1->SetBranchAddress("yStar", &yStar);
   t1->SetBranchAddress("jet_eta", &jet_eta);
  Int_t nentries = (Int_t)t1->GetEntries();
   t1->SetBranchAddress("jet_pt", &jet_pt);

std::cout<<"vector size is "<< jet_pt->size()<<endl;

TH1F *mass = new TH1F("mass", "mass", 100,0,8000);
//  TH1F *mjj = (TH1F*)_file0->Get("mjj");
  TH1F *h_weight = new TH1F("weight", "weight", 100,0,1);
  TH1F *h_yStar = new TH1F("yStar", "yStar",100, 0, 0.8);
  TH1F *h_jet_eta = new TH1F("eta", "eta", 100, 0, 5);

for(int i=0; i<nentries;i++){
t1->GetEntry(i);
std::cout<<"Check 1"<<endl;
std::cout<<"vector size is "<< jet_pt->size()<<endl;
 
 for (unsigned long j = 0; j <jet_pt->size(); j++) {
std::cout<<"Check 2"<<endl;
   
   if(yStar<0.6 && abs(jet_eta->at(j))<2.1){
std::cout<<"Check 3"<<endl;
        mass->Fill(mjj,weight);


}

}

The vector size output is fine. Then, if I comment out the t1->GetEntry(i); line, I get an immediate seg violation. If I leave it in it outputs Check2 and Check3 for many lines, then I get a seg violation. I guess at the point when it tries to move to i=1 or something, rather than just randomly in the middle of the first vector.

I’ve also just found that jet_eta and jet_pt are vectors of different length, shouldn’t they be the same size? I’m also curious to see that the eta size seems to change every time I re-run the macro (std::cout<<"vector size is "<< jet_eta->size()<<endl;).

Thanks for any help!


Please read tips for efficient and successful posting and posting code

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


std::vector<float> *jet_eta = 0, *jet_pt = 0;

1 Like

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