Trying to extract multiple leafs from a ROOT file to different 1D histogram in another root file

Hi,

I have a ROOT file with one tree and many leaves and sub branches. I can’t seem to reproduce the histograms in the new root file as seen by opening a TBrowser() and inspecting my original root file (MEGA link attached). There is a segfault error too. My code is this:

#include <TH1F.h>
#include <TFile.h>
#include <TList.h>
#include <TTree.h>

//using namespace std;
void extract(){
TFile *f = TFile::Open("flav_Akt4EMPf.root");
f->GetName();
TTree *t1 = (TTree*)f->Get("bTag_AntiKt4EMPFlowJets");
//TFile* fout = new TFile("analysis.root","RECREATE");

vector<float> *jet_mv2c10,*mymc_decayVtx_x,*mymc_decayVtx_y;
float_t truth_PVx,truth_PVy;
   t1->SetBranchAddress("jet_mv2c10",&jet_mv2c10);
   t1->SetBranchAddress("mymc_decayVtx_x",&mymc_decayVtx_x);
   t1->SetBranchAddress("mymc_decayVtx_y",&mymc_decayVtx_y);
   t1->SetBranchAddress("truth_PVx",&truth_PVx);
   t1->SetBranchAddress("truth_PVy",&truth_PVy);

//TList *l   = new TList();
TH1F *c1   = new TH1F("jet_mv2c10","jet_mv2c10",100,-1,1);
TH1F *c2   = new TH1F("mymc_decayVtx_x","jet_mv2c10",100,-1,1);
TH1F *c3   = new TH1F("mymc_decayVtx_y","jet_mv2c10",100,-1,1);
TH1F *c4   = new TH1F("truth_PVx","jet_mv2c10",100,-1,1);
TH1F *c5   = new TH1F("truth_PVy","jet_mv2c10",100,-1,1);

 Long64_t nentries = t1->GetEntries();
   for (Long64_t i=0;i<nentries;i++) {
     t1->GetEntry(i);
     c1->Fill((*jet_mv2c10)[i]);
     c2->Fill((*mymc_decayVtx_x)[i]);
     c3->Fill((*mymc_decayVtx_y)[i]);
     c4->Fill(truth_PVx);
     c5->Fill(truth_PVy);
     
  }
//l->Add(c1);
//l->Add(c2);
//l->Add(c3);
//l->Add(c4);
//l->Add(c5);
//l->Write("analysis", TObject::kSingleKey);
TFile* fout = new TFile("analysis.root","RECREATE");
c1->Write();
c2->Write();
c3->Write();
c4->Write();
c5->Write();
fout->Close();
}

Basically, it’s running into a segfault when I try to run the .c file via root as root -l -b -q extract.C

Any idea where I am going wrong?
My original root file is here
https://mega.nz/file/849xGCDa#sb5rJu13UiT0sGSD8GpzaTjrvQId3gs3Nxz1g12O9IY
extract.C (1.5 KB)
ROOT Version: 6.21.01
Platform: Ubuntu_20.04
Compiler: gcc

Hi,

There is an error in your code. You are probably reading outside the vector sizes:

for (Long64_t i=0;i<nentries;i++) {
  t1->GetEntry(i);
  c1->Fill((*jet_mv2c10)[i]);
}

should not be something like:

for (Long64_t i=0;i < nentries; i++) {
  t1->GetEntry(i);
  for (size_t j = 0; j < (*jet_mv2c10).size(); ++j)
     c1->Fill((*jet_mv2c10)[j]);
}

Lorenzo

I tried it, still doesn’t work. Runs into a segfault.

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