Print Branch with array data

Hi, I want to Print a array in Branch but it Print the address!
``TFile *myfile= new TFile(“ttbar.root”,“READ”);
//TTree tree= (TTree myfile)->Get(“LHEF”);
TTree *tree = (TTree *) myfile->Get(“LHEF”);

using namespace std;
Double_t Px[12], Py[12], Pz[12];
Int_t PID[12];
Double_t E[12];
tree->SetBranchAddress(“Particle.Px”, Px);
tree->SetBranchAddress(“Particle.Pz”, &Pz);
tree->SetBranchAddress(“Particle.Py”, &Py);
tree->SetBranchAddress(“Particle.PID”, &PID);
tree->SetBranchAddress(“Particle.E”, &E);
Long64_t nentries = tree->GetEntries();
for (int j=0; j<nentries; j++){
tree->GetEntry(j);
cout << Px << “\n”;}
___
Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18
Platform: Not Provided
Compiler: Not Provided


image

image

Hi Sima,

try dereferencing:

cout << *Px << "\n";

Hi yus

it didn’t work.
input_line_72:4:13: error: indirection requires pointer operand (‘Long64_t’ (aka ‘long long’) invalid)
cout << *Number << “\n”;}
^~~~~~~

What is Number? You did not have it in your example above. Could you maybe show your final code and your ROOT file?


image

I made another code using https://root.cern/doc/master/tree2_8C.html

TFile *myfile= new TFile(“ttbar.root”,“READ”);
//TTree tree= (TTree myfile)->Get(“LHEF”);
TTree *tree = (TTree *) myfile->Get(“LHEF”);
Double_t Px[12];
TBranch *bPx = tree->GetBranch(“Particle.Px”);
bPx->SetAddress(&Px);
TH1F *hdestep = new TH1F(“hdestep”,“destep in Mev”,100,1e-5,3e-5);
Long64_t nentries = tree->GetEntries();
for (Long64_t i=0;i<nentries;i++) {
bPx->GetEntry(i);
//hdestep->Fill(Px);
cout << Px << “\n”;
}

result:

In the tree2_8C.html, the variable they try to histogram is a static Float_t. In your example it’s an array of Double_t's.

@yus Can you help me write a correct code?

Sure, but to do that I’ll need your input ttbar.root.

it’s the link to Download the root file:
https://www.udrop.com/7zw/ttbar.root

Hi Sima,

I figured out how to print these Particle.Px values. Not with TTree::SetBranchAddress() functionality though, but with TTreeReader one. Hope that’s okay:

        TFile* myfile = TFile::Open("ttbar.root");

        TTreeReader myReader("LHEF", myfile);
        TTreeReaderArray<Double_t> particlePx(myReader, "Particle.Px");

        while (myReader.Next())
        {
                if (myReader.GetCurrentEntry() % 10000 == 0)
                {
                        printf("\tProcessing entry #%05lli out of %05lli: %lu particle(s)...\n", myReader.GetCurrentEntry(), myReader.GetEntries(false), particlePx.GetSize());
                        for (unsigned short pxIt = 0; pxIt < particlePx.GetSize(); ++pxIt)
                                printf("\t\tParticle #%02u: Px = %f\n", pxIt, particlePx[pxIt]);
                }
        }
        myfile->Close();

thanks you very much. it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.