Index: trunk/modules/ImpactParameterSmearing.cc
===================================================================
--- trunk/modules/ImpactParameterSmearing.cc	(revision 1367)
+++ trunk/modules/ImpactParameterSmearing.cc	(revision 1367)
@@ -0,0 +1,144 @@
+/** \class ImpactParameterSmearing
+ *
+ *  Performs transverse impact parameter smearing.
+ *
+ *  $Date: 2014-16-03 14:57:44 +0100   
+ *
+ *
+ *  \author M. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+ 
+
+#include "modules/ImpactParameterSmearing.h"
+
+#include "classes/DelphesClasses.h"
+#include "classes/DelphesFactory.h"
+#include "classes/DelphesFormula.h"
+
+#include "ExRootAnalysis/ExRootResult.h"
+#include "ExRootAnalysis/ExRootFilter.h"
+#include "ExRootAnalysis/ExRootClassifier.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;
+
+//------------------------------------------------------------------------------
+
+ImpactParameterSmearing::ImpactParameterSmearing() :
+  fFormula(0), fItInputArray(0)
+{
+  fFormula = new DelphesFormula;
+}
+
+//------------------------------------------------------------------------------
+
+ImpactParameterSmearing::~ImpactParameterSmearing()
+{
+  if(fFormula) delete fFormula;
+}
+
+//------------------------------------------------------------------------------
+
+void ImpactParameterSmearing::Init()
+{
+  // read resolution formula
+
+  fFormula->Compile(GetString("ResolutionFormula", "0.0"));
+
+  // import input array
+
+  fInputArray = ImportArray(GetString("InputArray", "TrackMerger/tracks"));
+  fItInputArray = fInputArray->MakeIterator();
+
+  // create output array
+
+  fOutputArray = ExportArray(GetString("OutputArray", "tracks"));
+}
+
+//------------------------------------------------------------------------------
+
+void ImpactParameterSmearing::Finish()
+{
+  if(fItInputArray) delete fItInputArray;
+}
+
+//------------------------------------------------------------------------------
+
+void ImpactParameterSmearing::Process()
+{
+  Candidate *candidate, *particle, *mother;
+  Double_t xd, yd, zd, dxy, dz, sx, sy, sz, ddxy, ddz;
+  Double_t pt, eta, px, py, ang_mom;
+
+ // cout<<"New event"<<endl;
+
+  fItInputArray->Reset();
+  while((candidate = static_cast<Candidate*>(fItInputArray->Next())))
+  {
+  
+    //take momentum before smearing (otherwise apply double smearing on dxy)
+    particle = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
+  
+    const TLorentzVector &candidateMomentum = particle->Momentum;
+    //  const TLorentzVector &candidateMomentum = candidate->Momentum;
+    
+    eta = candidateMomentum.Eta();
+    pt = candidateMomentum.Pt();
+    px = candidateMomentum.Px();
+    py = candidateMomentum.Py();
+      
+    // calculate coordinates of closest approach to track circle in transverse plane xd, yd, zd
+    xd =  candidate->Xd;
+    yd =  candidate->Yd;
+    zd =  candidate->Zd;
+   
+    // calculate smeared values   
+    sx = gRandom->Gaus(0,fFormula->Eval(pt, eta));
+    sy = gRandom->Gaus(0,fFormula->Eval(pt, eta));
+    sz = gRandom->Gaus(0,fFormula->Eval(pt, eta));
+     
+    xd += sx;
+    yd += sy;
+    zd += sz; 
+     
+    // calculate impact paramater (after-smearing)
+    ang_mom = (xd*py - yd*px);
+    dxy = ang_mom/pt;
+    dz = zd;
+  
+    ddxy = gRandom->Gaus(0,fFormula->Eval(pt, eta));
+    ddz  = gRandom->Gaus(0,fFormula->Eval(pt, eta));
+  
+    //fill smeared values in candidate
+    mother = candidate;
+    
+    candidate = static_cast<Candidate*>(candidate->Clone());
+    candidate -> Xd = xd;
+    candidate -> Yd = yd;
+    candidate -> Zd = zd;
+    
+    candidate -> Dxy = dxy;
+    candidate -> SDxy = ddxy;
+      
+    candidate->AddCandidate(mother);
+    fOutputArray->Add(candidate);
+    
+    
+    
+  }
+}
+
+//------------------------------------------------------------------------------
Index: trunk/modules/ImpactParameterSmearing.h
===================================================================
--- trunk/modules/ImpactParameterSmearing.h	(revision 1367)
+++ trunk/modules/ImpactParameterSmearing.h	(revision 1367)
@@ -0,0 +1,45 @@
+#ifndef ImpactParameterSmearing_h
+#define ImpactParameterSmearing_h
+
+/** \class ImpactParameterSmearing
+ *
+ *  Performs transverse impact parameter smearing.
+ *
+ *  $Date: 2014-16-03 14:57:44 +0100   
+ *
+ *
+ *  \author M. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "classes/DelphesModule.h"
+
+class TIterator;
+class TObjArray;
+class DelphesFormula;
+
+class ImpactParameterSmearing: public DelphesModule
+{
+public:
+
+  ImpactParameterSmearing();
+  ~ImpactParameterSmearing();
+
+  void Init();
+  void Process();
+  void Finish();
+
+private:
+
+  DelphesFormula *fFormula; //!
+
+  TIterator *fItInputArray; //!
+
+  const TObjArray *fInputArray; //!
+  
+  TObjArray *fOutputArray; //!
+
+  ClassDef(ImpactParameterSmearing, 1)
+};
+
+#endif
Index: trunk/modules/ModulesLinkDef.h
===================================================================
--- trunk/modules/ModulesLinkDef.h	(revision 1366)
+++ trunk/modules/ModulesLinkDef.h	(revision 1367)
@@ -19,4 +19,5 @@
 #include "modules/EnergySmearing.h"
 #include "modules/MomentumSmearing.h"
+#include "modules/ImpactParameterSmearing.h"
 #include "modules/TimeSmearing.h"
 #include "modules/Calorimeter.h"
@@ -24,4 +25,5 @@
 #include "modules/EnergyScale.h"
 #include "modules/UniqueObjectFinder.h"
+#include "modules/TrackCountingBTagging.h"
 #include "modules/BTagging.h"
 #include "modules/TauTagging.h"
@@ -53,4 +55,5 @@
 #pragma link C++ class EnergySmearing+;
 #pragma link C++ class MomentumSmearing+;
+#pragma link C++ class ImpactParameterSmearing+;
 #pragma link C++ class TimeSmearing+;
 #pragma link C++ class Calorimeter+;
@@ -58,4 +61,5 @@
 #pragma link C++ class EnergyScale+;
 #pragma link C++ class UniqueObjectFinder+;
+#pragma link C++ class TrackCountingBTagging+;
 #pragma link C++ class BTagging+;
 #pragma link C++ class TauTagging+;
Index: trunk/modules/ParticlePropagator.cc
===================================================================
--- trunk/modules/ParticlePropagator.cc	(revision 1366)
+++ trunk/modules/ParticlePropagator.cc	(revision 1367)
@@ -106,5 +106,6 @@
   Double_t tmp, discr, discr2;
   Double_t delta, gammam, omega, asinrho;
-
+  Double_t ang_mom, rcu, rc2, dxy, xd, yd, zd;
+  
   const Double_t c_light = 2.99792458E8;
 
@@ -214,4 +215,19 @@
       if(x_c < 0.0) phi += TMath::Pi();
 
+      rcu = TMath::Abs(r);
+      rc2 = r_c*r_c;
+     
+      // calculate coordinates of closest approach to track circle in transverse plane xd, yd, zd
+      xd = x_c*x_c*x_c - x_c*rcu*r_c + x_c*y_c*y_c; 
+      xd  = ( rc2 > 0.0 ) ? xd / rc2 : -999;
+      yd  = y_c*(-rcu*r_c + rc2);
+      yd  = ( rc2 > 0.0 ) ? yd / rc2 : -999;
+      zd  = z + (TMath::Sqrt(xd*xd+yd*yd) - TMath::Sqrt(x*x+y*y))*pz/pt;
+
+      // calculate impact paramater
+      ang_mom = (xd*py - yd*px);
+      dxy = ang_mom/pt;
+    
+         
       // 3. time evaluation t = TMath::Min(t_r, t_z)
       //    t_r : time to exit from the sides
@@ -267,5 +283,9 @@
 
         candidate->Momentum = candidateMomentum;
-        candidate->AddCandidate(mother);
+	candidate->Xd = xd*1.0E3; 
+	candidate->Yd = yd*1.0E3;
+        candidate->Zd = zd*1.0E3;
+	
+	candidate->AddCandidate(mother);
 
         fOutputArray->Add(candidate);
@@ -287,2 +307,3 @@
 
 //------------------------------------------------------------------------------
+
Index: trunk/modules/TrackCountingBTagging.cc
===================================================================
--- trunk/modules/TrackCountingBTagging.cc	(revision 1367)
+++ trunk/modules/TrackCountingBTagging.cc	(revision 1367)
@@ -0,0 +1,137 @@
+
+/** \class TrackCountingBTagging
+ *
+ *  btagging algorithm based on counting tracks with large impact parameterDetermines origin of jet,
+ *
+ *  $Date: 2014-03-27 12:39:14 +0200 (Fri, 27 March 2014) $
+ *  $Revision: 1099 $
+ *
+ *
+ *  \author M. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "modules/TrackCountingBTagging.h"
+
+#include "classes/DelphesClasses.h"
+#include "classes/DelphesFactory.h"
+#include "classes/DelphesFormula.h"
+
+#include "TMath.h"
+#include "TString.h"
+#include "TFormula.h"
+#include "TRandom3.h"
+#include "TObjArray.h"
+#include "TLorentzVector.h"
+
+#include <algorithm> 
+#include <stdexcept>
+#include <iostream>
+#include <sstream>
+
+using namespace std;
+
+
+//------------------------------------------------------------------------------
+
+TrackCountingBTagging::TrackCountingBTagging() :
+  fItTrackInputArray(0), fItJetInputArray(0)
+{
+}
+
+//------------------------------------------------------------------------------
+
+TrackCountingBTagging::~TrackCountingBTagging()
+{
+}
+
+//------------------------------------------------------------------------------
+
+void TrackCountingBTagging::Init()
+{
+ 
+  fBitNumber = GetInt("BitNumber", 0);
+  
+  fPtMin     = GetDouble("TrackPtMin", 1.0);
+  fDeltaR    = GetDouble("DeltaR", 0.3);
+  fIPmax     = GetDouble("TrackIPMax", 2.0);
+  
+  fSigMin    = GetDouble("SigMin", 6.5);
+  fNtracks   = GetInt("Ntracks", 3); 
+   
+  // import input array(s)
+
+  fTrackInputArray = ImportArray(GetString("TrackInputArray", "Calorimeter/eflowTracks"));
+  fItTrackInputArray = fTrackInputArray->MakeIterator();
+  
+  fJetInputArray = ImportArray(GetString("JetInputArray", "FastJetFinder/jets"));
+  fItJetInputArray = fJetInputArray->MakeIterator();
+}
+
+//------------------------------------------------------------------------------
+
+void TrackCountingBTagging::Finish()
+{
+  if(fItTrackInputArray) delete fItTrackInputArray;
+  if(fItJetInputArray) delete fItJetInputArray;
+}
+
+//------------------------------------------------------------------------------
+
+void TrackCountingBTagging::Process()
+{
+  Candidate *jet, *track;
+  
+  Double_t jpx, jpy;
+  Double_t dr, tpx, tpy, tpt;
+  Double_t xd, yd, dxy, ddxy, ip, sip;
+ 
+  Int_t sign;
+ 
+  Int_t count;
+ 
+  // loop over all input jets
+  fItJetInputArray->Reset();
+  while((jet = static_cast<Candidate*>(fItJetInputArray->Next())))
+  {
+    const TLorentzVector &jetMomentum = jet->Momentum;
+    jpx = jetMomentum.Px();
+    jpy = jetMomentum.Py();
+    
+    // loop over all input tracks
+    fItTrackInputArray->Reset();
+    count = 0;
+    while((track = static_cast<Candidate*>(fItTrackInputArray->Next())))
+    {
+     const TLorentzVector &trkMomentum = track->Momentum;
+     
+     dr = jetMomentum.DeltaR(trkMomentum);
+     
+     tpt = trkMomentum.Pt();
+     tpx = trkMomentum.Px();
+     tpy = trkMomentum.Py();
+    
+     xd   = track->Xd;
+     yd   = track->Yd;
+     dxy  = TMath::Abs(track->Dxy);
+     ddxy = track->SDxy;
+     
+     if( tpt < fPtMin ) continue;
+     if( dr  > fDeltaR ) continue;
+     if( dxy > fIPmax ) continue;
+        
+     sign  = ( jpx*xd + jpy*yd > 0.0 ) ? 1 : -1;
+	
+     ip  = sign*dxy;
+     sip = ip / TMath::Abs(ddxy);
+     
+     if( sip > fSigMin ) count++;
+     
+    }
+     
+    // set BTag flag to true if count >= Ntracks
+    jet->BTag |= ( count >= fNtracks ) << fBitNumber;
+  }
+}
+
+//------------------------------------------------------------------------------
Index: trunk/modules/TrackCountingBTagging.h
===================================================================
--- trunk/modules/TrackCountingBTagging.h	(revision 1367)
+++ trunk/modules/TrackCountingBTagging.h	(revision 1367)
@@ -0,0 +1,53 @@
+#ifndef TrackCountingBTagging_h
+#define TrackCountingBTagging_h
+
+/** \class TrackCountingBTagging
+ *
+ *  btagging algorithm based on counting tracks with large impact parameter
+ *
+ *  $Date: 2014-03-27 12:39:14 +0200 (Fri, 27 March 2014) $
+ *  $Revision: 1099 $
+ *
+ *
+ *  \author M. Selvaggi - UCL, Louvain-la-Neuve
+ *
+ */
+
+#include "classes/DelphesModule.h"
+
+#include <map>
+
+class TObjArray;
+class DelphesFormula;
+
+class TrackCountingBTagging: public DelphesModule
+{
+public:
+
+  TrackCountingBTagging();
+  ~TrackCountingBTagging();
+
+  void Init();
+  void Process();
+  void Finish();
+
+private:
+
+  Int_t fBitNumber;
+  
+  Double_t fPtMin;
+  Double_t fDeltaR;
+  Double_t fIPmax;
+  Double_t fSigMin;
+  Int_t    fNtracks;
+  
+  TIterator *fItTrackInputArray; //!
+  TIterator *fItJetInputArray; //!
+
+  const TObjArray *fTrackInputArray; //!
+  const TObjArray *fJetInputArray; //!
+  
+  ClassDef(TrackCountingBTagging, 1)
+};
+
+#endif
Index: trunk/modules/TreeWriter.cc
===================================================================
--- trunk/modules/TreeWriter.cc	(revision 1366)
+++ trunk/modules/TreeWriter.cc	(revision 1367)
@@ -277,5 +277,11 @@
     entry->ZOuter = position.Z();
     entry->TOuter = position.T()*1.0E-3/c_light;
-
+    
+    entry->Dxy = candidate->Dxy;
+    entry->SDxy = candidate->SDxy ;
+    entry->Xd = candidate -> Xd;
+    entry->Yd = candidate -> Yd;
+    entry->Zd = candidate -> Zd;
+   
     const TLorentzVector &momentum = candidate->Momentum;
 
