C++ CERN根目录:在循环问题中填充分支/树

C++ CERN根目录:在循环问题中填充分支/树,c++,tree,root-framework,C++,Tree,Root Framework,代码应该读取已创建的树,复制其分支名称,并为小于或等于0的条目记录0,为所有其他条目记录1。代码可以很好地复制分支,但是当要用它填充的信息填充分支时,每个分支都会复制它之前所有分支的所有信息。因此,如果一棵树有10个条目和10个分支,那么第10个分支将有100个条目,第9个90条等等。下面是完整的代码,填充在最后一个循环中完成 #include <vector> binaryTree() { //Creation and naming scheme TString fileName

代码应该读取已创建的树,复制其分支名称,并为小于或等于0的条目记录0,为所有其他条目记录1。代码可以很好地复制分支,但是当要用它填充的信息填充分支时,每个分支都会复制它之前所有分支的所有信息。因此,如果一棵树有10个条目和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();
}
#包括
二叉树()
{
//创建和命名方案
TString fileName=“binaryTree2.root”;//要创建的文件名。
TString treeName=“Binary Tree 2”//要创建的树的名称。
TFile*file=新的TFile(文件名,“重新创建”);
TTree*bTree=新的TTree(树名,二叉树);
//连接方案
TString fileNameCopy=“hodoscopehittree7.root”;//您将要访问的文件名。
TString treenaecopy=“tree”;//正在访问的文件中的树的名称。
TFile*filePtr=new-TFile(fileNameCopy);//指向具有先前创建的树的文件
TTree*treePtr=(TTree*)filePtr->Get(treeNameCopy);//Ptr到被访问文件中的树。
TObjArray*obj=treePtr->GetListOfBranches();//Ptr到分支。
int branchNum=obj->GetEntries();//访问树中的分支数。
//Vector保存树中的所有信息。
矢量数据保持器;
int*inHist;//Ptr将成为条目。
因瓦利;
vector entryVec;//分支将依赖的整数的向量。
入口向量调整大小(分支);
TString branchName;
const int entryNum=treePtr->GetEntries();//每个分支中的条目数。
//此循环在二叉树中创建一个与
//正在访问的树中的分支,并用
//向量。
for(int i=0;iAt(i)->GetName();
B树->分支(分支名称和入口向量[i],临时);
向量tempVec;
数据保持器。推回(tempVec);
}
//此循环读取所访问树中每个分支的条目
//值小于或等于零,将0添加到数据保持器,如果
//未将1添加到数据保持器。
for(int i=0;iAt(i)->GetName();//获取索引i处的分支名称
treepter->SetBranchAddress(branchName和InList);
对于(int j=0;jGetEntry(j);
inVal=inHist;
如果(无效填充();
}
文件->写入();

cout解决方案由我合作的其他人解决。分支创建温度的最后一部分应为branchName/I,因此当更改为:

温度=分支名称; 温度+=“/I”


代码工作正常。

解决方案由我合作的其他人解决。分支创建温度的最后一部分应为branchName/I,因此当更改为:

温度=分支名称; 温度+=“/I”


代码工作得非常完美。

您可能希望在一行中重写
TString temp=Form(“entryVec[%d]/I”,I);
Form
函数使用与
C
中的
printf
相同的语法(要搜索其用法),您可能希望在一行中重写
TString temp=Form(“entryVec[%d]/I”),I)
表单
函数使用与
C
中的
printf
相同的语法(用谷歌搜索其用法)