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