computeNormScales.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 
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/"+file_prefix+"_histos.root");
92  else:
93  t_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/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)-baseline_medians[0])
106  medians[1].append(GetPercentile(t_file.Get("h2"),75)-baseline_medians[1])
107  medians[2].append(GetPercentile(t_file.Get("h3"),75)-baseline_medians[2])
108 
109  elif(plotvar=="mh_minimum"):
110  medians[0].append(GetMinimum(t_file.Get("h1"))-baseline_medians[0])
111  medians[1].append(GetMinimum(t_file.Get("h2"))-baseline_medians[1])
112  medians[2].append(GetMinimum(t_file.Get("h3"))-baseline_medians[2])
113 
114  elif(plotvar=="cp_median"):
115  medians[0].append(GetMedian(t_file.Get("h1"))-baseline_medians[0])
116  medians[1].append(GetMedian(t_file.Get("h2"))-baseline_medians[1])
117  medians[2].append(GetMedian(t_file.Get("h3"))-baseline_medians[2])
118 
119  elif(plotvar=="cp_3sigma"):
120  medians[0].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h1"),3.0)-baseline_medians[0])
121  medians[1].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h2"),3.0)-baseline_medians[1])
122  medians[2].append(OptimizationUtils.GetFractionAtMoreThanXSigma(t_file.Get("h3"),3.0)-baseline_medians[2])
123 
124  # if the file is empty, just set to -999 so it can be caught later
125  #for i in range(3):
126  # for var in range(len(amounts_varied_good_files)):
127  # if medians[i][var]==0:
128  # medians[i][var]=-999
129 
130 
131  if(plotvar == "cp_75thpercentile"):
132  metrics = [[],[],[]]
133 
134  # Get fitness metric, for comparison
135  nomfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_200kA_LBNEFD_fastmc.root"
136  nomrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/-200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_-200kA_LBNEFD_fastmc.root"
137  if(tweak=="antinufrac"):
138  varfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_200kA_LBNEFD_fastmc.root"
139  varrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/-200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_-200kA_LBNEFD_fastmc.root"
140 
141  power = 120
142  antinufrac = 0.5
143  for var in range(len(amounts_varied_good_files)):
144 
145  if tweak != "antinufrac":
146  varfhcfile = "temp/fhc_"+tweak+str(amounts_varied_good_files[var])+".root"
147  varrhcfile = "temp/rhc_"+tweak+str(amounts_varied_good_files[var])+".root"
148 
149  else:
150  antinufrac = amounts_varied_good_files[var]/100.0
151 
152  [fitness_names,fitnesses,fiterrors] = OptimizationUtils.ComputeMean75PercentSensitivityAndError(varfhcfile,varrhcfile,power,antinufrac,"cp")
153 
154  print "metric",fitnesses[0],fitnesses[7],fitnesses[8]
155  if(hierarchy=="nh"):
156  metrics[1].append(fitnesses[7]-baseline_medians[1])
157  if(hierarchy=="ih"):
158  metrics[1].append(fitnesses[8]-baseline_medians[1])
159 
160 
161  # Make plots
162  ROOT.gStyle.SetMarkerStyle(21);
163  ROOT.gStyle.SetMarkerStyle(21);
164 
165  c1 = ROOT.TCanvas(file_prefix);
166 
167  t_graphs = []
168  mgraph = 0
169 
170  line_styles = [1,2,3]
171  line_colors = [1,2,4]
172 
173  for i in 0,1,2:
174 
175  temp = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',medians[i]));
176  temp.SetName(plotvar+"_"+hierarchy)
177 # if tweak == "antinufrac":
178 # temp.SetTitle("CP sensitivity versus fraction of antineutrino running")
179 # else:
180 # temp.SetTitle("CP sensitivity versus change in "+tweak+" flux")
181  t_graphs.append(temp)
182 
183  if(plotvar == "cp_75thpercentile"):
184  #t_graphs[i].SetMinimum(4);
185  t_graphs[i].GetYaxis().SetTitle("75% CP Sensitivity (#sigma)");
186  elif(plotvar == "mh_minimum"):
187  #t_graphs[i].SetMinimum(2.5);
188  t_graphs[i].GetYaxis().SetTitle("Change in Minimum MH Sensitivity (#sigma)");
189  elif(plotvar == "cp_median"):
190  #t_graphs[i].SetMinimum(2.5);
191  t_graphs[i].GetYaxis().SetTitle("Median CP Sensitivity (#sigma)");
192 
193  if tweak=="antinufrac":
194  t_graphs[i].GetXaxis().SetTitle("Antineutrino Running Fraction of Total");
195  else:
196  t_graphs[i].GetXaxis().SetTitle("Normalization of "+tweak+" flux to nominal configuration (%)");
197 
198  t_graphs[i].SetLineColor(line_colors[i])
199  t_graphs[i].SetMarkerColor(line_colors[i])
200 
201  c1.cd()
202  if(plotvar!="cp_75thpercentile"):
203  t_graphs[i].SetMinimum(-1);
204  t_graphs[i].SetMaximum(1);
205  if i == 0:
206  t_graphs[i].Draw("ACP");
207  else:
208  t_graphs[i].Draw("SAME CP");
209  if i ==2:
210  leg = ROOT.TLegend(0.35,0.2,0.65,0.5);
211  leg.SetHeader("Sig/Bkgd Uncertainties");
212  leg.AddEntry(t_graphs[0],"1%/5%","lp");
213  leg.AddEntry(t_graphs[1],"2%/5%","lp");
214  leg.AddEntry(t_graphs[2],"5%/10%","lp");
215  leg.SetFillStyle(0);
216  leg.SetBorderSize(0);
217  leg.Draw();
218 
219 
220  if(plotvar == "cp_75thpercentile" and i==1):
221  m_graph = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',metrics[i]));
222  m_graph.SetLineColor(line_colors[i])
223  m_graph.SetMarkerColor(line_colors[i])
224  m_graph.SetLineStyle(2)
225  t_graphs[i].SetMaximum(1);
226  t_graphs[i].SetMinimum(-1);
227  t_graphs[i].Draw("ACP")
228  m_graph.Draw("SAME CP")
229 
230  leg = ROOT.TLegend(0.4,0.7,0.6,0.9);
231  #leg.SetHeader("Sig/Bkgd Uncertainties");
232  leg.AddEntry(t_graphs[1],"Fast MC","l");
233  leg.AddEntry(m_graph,"Metric","l");
234  leg.SetFillStyle(0);
235  leg.SetBorderSize(0);
236  leg.Draw();
237 
238 
239  file_prefix = "sensitivity_plots/normscales_"+tweak+"_"+hierarchy+"_"+plotvar
240 
241 
242  c1.Print(file_prefix+".eps")
243  c1.Print(file_prefix+".png")
244 
245 
246  ofile = ROOT.TFile(file_prefix+".root","RECREATE")
247  ofile.cd()
248  m_graph.Write()
249  t_graphs[1].Write()
250  ofile.Close()
251 
252  """
253  c1.Clear()
254  colors = [2,4,6,1,3,5,7]
255  for i in range(len(histos[1])):
256  histos[1][i].SetLineColor(colors[i])
257  histos[1][i].SetMaximum(10)
258  if(i==1):
259  histos[1][i].Draw()
260  else:
261  histos[1][i].Draw("same")
262 
263  leg = ROOT.TLegend(0.45,0.5,0.85,0.85);
264  if(tweak=="antinufrac"):
265  leg.AddEntry(histos[1][0],"10/0");
266  leg.AddEntry(histos[1][1],"9/1");
267  leg.AddEntry(histos[1][2],"7.5/2.5");
268  leg.AddEntry(histos[1][3],"5/5");
269  leg.AddEntry(histos[1][4],"2.5/7.5");
270  leg.AddEntry(histos[1][5],"1/9");
271  leg.AddEntry(histos[1][6],"0/10");
272  else:
273  for i in range(len(histos[1])):
274  leg.AddEntry(histos[1][i],str(amounts_varied[i]))
275 
276 
277  leg.SetFillStyle(0);
278  leg.SetBorderSize(0);
279  leg.Draw();
280 
281  c1.Print(plotvar+"_"+hierarchy+"blah.eps")
282  """
def GetFractionAtMoreThanXSigma(histo, X)
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