antinufrac_test.py
Go to the documentation of this file.
1 import ROOT, os, array, sys, OptimizationUtils
2 
3 def GetPercentile(histo,X):
4  n = histo.GetNbinsX();
5  x = []
6  y = []
7 
8  for i in range(0,n):
9  x.append(histo.GetBinCenter(i+1));
10  y.append(histo.GetBinContent(i+1));
11  y_array = array.array('d',y)
12 
13  percentiles = array.array('d',[0.0]);
14  probs = array.array('d',[1-float(X)/100.0])
15 
16  ROOT.TMath.Quantiles(n,1,y_array,percentiles,probs,False);
17  return percentiles[0]
18 
19 
20 def GetMedian(histo):
21  n = histo.GetNbinsX();
22  x = []
23  y = []
24 
25  for i in range(0,n):
26  x.append(histo.GetBinCenter(i+1));
27  y.append(histo.GetBinContent(i+1));
28  y_array = array.array('d',y)
29 
30  return ROOT.TMath.Median(n,y_array);
31 
32 
33 
34 
35 def GetMinimum(histo):
36  min = 99999
37  for i in range(0,histo.GetNbinsX()):
38  if histo.GetBinContent(i+1) < min:
39  min = histo.GetBinContent(i+1)
40  return min
41 
42 hierarchies = ["nh","ih"]
43 plotvars = ["cp_75thpercentile","mh_minimum"]
44 #tweaks = ["fhc_numu"]
45 #tweaks = ["fhc_numu","fhc_numubar","fhc_nue","rhc_numu","rhc_numu","rhc_numubar","rhc_nuebar"]
46 tweaks = ["antinufrac"]
47 plotvars = ["cp_75thpercentile","mh_minimum", "cp_median"]
48 for tweak in tweaks:
49  for hierarchy in hierarchies:
50  for plotvar in plotvars:
51  if tweak=="antinufrac":
52  amounts_varied = [0,10,25,50,75,90,100]
53  else:
54  amounts_varied = [0,50,90,100,110,200]
55 
56  # get baseline median values
57  baseline_medians = []
58  mixing_param = plotvar[0:2]
59  baseline_file =ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/ProtonP120GeV_"+hierarchy+"_"+mixing_param+"_histos.root");
60 
61  if(plotvar=="cp_3sigma"):
62  baseline_medians.append(OptimizationUtils.GetFractionAtMoreThanXSigma(baseline_file.Get("h1"),3.0))
63  baseline_medians.append(OptimizationUtils.GetFractionAtMoreThanXSigma(baseline_file.Get("h2"),3.0))
64  baseline_medians.append(OptimizationUtils.GetFractionAtMoreThanXSigma(baseline_file.Get("h3"),3.0))
65  if(plotvar=="cp_75thpercentile"):
66  baseline_medians.append(GetPercentile(baseline_file.Get("h1"),75))
67  baseline_medians.append(GetPercentile(baseline_file.Get("h2"),75))
68  print "Baseline",GetPercentile(baseline_file.Get("h2"),75)
69  baseline_medians.append(GetPercentile(baseline_file.Get("h3"),75))
70  elif(plotvar=="mh_minimum"):
71  baseline_medians.append(GetMinimum(baseline_file.Get("h1")))
72  baseline_medians.append(GetMinimum(baseline_file.Get("h2")))
73  baseline_medians.append(GetMinimum(baseline_file.Get("h3")))
74  elif(plotvar=="cp_median"):
75  baseline_medians.append(GetMedian(baseline_file.Get("h1")))
76  baseline_medians.append(GetMedian(baseline_file.Get("h2")))
77  baseline_medians.append(GetMedian(baseline_file.Get("h3")))
78 
79  medians = [[],[],[]]
80 
81  histos = [[],[],[]]
82  print amounts_varied
83  amounts_varied_good_files = []
84  for amount_varied in amounts_varied:
85 
86  # get varied medians
87  var = tweak+str(amount_varied)
88  file_prefix = var+"_"+hierarchy+"_"+mixing_param
89 
90  if (tweak == "antinufrac" or amount_varied != 100):
91  t_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/bug/"+file_prefix+"_histos.root");
92  else:
93  t_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/bug/ProtonP120GeV_"+hierarchy+"_"+mixing_param+"_histos.root");
94 
95  ROOT.gROOT.cd()
96  if(t_file.IsZombie() or t_file.Get("h2").Integral()==0):
97  continue
98  amounts_varied_good_files.append(amount_varied)
99 
100  histos[0].append(t_file.Get("h1").Clone("h1_"+str(amount_varied)))
101  histos[1].append(t_file.Get("h2").Clone("h2_"+str(amount_varied)))
102  histos[2].append(t_file.Get("h3").Clone("h3_"+str(amount_varied)))
103 
104  if(plotvar=="cp_75thpercentile"):
105  medians[0].append(GetPercentile(t_file.Get("h1"),75))
106  medians[1].append(GetPercentile(t_file.Get("h2"),75))
107  print amount_varied,GetPercentile(t_file.Get("h2"),75),t_file.Get("h2").Integral()
108  medians[2].append(GetPercentile(t_file.Get("h3"),75))
109 
110  elif(plotvar=="mh_minimum"):
111  medians[0].append(GetMinimum(t_file.Get("h1")))
112  medians[1].append(GetMinimum(t_file.Get("h2")))
113  medians[2].append(GetMinimum(t_file.Get("h3")))
114 
115  elif(plotvar=="cp_median"):
116  medians[0].append(GetMedian(t_file.Get("h1")))
117  medians[1].append(GetMedian(t_file.Get("h2")))
118  medians[2].append(GetMedian(t_file.Get("h3")))
119 
120  elif(plotvar=="cp_3sigma"):
121  medians[0].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h1"),3.0))
122  medians[1].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h2"),3.0))
123  medians[2].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h3"),3.0))
124 
125  # if the file is empty, just set to -999 so it can be caught later
126  #for i in range(3):
127  # for var in range(len(amounts_varied_good_files)):
128  # if medians[i][var]==0:
129  # medians[i][var]=-999
130 
131 
132  if(plotvar == "cp_75thpercentile"):
133  metrics = [[],[],[]]
134 
135  # Get fitness metric, for comparison
136  nomfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_200kA_LBNEFD_fastmc.root"
137  nomrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/-200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_-200kA_LBNEFD_fastmc.root"
138  if(tweak=="antinufrac"):
139  varfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_200kA_LBNEFD_fastmc.root"
140  varrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/-200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_-200kA_LBNEFD_fastmc.root"
141 
142  power = 120
143  antinufrac = 0.5
144  for var in range(len(amounts_varied_good_files)):
145 
146  if tweak != "antinufrac":
147  varfhcfile = "temp/fhc_"+tweak+str(amounts_varied_good_files[var])+".root"
148  varrhcfile = "temp/rhc_"+tweak+str(amounts_varied_good_files[var])+".root"
149 
150  else:
151  antinufrac = amounts_varied_good_files[var]/100.0
152 
153  [fitness_names,fitnesses,fiterrors] = OptimizationUtils.ComputeMean75PercentSensitivityAndError(varfhcfile,varrhcfile,power,power,antinufrac,"cp")
154 
155  print "metric",fitnesses[0],fitnesses[7],fitnesses[8]
156  if(hierarchy=="nh"):
157  metrics[1].append(fitnesses[7])
158  if(hierarchy=="ih"):
159  metrics[1].append(fitnesses[8])
160 
161 
162  # Make plots
163  ROOT.gStyle.SetMarkerStyle(21);
164  ROOT.gStyle.SetMarkerStyle(21);
165 
166  c1 = ROOT.TCanvas(file_prefix);
167 
168  t_graphs = []
169  mgraph = 0
170 
171  line_styles = [1,2,3]
172  line_colors = [1,2,4]
173 
174  for i in 0,1,2:
175 
176  temp = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',medians[i]));
177  temp.SetName(plotvar+"_"+hierarchy)
178 # if tweak == "antinufrac":
179 # temp.SetTitle("CP sensitivity versus fraction of antineutrino running")
180 # else:
181 # temp.SetTitle("CP sensitivity versus change in "+tweak+" flux")
182  t_graphs.append(temp)
183 
184  if(plotvar == "cp_75thpercentile"):
185  #t_graphs[i].SetMinimum(4);
186  t_graphs[i].GetYaxis().SetTitle("75% CP Sensitivity (#sigma)");
187  elif(plotvar == "mh_minimum"):
188  #t_graphs[i].SetMinimum(2.5);
189  t_graphs[i].GetYaxis().SetTitle("Change in Minimum MH Sensitivity (#sigma)");
190  elif(plotvar == "cp_median"):
191  #t_graphs[i].SetMinimum(2.5);
192  t_graphs[i].GetYaxis().SetTitle("Median CP Sensitivity (#sigma)");
193 
194  if tweak=="antinufrac":
195  t_graphs[i].GetXaxis().SetTitle("Antineutrino Running Fraction of Total");
196  else:
197  t_graphs[i].GetXaxis().SetTitle("Normalization of "+tweak+" flux to nominal configuration (%)");
198 
199  t_graphs[i].SetLineColor(line_colors[i])
200  t_graphs[i].SetMarkerColor(line_colors[i])
201 
202  c1.cd()
203  if(plotvar!="cp_75thpercentile"):
204  t_graphs[i].SetMinimum(0);
205  t_graphs[i].SetMaximum(5);
206  t_graphs[i].SetTitle("")
207  if i == 0:
208  t_graphs[i].Draw("ACP");
209  else:
210  t_graphs[i].Draw("SAME CP");
211  if i ==2:
212  leg = ROOT.TLegend(0.35,0.2,0.65,0.5);
213  leg.SetHeader("Sig/Bkgd Uncertainties");
214  leg.AddEntry(t_graphs[0],"1%/5%","lp");
215  leg.AddEntry(t_graphs[1],"2%/5%","lp");
216  leg.AddEntry(t_graphs[2],"5%/10%","lp");
217  leg.SetFillStyle(0);
218  leg.SetBorderSize(0);
219  leg.Draw();
220 
221 
222  if(plotvar == "cp_75thpercentile" and i==1):
223  m_graph = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',metrics[i]));
224  m_graph.SetLineColor(line_colors[i])
225  m_graph.SetMarkerColor(line_colors[i])
226  m_graph.SetLineStyle(2)
227  t_graphs[i].SetMaximum(3.5);
228  t_graphs[i].SetMinimum(0);
229  t_graphs[i].SetTitle("")
230  t_graphs[i].Draw("ACP")
231  m_graph.Draw("SAME CP")
232 
233  leg = ROOT.TLegend(0.4,0.7,0.6,0.9);
234  #leg.SetHeader("Sig/Bkgd Uncertainties");
235  leg.AddEntry(t_graphs[1],"Fast MC","l");
236  leg.AddEntry(m_graph,"Metric","l");
237  leg.SetFillStyle(0);
238  leg.SetBorderSize(0);
239  leg.Draw();
240 
241 
242  file_prefix = "sensitivity_plots/"+tweak+"_"+hierarchy+"_"+plotvar
243 
244 
245  c1.Print(file_prefix+".eps")
246  c1.Print(file_prefix+".png")
247  """
248 
249 
250  ofile = ROOT.TFile(file_prefix+".root","RECREATE")
251  ofile.cd()
252  m_graph.Write()
253  t_graphs[1].Write()
254  ofile.Close()
255  """
256 
257  """
258  c1.Clear()
259  colors = [2,4,6,1,3,5,7]
260  for i in range(len(histos[1])):
261  histos[1][i].SetLineColor(colors[i])
262  histos[1][i].SetMaximum(10)
263  if(i==1):
264  histos[1][i].Draw()
265  else:
266  histos[1][i].Draw("same")
267 
268  leg = ROOT.TLegend(0.45,0.5,0.85,0.85);
269  if(tweak=="antinufrac"):
270  leg.AddEntry(histos[1][0],"10/0");
271  leg.AddEntry(histos[1][1],"9/1");
272  leg.AddEntry(histos[1][2],"7.5/2.5");
273  leg.AddEntry(histos[1][3],"5/5");
274  leg.AddEntry(histos[1][4],"2.5/7.5");
275  leg.AddEntry(histos[1][5],"1/9");
276  leg.AddEntry(histos[1][6],"0/10");
277  else:
278  for i in range(len(histos[1])):
279  leg.AddEntry(histos[1][i],str(amounts_varied[i]))
280 
281 
282  leg.SetFillStyle(0);
283  leg.SetBorderSize(0);
284  leg.Draw();
285 
286  c1.Print(plotvar+"_"+hierarchy+"blah.eps")
287  """
def GetMedian(histo)
def GetFractionAtMoreThanXSigma(histo, X)
def GetMinimum(histo)
def ComputeMean75PercentSensitivityAndError(varfhcfile, varrhcfile, fhcvarenergy=120.0, rhcvarenergy=120.0, antinufrac=0.5, quantity="cp")
def GetPercentile(histo, X)
if(!yymsg) yymsg
static QCString str