Ciao,
and my original test was in order to “skim” the file removing some events and also some branches (that is a ‘variation’ of the original copytree example). Here’s my code:
#include <unistd.h>
#include <TFile.h>
#include <TTree.h>
#include <TSystem.h>
#include <../../test/Event.h>
void copytree_mio() {
// Example of Root macro to copy a subset of a Tree to a new Tree
// The input file has been generated by the program in $ROOTSYS/test/Event
// with Event 1000 1 1 1
//Author: Rene Brun
gSystem->Load("$ROOTSYS/test/libEvent");
//Get old file, old tree and set top branch address
TFile *oldfile = new TFile("$ROOTSYS/test/Event.root");
TTree *oldtree = (TTree*)oldfile->Get("T");
// oldtree->GetEntry(0);
printf("Entries=%d\n", (int)oldtree->GetEntries());
printf("GetMakeClass = %d\n", oldtree->GetMakeClass());
oldtree->SetMakeClass(1);
oldtree->SetBranchStatus("*", 0);
// oldtree->SetBranchStatus("event",1);
oldtree->SetBranchStatus("fNtrack",1);
oldtree->SetBranchStatus("fNseg",1);
// oldtree->SetBranchStatus("fNvertex", 1);
//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile("small.root","recreate");
TTree *newtree = oldtree->CloneTree(0);
// oldtree->SetBranchStatus("event",1);
oldtree->SetBranchStatus("fNvertex", 1);
Int_t Nvertex;
oldtree->SetBranchAddress("fNvertex", &Nvertex);
TBranch* brfNvertex = oldtree->GetBranch("fNvertex");
printf("%p\n", brfNvertex);
Int_t Ntrack;
oldtree->SetBranchAddress("fNtrack", &Ntrack);
TBranch* brfNtrack = oldtree->GetBranch("fNtrack");
printf("%p\n", brfNtrack);
Int_t Nseg;
oldtree->SetBranchAddress("fNseg", &Nseg);
TBranch* brfNseg = oldtree->GetBranch("fNseg");
printf("%p\n", brfNseg);
// Event *event = new Event();
// oldtree->SetBranchAddress("event",&event);
for (int ii=0; ii<oldtree->GetEntries(); ii++) {
// oldtree->GetEntry(ii);
brfNvertex->GetEntry(ii);
brfNtrack->GetEntry(ii);
brfNseg->GetEntry(ii);
// if (event->GetNvertex()>12) {
// printf("%d %d\n", Nvertex, event->GetNvertex());
printf("%d\n", Nvertex);
printf("%d %d\n", Ntrack, Nseg);
if (Nvertex>12) {
newtree->Fill();
}
}
newtree->Print();
newfile->Write("Overwrite");
delete oldfile;
delete newfile;
}
I spent some time to find a combinations (and an order) of istructions to make the stuff working… Now I read the correct things and I stream on file the desired ones correctly:
...
10
604 6033
19
595 5933
******************************************************************************
*Tree :T : An example of a ROOT tree *
*Entries : 395 : Total = 9280 bytes File Size = 0 *
* : : Tree compression factor = 1.00 *
******************************************************************************
*Branch :event *
*Entries : 395 : BranchElement (see below) *
*............................................................................*
*Br 0 :fNtrack : Int_t *
*Entries : 395 : Total Size= 2224 bytes One basket in memory *
*Baskets : 0 : Basket Size= 51200 bytes Compression= 1.00 *
*............................................................................*
*Br 1 :fNseg : Int_t *
*Entries : 395 : Total Size= 2212 bytes One basket in memory *
*Baskets : 0 : Basket Size= 51200 bytes Compression= 1.00 *
*............................................................................*
*Br 2 :TRefTable : List of branch numbers with referenced objects *
*Entries : 395 : Total Size= 5102 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
and than if I read small.root:
root [1] gSystem->Load("$ROOTSYS/test/libEvent");
root [2] T->Scan("fNtrack")
************************
* Row * fNtrack *
************************
* 0 * 600 *
* 1 * 589 *
* 2 * 597 *
* 3 * 602 *
* 4 * 603 *
* 5 * 608 *
* 6 * 604 *
* 7 * 596 *
* 8 * 605 *
* 9 * 602 *
* 10 * 598 *
* 11 * 612 *
* 12 * 598 *
* 13 * 601 *
* 14 * 605 *
* 15 * 589 *
* 16 * 593 *
* 17 * 600 *
* 18 * 609 *
* 19 * 591 *
* 20 * 599 *
* 21 * 603 *
* 22 * 591 *
* 23 * 599 *
* 24 * 596 *
Type <CR> to continue or q to quit ==> q
************************
(Long64_t)25
(different ordering of the various SetBranchAddress, CloneTree, etc… were giving a wrong reading from the input file and/or a wring streaming on the output one.
If I uncomment:
Event *event = new Event();
oldtree->SetBranchAddress("event",&event);
I read just rubbish (uninitialized values) or at least this is what I print, but, really puzzling me, I stream on the small.root output file the right things! Really I’m unable to understand…
Can you help me in understand a little bit?
Thanks,
Matteo