Hi Dmitryi, Could you try the following variant? TChain *chain = new TChain("Global"); chain->Add("file1.root"); chain->Add("file2.root"); chain->Add("file3.root"); chain->GetEntry(0); //<========new line TFile *newFile = new TFile("small.root","recreate"); TTree *tree = (TTree*)chain->GetTree()->CloneTree(0); //<===new line Rene Brun //<=====Dmitriy Bandourine wrote: > > Thank you Rene! > > With your changes ew tree was created and written to the file "small.root" > But when I tried to read this tree in new session, I got: > *---- > Error in <TFile::ReadBuffer>: error reading all requested bytes from file small.root, got 0 of 26444 > *--- > > When I clicked on some leaf in one of braches of the tree in "small.root", I got > *--- > root [2] <TCanvas::MakeDefCanvas>: created default TCanvas with name c1 > Error in <TFile::ReadBuffer>: error reading all requested bytes from file small.root, got 0 of 26444 > C++ runtime abort: terminate() called by the exception handling mechanism. > *--- > > I expect that the size of the created "small.root" file should be at > least 5-6 times larger than it is. > > - Dmitriy - > > On Fri, 1 Feb 2002, Rene Brun wrote: > > > Hi Dmitriy, > > > > TTree::CloneTree returns a clone of the original object. In case of a TChain, > > CloneTree returns a TChain*. You cannot fill a TChain, only a TTree. > > I suggest the following changes in your code: > > > > > > void filter() > > { > > TChain *chain = new TChain("Global"); > > chain->Add("file1.root"); > > chain->Add("file2.root"); > > chain->Add("file3.root"); > > > > cout << " Creating new root-file ..."<< endl; > > TFile *newFile = new TFile("small.root","recreate"); > > cout << " Creating new tree ..."<< endl; > > TChain *newchain = (TChain*)chain->CloneTree(0); //<==== > > TTree *tree = newchain->GetTree(); //<===== > > > > if (fChain == 0) return; > > Int_t nentries = Int_t(fChain->GetEntries()); > > > > Int_t nbytes = 0, nb = 0; > > for (Int_t jentry=0; jentry<nentries;jentry++) { > > Int_t ientry = LoadTree(jentry); //in case of a TChain, > > nb = fChain->GetEntry(jentry); nbytes += nb; > > > > if ( SelectionFunction() ) tree->Fill(); > > > > }// end loop over events > > > > newFile->cd(); > > tree->Write(); > > } > > > > Rene Brun > > > > Dmitriy Bandourine wrote: > > > > > > Hi rooters, > > > > > > I need to filter out the events containing in the chain > > > of root-files using some function INSIDE the loop over events > > > and then save them in new tree with analogous branch structure. > > > The method with > > > TTree *tree = chain.CloneTree(Tcut); > > > does not suit me because, as mentioned above, I need to use a > > > selection function, which return value depends on the event parameters. > > > > > > My script looks like this: > > > #=========================================== > > > void filter() > > > { > > > TChain *chain = new TChain("Global"); > > > chain->Add("file1.root"); > > > chain->Add("file2.root"); > > > chain->Add("file3.root"); > > > > > > cout << " Creating new root-file ..."<< endl; > > > TFile *newFile = new TFile("small.root","recreate"); > > > cout << " Creating new tree ..."<< endl; > > > TTree *tree = chain.CloneTree(0); > > > > > > if (fChain == 0) return; > > > Int_t nentries = Int_t(fChain->GetEntries()); > > > > > > Int_t nbytes = 0, nb = 0; > > > for (Int_t jentry=0; jentry<nentries;jentry++) { > > > Int_t ientry = LoadTree(jentry); //in case of a TChain, > > > nb = fChain->GetEntry(jentry); nbytes += nb; > > > > > > if ( SelectionFunction() ) tree->Fill(); > > > > > > }// end loop over events > > > > > > newFile->cd(); > > > tree->Write(); > > > } > > > #============================================== > > > > > > For 3.01.06 in this case I get: > > > Warning in <TChain::Fill()>: may not use this method > > > and nothing written to "tree". > > > For 3.02.07 I get: > > > *** Break *** segmentation violation > > > > > > How to initialize properly "tree"? > > > Obviously, the string: > > > TTree *tree = chain.CloneTree(0); > > > does not help > > > > > > Thanks in advance, > > > Dmitri > >
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET