Added Add method to the various correction classes and update AliSystematicSelection.
[u/mrichter/AliRoot.git] / PWG0 / dNdEta / AlidNdEtaSystematicsSelector.cxx
index 7750b22..e00075f 100644 (file)
@@ -8,19 +8,23 @@
 #include <TVector3.h>
 #include <TChain.h>
 #include <TFile.h>
-#include <TH3F.h>
+#include <TH2F.h>
 #include <TH1F.h>
 #include <TParticle.h>
+#include <TParticlePDG.h>
 
 #include <AliLog.h>
 #include <AliESD.h>
-#include <AliGenEventHeader.h>
 #include <AliStack.h>
 #include <AliHeader.h>
+#include <AliGenEventHeader.h>
+#include <../PYTHIA6/AliGenPythiaEventHeader.h>
+#include <../EVGEN/AliGenCocktailEventHeader.h>
 
 #include "esdTrackCuts/AliESDtrackCuts.h"
 #include "AliPWG0Helper.h"
-#include "AlidNdEtaCorrection.h"
+#include "AliPWG0depHelper.h"
+#include "dNdEta/AlidNdEtaCorrection.h"
 
 ClassImp(AlidNdEtaSystematicsSelector)
 
@@ -29,17 +33,22 @@ AlidNdEtaSystematicsSelector::AlidNdEtaSystematicsSelector() :
   fSecondaries(0),
   fSigmaVertex(0),
   fEsdTrackCuts(0),
-  fOverallPrimaries(0),
-  fOverallSecondaries(0),
   fPIDParticles(0),
-  fPIDTracks(0)
+  fPIDTracks(0),
+  fSignMode(0),
+  fMultiplicityMode(0)
 {
   //
   // Constructor. Initialization of pointers
   //
 
   for (Int_t i=0; i<4; ++i)
-    fdNdEtaCorrection[i] = 0;
+    fdNdEtaCorrectionSpecies[i] = 0;
+
+  for (Int_t i=0; i<3; ++i) {
+    fdNdEtaCorrectionVertexReco[i] = 0;
+    fdNdEtaCorrectionTriggerBias[i] = 0;
+  }
 }
 
 AlidNdEtaSystematicsSelector::~AlidNdEtaSystematicsSelector()
@@ -88,7 +97,15 @@ void AlidNdEtaSystematicsSelector::SlaveBegin(TTree* tree)
 
   if (option.Contains("secondaries"))
   {
-    fSecondaries = new TH3F("fSecondaries", "fSecondaries;NacceptedTracks;CratioSecondaries;p_{T}", 2000, -0.5, 205.5, 16, 0.45, 2.05, 10, 0, 10);
+    fSecondaries = new TH2F("fSecondaries", "fSecondaries;Case;N", 8, 0.5, 8.5, 2001, -0.5, 2000.5);
+    fSecondaries->GetXaxis()->SetBinLabel(1, "All Primaries");
+    fSecondaries->GetXaxis()->SetBinLabel(2, "All Secondaries");
+    fSecondaries->GetXaxis()->SetBinLabel(3, "Primaries pT > 0.3 GeV/c");
+    fSecondaries->GetXaxis()->SetBinLabel(4, "Secondaries pT > 0.3 GeV/c");
+    fSecondaries->GetXaxis()->SetBinLabel(5, "Tracks from Primaries");
+    fSecondaries->GetXaxis()->SetBinLabel(6, "Tracks from Secondaries");
+    fSecondaries->GetXaxis()->SetBinLabel(7, "Accepted Tracks from Primaries");
+    fSecondaries->GetXaxis()->SetBinLabel(8, "Accepted Tracks from Secondaries");
   }
 
   if (option.Contains("particle-composition"))
@@ -97,18 +114,52 @@ void AlidNdEtaSystematicsSelector::SlaveBegin(TTree* tree)
     {
       TString name;
       name.Form("correction_%d", i);
-      fdNdEtaCorrection[i] = new AlidNdEtaCorrection(name, name);
+      fdNdEtaCorrectionSpecies[i] = new AlidNdEtaCorrection(name, name);
     }
   }
 
   if (option.Contains("sigma-vertex"))
   {
-    fSigmaVertex = new TH1F("fSigmaVertex", "fSigmaVertex;Nsigma2vertex;NacceptedTracks", 10, 0.25, 5.25);
+    fSigmaVertex = new TH1F("fSigmaVertex", "fSigmaVertex;Nsigma2vertex;NacceptedTracks", 50, 0.05, 5.05);
     printf("WARNING: sigma-vertex analysis enabled. This will produce weird results in the AliESDtrackCuts histograms\n");
   }
 
+  if (option.Contains("vertexreco")) {
+    fdNdEtaCorrectionVertexReco[0] = new AlidNdEtaCorrection("vertexRecoND", "vertexRecoND");
+    fdNdEtaCorrectionVertexReco[1] = new AlidNdEtaCorrection("vertexRecoSD", "vertexRecoSD");
+    fdNdEtaCorrectionVertexReco[2] = new AlidNdEtaCorrection("vertexRecoDD", "vertexRecoDD");
+  }
+
+  if (option.Contains("triggerbias")) {
+    fdNdEtaCorrectionTriggerBias[0] = new AlidNdEtaCorrection("triggerBiasND", "triggerBiasND");
+    fdNdEtaCorrectionTriggerBias[1] = new AlidNdEtaCorrection("triggerBiasSD", "triggerBiasSD");
+    fdNdEtaCorrectionTriggerBias[2] = new AlidNdEtaCorrection("triggerBiasDD", "triggerBiasDD");
+  }
+
   fPIDParticles = new TH1F("pid_particles", "PID of generated primary particles", 10001, -5000.5, 5000.5);
   fPIDTracks = new TH1F("pid_tracks", "MC PID of reconstructed tracks", 10001, -5000.5, 5000.5);
+
+  if (option.Contains("only-positive"))
+  {
+    AliInfo("Processing only positive particles.");
+    fSignMode = 1;
+  }
+  else if (option.Contains("only-negative"))
+  {
+    AliInfo("Processing only negative particles.");
+    fSignMode = -1;
+  }
+
+  if (option.Contains("low-multiplicity"))
+  {
+    AliInfo("Processing only events with low multiplicity.");
+    fMultiplicityMode = 1;
+  }
+  else if (option.Contains("high-multiplicity"))
+  {
+    AliInfo("Processing only events with high multiplicity.");
+    fMultiplicityMode = 2;
+  }
 }
 
 Bool_t AlidNdEtaSystematicsSelector::Process(Long64_t entry)
@@ -154,53 +205,78 @@ Bool_t AlidNdEtaSystematicsSelector::Process(Long64_t entry)
     return kFALSE;
   }
 
-  TObjArray* list = fEsdTrackCuts->GetAcceptedTracks(fESD);
-
-  if (fdNdEtaCorrection[0])
-    FillCorrectionMaps(list);
-
-  if (fSecondaries)
-    FillSecondaries(list);
-
-  if (fSigmaVertex)
-    FillSigmaVertex();
-
-  delete list;
-  list = 0;
-
-  return kTRUE;
-}
-
-void AlidNdEtaSystematicsSelector::FillCorrectionMaps(TObjArray* listOfTracks)
-{
-  // fills the correction maps for different particle species
+  // --------------------------------------------------------------
+  // related to events:
+  //
+  // stuff for vertex reconstruction correction systematics
+  Bool_t vertexRecoStudy  = kFALSE;
+  Bool_t triggerBiasStudy = kFALSE;
+  if (fdNdEtaCorrectionVertexReco[0])  vertexRecoStudy = kTRUE;
+  if (fdNdEtaCorrectionTriggerBias[0]) triggerBiasStudy = kTRUE;
 
-  AliStack* stack = GetStack();
   AliHeader* header = GetHeader();
-
-  Bool_t vertexReconstructed = AliPWG0Helper::IsVertexReconstructed(fESD);
-  Bool_t eventTriggered = AliPWG0Helper::IsEventTriggered(fESD);
-
-  for (Int_t i=0; i<4; ++i)
-  {
-    fdNdEtaCorrection[i]->IncreaseEventCount();
-    if (eventTriggered)
-      fdNdEtaCorrection[i]->IncreaseTriggeredEventCount();
+  if (!header) {
+    AliDebug(AliLog::kError, "Header not available");
+    return kFALSE;
   }
-
+  
+  // getting process information NB: this only works for Pythia !!!
+  Int_t processtype = AliPWG0depHelper::GetPythiaEventProcessType(header);
+    
+  // can only read pythia headers, either directly or from cocktalil header
+  AliGenEventHeader* genHeader = (AliGenEventHeader*)(header->GenEventHeader());
+  
+  if (!genHeader) {
+    AliDebug(AliLog::kError, "Gen header not available");
+    return kFALSE;
+  }
+  
   // get the MC vertex
-  AliGenEventHeader* genHeader = header->GenEventHeader();
-
   TArrayF vtxMC(3);
   genHeader->PrimaryVertex(vtxMC);
+  
+  TObjArray* listOfTracks    = fEsdTrackCuts->GetAcceptedTracks(fESD);
+  Int_t nGoodTracks          = listOfTracks->GetEntries();
+  
+  Bool_t eventTriggered      = AliPWG0Helper::IsEventTriggered(fESD);
+  Bool_t vertexReconstructed = AliPWG0Helper::IsVertexReconstructed(fESD);
+  
+  // non diffractive
+  if (processtype!=92 && processtype!=93 && processtype!=94) { 
+    // NB: passing the wrong process type here (1), since the process type is defined by the index in the array (here non-diffractive)
+    if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[0]->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);      
+    if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[0] ->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);
+  }
+  
+  // single diffractive
+  if (processtype==92 || processtype==93) { 
+    if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[1]->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);     
+    if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[1] ->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);
+  }
+  
+  // double diffractive
+  if (processtype==94) { 
+    if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[2]->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);     
+    if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[2] ->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);
+  }
+
+  if (eventTriggered & vertexReconstructed) {
+    for (Int_t i=0; i<4; ++i) {
+      if (fdNdEtaCorrectionSpecies[i])
+       fdNdEtaCorrectionSpecies[i]->FillEvent(vtxMC[2], nGoodTracks, eventTriggered, vertexReconstructed, 1);
+    }
+  }
+
+  // --------------------------------------------------------------
+  // MC particle loop
+  //
 
-  // loop over mc particles
   Int_t nPrim  = stack->GetNprimary();
 
   for (Int_t iMc = 0; iMc < nPrim; ++iMc)
   {
     TParticle* particle = stack->Particle(iMc);
-
+    
     if (!particle)
     {
       AliDebug(AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack (mc loop).", iMc));
@@ -209,43 +285,73 @@ void AlidNdEtaSystematicsSelector::FillCorrectionMaps(TObjArray* listOfTracks)
 
     if (AliPWG0Helper::IsPrimaryCharged(particle, nPrim) == kFALSE)
       continue;
-
+    
+    if (SignOK(particle->GetPDG()) == kFALSE)
+      continue;
+    
     Float_t eta = particle->Eta();
     Float_t pt = particle->Pt();
-
+    
     Int_t id = -1;
     switch (TMath::Abs(particle->GetPdgCode()))
-    {
+      {
       case 211: id = 0; break;
       case 321: id = 1; break;
       case 2212: id = 2; break;
-      case 11: 
+      case 11:
         {
-          if (pt < 0.1 && particle->GetMother(0) > -1)
+          /*if (pt < 0.1 && particle->GetMother(0) > -1)
           {
             TParticle* mother = stack->Particle(particle->GetMother(0));
             printf("Mother pdg code is %d\n", mother->GetPdgCode());
-          }   
+          } */
           //particle->Dump();
           //if (particle->GetUniqueID() == 1)
 
-          //printf("Found illegal particle mc id = %d file = %s event = %d\n", iMc,  fTree->GetCurrentFile()->GetName(), fTree->GetTree()->GetReadEntry()); 
+          //printf("Found illegal particle mc id = %d file = %s event = %d\n", iMc,  fTree->GetCurrentFile()->GetName(), fTree->GetTree()->GetReadEntry());
         }
       default: id = 3; break;
     }
 
-    if (vertexReconstructed)
-    {
-      fdNdEtaCorrection[id]->FillParticle(vtxMC[2], eta, pt);
-      if (pt < 0.1)
-        fPIDParticles->Fill(particle->GetPdgCode());
+    if (eventTriggered & vertexReconstructed) {
+      if (fdNdEtaCorrectionSpecies[id])
+       fdNdEtaCorrectionSpecies[id]->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+      //if (pt < 0.1)
+      if (fPIDParticles)
+       fPIDParticles->Fill(particle->GetPdgCode());
+    }
+    
+    // non diffractive
+    if (processtype!=92 && processtype!=93 && processtype!=94) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[0]->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[0] ->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+    }
+    // single diffractive
+    if (processtype==92 || processtype==93) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[1]->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[1] ->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+    }
+    // double diffractive
+    if (processtype==94) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[2]->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[2] ->FillMCParticle(vtxMC[2], eta, pt, eventTriggered, vertexReconstructed, 1);
     }
 
-    fdNdEtaCorrection[id]->FillParticleAllEvents(eta, pt);
-    if (eventTriggered)
-      fdNdEtaCorrection[id]->FillParticleWhenEventTriggered(eta, pt);
   }// end of mc particle
 
+
+  // --------------------------------------------------------------
+  // ESD track loop
+  //
+
+  if (fMultiplicityMode == 1 && listOfTracks->GetEntries() > 20 ||
+      fMultiplicityMode == 2 && listOfTracks->GetEntries() < 40)
+  {
+    delete listOfTracks;
+    listOfTracks = 0;
+    return kTRUE;
+  }
+
   // loop over esd tracks
   TIterator* iter = listOfTracks->MakeIterator();
   TObject* obj = 0;
@@ -255,6 +361,28 @@ void AlidNdEtaSystematicsSelector::FillCorrectionMaps(TObjArray* listOfTracks)
     if (!esdTrack)
       continue;
 
+    Float_t eta = particle->Eta();
+    Float_t pt  = particle->Pt();
+    
+    // non diffractive
+    if (processtype!=92 && processtype!=93 && processtype!=94) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[0]->FillTrackedParticle(vtxMC[2], eta, pt);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[0] ->FillTrackedParticle(vtxMC[2], eta, pt);
+    }
+    
+    // single diffractive
+    if (processtype==92 || processtype==93) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[1]->FillTrackedParticle(vtxMC[2], eta, pt);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[1] ->FillTrackedParticle(vtxMC[2], eta, pt);
+    }
+
+    // double diffractive
+    if (processtype==94) { 
+      if (triggerBiasStudy) fdNdEtaCorrectionTriggerBias[2]->FillTrackedParticle(vtxMC[2], eta, pt);
+      if (vertexRecoStudy)  fdNdEtaCorrectionVertexReco[2] ->FillTrackedParticle(vtxMC[2], eta, pt);
+    }
+    
+
     // using the properties of the mc particle
     Int_t label = TMath::Abs(esdTrack->GetLabel());
     TParticle* particle = stack->Particle(label);
@@ -264,8 +392,39 @@ void AlidNdEtaSystematicsSelector::FillCorrectionMaps(TObjArray* listOfTracks)
       continue;
     }
 
+    TParticle* mother = particle;
+    // find primary particle that created this particle
+    while (label >= nPrim)
+    {
+      //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
+
+      if (mother->GetMother(0) == -1)
+      {
+        AliDebug(AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
+        mother = 0;
+        break;
+      }
+
+      label = mother->GetMother(0);
+
+      mother = stack->Particle(label);
+      if (!mother)
+      {
+        AliDebug(AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack (find mother loop).", label));
+        break;
+      }
+    }
+
+    if (!mother)
+      continue;
+
+    if (SignOK(mother->GetPDG()) == kFALSE)
+      continue;
+
+    //printf("We continue with particle %d (pdg %d)\n", label, mother->GetPdgCode());
+
     Int_t id = -1;
-    switch (TMath::Abs(particle->GetPdgCode()))
+    switch (TMath::Abs(mother->GetPdgCode()))
     {
       case 211:  id = 0; break;
       case 321:  id = 1; break;
@@ -273,34 +432,96 @@ void AlidNdEtaSystematicsSelector::FillCorrectionMaps(TObjArray* listOfTracks)
       default:   id = 3; break;
     }
 
-    if (vertexReconstructed)
-    {
-      fdNdEtaCorrection[id]->FillParticleWhenMeasuredTrack(vtxMC[2], particle->Eta(), particle->Pt());
-      if (particle->Pt() < 0.1)
-        fPIDTracks->Fill(particle->GetPdgCode());
+    if (vertexReconstructed && eventTriggered) {
+      if (fdNdEtaCorrectionSpecies[id])
+       fdNdEtaCorrectionSpecies[id]->FillTrackedParticle(vtxMC[2], eta, pt);
+      //if (pt < 0.1)
+      if (fPIDTracks)
+       fPIDTracks->Fill(particle->GetPdgCode());
     }
   } // end of track loop
 
+  
   delete iter;
   iter = 0;
+
+  if (fSecondaries)
+    FillSecondaries();
+
+  if (fSigmaVertex)
+    FillSigmaVertex();
+
+
+  delete listOfTracks;
+  listOfTracks = 0;
+
+  return kTRUE;
 }
 
-void AlidNdEtaSystematicsSelector::FillSecondaries(TObjArray* listOfTracks)
+void AlidNdEtaSystematicsSelector::FillSecondaries()
 {
   // fills the secondary histograms
-
+  
   AliStack* stack = GetStack();
 
-  TH1* nPrimaries = new TH1F("secondaries_primaries", "secondaries_primaries", fSecondaries->GetZaxis()->GetNbins(), fSecondaries->GetZaxis()->GetXmin(), fSecondaries->GetZaxis()->GetXmax());
-  TH1* nSecondaries = new TH1F("secondaries_secondaries", "secondaries_secondaries", fSecondaries->GetZaxis()->GetNbins(), fSecondaries->GetZaxis()->GetXmin(), fSecondaries->GetZaxis()->GetXmax());
+  Int_t particlesPrimaries = 0;
+  Int_t particlesSecondaries = 0;
+  Int_t particlesPrimariesPtCut = 0;
+  Int_t particlesSecondariesPtCut = 0;
 
-  TIterator* iter = listOfTracks->MakeIterator();
-  TObject* obj = 0;
-  while ((obj = iter->Next()) != 0)
+  for (Int_t iParticle = 0; iParticle < stack->GetNtrack(); iParticle++)
   {
-    AliESDtrack* esdTrack = dynamic_cast<AliESDtrack*> (obj);
+    TParticle* particle = stack->Particle(iParticle);
+    if (!particle)
+    {
+      AliDebug(AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack (particle loop).", iParticle));
+      continue;
+    }
+
+    if (TMath::Abs(particle->Eta()) > 0.9)
+      continue;
+
+    Bool_t isPrimary = kFALSE;
+    if (iParticle < stack->GetNprimary())
+    {
+      if (AliPWG0Helper::IsPrimaryCharged(particle, stack->GetNprimary()) == kFALSE)
+        continue;
+
+      isPrimary = kTRUE;
+    }
+
+    if (isPrimary)
+      particlesPrimaries++;
+    else
+      particlesSecondaries++;
+
+    if (particle->Pt() < 0.3)
+      continue;
+
+    if (isPrimary)
+      particlesPrimariesPtCut++;
+    else
+      particlesSecondariesPtCut++;
+  }
+
+  fSecondaries->Fill(1, particlesPrimaries);
+  fSecondaries->Fill(2, particlesSecondaries);
+  fSecondaries->Fill(3, particlesPrimariesPtCut);
+  fSecondaries->Fill(4, particlesSecondariesPtCut);
+
+  Int_t allTracksPrimaries = 0;
+  Int_t allTracksSecondaries = 0;
+  Int_t acceptedTracksPrimaries = 0;
+  Int_t acceptedTracksSecondaries = 0;
+
+  for (Int_t iTrack = 0; iTrack < fESD->GetNumberOfTracks(); iTrack++)
+  {
+    AliESDtrack* esdTrack = fESD->GetTrack(iTrack);
     if (!esdTrack)
+    {
+      AliDebug(AliLog::kError, Form("UNEXPECTED: Could not get track %d.", iTrack));
       continue;
+    }
 
     Int_t label = TMath::Abs(esdTrack->GetLabel());
     TParticle* particle = stack->Particle(label);
@@ -310,42 +531,26 @@ void AlidNdEtaSystematicsSelector::FillSecondaries(TObjArray* listOfTracks)
       continue;
     }
 
-    Int_t nPrim  = stack->GetNprimary();
-    if (label < nPrim)
-      nPrimaries->Fill(particle->Pt());
+   if (label < stack->GetNprimary())
+      allTracksPrimaries++;
     else
-      nSecondaries->Fill(particle->Pt());
-  }
+      allTracksSecondaries++;
 
-  for (Int_t i=1; i<=nPrimaries->GetNbinsX(); ++i)
-  {
-    Int_t primaries = (Int_t) nPrimaries->GetBinContent(i);
-    Int_t secondaries = (Int_t) nSecondaries->GetBinContent(i);
-
-    if (primaries + secondaries > 0)
-    {
-      AliDebug(AliLog::kDebug, Form("The ratio between primaries and secondaries is %d:%d = %f", primaries, secondaries, ((secondaries > 0) ? (Double_t) primaries / secondaries : -1)));
+    if (!fEsdTrackCuts->AcceptTrack(esdTrack))
+      continue;
 
-      for (Double_t factor = 0.5; factor < 2.01; factor += 0.1)
-      {
-        Double_t nTracks = (Double_t) primaries + (Double_t) secondaries * factor;
-        fSecondaries->Fill(nTracks, factor, nPrimaries->GetBinCenter(i));
-        //if (secondaries > 0) printf("We fill: %f %f %f\n", nTracks, factor, nPrimaries->GetBinCenter(i));
-      }
-    }
+    if (label < stack->GetNprimary())
+      acceptedTracksPrimaries++;
+    else
+      acceptedTracksSecondaries++;
   }
 
-  fOverallPrimaries += (Int_t) nPrimaries->Integral();
-  fOverallSecondaries += (Int_t) nSecondaries->Integral();
-
-  delete nPrimaries;
-  nPrimaries = 0;
+  fSecondaries->Fill(5, allTracksPrimaries);
+  fSecondaries->Fill(6, allTracksSecondaries);
+  fSecondaries->Fill(7, acceptedTracksPrimaries);
+  fSecondaries->Fill(8, acceptedTracksSecondaries);
 
-  delete nSecondaries;
-  nSecondaries = 0;
-
-  delete iter;
-  iter = 0;
+  //printf("P = %d, S = %d, P_pt = %d, S_pt = %d, T_P = %d, T_S = %d, T_P_acc = %d, T_S_acc = %d\n", particlesPrimaries, particlesSecondaries, particlesPrimariesPtCut, particlesSecondariesPtCut, allTracksPrimaries, allTracksSecondaries, acceptedTracksPrimaries, acceptedTracksSecondaries);
 }
 
 void AlidNdEtaSystematicsSelector::FillSigmaVertex()
@@ -370,7 +575,7 @@ void AlidNdEtaSystematicsSelector::FillSigmaVertex()
 
     Float_t sigma2Vertex = fEsdTrackCuts->GetSigmaToVertex(esdTrack);
 
-    for (Double_t nSigma = 0.5; nSigma < 5.1; nSigma += 0.5)
+    for (Double_t nSigma = 0.1; nSigma < 5.05; nSigma += 0.1)
     {
       if (sigma2Vertex < nSigma)
         fSigmaVertex->Fill(nSigma);
@@ -406,11 +611,20 @@ void AlidNdEtaSystematicsSelector::SlaveTerminate()
     fOutput->Add(fSecondaries);
 
   for (Int_t i=0; i<4; ++i)
-    if (fdNdEtaCorrection[i])
-      fOutput->Add(fdNdEtaCorrection[i]);
+    if (fdNdEtaCorrectionSpecies[i])
+      fOutput->Add(fdNdEtaCorrectionSpecies[i]);
 
   if (fSigmaVertex)
     fOutput->Add(fSigmaVertex);
+
+  for (Int_t i=0; i<3; ++i) {
+    if (fdNdEtaCorrectionVertexReco[i])
+      fOutput->Add(fdNdEtaCorrectionVertexReco[i]);
+    
+    if (fdNdEtaCorrectionTriggerBias[i])
+      fOutput->Add(fdNdEtaCorrectionTriggerBias[i]);
+  }
+  
 }
 
 void AlidNdEtaSystematicsSelector::Terminate()
@@ -421,19 +635,43 @@ void AlidNdEtaSystematicsSelector::Terminate()
 
   AliSelector::Terminate();
 
-  fSecondaries = dynamic_cast<TH3F*> (fOutput->FindObject("fSecondaries"));
+  fSecondaries = dynamic_cast<TH2F*> (fOutput->FindObject("fSecondaries"));
   for (Int_t i=0; i<4; ++i)
-    fdNdEtaCorrection[i] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject(Form("correction_%d", i)));
+    fdNdEtaCorrectionSpecies[i] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject(Form("correction_%d", i)));
   fSigmaVertex = dynamic_cast<TH1F*> (fOutput->FindObject("fSigmaVertex"));
 
-  TDatabasePDG* pdgDB = new TDatabasePDG;
+  fdNdEtaCorrectionVertexReco[0] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("vertexRecoND"));
+  fdNdEtaCorrectionVertexReco[1] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("vertexRecoSD"));
+  fdNdEtaCorrectionVertexReco[2] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("vertexRecoDD"));
 
-  for (Int_t i=0; i <= fPIDParticles->GetNbinsX()+1; ++i)
-    if (fPIDParticles->GetBinContent(i) > 0)
-      printf("PDG = %d (%s): generated: %d, reconstructed: %d, ratio: %f\n", (Int_t) fPIDParticles->GetBinCenter(i), pdgDB->GetParticle((Int_t) fPIDParticles->GetBinCenter(i))->GetName(), (Int_t) fPIDParticles->GetBinContent(i), (Int_t) fPIDTracks->GetBinContent(i), ((fPIDTracks->GetBinContent(i) > 0) ? fPIDParticles->GetBinContent(i) / fPIDTracks->GetBinContent(i) : -1));
+  fdNdEtaCorrectionTriggerBias[0] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("triggerBiasND"));
+  fdNdEtaCorrectionTriggerBias[1] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("triggerBiasSD"));
+  fdNdEtaCorrectionTriggerBias[2] = dynamic_cast<AlidNdEtaCorrection*> (fOutput->FindObject("triggerBiasDD"));
+
+  if (fPIDParticles)
+  {
+    TDatabasePDG* pdgDB = new TDatabasePDG;
+
+    for (Int_t i=0; i <= fPIDParticles->GetNbinsX()+1; ++i)
+      if (fPIDParticles->GetBinContent(i) > 0)
+        printf("PDG = %d (%s): generated: %d, reconstructed: %d, ratio: %f\n", 
+              (Int_t) fPIDParticles->GetBinCenter(i), 
+              pdgDB->GetParticle((Int_t) fPIDParticles->GetBinCenter(i))->GetName(), 
+              (Int_t) fPIDParticles->GetBinContent(i), 
+              (Int_t) fPIDTracks->GetBinContent(i), 
+              ((fPIDTracks->GetBinContent(i) > 0) ? fPIDParticles->GetBinContent(i) / fPIDTracks->GetBinContent(i) : -1));
+
+    delete pdgDB;
+    pdgDB = 0;
+  }
 
-  delete pdgDB;
-  pdgDB = 0;
+  for (Int_t i=0; i<3; ++i) {
+    if (fdNdEtaCorrectionVertexReco[i])
+      fdNdEtaCorrectionVertexReco[i]->Finish();
+    
+    if (fdNdEtaCorrectionTriggerBias[i])
+      fdNdEtaCorrectionTriggerBias[i]->Finish();
+  }
 
   TFile* fout = TFile::Open("systematics.root", "RECREATE");
 
@@ -441,17 +679,22 @@ void AlidNdEtaSystematicsSelector::Terminate()
     fEsdTrackCuts->SaveHistograms("esd_track_cuts");
 
   if (fSecondaries)
-  {
     fSecondaries->Write();
-    printf("We had %d primaries and %d secondaries.\n", (Int_t) fOverallPrimaries, (Int_t) fOverallSecondaries);
-  }
 
   if (fSigmaVertex)
     fSigmaVertex->Write();
-
+  
   for (Int_t i=0; i<4; ++i)
-    if (fdNdEtaCorrection[i])
-      fdNdEtaCorrection[i]->SaveHistograms();
+    if (fdNdEtaCorrectionSpecies[i])
+      fdNdEtaCorrectionSpecies[i]->SaveHistograms();
+
+  for (Int_t i=0; i<3; ++i) {
+    if (fdNdEtaCorrectionVertexReco[i])
+      fdNdEtaCorrectionVertexReco[i]->SaveHistograms();
+
+    if (fdNdEtaCorrectionTriggerBias[i])
+      fdNdEtaCorrectionTriggerBias[i]->SaveHistograms();
+  }
 
   fout->Write();
   fout->Close();
@@ -459,7 +702,7 @@ void AlidNdEtaSystematicsSelector::Terminate()
   if (fSecondaries)
   {
     new TCanvas;
-    fSecondaries->Draw();
+    fSecondaries->Draw("COLZ");
   }
 
   if (fSigmaVertex)
@@ -468,3 +711,32 @@ void AlidNdEtaSystematicsSelector::Terminate()
     fSigmaVertex->Draw();
   }
 }
+
+Bool_t AlidNdEtaSystematicsSelector::SignOK(TParticlePDG* particle)
+{
+  // returns if a particle with this sign should be counted
+  // this is determined by the value of fSignMode, which should have the same sign
+  // as the charge
+  // if fSignMode is 0 all particles are counted
+
+  if (fSignMode == 0)
+    return kTRUE;
+
+  if (!particle)
+  {
+    printf("WARNING: not counting a particle that does not have a pdg particle\n");
+    return kFALSE;
+  }
+
+  Double_t charge = particle->Charge();
+
+  if (fSignMode > 0)
+    if (charge < 0)
+      return kFALSE;
+
+  if (fSignMode < 0)
+    if (charge > 0)
+      return kFALSE;
+
+  return kTRUE;
+}