]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG3/vertexingHF/AliCFHeavyFlavourTaskMultiVarMultiStep.cxx
Add selection on track filter mask in D meson tasks
[u/mrichter/AliRoot.git] / PWG3 / vertexingHF / AliCFHeavyFlavourTaskMultiVarMultiStep.cxx
index b8a27ac296b5325005bbc6cd2fc162ea78b175c5..df07265b1295ca6bc9694e0d6a05c3472f7c81a4 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 //-----------------------------------------------------------------------
 // Class for HF corrections as a function of many variables
 // 6 Steps introduced: MC, MC Acc, Reco, Reco Acc, Reco Acc + ITS Cl, 
 // Reco Acc + ITS Cl + PPR cuts
-// 11 variables used: pt, y, cosThetaStar, ptPi, ptK, ct,
-// dca, d0Pi, d0K, d0Pixd0K, cosPointingAngle
+// 13 variables used: pt, y, cosThetaStar, ptPi, ptK, ct,
+// dca, d0Pi, d0K, d0Pixd0K, cosPointingAngle, phi, z
 //
 //-----------------------------------------------------------------------
 // Author : C. Zampolli, CERN
 #include "AliCFManager.h"
 #include "AliCFContainer.h"
 #include "AliLog.h"
+#include "AliAnalysisManager.h"
+#include "AliAODHandler.h"
 #include "AliAODEvent.h"
 #include "AliAODRecoDecay.h"
 #include "AliAODRecoDecayHF.h"
 #include "AliAODRecoDecayHF2Prong.h"
 #include "AliAODMCParticle.h"
+#include "AliAODMCHeader.h"
+#include "AliESDtrack.h"
+#include "AliRDHFCutsD0toKpi.h"
 #include "TChain.h"
 #include "THnSparse.h"
 #include "TH2D.h"
+#include "AliAnalysisDataSlot.h"
+#include "AliAnalysisDataContainer.h"
+
 //__________________________________________________________________________
 AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep() :
        AliAnalysisTaskSE(),
@@ -58,21 +68,30 @@ AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep()
         fCorrelation(0x0),
        fCountMC(0),
        fCountAcc(0),
+       fCountVertex(0),
+       fCountRefit(0),
        fCountReco(0),
        fCountRecoAcc(0),
        fCountRecoITSClusters(0),
        fCountRecoPPR(0),
+       fCountRecoPID(0),
        fEvents(0),
        fFillFromGenerated(kFALSE),
        fMinITSClusters(5),
-        fAcceptanceUnf(kTRUE)
+        fAcceptanceUnf(kTRUE),
+       fKeepD0fromB(kFALSE),
+       fKeepD0fromBOnly(kFALSE),
+       fCuts(0),
+       fUseWeight(kFALSE),
+       fWeight(1.),
+       fSign(2)
 {
        //
        //Default ctor
        //
 }
 //___________________________________________________________________________
-AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep(const Char_t* name) :
+AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep(const Char_t* name, AliRDHFCutsD0toKpi* cuts) :
        AliAnalysisTaskSE(name),
        fPDG(0),
        fCFManager(0x0),
@@ -80,14 +99,23 @@ AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep(c
         fCorrelation(0x0),
        fCountMC(0),
        fCountAcc(0),
+       fCountVertex(0),
+       fCountRefit(0),
        fCountReco(0),
        fCountRecoAcc(0),
        fCountRecoITSClusters(0),
        fCountRecoPPR(0),
+       fCountRecoPID(0),
        fEvents(0),
        fFillFromGenerated(kFALSE),
        fMinITSClusters(5),
-        fAcceptanceUnf(kTRUE)
+        fAcceptanceUnf(kTRUE),
+       fKeepD0fromB(kFALSE),
+        fKeepD0fromBOnly(kFALSE),
+       fCuts(cuts),
+       fUseWeight(kFALSE),
+       fWeight(1.),
+       fSign(2)
 {
        //
        // Constructor. Initialization of Inputs and Outputs
@@ -100,6 +128,9 @@ AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep(c
        DefineOutput(1,TH1I::Class());
        DefineOutput(2,AliCFContainer::Class());
         DefineOutput(3,THnSparseD::Class());
+        DefineOutput(4,AliRDHFCutsD0toKpi::Class());
+
+       fCuts->PrintAll();
 }
 
 //___________________________________________________________________________
@@ -113,6 +144,7 @@ AliCFHeavyFlavourTaskMultiVarMultiStep& AliCFHeavyFlavourTaskMultiVarMultiStep::
                fPDG      = c.fPDG;
                fCFManager  = c.fCFManager;
                fHistEventsProcessed = c.fHistEventsProcessed;
+               fCuts = c.fCuts;
        }
        return *this;
 }
@@ -126,14 +158,23 @@ AliCFHeavyFlavourTaskMultiVarMultiStep::AliCFHeavyFlavourTaskMultiVarMultiStep(c
         fCorrelation(c.fCorrelation),
        fCountMC(c.fCountMC),
        fCountAcc(c.fCountAcc),
+       fCountVertex(c.fCountVertex),
+       fCountRefit(c.fCountRefit),
        fCountReco(c.fCountReco),
        fCountRecoAcc(c.fCountRecoAcc),
        fCountRecoITSClusters(c.fCountRecoITSClusters),
        fCountRecoPPR(c.fCountRecoPPR),
+       fCountRecoPID(c.fCountRecoPID),
        fEvents(c.fEvents),
        fFillFromGenerated(c.fFillFromGenerated),
        fMinITSClusters(c.fMinITSClusters),
-        fAcceptanceUnf(c.fAcceptanceUnf)
+        fAcceptanceUnf(c.fAcceptanceUnf),
+       fKeepD0fromB(c.fKeepD0fromB),
+        fKeepD0fromBOnly(c.fKeepD0fromBOnly),
+       fCuts(c.fCuts),
+       fUseWeight(c.fUseWeight),
+       fWeight(c.fWeight),
+       fSign(c.fSign)
 {
        //
        // Copy Constructor
@@ -148,6 +189,26 @@ AliCFHeavyFlavourTaskMultiVarMultiStep::~AliCFHeavyFlavourTaskMultiVarMultiStep(
        if (fCFManager)           delete fCFManager ;
        if (fHistEventsProcessed) delete fHistEventsProcessed ;
         if (fCorrelation) delete fCorrelation ;
+       //        if (fCuts) delete fCuts ;
+}
+
+//________________________________________________________________________
+void AliCFHeavyFlavourTaskMultiVarMultiStep::Init(){
+
+       //
+       // Initialization
+       //
+
+       if(fDebug > 1) printf("AliCFHeavyFlavourTaskMultiVarMultiStep::Init() \n");
+       
+       fMinITSClusters = fCuts->GetTrackCuts()->GetMinNClustersITS();
+       AliRDHFCutsD0toKpi* copyfCuts=new AliRDHFCutsD0toKpi(*fCuts);
+       const char* nameoutput=GetOutputSlot(4)->GetContainer()->GetName();
+       copyfCuts->SetName(nameoutput);
+       // Post the data
+       PostData(4,copyfCuts);
+       
+       return;
 }
 
 //_________________________________________________
@@ -157,6 +218,12 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
        // Main loop function
        //
        
+       PostData(1,fHistEventsProcessed) ;
+       PostData(2,fCFManager->GetParticleContainer()) ;
+        PostData(3,fCorrelation) ;
+
+       AliESDtrackCuts* trackCuts = fCuts->GetTrackCuts(); // track cuts
+
        if (fFillFromGenerated){
                AliWarning("Flag to fill container with generated value ON ---> dca, d0pi, d0K, d0xd0, cosPointingAngle will be set as dummy!");
        }
@@ -166,35 +233,97 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                return;
        }
        
-       fEvents++;
-       if (fEvents%10000 ==0) AliInfo(Form("Event %d",fEvents));
+       // check that the fKeepD0fromB flag is set to true when the fKeepD0fromBOnly flag is true
+       if(fKeepD0fromBOnly) { 
+         fKeepD0fromB=true;   
+         if(fEvents<2) AliInfo(Form("Both fKeepD0fromB and fKeepD0fromBOnly flags are true, looking _ONLY_ at D0 FROM B"));
+       }
+
        AliAODEvent* aodEvent = dynamic_cast<AliAODEvent*>(fInputEvent);
-       fCFManager->SetEventInfo(aodEvent);
+
+       TClonesArray *arrayD0toKpi=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());
+         if(aodHandler->GetExtensions()) {
+           AliAODExtension *ext = (AliAODExtension*)aodHandler->GetExtensions()->FindObject("AliAOD.VertexingHF.root");
+           AliAODEvent *aodFromExt = ext->GetAOD();
+           arrayD0toKpi=(TClonesArray*)aodFromExt->GetList()->FindObject("D0toKpi");
+         }
+       } else {
+         arrayD0toKpi=(TClonesArray*)aodEvent->GetList()->FindObject("D0toKpi");
+       }
+
+
+       if (!arrayD0toKpi) {
+         AliError("Could not find array of HF vertices");
+         return;
+       }
+
+       // fix for temporary bug in ESDfilter 
+       // the AODs with null vertex pointer didn't pass the PhysSel
+       if(!aodEvent->GetPrimaryVertex() || TMath::Abs(aodEvent->GetMagneticField())<0.001) return;
+
+       fEvents++;
+
+       fCFManager->SetRecEventInfo(aodEvent);
+       fCFManager->SetMCEventInfo(aodEvent);
        
        // MC-event selection
-       Double_t containerInput[11] ;
+       Double_t containerInput[13] ;
+       Double_t containerInputMC[13] ;
         
        //loop on the MC event
        
        TClonesArray* mcArray = dynamic_cast<TClonesArray*>(aodEvent->FindListObject(AliAODMCParticle::StdBranchName()));
-       if (!mcArray) AliError("Could not find Monte-Carlo in AOD");
+       if (!mcArray) {
+               AliError("Could not find Monte-Carlo in AOD");
+               return;
+       }
        Int_t icountMC = 0;
        Int_t icountAcc = 0;
        Int_t icountReco = 0;
+       Int_t icountVertex = 0;
+       Int_t icountRefit = 0;
        Int_t icountRecoAcc = 0;
        Int_t icountRecoITSClusters = 0;
        Int_t icountRecoPPR = 0;
+       Int_t icountRecoPID = 0;
        
+       AliAODMCHeader *mcHeader = dynamic_cast<AliAODMCHeader*>(aodEvent->GetList()->FindObject(AliAODMCHeader::StdBranchName()));
+       if (!mcHeader) {
+               AliError("Could not find MC Header in AOD");
+               return;
+       }
+
        Int_t cquarks = 0;
                
+       // AOD primary vertex
+       AliAODVertex *vtx1 = (AliAODVertex*)aodEvent->GetPrimaryVertex();
+       if(!vtx1) { 
+         AliError("There is no primary vertex !"); 
+         return; 
+       }
+       Double_t zPrimVertex = vtx1->GetZ();
+       Double_t zMCVertex = mcHeader->GetVtxZ();
+       Bool_t vtxFlag = kTRUE;
+       TString title=vtx1->GetTitle();
+       if(!title.Contains("VertexerTracks")) vtxFlag=kFALSE;
+
        for (Int_t iPart=0; iPart<mcArray->GetEntriesFast(); iPart++) { 
                AliAODMCParticle* mcPart = dynamic_cast<AliAODMCParticle*>(mcArray->At(iPart));
-               if (mcPart->GetPdgCode() == 4) cquarks++; 
-               if (mcPart->GetPdgCode() == -4) cquarks++; 
                if (!mcPart) {
                        AliWarning("Particle not found in tree, skipping"); 
                        continue;
                } 
+               if (mcPart->GetPdgCode() == 4) cquarks++; 
+               if (mcPart->GetPdgCode() == -4) cquarks++; 
                
                // check the MC-level cuts
 
@@ -202,28 +331,37 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                Int_t pdgGranma = CheckOrigin(mcPart, mcArray);
                Int_t abspdgGranma = TMath::Abs(pdgGranma);
                if ((abspdgGranma > 500 && abspdgGranma < 600) || (abspdgGranma > 5000 && abspdgGranma < 6000)) {
-                       AliInfo(Form("Particle has a b-meson, or b-baryon mother (pdg code mother = %d )--> not coming from a c-quark, skipping...", pdgGranma));
-                       continue;  // skipping particles that don't come from c quark
+                       AliDebug(2,Form("Particle has a b-meson, or b-baryon mother (pdg code mother = %d )--> not coming from a c-quark, skipping...", pdgGranma));
+                       if (!fKeepD0fromB) continue;  // skipping particles that don't come from c quark
                }
+               else { if(fKeepD0fromBOnly) continue; } // skipping particles that don't come from b quark
                
                //              if (TMath::Abs(pdgGranma)!=4) {
 
                // fill the container for Gen-level selection
                Double_t vectorMC[7] = {9999.,9999.,9999.,9999.,9999.,9999.,9999.};
+
                if (GetGeneratedValuesFromMCParticle(mcPart, mcArray, vectorMC)){
-                       containerInput[0] = vectorMC[0];
-                       containerInput[1] = vectorMC[1] ;
-                       containerInput[2] = vectorMC[2] ;
-                       containerInput[3] = vectorMC[3] ;
-                       containerInput[4] = vectorMC[4] ;
-                       containerInput[5] = vectorMC[5] ;  // in micron
-                       containerInput[6] = 0.;    // dummy value, meaningless in MC, in micron
-                       containerInput[7] = 0.;   // dummy value, meaningless in MC, in micron
-                       containerInput[8] = 0.;   // dummy value, meaningless in MC, in micron
-                       containerInput[9] = -100000.; // dummy value, meaningless in MC, in micron^2
-                       containerInput[10] = 1.01;    // dummy value, meaningless in MC
-                       containerInput[11] = vectorMC[6];    // dummy value, meaningless in MC
-                       fCFManager->GetParticleContainer()->Fill(containerInput,kStepGenerated);
+                       containerInputMC[0] = vectorMC[0];
+                       containerInputMC[1] = vectorMC[1] ;
+                       containerInputMC[2] = vectorMC[2] ;
+                       containerInputMC[3] = vectorMC[3] ;
+                       containerInputMC[4] = vectorMC[4] ;
+                       containerInputMC[5] = vectorMC[5] ;  // in micron
+                       containerInputMC[6] = 0.;    // dummy value, meaningless in MC, in micron
+                       containerInputMC[7] = 0.;   // dummy value, meaningless in MC, in micron
+                       containerInputMC[8] = 0.;   // dummy value, meaningless in MC, in micron
+                       containerInputMC[9] = -100000.; // dummy value, meaningless in MC, in micron^2
+                       containerInputMC[10] = 1.01;    // dummy value, meaningless in MC
+                       containerInputMC[11] = vectorMC[6];    // dummy value, meaningless in MC
+                       containerInputMC[12] = zMCVertex;    // z of reconstructed of primary vertex
+                       if (fUseWeight) fWeight = GetWeight(vectorMC[0]); // setting the weight according to the function defined in AliCFHeavyFlavourTaskMultiVarMultiStep::GetWeight(Float_t pt)
+                       AliDebug(3,Form("weight = %f",fWeight));
+                       if (!fCuts->IsInFiducialAcceptance(vectorMC[0],vectorMC[1])) continue;
+                       if (TMath::Abs(vectorMC[1]) < 0.5) {
+                               fCFManager->GetParticleContainer()->Fill(containerInputMC,kStepGeneratedLimAcc,fWeight);
+                       }
+                       fCFManager->GetParticleContainer()->Fill(containerInputMC,kStepGenerated,fWeight);
                        icountMC++;
 
                        // check the MC-Acceptance level cuts
@@ -242,6 +380,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                        AliAODMCParticle* mcPartDaughter1 = dynamic_cast<AliAODMCParticle*>(mcArray->At(daughter1));
                        if (!mcPartDaughter0 || !mcPartDaughter1) {
                                AliWarning("At least one Daughter Particle not found in tree, but it should be, this check was already done..."); 
+                               continue;
                        }
                        Double_t eta0 = mcPartDaughter0->Eta();
                        Double_t eta1 = mcPartDaughter1->Eta();
@@ -257,13 +396,63 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                // checking whether the cuts implemented in the CF are equivalent - simply a cross-check
                                AliDebug(2, "Daughter particles in acceptance");
                                if (!fCFManager->CheckParticleCuts(1, mcPartDaughter0)) {
-                                       AliInfo("Inconsistency with CF cut for daughter 0!");
+                                       AliDebug(2,"Inconsistency with CF cut for daughter 0!");
                                } 
                                if (!fCFManager->CheckParticleCuts(1, mcPartDaughter1)) {
-                                       AliInfo("Inconsistency with CF cut for daughter 1!");
+                                       AliDebug(2,"Inconsistency with CF cut for daughter 1!");
                                } 
-                               fCFManager->GetParticleContainer()->Fill(containerInput,kStepAcceptance);
+                               fCFManager->GetParticleContainer()->Fill(containerInputMC,kStepAcceptance,fWeight);
                                icountAcc++;
+
+                               // check on the vertex
+                               if (fCuts->IsEventSelected(aodEvent)){
+                                       AliDebug(2,"Vertex cut passed\n");
+                                       // filling the container if the vertex is ok
+                                       fCFManager->GetParticleContainer()->Fill(containerInputMC,kStepVertex,fWeight) ;
+                                       icountVertex++;
+                                       // check on the kTPCrefit and kITSrefit conditions of the daughters
+                                       Bool_t refitFlag = kTRUE;
+                                       if (trackCuts->GetRequireTPCRefit() || trackCuts->GetRequireITSRefit()){
+                                               Int_t foundDaughters = 0;
+                                               for (Int_t iaod =0; iaod<aodEvent->GetNumberOfTracks(); iaod++){
+                                                       AliAODTrack *track = (AliAODTrack*)aodEvent->GetTrack(iaod);
+                                                       if(track->GetStatus()&AliESDtrack::kITSpureSA) continue;
+                                                               if ((track->GetLabel() == daughter0) || (track->GetLabel() == daughter1)) {
+                                                               foundDaughters++;
+                                                               if (trackCuts->GetRequireTPCRefit()) {
+                                                                           if(!(track->GetStatus()&AliESDtrack::kTPCrefit)){
+                                                                                   refitFlag = kFALSE;
+                                                                                   break;
+                                                                           }
+                                                               }
+                                                               if (trackCuts->GetRequireITSRefit()) {
+                                                                           if(!(track->GetStatus()&AliESDtrack::kITSrefit)){
+                                                                                   refitFlag = kFALSE;
+                                                                                   break;
+                                                                           }
+                                                               }
+                                                       }
+                                                       if (foundDaughters == 2){  // both daughters have been checked
+                                                               break;
+                                                       }
+                                               }
+                                               if (foundDaughters != 2) refitFlag = kFALSE;
+                                       }
+                                       if (refitFlag){
+                                               AliDebug(3,"Refit cut passed\n");
+                                               fCFManager->GetParticleContainer()->Fill(containerInputMC,kStepRefit,fWeight);
+                                               icountRefit++;
+                                       }
+                                       else{
+                                               AliDebug(3,"Refit cut not passed\n");
+                                       }
+                               }
+                               else{
+                                       AliDebug(3,"Vertex cut not passed\n");
+                               }                       
+                       }
+                       else{
+                               AliDebug(3,"Acceptance cut not passed\n");
                        }
                }
                else {
@@ -271,26 +460,27 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                        continue;
                }
        }
-       if (cquarks<2) AliInfo(Form("Event found with %d c-quarks", cquarks));
+
+       if (cquarks<2) AliDebug(2,Form("Event found with %d c-quarks", cquarks));
        
-       AliDebug(2, Form("Found %i MC particles that are D0!!",icountMC));
-       AliDebug(2, Form("Found %i MC particles that are D0 and satisfy Acc cuts!!",icountAcc));
+       AliDebug(2,Form("Found %i MC particles that are D0!!",icountMC));
+       AliDebug(2,Form("Found %i MC particles that are D0 and satisfy Acc cuts!!",icountAcc));
+       AliDebug(2,Form("Found %i MC particles that are D0 and satisfy Vertex cuts!!",icountVertex));
+       AliDebug(2,Form("Found %i MC particles that are D0 and satisfy Refit cuts!!",icountRefit));
 
        // Now go to rec level
        fCountMC += icountMC;
        fCountAcc += icountAcc;
 
-       // AOD primary vertex
-       AliAODVertex *vtx1 = (AliAODVertex*)aodEvent->GetPrimaryVertex();
-
-       // load heavy flavour vertices
-       TClonesArray *arrayD0toKpi = (TClonesArray*)((aodEvent->GetList())->FindObject("D0toKpi"));     
-       if (!arrayD0toKpi) AliError("Could not find array of HF vertices");
        AliDebug(2, Form("Found %d vertices",arrayD0toKpi->GetEntriesFast()));
-       
+
+       Int_t pdgDgD0toKpi[2]={321,211};
+       Int_t isD0D0bar=1;// 1 for D0, 2 for D0bar, to be used for the PPR and PID selection steps
+
        for (Int_t iD0toKpi = 0; iD0toKpi<arrayD0toKpi->GetEntriesFast(); iD0toKpi++) {
                
                AliAODRecoDecayHF2Prong* d0tokpi = (AliAODRecoDecayHF2Prong*)arrayD0toKpi->At(iD0toKpi);
+               if(!d0tokpi) continue;
                Bool_t unsetvtx=kFALSE;
                if(!d0tokpi->GetOwnPrimaryVtx()) {
                  d0tokpi->SetOwnPrimaryVtx(vtx1); // needed to compute all variables
@@ -298,10 +488,11 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                }
 
                // find associated MC particle
-               Int_t mcLabel = d0tokpi->MatchToMC(421, mcArray) ;
+               Int_t mcLabel = d0tokpi->MatchToMC(421,mcArray,2,pdgDgD0toKpi) ;
                if (mcLabel == -1) 
                        {
                                AliDebug(2,"No MC particle found");
+                               continue;
                        }
                else {
                        AliAODMCParticle* mcVtxHF = (AliAODMCParticle*)mcArray->At(mcLabel);
@@ -309,7 +500,53 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                AliWarning("Could not find associated MC in AOD MC tree");
                                continue;
                        }
-                                       
+
+                       if (mcVtxHF->GetPdgCode() == 421){  // particle is D0
+                               if (fSign == 1){ // I ask for D0bar only
+                                       AliDebug(2,"particle is D0, I ask for D0bar only");
+                                       continue;
+                               }
+                               else{
+                                       isD0D0bar=1;
+                               }
+                       }
+                       else if (mcVtxHF->GetPdgCode()== -421){ // particle is D0bar
+                               if (fSign == 0){ // I ask for D0 only
+                                       AliDebug(2,"particle is D0bar, I ask for D0 only");
+                                       continue;
+                               }
+                               else{
+                                       isD0D0bar=2;
+                               }
+                       } 
+                       else continue;
+
+                       // check whether the daughters have kTPCrefit and kITSrefit set
+                       AliAODTrack *track0 = (AliAODTrack*)d0tokpi->GetDaughter(0);
+                       AliAODTrack *track1 = (AliAODTrack*)d0tokpi->GetDaughter(1);
+                       if( !track0 || !track1 ) {
+                         AliWarning("Could not find associated MC daughter tracks in AOD MC tree");
+                         continue;
+                       }
+                       if ((trackCuts->GetRequireTPCRefit() && (!(track0->GetStatus()&AliESDtrack::kTPCrefit) || !(track1->GetStatus()&AliESDtrack::kTPCrefit))) || 
+                           (trackCuts->GetRequireITSRefit() && (!(track0->GetStatus()&AliESDtrack::kITSrefit) || !(track1->GetStatus()&AliESDtrack::kITSrefit)))){
+                               // skipping if at least one daughter does not have kTPCrefit or kITSrefit, if they were required
+                               continue;
+                       }
+
+                       // check on the vertex -- could be moved outside the loop on the reconstructed D0... 
+                       if(!fCuts->IsEventSelected(aodEvent)) {
+                               // skipping cause vertex is not ok
+                               continue;
+                       }
+
+                        const Double_t d0tokpiCuts[9] = {0.3,999999.,1.1,0.,0.,999999.,999999.,999999.,0.};
+                        Int_t okD0, okD0bar;
+                       if (!(d0tokpi->SelectD0(&d0tokpiCuts[0],okD0,okD0bar))){
+                               // skipping candidate
+                               continue;
+                       } 
+
                        // check if associated MC v0 passes the cuts
                        if (!fCFManager->CheckParticleCuts(0 ,mcVtxHF)) {  // 0 stands for MC
                                AliDebug(2, "Skipping the particles due to cuts");
@@ -318,14 +555,15 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                        Int_t pdgGranma = CheckOrigin(mcVtxHF, mcArray);
                        Int_t abspdgGranma = TMath::Abs(pdgGranma);
                        if ((abspdgGranma > 500 && abspdgGranma < 600) || (abspdgGranma > 5000 && abspdgGranma < 6000)) {
-                               AliInfo(Form("At Reco level, from MC info: Particle has a b-meson, or b-baryon mother (pdg code mother = %d )--> not coming from a c-quark, skipping...", pdgGranma));
-                               continue;  // skipping particles that don't come from c quark
+                               AliDebug(2,Form("At Reco level, from MC info: Particle has a b-meson, or b-baryon mother (pdg code mother = %d )--> not coming from a c-quark, skipping...", pdgGranma));
+                               if (!fKeepD0fromB) continue;  // skipping particles that don't come from c quark
                        }
+                       else { if(fKeepD0fromBOnly) continue; } // skipping particles that don't come from b quark
 
                        // fill the container...
                        Double_t pt = d0tokpi->Pt();
                        Double_t rapidity = d0tokpi->YD0();
-                       
+                       Double_t invMass=0.;
                        Double_t cosThetaStar = 9999.;
                        Double_t pTpi = 0.;
                        Double_t pTK = 0.;
@@ -342,6 +580,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                pTK = d0tokpi->PtProng(1);
                                d0pi = d0tokpi->Getd0Prong(0);
                                d0K = d0tokpi->Getd0Prong(1);
+                               invMass=d0tokpi->InvMassD0();
                        }
                        else {
                                cosThetaStar = d0tokpi->CosThetaStarD0bar();
@@ -349,6 +588,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                pTK = d0tokpi->PtProng(0);
                                d0pi = d0tokpi->Getd0Prong(1);
                                d0K = d0tokpi->Getd0Prong(0);
+                               invMass=d0tokpi->InvMassD0bar();
                        }
 
                        Double_t cT = d0tokpi->CtD0();
@@ -367,6 +607,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                containerInput[9] = d0xd0*1.E8;  // in micron^2
                                containerInput[10] = cosPointingAngle;  // in micron
                                containerInput[11] = phi;  
+                               containerInput[12] = zPrimVertex;    // z of reconstructed of primary vertex
                        }
                        else {
                                // ... or with generated values                         
@@ -384,150 +625,93 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
                                        containerInput[9] = 100000.; // dummy value, meaningless in MC, in micron^2
                                        containerInput[10] = 1.01;    // dummy value, meaningless in MC
                                        containerInput[11] = vectorMC[6];   
+                                       containerInput[12] = zMCVertex;    // z of reconstructed of primary vertex
                                }
                                else {
                                        AliDebug(3,"Problems in filling the container");
                                        continue;
                                }
                        }
+
+                       if (!fCuts->IsInFiducialAcceptance(containerInput[0],containerInput[1])) continue; // fiducial region
+
                        AliDebug(2, Form("Filling the container with pt = %f, rapidity = %f, cosThetaStar = %f, pTpi = %f, pTK = %f, cT = %f", containerInput[0], containerInput[1], containerInput[2], containerInput[3], containerInput[4], containerInput[5]));
                        icountReco++;
-                       fCFManager->GetParticleContainer()->Fill(containerInput,kStepReconstructed) ;   
+                       AliDebug(2,Form("%d: filling RECO step\n",iD0toKpi));
+                       fCFManager->GetParticleContainer()->Fill(containerInput,kStepReconstructed,fWeight) ;   
 
                        // cut in acceptance
-                       Bool_t acceptanceProng0 = (TMath::Abs(d0tokpi->EtaProng(0))< 0.9 && d0tokpi->PtProng(0) > 0.1);
-                       Bool_t acceptanceProng1 = (TMath::Abs(d0tokpi->EtaProng(1))< 0.9 && d0tokpi->PtProng(1) > 0.1);
+                       Float_t etaCutMin, etaCutMax, ptCutMin, ptCutMax;
+                       trackCuts->GetEtaRange(etaCutMin,etaCutMax);
+                       trackCuts->GetPtRange(ptCutMin,ptCutMax);
+                       Bool_t acceptanceProng0 = (d0tokpi->EtaProng(0)>etaCutMin && d0tokpi->EtaProng(0)<etaCutMax && d0tokpi->PtProng(0) > ptCutMin && d0tokpi->PtProng(0) < ptCutMax);
+                       Bool_t acceptanceProng1 = (d0tokpi->EtaProng(1)>etaCutMin && d0tokpi->EtaProng(1)<etaCutMax && d0tokpi->PtProng(1) > ptCutMin && d0tokpi->PtProng(1) < ptCutMax);
                        if (acceptanceProng0 && acceptanceProng1) {
                                AliDebug(2,"D0 reco daughters in acceptance");
-                               fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoAcceptance) ;
+                               fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoAcceptance,fWeight) ;
                                icountRecoAcc++; 
-
-                                if(fAcceptanceUnf){
-
-                                   Double_t fill[4]; //fill response matrix
-                                   // dimensions 0&1 : pt,eta (Rec)
-
-                                   fill[0] = pt ;
-                                   fill[1] = rapidity;
-                                   // dimensions 2&3 : pt,eta (MC)
-
-                                   fill[2] = mcVtxHF->Pt();
-                                   fill[3] = mcVtxHF->Y();
-
-                                   fCorrelation->Fill(fill);
-
-                                }  
-
+                               
+                               if(fAcceptanceUnf){
+                                       
+                                       Double_t fill[4]; //fill response matrix
+                                       
+                                       // dimensions 0&1 : pt,eta (Rec)
+                                       
+                                       fill[0] = pt ;
+                                       fill[1] = rapidity;
+                                       
+                                       // dimensions 2&3 : pt,eta (MC)
+                                       
+                                       fill[2] = mcVtxHF->Pt();
+                                       fill[3] = mcVtxHF->Y();
+                                       
+                                       fCorrelation->Fill(fill);
+                                       
+                               }  
+                               
                                // cut on the min n. of clusters in ITS
-                               Int_t ncls0=0;
-                               for(Int_t l=0;l<6;l++) if(TESTBIT(d0tokpi->GetITSClusterMap(),l)) ncls0++;
-                               AliDebug(2, Form("n clusters = %d", ncls0));
-                               if (ncls0 >= fMinITSClusters){
+                               if (fCuts->IsSelected(d0tokpi,AliRDHFCuts::kTracks)){
                                        fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoITSClusters) ;
                                        icountRecoITSClusters++;   
                                        AliDebug(2,Form("pT = %f, dca = %f, cosThetaStar = %f, pTpi = %f, pTK = %f, d0pi = %f, d0K = %f, d0xd0 = %f, cosPointingAngle = %f", pt, dca, cosThetaStar,pTpi, pTK, d0pi*1E4, d0K*1E4, d0xd0*1E8, cosPointingAngle));
-
-                                       // PPR cuts 
-                                       Double_t cuts[6] = {9999999., 1.1, 0., 9999999., 9999999., 0.};
-                                       if (pt <= 1){
-                                               cuts[0] = 400;
-                                               cuts[1] = 0.8;
-                                               cuts[2] = 0.5;
-                                               cuts[3] = 500;
-                                               cuts[4] = -20000;
-                                               cuts[5] = 0.5;
-                                       }
-                                       else if (pt > 1 && pt <= 2){
-                                               cuts[0] = 300;
-                                               cuts[1] = 0.8;
-                                               cuts[2] = 0.6;
-                                               cuts[3] = 500;
-                                               cuts[4] = -20000;
-                                               cuts[5] = 0.6;
-                                       }
-                                       else if (pt > 2 && pt <= 3){
-                                               cuts[0] = 200;
-                                               cuts[1] = 0.8;
-                                               cuts[2] = 0.7;
-                                               cuts[3] = 500;
-                                               cuts[4] = -20000;
-                                               cuts[5] = 0.8;
-                                       }
-                                       else if (pt > 3 && pt <= 5){
-                                               cuts[0] = 200;
-                                               cuts[1] = 0.8;
-                                               cuts[2] = 0.7;
-                                               cuts[3] = 500;
-                                               cuts[4] = -10000;
-                                               cuts[5] = 0.8;
-                                       }
-                                       else if (pt > 5){
-                                               cuts[0] = 200;
-                                               cuts[1] = 0.8;
-                                               cuts[2] = 0.7;
-                                               cuts[3] = 500;
-                                               cuts[4] = -5000;
-                                               cuts[5] = 0.8;
-                                       }
-                                       if (dca*1E4 < cuts[0] 
-                                           && TMath::Abs(cosThetaStar) < cuts[1]  
-                                           && pTpi > cuts[2] 
-                                           && pTK > cuts[2]  
-                                           && TMath::Abs(d0pi*1E4) < cuts[3] 
-                                           && TMath::Abs(d0K*1E4) < cuts[3]  
-                                           && d0xd0*1E8 < cuts[4] 
-                                           && cosPointingAngle > cuts[5]
-                                           ){
-
-                                               AliDebug(2,"Particle passed PPR cuts");
-                                               fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoPPR) ;   
+               
+                                       // setting the use of the PID cut when applying the selection to FALSE - whatever it was. Keeping track of the original value
+                                       Bool_t iscutusingpid=fCuts->GetIsUsePID();
+                                       Int_t isselcuts=-1,isselpid=-1;
+                                       fCuts->SetUsePID(kFALSE);       
+                                       //Bool_t origFlag = fCuts->GetIsPrimaryWithoutDaughters();
+                                       //fCuts->SetRemoveDaughtersFromPrim(kFALSE);
+                                       isselcuts = fCuts->IsSelected(d0tokpi,AliRDHFCuts::kCandidate,aodEvent);
+                                       //fCuts->SetRemoveDaughtersFromPrim(origFlag);
+                                       fCuts->SetUsePID(iscutusingpid); // restoring usage of the PID from the cuts object
+                                       if (isselcuts == 3 || isselcuts == isD0D0bar){
+                                               AliDebug(2,"Particle passed PPR cuts (actually cuts for D0 analysis!)");
+                                               fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoPPR,fWeight) ;   
                                                icountRecoPPR++;
-
-                                                if(!fAcceptanceUnf){ // unfolding
-
-                                                  Double_t fill[4]; //fill response matrix
-                                                  // dimensions 0&1 : pt,eta (Rec)
-
-                                                  fill[0] = pt ;
-                                                  fill[1] = rapidity;
-                                                  // dimensions 2&3 : pt,eta (MC)
-
-                                                  fill[2] = mcVtxHF->Pt();
-                                                  fill[3] = mcVtxHF->Y();
-
-                                                  fCorrelation->Fill(fill);
-
-                                               }
-                                       }
-                                       else{
-                                               AliDebug(2,"Particle skipped due to PPR cuts");
-                                               if (dca*1E4 > cuts[0]){
-                                                       AliDebug(2,"Problems with dca");
-                                               }
-                                               if ( TMath::Abs(cosThetaStar) > cuts[1]){
-                                                       AliDebug(2,"Problems with cosThetaStar");
-                                               }
-                                               if (pTpi < cuts[2]){
-                                                       AliDebug(2,"Problems with pTpi");
-                                               }
-                                               if (pTK < cuts[2]){
-                                                       AliDebug(2,"Problems with pTK");
-                                               }
-                                               if (TMath::Abs(d0pi*1E4) > cuts[3]){
-                                                       AliDebug(2,"Problems with d0pi");
-                                               }
-                                               if (TMath::Abs(d0K*1E4) > cuts[3]){
-                                                       AliDebug(2,"Problems with d0K");
-                                               }
-                                               if (d0xd0*1E8 > cuts[4]){
-                                                       AliDebug(2,"Problems with d0xd0");
+                                               
+                                               if(!fAcceptanceUnf){ // unfolding
+                                                       
+                                                       Double_t fill[4]; //fill response matrix
+                                                       
+                                                       // dimensions 0&1 : pt,eta (Rec)
+                                                       
+                                                       fill[0] = pt ;
+                                                       fill[1] = rapidity;
+                                                       
+                                                       // dimensions 2&3 : pt,eta (MC)
+                                                       
+                                                       fill[2] = mcVtxHF->Pt();
+                                                       fill[3] = mcVtxHF->Y();
+                                                       
+                                                       fCorrelation->Fill(fill);
+                                                       
                                                }
-                                               if (cosPointingAngle < cuts[5]){
-                                                       AliDebug(2,"Problems with cosPointingAngle");
+
+                                               isselpid = fCuts->IsSelected(d0tokpi,AliRDHFCuts::kPID);
+                                               if((fCuts->CombineSelectionLevels(3,isselcuts,isselpid)==isD0D0bar)||(fCuts->CombineSelectionLevels(3,isselcuts,isselpid)==3)){
+                                                       AliDebug(2,"Particle passed PID cuts");
+                                                       fCFManager->GetParticleContainer()->Fill(containerInput,kStepRecoPID,fWeight) ;   
+                                                       icountRecoPID++;
                                                }
                                        }
                                }
@@ -539,15 +723,18 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserExec(Option_t *)
        AliDebug(2, Form("Found %i Reco particles that are D0!!",icountReco));
 
        fCountReco+= icountReco;
+       fCountVertex+= icountVertex;
+       fCountRefit+= icountRefit;
        fCountRecoAcc+= icountRecoAcc;
        fCountRecoITSClusters+= icountRecoITSClusters;
        fCountRecoPPR+= icountRecoPPR;
+       fCountRecoPID+= icountRecoPID;
        
        fHistEventsProcessed->Fill(0);
        /* PostData(0) is taken care of by AliAnalysisTaskSE */
-       PostData(1,fHistEventsProcessed) ;
-       PostData(2,fCFManager->GetParticleContainer()) ;
-        PostData(3,fCorrelation) ;
+       //PostData(1,fHistEventsProcessed) ;
+       //PostData(2,fCFManager->GetParticleContainer()) ;
+        //PostData(3,fCorrelation) ;
 }
 
 
@@ -562,13 +749,17 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        
        AliInfo(Form("Found %i MC particles that are D0 in MC, in %d events",fCountMC,fEvents));
        AliInfo(Form("Found %i MC particles that are D0 in MC and satisfy Acc cuts, in %d events",fCountAcc,fEvents));
+       AliInfo(Form("Found %i MC particles that are D0 in MC and satisfy Acc cuts, and satisfy Vertex requirement in %d events",fCountVertex,fEvents));
+       AliInfo(Form("Found %i MC particles that are D0 in MC and satisfy Acc cuts, and satisfy ITS+TPC refit requirementin %d events",fCountRefit,fEvents));
        AliInfo(Form("Found %i reco D0 that are decaying in K+pi, in %d events",fCountReco,fEvents));
        AliInfo(Form("Among the above, found %i reco D0 that are decaying in K+pi and are in the requested acceptance, in %d events",fCountRecoAcc,fEvents));
        AliInfo(Form("Among the above, found %i reco D0 that are decaying in K+pi and have at least %d clusters in ITS, in %d events",fCountRecoITSClusters,fMinITSClusters,fEvents));
        AliInfo(Form("Among the above, found %i reco D0 that are decaying in K+pi and satisfy PPR cuts, in %d events",fCountRecoPPR,fEvents));
+       AliInfo(Form("Among the above, found %i reco D0 that are decaying in K+pi and satisfy PID cuts, in %d events",fCountRecoPID,fEvents));
        
        // draw some example plots....
        
+       //      AliCFContainer *cont= dynamic_cast<AliCFContainer*> (GetOutputData(2));
        AliCFContainer *cont= dynamic_cast<AliCFContainer*> (GetOutputData(2));
        if(!cont) {
          printf("CONTAINER NOT FOUND\n");
@@ -606,18 +797,18 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        TH1D* h111 =   cont->ShowProjection(11,1) ;   // phi
 
        // Reco-level
-       TH1D* h02 =   cont->ShowProjection(0,2) ;   // pt
-       TH1D* h12 =   cont->ShowProjection(1,2) ;   // rapidity
-       TH1D* h22 =   cont->ShowProjection(2,2) ;   // cosThetaStar
-       TH1D* h32 =   cont->ShowProjection(3,2) ;   // pTpi
-       TH1D* h42 =   cont->ShowProjection(4,2) ;   // pTK
-       TH1D* h52 =   cont->ShowProjection(5,2) ;   // cT
-       TH1D* h62 =   cont->ShowProjection(6,2) ;   // dca
-       TH1D* h72 =   cont->ShowProjection(7,2) ;   // d0pi
-       TH1D* h82 =   cont->ShowProjection(8,2) ;   // d0K
-       TH1D* h92 =   cont->ShowProjection(9,2) ;   // d0xd0
-       TH1D* h102 =   cont->ShowProjection(10,2) ;   // cosPointingAngle
-       TH1D* h112 =   cont->ShowProjection(11,2) ;   // phi
+       TH1D* h04 =   cont->ShowProjection(0,4) ;   // pt
+       TH1D* h14 =   cont->ShowProjection(1,4) ;   // rapidity
+       TH1D* h24 =   cont->ShowProjection(2,4) ;   // cosThetaStar
+       TH1D* h34 =   cont->ShowProjection(3,4) ;   // pTpi
+       TH1D* h44 =   cont->ShowProjection(4,4) ;   // pTK
+       TH1D* h54 =   cont->ShowProjection(5,4) ;   // cT
+       TH1D* h64 =   cont->ShowProjection(6,4) ;   // dca
+       TH1D* h74 =   cont->ShowProjection(7,4) ;   // d0pi
+       TH1D* h84 =   cont->ShowProjection(8,4) ;   // d0K
+       TH1D* h94 =   cont->ShowProjection(9,4) ;   // d0xd0
+       TH1D* h104 =   cont->ShowProjection(10,4) ;   // cosPointingAngle
+       TH1D* h114 =   cont->ShowProjection(11,4) ;   // phi
        
        h00->SetTitle("pT_D0 (GeV/c)");
        h10->SetTitle("rapidity");
@@ -671,31 +862,31 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h101->GetXaxis()->SetTitle("cosPointingAngle");
        h111->GetXaxis()->SetTitle("phi (rad)");
 
-       h02->SetTitle("pT_D0 (GeV/c)");
-       h12->SetTitle("rapidity");
-       h22->SetTitle("cosThetaStar");
-       h32->SetTitle("pT_pi (GeV/c)");
-       h42->SetTitle("pT_K (Gev/c)");
-       h52->SetTitle("cT (#mum)");
-       h62->SetTitle("dca (#mum)");
-       h72->SetTitle("d0_pi (#mum)");
-       h82->SetTitle("d0_K (#mum)");
-       h92->SetTitle("d0xd0 (#mum^2)");
-       h102->SetTitle("cosPointingAngle");
-       h112->GetXaxis()->SetTitle("phi (rad)");
-
-       h02->GetXaxis()->SetTitle("pT_D0 (GeV/c)");
-       h12->GetXaxis()->SetTitle("rapidity");
-       h22->GetXaxis()->SetTitle("cosThetaStar");
-       h32->GetXaxis()->SetTitle("pT_pi (GeV/c)");
-       h42->GetXaxis()->SetTitle("pT_K (Gev/c)");
-       h52->GetXaxis()->SetTitle("cT (#mum)");
-       h62->GetXaxis()->SetTitle("dca (#mum)");
-       h72->GetXaxis()->SetTitle("d0_pi (#mum)");
-       h82->GetXaxis()->SetTitle("d0_K (#mum)");
-       h92->GetXaxis()->SetTitle("d0xd0 (#mum^2)");
-       h102->GetXaxis()->SetTitle("cosPointingAngle");
-       h112->GetXaxis()->SetTitle("phi (rad)");
+       h04->SetTitle("pT_D0 (GeV/c)");
+       h14->SetTitle("rapidity");
+       h24->SetTitle("cosThetaStar");
+       h34->SetTitle("pT_pi (GeV/c)");
+       h44->SetTitle("pT_K (Gev/c)");
+       h54->SetTitle("cT (#mum)");
+       h64->SetTitle("dca (#mum)");
+       h74->SetTitle("d0_pi (#mum)");
+       h84->SetTitle("d0_K (#mum)");
+       h94->SetTitle("d0xd0 (#mum^2)");
+       h104->SetTitle("cosPointingAngle");
+       h114->GetXaxis()->SetTitle("phi (rad)");
+
+       h04->GetXaxis()->SetTitle("pT_D0 (GeV/c)");
+       h14->GetXaxis()->SetTitle("rapidity");
+       h24->GetXaxis()->SetTitle("cosThetaStar");
+       h34->GetXaxis()->SetTitle("pT_pi (GeV/c)");
+       h44->GetXaxis()->SetTitle("pT_K (Gev/c)");
+       h54->GetXaxis()->SetTitle("cT (#mum)");
+       h64->GetXaxis()->SetTitle("dca (#mum)");
+       h74->GetXaxis()->SetTitle("d0_pi (#mum)");
+       h84->GetXaxis()->SetTitle("d0_K (#mum)");
+       h94->GetXaxis()->SetTitle("d0xd0 (#mum^2)");
+       h104->GetXaxis()->SetTitle("cosPointingAngle");
+       h114->GetXaxis()->SetTitle("phi (rad)");
 
        Double_t max0 = h00->GetMaximum();
        Double_t max1 = h10->GetMaximum();
@@ -788,31 +979,31 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h101->SetMarkerColor(8);
        h111->SetMarkerColor(8);
 
-       h02->SetMarkerStyle(20) ;
-       h12->SetMarkerStyle(24) ;
-       h22->SetMarkerStyle(21) ;
-       h32->SetMarkerStyle(25) ;
-       h42->SetMarkerStyle(27) ;
-       h52->SetMarkerStyle(28) ;
-       h62->SetMarkerStyle(20);
-       h72->SetMarkerStyle(24);
-       h82->SetMarkerStyle(21);
-       h92->SetMarkerStyle(25);
-       h102->SetMarkerStyle(27);
-       h112->SetMarkerStyle(28);
-
-       h02->SetMarkerColor(4);
-       h12->SetMarkerColor(4);
-       h22->SetMarkerColor(4);
-       h32->SetMarkerColor(4);
-       h42->SetMarkerColor(4);
-       h52->SetMarkerColor(4);
-       h62->SetMarkerColor(4);
-       h72->SetMarkerColor(4);
-       h82->SetMarkerColor(4);
-       h92->SetMarkerColor(4);
-       h102->SetMarkerColor(4);
-       h112->SetMarkerColor(4);
+       h04->SetMarkerStyle(20) ;
+       h14->SetMarkerStyle(24) ;
+       h24->SetMarkerStyle(21) ;
+       h34->SetMarkerStyle(25) ;
+       h44->SetMarkerStyle(27) ;
+       h54->SetMarkerStyle(28) ;
+       h64->SetMarkerStyle(20);
+       h74->SetMarkerStyle(24);
+       h84->SetMarkerStyle(21);
+       h94->SetMarkerStyle(25);
+       h104->SetMarkerStyle(27);
+       h114->SetMarkerStyle(28);
+
+       h04->SetMarkerColor(4);
+       h14->SetMarkerColor(4);
+       h24->SetMarkerColor(4);
+       h34->SetMarkerColor(4);
+       h44->SetMarkerColor(4);
+       h54->SetMarkerColor(4);
+       h64->SetMarkerColor(4);
+       h74->SetMarkerColor(4);
+       h84->SetMarkerColor(4);
+       h94->SetMarkerColor(4);
+       h104->SetMarkerColor(4);
+       h114->SetMarkerColor(4);
        
        gStyle->SetCanvasColor(0);
        gStyle->SetFrameFillColor(0);
@@ -830,7 +1021,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h01->Draw("p");
        c1->cd(2);
        c1->cd(3);
-       h02->Draw("p");
+       h04->Draw("p");
        c1->cd(3);
        c1->cd(4);
        h10->Draw("p");
@@ -839,7 +1030,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h11->Draw("p");
        c1->cd(5);
        c1->cd(6);
-       h12->Draw("p");
+       h14->Draw("p");
        c1->cd(6);
        c1->cd(7);
        h20->Draw("p");
@@ -848,7 +1039,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h21->Draw("p");
        c1->cd(8);
        c1->cd(9);
-       h22->Draw("p");
+       h24->Draw("p");
        c1->cd(9);
        c1->cd();
 
@@ -861,7 +1052,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h31->Draw("p");
        c2->cd(2);
        c2->cd(3);
-       h32->Draw("p");
+       h34->Draw("p");
        c2->cd(3);
        c2->cd(4);
        h40->Draw("p");
@@ -870,7 +1061,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h41->Draw("p");
        c2->cd(5);
        c2->cd(6);
-       h42->Draw("p");
+       h44->Draw("p");
        c2->cd(6);
        c2->cd(7);
        h50->Draw("p");
@@ -879,7 +1070,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h51->Draw("p");
        c2->cd(8);
        c2->cd(9);
-       h52->Draw("p");
+       h54->Draw("p");
        c2->cd(9);
        c2->cd();
 
@@ -892,7 +1083,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h61->Draw("p");
        c3->cd(2);
        c3->cd(3);
-       h62->Draw("p");
+       h64->Draw("p");
        c3->cd(3);
        c3->cd(4);
        h70->Draw("p");
@@ -901,7 +1092,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h71->Draw("p");
        c3->cd(5);
        c3->cd(6);
-       h72->Draw("p");
+       h74->Draw("p");
        c3->cd(6);
        c3->cd(7);
        h80->Draw("p");
@@ -910,7 +1101,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h81->Draw("p");
        c3->cd(8);
        c3->cd(9);
-       h82->Draw("p");
+       h84->Draw("p");
        c3->cd(9);
        c3->cd();
 
@@ -923,7 +1114,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h91->Draw("p");
        c4->cd(2);
        c4->cd(3);
-       h92->Draw("p");
+       h94->Draw("p");
        c4->cd(3);
        c4->cd(4);
        h100->Draw("p");
@@ -932,7 +1123,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h101->Draw("p");
        c4->cd(5);
        c4->cd(6);
-       h102->Draw("p");
+       h104->Draw("p");
        c4->cd(6);
        c4->cd(7);
        h110->Draw("p");
@@ -941,7 +1132,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h111->Draw("p");
        c4->cd(8);
        c4->cd(9);
-       h112->Draw("p");
+       h114->Draw("p");
        c4->cd(9);
        c4->cd();
 
@@ -958,7 +1149,11 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
         corr2->Draw("text");
       
 
-       TFile* file_projection = new TFile("file_projection.root","RECREATE");
+       TString projectionFilename="CFtaskHFprojection";
+       if(fKeepD0fromBOnly) projectionFilename+="_KeepD0fromBOnly";
+       else if(fKeepD0fromB) projectionFilename+="_KeepD0fromB";
+       projectionFilename+=".root";
+       TFile* fileProjection = new TFile(projectionFilename.Data(),"RECREATE");
 
         corr1->Write();
         corr2->Write();
@@ -988,20 +1183,20 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        h101->Write("cosPointingAngle_step1");
        h111->Write("phi_step1");
 
-       h02->Write("pT_D0_step2");
-       h12->Write("rapidity_step2");
-       h22->Write("cosThetaStar_step2");
-       h32->Write("pT_pi_step2");
-       h42->Write("pT_K_step2");
-       h52->Write("cT_step2");
-       h62->Write("dca_step2");
-       h72->Write("d0_pi_step2");
+       h04->Write("pT_D0_step2");
+       h14->Write("rapidity_step2");
+       h24->Write("cosThetaStar_step2");
+       h34->Write("pT_pi_step2");
+       h44->Write("pT_K_step2");
+       h54->Write("cT_step2");
+       h64->Write("dca_step2");
+       h74->Write("d0_pi_step2");
        h80->Write("d0_K_step2");
-       h92->Write("d0xd0_step2");
-       h102->Write("cosPointingAngle_step2");
-       h112->Write("phi_step2");
+       h94->Write("d0xd0_step2");
+       h104->Write("cosPointingAngle_step2");
+       h114->Write("phi_step2");
 
-       file_projection->Close();
+       fileProjection->Close();
 
     
 
@@ -1015,7 +1210,7 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::Terminate(Option_t*)
        c2->SaveAs("Plots/pTpi_pTK_cT.gif");
        c3->SaveAs("Plots/dca_d0pi_d0TK.gif");
        c4->SaveAs("Plots/d0xd0_cosPointingAngle.gif");
-       */
+       */      
 }
 
 //___________________________________________________________________________
@@ -1027,11 +1222,11 @@ void AliCFHeavyFlavourTaskMultiVarMultiStep::UserCreateOutputObjects() {
        
        //slot #1
        OpenFile(1);
-       fHistEventsProcessed = new TH1I("fHistEventsProcessed","",1,0,1) ;
+       fHistEventsProcessed = new TH1I("CFHFchist0","",1,0,1) ;
 }
 
 //___________________________________________________________________________
-Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CosThetaStar(AliAODMCParticle* mcPart, AliAODMCParticle* mcPartDaughter0, AliAODMCParticle* mcPartDaughter1) const {
+Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CosThetaStar(const AliAODMCParticle* mcPart, const AliAODMCParticle* mcPartDaughter0, const AliAODMCParticle* mcPartDaughter1) const {
 
        //
        // to calculate cos(ThetaStar) for generated particle
@@ -1063,7 +1258,7 @@ Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CosThetaStar(AliAODMCParticle*
        }
        if (pdgprong0 == 211){
                AliDebug(2,Form("pdgprong0 = %d, pdgprong1 = %d, switching...",pdgprong0,pdgprong1));
-               AliAODMCParticle* mcPartdummy = mcPartDaughter0;
+               const AliAODMCParticle* mcPartdummy = mcPartDaughter0;
                mcPartDaughter0 = mcPartDaughter1;
                mcPartDaughter1 = mcPartdummy;
                pdgprong0 = TMath::Abs(mcPartDaughter0->GetPdgCode());
@@ -1096,7 +1291,7 @@ Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CosThetaStar(AliAODMCParticle*
        return cts;
 }
 //___________________________________________________________________________
-Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CT(AliAODMCParticle* mcPart, AliAODMCParticle* mcPartDaughter0, AliAODMCParticle* mcPartDaughter1) const {
+Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CT(const AliAODMCParticle* mcPart, const AliAODMCParticle* mcPartDaughter0, const AliAODMCParticle* mcPartDaughter1) const {
 
        //
        // to calculate cT for generated particle
@@ -1145,7 +1340,7 @@ Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::CT(AliAODMCParticle* mcPart, Al
        return cT;
 }
 //________________________________________________________________________________
-Bool_t AliCFHeavyFlavourTaskMultiVarMultiStep::GetGeneratedValuesFromMCParticle(AliAODMCParticle* mcPart, TClonesArray* mcArray, Double_t* vectorMC) const {
+Bool_t AliCFHeavyFlavourTaskMultiVarMultiStep::GetGeneratedValuesFromMCParticle(AliAODMCParticle* mcPart, const TClonesArray* mcArray, Double_t* vectorMC) const {
 
        // 
        // collecting all the necessary info (pt, y, cosThetaStar, ptPi, ptKa, cT) from MC particle
@@ -1283,7 +1478,7 @@ Bool_t AliCFHeavyFlavourTaskMultiVarMultiStep::GetGeneratedValuesFromMCParticle(
        return isOk;
 }
 //_________________________________________________________________________________________________
-Int_t AliCFHeavyFlavourTaskMultiVarMultiStep::CheckOrigin(AliAODMCParticle* mcPart, TClonesArray* mcArray)const{
+Int_t AliCFHeavyFlavourTaskMultiVarMultiStep::CheckOrigin(const AliAODMCParticle* mcPart, const TClonesArray* mcArray)const{
 
        //
        // checking whether the very mother of the D0 is a charm or a bottom quark
@@ -1297,6 +1492,7 @@ Int_t AliCFHeavyFlavourTaskMultiVarMultiStep::CheckOrigin(AliAODMCParticle* mcPa
                istep++;
                AliDebug(2,Form("mother at step %d = %d", istep, mother));
                AliAODMCParticle* mcGranma = dynamic_cast<AliAODMCParticle*>(mcArray->At(mother));
+               if(!mcGranma) break;
                pdgGranma = mcGranma->GetPdgCode();
                AliDebug(2,Form("Pdg mother at step %d = %d", istep, pdgGranma));
                Int_t abspdgGranma = TMath::Abs(pdgGranma);
@@ -1307,4 +1503,43 @@ Int_t AliCFHeavyFlavourTaskMultiVarMultiStep::CheckOrigin(AliAODMCParticle* mcPa
        }
        return pdgGranma;
 }
+//__________________________________________________________________________________________________
+Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::GetWeight(Float_t pt){
+
+       //
+       // calculating the weight to fill the container
+       //
 
+       // FNOLL central:
+       // p0 = 1.63297e-01 --> 0.322643
+       // p1 = 2.96275e+00
+       // p2 = 2.30301e+00
+       // p3 = 2.50000e+00
+
+       // PYTHIA
+       // p0 = 1.85906e-01 --> 0.36609
+       // p1 = 1.94635e+00
+       // p2 = 1.40463e+00
+       // p3 = 2.50000e+00
+
+       Double_t func1[4] = {0.322643,2.96275,2.30301,2.5};
+       Double_t func2[4] = {0.36609,1.94635,1.40463,2.5};
+
+       Double_t dndptFunc1 = DodNdptFit(pt,func1);
+       Double_t dndptFunc2 = DodNdptFit(pt,func2);
+       AliDebug(2,Form("pt = %f, FONLL = %f, Pythia = %f, ratio = %f",pt,dndptFunc1,dndptFunc2,dndptFunc1/dndptFunc2));
+       return dndptFunc1/dndptFunc2;
+}
+
+//__________________________________________________________________________________________________
+Double_t AliCFHeavyFlavourTaskMultiVarMultiStep::DodNdptFit(Float_t pt, const Double_t* par)const{
+
+       // 
+       // calculating dNdpt
+       //
+
+       Double_t denom =  TMath::Power((pt/par[1]), par[3] );
+       Double_t dNdpt = par[0]*pt/TMath::Power(1.+denom, par[2]);
+       
+       return dNdpt;
+}