]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWGHF/vertexingHF/AliAnalysisTaskSELc2V0bachelorTMVA.cxx
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGHF / vertexingHF / AliAnalysisTaskSELc2V0bachelorTMVA.cxx
old mode 100644 (file)
new mode 100755 (executable)
index e47ec54..000a1fd
@@ -36,6 +36,8 @@
 #include <TTree.h>
 #include "TROOT.h"
 #include <TDatabasePDG.h>
+#include "TVector3.h"
+
 #include <AliAnalysisDataSlot.h>
 #include <AliAnalysisDataContainer.h>
 #include "AliStack.h"
@@ -64,7 +66,8 @@
 #include "AliAODRecoDecayHF3Prong.h"
 #include "AliKFParticle.h"
 #include "AliKFVertex.h"
-#include "AliVertexingHFUtils.h"
+#include "AliExternalTrackParam.h"
+#include "AliESDtrack.h"
 
 using std::cout;
 using std::endl;
@@ -93,10 +96,10 @@ AliAnalysisTaskSE(),
   fHistoLc(0),
   fHistoLcOnTheFly(0),
   fFillOnlySgn(kFALSE),
-fHistoLcBeforeCuts(0),
-fHistoFiducialAcceptance(0), 
-fHistoCodesSgn(0),
-fHistoCodesBkg(0),
+  fHistoLcBeforeCuts(0),
+  fHistoFiducialAcceptance(0), 
+  fHistoCodesSgn(0),
+  fHistoCodesBkg(0),
   fHistoLcpKpiBeforeCuts(0),
   fVtx1(0),
 
@@ -178,8 +181,17 @@ fHistoCodesBkg(0),
   fCallKFVertexing(kFALSE),
   fKeepingOnlyHIJINGBkg(kFALSE),
   fUtils(0),
-  fHistoBackground(0) 
-
+  fHistoBackground(0),
+  fCutKFChi2NDF(999999.),
+  fCutKFDeviationFromVtx(999999.),
+  fCutKFDeviationFromVtxV0(0.),
+  fCurrentEvent(-1),
+  fBField(0),
+  fKeepingOnlyPYTHIABkg(kFALSE),
+  fHistoMCLcK0SpGen(0x0),
+  fHistoMCLcK0SpGenAcc(0x0),
+  fHistoMCLcK0SpGenLimAcc(0x0),
+  fTriggerMask(0)
 {
   //
   // Default ctor
@@ -293,9 +305,17 @@ AliAnalysisTaskSELc2V0bachelorTMVA::AliAnalysisTaskSELc2V0bachelorTMVA(const Cha
   fCallKFVertexing(kFALSE),
   fKeepingOnlyHIJINGBkg(kFALSE),
   fUtils(0),
-  fHistoBackground(0) 
-
-
+  fHistoBackground(0),  
+  fCutKFChi2NDF(999999.),
+  fCutKFDeviationFromVtx(999999.),
+  fCutKFDeviationFromVtxV0(0.),
+  fCurrentEvent(-1),
+  fBField(0),
+  fKeepingOnlyPYTHIABkg(kFALSE),
+  fHistoMCLcK0SpGen(0x0),
+  fHistoMCLcK0SpGenAcc(0x0),
+  fHistoMCLcK0SpGenLimAcc(0x0),
+  fTriggerMask(0)
 {
   //
   // Constructor. Initialization of Inputs and Outputs
@@ -382,7 +402,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::Init() {
   fListCuts->Add(new AliRDHFCutsLctoV0(*fAnalCuts));
   PostData(3,fListCuts);
 
-  if (fUseMCInfo && fKeepingOnlyHIJINGBkg) fUtils = new AliVertexingHFUtils();
+  if (fUseMCInfo && (fKeepingOnlyHIJINGBkg || fKeepingOnlyPYTHIABkg)) fUtils = new AliVertexingHFUtils();
 
   return;
 }
@@ -394,7 +414,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::Terminate(Option_t*)
   // a query. It always runs on the client, it can be used to present
   // the results graphically or save the results to file.
   
-  //AliInfo("Terminate","");
+  AliInfo("Terminate");
   AliAnalysisTaskSE::Terminate();
   
   fOutput = dynamic_cast<TList*> (GetOutputData(1));
@@ -402,6 +422,27 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::Terminate(Option_t*)
     AliError("fOutput not available");
     return;
   }
+
+  
+  //AliDebug(2, Form("At MC level, %f Lc --> K0S + p were found", fHistoMCLcK0SpGen->GetEntries()));
+  //AliDebug(2, Form("At MC level, %f Lc --> K0S + p were found in the acceptance", fHistoMCLcK0SpGenAcc->GetEntries()));
+  //AliDebug(2, Form("At Reco level, %lld Lc --> K0S + p were found", fVariablesTreeSgn->GetEntries()));
+  if(fHistoMCLcK0SpGen) {
+    AliInfo(Form("At MC level, %f Lc --> K0S + p were found", fHistoMCLcK0SpGen->GetEntries()));
+  } else {
+    AliInfo("fHistoMCLcK0SpGen not available");
+  }
+  if(fHistoMCLcK0SpGenAcc) {
+    AliInfo(Form("At MC level, %f Lc --> K0S + p were found in the acceptance", fHistoMCLcK0SpGenAcc->GetEntries()));
+  } else {
+    AliInfo("fHistoMCLcK0SpGenAcc not available");
+  }
+  if(fVariablesTreeSgn) {
+    AliInfo(Form("At Reco level, %lld Lc --> K0S + p were found", fVariablesTreeSgn->GetEntries()));
+  } else {
+    AliInfo("fVariablesTreeSgn not available");
+  }    
+  
   fOutputKF = dynamic_cast<TList*> (GetOutputData(6));
   if (!fOutputKF) {     
     AliError("fOutputKF not available");
@@ -426,7 +467,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   const char* nameoutput = GetOutputSlot(1)->GetContainer()->GetName();
   fVariablesTreeSgn = new TTree(Form("%s_Sgn", nameoutput), "Candidates variables tree, Signal");
   fVariablesTreeBkg = new TTree(Form("%s_Bkg", nameoutput), "Candidates variables tree, Background");
-  Int_t nVar = 66;
+  Int_t nVar = 86;
   fCandidateVariables = new Float_t [nVar];
   TString * fCandidateVariableNames = new TString[nVar];
   fCandidateVariableNames[0]="massLc2K0Sp";
@@ -456,8 +497,8 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   fCandidateVariableNames[24]="LcEta";
   fCandidateVariableNames[25]="V0positiveEta";
   fCandidateVariableNames[26]="V0negativeEta";
-  fCandidateVariableNames[27]="combinedPionProb";
-  fCandidateVariableNames[28]="combinedKaonProb";
+  fCandidateVariableNames[27]="TPCProtonProb";
+  fCandidateVariableNames[28]="TOFProtonProb";
   fCandidateVariableNames[29]="bachelorEta";
   fCandidateVariableNames[30]="LcP";
   fCandidateVariableNames[31]="bachelorP";
@@ -501,6 +542,34 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   fCandidateVariableNames[64]="alphaArm";
   fCandidateVariableNames[65]="ptArm";
 
+  fCandidateVariableNames[66]="ITSrefitV0pos";
+  fCandidateVariableNames[67]="ITSrefitV0neg";
+
+  fCandidateVariableNames[68]="TPCClV0pos";
+  fCandidateVariableNames[69]="TPCClV0neg";
+
+  fCandidateVariableNames[70]="v0Xcoord";
+  fCandidateVariableNames[71]="v0Ycoord";
+  fCandidateVariableNames[72]="v0Zcoord";
+  fCandidateVariableNames[73]="primVtxX";
+  fCandidateVariableNames[74]="primVtxY";
+  fCandidateVariableNames[75]="primVtxZ";
+
+  fCandidateVariableNames[76]="ITSclBach";
+  fCandidateVariableNames[77]="SPDclBach";
+
+  fCandidateVariableNames[78]="ITSclV0pos";
+  fCandidateVariableNames[79]="SPDclV0pos";
+  fCandidateVariableNames[80]="ITSclV0neg";
+  fCandidateVariableNames[81]="SPDclV0neg";
+
+  fCandidateVariableNames[82]="alphaArmLc";
+  fCandidateVariableNames[83]="alphaArmLcCharge";
+  fCandidateVariableNames[84]="ptArmLc";
+  
+  fCandidateVariableNames[85]="CosThetaStar";
+
+
   for(Int_t ivar=0; ivar<nVar; ivar++){
     fVariablesTreeSgn->Branch(fCandidateVariableNames[ivar].Data(),&fCandidateVariables[ivar],Form("%s/f",fCandidateVariableNames[ivar].Data()));
     fVariablesTreeBkg->Branch(fCandidateVariableNames[ivar].Data(),&fCandidateVariables[ivar],Form("%s/f",fCandidateVariableNames[ivar].Data()));
@@ -555,8 +624,8 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
     fHistoLcpKpiBeforeCuts->GetXaxis()->SetBinLabel(ibin, labelBeforeCuts[ibin-1].Data());
   }
 
-  fHistoBackground = new TH1F("fHistoBackground", "fHistoBackground", 2, -0.5, 1.5);
-  TString labelBkg[2] = {"Injected", "Non-injected"};
+  fHistoBackground = new TH1F("fHistoBackground", "fHistoBackground", 4, -0.5, 3.5);
+  TString labelBkg[4] = {"Injected", "Non-injected", "Non-PYTHIA", "PYTHIA"};
   for (Int_t ibin = 1; ibin <= fHistoBackground->GetNbinsX(); ibin++){
     fHistoBackground->GetXaxis()->SetBinLabel(ibin, labelBkg[ibin-1].Data());
   }
@@ -564,6 +633,12 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   //fOutput->Add(fVariablesTreeSgn);
   //fOutput->Add(fVariablesTreeBkg);
 
+  const Float_t ptbins[15] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 12., 17., 25., 35.};
+
+  fHistoMCLcK0SpGen = new TH1F("fHistoMCLcK0SpGen", "fHistoMCLcK0SpGen", 14, ptbins);
+  fHistoMCLcK0SpGenAcc = new TH1F("fHistoMCLcK0SpGenAcc", "fHistoMCLcK0SpGenAcc", 14, ptbins);
+  fHistoMCLcK0SpGenLimAcc = new TH1F("fHistoMCLcK0SpGenLimAcc", "fHistoMCLcK0SpGenLimAcc", 14, ptbins);
+
   fOutput->Add(fHistoEvents);
   fOutput->Add(fHistoLc);
   fOutput->Add(fHistoLcOnTheFly);
@@ -573,6 +648,9 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   fOutput->Add(fHistoCodesBkg);
   fOutput->Add(fHistoLcpKpiBeforeCuts);
   fOutput->Add(fHistoBackground);
+  fOutput->Add(fHistoMCLcK0SpGen);
+  fOutput->Add(fHistoMCLcK0SpGenAcc);
+  fOutput->Add(fHistoMCLcK0SpGenLimAcc);
 
   PostData(1, fOutput);
   PostData(4, fVariablesTreeSgn);
@@ -599,177 +677,177 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserCreateOutputObjects() {
   fPIDCombined->SetDetectorMask(AliPIDResponse::kDetTPC+AliPIDResponse::kDetTOF);
   //fPIDCombined->SetPriorDistribution((AliPID::EParticleType)ispec,fPriors[ispec]);
 
-  //Printf("Here ok!");
-
   fCounter = new AliNormalizationCounter("NormalizationCounter");
   fCounter->Init();
   PostData(2, fCounter);
 
   // Histograms from KF
 
-  fHistoDistanceLcToPrimVtx    = new TH1D("fHistoDistanceLcToPrimVtx", "Lc distance to Prim Vertex from KF; distance [cm]", 1000, 0., 1);
-  fHistoDistanceV0ToPrimVtx    = new TH1D("fHistoDistanceV0ToPrimVtx", "V0 distance to Prim Vertex from KF; distance [cm]", 1000, 0., 100.);
-  fHistoDistanceV0ToLc         = new TH1D("fHistoDistanceV0ToLc", "V0 distance to Lc Vertex from KF; distance [cm]", 1000, 0., 100.);
-
-  fHistoDistanceLcToPrimVtxSgn = new TH1D("fHistoDistanceLcToPrimVtxSgn", "Lc Sgn distance to Prim Vertex from KF; distance [cm]", 1000, 0., 1);
-  fHistoDistanceV0ToPrimVtxSgn = new TH1D("fHistoDistanceV0ToPrimVtxSgn", "V0 Sgn distance to Prim Vertex from KF; distance [cm]", 1000, 0., 100.);
-  fHistoDistanceV0ToLcSgn      = new TH1D("fHistoDistanceV0ToLcSgn", "V0 Sgn distance to Lc Vertex from KF; distance [cm]", 1000, 0., 100.);
-
-  fHistoVtxLcResidualToPrimVtx = new TH1D("fHistoVtxLcResidualToPrimVtx", "Residual between MC and KF (MC - KF): Lc to Prim Vtx; distance [cm]", 1000, -5., 5.);
-  fHistoVtxV0ResidualToPrimVtx = new TH1D("fHistoVtxV0ResidualToPrimVtx", "Residual between MC and KF (MC - KF): V0 to Prim Vtx; distance [cm]", 1000, -5., 5.);
-  fHistoVtxV0ResidualToLc      = new TH1D("fHistoVtxV0ResidualToLc", "Residual between MC and KF: V0 to Lc (MC - KF); distance [cm]", 1000, -5., 5.);
-
-  fHistoMassV0All              = new TH1D("fHistoMassV0All", "V0 Mass; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0All       = new TH1D("fHistoDecayLengthV0All", "V0 Decay Length; decayLength", 500, -10, 10.0);
-  fHistoLifeTimeV0All          = new TH1D("fHistoLifeTimeV0All", "V0 Life Time; lifeTime", 500, -10.0, 10.0);
-
-  fHistoMassV0True             = new TH1D("fHistoMassV0True", "True V0 Mass; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0True      = new TH1D("fHistoDecayLengthV0True", "True V0 Decay Length; decayLength", 500, -10, 10.0);
-  fHistoLifeTimeV0True         = new TH1D("fHistoLifeTimeV0True", "True V0 Life Time; lifeTime", 500, -10.0, 10.0);
-
-  fHistoMassV0TrueFromAOD      = new TH1D("fHistoMassV0TrueFormAOD", "True V0 Mass (AOD); mass", 500, 0.4, 0.6);
-
-  fHistoMassV0TrueK0S          = new TH1D("fHistoMassV0TrueK0S", "True V0-K0S Mass; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0TrueK0S   = new TH1D("fHistoDecayLengthV0TrueK0S", "True V0-K0S Decay Length; decayLength", 500, -10, 10.0);
-  fHistoLifeTimeV0TrueK0S      = new TH1D("fHistoLifeTimeV0TrueK0S", "True V0-K0S Life Time; lifeTime", 500, -10.0, 10.0);
-
-  fHistoMassV0TrueK0SFromAOD   = new TH1D("fHistoMassV0TrueK0SFormAOD", "True V0-K0S Mass (AOD); mass", 500, 0.4, 0.6);
-
-  fHistoMassLcAll              = new TH1D("fHistoMassLcAll", "Lc Mass; mass", 500, 2.0, 3.0);
-  fHistoDecayLengthLcAll       = new TH1D("fHistoDecayLengthLcAll", "Lc Decay Length; decayLenght", 100000, -0.1, 0.1);
-  fHistoLifeTimeLcAll          = new TH1D("fHistoLifeTimeLcAll", "Lc Life Time; lifeTime", 100000, -0.1, 0.1);
-
-  fHistoMassLcTrue             = new TH1D("fHistoMassLcTrue", "True Lc Mass; mass", 500, 2.0, 3.0);
-  fHistoDecayLengthLcTrue      = new TH1D("fHistoDecayLengthLcTrue", "True Lc Decay Length; decayLength", 100000, -0.1, 0.1);
-  fHistoLifeTimeLcTrue         = new TH1D("fHistoLifeTimeLcTrue", "True Lc Life Time; lifeTime", 100000, -0.1, 0.1);
-
-  fHistoMassLcTrueFromAOD      = new TH1D("fHistoMassLcTrueFromAOD", "True Lc Mass (AOD); mass", 500, 2.0, 3.0);
-
-  fHistoMassV0fromLcAll        = new TH1D("fHistoMassV0fromLcAll", "V0 mass from Lc built in KF; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0fromLcAll = new TH1D("fHistoDecayLengthV0fromLcAll", "V0 Decay Length from Lc built in KF; decayLength", 500, 0, 10.0);
-  fHistoLifeTimeV0fromLcAll    = new TH1D("fHistoLifeTimeV0fromLcAll", "V0 Life Time from Lc built in KF; lifeTime", 500, 0.0, 3.0);
-
-  fHistoMassV0fromLcTrue       = new TH1D("fHistoMassV0fromLcTrue", "V0 mass from true Lc built in KF; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0fromLcTrue= new TH1D("fHistoDecayLengthV0fromLcTrue", "V0 Decay Length from true Lc built in KF; decayLength", 500, 0, 10.0);
-  fHistoLifeTimeV0fromLcTrue   = new TH1D("fHistoLifeTimeV0fromLcTrue", "V0 Life Time from true Lc built in KF; lifeTime", 500, 0.0, 3.0);
-
-  fHistoMassLcSgn              = new TH1D("fHistoMassLcSgn", "True Lc Signal Mass; mass", 500, 2.0, 3.0);
-  fHistoMassLcSgnFromAOD       = new TH1D("fHistoMassLcSgnFromAOD", "True Lc Signal Mass (AOD); mass", 500, 2.0, 3.0);
-  fHistoDecayLengthLcSgn       = new TH1D("fHistoDecayLengthLcSgn", "True Lc Signal Decay Length; decayLength", 100000, -0.1, 0.1);
-  fHistoLifeTimeLcSgn          = new TH1D("fHistoLifeTimeLcSgn", "True Lc Signal Life Time; lifeTime", 100000, -0.1, 0.1);
-
-  fHistoMassV0fromLcSgn        = new TH1D("fHistoMassV0fromLcSgn", "V0 from True Lc Signal Mass; mass", 500, 0.4, 0.6);
-  fHistoDecayLengthV0fromLcSgn = new TH1D("fHistoDecayLengthV0fromLcSgn", "V0 True Lc Signal Decay Length; decayLength", 500, 0, 10.0);
-  fHistoLifeTimeV0fromLcSgn    = new TH1D("fHistoLifeTimeV0fromLcSgn", "V0 True Lc Signal Life Time; lifeTime", 500, 0.0, 3.0);
-
-  fHistoKF = new TH2D("fHistoKF", "Summary from KF; V0 KF; Lc KF", 16, -0.5, 15.5, 16, -0.5, 15.5);
-  fHistoKFV0 = new TH1D("fHistoKFV0", "Summary from KF; V0 KF", 16, -0.5, 15.5);
-  fHistoKFLc = new TH1D("fHistoKFLc", "Summary from KF; V0 KF", 16, -0.5, 15.5);
-  TString axisLabel[16] = {"AllOk", "M_NotOk", "Sm_NotOk", "Dl_NotOk", 
-                          "Lt_NotOk", "M_Sm_NotOk", "M_Dl_NotOk", "M_Lt_NotOk", 
-                          "Dl_Sm_NotOk", "Dl_Lt_NotOk", "Sm_Lt_NotOk", "M_Sm_Dl_NotOk", 
-                          "M_Sm_Lt_NotOk", "Sm_Dl_Lt_NotOk", "M_Dl_Lt_NotOk", "All_NotOk"};
-
-  for (Int_t ibin = 1; ibin <=16; ibin++){
-    fHistoKF->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
-    fHistoKF->GetYaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
-    fHistoKFV0->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
-    fHistoKFLc->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
+  if (fCallKFVertexing){
+    fHistoDistanceLcToPrimVtx    = new TH1D("fHistoDistanceLcToPrimVtx", "Lc distance to Prim Vertex from KF; distance [cm]", 1000, 0., 1);
+    fHistoDistanceV0ToPrimVtx    = new TH1D("fHistoDistanceV0ToPrimVtx", "V0 distance to Prim Vertex from KF; distance [cm]", 1000, 0., 100.);
+    fHistoDistanceV0ToLc         = new TH1D("fHistoDistanceV0ToLc", "V0 distance to Lc Vertex from KF; distance [cm]", 1000, 0., 100.);
+    
+    fHistoDistanceLcToPrimVtxSgn = new TH1D("fHistoDistanceLcToPrimVtxSgn", "Lc Sgn distance to Prim Vertex from KF; distance [cm]", 1000, 0., 1);
+    fHistoDistanceV0ToPrimVtxSgn = new TH1D("fHistoDistanceV0ToPrimVtxSgn", "V0 Sgn distance to Prim Vertex from KF; distance [cm]", 1000, 0., 100.);
+    fHistoDistanceV0ToLcSgn      = new TH1D("fHistoDistanceV0ToLcSgn", "V0 Sgn distance to Lc Vertex from KF; distance [cm]", 1000, 0., 100.);
+    
+    fHistoVtxLcResidualToPrimVtx = new TH1D("fHistoVtxLcResidualToPrimVtx", "Residual between MC and KF (MC - KF): Lc to Prim Vtx; distance [cm]", 1000, -5., 5.);
+    fHistoVtxV0ResidualToPrimVtx = new TH1D("fHistoVtxV0ResidualToPrimVtx", "Residual between MC and KF (MC - KF): V0 to Prim Vtx; distance [cm]", 1000, -5., 5.);
+    fHistoVtxV0ResidualToLc      = new TH1D("fHistoVtxV0ResidualToLc", "Residual between MC and KF: V0 to Lc (MC - KF); distance [cm]", 1000, -5., 5.);
+    
+    fHistoMassV0All              = new TH1D("fHistoMassV0All", "V0 Mass; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0All       = new TH1D("fHistoDecayLengthV0All", "V0 Decay Length; decayLength", 500, -10, 10.0);
+    fHistoLifeTimeV0All          = new TH1D("fHistoLifeTimeV0All", "V0 Life Time; lifeTime", 500, -10.0, 10.0);
+    
+    fHistoMassV0True             = new TH1D("fHistoMassV0True", "True V0 Mass; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0True      = new TH1D("fHistoDecayLengthV0True", "True V0 Decay Length; decayLength", 500, -10, 10.0);
+    fHistoLifeTimeV0True         = new TH1D("fHistoLifeTimeV0True", "True V0 Life Time; lifeTime", 500, -10.0, 10.0);
+    
+    fHistoMassV0TrueFromAOD      = new TH1D("fHistoMassV0TrueFormAOD", "True V0 Mass (AOD); mass", 500, 0.4, 0.6);
+    
+    fHistoMassV0TrueK0S          = new TH1D("fHistoMassV0TrueK0S", "True V0-K0S Mass; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0TrueK0S   = new TH1D("fHistoDecayLengthV0TrueK0S", "True V0-K0S Decay Length; decayLength", 500, -10, 10.0);
+    fHistoLifeTimeV0TrueK0S      = new TH1D("fHistoLifeTimeV0TrueK0S", "True V0-K0S Life Time; lifeTime", 500, -10.0, 10.0);
+    
+    fHistoMassV0TrueK0SFromAOD   = new TH1D("fHistoMassV0TrueK0SFormAOD", "True V0-K0S Mass (AOD); mass", 500, 0.4, 0.6);
+    
+    fHistoMassLcAll              = new TH1D("fHistoMassLcAll", "Lc Mass; mass", 500, 2.0, 3.0);
+    fHistoDecayLengthLcAll       = new TH1D("fHistoDecayLengthLcAll", "Lc Decay Length; decayLenght", 100000, -0.1, 0.1);
+    fHistoLifeTimeLcAll          = new TH1D("fHistoLifeTimeLcAll", "Lc Life Time; lifeTime", 100000, -0.1, 0.1);
+    
+    fHistoMassLcTrue             = new TH1D("fHistoMassLcTrue", "True Lc Mass; mass", 500, 2.0, 3.0);
+    fHistoDecayLengthLcTrue      = new TH1D("fHistoDecayLengthLcTrue", "True Lc Decay Length; decayLength", 100000, -0.1, 0.1);
+    fHistoLifeTimeLcTrue         = new TH1D("fHistoLifeTimeLcTrue", "True Lc Life Time; lifeTime", 100000, -0.1, 0.1);
+    
+    fHistoMassLcTrueFromAOD      = new TH1D("fHistoMassLcTrueFromAOD", "True Lc Mass (AOD); mass", 500, 2.0, 3.0);
+    
+    fHistoMassV0fromLcAll        = new TH1D("fHistoMassV0fromLcAll", "V0 mass from Lc built in KF; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0fromLcAll = new TH1D("fHistoDecayLengthV0fromLcAll", "V0 Decay Length from Lc built in KF; decayLength", 500, 0, 10.0);
+    fHistoLifeTimeV0fromLcAll    = new TH1D("fHistoLifeTimeV0fromLcAll", "V0 Life Time from Lc built in KF; lifeTime", 500, 0.0, 3.0);
+    
+    fHistoMassV0fromLcTrue       = new TH1D("fHistoMassV0fromLcTrue", "V0 mass from true Lc built in KF; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0fromLcTrue= new TH1D("fHistoDecayLengthV0fromLcTrue", "V0 Decay Length from true Lc built in KF; decayLength", 500, 0, 10.0);
+    fHistoLifeTimeV0fromLcTrue   = new TH1D("fHistoLifeTimeV0fromLcTrue", "V0 Life Time from true Lc built in KF; lifeTime", 500, 0.0, 3.0);
+    
+    fHistoMassLcSgn              = new TH1D("fHistoMassLcSgn", "True Lc Signal Mass; mass", 500, 2.0, 3.0);
+    fHistoMassLcSgnFromAOD       = new TH1D("fHistoMassLcSgnFromAOD", "True Lc Signal Mass (AOD); mass", 500, 2.0, 3.0);
+    fHistoDecayLengthLcSgn       = new TH1D("fHistoDecayLengthLcSgn", "True Lc Signal Decay Length; decayLength", 100000, -0.1, 0.1);
+    fHistoLifeTimeLcSgn          = new TH1D("fHistoLifeTimeLcSgn", "True Lc Signal Life Time; lifeTime", 100000, -0.1, 0.1);
+    
+    fHistoMassV0fromLcSgn        = new TH1D("fHistoMassV0fromLcSgn", "V0 from True Lc Signal Mass; mass", 500, 0.4, 0.6);
+    fHistoDecayLengthV0fromLcSgn = new TH1D("fHistoDecayLengthV0fromLcSgn", "V0 True Lc Signal Decay Length; decayLength", 500, 0, 10.0);
+    fHistoLifeTimeV0fromLcSgn    = new TH1D("fHistoLifeTimeV0fromLcSgn", "V0 True Lc Signal Life Time; lifeTime", 500, 0.0, 3.0);
+    
+    fHistoKF = new TH2D("fHistoKF", "Summary from KF; V0 KF; Lc KF", 16, -0.5, 15.5, 16, -0.5, 15.5);
+    fHistoKFV0 = new TH1D("fHistoKFV0", "Summary from KF; V0 KF", 16, -0.5, 15.5);
+    fHistoKFLc = new TH1D("fHistoKFLc", "Summary from KF; V0 KF", 16, -0.5, 15.5);
+    TString axisLabel[16] = {"AllOk", "M_NotOk", "Sm_NotOk", "Dl_NotOk", 
+                            "Lt_NotOk", "M_Sm_NotOk", "M_Dl_NotOk", "M_Lt_NotOk", 
+                            "Dl_Sm_NotOk", "Dl_Lt_NotOk", "Sm_Lt_NotOk", "M_Sm_Dl_NotOk", 
+                            "M_Sm_Lt_NotOk", "Sm_Dl_Lt_NotOk", "M_Dl_Lt_NotOk", "All_NotOk"};
+    
+    for (Int_t ibin = 1; ibin <=16; ibin++){
+      fHistoKF->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
+      fHistoKF->GetYaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
+      fHistoKFV0->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
+      fHistoKFLc->GetXaxis()->SetBinLabel(ibin, axisLabel[ibin-1].Data());
+    }
+    
+    fHistoMassKFV0 = new TH2D("fHistoMassKFV0", "mass vs sigmaMass for V0; mass; sigmaMass", 500, 0.4, 0.6, 500, 0., 10);
+    fHistoDecayLengthKFV0 = new TH2D("fHistoDecayLengthKFV0", "decayLength vs sigmaDecayLength for V0; decayLength; sigmaDecayLength", 500, -10, 10, 500, 0., 10);
+    fHistoLifeTimeKFV0 = new TH2D("fHistoLifeTimeKFV0", "lifeTime vs sigmalifeTime for V0; lifeTime; sigmaLifeTime", 500, -10, 10, 500, 0., 10);
+    
+    fHistoMassKFLc = new TH2D("fHistoMassKFLc", "mass vs sigmaMass for Lc; mass; sigmaMass", 500, 0.4, 0.6, 500, 0., 10);
+    fHistoDecayLengthKFLc = new TH2D("fHistoDecayLengthKFLc", "decayLength vs sigmaDecayLength for Lc; decayLength; sigmaDecayLength", 500, -10, 10, 500, 0., 10);
+    fHistoLifeTimeKFLc = new TH2D("fHistoLifeTimeKFLc", "lifeTime vs sigmalifeTime for Lc; lifeTime; sigmaLifeTime", 500, -10, 10, 500, 0., 10);
+    
+    fHistoArmenterosPodolanskiV0KF = new TH2D("fHistoArmenterosPodolanskiV0KF", "V0 ArmenterosPodolanski from KF; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
+    fHistoArmenterosPodolanskiV0KFSgn = new TH2D("fHistoArmenterosPodolanskiV0KFSgn", "V0 (signal) ArmenterosPodolanski from KF; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
+    fHistoArmenterosPodolanskiV0AOD = new TH2D("fHistoArmenterosPodolanskiV0AOD", "V0 ArmenterosPodolanski from AOD; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
+    fHistoArmenterosPodolanskiV0AODSgn = new TH2D("fHistoArmenterosPodolanskiV0AODSgn", "V0 (signal) ArmenterosPodolanski from AOD; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
   }
 
-  fHistoMassKFV0 = new TH2D("fHistoMassKFV0", "mass vs sigmaMass for V0; mass; sigmaMass", 500, 0.4, 0.6, 500, 0., 10);
-  fHistoDecayLengthKFV0 = new TH2D("fHistoDecayLengthKFV0", "decayLength vs sigmaDecayLength for V0; decayLength; sigmaDecayLength", 500, -10, 10, 500, 0., 10);
-  fHistoLifeTimeKFV0 = new TH2D("fHistoLifeTimeKFV0", "lifeTime vs sigmalifeTime for V0; lifeTime; sigmaLifeTime", 500, -10, 10, 500, 0., 10);
-
-  fHistoMassKFLc = new TH2D("fHistoMassKFLc", "mass vs sigmaMass for Lc; mass; sigmaMass", 500, 0.4, 0.6, 500, 0., 10);
-  fHistoDecayLengthKFLc = new TH2D("fHistoDecayLengthKFLc", "decayLength vs sigmaDecayLength for Lc; decayLength; sigmaDecayLength", 500, -10, 10, 500, 0., 10);
-  fHistoLifeTimeKFLc = new TH2D("fHistoLifeTimeKFLc", "lifeTime vs sigmalifeTime for Lc; lifeTime; sigmaLifeTime", 500, -10, 10, 500, 0., 10);
-
-  fHistoArmenterosPodolanskiV0KF = new TH2D("fHistoArmenterosPodolanskiV0KF", "V0 ArmenterosPodolanski from KF; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
-  fHistoArmenterosPodolanskiV0KFSgn = new TH2D("fHistoArmenterosPodolanskiV0KFSgn", "V0 (signal) ArmenterosPodolanski from KF; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
-  fHistoArmenterosPodolanskiV0AOD = new TH2D("fHistoArmenterosPodolanskiV0AOD", "V0 ArmenterosPodolanski from AOD; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
-  fHistoArmenterosPodolanskiV0AODSgn = new TH2D("fHistoArmenterosPodolanskiV0AODSgn", "V0 (signal) ArmenterosPodolanski from AOD; #alpha; Qt", 1000, -1, 1, 1000, 0, 1); 
-
   fOutputKF = new TList();
   fOutputKF->SetOwner();
   fOutputKF->SetName("listHistoKF");
 
-  fOutputKF->Add(fHistoDistanceLcToPrimVtx);
-  fOutputKF->Add(fHistoDistanceV0ToPrimVtx);
-  fOutputKF->Add(fHistoDistanceV0ToLc);
-
-  fOutputKF->Add(fHistoDistanceLcToPrimVtxSgn);
-  fOutputKF->Add(fHistoDistanceV0ToPrimVtxSgn);
-  fOutputKF->Add(fHistoDistanceV0ToLcSgn);
-
-  fOutputKF->Add(fHistoVtxLcResidualToPrimVtx);
-  fOutputKF->Add(fHistoVtxV0ResidualToPrimVtx);
-  fOutputKF->Add(fHistoVtxV0ResidualToLc);
-
-  fOutputKF->Add(fHistoMassV0All);
-  fOutputKF->Add(fHistoDecayLengthV0All);
-  fOutputKF->Add(fHistoLifeTimeV0All);
-
-  fOutputKF->Add(fHistoMassV0True);
-  fOutputKF->Add(fHistoDecayLengthV0True);
-  fOutputKF->Add(fHistoLifeTimeV0True);
-
-  fOutputKF->Add(fHistoMassV0TrueFromAOD);
-
-  fOutputKF->Add(fHistoMassV0TrueK0S);
-  fOutputKF->Add(fHistoDecayLengthV0TrueK0S);
-  fOutputKF->Add(fHistoLifeTimeV0TrueK0S);
-
-  fOutputKF->Add(fHistoMassV0TrueK0SFromAOD);
-
-  fOutputKF->Add(fHistoMassLcAll);
-  fOutputKF->Add(fHistoDecayLengthLcAll);
-  fOutputKF->Add(fHistoLifeTimeLcAll);
-
-  fOutputKF->Add(fHistoMassLcTrue);
-  fOutputKF->Add(fHistoDecayLengthLcTrue);
-  fOutputKF->Add(fHistoLifeTimeLcTrue);
-
-  fOutputKF->Add(fHistoMassLcTrueFromAOD);
-
-  fOutputKF->Add(fHistoMassV0fromLcAll);
-  fOutputKF->Add(fHistoDecayLengthV0fromLcAll);
-  fOutputKF->Add(fHistoLifeTimeV0fromLcAll);
-
-  fOutputKF->Add(fHistoMassV0fromLcTrue);
-  fOutputKF->Add(fHistoDecayLengthV0fromLcTrue);
-  fOutputKF->Add(fHistoLifeTimeV0fromLcTrue);
-
-  fOutputKF->Add(fHistoMassLcSgn);
-  fOutputKF->Add(fHistoMassLcSgnFromAOD);
-  fOutputKF->Add(fHistoDecayLengthLcSgn);
-  fOutputKF->Add(fHistoLifeTimeLcSgn);
-
-  fOutputKF->Add(fHistoMassV0fromLcSgn);
-  fOutputKF->Add(fHistoDecayLengthV0fromLcSgn);
-  fOutputKF->Add(fHistoLifeTimeV0fromLcSgn);
-  
-  fOutputKF->Add(fHistoKF);
-  fOutputKF->Add(fHistoKFV0);
-  fOutputKF->Add(fHistoKFLc);
-
-  fOutputKF->Add(fHistoMassKFV0);
-  fOutputKF->Add(fHistoDecayLengthKFV0);
-  fOutputKF->Add(fHistoLifeTimeKFV0);
-
-  fOutputKF->Add(fHistoMassKFLc);
-  fOutputKF->Add(fHistoDecayLengthKFLc);
-  fOutputKF->Add(fHistoLifeTimeKFLc);
-
-  fOutputKF->Add(fHistoArmenterosPodolanskiV0KF);
-  fOutputKF->Add(fHistoArmenterosPodolanskiV0KFSgn);
-  fOutputKF->Add(fHistoArmenterosPodolanskiV0AOD);
-  fOutputKF->Add(fHistoArmenterosPodolanskiV0AODSgn);
+  if (fCallKFVertexing){
+    fOutputKF->Add(fHistoDistanceLcToPrimVtx);
+    fOutputKF->Add(fHistoDistanceV0ToPrimVtx);
+    fOutputKF->Add(fHistoDistanceV0ToLc);
+    
+    fOutputKF->Add(fHistoDistanceLcToPrimVtxSgn);
+    fOutputKF->Add(fHistoDistanceV0ToPrimVtxSgn);
+    fOutputKF->Add(fHistoDistanceV0ToLcSgn);
+    
+    fOutputKF->Add(fHistoVtxLcResidualToPrimVtx);
+    fOutputKF->Add(fHistoVtxV0ResidualToPrimVtx);
+    fOutputKF->Add(fHistoVtxV0ResidualToLc);
+    
+    fOutputKF->Add(fHistoMassV0All);
+    fOutputKF->Add(fHistoDecayLengthV0All);
+    fOutputKF->Add(fHistoLifeTimeV0All);
+    
+    fOutputKF->Add(fHistoMassV0True);
+    fOutputKF->Add(fHistoDecayLengthV0True);
+    fOutputKF->Add(fHistoLifeTimeV0True);
+    
+    fOutputKF->Add(fHistoMassV0TrueFromAOD);
+    
+    fOutputKF->Add(fHistoMassV0TrueK0S);
+    fOutputKF->Add(fHistoDecayLengthV0TrueK0S);
+    fOutputKF->Add(fHistoLifeTimeV0TrueK0S);
+    
+    fOutputKF->Add(fHistoMassV0TrueK0SFromAOD);
+    
+    fOutputKF->Add(fHistoMassLcAll);
+    fOutputKF->Add(fHistoDecayLengthLcAll);
+    fOutputKF->Add(fHistoLifeTimeLcAll);
+    
+    fOutputKF->Add(fHistoMassLcTrue);
+    fOutputKF->Add(fHistoDecayLengthLcTrue);
+    fOutputKF->Add(fHistoLifeTimeLcTrue);
+    
+    fOutputKF->Add(fHistoMassLcTrueFromAOD);
+    
+    fOutputKF->Add(fHistoMassV0fromLcAll);
+    fOutputKF->Add(fHistoDecayLengthV0fromLcAll);
+    fOutputKF->Add(fHistoLifeTimeV0fromLcAll);
+    
+    fOutputKF->Add(fHistoMassV0fromLcTrue);
+    fOutputKF->Add(fHistoDecayLengthV0fromLcTrue);
+    fOutputKF->Add(fHistoLifeTimeV0fromLcTrue);
+    
+    fOutputKF->Add(fHistoMassLcSgn);
+    fOutputKF->Add(fHistoMassLcSgnFromAOD);
+    fOutputKF->Add(fHistoDecayLengthLcSgn);
+    fOutputKF->Add(fHistoLifeTimeLcSgn);
+    
+    fOutputKF->Add(fHistoMassV0fromLcSgn);
+    fOutputKF->Add(fHistoDecayLengthV0fromLcSgn);
+    fOutputKF->Add(fHistoLifeTimeV0fromLcSgn);
+    
+    fOutputKF->Add(fHistoKF);
+    fOutputKF->Add(fHistoKFV0);
+    fOutputKF->Add(fHistoKFLc);
+    
+    fOutputKF->Add(fHistoMassKFV0);
+    fOutputKF->Add(fHistoDecayLengthKFV0);
+    fOutputKF->Add(fHistoLifeTimeKFV0);
+    
+    fOutputKF->Add(fHistoMassKFLc);
+    fOutputKF->Add(fHistoDecayLengthKFLc);
+    fOutputKF->Add(fHistoLifeTimeKFLc);
+    
+    fOutputKF->Add(fHistoArmenterosPodolanskiV0KF);
+    fOutputKF->Add(fHistoArmenterosPodolanskiV0KFSgn);
+    fOutputKF->Add(fHistoArmenterosPodolanskiV0AOD);
+    fOutputKF->Add(fHistoArmenterosPodolanskiV0AODSgn);
+  }
 
   PostData(6, fOutputKF);
 
-  //Printf("Here ok 1!");
-
   return;
 }
 
@@ -782,6 +860,8 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserExec(Option_t *)
     return;
   }
 
+  fCurrentEvent++;
+  AliDebug(2, Form("Processing event = %d", fCurrentEvent));
   AliAODEvent* aodEvent = dynamic_cast<AliAODEvent*>(fInputEvent);
   TClonesArray *arrayLctopKos=0;
 
@@ -811,33 +891,23 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserExec(Option_t *)
   
   if ( !fUseMCInfo && fIspA) {
     fAnalCuts->SetTriggerClass("");
-    fAnalCuts->SetTriggerMask(AliVEvent::kINT7);
+    fAnalCuts->SetTriggerMask(fTriggerMask);
   }
-
-  fCounter->StoreEvent(aodEvent,fAnalCuts,fUseMCInfo);
-
-  // AOD primary vertex
-  fVtx1 = (AliAODVertex*)aodEvent->GetPrimaryVertex();
-  if (!fVtx1) return;
-  if (fVtx1->GetNContributors()<1) return;
-
-  fIsEventSelected = fAnalCuts->IsEventSelected(aodEvent);
-
-  if ( !fIsEventSelected ) {
-    fHistoEvents->Fill(0);
-    return; // don't take into account not selected events 
+  
+  Int_t runnumber = aodEvent->GetRunNumber();
+  if (aodEvent->GetTriggerMask() == 0 && (runnumber >= 195344 && runnumber <= 195677)){
+    AliDebug(3,"Event rejected because of null trigger mask");
+    return;
   }
-  fHistoEvents->Fill(1);
-
-  // Setting magnetic field for KF vertexing
-  AliKFParticle::SetField(aodEvent->GetMagneticField());
-
+  
+  fCounter->StoreEvent(aodEvent,fAnalCuts,fUseMCInfo);
+  
   // mc analysis 
   TClonesArray *mcArray = 0;
   AliAODMCHeader *mcHeader=0;
 
   if (fUseMCInfo) {
-    // MC array need for maching
+    // MC array need for matching
     mcArray = dynamic_cast<TClonesArray*>(aodEvent->FindListObject(AliAODMCParticle::StdBranchName()));
     if (!mcArray) {
       AliError("Could not find Monte-Carlo in AOD");
@@ -849,7 +919,34 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserExec(Option_t *)
       AliError("AliAnalysisTaskSELc2V0bachelorTMVA::UserExec: MC header branch not found!\n");
       return;
     }
+
+    Double_t zMCVertex = mcHeader->GetVtxZ();
+    if (TMath::Abs(zMCVertex) > fAnalCuts->GetMaxVtxZ()){
+      AliDebug(3,Form("z coordinate of MC vertex = %f, it was required to be within [-%f, +%f], skipping event", zMCVertex, fAnalCuts->GetMaxVtxZ(), fAnalCuts->GetMaxVtxZ()));
+      AliInfo(Form("z coordinate of MC vertex = %f, it was required to be within [-%f, +%f], skipping event", zMCVertex, fAnalCuts->GetMaxVtxZ(), fAnalCuts->GetMaxVtxZ()));
+      return;
+    }
+    
+    //Printf("Filling MC histo");
+    FillMCHisto(mcArray);
+  }
+  
+  // AOD primary vertex
+  fVtx1 = (AliAODVertex*)aodEvent->GetPrimaryVertex();
+  if (!fVtx1) return;
+  if (fVtx1->GetNContributors()<1) return;
+
+  fIsEventSelected = fAnalCuts->IsEventSelected(aodEvent);
+
+  if ( !fIsEventSelected ) {
+    fHistoEvents->Fill(0);
+    return; // don't take into account not selected events 
   }
+  fHistoEvents->Fill(1);
+
+  // Setting magnetic field for KF vertexing
+  fBField = aodEvent->GetMagneticField();
+  AliKFParticle::SetField(fBField);
 
   Int_t nSelectedAnal = 0;
   if (fIsK0sAnalysis) {
@@ -865,6 +962,112 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::UserExec(Option_t *)
   PostData(5, fVariablesTreeBkg);
   PostData(6, fOutputKF);
 
+}
+//-------------------------------------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelorTMVA::FillMCHisto(TClonesArray *mcArray){
+
+  // method to fill MC histo: how many Lc --> K0S + p are there at MC level
+  for (Int_t iPart=0; iPart<mcArray->GetEntriesFast(); iPart++) { 
+    AliAODMCParticle* mcPart = dynamic_cast<AliAODMCParticle*>(mcArray->At(iPart));
+    if (!mcPart){
+      AliError("Failed casting particle from MC array!, Skipping particle");
+      continue;
+    }
+    Int_t pdg = mcPart->GetPdgCode();
+    if (TMath::Abs(pdg) != 4122){
+      AliDebug(2, Form("MC particle %d is not a Lc: its pdg code is %d", iPart, pdg));
+      continue;
+    }
+    AliDebug(2, Form("Step 0 ok: MC particle %d is a Lc: its pdg code is %d", iPart, pdg));
+    Int_t labeldaugh0 = mcPart->GetDaughter(0);
+    Int_t labeldaugh1 = mcPart->GetDaughter(1);
+    if (labeldaugh0 <= 0 || labeldaugh1 <= 0){
+      AliDebug(2, Form("The MC particle doesn't have correct daughters, skipping!!"));
+      continue;
+    }
+    else if (labeldaugh1 - labeldaugh0 == 1){
+      AliDebug(2, Form("Step 1 ok: The MC particle has correct daughters!!"));
+      AliAODMCParticle* daugh0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labeldaugh0));
+      AliAODMCParticle* daugh1 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labeldaugh1));
+      if(!daugh0 || !daugh1){
+       AliDebug(2,"Particle daughters not properly retrieved!");
+       return;
+      }
+      Int_t pdgCodeDaugh0 = TMath::Abs(daugh0->GetPdgCode());
+      Int_t pdgCodeDaugh1 = TMath::Abs(daugh1->GetPdgCode());
+      AliAODMCParticle* bachelorMC = daugh0;
+      AliAODMCParticle* v0MC = daugh1;
+      AliDebug(2, Form("pdgCodeDaugh0 = %d, pdgCodeDaugh1 = %d", pdgCodeDaugh0, pdgCodeDaugh1));
+      if ((pdgCodeDaugh0 == 311 && pdgCodeDaugh1 == 2212) || (pdgCodeDaugh0 == 2212 && pdgCodeDaugh1 == 311)){ 
+       // we are in the case of Lc --> K0 + p; now we have to check if the K0 decays in K0S, and if this goes in pi+pi-
+       /// first, we set the bachelor and the v0: above we assumed first proton and second V0, but we could have to change it:
+       if (pdgCodeDaugh0 == 311 && pdgCodeDaugh1 == 2212) {
+         bachelorMC = daugh1;
+         v0MC = daugh0;
+       }
+       AliDebug(2, Form("Number of Daughters of v0 = %d", v0MC->GetNDaughters()));
+       if (v0MC->GetNDaughters() != 1) { 
+         AliDebug(2, "The K0 does not decay in 1 body only! Impossible... Continuing...");
+         continue;
+       }
+       else { // So far: Lc --> K0 + p, K0 with 1 daughter 
+         AliDebug(2, "Step 2 ok: The K0 does decay in 1 body only! ");
+         Int_t labelK0daugh = v0MC->GetDaughter(0);
+         AliAODMCParticle* partK0S = dynamic_cast<AliAODMCParticle*>(mcArray->At(labelK0daugh));
+         if(!partK0S){
+           AliError("Error while casting particle! returning a NULL array");
+           continue;
+         }
+         else { // So far: Lc --> K0 + p, K0 with 1 daughter that we can access
+           if (partK0S->GetNDaughters() != 2 || TMath::Abs(partK0S->GetPdgCode() != 310)){
+             AliDebug(2, "The K0 daughter is not a K0S or does not decay in 2 bodies");
+             continue;
+           }
+           else { // So far: Lc --> K0 + p, K0 --> K0S, K0S in 2 bodies
+             AliDebug(2, "Step 3 ok: The K0 daughter is a K0S and does decay in 2 bodies");
+             Int_t labelK0Sdaugh0 = partK0S->GetDaughter(0);
+             Int_t labelK0Sdaugh1 = partK0S->GetDaughter(1);
+             AliAODMCParticle* daughK0S0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labelK0Sdaugh0));
+             AliAODMCParticle* daughK0S1 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labelK0Sdaugh1));
+             if (!daughK0S0 || ! daughK0S1){
+               AliDebug(2, "Could not access K0S daughters, continuing...");
+               continue;
+             }
+             else { // So far: Lc --> K0 + p, K0 --> K0S, K0S in 2 bodies that we can access
+               AliDebug(2, "Step 4 ok: Could access K0S daughters, continuing...");
+               Int_t pdgK0Sdaugh0 = daughK0S0->GetPdgCode();
+               Int_t pdgK0Sdaugh1 = daughK0S1->GetPdgCode();
+               if (TMath::Abs(pdgK0Sdaugh0) != 211 || TMath::Abs(pdgK0Sdaugh1) != 211){
+                 AliDebug(2, "The K0S does not decay in pi+pi-, continuing");
+                 //AliInfo("The K0S does not decay in pi+pi-, continuing");
+               }
+               else { // Full chain: Lc --> K0 + p, K0 --> K0S, K0S --> pi+pi-
+                 if (fAnalCuts->IsInFiducialAcceptance(mcPart->Pt(), mcPart->Y())) {
+                   AliDebug(2, Form("----> Filling histo with pt = %f", mcPart->Pt()));
+                   if(TMath::Abs(mcPart->Y()) < 0.5) fHistoMCLcK0SpGenLimAcc->Fill(mcPart->Pt());
+                   //AliInfo(Form("\nparticle = %d, Filling MC Gen histo\n", iPart));
+                   fHistoMCLcK0SpGen->Fill(mcPart->Pt());
+                   if(!(TMath::Abs(bachelorMC->Eta()) > 0.9 || bachelorMC->Pt() < 0.1 ||
+                        TMath::Abs(daughK0S0->Eta()) > 0.9 || daughK0S0->Pt() < 0.1 ||
+                        TMath::Abs(daughK0S1->Eta()) > 0.9 || daughK0S1->Pt() < 0.1)) {
+                     fHistoMCLcK0SpGenAcc->Fill(mcPart->Pt());
+                   }
+                 }
+                 else {
+                   AliDebug(2, "not in fiducial acceptance! Skipping");
+                   continue;
+                 }
+               }
+             }
+           }
+         }
+       }
+      }
+    }
+  } // closing loop over mcArray
+
+  return; 
+
 }
 
 //-------------------------------------------------------------------------------
@@ -932,7 +1135,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::MakeAnalysisForLc2prK0S(TClonesArray *a
       // find associated MC particle for Lc -> p+K0 and K0S->pi+pi
       fmcLabelLc = lcK0spr->MatchToMC(pdgCand, pdgDgLctoV0bachelor[1], pdgDgLctoV0bachelor, pdgDgV0toDaughters, mcArray, kTRUE);
       if (fmcLabelLc>=0) {
-       AliDebug(2,Form(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cascade number %d (total cascade number = %d)", iLctopK0s, nCascades));
+       AliDebug(2, Form("----> cascade number %d (total cascade number = %d) is a Lc!", iLctopK0s, nCascades));
 
        AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(fmcLabelLc));
        if(partLc){
@@ -979,7 +1182,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::MakeAnalysisForLc2prK0S(TClonesArray *a
 
     AliAODTrack * v0Pos = dynamic_cast<AliAODTrack*>(lcK0spr->Getv0PositiveTrack());
     AliAODTrack * v0Neg = dynamic_cast<AliAODTrack*>(lcK0spr->Getv0NegativeTrack());
-    if (!v0Neg || !v0Neg) {
+    if (!v0Neg || !v0Pos) {
       AliDebug(2,Form("V0 by cascade %d has no V0positive of V0negative object",iLctopK0s));
       continue;
     }
@@ -1036,10 +1239,46 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::MakeAnalysisForLc2prK0S(TClonesArray *a
            continue; 
          }
        }
+       else if (fKeepingOnlyPYTHIABkg){
+         // we have decided to fill the background only when the candidate has the daugthers that all come from HIJING underlying event!
+         AliAODTrack *bachelor = (AliAODTrack*)lcK0spr->GetBachelor();
+         AliAODTrack *v0pos = (AliAODTrack*)lcK0spr->Getv0PositiveTrack();
+         AliAODTrack *v0neg = (AliAODTrack*)lcK0spr->Getv0NegativeTrack();
+         if (!bachelor || !v0pos || !v0neg) {
+           AliDebug(2, "Cannot retrieve one of the tracks while checking origin, continuing");
+           continue;
+         }
+         else {
+           Int_t labelbachelor = TMath::Abs(bachelor->GetLabel());
+           Int_t labelv0pos = TMath::Abs(v0pos->GetLabel());
+           Int_t labelv0neg = TMath::Abs(v0neg->GetLabel());
+           AliAODMCParticle* MCbachelor =  (AliAODMCParticle*)mcArray->At(labelbachelor);
+           AliAODMCParticle* MCv0pos =  (AliAODMCParticle*)mcArray->At(labelv0pos);
+           AliAODMCParticle* MCv0neg =  (AliAODMCParticle*)mcArray->At(labelv0neg);
+           if (!MCbachelor || !MCv0pos || !MCv0neg) {
+             AliDebug(2, "Cannot retrieve MC particle for one of the tracks while checking origin, continuing");
+             continue;
+           }
+           else {
+             Int_t isBachelorFromPythia = fUtils->CheckOrigin(mcArray, MCbachelor, kTRUE);
+             Int_t isv0posFromPythia = fUtils->CheckOrigin(mcArray, MCv0pos, kTRUE);
+             Int_t isv0negFromPythia = fUtils->CheckOrigin(mcArray, MCv0neg, kTRUE);
+             if (isBachelorFromPythia != 0 && isv0posFromPythia != 0 && isv0negFromPythia != 0){
+               AliDebug(2, "The candidate is from PYTHIA (i.e. all daughters originate from a quark), keeping it to fill background");
+               fHistoBackground->Fill(2);
+             }
+             else {
+               AliDebug(2, "The candidate is NOT from PYTHIA, we skip it when filling background");
+               fHistoBackground->Fill(3);
+               continue; 
+             }
+           }
+         }
+       }
       }
     }
 
-    FillLc2pK0Sspectrum(lcK0spr, isLc, nSelectedAnal, cutsAnal, mcArray);
+    FillLc2pK0Sspectrum(lcK0spr, isLc, nSelectedAnal, cutsAnal, mcArray, iLctopK0s);
     
   }
   
@@ -1051,7 +1290,7 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
                                                             Int_t isLc,
                                                             Int_t &nSelectedAnal,
                                                             AliRDHFCutsLctoV0 *cutsAnal,
-                                                            TClonesArray *mcArray){
+                                                            TClonesArray *mcArray, Int_t iLctopK0s){
   //
   // Fill histos for Lc -> K0S+proton
   //
@@ -1109,24 +1348,23 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
   Int_t isInV0window = (((cutsAnal->IsSelectedSingleCut(part, AliRDHFCuts::kCandidate, 2)) & (AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr)); // cut on V0 invMass
 
   if (isInV0window == 0) {
-    AliDebug(2, "No: The candidate has NOT passed the mass cuts!");
-    if (isLc) Printf("SIGNAL candidate rejected");
+    AliDebug(2, "No: The candidate has NOT passed the V0 window cuts!");
+    if (isLc) Printf("SIGNAL candidate rejected: V0 window cuts");
     return;           
   }
   else AliDebug(2, "Yes: The candidate has passed the mass cuts!");
 
-  //   Printf("ciccio!!!!!!!");
-
   Bool_t isInCascadeWindow = (((cutsAnal->IsSelectedSingleCut(part, AliRDHFCuts::kCandidate, 0)) & (AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr)); // cut on Lc->p+K0S invMass
 
   if (!isInCascadeWindow) {
     AliDebug(2, "No: The candidate has NOT passed the cascade window cuts!");
-    if (isLc) Printf("SIGNAL candidate rejected");
+    if (isLc) Printf("SIGNAL candidate rejected: cascade window cuts");
     return;
   }
   else AliDebug(2, "Yes: The candidate has passed the cascade window cuts!");
 
   Bool_t isCandidateSelectedCuts = (((cutsAnal->IsSelected(part, AliRDHFCuts::kCandidate)) & (AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr)); // kinematic/topological cuts
+  AliDebug(2, Form("recoAnalysisCuts = %d", cutsAnal->IsSelected(part, AliRDHFCuts::kCandidate) & (AliRDHFCutsLctoV0::kLcToK0Spr)));
   if (!isCandidateSelectedCuts){
     AliDebug(2, "No: Analysis cuts kCandidate level NOT passed");
     if (isLc) Printf("SIGNAL candidate rejected");
@@ -1143,18 +1381,49 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
   }
 
   //Bool_t isBachelorID = (((cutsAnal->IsSelected(part,AliRDHFCuts::kPID))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // ID x bachelor
-  Double_t probTPCTOF[AliPID::kSPECIES]={0.};
+  Double_t probTPCTOF[AliPID::kSPECIES]={-1.};
        
   UInt_t detUsed = fPIDCombined->ComputeProbabilities(bachelor, fPIDResponse, probTPCTOF);
-  Double_t probProton = 0.;
-  Double_t probPion = 0.;
-  Double_t probKaon = 0.;
+  AliDebug(2, Form("detUsed (TPCTOF case) = %d", detUsed));
+  Double_t probProton = -1.;
+  //  Double_t probPion = -1.;
+  //  Double_t probKaon = -1.;
   if (detUsed == (UInt_t)fPIDCombined->GetDetectorMask() ) {
+    AliDebug(2, Form("We have found the detector mask for TOF + TPC: probProton will be set to %f", probTPCTOF[AliPID::kProton]));
     probProton = probTPCTOF[AliPID::kProton];
-    probPion = probTPCTOF[AliPID::kPion];
-    probKaon = probTPCTOF[AliPID::kKaon];
-  }
-
+    // probPion = probTPCTOF[AliPID::kPion];
+    // probKaon = probTPCTOF[AliPID::kKaon];
+  }
+  else { // if you don't have both TOF and TPC, try only TPC
+    fPIDCombined->SetDetectorMask(AliPIDResponse::kDetTPC);
+    AliDebug(2, "We did not find the detector mask for TOF + TPC, let's see only TPC");
+    detUsed = fPIDCombined->ComputeProbabilities(bachelor, fPIDResponse, probTPCTOF);
+    AliDebug(2,Form(" detUsed (TPC case) = %d", detUsed));
+    if (detUsed == (UInt_t)fPIDCombined->GetDetectorMask()) {
+      probProton = probTPCTOF[AliPID::kProton];
+      // probPion = probTPCTOF[AliPID::kPion];
+      // probKaon = probTPCTOF[AliPID::kKaon];
+      AliDebug(2, Form("TPC only worked: probProton will be set to %f", probTPCTOF[AliPID::kProton]));
+    }
+    else {
+      AliDebug(2, "Only TPC did not work...");
+    }
+    // resetting mask to ask for both TPC+TOF
+    fPIDCombined->SetDetectorMask(AliPIDResponse::kDetTPC+AliPIDResponse::kDetTOF);
+  }
+  AliDebug(2, Form("probProton = %f", probProton));
+
+  // now we get the TPC and TOF single PID probabilities (only for Proton, or the tree will explode :) )
+  Double_t probProtonTPC = -1.;
+  Double_t probProtonTOF = -1.;
+  Double_t pidTPC[AliPID::kSPECIES]={-1.};
+  Double_t pidTOF[AliPID::kSPECIES]={-1.};
+  Int_t respTPC = fPIDResponse->ComputePIDProbability(AliPIDResponse::kDetTPC, bachelor, AliPID::kSPECIES, pidTPC);
+  Int_t respTOF = fPIDResponse->ComputePIDProbability(AliPIDResponse::kDetTOF, bachelor, AliPID::kSPECIES, pidTOF);
+  if (respTPC == AliPIDResponse::kDetPidOk) probProtonTPC = pidTPC[AliPID::kProton];
+  if (respTOF == AliPIDResponse::kDetPidOk) probProtonTOF = pidTOF[AliPID::kProton];
+
+  // checking V0 status (on-the-fly vs offline)
   if ( !( !onFlyV0 || (onFlyV0 && fUseOnTheFlyV0) ) ) {
     AliDebug(2, "On-the-fly discarded");
     return;            
@@ -1306,8 +1575,8 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
     fCandidateVariables[24] = part->Eta();
     fCandidateVariables[25] = v0pos->Eta();
     fCandidateVariables[26] = v0neg->Eta();
-    fCandidateVariables[27] = probPion;
-    fCandidateVariables[28] = probKaon;
+    fCandidateVariables[27] = probProtonTPC;
+    fCandidateVariables[28] = probProtonTOF;
     fCandidateVariables[29] = bachelor->Eta();
 
     fCandidateVariables[30] = part->P();
@@ -1329,8 +1598,12 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
     fCandidateVariables[43] = part->Ct(4122);
     fCandidateVariables[44] = v0part->Ct(310, v0part->GetSecondaryVtx());
 
-    EBachelor bachCode = CheckBachelor(part, bachelor, mcArray);         
-    EK0S k0SCode = CheckK0S(part, v0part, mcArray);
+    EBachelor bachCode = kBachInvalid;   
+    EK0S k0SCode = kK0SInvalid;
+    if (fUseMCInfo) {
+      bachCode = CheckBachelor(part, bachelor, mcArray);         
+      k0SCode = CheckK0S(part, v0part, mcArray);
+    }
 
     fCandidateVariables[45] = bachCode;
     fCandidateVariables[46] = k0SCode;
@@ -1377,9 +1650,60 @@ void AliAnalysisTaskSELc2V0bachelorTMVA::FillLc2pK0Sspectrum(AliAODRecoCascadeHF
     fCandidateVariables[64] = v0part->AlphaV0();
     fCandidateVariables[65] = v0part->PtArmV0();
 
+    AliDebug(2, Form("v0pos->GetStatus() & AliESDtrack::kITSrefit= %d, v0neg->GetStatus() & AliESDtrack::kITSrefit = %d, v0pos->GetTPCClusterInfo(2, 1)= %f, v0neg->GetTPCClusterInfo(2, 1) = %f", (Int_t)(v0pos->GetStatus() & AliESDtrack::kITSrefit), (Int_t)(v0pos->GetStatus() & AliESDtrack::kITSrefit), v0pos->GetTPCClusterInfo(2, 1), v0neg->GetTPCClusterInfo(2, 1)));
+    fCandidateVariables[66] = v0pos->GetStatus() & AliESDtrack::kITSrefit;
+    fCandidateVariables[67] = v0neg->GetStatus() & AliESDtrack::kITSrefit;
+    fCandidateVariables[68] = v0pos->GetTPCClusterInfo(2, 1);
+    fCandidateVariables[69] = v0neg->GetTPCClusterInfo(2, 1);
+
+    fCandidateVariables[70] = v0part->Xv();
+    fCandidateVariables[71] = v0part->Yv();
+    fCandidateVariables[72] = v0part->Zv();
+
+    fCandidateVariables[73] = fVtx1->GetX();
+    fCandidateVariables[74] = fVtx1->GetY();
+    fCandidateVariables[75] = fVtx1->GetZ();
+
+    fCandidateVariables[76] = bachelor->GetITSNcls();
+    fCandidateVariables[77] = bachelor->HasPointOnITSLayer(0) + bachelor->HasPointOnITSLayer(1);
+
+    fCandidateVariables[78] = v0pos->GetITSNcls();
+    fCandidateVariables[79] = v0pos->HasPointOnITSLayer(0) + v0pos->HasPointOnITSLayer(1);
+
+    fCandidateVariables[80] = v0neg->GetITSNcls();
+    fCandidateVariables[81] = v0neg->HasPointOnITSLayer(0) + v0neg->HasPointOnITSLayer(1);
+
+    TVector3 mom1(bachelor->Px(), bachelor->Py(), bachelor->Pz());
+    TVector3 mom2(v0part->Px(), v0part->Py(), v0part->Pz());
+    TVector3 momTot(part->Px(), part->Py(), part->Pz());
+    
+    Double_t Ql1 = mom1.Dot(momTot)/momTot.Mag();   
+    Double_t Ql2 = mom2.Dot(momTot)/momTot.Mag(); 
+    
+    Double_t alphaArmLc = (Ql1 - Ql2)/(Ql1 + Ql2);
+    Double_t alphaArmLcCharge = ( bachelor->Charge() > 0 ? (Ql1 - Ql2)/(Ql1 + Ql2) : (Ql2 - Ql1)/(Ql1 + Ql2) );
+    Double_t ptArmLc = mom1.Perp(momTot);
+    
+    fCandidateVariables[82] = alphaArmLc;
+    fCandidateVariables[83] = alphaArmLcCharge;
+    fCandidateVariables[84] = ptArmLc;
+
+    Double_t massK0SPDG = TDatabasePDG::Instance()->GetParticle(310)->Mass();    // mass K0S PDG
+    Double_t massPrPDG = TDatabasePDG::Instance()->GetParticle(2212)->Mass();    // mass Proton PDG
+    Double_t massLcPDG = TDatabasePDG::Instance()->GetParticle(4122)->Mass();    // mass Lc PDG
+    
+    Double_t pStar = TMath::Sqrt((massLcPDG*massLcPDG-massPrPDG*massPrPDG-massK0SPDG*massK0SPDG)*(massLcPDG*massLcPDG-massPrPDG*massPrPDG-massK0SPDG*massK0SPDG)-4.*massPrPDG*massPrPDG*massK0SPDG*massK0SPDG)/(2.*massLcPDG);
+    Double_t e = part->E(4122);
+    Double_t beta = part->P()/e;
+    Double_t gamma = e/massLcPDG;
+    
+    Double_t cts = (Ql1/gamma-beta*TMath::Sqrt(pStar*pStar+massPrPDG*massPrPDG))/pStar;
+    
+    fCandidateVariables[85] = cts;
+
     if (fUseMCInfo) {
       if (isLc){
-       AliDebug(2, "Filling Sgn");
+       AliDebug(2, Form("Reco particle %d --> Filling Sgn", iLctopK0s));
        fVariablesTreeSgn->Fill();
        fHistoCodesSgn->Fill(bachCode, k0SCode);          
       }
@@ -1437,18 +1761,33 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
 
   // the KF vertex for the V0 has to be built with the prongs of the V0!
   Bool_t isMCokV0 = kTRUE, isBkgV0 = kFALSE;
-  AliKFParticle  V0, positiveV0KF, negativeV0KF;;
+  AliKFParticle  V0, positiveV0KF, negativeV0KF;
   Int_t labelsv0daugh[2] = {-1, -1};
+  Int_t idv0daugh[2] = {-1, -1};
+  AliExternalTrackParam* esdv0Daugh1 = 0x0;
+  AliExternalTrackParam* esdv0Daugh2 = 0x0;
   for(Int_t ipr= 0; ipr < 2; ipr++){ // 0 is positive, 1 is negative
     AliAODTrack *aodTrack = (AliAODTrack*)v0part->GetDaughter(ipr);
     if(!aodTrack) {
       AliDebug(2, "No V0 daughters available");
       return -1;
     }
+    Double_t xyz[3], pxpypz[3], cv[21];
+    Short_t sign;
+    aodTrack->GetXYZ(xyz);
+    aodTrack->PxPyPz(pxpypz);
+    aodTrack->GetCovarianceXYZPxPyPz(cv);
+    sign = aodTrack->Charge();
+    AliExternalTrackParam tmp1( xyz, pxpypz, cv, sign);
+
+    if (ipr == 0) esdv0Daugh1 = new AliExternalTrackParam( xyz, pxpypz, cv, sign);
+    else esdv0Daugh2 = new AliExternalTrackParam( xyz, pxpypz, cv, sign);
     labelsv0daugh[ipr] = TMath::Abs(aodTrack->GetLabel());
+    idv0daugh[ipr] = aodTrack->GetID();
     if (labelsv0daugh[ipr] == -1) isBkgV0 = kTRUE;
 
     //Printf("v0 daughter %d has label %d", ipr, labelsv0daugh[ipr]);
+
     AliKFParticle daughterKF(*aodTrack, pdg[ipr]); // we assume that the PDG is correct
     if (aodTrack->Charge() > 0) { // assigning positive and negative track to KF V0 for Armenteros-Podolanski plot 
       positiveV0KF = daughterKF;
@@ -1456,15 +1795,34 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
     else { 
       negativeV0KF = daughterKF;
     }  
-    V0.AddDaughter(daughterKF);
   }
-
+  
+  Double_t xn=0., xp=0.;//, dca;
+  AliDebug(2, Form("bField = %f, esdv0Daugh1 = %p, esdv0Daugh2 = %p", fBField, esdv0Daugh1, esdv0Daugh2));
+  //  dca = esdv0Daugh1->GetDCA(esdv0Daugh2, fBField, xn, xp);
+    
+  AliExternalTrackParam tr1(*esdv0Daugh1);
+  AliExternalTrackParam tr2(*esdv0Daugh2);
+  tr1.PropagateTo(xn, fBField); 
+  tr2.PropagateTo(xp, fBField);
+
+  AliKFParticle daughterKF1(tr1, 211);
+  AliKFParticle daughterKF2(tr2, 211);
+  V0.AddDaughter(positiveV0KF);
+  V0.AddDaughter(negativeV0KF);
+  //V0.AddDaughter(daughterKF1);
+  //V0.AddDaughter(daughterKF2);
+
+  delete esdv0Daugh1;
+  delete esdv0Daugh2;
+  esdv0Daugh1=0;
+  esdv0Daugh2=0;
   // Checking the quality of the KF V0 vertex
   if( V0.GetNDF() < 1 ) {
     //Printf("Number of degrees of freedom < 1, continuing");
     return -1;
   }
-  if( TMath::Sqrt(TMath::Abs(V0.GetChi2()/V0.GetNDF())) > 3. ) {
+  if( TMath::Sqrt(TMath::Abs(V0.GetChi2()/V0.GetNDF())) > fCutKFChi2NDF ) {
     //Printf("Chi2 per DOF too big, continuing");
     return -1;
   }
@@ -1483,27 +1841,13 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
     }
   }
 
-  /*
   // Check V0 Chi^2 deviation from primary vertex // not needed for V0 for Lc decay!!
-  if( V0.GetDeviationFromVertex( primVtxCopy ) >3. ) {
+  /*
+  if( V0.GetDeviationFromVertex( primVtxCopy ) < fCutKFDeviationFromVtxV0) {
   //Printf("Deviation from vertex too big, continuing");
-  return -1;
-  }
-  */
-  
-  if(ftopoConstraint){
-    if(ntcheck>0) { // number of constraints left with which the vertex was built, after removing V0 daughters
-      //* Add V0 to primary vertex to improve the primary vertex resolution
-      primVtxCopy += V0;
-      V0.SetProductionVertex(primVtxCopy); // FIXME: is this correct here? 
-    }
-  }
-
-  //* Check chi^2 
-  if( TMath::Sqrt( TMath::Abs(V0.GetChi2()/V0.GetNDF())) >3. ) {
-    AliDebug(2, "Final Chi2 per DOF too big, continuing");
     return -1;
   }
+  */
 
   //* Get V0 invariant mass
   Double_t massV0 = 999999, sigmaMassV0 = 999999;
@@ -1514,9 +1858,112 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
       if (sigmaMassV0 > 1e19) codeKFV0 = 5;  // Mass and SigmaMass not ok
     }
     else if (sigmaMassV0 > 1e19) codeKFV0 = 2; // SigmaMass not ok
-  }
+  } 
   fHistoMassKFV0->Fill(massV0, sigmaMassV0);   
 
+  if (massV0 < 0.4) Printf("\n\n>>>>>>>>>> Found the Funny V0 (mass = %f, sigma = %f, AOD mass = %f): labels of the tracks = %d, %d, id = %d and %d", massV0, sigmaMassV0, v0part->MassK0Short(), labelsv0daugh[0], labelsv0daugh[1], idv0daugh[0], idv0daugh[1]);
+  if (massV0 > 0.55) Printf("\n\n>>>>>>>>>> Found the Funny V0 (mass = %f, , sigma = %f, AOD mass = %f): labels of the tracks = %d, %d, id = %d and %d", massV0, sigmaMassV0, v0part->MassK0Short(), labelsv0daugh[0], labelsv0daugh[1], idv0daugh[0], idv0daugh[1]);
+
+  Printf("Vertices: KF:  x = %f, y = %f, z = %f", V0.GetX(), V0.GetY(), V0.GetZ());
+  Printf("Vertices: AOD: x = %f, y = %f, z = %f", v0part->Xv(), v0part->Yv(), v0part->Zv());
+
+  //Printf("Got MC vtx for V0");
+  if (fUseMCInfo && TMath::Abs(labelsv0daugh[0] - labelsv0daugh[1]) == 1) {
+    AliAODMCParticle* tmpdaughv01 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labelsv0daugh[0]));
+    AliAODMCParticle* tmpdaughv02 = dynamic_cast<AliAODMCParticle*>(mcArray->At(labelsv0daugh[1]));
+    if (!tmpdaughv01 && labelsv0daugh[0] > 0){
+      AliDebug(2, "Could not access MC info for first daughter of V0, continuing");
+    }
+    if (!tmpdaughv02 && labelsv0daugh[1] > 0){
+      AliDebug(2, "Could not access MC info for second daughter of V0, continuing");
+    }
+    if(tmpdaughv01){
+      Double_t xPionMC = tmpdaughv01->Xv(); //Production vertex of Pion --> Where K0S decays
+      Double_t yPionMC = tmpdaughv01->Yv();
+      Double_t zPionMC = tmpdaughv01->Zv();
+      //Printf("Got MC vtx for Pion");
+      Printf("Vertices: MC:  x = %f, y = %f, z = %f", xPionMC, yPionMC, zPionMC);
+    }
+  }
+  else {
+    Printf("Not a true V0");
+  }
+  //massV0=-1;//return -1;// !!!!
+
+  // now use what we just try with the bachelor, to build the Lc
+  
+  // topological constraint
+  nt = primVtxCopy.GetNContributors();
+  ntcheck = nt;
+  
+  Bool_t isMCokLc = kTRUE, isBkgLc = kFALSE;
+  AliKFParticle  Lc;
+  Int_t labelsLcdaugh[2] = {-1, -1};
+  labelsLcdaugh[0] = TMath::Abs(bach->GetLabel());
+  labelsLcdaugh[1] = mcLabelV0;
+
+  if (bach->Charge() < 0) pdgLc[0] = -pdgLc[0];
+  AliKFParticle daughterKFLc(*bach, pdgLc[0]);
+  Lc.AddDaughter(daughterKFLc);
+  TParticlePDG* particlePDG = TDatabasePDG::Instance()->GetParticle(310);
+  Double_t massPDGK0S = particlePDG->Mass();
+  V0.SetMassConstraint(massPDGK0S);
+  Lc.AddDaughter(V0);
+  if( Lc.GetNDF() < 1 ) {
+    AliDebug(2, Form("Lc: Number of degrees of freedom < 1 (%d), continuing", Lc.GetNDF()));
+    return -1;
+  }
+  if( TMath::Sqrt(TMath::Abs(Lc.GetChi2()/Lc.GetNDF())) > fCutKFChi2NDF) {
+    AliDebug(2, Form("Lc: Chi2 per DOF too big, continuing (%f)", TMath::Sqrt(TMath::Abs(Lc.GetChi2()/Lc.GetNDF()))));
+    return -1;
+  }
+  
+  if(ftopoConstraint && nt > 0){
+    //* subtruct daughters from primary vertex 
+    if(!bach->GetUsedForPrimVtxFit()) {
+      AliDebug(3, "Lc: Bachelor was not used for primary vertex, not subtracting it from primary vertex");
+    }
+    else{
+      primVtxCopy -= daughterKFLc;
+      ntcheck--;
+    }
+    /* the V0 was added above, so it is ok to remove it without checking
+       if(!V0->GetUsedForPrimVtxFit()) {
+       Printf("Lc: V0 was not used for primary vertex, continuing");
+       continue;
+       }
+    */
+    //primVtxCopy -= V0;
+    //ntcheck--;
+  }
+  
+  // Check Lc Chi^2 deviation from primary vertex 
+  /*
+  if( Lc.GetDeviationFromVertex( primVtxCopy ) > fCutKFDeviationFromVtx) {
+    AliDebug(2, Form("Lc: Deviation from vertex too big, continuing (%f)", Lc.GetDeviationFromVertex( primVtxCopy )));
+    return -1;
+  }
+  
+  if(ftopoConstraint){
+    if(ntcheck>0) {
+      // Add Lc to primary vertex to improve the primary vertex resolution
+      primVtxCopy += Lc;
+      Lc.SetProductionVertex(primVtxCopy);
+    }
+  }
+  */
+  //* Check chi^2 
+  if( TMath::Sqrt( TMath::Abs(Lc.GetChi2()/Lc.GetNDF())) > fCutKFChi2NDF) {
+    AliDebug(2, Form("Lc: Final Chi2 per DOF too big, continuing (%f)", TMath::Sqrt( TMath::Abs(Lc.GetChi2()/Lc.GetNDF()))));
+    return -1;
+  }
+
+  if(ftopoConstraint){
+    V0.SetProductionVertex(Lc); 
+  }
+  
+  // After setting the vertex of the V0, getting/filling some info
+
   //* Get V0 decayLength
   Double_t decayLengthV0 = 999999, sigmaDecayLengthV0 = 999999;
   Int_t retDLV0 = V0.GetDecayLength( decayLengthV0, sigmaDecayLengthV0 );
@@ -1627,69 +2074,7 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
       
   AliDebug(2, Form("isMCokV0 = %d, isBkgV0 = %d", (Int_t)isMCokV0, (Int_t)isBkgV0));
 
-  // now use what we just try with the bachelor, to build the Lc
-  
-  // topological constraint
-  nt = primVtxCopy.GetNContributors();
-  ntcheck = nt;
-  
-  Bool_t isMCokLc = kTRUE, isBkgLc = kFALSE;
-  AliKFParticle  Lc;
-  Int_t labelsLcdaugh[2] = {-1, -1};
-  labelsLcdaugh[0] = TMath::Abs(bach->GetLabel());
-  labelsLcdaugh[1] = mcLabelV0;
-
-  if (bach->Charge() < 0) pdgLc[0] = -pdgLc[0];
-  AliKFParticle daughterKFLc(*bach, pdgLc[0]);
-  Lc.AddDaughter(daughterKFLc);
-  Lc.AddDaughter(V0);
-  if( Lc.GetNDF() < 1 ) {
-    AliDebug(2, Form("Lc: Number of degrees of freedom < 1 (%d), continuing", Lc.GetNDF()));
-    return -1;
-  }
-  if( TMath::Sqrt(TMath::Abs(Lc.GetChi2()/Lc.GetNDF())) >3. ) {
-    AliDebug(2, Form("Lc: Chi2 per DOF too big, continuing (%f)", TMath::Sqrt(TMath::Abs(Lc.GetChi2()/Lc.GetNDF()))));
-    return -1;
-  }
-  
-  if(ftopoConstraint && nt > 0){
-    //* subtruct daughters from primary vertex 
-    if(!bach->GetUsedForPrimVtxFit()) {
-      AliDebug(3, "Lc: Bachelor was not used for primary vertex, not subtracting it from primary vertex");
-    }
-    else{
-      primVtxCopy -= daughterKFLc;
-      ntcheck--;
-    }
-    /* the V0 was added above, so it is ok to remove it without checking
-       if(!V0->GetUsedForPrimVtxFit()) {
-       Printf("Lc: V0 was not used for primary vertex, continuing");
-       continue;
-       }
-    */
-    primVtxCopy -= V0;
-    ntcheck--;
-  }
-  
-  //* Check V0 Chi^2 deviation from primary vertex 
-  if( Lc.GetDeviationFromVertex( primVtxCopy ) >3. ) {
-    AliDebug(2, Form("Lc: Deviation from vertex too big, continuing (%f)", Lc.GetDeviationFromVertex( primVtxCopy )));
-    return -1;
-  }
-  
-  if(ftopoConstraint){
-    if(ntcheck>0) {
-      //* Add Lc to primary vertex to improve the primary vertex resolution
-      primVtxCopy += Lc;
-      Lc.SetProductionVertex(primVtxCopy);
-    }
-  }
-  
-  //* Check chi^2 
-  if( TMath::Sqrt( TMath::Abs(Lc.GetChi2()/Lc.GetNDF())) >3. ) {
-    AliDebug(2, Form("Lc: Final Chi2 per DOF too big, continuing (%f)", TMath::Sqrt( TMath::Abs(Lc.GetChi2()/Lc.GetNDF()))));
-    return -1;
-  }
+  // Going back to Lc
 
   //* Get Lc invariant mass
   Double_t massLc = 999999, sigmaMassLc= 999999;
@@ -1847,7 +2232,7 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
            AliDebug(3, "Could not access MC info for second daughter of Lc");
          }
          else { // we can access safely the K0S mother in the MC
-           if( daughv01Lc && daughv02Lc && daughv01Lc->GetMother() ==  daughv02Lc->GetMother() && daughv01Lc->GetMother()>=0 ){  // This is a true cascade! bachelor and V0 come from the same mother
+           if( daughv01Lc && (daughv01Lc->GetMother() ==  daughv02Lc->GetMother()) && (daughv01Lc->GetMother()>=0) ){  // This is a true cascade! bachelor and V0 come from the same mother
              //Printf("Lc: The mother has label %d", daughv01Lc->GetMother());
              AliAODMCParticle *motherLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(daughv01Lc->GetMother()));
              Int_t pdgMum = 0, pdgBach = 0, pdgV0 = 0;
@@ -1909,11 +2294,13 @@ Int_t AliAnalysisTaskSELc2V0bachelorTMVA::CallKFVertexing(AliAODRecoCascadeHF *c
                Double_t xV0MC = motherV0->Xv();     //Production vertex of K0S --> Where Lc decays
                Double_t yV0MC = motherV0->Yv();
                Double_t zV0MC = motherV0->Zv();
+               
                //Printf("Got MC vtx for V0");
                Double_t xPionMC = daughv01->Xv(); //Production vertex of Pion --> Where K0S decays
                Double_t yPionMC = daughv01->Yv();
                Double_t zPionMC = daughv01->Zv();
                //Printf("Got MC vtx for Pion");
+               Printf("Vertices: MC:  x = %f, y = %f, z = %f", xPionMC, yPionMC, zPionMC);
                
                Double_t vtxV0ResidualToLc = TMath::Sqrt((xV0MC - xPionMC) * (xV0MC - xPionMC) + 
                                                         (yV0MC - yPionMC) * (yV0MC - yPionMC) +