|
1 | 1 | import numpy as np |
2 | 2 | import os, re, shutil |
3 | 3 | import argparse |
| 4 | +import itertools |
4 | 5 | import ROOT as rt |
5 | 6 | from root_numpy import tree2array |
6 | 7 | from lib.histo_utilities import create_TH1D, create_TH2D |
7 | | -from lib.cebefo_style import cebefo_style |
| 8 | +from lib.cebefo_style import cebefo_style, Set_2D_colz_graphics |
| 9 | + |
| 10 | +donotdelete = [] |
8 | 11 |
|
9 | 12 |
|
10 | 13 |
|
@@ -55,6 +58,18 @@ def __init__(self, infile): |
55 | 58 | print 'Cleaning tracks with:', cuts[:-4] |
56 | 59 | else: |
57 | 60 | self.TracksCleaning['cuts'] = '' |
| 61 | + elif '-->TracksConsistency' in l[0]: |
| 62 | + self.TracksConsistency = {} |
| 63 | + self.TracksConsistency['Bad'] = 0 |
| 64 | + self.TracksConsistency['Checked'] = 0 |
| 65 | + for aux in l[1:]: |
| 66 | + if aux[:5] == 'RefCh': |
| 67 | + self.TracksConsistency['RefCh'] = int(aux[5:]) |
| 68 | + print 'Tracks consistency: ch', int(aux[5:]), 'will be checked' |
| 69 | + |
| 70 | + if aux == 'List': |
| 71 | + self.TracksConsistency['List'] = 1 |
| 72 | + |
58 | 73 | elif len(self.labels) == 0: |
59 | 74 | self.labels = l[1:] |
60 | 75 | else: |
@@ -107,6 +122,36 @@ def define_range_around_peak(h, perc = [0.4, 0.4], Range=[0.0, 99999.0]): |
107 | 122 | x_up = h.GetBinLowEdge(n_up) + h.GetBinWidth(n_up) |
108 | 123 | return x_low, x_up, n_pk |
109 | 124 |
|
| 125 | +def rootTH2_to_np(h, cut = None, Norm = False): |
| 126 | + nx = h.GetNbinsX() |
| 127 | + ny = h.GetNbinsY() |
| 128 | + |
| 129 | + arr = np.zeros((ny, nx)) |
| 130 | + pos = np.zeros((ny, nx, 2)) |
| 131 | + |
| 132 | + for ix in range(nx): |
| 133 | + for iy in range(ny): |
| 134 | + x = h.GetXaxis().GetBinCenter( ix+1 ); |
| 135 | + y = h.GetYaxis().GetBinCenter( iy+1 ); |
| 136 | + z = h.GetBinContent(h.GetBin(ix+1, iy+1)) |
| 137 | + |
| 138 | + if cut == None: |
| 139 | + arr[iy, ix] = z |
| 140 | + else: |
| 141 | + arr[iy, ix] = z if z > cut else 0 |
| 142 | + pos[iy, ix] = [x,y] |
| 143 | + return arr, pos |
| 144 | + |
| 145 | +def circle_filter(h_arr, cx, cy, rx, ry): |
| 146 | + p = 0 |
| 147 | + for i,j in itertools.product(np.arange(h_arr.shape[0]), np.arange(h_arr.shape[1])): |
| 148 | + if (float(cx-j)/rx)**2 + (float(cy-i)/ry)**2 < 1: |
| 149 | + p += h_arr[i-1, j-1] |
| 150 | + |
| 151 | + return p/(np.pi*rx*ry) |
| 152 | + |
| 153 | + |
| 154 | + |
110 | 155 | if __name__ == '__main__': |
111 | 156 | cebefo_style() |
112 | 157 |
|
@@ -152,7 +197,7 @@ def define_range_around_peak(h, perc = [0.4, 0.4], Range=[0.0, 99999.0]): |
152 | 197 | if os.path.isdir(save_loc): |
153 | 198 | if save_loc[-1] != '/': |
154 | 199 | save_loc += '/' |
155 | | - out_dir = save_loc + 'Run' + str(flag) + '_plots' |
| 200 | + out_dir = save_loc + 'Run' + str(flag) + '_DQM' |
156 | 201 | if os.path.exists(out_dir): |
157 | 202 | shutil.rmtree(out_dir) |
158 | 203 | os.mkdir(out_dir) |
@@ -187,6 +232,7 @@ def define_range_around_peak(h, perc = [0.4, 0.4], Range=[0.0, 99999.0]): |
187 | 232 | canvas['risetime'] = {} |
188 | 233 | canvas['wave'] = {} |
189 | 234 | canvas['pos'] = {} |
| 235 | + canvas['pos_sel'] = {} |
190 | 236 | canvas['w_pos'] = {} |
191 | 237 | canvas['t_res_raw'] = {} |
192 | 238 | canvas['space_corr'] = {} |
@@ -391,40 +437,180 @@ def define_range_around_peak(h, perc = [0.4, 0.4], Range=[0.0, 99999.0]): |
391 | 437 | dy = configurations.xy_center[1] |
392 | 438 | dx = configurations.xy_center[0] |
393 | 439 | width = configurations.xy_center[2] |
| 440 | + N_bins = [100, 100] |
394 | 441 | if 'PosRaw' in configurations.plots: |
395 | 442 | name = 'h_pos_'+str(k) |
396 | 443 | title = 'Track position at z_DUT[' + str(conf['idx_dut']) + '], for channel {} selected event'.format(k) |
397 | 444 |
|
398 | | - h = rt.TH2D(name, title, 100, -width+dx, width+dx, 100, -width+dy, width+dy) |
| 445 | + h = rt.TH2D(name, title, N_bins[0], -width+dx, width+dx, N_bins[1], -width+dy, width+dy) |
399 | 446 | h.SetXTitle('x [mm]') |
400 | 447 | h.SetYTitle('y [mm]') |
401 | 448 |
|
402 | 449 | chain.Project(name, var, selection) |
403 | 450 |
|
404 | 451 | canvas['pos'][k] = rt.TCanvas('c_pos_'+str(k), 'c_pos_'+str(k), 800, 600) |
405 | 452 | h.DrawCopy('colz') |
406 | | - |
407 | 453 | canvas['pos'][k].Update() |
408 | 454 | canvas['pos'][k].SaveAs(out_dir + '/PositionXY_raw_ch{:02d}.png'.format(k)) |
409 | 455 |
|
410 | | - if 'PosWeight' in configurations.plots: |
411 | | - name = 'h_weight_pos_'+str(k) |
412 | | - title = 'Track position at z_DUT[' + str(conf['idx_dut']) + '] weighted with channel {} selected event'.format(k) |
413 | | - h_w = rt.TH2D(name, title, 100, -width+dx, width+dx, 100, -width+dy, width+dy) |
414 | | - h_w.SetXTitle('x [mm]') |
415 | | - h_w.SetYTitle('y [mm]') |
416 | | - h_w.SetZTitle('Average Integral [pC]') |
| 456 | + if ('PosSel' in configurations.plots) or ('PosSel+' in configurations.plots): |
| 457 | + canvas['pos_sel'][k] = rt.TCanvas('c_pos_sel_'+str(k), 'c_pos_sel_'+str(k), 800, 600) |
| 458 | + |
| 459 | + name = 'h_pos_sel' |
| 460 | + title = 'Events average selection efficiency' |
| 461 | + h = rt.TH3D(name, title, N_bins[0], -width+dx, width+dx, N_bins[1], -width+dy, width+dy, 2, -0.5, 1.5) |
417 | 462 |
|
418 | | - weights = '('+ selection +') * amp[' + str(k) + ']' |
419 | | - chain.Project(name, var, weights) |
| 463 | + chain.Project(name, selection + ':' + var) |
| 464 | + h = h.Project3DProfile('yx') |
| 465 | + h.SetYTitle('y [mm]') |
| 466 | + h.SetXTitle('x [mm]') |
| 467 | + h.DrawCopy('colz') |
420 | 468 |
|
421 | | - if 'PosRaw' in configurations.plots: |
422 | | - h_w.Divide(h) |
| 469 | + # if hasattr(configurations, 'TracksConsistency'): |
| 470 | + # if configurations.TracksConsistency['RefCh'] == k: |
| 471 | + # x_mean = h.GetMean(1) |
| 472 | + # bx = h.GetXaxis().FindBin(x_mean) |
| 473 | + # x_sx = h.GetRMS(1) |
| 474 | + # bx_sx = h.GetXaxis().GetBinCenter(1) - h.GetXaxis().GetBinWidth(1)*0.51 + x_sx |
| 475 | + # bx_sx = h.GetXaxis().FindBin(bx_sx) |
| 476 | + # |
| 477 | + # y_mean = h.GetMean(2) |
| 478 | + # by = h.GetYaxis().FindBin(y_mean) |
| 479 | + # y_sx = h.GetRMS(2) |
| 480 | + # by_sx = h.GetYaxis().GetBinCenter(1) - h.GetYaxis().GetBinWidth(1)*0.51 + y_sx |
| 481 | + # by_sx = h.GetYaxis().FindBin(by_sx) |
| 482 | + # |
| 483 | + # arr_h, pos_h = rootTH2_to_np(h, cut=0.2) |
| 484 | + # |
| 485 | + # |
| 486 | + # best_scale = 0.5 |
| 487 | + # p_max = 0 |
| 488 | + # for s in np.arange(0.5, 3, 0.05): |
| 489 | + # p = circle_filter(arr_h, bx, by, s*bx_sx, s*by_sx) |
| 490 | + # print p |
| 491 | + # |
| 492 | + # if p > p_max: |
| 493 | + # p_max = p |
| 494 | + # best_scale = s |
| 495 | + # |
| 496 | + # print best_scale |
| 497 | + # |
| 498 | + # ell = rt.TEllipse(x_mean, y_mean, best_scale*x_sx, best_scale*y_sx) |
| 499 | + # ell.SetLineColor(6) |
| 500 | + # ell.SetLineStyle(9) |
| 501 | + # ell.SetLineWidth(2) |
| 502 | + # ell.SetFillStyle(0) |
| 503 | + # ell.Draw() |
| 504 | + # donotdelete.append(ell) |
| 505 | + |
| 506 | + |
| 507 | + |
| 508 | + if ('PosSel+' in configurations.plots) and ('shape' in conf.keys()): |
| 509 | + if not conf['shape'] == 'None': |
| 510 | + size = conf['shape'].split('x') |
| 511 | + |
| 512 | + x = h.GetXaxis().GetBinCenter(1) - h.GetXaxis().GetBinWidth(1)*0.51 + float(size[0]) |
| 513 | + nbx = h.GetXaxis().FindBin(x) |
| 514 | + |
| 515 | + y = h.GetYaxis().GetBinCenter(1) - h.GetYaxis().GetBinWidth(1)*0.51 + float(size[1]) |
| 516 | + nby = h.GetYaxis().FindBin(y) |
| 517 | + # print h.GetYaxis().GetBinCenter(1), y, nby |
| 518 | + |
| 519 | + # print nby, nbx |
| 520 | + # x_mean = h.GetMean(1) |
| 521 | + # x_border = [x_mean - 0.5*float(size[0]), x_mean + 0.5*float(size[0])] |
| 522 | + # bx_border = [h.GetXaxis().FindBin(x_border[0]), h.GetXaxis().FindBin(x_border[1])] |
| 523 | + # |
| 524 | + # y_mean = h.GetMean(1) |
| 525 | + # y_border = [y_mean - 0.5*float(size[0]), y_mean + 0.5*float(size[0])] |
| 526 | + # by_border = [h.GetXaxis().FindBin(y_border[0]), h.GetXaxis().FindBin(y_border[1])] |
| 527 | + |
| 528 | + arr_h, pos_h = rootTH2_to_np(h, cut=0.2) |
| 529 | + # print arr_h |
| 530 | + |
| 531 | + p_max = 0 |
| 532 | + idx_max = [0, 0] |
| 533 | + for iy in range(0, arr_h.shape[0]-nby): |
| 534 | + for ix in range(0, arr_h.shape[1]-nbx): |
| 535 | + p = np.sum(arr_h[iy:iy+nby, ix:ix+nbx]) |
| 536 | + |
| 537 | + if p > p_max: |
| 538 | + p_max = p |
| 539 | + idx_max = [iy, ix] |
| 540 | + |
| 541 | + avg_prob = p_max/(nbx*nby) |
| 542 | + print avg_prob |
| 543 | + |
| 544 | + if hasattr(configurations, 'TracksConsistency'): |
| 545 | + configurations.TracksConsistency['Checked'] += 1 |
| 546 | + x_mean = h.GetMean(1) |
| 547 | + bx = h.GetXaxis().FindBin(x_mean) |
| 548 | + x_sx = h.GetRMS(1) |
| 549 | + bx_sx = h.GetXaxis().GetBinCenter(1) - h.GetXaxis().GetBinWidth(1)*0.51 + x_sx |
| 550 | + bx_sx = h.GetXaxis().FindBin(bx_sx) |
| 551 | + |
| 552 | + y_mean = h.GetMean(2) |
| 553 | + by = h.GetYaxis().FindBin(y_mean) |
| 554 | + y_sx = h.GetRMS(2) |
| 555 | + by_sx = h.GetYaxis().GetBinCenter(1) - h.GetYaxis().GetBinWidth(1)*0.51 + y_sx |
| 556 | + by_sx = h.GetYaxis().FindBin(by_sx) |
| 557 | + |
| 558 | + p_circle = -1 |
| 559 | + if (by_sx != 0) and (bx_sx != 0): |
| 560 | + p_circle = circle_filter(arr_h, bx, by, 2*bx_sx, 2*by_sx) |
| 561 | + |
| 562 | + print p_circle |
| 563 | + if p_circle > avg_prob*0.75 or p_circle == -1: |
| 564 | + print 'Possilble random scatter shape' |
| 565 | + configurations.TracksConsistency['Bad'] += 1 |
| 566 | + |
| 567 | + |
| 568 | + |
| 569 | + iy, ix = idx_max |
| 570 | + # arr_filter = np.zeros_like(arr_h) |
| 571 | + # arr_filter[iy:iy+nby, ix:ix+nbx] = 1. |
| 572 | + # print arr_filter |
| 573 | + |
| 574 | + x_start = h.GetXaxis().GetBinCenter(ix+1) - 0.5*h.GetXaxis().GetBinWidth(ix+1) |
| 575 | + x_stop = h.GetXaxis().GetBinCenter(ix+nbx) + 0.5*h.GetXaxis().GetBinWidth(ix+nbx) |
| 576 | + y_start = h.GetYaxis().GetBinCenter(iy+1) - 0.5*h.GetYaxis().GetBinWidth(iy+1) |
| 577 | + y_stop = h.GetYaxis().GetBinCenter(iy+nby) + 0.5*h.GetYaxis().GetBinWidth(iy+nby) |
| 578 | + |
| 579 | + line.SetLineStyle(9) |
| 580 | + line.SetLineColor(6) |
| 581 | + line.SetLineWidth(2) |
| 582 | + line.DrawLine(x_start, y_start, x_stop, y_start) |
| 583 | + line.DrawLine(x_start, y_stop, x_stop, y_stop) |
| 584 | + line.DrawLine(x_start, y_start, x_start, y_stop) |
| 585 | + line.DrawLine(x_stop, y_start, x_stop, y_stop) |
| 586 | + |
| 587 | + Set_2D_colz_graphics() |
| 588 | + canvas['pos_sel'][k].Update() |
| 589 | + canvas['pos_sel'][k].SaveAs(out_dir + '/PositionXY_sel_ch{:02d}.png'.format(k)) |
| 590 | + |
| 591 | + |
| 592 | + |
| 593 | + |
| 594 | + if 'PosWeight' in configurations.plots: |
| 595 | + name = 'h_weight_pos_'+str(k) |
| 596 | + title = 'Track position at z_DUT[' + str(conf['idx_dut']) + '] weighted with channel {} selected event'.format(k) |
| 597 | + h_w = rt.TH2D(name, title, N_bins[0], -width+dx, width+dx, N_bins[1], -width+dy, width+dy) |
| 598 | + h_w.SetXTitle('x [mm]') |
| 599 | + h_w.SetYTitle('y [mm]') |
| 600 | + h_w.SetZTitle('Average Integral [pC]') |
423 | 601 |
|
424 | | - canvas['w_pos'][k] = rt.TCanvas('c_w_pos_'+str(k), 'c_w_pos_'+str(k), 800, 600) |
425 | | - h_w.DrawCopy('colz') |
426 | | - canvas['w_pos'][k].Update() |
427 | | - canvas['w_pos'][k].SaveAs(out_dir + '/PositionXY_amp_weight_ch{:02d}.png'.format(k)) |
| 602 | + h = rt.TH2D('h_amp_aux'+str(k), title, N_bins[0], -width+dx, width+dx, N_bins[1], -width+dy, width+dy) |
| 603 | + chain.Project('h_amp_aux'+str(k), var, selection) |
| 604 | + |
| 605 | + weights = '('+ selection +') * amp[' + str(k) + ']' |
| 606 | + chain.Project(name, var, weights) |
| 607 | + |
| 608 | + h_w.Divide(h) |
| 609 | + |
| 610 | + canvas['w_pos'][k] = rt.TCanvas('c_w_pos_'+str(k), 'c_w_pos_'+str(k), 800, 600) |
| 611 | + h_w.DrawCopy('colz') |
| 612 | + canvas['w_pos'][k].Update() |
| 613 | + canvas['w_pos'][k].SaveAs(out_dir + '/PositionXY_amp_weight_ch{:02d}.png'.format(k)) |
428 | 614 |
|
429 | 615 | '''=========================== End Selections ===========================''' |
430 | 616 | conf['sel'] = selection |
@@ -638,3 +824,23 @@ def create_regression_input(x, y, amp): |
638 | 824 |
|
639 | 825 | canvas['dt_corr'][k].Update() |
640 | 826 | canvas['dt_corr'][k].SaveAs(out_dir + '/TimeResolution_OneShot_ch{:02d}.png'.format(k)) |
| 827 | + |
| 828 | + '''End of channels loop''' |
| 829 | + if hasattr(configurations, 'TracksConsistency'): |
| 830 | + aux = float(configurations.TracksConsistency['Bad'])/configurations.TracksConsistency['Checked'] |
| 831 | + if aux > 0.3: |
| 832 | + print '\n\n============ Run to be discarted!!!!! ===============\n\n' |
| 833 | + |
| 834 | + if 'List' in configurations.TracksConsistency.keys(): |
| 835 | + f_aux = flag[1:] |
| 836 | + file = args.save_loc |
| 837 | + if aux > 0.3: |
| 838 | + file += 'TracksConsistency_Bad.txt' |
| 839 | + else: |
| 840 | + file += 'TracksConsistency_Good.txt' |
| 841 | + if not os.path.exists(file): |
| 842 | + os.system('touch '+file) |
| 843 | + if not (int(f_aux) in np.loadtxt(file).astype(np.int)): |
| 844 | + cmd = 'echo ' + f_aux + ' >> ' + file |
| 845 | + print cmd |
| 846 | + os.system(cmd) |
0 commit comments