为什么这个循环不加载正确的事件?

Why won't this loop load the proper event?

这是我写的代码,它运行良好,但它似乎并没有真正加载事件......相反,它似乎用一个任意的小数字填充直方图和 cout。

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;

}

我得到的输出是一个很长的重复:

entry = 36
compression = 1.54768e-316

entry = 36
compression = 1.54768e-316

entry = 36
compression = 1.54768e-316

(最后是正确的条目数)。

我该怎么做才能让它读出正确的条目? 请帮忙!

看来您根本不需要使用 comp_tree 指针。 compchain 指针应该足以访问 TTree 中的数据,因为 TChainTTree。因此,您应该能够 "link" 将变量 chain/tree 分支为:

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

而不是

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