ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/copyTreeSubset.C
Revision: 1.1
Committed: Fri Mar 6 23:16:10 2009 UTC (16 years, 1 month ago) by anderson
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Copies entries in TTree that pass cuts to new file

File Contents

# Content
1 /*************************************************
2 This script copies only a subset of a TTree
3 to a new TTree.
4
5 Give a list of files, and a list of cuts.
6 A new file will be created for every original
7 file.
8
9 Michael B. Anderson
10 March 6, 2009
11 *************************************************/
12
13 void copyTreeSubset() {
14 //********************************************************************
15 //**** Variables ****//
16 // List of files to copy TTree from
17 vector<TString> fileName;
18 fileName.push_back( "QCD_Pt20to30-fakePhotons.root" );//file0
19 fileName.push_back( "QCD_Pt30to80-fakePhotons.root" );//file1
20 fileName.push_back( "QCD_Pt80to170-fakePhotons.root" );//file2
21 fileName.push_back( "QCD_pt170-fakePhotons.root" );//file3
22 fileName.push_back( "QCD_pt300-fakePhotons.root" );//file4
23 fileName.push_back( "QCD_pt470-fakePhotons.root" );//file5
24 fileName.push_back( "QCD_pt800-fakePhotons.root" );//file6
25
26 TString treeName = "TreePhotonAll";
27
28 // Each cut is applied only to one file
29 vector<TString> fileCuts;
30 fileCuts.push_back( "genEventScale<30" ); //file0
31 fileCuts.push_back( "genEventScale>30&&genEventScale<80" );//file1
32 fileCuts.push_back( "genEventScale>80&&genEventScale<170" );//file2
33 fileCuts.push_back( "genEventScale>170&&genEventScale<300" );//file3
34 fileCuts.push_back( "genEventScale>300&&genEventScale<470" );//file4
35 fileCuts.push_back( "genEventScale>470&&genEventScale<800" );//file5
36 fileCuts.push_back( "genEventScale>800" ); //file6
37 //**** END of Variables ****//
38 //********************************************************************
39
40
41
42 //********************************************************************
43 //**** Main part of Program ****//
44 // Loop over all the Files
45 for (int i=0; i < fileName.size(); i++) {
46 TFile* currentFile = new TFile(fileName[i],"read");
47
48 cout << "Opened " << fileName[i];
49
50 TTree *currentTree = (TTree*)currentFile->Get(treeName);
51 Int_t nentries = (Int_t)currentTree->GetEntries();
52
53 cout << ", found " << treeName << " has " << nentries << " entries before cuts." << endl;
54
55 // Create a new file
56 TString newFileName = fileName[i];
57 newFileName.ReplaceAll(".root","-TreeSubset.root");
58 TFile *newFile = new TFile(newFileName,"recreate");
59
60 // Copy the part of the tree that passes cuts
61 TTree *newTree = currentTree->CopyTree(fileCuts[i]);
62 cout << " saved entries passing" << endl << fileCuts[i] << endl << " to new file, " << newFileName << endl;
63
64 //newTree->Print();
65 newTree->AutoSave();
66 //delete oldfile;
67 //delete newfile;
68 currentFile->Close();
69 } // END of loop over files
70 }