Index: modules/PileUpSubtractor4D.cc
===================================================================
--- modules/PileUpSubtractor4D.cc	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
+++ modules/PileUpSubtractor4D.cc	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
@@ -0,0 +1,190 @@
+/*
+ *  Delphes: a framework for fast simulation of a generic collider experiment
+ *  Copyright (C) 2012-2014  Universite catholique de Louvain (UCL), Belgium
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \class TrackPileUpSubtractor
+ *
+ *  Subtract pile-up contribution from tracks.
+ *
+ *  \author P. Demin - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "modules/PileUpSubtractor4D.h"
+
+#include "classes/DelphesClasses.h"
+#include "classes/DelphesFactory.h"
+#include "classes/DelphesFormula.h"
+
+#include "ExRootAnalysis/ExRootClassifier.h"
+#include "ExRootAnalysis/ExRootFilter.h"
+#include "ExRootAnalysis/ExRootResult.h"
+
+#include "TDatabasePDG.h"
+#include "TFormula.h"
+#include "TLorentzVector.h"
+#include "TMath.h"
+#include "TObjArray.h"
+#include "TRandom3.h"
+#include "TString.h"
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <stdexcept>
+
+using namespace std;
+
+//------------------------------------------------------------------------------
+
+PileUpSubtractor4D::PileUpSubtractor4D() :
+  fFormula(0)
+{
+  fFormula = new DelphesFormula;
+}
+
+//------------------------------------------------------------------------------
+
+PileUpSubtractor4D::~PileUpSubtractor4D()
+{
+  if(fFormula) delete fFormula;
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpSubtractor4D::Init()
+{
+  // import input array
+
+  fVertexInputArray = ImportArray(GetString("VertexInputArray", "PileUpMerger/vertices"));
+  fItVertexInputArray = fVertexInputArray->MakeIterator();
+
+  // read resolution formula in m
+  fChargedMinSignificance = GetDouble("ChargedMinSignificance", 3);
+  fNeutralMinSignificance = GetDouble("NeutralMinSignificance", 3);
+
+  fPTMin = GetDouble("PTMin", 0.);
+
+  // import arrays with output from other modules
+
+  ExRootConfParam param = GetParam("InputArray");
+  Long_t i, size;
+  const TObjArray *array;
+  TIterator *iterator;
+
+  size = param.GetSize();
+  for(i = 0; i < size / 2; ++i)
+  {
+    array = ImportArray(param[i * 2].GetString());
+    iterator = array->MakeIterator();
+
+    fInputMap[iterator] = ExportArray(param[i * 2 + 1].GetString());
+  }
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpSubtractor4D::Finish()
+{
+  map<TIterator *, TObjArray *>::iterator itInputMap;
+  TIterator *iterator;
+
+  for(itInputMap = fInputMap.begin(); itInputMap != fInputMap.end(); ++itInputMap)
+  {
+    iterator = itInputMap->first;
+
+    if(iterator) delete iterator;
+  }
+
+  if(fItVertexInputArray) delete fItVertexInputArray;
+}
+
+//------------------------------------------------------------------------------
+
+void PileUpSubtractor4D::Process()
+{
+  Candidate *candidate, *particle;
+  map<TIterator *, TObjArray *>::iterator itInputMap;
+  TIterator *iterator;
+  TObjArray *array;
+  Double_t z, zvtx = 0;
+  Double_t z_err, zvtx_err = 0;
+  Double_t t, tvtx = 0;
+  Double_t t_err, tvtx_err = 0;
+  Double_t sumPTSquare = 0;
+  Double_t tempPTSquare = 0;
+  Double_t pt, eta, phi, e;
+  Double_t distanceCharged, distanceNeutral = 0;
+
+  // find z position of primary vertex
+
+  fItVertexInputArray->Reset();
+  while((candidate = static_cast<Candidate *>(fItVertexInputArray->Next())))
+  {
+    tempPTSquare = candidate->SumPT2;
+    if(tempPTSquare > sumPTSquare)
+    {
+      sumPTSquare = tempPTSquare;
+      zvtx = candidate->Position.Z();
+      zvtx_err = candidate->PositionError.Z();
+      tvtx = candidate->Position.T();
+      tvtx_err = candidate->PositionError.T();
+    } 
+  }
+
+  // loop over all input arrays
+  for(itInputMap = fInputMap.begin(); itInputMap != fInputMap.end(); ++itInputMap)
+  {
+    iterator = itInputMap->first;
+    array = itInputMap->second;
+
+    // loop over all candidates
+    iterator->Reset();
+    while((candidate = static_cast<Candidate *>(iterator->Next())))
+    {
+      particle = static_cast<Candidate *>(candidate->GetCandidates()->At(0));
+      const TLorentzVector &candidateMomentum = particle->Momentum;
+
+      eta = candidateMomentum.Eta();
+      pt = candidateMomentum.Pt();
+      phi = candidateMomentum.Phi();
+      e = candidateMomentum.E();
+
+      z = particle->Position.Z();
+      z_err = particle->PositionError.Z();
+      t = particle->InitialPosition.T();
+      t_err = particle->PositionError.T();
+
+      distanceCharged = pow((zvtx - z),2)/pow((zvtx_err - z_err),2) + pow((tvtx - t),2)/pow((tvtx_err - t_err),2);
+      distanceNeutral = pow((tvtx - t),2)/pow((tvtx_err - t_err),2);
+
+      if(candidate->Charge != 0 && distanceCharged < fChargedMinSignificance)
+      {
+        candidate->IsRecoPU = 1;
+      }
+      else if(candidate->Charge == 0 && distanceNeutral < fNeutralMinSignificance)
+      {
+        candidate->IsRecoPU = 1;
+      }  
+      else
+      {
+        candidate->IsRecoPU = 0;
+        if(candidate->Momentum.Pt() > fPTMin) array->Add(candidate);
+      }
+    }
+  }
+}
Index: modules/PileUpSubtractor4D.h
===================================================================
--- modules/PileUpSubtractor4D.h	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
+++ modules/PileUpSubtractor4D.h	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
@@ -0,0 +1,65 @@
+/*
+ *  Delphes: a framework for fast simulation of a generic collider experiment
+ *  Copyright (C) 2012-2014  Universite catholique de Louvain (UCL), Belgium
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PileUpSubtractor4D_h
+#define PileUpSubtractor4D_h
+
+/** \class TrackPileUpSubtractor
+ *
+ *  Subtract pile-up contribution from tracks.
+ *
+ *  \author P. Demin - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "classes/DelphesModule.h"
+
+#include <map>
+
+class TIterator;
+class TObjArray;
+class DelphesFormula;
+
+class PileUpSubtractor4D: public DelphesModule
+{
+public:
+  PileUpSubtractor4D();
+  ~PileUpSubtractor4D();
+
+  void Init();
+  void Process();
+  void Finish();
+
+private:
+  DelphesFormula *fFormula; //!
+
+  Double_t fChargedMinSignificance;
+  Double_t fNeutralMinSignificance;
+
+  Double_t fPTMin;
+
+  std::map<TIterator *, TObjArray *> fInputMap; //!
+
+  ClassDef(PileUpSubtractor4D, 1)
+
+    TIterator *fItVertexInputArray; //!
+
+  const TObjArray *fVertexInputArray; //!
+};
+
+#endif
Index: modules/TimeSmearing.cc
===================================================================
--- modules/TimeSmearing.cc	(revision 93bf9c6a8f6c496f741ded3ceabb7781bb21e7be)
+++ modules/TimeSmearing.cc	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
@@ -53,6 +53,7 @@
 
 TimeSmearing::TimeSmearing() :
-  fItInputArray(0)
+  fItInputArray(0), fFormula(0)
 {
+	fFormula = new DelphesFormula;
 }
 
@@ -61,4 +62,5 @@
 TimeSmearing::~TimeSmearing()
 {  
+	if(fFormula) delete fFormula;
 }
 
@@ -67,7 +69,6 @@
 void TimeSmearing::Init()
 {
-  // read resolution formula
-
-  fTimeResolution = GetDouble("TimeResolution", 1.);
+  // read resolution formula	
+  fFormula->Compile(GetString("TimeResolution", "0.001"));
 
   // import input array
@@ -94,10 +95,9 @@
   Double_t ti, tf_smeared, tf;
   Double_t pt, eta, phi, e, p, l;
-  Double_t sigma_t, beta_particle;
+  Double_t beta_particle;
+  Double_t timeResolution;
 
 
   const Double_t c_light = 2.99792458E8;
-
-  cout << " STARTINNNGG ---------->" << endl;
 
   fItInputArray->Reset();
@@ -117,24 +117,23 @@
     beta_particle = p/e;
     l = candidate->L;
-    // apply smearing formula
-
+    timeResolution = fFormula->Eval(e);
+    
     if(candidate->Charge != 0)
     { 
-      tf_smeared = tf + fTimeResolution*gRandom->Gaus(0, 1);
-      //mother = candidate;
-      //candidate = static_cast<Candidate*>(candidate->Clone());  // I am not sure that we need these lines !!!
-      //candidate->AddCandidate(mother);
+      tf_smeared = tf + timeResolution*gRandom->Gaus(0, 1);
+      mother = candidate;
+      candidate = static_cast<Candidate*>(candidate->Clone());  // I am not sure that we need these lines !!!
+      candidate->AddCandidate(mother);
       candidate->InitialPosition.SetT((100+ti)*1.0E3*c_light);
       candidate->Position.SetT(tf_smeared*1.0E3*c_light);
-      candidate->ErrorT = fTimeResolution*1.0E3*c_light;
+      candidate->ErrorT = timeResolution*1.0E3*c_light;
       fOutputArray->Add(candidate);
     }
     else
     {
-    	sigma_t = sqrt(pow(20,2) + pow(150,2)/e);
-    	ti = sigma_t - l*1.0E3/(c_light*beta_particle);   
-    	candidate->InitialPosition.SetT(ti);
-    	candidate->ErrorT = sigma_t*1.0E3*c_light;		// Do we need to sum with 100 like in upside ?
-    	fOutputArray->Add(candidate);
+      ti = timeResolution - l*1.0E3/(c_light*beta_particle);   
+      candidate->InitialPosition.SetT(ti);
+      candidate->ErrorT = timeResolution*1.0E3*c_light;		// Do we need to sum with 100 like in upside ?
+      fOutputArray->Add(candidate);
     }
    
Index: modules/TimeSmearing.h
===================================================================
--- modules/TimeSmearing.h	(revision 93bf9c6a8f6c496f741ded3ceabb7781bb21e7be)
+++ modules/TimeSmearing.h	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
@@ -45,5 +45,5 @@
 
 private:
-  Double_t fTimeResolution; //!
+  DelphesFormula *fFormula;
   Double_t fEtaMax;
 
Index: modules/TrackPileUpSubtractor.cc
===================================================================
--- modules/TrackPileUpSubtractor.cc	(revision 93bf9c6a8f6c496f741ded3ceabb7781bb21e7be)
+++ modules/TrackPileUpSubtractor.cc	(revision 604967294938e176fe5ff094b70b0e8926c39f9d)
@@ -117,5 +117,5 @@
 void TrackPileUpSubtractor::Process()
 {
-  Candidate *candidate, *particle, *particleTest;
+  Candidate *candidate, *particle;
   map<TIterator *, TObjArray *>::iterator itInputMap;
   TIterator *iterator;
@@ -123,37 +123,14 @@
   Double_t z, zvtx = 0;
   Double_t pt, eta, phi, e;
-  Double_t sumPT2 = 0;
-  Double_t sumSquare = 0;
-  int counter = 0;
-  Double_t tempPTSquare = 0;
-  Double_t tempZVertex = 0; 
+
   // find z position of primary vertex
 
-  cout << " ---------- NEW EVENT --------- " << endl; 
-
   fItVertexInputArray->Reset();
-  cout << " NUMBER OF VERTICES : " << fVertexInputArray->GetEntriesFast() << endl;
   while((candidate = static_cast<Candidate *>(fItVertexInputArray->Next())))
   {
-      cout << " ---------- NEW VERTEX --------- " << candidate->IsPU << endl; 
-      tempZVertex = candidate->Position.Z();
-      tempPTSquare = candidate->SumPT2;
-      if(tempPTSquare > sumSquare)
-      {
-        sumSquare = tempPTSquare;
-        zvtx = tempZVertex;
-        cout << " Sum Square : " << sumSquare << " Z of Vertex : " << zvtx << "  Is PileUp : " << candidate->IsPU << endl;
-      } 
-      
-      /*
-      for(int i = 0; i < candidate->GetCandidates()->GetEntriesFast(); i++)
-      {
-        particleTest = static_cast<Candidate *>(candidate->GetCandidates()->At(i));
-        TLorentzVector &candidateMomentumNew = particleTest->Momentum;
-        if(candidateMomentumNew.Pt() > 1.0)
-          sumSquare += (candidateMomentumNew.Pt()*candidateMomentumNew.Pt());
-        cout << candidateMomentumNew.Pt() << "  " << candidate->SumPT2 << " counter : " << candidate->GetCandidates()->GetEntriesFast() << endl;
-      }  
-      */
+    if(!candidate->IsPU)
+    {
+      zvtx = candidate->Position.Z();
+    }
   }
 
@@ -169,5 +146,5 @@
     {
       particle = static_cast<Candidate *>(candidate->GetCandidates()->At(0));
-      const TLorentzVector &candidateMomentum = candidate->Momentum;
+      const TLorentzVector &candidateMomentum = particle->Momentum;
 
       eta = candidateMomentum.Eta();
@@ -176,20 +153,12 @@
       e = candidateMomentum.E();
 
-      z = candidate->Position.Z();
-      counter ++; 
-      //sum = pt*pt;
-      if(pt > 1.0)
-        sumPT2 += pt*pt;
+      z = particle->Position.Z();
 
       // apply pile-up subtraction
       // assume perfect pile-up subtraction for tracks outside fZVertexResolution
-      // cout << particle->IsRecoPU << " ParticleSumSquare : " << sumPT2 << " VertexResult : " << sumSquare << " Added SumSquare : " << zvtx << endl;
-      //cout << pt << "  " << candidate->IsPU << "  " <<  TMath::Abs(z - zvtx) << "   " << sumPT2 << "   " << sumPT2 << endl;
 
-      
-      if(particle->Charge != 0 && TMath::Abs(z - zvtx) > fFormula->Eval(pt, eta, phi, e) * 1.0e3)
+      if(candidate->Charge != 0 && candidate->IsPU && TMath::Abs(z - zvtx) > fFormula->Eval(pt, eta, phi, e) * 1.0e3)
       {
         candidate->IsRecoPU = 1;
-        //cout <<  TMath::Abs(z - zvtx) << "   " << fFormula->Eval(pt, eta, phi, e) * 1.0e3 << endl;
       }
       else
