]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PWG1/TRD/AliTRDinfoGen.cxx
fix train AddWagon compilation
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDinfoGen.cxx
index 4dc3f59846f67172e28e33a92c0c5f1166d11f7a..9485f35de737e5c5693ea452684ee83a2a1142ab 100644 (file)
@@ -34,7 +34,9 @@
 #include <TClonesArray.h>
 #include <TObjArray.h>
 #include <TObject.h>
-#include <TH1F.h>
+#include <TString.h>
+#include <TH1S.h>
+#include <TPad.h>
 #include <TFile.h>
 #include <TTree.h>
 #include <TROOT.h>
@@ -52,6 +54,8 @@
 #include "AliESDfriendTrack.h"
 #include "AliESDHeader.h"
 #include "AliESDtrack.h"
+#include "AliESDv0.h"
+#include "AliESDtrackCuts.h"
 #include "AliMCParticle.h"
 #include "AliPID.h"
 #include "AliStack.h"
 #include "info/AliTRDtrackInfo.h"
 #include "info/AliTRDeventInfo.h"
 #include "info/AliTRDv0Info.h"
+#include "info/AliTRDeventCuts.h"
+#include "macros/AliTRDperformanceTrain.h"
 
 ClassImp(AliTRDinfoGen)
 
+const Float_t AliTRDinfoGen::fgkITS = 100.; // to be checked
 const Float_t AliTRDinfoGen::fgkTPC = 290.;
-const Float_t AliTRDinfoGen::fgkTOF = 365.;
+const Float_t AliTRDinfoGen::fgkTRD = 365.;
+
+const Float_t AliTRDinfoGen::fgkEvVertexZ = 15.;
+const Int_t   AliTRDinfoGen::fgkEvVertexN = 1;
+
+const Float_t AliTRDinfoGen::fgkTrkDCAxy  = 3.;
+const Float_t AliTRDinfoGen::fgkTrkDCAz   = 10.;
+const Int_t   AliTRDinfoGen::fgkNclTPC    = 70;
+const Float_t AliTRDinfoGen::fgkPt        = 0.2;
+const Float_t AliTRDinfoGen::fgkEta       = 0.9;
 
 //____________________________________________________________________
-AliTRDinfoGen::AliTRDinfoGen():
-  AliTRDrecoTask("infoGen", "MC-REC TRD-track list generator")
-  ,fESDev(0x0)
-  ,fMCev(0x0)
-  ,fESDfriend(0x0)
-  ,fTrackInfo(0x0)
-  ,fEventInfo(0x0)
-  ,fV0container(0x0)
-  ,fV0Info(0x0)
+AliTRDinfoGen::AliTRDinfoGen()
+  :AliAnalysisTaskSE()
+  ,fEvTrigger(NULL)
+  ,fESDev(NULL)
+  ,fMCev(NULL)
+  ,fEventCut(NULL)
+  ,fTrackCut(NULL)
+  ,fV0Cut(NULL)
+  ,fTrackInfo(NULL)
+  ,fEventInfo(NULL)
+  ,fV0Info(NULL)
+  ,fTracksBarrel(NULL)
+  ,fTracksSA(NULL)
+  ,fTracksKink(NULL)
+  ,fV0List(NULL)
+  ,fContainer(NULL)
+  ,fDebugStream(NULL)
 {
   //
   // Default constructor
   //
+  SetNameTitle("infoGen", "MC-REC TRD-track list generator");
+}
 
-  DefineInput(0, TChain::Class());
-  DefineOutput(0, TObjArray::Class());
-  DefineOutput(1, AliTRDeventInfo::Class());
-  DefineOutput(2, TObjArray::Class());
+//____________________________________________________________________
+AliTRDinfoGen::AliTRDinfoGen(char* name)
+  :AliAnalysisTaskSE(name)
+  ,fEvTrigger(NULL)
+  ,fESDev(NULL)
+  ,fMCev(NULL)
+  ,fEventCut(NULL)
+  ,fTrackCut(NULL)
+  ,fV0Cut(NULL)
+  ,fTrackInfo(NULL)
+  ,fEventInfo(NULL)
+  ,fV0Info(NULL)
+  ,fTracksBarrel(NULL)
+  ,fTracksSA(NULL)
+  ,fTracksKink(NULL)
+  ,fV0List(NULL)
+  ,fContainer(NULL)
+  ,fDebugStream(NULL)
+{
+  //
+  // Default constructor
+  //
+  SetTitle("MC-REC TRD-track list generator");
+  DefineOutput(kTracksBarrel, TObjArray::Class());
+  DefineOutput(kTracksSA, TObjArray::Class());
+  DefineOutput(kTracksKink, TObjArray::Class());
+  DefineOutput(kEventInfo, AliTRDeventInfo::Class());
+  DefineOutput(kV0List, TObjArray::Class());
+  DefineOutput(kMonitor, TObjArray::Class()); // histogram list
 }
 
 //____________________________________________________________________
 AliTRDinfoGen::~AliTRDinfoGen()
 {
 // Destructor
-  if(fTrackInfo) delete fTrackInfo; fTrackInfo = 0x0;
-  if(fEventInfo) delete fEventInfo; fEventInfo = 0x0;
-  if(fV0Info) delete fV0Info; fV0Info = 0x0;
-  if(fContainer){ 
-    fContainer->Delete(); delete fContainer;
-    fContainer = 0x0;
+  
+  if(fDebugStream) delete fDebugStream;
+  if(fEvTrigger) delete fEvTrigger;
+  if(fV0Cut) delete fV0Cut;
+  if(fTrackCut) delete fTrackCut;
+  if(fEventCut) delete fEventCut;
+  if(fTrackInfo) delete fTrackInfo; fTrackInfo = NULL;
+  if(fEventInfo) delete fEventInfo; fEventInfo = NULL;
+  if(fV0Info) delete fV0Info; fV0Info = NULL;
+  if(fTracksBarrel){ 
+    fTracksBarrel->Delete(); delete fTracksBarrel;
+    fTracksBarrel = NULL;
+  }
+  if(fTracksSA){ 
+    fTracksSA->Delete(); delete fTracksSA;
+    fTracksSA = NULL;
   }
-  if(fV0container){ 
-    fV0container->Delete(); delete fV0container;
-    fV0container = 0x0;
+  if(fTracksKink){ 
+    fTracksKink->Delete(); delete fTracksKink;
+    fTracksKink = NULL;
+  }
+  if(fV0List){ 
+    fV0List->Delete(); 
+    delete fV0List;
+    fV0List = NULL;
+  }
+  if(fContainer){ 
+    fContainer->Delete(); 
+    delete fContainer;
+    fContainer = NULL;
   }
 }
 
 //____________________________________________________________________
-void AliTRDinfoGen::ConnectInputData(Option_t *)
+Bool_t AliTRDinfoGen::GetRefFigure(Int_t)
 {
-  //
-  // Link the Input Data
-  //
-  TTree *tree = dynamic_cast<TChain*>(GetInputData(0));
-  if(!tree){
-    printf("ERROR - ESD event not found");
-  } else {
-    tree->SetBranchStatus("Tracks", 1);
-    tree->SetBranchStatus("ESDfriend*",1);
-  }
-
-  AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*>(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
-  if(!esdH){
-    printf("ERROR - ESD input handler not found");
-  } else {
-    fESDev = esdH->GetEvent();
-    if(!fESDev){
-      printf("ERROR - ESD event not found");
-    } else {
-      esdH->SetActiveBranches("ESDfriend*");
-      fESDfriend = (AliESDfriend *)fESDev->FindListObject("AliESDfriend");
-    }
-  }
-  if(HasMCdata()){
-    AliMCEventHandler *mcH = dynamic_cast<AliMCEventHandler*>(AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
-    if(!mcH){ 
-      AliError("MC input handler not found");
-    } else {
-      fMCev = mcH->MCEvent();
-    }
+  if(!gPad){
+    AliWarning("Please provide a canvas to draw results.");
+    return kFALSE;
   }
+  fContainer->At(0)->Draw("bar");
+  return kTRUE;
 }
 
 //____________________________________________________________________
-void AliTRDinfoGen::CreateOutputObjects()
+void AliTRDinfoGen::UserCreateOutputObjects()
 {      
   //
   // Create Output Containers (TObjectArray containing 1D histograms)
   //
   fTrackInfo = new AliTRDtrackInfo();
   fEventInfo = new AliTRDeventInfo();
   fV0Info    = new AliTRDv0Info();
-  fContainer = new TObjArray(1000);
-  fContainer->SetOwner(kTRUE);
-  fV0container = new TObjArray(50);
-  fV0container->SetOwner(kTRUE);
+  fTracksBarrel = new TObjArray(200); fTracksBarrel->SetOwner(kTRUE);
+  fTracksSA = new TObjArray(20); fTracksSA->SetOwner(kTRUE);
+  fTracksKink = new TObjArray(20); fTracksKink->SetOwner(kTRUE);
+  fV0List = new TObjArray(10); fV0List->SetOwner(kTRUE);
+
+  // define general monitor
+  fContainer = new TObjArray(1); fContainer->SetOwner(kTRUE);
+  TH1 *h=new TH1S("hStat", "Run statistics;Observable;Entries", 12, -0.5, 11.5);
+  TAxis *ax(h->GetXaxis());
+  ax->SetBinLabel( 1, "ESD");
+  ax->SetBinLabel( 2, "MC");
+  ax->SetBinLabel( 3, "V0");
+  ax->SetBinLabel( 4, "TPC");
+  ax->SetBinLabel( 5, "TRDin");
+  ax->SetBinLabel( 6, "TRDout");
+  ax->SetBinLabel( 7, "Barrel");
+  ax->SetBinLabel( 8, "BarrelMC");
+  ax->SetBinLabel( 9, "SA");
+  ax->SetBinLabel(10, "SAMC");
+  ax->SetBinLabel(11, "Kink");
+  ax->SetBinLabel(12, "KinkMC");
+  fContainer->AddAt(h, 0);
+}
+
+//____________________________________________________________________
+Bool_t AliTRDinfoGen::Load(const Char_t *file, const Char_t *dir, const Char_t *name)
+{
+// Load data from performance file
 
+  if(!TFile::Open(file)){
+    AliWarning(Form("Couldn't open file %s.", file));
+    return kFALSE;
+  }
+  if(dir){
+    if(!gFile->cd(dir)){
+      AliWarning(Form("Couldn't cd to %s in %s.", dir, file));
+      return kFALSE;
+    }
+  }
+  TObjArray *o(NULL);
+  const Char_t *tn=(name ? name : GetName());
+  if(!(o = (TObjArray*)gDirectory->Get(tn))){
+    AliWarning(Form("Missing histogram container %s.", tn));
+    return kFALSE;
+  }
+  fContainer = (TObjArray*)o->Clone(GetName());
+  gFile->Close();
+  return kTRUE;
 }
 
 //____________________________________________________________________
-void AliTRDinfoGen::Exec(Option_t *){
+void AliTRDinfoGen::UserExec(Option_t *){
   //
   // Run the Analysis
   //
+
+  fESDev = dynamic_cast<AliESDEvent*>(InputEvent());
+  fMCev = MCEvent();
+
   if(!fESDev){
     AliError("Failed retrieving ESD event");
     return;
   }
+
+  // event selection : trigger cut
+  if(UseLocalEvSelection() && fEvTrigger){ 
+    Bool_t kTRIGGERED(kFALSE);
+    const TObjArray *trig = fEvTrigger->Tokenize(" ");
+    for(Int_t itrig=trig->GetEntriesFast(); itrig--;){
+      const Char_t *trigClass(((TObjString*)(*trig)[itrig])->GetName());
+      if(fESDev->IsTriggerClassFired(trigClass)) {
+        AliDebug(2, Form("Ev[%4d] Trigger[%s]", fESDev->GetEventNumberInFile(), trigClass));
+        kTRIGGERED = kTRUE;
+        break; 
+      }
+    }
+    if(!kTRIGGERED){ 
+      AliDebug(2, Form("Reject Ev[%4d] Trigger", fESDev->GetEventNumberInFile()));
+      return;
+    }
+    // select only physical events
+    if(fESDev->GetEventType() != 7){ 
+      AliDebug(2, Form("Reject Ev[%4d] EvType[%d]", fESDev->GetEventNumberInFile(), fESDev->GetEventType()));
+      return;
+    }
+  }
+
+  // if the required trigger is a collision trigger then apply event vertex cut
+  if(UseLocalEvSelection() && IsCollision()){
+    const AliESDVertex *vertex = fESDev->GetPrimaryVertex();
+    if(TMath::Abs(vertex->GetZv())<1.e-10 || 
+       TMath::Abs(vertex->GetZv())>fgkEvVertexZ || 
+       vertex->GetNContributors()<fgkEvVertexN) {
+      AliDebug(2, Form("Reject Ev[%4d] Vertex Zv[%f] Nv[%d]", fESDev->GetEventNumberInFile(), TMath::Abs(vertex->GetZv()), vertex->GetNContributors()));
+      return;
+    }
+  }
+
+  if(fEventCut && !fEventCut->IsSelected(fESDev, IsCollision())) return;
+
   if(!fESDfriend){
     AliError("Failed retrieving ESD friend event");
     return;
@@ -184,14 +315,16 @@ void AliTRDinfoGen::Exec(Option_t *){
     AliError("Failed retrieving MC event");
     return;
   }
-  fContainer->Delete();
-  fV0container->Delete();
+
+  fTracksBarrel->Delete();
+  fTracksSA->Delete();
+  fTracksKink->Delete();
+  fV0List->Delete();
   fEventInfo->Delete("");
-  fESDev->SetESDfriend(fESDfriend);
   new(fEventInfo)AliTRDeventInfo(fESDev->GetHeader(), const_cast<AliESDRun *>(fESDev->GetESDRun()));
   
-  Bool_t *trackMap = 0x0;
-  AliStack * mStack = 0x0;
+  Bool_t *trackMap(NULL);
+  AliStack * mStack(NULL);
   Int_t nTracksMC = HasMCdata() ? fMCev->GetNumberOfTracks() : 0, nTracksESD = fESDev->GetNumberOfTracks();
   if(HasMCdata()){
     mStack = fMCev->Stack();
@@ -203,19 +336,26 @@ void AliTRDinfoGen::Exec(Option_t *){
     memset(trackMap, 0, sizeof(Bool_t) * nTracksMC);
   }
   
-  Int_t nTRD = 0, nTPC = 0, nclsTrklt;
-  AliDebug(2, Form("Entry[%3d] Tracks: ESD[%d] MC[%d]\n", (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTracksESD, nTracksMC));
-  AliESDtrack *esdTrack = 0x0;
-  AliESDfriendTrack *esdFriendTrack = 0x0;
-  TObject *calObject = 0x0;
-  AliTRDtrackV1 *track = 0x0;
-  AliTRDseedV1 *tracklet = 0x0;
-  AliTRDcluster *cl = 0x0;
+  Double32_t dedx[100]; Int_t nSlices(0);
+  Int_t nTRDout(0), nTRDin(0), nTPC(0)
+       ,nclsTrklt
+       ,nBarrel(0), nSA(0), nKink(0)
+       ,nBarrelMC(0), nSAMC(0), nKinkMC(0);
+  AliESDtrack *esdTrack = NULL;
+  AliESDfriendTrack *esdFriendTrack = NULL;
+  TObject *calObject = NULL;
+  AliTRDtrackV1 *track = NULL;
+  AliTRDseedV1 *tracklet = NULL;
+  AliTRDcluster *cl = NULL;
+  // LOOP 0 - over ESD tracks
   for(Int_t itrk = 0; itrk < nTracksESD; itrk++){
+    new(fTrackInfo) AliTRDtrackInfo();
     esdTrack = fESDev->GetTrack(itrk);
     AliDebug(3, Form("\n%3d ITS[%d] TPC[%d] TRD[%d]\n", itrk, esdTrack->GetNcls(0), esdTrack->GetNcls(1), esdTrack->GetNcls(2)));
-    if(esdTrack->GetNcls(1)) nTPC++;
-    if(esdTrack->GetNcls(2)) nTRD++;
+
+    if(esdTrack->GetStatus()&AliESDtrack::kTPCout) nTPC++;
+    if(esdTrack->GetStatus()&AliESDtrack::kTRDout) nTRDout++;
+    if(esdTrack->GetStatus()&AliESDtrack::kTRDin) nTRDin++;
 
     // look at external track param
     const AliExternalTrackParam *op = esdTrack->GetOuterParam();
@@ -239,35 +379,33 @@ void AliTRDinfoGen::Exec(Option_t *){
         AliError(Form("MC label[%d] outside scope for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
         continue; 
       }
-      AliMCParticle *mcParticle = 0x0
+      AliMCParticle *mcParticle = NULL
       if(!(mcParticle = (AliMCParticle*) fMCev->GetTrack(alab))){
         AliError(Form("MC particle label[%d] missing for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
         continue;
       }
       fPdg = mcParticle->Particle()->GetPdgCode();
       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
-      Int_t iref = 0; AliTrackReference *ref = 0x0
+      Int_t iref = 0; AliTrackReference *ref = NULL
       while(iref<nRefs){
         ref = mcParticle->GetTrackReference(iref);
         if(ref->LocalX() > fgkTPC) break;
         iref++;
       }
 
-      new(fTrackInfo) AliTRDtrackInfo();
+      fTrackInfo->SetMC();
       fTrackInfo->SetPDG(fPdg);
       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
+      fTrackInfo->SetLabel(label);
       Int_t jref = iref;//, kref = 0;
       while(jref<nRefs){
         ref = mcParticle->GetTrackReference(jref);
-        if(ref->LocalX() > fgkTOF) break;
+        if(ref->LocalX() > fgkTRD) break;
         AliDebug(4, Form("  trackRef[%2d (%2d)] @ %7.3f OK", jref-iref, jref, ref->LocalX()));
         fTrackInfo->AddTrackRef(ref);
         jref++;
       }
       AliDebug(3, Form("NtrackRefs[%d(%d)]", fTrackInfo->GetNTrackRefs(), nRefs));
-    } else {
-      new (fTrackInfo) AliTRDtrackInfo();
-      fTrackInfo->SetPDG(fPdg);
     }
 
     // copy some relevant info to TRD track info
@@ -276,7 +414,14 @@ void AliTRDinfoGen::Exec(Option_t *){
     Double_t p[AliPID::kSPECIES]; esdTrack->GetTRDpid(p);
     fTrackInfo->SetESDpid(p);
     fTrackInfo->SetESDpidQuality(esdTrack->GetTRDntrackletsPID());
-    fTrackInfo->SetLabel(label);
+    if(!nSlices) nSlices = esdTrack->GetNumberOfTRDslices();
+    memset(dedx, 0, 100*sizeof(Double32_t));
+    Int_t in(0);
+    for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++)
+      for(Int_t is=0; is<nSlices; is++) 
+        dedx[in++]=esdTrack->GetTRDslice(il, is);
+    for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++) dedx[in++]=esdTrack->GetTRDmomentum(il);
+    fTrackInfo->SetSlices(in, dedx);
     fTrackInfo->SetNumberOfClustersRefit(esdTrack->GetNcls(2));
     // some other Informations which we may wish to store in order to find problematic cases
     fTrackInfo->SetKinkIndex(esdTrack->GetKinkIndex(0));
@@ -291,7 +436,6 @@ void AliTRDinfoGen::Exec(Option_t *){
       while((calObject = esdFriendTrack->GetCalibObject(icalib++))){
         if(strcmp(calObject->IsA()->GetName(),"AliTRDtrackV1") != 0) continue; // Look for the TRDtrack
         if(!(track = dynamic_cast<AliTRDtrackV1*>(calObject))) break;
-        nTRD++;
         AliDebug(4, Form("TRD track OK"));
         // Set the clusters to unused
         for(Int_t ipl = 0; ipl < AliTRDgeometry::kNlayer; ipl++){
@@ -313,19 +457,92 @@ void AliTRDinfoGen::Exec(Option_t *){
       << "\n";
       info.Delete("");
     }
-  
-    fContainer->Add(new AliTRDtrackInfo(*fTrackInfo));
+
+    ULong_t status(esdTrack->GetStatus());
+    if((status&AliESDtrack::kTPCout)){
+      if(!esdTrack->GetKinkIndex(0)){ // Barrel  Track Selection
+        Bool_t selected(kTRUE);
+        if(UseLocalTrkSelection()){
+          if(esdTrack->Pt() < fgkPt){ 
+            AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Pt[%5.2f]", fESDev->GetEventNumberInFile(), itrk, esdTrack->Pt()));
+            selected = kFALSE;
+          }
+          if(selected && TMath::Abs(esdTrack->Eta()) > fgkEta){
+            AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Eta[%5.2f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(esdTrack->Eta())));
+            selected = kFALSE;
+          }
+          if(selected && esdTrack->GetTPCNcls() < fgkNclTPC){ 
+            AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] NclTPC[%d]", fESDev->GetEventNumberInFile(), itrk, esdTrack->GetTPCNcls()));
+            selected = kFALSE;
+          }
+          Float_t par[2], cov[3];
+          esdTrack->GetImpactParameters(par, cov);
+          if(IsCollision()){ // cuts on DCA
+            if(selected && TMath::Abs(par[0]) > fgkTrkDCAxy){ 
+              AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAxy[%f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(par[0])));
+              selected = kFALSE;
+            }
+            if(selected && TMath::Abs(par[1]) > fgkTrkDCAz){ 
+              AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAz[%f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(par[1])));
+              selected = kFALSE;
+            }
+          } else if(selected && fMCev && !fMCev->IsPhysicalPrimary(alab)){;
+            AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Primary", fESDev->GetEventNumberInFile(), itrk));
+            selected = kFALSE;
+          }
+        }
+        if(fTrackCut && !fTrackCut->IsSelected(esdTrack)) selected = kFALSE;
+        if(selected){ 
+          fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
+          nBarrel++;
+        }
+      } else {
+        fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
+        nKink++;
+      }
+    } else if((status&AliESDtrack::kTRDout) && !(status&AliESDtrack::kTRDin)){ 
+      fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
+      nSA++;
+    }
     fTrackInfo->Delete("");
   }
-  AliDebug(3, Form("%3d Tracks: TPC[%d] TRD[%d]\n", (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTPC, nTRD));
 
-//   AliESDv0 *v0 = 0x0;
-//   for(Int_t iv0=0; iv0<fESD->GetNumberOfV0s(); iv0++){
-//     if(!(v0 = fESD->GetV0(iv0))) continue;
-//     fV0container->Add(new AliTRDv0Info(v0));
-//   }
+  // LOOP 1 - over ESD v0s
+  Bool_t kFOUND(kFALSE);
+  Float_t bField(fESDev->GetMagneticField());
+  AliESDv0 *v0(NULL);
+  for(Int_t iv0(0); iv0<fESDev->GetNumberOfV0s(); iv0++){
+    if(!(v0 = fESDev->GetV0(iv0))) continue;
+
+    // purge V0 irelevant for TRD
+    Int_t jb(0); AliTRDtrackInfo *ti(NULL);
+    for(Int_t ib(0); ib<nBarrel; ib++){ 
+      ti =   (AliTRDtrackInfo*)fTracksBarrel->At(ib);
+      Int_t id(ti->GetTrackId());
+      if(id!=v0->GetPindex() && id!=v0->GetNindex()) continue; 
+      kFOUND=kTRUE; ti->SetV0(); jb=ib+1;
+      break;
+    }
+    if(!kFOUND) continue;
+
+    // register v0
+    if(fV0Cut) new(fV0Info) AliTRDv0Info(*fV0Cut);
+    else new(fV0Info) AliTRDv0Info();
+    fV0Info->SetMagField(bField);
+    fV0Info->SetV0tracks(fESDev->GetTrack(v0->GetPindex()), fESDev->GetTrack(v0->GetNindex()));
+    fV0Info->SetV0Info(v0);
+    // mark the other leg too 
+    for(Int_t ib(jb); ib<nBarrel; ib++){ 
+      ti =   (AliTRDtrackInfo*)fTracksBarrel->At(ib);
+      if(!fV0Info->HasTrack(ti)) continue;
+      ti->SetV0();
+      break;
+    }
+    //fV0Info->Print("a");
+    fV0List->Add(new AliTRDv0Info(*fV0Info)); kFOUND=kFALSE;
+  }
 
-  // Insert also MC tracks which are passing TRD where the track is not reconstructed
+  // LOOP 2 - over MC tracks which are passing TRD where the track is not reconstructed
   if(HasMCdata()){
     AliDebug(10, "Output of the MC track map:");
     for(Int_t itk = 0; itk < nTracksMC;  itk++) AliDebug(10, Form("trackMap[%d] = %s", itk, trackMap[itk] == kTRUE ? "TRUE" : "kFALSE"));
@@ -335,18 +552,19 @@ void AliTRDinfoGen::Exec(Option_t *){
       AliMCParticle *mcParticle =  (AliMCParticle*) fMCev->GetTrack(TMath::Abs(itk));
       Int_t fPdg = mcParticle->Particle()->GetPdgCode();
       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
-      Int_t iref = 0; AliTrackReference *ref = 0x0
+      Int_t iref = 0; AliTrackReference *ref = NULL
       Int_t nRefsTRD = 0;
       new(fTrackInfo) AliTRDtrackInfo();
+      fTrackInfo->SetMC();
       fTrackInfo->SetPDG(fPdg);
-      while(iref<nRefs){
+      while(iref<nRefs){ // count TRD TR
+        Bool_t kIN(kFALSE);
         ref = mcParticle->GetTrackReference(iref);
-        if(ref->LocalX() > 250. && ref->LocalX() < 370.){
-          AliDebug(4, Form("  trackRef[%2d] @ %7.3f IN", iref, ref->LocalX()));
+        if(ref->LocalX() > fgkTPC && ref->LocalX() < fgkTRD){
           fTrackInfo->AddTrackRef(ref);
-          nRefsTRD++;
+          nRefsTRD++;kIN=kTRUE;
         }
-        else AliDebug(4, Form("  trackRef[%2d] @ %7.3f OUT", iref, ref->LocalX()));
+        AliDebug(4, Form("  trackRef[%2d] @ x[%7.3f] %s", iref, ref->LocalX(), kIN?"IN":"OUT"));
         iref++;
       }
       if(!nRefsTRD){
@@ -364,14 +582,82 @@ void AliTRDinfoGen::Exec(Option_t *){
         << "\n";
         info.Delete("");
       }
-      AliDebug(3, Form("Registering rejected MC track with label %d", itk));
-      fContainer->Add(new AliTRDtrackInfo(*fTrackInfo));
+      AliDebug(3, Form("Add MC track @ label[%d] nTRDrefs[%d].", itk, nRefsTRD));
+      // check where the track starts
+      ref = mcParticle->GetTrackReference(0);
+      if(ref->LocalX() < fgkITS){ 
+        fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
+        nBarrelMC++;
+      } else if(ref->LocalX() < fgkTPC) {
+        fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
+        nKinkMC++;
+      } else if(nRefsTRD>6){
+        fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
+        nSAMC++;
+      }
       fTrackInfo->Delete("");
     }
     delete[] trackMap;
   }
-  PostData(0, fContainer);
-  PostData(1, fEventInfo);
-  PostData(2, fV0container);
+  AliDebug(1, Form(
+    "\nEv[%3d] Tracks: ESD[%d] MC[%d] V0[%d]\n"
+    "        TPCout[%d] TRDin[%d] TRDout[%d]\n"
+    "        Barrel[%3d+%3d=%3d] SA[%2d+%2d=%2d] Kink[%2d+%2d=%2d]"
+    ,(Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTracksESD, nTracksMC, fV0List->GetEntries()
+    , nTPC, nTRDin, nTRDout
+    ,nBarrel, nBarrelMC, fTracksBarrel->GetEntries()
+    ,nSA, nSAMC, fTracksSA->GetEntries()
+    ,nKink, nKinkMC, fTracksKink->GetEntries()
+  ));
+  // save track statistics
+  TH1 *h((TH1S*)fContainer->At(0));
+  h->Fill( 0., nTracksESD);
+  h->Fill( 1., nTracksMC);
+  h->Fill( 2., fV0List->GetEntries());
+  h->Fill( 3., nTPC);
+  h->Fill( 4., nTRDin);
+  h->Fill( 5., nTRDout);
+  h->Fill( 6., nBarrel);
+  h->Fill( 7., nBarrelMC);
+  h->Fill( 8., nSA);
+  h->Fill( 9., nSAMC);
+  h->Fill(10., nKink);
+  h->Fill(11., nKinkMC);
+
+  PostData(kTracksBarrel, fTracksBarrel);
+  PostData(kTracksSA, fTracksSA);
+  PostData(kTracksKink, fTracksKink);
+  PostData(kEventInfo, fEventInfo);
+  PostData(kV0List, fV0List);
+  PostData(kMonitor, fContainer);
+}
+
+//____________________________________________________________________
+void AliTRDinfoGen::SetLocalV0Selection(AliTRDv0Info *v0)
+{
+// Set V0 cuts from outside
+
+  if(!fV0Cut) fV0Cut = new AliTRDv0Info(*v0);
+  else new(fV0Cut) AliTRDv0Info(*v0);
+}
+
+//____________________________________________________________________
+void AliTRDinfoGen::SetTrigger(const Char_t *trigger)
+{
+  if(!fEvTrigger) fEvTrigger = new TString(trigger);
+  else (*fEvTrigger) = trigger;
+}
+
+//____________________________________________________________________
+TTreeSRedirector* AliTRDinfoGen::DebugStream()
+{
+  if(!fDebugStream){
+    TDirectory *savedir = gDirectory;
+    fDebugStream = new TTreeSRedirector("TRD.DebugInfoGen.root");
+    savedir->cd();
+  }
+  return fDebugStream;
 }
 
+
+