StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
History.cc
1 // History.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2018 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // This file is written by Stefan Prestel.
7 // Function definitions (not found in the header) for the
8 // Clustering and History classes.
9 
10 #include "Pythia8/History.h"
11 
12 namespace Pythia8 {
13 
14 //==========================================================================
15 
16 // The Clustering class.
17 
18 //--------------------------------------------------------------------------
19 
20 // Declaration of Clustering class
21 // This class holds information about one radiator, recoiler,
22 // emitted system.
23 // This class is a container class for History class use.
24 
25 // print for debug
26 void Clustering::list() const {
27  cout << " emt " << emitted
28  << " rad " << emittor
29  << " rec " << recoiler
30  << " partner " << partner
31  << " pTscale " << pTscale << endl;
32 }
33 
34 //==========================================================================
35 
36 // The History class.
37 
38 // A History object represents an event in a given step in the CKKW-L
39 // clustering procedure. It defines a tree-like recursive structure,
40 // where the root node represents the state with n jets as given by
41 // the matrix element generator, and is characterized by the member
42 // variable mother being null. The leaves on the tree corresponds to a
43 // fully clustered paths where the original n-jets has been clustered
44 // down to the Born-level state. Also states which cannot be clustered
45 // down to the Born-level are possible - these will be called
46 // incomplete. The leaves are characterized by the vector of children
47 // being empty.
48 
49 //--------------------------------------------------------------------------
50 
51 // Number of trial emission to use for calculating the average number of
52 // emissions
53 const int History::NTRIAL = 1;
54 
55 //--------------------------------------------------------------------------
56 
57 // Declaration of History class
58 // The only constructor. Default arguments are used when creating
59 // the initial history node. The \a depth is the maximum number of
60 // clusterings requested. \a scalein is the scale at which the \a
61 // statein was clustered (should be set to the merging scale for the
62 // initial history node. \a beamAIn and beamBIn are needed to
63 // calcutate PDF ratios, \a particleDataIn to have access to the
64 // correct masses of particles. If \a isOrdered is true, the previous
65 // clusterings has been ordered. \a is the PDF ratio for this
66 // clustering (=1 for FSR clusterings). \a probin is the accumulated
67 // probabilities for the previous clusterings, and \ mothin is the
68 // previous history node (null for the initial node).
69 
70 History::History( int depth,
71  double scalein,
72  Event statein,
73  Clustering c,
74  MergingHooks* mergingHooksPtrIn,
75  BeamParticle beamAIn,
76  BeamParticle beamBIn,
77  ParticleData* particleDataPtrIn,
78  Info* infoPtrIn,
79  PartonLevel* showersIn,
80  CoupSM* coupSMPtrIn,
81  bool isOrdered = true,
82  bool isStronglyOrdered = true,
83  bool isAllowed = true,
84  bool isNextInInput = true,
85  double probin = 1.0,
86  History * mothin = 0)
87  : state(statein),
88  mother(mothin),
89  selectedChild(-1),
90  sumpath(0.0),
91  sumGoodBranches(0.0),
92  sumBadBranches(0.0),
93  foundOrderedPath(false),
94  foundStronglyOrderedPath(false),
95  foundAllowedPath(false),
96  foundCompletePath(false),
97  scale(scalein),
98  nextInInput(isNextInInput),
99  prob(probin),
100  clusterIn(c),
101  iReclusteredOld(0),
102  doInclude(true),
103  mergingHooksPtr(mergingHooksPtrIn),
104  beamA(beamAIn),
105  beamB(beamBIn),
106  particleDataPtr(particleDataPtrIn),
107  infoPtr(infoPtrIn),
108  showers(showersIn),
109  coupSMPtr(coupSMPtrIn)
110 
111  {
112 
113  // Initialise beam particles
114  setupBeams();
115 
116  // Update probability with PDF ratio
117  if (mother && mergingHooksPtr->includeRedundant()) prob *= pdfForSudakov();
118 
119  // Minimal scalar sum of pT used in Herwig to choose history
120  // Keep track of scalar PT
121  if (mother) {
122  double acoll = (mother->state[clusterIn.emittor].isFinal())
123  ? mergingHooksPtr->herwigAcollFSR()
124  : mergingHooksPtr->herwigAcollISR();
125  sumScalarPT = mother->sumScalarPT + acoll*scale;
126  } else
127  sumScalarPT = 0.0;
128 
129  // Remember reclustered radiator in lower multiplicity state
130  if ( mother ) iReclusteredOld = mother->iReclusteredNew;
131 
132  // Check if more steps should be taken.
133  int nFinalP = 0, nFinalW = 0, nFinalZ = 0;
134  int nL = 0, nA= 0, nH = 0;
135  for ( int i = 0; i < int(state.size()); ++i )
136  if ( state[i].isFinal() ) {
137  if ( state[i].colType() != 0 )
138  nFinalP++;
139  if ( state[i].idAbs() == 23 )
140  nFinalZ++;
141  if ( state[i].idAbs() == 24 )
142  nFinalW++;
143  if ( state[i].idAbs() < 20 && state[i].idAbs() > 10)
144  nL++;
145  if ( state[i].idAbs() == 22)
146  nA++;
147  if ( state[i].idAbs() == 23
148  || state[i].idAbs() == 24
149  || state[i].idAbs() == 25)
150  nH++;
151  }
152  if ( mergingHooksPtr->doWeakClustering()
153  && nFinalP == 2 && nFinalW == 0 && nFinalZ == 0) depth = 0;
154 
155  // Stop clustering at 2->1 massive.
156  // Stop clustering at 2->2 massless.
157 
158  bool qcd = ( nFinalP > mergingHooksPtr->hardProcess->nQuarksOut() );
159 
160  // If this is not the fully clustered state, try to find possible
161  // QCD clusterings.
162  vector<Clustering> clusterings;
163  if ( qcd && depth > 0 ) clusterings = getAllQCDClusterings();
164 
165  bool dow = ( mergingHooksPtr->doWeakClustering()
166  && nFinalP > 1 && nFinalW+nFinalZ > 0 );
167 
168  // If necessary, try to find possible EW clusterings.
169  vector<Clustering> clusteringsEW;
170  // if ( depth > 0 && mergingHooksPtr->doWeakClustering() )
171  if ( depth > 0 && dow )
172  clusteringsEW = getAllEWClusterings();
173  if ( !clusteringsEW.empty() ) {
174  clusterings.insert( clusterings.end(), clusteringsEW.begin(),
175  clusteringsEW.end() );
176  }
177 
178  // If necessary, try to find possible SQCD clusterings.
179  vector<Clustering> clusteringsSQCD;
180  if ( depth > 0 && mergingHooksPtr->doSQCDClustering() )
181  clusteringsSQCD = getAllSQCDClusterings();
182  if ( !clusteringsSQCD.empty() )
183  clusterings.insert( clusterings.end(), clusteringsSQCD.begin(),
184  clusteringsSQCD.end() );
185 
186  // If no clusterings were found, the recursion is done and we
187  // register this node.
188  if ( clusterings.empty() ) {
189  // Multiply with hard process matrix element.
190  prob *= hardProcessME(state);
191  registerPath( *this, isOrdered, isStronglyOrdered, isAllowed, depth == 0 );
192  return;
193  }
194 
195  // Now we sort the possible clusterings so that we try the
196  // smallest scale first.
197  multimap<double, Clustering *> sorted;
198  for ( int i = 0, N = clusterings.size(); i < N; ++i ) {
199  sorted.insert(make_pair(clusterings[i].pT(), &clusterings[i]));
200  }
201 
202  for ( multimap<double, Clustering *>::iterator it = sorted.begin();
203  it != sorted.end(); ++it ) {
204 
205  // If this path is not strongly ordered and we already have found an
206  // ordered path, then we don't need to continue along this path.
207  bool stronglyOrdered = isStronglyOrdered;
208  if ( mergingHooksPtr->enforceStrongOrdering()
209  && ( !stronglyOrdered
210  || ( mother && ( it->first <
211  mergingHooksPtr->scaleSeparationFactor()*scale ) ))) {
212  if ( onlyStronglyOrderedPaths() ) continue;
213  stronglyOrdered = false;
214  }
215 
216  // Check if reclustering follows ordered sequence.
217  bool ordered = isOrdered;
218  if ( mergingHooksPtr->orderInRapidity()
219  && mergingHooksPtr->orderHistories() ) {
220  // Get new z value
221  double z = getCurrentZ((*it->second).emittor,
222  (*it->second).recoiler,(*it->second).emitted,
223  (*it->second).flavRadBef);
224  // Get z value of splitting that produced this state
225  double zOld = (!mother) ? 0. : mother->getCurrentZ(clusterIn.emittor,
226  clusterIn.recoiler,clusterIn.emitted,
227  clusterIn.flavRadBef);
228  // If this path is not ordered in pT and y, and we already have found
229  // an ordered path, then we don't need to continue along this path.
230  if ( !ordered || ( mother && (it->first < scale
231  || it->first < pow(1. - z,2) / (z * (1. - zOld ))*scale ))) {
232  if ( onlyOrderedPaths() ) continue;
233  ordered = false;
234  }
235 
236  } else if ( mergingHooksPtr->orderHistories() ) {
237  // If this path is not ordered in pT and we already have found an
238  // ordered path, then we don't need to continue along this path, unless
239  // we have not yet found an allowed path.
240  if ( !ordered || ( mother && (it->first < scale) ) ) {
241  if ( onlyOrderedPaths() && onlyAllowedPaths() ) continue;
242  ordered = false;
243  }
244  }
245 
246  // Check if reclustered state should be disallowed.
247  bool doCut = mergingHooksPtr->canCutOnRecState()
248  || mergingHooksPtr->allowCutOnRecState();
249  bool allowed = isAllowed;
250  if ( doCut
251  && mergingHooksPtr->doCutOnRecState(cluster(*it->second)) ) {
252  if ( onlyAllowedPaths() ) continue;
253  allowed = false;
254  }
255 
256  // Perform the clustering and recurse and construct the next
257  // history node.
258  children.push_back(new History(depth - 1,it->first,cluster(*it->second),
259  *it->second, mergingHooksPtr, beamA, beamB, particleDataPtr,
260  infoPtr, showers, coupSMPtr, ordered, stronglyOrdered, allowed,
261  true, prob*getProb(*it->second), this ));
262  }
263 }
264 
265 //--------------------------------------------------------------------------
266 
267 // Function to project all possible paths onto only the desired paths.
268 
269 bool History::projectOntoDesiredHistories() {
270  // At the moment, only trim histories.
271  return trimHistories();
272 }
273 
274 //--------------------------------------------------------------------------
275 
276 // In the initial history node, select one of the paths according to
277 // the probabilities. This function should be called for the initial
278 // history node.
279 // IN trialShower* : Previously initialised trialShower object,
280 // to perform trial showering and as
281 // repository of pointers to initialise alphaS
282 // PartonSystems* : PartonSystems object needed to initialise
283 // shower objects
284 // OUT double : (Sukadov) , (alpha_S ratios) , (PDF ratios)
285 
286 double History::weightTREE(PartonLevel* trial, AlphaStrong * asFSR,
287  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN) {
288 
289  if ( mergingHooksPtr->canCutOnRecState() && !foundAllowedPath ) {
290  string message="Warning in History::weightTREE: No allowed history";
291  message+=" found. Using disallowed history.";
292  infoPtr->errorMsg(message);
293  }
294  if ( mergingHooksPtr->orderHistories() && !foundOrderedPath ) {
295  string message="Warning in History::weightTREE: No ordered history";
296  message+=" found. Using unordered history.";
297  infoPtr->errorMsg(message);
298  }
299  if ( mergingHooksPtr->canCutOnRecState()
300  && mergingHooksPtr->orderHistories()
301  && !foundAllowedPath && !foundOrderedPath ) {
302  string message="Warning in History::weightTREE: No allowed or ordered";
303  message+=" history found.";
304  infoPtr->errorMsg(message);
305  }
306 
307  // Read alpha_S in ME calculation and maximal scale (eCM)
308  double asME = infoPtr->alphaS();
309  double aemME = infoPtr->alphaEM();
310  double maxScale = (foundCompletePath) ? infoPtr->eCM()
311  : mergingHooksPtr->muFinME();
312 
313  // Select a path of clusterings
314  History * selected = select(RN);
315 
316  // Set scales in the states to the scales pythia would have set
317  selected->setScalesInHistory();
318 
319  // Get weight.
320  double sudakov = 1.;
321  double asWeight = 1.;
322  double aemWeight = 1.;
323  double pdfWeight = 1.;
324 
325  // Do trial shower, calculation of alpha_S ratios, PDF ratios
326  sudakov = selected->weightTree( trial, asME, aemME, maxScale,
327  selected->clusterIn.pT(), asFSR, asISR, aemFSR, aemISR, asWeight,
328  aemWeight, pdfWeight );
329 
330  // MPI no-emission probability
331  int njetsMaxMPI = mergingHooksPtr->nMinMPI();
332  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
333  maxScale );
334 
335  // Set hard process renormalisation scale to default Pythia value.
336  bool resetScales = mergingHooksPtr->resetHardQRen();
337 
338  // For pure QCD dijet events, evaluate the coupling of the hard process at
339  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
340  // arbitrary scale.
341  if ( resetScales
342  && mergingHooksPtr->getProcessString().compare("pp>jj") == 0) {
343  // Reset to a running coupling. Here we choose FSR for simplicity.
344  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
345  double runningCoupling = (*asFSR).alphaS(newQ2Ren) / asME;
346  asWeight *= pow2(runningCoupling);
347  } else if (mergingHooksPtr->doWeakClustering()
348  && isQCD2to2(selected->state)) {
349  // Reset to a running coupling. Here we choose FSR for simplicity.
350  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
351  double runningCoupling = (*asFSR).alphaS(newQ2Ren) / asME;
352  asWeight *= pow2(runningCoupling);
353  }
354 
355  // For W clustering, correct the \alpha_em.
356  if (mergingHooksPtr->doWeakClustering() && isEW2to1(selected->state)) {
357  // Reset to a running coupling. Here we choose FSR for simplicity.
358  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
359  double runningCoupling = (*aemFSR).alphaEM(newQ2Ren) / aemME;
360  aemWeight *= runningCoupling;
361  }
362 
363  // For prompt photon events, evaluate the coupling of the hard process at
364  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
365  // arbitrary scale.
366  if ( resetScales
367  && mergingHooksPtr->getProcessString().compare("pp>aj") == 0) {
368  // Reset to a running coupling. In prompt photon always ISR.
369  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
370  double runningCoupling =
371  (*asISR).alphaS( newQ2Ren + pow(mergingHooksPtr->pT0ISR(),2) ) / asME;
372  asWeight *= runningCoupling;
373  }
374 
375  // Done
376  return (sudakov*asWeight*aemWeight*pdfWeight*mpiwt);
377 
378 }
379 
380 //--------------------------------------------------------------------------
381 
382 // Function to return weight of virtual correction and subtractive events
383 // for NL3 merging
384 
385 double History::weightLOOP(PartonLevel* trial, double RN ) {
386 
387  if ( mergingHooksPtr->canCutOnRecState() && !foundAllowedPath ) {
388  string message="Warning in History::weightLOOP: No allowed history";
389  message+=" found. Using disallowed history.";
390  infoPtr->errorMsg(message);
391  }
392 
393  // Select a path of clusterings
394  History * selected = select(RN);
395  // Set scales in the states to the scales pythia would have set
396  selected->setScalesInHistory();
397 
398  // So far, no reweighting
399  double wt = 1.;
400 
401  // Only reweighting with MPI no-emission probability
402  double maxScale = (foundCompletePath) ? infoPtr->eCM()
403  : mergingHooksPtr->muFinME();
404  int njetsMaxMPI = mergingHooksPtr->nMinMPI();
405  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
406  maxScale );
407  wt = mpiwt;
408  // Done
409  return wt;
410 }
411 
412 //--------------------------------------------------------------------------
413 
414 // Function to calculate O(\alpha_s)-term of CKKWL-weight for NLO merging
415 
416 double History::weightFIRST(PartonLevel* trial, AlphaStrong* asFSR,
417  AlphaStrong* asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN,
418  Rndm* rndmPtr ) {
419 
420  // Dummy statement to avoid compiler warnings.
421  if (false) cout << aemFSR << aemISR;
422 
423  // Read alpha_S in ME calculation and maximal scale (eCM)
424  double asME = infoPtr->alphaS();
425  double muR = mergingHooksPtr->muRinME();
426  double maxScale = (foundCompletePath)
427  ? infoPtr->eCM()
428  : mergingHooksPtr->muFinME();
429 
430  // Pick path of clusterings
431  History * selected = select(RN);
432  // Set scales in the states to the scales pythia would have set
433  selected->setScalesInHistory();
434 
435  double nSteps = mergingHooksPtr->getNumberOfClusteringSteps(state);
436 
437  // Get the lowest order k-factor and add first two terms in expansion
438  double kFactor = asME * mergingHooksPtr->k1Factor(nSteps);
439 
440  // If using Bbar, which includes a tree-level part, subtract an
441  // additional one, i.e. the O(\as^0) contribution as well
442  double wt = 1. + kFactor;
443 
444  // Calculate sum of O(alpha) terms
445  wt += selected->weightFirst(trial,asME, muR, maxScale, asFSR, asISR,
446  rndmPtr );
447 
448  // Get starting scale for trial showers.
449  double startingScale = (selected->mother) ? state.scale() : infoPtr->eCM();
450 
451  // Count emissions: New variant
452  // Generate true average, not only one-point
453  bool fixpdf = true;
454  bool fixas = true;
455  double nWeight1 = 0.;
456  for(int i=0; i < NTRIAL; ++i) {
457  // Get number of emissions
458  vector<double> unresolvedEmissionTerm = countEmissions( trial,
459  startingScale, mergingHooksPtr->tms(), 2, asME, asFSR, asISR, 3,
460  fixpdf, fixas );
461  nWeight1 += unresolvedEmissionTerm[1];
462  }
463 
464  wt += nWeight1/double(NTRIAL);
465 
466  // Done
467  return wt;
468 
469 }
470 
471 //--------------------------------------------------------------------------
472 
473 double History::weight_UMEPS_TREE(PartonLevel* trial, AlphaStrong * asFSR,
474  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN) {
475  // No difference to CKKW-L. Recycle CKKW-L function.
476  return weightTREE( trial, asFSR, asISR, aemFSR, aemISR, RN);
477 }
478 
479 //--------------------------------------------------------------------------
480 
481 // Function to return weight of virtual correction events for NLO merging
482 
483 double History::weight_UMEPS_SUBT(PartonLevel* trial, AlphaStrong * asFSR,
484  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN ) {
485 
486  // Read alpha_S in ME calculation and maximal scale (eCM)
487  double asME = infoPtr->alphaS();
488  double aemME = infoPtr->alphaEM();
489  double maxScale = (foundCompletePath) ? infoPtr->eCM()
490  : mergingHooksPtr->muFinME();
491  // Select a path of clusterings
492  History * selected = select(RN);
493  // Set scales in the states to the scales pythia would have set
494  selected->setScalesInHistory();
495 
496  // Get weight.
497  double sudakov = 1.;
498  double asWeight = 1.;
499  double aemWeight = 1.;
500  double pdfWeight = 1.;
501 
502  // Do trial shower, calculation of alpha_S ratios, PDF ratios
503  sudakov = selected->weightTree(trial, asME, aemME, maxScale,
504  selected->clusterIn.pT(), asFSR, asISR, aemFSR, aemISR, asWeight,
505  aemWeight, pdfWeight);
506 
507  // MPI no-emission probability.
508  int njetsMaxMPI = mergingHooksPtr->nMinMPI()+1;
509  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
510  maxScale );
511 
512  // Set hard process renormalisation scale to default Pythia value.
513  bool resetScales = mergingHooksPtr->resetHardQRen();
514  // For pure QCD dijet events, evaluate the coupling of the hard process at
515  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
516  // arbitrary scale.
517  if ( resetScales
518  && mergingHooksPtr->getProcessString().compare("pp>jj") == 0) {
519  // Reset to a running coupling. Here we choose FSR for simplicity.
520  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
521  double runningCoupling = (*asFSR).alphaS(newQ2Ren) / asME;
522  asWeight *= pow(runningCoupling,2);
523  }
524 
525  // For prompt photon events, evaluate the coupling of the hard process at
526  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
527  // arbitrary scale.
528  if ( resetScales
529  && mergingHooksPtr->getProcessString().compare("pp>aj") == 0) {
530  // Reset to a running coupling. In prompt photon always ISR.
531  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
532  double runningCoupling =
533  (*asISR).alphaS( newQ2Ren + pow(mergingHooksPtr->pT0ISR(),2) ) / asME;
534  asWeight *= runningCoupling;
535  }
536 
537  // Done
538  return (sudakov*asWeight*aemWeight*pdfWeight*mpiwt);
539 
540 }
541 
542 //--------------------------------------------------------------------------
543 
544 double History::weight_UNLOPS_TREE(PartonLevel* trial, AlphaStrong * asFSR,
545  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN,
546  int depth) {
547 
548  // Read alpha_S in ME calculation and maximal scale (eCM)
549  double asME = infoPtr->alphaS();
550  double aemME = infoPtr->alphaEM();
551  double maxScale = (foundCompletePath) ? infoPtr->eCM()
552  : mergingHooksPtr->muFinME();
553  // Select a path of clusterings
554  History * selected = select(RN);
555  // Set scales in the states to the scales pythia would have set
556  selected->setScalesInHistory();
557 
558  // Get weight.
559  double asWeight = 1.;
560  double aemWeight = 1.;
561  double pdfWeight = 1.;
562 
563  // Do trial shower, calculation of alpha_S ratios, PDF ratios
564  double wt = 1.;
565  if (depth < 0) wt = selected->weightTree(trial, asME, aemME, maxScale,
566  selected->clusterIn.pT(), asFSR, asISR, aemFSR, aemISR, asWeight,
567  aemWeight, pdfWeight);
568  else {
569  wt = selected->weightTreeEmissions( trial, 1, 0, depth, maxScale );
570  if (wt != 0.) asWeight = selected->weightTreeALPHAS( asME, asFSR, asISR,
571  depth);
572  if (wt != 0.) aemWeight = selected->weightTreeALPHAEM( aemME, aemFSR,
573  aemISR, depth);
574  if (wt != 0.) pdfWeight = selected->weightTreePDFs( maxScale,
575  selected->clusterIn.pT(), depth);
576  }
577 
578  // MPI no-emission probability.
579  int njetsMaxMPI = mergingHooksPtr->nMinMPI();
580  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
581  maxScale );
582 
583  // Set hard process renormalisation scale to default Pythia value.
584  bool resetScales = mergingHooksPtr->resetHardQRen();
585  // For pure QCD dijet events, evaluate the coupling of the hard process at
586  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
587  // arbitrary scale.
588  if ( resetScales
589  && mergingHooksPtr->getProcessString().compare("pp>jj") == 0) {
590  // Reset to a running coupling. Here we choose FSR for simplicity.
591  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
592  double runningCoupling = (*asFSR).alphaS(newQ2Ren) / asME;
593  asWeight *= pow(runningCoupling,2);
594  }
595 
596  // For prompt photon events, evaluate the coupling of the hard process at
597  // a more reasonable pT, rather than evaluation \alpha_s at a fixed
598  // arbitrary scale.
599  if ( resetScales
600  && mergingHooksPtr->getProcessString().compare("pp>aj") == 0) {
601  // Reset to a running coupling. In prompt photon always ISR.
602  double newQ2Ren = pow2( selected->hardRenScale(selected->state) );
603  double runningCoupling =
604  (*asISR).alphaS( newQ2Ren + pow(mergingHooksPtr->pT0ISR(),2) ) / asME;
605  asWeight *= runningCoupling;
606  }
607 
608  // Done
609  return (wt*asWeight*aemWeight*pdfWeight*mpiwt);
610 
611 }
612 
613 //--------------------------------------------------------------------------
614 
615 double History::weight_UNLOPS_LOOP(PartonLevel* trial, AlphaStrong * asFSR,
616  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN,
617  int depth) {
618  // No difference to default NL3
619  if (depth < 0) return weightLOOP(trial, RN);
620  else return weight_UNLOPS_TREE(trial, asFSR,asISR, aemFSR,aemISR, RN,depth);
621 }
622 
623 //--------------------------------------------------------------------------
624 
625 double History::weight_UNLOPS_SUBT(PartonLevel* trial, AlphaStrong * asFSR,
626  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN,
627  int depth) {
628 
629  // Select a path of clusterings
630  History * selected = select(RN);
631  // Set scales in the states to the scales pythia would have set
632  selected->setScalesInHistory();
633  // So far, no reweighting
634  double wt = 1.;
635 
636  // Read alpha_S in ME calculation and maximal scale (eCM)
637  double asME = infoPtr->alphaS();
638  double aemME = infoPtr->alphaEM();
639  double maxScale = (foundCompletePath)
640  ? infoPtr->eCM()
641  : mergingHooksPtr->muFinME();
642 
643  // Only allow two clusterings if all intermediate states above the
644  // merging scale.
645  double nSteps = mergingHooksPtr->getNumberOfClusteringSteps(state);
646  if ( nSteps == 2 && mergingHooksPtr->nRecluster() == 2
647  && ( !foundCompletePath
648  || !selected->allIntermediateAboveRhoMS( mergingHooksPtr->tms() )) )
649  return 0.;
650 
651  // Get weights: alpha_S ratios and PDF ratios
652  double asWeight = 1.;
653  double aemWeight = 1.;
654  double pdfWeight = 1.;
655  // Do trial shower, calculation of alpha_S ratios, PDF ratios
656  double sudakov = 1.;
657  if (depth < 0)
658  sudakov = selected->weightTree(trial, asME, aemME, maxScale,
659  selected->clusterIn.pT(), asFSR, asISR, aemFSR, aemISR, asWeight,
660  aemWeight, pdfWeight);
661  else {
662  sudakov = selected->weightTreeEmissions( trial, 1, 0, depth, maxScale );
663  if (sudakov > 0.) asWeight = selected->weightTreeALPHAS( asME, asFSR,
664  asISR, depth);
665  if (sudakov > 0.) aemWeight = selected->weightTreeALPHAEM( aemME, aemFSR,
666  aemISR, depth);
667  if (sudakov > 0.) pdfWeight = selected->weightTreePDFs( maxScale,
668  selected->clusterIn.pT(), depth);
669  }
670 
671  // MPI no-emission probability.
672  int njetsMaxMPI = mergingHooksPtr->nMinMPI()+1;
673  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
674  maxScale );
675 
676  // Set weight
677  wt = ( mergingHooksPtr->nRecluster() == 2 ) ? 1.
678  : asWeight*aemWeight*pdfWeight*sudakov*mpiwt;
679 
680  // Done
681  return wt;
682 
683 }
684 
685 //--------------------------------------------------------------------------
686 
687 double History::weight_UNLOPS_SUBTNLO(PartonLevel* trial, AlphaStrong * asFSR,
688  AlphaStrong * asISR, AlphaEM * aemFSR, AlphaEM * aemISR, double RN,
689  int depth) {
690 
691  if (depth < 0) {
692 
693  // Select a path of clusterings
694  History * selected = select(RN);
695  // Set scales in the states to the scales pythia would have set
696  selected->setScalesInHistory();
697  // So far, no reweighting
698  double wt = 1.;
699  // Only reweighting with MPI no-emission probability
700  double maxScale = (foundCompletePath) ? infoPtr->eCM()
701  : mergingHooksPtr->muFinME();
702  int njetsMaxMPI = mergingHooksPtr->nMinMPI()+1;
703  double mpiwt = selected->weightTreeEmissions( trial, -1, 0, njetsMaxMPI,
704  maxScale );
705  wt = mpiwt;
706  // Done
707  return wt;
708 
709  } else return weight_UNLOPS_SUBT(trial, asFSR, asISR, aemFSR, aemISR, RN,
710  depth);
711 
712 }
713 
714 //--------------------------------------------------------------------------
715 
716 // Function to calculate O(\alpha_s)-term of CKKWL-weight for NLO merging
717 
718 double History::weight_UNLOPS_CORRECTION( int order, PartonLevel* trial,
719  AlphaStrong* asFSR, AlphaStrong* asISR, AlphaEM * aemFSR, AlphaEM * aemISR,
720  double RN, Rndm* rndmPtr ) {
721 
722  // Dummy statement to avoid compiler warnings.
723  if (false) cout << aemFSR << aemISR;
724 
725  // Already done if no correction should be calculated
726  if ( order < 0 ) return 0.;
727 
728  // Read alpha_S in ME calculation and maximal scale (eCM)
729  double asME = infoPtr->alphaS();
730  //double aemME = infoPtr->alphaEM();
731  double muR = mergingHooksPtr->muRinME();
732  double maxScale = (foundCompletePath)
733  ? infoPtr->eCM()
734  : mergingHooksPtr->muFinME();
735 
736  // Pick path of clusterings
737  History * selected = select(RN);
738  // Set scales in the states to the scales pythia would have set
739  selected->setScalesInHistory();
740 
741  double nSteps = mergingHooksPtr->getNumberOfClusteringSteps(state);
742 
743  // Get the lowest order k-factor and add first two terms in expansion
744  double kFactor = asME * mergingHooksPtr->k1Factor(nSteps);
745 
746  // If using Bbar, which includes a tree-level part, subtract an
747  // additional one, i.e. the O(\as^0) contribution as well
748  double wt = 1.;
749 
750  // If only O(\alpha_s^0)-term is to be calculated, done already.
751  if ( order == 0 ) return wt;
752 
753  // Start by adding the O(\alpha_s^1)-term of the k-factor.
754  wt += kFactor;
755 
756  // Calculate sum of O(\alpha_s^1)-terms of the ckkw-l weight WITHOUT
757  // the O(\alpha_s^1)-term of the last no-emission probability.
758  bool fixpdf = true;
759  bool fixas = true;
760  // Get first term in expansion of alpha_s ratios.
761  double wA = selected->weightFirstALPHAS( asME, muR, asFSR, asISR );
762  // Add logarithm from \alpha_s expansion to weight.
763  wt += (fixas) ? wA : 0.;
764  // Generate true average, not only one-point.
765  double nWeight = 0.;
766  for ( int i=0; i < NTRIAL; ++i ) {
767  // Get average number of emissions.
768  double wE = selected->weightFirstEmissions(trial,asME, maxScale,
769  asFSR, asISR, fixpdf, fixas );
770  // Add average number of emissions off reconstructed states to weight.
771  nWeight += wE;
772  // Get first term in expansion of PDF ratios.
773  double pscale = selected->clusterIn.pT();
774  double wP = selected->weightFirstPDFs(asME, maxScale, pscale, rndmPtr);
775  // Add integral of DGLAP shifted PDF ratios from \alpha_s expansion to wt.
776  nWeight += (fixpdf) ? wP : 0.;
777  }
778  wt += nWeight/double(NTRIAL);
779 
780  // If O(\alpha_s^1)-term + O(\alpha_s^1)-term is to be calculated, done.
781  if ( order == 1 ) return wt;
782 
783  // So far, no calculation of O(\alpha_s^2)-term
784  return 0.;
785 
786 }
787 
788 //--------------------------------------------------------------------------
789 
790 // Function to set the state with complete scales for evolution.
791 
792 void History::getStartingConditions( const double RN, Event& outState ) {
793 
794  // Select the history
795  History * selected = select(RN);
796 
797  // Set scales in the states to the scales pythia would have set
798  selected->setScalesInHistory();
799 
800  // Get number of clustering steps.
801  int nSteps = mergingHooksPtr->getNumberOfClusteringSteps(state);
802 
803  // Update the lowest order process.
804  if (!selected->mother) {
805  int nFinal = 0;
806  for(int i=0; i < int(state.size()); ++i)
807  if ( state[i].isFinal()) nFinal++;
808  if (nFinal <=2)
809  state.scale(mergingHooksPtr->muF());
810 
811  // Save information on last splitting, to allow the next
812  // emission in the shower to have smaller rapidity with
813  // respect to the last ME splitting.
814  // For hard process, use dummy values.
815  if (mergingHooksPtr->getNumberOfClusteringSteps(state) == 0) {
816  infoPtr->zNowISR(0.5);
817  infoPtr->pT2NowISR(pow(state[0].e(),2));
818  infoPtr->hasHistory(true);
819  // For incomplete process, try to use real values.
820  } else {
821  infoPtr->zNowISR(selected->zISR());
822  infoPtr->pT2NowISR(pow(selected->pTISR(),2));
823  infoPtr->hasHistory(true);
824  }
825 
826  // Set QCD 2->2 starting scale different from arbitrary scale in LHEF!
827  // --> Set to minimal mT of partons.
828  int nFinalCol = 0;
829  double muf = state[0].e();
830  for ( int i=0; i < state.size(); ++i )
831  if ( state[i].isFinal()
832  && ( state[i].colType() != 0 || state[i].id() == 22 ) ) {
833  nFinalCol++;
834  muf = min( muf, abs(state[i].mT()) );
835  }
836  // For pure QCD dijet events (only!), set the process scale to the
837  // transverse momentum of the outgoing partons.
838  if ( nSteps == 0 && nFinalCol == 2
839  && ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
840  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0) ) {
841  state.scale(muf);
842  for (int i = 3;i < state.size();++i)
843  state[i].scale(muf);
844  }
845  // For weak inclusive merging, follow QCD 2->2 starting scale for dijet
846  // events. Also, restore input input polarisations.
847  if (nSteps == 0 && nFinalCol == 2 &&
848  mergingHooksPtr->getProcessString().find("inc") != string::npos) {
849  state.scale(muf);
850  for (int i = 3;i < state.size();++i)
851  state[i].scale(muf);
852  for ( int i=0; i < min(state.size(),outState.size()); ++i )
853  state[i].pol(outState[i].pol());
854  }
855 
856  } else {
857 
858  // Save information on last splitting, to allow the next
859  // emission in the shower to have smaller rapidity with
860  // respect to the last ME splitting.
861  infoPtr->zNowISR(selected->zISR());
862  infoPtr->pT2NowISR(pow(selected->pTISR(),2));
863  infoPtr->hasHistory(true);
864 
865  }
866 
867  // Copy the output state.
868  outState = state;
869 
870  // Save MPI starting scale.
871  if (nSteps == 0)
872  mergingHooksPtr->muMI(infoPtr->eCM());
873  else
874  mergingHooksPtr->muMI(outState.scale());
875 
876  // Setup the weak shower if W clustering is enabled.
877  if (mergingHooksPtr->doWeakClustering()) setupWeakShower(0);
878 
879 }
880 
881 //--------------------------------------------------------------------------
882 
883 // Function to print the history that would be chosen from the number RN.
884 
885 void History::printHistory( const double RN ) {
886  History * selected = select(RN);
887  selected->printStates();
888  // Done
889 }
890 
891 //--------------------------------------------------------------------------
892 
893 // Function to print the states in a history, starting from the hard process.
894 
895 void History::printStates() {
896  if ( !mother ) {
897  cout << scientific << setprecision(6) << "Probability=" << prob << endl;
898  state.list();
899  return;
900  }
901 
902  // Print.
903  double p = (mother) ? prob/mother->prob : prob;
904  cout << scientific << setprecision(6) << "Probability=" << p
905  << " scale=" << clusterIn.pT() << endl;
906  state.list();
907  // Recurse
908  mother->printStates();
909  // Done
910  return;
911 }
912 
913 //--------------------------------------------------------------------------
914 
915 // Function to set the state with complete scales for evolution.
916 
917 bool History::getClusteredEvent( const double RN, int nSteps,
918  Event& outState) {
919 
920  // Select history
921  History * selected = select(RN);
922  // Set scales in the states to the scales pythia would have set
923  // (Only needed if not done before in calculation of weights or
924  // setting of starting conditions)
925  selected->setScalesInHistory();
926  // If the history does not allow for nSteps clusterings (e.g. because the
927  // history is incomplete), return false
928  if (nSteps > selected->nClusterings()) return false;
929  // Return event with nSteps-1 additional partons (i.e. recluster the last
930  // splitting) and copy the output state
931  outState = selected->clusteredState(nSteps-1);
932  // Done.
933  return true;
934 
935 }
936 
937 //--------------------------------------------------------------------------
938 
939 bool History::getFirstClusteredEventAboveTMS( const double RN, int nDesired,
940  Event& process, int& nPerformed, bool doUpdate ) {
941 
942  // Do reclustering (looping) steps. Remember process scale.
943  int nTried = nDesired - 1;
944  // Get number of clustering steps.
945  int nSteps = select(RN)->nClusterings();
946  // Set scales in the states to the scales pythia would have set.
947  select(RN)->setScalesInHistory();
948 
949  // Recluster until reclustered event is above the merging scale.
950  Event dummy = Event();
951  do {
952  // Initialise temporary output of reclustering.
953  dummy.clear();
954  dummy.init( "(hard process-modified)", particleDataPtr );
955  dummy.clear();
956  // Recluster once more.
957  nTried++;
958  // If reclustered event does not exist, exit.
959  if ( !getClusteredEvent( RN, nSteps-nTried+1, dummy ) ) return false;
960  if ( nTried >= nSteps ) break;
961 
962  // Continue loop if reclustered event has unresolved partons.
963  } while ( mergingHooksPtr->getNumberOfClusteringSteps(dummy) > 0
964  && mergingHooksPtr->tmsNow( dummy) < mergingHooksPtr->tms() );
965 
966  // Update the hard process.
967  if ( doUpdate ) process = dummy;
968 
969  // Failed to produce output state.
970  if ( nTried > nSteps ) return false;
971 
972  nPerformed = nTried;
973  if ( doUpdate ) {
974  // Update to the actual number of steps.
975  mergingHooksPtr->nReclusterSave = nPerformed;
976  // Save MPI starting scale
977  if (mergingHooksPtr->getNumberOfClusteringSteps(state) == 0)
978  mergingHooksPtr->muMI(infoPtr->eCM());
979  else
980  mergingHooksPtr->muMI(state.scale());
981  }
982 
983  // Done
984  return true;
985 
986 }
987 
988 //--------------------------------------------------------------------------
989 
990 // Calculate and return pdf ratio.
991 
992 double History::getPDFratio( int side, bool forSudakov, bool useHardPDFs,
993  int flavNum, double xNum, double muNum,
994  int flavDen, double xDen, double muDen) {
995 
996  // Do nothing for e+e- beams
997  if ( abs(flavNum) > 10 && flavNum != 21 ) return 1.0;
998  if ( abs(flavDen) > 10 && flavDen != 21 ) return 1.0;
999 
1000  // Now calculate PDF ratio if necessary
1001  double pdfRatio = 1.0;
1002 
1003  // Get mother and daughter pdfs
1004  double pdfNum = 0.0;
1005  double pdfDen = 0.0;
1006 
1007  // Use hard process PDFs (i.e. PDFs NOT used in ISR, FSR or MPI).
1008  if ( useHardPDFs ) {
1009  if (side == 1) {
1010  if (forSudakov)
1011  pdfNum = mother->beamA.xfHard( flavNum, xNum, muNum*muNum);
1012  else
1013  pdfNum = beamA.xfHard( flavNum, xNum, muNum*muNum);
1014  if (forSudakov)
1015  pdfDen = max(1e-10, beamA.xfHard( flavDen, xDen, muDen*muDen));
1016  else
1017  pdfDen = max(1e-10, beamA.xfHard( flavDen, xDen, muDen*muDen));
1018  } else {
1019  if (forSudakov)
1020  pdfNum = mother->beamB.xfHard( flavNum, xNum, muNum*muNum);
1021  else
1022  pdfNum = beamB.xfHard( flavNum, xNum, muNum*muNum);
1023 
1024  if (forSudakov)
1025  pdfDen = max(1e-10,beamB.xfHard( flavDen, xDen, muDen*muDen));
1026  else
1027  pdfDen = max(1e-10,beamB.xfHard( flavDen, xDen, muDen*muDen));
1028  }
1029 
1030  // Use rescaled PDFs in the presence of multiparton interactions
1031  } else {
1032  if (side == 1) {
1033  if (forSudakov)
1034  pdfNum = mother->beamA.xfISR(0, flavNum, xNum, muNum*muNum);
1035  else
1036  pdfNum = beamA.xfISR(0, flavNum, xNum, muNum*muNum);
1037  if (forSudakov)
1038  pdfDen = max(1e-10, beamA.xfISR(0, flavDen, xDen, muDen*muDen));
1039  else
1040  pdfDen = max(1e-10, beamA.xfISR(0, flavDen, xDen, muDen*muDen));
1041 
1042  } else {
1043  if (forSudakov)
1044  pdfNum = mother->beamB.xfISR(0, flavNum, xNum, muNum*muNum);
1045  else
1046  pdfNum = beamB.xfISR(0, flavNum, xNum, muNum*muNum);
1047 
1048  if (forSudakov)
1049  pdfDen = max(1e-10,beamB.xfISR(0, flavDen, xDen, muDen*muDen));
1050  else
1051  pdfDen = max(1e-10,beamB.xfISR(0, flavDen, xDen, muDen*muDen));
1052  }
1053  }
1054 
1055  // Cut out charm threshold.
1056  if ( forSudakov && abs(flavNum) ==4 && abs(flavDen) == 4 && muDen == muNum
1057  && muNum < particleDataPtr->m0(4))
1058  pdfDen = pdfNum = 1.0;
1059 
1060  // Return ratio of pdfs
1061  if ( pdfNum > 1e-15 && pdfDen > 1e-10 ) {
1062  pdfRatio *= pdfNum / pdfDen;
1063  } else if ( pdfNum < pdfDen ) {
1064  pdfRatio = 0.;
1065  } else if ( pdfNum > pdfDen ) {
1066  pdfRatio = 1.;
1067  }
1068 
1069  // Done
1070  return pdfRatio;
1071 
1072 }
1073 
1074 //--------------------------------------------------------------------------
1075 
1076 /*--------------- METHODS USED FOR ONLY ONE PATH OF HISTORY NODES ------- */
1077 
1078 // Function to set all scales in the sequence of states. This is a
1079 // wrapper routine for setScales and setEventScales methods
1080 
1081 void History::setScalesInHistory() {
1082  // Find correct links from n+1 to n states (mother --> child), as
1083  // needed for enforcing ordered scale sequences
1084  vector<int> ident;
1085  findPath(ident);
1086 
1087  // Set production scales in the states to the scales pythia would
1088  // have set and enforce ordering
1089  setScales(ident,true);
1090 
1091  // Set the overall event scales to the scale of the last branching
1092  setEventScales();
1093 
1094 }
1095 
1096 //--------------------------------------------------------------------------
1097 
1098 // Setup function that call the real getWeakProb.
1099 
1100 double History::getWeakProb() {
1101  vector<int> modes, fermionLines;
1102  vector<Vec4> mom;
1103  return getWeakProb(modes, mom, fermionLines);
1104 }
1105 
1106 //--------------------------------------------------------------------------
1107 
1108 // Recursive function that returns the weak probability for the given path.
1109 // mode refers to which ME correction to use, 1 = sChannel, 2 = gluon channel,
1110 // 3 = double quark t-channel, 4 is double quark u-channel.
1111 
1112 double History::getWeakProb(vector<int> &mode, vector<Vec4> &mom,
1113  vector<int> fermionLines) {
1114 
1115  // If at end, return 1.
1116  if (!mother) return 1.;
1117 
1118  // Find the transfer map given the splitting.
1119  map<int,int> stateTransfer;
1120  findStateTransfer(stateTransfer);
1121 
1122  // Setup hard process.
1123  if (mode.empty()) setupWeakHard(mode,fermionLines,mom);
1124 
1125  // Update modes and fermionLines.
1126  vector<int> modeNew = updateWeakModes(mode, stateTransfer);
1127  vector<int> fermionLinesNew = updateWeakFermionLines(fermionLines,
1128  stateTransfer);
1129 
1130  // Get the probability if it is a weak emission.
1131  if (mother->state[clusterIn.emitted].idAbs() == 24 ||
1132  mother->state[clusterIn.emitted].idAbs() == 23)
1133  return getSingleWeakProb(modeNew, mom, fermionLinesNew) *
1134  mother->getWeakProb(modeNew, mom, fermionLinesNew);
1135  else return mother->getWeakProb(modeNew, mom, fermionLinesNew);
1136 }
1137 
1138 //--------------------------------------------------------------------------
1139 
1140 double History::getSingleWeakProb(vector<int> &mode, vector<Vec4> &mom,
1141  vector<int> fermionLines) {
1142 
1143  // Find the correct coupling coefficient.
1144  double weakCoupling = 0.0;
1145  if (mother->state[clusterIn.emitted].idAbs() == 24) {
1146  // No emissions from right handed particles.
1147  if (clusterIn.spinRadBef == 1) return 0.0;
1148  else if (clusterIn.spinRadBef == -1)
1149  weakCoupling = 4.*M_PI/ coupSMPtr->sin2thetaW()
1150  * coupSMPtr->V2CKMid(abs(clusterIn.flavRadBef),
1151  mother->state[clusterIn.emittor].idAbs());
1152  else {
1153  infoPtr->errorMsg("Warning in History::getSingleWeakProb: "
1154  "Spin not properly configurated. Skipping history");
1155  return 0.0;
1156  }
1157  } else if (mother->state[clusterIn.emitted].idAbs() == 23) {
1158  // No emissions from right handed particles.
1159  if (clusterIn.spinRadBef == 1)
1160  weakCoupling = 4.*M_PI*pow2(coupSMPtr->rf( abs(clusterIn.flavRadBef)))
1161  / (coupSMPtr->sin2thetaW() * coupSMPtr->cos2thetaW()) ;
1162  else if (clusterIn.spinRadBef == -1)
1163  weakCoupling = 4.*M_PI*pow2(coupSMPtr->lf( abs(clusterIn.flavRadBef)))
1164  / (coupSMPtr->sin2thetaW() * coupSMPtr->cos2thetaW()) ;
1165  else {
1166  infoPtr->errorMsg("Warning in History::getSingleWeakProb: "
1167  "Spin not properly configurated. Skipping history");
1168  return 0.0;
1169  }
1170  } else {
1171  infoPtr->errorMsg("Warning in History::getSingleWeakProb: "
1172  "Did not emit W/Z. Skipping history.");
1173  return 0.0;
1174  }
1175 
1176  // Find and store kinematics (e.g. z, pT, k1, k3).
1177 
1178  // Store momenta.
1179  Vec4 pRadAft = mother->state[clusterIn.emittor].p();
1180  Vec4 pEmtAft = mother->state[clusterIn.emitted].p();
1181  Vec4 pRecAft = mother->state[clusterIn.recoiler].p();
1182  Vec4 pSum = pRadAft + pEmtAft + pRecAft;
1183  double m2sum = pSum.m2Calc();
1184  double Qsq = (pRadAft + pEmtAft).m2Calc();
1185 
1186  double m2Rad0 = pRadAft.m2Calc();
1187  double m2Emt0 = pEmtAft.m2Calc();
1188  double lambda13 = sqrt( pow2(Qsq - m2Rad0 - m2Emt0 ) - 4. * m2Rad0*m2Emt0 );
1189  double k1 = ( Qsq - lambda13 + (m2Emt0 - m2Rad0 ) ) / ( 2. * Qsq );
1190  double k3 = ( Qsq - lambda13 - (m2Emt0 - m2Rad0 ) ) / ( 2. * Qsq );
1191 
1192  double z = mother->getCurrentZ(clusterIn.emittor, clusterIn.recoiler,
1193  clusterIn.emitted, clusterIn.flavRadBef);
1194  double pT2 = pow2(clusterIn.pTscale);
1195 
1196  double x1 = 2. * pRadAft * pSum / m2sum;
1197  double x2 = 2. * pRecAft * pSum / m2sum;
1198  double x3 = 2. * pEmtAft * pSum / m2sum;
1199 
1200  // Final state clustering.
1201  if ( state[clusterIn.radBef].status() > 0) {
1202  // s-channel
1203  if (mode[clusterIn.emittor] == 1) {
1204  // Calculate variables.
1205  double eCMME = pSum.mCalc();
1206  double r1 = mother->state[clusterIn.emittor].m() / eCMME;
1207  double r2 = mother->state[clusterIn.recoiler].m() / eCMME;
1208  double r3 = mother->state[clusterIn.emitted].m() / eCMME;
1209  double x1s = x1 * x1;
1210  double x2s = x2 * x2;
1211  double r1s = r1 * r1;
1212  double r2s = r2 * r2;
1213  double r3s = r3 * r3;
1214  double prop1 = 1. + r1s - r2s - x1;
1215  double prop2 = 1. + r2s - r1s - x2;
1216  double prop1s = prop1 * prop1;
1217  double prop2s = prop2 * prop2;
1218  double prop12 = prop1 * prop2;
1219 
1220  // Calculate Jacobian.
1221  double jac = 1./(1.-z) * 1./pT2 * (1-x2+r2-r1)*(x3 - k1*(x1+x3))
1222  * (1.-x1+r1-r2) / x3;
1223  return jac * weakCoupling * ((2. * r3s * r3s + 2. * r3s *
1224  (x1 + x2) + x1s + x2s) / prop12 - r3s / prop1s - r3s / prop2s);
1225  }
1226  // t-channel.
1227  else {
1228  // Store momentas needed.
1229  Vec4 p1 = mother->state[clusterIn.emittor].p();
1230  Vec4 p2 = mother->state[clusterIn.recoiler].p();
1231  Vec4 p3 = mother->state[clusterIn.emitted].p();
1232  Vec4 radBef = state[clusterIn.radBef].p();
1233  Vec4 recBef = state[clusterIn.recBef].p();
1234  Vec4 pIn1 = mom[0];
1235  Vec4 pIn2 = mom[1];
1236 
1237  // Check if a swap is needed.
1238  if (fermionLines[2] == clusterIn.emittor);
1239  else if (fermionLines[3] == clusterIn.emittor) swap(pIn1, pIn2);
1240 
1241  // Rescaling of incoming partons p3 and p4.
1242  double scaleFactor2 = (p1 + p2 + p3).m2Calc() / (pIn1 + pIn2).m2Calc();
1243  double scaleFactor = sqrt(scaleFactor2);
1244  pIn1 *= scaleFactor;
1245  pIn2 *= scaleFactor;
1246 
1247  // Longitudinal boost to rest frame of incoming partons of
1248  // hard interaction.
1249  RotBstMatrix rot2to2frame;
1250  rot2to2frame.bstback(pIn1 + pIn2);
1251  pIn1.rotbst(rot2to2frame);
1252  pIn2.rotbst(rot2to2frame);
1253  p1.rotbst(rot2to2frame);
1254  p2.rotbst(rot2to2frame);
1255  p3.rotbst(rot2to2frame);
1256  recBef.rotbst(rot2to2frame);
1257  radBef.rotbst(rot2to2frame);
1258 
1259  // Further boost to rest frame of outgoing state.
1260  RotBstMatrix rot2to3frame;
1261  rot2to3frame.bstback(p1 + p2 + p3);
1262  p1.rotbst(rot2to3frame);
1263  p2.rotbst(rot2to3frame);
1264  p3.rotbst(rot2to3frame);
1265  recBef.rotbst(rot2to3frame);
1266  radBef.rotbst(rot2to3frame);
1267 
1268  // Calculate variables;
1269  double sHat = (pIn1 + pIn2).m2Calc();
1270  double tHat = (radBef - pIn1).m2Calc();
1271  double uHat = (recBef - pIn1).m2Calc();
1272  double localProb = 0;
1273  double Q2 = pT2 / (z*(1.-z));
1274  double jac = 1./(4. * M_PI) * 1./( (1.-z) * z ) * sHat / (sHat - Q2)
1275  * (1. - k1 - k3);
1276 
1277  // Calculate the ME depending on the top of process.
1278  if (mode[clusterIn.emittor] == 2)
1279  localProb = weakShowerMEs.getMEqg2qgZ( pIn1, pIn2, p2, p3, p1)
1280  / weakShowerMEs.getMEqg2qg( sHat, tHat, uHat);
1281  else if (mode[clusterIn.emittor] == 3)
1282  localProb = weakShowerMEs.getMEqq2qqZ( pIn1, pIn2, p3, p2, p1)
1283  / weakShowerMEs.getMEqq2qq( sHat, tHat, uHat, false);
1284  else if (mode[clusterIn.emittor] == 4)
1285  localProb = weakShowerMEs.getMEqq2qqZ( pIn1, pIn2, p3, p2, p1)
1286  / weakShowerMEs.getMEqq2qq( sHat, tHat, uHat, true);
1287  else {
1288  string message="Warning in History::getSingleWeakProb: Wrong";
1289  message+=" mode setup. Setting probability for path to zero.";
1290  infoPtr->errorMsg(message);
1291  }
1292 
1293  // Split matrix element according to propagaters.
1294  localProb *= abs((-p3 + pIn1).m2Calc())
1295  / ((p3 + p1).m2Calc() + abs((-pIn1 + p3).m2Calc()));
1296 
1297  return jac * weakCoupling * localProb;
1298  }
1299  }
1300  // Initial clustering.
1301  else {
1302  // s-channel
1303  if (mode[clusterIn.emittor] == 1) {
1304  Vec4 pIn1 = mother->state[clusterIn.emittor].p();
1305  Vec4 pIn2 = mother->state[clusterIn.recoiler].p();
1306  Vec4 p1 = mother->state[clusterIn.emitted].p();
1307  Vec4 p2 = pIn1 + pIn2 -p1;
1308 
1309  double sH = (pIn1 + pIn2).m2Calc();
1310  double tH = (p1 - pIn1).m2Calc();
1311  double uH = (p1 - pIn2).m2Calc();
1312  double m3s = p1.m2Calc();
1313  double m4s = p2.m2Calc();
1314 
1315  double jac = 1./sH * tH*uH / ( tH * (tH + uH) );
1316  return jac * weakCoupling * ((uH*uH + tH*tH + 2 * sH * (m3s + m4s))
1317  / (uH*tH) - m3s * m4s * (1/(tH*tH) + 1/(uH*uH)));
1318  }
1319  else {
1320 
1321  // Store momenta.
1322  Vec4 pIn1 = mother->state[clusterIn.emittor].p();
1323  Vec4 pIn2 = mother->state[clusterIn.recoiler].p();
1324  Vec4 p3 = mother->state[clusterIn.emitted].p();
1325  Vec4 p1 = mom[2];
1326  Vec4 p2 = mom[3];
1327 
1328  // Check if radiator is from beam one or two.
1329  if (fermionLines[0] == clusterIn.emittor);
1330  else if (fermionLines[1] == clusterIn.emittor)
1331  swap(p1, p2);
1332 
1333 
1334  Vec4 pDaughter, pRecoiler;
1335  int signBeam = (pIn1.pz() > 0.) ? 1 : -1;
1336  double eCM = state[0].e(), phi = 0;
1337 
1338  // Undo the ISR boost.
1339  reverseBoostISR(pIn1, p3, pIn2, pDaughter, pRecoiler, signBeam,
1340  eCM, phi);
1341 
1342  // Scale outgoing vectors to conserve energy / momentum.
1343  //double scaleFactor2 = (pIn1 + pIn2 - p3).m2Calc() / (p1 + p2).m2Calc();
1344  double scaleFactor2 = (pIn1 + pIn2 - p3).m2Calc() / (p1 + p2).m2Calc();
1345  double scaleFactor = sqrt(scaleFactor2);
1346  RotBstMatrix rot2to2frame;
1347  rot2to2frame.bstback(p1 + p2);
1348  p1.rotbst(rot2to2frame);
1349  p2.rotbst(rot2to2frame);
1350  p1 *= scaleFactor;
1351  p2 *= scaleFactor;
1352 
1353  // Find 2 to 2 rest frame for incoming particles.
1354  // This is done before one of the two are made virtual (Q^2 mass).
1355  Vec4 radBef = state[clusterIn.radBef].p();
1356  Vec4 recBef = state[clusterIn.recBef].p();
1357 
1358  RotBstMatrix rot2to2frameInc;
1359  rot2to2frameInc.bstback(radBef + recBef);
1360  radBef.rotbst(rot2to2frameInc);
1361  recBef.rotbst(rot2to2frameInc);
1362  double sHat = (p1 + p2).m2Calc();
1363  double tHat = (p1 - radBef).m2Calc();
1364  double uHat = (p1 - recBef).m2Calc();
1365  double localProb = 0;
1366  p1.rot(0., -phi);
1367  p2.rot(0., -phi);
1368 
1369  // Calculating the Jacobian
1370  double jac = z / (4.*M_PI);
1371 
1372  if (mode[clusterIn.emittor] == 2)
1373  localProb = weakShowerMEs.getMEqg2qgZ(pIn1, pIn2, p2, p3, p1)
1374  / weakShowerMEs.getMEqg2qg(sHat, tHat, uHat);
1375  else if (mode[clusterIn.emittor] == 4)
1376  localProb = weakShowerMEs.getMEqq2qqZ(pIn1, pIn2, p3, p2, p1)
1377  / weakShowerMEs.getMEqq2qq(sHat, tHat, uHat, true);
1378  else if (mode[clusterIn.emittor] == 3)
1379  localProb = weakShowerMEs.getMEqq2qqZ(pIn1, pIn2, p3, p2, p1)
1380  / weakShowerMEs.getMEqq2qq(sHat, tHat, uHat, false);
1381  else {
1382  string message="Warning in History::getSingleWeakProb: Wrong";
1383  message+=" mode setup. Setting probability for path to zero.";
1384  infoPtr->errorMsg(message);
1385  }
1386 
1387  // Split of ME into an ISR part and FSR part.
1388  localProb *= (p3 + p1).m2Calc() / ( (p3 + p1).m2Calc()
1389  + abs((-pIn1 + p3).m2Calc()) );
1390 
1391  return jac * weakCoupling * localProb;
1392  }
1393  }
1394 
1395 }
1396 
1397 //--------------------------------------------------------------------------
1398 
1399 // Check if the weak recoil structure is allowed.
1400 bool History::checkWeakRecoils(map<int,int> &allowedRecoils, bool isFirst) {
1401  if (!mother) return true;
1402 
1403  // Setup if first
1404  if (isFirst) {
1405  // Drell-Yan production.
1406  if (state.size() != 8) {
1407  if (state[3].isQuark() || state[3].isLepton())
1408  allowedRecoils.insert(pair<int,int>(3,4));
1409  if (state[4].isQuark() || state[4].isLepton())
1410  allowedRecoils.insert(pair<int,int>(4,3));
1411 
1412  } else {
1413  if (state[3].isQuark() || state[3].isLepton())
1414  allowedRecoils.insert(pair<int,int>(3,4));
1415  if (state[4].isQuark() || state[4].isLepton())
1416  allowedRecoils.insert(pair<int,int>(4,3));
1417  if (state[5].isQuark() || state[5].isLepton())
1418  allowedRecoils.insert(pair<int,int>(5,6));
1419  if (state[6].isQuark() || state[6].isLepton())
1420  allowedRecoils.insert(pair<int,int>(6,5));
1421  }
1422  }
1423 
1424  // Find the transfer map.
1425  map<int,int> transfer;
1426  findStateTransfer(transfer);
1427 
1428  // Copy the new allowed recoils.
1429  map<int,int> allowedRecoilsNew;
1430  for (map<int,int>::iterator it = allowedRecoils.begin();
1431  it != allowedRecoils.end(); ++it) {
1432 
1433  // Start by considering final state splittings.
1434  if (state[clusterIn.radBef].status() > 0) {
1435  // If the dipole was not connected to current splitting.
1436  if (it->first != clusterIn.radBef &&
1437  it->second != clusterIn.radBef)
1438  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1439  transfer[it->second]));
1440  // If the recoiler is splitted into two.
1441  else if (it->second == clusterIn.radBef) {
1442  // Follow fermion line.
1443  if (state[clusterIn.recBef].isQuark() ||
1444  state[clusterIn.recBef].isLepton()) {
1445  if (mother->state[clusterIn.emittor].isQuark() ||
1446  mother->state[clusterIn.emittor].isLepton())
1447  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1448  clusterIn.emittor));
1449  else
1450  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1451  clusterIn.emitted));
1452  }
1453  // If no fermion line to follow, choose the largest invariant mass.
1454  else {
1455  double mEmittor = (mother->state[clusterIn.emittor].p() +
1456  mother->state[transfer[it->first]].p()).mCalc();
1457  double mEmitted = (mother->state[clusterIn.emitted].p() +
1458  mother->state[transfer[it->first]].p()).mCalc();
1459  if (mEmitted > mEmittor)
1460  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1461  clusterIn.emitted));
1462  else
1463  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1464  clusterIn.emittor));
1465  }
1466  }
1467  // If the radiator is splitted into two.
1468  if (mother->state[clusterIn.emittor].isQuark() ||
1469  mother->state[clusterIn.emittor].isLepton())
1470  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emittor,
1471  transfer[it->second]));
1472  else
1473  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emitted,
1474  transfer[it->second]));
1475  }
1476 
1477  // Look at initial splittings.
1478  else {
1479  // If not involved in the splitting.
1480  if (it->first != clusterIn.radBef &&
1481  it->second != clusterIn.radBef)
1482  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1483  transfer[it->second]));
1484 
1485  // If the recoiler is splitted, always choose the emittor.
1486  else if (it->second == clusterIn.radBef)
1487  allowedRecoilsNew.insert(pair<int,int>(transfer[it->first],
1488  clusterIn.emittor));
1489 
1490  // If the radiator splits into two.
1491  else {
1492  // If the the fermion line continues to be the beam particle.
1493  if (mother->state[clusterIn.emittor].isQuark() ||
1494  mother->state[clusterIn.emittor].isLepton())
1495  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emittor,
1496  clusterIn.recoiler));
1497 
1498  // If the fermion line is emitted, find recoiler in final state.
1499  else
1500  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emittor,
1501  findISRRecoiler()));
1502  }
1503  }
1504  }
1505 
1506  // If a gluon/phton is split into a quark-antiquark pair, add two new
1507  // possible configurations.
1508  if ( (state[clusterIn.radBef].idAbs() == 22 ||
1509  state[clusterIn.radBef].idAbs() == 21) &&
1510  (mother->state[clusterIn.emittor].isQuark() ||
1511  mother->state[clusterIn.emittor].isLepton() ) ) {
1512  // If it is a final splitting, just add the two.
1513  if (state[clusterIn.radBef].status() > 0) {
1514  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emittor,
1515  clusterIn.emitted));
1516  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emitted,
1517  clusterIn.emittor));
1518  }
1519 
1520  // If it is an initial splitting.
1521  else {
1522  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emittor,
1523  clusterIn.recoiler));
1524  allowedRecoilsNew.insert(pair<int,int>(clusterIn.emitted,
1525  findISRRecoiler()));
1526  }
1527  }
1528 
1529  // allowedRecoilsNew is now properly setup, so ready to check
1530  // if recoil works.
1531 
1532  // If weak emission, do the check.
1533  if (mother->state[clusterIn.emitted].idAbs() == 24 ||
1534  mother->state[clusterIn.emitted].idAbs() == 23)
1535  if ( clusterIn.recoiler != allowedRecoilsNew[clusterIn.emittor])
1536  return false;
1537 
1538  // check the mother.
1539  return mother->checkWeakRecoils(allowedRecoilsNew);
1540 
1541 }
1542 
1543 //--------------------------------------------------------------------------
1544 
1545 // Find the recoiler for an ISR scattered weak particle.
1546 // Always use 1 as weak weight, even though the shower uses a slightly
1547 // different value for Z emissions.
1548 int History::findISRRecoiler() {
1549 
1550  int flavRad = mother->state[clusterIn.emitted].id();
1551  Vec4 pRad = mother->state[clusterIn.emitted].p();
1552  double mRad = mother->state[clusterIn.emitted].m();
1553  int iRad = clusterIn.emitted;
1554  int iRec = 0;
1555  double ppMin = 1E20;
1556  for (int i = 0;i < mother->state.size(); ++i) {
1557  if (i == iRad) continue;
1558  if (mother->state[i].isFinal() && mother->state[i].id() == - flavRad) {
1559  double ppNow = mother->state[i].p() * pRad
1560  - mother->state[i].m() - mRad;
1561  if (ppNow < ppMin) {
1562  ppMin = ppNow;
1563  iRec = i;
1564  }
1565  }
1566  }
1567  if (iRec) return iRec;
1568 
1569  // Find nearest recoiler weak-charge-squared-weighted.
1570  for (int i = 0;i < mother->state.size(); ++i) {
1571  if (i == iRad) continue;
1572  if (mother->state[i].isFinal() && mother->state[i].idAbs() < 20) {
1573  double weakCoupNow = 1.;
1574  double ppNow = (mother->state[i].p() * pRad
1575  - mother->state[i].m() - mRad) / weakCoupNow;
1576  if (ppNow < ppMin) {
1577  ppMin = ppNow;
1578  iRec = i;
1579  }
1580  }
1581  }
1582  if (iRec) return iRec;
1583 
1584  // Find nearest recoiler in final state.
1585  for (int i = 0;i < mother->state.size(); ++i) {
1586  if (i == iRad) continue;
1587  if (mother->state[i].isFinal()) {
1588  double ppNow = mother->state[i].p() * pRad
1589  - mother->state[i].m() - mRad;
1590  if (ppNow < ppMin) {
1591  ppMin = ppNow;
1592  iRec = i;
1593  }
1594  }
1595  }
1596  if (iRec) return iRec;
1597 
1598  return 0;
1599 }
1600 
1601 //--------------------------------------------------------------------------
1602 
1603 // Find map between indecies in the current state and the state after
1604 // the splitting.
1605 // NOT IMPLEMENTED FOR MULTIPLE W/Z/GAMMA (NEED TO HAVE A WAY TO IDENTIFY THEM)
1606 void History::findStateTransfer(map<int,int> &transfer) {
1607  // No need to transfer if already at highest multiplicity.
1608  if (!mother) return;
1609  transfer.clear();
1610 
1611  // Directly assign the 3 first particles (system, beam1, beam2);
1612  for(int i = 0;i < 3; ++i)
1613  transfer.insert(pair<int,int>(i,i));
1614 
1615  transfer.insert(pair<int,int>(clusterIn.radBef, clusterIn.emittor));
1616  transfer.insert(pair<int,int>(clusterIn.recBef, clusterIn.recoiler));
1617 
1618  // Handle all particles that are not part of the clustering.
1619  for (int i = 0; i < int(mother->state.size()); ++i) {
1620  if (clusterIn.emitted == i ||
1621  clusterIn.emittor == i ||
1622  clusterIn.recoiler == i)
1623  continue;
1624 
1625  for (int j = 0;j < int(state.size()); ++j) {
1626  if (mother->state[i].id() == state[j].id()
1627  && mother->state[i].colType() == state[j].colType()
1628  && mother->state[i].chargeType() == state[j].chargeType()
1629  && mother->state[i].col() == state[j].col()
1630  && mother->state[i].acol() == state[j].acol()
1631  && mother->state[i].status() == state[j].status()) {
1632  transfer.insert(pair<int,int>(j,i));
1633  break;
1634  }
1635  }
1636  }
1637 }
1638 
1639 //--------------------------------------------------------------------------
1640 
1641 // Function to find the index (in the mother histories) of the
1642 // child history, thus providing a way access the path from both
1643 // initial history (mother == 0) and final history (all children == 0)
1644 // IN vector<int> : The index of each child in the children vector
1645 // of the current history node will be saved in
1646 // this vector
1647 // NO OUTPUT
1648 
1649 void History::findPath(vector<int>& out) {
1650 
1651  // If the initial and final nodes are identical, return
1652  if (!mother && int(children.size()) < 1) return;
1653  // Find the child by checking the children vector for the perfomed
1654  // clustering
1655  int iChild=-1;
1656  if ( mother ) {
1657  int size = int(mother->children.size());
1658  // Loop through children and identify child chosen
1659  for ( int i=0; i < size; ++i) {
1660  if ( mother->children[i]->scale == scale
1661  && mother->children[i]->prob == prob
1662  && equalClustering(mother->children[i]->clusterIn,clusterIn)) {
1663  iChild = i;
1664  break;
1665  }
1666  }
1667  // Save the index of the child in the children vector and recurse
1668  if (iChild >-1)
1669  out.push_back(iChild);
1670  mother->findPath(out);
1671  }
1672 }
1673 
1674 //--------------------------------------------------------------------------
1675 
1676 // Functions to set the parton production scales and enforce
1677 // ordering on the scales of the respective clusterings stored in
1678 // the History node:
1679 // Method will start from lowest multiplicity state and move to
1680 // higher states, setting the production scales the shower would
1681 // have used.
1682 // When arriving at the highest multiplicity, the method will switch
1683 // and go back in direction of lower states to check and enforce
1684 // ordering for unordered histories.
1685 // IN vector<int> : Vector of positions of the chosen child
1686 // in the mother history to allow to move
1687 // in direction initial->final along path
1688 // bool : True: Move in direction low->high
1689 // multiplicity and set production scales
1690 // False: Move in direction high->low
1691 // multiplicity and check and enforce
1692 // ordering
1693 // NO OUTPUT
1694 
1695 void History::setScales( vector<int> index, bool forward) {
1696 
1697  // First, set the scales of the hard process to the kinematial
1698  // limit (=s)
1699  if ( children.empty() && forward ) {
1700  // New "incomplete" configurations showered from mu
1701  if (!mother) {
1702  double scaleNew = 1.;
1703  if (mergingHooksPtr->incompleteScalePrescip()==0) {
1704  scaleNew = mergingHooksPtr->muF();
1705  } else if (mergingHooksPtr->incompleteScalePrescip()==1) {
1706  Vec4 pOut;
1707  pOut.p(0.,0.,0.,0.);
1708  for(int i=0; i<int(state.size()); ++i)
1709  if (state[i].isFinal())
1710  pOut += state[i].p();
1711  scaleNew = pOut.mCalc();
1712  } else if (mergingHooksPtr->incompleteScalePrescip()==2) {
1713  scaleNew = state[0].e();
1714  }
1715 
1716  scaleNew = max( mergingHooksPtr->pTcut(), scaleNew);
1717 
1718  state.scale(scaleNew);
1719  for(int i=3; i < int(state.size());++i)
1720  if (state[i].colType() != 0)
1721  state[i].scale(scaleNew);
1722  } else {
1723  // 2->2 with non-parton particles showered from eCM
1724  state.scale( state[0].e() );
1725  // Count final partons
1726  bool isLEP = ( state[3].isLepton() && state[4].isLepton() );
1727  int nFinal = 0;
1728  int nFinalPartons = 0;
1729  int nFinalPhotons = 0;
1730  for ( int i=0; i < int(state.size()); ++i ) {
1731  if ( state[i].isFinal() ) {
1732  nFinal++;
1733  if ( state[i].colType() != 0 ) nFinalPartons++;
1734  if ( state[i].id() == 22 ) nFinalPhotons++;
1735  }
1736  }
1737  bool isQCD = ( nFinal == 2 && nFinal == nFinalPartons );
1738  bool isPPh = ( nFinal == 2 && nFinalPartons == 1 && nFinalPhotons == 1);
1739  // If 2->2, purely partonic, set event scale to kinematic pT
1740  if ( !isLEP && ( isQCD || isPPh ) ) {
1741  double scaleNew = hardFacScale(state);
1742  state.scale( scaleNew );
1743  }
1744  }
1745  }
1746  // Set all particle production scales, starting from lowest
1747  // multiplicity (final) state
1748  if (mother && forward) {
1749  // When choosing splitting scale, beware of unordered splittings:
1750  double scaleNew = 1.;
1751  if (mergingHooksPtr->unorderedScalePrescip() == 0) {
1752  // Use larger scale as common splitting scale for mother and child
1753  scaleNew = max( mergingHooksPtr->pTcut(), max(scale,mother->scale));
1754  } else if (mergingHooksPtr->unorderedScalePrescip() == 1) {
1755  // Use smaller scale as common splitting scale for mother and child
1756  if (scale < mother->scale)
1757  scaleNew *= max( mergingHooksPtr->pTcut(), min(scale,mother->scale));
1758  else
1759  scaleNew *= max( mergingHooksPtr->pTcut(), max(scale,mother->scale));
1760  }
1761 
1762  // Rescale the mother state partons to the clustering scales
1763  // that have been found along the path
1764  mother->state[clusterIn.emitted].scale(scaleNew);
1765  mother->state[clusterIn.emittor].scale(scaleNew);
1766  mother->state[clusterIn.recoiler].scale(scaleNew);
1767 
1768  // Find unchanged copies of partons in higher multiplicity states
1769  // and rescale those
1770  mother->scaleCopies(clusterIn.emitted, mother->state, scaleNew);
1771  mother->scaleCopies(clusterIn.emittor, mother->state, scaleNew);
1772  mother->scaleCopies(clusterIn.recoiler, mother->state, scaleNew);
1773 
1774  // Recurse
1775  mother->setScales(index,true);
1776  }
1777 
1778  // Now, check and correct ordering from the highest multiplicity
1779  // state backwards to all the clustered states
1780  if (!mother || !forward) {
1781  // Get index of child along the path
1782  int iChild = -1;
1783  if ( int(index.size()) > 0 ) {
1784  iChild = index.back();
1785  index.pop_back();
1786  }
1787 
1788  // Check that the reclustered scale is above the shower cut
1789  if (mother) {
1790  scale = max(mergingHooksPtr->pTcut(), scale);
1791  }
1792  // If this is NOT the 2->2 process, check and enforce ordering
1793  if (iChild != -1 && !children.empty()) {
1794  if (scale > children[iChild]->scale ) {
1795  if (mergingHooksPtr->unorderedScalePrescip() == 0) {
1796  // Use larger scale as common splitting scale for mother and child
1797  double scaleNew = max( mergingHooksPtr->pTcut(),
1798  max(scale,children[iChild]->scale));
1799  // Enforce ordering in particle production scales
1800  for( int i = 0; i < int(children[iChild]->state.size()); ++i)
1801  if (children[iChild]->state[i].scale() == children[iChild]->scale)
1802  children[iChild]->state[i].scale(scaleNew);
1803  // Enforce ordering in saved clustering scale
1804  children[iChild]->scale = scaleNew;
1805 
1806  } else if ( mergingHooksPtr->unorderedScalePrescip() == 1) {
1807  // Use smaller scale as common splitting scale for mother & child
1808  double scaleNew = max(mergingHooksPtr->pTcut(),
1809  min(scale,children[iChild]->scale));
1810  // Enforce ordering in particle production scales
1811  for( int i = 0; i < int(state.size()); ++i)
1812  if (state[i].scale() == scale)
1813  state[i].scale(scaleNew);
1814  // Enforce ordering in saved clustering scale
1815  scale = scaleNew;
1816  }
1817  // Just set the overall event scale to the minimal scale
1818  } else {
1819  double scalemin = state[0].e();
1820  for( int i = 0; i < int(state.size()); ++i)
1821  if (state[i].colType() != 0)
1822  scalemin = max(mergingHooksPtr->pTcut(),
1823  min(scalemin,state[i].scale()));
1824  state.scale(scalemin);
1825  scale = max(mergingHooksPtr->pTcut(), scale);
1826  }
1827  //Recurse
1828  children[iChild]->setScales(index, false);
1829  }
1830  }
1831 
1832 }
1833 
1834 //--------------------------------------------------------------------------
1835 
1836 // Function to find a particle in all higher multiplicity events
1837 // along the history path and set its production scale to the input
1838 // scale
1839 // IN int iPart : Parton in refEvent to be checked / rescaled
1840 // Event& refEvent : Reference event for iPart
1841 // double scale : Scale to be set as production scale for
1842 // unchanged copies of iPart in subsequent steps
1843 
1844 void History::scaleCopies(int iPart, const Event& refEvent, double rho) {
1845 
1846  // Check if any parton recently rescaled is found unchanged:
1847  // Same charge, colours in mother->state
1848  if ( mother ) {
1849  for( int i=0; i < mother->state.size(); ++i) {
1850  if ( ( mother->state[i].id() == refEvent[iPart].id()
1851  && mother->state[i].colType() == refEvent[iPart].colType()
1852  && mother->state[i].chargeType() == refEvent[iPart].chargeType()
1853  && mother->state[i].col() == refEvent[iPart].col()
1854  && mother->state[i].acol() == refEvent[iPart].acol() )
1855  ) {
1856  // Rescale the unchanged parton
1857  mother->state[i].scale(rho);
1858  // Recurse
1859  if (mother->mother)
1860  mother->scaleCopies( iPart, refEvent, rho );
1861  } // end if found unchanged parton case
1862  } // end loop over particle entries in event
1863  }
1864 }
1865 
1866 //--------------------------------------------------------------------------
1867 
1868 // Functions to set the OVERALL EVENT SCALES [=state.scale()] to
1869 // the scale of the last clustering
1870 // NO INPUT
1871 // NO OUTPUT
1872 
1873 void History::setEventScales() {
1874  // Set the event scale to the scale of the last clustering,
1875  // except for the very lowest multiplicity state
1876  if (mother) {
1877  mother->state.scale(scale);
1878  // Recurse
1879  mother->setEventScales();
1880  }
1881 }
1882 
1883 //--------------------------------------------------------------------------
1884 
1885 // Functions to return the z value of the last ISR splitting
1886 // NO INPUT
1887 // OUTPUT double : z value of last ISR splitting in history
1888 
1889 double History::zISR() {
1890 
1891  // Do nothing for ME level state
1892  if (!mother) return 0.0;
1893  // Skip FSR splitting
1894  if (mother->state[clusterIn.emittor].isFinal()) return mother->zISR();
1895  // Calculate z
1896  int rad = clusterIn.emittor;
1897  int rec = clusterIn.recoiler;
1898  int emt = clusterIn.emitted;
1899  double z = (mother->state[rad].p() + mother->state[rec].p()
1900  - mother->state[emt].p()).m2Calc()
1901  / (mother->state[rad].p() + mother->state[rec].p()).m2Calc();
1902  // Recurse
1903  double znew = mother->zISR();
1904  // Update z
1905  if (znew > 0.) z = znew;
1906 
1907  return z;
1908 }
1909 
1910 //--------------------------------------------------------------------------
1911 
1912 // Functions to return the z value of the last FSR splitting
1913 // NO INPUT
1914 // OUTPUT double : z value of last FSR splitting in history
1915 
1916 double History::zFSR() {
1917 
1918  // Do nothing for ME level state
1919  if (!mother) return 0.0;
1920  // Skip ISR splitting
1921  if (!mother->state[clusterIn.emittor].isFinal()) return mother->zFSR();
1922  // Calculate z
1923  int rad = clusterIn.emittor;
1924  int rec = clusterIn.recoiler;
1925  int emt = clusterIn.emitted;
1926  // Construct 2->3 variables for FSR
1927  Vec4 sum = mother->state[rad].p() + mother->state[rec].p()
1928  + mother->state[emt].p();
1929  double m2Dip = sum.m2Calc();
1930  double x1 = 2. * (sum * mother->state[rad].p()) / m2Dip;
1931  double x3 = 2. * (sum * mother->state[emt].p()) / m2Dip;
1932  // Calculate z of splitting for FSR
1933  double z = x1/(x1+x3);
1934  // Recurse
1935  double znew = mother->zFSR();
1936  // Update z
1937  if (znew > 0.) z = znew;
1938 
1939  return z;
1940 }
1941 
1942 //--------------------------------------------------------------------------
1943 
1944 // Functions to return the pT scale of the last FSR splitting
1945 // NO INPUT
1946 // OUTPUT double : pT scale of last FSR splitting in history
1947 
1948 double History::pTISR() {
1949  // Do nothing for ME level state
1950  if (!mother) return 0.0;
1951  // Skip FSR splitting
1952  if (mother->state[clusterIn.emittor].isFinal()) return mother->pTISR();
1953  double pT = mother->state.scale();
1954  // Recurse
1955  double pTnew = mother->pTISR();
1956  // Update pT
1957  if (pTnew > 0.) pT = pTnew;
1958 
1959  return pT;
1960 }
1961 
1962 //--------------------------------------------------------------------------
1963 
1964 // Functions to return the pT scale of the last FSR splitting
1965 // NO INPUT
1966 // OUTPUT double : pT scale of last FSR splitting in history
1967 
1968 double History::pTFSR() {
1969 
1970  // Do nothing for ME level state
1971  if (!mother) return 0.0;
1972  // Skip ISR splitting
1973  if (!mother->state[clusterIn.emittor].isFinal()) return mother->pTFSR();
1974  double pT = mother->state.scale();
1975  // Recurse
1976  double pTnew = mother->pTFSR();
1977  // Update pT
1978  if (pTnew > 0.) pT = pTnew;
1979  return pT;
1980 }
1981 
1982 //--------------------------------------------------------------------------
1983 
1984 // Function to return the depth of the history (i.e. the number of
1985 // reclustered splittings)
1986 // NO INPUT
1987 // OUTPUT int : Depth of history
1988 
1989 int History::nClusterings() {
1990  if (!mother) return 0;
1991  int w = mother->nClusterings();
1992  w += 1;
1993  return w;
1994 }
1995 
1996 //--------------------------------------------------------------------------
1997 
1998 // Functions to return the event after nSteps splittings of the 2->2 process
1999 // Example: nSteps = 1 -> return event with one additional parton
2000 // INPUT int : Number of splittings in the event,
2001 // as counted from core 2->2 process
2002 // OUTPUT Event : event with nSteps additional partons
2003 
2004 Event History::clusteredState(int nSteps) {
2005 
2006  // Save state
2007  Event outState = state;
2008  // As long as there are steps to do, recursively save state
2009  if (mother && nSteps > 0)
2010  outState = mother->clusteredState(nSteps - 1);
2011  // Done
2012  return outState;
2013 
2014 }
2015 
2016 //--------------------------------------------------------------------------
2017 
2018 // Function to choose a path from all paths in the tree
2019 // according to their splitting probabilities
2020 // IN double : Random number
2021 // OUT History* : Leaf of history path chosen
2022 
2023 History * History::select(double rnd) {
2024 
2025  // No need to choose if no paths have been constructed.
2026  if ( goodBranches.empty() && badBranches.empty() ) return this;
2027 
2028  // Choose amongst paths allowed by projections.
2029  double sum = 0.;
2030  map<double, History*> selectFrom;
2031  if ( !goodBranches.empty() ) {
2032  selectFrom = goodBranches;
2033  sum = sumGoodBranches;
2034  } else {
2035  selectFrom = badBranches;
2036  sum = sumBadBranches;
2037  }
2038 
2039  if (mergingHooksPtr->pickBySumPT()) {
2040  // Find index of history with minimal sum of scalar pT
2041  int nFinal = 0;
2042  for (int i=0; i < state.size(); ++i)
2043  if (state[i].isFinal())
2044  nFinal++;
2045  double iMin = 0.;
2046  double sumMin = (nFinal-2)*state[0].e();
2047  for ( map<double, History*>::iterator it = selectFrom.begin();
2048  it != selectFrom.end(); ++it ) {
2049 
2050  if (it->second->sumScalarPT < sumMin) {
2051  sumMin = it->second->sumScalarPT;
2052  iMin = it->first;
2053  }
2054  }
2055  // Choose history with smallest sum of scalar pT
2056  return selectFrom.lower_bound(iMin)->second;
2057  } else {
2058  // Choose history according to probability, be careful about upper bound
2059  if ( rnd != 1. ) {
2060  return selectFrom.upper_bound(sum*rnd)->second;
2061  } else {
2062  return selectFrom.lower_bound(sum*rnd)->second;
2063  }
2064  }
2065  // Done
2066 }
2067 
2068 //--------------------------------------------------------------------------
2069 
2070 // Function to project paths onto desired paths.
2071 
2072 bool History::trimHistories() {
2073  // Do nothing if no paths have been constructed.
2074  if ( paths.empty() ) return false;
2075  // Loop through all constructed paths. Check all removal conditions.
2076  for ( map<double, History*>::iterator it = paths.begin();
2077  it != paths.end(); ++it ) {
2078  // Check if history is allowed.
2079  if ( it->second->keep() && !it->second->keepHistory() )
2080  it->second->remove();
2081  }
2082  // Project onto desired / undesired branches.
2083  double sumold, sumnew, sumprob, mismatch;
2084  sumold = sumnew = sumprob = mismatch = 0.;
2085  // Loop through all constructed paths and store allowed paths.
2086  // Skip undesired paths.
2087  for ( map<double, History*>::iterator it = paths.begin();
2088  it != paths.end(); ++it ) {
2089  // Update index
2090  sumnew = it->first;
2091  if ( it->second->keep() ) {
2092  // Fill branches with allowed paths.
2093  goodBranches.insert( make_pair( sumnew - mismatch, it->second) );
2094  // Add probability of this path.
2095  sumGoodBranches = sumnew - mismatch;
2096  } else {
2097  // Update mismatch in probabilities resulting from not including this
2098  // path
2099  double mismatchOld = mismatch;
2100  mismatch += sumnew - sumold;
2101  // Fill branches with allowed paths.
2102  badBranches.insert( make_pair( mismatchOld + sumnew - sumold,
2103  it->second ) );
2104  // Add probability of this path.
2105  sumBadBranches = mismatchOld + sumnew - sumold;
2106  }
2107  // remember index of this path in order to caclulate probability of
2108  // subsequent path.
2109  sumold = it->first;
2110  }
2111 
2112  // Done
2113  return !goodBranches.empty();
2114 }
2115 
2116 //--------------------------------------------------------------------------
2117 
2118 // Function implementing checks on a paths, deciding if the path is valid.
2119 
2120 bool History::keepHistory() {
2121  bool keepPath = true;
2122 
2123  // Tag unordered paths for removal.
2124  if ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
2125  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
2126  || isQCD2to2(state) ) {
2127  // Tag unordered paths for removal. Include scale of hard 2->2 process
2128  // into the ordering definition.
2129  double maxScale = hardFacScale(state);
2130  return keepPath = isOrderedPath( maxScale );
2131  }
2132 
2133  // Set starting scale to mass of Drell-Yan for 2->1.
2134  if (isEW2to1(state)) {
2135  Vec4 pSum(0,0,0,0);
2136  for (int i = 0;i < state.size(); ++i)
2137  if (state[i].isFinal()) pSum += state[i].p();
2138  return isOrderedPath( pSum.mCalc());
2139  }
2140 
2141  keepPath = isOrderedPath( infoPtr->eCM() );
2142 
2143  // More stringent criterion.
2144  //keepPath = allIntermediateAboveRhoMS( mergingHooksPtr->tms() );
2145 
2146  //Done
2147  return keepPath;
2148 }
2149 
2150 //--------------------------------------------------------------------------
2151 
2152 // Function to check if a path is ordered in evolution pT.
2153 
2154 bool History::isOrderedPath( double maxscale ) {
2155  double newscale = clusterIn.pT();
2156  if ( !mother ) return true;
2157  if ( mother->state[clusterIn.emittor].idAbs() == 21
2158  && mother->state[clusterIn.emitted].idAbs() == 5
2159  && !mother->state[clusterIn.emittor].isFinal())
2160  newscale=maxscale;
2161  bool ordered = mother->isOrderedPath(newscale);
2162  if ( !ordered || maxscale < newscale) return false;
2163  return ordered;
2164 }
2165 
2166 //--------------------------------------------------------------------------
2167 
2168 // Function to check if all reconstucted states in a path pass the merging
2169 // scale cut.
2170 
2171 bool History::allIntermediateAboveRhoMS( double rhoms, bool good ) {
2172  // If one state below the merging scale has already been found, no need to
2173  // check further.
2174  if ( !good ) return false;
2175  // Check merging scale for states with more than 0 jets
2176  int nFinal = 0;
2177  for ( int i = 0; i < state.size(); ++i )
2178  if ( state[i].isFinal() && state[i].colType() != 0 )
2179  nFinal++;
2180  double rhoNew = (nFinal > 0 ) ? mergingHooksPtr->tmsNow( state )
2181  : state[0].e();
2182  // Assume state from ME generator passes merging scale cut.
2183  if ( !mother ) return good;
2184  // Recurse.
2185  return good && mother->allIntermediateAboveRhoMS( rhoms, (rhoNew > rhoms) );
2186 }
2187 
2188 //--------------------------------------------------------------------------
2189 
2190 // Function to check if any ordered paths were found (and kept).
2191 
2192 bool History::foundAnyOrderedPaths() {
2193  //Do nothing if no paths were found
2194  if ( paths.empty() ) return false;
2195  double maxscale = infoPtr->eCM();
2196  // Loop through paths. Divide probability into ordered and unordered pieces.
2197  for ( map<double, History*>::iterator it = paths.begin();
2198  it != paths.end(); ++it )
2199  if ( it->second->isOrderedPath(maxscale) )
2200  return true;
2201  // Done
2202  return false;
2203 }
2204 
2205 //--------------------------------------------------------------------------
2206 
2207 // For a full path, find the weight calculated from the ratio of
2208 // couplings, the no-emission probabilities, and possible PDF
2209 // ratios. This function should only be called for the last history
2210 // node of a full path.
2211 // IN TimeShower : Already initialised shower object to be used as
2212 // trial shower
2213 // double : alpha_s value used in ME calculation
2214 // double : Maximal mass scale of the problem (e.g. E_CM)
2215 // AlphaStrong: Initialised shower alpha_s object for FSR
2216 // alpha_s ratio calculation
2217 // AlphaStrong: Initialised shower alpha_s object for ISR
2218 // alpha_s ratio calculation (can be different from previous)
2219 
2220 double History::weightTree(PartonLevel* trial, double as0, double aem0,
2221  double maxscale, double pdfScale, AlphaStrong * asFSR, AlphaStrong * asISR,
2222  AlphaEM * aemFSR, AlphaEM * aemISR, double& asWeight, double& aemWeight,
2223  double& pdfWeight) {
2224 
2225  // Use correct scale
2226  double newScale = scale;
2227 
2228  // For ME state, just multiply by PDF ratios
2229  if ( !mother ) {
2230 
2231  int sideRad = (state[3].pz() > 0) ? 1 :-1;
2232  int sideRec = (state[4].pz() > 0) ? 1 :-1;
2233 
2234  // Calculate PDF first leg
2235  if (state[3].colType() != 0) {
2236  // Find x value and flavour
2237  double x = 2.*state[3].e() / state[0].e();
2238  int flav = state[3].id();
2239  // Find numerator/denominator scale
2240  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2241  double scaleDen = mergingHooksPtr->muFinME();
2242  // For initial parton, multiply by PDF ratio
2243  double ratio = getPDFratio(sideRad, false, false, flav, x, scaleNum,
2244  flav, x, scaleDen);
2245  pdfWeight *= ratio;
2246  }
2247 
2248  // Calculate PDF ratio for second leg
2249  if (state[4].colType() != 0) {
2250  // Find x value and flavour
2251  double x = 2.*state[4].e() / state[0].e();
2252  int flav = state[4].id();
2253  // Find numerator/denominator scale
2254  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2255  double scaleDen = mergingHooksPtr->muFinME();
2256  // For initial parton, multiply with PDF ratio
2257  double ratio = getPDFratio(sideRec, false, false, flav, x, scaleNum,
2258  flav, x, scaleDen);
2259  pdfWeight *= ratio;
2260  }
2261 
2262  return 1.0;
2263  }
2264 
2265  // Remember new PDF scale n case true scale should be used for un-ordered
2266  // splittings.
2267  double newPDFscale = newScale;
2268  if (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2269  newPDFscale = clusterIn.pT();
2270 
2271  // Recurse
2272  double w = mother->weightTree(trial, as0, aem0, newScale, newPDFscale,
2273  asFSR, asISR, aemFSR, aemISR, asWeight, aemWeight, pdfWeight);
2274 
2275  // Do nothing for empty state
2276  if (state.size() < 3) return 1.0;
2277  // If up to now, trial shower was not successful, return zero
2278  if ( w < 1e-12 ) return 0.0;
2279  // Do trial shower on current state, return zero if not successful
2280  w *= doTrialShower(trial, 1, maxscale);
2281  if ( w < 1e-12 ) return 0.0;
2282 
2283  int emtType = mother->state[clusterIn.emitted].colType();
2284  // Calculate alpha_s ratio for current state.
2285  if ( asFSR && asISR && emtType != 0) {
2286  double asScale = pow2( newScale );
2287  if (mergingHooksPtr->unorderedASscalePrescip() == 1)
2288  asScale = pow2( clusterIn.pT() );
2289 
2290  // Add regularisation scale to initial state alpha_s.
2291  bool FSR = mother->state[clusterIn.emittor].isFinal();
2292  if (!FSR) asScale += pow2(mergingHooksPtr->pT0ISR());
2293 
2294  // Directly get argument of running alpha_s from shower plugin.
2295  if (mergingHooksPtr->useShowerPlugin() )
2296  asScale = getShowerPluginScale(mother->state, clusterIn.emittor,
2297  clusterIn.emitted, clusterIn.recoiler, "scaleAS", asScale);
2298 
2299  double alphaSinPS = (FSR) ? (*asFSR).alphaS(asScale)
2300  : (*asISR).alphaS(asScale);
2301  asWeight *= alphaSinPS / as0;
2302  }
2303 
2304  // Calculate alpha_em ratio for current state.
2305  if ( aemFSR && aemISR && emtType == 0 ) {
2306  double aemScale = pow2( newScale );
2307  if (mergingHooksPtr->unorderedASscalePrescip() == 1)
2308  aemScale = pow2( clusterIn.pT() );
2309 
2310  // Add regularisation scale to initial state alpha_s.
2311  bool FSR = mother->state[clusterIn.emittor].isFinal();
2312  if (!FSR) aemScale += pow2(mergingHooksPtr->pT0ISR());
2313 
2314  // Directly get argument of running alpha_em from shower plugin.
2315  if (mergingHooksPtr->useShowerPlugin() )
2316  aemScale = getShowerPluginScale(mother->state, clusterIn.emittor,
2317  clusterIn.emitted, clusterIn.recoiler, "scaleEM", aemScale);
2318 
2319  double alphaEMinPS = (FSR) ? (*aemFSR).alphaEM(aemScale)
2320  : (*aemISR).alphaEM(aemScale);
2321  aemWeight *= alphaEMinPS / aem0;
2322  }
2323 
2324  // Calculate pdf ratios: Get both sides of event
2325  int inP = 3;
2326  int inM = 4;
2327  int sideP = (mother->state[inP].pz() > 0) ? 1 :-1;
2328  int sideM = (mother->state[inM].pz() > 0) ? 1 :-1;
2329 
2330  if ( mother->state[inP].colType() != 0 ) {
2331  // Find x value and flavour
2332  double x = getCurrentX(sideP);
2333  int flav = getCurrentFlav(sideP);
2334  // Find numerator scale
2335  double scaleNum = (children.empty())
2336  ? hardFacScale(state)
2337  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2338  ? pdfScale : maxscale );
2339  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2340  ? clusterIn.pT() : newScale;
2341  // Multiply PDF ratio
2342  double ratio = getPDFratio(sideP, false, false, flav, x, scaleNum,
2343  flav, x, scaleDen);
2344  pdfWeight *= ratio;
2345  }
2346 
2347  if ( mother->state[inM].colType() != 0 ) {
2348  // Find x value and flavour
2349  double x = getCurrentX(sideM);
2350  int flav = getCurrentFlav(sideM);
2351  // Find numerator scale
2352  double scaleNum = (children.empty())
2353  ? hardFacScale(state)
2354  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2355  ? pdfScale : maxscale );
2356  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2357  ? clusterIn.pT() : newScale;
2358  // Multiply PDF ratio
2359  double ratio = getPDFratio(sideM, false, false, flav, x, scaleNum,
2360  flav, x, scaleDen);
2361  pdfWeight *= ratio;
2362  }
2363 
2364  // Done
2365  return w;
2366 }
2367 
2368 //--------------------------------------------------------------------------
2369 
2370 // Function to return the \alpha_s-ratio part of the CKKWL weight of a path.
2371 
2372 double History::weightTreeALPHAS( double as0, AlphaStrong * asFSR,
2373  AlphaStrong * asISR, int njetMax ) {
2374 
2375  // For ME state, do nothing.
2376  if ( !mother ) return 1.;
2377  // Recurse
2378  double w = mother->weightTreeALPHAS( as0, asFSR, asISR, njetMax );
2379  // Do nothing for empty state
2380  if (state.size() < 3) return w;
2381 
2382  // If this node has too many jets, no not calculate no-emission probability.
2383  int njetNow = mergingHooksPtr->getNumberOfClusteringSteps( state) ;
2384  if (njetNow >= njetMax) return 1.0;
2385 
2386  // Store variables for easy use.
2387  bool FSR = mother->state[clusterIn.emittor].isFinal();
2388  int emtID = mother->state[clusterIn.emitted].id();
2389 
2390  // Do not correct alphaS if it is an EW emission.
2391  if (abs(emtID) == 22 || abs(emtID) == 23 || abs(emtID) == 24) return w;
2392 
2393  // Calculate alpha_s ratio for current state
2394  if ( asFSR && asISR ) {
2395  double asScale = pow2( scale );
2396  if (mergingHooksPtr->unorderedASscalePrescip() == 1)
2397  asScale = pow2( clusterIn.pT() );
2398 
2399  // Add regularisation scale to initial state alpha_s.
2400  if (!FSR) asScale += pow2(mergingHooksPtr->pT0ISR());
2401 
2402  // Directly get argument of running alpha_s from shower plugin.
2403  if (mergingHooksPtr->useShowerPlugin() )
2404  asScale = getShowerPluginScale(mother->state, clusterIn.emittor,
2405  clusterIn.emitted, clusterIn.recoiler, "scaleAS", asScale);
2406 
2407  double alphaSinPS = (FSR) ? (*asFSR).alphaS(asScale)
2408  : (*asISR).alphaS(asScale);
2409  w *= alphaSinPS / as0;
2410  }
2411 
2412  // Done
2413  return w;
2414 }
2415 
2416 //--------------------------------------------------------------------------
2417 
2418 // Function to return the \alpha_em-ratio part of the CKKWL weight of a path.
2419 
2420 double History::weightTreeALPHAEM( double aem0, AlphaEM * aemFSR,
2421  AlphaEM * aemISR, int njetMax ) {
2422 
2423  // For ME state, do nothing.
2424  if ( !mother ) return 1.;
2425  // Recurse
2426  double w = mother->weightTreeALPHAEM( aem0, aemFSR, aemISR, njetMax );
2427  // Do nothing for empty state
2428  if (state.size() < 3) return w;
2429 
2430  // If this node has too many jets, no not calculate no-emission probability.
2431  int njetNow = mergingHooksPtr->getNumberOfClusteringSteps( state) ;
2432  if (njetNow >= njetMax) return 1.0;
2433 
2434  // Store variables for easy use.
2435  bool FSR = mother->state[clusterIn.emittor].isFinal();
2436  int emtID = mother->state[clusterIn.emitted].id();
2437 
2438  // Do not correct alpha EM if it not an EW emission.
2439  if (!(abs(emtID) == 22 || abs(emtID) == 23 || abs(emtID) == 24)) return w;
2440 
2441  // Calculate alpha_s ratio for current state
2442  if ( aemFSR && aemISR ) {
2443  double aemScale = pow2( scale );
2444  if (mergingHooksPtr->unorderedASscalePrescip() == 1)
2445  aemScale = pow2( clusterIn.pT() );
2446 
2447  // Add regularisation scale to initial state alpha_em.
2448  if (!FSR) aemScale += pow2(mergingHooksPtr->pT0ISR());
2449 
2450  // Directly get argument of running alpha_em from shower plugin.
2451  if (mergingHooksPtr->useShowerPlugin() )
2452  aemScale = getShowerPluginScale(mother->state, clusterIn.emittor,
2453  clusterIn.emitted, clusterIn.recoiler, "scaleEM", aemScale);
2454 
2455  double alphaEMinPS = (FSR) ? (*aemFSR).alphaEM(aemScale)
2456  : (*aemISR).alphaEM(aemScale);
2457  w *= alphaEMinPS / aem0;
2458  }
2459 
2460  // Done
2461  return w;
2462 }
2463 
2464 //--------------------------------------------------------------------------
2465 
2466 // Function to return the PDF-ratio part of the CKKWL weight of a path.
2467 
2468 double History::weightTreePDFs( double maxscale, double pdfScale,
2469  int njetMax ) {
2470 
2471  // Use correct scale
2472  double newScale = scale;
2473 
2474  // For ME state, just multiply by PDF ratios
2475  if ( !mother ) {
2476 
2477  // If this node has too many jets, no not calculate PDF ratio.
2478  int njet = mergingHooksPtr->getNumberOfClusteringSteps( state);
2479  if (njet > njetMax) return 1.0;
2480 
2481  double wt = 1.;
2482  int sideRad = (state[3].pz() > 0) ? 1 :-1;
2483  int sideRec = (state[4].pz() > 0) ? 1 :-1;
2484 
2485  // Calculate PDF first leg
2486  if (state[3].colType() != 0) {
2487  // Find x value and flavour
2488  double x = 2.*state[3].e() / state[0].e();
2489  int flav = state[3].id();
2490  // Find numerator/denominator scale
2491  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2492  double scaleDen = mergingHooksPtr->muFinME();
2493  // For initial parton, multiply by PDF ratio
2494  wt *= getPDFratio(sideRad, false, false, flav, x, scaleNum, flav, x,
2495  scaleDen);
2496  }
2497 
2498  // Calculate PDF ratio for second leg
2499  if (state[4].colType() != 0) {
2500  // Find x value and flavour
2501  double x = 2.*state[4].e() / state[0].e();
2502  int flav = state[4].id();
2503  // Find numerator/denominator scale
2504  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2505  double scaleDen = mergingHooksPtr->muFinME();
2506  // For initial parton, multiply with PDF ratio
2507  wt *= getPDFratio(sideRec, false, false, flav, x, scaleNum, flav, x,
2508  scaleDen);
2509  }
2510 
2511  return wt;
2512  }
2513 
2514  // Remember new PDF scale n case true scale should be used for un-ordered
2515  // splittings.
2516  double newPDFscale = newScale;
2517  if ( mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2518  newPDFscale = clusterIn.pT();
2519 
2520  // Recurse
2521  double w = mother->weightTreePDFs( newScale, newPDFscale, njetMax );
2522 
2523  // Do nothing for empty state
2524  if (state.size() < 3) return w;
2525 
2526  // If this node has too many jets, no not calculate PDF ratio.
2527  int njetNow = mergingHooksPtr->getNumberOfClusteringSteps( state) ;
2528 
2529  // Calculate pdf ratios: Get both sides of event
2530  int inP = 3;
2531  int inM = 4;
2532  int sideP = (mother->state[inP].pz() > 0) ? 1 :-1;
2533  int sideM = (mother->state[inM].pz() > 0) ? 1 :-1;
2534 
2535  if ( mother->state[inP].colType() != 0 ) {
2536  // Find x value and flavour
2537  double x = getCurrentX(sideP);
2538  int flav = getCurrentFlav(sideP);
2539  // Find numerator scale
2540  double scaleNum = (children.empty())
2541  ? hardFacScale(state)
2542  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2543  ? pdfScale : maxscale );
2544  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2545  ? clusterIn.pT() : newScale;
2546 
2547  double xDen = (njetNow == njetMax) ? mother->getCurrentX(sideP) : x;
2548  int flavDen = (njetNow == njetMax) ? mother->getCurrentFlav(sideP) : flav;
2549  double sDen = (njetNow == njetMax) ? mergingHooksPtr->muFinME() : scaleDen;
2550  double ratio = getPDFratio(sideP, false, false, flav, x, scaleNum,
2551  flavDen, xDen, sDen);
2552  w *= ratio;
2553 
2554  }
2555 
2556  if ( mother->state[inM].colType() != 0 ) {
2557  // Find x value and flavour
2558  double x = getCurrentX(sideM);
2559  int flav = getCurrentFlav(sideM);
2560  // Find numerator scale
2561  double scaleNum = (children.empty())
2562  ? hardFacScale(state)
2563  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2564  ? pdfScale : maxscale );
2565  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2566  ? clusterIn.pT() : newScale;
2567 
2568  double xDen = (njetNow == njetMax) ? mother->getCurrentX(sideM) : x;
2569  int flavDen = (njetNow == njetMax) ? mother->getCurrentFlav(sideM) : flav;
2570  double sDen = (njetNow == njetMax) ? mergingHooksPtr->muFinME() : scaleDen;
2571  double ratio = getPDFratio(sideM, false, false, flav, x, scaleNum,
2572  flavDen, xDen, sDen);
2573  w *= ratio;
2574  }
2575 
2576  // Done
2577  return w;
2578 }
2579 
2580 //--------------------------------------------------------------------------
2581 
2582 // Function to return the no-emission probability part of the CKKWL weight.
2583 
2584 double History::weightTreeEmissions( PartonLevel* trial, int type,
2585  int njetMin, int njetMax, double maxscale ) {
2586 
2587  // Use correct scale
2588  double newScale = scale;
2589  // For ME state, just multiply by PDF ratios
2590 
2591  if ( !mother ) return 1.0;
2592  // Recurse
2593  double w = mother->weightTreeEmissions(trial,type,njetMin,njetMax,newScale);
2594  // Do nothing for empty state
2595  if (state.size() < 3) return 1.0;
2596  // If up to now, trial shower was not successful, return zero
2597  if ( w < 1e-12 ) return 0.0;
2598  // If this node has too many jets, no not calculate no-emission probability.
2599  int njetNow = mergingHooksPtr->getNumberOfClusteringSteps( state) ;
2600  if (njetNow >= njetMax) return 1.0;
2601  if (njetNow < njetMin ) w *= 1.0;
2602  // Do trial shower on current state, return zero if not successful
2603  else w *= doTrialShower(trial, type, maxscale);
2604 
2605  if ( w < 1e-12 ) return 0.0;
2606  // Done
2607  return w;
2608 
2609 }
2610 
2611 //--------------------------------------------------------------------------
2612 
2613 // Function to generate the O(\alpha_s)-term of the CKKWL-weight.
2614 
2615 double History::weightFirst(PartonLevel* trial, double as0, double muR,
2616  double maxscale, AlphaStrong * asFSR, AlphaStrong * asISR, Rndm* rndmPtr ) {
2617 
2618  // Use correct scale
2619  double newScale = scale;
2620 
2621  if ( !mother ) {
2622 
2623  double weight = 0.;
2624 
2625  // Calculate PDF first leg
2626  if (state[3].colType() != 0) {
2627  // Find x value and flavour
2628  double x = 2.*state[3].e() / state[0].e();
2629  int flav = state[3].id();
2630  // Find numerator/denominator scale
2631  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2632  double scaleDen = mergingHooksPtr->muFinME();
2633  // Monte Carlo integrand.
2634  double intPDF4 = monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2635  mergingHooksPtr->muFinME(), as0, rndmPtr);
2636  weight += intPDF4;
2637  }
2638 
2639  // Calculate PDF ratio for second leg
2640  if (state[4].colType() != 0) {
2641  // Find x value and flavour
2642  double x = 2.*state[4].e() / state[0].e();
2643  int flav = state[4].id();
2644  // Find numerator/denominator scale
2645  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2646  double scaleDen = mergingHooksPtr->muFinME();
2647  // Monte Carlo integrand.
2648  double intPDF4 = monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2649  mergingHooksPtr->muFinME(), as0, rndmPtr);
2650  weight += intPDF4;
2651  }
2652 
2653  return weight;
2654  }
2655 
2656  // Recurse
2657  double w = mother->weightFirst(trial, as0, muR, newScale, asFSR, asISR,
2658  rndmPtr );
2659 
2660  // Do nothing for empty state
2661  if (state.size() < 3) return 0.0;
2662 
2663  // Find right scale
2664  double b = 1.;
2665  double asScale2 = newScale*newScale;
2666  int showerType = (mother->state[clusterIn.emittor].isFinal() ) ? 1 : -1;
2667  if (showerType == -1) {
2668  asScale2 += pow(mergingHooksPtr->pT0ISR(),2);
2669  b = 1.;
2670  }
2671 
2672  // Directly get argument of running alpha_s from shower plugin.
2673  if (mergingHooksPtr->useShowerPlugin() ){
2674  asScale2 = getShowerPluginScale(mother->state, clusterIn.emittor,
2675  clusterIn.emitted, clusterIn.recoiler, "scaleAS", asScale2);
2676  b = 1.;
2677  }
2678 
2679  // Find summand beta_0 / 2 * ln(muR^2/t_i) due to as expansion.
2680  double NF = 4.;
2681  double BETA0 = 11. - 2./3.* NF;
2682  // For fixed \alpha_s in matrix element
2683  w += as0 / (2.*M_PI) * 0.5 * BETA0 * log( (muR*muR) / (b*asScale2) );
2684 
2685  // Count emissions: New variant
2686  // Generate true average, not only one-point.
2687  bool fixpdf = true;
2688  bool fixas = true;
2689  double nWeight1 = 0.;
2690  double nWeight2 = 0.;
2691 
2692  for(int i=0; i < NTRIAL; ++i) {
2693  // Get number of emissions
2694  vector<double> unresolvedEmissionTerm = countEmissions(trial, maxscale,
2695  newScale, 2, as0, asFSR, asISR, 3, fixpdf, fixas);
2696  nWeight1 += unresolvedEmissionTerm[1];
2697  }
2698  w += nWeight1/double(NTRIAL) + nWeight2/double(NTRIAL);
2699 
2700  // Calculate pdf ratios: Get both sides of event
2701  int inP = 3;
2702  int inM = 4;
2703  int sideP = (mother->state[inP].pz() > 0) ? 1 :-1;
2704  int sideM = (mother->state[inM].pz() > 0) ? 1 :-1;
2705 
2706  if ( mother->state[inP].colType() != 0 ) {
2707  // Find x value and flavour
2708  double x = getCurrentX(sideP);
2709  int flav = getCurrentFlav(sideP);
2710  // Find numerator scale
2711  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2712  // Monte Carlo integrand.
2713  double intPDF4 = monteCarloPDFratios(flav, x, scaleNum, newScale,
2714  mergingHooksPtr->muFinME(), as0, rndmPtr);
2715  w += intPDF4;
2716 
2717  }
2718 
2719  if ( mother->state[inM].colType() != 0 ) {
2720  // Find x value and flavour
2721  double x = getCurrentX(sideM);
2722  int flav = getCurrentFlav(sideM);
2723  // Find numerator scale
2724  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2725  // Monte Carlo integrand.
2726  double intPDF4 = monteCarloPDFratios(flav, x, scaleNum, newScale,
2727  mergingHooksPtr->muFinME(), as0, rndmPtr);
2728  w += intPDF4;
2729 
2730  }
2731 
2732  // Done
2733  return w;
2734 
2735 }
2736 
2737 //--------------------------------------------------------------------------
2738 
2739 // Function to generate the O(\alpha_s)-term of the \alpha_s-ratios
2740 // appearing in the CKKWL-weight.
2741 
2742 double History::weightFirstALPHAS( double as0, double muR,
2743  AlphaStrong * asFSR, AlphaStrong * asISR ) {
2744 
2745  // Use correct scale
2746  double newScale = scale;
2747  // Done
2748  if ( !mother ) return 0.;
2749  // Recurse
2750  double w = mother->weightFirstALPHAS( as0, muR, asFSR, asISR );
2751  // Find right scale
2752  int showerType = (mother->state[clusterIn.emittor].isFinal() ) ? 1 : -1;
2753  double b = 1.;
2754  double asScale = pow2( newScale );
2755  if ( mergingHooksPtr->unorderedASscalePrescip() == 1 )
2756  asScale = pow2( clusterIn.pT() );
2757  if (showerType == -1) {
2758  asScale += pow2( mergingHooksPtr->pT0ISR() );
2759  b = 1.;
2760  }
2761 
2762  // Directly get argument of running alpha_s from shower plugin.
2763  if (mergingHooksPtr->useShowerPlugin() ) {
2764  asScale = getShowerPluginScale(mother->state, clusterIn.emittor,
2765  clusterIn.emitted, clusterIn.recoiler, "scaleAS", asScale);
2766  b = 1.;
2767  }
2768 
2769  // Find summand beta_0 / 2 * ln(muR^2/t_i) due to as expansion.
2770  double NF = 4.;
2771  double BETA0 = 11. - 2./3.* NF;
2772  // For fixed \alpha_s in matrix element
2773  w += as0 / (2.*M_PI) * 0.5 * BETA0 * log( (muR*muR) / (b*asScale) );
2774 
2775  // Done
2776  return w;
2777 
2778 }
2779 
2780 //--------------------------------------------------------------------------
2781 
2782 // Function to generate the O(\alpha_s)-term of the PDF-ratios
2783 // appearing in the CKKWL-weight.
2784 
2785 double History::weightFirstPDFs( double as0, double maxscale, double pdfScale,
2786  Rndm* rndmPtr ) {
2787 
2788  // Use correct scale
2789  double newScale = scale;
2790 
2791  if ( !mother ) {
2792 
2793  double wt = 0.;
2794 
2795  // Calculate PDF first leg
2796  if (state[3].colType() != 0) {
2797  // Find x value and flavour
2798  double x = 2.*state[3].e() / state[0].e();
2799  int flav = state[3].id();
2800  // Find numerator/denominator scale
2801  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2802  double scaleDen = mergingHooksPtr->muFinME();
2803  // Monte Carlo integrand.
2804  wt += monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2805  mergingHooksPtr->muFinME(), as0, rndmPtr);
2806  }
2807  // Calculate PDF ratio for second leg
2808  if (state[4].colType() != 0) {
2809  // Find x value and flavour
2810  double x = 2.*state[4].e() / state[0].e();
2811  int flav = state[4].id();
2812  // Find numerator/denominator scale
2813  double scaleNum = (children.empty()) ? hardFacScale(state) : maxscale;
2814  double scaleDen = mergingHooksPtr->muFinME();
2815  // Monte Carlo integrand.
2816  wt += monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2817  mergingHooksPtr->muFinME(), as0, rndmPtr);
2818  }
2819 
2820  // Done
2821  return wt;
2822  }
2823 
2824  // Remember new PDF scale n case true scale should be used for un-ordered
2825  // splittings.
2826  double newPDFscale = newScale;
2827  if (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2828  newPDFscale = clusterIn.pT();
2829 
2830  // Recurse
2831  double w = mother->weightFirstPDFs( as0, newScale, newPDFscale, rndmPtr);
2832 
2833  // Calculate pdf ratios: Get both sides of event
2834  int inP = 3;
2835  int inM = 4;
2836  int sideP = (mother->state[inP].pz() > 0) ? 1 :-1;
2837  int sideM = (mother->state[inM].pz() > 0) ? 1 :-1;
2838 
2839  if ( mother->state[inP].colType() != 0 ) {
2840  // Find x value and flavour
2841  double x = getCurrentX(sideP);
2842  int flav = getCurrentFlav(sideP);
2843  // Find numerator / denominator scales
2844  double scaleNum = (children.empty())
2845  ? hardFacScale(state)
2846  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2847  ? pdfScale : maxscale );
2848  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2849  ? clusterIn.pT() : newScale;
2850  // Monte Carlo integrand.
2851  w += monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2852  mergingHooksPtr->muFinME(), as0, rndmPtr);
2853  }
2854 
2855  if ( mother->state[inM].colType() != 0 ) {
2856  // Find x value and flavour
2857  double x = getCurrentX(sideM);
2858  int flav = getCurrentFlav(sideM);
2859  // Find numerator / denominator scales
2860  double scaleNum = (children.empty())
2861  ? hardFacScale(state)
2862  : ( (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2863  ? pdfScale : maxscale );
2864  double scaleDen = (mergingHooksPtr->unorderedPDFscalePrescip() == 1)
2865  ? clusterIn.pT() : newScale;
2866  // Monte Carlo integrand.
2867  w += monteCarloPDFratios(flav, x, scaleNum, scaleDen,
2868  mergingHooksPtr->muFinME(), as0, rndmPtr);
2869  }
2870 
2871  // Done
2872  return w;
2873 
2874 }
2875 
2876 
2877 //--------------------------------------------------------------------------
2878 
2879 // Function to generate the O(\alpha_s)-term of the no-emission
2880 // probabilities appearing in the CKKWL-weight.
2881 
2882 double History::weightFirstEmissions(PartonLevel* trial, double as0,
2883  double maxscale, AlphaStrong * asFSR, AlphaStrong * asISR,
2884  bool fixpdf, bool fixas ) {
2885 
2886  // Use correct scale
2887  double newScale = scale;
2888  if ( !mother ) return 0.0;
2889  // Recurse
2890  double w = mother->weightFirstEmissions(trial, as0, newScale, asFSR, asISR,
2891  fixpdf, fixas );
2892  // Do nothing for empty state
2893  if (state.size() < 3) return 0.0;
2894  // Generate true average.
2895  double nWeight1 = 0.;
2896  double nWeight2 = 0.;
2897  for(int i=0; i < NTRIAL; ++i) {
2898  // Get number of emissions
2899  vector<double> unresolvedEmissionTerm = countEmissions(trial, maxscale,
2900  newScale, 2, as0, asFSR, asISR, 3, fixpdf, fixas);
2901  nWeight1 += unresolvedEmissionTerm[1];
2902  }
2903 
2904  w += nWeight1/double(NTRIAL) + nWeight2/double(NTRIAL);
2905 
2906  // Done
2907  return w;
2908 
2909 }
2910 
2911 //--------------------------------------------------------------------------
2912 
2913 // Function to return the factorisation scale of the hard process in Pythia.
2914 
2915 double History::hardFacScale(const Event& event) {
2916  // Declare output scale.
2917  double hardscale = 0.;
2918  // If scale should not be reset, done.
2919  if ( !mergingHooksPtr->resetHardQFac() ) return mergingHooksPtr->muF();
2920  // For pure QCD dijet events, calculate the hadronic cross section
2921  // of the hard process at the pT of the dijet system, rather than at fixed
2922  // arbitrary scale.
2923  if ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
2924  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
2925  || isQCD2to2(event)) {
2926  // Find the mT in the hard sub-process.
2927  vector <double> mT;
2928  for ( int i=0; i < event.size(); ++i)
2929  if ( event[i].isFinal() && event[i].colType() != 0 )
2930  mT.push_back( abs(event[i].mT2()) );
2931  if ( int(mT.size()) != 2 )
2932  hardscale = infoPtr->QFac();
2933  else
2934  hardscale = sqrt( min( mT[0], mT[1] ) );
2935  } else {
2936  hardscale = mergingHooksPtr->muF();
2937  }
2938  // Done
2939  return hardscale;
2940 }
2941 
2942 //--------------------------------------------------------------------------
2943 
2944 // Function to return the factorisation scale of the hard process in Pythia.
2945 
2946 double History::hardRenScale(const Event& event) {
2947  // Declare output scale.
2948  double hardscale = 0.;
2949  // If scale should not be reset, done.
2950  if ( !mergingHooksPtr->resetHardQRen() ) return mergingHooksPtr->muR();
2951  // For pure QCD dijet events, calculate the hadronic cross section
2952  // of the hard process at the pT of the dijet system, rather than at fixed
2953  // arbitrary scale.
2954  if ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
2955  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
2956  || isQCD2to2(event)) {
2957  // Find the mT in the hard sub-process.
2958  vector <double> mT;
2959  for ( int i=0; i < event.size(); ++i)
2960  if ( event[i].isFinal()
2961  && ( event[i].colType() != 0 || event[i].id() == 22 ) )
2962  mT.push_back( abs(event[i].mT()) );
2963  if ( int(mT.size()) != 2 )
2964  hardscale = infoPtr->QRen();
2965  else
2966  hardscale = sqrt( mT[0]*mT[1] );
2967  } else {
2968  hardscale = mergingHooksPtr->muR();
2969  }
2970  // Done
2971  return hardscale;
2972 }
2973 
2974 //--------------------------------------------------------------------------
2975 
2976 // Perform a trial shower using the \a pythia object between
2977 // maxscale down to this scale and return the corresponding Sudakov
2978 // form factor.
2979 // IN trialShower : Shower object used as trial shower
2980 // double : Maximum scale for trial shower branching
2981 // OUT 0.0 : trial shower emission outside allowed pT range
2982 // 1.0 : trial shower successful (any emission was below
2983 // the minimal scale )
2984 
2985 double History::doTrialShower( PartonLevel* trial, int type,
2986  double maxscaleIn, double minscaleIn ) {
2987 
2988  // Copy state to local process
2989  Event process = state;
2990  // Set starting scale.
2991  double startingScale = maxscaleIn;
2992  // Careful when setting shower starting scale for pure QCD and prompt
2993  // photon case.
2994  if ( mergingHooksPtr->getNumberOfClusteringSteps(process) == 0
2995  && ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
2996  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
2997  || isQCD2to2(state) ) )
2998  startingScale = min( startingScale, hardFacScale(process) );
2999 
3000  // Set output.
3001  bool doVeto = false;
3002  double wt = 1.;
3003  bool canEnhanceTrial = (trial->userHooksPtr!=0)
3004  && trial->userHooksPtr->canEnhanceTrial();
3005 
3006  while ( true ) {
3007 
3008  // Reset trialShower object
3009  trial->resetTrial();
3010  // Construct event to be showered
3011  Event event = Event();
3012  event.init("(hard process-modified)", particleDataPtr);
3013  event.clear();
3014 
3015  // Reset process scale so that shower starting scale is correctly set.
3016  process.scale(startingScale);
3017  doVeto = false;
3018 
3019  // Get pT before reclustering
3020  double minScale = (minscaleIn > 0.) ? minscaleIn : scale;
3021 
3022  // If the maximal scale and the minimal scale coincide (as would
3023  // be the case for the corrected scales of unordered histories),
3024  // do not generate Sudakov
3025  if (minScale >= startingScale) break;
3026 
3027  // Find z and pT values at which the current state was formed, to
3028  // ensure that the showers can order the next emission correctly in
3029  // rapidity, if required.
3030  // NOT CORRECTLY SET FOR HIGHEST MULTIPLICITY STATE!
3031  double z = ( mergingHooksPtr->getNumberOfClusteringSteps(state) == 0
3032  || !mother )
3033  ? 0.5
3034  : mother->getCurrentZ(clusterIn.emittor,clusterIn.recoiler,
3035  clusterIn.emitted, clusterIn.flavRadBef);
3036  // Store z and pT values at which the current state was formed.
3037  infoPtr->zNowISR(z);
3038  infoPtr->pT2NowISR(pow(startingScale,2));
3039  infoPtr->hasHistory(true);
3040 
3041  // Setup weak shower settings.
3042  if (mergingHooksPtr->doWeakClustering()) setupWeakShower(0);
3043 
3044  // Perform trial shower emission
3045  trial->next(process,event);
3046  // Get trial shower pT.
3047  double pTtrial = trial->pTLastInShower();
3048  int typeTrial = trial->typeLastInShower();
3049 
3050  // Clear parton systems.
3051  trial->resetTrial();
3052 
3053  // Get enhanced trial emission weight.
3054  double pTEnhanced = (canEnhanceTrial)
3055  ? trial->userHooksPtr->getEnhancedTrialPT() : 0.;
3056  double wtEnhanced = (canEnhanceTrial)
3057  ? trial->userHooksPtr->getEnhancedTrialWeight() : 1.;
3058  if ( canEnhanceTrial && pTEnhanced > 0.) pTtrial = pTEnhanced;
3059 
3060  // Get veto (merging) scale value
3061  double vetoScale = (mother) ? 0. : mergingHooksPtr->tms();
3062  // Get merging scale in current event
3063  double tnow = mergingHooksPtr->tmsNow( event );
3064 
3065  // Done if evolution scale has fallen below minimum
3066  if ( pTtrial < minScale ) break;
3067  // Reset starting scale.
3068  startingScale = pTtrial;
3069 
3070  // Continue if this state is below the veto scale
3071  if ( tnow < vetoScale && vetoScale > 0. ) continue;
3072 
3073  // Retry if the trial emission was not allowed.
3074  if ( mergingHooksPtr->canVetoTrialEmission()
3075  && mergingHooksPtr->doVetoTrialEmission( process, event) ) continue;
3076 
3077  int iRecAft = event.size() - 1;
3078  int iEmt = event.size() - 2;
3079  int iRadAft = event.size() - 3;
3080  if ( (event[iRecAft].status() != 52 && event[iRecAft].status() != -53) ||
3081  event[iEmt].status() != 51 || event[iRadAft].status() != 51)
3082  iRecAft = iEmt = iRadAft = -1;
3083  for (int i = event.size() - 1; i > 0; i--) {
3084  if (iRadAft == -1 && event[i].status() == -41) iRadAft = i;
3085  else if (iEmt == -1 && event[i].status() == 43) iEmt = i;
3086  else if (iRecAft == -1 && event[i].status() == -42) iRecAft = i;
3087  if (iRadAft != -1 && iEmt != -1 && iRecAft != -1) break;
3088  }
3089 
3090  // Only consider allowed emissions for veto:
3091  // Only allow MPI for MPI no-emission probability.
3092  if ( type == -1 && typeTrial != 1 ) continue;
3093  // Only allow ISR or FSR for radiative no-emission probability.
3094  if ( type == 1 && !(typeTrial == 2 || typeTrial >= 3) ) continue;
3095 
3096  // Update enhanced trial shower weight.
3097  if (canEnhanceTrial && pTtrial > minScale) wt *= (1. - 1./wtEnhanced);
3098  // Done with enhanced trial showers if weight is zero.
3099  if ( canEnhanceTrial && wt == 0.) break;
3100  // Continue producing trial emissions in case of enhanced showers.
3101  if ( canEnhanceTrial && pTtrial > minScale) continue;
3102 
3103  // Continue if this state is below the veto scale
3104  // Veto event if trial pT was above the next nodal scale.
3105  if ( pTtrial > minScale ) doVeto = true;
3106 
3109  //if ( !mother && tnow > vetoScale && vetoScale > 0. ) doVeto = true;
3110 
3111  // For 2 -> 2 pure QCD state, do not allow multiparton interactions
3112  // above the kinematical pT of the 2 -> 2 state.
3113  if ( type == -1
3114  && typeTrial == 1
3115  && mergingHooksPtr->getNumberOfClusteringSteps(process) == 0
3116  && ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
3117  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
3118  || isQCD2to2(state))
3119  && pTtrial > hardFacScale(process) )
3120  return 0.0;
3121 
3122  // If pT of trial emission was in suitable range (trial shower
3123  // successful), return false
3124  if ( pTtrial < minScale ) doVeto = false;
3125 
3126  // Done
3127  break;
3128 
3129  }
3130 
3131  // Done
3132  double res = (canEnhanceTrial) ? wt : ( (doVeto) ? 0. : 1. );
3133  return res;
3134 
3135 }
3136 
3137 //--------------------------------------------------------------------------
3138 
3139 // Assume we have a vector of i elements containing indices into
3140 // another vector with N elements. Update the indices so that all
3141 // unique combinations (starting from 0,1,2,3, ...) are
3142 // covered. Return false when all combinations have been ehausted.
3143 
3144 bool History::updateind(vector<int> & ind, int i, int N) {
3145  if ( i < 0 ) return false;
3146  if ( ++ind[i] < N ) return true;
3147  if ( !updateind(ind, i - 1, N - 1) ) return false;
3148  ind[i] = ind[i - 1] + 1;
3149  return true;
3150 }
3151 
3152 //--------------------------------------------------------------------------
3153 
3154 // Return the expansion of the no-emission probability up to the Nth
3155 // term. Optionally calculate the the terms using fixed alphaS
3156 // and/or PDF ratios.
3157 
3158 vector<double>
3159 History::countEmissions(PartonLevel* trial, double maxscale,
3160  double minscale, int showerType, double as0,
3161  AlphaStrong * asFSR, AlphaStrong * asISR, int N = 1,
3162  bool fixpdf = true, bool fixas = true) {
3163 
3164  if ( N < 0 ) return vector<double>();
3165  vector<double> result(N+1);
3166  result[0] = 1.0;
3167  if ( N < 1 ) return result;
3168 
3169  // Copy state to local process
3170  Event process = state;
3171 
3172  double startingScale = maxscale;
3173  // Careful when setting shower starting scale for pure QCD and prompt
3174  // photon case.
3175  if ( mergingHooksPtr->getNumberOfClusteringSteps(process) == 0
3176  && ( mergingHooksPtr->getProcessString().compare("pp>jj") == 0
3177  || mergingHooksPtr->getProcessString().compare("pp>aj") == 0
3178  || isQCD2to2(state) ) )
3179  startingScale = min( startingScale, hardFacScale(process) );
3180 
3181  vector<double> wts;
3182  bool canEnhanceTrial = (trial->userHooksPtr!=0)
3183  && trial->userHooksPtr->canEnhanceTrial();
3184 
3185  while ( true ) {
3186  // Reset trialShower object
3187  trial->resetTrial();
3188  // Construct event to be showered
3189  Event event = Event();
3190  event.init("(hard process-modified)", particleDataPtr);
3191  event.clear();
3192 
3193  // Reset process scale
3194  process.scale(startingScale);
3195 
3196  // If the maximal scale and the minimal scale coincide (as would
3197  // be the case for the corrected scales of unordered histories),
3198  // do not generate Sudakov
3199  if (minscale >= startingScale) return result;
3200 
3201  // Find z and pT values at which the current state was formed, to
3202  // ensure that the showers can order the next emission correctly in
3203  // rapidity, if required
3204  if ( mother ) {
3205  double z = ( mergingHooksPtr->getNumberOfClusteringSteps(state) == 0)
3206  ? 0.5
3207  : mother->getCurrentZ(clusterIn.emittor,clusterIn.recoiler,
3208  clusterIn.emitted);
3209  // Store z and pT values at which the current state was formed
3210  infoPtr->zNowISR(z);
3211  infoPtr->pT2NowISR(pow(startingScale,2));
3212  infoPtr->hasHistory(true);
3213  }
3214 
3215  // Setup the weak shower information.
3216  if (mergingHooksPtr->doWeakClustering()) setupWeakShower(0);
3217 
3218  // Perform trial shower emission
3219  trial->next(process,event);
3220 
3221  // Get trial shower pT
3222  double pTtrial = trial->pTLastInShower();
3223  int typeTrial = trial->typeLastInShower();
3224 
3225  // Clear parton systems.
3226  trial->resetTrial();
3227 
3228  // Get enhanced trial emission weight.
3229  double pTEnhanced = (canEnhanceTrial)
3230  ? trial->userHooksPtr->getEnhancedTrialPT() : 0.;
3231  double wtEnhanced = (canEnhanceTrial)
3232  ? trial->userHooksPtr->getEnhancedTrialWeight() : 1.;
3233  if ( canEnhanceTrial && pTEnhanced > 0.) pTtrial = pTEnhanced;
3234 
3235  // Get veto (merging) scale value
3236  double vetoScale = (mother) ? 0. : mergingHooksPtr->tms();
3237  // Get merging scale in current event
3238  double tnow = mergingHooksPtr->tmsNow( event );
3239 
3240  // Save scale of current state.
3241  startingScale = pTtrial;
3242  // If the scale of the current state is below the minimal scale, exit.
3243  if ( pTtrial < minscale ) break;
3244  // If this state is below the merging scale, do not count emission.
3245  if ( tnow < vetoScale && vetoScale > 0. ) continue;
3246  // Retry if the trial emission was not allowed.
3247  if ( mergingHooksPtr->canVetoTrialEmission()
3248  && mergingHooksPtr->doVetoTrialEmission( process, event) ) continue;
3249 
3250  // Set weight of enhanced emission.
3251  double enhance = (canEnhanceTrial && pTtrial > minscale) ? wtEnhanced : 1.;
3252 
3253  // Check if a new emission should be generated, either because
3254  // the latest emission was not of the desired kind or if the
3255  // emission was above the minimal scale
3256  double alphaSinPS = as0;
3257  double pdfs = 1.0;
3258 
3259  double asScale2 = pTtrial*pTtrial;
3260  // Directly get argument of running alpha_s from shower plugin.
3261  if (mergingHooksPtr->useShowerPlugin() )
3262  asScale2 = getShowerPluginScale(mother->state, clusterIn.emittor,
3263  clusterIn.emitted, clusterIn.recoiler, "scaleAS", asScale2);
3264 
3265  // Initial state splittings.
3266  if ( (showerType == -1 || showerType == 2) && typeTrial == 2 ) {
3267  // Get weight to translate to alpha_s at fixed renormalisation scale.
3268  if ( fixas ) alphaSinPS = (*asISR).alphaS(asScale2);
3269  // Get weight to translate to PDFs at fixed factorisation scale.
3270  if ( fixpdf )
3271  pdfs = pdfFactor( event, typeTrial, pTtrial,
3272  mergingHooksPtr->muFinME() );
3273  // Final state splittings.
3274  } else if ( (showerType == 1 || showerType == 2) && typeTrial >= 3 ) {
3275  // Get weight to translate to alpha_s at fixed renormalisation scale.
3276  if ( fixas ) alphaSinPS = (*asFSR).alphaS(asScale2);
3277  // Get weight to translate to PDFs at fixed factorisation scale. Needed
3278  // for final state splittings with initial state recoiler.
3279  if ( fixpdf )
3280  pdfs = pdfFactor( event, typeTrial, pTtrial,
3281  mergingHooksPtr->muFinME() );
3282  }
3283 
3284  // Save weight correcting to emission generated with fixed scales.
3285  if ( typeTrial == 2 || typeTrial >= 3 )
3286  wts.push_back(as0/alphaSinPS * pdfs * 1./enhance);
3287 
3288  }
3289 
3290  for ( int n = 1; n <= min(N, int(wts.size())); ++n ) {
3291  vector<int> ind(N);
3292  for ( int i = 0; i < N; ++i ) ind[i] = i;
3293  do {
3294  double x = 1.0;
3295  for ( int j = 0; j < n; ++j ) x *= wts[ind[j]];
3296  result[n] += x;
3297  } while ( updateind(ind, n - 1, wts.size()) );
3298  if ( n%2 ) result[n] *= -1.0;
3299  }
3300 
3301  // Done
3302  return result;
3303 }
3304 
3305 //--------------------------------------------------------------------------
3306 
3307 // Function to integrate PDF ratios between two scales over x and t,
3308 // where the PDFs are always evaluated at the lower t-integration limit
3309 
3310 double History::monteCarloPDFratios(int flav, double x, double maxScale,
3311  double minScale, double pdfScale, double asME, Rndm* rndmPtr) {
3312 
3313  // Perform numerical integration for PDF ratios
3314  // Prefactor is as/2PI
3315  double factor = asME / (2.*M_PI);
3316  // Scale integration just produces a multiplicative logarithm
3317  factor *= log(maxScale/minScale);
3318 
3319  // For identical scales, done
3320  if (factor == 0.) return 0.;
3321 
3322  // Declare constants
3323  double CF = 4./3.;
3324  double CA = 3.;
3325  double NF = 4.;
3326  double TR = 1./2.;
3327 
3328  double integral = 0.;
3329  double RN = rndmPtr->flat();
3330 
3331  if (flav == 21) {
3332  double zTrial = pow(x,RN);
3333  integral = -log(x) * zTrial *
3334  integrand(flav, x, pdfScale, zTrial);
3335  integral += 1./6.*(11.*CA - 4.*NF*TR)
3336  + 2.*CA*log(1.-x);
3337  } else {
3338  double zTrial = x + RN*(1. - x);
3339  integral = (1.-x) *
3340  integrand(flav, x, pdfScale, zTrial);
3341  integral += 3./2.*CF
3342  + 2.*CF*log(1.-x);
3343  }
3344 
3345  // Done
3346  return (factor*integral);
3347 }
3348 
3349 /*--------------- METHODS USED FOR CONTRUCTION OF ALL HISTORIES --------- */
3350 
3351 // Check if a ordered (and complete) path has been found in the
3352 // initial node, in which case we will no longer be interested in
3353 // any unordered paths.
3354 
3355 bool History::onlyOrderedPaths() {
3356  if ( !mother || foundOrderedPath ) return foundOrderedPath;
3357  return foundOrderedPath = mother->onlyOrderedPaths();
3358 }
3359 
3360 //--------------------------------------------------------------------------
3361 
3362 // Check if a STRONGLY ordered (and complete) path has been found in the
3363 // initial node, in which case we will no longer be interested in
3364 // any unordered paths.
3365 
3366 bool History::onlyStronglyOrderedPaths() {
3367  if ( !mother || foundStronglyOrderedPath ) return foundStronglyOrderedPath;
3368  return foundStronglyOrderedPath = mother->onlyStronglyOrderedPaths();
3369 }
3370 
3371 //--------------------------------------------------------------------------
3372 
3373 // Check if an allowed (according to user-criterion) path has been found in
3374 // the initial node, in which case we will no longer be interested in
3375 // any forbidden paths.
3376 
3377 bool History::onlyAllowedPaths() {
3378  if ( !mother || foundAllowedPath ) return foundAllowedPath;
3379  return foundAllowedPath = mother->onlyAllowedPaths();
3380 }
3381 
3382 //--------------------------------------------------------------------------
3383 
3384 // When a full path has been found, register it with the initial
3385 // history node.
3386 // IN History : History to be registered as path
3387 // bool : Specifying if clusterings so far were ordered
3388 // bool : Specifying if path is complete down to 2->2 process
3389 // OUT true if History object forms a plausible path (eg prob>0 ...)
3390 
3391 bool History::registerPath(History & l, bool isOrdered,
3392  bool isStronglyOrdered, bool isAllowed, bool isComplete) {
3393 
3394  // We are not interested in improbable paths.
3395  if ( l.prob <= 0.0)
3396  return false;
3397  // We only register paths in the initial node.
3398  if ( mother ) return mother->registerPath(l, isOrdered,
3399  isStronglyOrdered, isAllowed, isComplete);
3400 
3401  // Again, we are not interested in improbable paths.
3402  if ( sumpath == sumpath + l.prob )
3403  return false;
3404  if ( mergingHooksPtr->canCutOnRecState()
3405  && foundAllowedPath && !isAllowed )
3406  return false;
3407  if ( mergingHooksPtr->enforceStrongOrdering()
3408  && foundStronglyOrderedPath && !isStronglyOrdered )
3409  return false;
3410  if ( mergingHooksPtr->orderHistories()
3411  && foundOrderedPath && !isOrdered ) {
3412  // Prefer complete or allowed paths to ordered paths.
3413  if ( (!foundCompletePath && isComplete)
3414  || (!foundAllowedPath && isAllowed) ) ;
3415  else return false;
3416  }
3417 
3418  if ( foundCompletePath && !isComplete)
3419  return false;
3420  if ( !mergingHooksPtr->canCutOnRecState()
3421  && !mergingHooksPtr->allowCutOnRecState() )
3422  foundAllowedPath = true;
3423 
3424  if ( mergingHooksPtr->canCutOnRecState() && isAllowed && isComplete) {
3425  if ( !foundAllowedPath || !foundCompletePath ) {
3426  // If this is the first complete, allowed path, discard the
3427  // old, disallowed or incomplete ones.
3428  paths.clear();
3429  sumpath = 0.0;
3430  }
3431  foundAllowedPath = true;
3432 
3433  }
3434 
3435  if ( mergingHooksPtr->enforceStrongOrdering() && isStronglyOrdered
3436  && isComplete ) {
3437  if ( !foundStronglyOrderedPath || !foundCompletePath ) {
3438  // If this is the first complete, ordered path, discard the
3439  // old, non-ordered or incomplete ones.
3440  paths.clear();
3441  sumpath = 0.0;
3442  }
3443  foundStronglyOrderedPath = true;
3444  foundCompletePath = true;
3445 
3446  }
3447 
3448  if ( mergingHooksPtr->orderHistories() && isOrdered && isComplete ) {
3449  if ( !foundOrderedPath || !foundCompletePath ) {
3450  // If this is the first complete, ordered path, discard the
3451  // old, non-ordered or incomplete ones.
3452  paths.clear();
3453  sumpath = 0.0;
3454  }
3455  foundOrderedPath = true;
3456  foundCompletePath = true;
3457 
3458  }
3459 
3460  if ( isComplete ) {
3461  if ( !foundCompletePath ) {
3462  // If this is the first complete path, discard the old,
3463  // incomplete ones.
3464  paths.clear();
3465  sumpath = 0.0;
3466  }
3467  foundCompletePath = true;
3468  }
3469 
3470  // Remember, if this path is ordered, even if no ordering is required
3471  if ( isOrdered ) {
3472  foundOrderedPath = true;
3473  }
3474 
3475  // Calculate the probability for weak emissions in the path.
3476  double weakProb = 1.;
3477  if (mergingHooksPtr->doWeakClustering())
3478  weakProb = l.getWeakProb();
3479 
3480  // Index path by probability
3481  sumpath += l.prob * weakProb;
3482  paths[sumpath] = &l;
3483 
3484  return true;
3485 }
3486 
3487 //--------------------------------------------------------------------------
3488 
3489 // For the history-defining state (and if necessary interfering
3490 // states), find all possible clusterings.
3491 // NO INPUT
3492 // OUT vector of all (rad,rec,emt) systems
3493 
3494 vector<Clustering> History::getAllQCDClusterings() {
3495  vector<Clustering> ret;
3496  // Initialise vectors to keep track of position of partons in the
3497  // history-defining state
3498  vector <int> posFinalPartn;
3499  vector <int> posInitPartn;
3500  vector <int> posFinalGluon;
3501  vector <int> posFinalQuark;
3502  vector <int> posFinalAntiq;
3503  vector <int> posInitGluon;
3504  vector <int> posInitQuark;
3505  vector <int> posInitAntiq;
3506 
3507  // Search event record for final state particles and store these in
3508  // quark, anti-quark and gluon vectors
3509  for ( int i=0; i < state.size(); ++i )
3510  if ( state[i].isFinal() && state[i].colType() !=0 ) {
3511  // Store final partons
3512  if ( state[i].id() == 21 ) posFinalGluon.push_back(i);
3513  else if ( state[i].idAbs() < 10 && state[i].id() > 0)
3514  posFinalQuark.push_back(i);
3515  else if ( state[i].idAbs() < 10 && state[i].id() < 0)
3516  posFinalAntiq.push_back(i);
3517  } else if (state[i].status() == -21 && state[i].colType() != 0 ) {
3518  // Store initial partons
3519  if ( state[i].id() == 21 ) posInitGluon.push_back(i);
3520  else if ( state[i].idAbs() < 10 && state[i].id() > 0)
3521  posInitQuark.push_back(i);
3522  else if ( state[i].idAbs() < 10 && state[i].id() < 0)
3523  posInitAntiq.push_back(i);
3524  }
3525 
3526  // Get all clusterings for input state
3527  vector<Clustering> systems;
3528  systems = getQCDClusterings(state);
3529  ret.insert(ret.end(), systems.begin(), systems.end());
3530  systems.resize(0);
3531 
3532  // If valid clusterings were found, return
3533  if ( !ret.empty() ) return ret;
3534  // If no clusterings have been found until now, try to find
3535  // clusterings of diagrams that interfere with the current one
3536  // (i.e. change the colours of the current event slightly and run
3537  // search again)
3538  else if ( ret.empty()
3539  && mergingHooksPtr->allowColourShuffling() ) {
3540  Event NewState = Event(state);
3541  // Start with changing final state quark colour
3542  for(int i = 0; i < int(posFinalQuark.size()); ++i) {
3543  // Never change the hard process candidates
3544  if ( mergingHooksPtr->hardProcess->matchesAnyOutgoing(posFinalQuark[i],
3545  NewState) )
3546  continue;
3547  int col = NewState[posFinalQuark[i]].col();
3548  for(int j = 0; j < int(posInitAntiq.size()); ++j) {
3549  // Now swap colours
3550  int acl = NewState[posInitAntiq[j]].acol();
3551  if ( col == acl ) continue;
3552  NewState[posFinalQuark[i]].col(acl);
3553  NewState[posInitAntiq[j]].acol(col);
3554  systems = getQCDClusterings(NewState);
3555  if (!systems.empty()) {
3556  state = NewState;
3557  NewState.clear();
3558  ret.insert(ret.end(), systems.begin(), systems.end());
3559  systems.resize(0);
3560  return ret;
3561  }
3562  }
3563  }
3564  // Now change final state antiquark anticolour
3565  for(int i = 0; i < int(posFinalAntiq.size()); ++i) {
3566  // Never change the hard process candidates
3567  if ( mergingHooksPtr->hardProcess->matchesAnyOutgoing(posFinalAntiq[i],
3568  NewState) )
3569  continue;
3570  int acl = NewState[posFinalAntiq[i]].acol();
3571  for(int j = 0; j < int(posInitQuark.size()); ++j) {
3572  // Now swap colours
3573  int col = NewState[posInitQuark[j]].col();
3574  if ( col == acl ) continue;
3575  NewState[posFinalAntiq[i]].acol(col);
3576  NewState[posInitQuark[j]].col(acl);
3577  systems = getQCDClusterings(NewState);
3578  if (!systems.empty()) {
3579  state = NewState;
3580  NewState.clear();
3581  ret.insert(ret.end(), systems.begin(), systems.end());
3582  systems.resize(0);
3583  return ret;
3584  }
3585  }
3586  }
3587 
3588  if ( !ret.empty() ) {
3589  string message="Warning in History::getAllQCDClusterings: Changed";
3590  message+=" colour structure to allow at least one clustering.";
3591  infoPtr->errorMsg(message);
3592  }
3593 
3594  }
3595 
3596  // Done
3597  return ret;
3598 }
3599 
3600 //--------------------------------------------------------------------------
3601 
3602 // For one given state, find all possible clusterings.
3603 // IN Event : state to be investigated
3604 // OUT vector of all (rad,rec,emt) systems in the state
3605 
3606 vector<Clustering> History::getQCDClusterings( const Event& event) {
3607  vector<Clustering> ret;
3608 
3609  // Initialise vectors to keep track of position of partons in the
3610  // input event
3611  vector <int> posFinalPartn;
3612  vector <int> posInitPartn;
3613 
3614  vector <int> posFinalGluon;
3615  vector <int> posFinalQuark;
3616  vector <int> posFinalAntiq;
3617  vector <int> posInitGluon;
3618  vector <int> posInitQuark;
3619  vector <int> posInitAntiq;
3620 
3621  // Search event record for final state particles and store these in
3622  // quark, anti-quark and gluon vectors
3623  for (int i=0; i < event.size(); ++i)
3624  if ( event[i].isFinal() && event[i].colType() !=0 ) {
3625  // Store final partons
3626  posFinalPartn.push_back(i);
3627  if ( event[i].id() == 21 ) posFinalGluon.push_back(i);
3628  else if ( event[i].idAbs() < 10 && event[i].id() > 0)
3629  posFinalQuark.push_back(i);
3630  else if ( event[i].idAbs() < 10 && event[i].id() < 0)
3631  posFinalAntiq.push_back(i);
3632  } else if ( event[i].status() == -21 && event[i].colType() != 0 ) {
3633  // Store initial partons
3634  posInitPartn.push_back(i);
3635  if ( event[i].id() == 21 ) posInitGluon.push_back(i);
3636  else if ( event[i].idAbs() < 10 && event[i].id() > 0)
3637  posInitQuark.push_back(i);
3638  else if ( event[i].idAbs() < 10 && event[i].id() < 0)
3639  posInitAntiq.push_back(i);
3640  }
3641 
3642  int nFiGluon = int(posFinalGluon.size());
3643  int nFiQuark = int(posFinalQuark.size());
3644  int nFiAntiq = int(posFinalAntiq.size());
3645  int nInGluon = int(posInitGluon.size());
3646  int nInQuark = int(posInitQuark.size());
3647  int nInAntiq = int(posInitAntiq.size());
3648 
3649  vector<Clustering> systems;
3650 
3651  // Find rad + emt + rec systems:
3652  // (1) Start from gluon and find all (rad,rec,emt=gluon) triples
3653  for (int i = 0; i < nFiGluon; ++i) {
3654  int EmtGluon = posFinalGluon[i];
3655  systems = findQCDTriple( EmtGluon, 2, event, posFinalPartn, posInitPartn);
3656  ret.insert(ret.end(), systems.begin(), systems.end());
3657  systems.resize(0);
3658  }
3659 
3660  // For more than one quark-antiquark pair in final state, check for
3661  // g -> qqbar splittings
3662  bool check_g2qq = true;
3663  if ( ( ( nInQuark + nInAntiq == 0 )
3664  && (nInGluon == 0)
3665  && (nFiQuark == 1) && (nFiAntiq == 1) )
3666  || ( ( nFiQuark + nFiAntiq == 0)
3667  && (nInQuark == 1) && (nInAntiq == 1) ) )
3668  check_g2qq = false;
3669 
3670  if ( check_g2qq ) {
3671 
3672  // (2) Start from quark and find all (rad,rec,emt=quark) triples
3673  // ( when g -> q qbar occured )
3674  for( int i=0; i < nFiQuark; ++i) {
3675  int EmtQuark = posFinalQuark[i];
3676  systems = findQCDTriple( EmtQuark,1,event, posFinalPartn, posInitPartn);
3677  ret.insert(ret.end(), systems.begin(), systems.end());
3678  systems.resize(0);
3679  }
3680 
3681  // (3) Start from anti-quark and find all (rad,rec,emt=anti-quark)
3682  // triples ( when g -> q qbar occured )
3683  for( int i=0; i < nFiAntiq; ++i) {
3684  int EmtAntiq = posFinalAntiq[i];
3685  systems = findQCDTriple( EmtAntiq,1,event, posFinalPartn, posInitPartn);
3686  ret.insert(ret.end(), systems.begin(), systems.end());
3687  systems.resize(0);
3688  }
3689  }
3690 
3691  return ret;
3692 }
3693 
3694 //--------------------------------------------------------------------------
3695 
3696 // Function to attach (spin-dependent duplicates of) a clustering.
3697 
3698 void History::attachClusterings (vector<Clustering>& clus, int iEmt, int iRad,
3699  int iRec, int iPartner, double pT, const Event& event) {
3700 
3701  if ( !mergingHooksPtr->doWeakClustering() ) {
3702 
3703  clus.push_back( Clustering(iEmt, iRad, iRec, iPartner,
3704  pT, 0, 0, 0, 0, 9));
3705 
3706  } else {
3707 
3708  // Check if spins are already assigned.
3709  int radSpin = event[iRad].intPol();
3710  int emtSpin = event[iEmt].intPol();
3711  int recSpin = event[iRec].intPol();
3712  bool hasRadSpin = (radSpin != 9);
3713  bool hasEmtSpin = (emtSpin != 9);
3714  bool hasRecSpin = (recSpin != 9);
3715 
3716  // Check if any of the partons are quarks.
3717  bool radQuark = event[iRad].idAbs() < 10;
3718  bool emtQuark = event[iEmt].idAbs() < 10;
3719  bool recQuark = event[iRec].idAbs() < 10;
3720 
3721  // Generate the vector of all spin structures
3722  vector < vector<int> > structs;
3723  structs.resize(0);
3724  for (int i = 0; i < 3; ++i){
3725  int sRad = (i==0) ? -1 : (i==1) ? 1 : 9;
3726  for (int j = 0; j < 3; ++j){
3727  int sEmt = (j==0) ? -1 : (j==1) ? 1 : 9;
3728  for (int k = 0; k < 3; ++k){
3729  int sRec = (k==0) ? -1 : (k==1) ? 1 : 9;
3730  vector <int> s;
3731  s.push_back(sRad); s.push_back(sEmt); s.push_back(sRec);
3732  structs.push_back(s);
3733  }
3734  }
3735  }
3736 
3737  vector < vector<int> > allStructs;
3738  for (int i = 0; i< int(structs.size()); ++i) {
3739  // Only pick allowed combinations if spin is already set.
3740  if (hasRadSpin && radQuark && structs[i][0] != radSpin) continue;
3741  if (hasEmtSpin && emtQuark && structs[i][1] != emtSpin) continue;
3742  if (hasRecSpin && recQuark && structs[i][2] != recSpin) continue;
3743  // Set +-1 for previously unset quark spins.
3744  if (!hasRadSpin && radQuark && structs[i][0] == 9) continue;
3745  if (!hasEmtSpin && emtQuark && structs[i][1] == 9) continue;
3746  if (!hasRecSpin && recQuark && structs[i][2] == 9) continue;
3747  // Leave spin 9 for gluons.
3748  if (!radQuark && structs[i][0] != radSpin) continue;
3749  if (!emtQuark && structs[i][1] != emtSpin) continue;
3750  if (!recQuark && structs[i][2] != recSpin) continue;
3751  // Ensure that the quarks in the g-> q qbar splitting have equal spin.
3752  if (radQuark && emtQuark && structs[i][0] != structs[i][1]) continue;
3753  // Store spin structure.
3754  allStructs.push_back(structs[i]);
3755  }
3756 
3757  // Get flavour of radiator before the splitting.
3758  int flavRadBef = getRadBeforeFlav(iRad, iEmt, event);
3759  // Push back all spin-dependent clusterings.
3760  for (int i = 0; i< int(allStructs.size()); ++i) {
3761  // Get spin of radiator before the splitting.
3762  int spinRadBef = getRadBeforeSpin(iRad, iEmt, allStructs[i][0],
3763  allStructs[i][1], event);
3764  clus.push_back( Clustering(iEmt, iRad, iRec, iPartner, pT, flavRadBef,
3765  allStructs[i][0], allStructs[i][1], allStructs[i][2], spinRadBef) );
3766  }
3767 
3768  } // doWeakClustering
3769 
3770  return;
3771 
3772 }
3773 
3774 //--------------------------------------------------------------------------
3775 
3776 // Function to construct (rad,rec,emt) triples from the event
3777 // IN int : Position of Emitted in event record for which
3778 // dipoles should be constructed
3779 // int : Colour topogy to be tested
3780 // 1= g -> qqbar, causing 2 -> 2 dipole splitting
3781 // 2= q(bar) -> q(bar) g && g -> gg,
3782 // causing a 2 -> 3 dipole splitting
3783 // Event : event record to be checked for ptential partners
3784 // OUT vector of all allowed radiator+recoiler+emitted triples
3785 
3786 vector<Clustering> History::findQCDTriple (int EmtTagIn, int colTopIn,
3787  const Event& event,
3788  vector<int> posFinalPartn,
3789  vector <int> posInitPartn ) {
3790 
3791  // Copy input parton tag
3792  int EmtTag = EmtTagIn;
3793  // Copy input colour topology tag
3794  // (1: g --> qqbar splitting present, 2:rest)
3795  int colTop = colTopIn;
3796 
3797  // Initialise FinalSize
3798  int finalSize = int(posFinalPartn.size());
3799  int initSize = int(posInitPartn.size());
3800  int size = initSize + finalSize;
3801 
3802  vector<Clustering> clus;
3803 
3804  // Search final partons to find partons colour-connected to
3805  // event[EmtTag], choose radiator, then choose recoiler
3806  for ( int a = 0; a < size; ++a ) {
3807  int i = (a < finalSize)? a : (a - finalSize) ;
3808  int iRad = (a < finalSize)? posFinalPartn[i] : posInitPartn[i];
3809 
3810  if ( event[iRad].col() == event[EmtTag].col()
3811  && event[iRad].acol() == event[EmtTag].acol() )
3812  continue;
3813 
3814  if (iRad != EmtTag ) {
3815  int pTdef = event[iRad].isFinal() ? 1 : -1;
3816  int sign = (a < finalSize)? 1 : -1 ;
3817 
3818  // First colour topology: g --> qqbar. Here, emt & rad should
3819  // have same flavour (causes problems for gamma->qqbar).
3820  if (colTop == 1) {
3821 
3822  if ( event[iRad].id() == -sign*event[EmtTag].id() ) {
3823  int col = -1;
3824  int acl = -1;
3825 
3826  if (event[iRad].isFinal() ) {
3827  if (event[iRad].id() < 0) {
3828  col = event[EmtTag].col();
3829  acl = 0;
3830  } else {
3831  acl = event[EmtTag].acol();
3832  col = 0;
3833  }
3834  } else {
3835  if (event[iRad].id() < 0) {
3836  acl = event[EmtTag].acol();
3837  col = 0;
3838  } else {
3839  col = event[EmtTag].col();
3840  acl = 0;
3841  }
3842  }
3843 
3844  // Recoiler
3845  int iRec = 0;
3846  // Colour partner
3847  int iPartner = 0;
3848 
3849  if (col > 0) {
3850  // Find recoiler by colour
3851  iRec = FindCol(col,iRad,EmtTag,event,1,true);
3852  // In initial state splitting has final state colour partner,
3853  // Save both partner and recoiler
3854  if ( (sign < 0) && (event[iRec].isFinal()) ) {
3855  // Save colour recoiler
3856  iPartner = iRec;
3857  // Reset kinematic recoiler to initial state parton
3858  for(int l = 0; l < int(posInitPartn.size()); ++l)
3859  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
3860  // For final state splittings, colour partner and recoiler are
3861  // identical
3862  } else {
3863  iPartner = iRec;
3864  }
3865  if ( iRec != 0 && iPartner != 0
3866  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
3867  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
3868  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
3869  continue;
3870  }
3871 
3872  // Reset partner
3873  iPartner = 0;
3874  // Find recoiler by colour
3875  iRec = FindCol(col,iRad,EmtTag,event,2,true);
3876  // In initial state splitting has final state colour partner,
3877  // Save both partner and recoiler
3878  if ( (sign < 0) && (event[iRec].isFinal()) ) {
3879  // Save colour recoiler
3880  iPartner = iRec;
3881  // Reset kinematic recoiler to initial state parton
3882  for(int l = 0; l < int(posInitPartn.size()); ++l)
3883  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
3884  // For final state splittings, colour partner and recoiler are
3885  // identical
3886  } else {
3887  iPartner = iRec;
3888  }
3889  if ( iRec != 0 && iPartner != 0
3890  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
3891  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
3892  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
3893  continue;
3894  }
3895  }
3896 
3897 
3898  if (acl > 0) {
3899 
3900  // Reset partner
3901  iPartner = 0;
3902  // Find recoiler by colour
3903  iRec = FindCol(acl,iRad,EmtTag,event,1,true);
3904  // In initial state splitting has final state colour partner,
3905  // Save both partner and recoiler
3906  if ( (sign < 0) && (event[iRec].isFinal()) ) {
3907  // Save colour recoiler
3908  iPartner = iRec;
3909  // Reset kinematic recoiler to initial state parton
3910  for(int l = 0; l < int(posInitPartn.size()); ++l)
3911  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
3912  // For final state splittings, colour partner and recoiler are
3913  // identical
3914  } else {
3915  iPartner = iRec;
3916  }
3917  if ( iRec != 0 && iPartner != 0
3918  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
3919  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
3920  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
3921  continue;
3922  }
3923 
3924  // Reset partner
3925  iPartner = 0;
3926  // Find recoiler by colour
3927  iRec = FindCol(acl,iRad,EmtTag,event,2,true);
3928  // In initial state splitting has final state colour partner,
3929  // Save both partner and recoiler
3930  if ( (sign < 0) && (event[iRec].isFinal()) ) {
3931  // Save colour recoiler
3932  iPartner = iRec;
3933  // Reset kinematic recoiler to initial state parton
3934  for(int l = 0; l < int(posInitPartn.size()); ++l)
3935  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
3936  // For final state splittings, colour partner and recoiler are
3937  // identical
3938  } else {
3939  iPartner = iRec;
3940  }
3941  if ( iRec != 0 && iPartner != 0
3942  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
3943  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
3944  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
3945  continue;
3946  }
3947  }
3948  // Initial gluon splitting
3949  } else if ( event[iRad].id() == 21
3950  &&( event[iRad].col() == event[EmtTag].col()
3951  || event[iRad].acol() == event[EmtTag].acol() )) {
3952  // For an initial state radiator, always set recoiler
3953  // to the other initial state parton (recoil is taken
3954  // by full remaining system, so this is just a
3955  // labelling for such a process)
3956  int RecInit = 0;
3957  for(int l = 0; l < int(posInitPartn.size()); ++l)
3958  if (posInitPartn[l] != iRad) RecInit = posInitPartn[l];
3959 
3960  // Find the colour connected partner
3961  // Find colour index of radiator before splitting
3962  int col = getRadBeforeCol(iRad, EmtTag, event);
3963  int acl = getRadBeforeAcol(iRad, EmtTag, event);
3964 
3965  // Find the correct partner: If a colour line has split,
3966  // the partner is connected to the radiator before the splitting
3967  // by a colour line (same reasoning for anticolour). The colour
3968  // that split is the colour appearing twice in the
3969  // radiator + emitted pair.
3970  // Thus, if we remove a colour index with the clustering,
3971  // we should look for a colour partner, else look for
3972  // an anticolour partner
3973  int colRemove = (event[iRad].col() == event[EmtTag].col())
3974  ? event[iRad].col() : event[iRad].acol();
3975 
3976  int iPartner = 0;
3977  if (colRemove > 0 && col > 0 && col != colRemove)
3978  iPartner = FindCol(col,iRad,EmtTag,event,1,true)
3979  + FindCol(col,iRad,EmtTag,event,2,true);
3980  else if (colRemove > 0 && acl > 0 && acl != colRemove)
3981  iPartner = FindCol(acl,iRad,EmtTag,event,1,true)
3982  + FindCol(acl,iRad,EmtTag,event,2,true);
3983 
3984  if ( allowedClustering( iRad, EmtTag, RecInit, iPartner, event ) ) {
3985  attachClusterings (clus, EmtTag, iRad, RecInit, iPartner,
3986  pTLund(event, iRad, EmtTag, RecInit, pTdef), event);
3987  continue;
3988  }
3989  }
3990 
3991  // Second colour topology: Gluon emission
3992 
3993  } else {
3994  if ( (event[iRad].col() == event[EmtTag].acol())
3995  || (event[iRad].acol() == event[EmtTag].col())
3996  || (event[iRad].col() == event[EmtTag].col())
3997  || (event[iRad].acol() == event[EmtTag].acol()) ) {
3998  // For the rest, choose recoiler to have a common colour
3999  // tag with radiator, while not being the "Emitted"
4000 
4001  int col = -1;
4002  int acl = -1;
4003 
4004  if (event[iRad].isFinal() ) {
4005 
4006  if ( event[iRad].id() < 0) {
4007  acl = event[EmtTag].acol();
4008  col = event[iRad].col();
4009  } else if ( event[iRad].id() > 0 && event[iRad].id() < 10) {
4010  col = event[EmtTag].col();
4011  acl = event[iRad].acol();
4012  } else {
4013  col = event[EmtTag].col();
4014  acl = event[EmtTag].acol();
4015  }
4016 
4017  int iRec = 0;
4018  if (col > 0) {
4019  iRec = FindCol(col,iRad,EmtTag,event,1,true);
4020  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4021  if (iRec != 0
4022  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4023  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4024  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4025  continue;
4026  }
4027 
4028  iRec = FindCol(col,iRad,EmtTag,event,2,true);
4029  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4030  if (iRec != 0
4031  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4032  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4033  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4034  continue;
4035  }
4036  }
4037 
4038  if (acl > 0) {
4039  iRec = FindCol(acl,iRad,EmtTag,event,1,true);
4040  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4041  if (iRec != 0
4042  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4043  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4044  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4045  continue;
4046  }
4047 
4048  iRec = FindCol(acl,iRad,EmtTag,event,2,true);
4049  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4050  if (iRec != 0
4051  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4052  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4053  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4054  continue;
4055  }
4056  }
4057 
4058  } else {
4059 
4060  // For an initial state radiator, always set recoiler
4061  // to the other initial state parton (recoil is taken
4062 
4063  // by full remaining system, so this is just a
4064  // labelling for such a process)
4065  int RecInit = 0;
4066  int iPartner = 0;
4067  for(int l = 0; l < int(posInitPartn.size()); ++l)
4068  if (posInitPartn[l] != iRad) RecInit = posInitPartn[l];
4069 
4070  // Find the colour connected partner
4071  // Find colour index of radiator before splitting
4072  col = getRadBeforeCol(iRad, EmtTag, event);
4073  acl = getRadBeforeAcol(iRad, EmtTag, event);
4074 
4075  // Find the correct partner: If a colour line has split,
4076  // the partner is connected to the radiator before the splitting
4077  // by a colour line (same reasoning for anticolour). The colour
4078  // that split is the colour appearing twice in the
4079  // radiator + emitted pair.
4080  // Thus, if we remove a colour index with the clustering,
4081  // we should look for a colour partner, else look for
4082  // an anticolour partner
4083  int colRemove = (event[iRad].col() == event[EmtTag].col())
4084  ? event[iRad].col() : 0;
4085  iPartner = (colRemove > 0)
4086  ? FindCol(col,iRad,EmtTag,event,1,true)
4087  + FindCol(col,iRad,EmtTag,event,2,true)
4088  : FindCol(acl,iRad,EmtTag,event,1,true)
4089  + FindCol(acl,iRad,EmtTag,event,2,true);
4090 
4091  if ( allowedClustering( iRad, EmtTag, RecInit, iPartner, event)) {
4092  attachClusterings (clus, EmtTag, iRad, RecInit, iPartner,
4093  pTLund(event, iRad, EmtTag, RecInit, pTdef), event);
4094  continue;
4095  }
4096  }
4097  }
4098  }
4099  }
4100  }
4101 
4102  // Done
4103  return clus;
4104 }
4105 
4106 //--------------------------------------------------------------------------
4107 
4108 // For the history-defining state (and if necessary interfering
4109 // states), find all possible clusterings.
4110 // NO INPUT
4111 // OUT vector of all (rad,rec,emt) systems
4112 
4113 vector<Clustering> History::getAllEWClusterings() {
4114  vector<Clustering> ret;
4115 
4116  // Get all clusterings for input state
4117  vector<Clustering> systems;
4118  systems = getEWClusterings(state);
4119  ret.insert(ret.end(), systems.begin(), systems.end());
4120  // Done
4121  return ret;
4122 }
4123 
4124 //--------------------------------------------------------------------------
4125 
4126 // For one given state, find all possible clusterings.
4127 // IN Event : state to be investigated
4128 // OUT vector of all (rad,rec,emt) systems in the state
4129 
4130 vector<Clustering> History::getEWClusterings( const Event& event) {
4131  vector<Clustering> ret;
4132 
4133  // Initialise vectors to keep track of position of partons in the
4134  // input event
4135  vector <int> posFinalPartn;
4136  vector <int> posInitPartn;
4137  vector <int> posFinalW;
4138  vector <int> posFinalZ;
4139 
4140  // Search event record for final state particles and store these in
4141  // quark, anti-quark and gluon vectors
4142  for ( int i=3; i < event.size(); ++i )
4143  if ( event[i].isFinal() ) {
4144  // Store final partons
4145  posFinalPartn.push_back(i);
4146  } else {
4147  // Store initial partons
4148  posInitPartn.push_back(i);
4149  }
4150  // Search event record for final W.
4151  for ( int i=0; i < event.size(); ++i )
4152  if ( event[i].isFinal() && event[i].idAbs() == 24 )
4153  posFinalW.push_back( i );
4154 
4155  // Search event record for final Z.
4156  for ( int i=0; i < event.size(); ++i )
4157  if ( event[i].isFinal() && event[i].idAbs() == 23 )
4158  posFinalZ.push_back( i );
4159 
4160 
4161  vector<Clustering> systems;
4162  // Find rad + emt + rec systems:
4163  // (1) Start from W boson and find all (rad,rec,emt=W) triples
4164  for ( int i = 0; i < int(posFinalW.size()); ++i ) {
4165  int emtW = posFinalW[i];
4166  systems = findEWTripleW( emtW, event, posFinalPartn, posInitPartn);
4167  ret.insert(ret.end(), systems.begin(), systems.end());
4168  systems.resize(0);
4169  }
4170  // Find rad + emt + rec systems:
4171  // (1) Start from Z boson and find all (rad,rec,emt=W) triples
4172  for ( int i = 0; i < int(posFinalZ.size()); ++i ) {
4173  int emtZ = posFinalZ[i];
4174 
4175  systems = findEWTripleZ( emtZ, event, posFinalPartn, posInitPartn);
4176  ret.insert(ret.end(), systems.begin(), systems.end());
4177  systems.resize(0);
4178  }
4179 
4180  return ret;
4181 }
4182 
4183 //--------------------------------------------------------------------------
4184 
4185 // Function to construct (rad,rec,emt) triples from the event
4186 // IN int : Position of Emitted in event record for which
4187 // dipoles should be constructed
4188 // int : Colour topogy to be tested
4189 // 1= g -> qqbar, causing 2 -> 2 dipole splitting
4190 // 2= q(bar) -> q(bar) g && g -> gg,
4191 // causing a 2 -> 3 dipole splitting
4192 // Event : event record to be checked for ptential partners
4193 // OUT vector of all allowed radiator+recoiler+emitted triples
4194 
4195 vector<Clustering> History::findEWTripleW ( int emtTagIn, const Event& event,
4196  vector<int> posFinalPartn, vector<int> posInitPartn ) {
4197  // Copy input parton tag
4198  int emtTag = emtTagIn;
4199  int flavEmt = event[emtTag].id();
4200 
4201  // Copy input colour topology tag
4202  // (1: g --> qqbar splitting present, 2:rest)
4203 
4204  // Initialise FinalSize
4205  int finalSize = int(posFinalPartn.size());
4206  int initSize = int(posInitPartn.size());
4207 
4208  // Store flavour count to check if the new flavour configuration is valid.
4209  vector<int> flavCounts(30,0);
4210 
4211  for ( int a = 0; a < finalSize; ++a ) {
4212  if (event[posFinalPartn[a]].idAbs() < 20) {
4213  int sign = 1;
4214  if (event[posFinalPartn[a]].id() < 0)
4215  sign = -1;
4216  flavCounts[event[posFinalPartn[a]].idAbs()] += sign;
4217  }
4218  if (event[posFinalPartn[a]].idAbs() == 24)
4219  flavCounts[24]++;
4220  }
4221 
4222  for ( int a = 0; a < initSize; ++a ) {
4223  if (event[posInitPartn[a]].idAbs() < 20) {
4224  int sign = 1;
4225  if (event[posInitPartn[a]].id() < 0)
4226  sign = -1;
4227  flavCounts[event[posInitPartn[a]].idAbs()] -= sign;
4228  }
4229  }
4230 
4231  vector<Clustering> clus;
4232 
4233  // Search final partons to find partons colour-connected to
4234  // event[EmtTag], choose radiator, then choose recoiler
4235  for ( int a = 0; a < finalSize; ++a ) {
4236 
4237  int iRad = posFinalPartn[a];
4238  if (iRad != emtTag) {
4239 
4240  // Spin information.
4241  int spinRad = event[iRad].intPol();
4242  if (spinRad == -1 || spinRad == 9 || spinRad == 0) {
4243 
4244  int pTdef = 1;
4245  // Find recoiler by flavour.
4246  int flavRad = event[iRad].id();
4247  // Only allow quark and leptons to be radiators.
4248  if (event[iRad].isQuark() || event[iRad].isLepton()) {
4249 
4250  // Check if the W+- matches that of the quark/lepton.
4251  int flavExp = (flavRad > 0) ? 24 : -24;
4252  if (abs(flavRad) % 2 == 0) flavExp = -flavExp;
4253  if (flavExp == flavEmt) {
4254 
4255  // Find possible flavours that the W can change the quark to.
4256  vector<int> flavRadBefs = posFlavCKM(flavRad);
4257 
4258  // Change to anti particles if radiator is an anti particle.
4259  if (flavRad < 0)
4260  for (int i = 0;i < int(flavRadBefs.size()); ++i)
4261  flavRadBefs[i] = - flavRadBefs[i];
4262 
4263  // Loop through final partons and try to find matching flavours.
4264  for ( int i = 0; i < finalSize; ++i ) {
4265  int iRec = posFinalPartn[i];
4266 
4267  // Check for particle overlaps.
4268  if ( iRec != iRad && iRec != emtTag ) {
4269 
4270  for (int j = 0;j < int(flavRadBefs.size()); ++j) {
4271  // Check new flavour structure. If multiple Ws
4272  // are still present, do not check flavour yet.
4273  if (flavCounts[24] <= 1 && !checkFlavour(flavCounts,
4274  flavRad, flavRadBefs[j], 1))
4275  continue;
4276 
4277  clus.push_back( Clustering(emtTag, iRad, iRec, iRec,
4278  pTLund(event, iRad, emtTag, iRec, pTdef, flavRadBefs[j]),
4279  flavRadBefs[j], -1 ) );
4280  }
4281  }
4282  }
4283  }
4284  }
4285  }
4286  }
4287  }
4288 
4289  // Search for initial shower histories.
4290  for (int a = 0;a < int(posInitPartn.size()); ++a) {
4291  int iRad = posInitPartn[a];
4292  int flavRad = event[iRad].id();
4293  // Only allow quarks and leptons to radiate weak bosons.
4294  if (event[iRad].isQuark() || event[iRad].isLepton()) {
4295 
4296  // Spin information.
4297  int spinRad = event[iRad].intPol();
4298  if (spinRad == -1 || spinRad == 9 || spinRad == 0) {
4299 
4300  // Check if the W+- matches that of the quark/lepton.
4301  int flavExp = (flavRad > 0) ? 24 : -24;
4302  if (abs(flavRad) % 2 == 1) flavExp = -flavExp;
4303  if (flavExp == flavEmt) {
4304  // Find possible flavours that the W cam change the quark to.
4305  vector<int> flavRadBefs = posFlavCKM(flavRad);
4306 
4307  // Change to anti particles if radiator is an anti particle.
4308  if (flavRad < 0)
4309  for (int i = 0;i < int(flavRadBefs.size()); ++i)
4310  flavRadBefs[i] = - flavRadBefs[i];
4311 
4312  // Loop through final partons and try to find matching flavours.
4313  for ( int i = 0; i < int(posInitPartn.size()); ++i ) {
4314  int iRec = posInitPartn[i];
4315 
4316  // Check for particle overlaps.
4317  if ( i != a && iRec != emtTag) {
4318  for (int j = 0;j < int(flavRadBefs.size()); ++j) {
4319 
4320  // Check new flavour structure.
4321  // If multiple Ws are still present, do not check flavour yet.
4322  if (flavCounts[24] <= 1 && !checkFlavour(flavCounts,
4323  flavRad, flavRadBefs[j], -1))
4324  continue;
4325  clus.push_back( Clustering(emtTag, iRad, iRec, iRec,
4326  pTLund(event, iRad, emtTag, iRec, -1, flavRadBefs[j]),
4327  flavRadBefs[j], -1 ) );
4328  }
4329  }
4330  }
4331  }
4332  }
4333  }
4334  }
4335 
4336  // Done
4337  return clus;
4338 }
4339 
4340 //--------------------------------------------------------------------------
4341 
4342 // Function to construct (rad,rec,emt) triples from the event
4343 // IN int : Position of Emitted in event record for which
4344 // dipoles should be constructed
4345 // int : Colour topogy to be tested
4346 // 1= g -> qqbar, causing 2 -> 2 dipole splitting
4347 // 2= q(bar) -> q(bar) g && g -> gg,
4348 // causing a 2 -> 3 dipole splitting
4349 // Event : event record to be checked for ptential partners
4350 // OUT vector of all allowed radiator+recoiler+emitted triples
4351 
4352 vector<Clustering> History::findEWTripleZ ( int emtTagIn, const Event& event,
4353  vector<int> posFinalPartn, vector<int> posInitPartn ) {
4354  // Copy input parton tag
4355  int emtTag = emtTagIn;
4356 
4357  // Copy input colour topology tag
4358  // (1: g --> qqbar splitting present, 2:rest)
4359 
4360  // Initialise FinalSize
4361  int finalSize = int(posFinalPartn.size());
4362  int initSize = int(posInitPartn.size());
4363 
4364  // Store flavour count to check if the new flavour configuration is valid.
4365  vector<int> flavCounts(30,0);
4366 
4367  for ( int a = 0; a < finalSize; ++a ) {
4368  if (event[posFinalPartn[a]].idAbs() < 20) {
4369  int sign = 1;
4370  if (event[posFinalPartn[a]].id() < 0)
4371  sign = -1;
4372  flavCounts[event[posFinalPartn[a]].idAbs()] += sign;
4373  }
4374  if (event[posFinalPartn[a]].idAbs() == 24)
4375  flavCounts[24]++;
4376  }
4377 
4378  for ( int a = 0; a < initSize; ++a ) {
4379  if (event[posInitPartn[a]].idAbs() < 20) {
4380  int sign = 1;
4381  if (event[posInitPartn[a]].id() < 0)
4382  sign = -1;
4383  flavCounts[event[posInitPartn[a]].idAbs()] -= sign;
4384  }
4385  }
4386 
4387  vector<Clustering> clus;
4388  // Add Z reclusterings.
4389 
4390  // Search final partons to find partons colour-connected to
4391  // event[EmtTag], choose radiator, then choose recoiler
4392  for ( int a = 0; a < finalSize; ++a ) {
4393 
4394  int iRad = posFinalPartn[a];
4395  if (iRad != emtTag) {
4396  int pTdef = 1;
4397  // Find recoiler by flavour.
4398  int flavRad = event[iRad].id();
4399 
4400  // Only allow quark and leptons to be radiators.
4401  if (event[iRad].isQuark() || event[iRad].isLepton())
4402  // Loop through final partons and try to find matching flavours.
4403  for ( int i = 0; i < finalSize; ++i ) {
4404  int iRec = posFinalPartn[i];
4405 
4406  // Check for particle overlaps.
4407  if ( iRec != iRad && iRec != emtTag ) {
4408  // Check new flavour structure.
4409  // If multiple Ws are still present, do not check flavour yet.
4410  if (flavCounts[24] <= 1 && !checkFlavour(flavCounts,
4411  flavRad, flavRad, 1))
4412  continue;
4413 
4414  clus.push_back( Clustering(emtTag, iRad, iRec, iRec,
4415  pTLund(event, iRad, emtTag, iRec, pTdef, flavRad),
4416  flavRad, -1 ) );
4417  }
4418  }
4419  }
4420  }
4421 
4422  // Search for initial shower histories.
4423  for (int a = 0;a < int(posInitPartn.size()); ++a) {
4424  int iRad = posInitPartn[a];
4425  int flavRad = event[iRad].id();
4426  // Only allow quarks and leptons to radiate weak bosons.
4427  if (event[iRad].isQuark() || event[iRad].isLepton()) {
4428 
4429  // Loop through initial partons and try to find matching flavours.
4430  for ( int i = 0; i < int(posInitPartn.size()); ++i ) {
4431  int iRec = posInitPartn[i];
4432 
4433  // Check for particle overlaps.
4434  if ( i != a && iRec != emtTag) {
4435  // Check new flavour structure.
4436  // If multiple Ws are still present, do not check flavour yet.
4437  if (flavCounts[24] <= 1 && !checkFlavour(flavCounts,
4438  flavRad, flavRad, -1))
4439  continue;
4440  clus.push_back( Clustering(emtTag, iRad, iRec, iRec,
4441  pTLund(event, iRad, emtTag, iRec, -1, flavRad),
4442  flavRad, -1 ) );
4443  }
4444  }
4445  }
4446  }
4447 
4448  // Done
4449  return clus;
4450 }
4451 
4452 //--------------------------------------------------------------------------
4453 
4454 // For the history-defining state (and if necessary interfering
4455 // states), find all possible clusterings.
4456 // NO INPUT
4457 // OUT vector of all (rad,rec,emt) systems
4458 
4459 vector<Clustering> History::getAllSQCDClusterings() {
4460  vector<Clustering> ret;
4461 
4462  // Get all clusterings for input state
4463  vector<Clustering> systems;
4464  systems = getSQCDClusterings(state);
4465  ret.insert(ret.end(), systems.begin(), systems.end());
4466  // Done
4467  return ret;
4468 }
4469 
4470 //--------------------------------------------------------------------------
4471 
4472 // For one given state, find all possible clusterings.
4473 // IN Event : state to be investigated
4474 // OUT vector of all (rad,rec,emt) systems in the state
4475 
4476 vector<Clustering> History::getSQCDClusterings( const Event& event) {
4477  vector<Clustering> ret;
4478 
4479  // Initialise vectors to keep track of position of partons in the
4480  // input event
4481  vector <int> posFinalPartn;
4482  vector <int> posInitPartn;
4483 
4484  vector <int> posFinalGluon;
4485  vector <int> posFinalQuark;
4486  vector <int> posFinalAntiq;
4487  vector <int> posInitGluon;
4488  vector <int> posInitQuark;
4489  vector <int> posInitAntiq;
4490 
4491  // Search event record for final state particles and store these in
4492  // quark, anti-quark and gluon vectors
4493  for (int i=0; i < event.size(); ++i)
4494  if ( event[i].isFinal() && event[i].colType() !=0 ) {
4495  // Store final partons
4496  posFinalPartn.push_back(i);
4497  if ( event[i].id() == 21 || event[i].id() == 1000021)
4498  posFinalGluon.push_back(i);
4499  else if ( (event[i].idAbs() < 10 && event[i].id() > 0)
4500  || (event[i].idAbs() < 1000010 && event[i].idAbs() > 1000000
4501  && event[i].id() > 0)
4502  || (event[i].idAbs() < 2000010 && event[i].idAbs() > 2000000
4503  && event[i].id() > 0))
4504  posFinalQuark.push_back(i);
4505  else if ( (event[i].idAbs() < 10 && event[i].id() < 0)
4506  || (event[i].idAbs() < 1000010 && event[i].idAbs() > 1000000
4507  && event[i].id() < 0)
4508  || (event[i].idAbs() < 2000010 && event[i].idAbs() > 2000000
4509  && event[i].id() < 0))
4510  posFinalAntiq.push_back(i);
4511  } else if ( event[i].status() == -21 && event[i].colType() != 0 ) {
4512  // Store initial partons
4513  posInitPartn.push_back(i);
4514  if ( event[i].id() == 21 || event[i].id() == 1000021)
4515  posInitGluon.push_back(i);
4516  else if ( (event[i].idAbs() < 10 && event[i].id() > 0)
4517  || (event[i].idAbs() < 1000010 && event[i].idAbs() > 1000000
4518  && event[i].id() > 0)
4519  || (event[i].idAbs() < 2000010 && event[i].idAbs() > 2000000
4520  && event[i].id() > 0))
4521  posInitQuark.push_back(i);
4522  else if ( (event[i].idAbs() < 10 && event[i].id() < 0)
4523  || (event[i].idAbs() < 1000010 && event[i].idAbs() > 1000000
4524  && event[i].id() < 0)
4525  || (event[i].idAbs() < 2000010 && event[i].idAbs() > 2000000
4526  && event[i].id() < 0))
4527  posInitAntiq.push_back(i);
4528  }
4529 
4530  int nFiGluon = int(posFinalGluon.size());
4531  int nFiQuark = int(posFinalQuark.size());
4532  int nFiAntiq = int(posFinalAntiq.size());
4533  int nInGluon = int(posInitGluon.size());
4534  int nInQuark = int(posInitQuark.size());
4535  int nInAntiq = int(posInitAntiq.size());
4536  vector<Clustering> systems;
4537 
4538  // Find rad + emt + rec systems:
4539  // (1) Start from gluon and find all (rad,rec,emt=gluon) triples
4540  for (int i = 0; i < nFiGluon; ++i) {
4541  int EmtGluon = posFinalGluon[i];
4542  systems = findSQCDTriple( EmtGluon, 2, event, posFinalPartn, posInitPartn);
4543  ret.insert(ret.end(), systems.begin(), systems.end());
4544  systems.resize(0);
4545  }
4546 
4547  // For more than one quark-antiquark pair in final state, check for
4548  // g -> qqbar splittings
4549  bool check_g2qq = true;
4550  if ( ( ( nInQuark + nInAntiq == 0 )
4551  && (nInGluon == 0)
4552  && (nFiQuark == 1) && (nFiAntiq == 1) )
4553  || ( ( nFiQuark + nFiAntiq == 0)
4554  && (nInQuark == 1) && (nInAntiq == 1) ) )
4555  check_g2qq = false;
4556 
4557  if ( check_g2qq ) {
4558 
4559  // (2) Start from quark and find all (rad,rec,emt=quark) triples
4560  // ( when g -> q qbar occured )
4561  for( int i=0; i < nFiQuark; ++i) {
4562  int EmtQuark = posFinalQuark[i];
4563  systems = findSQCDTriple( EmtQuark,1,event, posFinalPartn, posInitPartn);
4564  ret.insert(ret.end(), systems.begin(), systems.end());
4565  systems.resize(0);
4566  }
4567 
4568  // (3) Start from anti-quark and find all (rad,rec,emt=anti-quark)
4569  // triples ( when g -> q qbar occured )
4570  for( int i=0; i < nFiAntiq; ++i) {
4571  int EmtAntiq = posFinalAntiq[i];
4572  systems = findSQCDTriple( EmtAntiq,1,event, posFinalPartn, posInitPartn);
4573  ret.insert(ret.end(), systems.begin(), systems.end());
4574  systems.resize(0);
4575  }
4576 
4577  }
4578 
4579  return ret;
4580 }
4581 
4582 //--------------------------------------------------------------------------
4583 
4584 // Function to construct (rad,rec,emt) triples from the event
4585 // IN int : Position of Emitted in event record for which
4586 // dipoles should be constructed
4587 // int : Colour topogy to be tested
4588 // 1= g -> qqbar, causing 2 -> 2 dipole splitting
4589 // 2= q(bar) -> q(bar) g && g -> gg,
4590 // causing a 2 -> 3 dipole splitting
4591 // Event : event record to be checked for ptential partners
4592 // OUT vector of all allowed radiator+recoiler+emitted triples
4593 
4594 vector<Clustering> History::findSQCDTriple (int EmtTagIn, int colTopIn,
4595  const Event& event,
4596  vector<int> posFinalPartn,
4597  vector <int> posInitPartn ) {
4598 
4599  // Copy input parton tag
4600  int EmtTag = EmtTagIn;
4601  // Copy input colour topology tag
4602  // (1: g --> qqbar splitting present, 2:rest)
4603  int colTop = colTopIn;
4604 
4605  // PDG numbering offset for squarks
4606  int offsetL = 1000000;
4607  int offsetR = 2000000;
4608 
4609  // Initialise FinalSize
4610  int finalSize = int(posFinalPartn.size());
4611  int initSize = int(posInitPartn.size());
4612  int size = initSize + finalSize;
4613 
4614  vector<Clustering> clus;
4615 
4616  // Search final partons to find partons colour-connected to
4617  // event[EmtTag], choose radiator, then choose recoiler
4618  for ( int a = 0; a < size; ++a ) {
4619  int i = (a < finalSize)? a : (a - finalSize) ;
4620  int iRad = (a < finalSize)? posFinalPartn[i] : posInitPartn[i];
4621 
4622  if ( event[iRad].col() == event[EmtTag].col()
4623  && event[iRad].acol() == event[EmtTag].acol() )
4624  continue;
4625 
4626  // Save radiator flavour.
4627  int radID = event[iRad].id();
4628  // Remember if radiator is BSM.
4629  bool isSQCDrad = (abs(radID) > offsetL);
4630  // Remember if emitted is BSM.
4631  bool isSQCDemt = (event[EmtTag].idAbs() > offsetL );
4632 
4633  if (iRad != EmtTag ) {
4634  int pTdef = event[iRad].isFinal() ? 1 : -1;
4635  int sign = (a < finalSize)? 1 : -1 ;
4636 
4637  // Disalllow clusterings resulting in an initial state sQCD parton!
4638  int radBefID = getRadBeforeFlav(iRad,EmtTag,event);
4639  if ( pTdef == -1 && abs(radBefID) > offsetL ) continue;
4640 
4641  // First colour topology: g --> qqbar. Here, emt & rad should
4642  // have same flavour (causes problems for gamma->qqbar).
4643  if (colTop == 1) {
4644 
4645  int radSign = (event[iRad].id() < 0) ? -1 : 1;
4646  int emtSign = (event[EmtTag].id() < 0) ? -1 : 1;
4647 
4648  // Final gluino splitting.
4649  bool finalSplitting = false;
4650  if ( abs(radID) < 10
4651  && radSign*(abs(radID)+offsetL) == -sign*event[EmtTag].id() )
4652  finalSplitting = true;
4653  if ( abs(radID) < 10
4654  && radSign*(abs(radID)+offsetR) == -sign*event[EmtTag].id() )
4655  finalSplitting = true;
4656  if ( abs(radID) > offsetL && abs(radID) < offsetL+10
4657  && radID == -sign*emtSign*( event[EmtTag].idAbs() + offsetL) )
4658  finalSplitting = true;
4659  if ( abs(radID) > offsetR && abs(radID) < offsetR+10
4660  && radID == -sign*emtSign*( event[EmtTag].idAbs() + offsetR) )
4661  finalSplitting = true;
4662 
4663  // Initial gluon splitting.
4664  bool initialSplitting = false;
4665  if ( radID == 21 && ( ( event[EmtTag].idAbs() > offsetL
4666  && event[EmtTag].idAbs() < offsetL+10)
4667  || ( event[EmtTag].idAbs() > offsetR
4668  && event[EmtTag].idAbs() < offsetR+10) )
4669  && ( event[iRad].col() == event[EmtTag].col()
4670  || event[iRad].acol() == event[EmtTag].acol() ) )
4671  initialSplitting = true;
4672 
4673  if ( finalSplitting ) {
4674 
4675  int col = -1;
4676  int acl = -1;
4677  if ( radID < 0 && event[iRad].colType() == -1) {
4678  acl = event[EmtTag].acol();
4679  col = event[iRad].acol();
4680  } else if ( event[iRad].colType() == 1 ) {
4681  col = event[EmtTag].col();
4682  acl = event[iRad].col();
4683  }
4684 
4685  // Recoiler
4686  int iRec = 0;
4687  // Colour partner
4688  int iPartner = 0;
4689 
4690  if (col > 0) {
4691  // Find recoiler by colour
4692  iRec = FindCol(col,iRad,EmtTag,event,1,true);
4693  // In initial state splitting has final state colour partner,
4694  // Save both partner and recoiler
4695  if ( (sign < 0) && (event[iRec].isFinal()) ) {
4696  // Save colour recoiler
4697  iPartner = iRec;
4698  // Reset kinematic recoiler to initial state parton
4699  for(int l = 0; l < int(posInitPartn.size()); ++l)
4700  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
4701  // For final state splittings, colour partner and recoiler are
4702  // identical
4703  } else {
4704  iPartner = iRec;
4705  }
4706 
4707  // Not interested in pure QCD triples here.
4708  if ( !isSQCDrad && !isSQCDemt
4709  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4710  iRec = 0;
4711 
4712  if ( iRec != 0 && iPartner != 0
4713  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
4714  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
4715  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4716  continue;
4717  }
4718 
4719  // Reset partner
4720  iPartner = 0;
4721  // Find recoiler by colour
4722  iRec = FindCol(col,iRad,EmtTag,event,2,true);
4723  // In initial state splitting has final state colour partner,
4724  // Save both partner and recoiler
4725  if ( (sign < 0) && (event[iRec].isFinal()) ) {
4726  // Save colour recoiler
4727  iPartner = iRec;
4728  // Reset kinematic recoiler to initial state parton
4729  for(int l = 0; l < int(posInitPartn.size()); ++l)
4730  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
4731  // For final state splittings, colour partner and recoiler are
4732  // identical
4733  } else {
4734  iPartner = iRec;
4735  }
4736 
4737  // Not interested in pure QCD triples here.
4738  if ( !isSQCDrad && !isSQCDemt
4739  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4740  iRec = 0;
4741 
4742  if ( iRec != 0 && iPartner != 0
4743  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
4744  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
4745  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4746  continue;
4747  }
4748  }
4749 
4750  if (acl > 0) {
4751 
4752  // Reset partner
4753  iPartner = 0;
4754  // Find recoiler by colour
4755  iRec = FindCol(acl,iRad,EmtTag,event,1,true);
4756  // In initial state splitting has final state colour partner,
4757  // Save both partner and recoiler
4758  if ( (sign < 0) && (event[iRec].isFinal()) ) {
4759  // Save colour recoiler
4760  iPartner = iRec;
4761  // Reset kinematic recoiler to initial state parton
4762  for(int l = 0; l < int(posInitPartn.size()); ++l)
4763  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
4764  // For final state splittings, colour partner and recoiler are
4765  // identical
4766  } else {
4767  iPartner = iRec;
4768  }
4769 
4770  // Not interested in pure QCD triples here.
4771  if ( !isSQCDrad && !isSQCDemt
4772  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4773  iRec = 0;
4774 
4775  if ( iRec != 0 && iPartner != 0
4776  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
4777  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
4778  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4779  continue;
4780  }
4781 
4782  // Reset partner
4783  iPartner = 0;
4784  // Find recoiler by colour
4785  iRec = FindCol(acl,iRad,EmtTag,event,2,true);
4786  // In initial state splitting has final state colour partner,
4787  // Save both partner and recoiler
4788  if ( (sign < 0) && (event[iRec].isFinal()) ) {
4789  // Save colour recoiler
4790  iPartner = iRec;
4791  // Reset kinematic recoiler to initial state parton
4792  for(int l = 0; l < int(posInitPartn.size()); ++l)
4793  if (posInitPartn[l] != iRad) iRec = posInitPartn[l];
4794  // For final state splittings, colour partner and recoiler are
4795  // identical
4796  } else {
4797  iPartner = iRec;
4798  }
4799 
4800  // Not interested in pure QCD triples here.
4801  if ( !isSQCDrad && !isSQCDemt
4802  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4803  iRec = 0;
4804 
4805  if ( iRec != 0 && iPartner != 0
4806  && allowedClustering( iRad, EmtTag, iRec, iPartner, event) ) {
4807  attachClusterings (clus, EmtTag, iRad, iRec, iPartner,
4808  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4809  continue;
4810  }
4811  }
4812  // Initial gluon splitting
4813  } else if ( initialSplitting ) {
4814 
4815  // SM splittings already taken care of in findQCDTriple.
4816  if ( !isSQCDrad && !isSQCDemt ) continue;
4817 
4818  // For an initial state radiator, always set recoiler
4819  // to the other initial state parton (recoil is taken
4820  // by full remaining system, so this is just a
4821  // labelling for such a process)
4822  int RecInit = 0;
4823  for(int l = 0; l < int(posInitPartn.size()); ++l)
4824  if (posInitPartn[l] != iRad) RecInit = posInitPartn[l];
4825 
4826  // Find the colour connected partner
4827  // Find colour index of radiator before splitting
4828  int col = getRadBeforeCol(iRad, EmtTag, event);
4829  int acl = getRadBeforeAcol(iRad, EmtTag, event);
4830 
4831  // Find the correct partner: If a colour line has split,
4832  // the partner is connected to the radiator before the splitting
4833  // by a colour line (same reasoning for anticolour). The colour
4834  // that split is the colour appearing twice in the
4835  // radiator + emitted pair.
4836  // Thus, if we remove a colour index with the clustering,
4837  // we should look for a colour partner, else look for
4838  // an anticolour partner
4839  int colRemove = (event[iRad].col() == event[EmtTag].col())
4840  ? event[iRad].col() : 0;
4841 
4842  int iPartner = 0;
4843  if (colRemove > 0 && col > 0)
4844  iPartner = FindCol(col,iRad,EmtTag,event,1,true)
4845  + FindCol(col,iRad,EmtTag,event,2,true);
4846  else if (colRemove > 0 && acl > 0)
4847  iPartner = FindCol(acl,iRad,EmtTag,event,1,true)
4848  + FindCol(acl,iRad,EmtTag,event,2,true);
4849 
4850  if ( allowedClustering( iRad, EmtTag, RecInit, iPartner, event ) ) {
4851  attachClusterings (clus, EmtTag, iRad, RecInit, iPartner,
4852  pTLund(event, iRad, EmtTag, RecInit, pTdef), event);
4853  continue;
4854  }
4855  }
4856 
4857  // Second colour topology: Gluino emission
4858 
4859  } else {
4860 
4861  if ( (event[iRad].col() == event[EmtTag].acol())
4862  || (event[iRad].acol() == event[EmtTag].col())
4863  || (event[iRad].col() == event[EmtTag].col())
4864  || (event[iRad].acol() == event[EmtTag].acol()) ) {
4865  // For the rest, choose recoiler to have a common colour
4866  // tag with radiator, while not being the "Emitted"
4867 
4868  int col = -1;
4869  int acl = -1;
4870 
4871  if (event[iRad].isFinal() ) {
4872 
4873  if ( radID < 0 && event[iRad].colType() == -1) {
4874  acl = event[EmtTag].acol();
4875  col = event[iRad].col();
4876  } else if ( radID > 0 && event[iRad].colType() == 1 ) {
4877  col = event[EmtTag].col();
4878  acl = event[iRad].acol();
4879  } else {
4880  col = event[iRad].col();
4881  acl = event[iRad].acol();
4882  }
4883 
4884  int iRec = 0;
4885  if (col > 0) {
4886  iRec = FindCol(col,iRad,EmtTag,event,1,true);
4887  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4888  // Not interested in pure QCD triples here.
4889  if ( !isSQCDrad && !isSQCDemt
4890  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4891  iRec = 0;
4892  if (iRec != 0
4893  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4894  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4895  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4896  continue;
4897  }
4898 
4899  iRec = FindCol(col,iRad,EmtTag,event,2,true);
4900  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4901  // Not interested in pure QCD triples here.
4902  if ( !isSQCDrad && !isSQCDemt
4903  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4904  iRec = 0;
4905  if (iRec != 0
4906  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4907  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4908  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4909  continue;
4910  }
4911  }
4912 
4913  if (acl > 0) {
4914  iRec = FindCol(acl,iRad,EmtTag,event,1,true);
4915  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4916  // Not interested in pure QCD triples here.
4917  if ( !isSQCDrad && !isSQCDemt
4918  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4919  iRec = 0;
4920  if (iRec != 0
4921  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4922  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4923  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4924  continue;
4925  }
4926 
4927  iRec = FindCol(acl,iRad,EmtTag,event,2,true);
4928  if ( (sign < 0) && (event[iRec].isFinal()) ) iRec = 0;
4929  // Not interested in pure QCD triples here.
4930  if ( !isSQCDrad && !isSQCDemt
4931  && (event[iRec].idAbs() < 10 || event[iRec].id() == 21) )
4932  iRec = 0;
4933  if (iRec != 0
4934  && allowedClustering( iRad, EmtTag, iRec, iRec, event) ) {
4935  attachClusterings (clus, EmtTag, iRad, iRec, iRec,
4936  pTLund(event, iRad, EmtTag, iRec, pTdef), event);
4937  continue;
4938  }
4939  }
4940 
4941  } else {
4942 
4943  // SM splittings already taken care of in findQCDTriple. Since
4944  // initial state splittings will not know if the true
4945  // colour-connected recoiler is a SM particle, any ISR splitting
4946  // of a SM particle will be included by findQCDTriple. To not
4947  // include the same splitting twice, continue for SM ISR radiator
4948  if ( !isSQCDrad || !isSQCDemt ) continue;
4949 
4950  // For an initial state radiator, always set recoiler
4951  // to the other initial state parton (recoil is taken
4952  // by full remaining system, so this is just a
4953  // labelling for such a process)
4954  int RecInit = 0;
4955  int iPartner = 0;
4956  for(int l = 0; l < int(posInitPartn.size()); ++l)
4957  if (posInitPartn[l] != iRad) RecInit = posInitPartn[l];
4958 
4959  // Find the colour connected partner
4960  // Find colour index of radiator before splitting
4961  col = getRadBeforeCol(iRad, EmtTag, event);
4962  acl = getRadBeforeAcol(iRad, EmtTag, event);
4963 
4964  // Find the correct partner: If a colour line has split,
4965  // the partner is connected to the radiator before the splitting
4966  // by a colour line (same reasoning for anticolour). The colour
4967  // that split is the colour appearing twice in the
4968  // radiator + emitted pair.
4969  // Thus, if we remove a colour index with the clustering,
4970  // we should look for a colour partner, else look for
4971  // an anticolour partner
4972  int colRemove = (event[iRad].col() == event[EmtTag].col())
4973  ? event[iRad].col() : 0;
4974  iPartner = (colRemove > 0)
4975  ? FindCol(col,iRad,EmtTag,event,1,true)
4976  + FindCol(col,iRad,EmtTag,event,2,true)
4977  : FindCol(acl,iRad,EmtTag,event,1,true)
4978  + FindCol(acl,iRad,EmtTag,event,2,true);
4979 
4980  if ( allowedClustering( iRad, EmtTag, RecInit, iPartner, event)) {
4981  attachClusterings (clus, EmtTag, iRad, RecInit, iPartner,
4982  pTLund(event, iRad, EmtTag, RecInit, pTdef), event);
4983  continue;
4984  }
4985  }
4986  }
4987  }
4988  }
4989  }
4990 
4991  // Done
4992  return clus;
4993 
4994 }
4995 
4996 //--------------------------------------------------------------------------
4997 
4998 // Calculate and return the probability of a clustering.
4999 // IN Clustering : rad,rec,emt - System for which the splitting
5000 // probability should be calcuated
5001 // OUT splitting probability
5002 
5003 double History::getProb(const Clustering & SystemIn) {
5004 
5005  // Get local copies of input system
5006  int Rad = SystemIn.emittor;
5007  int Rec = SystemIn.recoiler;
5008  int Emt = SystemIn.emitted;
5009 
5010  // Initialise shower probability
5011  double showerProb = 0.0;
5012 
5013  // If the splitting resulted in disallowed evolution variable,
5014  // disallow the splitting
5015  if (SystemIn.pT() <= 0.) return 0.;
5016 
5017  // Initialise all combinatorical factors
5018  double CF = 4./3.;
5019  double CA = 3.;
5020  // Flavour is known when reclustring, thus n_f=1
5021  double TR = 1./2.;
5022 
5023  // Split up in FSR and ISR
5024  bool isFSR = (state[Rad].isFinal() && state[Rec].isFinal());
5025  bool isFSRinREC = (state[Rad].isFinal() && !state[Rec].isFinal());
5026  bool isISR = !state[Rad].isFinal();
5027 
5028  // Check if external splitting probability should be used.
5029  if ( mergingHooksPtr->useShowerPlugin() ) {
5030  int iPartner = (isISR && SystemIn.partner > 0) ? SystemIn.partner : Rec;
5031 
5032  double pr = 0.;
5033  bool isFSR2 = showers->timesPtr->isTimelike(state, Rad, Emt, iPartner, "");
5034  if (isFSR2) {
5035  string name = showers->timesPtr->getSplittingName( state, Rad, Emt,
5036  iPartner).front();
5037  pr = showers->timesPtr->getSplittingProb( state, Rad, Emt,
5038  iPartner, name);
5039  } else {
5040  string name = showers->spacePtr->getSplittingName(state, Rad, Emt,
5041  iPartner).front();
5042  pr = showers->spacePtr->getSplittingProb(state, Rad, Emt,
5043  iPartner, name);
5044  }
5045  return abs(pr);
5046  }
5047 
5048  // Check if this is the clustering 2->3 to 2->2.
5049  // If so, use weight for joined evolution
5050  int nFinal = 0;
5051  for(int i=0; i < state.size(); ++i)
5052  if (state[i].isFinal()) nFinal++;
5053  bool isLast = (nFinal == (mergingHooksPtr->hardProcess->nQuarksOut()
5054  +mergingHooksPtr->hardProcess->nLeptonOut()+1));
5055 
5056  // Do not calculate splitting functions for electroweak emissions
5057  bool isElectroWeak = (state[Emt].idAbs() == 23 || state[Emt].idAbs() == 24);
5058 
5059  if (isISR) {
5060  // Find incoming particles
5061 
5062  int inP = 0;
5063  int inM = 0;
5064  for(int i=0;i< int(state.size()); ++i) {
5065  if (state[i].mother1() == 1) inP = i;
5066  if (state[i].mother1() == 2) inM = i;
5067  }
5068  // Construct dipole mass, eCM and sHat = x1*x2*s
5069  Vec4 sum = state[Rad].p() + state[Rec].p() - state[Emt].p();
5070  double m2Dip = sum.m2Calc();
5071  double sHat = (state[inM].p() + state[inP].p()).m2Calc();
5072  // Energy fraction z=E_q1/E_qi in branch q(i)q(2) -> q(1)g(3)q(2)
5073  double z1 = m2Dip / sHat;
5074  // Virtuality of the splittings
5075  Vec4 Q1( state[Rad].p() - state[Emt].p() );
5076  Vec4 Q2( state[Rec].p() - state[Emt].p() );
5077  // Q^2 for emission off radiator line
5078  double Q1sq = -Q1.m2Calc();
5079  // pT^2 for emission off radiator line
5080  double pT1sq = pow2(pTLund(state, Rad, Emt, Rec, -1, SystemIn.flavRadBef));
5081  // Remember if massive particles involved: Mass corrections for
5082  // to g->QQ and Q->Qg splittings
5083  bool g2QQmassive = mergingHooksPtr->includeMassive()
5084  && state[Rad].id() == 21
5085  && ( (state[Emt].idAbs() >= 4 && state[Emt].idAbs() < 7)
5086  || (state[Emt].idAbs() > 1000000 && state[Emt].idAbs() < 1000010 )
5087  || (state[Emt].idAbs() > 2000000 && state[Emt].idAbs() < 2000010 ));
5088  bool Q2Qgmassive = mergingHooksPtr->includeMassive()
5089  && state[Emt].id() == 21
5090  && ( (state[Rad].idAbs() >= 4 && state[Rad].idAbs() < 7)
5091  || (state[Rad].idAbs() > 1000000 && state[Rad].idAbs() < 1000010 )
5092  || (state[Rad].idAbs() > 2000000 && state[Rad].idAbs() < 2000010 ));
5093  bool isMassive = mergingHooksPtr->includeMassive()
5094  && ( g2QQmassive || Q2Qgmassive
5095  || state[Emt].id() == 1000021);
5096  double m2Emt0 = state[Emt].p().m2Calc();
5097  double m2Rad0 = pow(particleDataPtr->m0(state[Rad].id()),2);
5098 
5099  // Correction of virtuality for massive splittings
5100  if ( g2QQmassive) Q1sq += m2Emt0;
5101  else if (Q2Qgmassive) Q1sq += m2Rad0;
5102 
5103  // pT0 dependence!!!
5104  double pT0sq = pow(mergingHooksPtr->pT0ISR(),2);
5105  double Q2sq = -Q2.m2Calc();
5106 
5107  // Correction of virtuality of other splitting
5108  bool g2QQmassiveRec = mergingHooksPtr->includeMassive()
5109  && state[Rec].id() == 21
5110  && ( state[Emt].idAbs() >= 4 && state[Emt].idAbs() < 7);
5111  bool Q2QgmassiveRec = mergingHooksPtr->includeMassive()
5112  && state[Emt].id() == 21
5113  && ( state[Rec].idAbs() >= 4 && state[Rec].idAbs() < 7);
5114  double m2Rec0 = pow(particleDataPtr->m0(state[Rec].id()),2);
5115  if ( g2QQmassiveRec) Q2sq += m2Emt0;
5116  else if (Q2QgmassiveRec) Q2sq += m2Rec0;
5117 
5118  bool hasJoinedEvol = (state[Emt].id() == 21
5119  || state[Rad].id() == state[Rec].id());
5120 
5121  // Initialise normalization factor multiplying the splitting
5122  // function numerator
5123  double fac = 1.;
5124  if ( mergingHooksPtr->pickByFull() || mergingHooksPtr->pickBySumPT()) {
5125  double facJoined = ( Q2sq + pT0sq/(1.-z1) )
5126  * 1./(Q1sq*Q2sq + pT0sq*sHat + pow(pT0sq/(1.-z1),2));
5127  double facSingle = mergingHooksPtr->nonJoinedNorm()*1./( pT1sq + pT0sq);
5128 
5129  fac = (hasJoinedEvol && isLast) ? facJoined : facSingle;
5130 
5131  } else if (mergingHooksPtr->pickByPoPT2()) {
5132  fac = 1./(pT1sq + pT0sq);
5133  } else {
5134  string message="Error in History::getProb: Scheme for calculating";
5135  message+=" shower splitting probability is undefined.";
5136  infoPtr->errorMsg(message);
5137  }
5138 
5139  // Calculate shower splitting probability:
5140  // Splitting functions*normalization*ME reweighting factors
5141 
5142  if ( isElectroWeak ) {
5143 
5144  // For electroweak splittings, the probabilities depend on the
5145  // full shower history, and can hence not be calculated correctly
5146  // here. Thus, use dummy value and "dress" with full probabilities
5147  // later, once the path is known.
5148  showerProb = 1.;
5149 
5150  // Calculate branching probability for q -> q g
5151  } else if ( state[Emt].id() == 21 && state[Rad].id() != 21) {
5152  // Find splitting kernel
5153  double num = CF*(1. + pow(z1,2)) / (1.-z1);
5154  if (isMassive) num -= CF * z1 * (1.-z1) * (m2Rad0/pT1sq);
5155 
5156  // Find ME reweighting factor
5157  double meReweighting = 1.;
5158  // Find the number of final state coloured particles, apart
5159  // from those coming from the hard process
5160  int nCol = 0;
5161  for(int i=0; i < state.size(); ++i)
5162  if (state[i].isFinal() && state[i].colType() != 0
5163  && !mergingHooksPtr->hardProcess->matchesAnyOutgoing(i,state) )
5164  nCol++;
5165  // For first splitting of single vector boson production,
5166  // apply ME corrections
5167  if (nCol == 1
5168  && int(mergingHooksPtr->hardProcess->hardIntermediate.size()) == 1) {
5169  double sH = m2Dip / z1;
5170  double tH = -Q1sq;
5171  double uH = Q1sq - m2Dip * (1. - z1) / z1;
5172  double misMatch = (uH*tH - (uH + tH)*pT0sq/(1.-z1)
5173  + pow(pT0sq/(1.-z1),2) ) / (uH*tH);
5174  meReweighting *= (tH*tH + uH*uH + 2. * m2Dip * sH)
5175  / (sH*sH + m2Dip*m2Dip);
5176  meReweighting *= misMatch;
5177  }
5178  // Multiply factors
5179  showerProb = num*fac*meReweighting;
5180 
5181  // Calculate branching probability for g -> g g
5182  } else if ( state[Emt].id() == 21 && state[Rad].id() == 21) {
5183  // Calculate splitting kernel
5184  double num = 2.*CA*pow2(1. - z1*(1.-z1)) / (z1*(1.-z1));
5185 
5186  // Include ME reweighting for higgs!!
5187  // Find ME reweighting factor
5188  double meReweighting = 1.;
5189  // Find the number of final state coloured particles, apart
5190  // from those coming from the hard process
5191  int nCol = 0;
5192  for(int i=0; i < state.size(); ++i)
5193  if (state[i].isFinal() && state[i].colType() != 0
5194  && !mergingHooksPtr->hardProcess->matchesAnyOutgoing(i,state) )
5195  nCol++;
5196  // For first splitting of single vector boson production,
5197  // apply ME corrections
5198  if ( nCol == 1
5199  && mergingHooksPtr->getProcessString().compare("pp>h") == 0
5200  && int(mergingHooksPtr->hardProcess->hardIntermediate.size()) == 1 ) {
5201  double sH = m2Dip / z1;
5202  double tH = -Q1sq;
5203  double uH = Q1sq - m2Dip * (1. - z1) / z1;
5204  meReweighting *= 0.5
5205  * (pow4(sH) + pow4(tH) + pow4(uH) + pow4(m2Dip))
5206  / pow2(sH*sH - m2Dip * (sH - m2Dip));
5207  }
5208 
5209  // Multiply factors
5210  showerProb = num*fac*meReweighting;
5211 
5212  // Calculate branching probability for q -> g q
5213  } else if ( state[Emt].id() != 21 && state[Rad].id() != 21 ) {
5214  // Calculate splitting kernel
5215  double num = CF*(1. + pow2(1.-z1)) / z1;
5216 
5217  // Include ME reweighting for higgs!!
5218  // Find ME reweighting factor
5219  double meReweighting = 1.;
5220  // Find the number of final state coloured particles, apart
5221  // from those coming from the hard process
5222  int nCol = 0;
5223  for ( int i=0; i < state.size(); ++i )
5224  if ( state[i].isFinal() && state[i].colType() != 0
5225  && !mergingHooksPtr->hardProcess->matchesAnyOutgoing(i,state) )
5226  nCol++;
5227  // For first splitting of single vector boson production,
5228  // apply ME corrections
5229  if (nCol == 1
5230  && mergingHooksPtr->getProcessString().compare("pp>h") == 0
5231  && int(mergingHooksPtr->hardProcess->hardIntermediate.size()) == 1) {
5232  double sH = m2Dip / z1;
5233  double uH = Q1sq - m2Dip * (1. - z1) / z1;
5234  meReweighting *= (sH*sH + uH*uH)
5235  / (sH*sH + pow2(sH -m2Dip));
5236  }
5237 
5238  // Multiply factors
5239  showerProb = num*fac*meReweighting;
5240 
5241  // Calculate branching probability for g -> q qbar
5242  } else if ( state[Emt].id() != 21 && state[Rad].id() == 21 ) {
5243  // Calculate splitting kernel
5244  double num = TR * ( pow(z1,2) + pow(1.-z1,2) );
5245  if (isMassive) num += TR * 2.*z1*(1.-z1)*(m2Emt0/pT1sq);
5246  // Calculate ME reweighting factor
5247  double meReweighting = 1.;
5248  // Find the number of final state coloured particles, apart
5249  // from those coming from the hard process
5250  int nCol = 0;
5251  for ( int i=0; i < state.size(); ++i )
5252  if ( state[i].isFinal() && state[i].colType() != 0
5253  && !mergingHooksPtr->hardProcess->matchesAnyOutgoing(i,state) )
5254  nCol++;
5255  // For first splitting of single vector boson production,
5256  // apply ME corrections
5257  if (nCol == 1
5258  && int(mergingHooksPtr->hardProcess->hardIntermediate.size()) == 1) {
5259  double sH = m2Dip / z1;
5260  double tH = -Q1sq;
5261  double uH = Q1sq - m2Dip * (1. - z1) / z1;
5262  swap( tH, uH);
5263  double misMatch = ( uH - pT0sq/(1.-z1) ) / uH;
5264  double me = (sH*sH + uH*uH + 2. * m2Dip * tH)
5265  / (pow2(sH - m2Dip) + m2Dip*m2Dip);
5266  // Weight with me/overestimate
5267  meReweighting *= me;
5268  meReweighting *= misMatch;
5269  }
5270  // Multiply factors
5271  showerProb = num*fac*meReweighting;
5272 
5273  // Print error if no kernel calculated
5274  } else {
5275  string message = "Error in History::getProb: Splitting kernel"
5276  " undefined in ISR in clustering.";
5277  infoPtr->errorMsg(message);
5278  }
5279 
5280  // If corrected pT below zero in ISR, put probability to zero
5281  double m2Sister = pow(state[Emt].m(),2);
5282  double pT2corr = (Q1sq - z1*(m2Dip + Q1sq)*(Q1sq + m2Sister)/m2Dip);
5283  if (pT2corr < 0.) showerProb = 0.0;
5284 
5285  // If creating heavy quark by Q -> gQ then next need g -> Q + Qbar.
5286  // So minimum total mass2 is 4 * m2Sister, but use more to be safe.
5287  if ( state[Emt].id() == state[Rad].id()
5288  && ( state[Rad].idAbs() == 4 || state[Rad].idAbs() == 5 )) {
5289  double m2QQsister = 2.*4.*m2Sister;
5290  double pT2QQcorr = Q1sq - z1*(m2Dip + Q1sq)*(Q1sq + m2QQsister)
5291  / m2Dip;
5292 
5293  if (pT2QQcorr < 0.0) showerProb = 0.0;
5294  }
5295 
5296  // Check cuts on momentum fraction.
5297  double pT2minNow = mergingHooksPtr->pTcut();
5298  double zMaxAbs = 1. - 0.5 * (pT2minNow / m2Dip) *
5299  ( sqrt( 1. + 4. * m2Dip / pT2minNow ) - 1. );
5300  zMaxAbs = min(1.,zMaxAbs);
5301  double zMinAbs = max(0.,1. - zMaxAbs);
5302 
5303  // Massive z limit.
5304  int radBefID = getRadBeforeFlav(Rad, Emt, state);
5305  if ( abs(radBefID) == 4 || abs(radBefID) == 5 ) {
5306  double m2Massive = pow2(particleDataPtr->m0(radBefID));
5307  double mRatio = sqrt( m2Massive / m2Dip );
5308  double zMaxMassive = (1. - mRatio) / ( 1. + mRatio * (1. - mRatio) );
5309  zMaxAbs = min(zMaxAbs, zMaxMassive);
5310  }
5311 
5312  if (z1 < zMinAbs || z1 > zMaxAbs) showerProb = 0.0;
5313 
5314  if (mergingHooksPtr->includeRedundant()) {
5315  // Initialise the spacelike shower alpha_S
5316  AlphaStrong* asISR = mergingHooksPtr->AlphaS_ISR();
5317  double as = (*asISR).alphaS(pT1sq + pT0sq) / (2.*M_PI);
5318  // Multiply with alpha_S
5319  showerProb *= as;
5320  }
5321 
5322  // Done for ISR case, begin FSR case
5323 
5324  } else if (isFSR || isFSRinREC) {
5325 
5326  // Construct dipole mass
5327  int recSign = (state[Rec].isFinal()) ? 1 : -1;
5328  Vec4 sum = state[Rad].p() + recSign*state[Rec].p() + state[Emt].p();
5329  double m2Dip = abs(sum.m2Calc());
5330 
5331  // Virtuality of the splittings
5332  Vec4 Q1( state[Rad].p() + state[Emt].p() );
5333  Vec4 Q2( state[Rec].p() + state[Emt].p() );
5334 
5335  // Get z value.
5336  double z1 = getCurrentZ( Rad, Rec, Emt, SystemIn.flavRadBef);
5337 
5338  // Q^2 for emission off radiator line
5339  double Q1sq = Q1.m2Calc();
5340  // pT^2 for emission off radiator line
5341  double pT1sq = pow(SystemIn.pT(),2);
5342  // Q^2 for emission off recoiler line
5343  double Q2sq = Q2.m2Calc();
5344 
5345  // Remember if radiator or recoiler is massive.
5346  bool isMassiveRad = ( state[Rad].idAbs() >= 4
5347  && state[Rad].id() != 21 );
5348  bool isMassiveRec = ( state[Rec].idAbs() >= 4
5349  && state[Rec].id() != 21 );
5350 
5351  // Correction of virtuality for massive splittings.
5352  double m2Rad0 = pow(particleDataPtr->m0(state[Rad].id()),2);
5353  double m2Rec0 = pow(particleDataPtr->m0(state[Rec].id()),2);
5354  if ( mergingHooksPtr->includeMassive() && isMassiveRad ) Q1sq -= m2Rad0;
5355  if ( mergingHooksPtr->includeMassive() && isMassiveRec ) Q2sq -= m2Rec0;
5356 
5357  // Initialise normalization factor multiplying the splitting
5358  // function numerator
5359  double fac = 1.;
5360  if ( mergingHooksPtr->pickByFull() || mergingHooksPtr->pickBySumPT()) {
5361  double facJoined = (1.-z1)/Q1sq * m2Dip/( Q1sq + Q2sq );
5362  double facSingle = mergingHooksPtr->fsrInRecNorm() * 1./ pT1sq;
5363  fac = (!isFSRinREC && isLast) ? facJoined : facSingle;
5364 
5365  } else if (mergingHooksPtr->pickByPoPT2()) {
5366  fac = 1. / pT1sq;
5367  } else {
5368  string message="Error in History::getProb: Scheme for calculating";
5369  message+=" shower splitting probability is undefined.";
5370  infoPtr->errorMsg(message);
5371  }
5372  // Calculate shower splitting probability:
5373  // Splitting functions*normalization*ME reweighting factors
5374 
5375  if ( isElectroWeak ) {
5376 
5377  // For electroweak splittings, the probabilities depend on the
5378  // full shower history, and can hence not be calculated correctly
5379  // here. Thus, use dummy value and "dress" with full probabilities
5380  // later, once the path is known.
5381  showerProb = 1.;
5382 
5383  // Calculate branching probability for g -> g_1 g_2
5384  } else if ( state[Emt].id() == 21 && state[Rad].colType() == 2) {
5385 
5386  // Calculate splitting kernel
5387  double num = 0.5* CA * (1. + pow3(z1)) / (1.-z1);
5388  // Multiply factors
5389  showerProb = num*fac;
5390 
5391  // Calculate branching probability for q -> q g with quark recoiler
5392  } else if ( state[Emt].id() == 21
5393  && state[Rad].colType() != 2 && state[Rec].colType() != 2) {
5394  // For a qqbar dipole in FSR, ME corrections exist and the
5395  // splitting function "z-weight" is set to 1.0 (only for 2->2 ??)
5396  double num = CF * 2./(1.-z1);
5397  // Find the number of final state coloured particles, apart
5398  // from those coming from the hard process
5399  int nCol = 0;
5400  for(int i=0; i < state.size(); ++i)
5401  if (state[i].isFinal() && state[i].colType() != 0
5402  && !mergingHooksPtr->hardProcess->matchesAnyOutgoing(i,state) )
5403  nCol++;
5404  // Calculate splitting kernel
5405  if ( nCol > 3
5406  || int(mergingHooksPtr->hardProcess->hardIntermediate.size()) > 1)
5407  num = CF * (1. + pow2(z1)) /(1.-z1);
5408  // Calculate ME reweighting factor
5409  double meReweighting = 1.;
5410  // Correct if this is the process created by the first
5411  // FSR splitting of a 2->2 process
5412  if ( nCol == 3
5413  && int(mergingHooksPtr->hardProcess->hardIntermediate.size()) == 1 ) {
5414  // Construct 2->3 variables for FSR
5415  double x1 = 2. * (sum * state[Rad].p()) / m2Dip;
5416  double x2 = 2. * (sum * state[Rec].p()) / m2Dip;
5417  double prop1 = max(1e-12, 1. - x1);
5418  double prop2 = max(1e-12, 1. - x2);
5419  double x3 = max(1e-12, 2. - x1 - x2);
5420  // Calculate the ME reweighting factor
5421  double ShowerRate1 = 2./( x3 * prop2 );
5422  double meDividingFactor1 = prop1 / x3;
5423  double me = (pow(x1,2) + pow(x2,2))/(prop1*prop2);
5424  meReweighting = meDividingFactor1 * me / ShowerRate1;
5425  }
5426  // Multiply factors
5427  showerProb = num*fac*meReweighting;
5428 
5429  // Calculate branching probability for q -> q g with gluon recoiler
5430  } else if ( state[Emt].id() == 21 && state[Rad].colType() != 2
5431  && state[Rec].colType() == 2 ) {
5432  // For qg /qbarg dipoles, the splitting function is
5433  // calculated and not weighted by a ME correction factor
5434  // Shower splitting function
5435  double num = CF * (1. + pow2(z1)) / (1.-z1);
5436  showerProb = num*fac;
5437 
5438  // Calculate branching probability for g -> q qbar
5439  } else if ( state[Emt].id() != 21 ) {
5440  // Get flavour of quark / antiquark
5441  int flavour = state[Emt].id();
5442  // Get correct masses for the quarks
5443  // (needed to calculate splitting function?)
5444  double mFlavour = particleDataPtr->m0(flavour);
5445  // Get mass of quark/antiquark pair
5446  double mDipole = m(state[Rad].p(), state[Emt].p());
5447  // Factor determining if gluon decay was allowed
5448  double beta = sqrtpos( 1. - 4.*pow2(mFlavour)/pow2(mDipole) );
5449  // Shower splitting function
5450  double num = 0.5*TR * ( z1*z1 + (1.-z1)*(1.-z1) );
5451  if (beta <= 0.) beta = 0.;
5452 
5453  showerProb = num*fac*beta;
5454 
5455  // Print error if no kernel calculated
5456  } else {
5457  string message="Error in History::getProb: Splitting kernel undefined";
5458  message+=" in FSR clustering.";
5459  infoPtr->errorMsg(message);
5460  }
5461 
5462  if (mergingHooksPtr->includeRedundant()) {
5463  // Initialise the spacelike shower alpha_S
5464  AlphaStrong* asFSR = mergingHooksPtr->AlphaS_FSR();
5465  double as = (*asFSR).alphaS(pT1sq) / (2.*M_PI);
5466  // Multiply with alpha_S
5467  showerProb *= as;
5468  }
5469 
5470  double m2DipCorr = pow2(sqrt(m2Dip) - sqrt(m2Rec0)) - m2Rad0;
5471  double zMin = 0.5 - sqrtpos( 0.25 - pT1sq / m2DipCorr );
5472  double m2 = m2Rad0 + 2. * state[Rad].p()*state[Emt].p();
5473  bool keepMassive = (z1 > zMin && z1 < 1. - zMin
5474  && m2 * m2Dip < z1 * (1. - z1) * pow2(m2Dip + m2 - m2Rec0) );
5475  // No emission probability outside disallowed z range.
5476  if (!keepMassive) showerProb *= 0.0;
5477 
5478  // Done for FSR
5479  } else {
5480  string message="Error in History::getProb: Radiation could not be";
5481  message+=" interpreted as FSR or ISR.";
5482  infoPtr->errorMsg(message);
5483  }
5484 
5485  if (showerProb <= 0.) showerProb = 0.;
5486 
5487  if (mergingHooksPtr->doWeakClustering()) {
5488 
5489  // Every time we assign spin, equivalent QCD histories are counted twice,
5490  // due to distingushing between quark spins.
5491  // To remove this overcounting, multiply by QCD probabilities by 1/2,
5492  // for each quark spin that has been assigned.
5493  double factor = 1.;
5494  if (state[Rad].idAbs() < 10 && state[Rad].intPol() == 9
5495  && SystemIn.spinRad != 9) factor *= 0.5;
5496  if (state[Emt].idAbs() < 10 && state[Emt].intPol() == 9
5497  && SystemIn.spinEmt != 9) factor *= 0.5;
5498  if (state[Rec].idAbs() < 10 && state[Rec].intPol() == 9
5499  && SystemIn.spinRec != 9) factor *= 0.5;
5500  if ( state[Emt].colType() != 0 ) {
5501  showerProb *= factor;
5502  }
5503 
5504  // The g -> q qbar splitting leads to two histories, not four.
5505  // The previous clustering steps overcounted the number of histories to be
5506  // four, leading to a too small probability of a path by 1/2. Correct!
5507  if ( state[Emt].idAbs() < 10 && state[Rad].colType() != 0) {
5508  showerProb *= 2.0;
5509  }
5510 
5511  }
5512 
5513  // Done
5514  return showerProb;
5515 
5516 }
5517 
5518 //--------------------------------------------------------------------------
5519 
5520 // Set up the beams (fill the beam particles with the correct
5521 // current incoming particles) to allow calculation of splitting
5522 // probability.
5523 // For interleaved evolution, set assignments dividing PDFs into
5524 // sea and valence content. This assignment is, until a history path
5525 // is chosen and a first trial shower performed, not fully correct
5526 // (since content is chosen form too high x and too low scale). The
5527 // assignment used for reweighting will be corrected after trial
5528 // showering
5529 
5530 void History::setupBeams() {
5531 
5532  // Do nothing for empty event, possible if sequence of
5533  // clusterings was ill-advised in that it results in
5534  // colour-disconnected states
5535  if (state.size() < 4) return;
5536  // Do nothing for e+e- beams
5537  if ( state[3].colType() == 0 ) return;
5538  if ( state[4].colType() == 0 ) return;
5539 
5540  // Incoming partons to hard process are stored in slots 3 and 4.
5541  int inS = 0;
5542  int inP = 0;
5543  int inM = 0;
5544  for(int i=0;i< int(state.size()); ++i) {
5545  if (state[i].mother1() == 1) inP = i;
5546  if (state[i].mother1() == 2) inM = i;
5547  }
5548 
5549  // Save some info before clearing beams
5550  // Mothers of incoming partons companion code
5551  int motherPcompRes = -1;
5552  int motherMcompRes = -1;
5553 
5554  bool sameFlavP = false;
5555  bool sameFlavM = false;
5556 
5557  if (mother) {
5558  int inMotherP = 0;
5559  int inMotherM = 0;
5560  for(int i=0;i< int(mother->state.size()); ++i) {
5561  if (mother->state[i].mother1() == 1) inMotherP = i;
5562  if (mother->state[i].mother1() == 2) inMotherM = i;
5563  }
5564  sameFlavP = (state[inP].id() == mother->state[inMotherP].id());
5565  sameFlavM = (state[inM].id() == mother->state[inMotherM].id());
5566 
5567  motherPcompRes = (sameFlavP) ? beamA[0].companion() : -2;
5568  motherMcompRes = (sameFlavM) ? beamB[0].companion() : -2;
5569  }
5570 
5571  // Append the current incoming particles to the beam
5572  beamA.clear();
5573  beamB.clear();
5574 
5575  // Get energy of incoming particles
5576  double Ep = 2. * state[inP].e();
5577  double Em = 2. * state[inM].e();
5578 
5579  // If incoming partons are massive then recalculate to put them massless.
5580  if (state[inP].m() != 0. || state[inM].m() != 0.) {
5581  Ep = state[inP].pPos() + state[inM].pPos();
5582  Em = state[inP].pNeg() + state[inM].pNeg();
5583  }
5584 
5585  // Add incoming hard-scattering partons to list in beam remnants.
5586  double x1 = Ep / state[inS].m();
5587  beamA.append( inP, state[inP].id(), x1);
5588  double x2 = Em / state[inS].m();
5589  beamB.append( inM, state[inM].id(), x2);
5590 
5591  // Scale. For ME multiplicity history, put scale to mu_F
5592  // (since sea/valence quark content is chosen from this scale)
5593  double scalePDF = (mother) ? scale : infoPtr->QFac();
5594  // Find whether incoming partons are valence or sea. Store.
5595  // Can I do better, e.g. by setting the scale to the hard process
5596  // scale (= M_W) or by replacing one of the x values by some x/z??
5597  beamA.xfISR( 0, state[inP].id(), x1, scalePDF*scalePDF);
5598  if (!mother) {
5599  beamA.pickValSeaComp();
5600  } else {
5601  beamA[0].companion(motherPcompRes);
5602  }
5603  beamB.xfISR( 0, state[inM].id(), x2, scalePDF*scalePDF);
5604  if (!mother) {
5605  beamB.pickValSeaComp();
5606  } else {
5607  beamB[0].companion(motherMcompRes);
5608  }
5609 
5610 }
5611 
5612 //--------------------------------------------------------------------------
5613 
5614 // Calculate the PDF ratio used in the argument of the no-emission
5615 // probability
5616 
5617 double History::pdfForSudakov() {
5618 
5619  // Do nothing for e+e- beams
5620  if ( state[3].colType() == 0 ) return 1.0;
5621  if ( state[4].colType() == 0 ) return 1.0;
5622 
5623  // Check if splittings was ISR or FSR
5624  bool FSR = ( mother->state[clusterIn.emittor].isFinal()
5625  && mother->state[clusterIn.recoiler].isFinal());
5626  bool FSRinRec = ( mother->state[clusterIn.emittor].isFinal()
5627  && !mother->state[clusterIn.recoiler].isFinal());
5628 
5629  // Done for pure FSR
5630  if (FSR) return 1.0;
5631 
5632  int iInMother = (FSRinRec)? clusterIn.recoiler : clusterIn.emittor;
5633  // Find side of event that was reclustered
5634  int side = ( mother->state[iInMother].pz() > 0 ) ? 1 : -1;
5635 
5636  int inP = 0;
5637  int inM = 0;
5638  for(int i=0;i< int(state.size()); ++i) {
5639  if (state[i].mother1() == 1) inP = i;
5640  if (state[i].mother1() == 2) inM = i;
5641  }
5642 
5643  // Save mother id
5644  int idMother = mother->state[iInMother].id();
5645  // Find daughter position and id
5646  int iDau = (side == 1) ? inP : inM;
5647  int idDaughter = state[iDau].id();
5648  // Get mother x value
5649  double xMother = 2. * mother->state[iInMother].e() / mother->state[0].e();
5650  // Get daughter x value of daughter
5651  double xDaughter = 2.*state[iDau].e() / state[0].e(); // x1 before isr
5652 
5653  // Calculate pdf ratio
5654  double ratio = getPDFratio(side, true, false, idMother, xMother, scale,
5655  idDaughter, xDaughter, scale);
5656 
5657  // For FSR with incoming recoiler, maximally return 1.0, as
5658  // is done in Pythia::TimeShower.
5659  // For ISR, return ratio
5660  return ( (FSRinRec)? min(1.,ratio) : ratio);
5661 }
5662 
5663 //--------------------------------------------------------------------------
5664 
5665 // Calculate the hard process matrix element to include in the selection
5666 // probability.
5667 
5668 double History::hardProcessME( const Event& event ) {
5669 
5670  // Calculate prob for Drell-Yan process.
5671  if (isEW2to1(event)) {
5672 
5673  // qqbar -> W.
5674  if (event[5].idAbs() == 24) {
5675  int idIn1 = event[3].id();
5676  int idIn2 = event[4].id();
5677  double mW = particleDataPtr->m0(24);
5678  double gW = particleDataPtr->mWidth(24) / mW;
5679  double sH = (event[3].p()+event[4].p()).m2Calc();
5680 
5681  double thetaWRat = 1. / (12. * coupSMPtr->sin2thetaW());
5682  double ckmW = coupSMPtr->V2CKMid(abs(idIn1), abs(idIn2));
5683 
5684  double bwW = 12. * M_PI / ( pow2(sH - pow2(mW)) + pow2(sH * gW) );
5685  double preFac = thetaWRat * sqrt(sH) * particleDataPtr->mWidth(24);
5686  return ckmW * preFac * bwW;
5687  }
5688  // qqbar -> Z. No interference with gamma included.
5689  else if (event[5].idAbs() == 23) {
5690  double mZ = particleDataPtr->m0(23);
5691  double gZ = particleDataPtr->mWidth(23) / mZ;
5692  double sH = (event[3].p()+event[4].p()).m2Calc();
5693 
5694  double thetaZRat = (pow2(coupSMPtr->rf( abs(clusterIn.flavRadBef))) +
5695  pow2(coupSMPtr->lf( abs(clusterIn.flavRadBef)))) /
5696  (24. * coupSMPtr->sin2thetaW() * coupSMPtr->cos2thetaW());
5697  double bwW = 12. * M_PI / ( pow2(sH - pow2(mZ)) + pow2(sH * gZ) );
5698  double preFac = thetaZRat * sqrt(sH) * particleDataPtr->mWidth(23);
5699  return preFac * bwW;
5700  }
5701  else {
5702  string message="Warning in History::hardProcessME: Only Z/W are";
5703  message+=" supported as 2->1 processes. Skipping history.";
5704  infoPtr->errorMsg(message);
5705  return 0;
5706  }
5707  }
5708  // 2 to 2 process, assume QCD.
5709  else if (isQCD2to2(event)) {
5710  int idIn1 = event[3].id();
5711  int idIn2 = event[4].id();
5712  int idOut1 = event[5].id();
5713  int idOut2 = event[6].id();
5714 
5715  double sH = (event[3].p()+event[4].p()).m2Calc();
5716  double tH = (event[3].p()-event[5].p()).m2Calc();
5717  double uH = (event[3].p()-event[6].p()).m2Calc();
5718 
5719  // Verify that it is QCD.
5720  bool isQCD = true;
5721  if (!(abs(idIn1) < 10 || abs(idIn1) == 21) ) isQCD = false;
5722  if (!(abs(idIn2) < 10 || abs(idIn2) == 21) ) isQCD = false;
5723  if (!(abs(idOut1) < 10 || abs(idOut1) == 21) ) isQCD = false;
5724  if (!(abs(idOut2) < 10 || abs(idOut2) == 21) ) isQCD = false;
5725 
5726  // Overall phase-space constant (dsigma/dcos(theta)).
5727  double cor = M_PI / (9. * pow2(sH));
5728 
5729  // If it is QCD calculate cross section.
5730  if (isQCD) {
5731  // Find out which 2->2 process it is.
5732 
5733  // incoming gluon pair.
5734  if (abs(idIn1) == 21 && abs(idIn2) == 21) {
5735  if (abs(idOut1) == 21 && abs(idOut2) == 21)
5736  return cor * weakShowerMEs.getMEgg2gg(sH, tH, uH);
5737  else return cor * weakShowerMEs.getMEgg2qqbar(sH, tH, uH);
5738 
5739  // Incoming single gluon
5740  } else if (abs(idIn1) == 21 || abs(idIn2) == 21) {
5741  if (idIn1 != idOut1) swap(uH, tH);
5742  return cor * weakShowerMEs.getMEqg2qg(sH, tH, uH);
5743  }
5744 
5745  // Incoming quarks
5746  else {
5747  if (abs(idOut1) == 21 && abs(idOut2) == 21)
5748  return cor * weakShowerMEs.getMEqqbar2gg(sH, tH, uH);
5749  if (idIn1 == -idIn2) {
5750  if (abs(idIn1) == abs(idOut1)) {
5751  if (idIn1 != idOut1) swap(uH, tH);
5752  return cor * weakShowerMEs.getMEqqbar2qqbar(sH, tH, uH, true);
5753  }
5754  else {
5755  return cor * weakShowerMEs.getMEqqbar2qqbar(sH, tH, uH, false);
5756  }
5757  }
5758  else if (idIn1 == idIn2)
5759  return cor * weakShowerMEs.getMEqq2qq(sH, tH, uH, true);
5760  else {
5761  if (idIn1 == idOut1) swap(uH,tH);
5762  return cor * weakShowerMEs.getMEqq2qq(sH, tH, uH, false);
5763  }
5764  }
5765  }
5766  }
5767 
5768 
5769  // Get hard process.
5770  string process = mergingHooksPtr->getProcessString();
5771  double result = 1.;
5772 
5773  if ( process.compare("pp>e+ve") == 0
5774  || process.compare("pp>e-ve~") == 0
5775  || process.compare("pp>LEPTONS,NEUTRINOS") == 0 ) {
5776  // Do nothing for incomplete process.
5777  int nFinal = 0;
5778  for ( int i=0; i < int(event.size()); ++i )
5779  if ( event[i].isFinal() ) nFinal++;
5780  if ( nFinal != 2 ) return 1.;
5781  // Get W-boson mass and width.
5782  double mW = particleDataPtr->m0(24);
5783  double gW = particleDataPtr->mWidth(24) / mW;
5784  // Get incoming particles.
5785  int inP = (event[3].pz() > 0) ? 3 : 4;
5786  int inM = (event[3].pz() > 0) ? 4 : 3;
5787  // Get outgoing particles.
5788  int outP = 0;
5789  for ( int i=0; i < int(event.size()); ++i ) {
5790  if ( event[i].isFinal() && event[i].px() > 0 ) outP = i;
5791  }
5792  // Get Mandelstam variables.
5793  double sH = (event[inP].p() + event[inM].p()).m2Calc();
5794  double tH = (event[inP].p() - event[outP].p()).m2Calc();
5795  double uH = - sH - tH;
5796 
5797  // Return kinematic part of matrix element.
5798  result = ( 1. + (tH - uH)/sH ) / ( pow2(sH - mW*mW) + pow2(sH*gW) );
5799  } else
5800  result = mergingHooksPtr->hardProcessME(event);
5801 
5802  return result;
5803 
5804 }
5805 
5806 //--------------------------------------------------------------------------
5807 
5808 // Perform the clustering of the current state and return the
5809 // clustered state.
5810 // IN Clustering : rad,rec,emt triple to be clustered to two partons
5811 // OUT clustered state
5812 
5813 Event History::cluster( Clustering & inSystem ) {
5814 
5815  // Initialise tags of particles to be changed
5816  int Rad = inSystem.emittor;
5817  int Rec = inSystem.recoiler;
5818  int Emt = inSystem.emitted;
5819  // Initialise eCM,mHat
5820  double eCM = state[0].e();
5821  // Flags for type of radiation
5822  int radType = state[Rad].isFinal() ? 1 : -1;
5823  int recType = state[Rec].isFinal() ? 1 : -1;
5824 
5825  // Construct the clustered event
5826  Event NewEvent = Event();
5827  NewEvent.init("(hard process-modified)", particleDataPtr);
5828  NewEvent.clear();
5829 
5830  // Check if external clustering should be used.
5831  if ( mergingHooksPtr->useShowerPlugin() ) {
5832  int iPartner = (radType == -1 && inSystem.partner > 0)
5833  ? inSystem.partner : Rec;
5834  bool isFSR = showers->timesPtr->isTimelike(state, Rad, Emt, iPartner, "");
5835  if (isFSR) {
5836  string name = showers->timesPtr->getSplittingName(state,Rad,Emt,
5837  iPartner).front();
5838  return showers->timesPtr->clustered(state,Rad,Emt,iPartner,name);
5839  } else {
5840  string name = showers->spacePtr->getSplittingName(state,Rad,Emt,
5841  iPartner).front();
5842  return showers->spacePtr->clustered(state,Rad,Emt,iPartner,name);
5843  }
5844  }
5845 
5846  // Copy all unchanged particles to NewEvent
5847  for (int i = 0; i < state.size(); ++i)
5848  if ( i != Rad && i != Rec && i != Emt )
5849  NewEvent.append( state[i] );
5850 
5851  // Copy all the junctions one by one
5852  for (int i = 0; i < state.sizeJunction(); ++i)
5853  NewEvent.appendJunction( state.getJunction(i) );
5854  // Find an appropriate scale for the hard process
5855  double mu = choseHardScale(state);
5856  // Initialise scales for new event
5857  NewEvent.saveSize();
5858  NewEvent.saveJunctionSize();
5859  NewEvent.scale(mu);
5860  NewEvent.scaleSecond(mu);
5861 
5862  // Set properties of radiator/recoiler after the clustering
5863  // Recoiler properties will be unchanged
5864  Particle RecBefore = Particle( state[Rec] );
5865  RecBefore.setEvtPtr(&NewEvent);
5866  RecBefore.daughters(0,0);
5867  RecBefore.pol(inSystem.spinRec);
5868  // Find flavour of radiator before splitting
5869  int radID = inSystem.flavRadBef;
5870  if (radID == 0) radID = getRadBeforeFlav(Rad, Emt, state);
5871  int recID = state[Rec].id();
5872  Particle RadBefore = Particle( state[Rad] );
5873  RadBefore.setEvtPtr(&NewEvent);
5874  RadBefore.id(radID);
5875  RadBefore.pol(inSystem.spinRadBef);
5876  RadBefore.daughters(0,0);
5877  // Put dummy values for colours
5878  RadBefore.cols(RecBefore.acol(),RecBefore.col());
5879 
5880  // Reset status if the reclustered radiator is a resonance.
5881  if ( particleDataPtr->isResonance(radID) && radType == 1)
5882  RadBefore.status(state[Rad].status());
5883 
5884  // Put mass for radiator and recoiler
5885  double radMass = particleDataPtr->m0(radID);
5886  double recMass = particleDataPtr->m0(recID);
5887  if (radType == 1 ) RadBefore.m(radMass);
5888  else RadBefore.m(0.0);
5889  if (recType == 1 ) RecBefore.m(recMass);
5890  else RecBefore.m(0.0);
5891 
5892  // Construct momenta and colours of clustered particles
5893  // ISR/FSR splittings are treated differently
5894  if ( radType + recType == 2 && state[Emt].idAbs() != 23 &&
5895  state[Emt].idAbs() != 24) {
5896  // Clustering of final(rad)/final(rec) dipole splitting
5897  // Get eCM of (rad,rec,emt) triple
5898  Vec4 sum = state[Rad].p() + state[Rec].p() + state[Emt].p();
5899  double eCMME = sum.mCalc();
5900 
5901  // Define radiator and recoiler back-to-back in the dipole
5902  // rest frame [=(state[rad]+state[emt])+state[rec] rest frame]
5903  Vec4 Rad4mom;
5904  Vec4 Rec4mom;
5905  double mDsq = pow2(eCMME);
5906  // If possible, keep the invariant mass of the radiator.
5907  double mRsq = (radID == state[Rad].id() )
5908  ? abs(state[Rad].p().m2Calc())
5909  : pow2(particleDataPtr->m0(radID));
5910  double mSsq = abs(state[Rec].p().m2Calc());
5911  double a = 0.5*sqrt(mDsq);
5912  double b = 0.25*(mRsq - mSsq) / a;
5913  double c = sqrt(pow2(a) + pow2(b) - 2.*a*b - mSsq);
5914 
5915  Rad4mom.p( 0., 0., c, a+b);
5916  Rec4mom.p( 0., 0.,-c, a-b);
5917 
5918  // Find boost from Rad4mom+Rec4mom rest frame to event cm frame
5919  Vec4 old1 = Vec4(state[Rad].p() + state[Emt].p());
5920  Vec4 old2 = Vec4(state[Rec].p());
5921  RotBstMatrix fromCM;
5922  fromCM.fromCMframe(old1, old2);
5923  // Transform momenta
5924  Rad4mom.rotbst(fromCM);
5925  Rec4mom.rotbst(fromCM);
5926 
5927  RadBefore.p(Rad4mom);
5928  RecBefore.p(Rec4mom);
5929  RadBefore.m(sqrt(mRsq));
5930  RecBefore.m(sqrt(mSsq));
5931 
5932  } else if ( radType + recType == 2 && (state[Emt].idAbs() == 23 ||
5933  state[Emt].idAbs() == 24) ) {
5934  // Clustering of final(rad)/final(rec) dipole splitting
5935  // Get eCM of (rad,rec,emt) triple
5936  Vec4 sum = state[Rad].p() + state[Rec].p() + state[Emt].p();
5937  double eCMME = sum.mCalc();
5938 
5939  // Define radiator and recoiler back-to-back in the dipole
5940  // rest frame [=(state[rad]+state[emt])+state[rec] rest frame]
5941  Vec4 Rad4mom;
5942  Vec4 Rec4mom;
5943  double mDsq = pow2(eCMME);
5944  // If possible, keep the invariant mass of the radiator.
5945  double mRsq = (radID == state[Rad].id() )
5946  ? abs(state[Rad].p().m2Calc())
5947  : pow2(particleDataPtr->m0(radID));
5948  double mSsq = abs(state[Rec].p().m2Calc());
5949  double a = 0.5*sqrt(mDsq);
5950  double b = 0.25*(mRsq - mSsq) / a;
5951  double c = sqrt(pow2(a) + pow2(b) - 2.*a*b - mSsq);
5952 
5953  Rad4mom.p( 0., 0., c, a+b);
5954  Rec4mom.p( 0., 0.,-c, a-b);
5955 
5956  // Find boost from Rad4mom+Rec4mom rest frame to event cm frame
5957  Vec4 old1 = Vec4(state[Rad].p() + state[Emt].p());
5958  Vec4 old2 = Vec4(state[Rec].p());
5959  RotBstMatrix fromCM;
5960  fromCM.fromCMframe(old1, old2);
5961  // Transform momenta
5962  Rad4mom.rotbst(fromCM);
5963  Rec4mom.rotbst(fromCM);
5964 
5965  RadBefore.p(Rad4mom);
5966  RecBefore.p(Rec4mom);
5967  RadBefore.m(sqrt(mRsq));
5968  RecBefore.m(sqrt(mSsq));
5969 
5970  } else if ( radType + recType == 0 ) {
5971 
5972  // Clustering of final(rad)/initial(rec) dipole splitting
5973  // Get eCM of (rad,rec,emt) triple
5974  Vec4 sum = state[Rad].p() + state[Rec].p() + state[Emt].p();
5975  double eCMME = sum.mCalc();
5976  // Define radiator and recoiler back-to-back in the dipole
5977  // rest frame [=(state[rad]+state[emt])+state[rec] rest frame]
5978  Vec4 Rad4mom;
5979  Vec4 Rec4mom;
5980  double mDsq = pow2(eCMME);
5981  // If possible, keep the invariant mass of the radiator.
5982  double mRsq = (radID == state[Rad].id() )
5983  ? abs(state[Rad].p().m2Calc())
5984  : pow2(particleDataPtr->m0(radID));
5985  double mSsq = abs(state[Rec].p().m2Calc());
5986  double a = 0.5*sqrt(mDsq);
5987  double b = 0.25*(mRsq - mSsq) / a;
5988  double c = sqrt(pow2(a) + pow2(b) - 2.*a*b - mSsq);
5989 
5990  Rad4mom.p( 0., 0., c, a+b);
5991  Rec4mom.p( 0., 0.,-c, a-b);
5992 
5993  // Find boost from Rad4mom+Rec4mom rest frame to event cm frame
5994  Vec4 old1 = Vec4(state[Rad].p() + state[Emt].p());
5995  Vec4 old2 = Vec4(state[Rec].p());
5996  RotBstMatrix fromCM;
5997  fromCM.fromCMframe(old1, old2);
5998  // Transform momenta
5999  Rad4mom.rotbst(fromCM);
6000  Rec4mom.rotbst(fromCM);
6001 
6002  // Rescale recoiler momentum
6003  Rec4mom = 2.*state[Rec].p() - Rec4mom;
6004 
6005  // Ensure that recoiler is massless to
6006  // very good accuracy.
6007  if ( abs(Rec4mom.mCalc()) > 1e-7 ) {
6008  double pzSign = (Rec4mom.pz() > 0.) ? 1. : -1.;
6009  double eRec = Rec4mom.e();
6010  Rec4mom.p(0., 0., pzSign*eRec, eRec);
6011  }
6012 
6013  RadBefore.p(Rad4mom);
6014  RecBefore.p(Rec4mom);
6015  RadBefore.m(sqrt(mRsq));
6016 
6017  // Set mass of initial recoiler to zero
6018  RecBefore.m( 0.0 );
6019 
6020  } else {
6021 
6022  // Clustering of initial(rad)/initial(rec) dipole splitting
6023  // We want to cluster: Meaning doing the inverse of a process
6024  // ( pDaughter + pRecoiler -> pOut )
6025  // ==> ( pMother + pPartner -> pOut' + pSister )
6026  // produced by an initial state splitting. The matrix element
6027  // provides us with pMother, pPartner, pSister and pOut'
6028  Vec4 pMother( state[Rad].p() );
6029  Vec4 pSister( state[Emt].p() );
6030  Vec4 pPartner( state[Rec].p() );
6031  Vec4 pDaughterBef( 0.,0.,0.,0. );
6032  Vec4 pRecoilerBef( 0.,0.,0.,0. );
6033  Vec4 pDaughter( 0.,0.,0.,0. );
6034  Vec4 pRecoiler( 0.,0.,0.,0. );
6035 
6036  // Find side that radiates event (mother moving in sign * p_z direction).
6037  int sign = (state[Rad].pz() > 0.) ? 1 : -1;
6038 
6039  // Find rotation by phi that would have been done for a
6040  // splitting daughter -> mother + sister
6041  double phi = pSister.phi();
6042  // Find rotation with -phi
6043  RotBstMatrix rot_by_mphi;
6044  rot_by_mphi.rot(0.,-phi);
6045  // Find rotation with +phi
6046  RotBstMatrix rot_by_pphi;
6047  rot_by_pphi.rot(0.,phi);
6048 
6049  // Get mother and partner x values
6050  // x1 after isr
6051  double x1 = 2. * pMother.e() / eCM;
6052  // x2 after isr
6053  double x2 = 2. * pPartner.e() / eCM;
6054 
6055  // Find z of the splitting
6056  Vec4 qDip( pMother - pSister);
6057  Vec4 qAfter(pMother + pPartner);
6058  Vec4 qBefore(qDip + pPartner);
6059  double z = qBefore.m2Calc() / qAfter.m2Calc();
6060 
6061  // Calculate e_CM^2 before the splitting.
6062  double x1New = z*x1; // x1 before isr
6063  double x2New = x2; // x2 before isr
6064  double sHat = x1New*x2New*eCM*eCM;
6065 
6066  // Construct daughter and recoiler momenta before the splitting.
6067  // (Note: For final result, only needs to be boosted into
6068  // frame with unchanged "recoiler" momentum)
6069  pDaughterBef.p( 0., 0., sign*0.5*sqrt(sHat), 0.5*sqrt(sHat));
6070  pRecoilerBef.p( 0., 0., -sign*0.5*sqrt(sHat), 0.5*sqrt(sHat));
6071 
6072  // Rotate momenta defined in the lab frame by phi
6073  pMother.rotbst( rot_by_mphi );
6074  pSister.rotbst( rot_by_mphi );
6075  pPartner.rotbst( rot_by_mphi );
6076  for(int i=3; i< NewEvent.size(); ++i)
6077  NewEvent[i].rotbst( rot_by_mphi );
6078 
6079  // Find boost from lab frame to rest frame of
6080  // off-shell daughter + on-shell recoiler dipole
6081  pDaughter.p( pMother - pSister);
6082  pRecoiler.p( pPartner );
6083  RotBstMatrix from_CM_to_DRoff;
6084  if (sign == 1)
6085  from_CM_to_DRoff.toCMframe(pDaughter, pRecoiler);
6086  else
6087  from_CM_to_DRoff.toCMframe(pRecoiler, pDaughter);
6088 
6089  // Rotate and boost all momenta to rest frame of off-shell daughter +
6090  // on-shell recoiler dipole
6091  pMother.rotbst( from_CM_to_DRoff );
6092  pPartner.rotbst( from_CM_to_DRoff );
6093  pSister.rotbst( from_CM_to_DRoff );
6094  for(int i=3; i< NewEvent.size(); ++i)
6095  NewEvent[i].rotbst( from_CM_to_DRoff );
6096 
6097  // Find longitudinal boost from on-shell daughter + on-shell recoiler
6098  // dipole rest frame to the frame in which the recoiler momentum (x-value)
6099  // does not change in the splitting process.
6100  RotBstMatrix from_DR_to_CM;
6101  from_DR_to_CM.bst( 0., 0., sign*( x1New - x2New ) / ( x1New + x2New ) );
6102 
6103  // Boost all momenta into the "unchanged recoiler" frame, thereby
6104  // correcting for momentum mismatch by transferring the recoil to all
6105  // final state particles.
6106  pDaughterBef.rotbst( from_DR_to_CM );
6107  pRecoilerBef.rotbst( from_DR_to_CM );
6108  for(int i=3; i< NewEvent.size(); ++i)
6109  NewEvent[i].rotbst( from_DR_to_CM );
6110 
6111  // Transform outgoing momenta
6112  for(int i=3; i< NewEvent.size(); ++i)
6113  NewEvent[i].rotbst( rot_by_pphi );
6114 
6115  // Ensure that radiator and recoiler are massless to
6116  // very good accuracy.
6117  if ( abs(pRecoilerBef.mCalc()) > 1e-7 ) {
6118  double pzSign = (pRecoilerBef.pz() > 0.) ? 1. : -1.;
6119  double eRec = pRecoilerBef.e();
6120  pRecoilerBef.p(0., 0., pzSign*eRec, eRec);
6121  }
6122  if ( abs(pDaughterBef.mCalc()) > 1e-7 ) {
6123  double pzSign = (pDaughterBef.pz() > 0.) ? 1. : -1.;
6124  double eDau = pDaughterBef.e();
6125  pDaughterBef.p(0., 0., pzSign*eDau, eDau);
6126  }
6127  // Transform pMother and outgoing momenta
6128  // Set momenta of particles to be attached to new event record
6129  RecBefore.p( pRecoilerBef );
6130  RadBefore.p( pDaughterBef );
6131  if (RecBefore.pz() > 0.) RecBefore.mother1(1);
6132  else RecBefore.mother1(2);
6133  if (RadBefore.pz() > 0.) RadBefore.mother1(1);
6134  else RadBefore.mother1(2);
6135 
6136  }
6137 
6138  // Put some dummy production scales for RecBefore, RadBefore
6139  RecBefore.scale(mu);
6140  RadBefore.scale(mu);
6141 
6142  // Append new recoiler and find new radiator colour
6143  NewEvent.append(RecBefore);
6144 
6145  // Assign the correct colour to re-clustered radiator.
6146  // Keep old radiator colours for electroweak emission.
6147  int emtID = state[Emt].id();
6148  if ( emtID == 22 || emtID == 23 || abs(emtID) == 24 )
6149  RadBefore.cols( state[Rad].col(), state[Rad].acol() );
6150  // For QCD, carefully construct colour.
6151  else if ( !connectRadiator( RadBefore, radType, RecBefore, recType,
6152  NewEvent ) ) {
6153  // Could happen if previous clustering produced several colour
6154  // singlett subsystems in the event
6155  NewEvent.reset();
6156  return NewEvent;
6157  }
6158 
6159  // Build the clustered event
6160  Event outState = Event();
6161  outState.init("(hard process-modified)", particleDataPtr);
6162  outState.clear();
6163 
6164  // Copy system and incoming beam particles to outState
6165  for (int i = 0; i < 3; ++i)
6166  outState.append( NewEvent[i] );
6167  // Copy all the junctions one by one
6168  for (int i = 0; i < state.sizeJunction(); ++i)
6169  outState.appendJunction( state.getJunction(i) );
6170  // Initialise scales for new event
6171  outState.saveSize();
6172  outState.saveJunctionSize();
6173  outState.scale(mu);
6174  outState.scaleSecond(mu);
6175  bool radAppended = false;
6176  bool recAppended = false;
6177  int size = int(outState.size());
6178  // Save position of radiator and recoiler in new event record.
6179  int radPos = 0, recPos = 0;
6180 
6181  // Append first incoming particle
6182  if ( RecBefore.mother1() == 1) {
6183  recPos = outState.append( RecBefore );
6184  recAppended = true;
6185  } else if ( RadBefore.mother1() == 1 ) {
6186  radPos = outState.append( RadBefore );
6187  radAppended = true;
6188  } else {
6189  // Find second incoming in input event
6190  int in1 = 0;
6191  for(int i=0; i < int(state.size()); ++i)
6192  if (state[i].mother1() == 1) in1 =i;
6193  outState.append( state[in1] );
6194  size++;
6195  }
6196  // Append second incoming particle
6197  if ( RecBefore.mother1() == 2) {
6198  recPos = outState.append( RecBefore );
6199  recAppended = true;
6200  } else if ( RadBefore.mother1() == 2 ) {
6201  radPos = outState.append( RadBefore );
6202  radAppended = true;
6203  } else {
6204  // Find second incoming in input event
6205  int in2 = 0;
6206  for(int i=0; i < int(state.size()); ++i)
6207  if (state[i].mother1() == 2) in2 =i;
6208 
6209  outState.append( state[in2] );
6210  size++;
6211  }
6212 
6213  // Append new recoiler if not done already
6214  if (!recAppended && !RecBefore.isFinal()) {
6215  recAppended = true;
6216  recPos = outState.append( RecBefore);
6217  }
6218  // Append new radiator if not done already
6219  if (!radAppended && !RadBefore.isFinal()) {
6220  radAppended = true;
6221  radPos = outState.append( RadBefore);
6222  }
6223 
6224  // Append intermediate particle
6225  // (careful not to append reclustered recoiler)
6226  // Append intermediate particle
6227  // (careful not to append reclustered recoiler)
6228  for (int i = 0; i < int(NewEvent.size()-1); ++i)
6229  if (NewEvent[i].status() == -22) outState.append( NewEvent[i] );
6230  // Append final state particles, resonances first
6231  for (int i = 0; i < int(NewEvent.size()-1); ++i)
6232  if (NewEvent[i].status() == 22) outState.append( NewEvent[i] );
6233  // Then start appending partons
6234  if (!radAppended && RadBefore.statusAbs() == 22)
6235  radPos = outState.append(RadBefore);
6236  if (!recAppended)
6237  recPos = outState.append(RecBefore);
6238  if (!radAppended && RadBefore.statusAbs() != 22)
6239  radPos = outState.append(RadBefore);
6240  // Then partons (not reclustered recoiler)
6241  for(int i = 0; i < int(NewEvent.size()-1); ++i)
6242  if ( NewEvent[i].status() != 22
6243  && NewEvent[i].colType() != 0
6244  && NewEvent[i].isFinal())
6245  outState.append( NewEvent[i] );
6246  // Then the rest
6247  for(int i = 0; i < int(NewEvent.size()-1); ++i)
6248  if ( NewEvent[i].status() != 22
6249  && NewEvent[i].colType() == 0
6250  && NewEvent[i].isFinal() )
6251  outState.append( NewEvent[i]);
6252 
6253  // Find intermediate and respective daughters
6254  vector<int> posIntermediate;
6255  vector<int> posDaughter1;
6256  vector<int> posDaughter2;
6257  for(int i=0; i < int(outState.size()); ++i)
6258  if (outState[i].status() == -22) {
6259  posIntermediate.push_back(i);
6260  int d1 = outState[i].daughter1();
6261  int d2 = outState[i].daughter2();
6262  // Find daughters in output state
6263  int daughter1 = FindParticle( state[d1], outState);
6264  int daughter2 = FindParticle( state[d2], outState);
6265  // If both daughters found, done
6266  // Else put first final particle as first daughter
6267  // and last final particle as second daughter
6268  if (daughter1 > 0)
6269  posDaughter1.push_back( daughter1);
6270  else {
6271  daughter1 = 0;
6272  while(!outState[daughter1].isFinal() ) daughter1++;
6273  posDaughter1.push_back( daughter1);
6274  }
6275  if (daughter2 > 0)
6276  posDaughter2.push_back( daughter2);
6277  else {
6278  daughter2 = outState.size()-1;
6279  while(!outState[daughter2].isFinal() ) daughter2--;
6280  posDaughter2.push_back( daughter2);
6281  }
6282  }
6283  // Set daughters and mothers
6284  for(int i=0; i < int(posIntermediate.size()); ++i) {
6285  outState[posIntermediate[i]].daughters(posDaughter1[i],posDaughter2[i]);
6286  outState[posDaughter1[i]].mother1(posIntermediate[i]);
6287  outState[posDaughter2[i]].mother1(posIntermediate[i]);
6288  }
6289 
6290  // Find range of final state partons
6291  int minParFinal = int(outState.size());
6292  int maxParFinal = 0;
6293  for(int i=0; i < int(outState.size()); ++i)
6294  if (outState[i].mother1() == 3 && outState[i].mother2() == 4) {
6295  minParFinal = min(i,minParFinal);
6296  maxParFinal = max(i,maxParFinal);
6297  }
6298 
6299  if (minParFinal == maxParFinal) maxParFinal = 0;
6300  outState[3].daughters(minParFinal,maxParFinal);
6301  outState[4].daughters(minParFinal,maxParFinal);
6302 
6303  // Update event properties
6304  outState.saveSize();
6305  outState.saveJunctionSize();
6306 
6307  // Store radiator and recoiler positions.
6308  inSystem.recBef = recPos;
6309  inSystem.radBef = radPos;
6310 
6311  // Almost there...
6312  // If an intermediate coloured parton exists which was directly
6313  // colour connected to the radiator before the splitting, and the
6314  // radiator before and after the splitting had only one colour, problems
6315  // will arise since the colour of the radiator will be changed, whereas
6316  // the intermediate parton still has the old colour. In effect, this
6317  // means that when setting up a event for trial showering, one colour will
6318  // be free.
6319  // Hence, check for an intermediate coloured triplet resonance has been
6320  // colour-connected to the "old" radiator.
6321  // Find resonance
6322  int iColRes = 0;
6323  if ( radType == -1 && state[Rad].colType() == 1) {
6324  // Find resonance connected to initial colour
6325  for(int i=0; i < int(state.size()); ++i)
6326  if ( i != Rad && i != Emt && i != Rec
6327  && state[i].status() == -22
6328  && state[i].col() == state[Rad].col() )
6329  iColRes = i;
6330  } else if ( radType == -1 && state[Rad].colType() == -1) {
6331  // Find resonance connected to initial anticolour
6332  for(int i=0; i < int(state.size()); ++i)
6333  if ( i != Rad && i != Emt && i != Rec
6334  && state[i].status() == -22
6335  && state[i].acol() == state[Rad].acol() )
6336  iColRes = i;
6337  } else if ( radType == 1 && state[Rad].colType() == 1) {
6338  // Find resonance connected to final state colour
6339  for(int i=0; i < int(state.size()); ++i)
6340  if ( i != Rad && i != Emt && i != Rec
6341  && state[i].status() == -22
6342  && state[i].col() == state[Rad].col() )
6343  iColRes = i;
6344  } else if ( radType == 1 && state[Rad].colType() == -1) {
6345  // Find resonance connected to final state anticolour
6346  for(int i=0; i < int(state.size()); ++i)
6347  if ( i != Rad && i != Emt && i != Rec
6348  && state[i].status() == -22
6349  && state[i].acol() == state[Rad].acol() )
6350  iColRes = i;
6351  }
6352 
6353  if (iColRes > 0) {
6354  // Now find this resonance in the reclustered state
6355  int iColResNow = FindParticle( state[iColRes], outState);
6356 
6357  // Find reclustered radiator colours
6358  int radCol = outState[radPos].col();
6359  int radAcl = outState[radPos].acol();
6360  // Find resonance radiator colours
6361  int resCol = outState[iColResNow].col();
6362  int resAcl = outState[iColResNow].acol();
6363  // Check if any of the reclustered radiators colours match the resonance
6364  bool matchesRes = (radCol > 0
6365  && ( radCol == resCol || radCol == resAcl))
6366  || (radAcl > 0
6367  && ( radAcl == resCol || radAcl == resAcl));
6368 
6369  // If a resonance has been found, but no colours match, change
6370  // the colour of the resonance
6371  if (!matchesRes && iColResNow > 0) {
6372  if ( radType == -1 && outState[radPos].colType() == 1)
6373  outState[iColResNow].col(radCol);
6374  else if ( radType ==-1 && outState[radPos].colType() ==-1)
6375  outState[iColResNow].acol(radAcl);
6376  else if ( radType == 1 && outState[radPos].colType() == 1)
6377  outState[iColResNow].col(radCol);
6378  else if ( radType == 1 && outState[radPos].colType() ==-1)
6379  outState[iColResNow].acol(radAcl);
6380  }
6381 
6382 
6383  // If a resonance has been found, but no colours match, and the position
6384  // of the resonance in the event record has been changed, update the
6385  // radiator mother
6386  if (!matchesRes && iColResNow > 0 && iColRes != iColResNow)
6387  outState[radPos].mother1(iColResNow);
6388 
6389  }
6390 
6391  // If event is not constructed properly, return false
6392  if ( !validEvent(outState) ) {
6393  // Set momenta of particles to be attached to new event record
6394  outState.reset();
6395  return outState;
6396  }
6397 
6398  // Remember position of reclustered radiator in state
6399  iReclusteredNew = radPos;
6400 
6401  // Done
6402  return outState;
6403 }
6404 
6405 //--------------------------------------------------------------------------
6406 
6407 // Function to get the flavour of the radiator before the splitting
6408 // for clustering
6409 // IN int : Flavour of the radiator after the splitting
6410 // int : Flavour of the emitted after the splitting
6411 // OUT int : Flavour of the radiator before the splitting
6412 
6413 int History::getRadBeforeFlav(const int RadAfter, const int EmtAfter,
6414  const Event& event) {
6415 
6416  int type = event[RadAfter].isFinal() ? 1 :-1;
6417  int emtID = event[EmtAfter].id();
6418  int radID = event[RadAfter].id();
6419  int emtCOL = event[EmtAfter].col();
6420  int radCOL = event[RadAfter].col();
6421  int emtACL = event[EmtAfter].acol();
6422  int radACL = event[RadAfter].acol();
6423 
6424  bool colConnected = ((type == 1) && ( (emtCOL !=0 && (emtCOL ==radACL))
6425  || (emtACL !=0 && (emtACL ==radCOL)) ))
6426  ||((type ==-1) && ( (emtCOL !=0 && (emtCOL ==radCOL))
6427  || (emtACL !=0 && (emtACL ==radACL)) ));
6428  // QCD splittings
6429  // Gluon radiation
6430  if ( emtID == 21 )
6431  return radID;
6432  // Final state gluon splitting
6433  if ( type == 1 && emtID == -radID && !colConnected )
6434  return 21;
6435  // Initial state s-channel gluon splitting
6436  if ( type ==-1 && radID == 21 )
6437  return -emtID;
6438  // Initial state t-channel gluon splitting
6439  if ( type ==-1 && !colConnected
6440  && emtID != 21 && radID != 21 && abs(emtID) < 10 && abs(radID) < 10)
6441  return 21;
6442 
6443  // SQCD splittings
6444  int radSign = (radID < 0) ? -1 : 1;
6445  int offsetL = 1000000;
6446  int offsetR = 2000000;
6447  // Gluino radiation
6448  if ( emtID == 1000021 ) {
6449  // Gluino radiation combined with quark yields squark.
6450  if (abs(radID) < 10 ) {
6451  int offset = offsetL;
6452  // Check if righthanded squark present. If so, make the reclustered
6453  // squark match. Works for squark pair production + gluino emission.
6454  for (int i=0; i < int(event.size()); ++i)
6455  if ( event[i].isFinal()
6456  && event[i].idAbs() < offsetR+10 && event[i].idAbs() > offsetR)
6457  offset = offsetR;
6458  return radSign*(abs(radID)+offset);
6459  }
6460  // Gluino radiation combined with squark yields quark.
6461  if (abs(radID) > offsetL && abs(radID) < offsetL+10 )
6462  return radSign*(abs(radID)-offsetL);
6463  if (abs(radID) > offsetR && abs(radID) < offsetR+10 )
6464  return radSign*(abs(radID)-offsetR);
6465  // Gluino radiation off gluon yields gluino.
6466  if (radID == 21 ) return emtID;
6467  }
6468 
6469  int emtSign = (emtID < 0) ? -1 : 1;
6470  // Get PDG numbering offsets.
6471  int emtOffset = 0;
6472  if ( abs(emtID) > offsetL && abs(emtID) < offsetL+10 )
6473  emtOffset = offsetL;
6474  if ( abs(emtID) > offsetR && abs(emtID) < offsetR+10 )
6475  emtOffset = offsetR;
6476  int radOffset = 0;
6477  if ( abs(radID) > offsetL && abs(radID) < offsetL+10 )
6478  radOffset = offsetL;
6479  if ( abs(radID) > offsetR && abs(radID) < offsetR+10 )
6480  radOffset = offsetR;
6481 
6482  // Final state gluino splitting
6483  if ( type == 1 && !colConnected ) {
6484  // Emitted squark, radiating quark.
6485  if ( emtOffset > 0 && radOffset == 0
6486  && emtSign*(abs(emtID) - emtOffset) == -radID )
6487  return 1000021;
6488  // Emitted quark, radiating squark.
6489  if ( emtOffset == 0 && radOffset > 0
6490  && emtID == -radSign*(abs(radID) - radOffset) )
6491  return 1000021;
6492  }
6493 
6494  // Initial state s-channel gluino splitting
6495  if ( type ==-1 && radID == 1000021 ) {
6496  // Quark entering underlying hard process.
6497  if ( emtOffset > 0 ) return -emtSign*(abs(emtID) - emtOffset);
6498  // Squark entering underlying hard process.
6499  else return -emtSign*(abs(emtID) + emtOffset);
6500  }
6501 
6502  // Initial state t-channel gluino splitting.
6503  if ( type ==-1
6504  && ( (abs(emtID) > offsetL && abs(emtID) < offsetL+10)
6505  || (abs(emtID) > offsetR && abs(emtID) < offsetR+10))
6506  && ( (abs(radID) > offsetL && abs(radID) < offsetL+10)
6507  || (abs(radID) > offsetR && abs(radID) < offsetR+10))
6508  && emtSign*(abs(emtID)+emtOffset) == radSign*(abs(radID) - radOffset)
6509  && !colConnected ) {
6510  return 1000021;
6511  }
6512 
6513  // Electroweak splittings splittings
6514  // Photon / Z radiation: Calculate invariant mass of system
6515  double m2final = (event[RadAfter].p()+ event[EmtAfter].p()).m2Calc();
6516 
6517  if ( emtID == 22 || emtID == 23 ) return radID;
6518  // Final state Photon splitting
6519  if ( type == 1 && emtID == -radID && colConnected && sqrt(m2final) <= 10. )
6520  return 22;
6521  // Final state Photon splitting
6522  if ( type == 1 && emtID == -radID && colConnected && sqrt(m2final) > 10. )
6523  return 23;
6524  // Initial state s-channel photon/ Z splitting
6525  if ( type ==-1 && (radID == 22 || radID == 23) )
6526  return -emtID;
6527  // Initial state t-channel photon / Z splitting: Always bookkeep as photon
6528  if ( type ==-1 && abs(emtID) < 10 && abs(radID) < 10 && colConnected )
6529  return 22;
6530 
6531  // W+ radiation
6532  // Final state W+ splitting
6533  if ( emtID == 24 && radID < 0 ) return radID + 1;
6534  if ( emtID == 24 && radID > 0 ) return radID + 1;
6535 
6536  // W- radiation
6537  // Final state W- splitting
6538  if ( emtID ==-24 && radID < 0 ) return radID - 1;
6539  if ( emtID ==-24 && radID > 0 ) return radID - 1;
6540 
6541  // Done.
6542  return 0;
6543 
6544 }
6545 
6546 //--------------------------------------------------------------------------
6547 
6548 // Function to get the spin of the radiator before the splitting
6549 // IN int : Spin of the radiator after the splitting
6550 // int : Spin of the emitted after the splitting
6551 // OUT int : Spin of the radiator before the splitting
6552 
6553 int History::getRadBeforeSpin(const int radAfter, const int emtAfter,
6554  const int spinRadAfter, const int spinEmtAfter,
6555  const Event& event) {
6556 
6557  // Get flavour before the splitting.
6558  int radBeforeFlav = getRadBeforeFlav(radAfter, emtAfter, event);
6559 
6560  // Gluon in final state g-> q qbar
6561  if ( event[radAfter].isFinal()
6562  && event[radAfter].id() == -event[emtAfter].id())
6563  return (spinRadAfter == 9) ? spinEmtAfter : spinRadAfter;
6564 
6565  // Quark in final state q -> q g
6566  if ( event[radAfter].isFinal() && abs(radBeforeFlav) < 10
6567  && event[radAfter].idAbs() < 10)
6568  // Special oddity: Gluon does not change spin.
6569  return spinRadAfter;
6570 
6571  // Quark in final state q -> g q
6572  if ( event[radAfter].isFinal() && abs(radBeforeFlav) < 10
6573  && event[emtAfter].idAbs() < 10)
6574  // Special oddity: Gluon does not change spin.
6575  return spinEmtAfter;
6576 
6577  // Gluon in final state g -> g g
6578  if ( event[radAfter].isFinal() && radBeforeFlav == 21
6579  && event[radAfter].id() == 21)
6580  // Special oddity: Gluon does not change spin.
6581  return (spinRadAfter == 9) ? spinEmtAfter : spinRadAfter;
6582 
6583  // Gluon in initial state g-> q qbar
6584  if ( !event[radAfter].isFinal()
6585  && radBeforeFlav == -event[emtAfter].id())
6586  return (spinRadAfter == 9) ? spinEmtAfter : spinRadAfter;
6587 
6588  // Quark in initial state q -> q g
6589  if ( !event[radAfter].isFinal() && abs(radBeforeFlav) < 10
6590  && event[radAfter].idAbs() < 10)
6591  // Special oddity: Gluon does not change spin.
6592  return spinRadAfter;
6593 
6594  // Gluon in initial state q -> g q
6595  if ( !event[radAfter].isFinal() && radBeforeFlav == 21
6596  && event[emtAfter].idAbs() < 10)
6597  // Special oddity: Gluon does not change spin.
6598  return spinEmtAfter;
6599 
6600  // Done. Return default value.
6601  return 9;
6602 
6603 }
6604 
6605 //--------------------------------------------------------------------------
6606 
6607 // Function to properly colour-connect the radiator to the rest of
6608 // the event, as needed during clustering
6609 // IN Particle& : Particle to be connected
6610 // Particle : Recoiler forming a dipole with Radiator
6611 // Event : event to which Radiator shall be appended
6612 // OUT true : Radiator could be connected to the event
6613 // false : Radiator could not be connected to the
6614 // event or the resulting event was
6615 // non-valid
6616 
6617 bool History::connectRadiator( Particle& Radiator, const int RadType,
6618  const Particle& Recoiler, const int RecType,
6619  const Event& event ) {
6620 
6621  // Start filling radiator colour indices with dummy values
6622  Radiator.cols( -1, -1 );
6623 
6624  // Radiator should always be colour-connected to recoiler.
6625  // Three cases (rad = Anti-Quark, Quark, Gluon) to be considered
6626  if ( Radiator.colType() == -1 ) {
6627  // For final state antiquark radiator, the anticolour is fixed
6628  // by the final / initial state recoiler colour / anticolour
6629  if ( RadType + RecType == 2 )
6630  Radiator.cols( 0, Recoiler.col());
6631  else if ( RadType + RecType == 0 )
6632  Radiator.cols( 0, Recoiler.acol());
6633  // For initial state antiquark radiator, the anticolour is fixed
6634  // by the colour of the emitted gluon (which will be the
6635  // leftover anticolour of a final state particle or the leftover
6636  // colour of an initial state particle ( = the recoiler))
6637  else {
6638  // Set colour of antiquark radiator to zero
6639  Radiator.col( 0 );
6640  for (int i = 0; i < event.size(); ++i) {
6641  int col = event[i].col();
6642  int acl = event[i].acol();
6643 
6644  if ( event[i].isFinal()) {
6645  // Search for leftover anticolour in final / initial state
6646  if ( acl > 0 && FindCol(acl,i,0,event,1,true) == 0
6647  && FindCol(acl,i,0,event,2,true) == 0 )
6648  Radiator.acol(event[i].acol());
6649  } else {
6650  // Search for leftover colour in initial / final state
6651  if ( col > 0 && FindCol(col,i,0,event,1,true) == 0
6652  && FindCol(col,i,0,event,2,true) == 0 )
6653  Radiator.acol(event[i].col());
6654  }
6655  } // end loop over particles in event record
6656  }
6657 
6658  } else if ( Radiator.colType() == 1 ) {
6659  // For final state quark radiator, the colour is fixed
6660  // by the final / initial state recoiler anticolour / colour
6661  if ( RadType + RecType == 2 )
6662  Radiator.cols( Recoiler.acol(), 0);
6663 
6664  else if ( RadType + RecType == 0 )
6665  Radiator.cols( Recoiler.col(), 0);
6666  // For initial state quark radiator, the colour is fixed
6667  // by the anticolour of the emitted gluon (which will be the
6668  // leftover colour of a final state particle or the leftover
6669  // anticolour of an initial state particle ( = the recoiler))
6670 
6671  else {
6672  // Set anticolour of quark radiator to zero
6673  Radiator.acol( 0 );
6674  for (int i = 0; i < event.size(); ++i) {
6675  int col = event[i].col();
6676  int acl = event[i].acol();
6677 
6678  if ( event[i].isFinal()) {
6679  // Search for leftover colour in final / initial state
6680  if ( col > 0 && FindCol(col,i,0,event,1,true) == 0
6681  && FindCol(col,i,0,event,2,true) == 0)
6682  Radiator.col(event[i].col());
6683  } else {
6684  // Search for leftover anticolour in initial / final state
6685  if ( acl > 0 && FindCol(acl,i,0,event,1,true) == 0
6686  && FindCol(acl,i,0,event,2,true) == 0)
6687  Radiator.col(event[i].acol());
6688  }
6689  } // end loop over particles in event record
6690 
6691  } // end distinction between fsr / fsr+initial recoiler / isr
6692 
6693  } else if ( Radiator.colType() == 2 ) {
6694  // For a gluon radiator, one (anticolour) colour index is defined
6695  // by the recoiler colour (anticolour).
6696  // The remaining index is chosen to match the free index in the
6697  // event
6698  // Search for leftover colour (anticolour) in the final state
6699  for (int i = 0; i < event.size(); ++i) {
6700  int col = event[i].col();
6701  int acl = event[i].acol();
6702  int iEx = i;
6703 
6704  if ( event[i].isFinal()) {
6705  if ( col > 0 && FindCol(col,iEx,0,event,1,true) == 0
6706  && FindCol(col,iEx,0,event,2,true) == 0) {
6707  if (Radiator.status() < 0 ) Radiator.col(event[i].col());
6708  else Radiator.acol(event[i].col());
6709  }
6710  if ( acl > 0 && FindCol(acl,iEx,0,event,2,true) == 0
6711  && FindCol(acl,iEx,0,event,1,true) == 0 ) {
6712  if (Radiator.status() < 0 ) Radiator.acol(event[i].acol());
6713  else Radiator.col(event[i].acol());
6714  }
6715  } else {
6716  if ( col > 0 && FindCol(col,iEx,0,event,1,true) == 0
6717  && FindCol(col,iEx,0,event,2,true) == 0) {
6718  if (Radiator.status() < 0 ) Radiator.acol(event[i].col());
6719  else Radiator.col(event[i].col());
6720  }
6721  if ( acl > 0 && (FindCol(acl,iEx,0,event,2,true) == 0
6722  && FindCol(acl,iEx,0,event,1,true) == 0)) {
6723  if (Radiator.status() < 0 ) Radiator.col(event[i].acol());
6724  else Radiator.acol(event[i].acol());
6725  }
6726  }
6727  } // end loop over particles in event record
6728  } // end cases of different radiator colour type
6729 
6730  // If either colour or anticolour has not been set, return false
6731  if (Radiator.col() < 0 || Radiator.acol() < 0) return false;
6732  // Done
6733  return true;
6734 }
6735 
6736 //--------------------------------------------------------------------------
6737 
6738 // Function to find a colour (anticolour) index in the input event
6739 // IN int col : Colour tag to be investigated
6740 // int iExclude1 : Identifier of first particle to be excluded
6741 // from search
6742 // int iExclude2 : Identifier of second particle to be excluded
6743 // from search
6744 // Event event : event to be searched for colour tag
6745 // int type : Tag to define if col should be counted as
6746 // colour (type = 1) [->find anti-colour index
6747 // contracted with col]
6748 // anticolour (type = 2) [->find colour index
6749 // contracted with col]
6750 // OUT int : Position of particle in event record
6751 // contraced with col [0 if col is free tag]
6752 
6753 int History::FindCol(int col, int iExclude1, int iExclude2,
6754  const Event& event, int type, bool isHardIn) {
6755 
6756  bool isHard = isHardIn;
6757  int index = 0;
6758 
6759  if (isHard) {
6760  // Search event record for matching colour & anticolour
6761  for(int n = 0; n < event.size(); ++n) {
6762  if ( n != iExclude1 && n != iExclude2
6763  && event[n].colType() != 0
6764  &&( event[n].status() > 0 // Check outgoing
6765  || event[n].status() == -21) ) { // Check incoming
6766  if ( event[n].acol() == col ) {
6767  index = -n;
6768  break;
6769  }
6770  if ( event[n].col() == col ) {
6771  index = n;
6772  break;
6773  }
6774  }
6775  }
6776  } else {
6777 
6778  // Search event record for matching colour & anticolour
6779  for(int n = 0; n < event.size(); ++n) {
6780  if ( n != iExclude1 && n != iExclude2
6781  && event[n].colType() != 0
6782  &&( event[n].status() == 43 // Check outgoing from ISR
6783  || event[n].status() == 51 // Check outgoing from FSR
6784  || event[n].status() == -41 // first initial
6785  || event[n].status() == -42) ) { // second initial
6786  if ( event[n].acol() == col ) {
6787  index = -n;
6788  break;
6789  }
6790  if ( event[n].col() == col ) {
6791  index = n;
6792  break;
6793  }
6794  }
6795  }
6796  }
6797  // if no matching colour / anticolour has been found, return false
6798  if ( type == 1 && index < 0) return abs(index);
6799  if ( type == 2 && index > 0) return abs(index);
6800 
6801  return 0;
6802 }
6803 
6804 //--------------------------------------------------------------------------
6805 
6806 // Function to in the input event find a particle with quantum
6807 // numbers matching those of the input particle
6808 // IN Particle : Particle to be searched for
6809 // Event : Event to be searched in
6810 // OUT int : > 0 : Position of matching particle in event
6811 // < 0 : No match in event
6812 
6813 int History::FindParticle( const Particle& particle, const Event& event,
6814  bool checkStatus ) {
6815 
6816  int index = -1;
6817 
6818  for ( int i = int(event.size()) - 1; i > 0; --i )
6819  if ( event[i].id() == particle.id()
6820  && event[i].colType() == particle.colType()
6821  && event[i].chargeType() == particle.chargeType()
6822  && event[i].col() == particle.col()
6823  && event[i].acol() == particle.acol()
6824  && event[i].charge() == particle.charge() ) {
6825  index = i;
6826  break;
6827  }
6828 
6829  if ( checkStatus && event[index].status() != particle.status() )
6830  index = -1;
6831 
6832  return index;
6833 }
6834 
6835 //--------------------------------------------------------------------------
6836 
6837 // Function to get the colour of the radiator before the splitting
6838 // for clustering
6839 // IN int : Position of the radiator after the splitting, in the event
6840 // int : Position of the emitted after the splitting, in the event
6841 // Event : Reference event
6842 // OUT int : Colour of the radiator before the splitting
6843 
6844 int History::getRadBeforeCol(const int rad, const int emt,
6845  const Event& event) {
6846 
6847  // Save type of splitting
6848  int type = (event[rad].isFinal()) ? 1 :-1;
6849  // Get flavour of radiator after potential clustering
6850  int radBeforeFlav = getRadBeforeFlav(rad,emt,event);
6851  // Get colours of the radiator before the potential clustering
6852  int radBeforeCol = -1;
6853  // Get reconstructed gluon colours
6854  if (radBeforeFlav == 21) {
6855 
6856  // Start with quark emissions in FSR
6857  if (type == 1 && event[emt].id() != 21) {
6858  radBeforeCol = (event[rad].col() > 0)
6859  ? event[rad].col() : event[emt].col();
6860  // Quark emissions in ISR
6861  } else if (type == -1 && event[emt].id() != 21) {
6862  radBeforeCol = (event[rad].col() > 0)
6863  ? event[rad].col() : event[emt].acol();
6864  //Gluon emissions in FSR
6865  } else if (type == 1 && event[emt].id() == 21) {
6866  // If emitted is a gluon, remove the repeated index, and take
6867  // the remaining indices as colour and anticolour
6868  int colRemove = (event[rad].col() == event[emt].acol())
6869  ? event[rad].col() : event[rad].acol();
6870  radBeforeCol = (event[rad].col() == colRemove)
6871  ? event[emt].col() : event[rad].col();
6872  //Gluon emissions in ISR
6873  } else if (type == -1 && event[emt].id() == 21) {
6874  // If emitted is a gluon, remove the repeated index, and take
6875  // the remaining indices as colour and anticolour
6876  int colRemove = (event[rad].col() == event[emt].col())
6877  ? event[rad].col() : event[rad].acol();
6878  radBeforeCol = (event[rad].col() == colRemove)
6879  ? event[emt].acol() : event[rad].col();
6880  }
6881 
6882  // Get reconstructed quark colours
6883  } else if ( radBeforeFlav != 21 && radBeforeFlav > 0) {
6884 
6885  // Quark emission in FSR
6886  if (type == 1 && event[emt].id() != 21) {
6887  // If radiating is a quark, remove the repeated index, and take
6888  // the remaining indices as colour and anticolour
6889  int colRemove = (event[rad].col() == event[emt].acol())
6890  ? event[rad].acol() : 0;
6891  radBeforeCol = (event[rad].col() == colRemove)
6892  ? event[emt].col() : event[rad].col();
6893  //Gluon emissions in FSR
6894  } else if (type == 1 && event[emt].id() == 21) {
6895  // If emitted is a gluon, remove the repeated index, and take
6896  // the remaining indices as colour and anticolour
6897  int colRemove = (event[rad].col() == event[emt].acol())
6898  ? event[rad].col() : 0;
6899  radBeforeCol = (event[rad].col() == colRemove)
6900  ? event[emt].col() : event[rad].col();
6901  //Quark emissions in ISR
6902  } else if (type == -1 && event[emt].id() != 21) {
6903  // If emitted is a quark, remove the repeated index, and take
6904  // the remaining indices as colour and anticolour
6905  int colRemove = (event[rad].col() == event[emt].col())
6906  ? event[rad].col() : 0;
6907  radBeforeCol = (event[rad].col() == colRemove)
6908  ? event[emt].acol() : event[rad].col();
6909  //Gluon emissions in ISR
6910  } else if (type == -1 && event[emt].id() == 21) {
6911  // If emitted is a gluon, remove the repeated index, and take
6912  // the remaining indices as colour and anticolour
6913  int colRemove = (event[rad].col() == event[emt].col())
6914  ? event[rad].col() : 0;
6915  radBeforeCol = (event[rad].col() == colRemove)
6916  ? event[emt].acol() : event[rad].col();
6917  }
6918  // Other particles are assumed uncoloured
6919  } else {
6920  radBeforeCol = 0;
6921  }
6922 
6923  return radBeforeCol;
6924 
6925 }
6926 
6927 //--------------------------------------------------------------------------
6928 
6929 // Function to get the anticolour of the radiator before the splitting
6930 // for clustering
6931 // IN int : Position of the radiator after the splitting, in the event
6932 // int : Position of the emitted after the splitting, in the event
6933 // Event : Reference event
6934 // OUT int : Anticolour of the radiator before the splitting
6935 
6936 int History::getRadBeforeAcol(const int rad, const int emt,
6937  const Event& event) {
6938 
6939  // Save type of splitting
6940  int type = (event[rad].isFinal()) ? 1 :-1;
6941  // Get flavour of radiator after potential clustering
6942 
6943  int radBeforeFlav = getRadBeforeFlav(rad,emt,event);
6944  // Get colours of the radiator before the potential clustering
6945  int radBeforeAcl = -1;
6946  // Get reconstructed gluon colours
6947  if (radBeforeFlav == 21) {
6948 
6949  // Start with quark emissions in FSR
6950  if (type == 1 && event[emt].id() != 21) {
6951  radBeforeAcl = (event[rad].acol() > 0)
6952  ? event[rad].acol() : event[emt].acol();
6953  // Quark emissions in ISR
6954  } else if (type == -1 && event[emt].id() != 21) {
6955  radBeforeAcl = (event[rad].acol() > 0)
6956  ? event[rad].acol() : event[emt].col();
6957  //Gluon emissions in FSR
6958  } else if (type == 1 && event[emt].id() == 21) {
6959  // If emitted is a gluon, remove the repeated index, and take
6960  // the remaining indices as colour and anticolour
6961  int colRemove = (event[rad].col() == event[emt].acol())
6962  ? event[rad].col() : event[rad].acol();
6963  radBeforeAcl = (event[rad].acol() == colRemove)
6964  ? event[emt].acol() : event[rad].acol();
6965  //Gluon emissions in ISR
6966  } else if (type == -1 && event[emt].id() == 21) {
6967  // If emitted is a gluon, remove the repeated index, and take
6968  // the remaining indices as colour and anticolour
6969  int colRemove = (event[rad].col() == event[emt].col())
6970  ? event[rad].col() : event[rad].acol();
6971  radBeforeAcl = (event[rad].acol() == colRemove)
6972  ? event[emt].col() : event[rad].acol();
6973  }
6974 
6975  // Get reconstructed anti-quark colours
6976  } else if ( radBeforeFlav != 21 && radBeforeFlav < 0) {
6977 
6978  // Antiquark emission in FSR
6979  if (type == 1 && event[emt].id() != 21) {
6980  // If radiating is a antiquark, remove the repeated index, and take
6981  // the remaining indices as colour and anticolour
6982  int colRemove = (event[rad].col() == event[emt].acol())
6983  ? event[rad].acol() : 0;
6984  radBeforeAcl = (event[rad].acol() == colRemove)
6985  ? event[emt].acol() : event[rad].acol();
6986  //Gluon emissions in FSR
6987  } else if (type == 1 && event[emt].id() == 21) {
6988  // If emitted is a gluon, remove the repeated index, and take
6989  // the remaining indices as colour and anticolour
6990  int colRemove = (event[rad].acol() == event[emt].col())
6991  ? event[rad].acol() : 0;
6992  radBeforeAcl = (event[rad].acol() == colRemove)
6993  ? event[emt].acol() : event[rad].acol();
6994  //Antiquark emissions in ISR
6995  } else if (type == -1 && event[emt].id() != 21) {
6996  // If emitted is an antiquark, remove the repeated index, and take
6997  // the remaining indices as colour and anticolour
6998  int colRemove = (event[rad].acol() == event[emt].acol())
6999  ? event[rad].acol() : 0;
7000  radBeforeAcl = (event[rad].acol() == colRemove)
7001  ? event[emt].col() : event[rad].acol();
7002  //Gluon emissions in ISR
7003  } else if (type == -1 && event[emt].id() == 21) {
7004  // If emitted is a gluon, remove the repeated index, and take
7005  // the remaining indices as colour and anticolour
7006  int colRemove = (event[rad].acol() == event[emt].acol())
7007  ? event[rad].acol() : 0;
7008  radBeforeAcl = (event[rad].acol() == colRemove)
7009  ? event[emt].col() : event[rad].acol();
7010  }
7011  // Other particles are considered uncoloured
7012  } else {
7013  radBeforeAcl = 0;
7014  }
7015 
7016  return radBeforeAcl;
7017 
7018 }
7019 
7020 //--------------------------------------------------------------------------
7021 
7022  // Function to get the parton connected to in by a colour line
7023  // IN int : Position of parton for which partner should be found
7024  // Event : Reference event
7025  // OUT int : If a colour line connects the "in" parton with another
7026  // parton, return the Position of the partner, else return 0
7027 
7028 int History::getColPartner(const int in, const Event& event) {
7029 
7030  if (event[in].col() == 0) return 0;
7031 
7032  int partner = 0;
7033  // Try to find anticolour index first
7034  partner = FindCol(event[in].col(),in,0,event,1,true);
7035  // If no anticolour index has been found, try colour
7036  if (partner == 0)
7037  partner = FindCol(event[in].col(),in,0,event,2,true);
7038 
7039  return partner;
7040 
7041 }
7042 
7043 //--------------------------------------------------------------------------
7044 
7045 
7046  // Function to get the parton connected to in by an anticolour line
7047  // IN int : Position of parton for which partner should be found
7048  // Event : Reference event
7049  // OUT int : If an anticolour line connects the "in" parton with another
7050  // parton, return the Position of the partner, else return 0
7051 
7052 int History::getAcolPartner(const int in, const Event& event) {
7053 
7054  if (event[in].acol() == 0) return 0;
7055 
7056  int partner = 0;
7057  // Try to find colour index first
7058  partner = FindCol(event[in].acol(),in,0,event,2,true);
7059  // If no colour index has been found, try anticolour
7060  if (partner == 0)
7061  partner = FindCol(event[in].acol(),in,0,event,1,true);
7062 
7063  return partner;
7064 
7065 }
7066 
7067 //--------------------------------------------------------------------------
7068 
7069 // Function to get the list of partons connected to the particle
7070 // formed by reclusterinf emt and rad by colour and anticolour lines
7071 // IN int : Position of radiator in the clustering
7072 // IN int : Position of emitted in the clustering
7073 // Event : Reference event
7074 // OUT vector<int> : List of positions of all partons that are connected
7075 // to the parton that will be formed
7076 // by clustering emt and rad.
7077 
7078 vector<int> History::getReclusteredPartners(const int rad, const int emt,
7079  const Event& event) {
7080 
7081  // Save type
7082  int type = event[rad].isFinal() ? 1 : -1;
7083  // Get reclustered colours
7084  int radBeforeCol = getRadBeforeCol(rad, emt, event);
7085  int radBeforeAcl = getRadBeforeAcol(rad, emt, event);
7086  // Declare output
7087  vector<int> partners;
7088 
7089 
7090  // Start with FSR clusterings
7091  if (type == 1) {
7092 
7093  for(int i=0; i < int(event.size()); ++i) {
7094  // Check all initial state partons
7095  if ( i != emt && i != rad
7096  && event[i].status() == -21
7097  && event[i].col() > 0
7098  && event[i].col() == radBeforeCol)
7099  partners.push_back(i);
7100  // Check all final state partons
7101  if ( i != emt && i != rad
7102  && event[i].isFinal()
7103  && event[i].acol() > 0
7104  && event[i].acol() == radBeforeCol)
7105  partners.push_back(i);
7106  // Check all initial state partons
7107  if ( i != emt && i != rad
7108  && event[i].status() == -21
7109  && event[i].acol() > 0
7110  && event[i].acol() == radBeforeAcl)
7111  partners.push_back(i);
7112  // Check all final state partons
7113  if ( i != emt && i != rad
7114  && event[i].isFinal()
7115  && event[i].col() > 0
7116  && event[i].col() == radBeforeAcl)
7117  partners.push_back(i);
7118  }
7119  // Start with ISR clusterings
7120  } else {
7121 
7122  for(int i=0; i < int(event.size()); ++i) {
7123  // Check all initial state partons
7124  if ( i != emt && i != rad
7125  && event[i].status() == -21
7126  && event[i].acol() > 0
7127  && event[i].acol() == radBeforeCol)
7128  partners.push_back(i);
7129  // Check all final state partons
7130  if ( i != emt && i != rad
7131  && event[i].isFinal()
7132  && event[i].col() > 0
7133  && event[i].col() == radBeforeCol)
7134  partners.push_back(i);
7135  // Check all initial state partons
7136  if ( i != emt && i != rad
7137  && event[i].status() == -21
7138  && event[i].col() > 0
7139  && event[i].col() == radBeforeAcl)
7140  partners.push_back(i);
7141  // Check all final state partons
7142  if ( i != emt && i != rad
7143  && event[i].isFinal()
7144  && event[i].acol() > 0
7145  && event[i].acol() == radBeforeAcl)
7146  partners.push_back(i);
7147  }
7148 
7149  }
7150  // Done
7151  return partners;
7152 }
7153 
7154 //--------------------------------------------------------------------------
7155 
7156 // Function to extract a chain of colour-connected partons in
7157 // the event
7158 // IN int : Type of parton from which to start extracting a
7159 // parton chain. If the starting point is a quark
7160 // i.e. flavType = 1, a chain of partons that are
7161 // consecutively connected by colour-lines will be
7162 // extracted. If the starting point is an antiquark
7163 // i.e. flavType =-1, a chain of partons that are
7164 // consecutively connected by anticolour-lines
7165 // will be extracted.
7166 // IN int : Position of the parton from which a
7167 // colour-connected chain should be derived
7168 // IN Event : Refernence event
7169 // IN/OUT vector<int> : Partons that should be excluded from the search.
7170 // OUT vector<int> : Positions of partons along the chain
7171 // OUT bool : Found singlet / did not find singlet
7172 
7173 bool History::getColSinglet( const int flavType, const int iParton,
7174  const Event& event, vector<int>& exclude, vector<int>& colSinglet) {
7175 
7176  // If no possible flavour to start from has been found
7177  if (iParton < 0) return false;
7178 
7179  // If no further partner has been found in a previous iteration,
7180  // and the whole final state has been excluded, we're done
7181  if (iParton == 0) {
7182 
7183  // Count number of final state partons
7184  int nFinal = 0;
7185  for(int i=0; i < int(event.size()); ++i)
7186  if ( event[i].isFinal() && event[i].colType() != 0)
7187  nFinal++;
7188 
7189  // Get number of initial state partons in the list of
7190  // excluded partons
7191  int nExclude = int(exclude.size());
7192  int nInitExclude = 0;
7193  if (!event[exclude[2]].isFinal())
7194  nInitExclude++;
7195  if (!event[exclude[3]].isFinal())
7196  nInitExclude++;
7197 
7198  // If the whole final state has been considered, return
7199  if (nFinal == nExclude - nInitExclude)
7200  return true;
7201  else
7202  return false;
7203 
7204  }
7205 
7206  // Declare colour partner</