drawUncertainties.py
Go to the documentation of this file.
1 import ROOT,array
2 from optparse import OptionParser
3 # Helper function that makes a error histogram out of a bunch of universes
4 # by calculating rms of universes in each bin
5 """
6 usage = "usage: %prog[options]"
7 parser = OptionParser(usage=usage)
8 
9 parser.add_option("-f","--filename",dest="filename",help="filename for histogram creation",default="NA")
10 
11 (options,args)=parser.parse_args()
12 pathname =options.filename
13 """
14 
15 #Use different approach instead
16 import os, sys, shutil
17 
18 if len(sys.argv)<2:
19  print " drawUncertainties.py <filename1> [filename2]"
20  sys.exit(1)
21 pathname = str(sys.argv[1])
22 pathname1=""
23 if len(sys.argv)>2:
24  pathname1 = str(sys.argv[2])
25 def MakeErrorHistogram(universes,asFrac=True):
26  #try the rebinning as well
27  counter=0
28  newbins=[]
29  for num in range(0,121): #using the 1 GeV uniform binning here
30  newbins.append(num*1.0)
31  counter+=1
32  n_universes = len(universes)
33  avg_hist = universes[0].Clone(universes[0].GetName()+"_avg")
34  for i in range(0,avg_hist.GetNbinsX()+1):
35  avg_hist.SetBinContent(i,0.0)
36  avg_hist.SetBinError(i,0.0)
37  avg_hist.Scale(1,"width")
38  error_hist = universes[0].Clone(universes[0].GetName()+"_error")
39  for i in range(0,error_hist.GetNbinsX()+1):
40  error_hist.SetBinContent(i,0.0)
41  error_hist.SetBinError(i,0.0)
42  for i in range(n_universes):
43  #universes[i].Print()
44  universes[i].Scale(1,"width")
45  avg_hist.Add(universes[i])
46  avg_hist.Scale(1/float(n_universes))
47 
48  # Calculate Error = RMS of universes
49  for j in range(error_hist.GetNbinsX()+2):
50  error = 0
51  for i in range(n_universes):
52  error += 1/float(n_universes)*ROOT.TMath.Power(universes[i].GetBinContent(j)-avg_hist.GetBinContent(j),2)
53  error_hist.SetBinContent(j,ROOT.TMath.Sqrt(error))
54  if asFrac:
55  error_hist.Divide(avg_hist)
56  return error_hist
57 #Additional function to plot things easily
58 
59 def AddUniverses(universes):
60  n_universes=len(universes)
61  avg_hist=universes[0].Clone(universes[0].GetName()+"_avg")
62  for i in range(n_universes):
63  avg_hist.Add(universes[i])
64  avg_hist.Scale(1/float(n_universes))
65  return avg_hist
66 
67 
68 def PrintPlots(rootfile,in_dir,in_files,universes,frac,urange):
69  histo_universes=[]
70  for i in range(universes):
71  histo_universes.append(rootfile.Get(in_dir+"/"+in_files+"_"+str(i)))
72  error_universes = MakeErrorHistogram(histo_universes,frac)
73  c1 = ROOT.TCanvas("c1","c1",750,750)
74  error_universes.SetLineWidth(3)
75  if "htotal" in error_universes.GetName():
76  print error_universes.GetName()[:-8],type(error_universes)
77  if urange:
78  (error_universes.GetXaxis()).SetRangeUser(0,20)
79  error_universes.Draw("hist")
80  c1.Print(str(in_files)+".pdf")
81  else:
82  error_universes.Draw("hist")
83  c1.Print(str(in_files)+".pdf")
84  del rootfile
85 
86 
87 def DrawFluxTogether(rootfile,in_dir,in_file,universes):
88  histo_universes=[]
89  for i in range(universes):
90  histo_universes.append(rootfile.Get(in_dir+"/"+in_file+"_"+str(i)))
91  c1 = ROOT.TCanvas("c1","c1",750,750)
92  c1.cd()
93  (histo_universes[0].GetXaxis()).SetRangeUser(0,20)
94  maxm= histo_universes[0].GetMaximum()
95  histo_universes[0].SetMaximum(maxm+0.1*maxm)
96  histo_universes[0].Draw("hist")
97  for i in range(0,universes):
98  histo_universes[i].Draw("same")
99  raw_input()
100 
101 
102 
103 def RebinHistogram(input_histos,uniform=False):
104  output_histos=[]
105  counter=0
106  newbins=array.array('d',[])
107  if uniform:
108  for num in range(0,17): #upto 8 GeV, 0.5 bins size
109  newbins.append(num*0.5)
110  counter+=1
111  for num in range(5,11): #From 8 to 20 GeV, we will use 2 GeV bins size
112  newbins.append(num*2)
113  counter+=1
114  if not uniform:
115  for num in range(0,41):
116  newbins.append(num*1.0)
117  counter+=1
118 
119  for hist in input_histos:
120  temphistname=hist.GetName()+"_rebinned"
121  temphist=hist.Rebin(counter-1,temphistname,newbins)
122  #if not drawing the fractional uncertainties, need to scale
123  output_histos.append(temphist)
124  return output_histos
125 
126 def DrawTogether(rootfile,nutype,universes,frac,urange):
127  errhists=[]
128  directory=["total","others","targetpcpi","targetpck","targetncpi",
129  "targetpcnu","targetmesonInc","targetnuA","totabsorption","tgtabsorption"]
130  files=["htotal","hothers","htargetpcpi","htargetpcK","htargetncpi",
131  "htargetpcnu","htargetmesonInc","htargetnucleonA","habsorption","htargetabsorption"]
132  legends=["Total HP","others","pC-->#pi X","pC-->KX","nC-->#pi X","pC-->nucleonX","meson inc.","nucloen-A",
133  "other abs.","Target Absorption"]
134  color=[38,38,4,4,46,46,43,43,3,3]
135  #style=[0,1,2,1,2,1,2,1,2,1,2]
136  style=[0,1,1,2,1,2,1,2,1,2,1]
137  for j in range(len(directory)):
138  histo_universes=[]
139  in_dir=nutype+"_"+directory[j]
140  in_files=files[j]+"_"+nutype
141  print in_dir+"/"+in_files
142  for i in range(universes):
143  histo_universes.append(rootfile.Get(str(in_dir)+"/"+str(in_files)+"_"+str(i)))
144  rebinned_histos=RebinHistogram(histo_universes,False)
145  error_universes = MakeErrorHistogram(rebinned_histos,frac)
146  errhists.append(error_universes)
147  #for i in range(0,errhists[4].GetNbinsX()+1):
148  #print errhists[4].GetBinContent(i)
149  #print len(errhists)
150  c1 = ROOT.TCanvas("c1","c1",750,750)
151  leg = ROOT.TLegend(0.3,0.5,0.5,0.9)
152  c1.cd()
153  errhists[0].SetLineColor(1)
154  errhists[0].SetLineWidth(3)
155  errhists[0].SetMinimum(0)
156  (errhists[0].GetXaxis()).SetRangeUser(0,20)
157  leg.AddEntry(errhists[0],str(legends[0]),"lp")
158  errhists[0].SetMaximum(0.20)
159  errhists[0].SetMinimum(0)
160  errhists[0].Draw("hist")
161  #print len(errhists),len(color)
162  for i in range(1,len(errhists)):
163  errhists[i].SetLineColor(color[i])
164  errhists[i].SetLineStyle(style[i])
165  errhists[i].SetLineWidth(3)
166  errhists[i].Draw("histsame")
167  leg.AddEntry(errhists[i],str(legends[i]),"lp")
168  leg.Draw("")
169  #c1.Print("Interaction_info.pdf")
170  raw_input()
171 
172  #Additional function only useful for comparing two uncertainty plots
173 
174 def PrintStackPlots(rfile1,rfile2,in_dir,in_files,universes,frac,urange):
175  if len(sys.argv)<2:
176  print "need at least two files to print stack plots...."
177  sys.exit(1)
178  histo1_universes=[]
179  histo2_universes=[]
180  for i in range(universes):
181  histo1_universes.append(rfile1.Get(in_dir+"/"+in_files+"_"+str(i)))
182  histo2_universes.append(rfile2.Get(in_dir+"/"+in_files+"_"+str(i)))
183  error_universes1 = MakeErrorHistogram(histo1_universes,frac)
184  error_universes2 = MakeErrorHistogram(histo2_universes,frac)
185  error_universes1.SetLineColor(1)
186  error_universes2.SetLineColor(2)
187  error_universes1.SetLineWidth(3)
188  error_universes2.SetLineWidth(3)
189  if urange:
190  (error_universes1.GetXaxis()).SetRangeUser(0,20)
191  (error_universes2.GetXaxis()).SetRangeUser(0,20)
192 
193  max1 = error_universes1.GetMaximum()
194  max2 = error_universes2.GetMaximum()
195 
196  c1 = ROOT.TCanvas("c1","c1",750,750)
197  if max1>max2:
198  error_universes1.SetMinimum(0)
199  error_universes1.Draw("hist")
200  error_universes2.Draw("histSame")
201  else:
202  error_universes2.SetMinimum(0)
203  error_universes2.Draw("hist")
204  error_universes1.Draw("histSame")
205  leg1 = ROOT.TLegend(0.5,0.7,0.7,0.9)
206  #Assuming that error_universes1 will be for the reference beam here.
207  leg1.AddEntry(error_universes1,"FTFP","lp")
208  leg1.AddEntry(error_universes2,"QGSP","lp")
209  leg1.Draw("")
210  current=os.getcwd()
211  newpath=current+"/comp_plots"
212  if not os.path.exists(newpath):
213  os.makedirs(newpath)
214  c1.Print(str(newpath)+"/"+str(in_files)+"_comp.pdf")
215 
216  return
217 
218 
219 
220 def PrintRatioErrorPlots(rfile1,rfile2,in_dir,in_files,universes,frac,urange,RPlots=False,rebin=True,uniform=True):
221  if len(sys.argv)<2:
222  print "need at least 2 files to compare the ratio error plots...exiting "
223  sys.exit(1)
224  print "Ratio and Error info for "+rfile1.GetName()+" / "+rfile2.GetName()
225 
226  histo1_universes=[]
227  histo2_universes=[]
228  ratio_universes=[]
229  for i in range(universes):
230  histo1_universes.append(rfile1.Get(in_dir+"/"+in_files+"_"+str(i)))
231  histo2_universes.append(rfile2.Get(in_dir+"/"+in_files+"_"+str(i)))
232  if not rebin:
233  for i in range(universes):
234  histo1_universes[i].Divide(histo2_universes[i])
235  error_universes = MakeErrorHistogram(histo1_universes,frac)
236  ratio_universe=AddUniverses(histo1_universes)
237  if rebin:
238  unibin=True
239  if not uniform:
240  unibin=False
241  rebin_histo1=RebinHistogram(histo1_universes,unibin)
242  rebin_histo2=RebinHistogram(histo2_universes,unibin)
243  for i in range(universes):
244  rebin_histo1[i].Divide(rebin_histo2[i])
245  error_universes=MakeErrorHistogram(rebin_histo1,frac)
246  ratio_universe=AddUniverses(rebin_histo1)
247 
248  if urange:
249  (error_universes.GetXaxis()).SetRangeUser(0,20)
250  (ratio_universe.GetXaxis()).SetRangeUser(0,20)
251  c1 = ROOT.TCanvas("c1","c1",750,750)
252  leg1 = ROOT.TLegend(0.5,0.7,0.7,0.9)
253  c1.cd()
254  if RPlots:
255  ratio_universe.SetLineWidth(3)
256  ratio_universe.Draw("histe")
257  raw_input()
258  #c1.Print(str(in_files)+"_ratio.pdf")
259  del c1
260  else:
261 
262  error_universes.SetMinimum(0)
263  error_universes.SetLineWidth(3)
264  error_universes.Draw("hist")
265  leg1.AddEntry(error_universes,"Error for Far/Near ")
266  leg1.Draw("")
267  raw_input()
268  #c1.Print(str(in_files)+"_ratioerror.pdf")
269 # Open ppfx output file
270 in_file = ROOT.TFile(str(pathname))
271 rfile = ROOT.TFile(str(pathname1))
272 #PrintStackPlots(in_file,rfile,"numu_targetnuA","htargetnucleonA_numu",100,True,True)
273 #DrawFluxTogether(in_file,"numu_targetnuA","htargetnucleonA_numu",100)
274 #PrintPlots(in_file,"numu_targetncpi","htargetncpi_numu",100,True,True)
275 #PrintStackPlots(in_file,rfile,"numu_targetnuA","htargetnucleonA_numu",100,True,True)
276 #PrintPlots(in_file,"numu_attenuation","hatt_numu",100,True,True)
277 #PrintPlots(in_file,"numu_totabsorption","habsorption_numu",100,True,True)
278 #PrintPlots(in_file,"numu_targetncpi","htargetncpi_numu",100,True,True)
279 #PrintRatioErrorPlots(in_file,rfile,"numu_total","htotal_numu",100,True,True,False)
def DrawTogether(rootfile, nutype, universes, frac, urange)
Scale(size_t pos, T factor) -> Scale< T >
def RebinHistogram(input_histos, uniform=False)
def PrintPlots(rootfile, in_dir, in_files, universes, frac, urange)
def PrintStackPlots(rfile1, rfile2, in_dir, in_files, universes, frac, urange)
def AddUniverses(universes)
def MakeErrorHistogram(universes, asFrac=True)
def PrintRatioErrorPlots(rfile1, rfile2, in_dir, in_files, universes, frac, urange, RPlots=False, rebin=True, uniform=True)
static QCString type
Definition: declinfo.cpp:672
static QCString str
def DrawFluxTogether(rootfile, in_dir, in_file, universes)