10 import eventLabelToLepton
as eltl
11 from collections
import defaultdict
13 if __name__ ==
"__main__":
14 parser = argparse.ArgumentParser()
15 parser.add_argument(
'--proto', type=str, required=
True)
16 parser.add_argument(
'--model', type=str, required=
True)
17 parser.add_argument(
'--leveldb', type=str, required=
True)
18 parser.add_argument(
'--hdf5', type=str, required=
False)
19 parser.add_argument(
'--output', type=str, required=
False)
20 args = parser.parse_args()
24 matrix = defaultdict(int)
27 net = caffe.Net(args.proto, args.model, caffe.TEST)
29 db = leveldb.LevelDB(args.leveldb)
31 if args.hdf5
is not None:
32 fileNameH = args.hdf5+
".0.h5" 33 h_f = h5py.File(fileNameH,
"r") 34 a_group_key = h_f.keys()[0] 35 h_data = list(h_f[a_group_key]) 43 t = ROOT.TTree(
't1',
'tree with histos' )
44 if args.output
is None:
45 fout = ROOT.TFile(
"output.root",
"RECREATE")
47 print(
"Output name: %s" % args.output)
48 fout = ROOT.TFile(
"%s.root" % args.output,
"RECREATE")
51 arrpidProton=np.zeros(1, dtype=float)
52 arrpidPion=np.zeros(1, dtype=float)
53 arrpidGamma=np.zeros(1, dtype=float)
54 arrpidMuon=np.zeros(1, dtype=float)
55 arrpidElectron=np.zeros(1, dtype=float)
56 arrpidPiZero=np.zeros(1, dtype=float)
57 arrpidNeutron=np.zeros(1, dtype=float)
59 truelabel = np.zeros(1, dtype=int)
60 truelabelall = np.zeros(1, dtype=int)
61 selectedlabel = np.zeros(1, dtype=int)
63 nhit = np.zeros(1, dtype=int)
65 tVertX = np.zeros(1, dtype=float)
66 tVertY = np.zeros(1, dtype=float)
67 tVertZ = np.zeros(1, dtype=float)
69 t.Branch(
'truelabelall', truelabelall,
'truelabelall/I' )
70 t.Branch(
'truelabel', truelabel,
'truelabel/I' )
71 t.Branch(
'selectedlabel', selectedlabel,
'selectedlabel/I')
72 t.Branch(
'nhit', nhit,
'nhit/I' )
73 t.Branch(
'tvertx', tVertX,
'tvertx/D' )
74 t.Branch(
'tverty', tVertY,
'tverty/D' )
75 t.Branch(
'tvertz', tVertZ,
'tvertz/D' )
76 t.Branch(
'proton', arrpidProton,
'proton/D' )
77 t.Branch(
'pion', arrpidPion,
'pion/D' )
78 t.Branch(
'gamma', arrpidGamma,
'gamma/D' )
79 t.Branch(
'muon', arrpidMuon,
'muon/D' )
80 t.Branch(
'electron', arrpidElectron,
'electron/D' )
81 t.Branch(
'pizero', arrpidPiZero,
'pizero/D' )
82 t.Branch(
'neutron', arrpidNeutron,
'neutron/D' )
88 for key, value
in db.RangeIter():
92 datum = caffe.proto.caffe_pb2.Datum()
93 datum.ParseFromString(value)
94 label =
int(datum.label)
96 truelabel[0]=eltl.labelToLepton(label)
98 image = caffe.io.datum_to_array(datum)
99 image = image.astype(np.uint8)
101 nhit[0]=np.count_nonzero(np.asarray([image]))
103 if args.hdf5
is not None:
104 if (count + 1) > h_size:
105 fileCountH=fileCountH+1
106 fileNameHadd = args.hdf5+
"."+
str(fileCountH)+
".h5" 107 hadd_f = h5py.File(fileNameHadd,
"r") 108 a_group_key_add = hadd_f.keys()[0] 109 h_data = list(hadd_f[a_group_key_add]) 111 h_size=h_size+len(h_data) 114 tVertX[0]=h_data[h_count][0] 115 tVertY[0]=h_data[h_count][1] 116 tVertZ[0]=h_data[h_count][2] 118 out = net.forward_all(data=np.asarray([image])) 120 plabel = int(out['prob'][0].argmax(axis=0))
121 selectedlabel[0]=plabel
123 arrpidProton[0]=out[
'prob'][0][2]
124 arrpidPion[0]=out[
'prob'][0][4]
125 arrpidGamma[0]=out[
'prob'][0][6]
126 arrpidMuon[0]=out[
'prob'][0][1]
127 arrpidElectron[0]=out[
'prob'][0][0]
128 arrpidNeutron[0]=out[
'prob'][0][3]
129 arrpidPiZero[0]=out[
'prob'][0][5]
133 h_count = h_count + 1
136 iscorrect = label == plabel
137 correct = correct + (1
if iscorrect
else 0)
138 matrix[(label, plabel)] += 1
139 labels_set.update([label, plabel])
145 sys.stdout.write(
"\rAccuracy: %.1f%% Progress: %.2f%%" % (100.*correct/count,100.*count/nEvents))
148 print(
str(correct) +
" out of " +
str(count) +
" were classified correctly")
150 fout.WriteTObject(t,
"t")