Ok, you do have events with more than 1000 particles, setting array length to e.g. 10 000 works for me w/o any seg. faults.
P.S. I also changed the type of PId
, it should be Int_t
instead of Float_t
.
#include <stdio.h>
#include <iostream>
#include "TFile.h"
#include <fstream>
#include "TTree.h"
#include "TH2F.h"
#include "TCanvas.h"
#include "TBranch.h"
#include "TLeaf.h"
#include "TTreeReader.h"
using namespace std;
void test()
{
Int_t np;
Int_t PId[10000];
Float_t Px[10000], Py[10000], Pz[10000];
TFile *f = new TFile("DAT000002.root");
TTree *tr = (TTree*)f->Get("cor");
TBranch *b = (TBranch*)tr->GetBranch("Particle");
b->GetLeaf("np")->SetAddress(&np);
b->GetLeaf("PId")->SetAddress(PId);
b->GetLeaf("Px")->SetAddress(Px);
Int_t nevent = tr->GetEntries();
cout<<"Total numer of events: "<<nevent<<endl;
for(Int_t i=0; i<nevent; i++){
cout<<"Event: "<<i+1<<endl;
tr->GetEntry(i);
cout<<"Number of particles: "<<np<<endl;
for(int j=0; j<np; j++){
cout<<" Particle: "<<j+1<<endl;
cout<<" PID: "<<PId[j]<<" Px: "<<Px[j]<<endl;
if(PId[j]==6){
cout<<"My PID is 6"<<endl;
}
}
}
cout<<" end "<<endl;
}
The previous seg. fault I observed was caused by the small array length. And it was breaking at the exit of the function after cout<<" end "<<endl;
. While reading the data was fine, except some missing particles due to concatenation at 1000