Index: /trunk/Makefile
===================================================================
--- /trunk/Makefile	(revision 1166)
+++ /trunk/Makefile	(revision 1167)
@@ -19,4 +19,9 @@
 DELPHES_LIBS = $(shell $(RC) --libs) -lEG $(SYSLIBS)
 DISPLAY_LIBS = $(shell $(RC) --evelibs) $(SYSLIBS)
+
+ifneq ($(PYTHIA8),)
+CXXFLAGS += -DHAS_PYTHIA8 -I$(PYTHIA8)/include
+DELPHES_LIBS += -L$(PYTHIA8)/lib/archive -lpythia8 -llhapdfdummy
+endif
 
 ###
@@ -201,5 +206,6 @@
 	modules/Cloner.h \
 	modules/Weighter.h \
-	modules/ExampleModule.h
+	modules/ExampleModule.h \
+	modules/PileUpMergerPythia8.h
 tmp/external/ExRootAnalysis/ExRootAnalysisDict.$(SrcSuf): \
 	external/ExRootAnalysis/ExRootAnalysisLinkDef.h \
@@ -356,4 +362,14 @@
 	external/fastjet/plugins/CDFCones/fastjet/CDFMidPointPlugin.hh \
 	external/fastjet/plugins/CDFCones/fastjet/CDFJetCluPlugin.hh
+tmp/modules/PileUpMergerPythia8.$(ObjSuf): \
+	modules/PileUpMergerPythia8.$(SrcSuf) \
+	modules/PileUpMergerPythia8.h \
+	classes/DelphesClasses.h \
+	classes/DelphesFactory.h \
+	classes/DelphesFormula.h \
+	classes/DelphesPileUpReader.h \
+	external/ExRootAnalysis/ExRootResult.h \
+	external/ExRootAnalysis/ExRootFilter.h \
+	external/ExRootAnalysis/ExRootClassifier.h
 tmp/modules/StatusPidFilter.$(ObjSuf): \
 	modules/StatusPidFilter.$(SrcSuf) \
@@ -889,4 +905,8 @@
 	tmp/external/fastjet/plugins/EECambridge/EECambridgePlugin.$(ObjSuf)
 
+ifneq ($(PYTHIA8),)
+DELPHES_OBJ +=  \
+	tmp/modules/PileUpMergerPythia8.$(ObjSuf)
+endif
 tmp/display/DelphesDisplay.$(ObjSuf): \
 	display/DelphesDisplay.$(SrcSuf) \
@@ -899,4 +919,8 @@
 	tmp/display/DelphesCaloData.$(ObjSuf)
 
+ifneq ($(PYTHIA8),)
+DISPLAY_OBJ +=  \
+	
+endif
 tmp/external/tcl/tclObj.$(ObjSuf): \
 	external/tcl/tclObj.c
@@ -1022,11 +1046,11 @@
 	@touch $@
 
-modules/ConstituentFilter.h: \
-	classes/DelphesModule.h
-	@touch $@
-
 external/fastjet/JetDefinition.hh: \
 	external/fastjet/internal/numconsts.hh \
 	external/fastjet/PseudoJet.hh
+	@touch $@
+
+modules/ConstituentFilter.h: \
+	classes/DelphesModule.h
 	@touch $@
 
@@ -1158,4 +1182,8 @@
 
 modules/UniqueObjectFinder.h: \
+	classes/DelphesModule.h
+	@touch $@
+
+modules/PileUpMergerPythia8.h: \
 	classes/DelphesModule.h
 	@touch $@
Index: /trunk/doc/genMakefile.tcl
===================================================================
--- /trunk/doc/genMakefile.tcl	(revision 1166)
+++ /trunk/doc/genMakefile.tcl	(revision 1167)
@@ -88,10 +88,15 @@
 
   set srcObjFiles {}
+  set srcObjFilesPythia8 {}
 
   foreach fileName $source {
     regsub {\.cc} $fileName {} srcName
     set srcObjName $prefix$srcName
-
-    lappend srcObjFiles $srcObjName$objSuf
+    
+    if {$fileName == "modules/PileUpMergerPythia8.cc"} {
+      lappend srcObjFilesPythia8 $srcObjName$objSuf
+    } else {
+      lappend srcObjFiles $srcObjName$objSuf
+    }
 
     dependencies $fileName "$srcObjName$objSuf:$suffix$srcName$srcSuf"
@@ -101,4 +106,9 @@
   puts [join $srcObjFiles $suffix]
   puts ""
+  
+  puts {ifneq ($(PYTHIA8),)}
+  puts -nonewline "${srcPrefix}_OBJ += $suffix"
+  puts [join $srcObjFilesPythia8 $suffix]
+  puts {endif}
 }
 
@@ -193,4 +203,9 @@
 DISPLAY_LIBS = $(shell $(RC) --evelibs) $(SYSLIBS)
 
+ifneq ($(PYTHIA8),)
+CXXFLAGS += -DHAS_PYTHIA8 -I$(PYTHIA8)/include
+DELPHES_LIBS += -L$(PYTHIA8)/lib/archive -lpythia8 -llhapdfdummy
+endif
+
 ###
 
Index: /trunk/modules/ModulesLinkDef.h
===================================================================
--- /trunk/modules/ModulesLinkDef.h	(revision 1166)
+++ /trunk/modules/ModulesLinkDef.h	(revision 1167)
@@ -37,4 +37,8 @@
 #include "modules/ExampleModule.h"
 
+#ifdef HAS_PYTHIA8
+#include "modules/PileUpMergerPythia8.h"
+#endif
+
 #ifdef __CINT__
 
@@ -68,3 +72,7 @@
 #pragma link C++ class ExampleModule+;
 
+#ifdef HAS_PYTHIA8
+#pragma link C++ class PileUpMergerPythia8+;
 #endif
+
+#endif
Index: /trunk/modules/PileUpMergerPythia8.cc
===================================================================
--- /trunk/modules/PileUpMergerPythia8.cc	(revision 1167)
+++ /trunk/modules/PileUpMergerPythia8.cc	(revision 1167)
@@ -0,0 +1,158 @@
+/** \class PileUpMergerPythia8
+ *
+ *  Merges particles from pile-up sample into event
+ *
+ *
+ *  $Date$
+ *  $Revision$
+ *
+ *
+ *  \author P. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "modules/PileUpMergerPythia8.h"
+
+#include "classes/DelphesClasses.h"
+#include "classes/DelphesFactory.h"
+#include "classes/DelphesFormula.h"
+#include "classes/DelphesPileUpReader.h"
+
+#include "ExRootAnalysis/ExRootResult.h"
+#include "ExRootAnalysis/ExRootFilter.h"
+#include "ExRootAnalysis/ExRootClassifier.h"
+
+#include "Pythia.h"
+
+#include "TMath.h"
+#include "TString.h"
+#include "TFormula.h"
+#include "TRandom3.h"
+#include "TObjArray.h"
+#include "TDatabasePDG.h"
+#include "TLorentzVector.h"
+
+#include <algorithm>
+#include <stdexcept>
+#include <iostream>
+#include <sstream>
+
+using namespace std;
+
+//------------------------------------------------------------------------------
+
+PileUpMergerPythia8::PileUpMergerPythia8() :
+  fPythia(0), fItInputArray(0)
+{
+}
+
+//------------------------------------------------------------------------------
+
+PileUpMergerPythia8::~PileUpMergerPythia8()
+{
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpMergerPythia8::Init()
+{
+  const char *fileName;
+
+  fMeanPileUp  = GetDouble("MeanPileUp", 10);
+  fZVertexSpread = GetDouble("ZVertexSpread", 0.05)*1.0E3;
+
+  fCMEnergy = GetDouble("CMEnergy", 14000.0);
+  fPTMin = GetDouble("PTMin", 0.5);
+
+  fRandomSeed = GetInt("RandomSeed", 1);
+  fBeamAID = GetInt("BeamAID", 2212);
+  fBeamBID = GetInt("BeamBID", 2212);
+
+  // Pythia8 pile-up initialization
+  fPythia = new Pythia8::Pythia();
+  fPythia->readString("SoftQCD:minBias = on");
+  fPythia->init(fBeamAID, fBeamBID, fCMEnergy);
+  fPythia->rndm.init(fRandomSeed);
+
+  // import input array
+  fInputArray = ImportArray(GetString("InputArray", "Delphes/stableParticles"));
+  fItInputArray = fInputArray->MakeIterator();
+
+  // create output arrays
+  fOutputArray = ExportArray(GetString("OutputArray", "stableParticles"));
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpMergerPythia8::Finish()
+{
+  if(fPythia) delete fPythia;
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpMergerPythia8::Process()
+{
+  TDatabasePDG *pdg = TDatabasePDG::Instance();
+  TParticlePDG *pdgParticle;
+  Int_t pid;
+  Float_t x, y, z, t;
+  Float_t px, py, pz, e;
+  Double_t dz, dphi;
+  Int_t poisson, event, i;
+  Long64_t allEntries, entry;
+  Candidate *candidate;
+  DelphesFactory *factory;
+
+  fItInputArray->Reset();
+  while((candidate = static_cast<Candidate*>(fItInputArray->Next())))
+  {
+    fOutputArray->Add(candidate);
+  }
+
+  factory = GetFactory();
+
+  poisson = gRandom->Poisson(fMeanPileUp);
+
+  for(event = 0; event < poisson; ++event)
+  {
+    while(!fPythia->next());
+
+    dz = gRandom->Gaus(0.0, fZVertexSpread);
+    dphi = gRandom->Uniform(-TMath::Pi(), TMath::Pi());
+
+    for(i = 0; i < fPythia->event.size(); ++i)
+    {
+      Pythia8::Particle &particle = pythia->event[i];
+
+      if(particle.status() != 1 || !particle.isVisible() || particle.pT() <= fPTMin) continue;
+
+      pid = particle.id();
+      px = particle.px(); py = particle.py(); pz = particle.pz(); e = particle.e();
+      x = particle.xProd(); y = particle.yProd(); z = particle.zProd(); t = particle.tProd();
+
+      candidate = factory->NewCandidate();
+
+      candidate->PID = pid;
+
+      candidate->Status = 1;
+
+      pdgParticle = pdg->GetParticle(pid);
+      candidate->Charge = pdgParticle ? Int_t(pdgParticle->Charge()/3.0) : -999;
+      candidate->Mass = pdgParticle ? pdgParticle->Mass() : -999.9;
+
+      candidate->IsPU = 1;
+
+      candidate->Momentum.SetPxPyPzE(px, py, pz, e);
+      candidate->Momentum.RotateZ(dphi);
+
+      candidate->Position.SetXYZT(x, y, z + dz, t);
+      candidate->Position.RotateZ(dphi);
+
+      fOutputArray->Add(candidate);
+    }
+  }
+}
+
+//------------------------------------------------------------------------------
+
Index: /trunk/modules/PileUpMergerPythia8.h
===================================================================
--- /trunk/modules/PileUpMergerPythia8.h	(revision 1167)
+++ /trunk/modules/PileUpMergerPythia8.h	(revision 1167)
@@ -0,0 +1,59 @@
+#ifndef PileUpMergerPythia8_h
+#define PileUpMergerPythia8_h
+
+/** \class PileUpMergerPythia8
+ *
+ *  Merges particles from pile-up sample into event
+ *
+ *
+ *  $Date$
+ *  $Revision$
+ *
+ *
+ *  \author M. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "classes/DelphesModule.h"
+
+class TObjArray;
+
+namespace Pythia8
+{
+ class Pythia;
+};
+
+class PileUpMergerPythia8: public DelphesModule
+{
+public:
+
+  PileUpMergerPythia8();
+  ~PileUpMergerPythia8();
+
+  void Init();
+  void Process();
+  void Finish();
+
+private:
+
+  Double_t fMeanPileUp;
+  Double_t fZVertexSpread;
+  Double_t fCMEnergy;
+  Double_t fPTMin;
+
+  Int_t fRandomSeed;
+  Int_t fBeamAID;
+  Int_t fBeamBID;
+
+  Pythia8::Pythia *fPythia; //!
+
+  TIterator *fItInputArray; //!
+
+  const TObjArray *fInputArray; //!
+
+  TObjArray *fOutputArray; //!
+
+  ClassDef(PileUpMergerPythia8, 1)
+};
+
+#endif
