optimization_test.py
Go to the documentation of this file.
1 import ROOT, os, array, sys, OptimizationUtils,Optimizations
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 GetMinimum(histo):
21  min = 99999
22  for i in range(0,histo.GetNbinsX()):
23  if histo.GetBinContent(i+1) < min:
24  min = histo.GetBinContent(i+1)
25  return min
26 
27 hierarchies = ["nh","ih"]
28 plotvars = ["cp_75thpercentile","mh_minimum"]
29 
30 print "Blah"
31 optimization = Optimizations.Optimization("CP_run3")
32 print "Blah2"
33 
34 for hierarchy in hierarchies:
35  for plotvar in plotvars:
36  amounts_varied = [177,3544,4177,6177,6911,7177,7862,8177,8186]
37 
38  # get baseline median values
39  baseline_medians = []
40  mixing_param = plotvar[0:2]
41  baseline_file =ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/ProtonP120GeV_"+hierarchy+"_"+mixing_param+"_histos.root");
42 
43  if(plotvar=="cp_75thpercentile"):
44  baseline_medians.append(GetPercentile(baseline_file.Get("h1"),75))
45  baseline_medians.append(GetPercentile(baseline_file.Get("h2"),75))
46  baseline_medians.append(GetPercentile(baseline_file.Get("h3"),75))
47  elif(plotvar=="mh_minimum"):
48  baseline_medians.append(GetMinimum(baseline_file.Get("h1")))
49  baseline_medians.append(GetMinimum(baseline_file.Get("h2")))
50  baseline_medians.append(GetMinimum(baseline_file.Get("h3")))
51 
52  medians = [[],[],[]]
53 
54  histos = [[],[],[]]
55  print amounts_varied
56  amounts_varied_good_files = []
57  for amount_varied in amounts_varied:
58 
59  # get varied medians
60  var = "Optimizations_CP_run3_"+str(amount_varied)
61  file_prefix = var+"_"+hierarchy+"_"+mixing_param
62 
63  t_file = ROOT.TFile("/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots/"+file_prefix+"_histos.root");
64 
65  ROOT.gROOT.cd()
66  if(t_file.IsZombie() or t_file.Get("h2").Integral()==0):
67  continue
68  amounts_varied_good_files.append(amount_varied)
69 
70  histos[0].append(t_file.Get("h1").Clone("h1_"+str(amount_varied)))
71  histos[1].append(t_file.Get("h2").Clone("h2_"+str(amount_varied)))
72  histos[2].append(t_file.Get("h3").Clone("h3_"+str(amount_varied)))
73 
74  if(plotvar=="cp_75thpercentile"):
75  medians[0].append(GetPercentile(t_file.Get("h1"),75))
76  medians[1].append(GetPercentile(t_file.Get("h2"),75))
77  print amount_varied,GetPercentile(t_file.Get("h2"),75),t_file.Get("h2").Integral()
78  medians[2].append(GetPercentile(t_file.Get("h3"),75))
79 
80  elif(plotvar=="mh_minimum"):
81  medians[0].append(GetMinimum(t_file.Get("h1")))
82  medians[1].append(GetMinimum(t_file.Get("h2")))
83  medians[2].append(GetMinimum(t_file.Get("h3")))
84 
85  # if the file is empty, just set to -999 so it can be caught later
86  #for i in range(3):
87  # for var in range(len(amounts_varied_good_files)):
88  # if medians[i][var]==0:
89  # medians[i][var]=-999
90 
91  if(plotvar == "cp_75thpercentile"):
92  metrics = [[],[],[]]
93 
94  # Get fitness metric, for comparison
95  nomfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_200kA_LBNEFD_fastmc.root"
96  nomrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r2p4/QGSP_BERT/Nominal/-200kA/flux/histos_g4lbne_v3r2p4_QGSP_BERT_Nominal_-200kA_LBNEFD_fastmc.root"
97  for var in range(len(amounts_varied_good_files)):
98  print var
99  varfhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r3p2/QGSP_BERT/Optimizations-CP_run3-"+str(amounts_varied_good_files[var])+"/200kA/flux/histos_g4lbne_"+optimization.g4lbne_version+"_QGSP_BERT_Optimizations-CP_run3-"+str(amounts_varied_good_files[var])+"_200kA_LBNEFD_fastmc.root"
100  varrhcfile = "/lbne/data/users/ljf26/fluxfiles/g4lbne/v3r3p2/QGSP_BERT/Optimizations-CP_run3-"+str(amounts_varied_good_files[var])+"/-200kA/flux/histos_g4lbne_"+optimization.g4lbne_version+"_QGSP_BERT_Optimizations-CP_run3-"+str(amounts_varied_good_files[var])+"_-200kA_LBNEFD_fastmc.root"
101 
102  energy = optimization.getEnergy(float(amounts_varied_good_files[var]),"")
103  antinufrac = 0.5
104 
105  type = "cp"
106  if plotvar.startswith("mh"):
107  type = "mh"
108 
109  [fitness_names,fitnesses,fiterrors] = OptimizationUtils.ComputeMean75PercentSensitivityAndError(varfhcfile,varrhcfile,energy,energy,antinufrac,type)
110 
111  print "metric",fitnesses[0],fitnesses[7],fitnesses[8]
112  if(hierarchy=="nh"):
113  metrics[1].append(fitnesses[7])
114  if(hierarchy=="ih"):
115  metrics[1].append(fitnesses[8])
116 
117 
118  # Make plots
119  ROOT.gStyle.SetMarkerStyle(21);
120  ROOT.gStyle.SetMarkerStyle(21);
121 
122  c1 = ROOT.TCanvas(file_prefix);
123 
124  t_graphs = []
125  mgraph = 0
126 
127  line_styles = [1,2,3]
128  line_colors = [1,2,4]
129 
130  for i in 0,1,2:
131 
132  temp = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',medians[i]));
133  temp.SetName(plotvar+"_"+hierarchy)
134 
135  t_graphs.append(temp)
136 
137  if(plotvar == "cp_75thpercentile"):
138  t_graphs[i].GetYaxis().SetTitle("75% CP Sensitivity (#sigma)");
139  elif(plotvar == "mh_minimum"):
140  t_graphs[i].GetYaxis().SetTitle("Change in Minimum MH Sensitivity (#sigma)");
141 
142  t_graphs[i].GetXaxis().SetTitle("Optimization Configuration");
143 
144  t_graphs[i].SetLineColor(line_colors[i])
145  t_graphs[i].SetMarkerColor(line_colors[i])
146 
147  c1.cd()
148 
149  if(i==1):
150  m_graph = ROOT.TGraph(len(amounts_varied_good_files),array.array('d',amounts_varied_good_files),array.array('d',metrics[i]));
151  m_graph.SetLineColor(line_colors[i])
152  m_graph.SetMarkerColor(line_colors[i])
153  m_graph.SetLineStyle(2)
154  t_graphs[i].SetMaximum(3.5);
155  t_graphs[i].SetMinimum(0);
156  t_graphs[i].Draw("ACP")
157  m_graph.Draw("SAME CP")
158 
159  leg = ROOT.TLegend(0.4,0.2,0.6,0.4);
160  #leg.SetHeader("Sig/Bkgd Uncertainties");
161  leg.AddEntry(t_graphs[1],"Fast MC","l");
162  leg.AddEntry(m_graph,"Metric","l");
163  leg.SetFillStyle(0);
164  leg.SetBorderSize(0);
165  leg.Draw();
166 
167 
168 
169  file_prefix = "sensitivity_plots/optimization_test_"+hierarchy+"_"+plotvar
170 
171 
172  c1.Print(file_prefix+".eps")
173  c1.Print(file_prefix+".png")
174 
175 
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