gpt4 book ai didi

c++ - CERN 根目录 : Filling TBranches/TTree in a loop issue

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

代码应该读取一棵已创建的树,复制其分支名称并为小于或等于 0 的条目记录 0,为所有其他条目记录 1。代码很好地复制了分支,但是当要用它填充的信息填充它们时,每个分支将包含它之前所有分支的所有信息。因此,如果一棵树有 10 个条目和 10 个分支,则第十个分支将有 100 个条目,第 9 个分支将有 90 个条目,等等。下面是完整代码,填充在最后一个循环中完成。

#include <vector>
binaryTree()
{
//Creation and naming scheme
TString fileName = "binaryTree2.root";//name of file wishing to create.
TString treeName = "Binary Tree 2";//name of tree wishing to create.
TFile *file = new TFile(fileName, "RECREATE");
TTree *bTree = new TTree(treeName,"Binary Tree");

//Connection scheme
TString fileNameCopy = "hodoscopehittree7.root";//name of file you will be accessing.
TString treeNameCopy = "tree";//Name of tree within file you are accessing.
TFile *filePtr = new TFile(fileNameCopy);//points to file with previously created tree
TTree *treePtr = (TTree *) filePtr->Get(treeNameCopy);//Ptr to tree within accessed file.
TObjArray *obj = treePtr->GetListOfBranches();//Ptr to branches.
int branchNum = obj->GetEntries();//Number of branches in accessed tree.

//Vector to hold all of the information from the tree.
vector<vector<int>> dataHolder;

int* inHist;//Ptr to become the entry.
int inVal;
vector <int> entryVec;//Vector of ints that the branches will rely on.
entryVec.resize(branchNum);
TString branchName;
const int entryNum = treePtr->GetEntries();//Number of entries in each branch.

//This loop creates a branch in the binary tree with the same name as the
//branch in the tree being accessed and fills the dataHolder vector with
//vectors.
for (int i = 0; i < branchNum; i++)
{
TString temp;
temp = "entryVec[";
temp += (i);
temp += "]/I";
branchName = obj -> At(i)-> GetName();
bTree -> Branch(branchName, &entryVec[i],temp);
vector <int> tempVec;
dataHolder.push_back(tempVec);
}

//This loop reads the entries of each branch within the accessed tree. If the
//value is less than or equal to zero, 0 is added to the dataHolder and if
//not 1 is added to the dataHolder.
for (int i = 0; i < branchNum; i++)
{
branchName = obj-> At(i)-> GetName(); //Gets name of branch at index i
treePtr -> SetBranchAddress(branchName, &inHist);

for (int j = 0; j < entryNum; j++)
{
treePtr -> GetEntry(j);
inVal = inHist;

if (inVal <= 0)
{
dataHolder[i].push_back(0);
}

else
{
dataHolder[i].push_back(1);
}
}

}

//This loop fills the tree. Each inner loop reads the jth element of the
//datHolder and inputs that information int the entryVec. The outer loop fills
//the tree and then loops over all of the entries.
for (int i = 0; i < entryNum; i++)
{
for (int j = 0; j < branchNum; j++)
{
entryVec[j] = dataHolder[j][i];
}
bTree -> Fill();
}

file -> Write();
cout << "Your program has finished; " << treeName << " has been created.";
cout << endl;
filePtr-> Close();
new TBrowser();
}

最佳答案

解决方案是由与我合作的其他人解决的。分支创建临时的最后一部分应该是 branchName/I 所以当更改为:

temp = 分支名称;temp += "/I";

代码完美运行。

关于c++ - CERN 根目录 : Filling TBranches/TTree in a loop issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592146/

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