Code works but still gives error mesage

My code works, but I don’t like that every time I run it I get:

Error in <TClonesArray::SetClass>: called with a null pointer

Which I’m not sure what it means, I suspect it means that the pointer is empty when I create it, but that shouldn’t be a problem.

Here is my code, its purpose is to fill a histogram with the mass of leptons in a collision (i omited the libraries):

int main(int argc, char* argv[])
{ 

 //Essentials
 
TFile *f=new TFile("his1.root","read");

  //Upload the file with the data, make sure the adress of the file matches the one in your computer
  TFile* file = TFile::Open("/Users/Fer/Documents/traajo/samples/NeroNtuples_9.root"); 
  
  //Upload the tree with the event data
  TTree *tree=(TTree*)file->Get("nero/events");

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


  //Histogram variables

  //Create the canvas were the histograms will be ploted
  TCanvas* c1 = new TCanvas("c1", "Masses", 600, 600);

  //Histogram to plot the distribution of electron mass 
  TH1F *emass = new TH1F("emass", "Electron mass", 50, 0, 150);


  //Variables for the for loop

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

  //Create a variable to store the mass values
  Double_t mass;

  //Loop through all the events
  for(int ientry = 0; ientry < 100; ientry++) 
  {
    //Reset the lepton data 
    leptondata->Clear();

    //This line stores the proper data in "leptondata", in "lepPdgId", and "metdata"
    tree->GetEntry(ientry);

    //We cannot use math with pointers for some reason, so we create a lorentz vectors that isn't a pointers
    TLorentzVector addable_lorentz_metdata = (TLorentzVector) leptondata->At(0);

    //Get the invariant transverse mass of that lorentz vector
    mass=addable_lorentz_leptondata.Mt();
  
          //Fill the histogram with the current data
        emass->Fill(mass);
  }


  //Make the histogram
  emass->Draw("H");

  //Put it in the canvas
  c1->Update();


  //Save the image
  c1->SaveAs("masses.pdf");
  c1->Close();

  etmass->Write();
  f->Close();


  // cleanup
  delete file; // automatically deletes "tree" too
  delete leptondata;
  return 0; 
}

Thanks a lot

new TClonesArray("leptondata");What is ‘leptondata’ here? The argument of the TClonesArray constructor is expected to be the class name described the content type. I.e. I think you may have meant:

[quote] //We cannot use math with pointers for some reason, so we create a lorentz vectors that isn’t a pointers
TLorentzVector addable_lorentz_metdata = (TLorentzVector) leptondata->At(0);
[/quote]

You should be able to use arithmetic even when you have pointer to TLorentzVector … However the syntax is a bit cumbersome: (*ptr1) + (*ptr2) .

You may want to have an unnecessary copy by using references:

(Note that the cast from At(0) directly to non-pointer is incorrect and should lead to compilation failure (and/or undefined behavior).

Cheers,
Philippe.