def print_chi2_breakdown(chain,mcf): import models import variables as v model = models.get_model_from_file(mcf) lhoods = models.get_lhood_names(mcf) MCVdict=v.mc_variables() if len( model ) > 0 : print "\nchi2 penalties from gaussian constraints :" print "===================================================================" print " Penalty Prediction Name Type Constraint" print "===================================================================" for constraint in model: sn=constraint.short_name MCV=MCVdict[sn] v_index = MCV.get_index(mcf) chi2=chain.treeVars["contributions"][v_index] pred=chain.treeVars["predictions"][v_index] #print "{:11g} {:<{width}{precision}{base}}{c!r}".format(chi2, pred, base='g', width=1, precision=4, c=constraint) print "{:11.2f} {:11.4g} {!r}".format(chi2, pred, constraint) #print "{chi2:>f} {".format(chi2=chi2) print "===================================================================" if len(lhoods.keys()) > 0 : print "\nThe likelihoods give penalties:\n" for i, lhood in enumerate(lhoods.items()): chi2=chain.treeVars["lhoods"][i] print "{:11.2f} {:16} {:16}". format( chi2, lhood[0], lhood[1] )
def get_lhood_names( mcf ) : lhd = ld.get_lhood_dict() out = OrderedDict() mcvars=v.mc_variables() filename=getattr( mcf, "LHoodFile", None ) if filename is not None : with open(filename, 'rb') as f: for line in f : name = line[:-1] #cut EOL out[name] = lhd[name]["name"] return out
def get_lhood_from_file( mcf ) : lhs = ld.get_lhood_dict() mcvars=v.mc_variables() out = OrderedDict() filename=getattr( mcf, "LHoodFile", None ) if filename is not None : with open(filename, 'rb') as f: for line in f : name = line[:-1] #cut EOL if name in lhs : var_ints = [ mcvars[varname].get_index(mcf) for varname in lhs[name]["vars"] ] out[name] = lhm.LHood( var_ints, lhs[name] ) else : print "Unknown Likelihood: %s, ignoring!" % name return out
def get_p_value_n_dof(chain,mcf): import models import variables as v model = models.get_model_from_file(mcf) lhoods = models.get_lhood_names(mcf) MCVdict=v.mc_variables() chi2_s=[] for constraint in model: sn=constraint.short_name chi2_s.append(get_contribution(chain,mcf,sn)) for i, lhood in enumerate(lhoods.items()): chi2_s.append(chain.treeVars["lhoods"][i]) count=0 for chi2 in chi2_s: if chi2 > 0 : count += 1 n_dof = count - mcf.Inputs chi2_tot = chain.treeVars["predictions"][0] p_value= r.TMath.Prob(chi2_tot, n_dof) return p_value, n_dof
def recalc_to_file( collection, output_file = "" ) : model = models.get_model_from_file(collection) lhoods = models.get_lhood_from_file(collection) outfile = collection.FileName if output_file == "" else output_file print "Output file is %s" % outfile # initialise the MC-variables MCVdict=v.mc_variables() chain = MCRecalcChain( collection ) nentries = chain.GetEntries() begin = getattr( collection, "StartEntry", 0) end = getattr( collection, "EndEntry", nentries+1) total_delta = 0 # create trees in scope of outfile out = r.TFile(outfile,"recreate") chi2tree = chain.chains["predictions"].CloneTree(0) # might need to do address of on contirbvars nTotVars = chain.nTotVars["predictions"] contribvars = array('d',[0.0]*nTotVars) contribtree = r.TTree( 'contribtree', 'chi2 contributions') varsOutName = "vars[%d]/D" % ( nTotVars ) contribtree.SetMaxTreeSize(10*chi2tree.GetMaxTreeSize()) contribtree.Branch("vars",contribvars,varsOutName) # same with lhood nLHoods = len(lhoods.keys()) lhoodvars = array('d',[0.0]*nLHoods) lhoodtree = r.TTree( 'lhoodtree', 'lhood contributions') varsOutName = "vars[%d]/D" % ( nLHoods ) lhoodtree.SetMaxTreeSize(10*chi2tree.GetMaxTreeSize()) lhoodtree.Branch("vars",lhoodvars,varsOutName) # want to save best fit point entry number: create new tree and branch bfname = getattr( collection, "BestFitEntryName", "BestFitEntry" ) bft=r.TTree(bfname, "Entry") bfn=array('i',[0]) bft.Branch('EntryNo',bfn,'EntryNo/I') # and the minChi minEntry minChi=1e9 minEntry=-1 count=-1 # becuase the first entry has number 0 prog = ProgressBar(begin, end, 77, mode='fixed', char='#') for entry in range(begin,end) : prog.increment_amount() print prog,'\r', stdout.flush() chain.GetEntry(entry) if good_point( chain.treeVars["predictions"], collection ) : delta = 0. chi2 = 0 for constraint in model : MCV=MCVdict[constraint.short_name] v_index = MCV.get_index(collection) chi2_t = constraint.get_chi2( chain.treeVars["predictions"][v_index] ) contribvars[v_index] = chi2_t chi2 += chi2_t for i,lh in enumerate(lhoods.values()) : chi2_t = lh.get_chi2( chain.treeVars["predictions"] ) lhoodvars[i] = chi2_t chi2 += chi2_t chi2 += spectrum_constraints( chain.treeVars["predictions"], collection ) if chi2 > getattr(collection, "MinChi2", 0 ) and \ chi2 < getattr(collection, "MaxChi2", 1e9 ) : # This was inserted to check on if there was a significant # calculation error ( average deltachi2 per entry: 1e-15 ) if __DEBUG : delta_chi2_val = chi2_t - chain.treeVars["contributions"][key] delta = delta + delta_chi2_val total_delta = total_delta + abs(delta) chain.treeVars["predictions"][0] = chi2 contribvars[0] = chi2 chi2tree.Fill() contribtree.Fill() lhoodtree.Fill() count+=1 #dealing with minChi if chi2 < minChi: minChi=chi2 minEntry=count #Saving best fit Entry number bft.GetEntry(0) bfn[0]=minEntry bft.Fill() bft.AutoSave() chi2tree.AutoSave() contribtree.AutoSave() lhoodtree.AutoSave() out.Close() if __DEBUG : print "\n--------------------------\n" print " TOTAL ( MEAN )" print "%10e(%10e)" % ( total_delta, (total_delta/(end-begin)) ) print "\n--------------------------\n"
def get_contribution(chain,mcf,shortname): import variables as v MCVdict=v.mc_variables() index = MCVdict[shortname].get_index(mcf) contribution=chain.treeVars["contributions"][index] return contribution
def get_prediction(chain,mcf,shortname): import variables as v MCVdict=v.mc_variables() index = MCVdict[shortname].get_index(mcf) prediction=chain.treeVars["predictions"][index] return prediction