PruneTuple.C

void PruneTuple(char* chainStem, UInt_t maxPerFile=200000) {
// Concatenate AnalysisNtuple ntuple, applying cuts on active distance and
// number of tracks
//
// Example of usage: from CINT -
// .L PruneTuple.C
// PrunteTuple("IndividualRuns/nt*.root")
//
// output is wired to be ntuple-prune.root for now

/// array of doubles for ntuple branches
double anaTup[1000];

//Setup the chain - out ntuple has "1" as tree name.
// get the list of branches from the tree and do the SetBranchAddress

TChain* c = new TChain("1");
c->Add(chainStem);

TObjArray* bList = c->GetListOfBranches();
for (int ib=0; ib<bList->GetEntries(); ib++) {
  const char* name = bList->At(ib)->GetName();
  c->SetBranchAddress(name,&anaTup[ib]);
}

//Create a new file + a clone of old tree in new file
TFile *newfile = new TFile("ntuple-prune.root","new");

// copy the tree entries that pass the cuts
TTree *newtree = c->CopyTree("AcdActiveDist < -20 && TkrNumTracks > 0");

newtree->Print();
newfile->Write();
printf("finished procesing full tree..now splitting if necessary\n");

if (newtree->GetEntries() > maxPerFile) {
  UInt_t numEntries = newtree->GetEntries();
  UInt_t numTrees = Int_t(numEntries/maxPerFile);
  if (numTrees*maxPerFile < numEntries) ++numTrees;
  UInt_t i;
  for (i=0; i<numTrees; i++) {
    Int_t total = maxPerFile;
    if (i==numTrees-1) total = numEntries-i*maxPerFile;
    Int_t first = 0 + i*maxPerFile;
    char filename[50];
    sprintf(filename, "%s%d%s", "ntuple_prune", i, ".root");
    printf("processing %d tree starting evt = %d tot = %d\n", i, first, total);
    TFile *file=new TFile(filename,"new");
    TTree *t = newtree->CopyTree("","", total, first);
    file->Write();
    delete file;
  }
}
delete newfile;
}