]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSComparisonV2.C
EMCAL/DCAL Trigger Mapping for Run 2
[u/mrichter/AliRoot.git] / ITS / AliITSComparisonV2.C
index 7209f90e45ed9b2acc2cf1e4693b4833ae621b3e..34aa7df27772fca536a10602164ba1e57d997bcf 100644 (file)
-#ifndef __CINT__
-  #include <iostream.h>
-  #include <fstream.h>
-
+/****************************************************************************
+ *           Very important, delicate and rather obscure macro.             *
+ *                                                                          *
+ *               Creates list of "trackable" tracks,                        *
+ *             calculates efficiency, resolutions etc.                      *
+ *  The ESD tracks must be in an appropriate state (kITSin or kITSrefit)    *
+ *                                                                          *
+ *           Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                 *
+ ****************************************************************************/
+
+#if !defined(__CINT__) || defined(__MAKECINT__)
+  #include <TMath.h>
+  #include <TError.h>
+  #include <Riostream.h>
+  #include <TH1F.h>
+  #include <TH2F.h>
+  #include <TTree.h>
+  #include <TParticle.h>
+  #include <TCanvas.h>
+  #include <TLine.h>
+  #include <TText.h>
+  #include <TBenchmark.h>
+  #include <TStyle.h>
+  #include <TFile.h>
+  #include <TROOT.h>
+
+  #include "AliStack.h"
+  #include "AliHeader.h"
+  #include "AliTrackReference.h"
+  #include "AliRunLoader.h"
   #include "AliRun.h"
-  #include "AliITS.h"
-  #include "AliITSgeom.h"
-  #include "AliITStrackerV2.h"
-  #include "AliITStrackV2.h"
-  #include "AliITSclusterV2.h"
-
-  #include "TFile.h"
-  #include "TTree.h"
-  #include "TH1.h"
-  #include "TObjArray.h"
-  #include "TStyle.h"
-  #include "TCanvas.h"
-  #include "TLine.h"
-  #include "TText.h"
-  #include "TParticle.h"
+  #include "AliESDEvent.h"
+  #include "AliESDtrack.h"
+
+  #include "AliITSRecPoint.h"
+  #include "AliITSLoader.h"
 #endif
 
-struct GoodTrack {
-  Int_t lab;
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
-Int_t good_tracks(GoodTrack *gt, Int_t max);
-
-Int_t AliITSComparisonV2() {
-   cerr<<"Doing comparison...\n";
-
-   TFile *cf=TFile::Open("AliITSclustersV2.root");
-   if (!cf->IsOpen()) {cerr<<"Can't open AliITSclustersV2.root !\n"; return 1;}
-   AliITSgeom *geom=(AliITSgeom*)cf->Get("AliITSgeom");
-   if (!geom) { cerr<<"Can't get the ITS geometry !\n"; return 2; }
-   AliITStrackerV2 tracker(geom);   
-
-// Load tracks
-   TFile *tf=TFile::Open("AliITStracksV2.root");
-   if (!tf->IsOpen()) {cerr<<"Can't open AliITStracksV2.root !\n"; return 3;}
-   TObjArray tarray(2000);
-   TTree *tracktree=(TTree*)tf->Get("ITSf");
-   if (!tracktree) {cerr<<"Can't get a tree with ITS tracks !\n"; return 4;}
-   TBranch *tbranch=tracktree->GetBranch("tracks");
-   Int_t nentr=(Int_t)tracktree->GetEntries(),i;
-   for (i=0; i<nentr; i++) {
-       AliITStrackV2 *iotrack=new AliITStrackV2;
-       tbranch->SetAddress(&iotrack);
-       tracktree->GetEvent(i);
-
-       Int_t tpcLabel=iotrack->GetLabel();
-       tracker.CookLabel(iotrack,0.);
-       Int_t itsLabel=iotrack->GetLabel();
-       if (itsLabel != tpcLabel) iotrack->SetLabel(-TMath::Abs(itsLabel));
-       if (tpcLabel < 0)         iotrack->SetLabel(-TMath::Abs(itsLabel));
-       /*
-       if (itsLabel==1234) {
-         Int_t nc=iotrack->GetNumberOfClusters();
-         for (Int_t k=0; k<nc; k++) {
-           Int_t index=iotrack->GetClusterIndex(k);
-           AliITSclusterV2 *c=tracker.GetCluster(index);
-           cout<<c->GetLabel(0)<<' '<<c->GetLabel(1)<<' '<<c->GetLabel(2)<<endl;
-         }
-       }
-       */
-       tarray.AddLast(iotrack);
-   }   
-   tf->Close();
-   cf->Close();
-
-/////////////////////////////////////////////////////////////////////////
-   GoodTrack gt[15000];
-   Int_t ngood=0;
-   ifstream in("good_tracks_its");
-   if (in) {
-      cerr<<"Reading good tracks...\n";
-      while (in>>gt[ngood].lab>>gt[ngood].code>>
-                 gt[ngood].px>>gt[ngood].py>>gt[ngood].pz>>
-                 gt[ngood].x >>gt[ngood].y >>gt[ngood].z) {
-         ngood++;
-         cerr<<ngood<<'\r';
-         if (ngood==15000) {
-            cerr<<"Too many good tracks !\n";
-            break;
-         }
-      }
-      if (!in.eof()) cerr<<"Read error (good_tracks_its) !\n";
-   } else {
-      cerr<<"Marking good tracks (this will take a while)...\n";
-      ngood=good_tracks(gt,15000);
-      ofstream out("good_tracks_its");
-      if (out) {
-         for (Int_t ngd=0; ngd<ngood; ngd++)            
-           out<<gt[ngd].lab<<' '<<gt[ngd].code<<' '<<
-                 gt[ngd].px<<' '<<gt[ngd].py<<' '<<gt[ngd].pz<<' '<<
-                 gt[ngd].x <<' '<<gt[ngd].y <<' '<<gt[ngd].z <<endl;
-      } else cerr<<"Can not open file (good_tracks_its) !\n";
-      out.close();
-   }
-   cerr<<"Number of good tracks : "<<ngood<<endl;
+Int_t GoodTracksITS(const Char_t *dir=".");
+
+extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
+extern TROOT *gROOT;
 
-   TH1F *hp=new TH1F("hp","PHI resolution",50,-20.,20.); hp->SetFillColor(4);
-   TH1F *hl=new TH1F("hl","LAMBDA resolution",50,-20,20);hl->SetFillColor(4);
-   TH1F *hpt=new TH1F("hpt","Relative Pt resolution",30,-10.,10.); 
-   hpt->SetFillColor(2); 
-   TH1F *hmpt=new TH1F("hmpt","Transverse impact parameter",30,-300,300); 
+static Int_t allgood=0;
+static Int_t allselected=0;
+static Int_t allfound=0;
+
+Int_t AliITSComparisonV2
+(Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir=".") {
+   gBenchmark->Start("AliITSComparisonV2");
+
+   ::Info("AliITSComparisonV2.C","Doing comparison...");
+   
+
+   TH1F *hp=(TH1F*)gROOT->FindObject("hp");
+   if (!hp) hp=new TH1F("hp","PHI resolution",50,-20.,20.); 
+   hp->SetFillColor(4);
+
+   TH1F *hl=(TH1F*)gROOT->FindObject("hl");
+   if (!hl) hl=new TH1F("hl","LAMBDA resolution",50,-20,20);
+   hl->SetFillColor(4);
+
+   TH1F *hpt=(TH1F*)gROOT->FindObject("hpt");
+   if (!hpt) hpt=new TH1F("hpt","Relative Pt resolution",30,-10.,10.); 
+   hpt->SetFillColor(2);
+   TH1F *hmpt=(TH1F*)gROOT->FindObject("hmpt");
+   if (!hmpt) 
+      hmpt=new TH1F("hmpt","Transverse impact parameter",30,-777,777); 
    hmpt->SetFillColor(6);
-   TH1F *hz=new TH1F("hz","Longitudinal impact parameter",30,-300,300); 
-   //hmpt->SetFillColor(6);
 
-   AliITStrackV2 *trk=(AliITStrackV2*)tarray.UncheckedAt(0);
-   Double_t pmin=0.1*(100/0.299792458/0.2/trk->GetConvConst());
-   Double_t pmax=6.0+pmin;
+   TH1F *hz=(TH1F*)gROOT->FindObject("hz");
+   if (!hz) hz=new TH1F("hz","Longitudinal impact parameter",30,-777,777); 
+
+
+
+   TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");
+   if (!hgood) hgood=new TH1F("hgood","Good tracks",30,0.2,6.1);
+    
+   TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
+   if (!hfound) hfound=new TH1F("hfound","Found tracks",30,0.2,6.1);
+
+   TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
+   if (!hfake) hfake=new TH1F("hfake","Fake tracks",30,0.2,6.1);
 
-   TH1F *hgood=new TH1F("hgood","Good tracks",30,pmin,pmax);    
-   TH1F *hfound=new TH1F("hfound","Found tracks",30,pmin,pmax);
-   TH1F *hfake=new TH1F("hfake","Fake tracks",30,pmin,pmax);
-   TH1F *hg=new TH1F("hg","",30,pmin,pmax); //efficiency for good tracks
+   TH1F *hg=(TH1F*)gROOT->FindObject("hg");
+   if (!hg) hg=new TH1F("hg","Efficiency for good tracks",30,0.2,6.1);
    hg->SetLineColor(4); hg->SetLineWidth(2);
-   TH1F *hf=new TH1F("hf","Efficiency for fake tracks",30,pmin,pmax);
+
+   TH1F *hf=(TH1F*)gROOT->FindObject("hf");
+   if (!hf) hf=new TH1F("hf","Efficiency for fake tracks",30,0.2,6.1);
    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
 
-   TH1F *hptw=new TH1F("hptw","Weghted pt",30,pmax,pmin);
+   TH1F *he=(TH1F*)gROOT->FindObject("he");
+   if (!he) 
+      he =new TH1F("he","dE/dX for pions with 0.4<p<0.5 GeV/c",50,0.,200.);
 
-   while (ngood--) {
-      Int_t lab=gt[ngood].lab, tlab=-1;
-      Double_t pxg=gt[ngood].px, pyg=gt[ngood].py, pzg=gt[ngood].pz;
-      Double_t ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
+   TH2F *hep=(TH2F*)gROOT->FindObject("hep");
+   if (!hep) hep=new TH2F("hep","dE/dX vs momentum",50,0.,2.,50,0.,400.);
+   hep->SetMarkerStyle(8);
+   hep->SetMarkerSize(0.4);
 
-      if (ptg<pmin) continue;
 
-      hgood->Fill(ptg);
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
 
-      AliITStrackV2 *track=0;
-      Int_t j;
-      for (j=0; j<nentr; j++) {
-          track=(AliITStrackV2*)tarray.UncheckedAt(j);
-          tlab=track->GetLabel();
-          if (lab==TMath::Abs(tlab)) break;
-      }
-      if (j==nentr) {
-       cerr<<"Track "<<lab<<" was not found !\n";
-        continue;
+   TFile *refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+   ::Info("AliITSComparisonV2.C","Marking good tracks (will take a while)...");
+     if (GoodTracksITS(dir)) {
+        ::Error("AliITSComparisonV2.C","Can't generate the reference file !");
+        return 1;
+     }
+   }
+   refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Error("AliITSComparisonV2.C","Can't open the reference file !");
+     return 1;
+   }   
+  
+   TTree *itsTree=(TTree*)refFile->Get("itsTree");
+   if (!itsTree) {
+     ::Error("AliITSComparisonV2.C","Can't get the reference tree !");
+     return 2;
+   }
+   TBranch *branch=itsTree->GetBranch("ITS");
+   if (!branch) {
+     ::Error("AliITSComparisonV2.C","Can't get the ITS branch !");
+     return 3;
+   }
+   TClonesArray dummy("AliTrackReference",1000), *refs=&dummy;
+   branch->SetAddress(&refs);
+
+
+   sprintf(fname,"%s/AliESDs.root",dir);
+   TFile *ef=TFile::Open(fname);
+   if ((!ef)||(!ef->IsOpen())) {
+      sprintf(fname,"%s/AliESDits.root",dir);
+      ef=TFile::Open(fname);
+      if ((!ef)||(!ef->IsOpen())) {
+         ::Error("AliITSComparisonV2.C","Can't open AliESDits.root !");
+         return 4;
       }
-      track->Propagate(track->GetAlpha(),3.,0.1/65.19*1.848,0.1*1.848);
-      track->PropagateToVertex();
+   }
+   AliESDEvent* event = new AliESDEvent();
+   TTree* esdTree = (TTree*) ef->Get("esdTree");
+   if (!esdTree) {
+      ::Error("AliITSComparison.C", "no ESD tree found");
+      return 6;
+   }
+   event->ReadFromTree(esdTree);
 
-      if (lab==tlab) hfound->Fill(ptg);
-      else { hfake->Fill(ptg); cerr<<lab<<" fake\n";}
 
-      Double_t xv,par[5]; track->GetExternalParameters(xv,par);
-      Float_t phi=TMath::ASin(par[2]) + track->GetAlpha();
-      if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
-      if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
-      Float_t lam=TMath::ATan(par[3]); 
-      Float_t pt_1=TMath::Abs(par[4]);
+   //******* Loop over events *********
+   Int_t e=0;
+   while (esdTree->GetEvent(e)) {
+     cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
+     Int_t nentr=event->GetNumberOfTracks();
+     allfound+=nentr;
 
-      Double_t phig=TMath::ATan2(pyg,pxg);
-      hp->Fill((phi - phig)*1000.);
+     if (itsTree->GetEvent(e++)==0) {
+        cerr<<"No reconstructable tracks !\n";
+        continue;
+     }
 
-      Double_t lamg=TMath::ATan2(pzg,ptg);
-      hl->Fill((lam - lamg)*1000.);
+     Int_t ngood=refs->GetEntriesFast(); 
+     allgood+=ngood;
+
+     const Int_t MAX=15000;
+     Int_t notf[MAX], nnotf=0;
+     Int_t fake[MAX], nfake=0;
+     Int_t mult[MAX], numb[MAX], nmult=0;
+     Int_t k;
+     for (k=0; k<ngood; k++) {
+       AliTrackReference *ref=(AliTrackReference*)refs->UncheckedAt(k); 
+        Int_t lab=ref->Label(), tlab=-1;
+        Float_t ptg=TMath::Sqrt(ref->Px()*ref->Px() + ref->Py()*ref->Py());
+
+        if (ptg<ptcutl) continue;
+        if (ptg>ptcuth) continue;
+
+        allselected++;
+
+        hgood->Fill(ptg);
+
+        AliESDtrack *esd=0;
+        Int_t cnt=0;
+        for (Int_t i=0; i<nentr; i++) {
+           AliESDtrack *t=event->GetTrack(i);
+          UInt_t status=t->GetStatus();
+
+           if ((status&AliESDtrack::kITSrefit)==0) continue;
+
+           Int_t lbl=t->GetLabel();
+           if (lab==TMath::Abs(lbl)) {
+             if (cnt==0) {esd=t; tlab=lbl;}
+              cnt++;
+           }
+        }
+        if (cnt==0) {
+           notf[nnotf++]=lab;
+           continue;
+        } else if (cnt>1){
+           mult[nmult]=lab;
+           numb[nmult]=cnt; nmult++;        
+        }
+
+        if (lab==tlab) hfound->Fill(ptg);
+        else {
+          fake[nfake++]=lab;
+          hfake->Fill(ptg); 
+        }
+
+        Double_t alpha=esd->GetAlpha(),xv,par[5]; 
+        esd->GetExternalParameters(xv,par);
+        Float_t phi=TMath::ASin(par[2]) + alpha;
+        if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
+        if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
+        Float_t lam=TMath::ATan(par[3]); 
+        Float_t pt_1=TMath::Abs(par[4]);
+
+        Float_t phig=TMath::ATan2(ref->Py(),ref->Px());
+        hp->Fill((phi - phig)*1000.);
+
+        Float_t lamg=TMath::ATan2(ref->Pz(),ptg);
+        hl->Fill((lam - lamg)*1000.);
+
+        Float_t d,z; esd->GetImpactParameters(d,z);
+        hmpt->Fill(10000*d);
+        hz->Fill(10000*z);
+
+        hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+
+        Float_t mom=1./(pt_1*TMath::Cos(lam));
+        Float_t dedx=esd->GetITSsignal();
+        hep->Fill(mom,dedx,1.);
+
+        Int_t pdg=(Int_t)ref->GetLength();  //this is particle's PDG !
+
+        if (TMath::Abs(pdg)==211) //pions
+           if (mom>0.4 && mom<0.5) he->Fill(dedx,1.);
 
-      Double_t d=10000*track->GetD();
-      hmpt->Fill(d);
+     }
 
-      hptw->Fill(ptg,TMath::Abs(d));
+     cout<<"\nList of Not found tracks :\n";
+     for (k=0; k<nnotf; k++){
+       cout<<notf[k]<<"\t";
+       if ((k%9)==8) cout<<"\n";
+     }
+     cout<<"\n\nList of fake  tracks :\n";
+     for (k=0; k<nfake; k++){
+       cout<<fake[k]<<"\t";
+       if ((k%9)==8) cout<<"\n";
+     }
+     cout<<"\n\nList of multiple found tracks :\n";
+     for (k=0; k<nmult; k++) {
+         cout<<"id.   "<<mult[k]
+             <<"     found - "<<numb[k]<<"times\n";
+     }
+     cout<<endl;
 
-      Double_t z=10000*track->GetZ();
-      hz->Fill(z);
+     cout<<"Number of found tracks : "<<nentr<<endl;
+     cout<<"Number of \"good\" tracks : "<<ngood<<endl;
 
-//if (TMath::Abs(gt[ngood].code)==11 && ptg>4.)
-      hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+     refs->Clear();
+   } //***** End of the loop over events
 
-   }
+   delete event;
+   delete esdTree;
+   ef->Close();
+   
+   delete itsTree;
+   refFile->Close();
 
-   Stat_t ng=hgood->GetEntries(); cerr<<"Good tracks "<<ng<<endl;
-   Stat_t nf=hfound->GetEntries();
-   if (ng!=0) 
-      cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
+   Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
+   if (ng!=0) cout<<"\n\nIntegral efficiency is about "<<nf/ng*100.<<" %\n";
+   cout<<"Total number selected of \"good\" tracks ="<<allselected<<endl<<endl;
+   cout<<"Total number of found tracks ="<<allfound<<endl;
+   cout<<"Total number of \"good\" tracks ="<<allgood<<endl;
+   cout<<endl;
 
    gStyle->SetOptStat(111110);
    gStyle->SetOptFit(1);
 
    TCanvas *c1=new TCanvas("c1","",0,0,700,850);
 
+   Int_t minc=33; 
+
    TPad *p1=new TPad("p1","",0,0.3,.5,.6); p1->Draw();
    p1->cd(); p1->SetFillColor(42); p1->SetFrameFillColor(10); 
-   hp->SetFillColor(4);  hp->SetXTitle("(mrad)"); hp->Fit("gaus"); c1->cd();
+   hp->SetFillColor(4);  hp->SetXTitle("(mrad)"); 
+   if (hp->GetEntries()<minc) hp->Draw(); else hp->Fit("gaus"); c1->cd();
 
    TPad *p2=new TPad("p2","",0.5,.3,1,.6); p2->Draw(); 
    p2->cd(); p2->SetFillColor(42); p2->SetFrameFillColor(10);
-   hl->SetXTitle("(mrad)"); hl->Fit("gaus"); c1->cd();
+   hl->SetXTitle("(mrad)");
+   if (hl->GetEntries()<minc) hl->Draw(); else hl->Fit("gaus"); c1->cd();
 
    TPad *p3=new TPad("p3","",0,0,0.5,0.3); p3->Draw();
    p3->cd(); p3->SetFillColor(42); p3->SetFrameFillColor(10); 
-   hpt->SetXTitle("(%)"); hpt->Fit("gaus"); c1->cd();
+   hpt->SetXTitle("(%)");
+   if (hpt->GetEntries()<minc) hpt->Draw(); else hpt->Fit("gaus"); c1->cd();
 
    TPad *p4=new TPad("p4","",0.5,0,1,0.3); p4->Draw();
    p4->cd(); p4->SetFillColor(42); p4->SetFrameFillColor(10);
-   hmpt->SetXTitle("(micron)"); hmpt->Fit("gaus"); hz->Draw("same"); c1->cd();
-   //hfound->Sumw2();
-   //hptw->Sumw2(); 
-   //hg->SetMaximum(333);
-   //hg->SetYTitle("Impact Parameter Resolution (micron)");
-   //hg->SetXTitle("Pt (GeV/c)");
-   //hg->GetXaxis()->SetRange(0,10);
-   //hg->Divide(hptw,hfound,1,1.);
-   //hg->DrawCopy(); c1->cd();
+   hmpt->SetXTitle("(micron)");
+   if (hmpt->GetEntries()<minc) hmpt->Draw(); else hmpt->Fit("gaus"); 
+   hz->Draw("same"); c1->cd();
    
 
    TPad *p5=new TPad("p5","",0,0.6,1,1); p5->Draw(); p5->cd(); 
@@ -221,9 +320,9 @@ Int_t AliITSComparisonV2() {
    hg->SetXTitle("Pt (GeV/c)");
    hg->Draw();
 
-   TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
+   TLine *line1 = new TLine(0.2,1.0,6.1,1.0); line1->SetLineStyle(4);
    line1->Draw("same");
-   TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
+   TLine *line2 = new TLine(0.2,0.9,6.1,0.9); line2->SetLineStyle(4);
    line2->Draw("same");
 
    hf->SetFillColor(1);
@@ -238,96 +337,194 @@ Int_t AliITSComparisonV2() {
    text->SetTextSize(0.05);
    text->Draw();
 
+   TCanvas *c2=new TCanvas("c2","",320,32,530,590);
+   TPad *p6=new TPad("p6","",0.,0.,1.,.5); p6->Draw();
+   p6->cd(); p6->SetFillColor(42); p6->SetFrameFillColor(10); 
+   he->SetFillColor(2); he->SetFillStyle(3005);  
+   he->SetXTitle("Arbitrary Units"); 
+   if (he->GetEntries()<minc) he->Draw(); else he->Fit("gaus"); c2->cd();
+
+   TPad *p7=new TPad("p7","",0.,0.5,1.,1.); p7->Draw(); 
+   p7->cd(); p7->SetFillColor(42); p7->SetFrameFillColor(10);
+   hep->SetXTitle("p (Gev/c)"); hep->SetYTitle("dE/dX (Arb. Units)"); 
+   hep->Draw(); c1->cd();
+
+   TFile fc("AliITSComparisonV2.root","RECREATE");
+   c1->Write();
+   c2->Write();
+   fc.Close();
+
+   gBenchmark->Stop("AliITSComparisonV2");
+   gBenchmark->Show("AliITSComparisonV2");
+
    return 0;
 }
 
-Int_t good_tracks(GoodTrack *gt, Int_t max) {
-   if (gAlice) {delete gAlice; gAlice=0;}
 
-   TFile *file=TFile::Open("galice.root");
-   if (!file->IsOpen()) {cerr<<"Can't open galice.root !\n"; exit(4);}
-   if (!(gAlice=(AliRun*)file->Get("gAlice"))) {
-     cerr<<"gAlice have not been found on galice.root !\n";
-     exit(5);
-   }
 
-   Int_t np=gAlice->GetEvent(0);
+Int_t GoodTracksITS(const Char_t *dir) {
+   if (gAlice) { 
+       delete AliRunLoader::Instance();
+       delete gAlice;//if everything was OK here it is already NULL
+       gAlice = 0x0;
+   }
 
-   Int_t *good=new Int_t[np];
-   Int_t k;
-   for (k=0; k<np; k++) good[k]=0;
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
 
-   AliITS *ITS=(AliITS*)gAlice->GetDetector("ITS");
-   if (!ITS) {
-      cerr<<"can't get ITS !\n"; exit(8);
-   }
-   AliITSgeom *geom=ITS->GetITSgeom();
-   if (!geom) {
-      cerr<<"cen't get ITS geometry !\n"; exit(9);
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
+   if (!rl) {
+      ::Error("GoodTracksITS","Can't start session !");
+      return 1;
    }
 
-   TFile *cf=TFile::Open("AliITSclustersV2.root");
-   if (!cf->IsOpen()){
-      cerr<<"Can't open AliITSclustersV2.root !\n"; exit(6);
+   rl->LoadgAlice();
+   rl->LoadHeader();
+   rl->LoadKinematics();
+
+   AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
+   if (itsl == 0x0) {
+       ::Error("GoodTracksITS","Can not find the ITSLoader");
+       delete rl;
+       return 4;
    }
-   TTree *cTree=(TTree*)cf->Get("cTree");
-   if (!cTree) {
-      cerr<<"Can't get cTree !\n"; exit(7);
+   itsl->LoadRecPoints();
+  
+
+   Int_t nev=rl->GetNumberOfEvents();
+   ::Info("GoodTracksITS","Number of events : %d\n",nev);  
+
+   sprintf(fname,"%s/GoodTracksTPC.root",dir);
+   TFile *tpcFile=TFile::Open(fname);
+   if ((!tpcFile)||(!tpcFile->IsOpen())) {
+       ::Error("GoodTracksITS","Can't open the GoodTracksTPC.root !");
+       delete rl;
+       return 5; 
    }
-   TBranch *branch=cTree->GetBranch("Clusters");
-   if (!branch) {
-      cerr<<"Can't get clusters branch !\n"; exit(8);
+   TClonesArray dum("AliTrackReference",1000), *tpcRefs=&dum;
+   TTree *tpcTree=(TTree*)tpcFile->Get("tpcTree");
+   if (!tpcTree) {
+       ::Error("GoodTracksITS","Can't get the TPC reference tree !");
+       delete rl;
+       return 6;
+   }
+   TBranch *tpcBranch=tpcTree->GetBranch("TPC");
+   if (!tpcBranch) {
+      ::Error("GoodTracksITS","Can't get the TPC reference branch !");
+      delete rl;
+      return 7;
    }
-   TClonesArray *clusters=new TClonesArray("AliITSclusterV2",10000);
-   branch->SetAddress(&clusters);
-
-   Int_t entr=(Int_t)cTree->GetEntries();
-   for (k=0; k<entr; k++) {
-     if (!cTree->GetEvent(k)) continue;
-     Int_t lay,lad,det;  geom->GetModuleId(k,lay,lad,det);
-     if (lay<1 || lay>6) {
-       cerr<<"wrong layer !\n"; exit(10);
+   tpcBranch->SetAddress(&tpcRefs);
+
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+   TFile *itsFile=TFile::Open(fname,"recreate");
+   TClonesArray dummy2("AliTrackReference",1000), *itsRefs=&dummy2;
+   TTree itsTree("itsTree","Tree with info about the reconstructable ITS tracks");
+   itsTree.Branch("ITS",&itsRefs);
+
+   //********  Loop over generated events 
+   for (Int_t e=0; e<nev; e++) {
+     Int_t k;
+
+     rl->GetEvent(e);  itsFile->cd();
+
+     Int_t np = rl->GetHeader()->GetNtrack();
+     cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
+
+     //******** Fill the "good" masks
+     Int_t *good=new Int_t[np]; for (k=0; k<np; k++) good[k]=0;
+
+     TTree *cTree=itsl->TreeR();
+     if (!cTree) {
+        ::Error("GoodTracksITS","Can't get the cluster tree !"); 
+        delete rl;
+        return 8;
      }
-     Int_t ncl=clusters->GetEntriesFast();
-     while (ncl--) {
-        AliITSclusterV2 *pnt=(AliITSclusterV2*)clusters->UncheckedAt(ncl);
-        Int_t l0=pnt->GetLabel(0);
-        Int_t l1=pnt->GetLabel(1);
-        Int_t l2=pnt->GetLabel(2);
-        Int_t mask=1<<(lay-1);
-        if (l0>=0) good[l0]|=mask; 
-        if (l1>=0) good[l1]|=mask; 
-        if (l2>=0) good[l2]|=mask;
+     TBranch *branch=cTree->GetBranch("ITSRecPoints");
+     if (!branch) {
+        ::Error("GoodTracksITS","Can't get the clusters branch !"); 
+        delete rl;
+        return 9;
      }
-   }
-   clusters->Delete(); delete clusters;
-   cf->Close();
+     TClonesArray dummy("AliITSRecPoint",10000), *clusters=&dummy;
+     branch->SetAddress(&clusters);
+
+     Int_t entr=(Int_t)cTree->GetEntries();
+     for (k=0; k<entr; k++) {
+         cTree->GetEvent(k);
+         Int_t ncl=clusters->GetEntriesFast(); if (ncl==0) continue;
+         while (ncl--) {
+            AliITSRecPoint *pnt=(AliITSRecPoint*)clusters->UncheckedAt(ncl);
+
+            Int_t lay=pnt->GetLayer();
+            if (lay<0 || lay>5) {
+               ::Error("GoodTracksITS","Wrong layer !");
+               delete rl;
+               return 10;
+            }
+
+            Int_t l0=pnt->GetLabel(0);
+              if (l0>=np) {
+//              cerr<<"Wrong label: "<<l0<<endl;
+                continue;
+              }
+            Int_t l1=pnt->GetLabel(1);
+              if (l1>=np) {
+//              cerr<<"Wrong label: "<<l1<<endl;
+                continue;
+              }
+            Int_t l2=pnt->GetLabel(2);
+              if (l2>=np) {
+//              cerr<<"Wrong label: "<<l2<<endl;
+                continue;
+              }
+            Int_t mask=1<<lay;
+            if (l0>=0) good[l0]|=mask; 
+            if (l1>=0) good[l1]|=mask; 
+            if (l2>=0) good[l2]|=mask;
+         }
+         clusters->Clear();
+     }
+   
 
-   ifstream in("good_tracks_tpc");
-   if (!in) {
-     cerr<<"can't get good_tracks_tpc !\n"; exit(11);
-   }
-   Int_t nt=0;
-   Double_t px,py,pz,x,y,z;
-   Int_t code,lab;
-   while (in>>lab>>code>>px>>py>>pz>>x>>y>>z) {
-      if (good[lab] != 0x3F) continue;
-      TParticle *p = (TParticle*)gAlice->Particle(lab);
-      gt[nt].lab=lab;
-      gt[nt].code=p->GetPdgCode();
-//**** px py pz - in global coordinate system
-      gt[nt].px=p->Px(); gt[nt].py=p->Py(); gt[nt].pz=p->Pz();
-      gt[nt].x=gt[nt].y=gt[nt].z=0.;
-      nt++;
-      if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
-   }
+     //****** select tracks which are "good" enough
+     AliStack* stack = rl->Stack();
+
+     tpcTree->GetEvent(e);
+     Int_t nk=tpcRefs->GetEntriesFast();
+     Int_t nt=0;
+     for (k=0; k<nk; k++) {
+        AliTrackReference *tpcRef=(AliTrackReference *)tpcRefs->UncheckedAt(k);
+        Int_t lab=tpcRef->Label();
+        if (good[lab] != 0x3F) continue;
+        TParticle *p = (TParticle*)stack->Particle(lab);
+        if (p == 0x0) {
+           cerr<<"Can not get particle "<<lab<<endl;
+           continue;
+        }
+
+       AliTrackReference *ref=new((*itsRefs)[nt]) AliTrackReference(*tpcRef);
+       ref->SetMomentum(p->Px(),p->Py(),p->Pz());
+       ref->SetPosition(p->Vx(),p->Vy(),p->Vz());
+        nt++;
+     }
+     tpcRefs->Clear();
+
+     itsTree.Fill();
+     itsRefs->Clear();
 
-   delete[] good;
+     delete[] good;
 
-   delete gAlice; gAlice=0;
-   file->Close();
+   } //*** end of the loop over generated events
 
-   return nt;
+   itsTree.Write();
+   itsFile->Close();
+
+   delete tpcTree;
+   tpcFile->Close();
+
+   delete rl;
+   return 0;
 }