この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
ja:root:ttree [2014/06/11 08:28] kota [読み込むBranchを限定する] |
ja:root:ttree [2016/06/20 05:36] kota [元treeから条件を満たすイベントの、必要なbranchだけを抽出したtreeを作る] |
||
---|---|---|---|
ライン 5: | ライン 5: | ||
==== 読み込むBranchを限定する ==== | ==== 読み込むBranchを限定する ==== | ||
+ | === 方法1 --- SetBranchStatus === | ||
デフォルトではGetEntryすると、SetBranchAddressで指定されていないBranchまでも読みにいく(らしい)。 | デフォルトではGetEntryすると、SetBranchAddressで指定されていないBranchまでも読みにいく(らしい)。 | ||
treeに多数のBranchが入っていて、その一部だけを使う場合、これは非常に無駄になる。 | treeに多数のBranchが入っていて、その一部だけを使う場合、これは非常に無駄になる。 | ||
ライン 34: | ライン 35: | ||
以下 event loop | 以下 event loop | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | === 方法2 --- TBranch::GetEntry=== | ||
+ | TBranch のGetEntryを用いることで、Branch毎に読み込むことができる。\\ | ||
+ | ただし、TChainを用いている時、気をつけなければならないことが1つ。\\ | ||
+ | TBranch::GetEntryは、引数として”当該イベントが含まれているTree内でのEntry Number”を取らなくてはならない。\\ | ||
+ | 具体的には次のようにする。 | ||
+ | <code c> | ||
+ | TChain *chain = ...; | ||
+ | Long64_t localEntry = chain->LoadTree(entry); | ||
+ | branch->GetEntry(localEntry); | ||
+ | </code> | ||
+ | |||
+ | Long64_t TChain::LoadTree(Long64_t entry)は | ||
+ | TChainで結びつけられたTreeの全エントリにつけられた通し番号(entry)を引数にとり、 | ||
+ | そのentryが含まれるTreeをCurrent Treeとし、 | ||
+ | Current Tree内でのEntry Numberを返り値とする関数。\\ | ||
+ | Errorがあると-1~-4の値を返すので、 if( localEntry < 0 ) でError判定ができる。 | ||
+ | |||
+ | ==== 元treeから条件を満たすイベントの、必要なbranchだけを抽出したtreeを作る ==== | ||
+ | |||
+ | |||
+ | TTree* TTree::CopyTree(const char *selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0) | ||
+ | を用いる。 | ||
+ | さらに | ||
+ | void TTree::SetBranchStatus (const char *bname, Bool_t status=1, UInt_t *found=0) | ||
+ | を用いることで、必要なbranchだけを取り出してくることができる。 | ||
+ | |||
+ | |||
+ | TChain* chain = new TChain("T"); | ||
+ | chain->Add(....); | ||
+ | std::string branchname[] = {"brA","br2","br3",...}; | ||
+ | const Int_t nBranch = sizeof(branchname)/sizeof(branchname[0]); | ||
+ | chain->SetBranchStatus("*",0); | ||
+ | for(Int_t iBranch = 0 ; iBranch < nBranch ; ++iBranch) | ||
+ | chain->SetBranchStatus(branchname[iBranch].c_str(),1); | ||
+ | |||
+ | TFile* ofile = new TFile(Form("data/CosmicNCC%d.root",RunNo),"RECREATE"); | ||
+ | TTree* newtree = chain->CopyTree("(DetectorBit&0x4)>0"); | ||
+ | |||
+ | ofile->cd(); | ||
+ | newtree->Write(); | ||
+ | ofile->Close(); |