@@ -184,7 +184,6 @@ protected:
184184
185185 for (auto iPart {0 }; iPart < mPythia .event .size (); ++ iPart )
186186 {
187-
188187 // search for Q-Qbar mother with at least one Q in rapidity window
189188 if (!isGoodAtPartonLevel )
190189 {
@@ -254,7 +253,7 @@ protected:
254253
255254 bool replaceParticle (int iPartToReplace , int pdgCodeNew ) {
256255
257- std :: array < int , 2 > mothers = { mPythia .event [iPartToReplace ].mother1 (), mPythia . event [ iPartToReplace ]. mother2 ()} ;
256+ auto mothers = mPythia .event [iPartToReplace ].motherList () ;
258257
259258 std ::array < int , 25 > pdgDiquarks = {1103 , 2101 , 2103 , 2203 , 3101 , 3103 , 3201 , 3203 , 3303 , 4101 , 4103 , 4201 , 4203 , 4301 , 4303 , 4403 , 5101 , 5103 , 5201 , 5203 , 5301 , 5303 , 5401 , 5403 , 5503 };
260259
@@ -288,15 +287,57 @@ protected:
288287 }
289288 float energy = std ::sqrt (px * px + py * py + pz * pz + mass * mass );
290289
290+ // buffer daughter indices of mothers
291+ std ::vector < std ::vector < int >> dauOfMothers {};
292+ for (auto const & mother : mothers ) {
293+ dauOfMothers .push_back (mPythia .event [mother ].daughterList ());
294+ }
295+
291296 // we remove particle to replace and its daughters
292297 mPythia .event [iPartToReplace ].undoDecay ();
293- mPythia .event .remove (iPartToReplace , iPartToReplace , true); // we remove the original particle
294-
295- int status = std ::abs (mPythia .event [iPartToReplace ].status ());
298+ int status = std ::abs (mPythia .event [iPartToReplace ].status ()); // we must get it here otherwise it is negative (already decayed)
296299 if (status < 81 || status > 89 ) {
297300 status = 81 ;
298301 }
299- mPythia .event .append (charge * pdgCodeNew , status , mothers [0 ], mothers [1 ], 0 , 0 , 0 , 0 , px , py , pz , energy , mass );
302+ mPythia .event .remove (iPartToReplace , iPartToReplace , true); // we remove the original particle
303+
304+ // we restore the daughter indices of the mothers after the removal
305+ int newPartIdx {0 };
306+ std ::array < int , 2 > newMothers = {0 , 0 };
307+ if (mGenConfig .includePartonEvent ) { // only necessary in case of parton event, otherwise we keep no mother
308+ newMothers [0 ] = mothers .front ();
309+ newMothers [1 ] = mothers .back ();
310+ newPartIdx = mPythia .event .size ();
311+ }
312+ for (auto iMom {0u }; iMom < mothers .size (); ++ iMom ) {
313+ auto dau1 = dauOfMothers [iMom ].front ();
314+ auto dau2 = dauOfMothers [iMom ].back ();
315+ if (dau2 > dau1 ) {
316+ mPythia .event [mothers [iMom ]].daughter1 (dau1 );
317+ mPythia .event [mothers [iMom ]].daughter2 (dau2 - 1 );
318+ } else if (dau1 == dau2 ) {
319+ if (dau1 == 0 ) {
320+ mPythia .event [mothers [iMom ]].daughter1 (0 );
321+ mPythia .event [mothers [iMom ]].daughter2 (0 );
322+ } else { // in this case we set it equal to the new particle
323+ mPythia .event [mothers [iMom ]].daughter1 (newPartIdx );
324+ mPythia .event [mothers [iMom ]].daughter2 (newPartIdx );
325+ }
326+ } else if (dau2 < dau1 ) { // in this case we set it equal to the new particle
327+ if (dau2 == 0 ) {
328+ mPythia .event [mothers [iMom ]].daughter1 (newPartIdx );
329+ } else {
330+ if (dau1 == iPartToReplace ) {
331+ mPythia .event [mothers [iMom ]].daughter1 (newPartIdx );
332+ } else {
333+ mPythia .event [mothers [iMom ]].daughter2 (newPartIdx );
334+ }
335+ }
336+ }
337+ auto dauOfMomUp = mPythia .event [mothers [iMom ]].daughterList ();
338+ }
339+
340+ mPythia .event .append (charge * pdgCodeNew , status , newMothers [0 ], newMothers [1 ], 0 , 0 , 0 , 0 , px , py , pz , energy , mass );
300341 mPythia .moreDecays (); // we need to decay the new particle
301342
302343 return true;
0 commit comments