Reading TBranchElement


_ROOT Version:6.12/04
_Platform:MacOS High Sierra - 10.13.6

Dear all,

When running the following simple code:

void readRoot()
{
    TFile *myfile = new TFile("test.root","READ");
    TTree *tree = (TTree*)myfile->Get("PartInfo");
    std::vector<int> *process = 0;
    tree->SetBranchAddress("CreatorProcessID",&process);
    TH1I *hp = new TH1I("Process","",100,0,1100);
	
    for (int i=0; i<tree->GetEntries(); i++)
        {
            tree->GetEntry(i);
            hp->Fill(process->at(i));
        }
		
	hp->Draw();
}

I am having the following message:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector

I don’t really understand why I cannot read this Branch. Any ideas?

Cheers,

Diego

test.root (774.6 KB)

tree->GetEntry(i);
for (unsigned j = 0; j < process->size(); j++) hp->Fill(process->at(j));

Hello, Thanks for you quick answer.
However, in this case the histogram is not filled. I am expecting the plot attached, done with New TBroser().

CreatorProcessID.pdf (13.5 KB)

TH1I *hp = new TH1I("Process", "", 1100, 0., 1100.);

Is that working for you? It does not for me…

well … that works … i.e. you need to either use the automatic binning or set the binning right.

void readRoot()
{
    TFile *myfile = new TFile("test.root","READ");
    TTree *tree = (TTree*)myfile->Get("PartInfo");
    std::vector<int> *process = 0;
    tree->SetBranchAddress("CreatorProcessID",&process);
    TH1I *hp = new TH1I("Process","",23,0,23);
	
    for (int i=0; i<tree->GetEntries(); i++)
        {
            tree->GetEntry(i);
            for (unsigned j = 0; j < process->size(); ++j) hp->Fill(process->at(j));
        }
		
    hp->Draw();
}

Ok, now I see the error! The first loop “for” missing. Thanks, it is working now.

Cheers,

Diego