energyTest.py
Go to the documentation of this file.
1 import ROOT, glob, sys, os
2 from array import array
3 import Optimizations, OptimizationUtils
4 
5 optimization_name = "power_test"
6 
7 ##### Find processed configurations and calculate fitnesses #####
8 
9 iterations = []
10 fitnesses = []
11 fitness_errors = []
12 proton_energies = []
13 zero_errors = []
14 
15 max_iterations = 1e7
16 
17 optimization = Optimizations.Optimization(optimization_name)
18 
19 fluxpath = "/lbne/data/users/ljf26/fluxfiles/g4lbne/"+optimization.g4lbne_version+"/QGSP_BERT/"
20 fluxdirs = glob.glob(fluxpath+"*")
21 
22 
23 
24 for dir in fluxdirs:
25 
26  if os.path.basename(dir).startswith("Optimizations-"+optimization_name):
27  iteration = dir.split("/")[9].split("-")[2]
28 
29  varfhcfile = dir+"/200kA/flux/histos_g4lbne_"+optimization.g4lbne_version+"_QGSP_BERT_"+os.path.basename(dir)+"_200kA_LBNEFD_fastmc.root"
30  varrhcfile = dir+"/-200kA/flux/histos_g4lbne_"+optimization.g4lbne_version+"_QGSP_BERT_"+os.path.basename(dir)+"_-200kA_LBNEFD_fastmc.root"
31 
32  #[tfitness_names,tfitnesses,tfiterrors] = optimization.getFitness(float(iteration))
33  energy = optimization.getEnergy(float(iteration),"FHC")
34  antinufrac = 0.5
35  print "energy",energy
36  [tfitness_names,tfitnesses,tfiterrors] = OptimizationUtils.ComputeMean75PercentSensitivityAndError(varfhcfile,varrhcfile,energy,energy,antinufrac,"cp")
37  fitness = tfitnesses[0]
38  print "fitness",fitness
39  fitness_error = tfiterrors[0]
40 
41  if(fitness>0):
42  iterations.append(float(iteration))
43  fitnesses.append(fitness)
44  fitness_errors.append(fitness_error)
45  zero_errors.append(0.0)
46 
47 # Read in knob turns from macros
48 iterations2 = []
49 parameter_values = []
50 
51 parameter_histos = []
52 
53 n_parameters = len(optimization.parameter_names);
54 for i in range(0,n_parameters):
55  parameter_name = optimization.parameter_names[i]
56  parameter_values.append([])
57  parameter_histos.append(ROOT.TH1F("h_parameters_"+str(i),parameter_name,10,optimization.parameter_lower_limits[i],optimization.parameter_upper_limits[i]));
58 
59 knobturns = optimization.getKnobturns()
60 
61 for iteration in iterations:
62  if float(iteration)>max_iterations: continue
63  iteration_found = False
64  for j in range(0,len(knobturns)):
65  if knobturns[j][0]==iteration:
66  iteration_found = True
67  iterations2.append(knobturns[j][0])
68  for i in range(0,n_parameters):
69  parameter_histos[i].Fill(knobturns[j][1][i])
70  parameter_values[i].append(knobturns[j][1][i])
71  if not iteration_found:
72  print "ERROR: iteration:",iteration," found in flux directories but can't find macros to look up knobturns... quitting."
73  sys.exit()
74 
75 # Make TGraphs
76 canv = ROOT.TCanvas("MyCanvas","MyCanvas")
77 
78 ROOT.gStyle.SetMarkerStyle(2);
79 
80 for i in range(0,n_parameters):
81 
82  fitness_vs_parameter = ROOT.TGraphErrors(len(iterations),array('d',parameter_values[i]),array('d',fitnesses),array('d',zero_errors),array('d',fitness_errors))
83  fitness_vs_parameter.GetYaxis().SetTitle("Fitness")
84  x_axis_title = optimization.parameter_names[i]
85  if(optimization.parameter_units[i]!=""):
86  x_axis_title = x_axis_title+" ("+optimization.parameter_units[i]+")"
87  fitness_vs_parameter.GetXaxis().SetTitle(x_axis_title);
88  fitness_vs_parameter.GetXaxis().CenterTitle()
89  fitness_vs_parameter.GetYaxis().CenterTitle()
90  fitness_vs_parameter.SetTitle("")
91  fitness_vs_parameter.Draw("AP");
92 
93  # Cross check from FMC
94  FMC_SENSIT = "/lbne/data/users/lblpwg_tools/FastMC_Data/outputs/ljf26/Sensitivity_Plots"
95 
96  energies = [20,30,50,60,70,80,90,110,120,130]
97  mean_sensitivities = []
98  for energy in energies:
99 
100  if energy != 120:
101  nh_cp_file = ROOT.TFile(FMC_SENSIT+"/ProtonP"+str(int(energy))+"GeV_RealisticPower_nh_cp_histos.root")
102  ih_cp_file = ROOT.TFile(FMC_SENSIT+"/ProtonP"+str(int(energy))+"GeV_RealisticPower_ih_cp_histos.root")
103  else:
104  nh_cp_file = ROOT.TFile(FMC_SENSIT+"/ProtonP"+str(int(energy))+"GeV_nh_cp_histos.root")
105  ih_cp_file = ROOT.TFile(FMC_SENSIT+"/ProtonP"+str(int(energy))+"GeV_ih_cp_histos.root")
106 
107  nh_sensitivity = OptimizationUtils.GetPercentile(nh_cp_file.Get("h2"),75)
108  ih_sensitivity = OptimizationUtils.GetPercentile(ih_cp_file.Get("h2"),75)
109 
110  mean_sensitivities.append((nh_sensitivity+ih_sensitivity)/2)
111 
112  blah = ROOT.TGraph(len(energies),array('d',energies),array('d',mean_sensitivities));
113  blah.SetMarkerColor(2);
114  blah.SetLineColor(2);
115  blah.SetMarkerStyle(21);
116  blah.GetXaxis().SetTitle("Proton Energy (GeV)")
117  blah.GetYaxis().SetTitle("Average 75% #delta_{CP} Coverage (#sigma)")
118  blah.SetTitle("")
119  blah.Draw("ACP");
120  fitness_vs_parameter.Draw("SAMEP");
121 
122  canv.Print("plots/"+optimization_name+"_fitness_vs_"+parameter_name+".eps")
123  canv.Print("plots/"+optimization_name+"_fitness_vs_"+parameter_name+".png")
def ComputeMean75PercentSensitivityAndError(varfhcfile, varrhcfile, fhcvarenergy=120.0, rhcvarenergy=120.0, antinufrac=0.5, quantity="cp")
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
def GetPercentile(histo, X)
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
if(!yymsg) yymsg
static QCString str