-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultifragStuff.py
More file actions
1470 lines (1220 loc) · 48.7 KB
/
multifragStuff.py
File metadata and controls
1470 lines (1220 loc) · 48.7 KB
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
from math import *
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from simpleKinematics import *
from miscellaneous import *
import copy
from termcolor import colored
def makeTreeCompletion(binTreeDict):
makeInitialTreeCompletion(binTreeDict)
print("Trying to pull every line automatically")
generalList=getDirectFreeRoute(binTreeDict)
if generalList == []:
print("Error!!! Are you sure you filled \
the free part dict correctly?!")
return False
boolPull=pullEveryLine(binTreeDict,generalList)
if boolPull == True:
print("Success in pulling every line")
else:
print("Unsuccessful pull :'-(")
return False
fillBool=fillMajorSols(binTreeDict,generalList)
print("The fillBool is ",fillBool)
fillSecSolsAlongTree(binTreeDict)
#Put here the special vLine for the initial dict
def makeInitialTreeCompletion(binTreeDict):
"""Solves the system on its local CM system"""
fillInit(binTreeDict)
myDesc=fillTreeDescriptorL(binTreeDict)
# print("myDesc = ",myDesc)
# printTree(binTreeDict)
completeTree0(binTreeDict)
completeTree1(binTreeDict)
# completeTree2(binTreeDict)
def getInitSolsDict(binTreeDict):
#This is our free CM solution, we at least know this one! ;-)
vCM=binTreeDict["outRedVcm"]
sCenter=np.array([0.0,0.0,0.0])
centerStr=str(sCenter.tolist())
sphereSols={centerStr:{}}
vCMVect=np.array([0.0,0.0,vCM])
sphereSols[centerStr]["vLabSols"]=[vCMVect]
#The only case when lab and cm solutions are the same
sphereSols[centerStr]["vCMSols"]=[vCMVect]
sphereSols[centerStr]["vCMPair"]=[sCenter,vCMVect]
#Implementing the npCenter for stopping error propagation
sphereSols[centerStr]["npCenter"]=sCenter
return sphereSols
def getCompletedSolTree(treeNode,solsDict):
myMass=treeNode["fMass"]
treeNode["structType"]="goType"
for vCenterStr in solsDict:
#Get the CM vel of the system
# sysCMVel=str2NPArray(vCenterStr)
#Here we can override the sysCMVel that is less accurate
npCenter=solsDict[vCenterStr]["npCenter"] #More precise
sysCMVel=npCenter
print("npCenter = ",npCenter)
print("vCenterStr = ",vCenterStr)
myLabVelList=solsDict[vCenterStr]["vLabSols"]
solsDict[vCenterStr]["labEnergy"]=[]
solsDict[vCenterStr]["vCMSols"]=[]
solsDict[vCenterStr]["vCMMags"]=[] #4 debugging
solsDict[vCenterStr]["thetaPhi"]=[]
for myLabVel in myLabVelList:
vCentNorm=np.linalg.norm(myLabVel)
ECentSol=1.0/2.0*myMass*(vCentNorm/100.0)**2
solsDict[vCenterStr]["labEnergy"].append(ECentSol)
#Now the vel @ the CM system
myCMVel=myLabVel-sysCMVel
solsDict[vCenterStr]["vCMSols"].append(myCMVel)
#4 debugging
myCMMag=np.linalg.norm(myCMVel)
solsDict[vCenterStr]["vCMMags"].append(myCMMag)
#Getting the lab angles
thetaPhi=getThetaPhi(myLabVel)
solsDict[vCenterStr]["thetaPhi"].append(thetaPhi)
return solsDict
def fillInit(binTreeDict):
if binTreeDict == {}:
return
if "ELab" not in binTreeDict:#Do more error cheching...
return
mPro=binTreeDict["massP"]
mTar=binTreeDict["massT"]
binTreeDict["mass"]=mPro+mTar
ELab=binTreeDict["ELab"]
EcmList=getAllEcms(mPro,mTar,ELab)
inEcmAvail=EcmList[2]
binTreeDict["inEcmAvail"]=inEcmAvail
inEcmSys=EcmList[3]
binTreeDict["inEcmSys"]=inEcmSys
binTreeDict["inESum"]=inEcmAvail+inEcmSys
#Saving the beta
binTreeDict["inBVcm"]=getVelcm(mPro,mTar,ELab)[2]/c
inRedVcm=binTreeDict["inBVcm"]*100
binTreeDict["inRedVcm"]=inRedVcm
#This is only for setting a search range, the final vel won't be
#this high. This criteria will be improved eventually.
binTreeDict["inBVLabMax"]=binTreeDict["inRedVcm"]
def completeTree0(binTreeDict):
if binTreeDict == {}:
return
finalMass=getFinalMass(binTreeDict)
if finalMass != None:
binTreeDict["fMass"]=finalMass
qVal=getNodeQVal(binTreeDict) #Always None
if qVal != None:
print("filling tree with", qVal)
binTreeDict["Q"]=qVal
if "dictList" not in binTreeDict:
return
for e in binTreeDict["dictList"]:
completeTree0(e)
def completeTree1(binTreeDict):
if binTreeDict == {} or "name" not in binTreeDict:
return
qVal=getNodeQVal(binTreeDict)
if qVal != None:
binTreeDict["Q"]=qVal
print("0qVal was = ",qVal)
print("Node name is "+binTreeDict["name"])
inOutMass=getNodeInOutMass(binTreeDict)
if inOutMass != None:
print("filling tree with", inOutMass)
inMass,outMass=inOutMass
binTreeDict["inMass"]=inMass
binTreeDict["outMass"]=outMass
print("Also including the out energies")
inEcmSys=binTreeDict["inEcmSys"]
inEcmAvail=binTreeDict["inEcmAvail"]
if "leaf" in binTreeDict["descriptors"]:
return
exE1,exE2=getChildEx(binTreeDict)
locEx=0.0
if "exE" in binTreeDict:
locEx=binTreeDict["exE"]
outEcmSys=1.0*(inMass/outMass)*inEcmSys
binTreeDict["outEcmSys"]=outEcmSys
outEcmAvail=inEcmSys*(1-1.0*(inMass/outMass))+inEcmAvail+qVal+locEx-(exE1+exE2)
binTreeDict["outEcmAvail"]=outEcmAvail
binTreeDict["outEcmSum"]=outEcmSys+outEcmAvail
binTreeDict["outBVcm"]=getVelFromEAndM(outEcmSys,outMass)/c
outRedVcm=binTreeDict["outBVcm"]*100
binTreeDict["outRedVcm"]=outRedVcm
print("in "+binTreeDict["name"]+"outRedVcm = "+str(outEcmSys))
#This is only for setting a search range, the final vel won't be
#this high. This criteria will be improved eventually.
binTreeDict["outBVLabMax"]=binTreeDict["outRedVcm"]
BVcm=sqrt(2.0*outEcmAvail/outMass)#Leaving out *c for now
# dictNode["outBVcm"]=BVcm
# dictNode["outRedVcm"]=BVcm*100
# maxVel=binTreeDict["outBVLabMax"]
binTreeDict["outBVLabMax"]+=BVcm*100
childMasses=getChildMasses(binTreeDict)
if childMasses != None:
m1,m2=childMasses
print("qVal was = ",qVal)
print("locEx,outEcmAvail = ",locEx,outEcmAvail)
E1cm,E2cm=getEcmsFromECM2(m1,m2,outEcmAvail)
# maxVel=binTreeDict["outBVLabMax"]
maxVel=65 #This needs to be changed!!!!!
pushNewEcmAndVels2(E1cm,E2cm,binTreeDict["dictList"],maxVel)
# specialPushNewEcmAndVels(E1cm,E2cm,outEcmSys,binTreeDict["dictList"],maxVel)
# else:
# binTreeDict["outEcmSys"]=binTreeDict["inEcmSys"]
# binTreeDict["outEcmAvail"]=binTreeDict["inEcmAvail"]
if "dictList" not in binTreeDict:
return
for e in binTreeDict["dictList"]:
completeTree1(e)
def completeTree2(binTreeDict):
if binTreeDict == {}:
return
outEcmAvail=getAvailE(binTreeDict)
# outEcmAvail=binTreeDict["outEcmAvail"]
print("outEcmAvail = ",outEcmAvail)
# eAvail=21.4147 #for ground
# eAvail=40.0147 #for 18.6
# Q = -7.506343852104692
if "inMass" in binTreeDict:
print("#####################")
print("###inMass is present####")
print("#####################")
print("eAvail and inEcmAvail = %0.4f,%0.4f" % (outEcmAvail,binTreeDict["inEcmAvail"]))
if outEcmAvail != None:
print("the local name is = "+binTreeDict["name"])
print("outEcmAvail=%0.4f" %(outEcmAvail))
binTreeDict["EAvail"]=outEcmAvail
childMasses=getChildMasses(binTreeDict)
if childMasses != None:
m1,m2=childMasses
E1cm,E2cm=getEcmsFromECM2(m1,m2,outEcmAvail)
inEcmSys=binTreeDict["inEcmSys"]
maxVel=binTreeDict["outBVLabMax"]
pushNewEcmAndVels(E1cm,E2cm,binTreeDict["dictList"],maxVel)
if "dictList" not in binTreeDict:
return
for e in binTreeDict["dictList"]:
completeTree2(e)
def pushNewEcmAndVels(inE1cmAvail,inE2cmAvail,dictNode,maxVel):
m1=dictNode[0]["fMass"]
Q1,locEx1=0.0, 0.0
if "exE" in dictNode[0]:
locEx1=dictNode[0]["exE"]
if "Q" in dictNode[0]:
Q1=dictNode[0]["Q"]
dictNode[0]["inEcmSys"]=inE1cmAvail# +locEx1
BVcm1=sqrt(2.0*inE1cmAvail/m1)#Leaving out *c for now
dictNode[0]["inBVcm"]=BVcm1
dictNode[0]["inRedVcm"]=BVcm1*100
dictNode[0]["inBVLabMax"]=maxVel+BVcm1*100
m2=dictNode[1]["fMass"]
Q2,locEx2=0.0, 0.0
if "exE" in dictNode[1]:
locEx1=dictNode[1]["exE"]
if "Q" in dictNode[1]:
Q1=dictNode[1]["Q"]
dictNode[1]["inEcmSys"]=inE2cmAvail# +locEx2
BVcm2=sqrt(2.0*inE2cmAvail/m2)#Leaving out *c for now
dictNode[1]["inBVcm"]=BVcm2
dictNode[1]["inRedVcm"]=BVcm2*100
dictNode[1]["inBVLabMax"]=maxVel+BVcm2*100
def pushNewEcmAndVels2(inE1cmSys,inE2cmSys,dictNode,maxVel):
m1=dictNode[0]["fMass"]
Q1,locEx1=0.0, 0.0
if "exE" in dictNode[0]:
locEx1=dictNode[0]["exE"]
if "Q" in dictNode[0]:
Q1=dictNode[0]["Q"]
dictNode[0]["inEcmSys"]=inE1cmSys
dictNode[0]["inEcmAvail"]=0.0
BVcm1=sqrt(2.0*inE1cmSys/m1)#Leaving out *c for now
dictNode[0]["inBVcm"]=BVcm1
dictNode[0]["inRedVcm"]=BVcm1*100
dictNode[0]["inBVLabMax"]=maxVel+BVcm1*100
if "leaf" in dictNode[0]["descriptors"]:
dictNode[0]["outEcmSys"]=inE1cmSys
dictNode[0]["outEcmAvail"]=inE1cmSys
dictNode[0]["outBVcm"]=BVcm1
dictNode[0]["outRedVcm"]=BVcm1*100
dictNode[0]["outBVLabMax"]=maxVel+BVcm1*100
m2=dictNode[1]["fMass"]
Q2,locEx2=0.0, 0.0
if "exE" in dictNode[1]:
locEx1=dictNode[1]["exE"]
if "Q" in dictNode[1]:
Q1=dictNode[1]["Q"]
dictNode[1]["inEcmSys"]=inE2cmSys
dictNode[1]["inEcmAvail"]=0.0
BVcm2=sqrt(2.0*inE2cmSys/m2)#Leaving out *c for now
dictNode[1]["inBVcm"]=BVcm2
dictNode[1]["inRedVcm"]=BVcm2*100
dictNode[1]["inBVLabMax"]=maxVel+BVcm2*100
if "leaf" in dictNode[1]["descriptors"]:
dictNode[1]["outEcmSys"]=inE2cmSys
dictNode[1]["outEcmAvail"]=inE2cmSys
dictNode[1]["outBVcm"]=BVcm2
dictNode[1]["outRedVcm"]=BVcm2*100
dictNode[1]["outBVLabMax"]=maxVel+BVcm2*100
def specialPushNewEcmAndVels(inE1cmAvail,inE2cmAvail,inEcmSys,dictNode,maxVel):
print("pushing vals into "+dictNode[0]["name"]+" and "+dictNode[1]["name"])
m1=dictNode[0]["fMass"]
Q1,locEx1=0.0, 0.0
if "exE" in dictNode[0]:
locEx1=dictNode[0]["exE"]
if "Q" in dictNode[0]:
Q1=dictNode[0]["Q"]
myEcm=inE1cmAvail
# if "leaf" in dictNode[0]["descriptors"]:
# myEcm=inE1cmAvail+inEcmSys
dictNode[0]["inEcmSys"]=inEcmSys
dictNode[0]["inEcmAvail"]=myEcm
BVcm1=sqrt(2.0*myEcm/m1)#Leaving out *c for now
dictNode[0]["inBVcm"]=BVcm1
dictNode[0]["inRedVcm"]=BVcm1*100
dictNode[0]["inBVLabMax"]=maxVel+BVcm1*100
if "leaf" in dictNode[0]["descriptors"]:
dictNode[0]["outEcmSys"]=myEcm # +locEx1
dictNode[0]["outEcmAvail"]=myEcm # +locEx1
dictNode[0]["outBVcm"]=BVcm1
dictNode[0]["outRedVcm"]=BVcm1*100
dictNode[0]["outBVLabMax"]=maxVel+BVcm1*100
m2=dictNode[1]["fMass"]
Q2,locEx2=0.0, 0.0
if "exE" in dictNode[1]:
locEx1=dictNode[1]["exE"]
if "Q" in dictNode[1]:
Q1=dictNode[1]["Q"]
myEcm=inE2cmAvail
# if "leaf" in dictNode[1]["descriptors"]:
# myEcm=inE2cmAvail+inEcmSys
dictNode[1]["inEcmSys"]=inEcmSys# +locEx2
dictNode[1]["inEcmAvail"]=myEcm
BVcm2=sqrt(2.0*myEcm/m2)#Leaving out *c for now
dictNode[1]["inBVcm"]=BVcm2
dictNode[1]["inRedVcm"]=BVcm2*100
dictNode[1]["inBVLabMax"]=maxVel+BVcm2*100
if "leaf" in dictNode[1]["descriptors"]:
dictNode[1]["outEcmSys"]=myEcm # +locEx1
dictNode[1]["outEcmAvail"]=myEcm # +locEx1
dictNode[1]["outBVcm"]=BVcm2
dictNode[1]["outRedVcm"]=BVcm2*100
dictNode[1]["outBVLabMax"]=maxVel+BVcm2*100
def getStraightLinePoints(theta,phi,vLabMax,part=4000):
vArray=np.linspace(0,vLabMax,part)
myVectArray=np.zeros( (part,3) )
for i in range(part):
v=vArray[i]
x,y,z=spherToCart(v,theta,phi)
myVectArray[i]=np.array([x,y,z])
return myVectArray
def getTrainStatus(aPoint,aVRad,train):
trueList=[True for e in train]
falseList=[False for e in train]
boolList=[np.linalg.norm(t-aPoint)<aVRad\
for t in train]
if boolList == trueList:
return -1
if boolList == falseList:
return 1
return 0
def getTrainSolIdx(vPoint,vRad,vLine,i=0,\
direction="forward",tolerance=None,\
trainLen=2):
if direction == "forward":
dIncr=1
else:
dIncr=-1
lineMax=len(vLine)
if tolerance == None:
tolerance=lineMax
#Train derail
if i < 0 or i+trainLen > lineMax:
return None
train=vLine[i:i+trainLen]
trainStatus=getTrainStatus(vPoint,vRad,train)
while trainStatus != 0:
#Train derail
i+=dIncr
if i < 0 or i+trainLen >= lineMax:
return None
if tolerance <= 0:
return None
tolerance-=1
train=vLine[i:i+trainLen]
trainStatus=getTrainStatus(vPoint,vRad,train)
return i
def getMidPointLine(vLine1,vLine2,vRad,frac=0.5):
fD="forward"
bD="backward"
forTol=4
backTol=4
oldI=0
foundAny=False
midPLine=[]
for vP1 in vLine1:
if not foundAny:
i=getTrainSolIdx(vP1,vRad,vLine2,oldI,fD)
if i == None:
continue
else:
foundAny=True
oldI=i
continue
i=getTrainSolIdx(vP1,vRad,vLine2,oldI,fD,forTol)
if i == None:
# print("Trying backward sol")
i=getTrainSolIdx(vP1,vRad,vLine2,oldI,bD,backTol)
if i == None:
# print("No back sol found")
break
oldI=i
train=vLine2[i:i+2]
myP=getLinSolPoint(vP1,vRad,train)
midPoint=vP1*(1-frac)+myP*frac
midPLine.append(midPoint)
midPLine=np.array(midPLine)
return midPLine
def getLinSol(vP,vRad,train):
#Here we expect to use only 2 points
p0=train[0]
p1=train[-1] #expecting len(train)==2 so this works properly
A=np.linalg.norm(p0-vP)-vRad**2
C=np.linalg.norm(p1-p0)
B=np.linalg.norm(p1-vP)-A-C-vRad**2
tPlus=(-B+sqrt(B**2-4*A*C))/(2*A)
tMinus=(-B-sqrt(B**2-4*A*C))/(2*A)
return [tPlus,tMinus]
def getLinSolPoint(vP,vRad,train):
p0=train[0]
p1=train[-1]
linSol=getLinSol(vP,vRad,train)
t=None
for tValue in linSol:
if 0 <= tValue <= 1:
t=tValue
if t == None:
return None
myP=p0*(1-t)+t*p1
return myP
def pullEveryLine(binTreeDict,freePartListRoute):
if len(freePartListRoute)==0:
return True
freePartIndex=freePartListRoute[0]
branchIndex=getOtherVal(freePartIndex)
if branchIndex==None:
return False
tree2Fill=binTreeDict["dictList"][branchIndex]
pullBool=pullLinesFromNode(tree2Fill)
if pullBool==False:
return False
newBinTree=binTreeDict["dictList"][freePartIndex]
return pullEveryLine(newBinTree,freePartListRoute[1:])
def pullLinesFromNode(binTreeDict):
emptyNpA=np.array([])
if binTreeDict == {}:
return emptyNpA
if checkIfLastPartNode(binTreeDict) == True:
idx=findDetectIndex(binTreeDict["dictList"])
if idx == None:
return False
vLabMax=binTreeDict["outBVLabMax"]
theta,phi=binTreeDict["dictList"][idx]["angles"]
vLine=getStraightLinePoints(theta,phi,vLabMax)
binTreeDict["vLines"]=[vLine]
return True
dList=binTreeDict["dictList"]
boolVal1=pullLinesFromNode(dList[0])
if boolVal1 == False:
return False
boolVal2=pullLinesFromNode(dList[1])
if boolVal2 == False:
return False
vLines1=dList[0]["vLines"]
l1EmptyBoolVal=checkIfAllAreEmpty(vLines1)
vLines2=dList[1]["vLines"]
l2EmptyBoolVal=checkIfAllAreEmpty(vLines2)
if l1EmptyBoolVal or l2EmptyBoolVal:
return False
#Preparing to do line sweeps
childVels=getChildVels(binTreeDict)
if childVels == None:
return False
vLeft,vRight=childVels
vRad=vLeft+vRight
myFrac=vLeft/vRad
vLineList=[]
lineParentsIdxList=[[],[]]
offsetList=[[],[]]
#Sweep from line 1 to line 2
vLLIdx=0#keeping track of the indices
for i in range(len(vLines1)):
vLine1=vLines1[i]
for j in range(len(vLines2)):
vLine2=vLines2[j]
cmLine=getMidPointLine(vLine1,vLine2,vRad,myFrac)
vLineList.append(cmLine)
# vLLIdx=vLineList.index(cmLine)
offsets=getMidPOffsets(vLine1,vLine2,vRad)
offsetList[0].append([vLLIdx,offsets])
parentChildIdx1=i #vLines1.index(vLine1)
parentChildIdx2=j #vLines2.index(vLine2)
lineParentsIdxList[0].append([vLLIdx,[parentChildIdx1,parentChildIdx2]])
vLLIdx+=1
#Sweep from line 2 to line 1
for i in range(len(vLines2)):
vLine2=vLines2[i]
for j in range(len(vLines1)):
vLine1=vLines1[j]
cmLine=getMidPointLine(vLine2,vLine1,vRad,1-myFrac)
vLineList.append(cmLine)
# print("cmLine = ",cmLine)
# vLLIdx=vLineList.index(cmLine)
print("Line idx = ",vLLIdx)
offsets=getMidPOffsets(vLine2,vLine1,vRad)
parentChildIdx1=j #vLines1.index(vLine1)
parentChildIdx2=i #vLines2.index(vLine2)
offsetList[1].append([vLLIdx,offsets])
lineParentsIdxList[1].append([vLLIdx,[parentChildIdx1,parentChildIdx2]])
vLLIdx+=1
binTreeDict["vLines"]=vLineList
binTreeDict["offsets"]=offsetList
binTreeDict["lParentsIdxs"]=lineParentsIdxList
return True
def getSphereLineSols(vSCent,vSRad,vLine):
i=getTrainSolIdx(vSCent,vSRad,vLine,i=0)
cmNormVects=[]
while i != None:
train=vLine[i:i+2]
myP=getLinSolPoint(vSCent,vSRad,train)
myPInCM=myP-vSCent
norm=np.linalg.norm(myPInCM)
cmNormVects.append(myPInCM/norm)
i=getTrainSolIdx(vSCent,vSRad,vLine,i+1)
return np.array(cmNormVects)
def getSphereLineIdxSolsList(vSCent,vSRad,vLine):
"""Gets a list of all the solution indices in the line with the given
sphere
"""
i=getTrainSolIdx(vSCent,vSRad,vLine,i=0)
idxSols=[]
while i != None:
idxSols.append(i)
i=getTrainSolIdx(vSCent,vSRad,vLine,i+1)
return idxSols
def fillMajorSols(binTreeDic,freePartRoute,solsDict={}):
if binTreeDic["type"] == "initial":
solsDict=getInitSolsDict(binTreeDic)
#Filling the local node
binTreeDic["solsDict"]=solsDict
#now given the dictionary is partially pre-filled we now fill it
#with the corresponding solutions
solsDict=getCompletedSolTree(binTreeDic,solsDict)
if len(freePartRoute)==0:
return True
#Now figure out the branches to fill and to go, first we get the
#indices
freePartIndex=freePartRoute[0]
branchIndex=getOtherVal(freePartIndex)
if branchIndex==None:
return False
#The branches to fill (solve) and to go
branch2Solve=binTreeDic["dictList"][branchIndex]
branch2Go=binTreeDic["dictList"][freePartIndex]
vInfoList=getVInfoList(solsDict)
solsD4B2Solve={}
for importantL in vInfoList:
newCent=importantL[0]
pairCM=importantL[1]
newCentStr=npArray2Str(newCent)
solsD4B2Solve=getDictWithIdxs(branch2Solve,\
newCent,solsD4B2Solve)
if solsD4B2Solve == {}:
return False
solsD4B2Solve=getSolVelsEnergiesEtcInNode(branch2Solve,\
solsD4B2Solve)
branch2Solve["solsDict"]=solsD4B2Solve
vMagL=[branch2Solve["outRedVcm"],branch2Go["outRedVcm"]]
dict4Branch2Go=getComplementarySolsDict(solsD4B2Solve,vMagL)
fillBool=fillMajorSols(branch2Go,freePartRoute[1:],dict4Branch2Go)
return fillBool
def cleanDict(binTreeDict,freePartRoute):
#Figure out the branches to fill and to go, first we get the
#indices
freePartIndex=freePartRoute[0]
branchIndex=getOtherVal(freePartIndex)
if branchIndex==None:
return False
#The branches to fill (solve) and to go
branch2Solve=binTreeDict["dictList"][branchIndex]
branch2Go=binTreeDict["dictList"][freePartIndex]
#Reached the node b4 the free part
if len(freePartRoute)==1:
#Pushing the clnDict in branch2Go, this is the only case it is
#done. Also, it happens to be an exact copy of the branch2Go
#solsDict.
childClnSDict=copy.copy(branch2Go["solsDict"])
#The pushing...
branch2Go["clnSD"]=childClnSDict
#The cleanded solutions dictionary reference
clnSDR=getVCent4IdxSearchD(binTreeDict,freePartRoute)
print(colored("clnSDR = ","red"))
print(colored(clnSDR,"red"))
#Incorporating it in the current node.
binTreeDict["clnSDR"]=clnSDR
#Now, the childClnSDict should be used to clean the current
#node and also the branch2Solve solutions node.
clnSD=getClnSD(binTreeDict,clnSDR)
binTreeDict["clnSD"]=clnSD
print(colored("clnSD = ","blue"))
print(colored(clnSD,"blue"))
getIdxL4B2Solve(branch2Go,branch2Solve)
cleanRestB2Solve(branch2Solve)
return True
aBool=cleanDict(branch2Go,freePartRoute[1:])
clnSDR=getVCent4IdxSearchD(binTreeDict,freePartRoute)
print(colored("After a recursive call","magenta"))
print(colored("clnSDR = ","blue"))
print(colored(clnSDR,"blue"))
#Incorporating it in the current node.
binTreeDict["clnSDR"]=clnSDR
#Now, the childClnSDict should be used to clean the current
#node and also the branch2Solve solutions node.
clnSD=getClnSD(binTreeDict,clnSDR)
binTreeDict["clnSD"]=clnSD
getIdxL4B2Solve(branch2Go,branch2Solve)
cleanRestB2Solve(branch2Solve)
return True
# clnSD=getClnSD(binTreeDict,clnSDR)
# print(colored(clnSD,"blue"))
def getLocalCleanDict(b2SolD,b2GD,referenceDict):
#All of the dicts need a solsDict entry, the b2GD needs a vCMPairL sub entry
pass
def getCleanB2Sol1(initB2Sol,refDict):
cleanB2Sol={}
for b2SolEStr in initB2Sol:
print("Current element is ",b2SolEStr)
if b2SolEStr in refDict:
cleanB2Sol[b2SolEStr]=initB2Sol[b2SolEStr]
return cleanB2Sol
def getVInfoList(solsDict):
vInfoList=[]
for sphCentStr in solsDict:
centList=solsDict[sphCentStr]["vLabSols"]
vCMPairL=solsDict[sphCentStr]["vCMPair"]
for newVCenter,newPair in zip(centList,vCMPairL):
#Please note that the vLabSols are the new centers.
vInfoList.append([newVCenter,newPair])
return vInfoList
def getComplementarySolsDict(solsDict,vMagL):
compSolsDict={}
vMag2Sol,vMag2Go=vMagL
print("Inside getComplementarySolsDict entering the for")
for sphCentStr in solsDict:
#Convert this string to an np array
# vCenter=str2NPArray(sphCentStr)
vCenter=solsDict[sphCentStr]["npCenter"]#More precise
print("npCenter val is = ",solsDict[sphCentStr]["npCenter"])
myVCMList=solsDict[sphCentStr]["vCMSols"]
compSolsDict[sphCentStr]={"vLabSols":[],\
"vCMPair":[],\
"npCenter":solsDict[sphCentStr]["npCenter"]}
for vCMSub in myVCMList:
for vCM in vCMSub:
vNormCM=vCM/np.linalg.norm(vCM)
newestCentCM=-vNormCM*vMag2Go
newestCentLab=vCenter+newestCentCM
compSolsDict[sphCentStr]["vLabSols"]\
.append(newestCentLab)
compSolsDict[sphCentStr]["vCMPair"]\
.append([vCM,newestCentCM])
return compSolsDict
def getDictWithIdxs(treeNode,vSCent,sphSolsDict):
#Getting rid of the -0. It messes with the string convertion
vSCent[vSCent==0.] = 0.
centerStr=str(vSCent.tolist())
nodeVLines=treeNode["vLines"]
vSRad=treeNode["outRedVcm"]
solIdxList=[]
#To properly connect the solIdxList to its corresponding vLine
idxLineList=[]
for i in range(len(nodeVLines)):
vLine=nodeVLines[i]
lineInterIdxList=getSphereLineIdxSolsList(vSCent,vSRad,vLine)
if lineInterIdxList == []:
continue
solIdxList.append(lineInterIdxList)
idxLineList.append(i)
#this ensures the j var gets the correct index in the
#solIdxList
if solIdxList != []:
sphSolsDict[centerStr]={}
sphSolsDict[centerStr]["solIdxList"]=solIdxList
sphSolsDict[centerStr]["idxLineList"]=idxLineList
sphSolsDict[centerStr]["npCenter"]=vSCent
return sphSolsDict
def getSolVelsEnergiesEtcInNode(treeNode,sphereSolsDict):
myMass=treeNode["fMass"]
treeNode["structType"]="solveType"
print("Inside getSolVelsEnergiesEtcInNode entering the for")
for sphereCenterStr in sphereSolsDict:
#Surely some numerical errors lying around but I'll live with
#it for now
# myNpCenter=str2NPArray(sphereCenterStr)
myNpCenter=sphereSolsDict[sphereCenterStr]["npCenter"]#More precise
print("npCenter present in for ",sphereCenterStr)
print("sphere..npCenter = ", sphereSolsDict[sphereCenterStr]["npCenter"])
indexSolLists=sphereSolsDict[sphereCenterStr]["solIdxList"]
#The proper indices on the outside list
idxLineList=sphereSolsDict[sphereCenterStr]["idxLineList"]
for i in range(len(indexSolLists)):
solIdxSubList=indexSolLists[i]
#Here the "j" index corresponds to an intersection with a
#line (that is outside this sub dict) with the same index.
j=idxLineList[i]
myVLine=treeNode["vLines"][j]
solVelList=[]
solEList=[]
vCMSolList=[]
debugECMList=[]
thetaPhiList=[]
for vSolIndex in solIdxSubList:
velSol=myVLine[vSolIndex]
solVelList.append(velSol)
vNorm=np.linalg.norm(velSol)
ESol=1.0/2.0*myMass*(vNorm/100.0)**2
solEList.append(ESol)
vCMSol=velSol-myNpCenter
vCMSolList.append(vCMSol)
#ECM energies should be the same as in the first
debugVCMNorm=np.linalg.norm(vCMSol)
debugECM=1.0/2.0*myMass*(debugVCMNorm/100.0)**2
debugECMList.append(debugECM)
thetaPhi=getThetaPhi(velSol)
thetaPhiList.append(thetaPhi)
if "vLabSols" not in sphereSolsDict[sphereCenterStr]:
sphereSolsDict[sphereCenterStr]["vLabSols"]=[]
sphereSolsDict[sphereCenterStr]["vLabSols"].append(solVelList)
if "energyLabSols" not in sphereSolsDict[sphereCenterStr]:
sphereSolsDict[sphereCenterStr]["energyLabSols"]=[]
sphereSolsDict[sphereCenterStr]["energyLabSols"].append(solEList)
if "vCMSols" not in sphereSolsDict[sphereCenterStr]:
sphereSolsDict[sphereCenterStr]["vCMSols"]=[]
sphereSolsDict[sphereCenterStr]["vCMSols"].append(vCMSolList)
if "debugECMs" not in sphereSolsDict[sphereCenterStr]:
sphereSolsDict[sphereCenterStr]["debugECMs"]=[]
sphereSolsDict[sphereCenterStr]["debugECMs"].append(debugECMList)
if "thetaPhi" not in sphereSolsDict[sphereCenterStr]:
sphereSolsDict[sphereCenterStr]["thetaPhi"]=[]
sphereSolsDict[sphereCenterStr]["thetaPhi"].append(thetaPhiList)
return sphereSolsDict
def getMidPOffsets(vLine1,vLine2,vRad):
"""Returns the i,j indices in line 1 and 2 where the first
intersection is found using the CM values.
"""
j=0
foundAny=False
for i in range(len(vLine1)):
vP1=vLine1[i]
j=getTrainSolIdx(vP1,vRad,vLine2,j)
if j == None:
continue
return [i,j]
return None
def getVelSolutions(treeNode):
if "type" not in treeNode:
return False
if treeNode["type"]==detector:
return False
if treeNode["type"]=="initial":
initRedVcm=treeNode["outRedVcm"]
centerPos=np.array([0.0,0.0,initRedVcm])
treeNode["velSolutions"]=[centerPos]
return True
#More is missing, check it later
def findLineParentSolsInChildNodes(branchDict):
if branchDict == {}:
return False
if "type" not in branchDict:
return False
if treeNode["type"] == "detector":
return True
boolVelStatus=getVelSolutions(branchDict)
if checkIfLastPartNode(binTreeDict) == True:
return True
#Still need to implement some functions
def solveEveryRoute(binTreeDict,freePartListRoute):
if len(freePartListRoute)==0:
return True
freePartIndex=freePartListRoute[0]
branchIndex=getOtherVal(freePartIndex)
if branchIndex==None:
return False
tree2Fill=binTreeDict["dictList"][branchIndex]
#Here, find the solutions of the velCirc with the lines. This func
#also fills all the indeces and velocities and energies etc. of the
#corresponding points.
boolSolStatus=findIntersectionsAndSolveBranch(tree2Fill)
if pullBool==False:
return False
newBinTree=binTreeDict["dictList"][freePartIndex]
return solveEveryRoute(newBinTree,freePartListRoute[1:])
def getSolListInParents(treeNode,solIdxList):
checkRes=checkIfNodeIsFreePart(treeNode)
if checkRes == True:
#First generation line return the index (or true?) no need to
#recall the function
return None
if "dictList" not in treeNode:
return None
if "offsets" not in treeNode:
return None
if "lParentsIdxs" not in treeNode:
return None
offS=treeNode["offsets"]
parIdx=treeNode["lParentsIdxs"]
sols4LeftNode=[]
sols4RightNode=[]
# print("solIdxList",solIdxList)
for pIdx,oSet,solIdx in zip(parIdx,offS,solIdxList):
# print("pIdx,oSet,solIdx = ",pIdx,oSet,solIdx)
leftIdxStuff=[pIdx[0],oSet[0]+solIdx]
sols4LeftNode.append(leftIdxStuff)
rightIdxStuff=[pIdx[1],oSet[1]+solIdx]
sols4RightNode.append(sols4RightNode)
return [leftIdxStuff,rightIdxStuff]
def getVLabCIdxP(vCentStr,solsDict):
for centStr in solsDict:
vList=solsDict[centStr]["vLabSols"]
for i in range(len(vList)):
v=vList[i]
#I think is better to change all the vals to str rather than
#converting back a single string to a np.array.
vStr=npArray2Str(v)
if vStr == vCentStr:
return [centStr,i]
print("This should not be printed")
return None
def getVCent4IdxSearchD(binTreeDict,freePartRoute):
#Get the solsDict of branch2Go
mySolsDict=binTreeDict["solsDict"]
branch2Go=binTreeDict["dictList"][freePartRoute[0]]
goClnSolsDict=branch2Go["clnSD"]
clnDictRef={}
for centerStr in goClnSolsDict:
print("My looping centerStr = "+centerStr)
cIdxP=getVLabCIdxP(centerStr,mySolsDict)
if cIdxP != None:
upperCStr,i=cIdxP
if upperCStr not in clnDictRef:
clnDictRef[upperCStr]=[]
clnDictRef[upperCStr].append([i,centerStr])
return clnDictRef
def getClnSD(nodeDict,clnSDR):
clnSD={}
#clnSDR={strC:[[idx,childCmStr],...],...}
for centStr in clnSDR:
myListOfPairs=clnSDR[centStr]
clnSD[centStr]={}
specificSolsD=nodeDict["solsDict"][centStr]
for e in specificSolsD:
clnSD[centStr][e]=[]
for pairL in myListOfPairs:
i=pairL[0]
clnSD[centStr][e].append(specificSolsD[e][i])
return clnSD
def getB2SolClnSolsIdxs(b2SSolsD,vCM2Search):
vCMSols=b2SSolsD["vCMSols"]
for i in range(len(vCMSols)):
print(colored("Inside getB2SolClnSolsIdxs","magenta"))
lineSolCML=vCMSols[i]
print(colored(lineSolCML,"magenta"))
for j in range(len(lineSolCML)):
vCM=lineSolCML[j]
print("vCM = ",vCM)
if np.array_equal(vCM2Search,vCM):