ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/dhidas/OSUAnalysis/Tools/scripts/HistGetter.py
Revision: 1.1.1.1 (vendor branch)
Committed: Thu Dec 1 16:28:48 2011 UTC (13 years, 5 months ago) by dhidas
Content type: text/x-python
Branch: dhidas, MAIN
CVS Tags: START, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
osu copy modified

File Contents

# User Rev Content
1 dhidas 1.1 from ROOT import *
2     from math import fsum
3    
4    
5     def getHistsFromFiles( histnames, files ):
6     TFileOpen = TFile.Open
7     allHists = {}
8     gcd = gROOT.cd
9     for sample, file in files.iteritems():
10     file = TFileOpen( file )
11     allHists[sample] = {}
12     fg = file.Get
13     gcd()
14     for hist in histnames:
15     allHists[sample][hist] = fg( hist ).Clone()
16     return allHists
17    
18     def addSampleSum( hists = {} ):
19     qcdList = {}
20     mc_all_list = {}
21     singleTopList = {}
22    
23     qcdSamples = ['bce1', 'bce2', 'bce3', 'enri1', 'enri2', 'enri3', 'pj1', 'pj2', 'pj3']
24     # allMCSamples = ['ttbar', 'wjets', 'zjets', 'tW', 'tchan', 'bce1', 'bce2', 'bce3', 'enri1',
25     # 'enri2', 'enri3', 'pj1', 'pj2', 'pj3']
26     singleTopSamples = ['tW', 'tchan']
27    
28    
29     for sample, histlist in hists.iteritems():
30     for histname, hist in histlist.iteritems():
31     if sample in qcdSamples:
32     if not qcdList.has_key( histname ):
33     qcdList[histname] = hist.Clone( 'qcd' )
34     else:
35     qcdList[histname].Add( hist )
36    
37     # if sample in allMCSamples:
38     # if not mc_all_list.has_key( histname ):
39     # mc_all_list[histname] = hist.Clone( 'all_mc' )
40     # else:
41     # mc_all_list[histname].Add( hist )
42    
43     if sample in singleTopSamples:
44     if not singleTopList.has_key( histname ):
45     singleTopList[histname] = hist.Clone( 'singleTop' )
46     else:
47     singleTopList[histname].Add( hist )
48    
49     hists['qcd'] = qcdList
50     # hists['allMC'] = mc_all_list
51     hists['singleTop'] = singleTopList
52    
53     return hists
54    
55     def makeDetailedMCStack( hists ):
56    
57     allMCSamples = ['bce1', 'bce2', 'bce3', 'enri1',
58     'enri2', 'enri3', 'pj1', 'pj2', 'pj3', 'zjets', 'wjets', 'tW', 'tchan', 'ttbar' ]
59    
60     hists['allMCDetailed'] = makeStack( hists, allMCSamples )
61     return hists
62     # mcStack = {}
63     #
64     # for sample in allMCSamples:
65     # if hists.has_key(sample):
66     # histlist = hists[sample]
67     # for histname, hist in histlist.iteritems():
68     # if sample in allMCSamples:
69     # if not mcStack.has_key( histname ):
70     # mcStack[histname] = THStack("MC", "MC")
71     # mcStack[histname].Add( hist )
72     # hists['allMCDetailed'] = mcStack
73     # return hists
74    
75     def makeMCStack( hists ):
76    
77     allMCSamples = ['qcd', 'zjets', 'wjets', 'singleTop', 'ttbar']
78     hists['allMC'] = makeStack( hists, allMCSamples )
79     return hists
80    
81     def makeStack( hists, samples ):
82     mcStack = {}
83    
84     for sample in samples:
85     if hists.has_key( sample ):
86     histlist = hists[sample]
87     for histname, hist in histlist.iteritems():
88     if not mcStack.has_key( histname ):
89     mcStack[histname] = THStack( "MC", "MC" )
90     mcStack[histname].Add( hist )
91    
92     return mcStack
93    
94     def addJetSum( hists ):
95     allhists = ['QCDest_CombRelIso_0jet', 'QCDest_CombRelIso_1jet', 'QCDest_CombRelIso_2jets',
96     'QCDest_CombRelIso_3jets', 'QCDest_CombRelIso_4orMoreJets']
97     oneOrMore = ['QCDest_CombRelIso_1jet', 'QCDest_CombRelIso_2jets',
98     'QCDest_CombRelIso_3jets', 'QCDest_CombRelIso_4orMoreJets']
99     twoOrMore = ['QCDest_CombRelIso_2jets',
100     'QCDest_CombRelIso_3jets', 'QCDest_CombRelIso_4orMoreJets']
101     threeOrMore = ['QCDest_CombRelIso_3jets', 'QCDest_CombRelIso_4orMoreJets']
102    
103     addUp = addUpHistograms
104     for sample, histlist in hists.iteritems():
105     if( len( hists[sample].keys() ) == 0 ):
106     continue
107     hists[sample]['QCDest_CombRelIso_0orMoreJets'] = addUp( hists[sample], allhists )
108     hists[sample]['QCDest_CombRelIso_1orMoreJets'] = addUp( hists[sample], oneOrMore )
109     hists[sample]['QCDest_CombRelIso_2orMoreJets'] = addUp( hists[sample], twoOrMore )
110     hists[sample]['QCDest_CombRelIso_3orMoreJets'] = addUp( hists[sample], threeOrMore )
111     return hists
112    
113     def addUpHistograms( dictOfHists, histsToAdd ):
114     hist = dictOfHists[histsToAdd[0]].Clone()
115     hadd = hist.Add
116     [hadd( h ) for name, h in dictOfHists.iteritems() if name in histsToAdd[1:]]
117     return hist
118    
119    
120    
121     if __name__ == "__main__":
122     histnames = ['QCDest_CombRelIso_0jet', 'QCDest_CombRelIso_1jet', 'QCDest_CombRelIso_2jets',
123     'QCDest_CombRelIso_3jets', 'QCDest_CombRelIso_4orMoreJets']
124     files = {'data':"/storage/workspace/BristolAnalysisTools/outputfiles/new/data_35pb_PFElectron_PF2PATJets_PFMET.root",
125     'ttbar' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/ttjet_35pb_PFElectron_PF2PATJets_PFMET.root",
126     'wjets' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/wj_35pb_PFElectron_PF2PATJets_PFMET.root",
127     'zjets' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/zj_35pb_PFElectron_PF2PATJets_PFMET.root",
128     # 'bce1' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/bce1_35pb.root",
129     'bce2' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/bce2_35pb_PFElectron_PF2PATJets_PFMET.root",
130     'bce3' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/bce3_35pb_PFElectron_PF2PATJets_PFMET.root",
131     'enri1' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/enri1_35pb_PFElectron_PF2PATJets_PFMET.root",
132     'enri2' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/enri2_35pb_PFElectron_PF2PATJets_PFMET.root",
133     'enri3' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/enri3_35pb_PFElectron_PF2PATJets_PFMET.root",
134     'pj1' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/pj1_35pb_PFElectron_PF2PATJets_PFMET.root",
135     'pj2' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/pj2_35pb_PFElectron_PF2PATJets_PFMET.root",
136     'pj3' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/pj3_35pb_PFElectron_PF2PATJets_PFMET.root"}
137     # 'tW' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/tW_35pb_PFElectron_PF2PATJets_PFMET.root",
138     # 'tchan' : "/storage/workspace/BristolAnalysisTools/outputfiles/new/tchan_35pb_PFElectron_PF2PATJets_PFMET.root"}
139     # HistGetter.samplefiles = files
140     # HG = HistGetter()
141     # HG.setStyle()
142     hists = getHistsFromFiles( histnames, files )
143     hists = addSampleSum( hists )
144     hists = addJetSum( hists )
145     qcdSamples = ['bce1', 'bce2', 'bce3', 'enri1', 'enri2', 'enri3', 'pj1', 'pj2', 'pj3']
146     allMCSamples = ['ttbar', 'wjets', 'zjets', 'tW', 'tchan', 'bce1', 'bce2', 'bce3', 'enri1',
147     'enri2', 'enri3', 'pj1', 'pj2', 'pj3']
148     singleTopSamples = ['tW', 'tchan']
149    
150     nqcd = 0
151     nstop = 0
152     nmc = 0
153     nqcd = fsum( [hists[sample]['QCDest_CombRelIso_0jet'].Integral() for sample in qcdSamples if hists.has_key( sample )] )
154     # nstop = sum([hists[sample][0].Integral() for sample in singleTopSamples if hists.has_key(sample)])
155     nmc = fsum( [hists[sample]['QCDest_CombRelIso_0jet'].Integral() for sample in allMCSamples if hists.has_key( sample )] )
156     print hists['qcd']['QCDest_CombRelIso_0jet'].Integral(), nqcd
157     # print hists['singleTop'][0].Integral(), nstop
158     print hists['allMC']['QCDest_CombRelIso_0jet'].Integral(), nmc
159     print
160     print hists['allMC']['QCDest_CombRelIso_0orMoreJets'].Integral(), hists['allMC']['QCDest_CombRelIso_0jet'].Integral()
161     print hists['allMC']['QCDest_CombRelIso_1orMoreJets'].Integral(), hists['allMC']['QCDest_CombRelIso_1jet'].Integral()
162     print hists['allMC']['QCDest_CombRelIso_2orMoreJets'].Integral(), hists['allMC']['QCDest_CombRelIso_2jets'].Integral()
163     print hists['allMC']['QCDest_CombRelIso_3orMoreJets'].Integral(), hists['allMC']['QCDest_CombRelIso_3jets'].Integral()
164     print hists['allMC']['QCDest_CombRelIso_4orMoreJets'].Integral()
165     print hists['allMC'].keys()
166     c = []
167     for histname, hist in hists['allMC'].iteritems():
168     c.append( TCanvas( "cname" + histname, histname, 800, 600 ) )
169     c[-1].cd()
170     hist.Draw()
171    
172     a = raw_input()