gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:25 25 4
gpt4 key购买 nike

这是我写的代码,它运行良好,但它似乎并没有真正加载事件……相反,它似乎用一个任意的极小数字填充直方图和 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 中的数据,因为 TChain 是一个 TTree。因此,您应该能够将变量“链接”到链/树分支,如下所示:

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

代替

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

关于c++ - 为什么这个循环不加载正确的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40634371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com