How to fill the data of a pointer- vector of a tree into a histogram

The header file of my tree fie has these kinds of variables:

vector<float> *jet_pt

My code is :

vector<float> *ptr  =nullptr;
TFile *f= new TFile(g+“myfile.root”);
TTree *t = (TTree *) f ->Get(“mytree”);
TH1F *h=new TH1F(“h”,“h”,10,0,100);

tree->SetBranchStatus(“jet_pt”, 1);
tree->SetBranchAdress(“jet_pt” ,&ptr);

Int_t N =t->GetEntries();
for Int_t i=0; i<N ;i++ {
 for (unsigned int j=0; i<ptr->size()

when i run this code terminal says :segmentation violation
What can i do to fill my histogram with the data of jet_pt variable?
P…S I want to do it with this kind of method(SetBranch) not with TTreeReader!
Cheers ,


  TFile *f = TFile::Open("myfile.root");
  if ((!f) || f->IsZombie()) { delete f; return; }
  TTree *t; f->GetObject("mytree", t);
  if (!t) { delete f; return; }
  std::vector<float> *ptr = 0;
  // t->SetBranchStatus("jet_pt", 1);
  t->SetBranchAdress("jet_pt", &ptr);
  if (!ptr) { delete f; return; }
  TH1F *h = new TH1F("h", "h", 10, 0, 100);
  Long64_t N = t->GetEntries();
  for (Long64_t i = 0; i < N; i++) {
    for (unsigned long j = 0; j < ptr->size(); j++) {
      h->Fill( (*ptr)[j] ); // (*ptr)[j] ... or ... ptr->at(j)
  delete f; // automatically deletes "t", too
  delete ptr;

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

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