1 |
#
|
2 |
# Run by typing:
|
3 |
# python plotFromMultipleFiles.py
|
4 |
#
|
5 |
# Creates histograms from TTree in several files
|
6 |
# that have different scales.
|
7 |
#
|
8 |
# Written in python for its power, and also because
|
9 |
# ROOT's C-interpreter (CINT) chokes and dies on
|
10 |
# code that tries to do things here.
|
11 |
#
|
12 |
# Michael Anderson
|
13 |
# Nov 4, 2009
|
14 |
|
15 |
from ROOT import TFile, TH1F, TH2F, TH3F, TTree # Import any ROOT class you want
|
16 |
from math import pi
|
17 |
from array import array # used to make Float_t array ROOT wants
|
18 |
from datetime import datetime # used in output filename
|
19 |
import sys # used for exiting program
|
20 |
|
21 |
########################################
|
22 |
# Class for root files to store
|
23 |
# file's name, scale, cuts, etc...
|
24 |
class rootFile:
|
25 |
def __init__(self, fileName, scale=1.0, cuts=""):
|
26 |
self.name = fileName
|
27 |
self.scale = scale
|
28 |
self.cuts = cuts
|
29 |
self.file = TFile(fileName, "read") # Open TFile
|
30 |
if self.file.IsZombie():
|
31 |
print "Error opening %s, exiting..." % self.name
|
32 |
sys.exit(0)
|
33 |
print "Opened %s, scale=%.2e, cuts='%s'" % (fileName, scale, cuts)
|
34 |
self.ttree = TTree() # Create empty TTree, and
|
35 |
try: # try to get TTree from file.
|
36 |
self.file.GetObject(ttreeName, self.ttree) # ttreeName set in variables below
|
37 |
except:
|
38 |
print "Error: %s not found in %s, exiting..." % (ttreeName, fileName)
|
39 |
sys.exit(0)
|
40 |
########################################
|
41 |
|
42 |
|
43 |
########################################
|
44 |
# Variables
|
45 |
invLum = 50.0 # Set inverse lum to scale plots to
|
46 |
|
47 |
ttreeName = "TreePhotonJet" # TTree name in all files
|
48 |
|
49 |
# Filename, scale=invLum*(cross section)/events analyzed, cuts
|
50 |
listOfFiles = [rootFile("QCD_Pt15_Summer09.root",invLum*(1.457E9-1.091E7)/4.667392e+06,"event_genEventScale>15&&event_genEventScale<30"),
|
51 |
rootFile("QCD_Pt30_Summer09.root",invLum*(1.091E7-1.93E6)/1.779232e+06, "event_genEventScale>30&&event_genEventScale<80"),
|
52 |
rootFile("QCD_Pt80_Summer09.root",invLum*(1.93E6-6.2E4)/2.14780e+06, "event_genEventScale>80&&event_genEventScale<170")]
|
53 |
|
54 |
# Cut(s) applied to all files
|
55 |
cutForAllFiles = "photon_et>15.0&&abs(photon_eta)<2.5"
|
56 |
|
57 |
outputFilename = "QCD_combined_%s.root" % datetime.now().strftime("%Y_%m_%d_%H_%M")
|
58 |
|
59 |
# Histogram bins
|
60 |
# Converting with "array" is done because ROOT wants an array of Float_t
|
61 |
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])
|
62 |
bins_eta = array('f', [-2.5, -1.55, -1.45, -0.9, 0.0, 0.9, 1.45, 1.55, 2.5])
|
63 |
bins_fisher = array('f', [i/20.0 for i in range(-40,21)]) # runs from -2.0 to 1.0 in steps of 0.05
|
64 |
|
65 |
# Variable name (in TTree) : Histogram to plot into
|
66 |
listOfPlots = {'photon_et' : TH1F("photonEt", "Photon E_{T} ;E_{T} (GeV);entries/bin" , len(bins_et)-1 , bins_et ),
|
67 |
'photon_eta': TH1F("photonEta", "Photon #eta ;#eta;entries/bin" , len(bins_eta)-1, bins_eta ),
|
68 |
'photon_phi': TH1F("photonPhi", "Photon #phi ;#phi;entries/bin" , 32, (-1.-1./15.)*pi, (1.+1./15.)*pi),
|
69 |
'fishValue' : TH1F("photonFisherValue","Photon Fisher Value;fisher Value", 40, -2.0, 1.0),
|
70 |
'fishValue:abs(photon_eta):photon_et' : TH3F("photonEtEtaFishValue", "Photon E_{T} vs |#eta| vs FishValue;E_{T};|#eta|;Fisher Value", len(bins_et)-1, bins_et, len(bins_eta)-1, bins_eta, len(bins_fisher)-1, bins_fisher)}
|
71 |
# END of Varibles
|
72 |
########################################
|
73 |
|
74 |
|
75 |
########################################
|
76 |
# Open output file
|
77 |
outputFile = TFile(outputFilename, "recreate")
|
78 |
if not outputFile.IsZombie():
|
79 |
print "Opened %s for output." % outputFilename
|
80 |
else:
|
81 |
print "Error opening %s for output exiting..." % outputFilename
|
82 |
sys.exit(0)
|
83 |
|
84 |
print "\nCuts applied to all files:\n %s" % cutForAllFiles
|
85 |
print "Creating plots..."
|
86 |
# Loop over all things to plot
|
87 |
for plot in listOfPlots:
|
88 |
print " %s >> %s" % (plot, listOfPlots[plot].GetName()) # Say what plot is being made
|
89 |
# Loop over all TTrees (from the different files)
|
90 |
for aFile in listOfFiles:
|
91 |
tempHist = listOfPlots[plot].Clone("temp") # Create temp histogram
|
92 |
cuts = "%s&&%s" % (cutForAllFiles, aFile.cuts) # Set cuts
|
93 |
aFile.ttree.Draw( "%s >> temp" % plot, cuts, "goff" ) # Draw into it (with cuts) graphics off
|
94 |
tempHist.Scale(aFile.scale) # Scale it
|
95 |
listOfPlots[plot].Add(tempHist) # Add it to total histogram
|
96 |
print "done."
|
97 |
########################################
|
98 |
|
99 |
|
100 |
########################################
|
101 |
# Store and save/close files
|
102 |
outputFile.cd()
|
103 |
for plot in listOfPlots:
|
104 |
listOfPlots[plot].Write()
|
105 |
|
106 |
print "Closing files...",
|
107 |
outputFile.Close()
|
108 |
for aFile in listOfFiles:
|
109 |
aFile.file.Close()
|
110 |
print "done.\n\nHistograms stored in %s" % outputFilename
|
111 |
########################################
|