Re: getting values from a TTree

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Fri, 3 Oct 2008 14:51:34 -0500


Hi Roger,

 > I'm trying to read values (energy) from a branch of a TTree into an  > array ( e[i] = (Double_t*)*b_energy->GetEntry(i); ) in the code below:

Why? You are already getting the value via the variable 'energy'. So I am guessing
you simply meant:

    e[i] = energy;

Cheers,
Philippe.

PS. e[i] = (Double_t*)...; is a syntax error since you declare e to be an array
of double and hence this is assigning a pointer to a double to variable of type double
(i.e. 2 incompatible types).

PPS. GetEntry returns the number bytes read from the TTree and _not_ the value of
the data. So if you actually tried *(Double_t*)...GetEntry(), you are then dereferencing
essentially a random pointer

PPPS. Rather than (which does indeed work)

   TFile *f = new TFile("Ca2Mn46d.topaz.20080506.160756.root");    TTree *tr = (TTree*)f->Get("t");
   TBranch *b_energy = tr->GetBranch("energy");    b_energy->SetAddress(&energy);

I recommend:

   TFile *f = new TFile("Ca2Mn46d.topaz.20080506.160756.root");    if (!f || f->IsZombie()) return;
   TTree *tr; f->GetObject("t",tr);
   if (!tr) return;
   TBranch *b_energy;
   tr->SetBranchAddress("energy",&energy,b_energy);

Roger Mason wrote:
> Hello,
>
> I'm trying to read values (energy) from a branch of a TTree into an
> array ( e[i] = (Double_t*)*b_energy->GetEntry(i); ) in the code below:
>
> #include "TFile.h"
> #include "TTree.h"
> #include "TBrowser.h"
> #include "TH2.h"
> #include "TRandom.h"
> #include "TCanvas.h"
>
> void read_tree_minimal()
> {
> static Double_t energy;
> Double_t e[1200];
>
> TFile *f = new TFile("Ca2Mn46d.topaz.20080506.160756.root");
> TTree *tr = (TTree*)f->Get("t");
> TBranch *b_energy = tr->GetBranch("energy");
> b_energy->SetAddress(&energy);
> TH1F *henergy = new TH1F("henergy","energy in eV",10,-2143.62,-2143.54);
>
> Int_t nentries = (Int_t)tr->GetEntries();
> printf("There are %i entries\n",(int*)nentries);
> for (Int_t i=0;i<nentries;i++) {
> b_energy->GetEntry(i);
> e[i] = (Double_t*)b_energy->GetEntry(i);
> henergy->Fill(energy);
> }
>
> printf("Energy # %i = %d\n",1,(float*)e[1]);
> henergy->Draw();
>
> return;
> }
>
> Trying to de-reference the pointer b_energy in this way does not work:
>
> root [3] .x read_tree_minimal.C
> There are 1200 entries
> Error: Illegal pointer operation (tovalue) read_tree_minimal.C:23:
> (Double_t)8.00000000000000000e+00
> *** Interpreter error recovered ***
>
> Can someone please tell me how to do this?
>
> Thanks,
> Roger
>
>
Received on Fri Oct 03 2008 - 21:51:38 CEST

This archive was generated by hypermail 2.2.0 : Sat Oct 04 2008 - 17:50:01 CEST