def plotPulls(optunf="Bayes", ntest=10, leff=True, loufl=False, optfun="exp", opttfun="", gmean=-1.0, nrebin=4): if opttfun == "": opttfun = optfun if optfun == "blobel": gmean = 0.0 funttxt, funtxt = funtxts(opttfun, optfun, leff, loufl) global histos, canv, canv2 histos = [] canv = TCanvas("canv", "thruth vs reco pulls", 600, 800) canv.Divide(1, 3) canv2 = TCanvas("canv2", "P(chi^2)", 600, 800) canv2.Divide(2, 3) if optunf == "BasisSplines": bininfo = BinInfo(nrebin) unfoldtester = UnfoldTester(optunf, nrebin) else: bininfo = BinInfo() unfoldtester = UnfoldTester(optunf) trainer = Trainer(bininfo, opttfun, optfun) tester = Tester(bininfo, optfun) hbininfo = bininfo.create(optfun) dx = hbininfo["mhi"] for sigma, ipad in [[0.01 * dx, 1], [0.03 * dx, 2], [0.1 * dx, 3]]: txt = optunf + ", smear mu, s.d.= " + str(gmean) + ", " + str( sigma) + ", train: " + funttxt + ", test: " + funtxt + ", " + str( ntest) + " tests" hPulls = TProfile("pulls", txt, hbininfo["tbins"], hbininfo["tlo"], hbininfo["thi"]) hPulls.SetErrorOption("s") hPulls.SetYTitle("Thruth reco pull") histos.append(hPulls) hChisq = TH1D("chisq", "P(chi^2) rec " + txt, 10, 0.0, 1.0) hChisqm = TH1D("chisqm", "P(chi^2) mea " + txt, 10, 0.0, 1.0) histos.append(hChisq) histos.append(hChisqm) measurement = createMeasurement(gmean, sigma, leff, optfun) response = trainer.train(measurement, loufl=loufl) for itest in range(ntest): print "Test", itest unfold, hTrue, hMeas = unfoldtester.rununfoldtest( tester, measurement, response) unfold.PrintTable(cout, hTrue, 2) hReco = unfold.Hreco(2) nbin = hReco.GetNbinsX() if hbininfo["nrebin"] > 1: hTrue = hTrue.Rebin(nrebin) for ibin in range(nbin + 1): truevalue = hTrue.GetBinContent(ibin) recvalue = hReco.GetBinContent(ibin) error = hReco.GetBinError(ibin) if error > 0.0: pull = (recvalue - truevalue) / error hPulls.Fill(hReco.GetBinCenter(ibin), pull) chisq = unfold.Chi2(hTrue, 2) hChisq.Fill(TMath.Prob(chisq, hTrue.GetNbinsX())) chisqm = unfold.Chi2measured() pchisqm = TMath.Prob(chisqm, hMeas.GetNbinsX() - hReco.GetNbinsX()) print "Chisq measured=", chisqm, "P(chi^2)=", pchisqm hChisqm.Fill(pchisqm) canv.cd(ipad) gStyle.SetErrorX(0) hPulls.SetMinimum(-3.0) hPulls.SetMaximum(3.0) hPulls.SetMarkerSize(1.0) hPulls.SetMarkerStyle(20) hPulls.SetStats(False) hPulls.Draw() canv2.cd(ipad * 2 - 1) hChisq.Draw() canv2.cd(ipad * 2) hChisqm.Draw() fname = "RooUnfoldTestPulls_" + optunf + "_" + opttfun + "_" + optfun if loufl: fname += "_oufl" canv.Print(fname + ".pdf") fname = "RooUnfoldTestChisq_" + optunf + "_" + opttfun + "_" + optfun if loufl: fname += "_oufl" canv2.Print(fname + ".pdf") return
def dqm_getSingleHist_json(server, run, dataset, hist, rootContent=False): postfix = "?rootcontent=1" if rootContent else "" datareq = urllib2.Request(('%s/jsonfairy/archive/%s/%s/%s%s') % (server, run, dataset, hist, postfix)) datareq.add_header('User-agent', ident) # Get data data = eval(re.sub(r"\bnan\b", "0", urllib2.build_opener(X509CertOpen()).open(datareq).read()), { "__builtins__": None }, {}) histo = data['hist'] # Now convert into real ROOT histogram object if 'TH1' in histo['type']: # The following assumes a TH1F object contents = histo['bins']['content'] nbins = len(contents) xmin = histo['xaxis']['first']['value'] xmax = histo['xaxis']['last']['value'] roothist = TH1F(histo['stats']['name'],histo['title'],nbins,xmin,xmax) for xx in range(1,nbins+1): roothist.SetBinContent(xx, contents[xx-1]) roothist.SetBinError(xx, histo['bins']['error'][xx-1]) roothist.SetEntries(histo['stats']['entries']) stats=array('d') stats.append(histo['stats']['entries']) stats.append(histo['stats']['entries']) stats.append(histo['stats']['entries']*histo['stats']['mean']['X']['value']) stats.append((histo['stats']['rms']['X']['value']*histo['stats']['rms']['X']['value']+histo['stats']['mean']['X']['value']*histo['stats']['mean']['X']['value'])*histo['stats']['entries']) roothist.PutStats(stats) elif(histo['type']=='TProfile'): contents = histo['bins']['content'] nbins = len(contents) xmin = histo['xaxis']['first']['value'] xmax = histo['xaxis']['last']['value'] roothist = TProfile(histo['stats']['name'],histo['title'],nbins,xmin,xmax) roothist.SetErrorOption("g") for xx in range(0,nbins): if(histo['bins']['error'][xx]!=0): ww=1./(histo['bins']['error'][xx]*histo['bins']['error'][xx]) else: ww=0. roothist.Fill(xmin+(2*xx+1)*((xmax-xmin)/(nbins*2.0)), contents[xx],ww) # roothist.SetBinContent(xx, contents[xx-1]) # roothist.SetBinError(xx, histo['bins']['error'][xx-1]) roothist.SetEntries(histo['stats']['entries']) stats=array('d') for i in range(0,6): stats.append(i) roothist.GetStats(stats) stats[0]=(histo['stats']['entries']) stats[1]=(histo['stats']['entries']) stats[2]=(histo['stats']['entries']*histo['stats']['mean']['X']['value']) stats[3]=((histo['stats']['rms']['X']['value']*histo['stats']['rms']['X']['value']+histo['stats']['mean']['X']['value']*histo['stats']['mean']['X']['value'])*histo['stats']['entries']) roothist.PutStats(stats) elif 'TH2' in histo['type']: contents = histo['bins']['content'] nbinsx = histo['xaxis']['last']['id'] xmin = histo['xaxis']['first']['value'] xmax = histo['xaxis']['last']['value'] nbinsy = histo['yaxis']['last']['id'] ymin = histo['yaxis']['first']['value'] ymax = histo['yaxis']['last']['value'] roothist = TH2F(histo['stats']['name'],histo['title'],nbinsx,xmin,xmax,nbinsy,ymin,ymax) for xx in range(1,nbinsx+1): for yy in range(1,nbinsy+1): roothist.SetBinContent(xx,yy, contents[yy-1][xx-1]) roothist.SetEntries(histo['stats']['entries']) stats=array('d') stats.append(histo['stats']['entries']) stats.append(histo['stats']['entries']) stats.append(histo['stats']['entries']*histo['stats']['mean']['X']['value']) stats.append((histo['stats']['rms']['X']['value']*histo['stats']['rms']['X']['value']+histo['stats']['mean']['X']['value']*histo['stats']['mean']['X']['value'])*histo['stats']['entries']) stats.append(histo['stats']['entries']*histo['stats']['mean']['Y']['value']) stats.append((histo['stats']['rms']['Y']['value']*histo['stats']['rms']['Y']['value']+histo['stats']['mean']['Y']['value']*histo['stats']['mean']['Y']['value'])*histo['stats']['entries']) roothist.PutStats(stats) elif(histo['type']=='TProfile2D'): contents = histo['bins']['content'] nbinsx = histo['xaxis']['last']['id'] xmin = histo['xaxis']['first']['value'] xmax = histo['xaxis']['last']['value'] nbinsy = histo['yaxis']['last']['id'] ymin = histo['yaxis']['first']['value'] ymax = histo['yaxis']['last']['value'] roothist = TProfile2D(histo['stats']['name'],histo['title'],nbinsx,xmin,xmax,nbinsy,ymin,ymax) for xx in range(0,nbinsx): for yy in range(0,nbinsy): roothist.Fill(xmin+(2*xx+1)*((xmax-xmin)/(nbinsx*2.0)),ymin+(2*yy+1)*((ymax-ymin)/(nbinsy*2.0)),0,1) for xx in range(1,nbinsx+1): for yy in range(1,nbinsy+1): roothist.SetBinContent(xx,yy, contents[yy-1][xx-1]) roothist.SetEntries(histo['stats']['entries']) return roothist
def main(network_name, do_weights, general_weights, electron_weights, muon_weights, data_files, no_true_comp, nbins, veto_all_jets, require_jets, WorkingP_List, Independant_Variable_List, Histogram_Variable_List, Profile_List, TH2D_List): TH1.SetDefaultSumw2() ## First we check that you havent vetoed and requested jets if veto_all_jets and require_jets: print("\n\n\nYOU CANT ASK FOR NONE YET SOME JETS\n\n\n") return 0 for data_dir, data_set_name in data_files: OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output") DATA_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Data") data_base_dir = os.path.join(DATA_dir, data_dir) print("\n\n" + data_set_name) """ ____ ____ _____ ____ _ ____ _ _____ ___ ___ _ _ | _ \ | _ \ | ____| | _ \ / \ | _ \ / \ |_ _| |_ _| / _ \ | \ | | | |_) | | |_) | | _| | |_) | / _ \ | |_) | / _ \ | | | | | | | | | \| | | __/ | _ < | |___ | __/ / ___ \ | _ < / ___ \ | | | | | |_| | | |\ | |_| |_| \_\ |_____| |_| /_/ \_\ |_| \_\ /_/ \_\ |_| |___| \___/ |_| \_| """ ## The list of matrices Indp_output = [None for var in Independant_Variable_List] TH1D_output = [[None for wp in WorkingP_List] for var in Histogram_Variable_List] Tail_output = [[None for wp in WorkingP_List] for var in Histogram_Variable_List] Prof_output = [[None for wp in WorkingP_List] for var in Profile_List] TH2D_output = [[None for wp in WorkingP_List] for var in TH2D_List] ## Open the root file root_file = TFile.Open(os.path.join(data_base_dir, data_set_name)) ## Loading the tree containing the working point information wpt_tree = root_file.Get("wpt_tree") ## Making the wpt_tree and the other trees friends so that they can be compared wpt_tree.AddFriend(network_name, root_file) wpt_tree.AddFriend("alt_tree", root_file) wpt_tree.AddFriend("var_tree", root_file) ## Creating a string of all the weights to be applied event_weights = [] if "SR" in data_dir: event_weights += general_weights if "ee" in data_dir: event_weights += electron_weights if "mumu" in data_dir: event_weights += muon_weights if veto_all_jets: event_weights += ["(var_tree.Jets_Loose_SumET==0)"] if require_jets: event_weights += ["(var_tree.Jets_Loose_SumET>0)"] if do_weights: weight_string = " * ".join(event_weights) else: weight_string = "" if len(weight_string): print("Events are weighted using: {}".format(weight_string)) ## The strings to call up the Truth Values if no_true_comp: True_Et = "0" True_Ex = "0" True_Ey = "0" True_Phi = "0" else: True_Et = "WP_Truth_ET" True_Ex = "WP_Truth_X" True_Ey = "WP_Truth_Y" True_Phi = "WP_Truth_Phi" """ ____ ____ _ __ __ ___ _ _ ____ | _ \ | _ \ / \ \ \ / / |_ _| | \ | | / ___| | | | | | |_) | / _ \ \ \ /\ / / | | | \| | | | _ | |_| | | _ < / ___ \ \ V V / | | | |\ | | |_| | |____/ |_| \_\ /_/ \_\ \_/\_/ |___| |_| \_| \____| """ ## Before the workingpoint loop, the independant variables are drawn for v, var in enumerate(Independant_Variable_List): print(" -- {}".format(var.name)) ## Creating the histogram which will be filled hist_name = var.name myhist = TH1D(hist_name, hist_name, var.nbins, var.xmin, var.xmax) myhist.SetStats(True) myhist.StatOverflows(True) ## Get Maths Function from variable maths_string = var.tree + "." + var.branch ## Drawing the tree and saving the hist to the matrix execution = "{}>>{}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) Indp_output[v] = myhist ## First we select the working point and create the correct strings for w, wp in enumerate(WorkingP_List): print(" -- {}:".format(wp.name)) Rec_Et = wp.Et Rec_Ex = wp.Ex Rec_Ey = wp.Ey Rec_Phi = wp.Phi if wp.tree == "ann_tree": Rec_Et = network_name + "." + Rec_Et Rec_Ex = network_name + "." + Rec_Ex Rec_Ey = network_name + "." + Rec_Ey rec_and_truth_vars = [ Rec_Et, Rec_Ex, Rec_Ey, Rec_Phi, True_Et, True_Ex, True_Ey, True_Phi ] ## Drawing the 1D histograms for v, var in enumerate(Histogram_Variable_List): print(" -- -- {}".format(var.name)) ## Creating the histogram which will be filled hist_name = "{}_{}".format(var.name, wp.name) myhist = TH1D(hist_name, hist_name, var.nbins, var.xmin, var.xmax) myhist.SetStats(True) myhist.StatOverflows(True) ## Individual special plots if var.name == "XY": ## Plot the X histogram maths_string = Evaluation.TH1D_Maths_String( "X", *rec_and_truth_vars) execution = "{} >> {}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Add the y histogram maths_string = Evaluation.TH1D_Maths_String( "Y", *rec_and_truth_vars) execution = "{} >> +{}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") else: ## Get Maths Function from variable maths_string = Evaluation.TH1D_Maths_String( var.name, *rec_and_truth_vars) ## Drawing the tree and saving the hist to the matrix execution = "{} >> {}".format(maths_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) TH1D_output[v][w] = myhist ## Drawing the Profiles for v, (vx, vy) in enumerate(Profile_List): print(" -- -- {} vs {}".format(vx.name, vy.name)) ## Creating the profile which will be filled hist_name = "{}_vs_{}_{}".format(vx.name, vy.name, wp.name) myhist = TProfile(hist_name, hist_name, vx.nbins, vx.xmin, vx.xmax) if vy.reso: myhist.SetErrorOption('s') ## The x variable is called from its branch in the correct tree x_string = vx.tree + "." + vx.branch ## Individual special plots if vy.name == "XY": y_string = Evaluation.TH1D_Maths_String( "X", *rec_and_truth_vars) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") y_string = Evaluation.TH1D_Maths_String( "Y", *rec_and_truth_vars) execution = "{}:{} >>+{}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") elif vy.name == "AZ": z_x = "(alt_tree.ll_px)" z_y = "(alt_tree.ll_py)" z_pt = "(alt_tree.ll_pt)" x_string = z_pt y_string = "({rx}*{zx} + {ry}*{zy})/{zpt}".format( rx=Rec_Ex, ry=Rec_Ey, zx=z_x, zy=z_y, zpt=z_pt) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") else: y_string = Evaluation.TH1D_Maths_String( vy.name, *rec_and_truth_vars) execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff prof") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) Prof_output[v][w] = myhist if wp.name not in ["Network", "Tight"]: continue ## Drawing the TH2Ds for v, (vx, vy) in enumerate(TH2D_List): print(" -- -- {} vs {}".format(vx.name, vy.name)) ## Creating the profile which will be filled hist_name = "2D_{}_vs_{}_{}".format(vx.name, vy.name, wp.name) myhist = TH2D(hist_name, hist_name, vx.nbins2d, vx.xmin2d, vx.xmax2d, vy.nbins2d, vy.xmin2d, vy.xmax2d) x_string = Evaluation.TH1D_Maths_String( vx.name, *rec_and_truth_vars) y_string = Evaluation.TH1D_Maths_String( vy.name, *rec_and_truth_vars) ## The x variable is called from its branch in the correct tree if x_string is None: x_string = vx.tree + "." + vx.branch execution = "{}:{} >> {}".format(y_string, x_string, hist_name) wpt_tree.Draw(execution, weight_string, "goff") ## Saving the Histogram to the Matrix myhist.SetDirectory(0) TH2D_output[v][w] = myhist root_file.Close() """ _____ ____ ___ _____ ___ _ _ ____ | ____| | _ \ |_ _| |_ _| |_ _| | \ | | / ___| | _| | | | | | | | | | | | \| | | | _ | |___ | |_| | | | | | | | | |\ | | |_| | |_____| |____/ |___| |_| |___| |_| \_| \____| """ ## We now go through the 1D histograms and make them include overflow and underflow for v, var in enumerate(Histogram_Variable_List): for w, wp in enumerate(WorkingP_List): ## Include the overflow last_bin = TH1D_output[v][w].GetBinContent(nbins) overflow = TH1D_output[v][w].GetBinContent(nbins + 1) TH1D_output[v][w].SetBinContent(nbins, last_bin + overflow) TH1D_output[v][w].SetBinContent(nbins + 1, 0) ## Include the underflow first_bin = TH1D_output[v][w].GetBinContent(1) underflow = TH1D_output[v][w].GetBinContent(0) TH1D_output[v][w].SetBinContent(1, first_bin + underflow) TH1D_output[v][w].SetBinContent(0, 0) ## We create a tail distrobution if it is requested if var.tail: tail_temp = TH1D_output[v][w].GetCumulative(False) tail_temp.Scale(1 / tail_temp.GetBinContent(1)) Tail_output[v][w] = tail_temp ## We go through the resolution profiles and replace their entries with the RMSE for each bin for v, (vx, vy) in enumerate(Profile_List): if not vy.reso: continue for w, wp in enumerate(WorkingP_List): old_prof = Prof_output[v][w] bin_width = old_prof.GetBinWidth(1) name = "{}_vs_{}_{}_res".format(vx.name, vy.name, wp.name) new_prof = TGraphErrors(vx.nbins) new_prof.SetName(name) new_prof.SetTitle(name) new_prof.SetLineWidth(2) for b_idx in range(vx.nbins): new_prof.SetPoint(b_idx, old_prof.GetBinCenter(b_idx + 1), old_prof.GetBinError(b_idx + 1)) new_prof.SetPointError(b_idx, bin_width / 2, 0) Prof_output[v][w] = new_prof """ ____ _ __ __ ___ _ _ ____ / ___| / \ \ \ / / |_ _| | \ | | / ___| \___ \ / _ \ \ \ / / | | | \| | | | _ ___) | / ___ \ \ V / | | | |\ | | |_| | |____/ /_/ \_\ \_/ |___| |_| \_| \____| """ ## Creating the output directory output_dir = os.path.join(OUTPUT_dir, network_name, data_set_name[:-5]) if veto_all_jets: output_dir = output_dir + "_NOJETS" if require_jets: output_dir = output_dir + "_SOMEJETS" ## Check that the file can be saved if not os.path.exists(output_dir): os.system("mkdir -p " + output_dir) ## We create an output file for the histograms output_file = TFile(os.path.join(output_dir, "histograms.root"), "update") gFile = output_file ## We save the independants for v, var in enumerate(Independant_Variable_List): Indp_output[v].Write("", TObject.kOverwrite) ## We save the TH1Ds and tails for v, var in enumerate(Histogram_Variable_List): for w in range(len(WorkingP_List)): TH1D_output[v][w].Write("", TObject.kOverwrite) if var.tail: Tail_output[v][w].Write("", TObject.kOverwrite) ## We save the profiles for v in range(len(Profile_List)): for w in range(len(WorkingP_List)): Prof_output[v][w].Write("", TObject.kOverwrite) ## We save the TH2Ds for v in range(len(TH2D_List)): for w in range(len(WorkingP_List[:2])): TH2D_output[v][w].Write("", TObject.kOverwrite) output_file.Close() return 0