lasagna lasagna - 23 days ago 8
C++ Question

Why won't this loop load the proper event?

Here is the code I wrote, and it runs fine, but it doesn't seem to actually load the events ... instead, it seems to fill the histograms and the cout with one arbitrary miniscule number.

include "TTree.h"
include "TFile.h"
include "TRandom.h"
include "TTree.h"
include "TCanvas.h"
include "TStyle.h"
include <iostream>
include "TH1.h"
include "TChain.h"

using namespace std;

void readtestsimple(){

TFile *file = TFile::Open("/file/");

TTree* comp_tree = (TTree*)file->Get("_compress_tree");

TChain* compchain = new TChain("_compress_tree");
compchain->Add("/file/");

TH1D *comp = new TH1D("comp","_compression",100,0,0.05);

Double_t _compression, _compressionU, _compressionV, _compressionY, _ch_compression;
Int_t _ch;
comp_tree->SetBranchAddress("_compression",&_compression);
comp_tree->SetBranchAddress("_compressionU",&_compressionU);
comp_tree->SetBranchAddress("_compressionV",&_compressionV);
comp_tree->SetBranchAddress("_compressionY",&_compressionY);

Long64_t nentries = compchain->GetEntries();
Long64_t nb = 0, nbytes = 0 ;

for (Long64_t i=0; i<nentries; i++) {
Long64_t entry = compchain->LoadTree(i);
if (entry <0 ) break;

nb = compchain->GetEntry(i); nbytes += nb;

cout << "entry = " << nb << endl;
cout << "compression = " << _compression << endl;

comp->Fill(_compression);
}

cout << "entries = " << nentries << endl;

}


And the output I get is a long repetition of this:

entry = 36
compression = 1.54768e-316

entry = 36
compression = 1.54768e-316

entry = 36
compression = 1.54768e-316


(and at the end, the correct number of entries).

What do I do to make it read out the correct entry?
Please help!

Answer

It looks you don't need to use the comp_tree pointer at all. The compchain pointer should be enough to access the data in the TTree because TChain is a TTree. Therefore, you should be able to "link" the variables to the chain/tree branches as:

compchain->SetBranchAddress("_compression",&_compression);

instead of

comp_tree->SetBranchAddress("_compression",&_compression);