New analysis task version. Reorganization of methods, usage of a single cut object...
authorzconesa <zconesa@f7af4fe6-9843-0410-8265-dc069ae4e863>
Wed, 30 Oct 2013 17:30:52 +0000 (17:30 +0000)
committerzconesa <zconesa@f7af4fe6-9843-0410-8265-dc069ae4e863>
Wed, 30 Oct 2013 17:30:52 +0000 (17:30 +0000)
PWGHF/vertexingHF/AliAnalysisTaskSELc2V0bachelor.cxx
PWGHF/vertexingHF/AliAnalysisTaskSELc2V0bachelor.h
PWGHF/vertexingHF/macros/AddTaskLc2V0bachelor.C

index 598b6dd..da5ccaf 100644 (file)
@@ -39,7 +39,7 @@
 #include <TParticle.h>
 #include <TParticlePDG.h>
 #include <TH1F.h>
-#include <TH1I.h>
+#include <TH1F.h>
 #include <TH2F.h>
 #include <TTree.h>
 #include "TROOT.h"
@@ -52,6 +52,7 @@
 #include "AliAODMCHeader.h"
 #include "AliAODHandler.h"
 #include "AliLog.h"
+#include "AliExternalTrackParam.h"
 #include "AliAODVertex.h"
 #include "AliAODRecoDecay.h"
 #include "AliAODRecoDecayHF.h"
@@ -68,6 +69,8 @@
 #include "AliPIDResponse.h"
 #include "AliTOFPIDResponse.h"
 #include "AliInputEventHandler.h"
+#include "AliESDtrackCuts.h"
+#include "AliNeutralTrackParam.h"
 
 using std::cout;
 using std::endl;
@@ -75,35 +78,33 @@ using std::endl;
 ClassImp(AliAnalysisTaskSELc2V0bachelor)
 
 //__________________________________________________________________________
-AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor():  
-  AliAnalysisTaskSE(),
+AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor() : AliAnalysisTaskSE(),
   fUseMCInfo(kFALSE),
   fOutput(0),
   fOutputAll(0),
   fOutputPIDBach(0),
   fCEvents(0),
   fPIDResponse(0),
-  fIsK0sAnalysis(kFALSE),
+  fIsK0SAnalysis(kFALSE),
   fCounter(0),
-  fProdCuts(0),
   fAnalCuts(0),
-  fListCuts(0),
   fUseOnTheFlyV0(kFALSE),
   fIsEventSelected(kFALSE),
   fWriteVariableTree(kFALSE),
   fVariablesTree(0),
   fCandidateVariables(),
   fVtx1(0),
-  fBzkG(0)
+  fBzkG(0),
+  fAdditionalChecks(kFALSE)
 {
   //
   // Default ctor
   //
 }
 //___________________________________________________________________________
-AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor(const Char_t* name, AliRDHFCutsLctoV0* prodCuts,
+AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor(const Char_t* name,
                                                               AliRDHFCutsLctoV0* analCuts, Bool_t useOnTheFly,
-                                                              Bool_t writeVariableTree) :
+                                                              Bool_t writeVariableTree, Bool_t additionalChecks) :
   AliAnalysisTaskSE(name),
   fUseMCInfo(kFALSE),
   fOutput(0),
@@ -111,18 +112,17 @@ AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor(const Char_t* nam
   fOutputPIDBach(0),
   fCEvents(0),
   fPIDResponse(0),
-  fIsK0sAnalysis(kFALSE),
+  fIsK0SAnalysis(kFALSE),
   fCounter(0),
-  fProdCuts(prodCuts),
   fAnalCuts(analCuts),
-  fListCuts(0),
   fUseOnTheFlyV0(useOnTheFly),
   fIsEventSelected(kFALSE),
   fWriteVariableTree(writeVariableTree),
   fVariablesTree(0),
   fCandidateVariables(),
   fVtx1(0),
-  fBzkG(0)
+  fBzkG(0),
+  fAdditionalChecks(additionalChecks)
 {
   //
   // Constructor. Initialization of Inputs and Outputs
@@ -130,13 +130,15 @@ AliAnalysisTaskSELc2V0bachelor::AliAnalysisTaskSELc2V0bachelor(const Char_t* nam
   Info("AliAnalysisTaskSELc2V0bachelor","Calling Constructor");
 
   DefineOutput(1,TList::Class());  //conters
-  DefineOutput(2,TList::Class());  //All Entries output
-  DefineOutput(3,TList::Class());  //3sigma PID output
-  DefineOutput(4,AliNormalizationCounter::Class());
-  DefineOutput(5,TList::Class());
-
-  // Output slot #6 keeps a tree of the candidate variables after track selection
-  if (fWriteVariableTree) DefineOutput(6,TTree::Class());  //My private output
+  DefineOutput(2,AliNormalizationCounter::Class());
+  DefineOutput(3,AliRDHFCutsLctoV0::Class());
+  if (!writeVariableTree) {
+    DefineOutput(4,TList::Class());  //All Entries output
+    DefineOutput(5,TList::Class());  //3sigma PID output
+  } else {
+    // Output slot #4 keeps a tree of the candidate variables after track selection
+    DefineOutput(4,TTree::Class());  //My private output
+  }
 
 }
 
@@ -171,22 +173,12 @@ AliAnalysisTaskSELc2V0bachelor::~AliAnalysisTaskSELc2V0bachelor() {
     fCounter = 0;
   }
 
-  if (fProdCuts) {
-    delete fProdCuts;
-    fProdCuts = 0;
-  }
-
   if (fAnalCuts) {
     delete fAnalCuts;
     fAnalCuts = 0;
   }
 
-  if (fListCuts) {
-    delete fListCuts;
-    fListCuts = 0;
-  }
-
-  if(fVariablesTree){
+  if (fVariablesTree) {
     delete fVariablesTree;
     fVariablesTree = 0;
   }
@@ -202,11 +194,7 @@ void AliAnalysisTaskSELc2V0bachelor::Init() {
 
   if (fDebug > 1) AliInfo("Init");
 
-  fListCuts = new TList();
-  fListCuts->SetOwner();
-  fListCuts->Add(new AliRDHFCutsLctoV0(*fProdCuts));
-  fListCuts->Add(new AliRDHFCutsLctoV0(*fAnalCuts));
-  PostData(5,fListCuts);
+  PostData(3,fAnalCuts);
 
   return;
 }
@@ -231,7 +219,7 @@ void AliAnalysisTaskSELc2V0bachelor::UserExec(Option_t *)
     // have to taken from the AOD event hold by the AliAODExtension
     AliAODHandler* aodHandler = (AliAODHandler*) 
       ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
-   
+
     if (aodHandler->GetExtensions()) {
       AliAODExtension *ext = (AliAODExtension*)aodHandler->GetExtensions()->FindObject("AliAOD.VertexingHF.root");
       AliAODEvent *aodFromExt = ext->GetAOD();
@@ -242,25 +230,24 @@ void AliAnalysisTaskSELc2V0bachelor::UserExec(Option_t *)
   }
 
   fCEvents->Fill(1);
-  fCounter->StoreEvent(aodEvent,fAnalCuts,fUseMCInfo);
+
+  if (fUseMCInfo)
+    fAnalCuts->SetTriggerClass("");
 
   // AOD primary vertex
   fVtx1 = (AliAODVertex*)aodEvent->GetPrimaryVertex();
   if (!fVtx1) return;
 
+  fIsEventSelected = fAnalCuts->IsEventSelected(aodEvent); // better to initialize before CheckEventSelection call
+
+  CheckEventSelection(aodEvent);
+
+
   // fix for temporary bug in ESDfilter 
   fBzkG = (Double_t)aodEvent->GetMagneticField(); 
   if (TMath::Abs(fBzkG)<0.001) return;
   fCEvents->Fill(2);
 
-  Float_t zVertex = fVtx1->GetZ();
-  ((TH1F*)(fOutput->FindObject("hZ2")))->Fill(zVertex);
-
-  if (fVtx1->GetNContributors()<1) return;
-  fCEvents->Fill(3);
-
-  ((TH1F*)(fOutput->FindObject("hZ3")))->Fill(zVertex);
-
   if (!arrayLctopKos) {
     AliInfo("Could not find array of HF cascades, skipping the event");
     return;
@@ -269,70 +256,7 @@ void AliAnalysisTaskSELc2V0bachelor::UserExec(Option_t *)
       AliInfo(Form("Found %d cascades",arrayLctopKos->GetEntriesFast()));
     }
   }
-  fCEvents->Fill(4);
-  ((TH1F*)(fOutput->FindObject("hZ4")))->Fill(zVertex);
-
-  ///////////////////////
-  Bool_t check1 = kFALSE;
-  TString firedTriggerClasses = aodEvent->GetFiredTriggerClasses(); // trigger class
-  if ( !fUseMCInfo && // don't do for MC...
-       (aodEvent->GetRunNumber()<136851 || aodEvent->GetRunNumber()>139517) ) { // ...and for PbPb 2010 data
-    if ( !(firedTriggerClasses.Contains("CINT1")) ) {
-      AliInfo(Form(" ======================== firedTriggerClasses.Data() = %s",firedTriggerClasses.Data()));
-      fCEvents->Fill(8);
-      ((TH1F*)(fOutput->FindObject("hZ8")))->Fill(zVertex);
-      check1 = kTRUE;
-    }
-  }
-
-  ULong64_t fTriggerMask=AliVEvent::kAnyINT;
-  Bool_t isSelectedAAA = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
-  if (!isSelectedAAA) {
-    fCEvents->Fill(9);
-    ((TH1F*)(fOutput->FindObject("hZ9")))->Fill(zVertex);
-  }
-
-  if (!isSelectedAAA || check1) {
-    fCEvents->Fill(16);
-    ((TH1F*)(fOutput->FindObject("hZ16")))->Fill(zVertex);
-  }
-
-  fTriggerMask=AliVEvent::kAny;
-  Bool_t isSelectedBBB = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
-  if (!isSelectedBBB) {
-    fCEvents->Fill(10);
-    ((TH1F*)(fOutput->FindObject("hZ10")))->Fill(zVertex);
-  }
-
-  TString titleVtx=fVtx1->GetTitle();
-  if (titleVtx.Contains("Z")) {
-    fCEvents->Fill(11);
-    ((TH1F*)(fOutput->FindObject("hZ11")))->Fill(zVertex);
-  }
-  else if (titleVtx.Contains("3D")) {
-    fCEvents->Fill(12);
-    ((TH1F*)(fOutput->FindObject("hZ12")))->Fill(zVertex);
-  } else {
-    fCEvents->Fill(13);
-    ((TH1F*)(fOutput->FindObject("hZ13")))->Fill(zVertex);
-  }
-
-  if (TMath::Abs(zVertex)<=fAnalCuts->GetMaxVtxZ()) {
-    fCEvents->Fill(14);
-    ((TH1F*)(fOutput->FindObject("hZ14")))->Fill(zVertex);
-  }
-
-  fIsEventSelected = fAnalCuts->IsEventSelected(aodEvent);
-  if ( fIsEventSelected ) {
-    fCEvents->Fill(7);
-    ((TH1F*)(fOutput->FindObject("hZ7")))->Fill(zVertex);
-  } else {
-    fCEvents->Fill(15);
-    ((TH1F*)(fOutput->FindObject("hZ15")))->Fill(zVertex);
-  }
-  ///////////////////////
-
-  if ( !fIsEventSelected ) return; // don't take into account not selected events 
+  fCEvents->Fill(3);
 
   // mc analysis 
   TClonesArray *mcArray = 0;
@@ -345,8 +269,7 @@ void AliAnalysisTaskSELc2V0bachelor::UserExec(Option_t *)
       AliError("Could not find Monte-Carlo in AOD");
       return;
     }
-    fCEvents->Fill(5); // in case of MC events
-    ((TH1F*)(fOutput->FindObject("hZ5")))->Fill(zVertex);
+    fCEvents->Fill(4); // in case of MC events
 
     // load MC header
     mcHeader = (AliAODMCHeader*)aodEvent->GetList()->FindObject(AliAODMCHeader::StdBranchName());
@@ -354,96 +277,43 @@ void AliAnalysisTaskSELc2V0bachelor::UserExec(Option_t *)
       AliError("AliAnalysisTaskSELc2V0bachelor::UserExec: MC header branch not found!\n");
       return;
     }
-    fCEvents->Fill(6);
-    ((TH1F*)(fOutput->FindObject("hZ6")))->Fill(zVertex);
-
-    // check on MC Lc Daughter
-    for (Int_t iii=0; iii<mcArray->GetEntries(); iii++) {
-      SearchLcDaughter(mcArray,iii);
-    }
-
+    fCEvents->Fill(5); // in case of MC events
   }
 
-  Int_t nSelectedProd = 0;
-  Int_t nSelectedAnal = 0;
-  if (fIsK0sAnalysis) {
-    MakeAnalysisForLc2prK0S(arrayLctopKos,mcArray,
-                           nSelectedProd, fProdCuts, nSelectedAnal, fAnalCuts);
-
-    if (nSelectedAnal) {
-
-      ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(4);
-      ((TH1F*)(fOutput->FindObject("hZ4a")))->Fill(zVertex);
-
-      Bool_t check1a = kFALSE;
-      if ( !fUseMCInfo && // don't do for MC...
-          (aodEvent->GetRunNumber()<136851 || aodEvent->GetRunNumber()>139517) ) { // ...and for PbPb 2010 data
-       if ( !(firedTriggerClasses.Contains("CINT1")) ) {
-         AliInfo(Form(" ======================== firedTriggerClasses.Data() = %s",firedTriggerClasses.Data()));
-         ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(8);
-         ((TH1F*)(fOutput->FindObject("hZ8a")))->Fill(zVertex);
-         check1a = kTRUE;
-       }
-      }
-
-      fTriggerMask=AliVEvent::kAnyINT;
-      Bool_t isSelectedAAAa = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
-      if (!isSelectedAAAa) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(9);
-       ((TH1F*)(fOutput->FindObject("hZ9a")))->Fill(zVertex);
-      }
-
-      if (!isSelectedAAAa || check1a) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(16);
-       ((TH1F*)(fOutput->FindObject("hZ16a")))->Fill(zVertex);
-      }
+  fCounter->StoreEvent(aodEvent,fAnalCuts,fUseMCInfo); // it is very important that it stays BEFORE any other event selection
 
-      fTriggerMask=AliVEvent::kAny;
-      Bool_t isSelectedBBBa = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
-      if (!isSelectedBBBa) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(10);
-       ((TH1F*)(fOutput->FindObject("hZ10a")))->Fill(zVertex);
-      }
+  if (fVtx1->GetNContributors()>0) // this check is done in IsEventSelected
+    fCEvents->Fill(6);
 
-      if (titleVtx.Contains("Z")) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(11);
-       ((TH1F*)(fOutput->FindObject("hZ11a")))->Fill(zVertex);
-      }
-      else if (titleVtx.Contains("3D")) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(12);
-       ((TH1F*)(fOutput->FindObject("hZ12a")))->Fill(zVertex);
-      } else {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(13);
-       ((TH1F*)(fOutput->FindObject("hZ13a")))->Fill(zVertex);
-      }
+  if ( !fIsEventSelected ) return; // don't take into account not selected events 
+  fCEvents->Fill(7);
 
-      if (TMath::Abs(zVertex)<=fAnalCuts->GetMaxVtxZ()) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(14);
-       ((TH1F*)(fOutput->FindObject("hZ14a")))->Fill(zVertex);
-      }
+  Int_t nSelectedAnal = 0;
+  if (fIsK0SAnalysis) {
+    MakeAnalysisForLc2prK0S(arrayLctopKos,mcArray, nSelectedAnal, fAnalCuts);
 
-      if ( fIsEventSelected ) {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(7);
-       ((TH1F*)(fOutput->FindObject("hZ7a")))->Fill(zVertex);
-      } else {
-       ((TH1I*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(15);
-       ((TH1F*)(fOutput->FindObject("hZ15a")))->Fill(zVertex);
-      }
-    }
+    if (nSelectedAnal)
+      CheckEventSelectionWithCandidates(aodEvent);
 
   }
 
-
-  fCounter->StoreCandidates(aodEvent,nSelectedProd,kTRUE);
+  fCounter->StoreCandidates(aodEvent,nSelectedAnal,kTRUE);
   fCounter->StoreCandidates(aodEvent,nSelectedAnal,kFALSE);
 
   PostData(1,fOutput);
-  PostData(2,fOutputAll);
-  PostData(3,fOutputPIDBach);
-  PostData(4,fCounter);
-  if (fWriteVariableTree) PostData(6,fVariablesTree);
+  PostData(2,fCounter);
+  if (!fWriteVariableTree) {
+    PostData(4,fOutputAll);
+    PostData(5,fOutputPIDBach);
+  } else {
+    PostData(4,fVariablesTree);
+  }
+
+  fIsEventSelected=kFALSE;
 
+  return;
 }
+
 //________________________________________ terminate ___________________________
 void AliAnalysisTaskSELc2V0bachelor::Terminate(Option_t*)
 {    
@@ -461,17 +331,18 @@ void AliAnalysisTaskSELc2V0bachelor::Terminate(Option_t*)
   }
   
   //fCEvents = dynamic_cast<TH1F*>(fOutput->FindObject("fCEvents"));
+  if (!fWriteVariableTree) {
+    fOutputAll = dynamic_cast<TList*> (GetOutputData(4));
+    if (!fOutputAll) {
+      AliError("fOutputAll not available");
+      return;
+    }
 
-  fOutputAll = dynamic_cast<TList*> (GetOutputData(2));
-  if (!fOutputAll) {
-    AliError("fOutputAll not available");
-    return;
-  }
-
-  fOutputPIDBach = dynamic_cast<TList*> (GetOutputData(3));
-  if (!fOutputPIDBach) {
-    AliError("fOutputPIDBach not available");
-    return;
+    fOutputPIDBach = dynamic_cast<TList*> (GetOutputData(5));
+    if (!fOutputPIDBach) {
+      AliError("fOutputPIDBach not available");
+      return;
+    }
   }
 
   return;
@@ -481,35 +352,10 @@ void AliAnalysisTaskSELc2V0bachelor::UserCreateOutputObjects() {
   // output
   AliInfo(Form("CreateOutputObjects of task %s\n", GetName()));
   
-  //slot #1  
-  //OpenFile(1);
-  fOutput = new TList();
-  fOutput->SetOwner();
-  fOutput->SetName("chist0");
-
-  fOutputAll = new TList();
-  fOutputAll->SetOwner();
-  fOutputAll->SetName("listAll");
-
-  fOutputPIDBach = new TList();
-  fOutputPIDBach->SetOwner();
-  fOutputPIDBach->SetName("listPIDBach");
-
-  // define histograms
-  DefineHistograms();
-  
   AliAnalysisManager *man=AliAnalysisManager::GetAnalysisManager();
   AliInputEventHandler* inputHandler = (AliInputEventHandler*) (man->GetInputEventHandler());
   fPIDResponse = inputHandler->GetPIDResponse();
 
-  if (fProdCuts->GetIsUsePID()){
-    fProdCuts->GetPidHF()->SetPidResponse(fPIDResponse);
-    fProdCuts->GetPidV0pos()->SetPidResponse(fPIDResponse);
-    fProdCuts->GetPidV0neg()->SetPidResponse(fPIDResponse);
-    fProdCuts->GetPidHF()->SetOldPid(kFALSE);
-    fProdCuts->GetPidV0pos()->SetOldPid(kFALSE);
-    fProdCuts->GetPidV0neg()->SetOldPid(kFALSE);
-  }
   if (fAnalCuts->GetIsUsePID()){
     fAnalCuts->GetPidHF()->SetPidResponse(fPIDResponse);
     fAnalCuts->GetPidV0pos()->SetPidResponse(fPIDResponse);
@@ -519,309 +365,147 @@ void AliAnalysisTaskSELc2V0bachelor::UserCreateOutputObjects() {
     fAnalCuts->GetPidV0neg()->SetOldPid(kFALSE);
   }
 
+  fOutput = new TList();
+  fOutput->SetOwner();
+  fOutput->SetName("chist0");
+  DefineGeneralHistograms(); // define general histograms
   PostData(1,fOutput);
-  PostData(2,fOutputAll);
-  PostData(3,fOutputPIDBach);
 
   fCounter = new AliNormalizationCounter("NormalizationCounter");
   fCounter->Init();
-  PostData(4,fCounter);
-
-  // Output slot 6: tree of the candidate variables
-  if (fWriteVariableTree) {
-    const char* nameoutput = GetOutputSlot(6)->GetContainer()->GetName();
-    fVariablesTree = new TTree(nameoutput,"Candidates variables tree");
-    Int_t nVar = 80;
-    fCandidateVariables = new Float_t [nVar];
-    TString * fCandidateVariableNames = new TString[nVar];
-    fCandidateVariableNames[0]="isLcByMC";
-    fCandidateVariableNames[1]="isV0ByMC";
-    fCandidateVariableNames[2]="massLc2K0Sp";
-    fCandidateVariableNames[3]="massLc2Lambdapi";
-    fCandidateVariableNames[4]="massK0S";
-    fCandidateVariableNames[5]="massLambda";
-    fCandidateVariableNames[6]="massLambdaBar";
-    fCandidateVariableNames[7]="cosPAK0S";
-    fCandidateVariableNames[8]="dcaV0ptp";
-    fCandidateVariableNames[9]="tImpParBach";
-    fCandidateVariableNames[10]="tImpParV0";
-    fCandidateVariableNames[11]="nSigmaTPCpr";
-    fCandidateVariableNames[12]="nSigmaTPCpi";
-    fCandidateVariableNames[13]="nSigmaTPCka";
-    fCandidateVariableNames[14]="nSigmaTOFpr";
-    fCandidateVariableNames[15]="nSigmaTOFpi";
-    fCandidateVariableNames[16]="nSigmaTOFka";
-    fCandidateVariableNames[17]="bachelorPx";
-    fCandidateVariableNames[18]="bachelorPy";
-    fCandidateVariableNames[19]="bachelorPz";
-    fCandidateVariableNames[20]="V0positivePx";
-    fCandidateVariableNames[21]="V0positivePy";
-    fCandidateVariableNames[22]="V0positivePz";
-    fCandidateVariableNames[23]="V0negativePx";
-    fCandidateVariableNames[24]="V0negativePy";
-    fCandidateVariableNames[25]="V0negativePz";
-    fCandidateVariableNames[26]="qtLc";
-    fCandidateVariableNames[27]="alphaLc";
-    fCandidateVariableNames[28]="dcaV0postoPV";
-    fCandidateVariableNames[29]="dcaV0negtoPV";
-    fCandidateVariableNames[30]="bachelorPxDCA";
-    fCandidateVariableNames[31]="bachelorPyDCA";
-    fCandidateVariableNames[32]="bachelorPzDCA";
-    fCandidateVariableNames[33]="v0PxDCA";
-    fCandidateVariableNames[34]="v0PyDCA";
-    fCandidateVariableNames[35]="v0PzDCA";
-    fCandidateVariableNames[36]="V0positivePxDCA";
-    fCandidateVariableNames[37]="V0positivePyDCA";
-    fCandidateVariableNames[38]="V0positivePzDCA";
-    fCandidateVariableNames[39]="V0negativePxDCA";
-    fCandidateVariableNames[40]="V0negativePyDCA";
-    fCandidateVariableNames[41]="V0negativePzDCA";
-    fCandidateVariableNames[42]="flagToCheckCandidate";
-    fCandidateVariableNames[43]="massGamma";
-
-    fCandidateVariableNames[44]="bachelorP"; // @ prim vtx
-    fCandidateVariableNames[45]="bachelorPt"; // @ prim vtx
-    fCandidateVariableNames[46]="V0positiveP"; // @ prim vtx
-    fCandidateVariableNames[47]="V0positivePt"; // @ prim vtx
-    fCandidateVariableNames[48]="V0negativeP"; // @ prim vtx
-    fCandidateVariableNames[49]="V0negativePt"; // @ prim vtx
-    fCandidateVariableNames[50]="bachelorPDCA"; // @ DCA
-    fCandidateVariableNames[51]="bachelorPtDCA"; // @ DCA
-    fCandidateVariableNames[52]="v0PDCA"; // @ DCA
-    fCandidateVariableNames[53]="v0PtDCA"; // @ DCA
-    fCandidateVariableNames[54]="V0positivePDCA"; // @ DCA
-    fCandidateVariableNames[55]="V0positivePtDCA"; // @ DCA
-    fCandidateVariableNames[56]="V0negativePDCA"; // @ DCA
-    fCandidateVariableNames[57]="V0negativePtDCA"; // @ DCA
-    fCandidateVariableNames[58]="LcP"; // @ DCA
-    fCandidateVariableNames[59]="LcPt"; // @ DCA
-    fCandidateVariableNames[60]="v0P"; // @ V0 DCA
-    fCandidateVariableNames[61]="v0Pt"; // @ V0 DCA
-
-    fCandidateVariableNames[62]="cosPALc";
-    fCandidateVariableNames[63]="decayLengthLc";
-    fCandidateVariableNames[64]="decayLengthV0";
-
-    fCandidateVariableNames[65]="yLc";
-
-    fCandidateVariableNames[66]="massD2K0Spi"; // D+ -> pi+ K0S
-    fCandidateVariableNames[67]="massDS2K0SK"; // D+S -> K+ K0S
-
-    fCandidateVariableNames[68]="nSigmaITSpr"; // nSigmaITSpr
-    fCandidateVariableNames[69]="nSigmaITSpi"; // nSigmaITSpi
-    fCandidateVariableNames[70]="nSigmaITSka"; // nSigmaITSka
-
-    fCandidateVariableNames[71]="dcaLcptp"; // DCA Lc prong-to-prong
-
-    fCandidateVariableNames[72]="cosPAV0XY"; // cosPA XY x V0
-    fCandidateVariableNames[73]="cosPALcXY"; // cosPA XY x V0
-
-    fCandidateVariableNames[74]="decayLengthV0XY"; // decay length XY x V0
-    fCandidateVariableNames[75]="decayLengthLcXY"; // decay length XY x V0
-
-    fCandidateVariableNames[76]="normalizedDecayLengthV0"; // normalized decay length x V0
-    fCandidateVariableNames[77]="normalizedDecayLengthLc"; // normalized decay length x Lc
-
-    fCandidateVariableNames[78]="normalizedDecayLengthXYV0"; // normalized decay length XY x V0
-    fCandidateVariableNames[79]="normalizedDecayLengthXYLc"; // normalized decay length XY x Lc
-
-    for(Int_t ivar=0; ivar<nVar; ivar++){
-      fVariablesTree->Branch(fCandidateVariableNames[ivar].Data(),&fCandidateVariables[ivar],Form("%s/f",fCandidateVariableNames[ivar].Data()));
-    }
-    PostData(6,fVariablesTree);
+  PostData(2,fCounter);
+
+  if (!fWriteVariableTree) {
+
+    fOutputAll = new TList();
+    fOutputAll->SetOwner();
+    fOutputAll->SetName("listAll");
+
+    fOutputPIDBach = new TList();
+    fOutputPIDBach->SetOwner();
+    fOutputPIDBach->SetName("listPIDBach");
+
+    DefineAnalysisHistograms(); // define analysis histograms
+  
+    PostData(4,fOutputAll);
+    PostData(5,fOutputPIDBach);
+  }
+  else {
+    DefineTreeVariables();
+    PostData(4,fVariablesTree);
   }
 
   return;
 }
-//___________________________________ hiostograms _______________________________________
-void  AliAnalysisTaskSELc2V0bachelor::DefineHistograms() {
 
-  fCEvents = new TH1F("fCEvents","conter",17,0,17);
-  fCEvents->SetStats(kTRUE);
-  fCEvents->GetXaxis()->SetBinLabel(1,"X1");
-  fCEvents->GetXaxis()->SetBinLabel(2,"Analyzed events");
-  fCEvents->GetXaxis()->SetBinLabel(3,"AliAODVertex exists");
-  fCEvents->GetXaxis()->SetBinLabel(4,"GetNContributors()>0");
-  fCEvents->GetXaxis()->SetBinLabel(5,"MCarray exists");
-  fCEvents->GetXaxis()->SetBinLabel(6,"CascadesHF exists");
-  fCEvents->GetXaxis()->SetBinLabel(7,"MCheader exists");
-  fCEvents->GetXaxis()->SetBinLabel(8,"IsEventSelected");
-  fCEvents->GetXaxis()->SetBinLabel(9,"triggerClass!=CINT1");
-  fCEvents->GetXaxis()->SetBinLabel(10,"triggerMask!=kAnyINT");
-  fCEvents->GetXaxis()->SetBinLabel(11,"triggerMask!=kAny");
-  fCEvents->GetXaxis()->SetBinLabel(12,"vtxTitle.Contains(Z)");
-  fCEvents->GetXaxis()->SetBinLabel(13,"vtxTitle.Contains(3D)");
-  fCEvents->GetXaxis()->SetBinLabel(14,"vtxTitle.Doesn'tContain(Z-3D)");
-  fCEvents->GetXaxis()->SetBinLabel(15,Form("zVtx<=%2.0fcm",fAnalCuts->GetMaxVtxZ()));
-  fCEvents->GetXaxis()->SetBinLabel(16,"!IsEventSelected");
-  fCEvents->GetXaxis()->SetBinLabel(17,"triggerMask!=kAnyINT || triggerClass!=CINT1");
-  //fCEvents->GetXaxis()->SetTitle("");
-  fCEvents->GetYaxis()->SetTitle("counts");
-
-  fOutput->Add(fCEvents);
-  TString fillthis="";
+//-------------------------------------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelor::MakeAnalysisForLc2prK0S(TClonesArray *arrayLctopKos,
+                                                            TClonesArray *mcArray,
+                                                            Int_t &nSelectedAnal,
+                                                            AliRDHFCutsLctoV0 *cutsAnal)
+{
 
-  if (fUseMCInfo) {
-    fillthis="histMcStatLc";
-    TH1F* mcStatisticLc = new TH1F(fillthis.Data(),"#Lambda_{C} generated and their decays",19,-9.5,9.5);
-    fOutput->Add(mcStatisticLc);
-  }
+  // make the analysis
 
-  //fillthis="histopionV0SigmaVspTOF";
-  //TH2F *hpionV0SigmaVspTOF=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-  fillthis="histoprotonBachSigmaVspTOF";
-  TH2F *hprotonBachSigmaVspTOF=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+  Int_t pdgCand = 4122;
+  Int_t pdgDgLctoV0bachelor[2]={2212,310}; // always 1st bachelor, 2nd V0
+  Int_t pdgDgV0toDaughters[2]={211,211};
 
-  //fOutput->Add(hpionV0SigmaVspTOF);
-  fOutput->Add(hprotonBachSigmaVspTOF);
+  // loop over cascades to search for candidates Lc->p+K0S
+  Int_t nCascades= arrayLctopKos->GetEntriesFast();
+  if (nCascades==0) {
+    AliInfo("Could not find cascades, skipping the event");
+    return;
+  }
 
-  //fillthis="histopionV0SigmaVspTPC";
-  //TH2F *hpionV0SigmaVspTPC=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-  fillthis="histoprotonBachSigmaVspTPC";
-  TH2F *hprotonBachSigmaVspTPC=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+  for (Int_t iLctopK0S = 0; iLctopK0S<nCascades; iLctopK0S++) {
 
-  //fOutput->Add(hpionV0SigmaVspTPC);
-  fOutput->Add(hprotonBachSigmaVspTPC);
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(0);
 
-  if (fUseMCInfo) {
+    // Lc candidates and K0S from Lc
+    AliAODRecoCascadeHF* lcK0Spr = dynamic_cast<AliAODRecoCascadeHF*>(arrayLctopKos->At(iLctopK0S));
+    if (!lcK0Spr) {
+      AliDebug(2,Form("Cascade %d doens't exist, skipping",iLctopK0S));
+      continue;
+    }
 
-    //fillthis="histopionV0SigmaVspTOFsgn";
-    //TH2F *hpionV0SigmaVspTOFsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-    fillthis="histoprotonBachSigmaVspTOFsgn";
-    TH2F *hprotonBachSigmaVspTOFsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    if (!lcK0Spr->GetSecondaryVtx()) {
+      AliInfo("No secondary vertex"); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
-    //fOutput->Add(hpionV0SigmaVspTOFsgn);
-    fOutput->Add(hprotonBachSigmaVspTOFsgn);
+    if (lcK0Spr->GetNDaughters()!=2) {
+      AliDebug(2,Form("Cascade %d has not 2 daughters (nDaughters=%d)",iLctopK0S,lcK0Spr->GetNDaughters())); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
-    //fillthis="histopionV0SigmaVspTPCsgn";
-    //TH2F *hpionV0SigmaVspTPCsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-    fillthis="histoprotonBachSigmaVspTPCsgn";
-    TH2F *hprotonBachSigmaVspTPCsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    AliAODv0 * v0part = dynamic_cast<AliAODv0*>(lcK0Spr->Getv0());
+    AliAODTrack * bachPart = dynamic_cast<AliAODTrack*>(lcK0Spr->GetBachelor());
+    if (!v0part || !bachPart) {
+      AliDebug(2,Form("Cascade %d has no V0 or no bachelor object",iLctopK0S)); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
-    //fOutput->Add(hpionV0SigmaVspTPCsgn);
-    fOutput->Add(hprotonBachSigmaVspTPCsgn);
+    if (!v0part->GetSecondaryVtx()) {
+      AliDebug(2,Form("No secondary vertex for V0 by cascade %d",iLctopK0S)); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
+    if (v0part->GetNDaughters()!=2) {
+      AliDebug(2,Form("current V0 has not 2 daughters (onTheFly=%d, nDaughters=%d)",v0part->GetOnFlyStatus(),v0part->GetNDaughters())); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
-    //fillthis="histopionV0SigmaVspTOFbkg";
-    //TH2F *hpionV0SigmaVspTOFbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-    fillthis="histoprotonBachSigmaVspTOFbkg";
-    TH2F *hprotonBachSigmaVspTOFbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    AliAODTrack * v0Pos = dynamic_cast<AliAODTrack*>(lcK0Spr->Getv0PositiveTrack());
+    AliAODTrack * v0Neg = dynamic_cast<AliAODTrack*>(lcK0Spr->Getv0NegativeTrack());
+    if (!v0Neg || !v0Neg) {
+      AliDebug(2,Form("V0 by cascade %d has no V0positive of V0negative object",iLctopK0S)); // it will be done in AliRDHFCutsLctoV0::IsSelected
+      continue;
+    }
 
-    //fOutput->Add(hpionV0SigmaVspTOFbkg);
-    fOutput->Add(hprotonBachSigmaVspTOFbkg);
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(1);
 
-    //fillthis="histopionV0SigmaVspTPCbkg";
-    //TH2F *hpionV0SigmaVspTPCbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
-    fillthis="histoprotonBachSigmaVspTPCbkg";
-    TH2F *hprotonBachSigmaVspTPCbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    if (v0Pos->Charge() == v0Neg->Charge()) continue;
 
-    //fOutput->Add(hpionV0SigmaVspTPCbkg);
-    fOutput->Add(hprotonBachSigmaVspTPCbkg);
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(2);
 
+    Int_t isLc = 0;
 
-  }
+    if (fUseMCInfo) {
 
+      Int_t pdgCode=-2;
 
-  TH1F *hZ2 = new TH1F("hZ2","",100,-50.,50.);
-  fOutput->Add(hZ2);
-  TH1F *hZ3 = new TH1F("hZ3","",100,-50.,50.);
-  fOutput->Add(hZ3);
-  TH1F *hZ4 = new TH1F("hZ4","",100,-50.,50.);
-  fOutput->Add(hZ4);
-  TH1F *hZ5 = new TH1F("hZ5","",100,-50.,50.);
-  fOutput->Add(hZ5);
-  TH1F *hZ6 = new TH1F("hZ6","",100,-50.,50.);
-  fOutput->Add(hZ6);
-  TH1F *hZ7 = new TH1F("hZ7","",100,-50.,50.);
-  fOutput->Add(hZ7);
-  TH1F *hZ8 = new TH1F("hZ8","",100,-50.,50.);
-  fOutput->Add(hZ8);
-  TH1F *hZ9 = new TH1F("hZ9","",100,-50.,50.);
-  fOutput->Add(hZ9);
-  TH1F *hZ10 = new TH1F("hZ10","",100,-50.,50.);
-  fOutput->Add(hZ10);
-  TH1F *hZ11 = new TH1F("hZ11","",100,-50.,50.);
-  fOutput->Add(hZ11);
-  TH1F *hZ12 = new TH1F("hZ12","",100,-50.,50.);
-  fOutput->Add(hZ12);
-  TH1F *hZ13 = new TH1F("hZ13","",100,-50.,50.);
-  fOutput->Add(hZ13);
-  TH1F *hZ14 = new TH1F("hZ14","",100,-50.,50.);
-  fOutput->Add(hZ14);
-  TH1F *hZ15 = new TH1F("hZ15","",100,-50.,50.);
-  fOutput->Add(hZ15);
-  TH1F *hZ16 = new TH1F("hZ16","",100,-50.,50.);
-  fOutput->Add(hZ16);
-
-  TH1I *hCandidateSelection = new TH1I("hCandidateSelection","",14,0,14);
-  hCandidateSelection->GetXaxis()->SetBinLabel(1,"IsEventSelected");
-  hCandidateSelection->GetXaxis()->SetBinLabel(2,"IsSecondaryVtx");
-  hCandidateSelection->GetXaxis()->SetBinLabel(3,"V0toPosNeg");
-  hCandidateSelection->GetXaxis()->SetBinLabel(4,"offlineV0");
-  hCandidateSelection->GetXaxis()->SetBinLabel(5,"prodCuts::kTracks");
-  hCandidateSelection->GetXaxis()->SetBinLabel(6,"prodCuts::kCandidate");
-  hCandidateSelection->GetXaxis()->SetBinLabel(7,"prodCuts::kPID");
-  hCandidateSelection->GetXaxis()->SetBinLabel(8,"prodCuts::kAll");
-  hCandidateSelection->GetXaxis()->SetBinLabel(9,"offlineV0");
-  hCandidateSelection->GetXaxis()->SetBinLabel(10,"isInFiducialAcceptance");
-  hCandidateSelection->GetXaxis()->SetBinLabel(11,"analCuts::kTracks");
-  hCandidateSelection->GetXaxis()->SetBinLabel(12,"analCuts::kCandidate");
-  hCandidateSelection->GetXaxis()->SetBinLabel(13,"analCuts::kPID");
-  hCandidateSelection->GetXaxis()->SetBinLabel(14,"analCuts::kAll");
-  fOutput->Add(hCandidateSelection);
+      // find associated MC particle for Lc -> p+K0 and K0S->pi+pi
+      Int_t mcLabel = lcK0Spr->MatchToMC(pdgCand,pdgDgLctoV0bachelor[1],pdgDgLctoV0bachelor,pdgDgV0toDaughters,mcArray,kTRUE);
+      if (mcLabel!=-1) {
+       AliDebug(2,Form(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cascade number %d (total cascade number = %d)", iLctopK0S,nCascades));
 
-  TH1I *hEventsWithCandidates = new TH1I("hEventsWithCandidates","conter",17,0,17);
-  fOutput->Add(hEventsWithCandidates);
+       AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel));
+       if (partLc) {
+         pdgCode = partLc->GetPdgCode();
+         if (pdgCode<0) AliDebug(2,Form(" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ MClabel=%d ~~~~~~~~~~ pdgCode=%d", mcLabel, pdgCode));
+         pdgCode = TMath::Abs(pdgCode);
+         isLc = 1;
+       }
+      } else {
+       AliDebug(2,Form("No candidate (cascade number %d -total cascade number = %d -)", iLctopK0S,nCascades));
+       pdgCode=-1;
+      }
+    }
 
-  TH1F *hZ2a = new TH1F("hZ2a","",100,-50.,50.);
-  fOutput->Add(hZ2a);
-  TH1F *hZ3a = new TH1F("hZ3a","",100,-50.,50.);
-  fOutput->Add(hZ3a);
-  TH1F *hZ4a = new TH1F("hZ4a","",100,-50.,50.);
-  fOutput->Add(hZ4a);
-  TH1F *hZ5a = new TH1F("hZ5a","",100,-50.,50.);
-  fOutput->Add(hZ5a);
-  TH1F *hZ6a = new TH1F("hZ6a","",100,-50.,50.);
-  fOutput->Add(hZ6a);
-  TH1F *hZ7a = new TH1F("hZ7a","",100,-50.,50.);
-  fOutput->Add(hZ7a);
-  TH1F *hZ8a = new TH1F("hZ8a","",100,-50.,50.);
-  fOutput->Add(hZ8a);
-  TH1F *hZ9a = new TH1F("hZ9a","",100,-50.,50.);
-  fOutput->Add(hZ9a);
-  TH1F *hZ10a = new TH1F("hZ10a","",100,-50.,50.);
-  fOutput->Add(hZ10a);
-  TH1F *hZ11a = new TH1F("hZ11a","",100,-50.,50.);
-  fOutput->Add(hZ11a);
-  TH1F *hZ12a = new TH1F("hZ12a","",100,-50.,50.);
-  fOutput->Add(hZ12a);
-  TH1F *hZ13a = new TH1F("hZ13a","",100,-50.,50.);
-  fOutput->Add(hZ13a);
-  TH1F *hZ14a = new TH1F("hZ14a","",100,-50.,50.);
-  fOutput->Add(hZ14a);
-  TH1F *hZ15a = new TH1F("hZ15a","",100,-50.,50.);
-  fOutput->Add(hZ15a);
-  TH1F *hZ16a = new TH1F("hZ16a","",100,-50.,50.);
-  fOutput->Add(hZ16a);
-
-  TH1I *hSwitchOnCandidates1 = new TH1I("hSwitchOnCandidates1","",16,-8,8);
-  fOutput->Add(hSwitchOnCandidates1);
-  TH1I *hSwitchOnCandidates2 = new TH1I("hSwitchOnCandidates2","",16,-8,8);
-  fOutput->Add(hSwitchOnCandidates2);
-  TH1I *hSwitchOnCandidates3 = new TH1I("hSwitchOnCandidates3","",16,-8,8);
-  fOutput->Add(hSwitchOnCandidates3);
-  TH1I *hSwitchOnCandidates4 = new TH1I("hSwitchOnCandidates4","",16,-8,8);
-  fOutput->Add(hSwitchOnCandidates4);
+    FillLc2pK0Sspectrum(lcK0Spr, isLc,
+                       nSelectedAnal, cutsAnal,
+                       mcArray);
+  }
 
-  if (fIsK0sAnalysis) DefineK0SHistos();// hK0S histos declarations
+  AliDebug(2, Form("Found %d Reco particles that are Lc!!", nSelectedAnal));
 
   return;
 }
+
 //________________________________________________________________________
 void AliAnalysisTaskSELc2V0bachelor::FillLc2pK0Sspectrum(AliAODRecoCascadeHF *part,
                                                         Int_t isLc,
-                                                        Int_t &nSelectedProd,
-                                                        AliRDHFCutsLctoV0 *cutsProd,
                                                         Int_t &nSelectedAnal,
                                                         AliRDHFCutsLctoV0 *cutsAnal,
                                                         TClonesArray *mcArray)
@@ -832,657 +516,675 @@ void AliAnalysisTaskSELc2V0bachelor::FillLc2pK0Sspectrum(AliAODRecoCascadeHF *pa
 
   TString fillthis="";
 
-  Double_t invmassLc = part->InvMassLctoK0sP();
-  Double_t lambdacpt = part->Pt();
+  AliAODTrack *bachelor = (AliAODTrack*)part->GetBachelor();
+  Double_t momBach  = bachelor->P();
 
   AliAODv0 * v0part = (AliAODv0*)part->Getv0();
   Bool_t onFlyV0 = v0part->GetOnFlyStatus(); // on-the-flight V0s
-  Double_t momK0s = TMath::Sqrt(v0part->Ptot2V0());
-  Double_t ptK0s = TMath::Sqrt(v0part->Pt2V0());
-  Double_t dcaV0ptp = v0part->GetDCA();
-  Double_t invmassK0s = v0part->MassK0Short();
-  Bool_t isInV0windowProd = (((cutsProd->IsSelectedSingleCut(part,AliRDHFCuts::kCandidate,2))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // cut on V0 invMass
+
+  Bool_t areCutsUsingPID = cutsAnal->GetIsUsePID();
+  cutsAnal->SetUsePID(kFALSE);
   Bool_t isInCascadeWindow = (((cutsAnal->IsSelectedSingleCut(part,AliRDHFCuts::kCandidate,0))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // cut on Lc->p+K0S invMass
-  Bool_t isCandidateSelectedCuts = (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // kinematic/topological cuts
+  cutsAnal->SetUsePID(areCutsUsingPID);
 
-  AliAODTrack *bachelor = (AliAODTrack*)part->GetBachelor();
-  Double_t momBach  = bachelor->P();
+  //if ( !( !onFlyV0 || (onFlyV0 && fUseOnTheFlyV0) ) ) return;
+  if ( onFlyV0 && !fUseOnTheFlyV0 ) return;
+
+  if (fAdditionalChecks) CheckCandidatesAtDifferentLevels(part,cutsAnal);
+
+  if ( !( ( (cutsAnal->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) ) return;
+
+  if ( !(cutsAnal->IsInFiducialAcceptance(part->Pt(),part->Y(4122))) ) return;
+
+  if ( ( ( (cutsAnal->IsSelected(part,AliRDHFCuts::kAll))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) ) nSelectedAnal++;
+
+  // Fill candidate variable Tree (track selection, V0 invMass selection)
+  if ( fWriteVariableTree ) {
+    Double_t invmassK0S = v0part->MassK0Short();
+    Double_t mk0sPDG = TDatabasePDG::Instance()->GetParticle(310)->Mass();
+    if ( !onFlyV0 && isInCascadeWindow && part->CosV0PointingAngle()>0.99 && TMath::Abs(invmassK0S-mk0sPDG)<=0.05)
+      FillTheTree(part,cutsAnal,mcArray,isLc);
+    return;
+  }
+
+  cutsAnal->SetUsePID(kFALSE);
+  Bool_t isCandidateSelectedCuts = (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // kinematic/topological cuts
+  cutsAnal->SetUsePID(areCutsUsingPID);
   Bool_t isBachelorID = (((cutsAnal->IsSelected(part,AliRDHFCuts::kPID))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)); // ID x bachelor
 
-  /*
-  if (fIsEventSelected) {
-    if ( ( !onFlyV0 ||
-          (onFlyV0 && fUseOnTheFlyV0) ) &&
-        isCandidateSelectedCuts && isBachelorID) {
-      fillthis="hist1LcMassOfflineByK0S";
-      if (isBachelorID) ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-    }
-  } else {
-    if ( ( !onFlyV0 ||
-          (onFlyV0 && fUseOnTheFlyV0) ) &&
-        isCandidateSelectedCuts && isBachelorID) {
-      fillthis="hist0LcMassOfflineByK0S";
-      if (isBachelorID) ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
+  //if (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
+  if (((cutsAnal->IsSelected(part,AliRDHFCuts::kAll))&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
+    if (fUseMCInfo && isLc && !fWriteVariableTree) {
+      Int_t pdgCand1 = 4122;
+      Int_t pdgDgLctoV0bachelor1[2]={2212,310};
+      Int_t pdgDgV0toDaughters1[2]={211,211};
+      Int_t mcLabel1=part->MatchToMC(pdgCand1,pdgDgLctoV0bachelor1[1],pdgDgLctoV0bachelor1,pdgDgV0toDaughters1,mcArray,kTRUE);
+      AliDebug(2,Form(" Found true MC candidate: Lc->pK0S(%d) - onTheFly=%1d",mcLabel1,onFlyV0));
     }
-    return; // don't take into account not selected events
   }
-  */
 
-  if ( !onFlyV0 )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(3); // it counts number of candidates coming from offline V0s
-
-  if ( !( !onFlyV0 || (onFlyV0 && fUseOnTheFlyV0) ) ) return;
-
-  if ( !( ( (cutsProd->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) ) return;
-  ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(4);
-  if ( ( ( (cutsProd->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(5);
-  if ( ( ( (cutsProd->IsSelected(part,AliRDHFCuts::kPID))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(6);
-  if ( ( ( (cutsProd->IsSelected(part,AliRDHFCuts::kAll))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) ) {
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(7);
-    nSelectedProd++;
-  }
+  Double_t nSigmaTPCpr=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,4,nSigmaTPCpr);
+  Double_t nSigmaTOFpr=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,4,nSigmaTOFpr);
 
+  Double_t nSigmaTPCpi=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,2,nSigmaTPCpi);
+  Double_t nSigmaTOFpi=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,2,nSigmaTOFpi);
 
-  if ( !onFlyV0 )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(8); // it counts number of candidates coming from offline V0s
+  Double_t nSigmaTPCka=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,3,nSigmaTPCka);
+  Double_t nSigmaTOFka=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,3,nSigmaTOFka);
+
+  //if (onFlyV0 && fUseOnTheFlyV0) {  
+  if (onFlyV0) {  
+
+    if (isCandidateSelectedCuts)
+      FillAnalysisHistograms(part,isBachelorID,"");
 
-  if ( cutsAnal->IsInFiducialAcceptance(part->Pt(),part->Y(4122)) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(9);
-  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(10);
-  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(11);
-  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kPID))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(12);
-  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kAll))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) ) {
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(13);
-    nSelectedAnal++;
   }
+  //else if (!onFlyV0) {
+  else {
 
-  if ( !(cutsAnal->IsInFiducialAcceptance(part->Pt(),part->Y(4122))) ) return;
+    if (isCandidateSelectedCuts) {
 
-  if ( !( ( (cutsAnal->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) ) return;
+      fillthis="histoprotonBachSigmaVspTOF";
+      ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
+      fillthis="histoprotonBachSigmaVspTPC";
+      ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
 
-  Int_t aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kTracks);
-  if ( (aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr ) {
-    if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1)  ||
-        ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates1")))->Fill( -aaa );
-    else
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates1")))->Fill( aaa );
-  }
+      FillAnalysisHistograms(part,isBachelorID,"Offline");
 
-  aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate);
-  if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
-    if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
-        ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates2")))->Fill( -aaa );
-    else
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates2")))->Fill( aaa );
-  }
+    }
 
-  aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kPID);
-  if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
-    if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
-        ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates3")))->Fill( -aaa );
-    else
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates3")))->Fill( aaa );
   }
 
-  aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kAll);
-  if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
-    if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
-        ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates4")))->Fill( -aaa );
-    else
-      ((TH1I*)(fOutput->FindObject("hSwitchOnCandidates4")))->Fill( aaa );
-  }
 
+  if (fUseMCInfo) {
+    if (isLc==1) {
+      //if (onFlyV0 && fUseOnTheFlyV0) {
+      if (onFlyV0) {
+
+       if (isCandidateSelectedCuts)
+         FillAnalysisHistograms(part,isBachelorID,"Sgn");
+    
+      }     
+      //else if (!onFlyV0) {  
+      else {  
 
+       if (isCandidateSelectedCuts) {
 
+         fillthis="histoprotonBachSigmaVspTOFsgn";
+         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
+         fillthis="histoprotonBachSigmaVspTPCsgn";
+         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
+
+         FillAnalysisHistograms(part,isBachelorID,"OfflineSgn");
+
+       }
 
-  aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate);
-  Int_t flagToCheckCandidate = 0;
-  if ( (aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr ) {
-    if ( aaa==AliRDHFCutsLctoV0::kLcToK0Spr ) {
-      flagToCheckCandidate = aaa; // Lc->K0S+p OK
-    } else {
-      if ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi ) {
-       if (bachelor->Charge()==+1)
-         flagToCheckCandidate = aaa; // Lc->Lambda+pi+
-       else if (bachelor->Charge()==-1)
-         flagToCheckCandidate =-aaa;//+(AliRDHFCutsLctoV0::kLcToK0Spr); // Lambda+pi- AS Lc->K0S+p candidate
-      }
-      if ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi ) {
-       if (bachelor->Charge()==-1)
-         flagToCheckCandidate = aaa; // Lc->LambdaBar+pi-
-       else if (bachelor->Charge()==+1)
-         flagToCheckCandidate =-aaa;//+(AliRDHFCutsLctoV0::kLcToK0Spr); // LambdaBar+pi+ AS Lc->K0S+p candidate
       }
-    }
-  } else {
-    if ( aaa==AliRDHFCutsLctoV0::kLcToK0Spr ) {
-      flagToCheckCandidate = -10-(AliRDHFCutsLctoV0::kLcToK0Spr); // NEVER
-    } else {
-      if ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi ) {
-       if (bachelor->Charge()==+1)
-         flagToCheckCandidate = aaa; // Lc->Lambda+pi+ OK
-       else if (bachelor->Charge()==-1)
-         flagToCheckCandidate =-aaa;//+(AliRDHFCutsLctoV0::kLcToK0Spr); // Lambda+pi- AS Lc->Lambda+pi+ candidate
+
+    }// sgn
+    else { // bkg
+      //if (onFlyV0 && fUseOnTheFlyV0) {
+      if (onFlyV0) {
+
+       if (isCandidateSelectedCuts)
+         FillAnalysisHistograms(part,isBachelorID,"Bkg");
+
       }
-      if ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi ) {
-       if (bachelor->Charge()==-1)
-         flagToCheckCandidate = aaa; // Lc->LambdaBar+pi- OK
-       else if (bachelor->Charge()==+1)
-         flagToCheckCandidate =-aaa;//+(AliRDHFCutsLctoV0::kLcToK0Spr); // LambdaBar+pi+ AS Lc->LambdaBar+pi- candidate
+      //else if (!onFlyV0) {
+      else {
+
+       if (isCandidateSelectedCuts) {
+
+         fillthis="histoprotonBachSigmaVspTOFbkg";
+         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
+         fillthis="histoprotonBachSigmaVspTPCbkg";
+         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
+
+         FillAnalysisHistograms(part,isBachelorID,"OfflineBkg");
+       }
+
       }
+
     }
-  }
+  } // if fUseMCInfo
+  return;
+}
 
+//----------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelor::DefineK0SHistos()
+{ 
 
+  TString nameMass=" ", nameSgn=" ", nameBkg=" ";
 
-  Int_t pdgCand = 4122;
-  Int_t pdgDgLctoV0bachelor[2]={211,3122};
-  Int_t pdgDgV0toDaughters[2]={2212,211};
-  Int_t isLc2LBarpi=0, isLc2Lpi=0;
-  Int_t mcLabel = 0;
-  if (fUseMCInfo) {
-    mcLabel = part->MatchToMC(pdgCand,pdgDgLctoV0bachelor[1],pdgDgLctoV0bachelor,pdgDgV0toDaughters,mcArray,kTRUE);
-    if (mcLabel>=0) {
-      if (bachelor->Charge()==-1) isLc2LBarpi=1;
-      if (bachelor->Charge()==+1) isLc2Lpi=1;
-    }
-  }
+  Double_t mLcPDG  = TDatabasePDG::Instance()->GetParticle(4122)->Mass();
+  Double_t mK0SPDG = TDatabasePDG::Instance()->GetParticle(310)->Mass();
 
-  Int_t pdgDg2prong[2] = {211, 211};
-  Int_t labelK0S = 0;
-  Int_t isK0S = 0;
-  if (fUseMCInfo) {
-    labelK0S = v0part->MatchToMC(310,mcArray,2,pdgDg2prong);
-    if (labelK0S>=0) isK0S = 1;
-  }
+  if (fUseOnTheFlyV0) {
 
-  pdgDg2prong[0] = 211;
-  pdgDg2prong[1] = 2212;
-  Int_t isLambda = 0;
-  Int_t isLambdaBar = 0;
-  Int_t lambdaLabel = 0;
-  if (fUseMCInfo) {
-    lambdaLabel = v0part->MatchToMC(3122,mcArray,2,pdgDg2prong);
-    if (lambdaLabel>=0) {
-      AliAODMCParticle *lambdaTrack = (AliAODMCParticle*)mcArray->At(lambdaLabel);
-      if (lambdaTrack->GetPdgCode()==3122) isLambda = 1;
-      else if (lambdaTrack->GetPdgCode()==-3122) isLambdaBar = 1;
-    }
-  }
+    // V0 invariant masses (on-the-fly)
+    nameMass="histK0SMass";
+    TH2F* spectrumK0SMass = new TH2F(nameMass.Data(),"K^{0}_{S} invariant mass VS p_{T}; M(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                   1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
 
-  pdgDg2prong[0] = 11;
-  pdgDg2prong[1] = 11;
-  Int_t isGamma = 0;
-  Int_t gammaLabel = 0;
-  if (fUseMCInfo) {
-    gammaLabel = v0part->MatchToMC(22,mcArray,2,pdgDg2prong);
-    if (gammaLabel>=0) {
-      AliAODMCParticle *gammaTrack = (AliAODMCParticle*)mcArray->At(gammaLabel);
-      if (gammaTrack->GetPdgCode()==22) isGamma = 1;
-    }
-  }
+    // Lc invariant masses (x K0S on-the-fly)
+    nameMass="histLcMassByK0S";
+    TH2F* spectrumLcMassByK0S = new TH2F(nameMass.Data(),"#Lambda_{c} invariant mass (by K^{0}_{S}) vs p_{T} ; m_{inv}(p-K^{0}_{S}) [GeV/c^{2}]; p_{T}(#Lambda_{c}) [GeV/c]",
+                                        1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
 
-  Double_t invmassLc2Lpi = part->InvMassLctoLambdaPi();
-  Double_t invmassLambda = v0part->MassLambda();
-  Double_t invmassLambdaBar = v0part->MassAntiLambda();
+    nameMass="histpK0Svsp";
+    TH2F* momentumDistributionK0Svsp = new TH2F(nameMass.Data(),"#Lambda_{c}: p(K^{0}_{S}) vs p(p);  p_{p}; p_{K^{0}_{S}}  ",
+                                               175,0.,35.,175,0.,35.);
 
-  Int_t isLcByMC = isLc+isLc2LBarpi*2+isLc2Lpi*4;
-  Int_t isV0ByMC = isK0S+isLambdaBar*2+isLambda*4+isGamma*8;
+    nameMass="histArmPodK0S";
+    TH2F* armenterosPodK0S = new TH2F(nameMass.Data(),"K^{0}_{S}: Armenteros-Podolanski distribution; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                     200,-1.,1.,300,0.,0.3);
+    nameMass="histDCAtoPVvspK0S";
+    TH2F *dcatoPVvspK0S = new TH2F(nameMass.Data(),"K^{0}_{S}: DCA to Primary Vertex vs K^{0}_{S} momentum ; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",
+                                  175,0.,35.,50,0.,5.);
 
-  Double_t nSigmaITSpr=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,4,nSigmaITSpr);
-  Double_t nSigmaTPCpr=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,4,nSigmaTPCpr);
-  Double_t nSigmaTOFpr=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,4,nSigmaTOFpr);
+    nameMass="histK0ScosPAwrtPVvspK0S";
+    TH2F *cosPAwrtPVvspK0S = new TH2F(nameMass.Data(),"K^{0}_{S}: cosine of pointing angle wrt primary vertex vs K^{0}_{S} momentum ; p(K^{0}_{S}) [GeV/c]; cosine; Entries",
+                                     175,0.,35.,100,0.99,1.);
 
-  Double_t nSigmaITSpi=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,2,nSigmaITSpi);
-  Double_t nSigmaTPCpi=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,2,nSigmaTPCpi);
-  Double_t nSigmaTOFpi=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,2,nSigmaTOFpi);
+    TH2F* allspectrumK0SMass = (TH2F*)spectrumK0SMass->Clone(); 
+    TH2F* allspectrumLcMassByK0S    = (TH2F*)spectrumLcMassByK0S->Clone(); 
+    TH2F* allmomentumDistributionK0Svsp= (TH2F*)momentumDistributionK0Svsp->Clone(); 
+    TH2F* alldcatoPVvspK0S=(TH2F*)dcatoPVvspK0S->Clone(); 
+    TH2F* allcosV0PAwrtPVvspK0S=(TH2F*)cosPAwrtPVvspK0S->Clone(); 
 
-  Double_t nSigmaITSka=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,3,nSigmaITSka);
-  Double_t nSigmaTPCka=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,3,nSigmaTPCka);
-  Double_t nSigmaTOFka=-999.;
-  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,3,nSigmaTOFka);
+    TH2F* pidBachspectrumK0SMass = (TH2F*)spectrumK0SMass->Clone(); 
+    TH2F* pidBachspectrumLcMassByK0S    = (TH2F*)spectrumLcMassByK0S->Clone(); 
+    TH2F* pidBachmomentumDistributionK0Svsp= (TH2F*)momentumDistributionK0Svsp->Clone(); 
+    TH2F* pidBachdcatoPVvspK0S=(TH2F*)dcatoPVvspK0S->Clone(); 
+    TH2F* pidBachcosV0PAwrtPVvspK0S=(TH2F*)cosPAwrtPVvspK0S->Clone(); 
 
+    TH2F* allArmenterosPodK0S = (TH2F*)armenterosPodK0S->Clone();
+    TH2F* pidBachArmenterosPodK0S = (TH2F*)armenterosPodK0S->Clone();
 
-  // Fill candidate variable Tree (track selection, V0 invMass selection)
-  if ( fWriteVariableTree && !onFlyV0 && isInV0windowProd && isInCascadeWindow && part->CosV0PointingAngle()>0.99) {
-
-    fCandidateVariables[0] = fUseMCInfo+isLcByMC; // 0: real data; 1: bkg; 2: Lc->K0S+p; 3: Lc->LambdaBar+pbar; 5: Lc->Lambda+p
-    fCandidateVariables[1] = fUseMCInfo+isV0ByMC; // 0: real data; 1: bkg; 2: K0S->pi+pi; 3: LambdaBar->pbar+pi+; 5: Lambda->p+pi-
-    fCandidateVariables[2] = invmassLc;
-    fCandidateVariables[3] = invmassLc2Lpi;
-    fCandidateVariables[4] = invmassK0s;
-    fCandidateVariables[5] = invmassLambda;
-    fCandidateVariables[6] = invmassLambdaBar;
-    fCandidateVariables[7] = part->CosV0PointingAngle();
-    fCandidateVariables[8] = dcaV0ptp;
-    fCandidateVariables[9] = part->Getd0Prong(0);
-    fCandidateVariables[10] = part->Getd0Prong(1);
-    fCandidateVariables[11] = nSigmaTPCpr;
-    fCandidateVariables[12] = nSigmaTPCpi;
-    fCandidateVariables[13] = nSigmaTPCka;
-    fCandidateVariables[14] = nSigmaTOFpr;
-    fCandidateVariables[15] = nSigmaTOFpi;
-    fCandidateVariables[16] = nSigmaTOFka;
-    fCandidateVariables[17] = bachelor->Px();
-    fCandidateVariables[18] = bachelor->Py();
-    fCandidateVariables[19] = bachelor->Pz();
-    AliAODTrack *v0neg = (AliAODTrack*)part->Getv0NegativeTrack(); 
-    fCandidateVariables[20] = v0neg->Px();
-    fCandidateVariables[21] = v0neg->Py();
-    fCandidateVariables[22] = v0neg->Pz();
-    AliAODTrack *v0pos = (AliAODTrack*)part->Getv0PositiveTrack();
-    fCandidateVariables[23] = v0pos->Px();
-    fCandidateVariables[24] = v0pos->Py();
-    fCandidateVariables[25] = v0pos->Pz();
-    fCandidateVariables[26] = part->QtProng(0);
-    fCandidateVariables[27] = part->Alpha();
-    fCandidateVariables[28] = v0part->Getd0Prong(0);
-    fCandidateVariables[29] = v0part->Getd0Prong(1);
-    fCandidateVariables[30] = part->PxProng(0);
-    fCandidateVariables[31] = part->PyProng(0);
-    fCandidateVariables[32] = part->PzProng(0);
-    fCandidateVariables[33] = part->PxProng(1);
-    fCandidateVariables[34] = part->PyProng(1);
-    fCandidateVariables[35] = part->PzProng(1);
-    fCandidateVariables[36] = v0part->PxProng(0);
-    fCandidateVariables[37] = v0part->PyProng(0);
-    fCandidateVariables[38] = v0part->PzProng(0);
-    fCandidateVariables[39] = v0part->PxProng(1);
-    fCandidateVariables[40] = v0part->PyProng(1);
-    fCandidateVariables[41] = v0part->PzProng(1);
-    fCandidateVariables[42] = flagToCheckCandidate;
-    fCandidateVariables[43] = v0part->InvMass2Prongs(0,1,11,11);
-
-    fCandidateVariables[44] = bachelor->P();
-    fCandidateVariables[45] = bachelor->Pt();
-    fCandidateVariables[46] = v0pos->P();
-    fCandidateVariables[47] = v0pos->Pt();
-    fCandidateVariables[48] = v0neg->P();
-    fCandidateVariables[49] = v0neg->Pt();
-    fCandidateVariables[50] = part->PProng(0);
-    fCandidateVariables[51] = part->PtProng(0);
-    fCandidateVariables[52] = part->PProng(1);
-    fCandidateVariables[53] = part->PtProng(1);
-    fCandidateVariables[54] = v0part->PProng(0);
-    fCandidateVariables[55] = v0part->PtProng(0);
-    fCandidateVariables[56] = v0part->PProng(1);
-    fCandidateVariables[57] = v0part->PtProng(1);
-    fCandidateVariables[58] = part->P();
-    fCandidateVariables[59] = part->Pt();
-    fCandidateVariables[60] = v0part->P();
-    fCandidateVariables[61] = v0part->Pt();
-
-    fCandidateVariables[62] = part->CosPointingAngle();
-    fCandidateVariables[63] = part->DecayLength();
-    fCandidateVariables[64] = part->DecayLengthV0();
-
-    fCandidateVariables[65] = part->Y(4122);
-
-    fCandidateVariables[66] = part->InvMass2Prongs(0,1,211,310); // D+ -> pi+ K0S
-    fCandidateVariables[67] = part->InvMass2Prongs(0,1,321,310); // D+S -> K+ K0S
-
-    fCandidateVariables[68] = nSigmaITSpr;
-    fCandidateVariables[69] = nSigmaITSpi;
-    fCandidateVariables[70] = nSigmaITSka;
-
-    fCandidateVariables[71] = part->GetDCA();
-
-    fCandidateVariables[72] = part->CosV0PointingAngleXY();
-    fCandidateVariables[73] = part->CosPointingAngleXY();
-
-    fCandidateVariables[74] = part->DecayLengthXYV0();
-    fCandidateVariables[75] = part->DecayLengthXY();
-
-    fCandidateVariables[76] = part->NormalizedV0DecayLength();
-    fCandidateVariables[77] = part->NormalizedDecayLength();
-
-    fCandidateVariables[78] = part->NormalizedV0DecayLengthXY();
-    fCandidateVariables[79] = part->NormalizedDecayLengthXY();
-
-    Double_t v0Momentum = (v0part->PxProng(0)+v0part->PxProng(1))*(v0part->PxProng(0)+v0part->PxProng(1));
-    v0Momentum += (v0part->PyProng(0)+v0part->PyProng(1))*(v0part->PyProng(0)+v0part->PyProng(1));
-    v0Momentum += (v0part->PzProng(0)+v0part->PzProng(1))*(v0part->PzProng(0)+v0part->PzProng(1));
-    v0Momentum = TMath::Sqrt(v0Momentum);
-
-    Double_t lcMomentum = (part->PxProng(0)+part->PxProng(1))*(part->PxProng(0)+part->PxProng(1));
-    lcMomentum += (part->PyProng(0)+part->PyProng(1))*(part->PyProng(0)+part->PyProng(1));
-    lcMomentum += (part->PzProng(0)+part->PzProng(1))*(part->PzProng(0)+part->PzProng(1));
-    lcMomentum = TMath::Sqrt(lcMomentum);
-
-    fVariablesTree->Fill();
+    fOutputAll->Add(allspectrumK0SMass);
+    fOutputAll->Add(allspectrumLcMassByK0S);
+    fOutputAll->Add(allmomentumDistributionK0Svsp); 
+    fOutputAll->Add(allArmenterosPodK0S);
+    fOutputAll->Add(alldcatoPVvspK0S);
+    fOutputAll->Add(allcosV0PAwrtPVvspK0S);
+
+    fOutputPIDBach->Add(pidBachspectrumK0SMass);
+    fOutputPIDBach->Add(pidBachspectrumLcMassByK0S);
+    fOutputPIDBach->Add(pidBachmomentumDistributionK0Svsp); 
+    fOutputPIDBach->Add(pidBachArmenterosPodK0S);
+    fOutputPIDBach->Add(pidBachdcatoPVvspK0S);
+    fOutputPIDBach->Add(pidBachcosV0PAwrtPVvspK0S);
   }
 
+  // V0 invariant masses (offline)
+  nameMass="histK0SMassOffline";
+  TH2F* spectrumK0SMassOffline = new TH2F(nameMass.Data(),"K^{0}_{S} invariant mass VS p_{T}; M(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                        1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
 
+  // Lc invariant masses (x K0S offline)
+  nameMass="histLcMassByK0SOffline";
+  TH2F* spectrumLcMassOfflineByK0S = new TH2F(nameMass.Data(),"#Lambda_{c} invariant mass (by K^{0}_{S}) vs p_{T}; M(K^{0}_{S}p) [GeV/c^{2}]; p_{T} [GeV/c]",
+                                             1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
 
-  if (onFlyV0 && fUseOnTheFlyV0) {  
+  nameMass="histpK0SvspOffline";
+  TH2F* momentumDistributionK0SvspOffline = new TH2F(nameMass.Data(),"#Lambda_{c}: p(K^{0}_{S}) vs p(p) - Offline ;  p_{p} [GeV/c]; p_{K^{0}_{S}} [GeV/c]",
+                                                    175,0.,35.,175,0.,35.);
 
-    fillthis="histK0SMass";
-    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-    if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
+  nameMass="histArmPodK0SOffline";
+  TH2F* armenterosPodK0SOff = new TH2F(nameMass.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution - Offline; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                      200,-1.,1.,300,0.,0.3);
 
-    if (isCandidateSelectedCuts) {
+  nameMass="histDCAtoPVvspK0SOffline";
+  TH2F *dcatoPVvspK0SOffline = new TH2F(nameMass.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass - Offline; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",
+                                       175,0.,35.,50,0.,5.);
 
-      fillthis="histpK0Svsp";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-      if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
+  nameMass="histK0ScosPAwrtPVvspK0SOffline";
+  TH2F *cosPAwrtPVvspK0SOffline = new TH2F(nameMass.Data(),"K^{0}_{S}: cosine of pointing angle wrt primary vertex vs K^{0}_{S} momentum - Offline; p(K^{0}_{S}) [GeV/c]; cosine; Entries",
+                                          175,0.,35.,100,0.99,1.);
 
-      fillthis="histDCAtoPVvspK0S";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-      if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
 
-      fillthis="histArmPodK0s";
-      FillArmPodDistribution(v0part,fillthis,fOutputAll);
-      if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
 
-      fillthis="histLcMassByK0S";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-      if (isBachelorID)((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-    }
-  }
-  else if (!onFlyV0) {
+  TH2F* allspectrumK0SMassOffline = (TH2F*)spectrumK0SMassOffline->Clone(); 
+  TH2F* allspectrumLcMassOfflineByK0S    = (TH2F*)spectrumLcMassOfflineByK0S->Clone(); 
+  TH2F* allmomentumDistributionK0SvspOffline= (TH2F*)momentumDistributionK0SvspOffline->Clone(); 
+  TH2F* alldcatoPVvspK0SOffline=(TH2F*)dcatoPVvspK0SOffline->Clone(); 
+  TH2F* allcosPAwrtPVvspK0SOffline=(TH2F*)cosPAwrtPVvspK0SOffline->Clone(); 
+  TH2F* allArmenterosPodK0SOff = (TH2F*)armenterosPodK0SOff->Clone();
 
-    fillthis="histK0SMassOffline";
-    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-    if (isBachelorID) ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
+  TH2F* pidBachspectrumK0SMassOffline = (TH2F*)spectrumK0SMassOffline->Clone(); 
+  TH2F* pidBachspectrumLcMassOfflineByK0S    = (TH2F*)spectrumLcMassOfflineByK0S->Clone(); 
+  TH2F* pidBachmomentumDistributionK0SvspOffline= (TH2F*)momentumDistributionK0SvspOffline->Clone(); 
+  TH2F* pidBachdcatoPVvspK0SOffline=(TH2F*)dcatoPVvspK0SOffline->Clone(); 
+  TH2F* pidBachcosPAwrtPVvspK0SOffline=(TH2F*)cosPAwrtPVvspK0SOffline->Clone(); 
+  TH2F* pidBachArmenterosPodK0SOff = (TH2F*)armenterosPodK0SOff->Clone();
 
-    if (isCandidateSelectedCuts) {
 
-      fillthis="histoprotonBachSigmaVspTOF";
-      ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
-      fillthis="histoprotonBachSigmaVspTPC";
-      ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
+  fOutputAll->Add(allspectrumK0SMassOffline);
+  fOutputAll->Add(allspectrumLcMassOfflineByK0S);
+  fOutputAll->Add(allmomentumDistributionK0SvspOffline); 
+  fOutputAll->Add(allArmenterosPodK0SOff);
+  fOutputAll->Add(alldcatoPVvspK0SOffline);
+  fOutputAll->Add(allcosPAwrtPVvspK0SOffline);
 
+  fOutputPIDBach->Add(pidBachspectrumK0SMassOffline);
+  fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0S);
+  fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOffline); 
+  fOutputPIDBach->Add(pidBachArmenterosPodK0SOff);
+  fOutputPIDBach->Add(pidBachdcatoPVvspK0SOffline);
+  fOutputPIDBach->Add(pidBachcosPAwrtPVvspK0SOffline);
 
-      fillthis="histpK0SvspOffline";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-      if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
+  if (fUseMCInfo) {
 
-      fillthis="histDCAtoPVvspK0SOffline";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-      if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
+    if (fUseOnTheFlyV0) {
 
-      fillthis="histOfflineArmPodK0s";
-      FillArmPodDistribution(v0part,fillthis,fOutputAll);
-      if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
+      nameSgn="histK0SMassSgn";
+      nameBkg="histK0SMassBkg";
+      TH2F* spectrumK0SMassSgn = new TH2F(nameSgn.Data(), "K^{0}_{S} - sgn: invariant mass VS p_{T} - MC; m_{inv}(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                         1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
+      TH2F* spectrumK0SMassBkg = new TH2F(nameBkg.Data(), "K^{0}_{S} - bkg: invariant mass VS p_{T} - MC; m_{inv}(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                         1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
 
-      fillthis="histLcMassOfflineByK0S";
-      ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-      if (isBachelorID)((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt); // main histogram
+      TH2F* allspectrumK0SMassSgn = (TH2F*)spectrumK0SMassSgn->Clone(); 
+      TH2F* allspectrumK0SMassBkg = (TH2F*) spectrumK0SMassBkg->Clone();  
+      TH2F* pidBachspectrumK0SMassSgn = (TH2F*)spectrumK0SMassSgn->Clone(); 
+      TH2F* pidBachspectrumK0SMassBkg = (TH2F*) spectrumK0SMassBkg->Clone();  
+      fOutputAll->Add(allspectrumK0SMassSgn);
+      fOutputAll->Add(allspectrumK0SMassBkg);
+      fOutputPIDBach->Add(pidBachspectrumK0SMassSgn);
+      fOutputPIDBach->Add(pidBachspectrumK0SMassBkg);
 
-    }
-  }
 
+      nameSgn="histLcMassByK0SSgn";
+      nameBkg="histLcMassByK0SBkg";
+      TH2F* spectrumLcMassByK0SSgn = new TH2F(nameSgn.Data(), "#Lambda_{c} - sgn: invariant mass (by K^{0}_{S}) vs p_{T}  - MC; m_{inv}(p-K^{0}_{S}) [GeV/c^{2}];  p_{T}",
+                                             1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
+      TH2F* spectrumLcMassByK0SBkg = new TH2F(nameBkg.Data(), "#Lambda_{c} - bkg: invariant mass (by K^{0}_{S}) vs p_{T}  - MC; m_{inv}(p-K^{0}_{S}) [GeV/c^{2}]; p_{T}",
+                                             1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
 
-  if (fUseMCInfo) {
-    if (isLc==1) {
-      if (onFlyV0 && fUseOnTheFlyV0) {
+      TH2F* allspectrumLcMassByK0SSgn = (TH2F*)spectrumLcMassByK0SSgn->Clone(); 
+      TH2F* allspectrumLcMassByK0SBkg = (TH2F*) spectrumLcMassByK0SBkg->Clone();  
+      TH2F* pidBachspectrumLcMassByK0SSgn = (TH2F*)spectrumLcMassByK0SSgn->Clone(); 
+      TH2F* pidBachspectrumLcMassByK0SBkg = (TH2F*) spectrumLcMassByK0SBkg->Clone();  
+      fOutputAll->Add(allspectrumLcMassByK0SSgn);
+      fOutputAll->Add(allspectrumLcMassByK0SBkg);
+      fOutputPIDBach->Add(pidBachspectrumLcMassByK0SSgn);
+      fOutputPIDBach->Add(pidBachspectrumLcMassByK0SBkg);
 
-       fillthis="histK0SMassSgn";
-       ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-       if (isBachelorID) ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
+      nameSgn="histpK0SvspSgn";
+      nameBkg="histpK0SvspBkg";
+      TH2F* momentumDistributionK0SvspSgn= new TH2F(nameSgn.Data(),"#Lambda_{c} - sgn: K^{0}_{S} vs p Total Momentum Distribution - MC; p_{p}; p_{K^{0}_{S}}",
+                                                   175,0.,35.,175,0.,35.);
+      TH2F* momentumDistributionK0SvspBkg= new TH2F(nameBkg.Data(),"#Lambda_{c} - bkg: K^{0}_{S} vs p Total Momentum Distribution - MC; p_{p}; p_{K^{0}_{S}}",
+                                                   175,0.,35.,175,0.,35.);
 
-       if (isCandidateSelectedCuts) {
+      TH2F* allmomentumDistributionK0SvspSgn= (TH2F*)momentumDistributionK0SvspSgn->Clone(); 
+      TH2F* allmomentumDistributionK0SvspBkg= (TH2F*)momentumDistributionK0SvspBkg->Clone(); 
+      TH2F* pidBachmomentumDistributionK0SvspSgn= (TH2F*)momentumDistributionK0SvspSgn->Clone(); 
+      TH2F* pidBachmomentumDistributionK0SvspBkg= (TH2F*)momentumDistributionK0SvspBkg->Clone(); 
+      fOutputAll->Add(allmomentumDistributionK0SvspSgn); 
+      fOutputAll->Add(allmomentumDistributionK0SvspBkg); 
+      fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspSgn); 
+      fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspBkg); 
 
-         fillthis="histpK0SvspSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
 
-         fillthis="histDCAtoPVvspK0SSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
+      // armenteros-podolanski plots K0S
+      nameSgn="histArmPodK0SSgn";
+      nameBkg="histArmPodK0SBkg";
+      TH2F* armenterosPodK0SSgn = new TH2F(nameSgn.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (sgn); #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                          200,-1.,1.,300,0.,0.3);
+      TH2F* armenterosPodK0SBkg = new TH2F(nameBkg.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (bkg); #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                          200,-1.,1.,300,0.,0.3);
 
-         fillthis="histArmPodK0sSgn";
-         FillArmPodDistribution(v0part,fillthis,fOutputAll);
-         if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
+      TH2F* allArmenterosPodK0SSgn = (TH2F*)armenterosPodK0SSgn->Clone();
+      TH2F* allArmenterosPodK0SBkg = (TH2F*)armenterosPodK0SBkg->Clone();
+      TH2F* pidBachArmenterosPodK0SSgn = (TH2F*)armenterosPodK0SSgn->Clone();
+      TH2F* pidBachArmenterosPodK0SBkg = (TH2F*)armenterosPodK0SBkg->Clone();
 
-         fillthis="histLcMassByK0SSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
+      fOutputAll->Add(allArmenterosPodK0SSgn);
+      fOutputAll->Add(allArmenterosPodK0SBkg);
+      fOutputPIDBach->Add(pidBachArmenterosPodK0SSgn);
+      fOutputPIDBach->Add(pidBachArmenterosPodK0SBkg);
 
-       }
-    
-      }     
-      else if (!onFlyV0) {  
 
-       fillthis="histK0SMassOfflineSgn";
-       ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-       if (isBachelorID) ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
+      nameSgn="histDCAtoPVvspK0SSgn";
+      nameBkg="histDCAtoPVvspK0SBkg";
+      TH2F *dcatoPVvspK0SSgn=new TH2F(nameSgn.Data(),"K^{0}_{S} - sgn: DCA to Primary Vertex vs  K^{0}_{S} invariant mass (sgn); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",175,0.,35.,50,0.,5.);
+      TH2F *dcatoPVvspK0SBkg=new TH2F(nameBkg.Data(),"K^{0}_{S} - bkg: DCA to Primary Vertex vs  K^{0}_{S} invariant mass (bkg); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",175,0.,35.,50,0.,5.);
 
-       if (isCandidateSelectedCuts) {
+      TH2F* alldcatoPVvspK0SSgn= (TH2F*)dcatoPVvspK0SSgn->Clone();
+      TH2F* alldcatoPVvspK0SBkg= (TH2F*)dcatoPVvspK0SBkg->Clone();
+      TH2F* pidBachdcatoPVvspK0SSgn= (TH2F*)dcatoPVvspK0SSgn->Clone();
+      TH2F* pidBachdcatoPVvspK0SBkg= (TH2F*)dcatoPVvspK0SBkg->Clone();
 
-         fillthis="histoprotonBachSigmaVspTOFsgn";
-         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
-         fillthis="histoprotonBachSigmaVspTPCsgn";
-         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
+      fOutputAll->Add(alldcatoPVvspK0SSgn);
+      fOutputPIDBach->Add(pidBachdcatoPVvspK0SSgn);
+      fOutputAll->Add(alldcatoPVvspK0SBkg);
+      fOutputPIDBach->Add(pidBachdcatoPVvspK0SBkg);
 
+    }
 
-         fillthis="histpK0SvspOfflineSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
-  
-         fillthis="histDCAtoPVvspK0SOfflineSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-   
-         fillthis="histOfflineArmPodK0sSgn";
-         FillArmPodDistribution(v0part,fillthis,fOutputAll);
-         if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
-
-         fillthis="histLcMassOfflineByK0SSgn";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-         
-       }
 
-      }
+    nameSgn="histK0SMassOfflineSgn";
+    nameBkg="histK0SMassOfflineBkg";
+    TH2F* spectrumK0SMassOfflineSgn = new TH2F(nameSgn.Data(), "K^{0}_{S} - sgn: invariant mass VS p_{T} - MC; m_{inv}(#pi^{+}-#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                             1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
+    TH2F* spectrumK0SMassOfflineBkg = new TH2F(nameBkg.Data(), "K^{0}_{S} - bkg: invariant mass VS p_{T} - MC; m_{inv}(#pi^{+}-#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
+                                             1000,mK0SPDG-0.050,mK0SPDG+0.050,175,0.,35.);
 
-    }// sgn
-    else { // bkg
-      if (onFlyV0 && fUseOnTheFlyV0) {
+    TH2F* allspectrumK0SMassOfflineSgn = (TH2F*)spectrumK0SMassOfflineSgn->Clone(); 
+    TH2F* allspectrumK0SMassOfflineBkg = (TH2F*) spectrumK0SMassOfflineBkg->Clone();  
+    fOutputAll->Add(allspectrumK0SMassOfflineSgn);
+    fOutputAll->Add(allspectrumK0SMassOfflineBkg);
 
-       fillthis="histK0SMassBkg";
-       ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-       if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
 
-       if (isCandidateSelectedCuts) {
+    TH2F* pidBachspectrumK0SMassOfflineSgn = (TH2F*)spectrumK0SMassOfflineSgn->Clone(); 
+    TH2F* pidBachspectrumK0SMassOfflineBkg = (TH2F*) spectrumK0SMassOfflineBkg->Clone();
+    fOutputPIDBach->Add(pidBachspectrumK0SMassOfflineSgn);
+    fOutputPIDBach->Add(pidBachspectrumK0SMassOfflineBkg);
 
-         fillthis="histpK0SvspBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
 
-         fillthis="histDCAtoPVvspK0SBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
+    nameSgn="histLcMassByK0SOfflineSgn";
+    nameBkg="histLcMassByK0SOfflineBkg";
+    TH2F* spectrumLcMassOfflineByK0SSgn = new TH2F(nameSgn.Data(), "#Lambda_{c} - sgn: invariant mass (by K^{0}_{S})  vs p_{T} - MC; M(#Lambda_{c}) [GeV/c^{2}]; p_{T}",
+                                                  1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
+    TH2F* spectrumLcMassOfflineByK0SBkg = new TH2F(nameBkg.Data(), "#Lambda_{c} - bkg: invariant mass (by K^{0}_{S})  vs p_{T} - MC; M(#Lambda_{c}) [GeV/c^{2}]; p_{T}",
+                                                  1000,mLcPDG-0.250,mLcPDG+0.250,175,0.,35.);
 
-         fillthis="histArmPodK0sBkg";
-         FillArmPodDistribution(v0part,fillthis,fOutputAll);
-         if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
 
-         fillthis="histLcMassByK0SBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
+    TH2F* allspectrumLcMassOfflineByK0SSgn = (TH2F*)spectrumLcMassOfflineByK0SSgn->Clone(); 
+    TH2F* allspectrumLcMassOfflineByK0SBkg = (TH2F*) spectrumLcMassOfflineByK0SBkg->Clone();  
+    TH2F* pidBachspectrumLcMassOfflineByK0SSgn = (TH2F*)spectrumLcMassOfflineByK0SSgn->Clone(); 
+    TH2F* pidBachspectrumLcMassOfflineByK0SBkg = (TH2F*) spectrumLcMassOfflineByK0SBkg->Clone();  
+    fOutputAll->Add(allspectrumLcMassOfflineByK0SSgn);
+    fOutputAll->Add(allspectrumLcMassOfflineByK0SBkg);
+    fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0SSgn);
+    fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0SBkg);
+  
  
-       }
+    nameSgn="histpK0SvspOfflineSgn";
+    nameBkg="histpK0SvspOfflineBkg";
+    TH2F* momentumDistributionK0SvspOfflineSgn= new TH2F(nameSgn.Data(),"#Lambda_{c} - sgn: K^{0}_{S} vs p Total Momentum Distribution - Offline  - MC; p_{p}; p_{K^{0}_{S}}",
+                                                        175,0.,35.,175,0.,35.);
+    TH2F* momentumDistributionK0SvspOfflineBkg= new TH2F(nameBkg.Data(),"#Lambda_{c} - bkg: K^{0}_{S} vs p Total Momentum Distribution - Offline  - MC; p_{p}; p_{K^{0}_{S}}",
+                                                        175,0.,35.,175,0.,35.);
 
-      }
-      else if (!onFlyV0) {
 
-       fillthis="histK0SMassOfflineBkg";
-       ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
-       if (isBachelorID) ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0s,ptK0s);
+    TH2F* allmomentumDistributionK0SvspOfflineSgn= (TH2F*)momentumDistributionK0SvspOfflineSgn->Clone(); 
+    TH2F* allmomentumDistributionK0SvspOfflineBkg= (TH2F*)momentumDistributionK0SvspOfflineBkg->Clone(); 
+    TH2F* pidBachmomentumDistributionK0SvspOfflineSgn= (TH2F*)momentumDistributionK0SvspOfflineSgn->Clone(); 
+    TH2F* pidBachmomentumDistributionK0SvspOfflineBkg= (TH2F*)momentumDistributionK0SvspOfflineBkg->Clone(); 
+    fOutputAll->Add(allmomentumDistributionK0SvspOfflineSgn); 
+    fOutputAll->Add(allmomentumDistributionK0SvspOfflineBkg); 
+    fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOfflineSgn); 
+    fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOfflineBkg); 
+
+
+
+
+
+    // armenteros-podolanski plots K0S (offline)
+    nameSgn="histArmPodK0SOfflineSgn";
+    nameBkg="histArmPodK0SOfflineBkg";
+    TH2F* armenterosPodK0SOffSgn = new TH2F(nameSgn.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (sgn) -offline-; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                           200,-1.,1.,300,0.,0.3);
+    TH2F* armenterosPodK0SOffBkg = new TH2F(nameBkg.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (bkg) -offline-; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
+                                           200,-1.,1.,300,0.,0.3);
   
-       if (isCandidateSelectedCuts) {
 
-         fillthis="histoprotonBachSigmaVspTOFbkg";
-         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTOFpr);
-         fillthis="histoprotonBachSigmaVspTPCbkg";
-         ((TH2F*)(fOutput->FindObject(fillthis)))->Fill(momBach,nSigmaTPCpr);
+    TH2F* allArmenterosPodK0SOffSgn = (TH2F*)armenterosPodK0SOffSgn->Clone();
+    TH2F* allArmenterosPodK0SOffBkg = (TH2F*)armenterosPodK0SOffBkg->Clone();
+    TH2F* pidBachArmenterosPodK0SOffSgn = (TH2F*)armenterosPodK0SOffSgn->Clone();
+    TH2F* pidBachArmenterosPodK0SOffBkg = (TH2F*)armenterosPodK0SOffBkg->Clone();
 
 
-         fillthis="histpK0SvspOfflineBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0s);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0s);
-   
-         fillthis="histDCAtoPVvspK0SOfflineBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0s,dcaV0ptp);
+    fOutputAll->Add(allArmenterosPodK0SOffSgn);
+    fOutputAll->Add(allArmenterosPodK0SOffBkg);
+    fOutputPIDBach->Add(pidBachArmenterosPodK0SOffSgn);
+    fOutputPIDBach->Add(pidBachArmenterosPodK0SOffBkg);
 
-         fillthis="histOfflineArmPodK0sBkg";
-         FillArmPodDistribution(v0part,fillthis,fOutputAll);
-         if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
 
-         fillthis="histLcMassOfflineByK0SBkg";
-         ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-         if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
-       }
+    nameSgn="histDCAtoPVvspK0SOfflineSgn";
+    nameBkg="histDCAtoPVvspK0SOfflineBkg";
+    TH2F *dcatoPVvspK0SOfflineSgn=new TH2F(nameSgn.Data(),"K^{0}_{S} -offline - (sgn): DCA to Primary Vertex vs  K^{0}_{S} invariant mass; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",
+                                          175,0.,35.,50,0.,5.);
+    TH2F *dcatoPVvspK0SOfflineBkg=new TH2F(nameBkg.Data(),"K^{0}_{S} -offline - (bkg): DCA to Primary Vertex vs  K^{0}_{S} invariant mass; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex [n#sigmas]; Entries",
+                                          175,0.,35.,50,0.,5.);
+    
 
-      }
+    TH2F* alldcatoPVvspK0SOfflineSgn= (TH2F*)dcatoPVvspK0SOfflineSgn->Clone();
+    TH2F* pidBachdcatoPVvspK0SOfflineSgn= (TH2F*)dcatoPVvspK0SOfflineSgn->Clone();
+    TH2F* alldcatoPVvspK0SOfflineBkg= (TH2F*)dcatoPVvspK0SOfflineBkg->Clone();
+    TH2F* pidBachdcatoPVvspK0SOfflineBkg= (TH2F*)dcatoPVvspK0SOfflineBkg->Clone();
+
+
+
+    fOutputAll->Add(alldcatoPVvspK0SOfflineSgn);
+    fOutputPIDBach->Add(pidBachdcatoPVvspK0SOfflineSgn);
+    fOutputAll->Add(alldcatoPVvspK0SOfflineBkg);
+    fOutputPIDBach->Add(pidBachdcatoPVvspK0SOfflineBkg);
+
+  }
 
-    }
-  } // if fUseMCInfo
   return;
 }
-//-------------------------------------------------------------------------------
-void AliAnalysisTaskSELc2V0bachelor::MakeAnalysisForLc2prK0S(TClonesArray *arrayLctopKos,
-                                                            TClonesArray *mcArray,
-                                                            Int_t &nSelectedProd,
-                                                            AliRDHFCutsLctoV0 *cutsProd,
-                                                            Int_t &nSelectedAnal,
-                                                            AliRDHFCutsLctoV0 *cutsAnal)
-{
 
-  // counters for efficiencies
-  Int_t icountReco = 0;
-
-  //Lc prong needed to MatchToMC method
+//---------------------------
+void AliAnalysisTaskSELc2V0bachelor::CheckEventSelection(AliAODEvent *aodEvent) {
+  //
+  // To fill control histograms
+  //
 
-  Int_t pdgCand = 4122;
-  Int_t pdgDgLctoV0bachelor[2]={2212,310};
-  Int_t pdgDgV0toDaughters[2]={211,211};
+  TClonesArray *arrayLctopKos=0;
+  if (!aodEvent && AODEvent() && IsStandardAOD()) {
+    // In case there is an AOD handler writing a standard AOD, use the AOD 
+    // event in memory rather than the input (ESD) event.    
+    aodEvent = dynamic_cast<AliAODEvent*> (AODEvent());
+    // in this case the braches in the deltaAOD (AliAOD.VertexingHF.root)
+    // have to taken from the AOD event hold by the AliAODExtension
+    AliAODHandler* aodHandler = (AliAODHandler*) 
+      ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
 
-  // loop over cascades to search for candidates Lc->p+K0S
-  Int_t nCascades= arrayLctopKos->GetEntriesFast();
-  if (nCascades==0) {
-    AliInfo("Could not find cascades, skipping the event");
-    return;
+    if (aodHandler->GetExtensions()) {
+      AliAODExtension *ext = (AliAODExtension*)aodHandler->GetExtensions()->FindObject("AliAOD.VertexingHF.root");
+      AliAODEvent *aodFromExt = ext->GetAOD();
+      arrayLctopKos=(TClonesArray*)aodFromExt->GetList()->FindObject("CascadesHF");
+    }
+  } else {
+    arrayLctopKos=(TClonesArray*)aodEvent->GetList()->FindObject("CascadesHF");
   }
-  for (Int_t iLctopK0s = 0; iLctopK0s<nCascades; iLctopK0s++) {
 
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(0);
+  Float_t zVertex = fVtx1->GetZ();
+  TString titleVtx=fVtx1->GetTitle();
 
-    // Lc candidates and K0s from Lc
-    AliAODRecoCascadeHF* lcK0spr = dynamic_cast<AliAODRecoCascadeHF*>(arrayLctopKos->At(iLctopK0s));
-    if (!lcK0spr) {
-      AliDebug(2,Form("Cascade %d doens't exist, skipping",iLctopK0s));
-      continue;
-    }
+  if (TMath::Abs(fBzkG)>=0.001) {
 
-    if (!lcK0spr->GetSecondaryVtx()) {
-      AliInfo("No secondary vertex");
-      continue;
-    }
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ2")))->Fill(zVertex);
 
-    if (lcK0spr->GetNDaughters()!=2) {
-      AliDebug(2,Form("Cascade %d has not 2 daughters (nDaughters=%d)",iLctopK0s,lcK0spr->GetNDaughters()));
-      continue;
-    }
+    if (arrayLctopKos) {
 
-    AliAODv0 * v0part = dynamic_cast<AliAODv0*>(lcK0spr->Getv0());
-    AliAODTrack * bachPart = dynamic_cast<AliAODTrack*>(lcK0spr->GetBachelor());
-    if (!v0part || !bachPart) {
-      AliDebug(2,Form("Cascade %d has no V0 or no bachelor object",iLctopK0s));
-      continue;
-    }
+      if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ3")))->Fill(zVertex);
 
+      // mc analysis 
+      TClonesArray *mcArray = 0;
+      AliAODMCHeader *mcHeader=0;
 
-    if (!v0part->GetSecondaryVtx()) {
-      AliDebug(2,Form("No secondary vertex for V0 by cascade %d",iLctopK0s));
-      continue;
-    }
+      if (fUseMCInfo) {
+       // MC array need for maching
+       mcArray = dynamic_cast<TClonesArray*>(aodEvent->FindListObject(AliAODMCParticle::StdBranchName()));
+       if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ4")))->Fill(zVertex);
 
-    if (v0part->GetNDaughters()!=2) {
-      AliDebug(2,Form("current V0 has not 2 daughters (onTheFly=%d, nDaughters=%d)",v0part->GetOnFlyStatus(),v0part->GetNDaughters()));
-      continue;
-    }
+       if (mcArray) {
+         // load MC header
+         mcHeader = (AliAODMCHeader*)aodEvent->GetList()->FindObject(AliAODMCHeader::StdBranchName());
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ5")))->Fill(zVertex);
 
-    AliAODTrack * v0Pos = dynamic_cast<AliAODTrack*>(lcK0spr->Getv0PositiveTrack());
-    AliAODTrack * v0Neg = dynamic_cast<AliAODTrack*>(lcK0spr->Getv0NegativeTrack());
-    if (!v0Neg || !v0Neg) {
-      AliDebug(2,Form("V0 by cascade %d has no V0positive of V0negative object",iLctopK0s));
-      continue;
-    }
+         // check on MC Lc Daughter
+         if (fAdditionalChecks) {
+           for (Int_t iii=0; iii<mcArray->GetEntries(); iii++)
+             SearchLcDaughter(mcArray,iii);
+         }
 
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(1);
+       }
 
-    if (v0Pos->Charge() == v0Neg->Charge()) continue;
+      }
 
-    ((TH1I*)(fOutput->FindObject("hCandidateSelection")))->Fill(2);
+      if (fVtx1->GetNContributors()>0) {
+       if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ6")))->Fill(zVertex);
+
+       TString firedTriggerClasses = aodEvent->GetFiredTriggerClasses(); // trigger class
+       ULong64_t fTriggerMask=AliVEvent::kAnyINT;
+       Bool_t check1 = kFALSE;
+       if ( !fUseMCInfo && // don't do for MC...
+            (aodEvent->GetRunNumber()<136851 || aodEvent->GetRunNumber()>139517) ) { // ...and for PbPb 2010 data
+         if ( !(firedTriggerClasses.Contains("CINT1")) ) {
+           AliInfo(Form(" ======================== firedTriggerClasses.Data() = %s",firedTriggerClasses.Data()));
+           fCEvents->Fill(8);
+           if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ8")))->Fill(zVertex);
+           check1 = kTRUE;
+         }
+       }
 
-    Int_t isLc = 0;
+       Bool_t isSelectedAAA = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
+       if (!isSelectedAAA) {
+         fCEvents->Fill(9);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ9")))->Fill(zVertex);
+       }
 
-    if (fUseMCInfo) {
+       if (!isSelectedAAA || check1) {
+         fCEvents->Fill(16);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ16")))->Fill(zVertex);
+       }
 
-      Int_t pdgCode=-2;
+       fTriggerMask=AliVEvent::kAny;
+       Bool_t isSelectedBBB = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
+       if (!isSelectedBBB) {
+         fCEvents->Fill(10);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ10")))->Fill(zVertex);
+       }
 
-      // find associated MC particle for Lc -> p+K0 and K0S->pi+pi
-      Int_t mcLabelOld = MatchToMC(lcK0spr,pdgDgLctoV0bachelor,pdgDgV0toDaughters,mcArray);
-      Int_t mcLabel = lcK0spr->MatchToMC(pdgCand,pdgDgLctoV0bachelor[1],pdgDgLctoV0bachelor,pdgDgV0toDaughters,mcArray,kTRUE);
-      if (mcLabelOld!=mcLabel) AliDebug(2,Form(" Changed MC label: oldONE=%d wrt rightONE=%d",mcLabelOld,mcLabel));
-      if (mcLabel>=0) {
-       AliDebug(2,Form(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cascade number %d (total cascade number = %d)", iLctopK0s,nCascades));
+       if (titleVtx.Contains("Z")) {
+         fCEvents->Fill(11);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ11")))->Fill(zVertex);
+       }
+       else if (titleVtx.Contains("3D")) {
+         fCEvents->Fill(12);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ12")))->Fill(zVertex);
+       } else {
+         fCEvents->Fill(13);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ13")))->Fill(zVertex);
+       }
 
-       AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel));
-       if(partLc){
-         pdgCode = partLc->GetPdgCode();
-         if (pdgCode<0) AliDebug(2,Form(" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ MClabel=%d ~~~~~~~~~~ pdgCode=%d", mcLabel, pdgCode));
-         pdgCode = TMath::Abs(pdgCode);
-         isLc = 1;
+       if (TMath::Abs(zVertex)<=fAnalCuts->GetMaxVtxZ()) {
+         fCEvents->Fill(14);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ14")))->Fill(zVertex);
        }
-      } else {
-       pdgCode=-1;
-      }
+
+       if ( fIsEventSelected ) {
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ7")))->Fill(zVertex);
+       } else {
+         fCEvents->Fill(15);
+         if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ15")))->Fill(zVertex);
+       }
+
+      } // nContributors>=1
+    } // analysisArray exists
+  } // magnetic field exists
+
+  return;
+}
+
+//-----------------
+void AliAnalysisTaskSELc2V0bachelor::CheckEventSelectionWithCandidates(AliAODEvent *aodEvent) {
+  //
+  // To fill control histograms
+  //
+
+  Float_t zVertex = fVtx1->GetZ();
+  TString titleVtx=fVtx1->GetTitle();
+  TString firedTriggerClasses = aodEvent->GetFiredTriggerClasses(); // trigger class
+  ULong64_t fTriggerMask=AliVEvent::kAnyINT;
+
+  ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(6);
+  if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ6a")))->Fill(zVertex);
+
+  Bool_t check1a = kFALSE;
+  if ( !fUseMCInfo && // don't do for MC...
+       (aodEvent->GetRunNumber()<136851 || aodEvent->GetRunNumber()>139517) ) { // ...and for PbPb 2010 data
+    if ( !(firedTriggerClasses.Contains("CINT1")) ) {
+      AliInfo(Form(" ======================== firedTriggerClasses.Data() = %s",firedTriggerClasses.Data()));
+      ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(8);
+      if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ8a")))->Fill(zVertex);
+      check1a = kTRUE;
     }
+  }
 
-    FillLc2pK0Sspectrum(lcK0spr, isLc,
-                       nSelectedProd, cutsProd,
-                       nSelectedAnal, cutsAnal,
-                       mcArray);
+  Bool_t isSelectedAAAa = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
+  if (!isSelectedAAAa) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(9);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ9a")))->Fill(zVertex);
+  }
 
+  if (!isSelectedAAAa || check1a) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(16);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ16a")))->Fill(zVertex);
   }
-  
-  AliDebug(2, Form("Found %d Reco particles that are Lc!!", icountReco));
 
+  fTriggerMask=AliVEvent::kAny;
+  Bool_t isSelectedBBBa = (((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected() & fTriggerMask);
+  if (!isSelectedBBBa) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(10);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ10a")))->Fill(zVertex);
+  }
+
+  if (titleVtx.Contains("Z")) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(11);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ11a")))->Fill(zVertex);
+  }
+  else if (titleVtx.Contains("3D")) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(12);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ12a")))->Fill(zVertex);
+  } else {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(13);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ13a")))->Fill(zVertex);
+  }
+
+  if (TMath::Abs(zVertex)<=fAnalCuts->GetMaxVtxZ()) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(14);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ14a")))->Fill(zVertex);
+  }
+
+  if ( fIsEventSelected ) {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(7);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ7a")))->Fill(zVertex);
+  } else {
+    ((TH1F*)(fOutput->FindObject("hEventsWithCandidates")))->Fill(15);
+    if (fAdditionalChecks) ((TH1F*)(fOutput->FindObject("hZ15a")))->Fill(zVertex);
+  }
+
+  return;
 }
-//-------------------------------------------------------------------------------
+
+//-----------------------------
 Int_t AliAnalysisTaskSELc2V0bachelor::MatchToMC(AliAODRecoCascadeHF *lc2bacV0,
-                                         Int_t *pdgDgLc2bacV0, Int_t *pdgDgV0,
-                                         TClonesArray *mcArray)
-{
+                                               Int_t *pdgDgLc2bacV0, Int_t *pdgDgV0,
+                                               TClonesArray *mcArray) {
+  //
+  // This is now implemented in AliAODRecoCascadeHF
+  //
 
   // bachelor
   AliAODTrack *bachelor = (AliAODTrack*)lc2bacV0->GetBachelor();
   if (!bachelor) return -1;
-  Int_t labBachelor = bachelor->GetLabel();
+  Int_t labBachelor = TMath::Abs(bachelor->GetLabel());
   if (labBachelor<0) return -1;
   AliAODMCParticle *partBachelor = (AliAODMCParticle*)mcArray->At(labBachelor);
   if (!partBachelor) return -1;
@@ -1499,8 +1201,8 @@ Int_t AliAnalysisTaskSELc2V0bachelor::MatchToMC(AliAODRecoCascadeHF *lc2bacV0,
   AliAODTrack *negV0Daugh = (AliAODTrack*)lc2bacV0->Getv0NegativeTrack();
   if (!posV0Daugh || !negV0Daugh) return -1;
 
-  Int_t labV0pos = posV0Daugh->GetLabel();
-  Int_t labV0neg = negV0Daugh->GetLabel();
+  Int_t labV0pos = TMath::Abs(posV0Daugh->GetLabel());
+  Int_t labV0neg = TMath::Abs(negV0Daugh->GetLabel());
   if (labV0pos<0 || labV0neg<0) return -1;
 
   AliAODMCParticle *partV0pos = (AliAODMCParticle*)mcArray->At(labV0neg);
@@ -1544,26 +1246,18 @@ Int_t AliAnalysisTaskSELc2V0bachelor::MatchToMC(AliAODRecoCascadeHF *lc2bacV0,
   }
 
   if (labBacMother!=labV0mother) {
-    //AliInfo(Form(" bachelor mother label=%d - V0 mother label=%d",labBacMother, labV0mother));
     return -1;
   }
 
-  //AliInfo(Form(" V0: %d (label=%d) ->  %d (label=%d) + %d (label=%d) --- GM %d (PDG=%d)",
-  //motherV0->GetPdgCode(), labV0posMother,
-  //partV0pos->GetPdgCode(), labV0pos,
-  //partV0neg->GetPdgCode(), labV0neg,
-  //labV0mother, gMotherV0->GetPdgCode()));
-
-  //AliInfo(Form(" Bachelor: %d (label=%d) --- M %d (label=%d)",
-  //partBachelor->GetPdgCode(), labBachelor,
-  //labBacMother, partBacMother->GetPdgCode()));
-  return labBacMother;//labV0mother;//
+  return labBacMother;
 
 }
 
 //________________________________________________________________
-Int_t AliAnalysisTaskSELc2V0bachelor::SearchLcDaughter(TClonesArray *arrayMC, Int_t iii)
-{
+Int_t AliAnalysisTaskSELc2V0bachelor::SearchLcDaughter(TClonesArray *arrayMC, Int_t iii) {
+  //
+  // This is to check Lc dinasty
+  //
 
   Int_t indexToBeReturned=-999;
 
@@ -1662,19 +1356,19 @@ Int_t AliAnalysisTaskSELc2V0bachelor::SearchLcDaughter(TClonesArray *arrayMC, In
       return -999;
     }
 
-    AliAODMCParticle *daughK0s1 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(index1));
-    AliAODMCParticle *daughK0s2 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(index2));
-    if (!daughK0s1 || !daughK0s2) return -999;
+    AliAODMCParticle *daughK0S1 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(index1));
+    AliAODMCParticle *daughK0S2 = dynamic_cast<AliAODMCParticle*>(arrayMC->At(index2));
+    if (!daughK0S1 || !daughK0S2) return -999;
 
-    Int_t daughK0s1pdg=TMath::Abs(daughK0s1->GetPdgCode());
-    Int_t daughK0s2pdg=TMath::Abs(daughK0s2->GetPdgCode());
+    Int_t daughK0S1pdg=TMath::Abs(daughK0S1->GetPdgCode());
+    Int_t daughK0S2pdg=TMath::Abs(daughK0S2->GetPdgCode());
 
-    if ( daughK0s1pdg==211 && daughK0s2pdg==211 ) {
+    if ( daughK0S1pdg==211 && daughK0S2pdg==211 ) {
       ((TH1F*)(fOutput->FindObject(fillthis)))->Fill(TMath::Nint(searchLc->Charge()/3.)*6); // K0S -> pi+ pi-
       indexToBeReturned = TMath::Nint(searchLc->Charge()/3.)*6;
     } else {
       ((TH1F*)(fOutput->FindObject(fillthis)))->Fill(TMath::Nint(searchLc->Charge()/3.)*5); // other decays for K0S
-    indexToBeReturned = TMath::Nint(searchLc->Charge()/3.)*5;
+      indexToBeReturned = TMath::Nint(searchLc->Charge()/3.)*5;
     }
 
   } //if (daughPdg1==pdgBachelor && daughPdg2==pdgK0)
@@ -1714,12 +1408,15 @@ Int_t AliAnalysisTaskSELc2V0bachelor::SearchLcDaughter(TClonesArray *arrayMC, In
   } //else if (daughPdg1==pdgBachelorPi && daughPdg2==pdgLambda)
 
   return indexToBeReturned;
-
 }
+
 //________________________________________________________________
 void AliAnalysisTaskSELc2V0bachelor::FillArmPodDistribution(AliAODv0 *vZero,
-                                                     TString histoTitle,
-                                                     TList *histoList) {
+                                                           TString histoTitle,
+                                                           TList *histoList) {
+  //
+  // This is to fill Armenteros Podolanski plots
+  //
 
   Double_t alpha = vZero->AlphaV0();
   Double_t qT    = vZero->PtArmV0();
@@ -1727,387 +1424,912 @@ void AliAnalysisTaskSELc2V0bachelor::FillArmPodDistribution(AliAODv0 *vZero,
   ((TH2F*)(histoList->FindObject(histoTitle)))->Fill(alpha,qT);
 
 }
-//----------------------------------------------------
 
-void AliAnalysisTaskSELc2V0bachelor::DefineK0SHistos()
-{ 
+//-------------------------------------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelor::CheckCandidatesAtDifferentLevels(AliAODRecoCascadeHF *part, AliRDHFCutsLctoV0* cutsAnal) {
+  //
+  // This is to check candidates at different levels
+  //
 
-  TString nameMass=" ", nameSgn=" ", nameBkg=" ";
+  Bool_t areCutsUsingPID = cutsAnal->GetIsUsePID();
 
-  ///---------------- START  K0S HISTOS DECLARATIONS -------------------///
+  AliAODv0 * v0part = (AliAODv0*)part->Getv0();
+  Bool_t onFlyV0 = v0part->GetOnFlyStatus(); // on-the-flight V0s
 
-  if (fUseOnTheFlyV0) {
+  AliAODTrack *bachelor = (AliAODTrack*)part->GetBachelor();
 
-    // V0 invariant masses (on-the-fly)
-    nameMass="histK0SMass";
-    TH2F* spectrumK0SMass = new TH2F(nameMass.Data(),"K^{0}_{S} invariant mass VS p_{T}; M(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
-                                    520,0.43,0.56,200,0.,20.);
+  if ( !onFlyV0 )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(3); // it counts number of candidates coming from offline V0s
 
-    // Lc invariant masses (x K0S on-the-fly)
-    nameMass="histLcMassByK0S";
-    TH2F* spectrumLcMassByK0S = new TH2F(nameMass.Data(),"#Lambda_{C} invariant mass (by K^{0}_{S}) vs p_{T} ; M(#Lambda_{C}) [GeV/c^{2}]; p_{T} [GeV/c]",
-                                        1200,2.,2.6,200,0.,20.);
+  if ( cutsAnal->IsInFiducialAcceptance(part->Pt(),part->Y(4122)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(4);
+  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(5);
+  cutsAnal->SetUsePID(kFALSE);
+  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(6);
+  cutsAnal->SetUsePID(areCutsUsingPID);
+  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kPID))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(7);
+  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(8);
+  if ( (((cutsAnal->IsSelected(part,AliRDHFCuts::kAll))&(AliRDHFCutsLctoV0::kLcToK0Spr))==(AliRDHFCutsLctoV0::kLcToK0Spr)) )
+    ((TH1F*)(fOutput->FindObject("hCandidateSelection")))->Fill(9);
 
-    //nameMass="histcosOAK0Spvsp";
-    //TH2F* cosOpeningAngleK0Spvsp = new TH2F(nameMass.Data(),"#Lambda_{C}: cosine of K^{0}_{S} - p opening angle vs #Lambda_{c} momentum; Cos(Opening Angle)  ; p [GeV/c]",
-    //200,-1.,1.,200,0.,20.);
+  if ( cutsAnal->IsInFiducialAcceptance(part->Pt(),part->Y(4122)) ) {
 
-    nameMass="histpK0Svsp";
-    TH2F* momentumDistributionK0Svsp = new TH2F(nameMass.Data(),"#Lambda_{C}: p(K^{0}_{S}) vs p(p);  p_{p}; p_{K^{0}_{S}}  ",
-                                               200,0.,20.,200,0.,20.);
+    if ( ( (cutsAnal->IsSelected(part,AliRDHFCuts::kTracks))&(AliRDHFCutsLctoV0::kLcToK0Spr)) == (AliRDHFCutsLctoV0::kLcToK0Spr) ) {
 
-    nameMass="histArmPodK0s";
-    TH2F* armenterosPodK0s = new TH2F(nameMass.Data(),"K^{0}_{S}: Armenteros-Podolanski distribution; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
-                                     200,-1.,1.,300,0.,0.3);
-    nameMass="histDCAtoPVvspK0S";
-    TH2F *dcatoPVvspK0s = new TH2F(nameMass.Data(),"K^{0}_{S}: DCA to Primary Vertex vs K^{0}_{S} momentum ; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",
-                                  200,0.,20.,100,0.,10.);
+      Int_t aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kTracks);
+      if ( (aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr ) {
+       if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1)  ||
+            ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates1")))->Fill( -aaa );
+       else
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates1")))->Fill( aaa );
+      }
 
-    nameMass="histK0ScosPAwrtPVvspK0s";
-    TH2F *cosPAwrtPVvspK0s = new TH2F(nameMass.Data(),"K^{0}_{S}: cosine of pointing angle wrt primary vertex vs K^{0}_{S} momentum ; p(K^{0}_{S}) [GeV/c]; cosine; Entries",
-                                     200,0.,20.,100,0.99,1.);
+      cutsAnal->SetUsePID(kFALSE);
+      aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate);
+      if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
+       if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
+            ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates2")))->Fill( -aaa );
+       else
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates2")))->Fill( aaa );
+      }
+      cutsAnal->SetUsePID(areCutsUsingPID);
+
+      aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kPID);
+      if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
+       if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
+            ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates3")))->Fill( -aaa );
+       else
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates3")))->Fill( aaa );
+      }
 
-    TH2F* allspectrumK0SMass = (TH2F*)spectrumK0SMass->Clone(); 
-    TH2F* allspectrumLcMassByK0S    = (TH2F*)spectrumLcMassByK0S->Clone(); 
-    //TH2F* allcosOpeningAngleK0Spvsp= (TH2F*)cosOpeningAngleK0Spvsp->Clone(); 
-    TH2F* allmomentumDistributionK0Svsp= (TH2F*)momentumDistributionK0Svsp->Clone(); 
-    TH2F* alldcatoPVvspK0s=(TH2F*)dcatoPVvspK0s->Clone(); 
-    TH2F* allcosV0PAwrtPVvspK0s=(TH2F*)cosPAwrtPVvspK0s->Clone(); 
+      aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kAll);
+      if ((aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr) {
+       if ( ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi && bachelor->Charge()==-1) ||
+            ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi && bachelor->Charge()==+1) )
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates4")))->Fill( -aaa );
+       else
+         ((TH1F*)(fOutput->FindObject("hSwitchOnCandidates4")))->Fill( aaa );
+      }
 
-    TH2F* pidBachspectrumK0SMass = (TH2F*)spectrumK0SMass->Clone(); 
-    TH2F* pidBachspectrumLcMassByK0S    = (TH2F*)spectrumLcMassByK0S->Clone(); 
-    //TH2F* pidBachcosOpeningAngleK0Spvsp= (TH2F*)cosOpeningAngleK0Spvsp->Clone(); 
-    TH2F* pidBachmomentumDistributionK0Svsp= (TH2F*)momentumDistributionK0Svsp->Clone(); 
-    TH2F* pidBachdcatoPVvspK0s=(TH2F*)dcatoPVvspK0s->Clone(); 
-    TH2F* pidBachcosV0PAwrtPVvspK0s=(TH2F*)cosPAwrtPVvspK0s->Clone(); 
+    }
+  }
 
-    TH2F* allArmenterosPodK0s = (TH2F*)armenterosPodK0s->Clone();
-    TH2F* pidBachArmenterosPodK0s = (TH2F*)armenterosPodK0s->Clone();
+  return;
+}
 
-    fOutputAll->Add(allspectrumK0SMass);
-    fOutputAll->Add(allspectrumLcMassByK0S);
-    //fOutputAll->Add(allcosOpeningAngleK0Spvsp); 
-    fOutputAll->Add(allmomentumDistributionK0Svsp); 
-    fOutputAll->Add(allArmenterosPodK0s);
-    fOutputAll->Add(alldcatoPVvspK0s);
-    fOutputAll->Add(allcosV0PAwrtPVvspK0s);
+//-------------------------------------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelor::FillTheTree(AliAODRecoCascadeHF *part, AliRDHFCutsLctoV0 *cutsAnal, TClonesArray *mcArray, Int_t isLc) {
+  //
+  // This is to fill tree
+  //
 
-    fOutputPIDBach->Add(pidBachspectrumK0SMass);
-    fOutputPIDBach->Add(pidBachspectrumLcMassByK0S);
-    //fOutputPIDBach->Add(pidBachcosOpeningAngleK0Spvsp); 
-    fOutputPIDBach->Add(pidBachmomentumDistributionK0Svsp); 
-    fOutputPIDBach->Add(pidBachArmenterosPodK0s);
-    fOutputPIDBach->Add(pidBachdcatoPVvspK0s);
-    fOutputPIDBach->Add(pidBachcosV0PAwrtPVvspK0s);
-  }
+  Double_t mk0sPDG = TDatabasePDG::Instance()->GetParticle(310)->Mass();
+  Double_t mLPDG   = TDatabasePDG::Instance()->GetParticle(3122)->Mass();
 
-  // V0 invariant masses (offline)
-  nameMass="histK0SMassOffline";
-  TH2F* spectrumK0SMassOffline = new TH2F(nameMass.Data(),"K^{0}_{S} invariant mass VS p_{T}; M(#pi^{+}#pi^{-}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
-                                         520,0.43,0.56,200,0.,20.);
+  Double_t invmassLc = part->InvMassLctoK0sP();
+  Double_t invmassLc2Lpi = part->InvMassLctoLambdaPi();
 
-  // Lc invariant masses (x K0S offline)
-  nameMass="histLcMassOfflineByK0S";
-  TH2F* spectrumLcMassOfflineByK0S = new TH2F(nameMass.Data(),"#Lambda_{C} invariant mass (by K^{0}_{S}) vs p_{T}; M(K^{0}_{S}p) [GeV/c^{2}]; p_{T} [GeV/c]",
-                                             1200,2.,2.6,200,0.,20.);
+  AliAODTrack *bachelor = (AliAODTrack*)part->GetBachelor();
 
+  AliAODv0 * v0part = (AliAODv0*)part->Getv0();
+  Double_t dcaV0ptp = v0part->GetDCA();
+  Double_t invmassK0S = v0part->MassK0Short();
+  Double_t invmassLambda = v0part->MassLambda();
+  Double_t invmassLambdaBar = v0part->MassAntiLambda();
 
-  //nameMass="histcosOAK0SpvspOffline";
-  //TH2F* cosOpeningAngleK0SpvspOffline = new TH2F(nameMass.Data(),"#Lambda_{C}: K^{0}_{S} - p opening angle vs p  -  Offline ; Cos(Opening Angle)  ; p [GeV/c]",
-  //200,-1.,1.,200,0.,20.);
+  Int_t isLc2LBarpi=0, isLc2Lpi=0;
+  Int_t mcLabel = -1;
+  Int_t isDp2K0Spi=0, isDs2K0SK=0;
+  Int_t mcLabel2 = -1;
+  Int_t mcLabel3 = -1;
+  if (fUseMCInfo) {
+    Int_t pdgCand = 4122;
+    Int_t pdgDgLctoV0bachelor[2]={211,3122};
+    Int_t pdgDgV0toDaughters[2]={2212,211};
+    mcLabel = part->MatchToMC(pdgCand,pdgDgLctoV0bachelor[1],pdgDgLctoV0bachelor,pdgDgV0toDaughters,mcArray,kTRUE);
+    if (mcLabel!=-1) {
+      if (bachelor->Charge()==-1) isLc2LBarpi=1;
+      if (bachelor->Charge()==+1) isLc2Lpi=1;
+    }
 
-  nameMass="histpK0SvspOffline";
-  TH2F* momentumDistributionK0SvspOffline = new TH2F(nameMass.Data(),"#Lambda_{C}: p(K^{0}_{S}) vs p(p) - Offline ;  p_{p} [GeV/c]; p_{K^{0}_{S}} [GeV/c]",
-                                                   200,0.,20.,200,0.,20.);
+    Int_t pdgCand2 = 411; // D+ -> pi+ K0S
+    Int_t pdgCand3 = 431; // Ds+ -> K+ K0S
+    Int_t pdgDgCand2[2]={211,310};
+    Int_t pdgDgCand3[2]={321,310};
+    pdgDgV0toDaughters[0]=211;
+    pdgDgV0toDaughters[1]=211;
+    mcLabel2 = part->MatchToMC(pdgCand2,pdgDgCand2[1],pdgDgCand2,pdgDgV0toDaughters,mcArray,kTRUE);
+    mcLabel3 = part->MatchToMC(pdgCand3,pdgDgCand3[1],pdgDgCand3,pdgDgV0toDaughters,mcArray,kTRUE);
+    if (mcLabel2!=-1) isDp2K0Spi=1;
+    if (mcLabel3!=-1) isDs2K0SK=1;
+  }
 
-  nameMass="histOfflineArmPodK0s";
-  TH2F* armenterosPodK0sOff = new TH2F(nameMass.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution - Offline; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
-                                      200,-1.,1.,300,0.,0.3);
+  Int_t isLcByMC = isLc+isLc2LBarpi*2+isLc2Lpi*4+isDp2K0Spi*8+isDs2K0SK*16;
 
-  nameMass="histDCAtoPVvspK0SOffline";
-  TH2F *dcatoPVvspK0sOffline = new TH2F(nameMass.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass - Offline; p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",
-                                       200,0.,20.,100,0.,10.);
+  Int_t isK0S = 0;
+  Int_t isLambda = 0;
+  Int_t isLambdaBar = 0;
+  Int_t isGamma = 0;
+  if (fUseMCInfo) {
+    Int_t pdgDg2prong[2] = {211, 211};
+    Int_t labelK0S = v0part->MatchToMC(310,mcArray,2,pdgDg2prong);
+    if (labelK0S>=0) isK0S = 1;
+
+    pdgDg2prong[0] = 211;
+    pdgDg2prong[1] = 2212;
+    Int_t lambdaLabel = v0part->MatchToMC(3122,mcArray,2,pdgDg2prong);
+    if (lambdaLabel>=0) {
+      AliAODMCParticle *lambdaTrack = (AliAODMCParticle*)mcArray->At(lambdaLabel);
+      if (lambdaTrack->GetPdgCode()==3122) isLambda = 1;
+      else if (lambdaTrack->GetPdgCode()==-3122) isLambdaBar = 1;
+    }
+
+    pdgDg2prong[0] = 11;
+    pdgDg2prong[1] = 11;
+    Int_t gammaLabel = v0part->MatchToMC(22,mcArray,2,pdgDg2prong);
+    if (gammaLabel>=0) {
+      AliAODMCParticle *gammaTrack = (AliAODMCParticle*)mcArray->At(gammaLabel);
+      if (gammaTrack->GetPdgCode()==22) isGamma = 1;
+    }
+  }
 
-  nameMass="histK0ScosPAwrtPVvspK0sOffline";
-  TH2F *cosPAwrtPVvspK0sOffline = new TH2F(nameMass.Data(),"K^{0}_{S}: cosine of pointing angle wrt primary vertex vs K^{0}_{S} momentum - Offline; p(K^{0}_{S}) [GeV/c]; cosine; Entries",
-                                          200,0.,20.,100,0.99,1.);
+  Int_t isV0ByMC = isK0S+isLambdaBar*2+isLambda*4+isGamma*8;
 
+  Int_t isBachelorSelected = (bachelor->TestFilterMask(BIT(4)))*1 + (!(bachelor->TestFilterMask(BIT(4))))*2;
+  isBachelorSelected += (bachelor->GetLabel()<0)*4 + (bachelor->GetLabel()>=0)*8;
+  if ( ( !(bachelor->HasPointOnITSLayer(0)) && !(bachelor->HasPointOnITSLayer(1)) ) )
+    isBachelorSelected += 16;
+  else {
+    if ( bachelor->HasPointOnITSLayer(0) && !(bachelor->HasPointOnITSLayer(1)) )
+      isBachelorSelected += 32;
+    else if ( !(bachelor->HasPointOnITSLayer(0)) && bachelor->HasPointOnITSLayer(1) )
+      isBachelorSelected += 64;
+    else
+      isBachelorSelected += 128;
+  }
 
+  AliAODTrack *v0pos = (AliAODTrack*)part->Getv0PositiveTrack();
+  AliAODTrack *v0neg = (AliAODTrack*)part->Getv0NegativeTrack(); 
 
-  TH2F* allspectrumK0SMassOffline = (TH2F*)spectrumK0SMassOffline->Clone(); 
-  TH2F* allspectrumLcMassOfflineByK0S    = (TH2F*)spectrumLcMassOfflineByK0S->Clone(); 
-  //TH2F* allcosOpeningAngleK0SpvspOffline= (TH2F*)cosOpeningAngleK0SpvspOffline->Clone(); 
-  TH2F* allmomentumDistributionK0SvspOffline= (TH2F*)momentumDistributionK0SvspOffline->Clone(); 
-  TH2F* alldcatoPVvspK0sOffline=(TH2F*)dcatoPVvspK0sOffline->Clone(); 
-  TH2F* allcosPAwrtPVvspK0sOffline=(TH2F*)cosPAwrtPVvspK0sOffline->Clone(); 
+  Int_t areV0daughtersSelected = (v0pos->TestFilterMask(BIT(4)))*1 + (!(v0pos->TestFilterMask(BIT(4))))*2;
+  areV0daughtersSelected += (v0pos->GetLabel()<0)*4 + (v0pos->GetLabel()>=0)*8;
+  areV0daughtersSelected += (v0neg->TestFilterMask(BIT(4)))*16 + (!(v0neg->TestFilterMask(BIT(4))))*32;
+  areV0daughtersSelected += (v0neg->GetLabel()<0)*64 + (v0neg->GetLabel()>=0)*128;
 
-  TH2F* pidBachspectrumK0SMassOffline = (TH2F*)spectrumK0SMassOffline->Clone(); 
-  TH2F* pidBachspectrumLcMassOfflineByK0S    = (TH2F*)spectrumLcMassOfflineByK0S->Clone(); 
-  //TH2F* pidBachcosOpeningAngleK0SpvspOffline= (TH2F*)cosOpeningAngleK0SpvspOffline->Clone(); 
-  TH2F* pidBachmomentumDistributionK0SvspOffline= (TH2F*)momentumDistributionK0SvspOffline->Clone(); 
-  TH2F* pidBachdcatoPVvspK0sOffline=(TH2F*)dcatoPVvspK0sOffline->Clone(); 
-  TH2F* pidBachcosPAwrtPVvspK0sOffline=(TH2F*)cosPAwrtPVvspK0sOffline->Clone(); 
+  Double_t nSigmaITSpr=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,4,nSigmaITSpr);
+  Double_t nSigmaTPCpr=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,4,nSigmaTPCpr);
+  Double_t nSigmaTOFpr=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,4,nSigmaTOFpr);
 
-  TH2F* allArmenterosPodK0sOff = (TH2F*)armenterosPodK0sOff->Clone();
-  TH2F* pidBachArmenterosPodK0sOff = (TH2F*)armenterosPodK0sOff->Clone();
+  Double_t nSigmaITSpi=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,2,nSigmaITSpi);
+  Double_t nSigmaTPCpi=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,2,nSigmaTPCpi);
+  Double_t nSigmaTOFpi=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,2,nSigmaTOFpi);
 
+  Double_t nSigmaITSka=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaITS(bachelor,3,nSigmaITSka);
+  Double_t nSigmaTPCka=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTPC(bachelor,3,nSigmaTPCka);
+  Double_t nSigmaTOFka=-999.;
+  cutsAnal->GetPidHF()->GetnSigmaTOF(bachelor,3,nSigmaTOFka);
 
-  fOutputAll->Add(allspectrumK0SMassOffline);
-  fOutputAll->Add(allspectrumLcMassOfflineByK0S);
-  //fOutputAll->Add(allcosOpeningAngleK0SpvspOffline); 
-  fOutputAll->Add(allmomentumDistributionK0SvspOffline); 
-  fOutputAll->Add(allArmenterosPodK0sOff);
-  fOutputAll->Add(alldcatoPVvspK0sOffline);
-  fOutputAll->Add(allcosPAwrtPVvspK0sOffline);
 
-  fOutputPIDBach->Add(pidBachspectrumK0SMassOffline);
-  fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0S);
-  //fOutputPIDBach->Add(pidBachcosOpeningAngleK0SpvspOffline); 
-  fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOffline); 
-  fOutputPIDBach->Add(pidBachArmenterosPodK0sOff);
-  fOutputPIDBach->Add(pidBachdcatoPVvspK0sOffline);
-  fOutputPIDBach->Add(pidBachcosPAwrtPVvspK0sOffline);
+  Int_t flagToCheckCandidate = 1*(TMath::Abs(invmassK0S-mk0sPDG)<=0.050);
+  flagToCheckCandidate+=2*((TMath::Abs(invmassLambdaBar-mLPDG)<=0.050) && (bachelor->Charge()==-1));
+  flagToCheckCandidate+=4*((TMath::Abs(invmassLambda-mLPDG)<=0.050) && (bachelor->Charge()==+1));
+  flagToCheckCandidate+=8*((TMath::Abs(invmassLambdaBar-mLPDG)<=0.050) && (bachelor->Charge()==+1));
+  flagToCheckCandidate+=16*((TMath::Abs(invmassLambda-mLPDG)<=0.050) && (bachelor->Charge()==-1));
 
   /*
-  nameMass="hist1LcMassOfflineByK0S";
-  TH2D* h1 = new TH2D(nameMass.Data(),"IsEventSelected; M(#Lambda_{C}) [GeV/c^{2}]; p_{T}",1200,2.,2.6,200,0.,20.);
-  fOutput->Add(h1);
-  nameMass="hist0LcMassOfflineByK0S";
-  TH2D* h0 = new TH2D(nameMass.Data(),"!IsEventSelected; M(#Lambda_{C}) [GeV/c^{2}]; p_{T}",1200,2.,2.6,200,0.,20.);
-  fOutput->Add(h0);
+  Bool_t areCutsUsingPID = cutsAnal->GetIsUsePID();
+  cutsAnal->SetUsePID(kFALSE);
+  Int_t aaa = cutsAnal->IsSelected(part,AliRDHFCuts::kCandidate);
+  if ( (aaa&AliRDHFCutsLctoV0::kLcToK0Spr)==AliRDHFCutsLctoV0::kLcToK0Spr ) {
+    if ( aaa==AliRDHFCutsLctoV0::kLcToK0Spr ) {
+      flagToCheckCandidate = aaa; // Lc->K0S+p OK
+    } else {
+      if ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi ) {
+       if (bachelor->Charge()==+1)
+         flagToCheckCandidate = aaa; // Lc->Lambda+pi+
+       else if (bachelor->Charge()==-1)
+         flagToCheckCandidate =-aaa; // Lambda+pi- AS Lc->K0S+p candidate
+      }
+      if ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi ) {
+       if (bachelor->Charge()==-1)
+         flagToCheckCandidate = aaa; // Lc->LambdaBar+pi-
+       else if (bachelor->Charge()==+1)
+         flagToCheckCandidate =-aaa; // LambdaBar+pi+ AS Lc->K0S+p candidate
+      }
+    }
+  } else {
+    //if ( aaa==AliRDHFCutsLctoV0::kLcToK0Spr ) {
+    //flagToCheckCandidate = -10-(AliRDHFCutsLctoV0::kLcToK0Spr); // NEVER
+    //} else {
+      if ( (aaa&AliRDHFCutsLctoV0::kLcToLpi)==AliRDHFCutsLctoV0::kLcToLpi ) {
+       if (bachelor->Charge()==+1)
+         flagToCheckCandidate = aaa; // Lc->Lambda+pi+ OK
+       else if (bachelor->Charge()==-1)
+         flagToCheckCandidate =-aaa; // Lambda+pi- AS Lc->Lambda+pi+ candidate
+      }
+      if ( (aaa&AliRDHFCutsLctoV0::kLcToLBarpi)==AliRDHFCutsLctoV0::kLcToLBarpi ) {
+       if (bachelor->Charge()==-1)
+         flagToCheckCandidate = aaa; // Lc->LambdaBar+pi- OK
+       else if (bachelor->Charge()==+1)
+         flagToCheckCandidate =-aaa; // LambdaBar+pi+ AS Lc->LambdaBar+pi- candidate
+      }
+      //}
+  }
+  cutsAnal->SetUsePID(areCutsUsingPID);
   */
 
-  if (fUseMCInfo) {
+  fCandidateVariables[ 0] = fUseMCInfo+isLcByMC; // 0: real data; 1: bkg; 2: Lc->K0S+p; 3: Lc->LambdaBar+pbar; 5: Lc->Lambda+p
+  fCandidateVariables[ 1] = fUseMCInfo+isV0ByMC; // 0: real data; 1: bkg; 2: K0S->pi+pi; 3: LambdaBar->pbar+pi+; 5: Lambda->p+pi-
+  fCandidateVariables[ 2] = isBachelorSelected;
+  fCandidateVariables[ 3] = areV0daughtersSelected;
+  fCandidateVariables[ 4] = flagToCheckCandidate;
+  fCandidateVariables[ 5] = invmassLc;
+  fCandidateVariables[ 6] = invmassLc2Lpi;
+  fCandidateVariables[ 7] = part->InvMass2Prongs(0,1,211,310); // D+ -> pi+ K0S
+  fCandidateVariables[ 8] = part->InvMass2Prongs(0,1,321,310); // D+S -> K+ K0S
+  fCandidateVariables[ 9] = invmassK0S;
+  fCandidateVariables[10] = invmassLambda;
+  fCandidateVariables[11] = invmassLambdaBar;
+  fCandidateVariables[12] = v0part->InvMass2Prongs(0,1,11,11);
+  fCandidateVariables[13] = part->GetDCA();
+  fCandidateVariables[14] = dcaV0ptp;
+  fCandidateVariables[15] = part->Getd0Prong(0);
+  fCandidateVariables[16] = part->Getd0Prong(1);
+  fCandidateVariables[17] = v0part->Getd0Prong(0);
+  fCandidateVariables[18] = v0part->Getd0Prong(1);
+  fCandidateVariables[19] = part->CosPointingAngle();
+  fCandidateVariables[20] = part->CosV0PointingAngle();
+  fCandidateVariables[21] = v0part->RadiusSecVtx();
+  fCandidateVariables[22] = nSigmaITSpr;
+  fCandidateVariables[23] = nSigmaITSpi;
+  fCandidateVariables[24] = nSigmaITSka;
+  fCandidateVariables[25] = nSigmaTPCpr;
+  fCandidateVariables[26] = nSigmaTPCpi;
+  fCandidateVariables[27] = nSigmaTPCka;
+  fCandidateVariables[28] = nSigmaTOFpr;
+  fCandidateVariables[29] = nSigmaTOFpi;
+  fCandidateVariables[30] = nSigmaTOFka;
+  fCandidateVariables[31] = part->Y(4122);
+  fCandidateVariables[32] = bachelor->Eta();
+  fCandidateVariables[33] = v0pos->Eta();
+  fCandidateVariables[34] = v0neg->Eta();
+  fCandidateVariables[35] = part->P();
+  fCandidateVariables[36] = part->Pt();
+  fCandidateVariables[37] = v0part->P();
+  fCandidateVariables[38] = v0part->Pt();
+  fCandidateVariables[39] = bachelor->P();
+  fCandidateVariables[40] = bachelor->Pt();
+  fCandidateVariables[41] = v0pos->P();
+  fCandidateVariables[42] = v0pos->Pt();
+  fCandidateVariables[43] = v0neg->P();
+  fCandidateVariables[44] = v0neg->Pt();
+  fCandidateVariables[45] = part->DecayLength();
+  fCandidateVariables[46] = part->DecayLengthV0();
+  fCandidateVariables[47] = part->CosPointingAngleXY();
+  fCandidateVariables[48] = part->CosV0PointingAngleXY();
+  fCandidateVariables[49] = part->DecayLengthXY();
+  fCandidateVariables[50] = part->DecayLengthXYV0();
+  fCandidateVariables[51] = part->NormalizedDecayLength();
+  fCandidateVariables[52] = part->NormalizedV0DecayLength();
+  fCandidateVariables[53] = part->NormalizedDecayLengthXY();
+  fCandidateVariables[54] = part->NormalizedV0DecayLengthXY();
+  Double_t xVtxLc=0, yVtxLc=0, zVtxLc=0;
+  Double_t xLcMC=0,yLcMC=0,zLcMC=0;
+  Double_t pxVtxBachelor=0, pyVtxBachelor=0, pzVtxBachelor=0;
+  Double_t dcaForLc = PropagateToDCA(v0part,bachelor,fBzkG, xVtxLc, yVtxLc, zVtxLc, pxVtxBachelor, pyVtxBachelor, pzVtxBachelor);
+  if (isLc) {
+    Int_t pdgCand0 = 4122;
+    Int_t pdgDgLctoV0bachelor0[2]={2212,310};
+    Int_t pdgDgV0toDaughters0[2]={211,211};
+    Int_t mcLabel0 = part->MatchToMC(pdgCand0,pdgDgLctoV0bachelor0[1],pdgDgLctoV0bachelor0,pdgDgV0toDaughters0,mcArray,kTRUE);
+    AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel0));
+    AliAODMCParticle *partLcDaug0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(partLc->GetDaughter(0)));
+    xLcMC=partLcDaug0->Xv(), yLcMC=partLcDaug0->Yv(), zLcMC=partLcDaug0->Zv();
+  } else if (isLc2LBarpi || isLc2Lpi) {
+    AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel));
+    AliAODMCParticle *partLcDaug0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(partLc->GetDaughter(0)));
+    xLcMC=partLcDaug0->Xv(), yLcMC=partLcDaug0->Yv(), zLcMC=partLcDaug0->Zv();
+  } else if (isDp2K0Spi) {
+    AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel2));
+    AliAODMCParticle *partLcDaug0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(partLc->GetDaughter(0)));
+    xLcMC=partLcDaug0->Xv(), yLcMC=partLcDaug0->Yv(), zLcMC=partLcDaug0->Zv();
+  } else if (isDs2K0SK) {
+    AliAODMCParticle *partLc = dynamic_cast<AliAODMCParticle*>(mcArray->At(mcLabel3));
+    AliAODMCParticle *partLcDaug0 = dynamic_cast<AliAODMCParticle*>(mcArray->At(partLc->GetDaughter(0)));
+    xLcMC=partLcDaug0->Xv(), yLcMC=partLcDaug0->Yv(), zLcMC=partLcDaug0->Zv();
+  }
+  fCandidateVariables[55]=dcaForLc;
+  fCandidateVariables[56]=part->GetSecVtxX();
+  fCandidateVariables[57]=part->GetSecVtxY();
+  fCandidateVariables[58]=part->GetSecVtxZ();
+  fCandidateVariables[59]=xVtxLc;
+  fCandidateVariables[60]=yVtxLc;
+  fCandidateVariables[61]=zVtxLc;
+  fCandidateVariables[62]=xLcMC;
+  fCandidateVariables[63]=yLcMC;
+  fCandidateVariables[64]=zLcMC;
+  fCandidateVariables[65]=bachelor->Px();
+  fCandidateVariables[66]=bachelor->Py();
+  fCandidateVariables[67]=pxVtxBachelor;
+  fCandidateVariables[68]=pyVtxBachelor;
+  fCandidateVariables[69]=v0part->Px();
+  fCandidateVariables[70]=v0part->Py();
+  fCandidateVariables[71]=fVtx1->GetX();
+  fCandidateVariables[72]=fVtx1->GetY();
+  fCandidateVariables[73]=fVtx1->GetZ();
+  fCandidateVariables[74]=part->CosThetaStar(0,4122,2212,310);
+  fCandidateVariables[75]=part->CosThetaStar(1,4122,2212,310);
+  fCandidateVariables[76]=v0part->Eta();
+  fCandidateVariables[77]=v0part->Y(310);
+  fCandidateVariables[78]=pzVtxBachelor;
+  fCandidateVariables[79]=v0part->Pz();
+
+  //fCandidateVariables[65] = bachelor->Px();
+  //fCandidateVariables[66] = bachelor->Py();
+  //fCandidateVariables[67] = bachelor->Pz();
+  //fCandidateVariables[68] = v0pos->Px();
+  //fCandidateVariables[69] = v0pos->Py();
+  //fCandidateVariables[70] = v0pos->Pz();
+  //fCandidateVariables[71] = v0neg->Px();
+  //fCandidateVariables[72] = v0neg->Py();
+  //fCandidateVariables[73] = v0neg->Pz();
+  //fCandidateVariables[74] = part->PxProng(0);
+  //fCandidateVariables[75] = part->PyProng(0);
+  //fCandidateVariables[76] = part->PzProng(0);
+  //fCandidateVariables[77] = part->PxProng(1);
+  //fCandidateVariables[78] = part->PyProng(1);
+  //fCandidateVariables[79] = part->PzProng(1);
+  //fCandidateVariables[80] = v0part->PxProng(0);
+  //fCandidateVariables[81] = v0part->PyProng(0);
+  //fCandidateVariables[82] = v0part->PzProng(0);
+  //fCandidateVariables[83] = v0part->PxProng(1);
+  //fCandidateVariables[84] = v0part->PyProng(1);
+  //fCandidateVariables[85] = v0part->PzProng(1);
+  //fCandidateVariables[86] = part->QtProng(0);
+  //fCandidateVariables[87] = part->Alpha();
+
+  fVariablesTree->Fill();
 
-    if (fUseOnTheFlyV0) {
+  return;
+}
 
-      nameSgn="histK0SMassSgn";
-      nameBkg="histK0SMassBkg";
-      TH2F* spectrumK0SMassSgn = new TH2F(nameSgn.Data(), "K^{0}_{S} Signal invariant mass VS p_{T} - MC; M(K^{0}_{S}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries", 520,0.43,0.56,200,0.,20.);
-      TH2F* spectrumK0SMassBkg = new TH2F(nameBkg.Data(), "K^{0}_{S} Background invariant mass VS p_{T} - MC; M(K^{0}_{S}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",  520,0.43,0.56,200,0.,20.);
+//-------------------------------------------------------------------------------
+void AliAnalysisTaskSELc2V0bachelor::DefineTreeVariables() {
+  //
+  // This is to define tree variables
+  //
 
-      TH2F* allspectrumK0SMassSgn = (TH2F*)spectrumK0SMassSgn->Clone(); 
-      TH2F* allspectrumK0SMassBkg = (TH2F*) spectrumK0SMassBkg->Clone();  
-      TH2F* pidBachspectrumK0SMassSgn = (TH2F*)spectrumK0SMassSgn->Clone(); 
-      TH2F* pidBachspectrumK0SMassBkg = (TH2F*) spectrumK0SMassBkg->Clone();  
-  
+  const char* nameoutput = GetOutputSlot(4)->GetContainer()->GetName();
+  fVariablesTree = new TTree(nameoutput,"Candidates variables tree");
+  Int_t nVar = 80;
+  fCandidateVariables = new Float_t [nVar];
+  TString * fCandidateVariableNames = new TString[nVar];
+  fCandidateVariableNames[ 0]="isLcByMC";
+  fCandidateVariableNames[ 1]="isV0ByMC";
+  fCandidateVariableNames[ 2]="flagToCheckBachelor";
+  fCandidateVariableNames[ 3]="flagToCheckV0daughters";
+  fCandidateVariableNames[ 4]="flagToCheckCandidate";
+  fCandidateVariableNames[ 5]="massLc2K0Sp";
+  fCandidateVariableNames[ 6]="massLc2Lambdapi";
+  fCandidateVariableNames[ 7]="massD2K0Spi"; // D+ -> pi+ K0S
+  fCandidateVariableNames[ 8]="massDS2K0SK"; // D+S -> K+ K0S
+  fCandidateVariableNames[ 9]="massK0S";
+  fCandidateVariableNames[10]="massLambda";
+  fCandidateVariableNames[11]="massLambdaBar";
+  fCandidateVariableNames[12]="massGamma";
+  fCandidateVariableNames[13]="dcaLcptp"; // DCA Lc prong-to-prong
+  fCandidateVariableNames[14]="dcaV0ptp";
+  fCandidateVariableNames[15]="tImpParBach";
+  fCandidateVariableNames[16]="tImpParV0";
+  fCandidateVariableNames[17]="dcaV0postoPV";
+  fCandidateVariableNames[18]="dcaV0negtoPV";
+  fCandidateVariableNames[19]="cosPALc";
+  fCandidateVariableNames[20]="cosPAK0S";
+  fCandidateVariableNames[21]="rhoV0";
+  fCandidateVariableNames[22]="nSigmaITSpr";
+  fCandidateVariableNames[23]="nSigmaITSpi";
+  fCandidateVariableNames[24]="nSigmaITSka";
+  fCandidateVariableNames[25]="nSigmaTPCpr";
+  fCandidateVariableNames[26]="nSigmaTPCpi";
+  fCandidateVariableNames[27]="nSigmaTPCka";
+  fCandidateVariableNames[28]="nSigmaTOFpr";
+  fCandidateVariableNames[29]="nSigmaTOFpi";
+  fCandidateVariableNames[30]="nSigmaTOFka";
+  fCandidateVariableNames[31]="yLc";
+  fCandidateVariableNames[32]="etaBach"; // etaBachelor
+  fCandidateVariableNames[33]="etaV0pos"; // etaV0pos
+  fCandidateVariableNames[34]="etaV0neg"; // etaV0neg
+  fCandidateVariableNames[35]="LcP"; // @ DCA
+  fCandidateVariableNames[36]="LcPt"; // @ DCA
+  fCandidateVariableNames[37]="v0P"; // @ V0 DCA
+  fCandidateVariableNames[38]="v0Pt"; // @ V0 DCA
+  fCandidateVariableNames[39]="bachelorP"; // @ prim vtx
+  fCandidateVariableNames[40]="bachelorPt"; // @ prim vtx
+  fCandidateVariableNames[41]="V0positiveP"; // @ prim vtx
+  fCandidateVariableNames[42]="V0positivePt"; // @ prim vtx
+  fCandidateVariableNames[43]="V0negativeP"; // @ prim vtx
+  fCandidateVariableNames[44]="V0negativePt"; // @ prim vtx
+  fCandidateVariableNames[45]="decayLengthLc";
+  fCandidateVariableNames[46]="decayLengthV0";
+  fCandidateVariableNames[47]="cosPALcXY"; // cosPA XY x Lc
+  fCandidateVariableNames[48]="cosPAV0XY"; // cosPA XY x V0
+  fCandidateVariableNames[49]="decayLengthLcXY"; // decay length XY x Lc
+  fCandidateVariableNames[50]="decayLengthV0XY"; // decay length XY x V0
+  fCandidateVariableNames[51]="normalizedDecayLengthLc"; // normalized decay length x Lc
+  fCandidateVariableNames[52]="normalizedDecayLengthV0"; // normalized decay length x V0
+  fCandidateVariableNames[53]="normalizedDecayLengthXYLc"; // normalized decay length XY x Lc
+  fCandidateVariableNames[54]="normalizedDecayLengthXYV0"; // normalized decay length XY x V0
+  fCandidateVariableNames[55]="newLcDCA";
+  fCandidateVariableNames[56]="xVtxLcBad";
+  fCandidateVariableNames[57]="yVtxLcBad";
+  fCandidateVariableNames[58]="zVtxLcBad";
+  fCandidateVariableNames[59]="xVtxLcGood";
+  fCandidateVariableNames[60]="yVtxLcGood";
+  fCandidateVariableNames[61]="zVtxLcGood";
+  fCandidateVariableNames[62]="xVtxLcMC";
+  fCandidateVariableNames[63]="yVtxLcMC";
+  fCandidateVariableNames[64]="zVtxLcMC";
+  fCandidateVariableNames[65]="pxVtxBachelorBad";
+  fCandidateVariableNames[66]="pyVtxBachelorBad";
+  fCandidateVariableNames[67]="pxVtxBachelorGood";
+  fCandidateVariableNames[68]="pyVtxBachelorGood";
+  fCandidateVariableNames[69]="pxVtxV0";
+  fCandidateVariableNames[70]="pyVtxV0";
+  fCandidateVariableNames[71]="xPvtx";
+  fCandidateVariableNames[72]="yPvtx";
+  fCandidateVariableNames[73]="zPvtx";
+  fCandidateVariableNames[74]="cosThetaStarBachelor";
+  fCandidateVariableNames[75]="cosThetaStarV0";
+  fCandidateVariableNames[76]="etaV0";
+  fCandidateVariableNames[77]="yV0";
+  fCandidateVariableNames[78]="pzVtxBachelorGood";
+  fCandidateVariableNames[79]="pzVtxV0";
+
+  //fCandidateVariableNames[65]="bachelorPx";
+  //fCandidateVariableNames[66]="bachelorPy";
+  //fCandidateVariableNames[67]="bachelorPz";
+  //fCandidateVariableNames[68]="V0positivePx";
+  //fCandidateVariableNames[69]="V0positivePy";
+  //fCandidateVariableNames[70]="V0positivePz";
+  //fCandidateVariableNames[71]="V0negativePx";
+  //fCandidateVariableNames[72]="V0negativePy";
+  //fCandidateVariableNames[73]="V0negativePz";
+  //fCandidateVariableNames[74]="bachelorPxDCA";
+  //fCandidateVariableNames[75]="bachelorPyDCA";
+  //fCandidateVariableNames[76]="bachelorPzDCA";
+  //fCandidateVariableNames[77]="v0PxDCA";
+  //fCandidateVariableNames[78]="v0PyDCA";
+  //fCandidateVariableNames[79]="v0PzDCA";
+  //fCandidateVariableNames[80]="V0positivePxDCA";
+  //fCandidateVariableNames[81]="V0positivePyDCA";
+  //fCandidateVariableNames[82]="V0positivePzDCA";
+  //fCandidateVariableNames[83]="V0negativePxDCA";
+  //fCandidateVariableNames[84]="V0negativePyDCA";
+  //fCandidateVariableNames[85]="V0negativePzDCA";
+  //fCandidateVariableNames[86]="qtLc";
+  //fCandidateVariableNames[87]="alphaLc";
+
+  for (Int_t ivar=0; ivar<nVar; ivar++) {
+    fVariablesTree->Branch(fCandidateVariableNames[ivar].Data(),&fCandidateVariables[ivar],Form("%s/f",fCandidateVariableNames[ivar].Data()));
+  }
 
-      fOutputAll->Add(allspectrumK0SMassSgn);
-      fOutputAll->Add(allspectrumK0SMassBkg);
-      fOutputPIDBach->Add(pidBachspectrumK0SMassSgn);
-      fOutputPIDBach->Add(pidBachspectrumK0SMassBkg);
+  return;
+}
 
+//__________________________________________________________________________
+void  AliAnalysisTaskSELc2V0bachelor::DefineGeneralHistograms() {
+  //
+  // This is to define general histograms
+  //
 
-      nameSgn="histLcMassByK0SSgn";
-      nameBkg="histLcMassByK0SBkg";
-      TH2F* spectrumLcMassByK0SSgn = new TH2F(nameSgn.Data(), "#Lambda_{C} Signal invariant mass (by K^{0}_{S}) vs p_{T}  - MC; M(#Lambda_{C}) [GeV/c^{2}];  p_{T}",
-                                             1200,2.,2.6,200,0.,20.);
-      TH2F* spectrumLcMassByK0SBkg = new TH2F(nameBkg.Data(), "#Lambda_{C} Background invariant mass (by K^{0}_{S}) vs p_{T}  - MC; M(#Lambda_{C}) [GeV/c^{2}]; p_{T}",
-                                             1200,2.,2.6,200,0.,20.);
+  fCEvents = new TH1F("fCEvents","conter",17,0,17);
+  fCEvents->SetStats(kTRUE);
+  fCEvents->GetXaxis()->SetBinLabel(1,"X1");
+  fCEvents->GetXaxis()->SetBinLabel(2,"Analyzed events");
+  fCEvents->GetXaxis()->SetBinLabel(3,"AliAODVertex exists");
+  fCEvents->GetXaxis()->SetBinLabel(4,"CascadesHF exists");
+  fCEvents->GetXaxis()->SetBinLabel(5,"MCarray exists");
+  fCEvents->GetXaxis()->SetBinLabel(6,"MCheader exists");
+  fCEvents->GetXaxis()->SetBinLabel(7,"GetNContributors()>0");
+  fCEvents->GetXaxis()->SetBinLabel(8,"IsEventSelected");
+  fCEvents->GetXaxis()->SetBinLabel(9,"triggerClass!=CINT1");
+  fCEvents->GetXaxis()->SetBinLabel(10,"triggerMask!=kAnyINT");
+  fCEvents->GetXaxis()->SetBinLabel(11,"triggerMask!=kAny");
+  fCEvents->GetXaxis()->SetBinLabel(12,"vtxTitle.Contains(Z)");
+  fCEvents->GetXaxis()->SetBinLabel(13,"vtxTitle.Contains(3D)");
+  fCEvents->GetXaxis()->SetBinLabel(14,"vtxTitle.Doesn'tContain(Z-3D)");
+  fCEvents->GetXaxis()->SetBinLabel(15,Form("zVtx<=%2.0fcm",fAnalCuts->GetMaxVtxZ()));
+  fCEvents->GetXaxis()->SetBinLabel(16,"!IsEventSelected");
+  fCEvents->GetXaxis()->SetBinLabel(17,"triggerMask!=kAnyINT || triggerClass!=CINT1");
+  //fCEvents->GetXaxis()->SetTitle("");
+  fCEvents->GetYaxis()->SetTitle("counts");
 
-      TH2F* allspectrumLcMassByK0SSgn = (TH2F*)spectrumLcMassByK0SSgn->Clone(); 
-      TH2F* allspectrumLcMassByK0SBkg = (TH2F*) spectrumLcMassByK0SBkg->Clone();  
-      TH2F* pidBachspectrumLcMassByK0SSgn = (TH2F*)spectrumLcMassByK0SSgn->Clone(); 
-      TH2F* pidBachspectrumLcMassByK0SBkg = (TH2F*) spectrumLcMassByK0SBkg->Clone();  
-      fOutputAll->Add(allspectrumLcMassByK0SSgn);
-      fOutputAll->Add(allspectrumLcMassByK0SBkg);
-      fOutputPIDBach->Add(pidBachspectrumLcMassByK0SSgn);
-      fOutputPIDBach->Add(pidBachspectrumLcMassByK0SBkg);
+  fOutput->Add(fCEvents);
+  TString fillthis="";
 
-      /*
-      nameSgn="histcosOAK0SpSgnvsp";
-      nameBkg="histcosOAK0SpBkgvsp";
-      TH2F* cosOpeningAngleK0SpSgnvsp= new TH2F(nameSgn.Data(),"#Lambda_{C} SGN : K^{0}_{S} - p Opening Angle  vs p - MC ; Cos(Opening Angle);  p ",
-                                               200,-1.,1.,200,0.,20.);
-      TH2F* cosOpeningAngleK0SpBkgvsp= new TH2F(nameBkg.Data(),"#Lambda_{C} BKG : K^{0}_{S} - p Opening Angle  vs p - MC;  Cos(Opening Angle);  p ",
-                                               200,-1.,1.,200,0.,20.);
-
-      TH2F* allcosOpeningAngleK0SpSgnvsp= (TH2F*)cosOpeningAngleK0SpSgnvsp->Clone(); 
-      TH2F* allcosOpeningAngleK0SpBkgvsp= (TH2F*)cosOpeningAngleK0SpBkgvsp->Clone(); 
-      TH2F* pidBachcosOpeningAngleK0SpSgnvsp= (TH2F*)cosOpeningAngleK0SpSgnvsp->Clone(); 
-      TH2F* pidBachcosOpeningAngleK0SpBkgvsp= (TH2F*)cosOpeningAngleK0SpBkgvsp->Clone(); 
-      fOutputAll->Add(allcosOpeningAngleK0SpSgnvsp); 
-      fOutputAll->Add(allcosOpeningAngleK0SpBkgvsp); 
-      fOutputPIDBach->Add(pidBachcosOpeningAngleK0SpSgnvsp); 
-      fOutputPIDBach->Add(pidBachcosOpeningAngleK0SpBkgvsp); 
-      */
+  if (fUseMCInfo && fAdditionalChecks) {
+    fillthis="histMcStatLc";
+    TH1F* mcStatisticLc = new TH1F(fillthis.Data(),"#Lambda_{c} generated and their decays",21,-10.5,10.5);
+    fOutput->Add(mcStatisticLc);
+  }
 
-      nameSgn="histpK0SvspSgn";
-      nameBkg="histpK0SvspBkg";
-      TH2F* momentumDistributionK0SvspSgn= new TH2F(nameSgn.Data(),"#Lambda_{C} SGN : K^{0}_{S} vs p Total Momentum Distribution - MC; p_{p}; p_{K^{0}_{S}}",
-                                                   200,0.,20.,200,0.,20.);
-      TH2F* momentumDistributionK0SvspBkg= new TH2F(nameBkg.Data(),"#Lambda_{C} BKG : K^{0}_{S} vs p Total Momentum Distribution - MC; p_{p}; p_{K^{0}_{S}}",
-                                                   200,0.,20.,200,0.,20.);
+  //fillthis="histopionV0SigmaVspTOF";
+  //TH2F *hpionV0SigmaVspTOF=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+  fillthis="histoprotonBachSigmaVspTOF";
+  TH2F *hprotonBachSigmaVspTOF=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-      TH2F* allmomentumDistributionK0SvspSgn= (TH2F*)momentumDistributionK0SvspSgn->Clone(); 
-      TH2F* allmomentumDistributionK0SvspBkg= (TH2F*)momentumDistributionK0SvspBkg->Clone(); 
-      TH2F* pidBachmomentumDistributionK0SvspSgn= (TH2F*)momentumDistributionK0SvspSgn->Clone(); 
-      TH2F* pidBachmomentumDistributionK0SvspBkg= (TH2F*)momentumDistributionK0SvspBkg->Clone(); 
-      fOutputAll->Add(allmomentumDistributionK0SvspSgn); 
-      fOutputAll->Add(allmomentumDistributionK0SvspBkg); 
-      fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspSgn); 
-      fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspBkg); 
+  //fOutput->Add(hpionV0SigmaVspTOF);
+  fOutput->Add(hprotonBachSigmaVspTOF);
 
+  //fillthis="histopionV0SigmaVspTPC";
+  //TH2F *hpionV0SigmaVspTPC=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+  fillthis="histoprotonBachSigmaVspTPC";
+  TH2F *hprotonBachSigmaVspTPC=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-      // armenteros-podolanski plots K0S
-      nameSgn="histArmPodK0sSgn";
-      nameBkg="histArmPodK0sBkg";
-      TH2F* armenterosPodK0sSgn = new TH2F(nameSgn.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (sgn); #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
-                                          200,-1.,1.,300,0.,0.3);
-      TH2F* armenterosPodK0sBkg = new TH2F(nameBkg.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (bkg); #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",
-                                          200,-1.,1.,300,0.,0.3);
+  //fOutput->Add(hpionV0SigmaVspTPC);
+  fOutput->Add(hprotonBachSigmaVspTPC);
 
-      TH2F* allArmenterosPodK0sSgn = (TH2F*)armenterosPodK0sSgn->Clone();
-      TH2F* allArmenterosPodK0sBkg = (TH2F*)armenterosPodK0sBkg->Clone();
-      TH2F* pidBachArmenterosPodK0sSgn = (TH2F*)armenterosPodK0sSgn->Clone();
-      TH2F* pidBachArmenterosPodK0sBkg = (TH2F*)armenterosPodK0sBkg->Clone();
+  if (fUseMCInfo) {
 
-      fOutputAll->Add(allArmenterosPodK0sSgn);
-      fOutputAll->Add(allArmenterosPodK0sBkg);
+    //fillthis="histopionV0SigmaVspTOFsgn";
+    //TH2F *hpionV0SigmaVspTOFsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    fillthis="histoprotonBachSigmaVspTOFsgn";
+    TH2F *hprotonBachSigmaVspTOFsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-      fOutputPIDBach->Add(pidBachArmenterosPodK0sSgn);
-      fOutputPIDBach->Add(pidBachArmenterosPodK0sBkg);
+    //fOutput->Add(hpionV0SigmaVspTOFsgn);
+    fOutput->Add(hprotonBachSigmaVspTOFsgn);
 
+    //fillthis="histopionV0SigmaVspTPCsgn";
+    //TH2F *hpionV0SigmaVspTPCsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    fillthis="histoprotonBachSigmaVspTPCsgn";
+    TH2F *hprotonBachSigmaVspTPCsgn=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-      nameSgn="histDCAtoPVvspK0SSgn";
-      nameBkg="histDCAtoPVvspK0SBkg";
-      TH2F *dcatoPVvspK0sSgn=new TH2F(nameSgn.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass (sgn); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",200,0.,20.,100,0.,10.0);
-      TH2F *dcatoPVvspK0sBkg=new TH2F(nameBkg.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass (bkg); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",200,0.,20.,100,0.,10.0);
+    //fOutput->Add(hpionV0SigmaVspTPCsgn);
+    fOutput->Add(hprotonBachSigmaVspTPCsgn);
 
-      TH2F* alldcatoPVvspK0sSgn= (TH2F*)dcatoPVvspK0sSgn->Clone();
-      TH2F* alldcatoPVvspK0sBkg= (TH2F*)dcatoPVvspK0sBkg->Clone();
-      TH2F* pidBachdcatoPVvspK0sSgn= (TH2F*)dcatoPVvspK0sSgn->Clone();
-      TH2F* pidBachdcatoPVvspK0sBkg= (TH2F*)dcatoPVvspK0sBkg->Clone();
 
-      fOutputAll->Add(alldcatoPVvspK0sSgn);
-      fOutputPIDBach->Add(pidBachdcatoPVvspK0sSgn);
-      fOutputAll->Add(alldcatoPVvspK0sBkg);
-      fOutputPIDBach->Add(pidBachdcatoPVvspK0sBkg);
+    //fillthis="histopionV0SigmaVspTOFbkg";
+    //TH2F *hpionV0SigmaVspTOFbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    fillthis="histoprotonBachSigmaVspTOFbkg";
+    TH2F *hprotonBachSigmaVspTOFbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-    }
+    //fOutput->Add(hpionV0SigmaVspTOFbkg);
+    fOutput->Add(hprotonBachSigmaVspTOFbkg);
 
+    //fillthis="histopionV0SigmaVspTPCbkg";
+    //TH2F *hpionV0SigmaVspTPCbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
+    fillthis="histoprotonBachSigmaVspTPCbkg";
+    TH2F *hprotonBachSigmaVspTPCbkg=new TH2F(fillthis.Data(),fillthis.Data(),300,0.,30.,100,-5.,5.);
 
-    nameSgn="histK0SMassOfflineSgn";
-    nameBkg="histK0SMassOfflineBkg";
-    TH2F* spectrumK0SMassOfflineSgn = new TH2F(nameSgn.Data(), "K^{0}_{S} Signal invariant mass VS p_{T} - MC; M(K^{0}_{S}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
-                                              520,0.43,0.56,200,0.,20.);
-    TH2F* spectrumK0SMassOfflineBkg = new TH2F(nameBkg.Data(), "K^{0}_{S} Background invariant mass VS p_{T} - MC; M(K^{0}_{S}) [GeV/c^{2}]; p_{T}(K^{0}_{S}) [GeV/c]; Entries",
-                                              520,0.43,0.56,200,0.,20.);
+    //fOutput->Add(hpionV0SigmaVspTPCbkg);
+    fOutput->Add(hprotonBachSigmaVspTPCbkg);
 
-    TH2F* allspectrumK0SMassOfflineSgn = (TH2F*)spectrumK0SMassOfflineSgn->Clone(); 
-    TH2F* allspectrumK0SMassOfflineBkg = (TH2F*) spectrumK0SMassOfflineBkg->Clone();  
-    fOutputAll->Add(allspectrumK0SMassOfflineSgn);
-    fOutputAll->Add(allspectrumK0SMassOfflineBkg);
+  }
 
+  if (fAdditionalChecks) {
+
+    TH1F *hZ2 = new TH1F("hZ2","",100,-50.,50.);
+    fOutput->Add(hZ2);
+    TH1F *hZ3 = new TH1F("hZ3","",100,-50.,50.);
+    fOutput->Add(hZ3);
+    TH1F *hZ4 = new TH1F("hZ4","",100,-50.,50.);
+    fOutput->Add(hZ4);
+    TH1F *hZ5 = new TH1F("hZ5","",100,-50.,50.);
+    fOutput->Add(hZ5);
+    TH1F *hZ6 = new TH1F("hZ6","",100,-50.,50.);
+    fOutput->Add(hZ6);
+    TH1F *hZ7 = new TH1F("hZ7","",100,-50.,50.);
+    fOutput->Add(hZ7);
+    TH1F *hZ8 = new TH1F("hZ8","",100,-50.,50.);
+    fOutput->Add(hZ8);
+    TH1F *hZ9 = new TH1F("hZ9","",100,-50.,50.);
+    fOutput->Add(hZ9);
+    TH1F *hZ10 = new TH1F("hZ10","",100,-50.,50.);
+    fOutput->Add(hZ10);
+    TH1F *hZ11 = new TH1F("hZ11","",100,-50.,50.);
+    fOutput->Add(hZ11);
+    TH1F *hZ12 = new TH1F("hZ12","",100,-50.,50.);
+    fOutput->Add(hZ12);
+    TH1F *hZ13 = new TH1F("hZ13","",100,-50.,50.);
+    fOutput->Add(hZ13);
+    TH1F *hZ14 = new TH1F("hZ14","",100,-50.,50.);
+    fOutput->Add(hZ14);
+    TH1F *hZ15 = new TH1F("hZ15","",100,-50.,50.);
+    fOutput->Add(hZ15);
+    TH1F *hZ16 = new TH1F("hZ16","",100,-50.,50.);
+    fOutput->Add(hZ16);
+  }
 
-    TH2F* pidBachspectrumK0SMassOfflineSgn = (TH2F*)spectrumK0SMassOfflineSgn->Clone(); 
-    TH2F* pidBachspectrumK0SMassOfflineBkg = (TH2F*) spectrumK0SMassOfflineBkg->Clone();
-    fOutputPIDBach->Add(pidBachspectrumK0SMassOfflineSgn);
-    fOutputPIDBach->Add(pidBachspectrumK0SMassOfflineBkg);
+  TH1F *hCandidateSelection = new TH1F("hCandidateSelection","",10,-0.5,9.5);
+  hCandidateSelection->GetXaxis()->SetBinLabel(1,"IsEventSelected");
+  hCandidateSelection->GetXaxis()->SetBinLabel(2,"IsSecondaryVtx");
+  hCandidateSelection->GetXaxis()->SetBinLabel(3,"V0toPosNeg");
+  hCandidateSelection->GetXaxis()->SetBinLabel(4,"offlineV0");
+  hCandidateSelection->GetXaxis()->SetBinLabel(5,"isInFiducialAcceptance");
+  hCandidateSelection->GetXaxis()->SetBinLabel(6,"analCuts::kTracks");
+  hCandidateSelection->GetXaxis()->SetBinLabel(7,"analCuts::kCandidateNoPID");
+  hCandidateSelection->GetXaxis()->SetBinLabel(8,"analCuts::kPID");
+  hCandidateSelection->GetXaxis()->SetBinLabel(9,"analCuts::kCandidateWithPID");
+  hCandidateSelection->GetXaxis()->SetBinLabel(10,"analCuts::kAll");
+  fOutput->Add(hCandidateSelection);
 
+  TH1F *hEventsWithCandidates = new TH1F("hEventsWithCandidates","conter",11,5.5,16.5);
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(1,"GetNContributors()>0");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(2,"IsEventSelected");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(3,"triggerClass!=CINT1");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(4,"triggerMask!=kAnyINT");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(5,"triggerMask!=kAny");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(6,"vtxTitle.Contains(Z)");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(7,"vtxTitle.Contains(3D)");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(8,"vtxTitle.Doesn'tContain(Z-3D)");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(9,Form("zVtx<=%2.0fcm",fAnalCuts->GetMaxVtxZ()));
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(10,"!IsEventSelected");
+  hEventsWithCandidates->GetXaxis()->SetBinLabel(11,"triggerMask!=kAnyINT || triggerClass!=CINT1");
+  fOutput->Add(hEventsWithCandidates);
 
-    nameSgn="histLcMassOfflineByK0SSgn";
-    nameBkg="histLcMassOfflineByK0SBkg";
-    TH2F* spectrumLcMassOfflineByK0SSgn = new TH2F(nameSgn.Data(), "#Lambda_{C} Signal invariant mass (by K^{0}_{S})  vs p_{T} - MC; M(#Lambda_{C}) [GeV/c^{2}]; p_{T}",
-                                                  1200,2.,2.6,200,0.,20.);
-    TH2F* spectrumLcMassOfflineByK0SBkg = new TH2F(nameBkg.Data(), "#Lambda_{C} Background invariant mass (by K^{0}_{S})  vs p_{T} - MC; M(#Lambda_{C}) [GeV/c^{2}]; p_{T}",
-                                                  1200,2.,2.6,200,0.,20.);
+  if (fAdditionalChecks) {
+
+    TH1F *hZ6a = new TH1F("hZ6a","",100,-50.,50.);
+    fOutput->Add(hZ6a);
+    TH1F *hZ7a = new TH1F("hZ7a","",100,-50.,50.);
+    fOutput->Add(hZ7a);
+    TH1F *hZ8a = new TH1F("hZ8a","",100,-50.,50.);
+    fOutput->Add(hZ8a);
+    TH1F *hZ9a = new TH1F("hZ9a","",100,-50.,50.);
+    fOutput->Add(hZ9a);
+    TH1F *hZ10a = new TH1F("hZ10a","",100,-50.,50.);
+    fOutput->Add(hZ10a);
+    TH1F *hZ11a = new TH1F("hZ11a","",100,-50.,50.);
+    fOutput->Add(hZ11a);
+    TH1F *hZ12a = new TH1F("hZ12a","",100,-50.,50.);
+    fOutput->Add(hZ12a);
+    TH1F *hZ13a = new TH1F("hZ13a","",100,-50.,50.);
+    fOutput->Add(hZ13a);
+    TH1F *hZ14a = new TH1F("hZ14a","",100,-50.,50.);
+    fOutput->Add(hZ14a);
+    TH1F *hZ15a = new TH1F("hZ15a","",100,-50.,50.);
+    fOutput->Add(hZ15a);
+    TH1F *hZ16a = new TH1F("hZ16a","",100,-50.,50.);
+    fOutput->Add(hZ16a);
+  }
 
+  TH1F *hSwitchOnCandidates1 = new TH1F("hSwitchOnCandidates1","",15,-7.5,7.5);
+  fOutput->Add(hSwitchOnCandidates1);
+  TH1F *hSwitchOnCandidates2 = new TH1F("hSwitchOnCandidates2","",15,-7.5,7.5);
+  fOutput->Add(hSwitchOnCandidates2);
+  TH1F *hSwitchOnCandidates3 = new TH1F("hSwitchOnCandidates3","",15,-7.5,7.5);
+  fOutput->Add(hSwitchOnCandidates3);
+  TH1F *hSwitchOnCandidates4 = new TH1F("hSwitchOnCandidates4","",15,-7.5,7.5);
+  fOutput->Add(hSwitchOnCandidates4);
 
-    TH2F* allspectrumLcMassOfflineByK0SSgn = (TH2F*)spectrumLcMassOfflineByK0SSgn->Clone(); 
-    TH2F* allspectrumLcMassOfflineByK0SBkg = (TH2F*) spectrumLcMassOfflineByK0SBkg->Clone();  
-    TH2F* pidBachspectrumLcMassOfflineByK0SSgn = (TH2F*)spectrumLcMassOfflineByK0SSgn->Clone(); 
-    TH2F* pidBachspectrumLcMassOfflineByK0SBkg = (TH2F*) spectrumLcMassOfflineByK0SBkg->Clone();  
-    fOutputAll->Add(allspectrumLcMassOfflineByK0SSgn);
-    fOutputAll->Add(allspectrumLcMassOfflineByK0SBkg);
-    fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0SSgn);
-    fOutputPIDBach->Add(pidBachspectrumLcMassOfflineByK0SBkg);
-  
+  return;
+}
 
-    /*
-    nameSgn="histcosOAK0SpSgnvspOffline";
-    nameBkg="histcosOAK0SpBkgvspOffline";
-    TH2F* cosOpeningAngleK0SpSgnvspOffline= new TH2F(nameSgn.Data(),"#Lambda_{C} SGN : K^{0}_{S} - p Opening Angle  vs p  -  Offline  - MC ; Cos(Opening Angle);  p ",
-                                                    200,-1.,1.,200,0.,20.);
-    TH2F* cosOpeningAngleK0SpBkgvspOffline= new TH2F(nameBkg.Data(),"#Lambda_{C} BKG : K^{0}_{S} - p Opening Angle  vs p  -  Offline  - MC;  Cos(Opening Angle);  p ",
-                                                    200,-1.,1.,200,0.,20.);
-    
+//________________________________________________________________________
+void  AliAnalysisTaskSELc2V0bachelor::DefineAnalysisHistograms() {
+  //
+  // This is to define analysis histograms
+  //
 
-    TH2F* allcosOpeningAngleK0SpSgnvspOffline= (TH2F*)cosOpeningAngleK0SpSgnvspOffline->Clone(); 
-    TH2F* allcosOpeningAngleK0SpBkgvspOffline= (TH2F*)cosOpeningAngleK0SpBkgvspOffline->Clone(); 
-    TH2F* pidBachcosOpeningAngleK0SpSgnvspOffline= (TH2F*)cosOpeningAngleK0SpSgnvspOffline->Clone(); 
-    TH2F* pidBachcosOpeningAngleK0SpBkgvspOffline= (TH2F*)cosOpeningAngleK0SpBkgvspOffline->Clone(); 
-    fOutputAll->Add(allcosOpeningAngleK0SpSgnvspOffline); 
-    fOutputAll->Add(allcosOpeningAngleK0SpBkgvspOffline); 
-    fOutputPIDBach->Add(pidBachcosOpeningAngleK0SpSgnvspOffline); 
-    fOutputPIDBach->Add(pidBachcosOpeningAngleK0SpBkgvspOffline); 
-    */
+  if (fIsK0SAnalysis) DefineK0SHistos();// hK0S histos declarations
 
+  return;
+}
 
+//________________________________________________________________________
+void  AliAnalysisTaskSELc2V0bachelor::FillAnalysisHistograms(AliAODRecoCascadeHF *part, Bool_t isBachelorID, TString appendthis) {
+  //
+  // This is to fill analysis histograms
+  //
 
-    nameSgn="histpK0SvspOfflineSgn";
-    nameBkg="histpK0SvspOfflineBkg";
-    TH2F* momentumDistributionK0SvspOfflineSgn= new TH2F(nameSgn.Data(),"#Lambda_{C} SGN : K^{0}_{S} vs p Total Momentum Distribution - Offline  - MC; p_{p};  p_{K^{0}_{S}}",
-                                                        200,0.,20.,200,0.,20.);
-    TH2F* momentumDistributionK0SvspOfflineBkg= new TH2F(nameBkg.Data(),"#Lambda_{C} BKG : K^{0}_{S} vs p Total Momentum Distribution - Offline  - MC;  p_{p}; p_{K^{0}_{S}}",
-                                                        200,0.,20.,200,0.,20.);
+  TString fillthis="";
 
+  Double_t invmassLc = part->InvMassLctoK0sP();
+  Double_t lambdacpt = part->Pt();
 
-    TH2F* allmomentumDistributionK0SvspOfflineSgn= (TH2F*)momentumDistributionK0SvspOfflineSgn->Clone(); 
-    TH2F* allmomentumDistributionK0SvspOfflineBkg= (TH2F*)momentumDistributionK0SvspOfflineBkg->Clone(); 
-    TH2F* pidBachmomentumDistributionK0SvspOfflineSgn= (TH2F*)momentumDistributionK0SvspOfflineSgn->Clone(); 
-    TH2F* pidBachmomentumDistributionK0SvspOfflineBkg= (TH2F*)momentumDistributionK0SvspOfflineBkg->Clone(); 
-    fOutputAll->Add(allmomentumDistributionK0SvspOfflineSgn); 
-    fOutputAll->Add(allmomentumDistributionK0SvspOfflineBkg); 
-    fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOfflineSgn); 
-    fOutputPIDBach->Add(pidBachmomentumDistributionK0SvspOfflineBkg); 
+  AliAODTrack *bachelor = (AliAODTrack*)part->GetBachelor();
+  Double_t momBach  = bachelor->P();
 
+  AliAODv0 * v0part = (AliAODv0*)part->Getv0();
+  Double_t momK0S = v0part->P();
+  Double_t ptK0S = v0part->Pt();
+  Double_t dcaV0ptp = v0part->GetDCA();
+  Double_t invmassK0S = v0part->MassK0Short();
 
+    fillthis="histK0SMass"+appendthis;
+    cout << fillthis << endl;
+    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassK0S,ptK0S);
+    if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassK0S,ptK0S);
 
+    fillthis="histpK0Svsp"+appendthis;
+    cout << fillthis << endl;
+    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momBach,momK0S);
+    if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momBach,momK0S);
 
+    fillthis="histDCAtoPVvspK0S"+appendthis;
+    cout << fillthis << endl;
+    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(momK0S,dcaV0ptp);
+    if (isBachelorID)  ((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(momK0S,dcaV0ptp);
 
-    // armenteros-podolanski plots K0S (offline)
-    nameSgn="histOfflineArmPodK0sSgn";
-    nameBkg="histOfflineArmPodK0sBkg";
-    TH2F* armenterosPodK0sOffSgn = new TH2F(nameSgn.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (sgn) -offline-; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",200,-1.,1.,300,0.,0.3);
-    TH2F* armenterosPodK0sOffBkg = new TH2F(nameBkg.Data(),"K^{0}_{S}  Armenteros-Podolanski distribution (bkg) -offline-; #frac{p_{L}^{+}-p_{L}^{-}}{p_{L}^{+}+p_{L}^{-}}; p_{T}^{+} [GeV/c]",200,-1.,1.,300,0.,0.3);
-  
+    fillthis="histArmPodK0S"+appendthis;
+    cout << fillthis << endl;
+    FillArmPodDistribution(v0part,fillthis,fOutputAll);
+    if (isBachelorID) FillArmPodDistribution(v0part,fillthis,fOutputPIDBach);
 
-    TH2F* allArmenterosPodK0sOffSgn = (TH2F*)armenterosPodK0sOffSgn->Clone();
-    TH2F* allArmenterosPodK0sOffBkg = (TH2F*)armenterosPodK0sOffBkg->Clone();
-    TH2F* pidBachArmenterosPodK0sOffSgn = (TH2F*)armenterosPodK0sOffSgn->Clone();
-    TH2F* pidBachArmenterosPodK0sOffBkg = (TH2F*)armenterosPodK0sOffBkg->Clone();
+    fillthis="histLcMassByK0S"+appendthis;
+    cout << fillthis << endl;
+    ((TH2F*)(fOutputAll->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
+    if (isBachelorID)((TH2F*)(fOutputPIDBach->FindObject(fillthis)))->Fill(invmassLc,lambdacpt);
 
+    return;
+}
+//---------------------------
+Double_t AliAnalysisTaskSELc2V0bachelor::PropagateToDCA(AliAODv0 *v, AliAODTrack *bachelor, Double_t b,
+                                                       Double_t &xVtxLc, Double_t &yVtxLc, Double_t &zVtxLc,
+                                                       Double_t &pxVtxBachelor, Double_t &pyVtxBachelor, Double_t &pzVtxBachelor) {
+  //--------------------------------------------------------------------
+  // This function returns the DCA between the V0 and the track
+  // This is a copy of AliCascadeVertexer::PropagateToDCA(...) method
+  //--------------------------------------------------------------------
+
+  // Get AliExternalTrackParam out of the AliAODTracks                                                      
+  Double_t xyz[3], pxpypz[3], cv[21]; Short_t sign;
+  bachelor->PxPyPz(pxpypz);
+  bachelor->XvYvZv(xyz);
+  bachelor->GetCovarianceXYZPxPyPz(cv);
+  sign=bachelor->Charge();
+  AliExternalTrackParam *t = new AliExternalTrackParam(xyz,pxpypz,cv,sign);
+
+  Double_t alpha=t->GetAlpha(), cs1=TMath::Cos(alpha), sn1=TMath::Sin(alpha);
+  //Double_t alpha = GetAlpha(xyz,pxpypz), cs1=TMath::Cos(alpha), sn1=TMath::Sin(alpha);
+
+  // position and momentum of bachelor
+  Double_t x1=xyz[0], y1=xyz[1], z1=xyz[2];
+  Double_t px1=pxpypz[0], py1=pxpypz[1], pz1=pxpypz[2];
+
+  // position and momentum of V0
+  Double_t x2=v->DecayVertexV0X(),
+    y2=v->DecayVertexV0Y(),
+    z2=v->DecayVertexV0Z();
+  Double_t px2=v->Px(),
+    py2=v->Py(),
+    pz2=v->Pz();
 
-    fOutputAll->Add(allArmenterosPodK0sOffSgn);
-    fOutputAll->Add(allArmenterosPodK0sOffBkg);
-    fOutputPIDBach->Add(pidBachArmenterosPodK0sOffSgn);
-    fOutputPIDBach->Add(pidBachArmenterosPodK0sOffBkg);
+  /*
+  AliAODTrack *trackP = (AliAODTrack*) v->GetDaughter(0);
+  //Double_t pxpypzP[3]; trackP->PxPyPz(pxpypzP);
+  //Double_t xyzP[3]; trackP->XvYvZv(xyzP);
+  Double_t cvP[21]; trackP->GetCovarianceXYZPxPyPz(cvP);
+  //Short_t signP=trackP->Charge();
+  //AliExternalTrackParam *tP = new AliExternalTrackParam(xyzP,pxpypzP,cvP,signP);
+
+  // Get AliExternalTrackParam out of the AliAODTrack
+  AliAODTrack *trackN = (AliAODTrack*) v->GetDaughter(1);
+  //Double_t pxpypzN[3]; trackN->PxPyPz(pxpypzN);
+  //Double_t xyzN[3]; trackN->XvYvZv(xyzN);
+  Double_t cvN[21]; trackN->GetCovarianceXYZPxPyPz(cvN);
+  //Short_t signN=trackN->Charge();
+  //AliExternalTrackParam *tN = new AliExternalTrackParam(xyzN,pxpypzN,cvN,signN);
+
+  Double_t xyzV0[3]={x2,y2,z2};
+  Double_t pxpypzV0[3]={px2,py2,pz2};
+  Double_t cvV0[21]; for (Int_t ii=0; ii<21; ii++) cvV0[ii]=cvP[ii]+cvN[ii];
+  AliNeutralTrackParam *trackV0 = new AliNeutralTrackParam(xyzV0,pxpypzV0,cvV0,0);
+  */
+  // calculation dca
+  Double_t dd= Det(x2-x1,y2-y1,z2-z1,px1,py1,pz1,px2,py2,pz2);
+  Double_t ax= Det(py1,pz1,py2,pz2);
+  Double_t ay=-Det(px1,pz1,px2,pz2);
+  Double_t az= Det(px1,py1,px2,py2);
+
+  Double_t dca=TMath::Abs(dd)/TMath::Sqrt(ax*ax + ay*ay + az*az);
+
+  // bachelor point @ the DCA
+  Double_t t1 = Det(x2-x1,y2-y1,z2-z1,px2,py2,pz2,ax,ay,az)/
+    Det(px1,py1,pz1,px2,py2,pz2,ax,ay,az);
+  x1 += px1*t1; y1 += py1*t1; z1 += pz1*t1;
+
+  //propagate track to the point of DCA
+  Double_t rho1=x1*cs1 + y1*sn1;
+  if (!t->PropagateTo(rho1,b)) {
+    Error("PropagateToDCA","Propagation failed !");
+    delete t; t=NULL;
+    return 1.e+33;
+  }
 
+  Double_t pBachelorDCA[3]; t->GetPxPyPz(pBachelorDCA);
+  pxVtxBachelor=pBachelorDCA[0], pyVtxBachelor=pBachelorDCA[1], pzVtxBachelor=pBachelorDCA[2];
+
+  delete t; t=NULL;
+
+  // V0 point @ the DCA
+  Double_t t2 = Det(x1-x2,y1-y2,z1-z2,px1,py1,pz1,ax,ay,az)/
+    Det(px2,py2,pz2,px1,py1,pz1,ax,ay,az);
+  x2 += px2*t2; y2 += py2*t2; z2 += pz2*t2;
 
-    nameSgn="histDCAtoPVvspK0SOfflineSgn";
-    nameBkg="histDCAtoPVvspK0SOfflineBkg";
-    TH2F *dcatoPVvspK0sOfflineSgn=new TH2F(nameSgn.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass  -offline - (sgn); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",200,0.,20.,100,0.,10.0);
-    TH2F *dcatoPVvspK0sOfflineBkg=new TH2F(nameBkg.Data(),"K^{0}_{S}: DCA to Primary Vertex vs  K^{0}_{S} invariant mass  -offline - (bkg); p(K^{0}_{S}) [GeV/c]; DCA to Primary Vertex []; Entries",200,0.,20.,100,0.,10.0);
-    
 
-    TH2F* alldcatoPVvspK0sOfflineSgn= (TH2F*)dcatoPVvspK0sOfflineSgn->Clone();
-    TH2F* pidBachdcatoPVvspK0sOfflineSgn= (TH2F*)dcatoPVvspK0sOfflineSgn->Clone();
-    TH2F* alldcatoPVvspK0sOfflineBkg= (TH2F*)dcatoPVvspK0sOfflineBkg->Clone();
-    TH2F* pidBachdcatoPVvspK0sOfflineBkg= (TH2F*)dcatoPVvspK0sOfflineBkg->Clone();
+  // Lc decay vtx
+  xVtxLc = 0.5*(x1+x2);
+  yVtxLc = 0.5*(y1+y2);
+  zVtxLc = 0.5*(z1+z2);
+  
+  return dca;
 
+}
 
+//---------------------------
+Double_t AliAnalysisTaskSELc2V0bachelor::GetAlpha(Double_t xyz[3],Double_t pxpypz[3])
+{
+  //
+  // To estimate alpha according to what done in the AliExternalTrackParam::Set(...) method
+  //
 
-    fOutputAll->Add(alldcatoPVvspK0sOfflineSgn);
-    fOutputPIDBach->Add(pidBachdcatoPVvspK0sOfflineSgn);
-    fOutputAll->Add(alldcatoPVvspK0sOfflineBkg);
-    fOutputPIDBach->Add(pidBachdcatoPVvspK0sOfflineBkg);
+  Double_t alpha = 0.;
+
+  const double kSafe = 1e-5;
+  Double_t radPos2 = xyz[0]*xyz[0]+xyz[1]*xyz[1];
+  Double_t radMax  = 45.; // approximately ITS outer radius
+  if (radPos2 < radMax*radMax) { // inside the ITS
+    alpha = TMath::ATan2(pxpypz[1],pxpypz[0]);
+  } else { // outside the ITS
+    Float_t phiPos = TMath::Pi()+TMath::ATan2(-xyz[1], -xyz[0]);
+     alpha =
+       TMath::DegToRad()*(20*((((Int_t)(phiPos*TMath::RadToDeg()))/20))+10);
+  }
 
+  Double_t cs=TMath::Cos(alpha), sn=TMath::Sin(alpha);
+  // protection: avoid alpha being too close to 0 or +-pi/2
+  if (TMath::Abs(sn)<2*kSafe) {
+    if (alpha>0) alpha += alpha< TMath::Pi()/2. ?  2*kSafe : -2*kSafe;
+    else         alpha += alpha>-TMath::Pi()/2. ? -2*kSafe :  2*kSafe;
+    cs=TMath::Cos(alpha);
+    sn=TMath::Sin(alpha);
   }
+  else if (TMath::Abs(cs)<2*kSafe) {
+    if (alpha>0) alpha += alpha> TMath::Pi()/2. ? 2*kSafe : -2*kSafe;
+    else         alpha += alpha>-TMath::Pi()/2. ? 2*kSafe : -2*kSafe;
+    cs=TMath::Cos(alpha);
+    sn=TMath::Sin(alpha);
+  }
+
 
+  return alpha;
+}
+
+//---------------------------
+Double_t AliAnalysisTaskSELc2V0bachelor::Det(Double_t a00, Double_t a01,
+                                            Double_t a10, Double_t a11) const {
+  //--------------------------------------------------------------------
+  // This function calculates locally a 2x2 determinant.
+  // This is a copy of the AliCascadeVertexer::Det(...) method
+  //--------------------------------------------------------------------
+  return a00*a11 - a01*a10;
+}
 
-  ///---------------- END  K0S HISTOS DECLARATIONS -------------------///
+//---------------------------
+Double_t AliAnalysisTaskSELc2V0bachelor::Det(Double_t a00,Double_t a01,Double_t a02,
+                                            Double_t a10,Double_t a11,Double_t a12,
+                                            Double_t a20,Double_t a21,Double_t a22) const {
+  //--------------------------------------------------------------------
+  // This function calculates locally a 3x3 determinant
+  // This is a copy of the AliCascadeVertexer::Det(...) method
+  //--------------------------------------------------------------------
+  return  a00*Det(a11,a12,a21,a22)-a01*Det(a10,a12,a20,a22)+a02*Det(a10,a11,a20,a21);
 }
+
index 425725e..7448551 100644 (file)
@@ -37,8 +37,8 @@ class AliAnalysisTaskSELc2V0bachelor : public AliAnalysisTaskSE
  public:
   
   AliAnalysisTaskSELc2V0bachelor();
-  AliAnalysisTaskSELc2V0bachelor(const Char_t* name, AliRDHFCutsLctoV0* cutsA, AliRDHFCutsLctoV0* cutsB,
-                                Bool_t useOnTheFly=kFALSE, Bool_t writeVariableTree=kTRUE);
+  AliAnalysisTaskSELc2V0bachelor(const Char_t* name, AliRDHFCutsLctoV0* cuts,
+                                Bool_t useOnTheFly=kFALSE, Bool_t writeVariableTree=kTRUE, Bool_t additionalChecks=kFALSE);
   virtual ~AliAnalysisTaskSELc2V0bachelor();
 
   // Implementation of interface methods  
@@ -50,65 +50,84 @@ class AliAnalysisTaskSELc2V0bachelor : public AliAnalysisTaskSE
  
   // histos
   void FillLc2pK0Sspectrum(AliAODRecoCascadeHF *part, Int_t isLc,
-                          Int_t &nSelectedProd, AliRDHFCutsLctoV0 *cutsProd,
                           Int_t &nSelectedAnal, AliRDHFCutsLctoV0 *cutsAnal,
                           TClonesArray *mcArray);
 
-  void DefineHistograms();
-
-  void MakeAnalysisForLc2prK0S(TClonesArray *arrayLctopK0s,
+  void MakeAnalysisForLc2prK0S(TClonesArray *arrayLctopK0S,
                               TClonesArray *mcArray,
-                              Int_t &nSelectedProd, AliRDHFCutsLctoV0 *cutsProd,
                               Int_t &nSelectedAnal, AliRDHFCutsLctoV0 *cutsAnal);
  
   // set MC usage
   void SetMC(Bool_t theMCon) {fUseMCInfo = theMCon;}
   Bool_t GetMC() const {return fUseMCInfo;}
 
+  // set flag for additional checks
+  void SetAdditionalChecks(Bool_t additionalChecks) {fAdditionalChecks = additionalChecks;}
+  Bool_t GetAdditionalChecks() const {return fAdditionalChecks;}
+
   void FillArmPodDistribution(AliAODv0 *vZero, TString histoTitle, TList *histoList);
 
-  void SetK0sAnalysis(Bool_t a) {fIsK0sAnalysis=a;}
-  Bool_t GetK0sAnalysis() const {return fIsK0sAnalysis;}
+  void SetK0SAnalysis(Bool_t a) {fIsK0SAnalysis=a;}
+  Bool_t GetK0SAnalysis() const {return fIsK0SAnalysis;}
 
   void SetUseOnTheFlyV0(Bool_t a) { fUseOnTheFlyV0=a; }
   Bool_t GetUseOnTheFlyV0() { return fUseOnTheFlyV0; }
 
  private:
   
+  void CheckEventSelection(AliAODEvent *aodEvent);
+  void CheckEventSelectionWithCandidates(AliAODEvent *aodEvent);
+  void CheckCandidatesAtDifferentLevels(AliAODRecoCascadeHF *part,AliRDHFCutsLctoV0* cutsAnal);
+  void FillTheTree(AliAODRecoCascadeHF *part, AliRDHFCutsLctoV0 *cutsAnal, TClonesArray *mcArray, Int_t isLc);
+  void DefineTreeVariables();
+
   Int_t MatchToMC(AliAODRecoCascadeHF *lc2bacV0,
                  Int_t *pdgDgLc2bacV0, Int_t *pdgDgV0,
                  TClonesArray *mcArray);
 
   Int_t SearchLcDaughter(TClonesArray *arrayMC, Int_t iii);
 
+  void DefineGeneralHistograms();
+  void DefineAnalysisHistograms();
   void DefineK0SHistos();
+  void  FillAnalysisHistograms(AliAODRecoCascadeHF *part, Bool_t isBachelorID, TString appendthis);
 
   AliAnalysisTaskSELc2V0bachelor(const AliAnalysisTaskSELc2V0bachelor &source);
   AliAnalysisTaskSELc2V0bachelor& operator=(const AliAnalysisTaskSELc2V0bachelor& source); 
   
+  Double_t Det(Double_t a00,Double_t a01,
+              Double_t a10,Double_t a11) const;
+  Double_t Det(Double_t a00,Double_t a01,Double_t a02,
+              Double_t a10,Double_t a11,Double_t a12,
+              Double_t a20,Double_t a21,Double_t a22) const;
+  Double_t PropagateToDCA(AliAODv0 *v, AliAODTrack *bachelor, Double_t b,
+                         Double_t &xVtxLc, Double_t &yVtxLc, Double_t &zVtxLc,
+                         Double_t &pxVtxLc, Double_t &pyVtxLc, Double_t &pzVtxLc);
+
+  Double_t GetAlpha(Double_t xyz[3],Double_t pxpypz[3]);
+
   Bool_t fUseMCInfo;          // Use MC info
-  TList *fOutput;             // User output1 // general histos
-  TList *fOutputAll;          // User output2 // histos without pid and cut on V0
-  TList *fOutputPIDBach;      // User output3 // histos with PID on Bachelor
+  TList *fOutput;             // User output slot 1 // general histos
+  TList *fOutputAll;          // User output slot 4 // histos without pid and cut on V0
+  TList *fOutputPIDBach;      // User output slot 5 // histos with PID on Bachelor
 
-  // define the histograms
   TH1F *fCEvents;                    // Histogram to check selected events
   AliPIDResponse *fPIDResponse;      //! PID response object
-  Bool_t fIsK0sAnalysis;             // switch between Lpi and K0sp
-  AliNormalizationCounter *fCounter; // AliNormalizationCounter on output slot 4
-  AliRDHFCutsLctoV0 *fProdCuts;      // Cuts - sent to output slot 5
-  AliRDHFCutsLctoV0 *fAnalCuts;      // Cuts - sent to output slot 5
-  TList *fListCuts;                  // list of cuts
+  Bool_t fIsK0SAnalysis;             // switch between Lpi and K0Sp
+  AliNormalizationCounter *fCounter; // AliNormalizationCounter on output slot 2
+  AliRDHFCutsLctoV0 *fAnalCuts;      // Cuts - sent to output slot 3
+  //TList *fListCuts;                  // list of cuts
   Bool_t fUseOnTheFlyV0;             // flag to analyze also on-the-fly V0 candidates
   Bool_t fIsEventSelected;           // flag for event selected
 
   Bool_t    fWriteVariableTree;       // flag to decide whether to write the candidate variables on a tree variables
-  TTree    *fVariablesTree;           //! tree of the candidate variables after track selection on output slot 6
+  TTree    *fVariablesTree;           //! tree of the candidate variables after track selection on output slot 4
   Float_t *fCandidateVariables;       //! variables to be written to the tree
   AliAODVertex *fVtx1;                // primary vertex
   Float_t fBzkG;                      // magnetic field value [kG]
+  Bool_t fAdditionalChecks;           // flag to fill additional histograms
 
-  ClassDef(AliAnalysisTaskSELc2V0bachelor,3); // class for Lc->p K0
+  ClassDef(AliAnalysisTaskSELc2V0bachelor,4); // class for Lc->p K0
 };
 
 #endif
index a2838bd..f8b2a0e 100644 (file)
@@ -1,7 +1,9 @@
 AliAnalysisTaskSELc2V0bachelor *AddTaskLc2V0bachelor(TString finname="Lc2V0bachelorCuts.root",
                                                     Bool_t theMCon=kFALSE,
                                                     Bool_t onTheFly=kFALSE,
-                                                    Bool_t writeVariableTree=kTRUE)
+                                                    Bool_t writeVariableTree=kTRUE,
+                                                    Int_t nTour=0,
+                                                    Bool_t additionalChecks=kFALSE)
 
 {
 
@@ -25,13 +27,7 @@ AliAnalysisTaskSELc2V0bachelor *AddTaskLc2V0bachelor(TString finname="Lc2V0bache
       }
   }
 
-  AliRDHFCutsLctoV0* RDHFCutsLctoV0prod = new AliRDHFCutsLctoV0();
-  if (stdcuts) RDHFCutsLctoV0prod->SetStandardCutsPP2010();
-  else RDHFCutsLctoV0prod = (AliRDHFCutsLctoV0*)filecuts->Get("LctoV0ProductionCuts");
-  RDHFCutsLctoV0prod->SetName("LctoV0ProductionCuts");
-  RDHFCutsLctoV0prod->SetMinPtCandidate(-1.);
-  RDHFCutsLctoV0prod->SetMaxPtCandidate(10000.);
-
+  cout << " Is it std on not std cut? " << stdcuts << endl;
   AliRDHFCutsLctoV0* RDHFCutsLctoV0anal = new AliRDHFCutsLctoV0();
   if (stdcuts) RDHFCutsLctoV0anal->SetStandardCutsPP2010();
   else RDHFCutsLctoV0anal = (AliRDHFCutsLctoV0*)filecuts->Get("LctoV0AnalysisCuts");
@@ -41,7 +37,7 @@ AliAnalysisTaskSELc2V0bachelor *AddTaskLc2V0bachelor(TString finname="Lc2V0bache
 
 
   // mm let's see if everything is ok
-  if (!RDHFCutsLctoV0prod || !RDHFCutsLctoV0anal) {
+  if (!RDHFCutsLctoV0anal) {
     cout << "Specific AliRDHFCutsLctoV0 not found\n";
     return;
   }
@@ -50,33 +46,35 @@ AliAnalysisTaskSELc2V0bachelor *AddTaskLc2V0bachelor(TString finname="Lc2V0bache
   //CREATE THE TASK
 
   printf("CREATE TASK\n");
-  AliAnalysisTaskSELc2V0bachelor *task = new AliAnalysisTaskSELc2V0bachelor("AliAnalysisTaskSELc2V0bachelor",RDHFCutsLctoV0prod,RDHFCutsLctoV0anal,onTheFly,writeVariableTree);
+  AliAnalysisTaskSELc2V0bachelor *task = new AliAnalysisTaskSELc2V0bachelor("AliAnalysisTaskSELc2V0bachelor",RDHFCutsLctoV0anal,onTheFly,writeVariableTree);
+  task->SetAdditionalChecks(additionalChecks);
   task->SetMC(theMCon);
-  task->SetK0sAnalysis(kTRUE);
+  task->SetK0SAnalysis(kTRUE);
   task->SetDebugLevel(0);
   mgr->AddTask(task);
 
   // Create and connect containers for input/output  
   TString outputfile = AliAnalysisManager::GetCommonFileName();
-  outputfile += ":PWG3_D2H_Lc2pK0S";
-
+  outputfile += ":PWG3_D2H_Lc2pK0S_";
+  outputfile += nTour;
 
   mgr->ConnectInput(task,0,mgr->GetCommonInputContainer());
 
   // ----- output data -----
-  AliAnalysisDataContainer *coutput1   = mgr->CreateContainer("chist1",TList::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // general histos
+  AliAnalysisDataContainer *coutput1   = mgr->CreateContainer(Form("chist%1d",nTour),TList::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // general histos
   mgr->ConnectOutput(task,1,coutput1);
-  AliAnalysisDataContainer *coutputLc1 = mgr->CreateContainer("Lc2pK0SAll",TList::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // production histos
-  mgr->ConnectOutput(task,2,coutputLc1);
-  AliAnalysisDataContainer *coutputLc2 = mgr->CreateContainer("Lc2pK0SPIDBach",TList::Class(),AliAnalysisManager::kOutputContainer, outputfile.Data()); // analysis histos
-  mgr->ConnectOutput(task,3,coutputLc2);
-  AliAnalysisDataContainer *coutputLc3 = mgr->CreateContainer("Lc2pK0Scounter",AliNormalizationCounter::Class(),AliAnalysisManager::kOutputContainer, outputfile.Data()); //counter
-  mgr->ConnectOutput(task,4,coutputLc3);
-  AliAnalysisDataContainer *coutputLc4 = mgr->CreateContainer("Lc2pK0SCuts",TList::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // cuts
-  mgr->ConnectOutput(task,5,coutputLc4);
-  if (writeVariableTree) {
-    AliAnalysisDataContainer *coutputLc5 = mgr->CreateContainer("Lc2pK0Svariables",TTree::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // variables tree
-    mgr->ConnectOutput(task,6,coutputLc5);
+  AliAnalysisDataContainer *coutputLc2 = mgr->CreateContainer(Form("Lc2pK0Scounter%1d",nTour),AliNormalizationCounter::Class(),AliAnalysisManager::kOutputContainer, outputfile.Data()); //counter
+  mgr->ConnectOutput(task,2,coutputLc2);
+  AliAnalysisDataContainer *coutputLc3 = mgr->CreateContainer(Form("Lc2pK0SCuts%1d",nTour),AliRDHFCutsLctoV0::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // cuts
+  mgr->ConnectOutput(task,3,coutputLc3);
+  if (!writeVariableTree) {
+    AliAnalysisDataContainer *coutputLc4 = mgr->CreateContainer(Form("Lc2pK0SAll%1d",nTour),TList::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // production histos
+    mgr->ConnectOutput(task,4,coutputLc4);
+    AliAnalysisDataContainer *coutputLc5 = mgr->CreateContainer(Form("Lc2pK0SPIDBach%1d",nTour),TList::Class(),AliAnalysisManager::kOutputContainer, outputfile.Data()); // analysis histos
+    mgr->ConnectOutput(task,5,coutputLc5);
+  } else {
+    AliAnalysisDataContainer *coutputLc4 = mgr->CreateContainer(Form("Lc2pK0Svariables%1d",nTour),TTree::Class(),AliAnalysisManager::kOutputContainer,outputfile.Data()); // variables tree
+    mgr->ConnectOutput(task,4,coutputLc4);
   }
 
   return task;