Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyprep.f
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file pyprep.f
1 
2 C*********************************************************************
3 
4 C...PYPREP
5 C...Rearranges partons along strings.
6 C...Special considerations for systems with junctions, with
7 C...possibility of junction-antijunction annihilation.
8 C...Allows small systems to collapse into one or two particles.
9 C...Checks flavours and colour singlet invariant masses.
10 
11  SUBROUTINE pyprep(IP)
12 
13 C...Double precision and integer declarations.
14  IMPLICIT DOUBLE PRECISION(a-h, o-z)
15  INTEGER pyk,pychge,pycomp
16 C...Commonblocks.
17  common/pyjets/n,npad,k(4000,5),p(4000,5),v(4000,5)
18  common/pydat1/mstu(200),paru(200),mstj(200),parj(200)
19  common/pypars/mstp(200),parp(200),msti(200),pari(200)
20  common/pydat2/kchg(500,4),pmas(500,4),parf(2000),vckm(4,4)
21  common/pydat3/mdcy(500,3),mdme(8000,2),brat(8000),kfdp(8000,5)
22  common/pyint1/mint(400),vint(400)
23 C...The common block of colour tags.
24  common/pyctag/nct,mct(4000,2)
25  SAVE /pyjets/,/pydat1/,/pydat2/,/pydat3/,/pyint1/,/pyctag/,
26  &/pypars/
27  DATA nerrpr/0/
28  SAVE nerrpr
29 C...Local arrays.
30  dimension dps(5),dpc(5),ue(3),pg(5),e1(3),e2(3),e3(3),e4(3),
31  &ecl(3),ijunc(10,0:4),ipiece(30,0:4),kfend(4),kfq(4),
32  &ijur(4),pju(4,6),irng(4,2),tjj(2,5),t(5),pul(3,5),
33  &ijcp(0:6),tjuold(5)
34  CHARACTER chtmp*6
35 
36 C...Function to give four-product.
37  four(i,j)=p(i,4)*p(j,4)-p(i,1)*p(j,1)-p(i,2)*p(j,2)-p(i,3)*p(j,3)
38 
39 C...Rearrange parton shower product listing along strings: begin loop.
40  mstu(24)=0
41  nold=n
42  i1=n
43  njunc=0
44  npiece=0
45  njjstr=0
46  mstu32=mstu(32)+1
47  DO 100 i=max(1,ip),n
48 C...First store junction positions.
49  IF(k(i,1).EQ.42) THEN
50  njunc=njunc+1
51  ijunc(njunc,0)=i
52  ijunc(njunc,4)=0
53  ENDIF
54  100 CONTINUE
55 
56  DO 250 mqgst=1,3
57  DO 240 i=max(1,ip),n
58 C...Special treatment for junctions
59  IF (k(i,1).LE.0) goto 240
60  IF(k(i,1).EQ.42) THEN
61 C...MQGST=2: Look for junction-junction strings (not detected in the
62 C...main search below).
63  IF (mqgst.EQ.2.AND.npiece.NE.3*njunc) THEN
64  IF (njjstr.EQ.0) THEN
65  njjstr = (3*njunc-npiece)/2
66  ENDIF
67 C...Check how many already identified strings end on this junction
68  ilc=0
69  DO 110 j=1,npiece
70  IF (ipiece(j,4).EQ.i) ilc=ilc+1
71  110 CONTINUE
72 C...If less than 3, remaining must be to another junction
73  IF (ilc.LT.3) THEN
74  IF (ilc.NE.2) THEN
75 C...Multiple j-j connections not handled yet.
76  CALL pyerrm(2,
77  & '(PYPREP:) Too many junction-junction strings.')
78  mint(51)=1
79  RETURN
80  ENDIF
81 C...The colour information in the junction is unreadable for the
82 C...colour space search further down in this routine, so we must
83 C...start on the colour mother of this junction and then "artificially"
84 C...prevent the colour mother from connecting here again.
85  itjunc=mod(k(i,4)/mstu(5),mstu(5))
86  kcs=4
87  IF (mod(itjunc,2).EQ.0) kcs=5
88 C...Switch colour if the junction-junction leg is presumably a
89 C...junction mother leg rather than a junction daughter leg.
90  IF (itjunc.GE.3) kcs=9-kcs
91  IF (mint(33).EQ.0) THEN
92 C...Find the unconnected leg and reorder junction daughter pointers so
93 C...MOD(K(I,4),MSTU(5)) always points to the junction-junction string
94 C...piece.
95  ia=mod(k(i,4),mstu(5))
96  IF (k(ia,kcs)/mstu(5)**2.GE.2) THEN
97  itmp=mod(k(i,5),mstu(5))
98  IF (k(itmp,kcs)/mstu(5)**2.GE.2) THEN
99  itmp=mod(k(i,5)/mstu(5),mstu(5))
100  k(i,5)=k(i,5)+(ia-itmp)*mstu(5)
101  ELSE
102  k(i,5)=k(i,5)+(ia-itmp)
103  ENDIF
104  k(i,4)=k(i,4)+(itmp-ia)
105  ia=itmp
106  ENDIF
107  IF (itjunc.LE.2) THEN
108 C...Beam baryon junction
109  k(ia,kcs) = k(ia,kcs) + 2*mstu(5)**2
110  k(i,kcs) = k(i,kcs) + 1*mstu(5)**2
111 C...Else 1 -> 2 decay junction
112  ELSE
113  k(ia,kcs) = k(ia,kcs) + mstu(5)**2
114  k(i,kcs) = k(i,kcs) + 2*mstu(5)**2
115  ENDIF
116  i1beg = i1
117  nstp = 0
118  goto 170
119 C...Alternatively use colour tag information.
120  ELSE
121 C...Find a final state parton with appropriate dangling colour tag.
122  jct=0
123  ia=0
124  ijumo=k(i,3)
125  DO 140 j1=max(1,ip),n
126  IF (k(j1,1).NE.3) goto 140
127 C...Check for matching final-state colour tag
128  imatch=0
129  DO 120 j2=max(1,ip),n
130  IF (k(j2,1).NE.3) goto 120
131  IF (mct(j1,kcs-3).EQ.mct(j2,6-kcs)) imatch=1
132  120 CONTINUE
133  IF (imatch.EQ.1) goto 140
134 C...Check whether this colour tag belongs to the present junction
135 C...by seeing whether any parton with this colour tag has the same
136 C...mother as the junction.
137  jct=mct(j1,kcs-3)
138  imatch=0
139  DO 130 j2=mint(84)+1,n
140  imo2=k(j2,3)
141 C...First scattering partons have IMO1 = 3 and 4.
142  IF (imo2.EQ.mint(83)+3.OR.imo2.EQ.mint(83)+4)
143  & imo2=imo2-2
144  IF (mct(j2,kcs-3).EQ.jct.AND.imo2.EQ.ijumo)
145  & imatch=1
146  130 CONTINUE
147  IF (imatch.EQ.0) goto 140
148  ia=j1
149  140 CONTINUE
150 C...Check for junction-junction strings without intermediate final state
151 C...glue (not detected above).
152  IF (ia.EQ.0) THEN
153  DO 160 mju=1,njunc
154  iju2=ijunc(mju,0)
155  IF (iju2.EQ.i) goto 160
156  itju2=mod(k(iju2,4)/mstu(5),mstu(5))
157 C...Only opposite types of junctions can connect to each other.
158  IF (mod(itju2,2).EQ.mod(itjunc,2)) goto 160
159  is=0
160  DO 150 j=1,npiece
161  IF (ipiece(j,4).EQ.iju2) is=is+1
162  150 CONTINUE
163  IF (is.EQ.3) goto 160
164  ib=i
165  ia=iju2
166  160 CONTINUE
167  ENDIF
168 C...Switch to other side of adjacent parton and step from there.
169  kcs=9-kcs
170  i1beg = i1
171  nstp = 0
172  goto 170
173  ENDIF
174  ELSE IF (ilc.NE.3) THEN
175  ENDIF
176  ENDIF
177  ENDIF
178 
179 C...Look for coloured string endpoint, or (later) leftover gluon.
180  IF(k(i,1).NE.3) goto 240
181  kc=pycomp(k(i,2))
182  IF(kc.EQ.0) goto 240
183  kq=kchg(kc,2)
184  IF(kq.EQ.0.OR.(mqgst.LE.2.AND.kq.EQ.2)) goto 240
185 
186 C...Pick up loose string end.
187  kcs=4
188  IF(kq*isign(1,k(i,2)).LT.0) kcs=5
189  ia=i
190  ib=i
191  i1beg=i1
192  nstp=0
193  170 nstp=nstp+1
194  IF(nstp.GT.4*n) THEN
195  CALL pyerrm(14,'(PYPREP:) caught in infinite loop')
196  mint(51)=1
197  RETURN
198  ENDIF
199 
200 C...Copy undecayed parton. Finished if reached string endpoint.
201  IF(k(ia,1).EQ.3) THEN
202  IF(i1.GE.mstu(4)-mstu32-5) THEN
203  CALL pyerrm(11,'(PYPREP:) no more memory left in PYJETS')
204  mint(51)=1
205  mstu(24)=1
206  RETURN
207  ENDIF
208  i1=i1+1
209  k(i1,1)=2
210  IF(nstp.GE.2.AND.kchg(pycomp(k(ia,2)),2).NE.2) k(i1,1)=1
211  k(i1,2)=k(ia,2)
212  k(i1,3)=ia
213  k(i1,4)=0
214  k(i1,5)=0
215  DO 180 j=1,5
216  p(i1,j)=p(ia,j)
217  v(i1,j)=v(ia,j)
218  180 CONTINUE
219  k(ia,1)=k(ia,1)+10
220  IF(k(i1,1).EQ.1) goto 240
221  ENDIF
222 
223 C...Also finished (for now) if reached junction; then copy to end.
224  IF(k(ia,1).EQ.42) THEN
225  ncopy=i1-i1beg
226  IF(i1.GE.mstu(4)-mstu32-ncopy-5) THEN
227  CALL pyerrm(11,'(PYPREP:) no more memory left in PYJETS')
228  mint(51)=1
229  mstu(24)=1
230  RETURN
231  ENDIF
232  IF (mqgst.LE.2.AND.ncopy.NE.0) THEN
233  DO 200 icopy=1,ncopy
234  DO 190 j=1,5
235  k(mstu(4)-mstu32-icopy,j)=k(i1beg+icopy,j)
236  p(mstu(4)-mstu32-icopy,j)=p(i1beg+icopy,j)
237  v(mstu(4)-mstu32-icopy,j)=v(i1beg+icopy,j)
238  190 CONTINUE
239  200 CONTINUE
240  ENDIF
241 C...For junction-junction strings, find end leg and reorder junction
242 C...daughter pointers so MOD(K(I,4),MSTU(5)) always points to the
243 C...junction-junction string piece.
244  IF (k(i,1).EQ.42.AND.mint(33).EQ.0) THEN
245  itmp=mod(k(ia,4),mstu(5))
246  IF (itmp.NE.ib) THEN
247  IF (mod(k(ia,5),mstu(5)).EQ.ib) THEN
248  k(ia,5)=k(ia,5)+(itmp-ib)
249  ELSE
250  k(ia,5)=k(ia,5)+(itmp-ib)*mstu(5)
251  ENDIF
252  k(ia,4)=k(ia,4)+(ib-itmp)
253  ENDIF
254  ENDIF
255  npiece=npiece+1
256 C...IPIECE:
257 C...0: endpoint in original ER
258 C...1:
259 C...2:
260 C...3: Parton immediately next to junction
261 C...4: Junction
262  ipiece(npiece,0)=i
263  ipiece(npiece,1)=mstu32+1
264  ipiece(npiece,2)=mstu32+ncopy
265  ipiece(npiece,3)=ib
266  ipiece(npiece,4)=ia
267  mstu32=mstu32+ncopy
268  i1=i1beg
269  goto 240
270  ENDIF
271 
272 C...GOTO next parton in colour space.
273  ib=ia
274  IF (mint(33).EQ.0) THEN
275  IF(mod(k(ib,kcs)/mstu(5)**2,2).EQ.0.AND.mod(k(ib,kcs),mstu(5
276  & )).NE.0) THEN
277  ia=mod(k(ib,kcs),mstu(5))
278  k(ib,kcs)=k(ib,kcs)+mstu(5)**2
279  mrev=0
280  ELSE
281  IF(k(ib,kcs).GE.2*mstu(5)**2.OR.mod(k(ib,kcs)/mstu(5),
282  & mstu(5)).EQ.0) kcs=9-kcs
283  ia=mod(k(ib,kcs)/mstu(5),mstu(5))
284  k(ib,kcs)=k(ib,kcs)+2*mstu(5)**2
285  mrev=1
286  ENDIF
287  IF(ia.LE.0.OR.ia.GT.n) THEN
288  CALL pyerrm(12,'(PYPREP:) colour rearrangement failed')
289  IF(nerrpr.LT.5) THEN
290  nerrpr=nerrpr+1
291  WRITE(mstu(11),*) 'started at:', i
292  WRITE(mstu(11),*) 'ended going from',ib,' to',ia
293  WRITE(mstu(11),*) 'MQGST =',mqgst
294  CALL pylist(4)
295  ENDIF
296  mint(51)=1
297  RETURN
298  ENDIF
299  IF(mod(k(ia,4)/mstu(5),mstu(5)).EQ.ib.OR.mod(k(ia,5)/mstu(5)
300  & ,mstu(5)).EQ.ib) THEN
301  IF(mrev.EQ.1) kcs=9-kcs
302  IF(mod(k(ia,kcs)/mstu(5),mstu(5)).NE.ib) kcs=9-kcs
303  k(ia,kcs)=k(ia,kcs)+2*mstu(5)**2
304  ELSE
305  IF(mrev.EQ.0) kcs=9-kcs
306  IF(mod(k(ia,kcs),mstu(5)).NE.ib) kcs=9-kcs
307  k(ia,kcs)=k(ia,kcs)+mstu(5)**2
308  ENDIF
309  IF(ia.NE.i) goto 170
310 C...Use colour tag information
311  ELSE
312 C...First create colour tags starting on IB if none already present.
313  IF (mct(ib,kcs-3).EQ.0) THEN
314  CALL pycttr(ib,kcs,ib)
315  IF(mint(51).NE.0) RETURN
316  ENDIF
317  jct=mct(ib,kcs-3)
318  ifound=0
319 C...Find final state tag partner
320  DO 210 it=max(1,ip),n
321  IF (it.EQ.ib) goto 210
322  IF (mct(it,6-kcs).EQ.jct.AND.k(it,1).LT.10.AND.k(it,1).gt
323  & .0) THEN
324  ifound=ifound+1
325  ia=it
326  ENDIF
327  210 CONTINUE
328 C...Just copy and goto next if exactly one partner found.
329  IF (ifound.EQ.1) THEN
330  goto 170
331 C...When no match found, match is presumably junction.
332  ELSEIF (ifound.EQ.0.AND.mqgst.LE.2) THEN
333 C...Check whether this colour tag matches a junction
334 C...by seeing whether any parton with this colour tag has the same
335 C...mother as a junction.
336 C...NB: Only type 1 and 2 junctions handled presently.
337  DO 230 iju=1,njunc
338  ijumo=k(ijunc(iju,0),3)
339  itjunc=mod(k(ijunc(iju,0),4)/mstu(5),mstu(5))
340 C...Colours only connect to junctions, anti-colours to antijunctions:
341  IF (mod(itjunc+1,2)+1.NE.kcs-3) goto 230
342  imatch=0
343  DO 220 j1=max(1,ip),n
344  IF (k(j1,1).LE.0) goto 220
345 C...First scattering partons have IMO1 = 3 and 4.
346  imo=k(j1,3)
347  IF (imo.EQ.mint(83)+3.OR.imo.EQ.mint(83)+4)
348  & imo=imo-2
349  IF (mct(j1,kcs-3).EQ.jct.AND.imo.EQ.ijumo.AND.mod(k(j1
350  & ,3+itjunc)/mstu(5),mstu(5)).EQ.ijunc(iju,0))
351  & imatch=1
352 C...Attempt at handling type > 3 junctions also. Not tested.
353  IF (itjunc.GE.3.AND.mct(j1,6-kcs).EQ.jct.AND.imo.eq
354  & .ijumo) imatch=1
355  220 CONTINUE
356  IF (imatch.EQ.0) goto 230
357  ia=ijunc(iju,0)
358  ifound=ifound+1
359  230 CONTINUE
360 
361  IF (ifound.EQ.1) THEN
362  goto 170
363  ELSEIF (ifound.EQ.0) THEN
364  WRITE(chtmp,*) jct
365  CALL pyerrm(12,'(PYPREP:) no matching colour tag: '
366  & //chtmp)
367  IF(nerrpr.LT.5) THEN
368  nerrpr=nerrpr+1
369  CALL pylist(4)
370  ENDIF
371  mint(51)=1
372  RETURN
373  ENDIF
374  ELSEIF (ifound.GE.2) THEN
375  WRITE(chtmp,*) jct
376  CALL pyerrm(12
377  & ,'(PYPREP:) too many occurences of colour line: '//
378  & chtmp)
379  IF(nerrpr.LT.5) THEN
380  nerrpr=nerrpr+1
381  CALL pylist(4)
382  ENDIF
383  mint(51)=1
384  RETURN
385  ENDIF
386  ENDIF
387  k(i1,1)=1
388  240 CONTINUE
389  250 CONTINUE
390 
391 C...Junction systems remain.
392  iju=0
393  ijus=0
394  ijucnt=0
395  mrev=0
396  ijjstr=0
397  260 ijucnt=ijucnt+1
398  IF (ijucnt.LE.njunc) THEN
399 C...If we are not processing a j-j string, treat this junction as new.
400  IF (ijjstr.EQ.0) THEN
401  iju=ijunc(ijucnt,0)
402  mrev=0
403 C...If junction has already been read, ignore it.
404  IF (ijunc(ijucnt,4).EQ.1) goto 260
405 C...If we are on a j-j string, goto second j-j junction.
406  ELSE
407  ijucnt=ijucnt-1
408  iju=ijus
409  ENDIF
410 C...Mark selected junction read.
411  DO 270 j=1,njunc
412  IF (ijunc(j,0).EQ.iju) ijunc(j,4)=1
413  270 CONTINUE
414 C...Determine junction type
415  itjunc = mod(k(iju,4)/mstu(5),mstu(5))
416 C...Type 1 and 2 junctions: ~chi -> q q q, ~chi -> qbar,qbar,qbar
417 C...Type 3 and 4 junctions: ~qbar -> q q , ~q -> qbar qbar
418 C...Type 5 and 6 junctions: ~g -> q q q, ~g -> qbar qbar qbar
419  IF (itjunc.GE.1.AND.itjunc.LE.6) THEN
420  ihk=0
421  280 ihk=ihk+1
422 C...Find which quarks belong to given junction.
423  ihf=0
424  DO 290 ipc=1,npiece
425  IF (ipiece(ipc,4).EQ.iju) THEN
426  ihf=ihf+1
427  IF (ihf.EQ.ihk) iend=ipiece(ipc,3)
428  ENDIF
429  IF (ihk.EQ.3.AND.ipiece(ipc,0).EQ.iju) iend=ipiece(ipc,3)
430  290 CONTINUE
431 C...IHK = 3 is special. Either normal string piece, or j-j string.
432  IF(ihk.EQ.3) THEN
433  IF (mrev.NE.1) THEN
434  DO 300 ipc=1,npiece
435 C...If there is a j-j string starting on the present junction which has
436 C...zero length, insert next junction immediately.
437  IF (ipiece(ipc,0).EQ.iju.AND.k(ipiece(ipc,4),1)
438  & .EQ.42.AND.ipiece(ipc,1)-1-ipiece(ipc,2).EQ.0) THEN
439  ijjstr = 1
440  goto 340
441  ENDIF
442  300 CONTINUE
443  mrev = 1
444 C...If MREV is 1 and IHK is 3 we are finished with this system.
445  ELSE
446  mrev=0
447  goto 260
448  ENDIF
449  ENDIF
450 
451 C...If we've gotten this far, then either IHK < 3, or
452 C...an interjunction string exists, or just a third normal string.
453  ijunc(ijucnt,ihk)=0
454  ijjstr = 0
455 C..Order pieces belonging to this junction. Also look for j-j.
456  DO 310 ipc=1,npiece
457  IF (ipiece(ipc,3).EQ.iend) ijunc(ijucnt,ihk)=ipc
458  IF (ihk.EQ.3.AND.ipiece(ipc,0).EQ.ijunc(ijucnt,0)
459  & .AND.k(ipiece(ipc,4),1).EQ.42) THEN
460  ijunc(ijucnt,ihk)=ipc
461  ijjstr = 1
462  mrev = 0
463  ENDIF
464  310 CONTINUE
465 C...Copy back chains in proper order. MREV=0/1 : descending/ascending
466  ipc=ijunc(ijucnt,ihk)
467 C...Temporary solution to cover for bug.
468  IF(ipc.LE.0) THEN
469  CALL pyerrm(12,'(PYPREP:) fails to hook up junctions')
470  mint(51)=1
471  RETURN
472  ENDIF
473  DO 330 icp=ipiece(ipc,1+mrev),ipiece(ipc,2-mrev),1-2*mrev
474  i1=i1+1
475  DO 320 j=1,5
476  k(i1,j)=k(mstu(4)-icp,j)
477  p(i1,j)=p(mstu(4)-icp,j)
478  v(i1,j)=v(mstu(4)-icp,j)
479  320 CONTINUE
480  330 CONTINUE
481  k(i1,1)=2
482 C...Mark last quark.
483  IF (mrev.EQ.1.AND.ihk.GE.2) k(i1,1)=1
484 C...Do not insert junctions at wrong places.
485  IF(ihk.LT.2.OR.mrev.NE.0) goto 360
486 C...Insert junction.
487  340 ijus = iju
488  IF (ihk.EQ.3) THEN
489 C...Shift to end junction if a j-j string has been processed.
490  IF (ijjstr.NE.0) ijus = ipiece(ipc,4)
491  mrev= 1
492  ENDIF
493  i1=i1+1
494  DO 350 j=1,5
495  k(i1,j)=0
496  p(i1,j)=0.
497  v(i1,j)=0.
498  350 CONTINUE
499  k(i1,1)=41
500  k(ijus,1)=k(ijus,1)+10
501  k(i1,2)=k(ijus,2)
502  k(i1,3)=ijus
503  360 IF (ihk.LT.3) goto 280
504  ELSE
505  CALL pyerrm(12,'(PYPREP:) Unknown junction type')
506  mint(51)=1
507  RETURN
508  ENDIF
509  IF (ijucnt.NE.njunc) goto 260
510  ENDIF
511  n=i1
512 
513 C...Rearrange three strings from junction, e.g. in case one has been
514 C...shortened by shower, so the last is the largest-energy one.
515  IF(njunc.GE.1) THEN
516 C...Find systems with exactly one junction.
517  mjun1=0
518  nbeg=nold+1
519  DO 470 i=nold+1,n
520  IF(k(i,1).NE.1.AND.k(i,1).NE.41) THEN
521  ELSEIF(k(i,1).EQ.41) THEN
522  mjun1=mjun1+1
523  ELSEIF(k(i,1).EQ.1.AND.mjun1.NE.1) THEN
524  mjun1=0
525  nbeg=i+1
526  ELSE
527  nend=i
528 C...Sum up energy-momentum in each junction string.
529  DO 370 j=1,5
530  pju(1,j)=0d0
531  pju(2,j)=0d0
532  pju(3,j)=0d0
533  370 CONTINUE
534  nju=0
535  DO 390 i1=nbeg,nend
536  IF(k(i1,2).NE.21) THEN
537  nju=nju+1
538  ijur(nju)=i1
539  ENDIF
540  DO 380 j=1,5
541  pju(min(nju,3),j)=pju(min(nju,3),j)+p(i1,j)
542  380 CONTINUE
543  390 CONTINUE
544 C...Find which of them has highest energy (minus mass) in rest frame.
545  DO 400 j=1,5
546  pju(4,j)=pju(1,j)+pju(2,j)+pju(3,j)
547  400 CONTINUE
548  pmju=sqrt(max(0d0,pju(4,4)**2-pju(4,1)**2-pju(4,2)**2-
549  & pju(4,3)**2))
550  DO 410 i2=1,3
551  pju(i2,6)=(pju(4,4)*pju(i2,4)-pju(4,1)*pju(i2,1)-
552  & pju(4,2)*pju(i2,2)-pju(4,3)*pju(i2,3))/pmju-pju(i2,5)
553  410 CONTINUE
554  IF(pju(3,6).LT.min(pju(1,6),pju(2,6))) THEN
555 C...Decide how to rearrange so that new last has highest energy.
556  IF(pju(1,6).LT.pju(2,6)) THEN
557  irng(1,1)=ijur(1)
558  irng(1,2)=ijur(2)-1
559  irng(2,1)=ijur(4)
560  irng(2,2)=ijur(3)+1
561  irng(4,1)=ijur(3)-1
562  irng(4,2)=ijur(2)
563  ELSE
564  irng(1,1)=ijur(4)
565  irng(1,2)=ijur(3)+1
566  irng(2,1)=ijur(2)
567  irng(2,2)=ijur(3)-1
568  irng(4,1)=ijur(2)-1
569  irng(4,2)=ijur(1)
570  ENDIF
571  irng(3,1)=ijur(3)
572  irng(3,2)=ijur(3)
573 C...Copy in correct order below bottom of current event record.
574  i2=n
575  DO 440 ii=1,4
576  DO 430 i1=irng(ii,1),irng(ii,2),
577  & isign(1,irng(ii,2)-irng(ii,1))
578  i2=i2+1
579  IF(i2.GE.mstu(4)-mstu32-5) THEN
580  CALL pyerrm(11,
581  & '(PYPREP:) no more memory left in PYJETS')
582  mint(51)=1
583  mstu(24)=1
584  RETURN
585  ENDIF
586  DO 420 j=1,5
587  k(i2,j)=k(i1,j)
588  p(i2,j)=p(i1,j)
589  v(i2,j)=v(i1,j)
590  420 CONTINUE
591  IF(k(i2,1).EQ.1) k(i2,1)=2
592  430 CONTINUE
593  440 CONTINUE
594  k(i2,1)=1
595 C...Copy back up, overwriting but now in correct order.
596  DO 460 i1=nbeg,nend
597  i2=i1-nbeg+n+1
598  DO 450 j=1,5
599  k(i1,j)=k(i2,j)
600  p(i1,j)=p(i2,j)
601  v(i1,j)=v(i2,j)
602  450 CONTINUE
603  460 CONTINUE
604  ENDIF
605  mjun1=0
606  nbeg=i+1
607  ENDIF
608  470 CONTINUE
609 
610 C...Check whether q-q-j-j-qbar-qbar systems should be collapsed
611 C...to two q-qbar systems.
612 C...(MSTJ(19)=1 forces q-q-j-j-qbar-qbar.)
613  IF (mstj(19).NE.1) THEN
614  mjun1 = 0
615  jjglue = 0
616  nbeg = nold+1
617 C...Force collapse when MSTJ(19)=2.
618  IF (mstj(19).EQ.2) THEN
619  delmjj = 1d9
620  delmqq = 0d0
621  ENDIF
622 C...Find systems with exactly two junctions.
623  DO 700 i=nold+1,n
624 C...Count junctions
625  IF (k(i,1).EQ.41) THEN
626  mjun1 = mjun1+1
627 C...Check for interjunction gluons
628  IF (mjun1.EQ.2.AND.k(i-1,1).NE.41) THEN
629  jjglue = 1
630  ENDIF
631  ELSEIF(k(i,1).EQ.1.AND.(mjun1.NE.2)) THEN
632 C...If end of system reached with either zero or one junction, restart
633 C...with next system.
634  mjun1 = 0
635  jjglue = 0
636  nbeg = i+1
637  ELSEIF(k(i,1).EQ.1) THEN
638 C...If end of system reached with exactly two junctions, compute string
639 C...length measure for the (q-q-j-j-qbar-qbar) topology and compare with
640 C...length measure for the (q-qbar)(q-qbar) topology.
641  nend=i
642 C...Loop down through chain.
643  isid=0
644  DO 480 i1=nbeg,nend
645 C...Store string piece division locations in event record
646  IF (k(i1,2).NE.21) THEN
647  isid = isid+1
648  ijcp(isid) = i1
649  ENDIF
650  480 CONTINUE
651 C...Randomly choose between (1,3)(2,4) and (1,4)(2,3) topologies.
652  isw=0
653  IF (pyr(0).LT.0.5d0) isw=1
654 C...Randomly choose which qqbar string gets the jj gluons.
655  igs=1
656  IF (pyr(0).GT.0.5d0) igs=2
657 C...Only compute string lengths when no topology forced.
658  IF (mstj(19).EQ.0) THEN
659 C...Repeat following for each junction
660  DO 570 iju=1,2
661 C...Initialize iterative procedure for finding JRF
662  ijrfit=0
663  DO 490 ix=1,3
664  tjuold(ix)=0d0
665  490 CONTINUE
666  tjuold(4)=1d0
667 C...Start iteration. Sum up momenta in string pieces
668  500 DO 540 ijs=1,3
669 C...JD=-1 for first junction, +1 for second junction.
670 C...Find out where piece starts and ends and which direction to go.
671  jd=2*iju-3
672  IF (ijs.LE.2) THEN
673  ia = ijcp((iju-1)*7 - jd*(ijs+1)) + jd
674  ib = ijcp((iju-1)*7 - jd*ijs)
675  ELSEIF (ijs.EQ.3) THEN
676  jd =-jd
677  ia = ijcp((iju-1)*7 + jd*(ijs)) + jd
678  ib = ijcp((iju-1)*7 + jd*(ijs+3))
679  ENDIF
680 C...Initialize junction pull 4-vector.
681  DO 510 j=1,5
682  pul(ijs,j)=0d0
683  510 CONTINUE
684 C...Initialize weight
685  pwt = 0d0
686  pwtold = 0d0
687 C...Sum up (weighted) momenta along each string piece
688  DO 530 isp=ia,ib,jd
689 C...If present parton not last in chain
690  IF (isp.NE.ia.AND.isp.NE.ib) THEN
691 C...If last parton was a junction, store present weight
692  IF (k(isp-jd,2).EQ.88) THEN
693  pwtold = pwt
694 C...If last parton was a quark, reset to stored weight.
695  ELSEIF (k(isp-jd,2).NE.21) THEN
696  pwt = pwtold
697  ENDIF
698  ENDIF
699 C...Skip next parton if weight already large
700  IF (pwt.GT.10d0) goto 530
701 C...Compute momentum in TJUOLD frame:
702  tdp=tjuold(1)*p(isp,1)+tjuold(2)*p(isp,2)+tjuold(3
703  & )*p(isp,3)
704  bfc=tdp/(1d0+tjuold(4))+p(isp,4)
705  DO 520 j=1,3
706  tmp=p(isp,j)+tjuold(j)*bfc
707  pul(ijs,j)=pul(ijs,j)+tmp*exp(-pwt)
708  520 CONTINUE
709 C...Boosted energy
710  tmp=tjuold(4)*p(isp,4)+tdp
711  pul(ijs,4)=pul(ijs,j)+tmp*exp(-pwt)
712 C...Update weight
713  pwt=pwt+tmp/parj(48)
714 C...Put |p| rather than m in 5th slot
715  pul(ijs,5)=sqrt(pul(ijs,1)**2+pul(ijs,2)**2
716  & +pul(ijs,3)**2)
717  530 CONTINUE
718  540 CONTINUE
719 C...Compute boost
720  ijrfit=ijrfit+1
721  CALL pyjurf(pul,t)
722 C...Combine new boost (T) with old boost (TJUOLD)
723  tmp=t(1)*tjuold(1)+t(2)*tjuold(2)+t(3)*tjuold(3)
724  DO 550 ix=1,3
725  tjuold(ix)=t(ix)+tjuold(ix)*(tmp/(1d0+tjuold(4))+t(4
726  & ))
727  550 CONTINUE
728  tjuold(4)=sqrt(1d0+tjuold(1)**2+tjuold(2)**2+tjuold(3)
729  & **2)
730 C...If last boost small, accept JRF, else iterate.
731 C...Also prevent possibility of infinite loop.
732  IF (abs((t(4)-1d0)/tjuold(4)).GT.0.01d0.AND.
733  & ijrfit.LT.mstj(18))THEN
734  goto 500
735  ELSEIF (ijrfit.GE.mstj(18)) THEN
736  CALL pyerrm(1,'(PYPREP:) failed to converge on JRF')
737  ENDIF
738 C...Store final boost, with change of sign since TJJ motion vector.
739  DO 560 ix=1,3
740  tjj(iju,ix)=-tjuold(ix)
741  560 CONTINUE
742  tjj(iju,4)=sqrt(1d0+tjj(iju,1)**2+tjj(iju,2)**2
743  & +tjj(iju,3)**2)
744  570 CONTINUE
745 C...String length measure for (q-qbar)(q-qbar) topology.
746 C...Note only momenta of nearest partons used (since rest of system
747 C...identical).
748  IF (jjglue.EQ.0) THEN
749  delmqq=4d0*four(ijcp(2)-1,ijcp(4+isw)+1)*four(ijcp(3)
750  & -1,ijcp(5-isw)+1)
751  ELSE
752 C...Put jj gluons on selected string (IGS selected randomly above).
753  IF (igs.EQ.1) THEN
754  delmqq=8d0*four(ijcp(2)-1,ijcp(4)-1)*four(ijcp(3)+1
755  & ,ijcp(4+isw)+1)*four(ijcp(3)-1,ijcp(5-isw)+1)
756  ELSE
757  delmqq=8d0*four(ijcp(2)-1,ijcp(4+isw)+1)
758  & *four(ijcp(3)-1,ijcp(4)-1)*four(ijcp(3)+1
759  & ,ijcp(5-isw)+1)
760  ENDIF
761  ENDIF
762 C...String length measure for q-q-j-j-q-q topology.
763  t1g1=0d0
764  t2g2=0d0
765  t1t2=0d0
766  t1p1=0d0
767  t1p2=0d0
768  t2p3=0d0
769  t2p4=0d0
770  isgn=-1
771 C...Note only momenta of nearest partons used (since rest of system
772 C...identical).
773  DO 580 ix=1,4
774  IF (ix.EQ.4) isgn=1
775  t1p1=t1p1+isgn*tjj(1,ix)*p(ijcp(2)-1,ix)
776  t1p2=t1p2+isgn*tjj(1,ix)*p(ijcp(3)-1,ix)
777  t2p3=t2p3+isgn*tjj(2,ix)*p(ijcp(4)+1,ix)
778  t2p4=t2p4+isgn*tjj(2,ix)*p(ijcp(5)+1,ix)
779  IF (jjglue.EQ.0) THEN
780 C...Junction motion vector dot product gives length when inter-junction
781 C...gluons absent.
782  t1t2=t1t2+isgn*tjj(1,ix)*tjj(2,ix)
783  ELSE
784 C...Junction motion vector dot products with gluon momenta give length
785 C...when inter-junction gluons present.
786  t1g1=t1g1+isgn*tjj(1,ix)*p(ijcp(3)+1,ix)
787  t2g2=t2g2+isgn*tjj(2,ix)*p(ijcp(4)-1,ix)
788  ENDIF
789  580 CONTINUE
790  delmjj=16d0*t1p1*t1p2*t2p3*t2p4
791  IF (jjglue.EQ.0) THEN
792  delmjj=delmjj*(t1t2+sqrt(t1t2**2-1))
793  ELSE
794  delmjj=delmjj*4d0*t1g1*t2g2
795  ENDIF
796  ENDIF
797 C...If delmjj > delmqq collapse string system to q-qbar q-qbar
798 C...(Always the case for MSTJ(19)=2 due to initialization above)
799  IF (delmjj.GT.delmqq) THEN
800 C...Put new system at end of event record
801  ncop=n
802  DO 650 ist=1,2
803  DO 600 icop=ijcp(ist),ijcp(ist+1)-1
804  ncop=ncop+1
805  DO 590 ix=1,5
806  p(ncop,ix)=p(icop,ix)
807  k(ncop,ix)=k(icop,ix)
808  590 CONTINUE
809  600 CONTINUE
810  IF (jjglue.NE.0.AND.ist.EQ.igs) THEN
811 C...Insert inter-junction gluon string piece (reversed)
812  njjgl=0
813  DO 620 icop=ijcp(4)-1,ijcp(3)+1,-1
814  njjgl=njjgl+1
815  ncop=ncop+1
816  DO 610 ix=1,5
817  p(ncop,ix)=p(icop,ix)
818  k(ncop,ix)=k(icop,ix)
819  610 CONTINUE
820  620 CONTINUE
821  ENDIF
822  ifc=-2*ist+3
823  DO 640 icop=ijcp(ist+ifc*isw+3)+1,ijcp(ist+ifc*isw+4)
824  ncop=ncop+1
825  DO 630 ix=1,5
826  p(ncop,ix)=p(icop,ix)
827  k(ncop,ix)=k(icop,ix)
828  630 CONTINUE
829  640 CONTINUE
830  k(ncop,1)=1
831  650 CONTINUE
832 C...Copy system back in right order
833  DO 670 icop=nbeg,nend-2
834  DO 660 ix=1,5
835  p(icop,ix)=p(n+icop-nbeg+1,ix)
836  k(icop,ix)=k(n+icop-nbeg+1,ix)
837  660 CONTINUE
838  670 CONTINUE
839 C...Shift down rest of event record
840  DO 690 icop=nend+1,n
841  DO 680 ix=1,5
842  p(icop-2,ix)=p(icop,ix)
843  k(icop-2,ix)=k(icop,ix)
844  680 CONTINUE
845  690 CONTINUE
846 C...Update length of event record.
847  n=n-2
848  ENDIF
849  mjun1=0
850  nbeg=i+1
851  ENDIF
852  700 CONTINUE
853  ENDIF
854  ENDIF
855 
856 C...Done if no checks on small-mass systems.
857  IF(mstj(14).LT.0) RETURN
858  IF(mstj(14).EQ.0) goto 1140
859 
860 C...Find lowest-mass colour singlet jet system.
861  ns=n
862  710 nsin=n-ns
863  pdmin=1d0+parj(32)
864  ic=0
865  DO 770 i=max(1,ip),n
866  IF(k(i,1).NE.1.AND.k(i,1).NE.2) THEN
867  ELSEIF(k(i,1).EQ.2.AND.ic.EQ.0) THEN
868  nsin=nsin+1
869  ic=i
870  DO 720 j=1,4
871  dps(j)=p(i,j)
872  720 CONTINUE
873  mstj(93)=1
874  dps(5)=pymass(k(i,2))
875  ELSEIF(k(i,1).EQ.2.AND.k(i,2).NE.21) THEN
876  DO 730 j=1,4
877  dps(j)=dps(j)+p(i,j)
878  730 CONTINUE
879  mstj(93)=1
880  dps(5)=dps(5)+pymass(k(i,2))
881  ELSEIF(k(i,1).EQ.2) THEN
882  DO 740 j=1,4
883  dps(j)=dps(j)+p(i,j)
884  740 CONTINUE
885  ELSEIF(ic.NE.0.AND.kchg(pycomp(k(i,2)),2).NE.0) THEN
886  DO 750 j=1,4
887  dps(j)=dps(j)+p(i,j)
888  750 CONTINUE
889  mstj(93)=1
890  dps(5)=dps(5)+pymass(k(i,2))
891  pd=sqrt(max(0d0,dps(4)**2-dps(1)**2-dps(2)**2-dps(3)**2))-
892  & dps(5)
893  IF(pd.LT.pdmin) THEN
894  pdmin=pd
895  DO 760 j=1,5
896  dpc(j)=dps(j)
897  760 CONTINUE
898  ic1=ic
899  ic2=i
900  ENDIF
901  ic=0
902  ELSE
903  nsin=nsin+1
904  ENDIF
905  770 CONTINUE
906 
907 C...Done if lowest-mass system above threshold for string frag.
908  IF(pdmin.GE.parj(32)) goto 1140
909 
910 C...Fill small-mass system as cluster.
911  nsav=n
912  pecm=sqrt(max(0d0,dpc(4)**2-dpc(1)**2-dpc(2)**2-dpc(3)**2))
913  k(n+1,1)=11
914  k(n+1,2)=91
915  k(n+1,3)=ic1
916  p(n+1,1)=dpc(1)
917  p(n+1,2)=dpc(2)
918  p(n+1,3)=dpc(3)
919  p(n+1,4)=dpc(4)
920  p(n+1,5)=pecm
921 
922 C...Set up history, assuming cluster -> 2 hadrons.
923  nbody=2
924  k(n+1,4)=n+2
925  k(n+1,5)=n+3
926  k(n+2,1)=1
927  k(n+3,1)=1
928  IF(mstu(16).NE.2) THEN
929  k(n+2,3)=n+1
930  k(n+3,3)=n+1
931  ELSE
932  k(n+2,3)=ic1
933  k(n+3,3)=ic2
934  ENDIF
935  k(n+2,4)=0
936  k(n+3,4)=0
937  k(n+2,5)=0
938  k(n+3,5)=0
939  v(n+1,5)=0d0
940  v(n+2,5)=0d0
941  v(n+3,5)=0d0
942 
943 C...Find total flavour content - complicated by presence of junctions.
944  nq=0
945  ndiq=0
946  DO 780 i=ic1,ic2
947  IF((k(i,1).EQ.1.OR.k(i,1).EQ.2).AND.k(i,2).NE.21) THEN
948  nq=nq+1
949  kfq(nq)=k(i,2)
950  IF(iabs(k(i,2)).GT.1000) ndiq=ndiq+1
951  ENDIF
952  780 CONTINUE
953 
954 C...If several diquarks, split up one to give even number of flavours.
955  IF(nq.EQ.3.AND.ndiq.GE.2) THEN
956  i1=3
957  IF(iabs(kfq(3)).LT.1000) i1=1
958  kfq(4)=isign(mod(iabs(kfq(i1))/100,10),kfq(i1))
959  kfq(i1)=kfq(i1)/1000
960  nq=4
961  ndiq=ndiq-1
962  ENDIF
963 
964 C...If four quark ends, join two to diquark.
965  IF(nq.EQ.4.AND.ndiq.EQ.0) THEN
966  i1=1
967  i2=2
968  IF(kfq(i1)*kfq(i2).LT.0) i2=3
969  IF(i2.EQ.3.AND.kfq(i1)*kfq(i2).LT.0) i2=4
970  kfls=2*int(pyr(0)+3d0*parj(4)/(1d0+3d0*parj(4)))+1
971  IF(kfq(i1).EQ.kfq(i2)) kfls=3
972  kfq(i1)=isign(1000*max(iabs(kfq(i1)),iabs(kfq(i2)))+
973  & 100*min(iabs(kfq(i1)),iabs(kfq(i2)))+kfls,kfq(i1))
974  kfq(i2)=kfq(4)
975  nq=3
976  ndiq=1
977  ENDIF
978 
979 C...If two quark ends, plus quark or diquark, join quarks to diquark.
980  IF(nq.EQ.3) THEN
981  i1=1
982  i2=2
983  IF(iabs(kfq(i1)).GT.1000) i1=3
984  IF(iabs(kfq(i2)).GT.1000) i2=3
985  kfls=2*int(pyr(0)+3d0*parj(4)/(1d0+3d0*parj(4)))+1
986  IF(kfq(i1).EQ.kfq(i2)) kfls=3
987  kfq(i1)=isign(1000*max(iabs(kfq(i1)),iabs(kfq(i2)))+
988  & 100*min(iabs(kfq(i1)),iabs(kfq(i2)))+kfls,kfq(i1))
989  kfq(i2)=kfq(3)
990  nq=2
991  ndiq=ndiq+1
992  ENDIF
993 
994 C...Form two particles from flavours of lowest-mass system, if feasible.
995  ntry = 0
996  790 ntry = ntry + 1
997 
998 C...Open string with two specified endpoint flavours.
999  IF(nq.EQ.2) THEN
1000  kc1=pycomp(kfq(1))
1001  kc2=pycomp(kfq(2))
1002  IF(kc1.EQ.0.OR.kc2.EQ.0) goto 1140
1003  kq1=kchg(kc1,2)*isign(1,kfq(1))
1004  kq2=kchg(kc2,2)*isign(1,kfq(2))
1005  IF(kq1+kq2.NE.0) goto 1140
1006 C...Start with qq, if there is one. Only allow for rank 1 popcorn meson
1007  800 k1=kfq(1)
1008  IF(iabs(kfq(2)).GT.1000) k1=kfq(2)
1009  mstu(125)=0
1010  CALL pydcyk(k1,0,kfln,k(n+2,2))
1011  CALL pydcyk(kfq(1)+kfq(2)-k1,-kfln,kfldmp,k(n+3,2))
1012  IF(k(n+2,2).EQ.0.OR.k(n+3,2).EQ.0) goto 800
1013 
1014 C...Open string with four specified flavours.
1015  ELSEIF(nq.EQ.4) THEN
1016  kc1=pycomp(kfq(1))
1017  kc2=pycomp(kfq(2))
1018  kc3=pycomp(kfq(3))
1019  kc4=pycomp(kfq(4))
1020  IF(kc1.EQ.0.OR.kc2.EQ.0.OR.kc3.EQ.0.OR.kc4.EQ.0) goto 1140
1021  kq1=kchg(kc1,2)*isign(1,kfq(1))
1022  kq2=kchg(kc2,2)*isign(1,kfq(2))
1023  kq3=kchg(kc3,2)*isign(1,kfq(3))
1024  kq4=kchg(kc4,2)*isign(1,kfq(4))
1025  IF(kq1+kq2+kq3+kq4.NE.0) goto 1140
1026 C...Combine flavours pairwise to form two hadrons.
1027  810 i1=1
1028  i2=2
1029  IF(kq1*kq2.GT.0.OR.(iabs(kfq(1)).GT.1000.AND.
1030  & iabs(kfq(2)).GT.1000)) i2=3
1031  IF(i2.EQ.3.AND.(kq1*kq3.GT.0.OR.(iabs(kfq(1)).GT.1000.AND.
1032  & iabs(kfq(3)).GT.1000))) i2=4
1033  i3=3
1034  IF(i2.EQ.3) i3=2
1035  i4=10-i1-i2-i3
1036  CALL pydcyk(kfq(i1),kfq(i2),kfldmp,k(n+2,2))
1037  CALL pydcyk(kfq(i3),kfq(i4),kfldmp,k(n+3,2))
1038  IF(k(n+2,2).EQ.0.OR.k(n+3,2).EQ.0) goto 810
1039 
1040 C...Closed string.
1041  ELSE
1042  IF(iabs(k(ic2,2)).NE.21) goto 1140
1043 C...No room for popcorn mesons in closed string -> 2 hadrons.
1044  mstu(125)=0
1045  820 CALL pydcyk(1+int((2d0+parj(2))*pyr(0)),0,kfln,kfdmp)
1046  CALL pydcyk(kfln,0,kflm,k(n+2,2))
1047  CALL pydcyk(-kfln,-kflm,kfldmp,k(n+3,2))
1048  IF(k(n+2,2).EQ.0.OR.k(n+3,2).EQ.0) goto 820
1049  ENDIF
1050  p(n+2,5)=pymass(k(n+2,2))
1051  p(n+3,5)=pymass(k(n+3,2))
1052 
1053 C...If it does not work: try again (a number of times), give up (if no
1054 C...place to shuffle momentum or too many flavours), or form one hadron.
1055  IF(p(n+2,5)+p(n+3,5)+parj(64).GE.pecm) THEN
1056  IF(ntry.LT.mstj(17).OR.(nq.EQ.4.AND.ntry.LT.5*mstj(17))) THEN
1057  goto 790
1058  ELSEIF(nsin.EQ.1.OR.nq.EQ.4) THEN
1059  goto 1140
1060  ELSE
1061  goto 890
1062  END IF
1063  END IF
1064 
1065 C...Perform two-particle decay of jet system.
1066 C...First step: find reference axis in decaying system rest frame.
1067 C...(Borrow slot N+2 for temporary direction.)
1068  DO 830 j=1,4
1069  p(n+2,j)=p(ic1,j)
1070  830 CONTINUE
1071  DO 850 i=ic1+1,ic2-1
1072  IF((k(i,1).EQ.1.OR.k(i,1).EQ.2).AND.
1073  & kchg(pycomp(k(i,2)),2).NE.0) THEN
1074  frac1=four(ic2,i)/(four(ic1,i)+four(ic2,i))
1075  DO 840 j=1,4
1076  p(n+2,j)=p(n+2,j)+frac1*p(i,j)
1077  840 CONTINUE
1078  ENDIF
1079  850 CONTINUE
1080  CALL pyrobo(n+2,n+2,0d0,0d0,-dpc(1)/dpc(4),-dpc(2)/dpc(4),
1081  &-dpc(3)/dpc(4))
1082  the1=pyangl(p(n+2,3),sqrt(p(n+2,1)**2+p(n+2,2)**2))
1083  phi1=pyangl(p(n+2,1),p(n+2,2))
1084 
1085 C...Second step: generate isotropic/anisotropic decay.
1086  pa=sqrt((pecm**2-(p(n+2,5)+p(n+3,5))**2)*(pecm**2-
1087  &(p(n+2,5)-p(n+3,5))**2))/(2d0*pecm)
1088  860 ue(3)=pyr(0)
1089  IF(parj(21).LE.0.01d0) ue(3)=1d0
1090  pt2=(1d0-ue(3)**2)*pa**2
1091  IF(mstj(16).LE.0) THEN
1092  prev=0.5d0
1093  ELSE
1094  IF(exp(-pt2/(2d0*max(0.01d0,parj(21))**2)).LT.pyr(0)) goto 860
1095  pr1=p(n+2,5)**2+pt2
1096  pr2=p(n+3,5)**2+pt2
1097  alambd=sqrt(max(0d0,(pecm**2-pr1-pr2)**2-4d0*pr1*pr2))
1098  prevcf=parj(42)
1099  IF(mstj(11).EQ.2) prevcf=parj(39)
1100  prev=1d0/(1d0+exp(min(50d0,prevcf*alambd*parj(40))))
1101  ENDIF
1102  IF(pyr(0).LT.prev) ue(3)=-ue(3)
1103  phi=paru(2)*pyr(0)
1104  ue(1)=sqrt(1d0-ue(3)**2)*cos(phi)
1105  ue(2)=sqrt(1d0-ue(3)**2)*sin(phi)
1106  DO 870 j=1,3
1107  p(n+2,j)=pa*ue(j)
1108  p(n+3,j)=-pa*ue(j)
1109  870 CONTINUE
1110  p(n+2,4)=sqrt(pa**2+p(n+2,5)**2)
1111  p(n+3,4)=sqrt(pa**2+p(n+3,5)**2)
1112 
1113 C...Third step: move back to event frame and set production vertex.
1114  CALL pyrobo(n+2,n+3,the1,phi1,dpc(1)/dpc(4),dpc(2)/dpc(4),
1115  &dpc(3)/dpc(4))
1116  DO 880 j=1,4
1117  v(n+1,j)=v(ic1,j)
1118  v(n+2,j)=v(ic1,j)
1119  v(n+3,j)=v(ic2,j)
1120  880 CONTINUE
1121  n=n+3
1122  goto 1120
1123 
1124 C...Else form one particle, if possible.
1125  890 nbody=1
1126  k(n+1,5)=n+2
1127  DO 900 j=1,4
1128  v(n+1,j)=v(ic1,j)
1129  v(n+2,j)=v(ic1,j)
1130  900 CONTINUE
1131 
1132 C...Select hadron flavour from available quark flavours.
1133  910 IF(nq.EQ.2.AND.iabs(kfq(1)).GT.100.AND.iabs(kfq(2)).GT.100) THEN
1134  goto 1140
1135  ELSEIF(nq.EQ.2) THEN
1136  CALL pykfdi(kfq(1),kfq(2),kfldmp,k(n+2,2))
1137  ELSE
1138  kfln=1+int((2d0+parj(2))*pyr(0))
1139  CALL pykfdi(kfln,-kfln,kfldmp,k(n+2,2))
1140  ENDIF
1141  IF(k(n+2,2).EQ.0) goto 910
1142  p(n+2,5)=pymass(k(n+2,2))
1143 
1144 C...Use old algorithm for E/p conservation? (EN)
1145  IF (mstj(16).LE.0) goto 1080
1146 
1147 C...Find the string piece closest to the cluster by a loop
1148 C...over the undecayed partons not in present cluster. (EN)
1149  dglomi=1d30
1150  ibeg=0
1151  i0=0
1152  njunc=0
1153  DO 940 i1=max(1,ip),n-1
1154  IF(k(i1,1).EQ.1) njunc=0
1155  IF(k(i1,1).EQ.41) njunc=njunc+1
1156  IF(k(i1,1).EQ.41) goto 940
1157  IF(i1.GE.ic1-1.AND.i1.LE.ic2) THEN
1158  i0=0
1159  ELSEIF(k(i1,1).EQ.2) THEN
1160  IF(i0.EQ.0) i0=i1
1161  i2=i1
1162  920 i2=i2+1
1163  IF(k(i2,1).EQ.41) goto 940
1164  IF(k(i2,1).GT.10) goto 920
1165  IF(kchg(pycomp(k(i2,2)),2).EQ.0) goto 920
1166  IF(k(i1,2).EQ.21.AND.k(i2,2).NE.21.AND.k(i2,1).NE.1.AND.
1167  & njunc.EQ.0) goto 940
1168  IF(k(i1,2).NE.21.AND.k(i2,2).EQ.21.AND.njunc.NE.0) goto 940
1169  IF(k(i1,2).NE.21.AND.k(i2,2).NE.21.AND.(i1.GT.i0.OR.
1170  & k(i2,1).NE.1)) goto 940
1171 
1172 C...Define velocity vectors e1, e2, ecl and differences e3, e4.
1173  DO 930 j=1,3
1174  e1(j)=p(i1,j)/p(i1,4)
1175  e2(j)=p(i2,j)/p(i2,4)
1176  ecl(j)=p(n+1,j)/p(n+1,4)
1177  e3(j)=e2(j)-e1(j)
1178  e4(j)=ecl(j)-e1(j)
1179  930 CONTINUE
1180 
1181 C...Calculate minimal D=(e4-alpha*e3)**2 for 0<alpha<1.
1182  e3s=e3(1)**2+e3(2)**2+e3(3)**2
1183  e4s=e4(1)**2+e4(2)**2+e4(3)**2
1184  e34=e3(1)*e4(1)+e3(2)*e4(2)+e3(3)*e4(3)
1185  IF(e34.LE.0d0) THEN
1186  ddmin=e4s
1187  ELSEIF(e34.LT.e3s) THEN
1188  ddmin=e4s-e34**2/e3s
1189  ELSE
1190  ddmin=e4s-2d0*e34+e3s
1191  ENDIF
1192 
1193 C...Is this the smallest so far?
1194  IF(ddmin.LT.dglomi) THEN
1195  dglomi=ddmin
1196  ibeg=i0
1197  ipcs=i1
1198  ENDIF
1199  ELSEIF(k(i1,1).EQ.1.AND.kchg(pycomp(k(i1,2)),2).NE.0) THEN
1200  i0=0
1201  ENDIF
1202  940 CONTINUE
1203 
1204 C... Check if there are any strings to connect to the new gluon. (EN)
1205  IF (ibeg.EQ.0) goto 1080
1206 
1207 C...Delta_m = m_clus - m_had > 0: emit a 'gluon' (EN)
1208  IF (p(n+1,5).GE.p(n+2,5)) THEN
1209 
1210 C...Construct 'gluon' that is needed to put hadron on the mass shell.
1211  frac=p(n+2,5)/p(n+1,5)
1212  DO 950 j=1,5
1213  p(n+2,j)=frac*p(n+1,j)
1214  pg(j)=(1d0-frac)*p(n+1,j)
1215  950 CONTINUE
1216 
1217 C... Copy string with new gluon put in.
1218  n=n+2
1219  i=ibeg-1
1220  960 i=i+1
1221  IF(k(i,1).NE.1.AND.k(i,1).NE.2.AND.k(i,1).NE.41) goto 960
1222  IF(kchg(pycomp(k(i,2)),2).EQ.0.AND.k(i,1).NE.41) goto 960
1223  n=n+1
1224  DO 970 j=1,5
1225  k(n,j)=k(i,j)
1226  p(n,j)=p(i,j)
1227  v(n,j)=v(i,j)
1228  970 CONTINUE
1229  k(i,1)=k(i,1)+10
1230  k(i,4)=n
1231  k(i,5)=n
1232  k(n,3)=i
1233  IF(i.EQ.ipcs) THEN
1234  n=n+1
1235  DO 980 j=1,5
1236  k(n,j)=k(n-1,j)
1237  p(n,j)=pg(j)
1238  v(n,j)=v(n-1,j)
1239  980 CONTINUE
1240  k(n,2)=21
1241  k(n,3)=nsav+1
1242  ENDIF
1243  IF(k(i,1).EQ.12.OR.k(i,1).EQ.51) goto 960
1244  goto 1120
1245 
1246 C...Delta_m = m_clus - m_had < 0: have to absorb a 'gluon' instead,
1247 C...from string piece endpoints.
1248  ELSE
1249 
1250 C...Begin by copying string that should give energy to cluster.
1251  n=n+2
1252  i=ibeg-1
1253  990 i=i+1
1254  IF(k(i,1).NE.1.AND.k(i,1).NE.2.AND.k(i,1).NE.41) goto 990
1255  IF(kchg(pycomp(k(i,2)),2).EQ.0.AND.k(i,1).NE.41) goto 990
1256  n=n+1
1257  DO 1000 j=1,5
1258  k(n,j)=k(i,j)
1259  p(n,j)=p(i,j)
1260  v(n,j)=v(i,j)
1261  1000 CONTINUE
1262  k(i,1)=k(i,1)+10
1263  k(i,4)=n
1264  k(i,5)=n
1265  k(n,3)=i
1266  IF(i.EQ.ipcs) i1=n
1267  IF(k(i,1).EQ.12.OR.k(i,1).EQ.51) goto 990
1268  i2=i1+1
1269 
1270 C...Set initial Phad.
1271  DO 1010 j=1,4
1272  p(nsav+2,j)=p(nsav+1,j)
1273  1010 CONTINUE
1274 
1275 C...Calculate Pg, a part of which will be added to Phad later. (EN)
1276  1020 IF(mstj(16).EQ.1) THEN
1277  alpha=1d0
1278  beta=1d0
1279  ELSE
1280  alpha=four(nsav+1,i2)/four(i1,i2)
1281  beta=four(nsav+1,i1)/four(i1,i2)
1282  ENDIF
1283  DO 1030 j=1,4
1284  pg(j)=alpha*p(i1,j)+beta*p(i2,j)
1285  1030 CONTINUE
1286  pg(5)=sqrt(max(1d-20,pg(4)**2-pg(1)**2-pg(2)**2-pg(3)**2))
1287 
1288 C..Solve 2nd order equation, use the best (smallest) solution. (EN)
1289  pmscol=p(nsav+2,4)**2-p(nsav+2,1)**2-p(nsav+2,2)**2-
1290  & p(nsav+2,3)**2
1291  pclpg=(p(nsav+2,4)*pg(4)-p(nsav+2,1)*pg(1)-
1292  & p(nsav+2,2)*pg(2)-p(nsav+2,3)*pg(3))/pg(5)**2
1293  delta=sqrt(pclpg**2+(p(nsav+2,5)**2-pmscol)/pg(5)**2)-pclpg
1294 
1295 C...If all gluon energy eaten, zero it and take a step back.
1296  iter=0
1297  IF(delta*alpha.GT.1d0.AND.i1.GT.nsav+3.AND.k(i1,2).EQ.21) THEN
1298  iter=1
1299  DO 1040 j=1,4
1300  p(nsav+2,j)=p(nsav+2,j)+p(i1,j)
1301  p(i1,j)=0d0
1302  1040 CONTINUE
1303  p(i1,5)=0d0
1304  k(i1,1)=k(i1,1)+10
1305  i1=i1-1
1306  IF(k(i1,1).EQ.41) iter=-1
1307  ENDIF
1308  IF(delta*beta.GT.1d0.AND.i2.LT.n.AND.k(i2,2).EQ.21) THEN
1309  iter=1
1310  DO 1050 j=1,4
1311  p(nsav+2,j)=p(nsav+2,j)+p(i2,j)
1312  p(i2,j)=0d0
1313  1050 CONTINUE
1314  p(i2,5)=0d0
1315  k(i2,1)=k(i2,1)+10
1316  i2=i2+1
1317  IF(k(i2,1).EQ.41) iter=-1
1318  ENDIF
1319  IF(iter.EQ.1) goto 1020
1320 
1321 C...If also all endpoint energy eaten, revert to old procedure.
1322  IF((1d0-delta*alpha)*p(i1,4).LT.p(i1,5).OR.
1323  & (1d0-delta*beta)*p(i2,4).LT.p(i2,5).OR.iter.EQ.-1) THEN
1324  DO 1060 i=nsav+3,n
1325  im=k(i,3)
1326  k(im,1)=k(im,1)-10
1327  k(im,4)=0
1328  k(im,5)=0
1329  1060 CONTINUE
1330  n=nsav
1331  goto 1080
1332  ENDIF
1333 
1334 C... Construct the collapsed hadron and modified string partons.
1335  DO 1070 j=1,4
1336  p(nsav+2,j)=p(nsav+2,j)+delta*pg(j)
1337  p(i1,j)=(1d0-delta*alpha)*p(i1,j)
1338  p(i2,j)=(1d0-delta*beta)*p(i2,j)
1339  1070 CONTINUE
1340  p(i1,5)=(1d0-delta*alpha)*p(i1,5)
1341  p(i2,5)=(1d0-delta*beta)*p(i2,5)
1342 
1343 C...Finished with string collapse in new scheme.
1344  goto 1120
1345  ENDIF
1346 
1347 C... Use old algorithm; by choice or when in trouble.
1348  1080 CONTINUE
1349 C...Find parton/particle which combines to largest extra mass.
1350  ir=0
1351  ha=0d0
1352  hsm=0d0
1353  DO 1100 mcomb=1,3
1354  IF(ir.NE.0) goto 1100
1355  DO 1090 i=max(1,ip),n
1356  IF(k(i,1).LE.0.OR.k(i,1).GT.10.OR.(i.GE.ic1.AND.i.LE.ic2
1357  & .AND.k(i,1).GE.1.AND.k(i,1).LE.2)) goto 1090
1358  IF(mcomb.EQ.1) kci=pycomp(k(i,2))
1359  IF(mcomb.EQ.1.AND.kci.EQ.0) goto 1090
1360  IF(mcomb.EQ.1.AND.kchg(kci,2).EQ.0.AND.i.LE.ns) goto 1090
1361  IF(mcomb.EQ.2.AND.iabs(k(i,2)).GT.10.AND.iabs(k(i,2)).LE.100)
1362  & goto 1090
1363  hcr=dpc(4)*p(i,4)-dpc(1)*p(i,1)-dpc(2)*p(i,2)-dpc(3)*p(i,3)
1364  hsr=2d0*hcr+pecm**2-p(n+2,5)**2-2d0*p(n+2,5)*p(i,5)
1365  IF(hsr.GT.hsm) THEN
1366  ir=i
1367  ha=hcr
1368  hsm=hsr
1369  ENDIF
1370  1090 CONTINUE
1371  1100 CONTINUE
1372 
1373 C...Shuffle energy and momentum to put new particle on mass shell.
1374  IF(ir.NE.0) THEN
1375  hb=pecm**2+ha
1376  hc=p(n+2,5)**2+ha
1377  hd=p(ir,5)**2+ha
1378  hk2=0.5d0*(hb*sqrt(max(0d0,((hb+hc)**2-4d0*(hb+hd)*p(n+2,5)**2)/
1379  & (ha**2-(pecm*p(ir,5))**2)))-(hb+hc))/(hb+hd)
1380  hk1=(0.5d0*(p(n+2,5)**2-pecm**2)+hd*hk2)/hb
1381  DO 1110 j=1,4
1382  p(n+2,j)=(1d0+hk1)*dpc(j)-hk2*p(ir,j)
1383  p(ir,j)=(1d0+hk2)*p(ir,j)-hk1*dpc(j)
1384  1110 CONTINUE
1385  n=n+2
1386  ELSE
1387  CALL pyerrm(3,'(PYPREP:) no match for collapsing cluster')
1388  RETURN
1389  ENDIF
1390 
1391 C...Mark collapsed system and store daughter pointers. Iterate.
1392  1120 DO 1130 i=ic1,ic2
1393  IF((k(i,1).EQ.1.OR.k(i,1).EQ.2).AND.
1394  & kchg(pycomp(k(i,2)),2).NE.0) THEN
1395  k(i,1)=k(i,1)+10
1396  IF(mstu(16).NE.2) THEN
1397  k(i,4)=nsav+1
1398  k(i,5)=nsav+1
1399  ELSE
1400  k(i,4)=nsav+2
1401  k(i,5)=nsav+1+nbody
1402  ENDIF
1403  ENDIF
1404  IF(k(i,1).EQ.41) k(i,1)=k(i,1)+10
1405  1130 CONTINUE
1406  IF(n.LT.mstu(4)-mstu(32)-5) goto 710
1407 
1408 C...Check flavours and invariant masses in parton systems.
1409  1140 np=0
1410  kfn=0
1411  kqs=0
1412  nju=0
1413  DO 1150 j=1,5
1414  dps(j)=0d0
1415  1150 CONTINUE
1416  DO 1180 i=max(1,ip),n
1417  IF(k(i,1).EQ.41) nju=nju+1
1418  IF(k(i,1).LE.0.OR.k(i,1).GT.10) goto 1180
1419  kc=pycomp(k(i,2))
1420  IF(kc.EQ.0) goto 1180
1421  kq=kchg(kc,2)*isign(1,k(i,2))
1422  IF(kq.EQ.0) goto 1180
1423  np=np+1
1424  IF(kq.NE.2) THEN
1425  kfn=kfn+1
1426  kqs=kqs+kq
1427  mstj(93)=1
1428  dps(5)=dps(5)+pymass(k(i,2))
1429  ENDIF
1430  DO 1160 j=1,4
1431  dps(j)=dps(j)+p(i,j)
1432  1160 CONTINUE
1433  IF(k(i,1).EQ.1) THEN
1434  nferr=0
1435  IF(nju.EQ.0.AND.np.NE.1) THEN
1436  IF(kfn.EQ.1.OR.kfn.GE.3.OR.kqs.NE.0) nferr=1
1437  ELSEIF(nju.EQ.1) THEN
1438  IF(kfn.NE.3.OR.iabs(kqs).NE.3) nferr=1
1439  ELSEIF(nju.EQ.2) THEN
1440  IF(kfn.NE.4.OR.kqs.NE.0) nferr=1
1441  ELSEIF(nju.GE.3) THEN
1442  nferr=1
1443  ENDIF
1444  IF(nferr.EQ.1) THEN
1445  CALL pyerrm(2,'(PYPREP:) unphysical flavour combination')
1446  mint(51)=1
1447  RETURN
1448  ENDIF
1449  IF(np.NE.1.AND.dps(4)**2-dps(1)**2-dps(2)**2-dps(3)**2.LT.
1450  & (0.9d0*parj(32)+dps(5))**2) CALL pyerrm(3,
1451  & '(PYPREP:) too small mass in jet system')
1452  np=0
1453  kfn=0
1454  kqs=0
1455  nju=0
1456  DO 1170 j=1,5
1457  dps(j)=0d0
1458  1170 CONTINUE
1459  ENDIF
1460  1180 CONTINUE
1461 
1462  RETURN
1463  END