I can print a value but I can't store in a variable

I have this code that accesses certain values and I can print them. I want to do math with this values and for that I want to store them in a vector, but for the life of me I can’t make it work.

The vectors are filled in a “for” loop, if I just put there one vector, there seems to be no problem, until I try to do anything with the values on that vector, when I get “bus error” and it “generates a stack trace…”, but I’m not sure what that means.
If I put two or more vectors to be filled I get bus error again.

I signaled where the vectors should be filled in the code in capital letters, is almost at the end.

Thanks a lot

// All libraries you could wish for
#include <iostream>                 // class for inputoutput streams
#include <stdio.h>                  // printf, scanf, puts, NULL
#include <stdlib.h>                 // srand, rand, atof
#include <time.h>                   // time
#include <fstream>                  // class for inputoutput file streams
#include <dirent.h>
#include <string.h>           // class to manipulate strings
#include <sstream>                    // class for parsing strings
#include <vector>


#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1F.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TPaveStats.h>
#include <TPad.h>
#include <TMath.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TLegend.h>
#include <TString.h>
#include <TGraph.h>
#include <TLatex.h>
#include <TLorentzVector.h>


#include <unistd.h>
#define GetCurrentDir getcwd

#include <TTree.h>
#include <TClonesArray.h>
#include "Math/SMatrix.h"

using namespace std;

int main(int argc, char* argv[])
{ 
  //Upload the file with the data
  TFile* file = TFile::Open("/Users/Fer/Documents/traajo/samples/NeroNtuples_9.root"); // TFile::Open() instead of a constructor since it works over xrootd etc.
  //Upload the tree with the event data
  TTree *tree=(TTree*)file->Get("nero/events");

  //Create the vector to store all the particle identifiers
  std::vector<Int_t> * lepPdgId;

  //Create a variable to store all the lepton event data
  TClonesArray *leptondata = new TClonesArray("leptondata");

  //Specify where all the lepton event data will be stores
  tree->SetBranchAddress("lepP4", &leptondata);
  //Specify where all the lepton identifiers will be stored
  tree->SetBranchAddress("lepPdgId", &lepPdgId);

  //Get how many events we have to loop through
  int nentries = tree->GetEntries();

  std::vector<Double_t> * px;  
  std::vector<Double_t> * py;
  std::vector<Double_t> * pz;
  std::vector<Double_t> * energy;

  //Loop through all the events
  for(int ientry = 0; ientry < nentries; ientry++) 
  {
    //Reset the lepton data 
    leptondata->Clear();
    //This line stores the proper data both in "leptondata" and in "lepPdgId"
    tree->GetEntry(ientry);
    
    //Only if "leptondata" is not empty continue, this is to avoid segmentation errors
    if(leptondata->GetSize() == 0) continue;

    //Loop through all the entries in the current event 
    for(int j=0; j<leptondata->GetEntriesFast()-1; j++) 
    {
        //Only if the identifier of the particle is + or - 11 (electron or antielectron) store the data in electrondata
        if(abs(lepPdgId->at(j))==11) continue;
        //Store all the data of the electron in this variable
        TLorentzVector *electrondata = (TLorentzVector *)leptondata->At(j);
        //Store the momentum and the energy in separate vectors
        //
        //HERE IS THE PROBLEM
        //
        px->at(j)=electrondata->Px();
        //py->at(j)=electrondata->Py();
        //pz->at(j)=electrondata->Pz();
        //energy->at(j)=electrondata->E();
        //Get some specific property such as momentum, position or energy
        cout << electrondata->E() << endl;
         }
  }
  return 0; 
}

std::vector<Int_t> * lepPdgId = 0; And there is a problem with your “px”, “py”. “pz” and “energy” vectors -> you nowhere create them. You could try somehting like this maybe: [code]// All libraries you could wish for
#include // class for inputoutput streams
#include <stdio.h> // printf, scanf, puts, NULL
#include <stdlib.h> // srand, rand, atof
#include <time.h> // time
#include // class for inputoutput file streams
#include <dirent.h>
#include <string.h> // class to manipulate strings
#include // class for parsing strings
#include

#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1F.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TPaveStats.h>
#include <TPad.h>
#include <TMath.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TLegend.h>
#include <TString.h>
#include <TGraph.h>
#include <TLatex.h>
#include <TLorentzVector.h>

#include <unistd.h>
#define GetCurrentDir getcwd

#include <TTree.h>
#include <TClonesArray.h>
#include “Math/SMatrix.h”

using namespace std;

int main(int argc, char* argv[])
{
//Upload the file with the data
TFile* file = TFile::Open("/Users/Fer/Documents/traajo/samples/NeroNtuples_9.root"); // TFile::Open() instead of a constructor since it works over xrootd etc.
//Upload the tree with the event data
TTree tree=(TTree)file->Get(“nero/events”);

//Create the vector to store all the particle identifiers
std::vector<Int_t> * lepPdgId = 0;

//Create a variable to store all the lepton event data
TClonesArray *leptondata = new TClonesArray(“leptondata”);

//Specify where all the lepton event data will be stores
tree->SetBranchAddress(“lepP4”, &leptondata);
//Specify where all the lepton identifiers will be stored
tree->SetBranchAddress(“lepPdgId”, &lepPdgId);

//Get how many events we have to loop through
int nentries = tree->GetEntries();

std::vector<Double_t> * px = new std::vector<Double_t>;
std::vector<Double_t> * py = new std::vector<Double_t>;
std::vector<Double_t> * pz = new std::vector<Double_t>;
std::vector<Double_t> * energy = new std::vector<Double_t>;

//Loop through all the events
for(int ientry = 0; ientry < nentries; ientry++)
{
//Reset the lepton data
leptondata->Clear();
//This line stores the proper data both in “leptondata” and in "lepPdgId"
tree->GetEntry(ientry);

//Only if "leptondata" is not empty continue, this is to avoid segmentation errors
if(leptondata->GetSize() == 0) continue;

px->clear();
py->clear();
pz->clear();
energy->clear();

//Loop through all the entries in the current event
for(int j=0; j<leptondata->GetEntriesFast()-1; j++)
{
    //Only if the identifier of the particle is + or - 11 (electron or antielectron) store the data in electrondata
    if(abs(lepPdgId->at(j))==11) continue;
    //Store all the data of the electron in this variable
    TLorentzVector *electrondata = (TLorentzVector *)leptondata->At(j);
    //Store the momentum and the energy in separate vectors
    //
    //HERE IS THE PROBLEM
    //
    px->push_back(electrondata->Px());
    py->push_back(electrondata->Py());
    pz->push_back(electrondata->Pz());
    energy->push_back(electrondata->E());
    //Get some specific property such as momentum, position or energy
    cout << electrondata->E() << endl;
     }

}

// cleanup
delete px;
delete py;
delete pz;
delete energy;
delete file; // automatically deletes “tree”, too
delete lepPdgId;
delete leptondata;

return 0;
}[/code]