-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSeed.py
1616 lines (1357 loc) · 68.6 KB
/
findSeed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########################################################################################################################
##### code to make seeds from list of tracks #####
##### The track list must have this order: <BX> <pdgID> <trackId> <stave> <x> <y> <E> <weight> #####
##### Run this code: python findSeed.py -l <trackList.txt> -s <bool, true for signal positron> -e <energyCutinKeV> #####
##### written by: [email protected] #####
########################################################################################################################
import os
import sys
import time
import pprint
import math
from ROOT import *
from collections import OrderedDict
import argparse
import csv, array
import numpy as np
### needed for plotting of tracks and dimension from the spreadsheet
from makeTrackDiagrams import *
### the seed energy width
EseedMin = 2.0 # GeV
EseedMax = 15.0 # GeV
EseedMinPrelim = 0.2 # GeV
EseedMaxPrelim = 18.0 # GeV
### the seed pY width
PseedMin = -0.005 # GeV
PseedMax = 0.005 # GeV
###
xDipoleExitMax = 330.
### cut on nseeds
nseedsNoFitMax = 50
nseedsNoFitMax2 = 320
nseedsNoFitMax3 = 20000
nseedsNoFitMax4 = 250000
nseedsNoFitMax5 = 400000
#//MeV mass of electron/positron
meMeV = 0.5109989461
meGeV = meMeV/1000.
### me^2 in GeV
meGeV2 = meGeV*meGeV
### the width of the road around the pivot tracks
yAbsMargins = 0.13 # mm (a "road" of 200 microns around the line between r4 and r1)
xAbsMargins = 0.13 # mm (a "road" of 200 microns around the line between r4 and r1)
zPositionListInner = [z2inner, z3inner]
zPositionListOuter = [z2outer, z3outer]
ddList = []
#### This is the cutflow dictionary
cutFlowDict = OrderedDict(
[('noCut', 0),
('x1Gtx4',0),
('x1*x4Negative',0),
('z1Eqz4', 0),
('yDipoleExitGt5p4',0),
('xDipoleExitLt25',0),
('xDipoleExitGt165', 0),
('xDipoleExitLt0',0),
('seedEnergy', 0),
('checkClusterTracksMiddleLayers', 0),
('checkClusterFit', 0),
('trackEnergy', 0),
('checkClusterXDistance', 0),
('checkClusterTrackPy', 0),
('checkClusterTrackPyLoose', 0),
('checkClusterTrackPyTight', 0)]
)
histos = {'hSVDValues2Global': TH1D("hSVDValues2Global", "output of SVD[1]; fit quality [1]; Events", 200, 0, 0.3),
'hSVDValues3Global': TH1D("hSVDValues3Global", "output of SVD[2]; fit quality [2]; Events", 1000, 0, 0.1),
'hSVDValues2LooseGlobal': TH1D("hSVDValues2LooseGlobal", "output of SVD[1]; fit quality [1]; Events", 200, 0, 0.3),
'hSVDValues3LooseGlobal': TH1D("hSVDValues3LooseGlobal", "output of SVD[2]; fit quality [2]; Events", 1000, 0, 0.1),
'hSVDValues2TightGlobal': TH1D("hSVDValues2TightGlobal", "output of SVD[1]; fit quality [1]; Events", 200, 0, 0.3),
'hSVDValues3TightGlobal': TH1D("hSVDValues3TightGlobal", "output of SVD[2]; fit quality [2]; Events", 1000, 0, 0.1),
'hSeedDistanceGlobal': TH1D("hSeedDistanceGlobal", "seed distance wrt analytical line; d [m]; Entries", 500, 0, 0.1),
'hSeedDistanceLooseGlobal': TH1D("hSeedDistanceLooseGlobal", "seed distance wrt analytical line (loose); d [m]; Entries", 500, 0, 0.10),
'hSeedDistanceTightGlobal': TH1D("hSeedDistanceTightGlobal", "seed distance wrt analytical line (tight); d [m]; Entries", 500, 0, 0.10),
'hSVDValues2TightVsEnergyGlobal': TH2D("hSVDValues2TightVsEnergyGlobal", "output of SVD[1]; Energy [GeV]; fit quality [1]; Events", 35, 0, 17.5, 100, 0, 0.3),
'hSeedDistanceTightVsEnergyGlobal': TH2D("hSeedDistanceTightVsEnergyGlobal", "seed distance wrt analytical line (tight) vs Energy; Energy [GeV]; d[m]; Entries", 35, 0, 17.5, 100, 0, 0.10)
}
### read the dEdX vs E curve for the electron
results = csv.reader(open("dEdXSilicon_ForElectron.csv"), delimiter=",")
xdEdX = array.array('f',[]); ydEdX = array.array('f', [])
for eachRow in results:
xdEdX.append(float(eachRow[0]))
ydEdX.append(float(eachRow[1]))
### This is to find the dEdx
def getdEdX(energy):
### the dEdX vs E curve, x axis energy is in MeV, dEdX in y is in keV/um, remember the unit difference
mevEnergy = energy*1e3
interpolateddEdX = np.interp(mevEnergy, xdEdX, ydEdX)
### interpolateddEdX in keV/um
return interpolateddEdX
### This is to find the absorbed energy in the FPC layer and cooling region
def energyAbsorbed(staveId, energy, vtxZ):
if(staveId == 0 or staveId == 8):
zPos = z1inner
elif(staveId == 1 or staveId == 9):
zPos = z1outer
elif(staveId == 2 or staveId == 10):
zPos = z2inner
elif(staveId == 3 or staveId == 11):
zPos = z2outer
elif(staveId == 4 or staveId == 12):
zPos = z3inner
elif(staveId == 5 or staveId == 13):
zPos = z3outer
elif(staveId == 6 or staveId == 14):
zPos = z4inner
elif(staveId == 7 or staveId == 15):
zPos = z4outer
else:
print("No suitable stave found!!! Exiting")
quit()
### front side
if((zPos - vtxZ) > 0):
absorbingFactor = 150
### back side
else:
absorbingFactor = 250
### this dEdX is in keV/um, multiply by the width to get the energy loss
dEdX = getdEdX(energy)
absorbed = dEdX*absorbingFactor
remainingEnergy = energy*1e6 - absorbed
### print("staveId: ", staveId, " zPos: ", zPos, " vtxZ: ", vtxZ, " energy (keV): ", energy*1e6, "dEdX (keV/um): ", dEdX, " factor: ", absorbingFactor, " absorbed (keV): ", absorbed, " remainingEnergy (keV): ", remainingEnergy)
return remainingEnergy
### functions needed for distance cut from the analytical line in x1 vs x4 plane
## get R in m given momentum p
def getR(p):
R = p/(0.3*B)
return R
### get R^2 in m^2 given momentum p
def getR2(p):
R = getR(p)
return R*R
### get the xTangent in m given ZTangent in m and momentum p
def getXTangent(ZT,p):
R2 = getR2(p)
XT = (R2/LB-ZT)*(LB/math.sqrt(R2-LB2))
return XT
### get x in Sasha's coordinate in m given zTangent and momentum p
def getx(ZT,p):
XT = getXTangent(ZT,p)
R = getR(p)
x = R-XT
return x
### get r1 and r2 (x,y --> 2 points) given the Z0 and Z4. This is in m
def getR1R2(p1, p2):
r1 = [getx(Z0,p1), getx(Z4,p1)]
r2 = [getx(Z0,p2), getx(Z4, p2)]
#print("r1: ",r1, " r2: ",r2)
#quit()
return r1, r2
#### find the distance from the analytical line to the r1 and r2 point in m
def Distance(x0Test,x4Test,r1,r2):
aOb = -(r1[1]-r2[1])/(r1[0]-r2[0])
cOb = (r1[1]-r2[1])/(r1[0]-r2[0])*r1[0] - r1[1]
### now find the distance
# d = |a*x0Test + b*x4Test + c|/sqrt(a^2 + b^2) --> d = |(a/b)*x0Test + x4Test + (c/b)|/sqrt((a/b)^2 + 1^2)
d = abs(aOb*x0Test + x4Test + (cOb))/math.sqrt(aOb*aOb + 1)
return d
# get the unit vector along one vector
def rUnit2(r1, r2):
r = (r2-r1).Unit()
return r
### get the 3d line
def line3d(z,m_xz,c_xz,m_yz,c_yz):
# the intersection of those two planes and
# the function for the line would be:
# or:
x = (z - c_xz)/m_xz
y = (z - c_yz)/m_yz
return x,y
### get the TLorentzVector of the seed
def getSeedMomentum(r1, r4):
#### find the energy of the tracks
x0 = 0
z0 = zofx(r1, r4, x0)
xExit = abs(xofz(r1, r4, zDipoleActiveExit))
yExit = yofz(r1, r4, zDipoleActiveExit)
H = abs((zDipoleActiveExit-z0))*mm2m ### converting H from mm to m
xExitInM = xExit*mm2m ### converting xExit from mm to m
R = H*(LB)/xExitInM + xExitInM ### // This is the radius of curvature for a track
P = 0.3*B*R ### here B in Tesla and R in m
v1 = TVector2(r1[2], r1[1])
v4 = TVector2(r4[2], r4[1])
u = rUnit2(v1, v4)
uz = u.X()
uy = u.Y()
px = 0
py = P*uy
pz = P*uz
### The seed lorentz vector, need for energy plotting
p = TLorentzVector()
p.SetPxPyPzE(px, py, pz, math.sqrt(px*px + py*py + pz*pz + meGeV2))
return p, xExit, yExit
#### fit with chi2
def seed3dChi2fit(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4):
xList = [r1[0],r4[0]]
yList = [r1[1],r4[1]]
zList = [r1[2],r4[2]]
if len(r2Inner)>0:
xList.append(r2Inner[0])
yList.append(r2Inner[1])
zList.append(r2Inner[2])
if len(r2Outer)>0:
xList.append(r2Outer[0])
yList.append(r2Outer[1])
zList.append(r2Outer[2])
if len(r3Inner)>0:
xList.append(r3Inner[0])
yList.append(r3Inner[1])
zList.append(r3Inner[2])
if len(r3Outer)>0:
xList.append(r3Outer[0])
yList.append(r3Outer[1])
zList.append(r3Outer[2])
x = np.array(xList)
y = np.array(yList)
z = np.array(zList)
# this will find the slope and x-intercept of a plane
# parallel to the y-axis that best fits the data
A_xz = np.vstack((x, np.ones(len(x)))).T
result_xz = np.linalg.lstsq(A_xz, z,rcond=None)
m_xz, c_xz = result_xz[0]
residuals_xz = result_xz[1]
# again for a plane parallel to the x-axis
A_yz = np.vstack((y, np.ones(len(y)))).T
result_yz = np.linalg.lstsq(A_yz, z,rcond=None)
m_yz, c_yz = result_yz[0]
residuals_yz = result_yz[1]
zz = np.array([zDipoleActiveExit, r4[2]])
xx,yy = line3d(zz, m_xz, c_xz, m_yz, c_yz)
lfit = TPolyLine3D()
for i in range(2):
lfit.SetNextPoint(zz[i],yy[i],xx[i])
return residuals_xz,residuals_yz, lfit
### use svd fit
def seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4):
xList = [r1[0],r4[0]]
yList = [r1[1],r4[1]]
zList = [r1[2],r4[2]]
if len(r2Inner)>0:
xList.append(r2Inner[0])
yList.append(r2Inner[1])
zList.append(r2Inner[2])
if len(r2Outer)>0:
xList.append(r2Outer[0])
yList.append(r2Outer[1])
zList.append(r2Outer[2])
if len(r3Inner)>0:
xList.append(r3Inner[0])
yList.append(r3Inner[1])
zList.append(r3Inner[2])
if len(r3Outer)>0:
xList.append(r3Outer[0])
yList.append(r3Outer[1])
zList.append(r3Outer[2])
x = np.array(xList)
y = np.array(yList)
z = np.array(zList)
data = np.concatenate((x[:, np.newaxis],
y[:, np.newaxis],
z[:, np.newaxis]),
axis=1)
# Calculate the mean of the points, i.e. the 'center' of the cloud
datamean = data.mean(axis=0)
# Do an SVD on the mean-centered data (Singular Value Decomposition)
uu, dd, vv = np.linalg.svd(data - datamean)
# Now vv[0] contains the first principal component, i.e. the direction
# vector of the 'best fit' line in the least squares sense.
# Now generate some points along this best fit line, for plotting.
# I use -7, 7 since the spread of the data is roughly 14
# and we want it to have mean 0 (like the points we did
# the svd on). Also, it's a straight line, so we only need 2 points.
# linepts = vv[0] * np.mgrid[-7:7:2j][:, np.newaxis]
linepts = vv[0] * np.mgrid[-170:170:2j][:, np.newaxis]
# shift by the mean to get the line in the right place
linepts += datamean
#print("linepoints: ", linepts)
return linepts, dd ## dd is a 1D array of the data singular values
### draw the SVD fit
def drawFit(name,linepts,hits):
g = TGraph2D()
g.SetMarkerSize(3)
g.SetMarkerStyle(20)
g.SetMarkerColor(ROOT.kRed)
g.SetLineColor(ROOT.kRed)
for i in range(len(hits)):
g.SetPoint(i,hits[i][0],hits[i][1],hits[i][2])
g.GetXaxis().SetRangeUser(0, 650)
g.GetYaxis().SetRangeUser(-10,+10)
g.GetZaxis().SetRangeUser(3900, 4400)
lfit = TPolyLine3D()
for point in linepts:
lfit.SetNextPoint(point[0],point[1],point[2])
lfit.SetLineColor(ROOT.kBlue)
cnv = TCanvas("","",2000,2000)
view = TView.CreateView(1)
xviewmin = 3900 #if(isel(r1[0])) else xPsideL
xviewmax = 4200 #xEsideR if(isel(r1[0])) else 0
#view.SetAutoRange()
view.SetRange(0,-0.8, xviewmin , 650,+0.8,xviewmax)
view.ShowAxis()
g.Draw("p0")
lfit.Draw("same")
cnv.SaveAs(name)
### get the expected number of hits in the layer 2 and layer 3 of the tracker given the position of the track
def getExpectedHits(r1, r4, energy, side):
global xAbsMargins, yAbsMargins
if(energy < 4.0):
xAbsMargins = 0.22
yAbsMargins = 0.22
else:
xAbsMargins = 0.13
yAbsMargins = 0.13
expectedHits2Inner = 0
expectedHits2Outer = 0
expectedHits3Inner = 0
expectedHits3Outer = 0
r1min = [r1[0]-xAbsMargins, r1[1]-yAbsMargins, r1[2]]
r1max = [r1[0]+xAbsMargins, r1[1]+yAbsMargins, r1[2]]
r4min = [r4[0]-xAbsMargins, r4[1]-yAbsMargins, r4[2]]
r4max = [r4[0]+xAbsMargins, r4[1]+yAbsMargins, r4[2]]
# check possible clusters in layer 2, for both inner and outer stave
x2minInner = xofz(r1min, r4min, z2inner)
x2maxInner = xofz(r1max, r4max, z2inner)
x2minOuter = xofz(r1min, r4min, z2outer)
x2maxOuter = xofz(r1max, r4max, z2outer)
#/// check possible clusters in layer 3, for both inner and outer stave
x3minInner = xofz(r1min, r4min, z3inner)
x3maxInner = xofz(r1max, r4max, z3inner)
x3minOuter = xofz(r1min, r4min, z3outer)
x3maxOuter = xofz(r1max, r4max, z3outer)
x2Inner = xofz(r1, r4, z2inner)
x2Outer = xofz(r1, r4, z2outer)
x3Inner = xofz(r1, r4, z3inner)
x3Outer = xofz(r1, r4, z3outer)
for chipName, boundaries in xBoundaries.items():
if(side=="Positron"):
if "layerid2" in chipName and (boundaries[0] < x2Inner < boundaries[1]):
expectedHits2Inner += 1
if "layerid3" in chipName and (boundaries[0] < x2Outer < boundaries[1]):
expectedHits2Outer += 1
if "layerid4" in chipName and (boundaries[0] < x3Inner < boundaries[1]):
expectedHits3Inner += 1
if "layerid5" in chipName and (boundaries[0] < x3Outer < boundaries[1]):
expectedHits3Outer += 1
else:
if "layerid10" in chipName and (boundaries[0] < x2Inner < boundaries[1]):
expectedHits2Inner += 1
if "layerid11" in chipName and (boundaries[0] < x2Outer < boundaries[1]):
expectedHits2Outer += 1
if "layerid12" in chipName and (boundaries[0] < x3Inner < boundaries[1]):
expectedHits3Inner += 1
if "layerid13" in chipName and (boundaries[0] < x3Outer < boundaries[1]):
expectedHits3Outer += 1
return expectedHits2Inner, expectedHits2Outer, expectedHits3Inner, expectedHits3Outer
### check if there are hits along one track in layer 2 and layer 3
def check_clusters(r1, r4, allR2Inner, allR2Outer, allR3Inner, allR3Outer, side, energy):
global ddList, xAbsMargins, yAbsMargins
expectedHits2Inner, expectedHits2Outer, expectedHits3Inner, expectedHits3Outer = getExpectedHits(r1, r4, energy, side)
hitsOnRoad2Inner = 0 ### how many tracks accepted in the road along the r1 and r4
hitsOnRoad2Outer = 0
hitsOnRoad3Inner = 0 ### how many tracks accepted in the road along the r1 and r4
hitsOnRoad3Outer = 0
if(energy < 4.0):
xAbsMargins = 0.22
yAbsMargins = 0.22
else:
xAbsMargins = 0.13
yAbsMargins = 0.13
r1min = [r1[0]-xAbsMargins, r1[1]-yAbsMargins, r1[2]]
r1max = [r1[0]+xAbsMargins, r1[1]+yAbsMargins, r1[2]]
r4min = [r4[0]-xAbsMargins, r4[1]-yAbsMargins, r4[2]]
r4max = [r4[0]+xAbsMargins, r4[1]+yAbsMargins, r4[2]]
# check possible clusters in layer 2, for both inner and outer stave
y2minInner = yofz(r1min, r4min, z2inner)
y2maxInner = yofz(r1max, r4max, z2inner)
x2minInner = xofz(r1min, r4min, z2inner)
x2maxInner = xofz(r1max, r4max, z2inner)
y2minOuter = yofz(r1min, r4min, z2outer)
y2maxOuter = yofz(r1max, r4max, z2outer)
x2minOuter = xofz(r1min, r4min, z2outer)
x2maxOuter = xofz(r1max, r4max, z2outer)
### separate out staves for allR2, work on the inner stave of layer 2
accept2Inner = False
accept2Outer = False
innerR2FromMatching = []
outerR2FromMatching = []
for i2 in range(0, len(allR2Inner)):
### remember allR2 has x, y, z and E saved in the list
accept2yzInner = ( (allR2Inner[i2][1] >= y2minInner and allR2Inner[i2][1] <= y2maxInner) )
if(not accept2yzInner): continue
accept2xzInner = ( ( allR2Inner[i2][0] >= x2minInner and allR2Inner[i2][0] <= x2maxInner ) )
if(not accept2xzInner): continue
accept2Inner = True
hitsOnRoad2Inner += 1
innerR2FromMatching.append(allR2Inner[i2]) ### collect all matched tracks from layer 2, remove the break
### separate out staves for allR2
for i2 in range(0, len(allR2Outer)):
### remember allR2 has x, y, z and E saved in the list
accept2yzOuter = ( (allR2Outer[i2][1] >= y2minOuter and allR2Outer[i2][1] <= y2maxOuter) )
if(not accept2yzOuter): continue
accept2xzOuter = ( ( allR2Outer[i2][0] >= x2minOuter and allR2Outer[i2][0] <= x2maxOuter ) )
if(not accept2xzOuter): continue
accept2Outer = True
hitsOnRoad2Outer += 1
outerR2FromMatching.append(allR2Outer[i2]) ### collect all matched tracks from layer 2, remove the break
#/// check possible clusters in layer 3, for both inner and outer stave
y3minInner = yofz(r1min, r4min, z3inner) ### z for inner stave layer 3 is 4064.5125
y3maxInner = yofz(r1max, r4max, z3inner)
x3minInner = xofz(r1min, r4min, z3inner)
x3maxInner = xofz(r1max, r4max, z3inner)
y3minOuter = yofz(r1min, r4min, z3outer) ### z for Outer stave layer 3 is 4076.5125
y3maxOuter = yofz(r1max, r4max, z3outer)
x3minOuter = xofz(r1min, r4min, z3outer)
x3maxOuter = xofz(r1max, r4max, z3outer)
### separate out staves for allR3, work on the inner stave of layer 3
accept3Inner = False
accept3Outer = False
innerR3FromMatching = []
outerR3FromMatching = []
for i3 in range(0, len(allR3Inner)):
### remember allR3 has x, y, z and E saved in the list
accept3yzInner = ( (allR3Inner[i3][1] >= y3minInner and allR3Inner[i3][1] <= y3maxInner) )
if(not accept3yzInner): continue
accept3xzInner = ( ( allR3Inner[i3][0] >= x3minInner and allR3Inner[i3][0] <= x3maxInner ) )
if(not accept3xzInner): continue
accept3Inner = True
hitsOnRoad3Inner += 1
innerR3FromMatching.append(allR3Inner[i3]) ### collect all matched tracks from layer 3, remove the break
#break
### separate out staves for allR3
for i3 in range(0, len(allR3Outer)):
### remember allR3 has x, y, z and E saved in the list
accept3yzOuter = ( (allR3Outer[i3][1] >= y3minOuter and allR3Outer[i3][1] <= y3maxOuter) )
if(not accept3yzOuter): continue
accept3xzOuter = ( ( allR3Outer[i3][0] >= x3minOuter and allR3Outer[i3][0] <= x3maxOuter ) )
if(not accept3xzOuter): continue
accept3Outer = True
hitsOnRoad3Outer += 1
outerR3FromMatching.append(allR3Outer[i3]) ### collect all matched tracks from layer 3, remove the break
### find the number of actual matched tracks
nMatched = (expectedHits2Inner<=hitsOnRoad2Inner)+(expectedHits2Outer<=hitsOnRoad2Outer)+(expectedHits3Inner<=hitsOnRoad3Inner)+(expectedHits3Outer<=hitsOnRoad3Outer)
### find the number of expected tracks
nExpected = expectedHits2Inner + expectedHits2Outer + expectedHits3Inner + expectedHits3Outer
### only if we have hit in one of the two inner layer of the tracker, we select the seed track, otherwise we reject the track
if(nMatched>=3):
return nMatched, nExpected, innerR2FromMatching, outerR2FromMatching, innerR3FromMatching, outerR3FromMatching
else:
return nMatched, nExpected, [], [], [], []
#### prepare the SVD fit for the seed track
def makeSeedFit(r1, r4, nMatched, nExpected, innerR2FromMatching, outerR2FromMatching, innerR3FromMatching, outerR3FromMatching, nseedsNoFit):
### now check if the SVD match works
allDDList = {'dd':[], 'linepts':[], 'i2Inner':[], 'i2Outer':[], 'i3Inner':[], 'i3Outer':[]}
#### number of matches tracks in each of the staves of the layer 2 and layer 3
n2i = len(innerR2FromMatching)
n2o = len(outerR2FromMatching)
n3i = len(innerR3FromMatching)
n3o = len(outerR3FromMatching)
if(n2i> 0) :
for i2Inner in range(n2i):
r2Inner = innerR2FromMatching[i2Inner]
if(n2o > 0):
for i2Outer in range(n2o):
r2Outer = outerR2FromMatching[i2Outer]
if(n3i > 0):
for i3Inner in range(n3i):
r3Inner = innerR3FromMatching[i3Inner]
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r3Inner = []
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r2Outer = []
if(n3i > 0):
for i3Inner in range(n3i):
r3Inner = innerR3FromMatching[i3Inner]
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r3Inner = []
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(i2Inner)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r2Inner = []
if(n2o > 0):
for i2Outer in range(n2o):
r2Outer = outerR2FromMatching[i2Outer]
if(n3i > 0):
for i3Inner in range(n3i):
r3Inner = innerR3FromMatching[i3Inner]
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r3Inner = []
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(i2Outer)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r2Outer = []
if(n3i > 0):
for i3Inner in range(n3i):
r3Inner = innerR3FromMatching[i3Inner]
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
r3Outer = []
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(i3Inner)
allDDList['i3Outer'].append(-1)
allDDList['linepts'].append(lfitpts)
else:
r3Inner = []
if(n3o > 0):
for i3Outer in range(n3o):
r3Outer = outerR3FromMatching[i3Outer]
lfitpts, dd = seed3dfitSVDWithList(r1,r2Inner, r2Outer, r3Inner, r3Outer, r4)
allDDList['dd'].append(dd.tolist())
allDDList['i2Inner'].append(-1)
allDDList['i2Outer'].append(-1)
allDDList['i3Inner'].append(-1)
allDDList['i3Outer'].append(i3Outer)
allDDList['linepts'].append(lfitpts)
else:
return False, {}
### start with a very high value, in the end we will take the smallest one
ddValue0 = 1e11
ddValue1 = 1e11
ddValue2 = 1e11
### the index of the best matched track
iWinner = -1
#print("ddList:", allDDList)
for i in range(len(allDDList['dd'])):
### calculate the seed energy
xDipoleFromFit = xofz(allDDList['linepts'][i][0], allDDList['linepts'][i][1], zDipoleExit)*mm2m
xLayer4FromFit = xofz(allDDList['linepts'][i][0], allDDList['linepts'][i][1], r4[2])*mm2m
yDipoleFromFit = yofz(allDDList['linepts'][i][0], allDDList['linepts'][i][1], zDipoleExit)*mm2m
yLayer4FromFit = yofz(allDDList['linepts'][i][0], allDDList['linepts'][i][1], r4[2])*mm2m
### here r1 and r4 must be in mm
R1 = [xDipoleFromFit*m2mm, yDipoleFromFit*m2mm, zDipoleExit]
R4 = [xLayer4FromFit*m2mm, yLayer4FromFit*m2mm, r4[2]]
#### re-evaluate the seed energy from the best fit track
pSeed, xExit, yExit = getSeedMomentum(R1, R4)
ddValue = allDDList['dd'][i]
#print("ddValue1", ddValue)
histos['hSVDValues2Global'].Fill(ddValue[1])
histos['hSVDValues3Global'].Fill(ddValue[2])
if(nMatched==4):
histos['hSVDValues2TightGlobal'].Fill(ddValue[1])
histos['hSVDValues3TightGlobal'].Fill(ddValue[2])
histos['hSVDValues2TightVsEnergyGlobal'].Fill(pSeed.E(),ddValue[1])
else:
histos['hSVDValues2LooseGlobal'].Fill(ddValue[1])
histos['hSVDValues3LooseGlobal'].Fill(ddValue[2])
#if ((0.0 < ddValue[1] < ddValueCut1) and (0.0 < ddValue[2] < 0.01)):
#if ((0.0 < ddValue[1] < 0.1) and (0.0 < ddValue[2] < 0.05)):
### printing out py
#print("-->before cut: ddValue1: ", ddValue[1], " ddValue2 :", ddValue[2], " and pSeed.E() ", pSeed.E(), " nseedsNoFit: ", nseedsNoFit, " nMatched: ",nMatched)
### high multiplicity
### high multiplicity
if(nseedsNoFit > nseedsNoFitMax5):
if ((0.0 < ddValue[1] < 0.054) and (0.0 < ddValue[2] < 0.01)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
elif(nseedsNoFitMax4 < nseedsNoFit <= nseedsNoFitMax5):
if ((0.0 < ddValue[1] < 0.060) and (0.0 < ddValue[2] < 0.012)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
elif(nseedsNoFitMax3 < nseedsNoFit <= nseedsNoFitMax4):
if ((0.0 < ddValue[1] < 0.063) and (0.0 < ddValue[2] < 0.015)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### moderate multiplicity
elif(nseedsNoFitMax2 < nseedsNoFit <= nseedsNoFitMax3):
if ((0.0 < ddValue[1] < 0.067) and (0.0 < ddValue[2] < 0.018)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
elif(nseedsNoFitMax < nseedsNoFit <= nseedsNoFitMax2):
### energy < 4 GeV
if pSeed.E() < 4.0:
if ((0.0 < ddValue[1] < 0.08) and (0.0 < ddValue[2] < 0.02)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### energy > 4 GeV
else:
if ((0.0 < ddValue[1] < 0.05) and (0.0 < ddValue[2] < 0.01)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
#### medium multiplicity
elif(nseedsNoFitMax < nseedsNoFit <= nseedsNoFitMax2):
### energy < 4 GeV
if(pSeed.E()) < 4:
if ((0.0 < ddValue[1] < 0.06) and (0.0 < ddValue[2] < 0.05)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### energy > 4 GeV
else:
if ((0.0 < ddValue[1] < 0.06) and (0.0 < ddValue[2] < 0.06)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### low multiplicity
else:
### tight tracks
if(nMatched == 4):
### energy < 4 GeV
if(pSeed.E() < 4):
if ((0.0 < ddValue[1] < 0.1) and (0.0 < ddValue[2] < 0.1)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### energy > 4 GeV
else:
if ((0.0 < ddValue[1] < 0.1) and (0.0 < ddValue[2] < 0.1)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### loose tracks
else:
### energy < 4 GeV
if(pSeed.E() < 4.):
if ((0.0 < ddValue[1] < 0.2) and (0.0 < ddValue[2] < 0.05)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
### energy > 4 GeV
else:
if ((0.0 < ddValue[1] < 0.065) and (0.0 < ddValue[2] < 0.065)):
if ddValue[1] < ddValue1:
ddValue0 = ddValue[0]
ddValue1 = ddValue[1]
ddValue2 = ddValue[2]
iWinner = i
#print("-->after cut: ddValue1: ", ddValue[1], " ddValue2 :", ddValue[2], " and pSeed.E() ", pSeed.E(), " nseedsNoFit: ", nseedsNoFit, " nMatched: ",nMatched)
### only if a good fit is available, return True
if iWinner >= 0:
#print("ddValue0: ", ddValue0, "ddValue1: ",ddValue1, "ddValue2: ", ddValue2)
return True, {"ddValue0": ddValue0, "ddValue1":ddValue1, "ddValue2": ddValue2, "linepts":allDDList['linepts'][iWinner], "r2Inner":allDDList['i2Inner'][iWinner], "r2Outer":allDDList['i2Outer'][iWinner], "r3Inner":allDDList['i3Inner'][iWinner], "r3Outer":allDDList['i3Outer'][iWinner]}
else:
return False, {}
### making the seeds from two tracks from innermost layer and outermost layer, check the seeding cuts in the process
def makeseedNoFit(r1, r4, allR2Inner, allR2Outer, allR3Inner, allR3Outer, side):
if(abs(r1[0]) >= abs(r4[0])):
return False, {} ### |x1| must be smaller than |x4|
if(r1[0]*r4[0] < 0):
return False, {} ### the x value should have the same sign, i.e. on the same side of the beam
if(r1[2] == r4[2]):
return False, {} ### if z1=z4..., this is also impossible
yDipoleExit = yofz(r1, r4, zDipoleExit)
xDipoleExit = xofz(r1, r4, zDipoleExit)
### The following cuts are coming because of the distribution of the signal
### the following cannot be 10.8mm/2 according to the signal tracks
if(abs(yDipoleExit) > 10.8/2):
return False, {}
### This is according to the signal x:y at the dipole exit
if(abs(xDipoleExit) < 20.0):
return False, {} # the track should point to |x|<~1.0 at the dipole exit
if(abs(xDipoleExit) > xDipoleExitMax/2):
return False, {}
### select only positive x or negative x accroding to the particle
if( (side=="Positron" and xDipoleExit < 0) or (side=="Electron" and xDipoleExit > 0)):
return False, {}
### get the momentum of the seed from the r1 and r4
p, xExit, yExit = getSeedMomentum(r1, r4)
### checking the track energy
if(p.E() < EseedMinPrelim or p.E() > EseedMaxPrelim):
return False, {}
### add a 2 GeV cut
if(p.E() < 2.0):
return False, {}
nMatched, nExpected, innerR2FromMatching, outerR2FromMatching, innerR3FromMatching, outerR3FromMatching = check_clusters(r1, r4, allR2Inner, allR2Outer, allR3Inner, allR3Outer, side, p.E())
if nMatched < 3:
return False, {}
return True, {}
### making the seeds from two tracks from innermost layer and outermost layer, check the seeding cuts in the process
def makeseed(r1, r4, allR2Inner, allR2Outer, allR3Inner, allR3Outer, side, r1GeV, r10GeV, useFit=1, nseedsNoFit=0):
cutFlowDict['noCut'] += 1
if(abs(r1[0]) >= abs(r4[0])):
return False, {} ### |x1| must be smaller than |x4|
cutFlowDict['x1Gtx4'] += 1
if(r1[0]*r4[0] < 0):
return False, {} ### the x value should have the same sign, i.e. on the same side of the beam
cutFlowDict['x1*x4Negative'] += 1
if(r1[2] == r4[2]):
return False, {} ### if z1=z4..., this is also impossible
cutFlowDict['z1Eqz4'] += 1
yDipoleExit = yofz(r1, r4, zDipoleExit)
xDipoleExit = xofz(r1, r4, zDipoleExit)
### The following cuts are coming because of the distribution of the signal
### the following cannot be 10.8mm/2 according to the signal tracks
if(abs(yDipoleExit) > 10.8/2):
return False, {}