ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/copytree.py
Revision: 1.5
Committed: Wed Dec 5 13:07:24 2012 UTC (12 years, 5 months ago) by bortigno
Content type: text/x-python
Branch: MAIN
Changes since 1.4: +13 -1 lines
Log Message:
fix. added write output file before closing it, against corrupted output.

File Contents

# User Rev Content
1 peller 1.1 import ROOT
2     from ROOT import TFile
3     from printcolor import printc
4    
5    
6 nmohr 1.4 def copytree(pathIN,pathOUT,prefix,newprefix,file,Aprefix,Acut):
7 bortigno 1.5
8     print "##### COPY TREE - BEGIN ######"
9     print "Input File : %s/%s%s.root " %(pathIN,prefix,file)
10     print "Output File : %s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file)
11    
12 peller 1.1 input = TFile.Open("%s/%s%s.root" %(pathIN,prefix,file),'read')
13 nmohr 1.4 output = TFile.Open("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'recreate')
14 peller 1.1
15     input.cd()
16     obj = ROOT.TObject
17     for key in ROOT.gDirectory.GetListOfKeys():
18     input.cd()
19     obj = key.ReadObj()
20 peller 1.2 #print obj.GetName()
21 peller 1.1 if obj.GetName() == 'tree':
22     continue
23     output.cd()
24 peller 1.2 #print key.GetName()
25 peller 1.1 obj.Write(key.GetName())
26    
27     inputTree = input.Get("tree")
28     nEntries = inputTree.GetEntries()
29     output.cd()
30     print '\n\t copy file: %s with cut: %s' %(file,Acut)
31     outputTree = inputTree.CopyTree(Acut)
32     kEntries = outputTree.GetEntries()
33     printc('blue','',"\t before cuts\t %s" %nEntries)
34     printc('green','',"\t survived\t %s" %kEntries)
35     #print "\t Factor for Scaling is %s" %factor
36     outputTree.AutoSave()
37 bortigno 1.5 output.ls()
38 peller 1.1 #Count.Scale(factor)
39     #CountWithPU.Scale(factor)
40     #CountWithPU2011B.Scale(factor)
41 bortigno 1.5 print "Writing output file"
42     output.Write()
43     print "Closing output file"
44     output.Close()
45     print "Closing input file"
46 peller 1.1 input.Close()
47 bortigno 1.5
48     print "##### COPY TREE - END ######"