ATLAS Offline Software
Loading...
Searching...
No Matches
RTTAlgmain.py
Go to the documentation of this file.
1import os, sys
2import numpy as np
3import ROOT
4
5sys.path.append(os.path.join(os.path.dirname(__file__),'HistCompare'))
6print (sys.path)
7from RecExOnline.power_of_test import power_of_test
8from RecExOnline.utils import get_array
9import warnings
10warnings.filterwarnings("ignore")
11
12from RecExOnline.setupRTTAlg import *
13#YJ add for athena
14theApp.EvtMax = 1
15#YJ add for html generator (5test & 1 plot)* total his
16w = 6*len(histnames)
17h = 7
18h_th =0
19Matrix = [0 for x in range(w)]
20from collections import Counter, defaultdict
21#YJ for check time for rel
22
23import datetime
24currenttime=datetime.datetime.now()
25rel=currenttime.isoweekday()
26print ("Time for RTT Alg : %d" %currenttime.hour)
27if currenttime.hour>15:
28 rel=rel+1
29if rel==7:
30 rel=0
31print ("rel for RTT Alg : %d" %rel)
32
33#output format
34header = dict([("Object", max(len(h) for h in histnames)),("Test",max(len(t.split("_")[0]) for t in tests)),("p-value",21)])
35std_line = ["Object","Test","p-value"]
36#check system
37if not os.path.isdir(output_directory):
38 os.makedirs(output_directory)
39files = ROOT.TFile(ref_filename), ROOT.TFile(rtt_filename)
40trees = [file.Get(treename) for treename, file in zip([ref_treename, rtt_treename], files)]
41hist_pairs = [(histname, tuple(tree.Get(histname) for tree in trees)) for histname in histnames]
42
43output_file = ROOT.TFile(os.path.join(output_directory, "power_of_test.root"), "RECREATE")
44output_tree = ROOT.TTree("Tree", "Tree")
45output_tree.SetEntries(size)
46
47#main program
48print (std_line[0].ljust(header["Object"]), std_line[1].ljust(header["Test"]), std_line[2].center(header["p-value"]))
49print ("="*header["Object"], "="*header["Test"], "="*header["p-value"])
50for histname, hist_pair in hist_pairs:
51 output_tree.Branch("/"+histname.replace("/","."))
52 bins = np.intersect1d(*map(lambda hist: [hist.GetBinLowEdge(i) for i in range(1, hist.GetNbinsX()+2)], hist_pair))
53 for hist in hist_pair: hist.SetBins(bins.size-1, bins)
54 data1, data2 = map(get_array, hist_pair)
55 address = np.zeros(1, 'float32')
56 htmalhisname="tot_"+rtt_treename+"_"+histname.replace("/", "_")+".png"
57 Matrix[h_th]=("<img src=\"https://atlas-rtt.cern.ch/prod/rtt/rel_%d/20.11.X.Y.Z-VAL/build/x86_64-slc6-gcc49-opt/AtlasP1MON/RecExOnline/RecExOnline_User_Offline_test_v1/" +htmalhisname+"\" alt=\""+htmalhisname+"\" height=\"250\">") %rel
58 h_th+=1
59 for test, p_arr in power_of_test(data1+data2, data2, tests = tests, rvs_key = {'size' : size, 'freeze' : (False, True), 'entries' : data2.sum()}.copy(), parallel = False).items():
60 branchname = '.'.join([histname.replace('/','.'), test.split('_')[0]])
61 branch = output_tree.Branch(branchname, address, "F")
62 p_arr = np.array(p_arr).round(5)
63 if test == "anderson_ksamp":
64 p_arr[p_arr >= 1.2] = 0.
65 print ((histname if std_line[0]!=histname else "").ljust(header["Object"]), (test.split("_")[0] if std_line[1]!=test else "").ljust(header["Test"]), "{:>8} +/- {:<8} (%)".format(round(p_arr.mean()*100,1),round(p_arr.std()*100,2)))
66 Testresult= "%s %s:%s +- %s" % (histname,test ,p_arr.mean(),p_arr.std())
67 if test =="ks_2samp" and (p_arr.mean()+p_arr.std()<0.05):
68 Testresult= "<font color=\"red\">%s %s:%s +- %s </font>" % (histname,test ,p_arr.mean(),p_arr.std())
69 Matrix[h_th]= Testresult
70 h_th+=1
71 print ("matrix index check : %d "%(h_th))
72 sys.stdout.flush()
73 std_line[:2] = histname, test
74 for p in p_arr:
75 address[0] = p
76 branch.Fill()
77output_file.Write("", 2)
78for f in files: f.Close()
79output_file.Close()
80print (Matrix)
81
82
83
84
85f_html = open('RTTAlg.html', 'w')
86f_html.write("<table> \n")
87for nprint in range(w):
88 f_html.write("<tr> \n")
89 f_html.write("<td>" + Matrix[nprint] + "</td> \n")
90 f_html.write("</tr> \n")
91f_html.write("</table> \n")
#define max(a, b)
Definition cfImp.cxx:41
STL class.