ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/plotFromMultipleFiles.py
Revision: 1.2
Committed: Wed Nov 4 22:52:35 2009 UTC (15 years, 5 months ago) by anderson
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +17 -19 lines
Log Message:
Improved script for plotting from multiple files with different scales, etc

File Contents

# Content
1 #
2 #
3 # Written in python because ROOT's c-interpreter (CINT)
4 # chokes and dies on code that tries to do this.
5 #
6 # Creates histograms from TTree in several files
7 # that have different scales.
8 #
9 # Michael Anderson
10 # Nov 4, 2009
11
12 from ROOT import TFile, TH1F, TTree
13 from math import pi
14 from array import array # used to make Float_t's that ROOT wants
15 from datetime import datetime # used in output filename
16 import sys # used for exiting program
17
18 ########################################
19 # Class for root files to store
20 # file's name, scale, cuts, etc...
21 class rootFile:
22 def __init__(self, fileName, theScale, theCuts):
23 self.name = fileName
24 self.scale = theScale
25 self.cuts = theCuts
26 self.file = TFile(fileName, "read") # Open TFile
27 if self.file.IsZombie():
28 print "Error opening %s, exiting..." % self.name
29 sys.exit(0)
30 self.ttree = TTree()
31 self.file.GetObject(ttreeName, self.ttree) # ttreeName is set in variables below
32 ########################################
33
34
35 ########################################
36 # Variables
37 invLum = 50.0 # Set inverse lum to scale plots to
38
39 ttreeName = "TreePhotonJet" # TTree name in all files
40
41 # Filename, scale=invLum*(cross section)/events analyzed, cuts
42 listOfFiles = [rootFile("QCD_Pt15_Summer09.root",invLum*(1.457E9-1.091E7)/4.667392e+06,"event_genEventScale>15&&event_genEventScale<30"),
43 rootFile("QCD_Pt30_Summer09.root",invLum*(1.091E7-1.93E6)/1.779232e+06, "event_genEventScale>30&&event_genEventScale<80"),
44 rootFile("QCD_Pt80_Summer09.root",invLum*(1.93E6-6.2E4)/2.14780e+06, "event_genEventScale>80&&event_genEventScale<170")]
45
46 # Cut applied to all files
47 cutForAllFiles = "photon_et>15.0&&abs(photon_eta)<2.5"
48
49 # Histogram bins
50 # Converting with "array" is done because ROOT wants an array of Float_t
51 bins_et = array('f', [15.0, 20.0, 27.0, 35.0, 45.0, 57.0, 72.0, 90.0, 120.0, 150.0, 200.0, 300.0, 400.0, 550.0])
52 bins_eta = array('f', [-2.5, -1.55, -1.45, -0.9, 0.0, 0.9, 1.45, 1.55, 2.5])
53
54 listOfPlots = {'photon_et' : TH1F("photonEt", "Photon E_{T} ;E_{T} (GeV);entries/15 GeV bin", len(bins_et)-1 , bins_et ),
55 'photon_eta': TH1F("photonEta", "Photon #eta ;#eta;entries/0.1 bin" , len(bins_eta)-1, bins_eta ),
56 'photon_phi': TH1F("photonPhi", "Photon #phi ;#phi;entries/bin" , 62, (-1.-1./30.)*pi, (1.+1./30.)*pi)}
57
58 outputFilename = "QCD_combined_%s.root" % datetime.now().strftime("%Y_%m_%d_%H_%M")
59 # END of Varibles
60 ########################################
61
62
63 ########################################
64 # Print names of opened files
65 for aFile in listOfFiles:
66 print "Opened %s, scale=%.2e, cuts='%s'" % (aFile.name, aFile.scale, aFile.cuts)
67
68 outputFile = TFile(outputFilename, "recreate")
69 if not outputFile.IsZombie():
70 print "Opened %s for output." % outputFilename
71 else:
72 print "Error opening "+self.name+" for output exiting..."
73 sys.exit(0)
74
75 print "Creating plots...",
76 # Loop over all things to plot
77 for plot in listOfPlots:
78 # Loop over all TTrees (from the different files)
79 for aFile in listOfFiles:
80 tempHist = listOfPlots[plot].Clone("temp") # Create temp histogram
81 cuts = "%s&&%s" % (cutForAllFiles, aFile.cuts) # Set cuts
82 aFile.ttree.Draw( "%s >> temp" % plot, cuts, "goff" ) # Draw into it (with cuts)
83 tempHist.Scale(aFile.scale) # Scale it
84 listOfPlots[plot].Add(tempHist) # Add it to total histogram
85 print "done."
86 ########################################
87
88
89 ########################################
90 # Store and save/close files
91 outputFile.cd()
92 for plot in listOfPlots:
93 listOfPlots[plot].Write()
94
95 print "Closing files...",
96 outputFile.Close()
97 for aFile in listOfFiles:
98 aFile.file.Close()
99 print "done.\n\nHistograms stored in %s" % outputFilename
100 ########################################