drawSensitivitySummaries_simplevars_improved.py
Go to the documentation of this file.
1 import ROOT, os, array, sys, math
2 
3 use_realistic_powers = True
4 
5 use_fixed_scale = True
6 
7 tempgraphs = []
8 for i in range(20):
9  tempgraphs.append(0)
10 def myfunc(x, par):
11  return tempgraphs[int(par[0])].Eval(x[0]);
12 
13 def GetPercentile(histo,X):
14  n = histo.GetNbinsX();
15  x = []
16  y = []
17 
18  for i in range(0,n):
19  x.append(histo.GetBinCenter(i+1));
20  y.append(histo.GetBinContent(i+1));
21  y_array = array.array('d',y)
22 
23  percentiles = array.array('d',[0.0]);
24  probs = array.array('d',[1-float(X)/100.0])
25 
26  ROOT.TMath.Quantiles(n,1,y_array,percentiles,probs,False);
27  return percentiles[0]
28 
29 
30 def GetMedian(histo):
31  n = histo.GetNbinsX();
32  x = []
33  y = []
34 
35  for i in range(0,n):
36  x.append(histo.GetBinCenter(i+1));
37  y.append(histo.GetBinContent(i+1));
38  y_array = array.array('d',y)
39 
40  return ROOT.TMath.Median(n,y_array);
41 
42 def GetMinimum(histo):
43  min = 99999
44  for i in range(0,histo.GetNbinsX()):
45  if histo.GetBinContent(i+1) < min:
46  min = histo.GetBinContent(i+1)
47  return min
48 
50  n = histo.GetNbinsX();
51 
52  greaterThanX = 0.0;
53  total = 0.0;
54 
55  for i in range(0,n):
56  total = total + float(histo.GetBinWidth(i+1));
57  if(histo.GetBinContent(i+1)>=X):
58  greaterThanX = greaterThanX + float(histo.GetBinWidth(i+1));
59 
60 
61  return greaterThanX/total
62 
63 amounts_varied = [0,50,90,110,200,500]
64 
65 modes = ["fhc","rhc"]
66 nus = ["numu","numubar","nuenuebar"]
67 #nus = ["numu","numubar"]
68 vars = ["0to0p5GeVPlus10per","0p5to1GeVPlus10per","1to2GeVPlus10per","2to3GeVPlus10per","3to4GeVPlus10per","4to5GeVPlus10per","5to6GeVPlus10per","6to7GeVPlus10per","7to8GeVPlus10per","8to9GeVPlus10per","9to10GeVPlus10per","10to15GeVPlus10per","15to20GeVPlus10per","20to120GeVPlus10per"]
69 bin_centers = [0.25,0.75,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,12.5,17.5,70.0]
70 #bin_centers = [0.25,0.75,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,12.5,17.5]
71 bin_centers_array = array.array('d',bin_centers)
72 hierarchies = ["nh","ih"]
73 #plotvars = ["cp_median","cp_75thpercentile","cp_3sigma","cp_5sigma","mh_minimum"]
74 plotvars = ["cp_75thpercentile","mh_minimum"]
75 
76 for amount_varied in amounts_varied:
77 
78  outfile = ROOT.TFile("/lbne/data/users/ljf26/fluxfiles/sensitivity_comps_"+str(amount_varied)+"_improved.root","RECREATE");
79 
80  for mode in modes:
81  for nu in nus:
82  for hierarchy in hierarchies:
83  for plotvar in plotvars:
84  medians = [[],[],[]]
85  # get baseline median vailes
86  baseline_medians = []
87  mixing_param = plotvar[0:2]
88  baseline_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/ProtonP120GeV_"+hierarchy+"_"+mixing_param+"_histos.root");
89 
90  if(plotvar=="cp_median"):
91  baseline_medians.append(GetMedian(baseline_file.Get("h1")))
92  baseline_medians.append(GetMedian(baseline_file.Get("h2")))
93  baseline_medians.append(GetMedian(baseline_file.Get("h3")))
94  if(plotvar=="cp_75thpercentile"):
95  baseline_medians.append(GetPercentile(baseline_file.Get("h1"),75))
96  baseline_medians.append(GetPercentile(baseline_file.Get("h2"),75))
97  baseline_medians.append(GetPercentile(baseline_file.Get("h3"),75))
98  elif(plotvar=="cp_3sigma"):
99  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h1"),3.0))
100  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h2"),3.0))
101  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h3"),3.0))
102  elif(plotvar=="cp_5sigma"):
103  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h1"),5.0))
104  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h2"),5.0))
105  baseline_medians.append(GetFractionAtMoreThanXSigma(baseline_file.Get("h3"),5.0))
106  elif(plotvar=="mh_minimum"):
107  baseline_medians.append(GetMinimum(baseline_file.Get("h1")))
108  baseline_medians.append(GetMinimum(baseline_file.Get("h2")))
109  baseline_medians.append(GetMinimum(baseline_file.Get("h3")))
110 
111  for i in range(0,len(vars)):
112 
113  var = vars[i]+str(amount_varied)
114  if(amount_varied==110): # special case has no suffix
115  var = vars[i]
116  file_prefix = mode+nu+var+"_"+hierarchy+"_"+mixing_param
117 
118  t_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/"+file_prefix+"_histos.root");
119  t_file.Print()
120  if(plotvar=="cp_median"):
121  medians[0].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMedian(t_file.Get("h1")),2)-math.pow(baseline_medians[0],2))),math.pow(GetMedian(t_file.Get("h1")),2)-math.pow(baseline_medians[0],2)))
122  medians[1].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMedian(t_file.Get("h2")),2)-math.pow(baseline_medians[1],2))),math.pow(GetMedian(t_file.Get("h2")),2)-math.pow(baseline_medians[1],2)))
123  medians[2].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMedian(t_file.Get("h3")),2)-math.pow(baseline_medians[2],2))),math.pow(GetMedian(t_file.Get("h3")),2)-math.pow(baseline_medians[2],2)))
124 
125  if(plotvar=="cp_75thpercentile"):
126  medians[0].append(math.copysign(math.sqrt(math.fabs(math.pow(GetPercentile(t_file.Get("h1"),75),2)-math.pow(baseline_medians[0],2))),math.pow(GetPercentile(t_file.Get("h1"),75),2)-math.pow(baseline_medians[0],2)))
127  medians[1].append(math.copysign(math.sqrt(math.fabs(math.pow(GetPercentile(t_file.Get("h2"),75),2)-math.pow(baseline_medians[1],2))),math.pow(GetPercentile(t_file.Get("h2"),75),2)-math.pow(baseline_medians[1],2)))
128  medians[2].append(math.copysign(math.sqrt(math.fabs(math.pow(GetPercentile(t_file.Get("h3"),75),2)-math.pow(baseline_medians[2],2))),math.pow(GetPercentile(t_file.Get("h3"),75),2)-math.pow(baseline_medians[2],2)))
129 
130  elif(plotvar=="mh_minimum"):
131  medians[0].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMinimum(t_file.Get("h1")),2)-math.pow(baseline_medians[0],2))),math.pow(GetMinimum(t_file.Get("h1")),2)-math.pow(baseline_medians[0],2)))
132  medians[1].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMinimum(t_file.Get("h2")),2)-math.pow(baseline_medians[1],2))),math.pow(GetMinimum(t_file.Get("h2")),2)-math.pow(baseline_medians[1],2)))
133  medians[2].append(math.copysign(math.sqrt(math.fabs(math.pow(GetMinimum(t_file.Get("h3")),2)-math.pow(baseline_medians[2],2))),math.pow(GetMinimum(t_file.Get("h3")),2)-math.pow(baseline_medians[2],2)))
134 
135  # if the file is empty, just set to -999 so it can be caught later
136  for i in range(3):
137  for var in range(len(vars)):
138  if medians[i][var]==-1.0*baseline_medians[i]:
139  medians[i][var]=-999
140 
141  ROOT.gStyle.SetMarkerStyle(21);
142  ROOT.gStyle.SetMarkerStyle(21);
143 
144  c1 = ROOT.TCanvas(file_prefix);
145 
146  t_graphs = []
147 
148  line_styles = [1,2,3]
149  line_colors = [1,2,4]
150 
151  for i in 0,1,2:
152 
153  temp = ROOT.TGraph(len(bin_centers_array),bin_centers_array,array.array('d',medians[i]));
154  temp.SetName(plotvar+"_"+mode+"_"+nu+"_"+hierarchy)
155  nu_string = "#nu_{#mu}"
156  if(nu == "numubar"):
157  nu_string = "#bar{#nu}_{#mu}"
158  temp.SetTitle("Effect of increasing "+nu_string+" "+mode+" flux by 10%")
159  t_graphs.append(temp)
160 
161  if(plotvar == "cp_median"):
162  #t_graphs[i].SetMinimum(4);
163  t_graphs[i].GetYaxis().SetTitle("Change in Median CP Sensitivity (#sigma)");
164  elif(plotvar == "cp_75thpercentile"):
165  #t_graphs[i].SetMinimum(4);
166  t_graphs[i].GetYaxis().SetTitle("75% CP Sensitivity (#sigma)");
167  elif(plotvar == "cp_3sigma"):
168  #t_graphs[i].SetMinimum(4);
169  t_graphs[i].GetYaxis().SetTitle("Fraction > 3#sigma Coverage (%)");
170  elif(plotvar == "cp_5sigma"):
171  #t_graphs[i].SetMinimum(4);
172  t_graphs[i].GetYaxis().SetTitle("Fraction > 5#sigma Coverage (%)");
173  elif(plotvar == "mh_minimum"):
174  #t_graphs[i].SetMinimum(2.5);
175  t_graphs[i].GetYaxis().SetTitle("Change in Minimum MH Sensitivity (#sigma)");
176 
177 
178  t_graphs[i].GetXaxis().SetTitle("Neutrino Energy (GeV)");
179 
180 
181  t_graphs[i].SetLineColor(line_colors[i])
182  t_graphs[i].SetMarkerColor(line_colors[i])
183 
184  c1.cd()
185  if i == 0:
186  t_graphs[i].Draw("ACP");
187  else:
188  t_graphs[i].Draw("SAME CP");
189 
190 
191  leg = ROOT.TLegend(0.45,0.3,0.85,0.55);
192  leg.SetHeader("Sig/Bkgd Uncertainties");
193  leg.AddEntry(t_graphs[0],"1%/5%","lp");
194  leg.AddEntry(t_graphs[1],"2%/5%","lp");
195  leg.AddEntry(t_graphs[2],"5%/10%","lp");
196  leg.SetFillStyle(0);
197  leg.SetBorderSize(0);
198 
199  leg.Draw();
200  file_prefix = "sensitivity_plots/"+mode+str(amount_varied)+nu+"_"+hierarchy+"_"+plotvar
201 
202  outfile.cd()
203  t_graphs[1].Write();
204 
205  c1.Print(file_prefix+"_improved.eps")
206  c1.Print(file_prefix+"_improved.png")
207 
208 
209  outfile.Close()
210 
211 
212 # read in the above and plot change in sensitivity as a function of change in flux
213 #unicorns (for searching)
214 
215 modes = ["fhc","rhc"]
216 nus = ["numu","numubar","nuenuebar"]
217 vars = ["0to0p5GeVPlus10per","0p5to1GeVPlus10per","1to2GeVPlus10per","2to3GeVPlus10per","3to4GeVPlus10per","4to5GeVPlus10per","5to6GeVPlus10per","6to7GeVPlus10per","7to8GeVPlus10per","8to9GeVPlus10per","9to10GeVPlus10per","10to15GeVPlus10per","15to20GeVPlus10per","20to120GeVPlus10per"]
218 bin_centers_array = array.array('d',bin_centers)
219 hierarchies = ["nh","ih"]
220 #plotvars = ["cp_median","cp_75thpercentile","cp_3sigma","cp_5sigma","mh_minimum"]
221 plotvars = ["cp_75thpercentile","mh_minimum"]
222 
223 amounts_varied = [0, 50,90,100,110,200,500,1000]
224 
225 for mode in modes:
226  for nu in nus:
227  for hierarchy in hierarchies:
228  for plotvar in plotvars:
229  file_prefix = "sensitivity_plots/"+mode+nu+"_"+hierarchy+"_"+plotvar
230  c1 = ROOT.TCanvas(file_prefix,file_prefix,1800,1000)
231  c1.Divide(5,3)
232 
233  # declare and initialize list of sensitivity changes
234  sensitivity_changes = []
235  flux_changes = []
236  for bin_iter in range(0,len(vars)):
237  sensitivity_changes.append([])
238  flux_changes.append([])
239 
240  for variation_iter in range(0,len(amounts_varied)):
241  variation = amounts_varied[variation_iter]
242  if(variation==100):
243  for bin_iter in range(len(vars)):
244  sensitivity_changes[bin_iter].append(0)
245  flux_changes[bin_iter].append(100)
246  else:
247  temp_file = ROOT.TFile("/lbne/data/users/ljf26/fluxfiles/sensitivity_comps_"+str(variation)+"_improved.root");
248  mygraph = temp_file.Get(plotvar+"_"+mode+"_"+nu+"_"+hierarchy)
249  for bin_iter in range(mygraph.GetN()):
250  if(mygraph.GetY()[bin_iter] != -999):
251  sensitivity_changes[bin_iter].append(mygraph.GetY()[bin_iter])
252  flux_changes[bin_iter].append(amounts_varied[variation_iter])
253 
254  temps = []
255  lines = []
256  tempfuncs = []
257  for bin_iter in range(0,len(vars)):
258  c1.cd(bin_iter+1)
259  temps.append(ROOT.TGraph(len(flux_changes[bin_iter]),array.array('d',flux_changes[bin_iter]),array.array('d',sensitivity_changes[bin_iter])));
260  temps[bin_iter].Draw("AP")
261  xlow = -20
262  xhigh = 1000
263  temps[bin_iter].GetXaxis().SetLimits(xlow,xhigh);
264  if(use_fixed_scale):
265  maximum = 0.6*5
266  minimum = -0.6*5
267  else:
268  maximum = temps[bin_iter].GetHistogram().GetMaximum()
269  minimum = temps[bin_iter].GetHistogram().GetMinimum()
270  myrange = ROOT.TMath.Max(ROOT.TMath.Abs(maximum),ROOT.TMath.Abs(minimum));
271  temps[bin_iter].GetHistogram().SetMinimum(-1.0*myrange)
272  temps[bin_iter].GetHistogram().SetMaximum(myrange)
273  temps[bin_iter].SetMarkerStyle(21);
274  temps[bin_iter].SetMarkerSize(2);
275  temps[bin_iter].SetMarkerColor(4);
276  temps[bin_iter].GetXaxis().SetNdivisions(4);
277  temps[bin_iter].GetYaxis().SetNdivisions(4);
278  temps[bin_iter].Draw("AP")
279  c1.Update()
280  temps[bin_iter].GetXaxis().SetLabelSize(0.08)
281  ROOT.gStyle.SetOptTitle(1);
282  temps[bin_iter].GetXaxis().SetTitle("Change in Flux (%)")
283  temps[bin_iter].GetYaxis().SetTitle("Change in CP Sens. (#sigma)")
284  temps[bin_iter].GetXaxis().SetTitleSize(0.07)
285  temps[bin_iter].GetYaxis().SetTitleSize(0.07)
286  temps[bin_iter].GetXaxis().SetTitleOffset(0.9)
287  temps[bin_iter].GetYaxis().SetTitleOffset(1.05)
288  temps[bin_iter].GetYaxis().SetLabelSize(0.07)
289  temps[bin_iter].SetTitle(vars[bin_iter].split("Plus")[0])
290  ROOT.gPad.Update()
291  title = ROOT.gPad.GetPrimitive("title");
292  title.SetBorderSize(0);
293  ROOT.gStyle.SetTitleY(0.95)
294  title.SetTextSize(0.08)
295  title.SetTextColor(8)
296  #temps[bin_iter].SetTitle(vars[bin_iter])
297  #add_plot_label(Form("%1.1f < E < %1.1f GeV",h_nof[0]->GetBinLowEdge(k+1),h_nof[0]->GetBinLowEdge(k+1)+h_nof[0]->GetBinWidth(k+1)),0.55,0.96,0.09,38,62,22);
298 
299  # draw a line through the points at 100,110%
300  # this was the first estimator I used -- want to see how much the other points deviate from it
301  iter_100 = -1
302  iter_110 = -1
303  for i in range(len(flux_changes[bin_iter])):
304  if flux_changes[bin_iter][i] == 100:
305  iter_100 = i
306  if flux_changes[bin_iter][i] == 110:
307  iter_110 = i
308 
309  if(iter_100 == -1 or iter_110==-1):
310  continue # Don't draw a line
311  slope = (sensitivity_changes[bin_iter][iter_110]-sensitivity_changes[bin_iter][iter_100])/(flux_changes[bin_iter][iter_110]-flux_changes[bin_iter][iter_100]);
312  y_int = sensitivity_changes[bin_iter][iter_110]-slope*flux_changes[bin_iter][iter_110];
313  ylow_line = slope*xlow+y_int
314  yhigh_line = slope*xhigh+y_int
315  xlow_line = xlow
316  xhigh_line = xhigh
317  if ylow_line < -1.0*myrange:
318  ylow_line = -1.0*myrange
319  xlow_line = (ylow_line-y_int)/slope
320  if yhigh_line > 1.0*myrange:
321  yhigh_line = 1.0*myrange
322  xhigh_line = (yhigh_line-y_int)/slope
323  lines.append(ROOT.TLine(xlow_line,ylow_line,xhigh_line,yhigh_line))
324  lines[bin_iter].SetLineColor(2)
325  lines[bin_iter].Draw("SAME")
326 
327 
328  # Now draw a linear interpolation
329  tempgraphs[bin_iter] = temps[bin_iter]
330  tempfuncs.append(ROOT.TF1("f"+str(bin_iter),myfunc,xlow,xhigh,1))
331  tempfuncs[bin_iter].SetParameter(0,bin_iter)
332  tempfuncs[bin_iter].SetLineColor(38)
333  tempfuncs[bin_iter].Draw("SAME")
334 
335  scale_string = "fixedScale"
336  if not use_fixed_scale:
337  scale_string = "variableScale"
338  c1.Print(file_prefix+"_"+scale_string+"_improved.eps")
339  c1.Print(file_prefix+"_"+scale_string+"_improved.png")
340  outfile = ROOT.TFile(file_prefix+"_"+scale_string+"_improved.root","RECREATE")
341  for graph in temps:
342  graph.Write()
343  outfile.Close()
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
if(!yymsg) yymsg
static QCString str