1
이 코드는 이미 생성 된 트리를 읽고 해당 분기 이름을 복사하고 0보다 작거나 같은 항목의 경우 0을 기록하고 다른 모든 항목의 경우 1을 기록합니다. 코드는 분기를 잘 복사하지만, 정보로 채울 때 각 분기를 채우고 그 전에 모든 분기의 모든 정보를 채 웁니다. 따라서 트리가 10 개의 엔트리와 10 개의 브랜치를 가지면 10 번째 브랜치는 100 개의 엔트리와 9 번째 90 등을 가질 것입니다. 아래는 전체 코드이며, 마지막 루프에서 채우기가 수행됩니다.CERN ROOT : 루프 문제로 TBranches/TTree 채우기
#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 temp = Form ("entryVec [% d]/I", i);'한 줄로 다시 쓸 수 있습니다. 'Form' 함수는'C'의'printf'와 같은 문법을 사용합니다. (구글의 사용법에) – pseyfert