Possiblity to run over several events in a file (Yu.Belikov)
authorhristov <hristov@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 22 Mar 2004 07:23:44 +0000 (07:23 +0000)
committerhristov <hristov@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 22 Mar 2004 07:23:44 +0000 (07:23 +0000)
ITS/AliCascadeComparison.C
ITS/AliITSComparisonV2.C
ITS/AliV0Comparison.C
STEER/AliESDComparison.C
TOF/AliTOFComparison.C
TPC/AliTPCComparison.C

index d21aa88..122a68d 100644 (file)
@@ -8,98 +8,50 @@
  ****************************************************************************/
 
 #if !defined(__CINT__) || defined(__MAKECINT__)
+  #include <TMath.h>
+  #include <TError.h>
   #include <Riostream.h>
-  #include <fstream.h>
-
-  #include "AliRun.h"
-  #include "AliMC.h"
+  #include <TH1F.h>
+  #include <TTree.h>
+  #include <TParticle.h>
+  #include <TCanvas.h>
+  #include <TLine.h>
+  #include <TText.h>
+  #include <TBenchmark.h>
+  #include <TStyle.h>
+  #include <TKey.h>
+  #include <TROOT.h>
+
+  #include "AliStack.h"
   #include "AliHeader.h"
+  #include "AliTrackReference.h"
   #include "AliRunLoader.h"
-  #include "AliITSLoader.h"
-
-  #include "TH1.h"
-  #include "TFile.h"
-  #include "TKey.h"
-  #include "TObjArray.h"
-  #include "TStyle.h"
-  #include "TCanvas.h"
-  #include "TLine.h"
-  #include "TText.h"
-  #include "TParticle.h"
-  #include "TStopwatch.h"
-  #include "TPDGCode.h"
-
   #include "AliRun.h"
   #include "AliESD.h"
-  #include "AliCascadeVertex.h"
+#else
+const Int_t kXiMinus    = 3312;
+const Int_t kXiPlusBar  = -3312;
+const Int_t kOmegaMinus = 3334;
+const Int_t kOmegaPlusBar = -3334;
 #endif
 
-struct GoodCascade {
-  Int_t nlab,plab;   // V0's daughter labels
-  Int_t blab;        // Bachelor label
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
-Int_t good_cascades(GoodCascade *gt, Int_t max);
+Int_t GoodCascades(const Char_t *dir=".");
 
 extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
+extern TROOT *gROOT;
+
+static Int_t allgood=0;
+static Int_t allfound=0;
 
-Int_t AliCascadeComparison(Int_t code=3312) {
+Int_t AliCascadeComparison(Int_t code=3312, const Char_t *dir=".") {
   //code= 3312; //kXiMinus 
   //code=-3312; //kXiPlusBar
   //code= 3334; //kOmegaMinus
   //code=-3334; //kOmegaPlusBar
-   cerr<<"Doing comparison...\n";
-   TStopwatch timer;
-
-   /*** Check if the file with the "good" cascades exists ***/
-   GoodCascade gc[100];
-   Int_t ngood=0;
-   ifstream in("good_cascades");
-   if (in) {
-      cerr<<"Reading good cascades...\n";
-      while (in>>gc[ngood].nlab>>gc[ngood].plab>>
-                gc[ngood].blab>>gc[ngood].code>>
-                 gc[ngood].px>>gc[ngood].py>>gc[ngood].pz>>
-                 gc[ngood].x >>gc[ngood].y >>gc[ngood].z) {
-         ngood++;
-         cerr<<ngood<<'\r';
-         if (ngood==100) {
-            cerr<<"Too many good cascades !\n";
-            break;
-         }
-      }
-      if (!in.eof()) cerr<<"Read error (good_cascades) !\n";
-   } else {
-     /*** generate a file with the "good" cascades ***/
-      cerr<<"Marking good cascades (this will take a while)...\n";
-      ngood=good_cascades(gc,100);
-      ofstream out("good_cascades");
-      if (out) {
-         for (Int_t ngd=0; ngd<ngood; ngd++)            
-            out<<gc[ngd].nlab<<' '<<gc[ngd].plab<<' '<<
-                gc[ngd].blab<<' '<<gc[ngd].code<<' '<<
-                 gc[ngd].px<<' '<<gc[ngd].py<<' '<<gc[ngd].pz<<' '<<
-                 gc[ngd].x <<' '<<gc[ngd].y <<' '<<gc[ngd].z <<endl;
-      } else cerr<<"Can not open file (good_cascades) !\n";
-      out.close();
-   }
-
-   AliESD *event=0;
-   { /*** Load reconstructed vertices ***/
-   TFile *ef=TFile::Open("AliESDcas.root");
-   if ((!ef)||(!ef->IsOpen())) {
-     cerr<<"AliCascadeComparison.C: Can't open AliESDcas.root !\n";
-     return 1;
-   }
-   TKey *key=0;
-   TIter next(ef->GetListOfKeys());
-   if ((key=(TKey*)next())!=0) event=(AliESD*)key->ReadObj();
-   ef->Close();
-   }
-   Int_t nentr=event->GetNumberOfCascades();
+   gBenchmark->Start("AliCascadeComparison");
 
+   ::Info("AliCascadeComparison.C","Doing comparison...");
 
    const Double_t cascadeWindow=0.05, cascadeWidth=0.015; 
    Double_t ptncut=0.12, ptpcut=0.33, kine0cut=0.003;
@@ -125,161 +77,297 @@ Int_t AliCascadeComparison(Int_t code=3312) {
    default: cerr<<"Invalid PDG code !\n"; return 1;
    }
 
-   /*** create some histograms ***/
-   TH1F *hp=new TH1F("hp","Angular Resolution",30,-30.,30.); //phi resolution 
+
+   TH1F *hp=(TH1F*)gROOT->FindObject("hp");
+   if (!hp) hp=new TH1F("hp","Angular Resolution",30,-30.,30.);
    hp->SetXTitle("(mrad)"); hp->SetFillColor(2);
-   TH1F *hl=new TH1F("hl","Lambda Resolution",30,-30,30);
-   hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013); 
-   TH1F *hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
+
+   TH1F *hl=(TH1F*)gROOT->FindObject("hl");
+   if (!hl) hl=new TH1F("hl","Lambda Resolution",30,-30,30);
+   hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013);
+   TH1F *hpt=(TH1F*)gROOT->FindObject("hpt");
+   if (!hpt) hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
    hpt->SetXTitle("(%)"); hpt->SetFillColor(2); 
 
-   TH1F *hx=new TH1F("hx","Position Resolution (X,Y)",30,-3.,3.); //x res. 
+   TH1F *hx=(TH1F*)gROOT->FindObject("hx");
+   if (!hx) hx=new TH1F("hx","Position Resolution (X,Y)",30,-3.,3.);
    hx->SetXTitle("(mm)"); hx->SetFillColor(6);
-   TH1F *hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.);   //y res
+
+   TH1F *hy=(TH1F*)gROOT->FindObject("hy");
+   if (!hy) hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.);  
    hy->SetXTitle("(mm)"); hy->SetFillColor(1); hy->SetFillStyle(3013);
-   TH1F *hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);   //z res. 
+
+   TH1F *hz=(TH1F*)gROOT->FindObject("hz");
+   if (!hz) hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);
    hz->SetXTitle("(mm)"); hz->SetFillColor(6);
 
+
    Double_t pmin=0.2, pmax=4.2; Int_t nchan=20;
-   TH1F *hgood=new TH1F("hgood","Good Cascades",nchan,pmin,pmax);    
-   TH1F *hfound=new TH1F("hfound","Found Cascades",nchan,pmin,pmax);
-   TH1F *hfake=new TH1F("hfake","Fake Cascades",nchan,pmin,pmax);
-   TH1F *hg=new TH1F("hg","Efficiency for Good Cascades",nchan,pmin,pmax);
+   TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");    
+   if (!hgood) hgood=new TH1F("hgood","Good Cascades",nchan,pmin,pmax);
+    
+   TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
+   if (!hfound) hfound=new TH1F("hfound","Found Cascades",nchan,pmin,pmax);
+
+   TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
+   if (!hfake) hfake=new TH1F("hfake","Fake Cascades",nchan,pmin,pmax);
+
+   TH1F *hg=(TH1F*)gROOT->FindObject("hg");
+   if (!hg) hg=new TH1F("hg","Efficiency for Good Cascades",nchan,pmin,pmax);
    hg->SetLineColor(4); hg->SetLineWidth(2);
-   TH1F *hf=new TH1F("hf","Probability of Fake Cascades",nchan,pmin,pmax);
+
+   TH1F *hf=(TH1F*)gROOT->FindObject("hf");
+   if (!hf) hf=new TH1F("hf","Probability of Fake Cascades",nchan,pmin,pmax);
    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
 
+
    Double_t mmin=cascadeMass-cascadeWindow, mmax=cascadeMass+cascadeWindow;
-   TH1F *cs =new TH1F("cs","Cascade Effective Mass",40, mmin, mmax);
-   cs->SetXTitle("(GeV)");
-   cs->SetLineColor(4); cs->SetLineWidth(4);
-   TH1F *csf =new TH1F("csf","Fake Cascade Effective Mass",40, mmin, mmax);
+   TH1F *cs=(TH1F*)gROOT->FindObject("cs");
+   if (!cs) cs=new TH1F("cs","Cascade Effective Mass",40, mmin, mmax);
+   cs->SetXTitle("(GeV)"); cs->SetLineColor(4); cs->SetLineWidth(4);
+
+   TH1F *csf=(TH1F*)gROOT->FindObject("csf");
+   if (!csf) csf=new TH1F("csf","Fake Cascade Effective Mass",40, mmin, mmax);
    csf->SetXTitle("(GeV)"); csf->SetFillColor(6);
 
-   Double_t pxg=0.,pyg=0.,ptg=0.;
-   Int_t nlab=-1, plab=-1, blab=-1;
-   Int_t i;
-   for (i=0; i<nentr; i++) {
-       AliESDcascade *cascade=event->GetCascade(i);
-
-       Int_t nidx=TMath::Abs(cascade->GetNindex());
-       Int_t pidx=TMath::Abs(cascade->GetPindex());
-       Int_t bidx=TMath::Abs(cascade->GetBindex());
-
-       AliESDtrack *ntrack=event->GetTrack(nidx);
-       AliESDtrack *ptrack=event->GetTrack(pidx);
-       AliESDtrack *btrack=event->GetTrack(bidx);
-
-       nlab=TMath::Abs(ntrack->GetLabel()); 
-       plab=TMath::Abs(ptrack->GetLabel());
-       blab=TMath::Abs(btrack->GetLabel());
-
-       /** Kinematical cuts **/
-       Double_t pxn,pyn,pzn; cascade->GetNPxPyPz(pxn,pyn,pzn); 
-       Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
-       if (ptn < ptncut) continue;
-       Double_t pxp,pyp,pzp; cascade->GetPPxPyPz(pxp,pyp,pzp); 
-       Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
-       if (ptp < ptpcut) continue;
-       Double_t pxb,pyb,pzb; cascade->GetBPxPyPz(pxb,pyb,pzb); 
-       Double_t ptb=TMath::Sqrt(pxb*pxb + pyb*pyb);
-       if (ptb < ptbcut) continue;
-       Double_t kine0;
-       Double_t kine=cascade->ChangeMassHypothesis(kine0,code);
-       if (TMath::Abs(kine0)>kine0cut) continue;
-       //if (TMath::Abs(kine)>kinecut) continue;
-
-       Double_t mass=cascade->GetEffMass();
-       cs->Fill(mass);
-       csf->Fill(mass);
-
-       if (TMath::Abs(mass-cascadeMass)>cascadeWidth) continue;
-
-       Int_t j;
-       for (j=0; j<ngood; j++) {
-          if (gc[j].code != cascade->GetPdgCode()) continue;
-          if (gc[j].nlab == nlab)
-          if (gc[j].plab == plab)
-          if (gc[j].blab == blab) break;
-       }
-
-       Double_t px,py,pz; cascade->GetPxPyPz(px,py,pz);
-       Double_t pt=TMath::Sqrt(px*px+py*py);
-
-       if (j==ngood) {
-          hfake->Fill(pt);
-          cerr<<"Fake cascade: ("<<nlab<<","<<plab<<","<<blab<<")\n";
-          continue;
-       }
-       csf->Fill(mass,-1);
-
-       pxg=gc[j].px; pyg=gc[j].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-       Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
-       Double_t lamg=TMath::ATan2(gc[j].pz,ptg), lam=TMath::ATan2(pz,pt);
-       hp->Fill((phi - phig)*1000.);
-       hl->Fill((lam - lamg)*1000.);
-       hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
-
-       Double_t x,y,z; cascade->GetXYZ(x,y,z);
-       hx->Fill((x-gc[j].x)*10);
-       hy->Fill((y-gc[j].y)*10);
-       hz->Fill((z-gc[j].z)*10);
-
-       hfound->Fill(ptg);
-       gc[j].nlab=-1;
 
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodCascades.root",dir);
+
+   TFile *refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+   ::Info("AliCascadeComparison.C","Marking good cascades (will take a while)...");
+     if (GoodCascades(dir)) {
+        ::Error("AliCascadesComparison.C","Can't generate the reference file !");
+        return 1;
+     }
    }
-   for (i=0; i<ngood; i++) {
-      if (gc[i].code != code) continue;
-      pxg=gc[i].px; pyg=gc[i].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-      hgood->Fill(ptg);
-      nlab=gc[i].nlab; plab=gc[i].plab; blab=gc[i].blab;
-      if (nlab < 0) continue;
-     cerr<<"Cascade ("<<nlab<<','<<plab<<","<<blab<<") has not been found !\n";
+   refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Error("AliCascadeComparison.C","Can't open the reference file !");
+     return 1;
+   }   
+  
+   TTree *csTree=(TTree*)refFile->Get("csTree");
+   if (!csTree) {
+     ::Error("AliCascadeComparison.C","Can't get the reference tree !");
+     return 2;
    }
+   TBranch *pbranch=csTree->GetBranch("positive");
+   if (!pbranch) {
+     ::Error("AliCascadeComparison.C","Can't get the positive daughter branch !");
+     return 3;
+   }
+   TClonesArray dummy("AliTrackReference",1000), *prefs=&dummy;
+   pbranch->SetAddress(&prefs);
+
+   TBranch *nbranch=csTree->GetBranch("negative");
+   if (!nbranch) {
+     ::Error("AliCascadeComparison.C","Can't get the negative daughter branch !");
+     return 4;
+   }
+   TClonesArray dumm("AliTrackReference",1000), *nrefs=&dumm;
+   nbranch->SetAddress(&nrefs);
+
+   TBranch *bbranch=csTree->GetBranch("bachelor");
+   if (!nbranch) {
+     ::Error("AliCascadeComparison.C","Can't get the bachelor branch !");
+     return 4;
+   }
+   TClonesArray dum("AliTrackReference",1000), *brefs=&dum;
+   bbranch->SetAddress(&brefs);
+
+
+   
+   sprintf(fname,"%s/AliESDs.root",dir);
+   TFile *ef=TFile::Open(fname);
+   if ((!ef)||(!ef->IsOpen())) {
+      sprintf(fname,"%s/AliESDcascade.root",dir);
+      ef=TFile::Open(fname);
+      if ((!ef)||(!ef->IsOpen())) {
+         ::Error("AliCascadeComparison.C","Can't open AliESDcascade.root !");
+         return 5;
+      }
+   }
+   TKey *key=0;
+   TIter next(ef->GetListOfKeys());
+
+
 
-   delete event;
+   //******* Loop over events *********
+   Int_t e=0;
+   while ((key=(TKey*)next())!=0) {
+      cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
+      AliESD *event=(AliESD*)key->ReadObj();
 
-   Stat_t ng=hgood->GetEntries();
-   Stat_t nf=hfound->GetEntries();
+      Int_t nentr=event->GetNumberOfCascades();
+      allfound+=nentr;
+
+
+      if (csTree->GetEvent(e++)==0) {
+         cerr<<"No reconstructable cascades !\n";
+         continue;
+      }
+
+      Int_t ngood=prefs->GetEntriesFast(),ng=0; 
+
+      Double_t pxg=0.,pyg=0.,pzg=0.,ptg=0.;
+      Int_t nlab=-1, plab=-1, blab=-1;
+      Int_t i;
+      for (i=0; i<nentr; i++) {
+          AliESDcascade *cascade=event->GetCascade(i);
+
+          Int_t nidx=TMath::Abs(cascade->GetNindex());
+          Int_t pidx=TMath::Abs(cascade->GetPindex());
+          Int_t bidx=TMath::Abs(cascade->GetBindex());
+
+          AliESDtrack *ntrack=event->GetTrack(nidx);
+          AliESDtrack *ptrack=event->GetTrack(pidx);
+          AliESDtrack *btrack=event->GetTrack(bidx);
+
+          nlab=TMath::Abs(ntrack->GetLabel()); 
+          plab=TMath::Abs(ptrack->GetLabel());
+          blab=TMath::Abs(btrack->GetLabel());
+
+          /** Kinematical cuts **/
+          Double_t pxn,pyn,pzn; cascade->GetNPxPyPz(pxn,pyn,pzn); 
+          Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
+          if (ptn < ptncut) continue;
+          Double_t pxp,pyp,pzp; cascade->GetPPxPyPz(pxp,pyp,pzp); 
+          Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
+          if (ptp < ptpcut) continue;
+          Double_t pxb,pyb,pzb; cascade->GetBPxPyPz(pxb,pyb,pzb); 
+          Double_t ptb=TMath::Sqrt(pxb*pxb + pyb*pyb);
+          if (ptb < ptbcut) continue;
+          Double_t kine0;
+          Double_t kine=cascade->ChangeMassHypothesis(kine0,code);
+          if (TMath::Abs(kine0)>kine0cut) continue;
+          //if (TMath::Abs(kine)>kinecut) continue;
+
+          Double_t mass=cascade->GetEffMass();
+          cs->Fill(mass);
+          csf->Fill(mass);
+
+          AliTrackReference *nref=0, *pref=0, *bref=0;
+          Int_t j;
+          for (j=0; j<ngood; j++) {
+              bref=(AliTrackReference*)brefs->UncheckedAt(j);
+              nref=(AliTrackReference*)nrefs->UncheckedAt(j);
+              pref=(AliTrackReference*)prefs->UncheckedAt(j);
+              if (bref->Label() == blab)
+              if (nref->Label() == nlab)
+              if (pref->Label() == plab) break;
+          }
+
+          if (TMath::Abs(mass-cascadeMass)>cascadeWidth) continue;
+
+          Double_t px,py,pz; cascade->GetPxPyPz(px,py,pz);
+          Double_t pt=TMath::Sqrt(px*px+py*py);
+
+          if (j==ngood) {
+             hfake->Fill(pt);
+             cout<<"Fake cascade: ("<<nlab<<","<<plab<<","<<blab<<")\n";
+             continue;
+          }
+          csf->Fill(mass,-1);
+
+          pxg=bref->Px()+nref->Px()+pref->Px(); 
+          pyg=bref->Px()+nref->Py()+pref->Py(); 
+          pzg=nref->Pz()+pref->Pz(); 
+          ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
+          Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
+          Double_t lamg=TMath::ATan2(pzg,ptg), lam=TMath::ATan2(pz,pt);
+          hp->Fill((phi - phig)*1000.);
+          hl->Fill((lam - lamg)*1000.);
+          hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
+
+          Double_t x,y,z; cascade->GetXYZ(x,y,z);
+          hx->Fill((x-nref->X())*10);
+          hy->Fill((y-nref->Y())*10);
+          hz->Fill((z-nref->Z())*10);
+
+          hfound->Fill(ptg);
+          nref->SetLabel(-1);
+
+      }
+      for (i=0; i<ngood; i++) {
+         AliTrackReference *bref=(AliTrackReference*)brefs->UncheckedAt(i);
+         AliTrackReference *nref=(AliTrackReference*)nrefs->UncheckedAt(i);
+         AliTrackReference *pref=(AliTrackReference*)prefs->UncheckedAt(i);
+         Int_t pdg=(Int_t)bref->GetLength();  //this is the cascade's PDG !
+         if (code!=pdg) continue;
+         ng++;
+         pxg=bref->Px()+nref->Px()+pref->Px(); 
+         pyg=bref->Px()+nref->Py()+pref->Py(); 
+         ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
+         hgood->Fill(ptg);
+         nlab=nref->Label(); plab=pref->Label(); blab=bref->Label();
+         if (nlab < 0) continue;
+         cout<<"Cascade ("<<nlab<<','<<plab<<","<<blab<<") has not been found !\n";
+      }
+      allgood+=ng;
+
+      cout<<"Number of found cascades : "<<nentr<<endl;
+      cout<<"Number of \"good\" cascades : "<<ng<<endl;
+
+      brefs->Clear();
+      prefs->Clear();
+      nrefs->Clear();
+      delete event;
+
+   } //**** End of the loop over events
+
+   ef->Close();
 
-   cerr<<"Number of found cascades: "<<nentr<<" ("<<nf<<" in the peak)\n";
-   cerr<<"Number of good cascades: "<<ng<<endl;
+   delete csTree;
+   refFile->Close();
+
+   Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
+   if (ng!=0) cout<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
+   cout<<
+   "Total number of found cascades: "<<allfound<<" ("<<nf<<" in the peak)\n";
+   cout<<"Total number of \"good\" cascades: "<<allgood<<endl;
 
-   if (ng!=0) 
-      cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
 
    gStyle->SetOptStat(111110);
    gStyle->SetOptFit(1);
 
-   TCanvas *c1=new TCanvas("c1","",0,0,580,610);
-   c1->Divide(2,2);
+   TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
+   if (!c1) {
+      c1=new TCanvas("c1","",0,0,580,610);
+      c1->Divide(2,2);
+   }
+
+   Int_t minc=33; 
 
    c1->cd(1);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
-   //hp->Fit("gaus");
-   hp->Draw();
+   if (hp->GetEntries()<minc) hp->Draw(); else hp->Fit("gaus");
    hl->Draw("same"); c1->cd();
 
    c1->cd(2);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //hpt->Fit("gaus"); c1->cd();
-   hpt->Draw(); c1->cd();
+   if (hpt->GetEntries()<minc) hpt->Draw(); else hpt->Fit("gaus");
+   c1->cd();
 
    c1->cd(3);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
-   //hx->Fit("gaus"); 
-   hx->Draw(); 
+   if (hx->GetEntries()<minc) hx->Draw(); else hx->Fit("gaus"); 
    hy->Draw("same"); c1->cd();
 
    c1->cd(4);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //hz->Fit("gaus");
-   hz->Draw();
+   if (hz->GetEntries()<minc) hz->Draw(); else hz->Fit("gaus");
 
+   c1->Update();
 
-   TCanvas *c2=new TCanvas("c2","",600,0,580,610);
-   c2->Divide(1,2);
+   TCanvas *c2=(TCanvas*)gROOT->FindObject("c2");
+   if (!c2) {
+      c2=new TCanvas("c2","",600,0,580,610);
+      c2->Divide(1,2);
+   }
 
    c2->cd(1);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
@@ -311,8 +399,8 @@ Int_t AliCascadeComparison(Int_t code=3312) {
 
    c2->cd(2);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //cs->Fit("gaus","","",cascadeMass-cascadeWidth,cascadeMass+cascadeWidth);
-   cs->Draw();
+   if (cs->GetEntries()<minc) cs->Draw();
+   else cs->Fit("gaus","","",cascadeMass-cascadeWidth,cascadeMass+cascadeWidth);
    csf->Draw("same");
    Double_t max=cs->GetMaximum();
    TLine *line3 = 
@@ -322,116 +410,183 @@ Int_t AliCascadeComparison(Int_t code=3312) {
       new TLine(cascadeMass+cascadeWidth,0.,cascadeMass+cascadeWidth,max);
    line4->Draw("same");
 
-   timer.Stop(); timer.Print();
+   c2->Update();
 
-   return 0;
-}
+   TFile fc("AliCascadeComparison.root","RECREATE");
+   c1->Write();
+   c2->Write();
+   fc.Close();
 
+   gBenchmark->Stop("AliCascadeComparison");
+   gBenchmark->Show("AliCascadeComparison");
 
-Int_t good_cascades(GoodCascade *gc, Int_t max) {
-   Int_t nc=0;
-   /*** Get information about the cuts ***/
-   Double_t r2min=0.9*0.9;
-   Double_t r2max=2.9*2.9;
 
-   /*** Get labels of the "good" tracks ***/
-   Double_t dd; Int_t id, label[15000], ngt=0;
-   ifstream in("good_tracks_its");
-   if (!in) {
-     cerr<<"Can't open the file good_tracks_its \n";
-     return nc;
-   }
-   while (in>>label[ngt]>>id>>dd>>dd>>dd>>dd>>dd>>dd) {
-     ngt++;
-     if (ngt>=15000) {
-       cerr<<"Too many good ITS tracks !\n";
-       return nc;
-     }
-   }   
-   if (!in.eof()) {
-      cerr<<"Read error (good_tracks_its) !\n";
-      return nc;
-   }
+   return 0;
+}
 
-   /*** Get an access to the kinematics ***/
+
+Int_t GoodCascades(const Char_t *dir) {
    if (gAlice) {
       delete gAlice->GetRunLoader();
       delete gAlice; 
       gAlice=0;
    }   
-   AliRunLoader *rl = AliRunLoader::Open("galice.root");
+
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
+
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
    if (!rl) {
-       cerr<<"AliCascadeComparison.C, good_cascades :Can't start session !\n";
+       ::Error("GoodCascades","Can't start session !");
        return 1;
    }
 
    rl->LoadgAlice();
    rl->LoadHeader();
    rl->LoadKinematics();
-   Int_t np = rl->GetHeader()->GetNtrack();
-
-   while (np--) {
-      cerr<<np<<'\r';
-      TParticle *cp=gAlice->GetMCApp()->Particle(np);
-
-      /*** only these cascades are "good" ***/
-      Int_t code=cp->GetPdgCode();
-      if (code!=kXiMinus)    if (code!=kXiPlusBar)
-      if (code!=kOmegaMinus) if (code!=kOmegaPlusBar) continue; 
-
-      /*** daughter tracks must be "good" ***/
-      Int_t v0lab=cp->GetFirstDaughter(), blab=cp->GetLastDaughter();
-      if (v0lab==blab) continue;
-      if (v0lab<0) continue;
-      if (blab<0) continue;
-
-      TParticle *p0=gAlice->GetMCApp()->Particle(v0lab);
-      TParticle *bp=gAlice->GetMCApp()->Particle(blab);
-      if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar)) {
-         TParticle *p=p0; p0=bp; bp=p;
-         Int_t i=v0lab; v0lab=blab; blab=i;         
-         if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar))
+
+
+   Int_t nev=rl->GetNumberOfEvents();
+   ::Info("GoodCascades","Number of events : %d\n",nev);  
+
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+   TFile *itsFile=TFile::Open(fname);
+   if ((!itsFile)||(!itsFile->IsOpen())) {
+       ::Error("GoodCAscades","Can't open the GoodTracksITS.root !");
+       delete rl;
+       return 5; 
+   }
+   TClonesArray dm("AliTrackReference",1000), *itsRefs=&dm;
+   TTree *itsTree=(TTree*)itsFile->Get("itsTree");
+   if (!itsTree) {
+       ::Error("GoodCascades","Can't get the ITS reference tree !");
+       delete rl;
+       return 6;
+   }
+   TBranch *itsBranch=itsTree->GetBranch("ITS");
+   if (!itsBranch) {
+      ::Error("GoodCascades","Can't get the ITS reference branch !");
+      delete rl;
+      return 7;
+   }
+   itsBranch->SetAddress(&itsRefs);
+
+
+   sprintf(fname,"%s/GoodCascades.root",dir);
+   TFile *csFile=TFile::Open(fname,"recreate");
+   TClonesArray dummy("AliTrackReference",1000), *nrefs=&dummy;
+   TClonesArray dumm("AliTrackReference",1000), *prefs=&dumm;
+   TClonesArray dum("AliTrackReference",1000), *brefs=&dum;
+   TTree csTree("csTree","Tree with info about the reconstructable cascades");
+   csTree.Branch("negative",&nrefs);
+   csTree.Branch("positive",&prefs);
+   csTree.Branch("bachelor",&brefs);
+
+
+   // *** Get information about the cuts ***
+   Double_t r2min=0.9*0.9;
+   Double_t r2max=2.9*2.9;
+
+
+   //********  Loop over generated events
+   for (Int_t e=0; e<nev; e++) {
+      rl->GetEvent(e);  csFile->cd();
+
+      Int_t np = rl->GetHeader()->GetNtrack();
+      cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
+
+      itsTree->GetEvent(e);
+      Int_t nk=itsRefs->GetEntriesFast();
+
+      AliStack *stack=rl->Stack();
+
+      AliTrackReference *nref=0, *pref=0, *bref=0;
+
+      Int_t nc=0;
+      while (np--) {
+        //cerr<<np<<'\r';
+         TParticle *cp=stack->Particle(np);
+
+         // *** only these cascades are "good" ***
+         Int_t code=cp->GetPdgCode();
+         if (code!=kXiMinus)    if (code!=kXiPlusBar)
+         if (code!=kOmegaMinus) if (code!=kOmegaPlusBar) continue; 
+
+         // *** daughter tracks must be "good" ***
+         Int_t v0lab=cp->GetFirstDaughter(), blab=cp->GetLastDaughter();
+         if (v0lab==blab) continue;
+         if (v0lab<0) continue;
+         if (blab<0) continue;
+
+         TParticle *p0=stack->Particle(v0lab);
+         TParticle *bp=stack->Particle(blab);
+         Int_t i;
+         if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar)) {
+            TParticle *p=p0; p0=bp; bp=p;
+            i=v0lab; v0lab=blab; blab=i;         
+            if ((p0->GetPdgCode()!=kLambda0)&&(p0->GetPdgCode()!=kLambda0Bar))
                                                                    continue;
-      }
+         }
 
-      /** is the bachelor "good" ? **/
-      Int_t i;
-      for (i=0; i<ngt; i++) if (label[i]==blab) break;
-      if (i==ngt) continue; 
-
-      /** is the V0 "good" ? **/
-      Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
-      if (nlab==plab) continue;
-      if (nlab<0) continue;
-      if (plab<0) continue;
-
-      for (i=0; i<ngt; i++) if (label[i]==nlab) break;
-      if (i==ngt) continue;
-      for (i=0; i<ngt; i++) if (label[i]==plab) break;
-      if (i==ngt) continue;
-
-      /*** fiducial volume ***/
-      Double_t x=bp->Vx(), y=bp->Vy(), r2=x*x+y*y; //bachelor
-      if (r2<r2min) continue;
-      if (r2>r2max) continue;
-      TParticle *pp=gAlice->GetMCApp()->Particle(plab);
-      x=pp->Vx(); y=pp->Vy(); r2=x*x+y*y;                      //V0
-      if (r2<r2min) continue;
-      if (r2>r2max) continue;
-
-      if (gAlice->GetMCApp()->Particle(plab)->GetPDG()->Charge() < 0.) {
-         i=plab; plab=nlab; nlab=i;
+         // ** is the bachelor "good" ? **
+         for (i=0; i<nk; i++) {
+            bref=(AliTrackReference*)itsRefs->UncheckedAt(i);
+             if (bref->Label()==blab) break;
+         }
+         if (i==nk) continue; 
+
+         // ** is the V0 "good" ? **
+         Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
+         if (nlab==plab) continue;
+         if (nlab<0) continue;
+         if (plab<0) continue;
+         if (stack->Particle(plab)->GetPDG()->Charge() < 0.) {
+            i=plab; plab=nlab; nlab=i;
+         }
+         for (i=0; i<nk; i++) {
+            nref=(AliTrackReference*)itsRefs->UncheckedAt(i);
+             if (nref->Label()==nlab) break;
+         }
+         if (i==nk) continue;
+         for (i=0; i<nk; i++) {
+            pref=(AliTrackReference*)itsRefs->UncheckedAt(i);
+             if (pref->Label()==plab) break;
+         }
+         if (i==nk) continue;
+
+
+         // *** fiducial volume ***
+         Double_t x=bp->Vx(), y=bp->Vy(), r2=x*x+y*y; //bachelor
+         if (r2<r2min) continue;
+         if (r2>r2max) continue;
+         TParticle *pp=stack->Particle(plab);
+         x=pp->Vx(); y=pp->Vy(); r2=x*x+y*y;          //V0
+         if (r2<r2min) continue;
+         if (r2>r2max) continue;
+
+         Int_t pdg=cp->GetPdgCode();
+         nref->SetLength(pdg);  //This will the cascade's PDG !
+
+         new((*nrefs)[nc]) AliTrackReference(*nref);
+         new((*prefs)[nc]) AliTrackReference(*pref);
+         new((*brefs)[nc]) AliTrackReference(*bref);
+
+         nc++;
       }
+      itsRefs->Clear();
 
-      gc[nc].code=code;
-      gc[nc].plab=plab;   gc[nc].nlab=nlab; gc[nc].blab=blab;
-      gc[nc].px=cp->Px(); gc[nc].py=cp->Py(); gc[nc].pz=cp->Pz();
-      gc[nc].x=bp->Vx(); gc[nc].y=bp->Vy(); gc[nc].z=bp->Vz();
-      nc++;
+      csTree.Fill();
+      nrefs->Clear(); prefs->Clear(); brefs->Clear();
 
-   }
+   } //**** end of the loop over generated events
 
-   delete rl;
+   csTree.Write();
+   csFile->Close();
 
-   return nc;
+   delete itsTree;
+   itsFile->Close();
+
+   delete rl;
+   return 0;
 }
index 71034d9..b86d419 100644 (file)
  ****************************************************************************/
 
 #if !defined(__CINT__) || defined(__MAKECINT__)
+  #include <TMath.h>
+  #include <TError.h>
   #include <Riostream.h>
-  #include <fstream.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 <TKey.h>
+  #include <TROOT.h>
 
-  #include "AliRun.h"
-  #include "AliHeader.h"
   #include "AliStack.h"
+  #include "AliHeader.h"
+  #include "AliTrackReference.h"
   #include "AliRunLoader.h"
-  #include "AliLoader.h"
-  #include "AliITSLoader.h"
+  #include "AliRun.h"
+  #include "AliESD.h"
+
   #include "AliITS.h"
   #include "AliITSgeom.h"
-  #include "AliITStrackerV2.h"
   #include "AliITStrackV2.h"
   #include "AliITSclusterV2.h"
-  #include "AliMagF.h"
-  #include "AliESD.h"
-
-  #include "TFile.h"
-  #include "TKey.h"
-  #include "TTree.h"
-  #include "TH1.h"
-  #include "TH2.h"
-  #include "TObjArray.h"
-  #include "TStyle.h"
-  #include "TCanvas.h"
-  #include "TLine.h"
-  #include "TText.h"
-  #include "TParticle.h"
+  #include "AliITSLoader.h"
 #endif
 
-struct GoodTrackITS {
-  Int_t lab;
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
-
-Int_t good_tracks_its(GoodTrackITS *gt, const Int_t max, 
-const char* evfoldname = AliConfig::fgkDefaultEventFolderName);
+Int_t GoodTracksITS(const Char_t *dir=".");
 
 extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
+extern TROOT *gROOT;
 
-Int_t AliITSComparisonV2(Float_t ptcutl=0.2, Float_t ptcuth=10.) {
-   cerr<<"Doing comparison...\n";
-   if (gAlice) {
-      delete gAlice->GetRunLoader();
-      delete gAlice; 
-      gAlice=0;
-   }
+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=".", Float_t ratio=0.0) {
+   gBenchmark->Start("AliITSComparisonV2");
+
+   ::Info("AliITSComparisonV2.C","Doing comparison...");
    
-   AliRunLoader *rl = AliRunLoader::Open("galice.root");
-   if (!rl) {
-       cerr<<"AliITSComparisonV2.C :Can't start sesion !\n";
-       return 1;
+
+   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,-300,300); 
+   hmpt->SetFillColor(6);
+
+   TH1F *hz=(TH1F*)gROOT->FindObject("hz");
+   if (!hz) hz=new TH1F("hz","Longitudinal impact parameter",30,-300,300); 
+
+
+
+   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 *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=(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 *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.,100.);
+
+   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);
+
+
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+
+   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;
+     }
    }
-   rl->LoadgAlice();
-   if (rl->GetAliRun())
-   AliKalmanTrack::SetConvConst(
-     1000/0.299792458/rl->GetAliRun()->Field()->SolenoidField()
-   );
-   else {
-      cerr<<"AliITSComparisonV2.C :Can't get AliRun !\n";
-      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;
    }
-   //rl->UnloadgAlice();
-   
-
-   /* Generate a list of "good" tracks */
-   const Int_t MAX=15000;
-   GoodTrackITS gt[MAX];
-   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++;
-        //PH         cerr<<ngood<<'\r';
-         if (ngood==MAX) {
-            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_its(gt,MAX,AliConfig::fgkDefaultEventFolderName);
-      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();
+   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);
+
 
-   TObjArray tarray(2000);
-   { /*Load tracks*/
-   TFile *ef=TFile::Open("AliESDs.root");
+   sprintf(fname,"%s/AliESDs.root",dir);
+   TFile *ef=TFile::Open(fname);
    if ((!ef)||(!ef->IsOpen())) {
-     ef=TFile::Open("AliESDits.root");
-     if ((!ef)||(!ef->IsOpen())) {
-       ::Fatal("AliITSComparisonV2.C","Can't open AliESDits.root !");
-     }
+      sprintf(fname,"%s/AliESDits.root",dir);
+      ef=TFile::Open(fname);
+      if ((!ef)||(!ef->IsOpen())) {
+         ::Error("AliITSComparisonV2.C","Can't open AliESDits.root !");
+         return 4;
+      }
    }
    TKey *key=0;
    TIter next(ef->GetListOfKeys());
-   if ((key=(TKey*)next())!=0) {
+
+
+
+   //******* Loop over events *********
+   Int_t e=0;
+   while ((key=(TKey*)next())!=0) {
+     cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
      AliESD *event=(AliESD*)key->ReadObj();
-     Int_t ntrk=event->GetNumberOfTracks();
-     for (Int_t i=0; i<ntrk; i++) {
-        AliESDtrack *t=event->GetTrack(i);
-       UInt_t status=t->GetStatus();
-       UInt_t flags=AliESDtrack::kTPCin|AliESDtrack::kITSin;
-
-        if ((status&AliESDtrack::kITSrefit)==0)
-         if ((status&flags)!=flags) continue;
-
-        AliITStrackV2 *iotrack=0;
-        iotrack=new AliITStrackV2(*t);
-        //if (t->GetConstrainedChi2()>=20) continue;   //  constrained 
-        //else iotrack=new AliITStrackV2(*t,kTRUE);    //     track
-        if ((status&flags)==flags) {
-           iotrack->PropagateTo(3.,0.0028,65.19);
-           iotrack->PropagateToVertex();
-        }
-        tarray.AddLast(iotrack);
+
+     Float_t field=event->GetMagneticField();
+     AliKalmanTrack::SetConvConst(1000/0.299792458/field);
+     Int_t nentr=event->GetNumberOfTracks();
+     allfound+=nentr;
+
+     if (itsTree->GetEvent(e++)==0) {
+        cerr<<"No reconstructable tracks !\n";
+        continue;
      }
-     delete event;
-   }
-   ef->Close();
-   }
-   Int_t nentr=tarray.GetEntriesFast();
 
-   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); 
-   hmpt->SetFillColor(6);
-   TH1F *hz=new TH1F("hz","Longitudinal impact parameter",30,-300,300); 
+     Int_t ngood=refs->GetEntriesFast(); 
+     allgood+=ngood;
 
-   AliITStrackV2 *trk=(AliITStrackV2*)tarray.UncheckedAt(0);
-   Double_t pmin=0.1*(100/0.299792458/0.2/trk->GetConvConst());
-   Double_t pmax=6.0+pmin;
+     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());
 
-   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
-   hg->SetLineColor(4); hg->SetLineWidth(2);
-   TH1F *hf=new TH1F("hf","Efficiency for fake tracks",30,pmin,pmax);
-   hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
+        if (ptg<ptcutl) continue;
+        if (ptg>ptcuth) continue;
 
-   //TH1F *hptw=new TH1F("hptw","Weghted pt",30,pmax,pmin);
+        allselected++;
 
-   TH1F *he =new TH1F("he","dE/dX for pions with 0.4<p<0.5 GeV/c",50,0.,100.);
-   TH2F *hep=new TH2F("hep","dE/dX vs momentum",50,0.,2.,50,0.,200.);
-   hep->SetMarkerStyle(8);
-   hep->SetMarkerSize(0.4);
+        hgood->Fill(ptg);
 
+        AliESDtrack *esd=0;
+        Int_t cnt=0;
+        Int_t pipe=0;
+        for (Int_t i=0; i<nentr; i++) {
+           AliESDtrack *t=event->GetTrack(i);
+          UInt_t status=t->GetStatus();
+          UInt_t flags=AliESDtrack::kTPCin|AliESDtrack::kITSin;
 
-   Int_t notf[MAX], nnotf=0;
-   Int_t fake[MAX], nfake=0;
-   Int_t mult[MAX], numb[MAX], nmult=0;
-
-   Int_t ng;
-   for (ng=0; ng<ngood; ng++) {
-      Int_t lab=gt[ng].lab, tlab=-1;
-      Double_t pxg=gt[ng].px, pyg=gt[ng].py, pzg=gt[ng].pz;
-      Double_t ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-      if (ptg<ptcutl) continue;
-      if (ptg>ptcuth) continue;
-
-      if (ptg>pmin) hgood->Fill(ptg);
-
-      AliITStrackV2 *track=0;
-      Int_t cnt=0;
-      Int_t j;
-      for (j=0; j<nentr; j++) {
-          AliITStrackV2 *trk=(AliITStrackV2*)tarray.UncheckedAt(j);
-          Int_t lbl=trk->GetLabel();
-          if (lab==TMath::Abs(lbl)) {
-           if (cnt==0) {track=trk; tlab=lbl;}
-            cnt++;
-          }
-      }
-      if (cnt==0) {
-        notf[nnotf++]=lab;
-        continue;
-      } else if (cnt>1){
-        mult[nmult]=lab;
-        numb[nmult]=cnt; nmult++;        
-      }
+           if ((status&AliESDtrack::kITSrefit)==0) continue;
+
+           if ((status&flags)==status) pipe=1;
+
+           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 (esd->GetITSFakeRatio()<ratio) tlab = TMath::Abs(tlab);
 
-      if (ptg>pmin) {
         if (lab==tlab) hfound->Fill(ptg);
         else {
           fake[nfake++]=lab;
           hfake->Fill(ptg); 
         }
-      }
 
-      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]);
+        AliITStrackV2 track(*esd);
+        if (pipe!=0) {
+           track.PropagateTo(3.,0.0028,65.19);
+           track.PropagateToVertex();  // This is not "exactly" the vertex 
+        }
 
-      Double_t phig=TMath::ATan2(pyg,pxg);
-      hp->Fill((phi - phig)*1000.);
+        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]);
 
-      Double_t lamg=TMath::ATan2(pzg,ptg);
-      hl->Fill((lam - lamg)*1000.);
+        Float_t phig=TMath::ATan2(ref->Py(),ref->Px());
+        hp->Fill((phi - phig)*1000.);
 
-      Double_t d=10000*track->GetD();
-      hmpt->Fill(d);
+        Float_t lamg=TMath::ATan2(ref->Pz(),ptg);
+        hl->Fill((lam - lamg)*1000.);
 
-      //hptw->Fill(ptg,TMath::Abs(d));
+        Double_t d=10000*track.GetD(ref->X(),ref->Y());
+        hmpt->Fill(d);
 
-      Double_t z=10000*track->GetZ();
-      hz->Fill(z);
+        Double_t z=10000*(track.GetZ()-ref->Z());
+        hz->Fill(z);
 
-      hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+        hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
 
-      Float_t mom=1./(pt_1*TMath::Cos(lam));
-      Float_t dedx=track->GetdEdx();
-      hep->Fill(mom,dedx,1.);
-      if (TMath::Abs(gt[ng].code)==211)
-         if (mom>0.4 && mom<0.5) he->Fill(dedx,1.);
-   }
+        Float_t mom=1./(pt_1*TMath::Cos(lam));
+        Float_t dedx=track.GetdEdx();
+        hep->Fill(mom,dedx,1.);
 
+        Int_t pdg=(Int_t)ref->GetLength();  //this is particle's PDG !
 
-   cout<<"\nList of Not found tracks :\n";
-   for (ng=0; ng<nnotf; ng++){
-     cout<<notf[ng]<<"\t";
-     if ((ng%9)==8) cout<<"\n";
-   }
-   cout<<"\n\nList of fake  tracks :\n";
-   for (ng=0; ng<nfake; ng++){
-     cout<<fake[ng]<<"\t";
-     if ((ng%9)==8) cout<<"\n";
-   }
-   cout<<"\n\nList of multiple found tracks :\n";
-   for (ng=0; ng<nmult; ng++) {
-       cout<<"id.   "<<mult[ng]
-           <<"     found - "<<numb[ng]<<"times\n";
-   }
-   cout<<endl;
+        if (TMath::Abs(pdg)==211) //pions
+           if (mom>0.4 && mom<0.5) he->Fill(dedx,1.);
 
-   cerr<<"Number of good tracks : "<<ngood<<endl;
-   cerr<<"Number of found tracks : "<<nentr<<endl;
+     }
+
+     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;
+
+     cout<<"Number of found tracks : "<<nentr<<endl;
+     cout<<"Number of \"good\" tracks : "<<ngood<<endl;
 
-   ng=(Int_t)hgood->GetEntries(); //cerr<<"Good tracks "<<ng<<endl;
-   if (ng!=0)  
-   cerr<<"Integral efficiency is about "<<hfound->GetEntries()/ng*100.<<" %\n";
-   cerr<<endl;
+     refs->Clear();
+     delete event;
+   } //***** End of the loop over events
+
+   ef->Close();
+   
+   delete itsTree;
+   refFile->Close();
+
+   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);
@@ -297,14 +323,6 @@ Int_t AliITSComparisonV2(Float_t ptcutl=0.2, Float_t ptcuth=10.) {
    hmpt->SetXTitle("(micron)");
    if (hmpt->GetEntries()<minc) hmpt->Draw(); else 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();
    
 
    TPad *p5=new TPad("p5","",0,0.6,1,1); p5->Draw(); p5->cd(); 
@@ -317,9 +335,9 @@ Int_t AliITSComparisonV2(Float_t ptcutl=0.2, Float_t ptcuth=10.) {
    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);
@@ -351,113 +369,188 @@ Int_t AliITSComparisonV2(Float_t ptcutl=0.2, Float_t ptcuth=10.) {
    c2->Write();
    fc.Close();
 
+   gBenchmark->Stop("AliITSComparisonV2");
+   gBenchmark->Show("AliITSComparisonV2");
+
    return 0;
 }
 
-Int_t good_tracks_its(GoodTrackITS *gt, const Int_t max, const char* evfoldname) {
-   AliRunLoader* rl = AliRunLoader::GetRunLoader(evfoldname);
-   if (rl == 0x0) {
-      ::Fatal("AliITSComparisonV2.C::good_tracks_its",
-              "Can not find Run Loader in Folder Named %s",
-              evfoldname);
+
+
+Int_t GoodTracksITS(const Char_t *dir) {
+   if (gAlice) { 
+       delete gAlice->GetRunLoader();
+       delete gAlice;//if everything was OK here it is already NULL
+       gAlice = 0x0;
    }
 
-   AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
-   if (itsl == 0x0) {
-       cerr<<"AliITSComparisonV2.C : Can not find ITSLoader\n";
-       delete rl;
-       return 3;
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
+
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
+   if (!rl) {
+      ::Error("GoodTracksITS","Can't start session !");
+      return 1;
    }
-   
+
    rl->LoadgAlice();
    rl->LoadHeader();
-   Int_t np = rl->GetHeader()->GetNtrack();
-
-   Int_t *good=new Int_t[np];
-   Int_t k;
-   for (k=0; k<np; k++) good[k]=0;
+   rl->LoadKinematics();
 
    AliITS *ITS=(AliITS*)rl->GetAliRun()->GetDetector("ITS");
    if (!ITS) {
-      cerr<<"can't get ITS !\n"; exit(8);
+      ::Error("GoodTracksITS","Can't get the ITS !");
+      delete rl;
+      return 2;
    }
    AliITSgeom *geom=ITS->GetITSgeom();
    if (!geom) {
-      cerr<<"can't get ITS geometry !\n"; exit(9);
+      ::Error("GoodTracksITS","Can't get the ITS geometry !"); 
+      delete rl;
+      return 3;
    }
 
+   AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
+   if (itsl == 0x0) {
+       ::Error("GoodTracksITS","Can not find the ITSLoader");
+       delete rl;
+       return 4;
+   }
    itsl->LoadRecPoints();
-   TTree *cTree=itsl->TreeR();
-   if (!cTree) {
-      cerr<<"Can't get cTree !\n"; exit(7);
+  
+
+   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;
    }
-   TClonesArray *clusters=new TClonesArray("AliITSclusterV2",10000);
-   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;
-     Int_t lay,lad,det;  geom->GetModuleId(k,lay,lad,det);
-     if (lay<1 || lay>6) {
-       cerr<<"wrong layer !\n"; exit(10);
+   TBranch *tpcBranch=tpcTree->GetBranch("TPC");
+   if (!tpcBranch) {
+      ::Error("GoodTracksITS","Can't get the TPC reference branch !");
+      delete rl;
+      return 7;
+   }
+   tpcBranch->SetAddress(&tpcRefs);
+
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+   TFile *itsFile=TFile::Open(fname,"recreate");
+   TClonesArray dummy("AliTrackReference",1000), *itsRefs=&dummy;
+   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;
      }
-     while (ncl--) {
-        AliITSclusterV2 *pnt=(AliITSclusterV2*)clusters->UncheckedAt(ncl);
-        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-1);
-        if (l0>=0) good[l0]|=mask; 
-        if (l1>=0) good[l1]|=mask; 
-        if (l2>=0) good[l2]|=mask;
+     TBranch *branch=cTree->GetBranch("Clusters");
+     if (!branch) {
+        ::Error("GoodTracksITS","Can't get the clusters branch !"); 
+        delete rl;
+        return 9;
+     }
+     TClonesArray dummy("AliITSclusterV2",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;
+         Int_t lay,lad,det;  geom->GetModuleId(k,lay,lad,det);
+         if (lay<1 || lay>6) {
+           ::Error("GoodTracksITS","Wrong layer !"); 
+            delete rl;
+            return 10;
+         }
+         while (ncl--) {
+            AliITSclusterV2 *pnt=(AliITSclusterV2*)clusters->UncheckedAt(ncl);
+            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-1);
+            if (l0>=0) good[l0]|=mask; 
+            if (l1>=0) good[l1]|=mask; 
+            if (l2>=0) good[l2]|=mask;
+         }
+         clusters->Clear();
      }
-   }
-   clusters->Delete(); delete clusters;
-   itsl->UnloadRawClusters();
-   
-   ifstream in("good_tracks_tpc");
-   if (!in) {
-     cerr<<"can't get good_tracks_tpc !\n"; exit(11);
-   }
    
-   rl->LoadKinematics();
-   AliStack* stack = rl->Stack();
-   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*)stack->Particle(lab);
-      if (p == 0x0) {
-         cerr<<"Can not get particle "<<lab<<endl;
-         nt++;
-         if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
-         continue;
-      }
-      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;}
-   }
 
-   delete[] good;
+     //****** 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();
 
-   rl->UnloadKinematics();
-   rl->UnloadHeader();
-   rl->UnloadgAlice();
+     delete[] good;
 
-   return nt;
+   } //*** end of the loop over generated events
+
+   itsTree.Write();
+   itsFile->Close();
+
+   delete tpcTree;
+   tpcFile->Close();
+
+   delete rl;
+   return 0;
 }
 
 
index c6f31f7..d8adee4 100644 (file)
@@ -8,91 +8,42 @@
  ****************************************************************************/
 
 #if !defined(__CINT__) || defined(__MAKECINT__)
-  #include "Riostream.h"
-  #include <fstream.h>
-
-  #include "AliRun.h"
-  #include "AliMC.h"
+  #include <TMath.h>
+  #include <TError.h>
+  #include <Riostream.h>
+  #include <TH1F.h>
+  #include <TTree.h>
+  #include <TParticle.h>
+  #include <TCanvas.h>
+  #include <TLine.h>
+  #include <TText.h>
+  #include <TBenchmark.h>
+  #include <TStyle.h>
+  #include <TKey.h>
+  #include <TROOT.h>
+
+  #include "AliStack.h"
   #include "AliHeader.h"
+  #include "AliTrackReference.h"
   #include "AliRunLoader.h"
-  #include "AliITSLoader.h"
-
-  #include "TH1.h"
-  #include "TFile.h"
-  #include "TKey.h"
-  #include "TTree.h"
-  #include "TObjArray.h"
-  #include "TStyle.h"
-  #include "TCanvas.h"
-  #include "TLine.h"
-  #include "TText.h"
-  #include "TParticle.h"
-  #include "TStopwatch.h"
-
   #include "AliRun.h"
   #include "AliESD.h"
-  #include "AliV0vertex.h"
 #endif
 
-struct GoodVertex {
-  Int_t nlab,plab;
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
-Int_t good_vertices(GoodVertex *gt, Int_t max);
+Int_t GoodV0s(const Char_t *dir=".");
 
 extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
+extern TROOT *gROOT;
 
-Int_t AliV0Comparison(Int_t code=310) { //Lambda=3122, LambdaBar=-3122
-   cerr<<"Doing comparison...\n";
-   TStopwatch timer;
-
-   /*** Check if the file with the "good" vertices exists ***/
-   GoodVertex gv[1000];
-   Int_t ngood=0;
-   ifstream in("good_vertices");
-   if (in) {
-      cerr<<"Reading good vertices...\n";
-      while (in>>gv[ngood].nlab>>gv[ngood].plab>>gv[ngood].code>>
-                 gv[ngood].px>>gv[ngood].py>>gv[ngood].pz>>
-                 gv[ngood].x >>gv[ngood].y >>gv[ngood].z) {
-         ngood++;
-         cerr<<ngood<<'\r';
-         if (ngood==1000) {
-            cerr<<"Too many good vertices !\n";
-            break;
-         }
-      }
-      if (!in.eof()) cerr<<"Read error (good_vertices) !\n";
-   } else {
-     /*** generate a file with the "good" vertices ***/
-      cerr<<"Marking good vertices (this will take a while)...\n";
-      ngood=good_vertices(gv,1000);
-      ofstream out("good_vertices");
-      if (out) {
-         for (Int_t ngd=0; ngd<ngood; ngd++)            
-           out<<gv[ngd].nlab<<' '<<gv[ngd].plab<<' '<<gv[ngd].code<<' '<<
-                 gv[ngd].px<<' '<<gv[ngd].py<<' '<<gv[ngd].pz<<' '<<
-                 gv[ngd].x <<' '<<gv[ngd].y <<' '<<gv[ngd].z <<endl;
-      } else cerr<<"Can not open file (good_vertices) !\n";
-      out.close();
-   }
+static Int_t allgood=0;
+static Int_t allfound=0;
 
-   
-   AliESD *event=0;
-   { /*** Load reconstructed vertices ***/
-   TFile *ef=TFile::Open("AliESDv0.root");
-   if ((!ef)||(!ef->IsOpen())) {
-     cerr<<"AliV0Comparison.C: Can't open AliESDv0.root !\n";
-     return 1;
-   }
-   TKey *key=0;
-   TIter next(ef->GetListOfKeys());
-   if ((key=(TKey*)next())!=0) event=(AliESD*)key->ReadObj();
-   ef->Close();
-   }
-   Int_t nentr=event->GetNumberOfV0s();
+Int_t AliV0Comparison(Int_t code=310, const Char_t *dir=".") { 
+   //Lambda=3122, LambdaBar=-3122
+   gBenchmark->Start("AliV0Comparison");
+
+   ::Info("AliV0Comparison.C","Doing comparison...");
 
 
    const Double_t V0window=0.05;
@@ -110,153 +61,269 @@ Int_t AliV0Comparison(Int_t code=310) { //Lambda=3122, LambdaBar=-3122
    default: cerr<<"Invalid PDG code !\n"; return 1;
    }
 
-   TH1F *hp=new TH1F("hp","Angular Resolution",30,-30.,30.); //phi resolution 
+   TH1F *hp=(TH1F*)gROOT->FindObject("hp");
+   if (!hp) hp=new TH1F("hp","PHI Resolution",30,-30.,30.);
    hp->SetXTitle("(mrad)"); hp->SetFillColor(2);
-   TH1F *hl=new TH1F("hl","Lambda Resolution",30,-30,30);
-   hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013); 
-   TH1F *hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
+
+   TH1F *hl=(TH1F*)gROOT->FindObject("hl");
+   if (!hl) hl=new TH1F("hl","LAMBDA Resolution",30,-30,30);
+   hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013);
+   TH1F *hpt=(TH1F*)gROOT->FindObject("hpt");
+   if (!hpt) hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
    hpt->SetXTitle("(%)"); hpt->SetFillColor(2); 
 
-   TH1F *hx=new TH1F("hx","Position Resolution (X,Y)",30,-3.,3.); //x res. 
+   TH1F *hx=(TH1F*)gROOT->FindObject("hx");
+   if (!hx) hx=new TH1F("hx","Position Resolution (X)",30,-3.,3.);
    hx->SetXTitle("(mm)"); hx->SetFillColor(6);
-   TH1F *hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.);   //y res
+
+   TH1F *hy=(TH1F*)gROOT->FindObject("hy");
+   if (!hy) hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.); 
    hy->SetXTitle("(mm)"); hy->SetFillColor(1); hy->SetFillStyle(3013);
-   TH1F *hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);   //z res. 
+
+   TH1F *hz=(TH1F*)gROOT->FindObject("hz");
+   if (!hz) hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);
    hz->SetXTitle("(mm)"); hz->SetFillColor(6);
 
 
    Double_t pmin=0.2, pmax=4.2; Int_t nchan=20;
-   TH1F *hgood=new TH1F("hgood","Good Vertices",nchan,pmin,pmax);    
-   TH1F *hfound=new TH1F("hfound","Found Vertices",nchan,pmin,pmax);
-   TH1F *hfake=new TH1F("hfake","Fake Vertices",nchan,pmin,pmax);
-   TH1F *hg=new TH1F("hg","Efficiency for Good Vertices",nchan,pmin,pmax);
+   TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");    
+   if (!hgood) hgood=new TH1F("hgood","Good Vertices",nchan,pmin,pmax);
+    
+   TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
+   if (!hfound) hfound=new TH1F("hfound","Found Vertices",nchan,pmin,pmax);
+
+   TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
+   if (!hfake) hfake=new TH1F("hfake","Fake Vertices",nchan,pmin,pmax);
+
+   TH1F *hg=(TH1F*)gROOT->FindObject("hg");
+   if (!hg) hg=new TH1F("hg","Efficiency for Good Vertices",nchan,pmin,pmax);
    hg->SetLineColor(4); hg->SetLineWidth(2);
-   TH1F *hf=new TH1F("hf","Probability of Fake Vertices",nchan,pmin,pmax);
+
+   TH1F *hf=(TH1F*)gROOT->FindObject("hf");
+   if (!hf) hf=new TH1F("hf","Probability of Fake Vertices",nchan,pmin,pmax);
    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
 
+
    Double_t mmin=V0mass-V0window, mmax=V0mass+V0window;
-   TH1F *v0s =new TH1F("v0s","V0s Effective Mass",40, mmin, mmax);
-   v0s->SetXTitle("(GeV)");
-   v0s->SetLineColor(4); v0s->SetLineWidth(4);
-   TH1F *v0sf =new TH1F("v0sf","Fake V0s Effective Mass",40, mmin, mmax);
+   TH1F *v0s=(TH1F*)gROOT->FindObject("v0s");
+   if (!v0s) v0s=new TH1F("v0s","V0s Effective Mass",40, mmin, mmax);
+   v0s->SetXTitle("(GeV)"); v0s->SetLineColor(4); v0s->SetLineWidth(4);
+
+   TH1F *v0sf=(TH1F*)gROOT->FindObject("v0sf");
+   if (!v0sf) v0sf=new TH1F("v0sf","Fake V0s Effective Mass",40, mmin, mmax);
    v0sf->SetXTitle("(GeV)"); v0sf->SetFillColor(6);
 
 
-   Double_t pxg=0.,pyg=0.,ptg=0.;
-   Int_t nlab=-1, plab=-1;
-   Int_t i;
-   for (i=0; i<nentr; i++) {
-       AliESDv0 *vertex=event->GetV0(i);
-
-       Int_t nidx=TMath::Abs(vertex->GetNindex());
-       Int_t pidx=TMath::Abs(vertex->GetPindex());
-
-       AliESDtrack *ntrack=event->GetTrack(nidx);
-       AliESDtrack *ptrack=event->GetTrack(pidx);
-
-       nlab=TMath::Abs(ntrack->GetLabel());
-       plab=TMath::Abs(ptrack->GetLabel());
-
-       /** Kinematical cuts **/
-       Double_t pxn,pyn,pzn; vertex->GetNPxPyPz(pxn,pyn,pzn); 
-       Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
-       if (ptn < ptncut) continue;
-       Double_t pxp,pyp,pzp; vertex->GetPPxPyPz(pxp,pyp,pzp); 
-       Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
-       if (ptp < ptpcut) continue;
-       Double_t kine=vertex->ChangeMassHypothesis(code);
-       //if (TMath::Abs(kine)>kinecut) continue;
-
-       Double_t mass=vertex->GetEffMass();
-       v0s->Fill(mass);
-       v0sf->Fill(mass);
-
-       Int_t j;
-       for (j=0; j<ngood; j++) {
-          if (gv[j].code != vertex->GetPdgCode()) continue;
-          if (gv[j].nlab == nlab)
-          if (gv[j].plab == plab) break;
-       }
-
-       if (TMath::Abs(mass-V0mass)>V0width) continue;
-
-       Double_t px,py,pz; vertex->GetPxPyPz(px,py,pz);
-       Double_t pt=TMath::Sqrt(px*px+py*py);
-
-       if (j==ngood) {
-          hfake->Fill(pt);
-          cerr<<"Fake vertex: ("<<nlab<<","<<plab<<")\n";
-          continue;
-       }
-       v0sf->Fill(mass,-1);
-
-       pxg=gv[j].px; pyg=gv[j].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-       Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
-       Double_t lamg=TMath::ATan2(gv[j].pz,ptg), lam=TMath::ATan2(pz,pt);
-       hp->Fill((phi - phig)*1000.);
-       hl->Fill((lam - lamg)*1000.);
-       hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
-
-       Double_t x,y,z; vertex->GetXYZ(x,y,z);
-       hx->Fill((x-gv[j].x)*10);
-       hy->Fill((y-gv[j].y)*10);
-       hz->Fill((z-gv[j].z)*10);
-
-       hfound->Fill(ptg);
-       gv[j].nlab=-1;
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodV0s.root",dir);
+
+   TFile *refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+   ::Info("AliV0Comparison.C","Marking good V0s (will take a while)...");
+     if (GoodV0s(dir)) {
+        ::Error("AliV0Comparison.C","Can't generate the reference file !");
+        return 1;
+     }
+   }
+   refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Error("AliV0Comparison.C","Can't open the reference file !");
+     return 1;
+   }   
+  
+   TTree *v0Tree=(TTree*)refFile->Get("v0Tree");
+   if (!v0Tree) {
+     ::Error("AliV0Comparison.C","Can't get the reference tree !");
+     return 2;
+   }
+   TBranch *pbranch=v0Tree->GetBranch("positive");
+   if (!pbranch) {
+     ::Error("AliV0Comparison.C","Can't get the positive daughter branch !");
+     return 3;
+   }
+   TClonesArray dummy("AliTrackReference",1000), *prefs=&dummy;
+   pbranch->SetAddress(&prefs);
 
+   TBranch *nbranch=v0Tree->GetBranch("negative");
+   if (!nbranch) {
+     ::Error("AliV0Comparison.C","Can't get the negative daughter branch !");
+     return 4;
    }
-   for (i=0; i<ngood; i++) {
-      if (gv[i].code != code) continue;
-      pxg=gv[i].px; pyg=gv[i].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-      hgood->Fill(ptg);
-      nlab=gv[i].nlab; plab=gv[i].plab;
-      if (nlab < 0) continue;
-      cerr<<"Vertex ("<<nlab<<','<<plab<<") has not been found !\n";
+   TClonesArray dumm("AliTrackReference",1000), *nrefs=&dumm;
+   nbranch->SetAddress(&nrefs);
+
+   
+   sprintf(fname,"%s/AliESDs.root",dir);
+   TFile *ef=TFile::Open(fname);
+   if ((!ef)||(!ef->IsOpen())) {
+      sprintf(fname,"%s/AliESDv0.root",dir);
+      ef=TFile::Open(fname);
+      if ((!ef)||(!ef->IsOpen())) {
+         ::Error("AliV0Comparison.C","Can't open AliESDv0.root !");
+         return 5;
+      }
    }
+   TKey *key=0;
+   TIter next(ef->GetListOfKeys());
+
+
+   //******* Loop over events *********
+   Int_t e=0;
+   while ((key=(TKey*)next())!=0) { 
+      cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
+      AliESD *event=(AliESD*)key->ReadObj();
+
+      Int_t nentr=event->GetNumberOfV0s();
+      allfound+=nentr;
 
-   delete event;
+      if (v0Tree->GetEvent(e++)==0) {
+         cerr<<"No reconstructable V0s !\n";
+         continue;
+      }
 
-   Stat_t ng=hgood->GetEntries();
-   Stat_t nf=hfound->GetEntries();
+      Int_t ngood=prefs->GetEntriesFast(),ng=0; 
 
-   cerr<<"Number of found vertices: "<<nentr<<" ("<<nf<<" in the peak)\n";
-   cerr<<"Number of good vertices: "<<ng<<endl;
+      Double_t pxg=0.,pyg=0.,pzg=0.,ptg=0.;
+      Int_t nlab=-1, plab=-1;
+      Int_t i;
+      for (i=0; i<nentr; i++) {
+          AliESDv0 *vertex=event->GetV0(i);
+
+          Int_t nidx=TMath::Abs(vertex->GetNindex());
+          Int_t pidx=TMath::Abs(vertex->GetPindex());
+
+          AliESDtrack *ntrack=event->GetTrack(nidx);
+          AliESDtrack *ptrack=event->GetTrack(pidx);
+
+          nlab=TMath::Abs(ntrack->GetLabel());
+          plab=TMath::Abs(ptrack->GetLabel());
+
+          /** Kinematical cuts **/
+          Double_t pxn,pyn,pzn; vertex->GetNPxPyPz(pxn,pyn,pzn); 
+          Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
+          if (ptn < ptncut) continue;
+          Double_t pxp,pyp,pzp; vertex->GetPPxPyPz(pxp,pyp,pzp); 
+          Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
+          if (ptp < ptpcut) continue;
+          Double_t kine=vertex->ChangeMassHypothesis(code);
+          //if (TMath::Abs(kine)>kinecut) continue;
+
+          Double_t mass=vertex->GetEffMass();
+          v0s->Fill(mass);
+          v0sf->Fill(mass);
+
+          AliTrackReference *nref=0, *pref=0;
+          Int_t j;
+          for (j=0; j<ngood; j++) {
+              nref=(AliTrackReference*)nrefs->UncheckedAt(j);
+              pref=(AliTrackReference*)prefs->UncheckedAt(j);
+              if (nref->Label() == nlab)
+              if (pref->Label() == plab) break;
+          }
+
+          if (TMath::Abs(mass-V0mass)>V0width) continue;
+
+          Double_t px,py,pz; vertex->GetPxPyPz(px,py,pz);
+          Double_t pt=TMath::Sqrt(px*px+py*py);
+
+          if (j==ngood) {
+             hfake->Fill(pt);
+             cout<<"Fake vertex: ("<<nlab<<","<<plab<<")\n";
+             continue;
+          }
+          v0sf->Fill(mass,-1);
+
+          pxg=nref->Px()+pref->Px(); pyg=nref->Py()+pref->Py();
+          pzg=nref->Pz()+pref->Pz(); 
+          ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
+          Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
+          Double_t lamg=TMath::ATan2(pzg,ptg), lam=TMath::ATan2(pz,pt);
+          hp->Fill((phi - phig)*1000.);
+          hl->Fill((lam - lamg)*1000.);
+          hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
+
+          Double_t x,y,z; vertex->GetXYZ(x,y,z);
+          hx->Fill((x - nref->X())*10);
+          hy->Fill((y - nref->Y())*10);
+          hz->Fill((z - nref->Z())*10);
+
+          hfound->Fill(ptg);
+          nref->SetLabel(-1);
+
+      }
+      for (i=0; i<ngood; i++) {
+         AliTrackReference *nref=(AliTrackReference*)nrefs->UncheckedAt(i);
+         AliTrackReference *pref=(AliTrackReference*)prefs->UncheckedAt(i);
+         Int_t pdg=(Int_t)nref->GetLength();//this is the mother's PDG !
+         if (code!=pdg) continue;
+         ng++;
+         pxg=nref->Px()+pref->Px(); pyg=nref->Py()+pref->Py(); 
+         ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
+         hgood->Fill(ptg);
+         nlab=nref->Label(); plab=pref->Label();
+         if (nlab < 0) continue;
+         cout<<"Vertex ("<<nlab<<','<<plab<<") has not been found !\n";
+      }
+      allgood+=ng;
+
+      cout<<"Number of found V0s : "<<nentr<<endl;
+      cout<<"Number of \"good\" V0s : "<<ng<<endl;
+
+      prefs->Clear();
+      nrefs->Clear();
+      delete event;
+
+   } //**** End of the loop over events
+
+   ef->Close();
+   
+   delete v0Tree;
+   refFile->Close();
 
-   if (ng!=0) 
-      cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
+   Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
+   if (ng!=0) cout<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
+   cout<<"Total number of found V0s: "<<allfound<<" ("<<nf<<" in the peak)\n";
+   cout<<"Total number of \"good\" V0s: "<<allgood<<endl;
 
    gStyle->SetOptStat(111110);
    gStyle->SetOptFit(1);
 
-   TCanvas *c1=new TCanvas("c1","",0,0,580,610);
-   c1->Divide(2,2);
+   TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
+   if (!c1) {
+      c1=new TCanvas("c1","",0,0,580,610);
+      c1->Divide(2,2);
+   }
+
+   Int_t minc=33; 
 
    c1->cd(1);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
-   //hp->Fit("gaus");
-   hp->Draw();
+   if (hp->GetEntries()<minc) hp->Draw(); else hp->Fit("gaus");
    hl->Draw("same"); c1->cd();
 
    c1->cd(2);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //hpt->Fit("gaus"); c1->cd();
-   hpt->Draw(); c1->cd();
+   if (hpt->GetEntries()<minc) hpt->Draw(); else hpt->Fit("gaus");
+   c1->cd();
 
    c1->cd(3);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
-   //hx->Fit("gaus"); 
-   hx->Draw(); 
+   if (hx->GetEntries()<minc) hx->Draw(); else hx->Fit("gaus"); 
    hy->Draw("same"); c1->cd();
 
    c1->cd(4);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //hz->Fit("gaus");
-   hz->Draw();
+   if (hz->GetEntries()<minc) hz->Draw(); else hz->Fit("gaus");
 
+   c1->Update();   
 
-   TCanvas *c2=new TCanvas("c2","",600,0,580,610);
-   c2->Divide(1,2);
+   TCanvas *c2=(TCanvas*)gROOT->FindObject("c2");
+   if (!c2) {
+      c2=new TCanvas("c2","",600,0,580,610);
+      c2->Divide(1,2);
+   }
 
    c2->cd(1);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
@@ -288,8 +355,8 @@ Int_t AliV0Comparison(Int_t code=310) { //Lambda=3122, LambdaBar=-3122
 
    c2->cd(2);
    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
-   //v0s->Fit("gaus","","",V0mass-V0width,V0mass+V0width);
-   v0s->Draw();
+   if (v0s->GetEntries()<minc) v0s->Draw();
+   else v0s->Fit("gaus","","",V0mass-V0width,V0mass+V0width);
    v0sf->Draw("same");
    Double_t max=v0s->GetMaximum();
    TLine *line3 = new TLine(V0mass-V0width,0.,V0mass-V0width,max);
@@ -297,92 +364,155 @@ Int_t AliV0Comparison(Int_t code=310) { //Lambda=3122, LambdaBar=-3122
    TLine *line4 = new TLine(V0mass+V0width,0.,V0mass+V0width,max);
    line4->Draw("same");
 
-   timer.Stop(); timer.Print();
+   c2->Update();
+
+   TFile fc("AliV0Comparison.root","RECREATE");
+   c1->Write();
+   c2->Write();
+   fc.Close();
+
+   gBenchmark->Stop("AliV0Comparison");
+   gBenchmark->Show("AliV0Comparison");
 
    return 0;
 }
 
-Int_t good_vertices(GoodVertex *gv, Int_t max) {
-   Int_t nv=0;
-   /*** Get information about the cuts ***/
-   Double_t r2min=0.9*0.9;
-   Double_t r2max=2.9*2.9;
 
-   /*** Get labels of the "good" tracks ***/
-   Double_t dd; Int_t id, label[15000], ngt=0;
-   ifstream in("good_tracks_its");
-   if (!in) {
-     cerr<<"Can't open the file good_tracks_its \n";
-     return nv;
-   }
-   while (in>>label[ngt]>>id>>dd>>dd>>dd>>dd>>dd>>dd) {
-     ngt++;
-     if (ngt>=15000) {
-       cerr<<"Too many good ITS tracks !\n";
-       return nv;
-     }
-   }   
-   if (!in.eof()) {
-      cerr<<"Read error (good_tracks_its) !\n";
-      return nv;
+Int_t GoodV0s(const Char_t *dir) {
+   if (gAlice) { 
+       delete gAlice->GetRunLoader();
+       delete gAlice;//if everything was OK here it is already NULL
+       gAlice = 0x0;
    }
 
-   /*** Get an access to the kinematics ***/
-   if (gAlice) {
-      delete gAlice->GetRunLoader();
-      delete gAlice; 
-      gAlice=0;
-   }   
-   AliRunLoader *rl = AliRunLoader::Open("galice.root");
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
+
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
    if (!rl) {
-       cerr<<"AliV0Comparison.C, good_vertices :Can't start session !\n";
-       return 1;
+      ::Error("GoodTracksITS","Can't start session !");
+      return 1;
    }
 
    rl->LoadgAlice();
    rl->LoadHeader();
    rl->LoadKinematics();
-   Int_t np = rl->GetHeader()->GetNtrack();
-
-   while (np--) {
-      cerr<<np<<'\r';
-      TParticle *p0=gAlice->GetMCApp()->Particle(np);
-
-      /*** only these V0s are "good" ***/
-      Int_t code=p0->GetPdgCode();
-      if (code!=kK0Short)
-      if (code!=kLambda0)
-      if (code!=kLambda0Bar) continue; 
-
-      /*** daughter tracks should be "good" ***/
-      Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
-      if (nlab==plab) continue;
-      if (nlab<0) continue;
-      if (plab<0) continue;
-      Int_t i;
-      for (i=0; i<ngt; i++) if (label[i]==nlab) break;
-      if (i==ngt) continue;
-      for (i=0; i<ngt; i++) if (label[i]==plab) break;
-      if (i==ngt) continue;
-
-      /*** fiducial volume ***/
-      TParticle *p=gAlice->GetMCApp()->Particle(nlab);
-      Double_t x=p->Vx(), y=p->Vy(), z=p->Vz(), r2=x*x+y*y;
-      if (r2<r2min) continue;
-      if (r2>r2max) continue;
+
+
+   Int_t nev=rl->GetNumberOfEvents();
+   ::Info("GoodV0s","Number of events : %d\n",nev);  
+
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+   TFile *itsFile=TFile::Open(fname);
+   if ((!itsFile)||(!itsFile->IsOpen())) {
+       ::Error("GoodV0s","Can't open the GoodTracksITS.root !");
+       delete rl;
+       return 5; 
+   }
+   TClonesArray dum("AliTrackReference",1000), *itsRefs=&dum;
+   TTree *itsTree=(TTree*)itsFile->Get("itsTree");
+   if (!itsTree) {
+       ::Error("GoodV0s","Can't get the ITS reference tree !");
+       delete rl;
+       return 6;
+   }
+   TBranch *itsBranch=itsTree->GetBranch("ITS");
+   if (!itsBranch) {
+      ::Error("GoodV0s","Can't get the ITS reference branch !");
+      delete rl;
+      return 7;
+   }
+   itsBranch->SetAddress(&itsRefs);
+
+
+   sprintf(fname,"%s/GoodV0s.root",dir);
+   TFile *v0File=TFile::Open(fname,"recreate");
+   TClonesArray dummy("AliTrackReference",1000), *nrefs=&dummy;
+   TClonesArray dumm("AliTrackReference",1000), *prefs=&dumm;
+   TTree v0Tree("v0Tree","Tree with info about the reconstructable V0s");
+   v0Tree.Branch("negative",&nrefs);
+   v0Tree.Branch("positive",&prefs);
+
+
+   /*** Some information about the cuts ***/
+   Double_t r2min=0.9*0.9;
+   Double_t r2max=2.9*2.9;
+
+
+
+   //********  Loop over generated events
+   for (Int_t e=0; e<nev; e++) {
+      rl->GetEvent(e);  v0File->cd();
+
+      Int_t np = rl->GetHeader()->GetNtrack();
+      cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
+
+      itsTree->GetEvent(e);
+      Int_t nk=itsRefs->GetEntriesFast();
+
+      AliStack *stack=rl->Stack();
+
+      AliTrackReference *nref=0, *pref=0;
+
+      Int_t nv=0;
+      while (np--) {
+       //cerr<<np<<'\r';
+         TParticle *p0=stack->Particle(np);
+
+         /*** only these V0s are "good" ***/
+         Int_t code=p0->GetPdgCode();
+         if (code!=kK0Short)
+         if (code!=kLambda0)
+         if (code!=kLambda0Bar) continue; 
+
+         /*** daughter tracks should be "good" ***/
+         Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
+         if (nlab==plab) continue;
+         if (nlab<0) continue;
+         if (plab<0) continue;
+         Int_t i;
+         if (stack->Particle(plab)->GetPDG()->Charge() < 0.) {
+            i=plab; plab=nlab; nlab=i;
+         }
+
+         for (i=0; i<nk; i++) {
+            nref=(AliTrackReference*)itsRefs->UncheckedAt(i);
+             if (nref->Label()==nlab) break;
+         }
+         if (i==nk) continue;
+         for (i=0; i<nk; i++) {
+            pref=(AliTrackReference*)itsRefs->UncheckedAt(i);
+             if (pref->Label()==plab) break;
+         }
+         if (i==nk) continue;
+
+         /*** fiducial volume ***/
+         TParticle *p=stack->Particle(nlab);
+         Double_t x=p->Vx(), y=p->Vy(), z=p->Vz(), r2=x*x+y*y;
+         if (r2<r2min) continue;
+         if (r2>r2max) continue;
        
-      if (gAlice->GetMCApp()->Particle(plab)->GetPDG()->Charge() < 0.) {
-         i=plab; plab=nlab; nlab=i;
+         Int_t pdg=p0->GetPdgCode();
+         nref->SetLength(pdg);  //This will the V0's PDG !
+
+         new((*nrefs)[nv]) AliTrackReference(*nref);
+         new((*prefs)[nv]) AliTrackReference(*pref);
+
+         nv++;
       }
-      
-      gv[nv].code=code;
-      gv[nv].plab=plab; gv[nv].nlab=nlab;
-      gv[nv].px=p0->Px(); gv[nv].py=p0->Py(); gv[nv].pz=p0->Pz();
-      gv[nv].x=x; gv[nv].y=y; gv[nv].z=z;
-      nv++;
-   }
-   delete rl;
+      itsRefs->Clear();
 
-   return nv;
+      v0Tree.Fill();
+      nrefs->Clear(); prefs->Clear();
+
+   }  //**** end of the loop over generated events
+
+   v0Tree.Write();
+   v0File->Close();
+
+   delete itsTree;
+   itsFile->Close();
+
+   delete rl;
+   return 0;
 }
index f0a59f6..707ae6b 100644 (file)
@@ -5,29 +5,38 @@
 //********************************************************************
 
 #if !defined( __CINT__) || defined(__MAKECINT__)
+  #include <TMath.h>
+  #include <TError.h>
   #include <Riostream.h>
-  #include "TKey.h"
-  #include "TFile.h"
-  #include "TH1F.h"
-  #include "TH2F.h"
-  #include "TCanvas.h"
-  #include "TStopwatch.h"
-  #include "TParticle.h"
-  #include "TROOT.h"
-
-  #include "AliRun.h"
-  #include "AliStack.h"
-  #include "AliRunLoader.h"
-  #include "AliLoader.h"
-
-  #include "AliESD.h"
+  #include <TH1F.h>
+  #include <TH2F.h>
+  #include <TParticle.h>
+  #include <TCanvas.h>
+  #include <TBenchmark.h>
+  #include <TKey.h>
+  #include <TROOT.h>
+
+  #include <AliStack.h>
+  #include <AliRunLoader.h>
+  #include <AliRun.h>
+  #include <AliESD.h>
 #endif
 
 extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
 extern TROOT *gROOT;
 
+static Int_t allpisel=0;
+static Int_t allkasel=0;
+static Int_t allprsel=0;
+static Int_t allnsel=0;
+
 Int_t AliESDComparison(const Char_t *dir=".") { 
-   Double_t pi=0.2,pa=2;
+   gBenchmark->Start("AliESDComparison");
+
+   ::Info("AliESDComparison.C","Doing comparison...");
+
+   Double_t pi=0.2,pa=3;
 
    TH2F *tpcHist=(TH2F*)gROOT->FindObject("tpcHist");
    if (!tpcHist)
@@ -85,16 +94,16 @@ Int_t AliESDComparison(const Char_t *dir=".") {
    sprintf(fname,"%s/galice.root",dir);
    AliRunLoader *rl = AliRunLoader::Open(fname);
    if (rl == 0x0) {
-      cerr<<"Can not open session"<<endl;
+      ::Error("AliESDComparison.C","Can not open session !");
       return 1;
    }
    if (rl->LoadgAlice()) {
-      cerr<<"LoadgAlice returned error"<<endl;
+      ::Error("AliESDComparison.C","LoadgAlice returned error !");
       delete rl;
       return 1;
    }
    if (rl->LoadHeader()) {
-      cerr<<"LoadHeader returned error"<<endl;
+      ::Error("AliESDComparison.C","LoadHeader returned error !");
       delete rl;
       return 1;
    }
@@ -102,10 +111,11 @@ Int_t AliESDComparison(const Char_t *dir=".") {
 
    sprintf(fname,"%s/AliESDs.root",dir);
    TFile *ef=TFile::Open(fname);
-   if (!ef->IsOpen()) {cerr<<"Can't AliESDs.root !\n"; return 1;}
-
-   TStopwatch timer;
-   Int_t rc=0,n=0;
+   if (!ef || !ef->IsOpen()) {
+      ::Error("AliESDComparison.C","Can't AliESDs.root !"); 
+      delete rl;
+      return 1;
+   }
    TKey *key=0;
    TIter next(ef->GetListOfKeys());
 
@@ -113,91 +123,92 @@ Int_t AliESDComparison(const Char_t *dir=".") {
    Double_t c[5]={0.01, 0.01, 0.85, 0.10, 0.05};
 
    //******* The loop over events
+   Int_t e=0;
    while ((key=(TKey*)next())!=0) {
-     rl->GetEvent(n);
-     AliStack *stack = rl->Stack();
-
-     cerr<<"Processing event number : "<<n++<<endl;
+      cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
 
-     ef->cd();
-
-     AliESD *event=(AliESD*)key->ReadObj();
+      rl->GetEvent(e); ef->cd();
+      e++;
 
-     Int_t ntrk=event->GetNumberOfTracks();
-     cerr<<"Number of ESD tracks : "<<ntrk<<endl; 
+      AliESD *event=(AliESD*)key->ReadObj();
 
-     // ****** The loop over tracks
+      Int_t ntrk=event->GetNumberOfTracks();
+      cerr<<"Number of ESD tracks : "<<ntrk<<endl; 
 
-     Int_t pisel=0,kasel=0,prsel=0,nsel=0;
+      Int_t pisel=0,kasel=0,prsel=0,nsel=0;
 
-     while (ntrk--) {
-       AliESDtrack *t=event->GetTrack(ntrk);
+      AliStack *stack = rl->Stack();
 
-       UInt_t status=AliESDtrack::kESDpid;
-       status|=AliESDtrack::kITSpid; 
-       status|=AliESDtrack::kTPCpid; 
-       status|=AliESDtrack::kTOFpid; 
+      while (ntrk--) {
+         AliESDtrack *t=event->GetTrack(ntrk);
 
-       if ((t->GetStatus()&status) == status) {
-         nsel++;
+         UInt_t status=AliESDtrack::kESDpid;
+         status|=AliESDtrack::kITSpid; 
+         status|=AliESDtrack::kTPCpid; 
+         status|=AliESDtrack::kTOFpid; 
 
-         Double_t p=t->GetP();
-        Double_t dedx=t->GetTPCsignal();
-         tpcHist->Fill(p,dedx,1);
+        if ((t->GetStatus()&status) == status) {
+           nsel++;
 
-         Int_t lab=TMath::Abs(t->GetLabel());
-         TParticle *part=stack->Particle(lab);
-         Int_t code=part->GetPdgCode();
+           Double_t p=t->GetP();
+          Double_t dedx=t->GetTPCsignal();
+           tpcHist->Fill(p,dedx,1);
 
-         Double_t r[10]; t->GetESDpid(r);
+           Int_t lab=TMath::Abs(t->GetLabel());
+           TParticle *part=stack->Particle(lab);
+           Int_t code=part->GetPdgCode();
 
-         Double_t rcc=0.;
-         Int_t i;
-         for (i=0; i<AliESDtrack::kSPECIES; i++) rcc+=(c[i]*r[i]);
-         if (rcc==0.) continue;
+           Double_t r[10]; t->GetESDpid(r);
 
-        //Here we apply Bayes' formula
-         Double_t w[10];
-         for (i=0; i<AliESDtrack::kSPECIES; i++) w[i]=c[i]*r[i]/rcc;
+           Double_t rcc=0.;
+           Int_t i;
+           for (i=0; i<AliESDtrack::kSPECIES; i++) rcc+=(c[i]*r[i]);
+           if (rcc==0.) continue;
 
-         if (TMath::Abs(code)==2212) prR->Fill(p);
-         if (w[4]>w[3] && w[4]>w[2] && w[4]>w[1] && w[4]>w[0]) {//proton
-           prsel++;
-           prG->Fill(p);
-            if (TMath::Abs(code)!=2212) prF->Fill(p);
-         }
+          //Here we apply Bayes' formula
+           Double_t w[10];
+           for (i=0; i<AliESDtrack::kSPECIES; i++) w[i]=c[i]*r[i]/rcc;
 
-         if (TMath::Abs(code)==321) kaR->Fill(p);
-         if (w[3]>w[4] && w[3]>w[2] && w[3]>w[1] && w[3]>w[0]) {//kaon
-           kasel++;
-           kaG->Fill(p);
-            if (TMath::Abs(code)!=321) kaF->Fill(p);
-         }
+           if (TMath::Abs(code)==2212) prR->Fill(p);
+           if (w[4]>w[3] && w[4]>w[2] && w[4]>w[1] && w[4]>w[0]) {//proton
+             prsel++;
+             prG->Fill(p);
+              if (TMath::Abs(code)!=2212) prF->Fill(p);
+           }
 
-        if (TMath::Abs(code)==211) piR->Fill(p);
-        if (w[2]>w[3] && w[2]>w[4] && w[2]>w[1] && w[2]>w[0]) {//pion
-           pisel++;
-           piG->Fill(p);
-           if (TMath::Abs(code)!=211) piF->Fill(p);
-         }
+           if (TMath::Abs(code)==321) kaR->Fill(p);
+           if (w[3]>w[4] && w[3]>w[2] && w[3]>w[1] && w[3]>w[0]) {//kaon
+             kasel++;
+             kaG->Fill(p);
+              if (TMath::Abs(code)!=321) kaF->Fill(p);
+           }
 
-       }
+          if (TMath::Abs(code)==211) piR->Fill(p);
+          if (w[2]>w[3] && w[2]>w[4] && w[2]>w[1] && w[2]>w[0]) {//pion
+             pisel++;
+             piG->Fill(p);
+             if (TMath::Abs(code)!=211) piF->Fill(p);
+           }
+       }
+      }
+      delete event;
+      cout<<"Number of selected ESD tracks : "<<nsel<<endl;
+      cout<<"Number of selected pion ESD tracks : "<<pisel<<endl;
+      cout<<"Number of selected kaon ESD tracks : "<<kasel<<endl;
+      cout<<"Number of selected proton ESD tracks : "<<prsel<<endl;
 
-     } 
-     delete event;
-     cerr<<"Number of selected ESD tracks : "<<nsel<<endl;
-     cerr<<"Number of selected pion ESD tracks : "<<pisel<<endl;
-     cerr<<"Number of selected kaon ESD tracks : "<<kasel<<endl;
-     cerr<<"Number of selected proton ESD tracks : "<<prsel<<endl;
+      allnsel+=nsel; allpisel+=pisel; allkasel+=kasel; allprsel+=prsel;
 
-   }
+   } // ***** End of the loop over events
 
-   timer.Stop(); timer.Print();
+   ef->Close();
 
    TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
-   if (c1) delete c1; 
-   c1=new TCanvas("c1","",0,0,600,1200);
-   c1->Divide(1,4);
+   if (!c1) {
+      c1=new TCanvas("c1","",0,0,600,1200);
+      c1->Divide(1,4);
+   }
 
    c1->cd(1);
    tpcHist->Draw();
@@ -226,12 +237,37 @@ Int_t AliESDComparison(const Char_t *dir=".") {
    prGood->Draw("hist");
    prFake->Draw("same");
 
+   c1->Update();
+
+   cout<<endl;
+   cout<<endl;
+   e=(Int_t)piR->GetEntries();
+   Int_t o=(Int_t)piG->GetEntries();
+   if (e*o) cout<<"Efficiency (contamination) for pions : "<<
+      piGood->GetEntries()/e<<'('<<piF->GetEntries()/o<<')'<<endl; 
+   e=(Int_t)kaR->GetEntries();
+   o=(Int_t)kaG->GetEntries();
+   if (e*o) cout<<"Efficiency (contamination) for kaons : "<<
+      kaGood->GetEntries()/e<<'('<<kaF->GetEntries()/o<<')'<<endl; 
+   e=(Int_t)prR->GetEntries();
+   o=(Int_t)prG->GetEntries();
+   if (e*o) cout<<"Efficiency (contamination) for protons : "<<
+      prGood->GetEntries()/e<<'('<<prF->GetEntries()/o<<')'<<endl;
+   cout<<endl; 
+
+   cout<<"Total number of selected ESD tracks : "<<allnsel<<endl;
+   cout<<"Total number of selected pion ESD tracks : "<<allpisel<<endl;
+   cout<<"Total number of selected kaon ESD tracks : "<<allkasel<<endl;
+   cout<<"Total number of selected proton ESD tracks : "<<allprsel<<endl;
+
    ef->Close();
    TFile fc("AliESDComparison.root","RECREATE");
    c1->Write();
    fc.Close();
 
-   delete rl;
+   gBenchmark->Stop("AliESDComparison");
+   gBenchmark->Show("AliESDComparison");
 
-   return rc;
+   delete rl;
+   return 0;
 }
index 5246387..640e851 100644 (file)
  ****************************************************************************/
 
 #if !defined(__CINT__) || defined(__MAKECINT__)
+  #include <TMath.h>
+  #include <TError.h>
   #include <Riostream.h>
-  #include <fstream.h>
+  #include <TH1F.h>
+  #include <TTree.h>
+  #include <TParticle.h>
+  #include <TCanvas.h>
+  #include <TLine.h>
+  #include <TText.h>
+  #include <TBenchmark.h>
+  #include <TStyle.h>
+  #include <TKey.h>
+  #include <TROOT.h>
 
-  #include "AliRun.h"
-  #include "AliHeader.h"
   #include "AliStack.h"
+  #include "AliHeader.h"
+  #include "AliTrackReference.h"
   #include "AliRunLoader.h"
-  #include "AliLoader.h"
-
+  #include "AliRun.h"
   #include "AliESD.h"
+
   #include "AliTOFdigit.h"
+  #include "AliLoader.h"
 
-  #include "TKey.h"
-  #include "TFile.h"
-  #include "TTree.h"
-  #include "TH1.h"
   #include "TClonesArray.h"
-  #include "TStyle.h"
-  #include "TCanvas.h"
-  #include "TLine.h"
-  #include "TText.h"
-  #include "TParticle.h"
 #endif
 
-struct GoodTrackTOF {
-  Int_t lab;
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
+Int_t GoodTracksTOF(const Char_t *dir=".");
 
 extern AliRun *gAlice;
+extern TBenchmark *gBenchmark;
+extern TROOT *gROOT;
+
+static Int_t allgood=0;
+static Int_t allmatched=0;
+static Int_t allmismatched=0;
+
+Int_t AliTOFComparison(const Char_t *dir=".") {
+   gBenchmark->Start("AliTOFComparison");
+
+   ::Info("AliTOFComparison.C","Doing comparison...");
+   
+
+   Double_t pmin=0.2;
+   Double_t pmax=3.0;
+
+   TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");    
+   if (!hgood) hgood=new TH1F("hgood","Good tracks",30,pmin,pmax);
+    
+   TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
+   if (!hfound) hfound=new TH1F("hfound","Matched tracks",30,pmin,pmax);
+
+   TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
+   if (!hfake) hfake=new TH1F("hfake","Mismatched tracks",30,pmin,pmax);
+
+   TH1F *hgp=(TH1F*)gROOT->FindObject("hgp");
+   if (!hgp) hgp=new TH1F("hgp","",30,pmin,pmax);
+   hgp->SetLineColor(4); hgp->SetLineWidth(2);
+
+   TH1F *hfp=(TH1F*)gROOT->FindObject("hfp");
+   if (!hfp) hfp=new TH1F("hfp","Probability of mismatching",30,pmin,pmax);
+   hfp->SetFillColor(1); hfp->SetFillStyle(3013); hfp->SetLineWidth(2);
+
+   TH1F *hgoo=(TH1F*)gROOT->FindObject("hgoo");    
+   if (!hgoo) hgoo=new TH1F("hgoo","Good tracks",30,-1,1);
+    
+   TH1F *hfoun=(TH1F*)gROOT->FindObject("hfoun");
+   if (!hfoun) hfoun=new TH1F("hfoun","Matched tracks",30,-1,1);
+
+   TH1F *hfak=(TH1F*)gROOT->FindObject("hfak");
+   if (!hfak) hfak=new TH1F("hfak","Mismatched tracks",30,-1,1);
+
+   TH1F *hgl=(TH1F*)gROOT->FindObject("hgl");
+   if (!hgl) hgl=new TH1F("hgl","",30,-1,1);
+   hgl->SetLineColor(4); hgl->SetLineWidth(2);
+
+   TH1F *hfl=(TH1F*)gROOT->FindObject("hfl");
+   if (!hfl) hfl=new TH1F("hfl","Probability of mismatching",30,-1,1);
+   hfl->SetFillColor(1); hfl->SetFillStyle(3013); hfl->SetLineWidth(2);
+
 
-Int_t AliTOFComparison() {
-   Int_t good_tracks_tof(GoodTrackTOF *gt, const Int_t max);
 
-   cerr<<"Doing comparison...\n";
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodTracksTOF.root",dir);
+
+   TFile *refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+   ::Info("AliTOFComparison.C","Marking good tracks (will take a while)...");
+     if (GoodTracksTOF(dir)) {
+        ::Error("AliTOFComparison.C","Can't generate the reference file !");
+        return 1;
+     }
+   }
+   refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Error("AliTOFComparison.C","Can't open the reference file !");
+     return 1;
+   }   
+
+   TTree *tofTree=(TTree*)refFile->Get("tofTree");
+   if (!tofTree) {
+     ::Error("AliTOFComparison.C","Can't get the reference tree !");
+     return 2;
+   }
+   TBranch *branch=tofTree->GetBranch("TOF");
+   if (!branch) {
+     ::Error("AliTOFComparison.C","Can't get the TOF branch !");
+     return 3;
+   }
+   TClonesArray dummy("AliTrackReference",1000), *refs=&dummy;
+   branch->SetAddress(&refs);
+
 
    if (gAlice) { 
      delete gAlice->GetRunLoader();
      delete gAlice;//if everything was OK here it is already NULL
      gAlice = 0x0;
    }
-
-   TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;   
-
-   {
-   AliRunLoader *rl = AliRunLoader::Open("galice.root");
+   sprintf(fname,"%s/galice.root",dir);
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
    if (rl == 0x0) {
       cerr<<"Can not open session"<<endl;
       return 1;
@@ -67,103 +139,65 @@ Int_t AliTOFComparison() {
    }
    tofl->LoadDigits("read");
 
-   rl->GetEvent(0);
 
-   TTree *tofTree=tofl->TreeD();
-   if (!tofTree) {
-      cerr<<"Can't get the TOF cluster tree !\n";
-      return 3;
-   } 
-   TBranch *branch=tofTree->GetBranch("TOF");
-   if (!branch) { 
-      cerr<<"Can't get the branch with the TOF digits !\n";
+   sprintf(fname,"%s/AliESDs.root",dir);
+   TFile *ef=TFile::Open(fname);
+   if ((!ef)||(!ef->IsOpen())) {
+      ::Error("AliTOFComparison.C","Can't open AliESDs.root !");
+      delete rl;
       return 4;
    }
-   branch->SetAddress(&digits);
-
-   tofTree->GetEvent(0);
-
-   delete rl;
-   }
-
-   Int_t nd=digits->GetEntriesFast();
-   cerr<<"Number of digits: "<<nd<<endl;
-
-   const Int_t MAX=15000;
-   GoodTrackTOF gt[MAX];
-   Int_t ngood=0;
-   ifstream in("good_tracks_tof");
-   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==MAX) {
-            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_tof(gt,MAX);
-      ofstream out("good_tracks_tof");
-      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_tof) !\n";
-      out.close();
-   }
+   TKey *key=0;
+   TIter next(ef->GetListOfKeys());
 
-   Double_t pmin=0.2;
-   Double_t pmax=4.0;
 
-   TH1F *hgood=new TH1F("hgood","Good tracks",30,pmin,pmax);    
-   TH1F *hfound=new TH1F("hfound","Matched tracks",30,pmin,pmax);
-   TH1F *hfake=new TH1F("hfake","Mismatched tracks",30,pmin,pmax);
-   TH1F *hgp=new TH1F("hgp","",30,pmin,pmax); //efficiency for good tracks
-   hgp->SetLineColor(4); hgp->SetLineWidth(2);
-   TH1F *hfp=new TH1F("hfp","Probability of mismatching",30,pmin,pmax);
-   hfp->SetFillColor(1); hfp->SetFillStyle(3013); hfp->SetLineWidth(2);
 
-   TH1F *hgoo=new TH1F("hgoo","Good tracks",30,-1,1);    
-   TH1F *hfoun=new TH1F("hfoun","Matched tracks",30,-1,1);
-   TH1F *hfak=new TH1F("hfak","Mismatched tracks",30,-1,1);
-   TH1F *hgl=new TH1F("hgl","",30,-1,1); //efficiency for good tracks
-   hgl->SetLineColor(4); hgl->SetLineWidth(2);
-   TH1F *hfl=new TH1F("hfl","Probability of mismatching",30,-1,1);
-   hfl->SetFillColor(1); hfl->SetFillStyle(3013); hfl->SetLineWidth(2);
+   //******* Loop over events *********
+   Int_t e=0;
+   while ((key=(TKey*)next())!=0) {
+     cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
+
+     rl->GetEvent(e); ef->cd();
+
+     TTree *digTree=tofl->TreeD();
+     if (!digTree) {
+        cerr<<"Can't get the TOF cluster tree !\n";
+        return 3;
+     } 
+     TBranch *branch=digTree->GetBranch("TOF");
+     if (!branch) { 
+        cerr<<"Can't get the branch with the TOF digits !\n";
+        return 4;
+     }
+     TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;   
+     branch->SetAddress(&digits);
 
-   TCanvas *c1=new TCanvas("c1","",0,0,600,900);
-   c1->Divide(1,2);
+     digTree->GetEvent(0);
+     Int_t nd=digits->GetEntriesFast();
+     cerr<<"Number of the TOF digits: "<<nd<<endl;
 
-   TFile *ef=TFile::Open("AliESDs.root");
-   if (!ef->IsOpen()) {cerr<<"Can't AliESDs.root !\n"; return 1;}
 
-   TIter next(ef->GetListOfKeys());
-   TKey *key=0;
-   Int_t nev=0;
-   while ((key=(TKey*)next())!=0) {
-     cerr<<"Processing event number : "<<nev++<<endl;
 
      AliESD *event=(AliESD*)key->ReadObj();
      Int_t ntrk=event->GetNumberOfTracks();
      cerr<<"Number of ESD tracks : "<<ntrk<<endl; 
 
+
+     if (tofTree->GetEvent(e++)==0) {
+        cerr<<"No reconstructable tracks !\n";
+        continue;
+     }
+
+     Int_t ngood=refs->GetEntriesFast(); 
+
      Int_t matched=0;
      Int_t mismatched=0;
      for (Int_t i=0; i<ngood; i++) {
-         Int_t lab=gt[i].lab;
-         Double_t pxg=gt[i].px, pyg=gt[i].py, pzg=gt[i].pz;
-         Double_t ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
-
-        if (ptg<0.1) continue;
+       AliTrackReference *ref=(AliTrackReference*)refs->UncheckedAt(i); 
+        Int_t lab=ref->Label();
+        Float_t ptg=TMath::Sqrt(ref->Px()*ref->Px() + ref->Py()*ref->Py());
 
-         Double_t tgl=pzg/ptg; //tan(lambda)
+        Double_t tgl=ref->Pz()/ptg; //tan(lambda)
 
          if (ptg>pmin) { hgood->Fill(ptg); hgoo->Fill(tgl); }
 
@@ -204,147 +238,220 @@ Int_t AliTOFComparison() {
             cerr<<endl;
          }
      }
+     cout<<"Number of good tracks: "<<ngood<<endl;
+     cout<<"Number of matched tracks: "<<matched<<endl;
+     cout<<"Number of mismatched tracks: "<<mismatched<<endl;
 
-     cerr<<"Number of good tracks: "<<ngood<<endl;
-     cerr<<"Number of matched tracks: "<<matched<<endl;
-     cerr<<"Number of mismatched tracks: "<<mismatched<<endl;
-     if (ngood!=0) cerr<<"Efficiency: "<<Float_t(matched)/ngood<<endl;
-
-     hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
-     hgp->Divide(hfound,hgood,1,1.,"b");
-     hfp->Divide(hfake,hgood,1,1.,"b");
-     hgp->SetMaximum(1.4);
-     hgp->SetYTitle("Matching efficiency");
-     hgp->SetXTitle("Pt (GeV/c)");
-
-     hfoun->Sumw2(); hgoo->Sumw2(); hfak->Sumw2();
-     hgl->Divide(hfoun,hgoo,1,1.,"b");
-     hfl->Divide(hfak,hgoo,1,1.,"b");
-     hgl->SetMaximum(1.4);
-     hgl->SetYTitle("Matching efficiency");
-     hgl->SetXTitle("Tan(lambda)");
-
-     c1->cd(1);
-
-     hgp->Draw();
-     hfp->Draw("histsame");
-     TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
-     line1->Draw("same");
-     TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
-     line2->Draw("same");
-
-     c1->cd(2);
-
-     hgl->Draw();
-     hfl->Draw("histsame");
-     TLine *line3 = new TLine(-1,1.0,1,1.0); line3->SetLineStyle(4);
-     line3->Draw("same");
-     TLine *line4 = new TLine(-1,0.9,1,0.9); line4->SetLineStyle(4);
-     line4->Draw("same");
-
-     break;
-   }
+     allgood+=ngood; allmatched+=matched; allmismatched+=mismatched;
+
+     refs->Clear();
+     delete event;
+   } //***** End of the loop over events
 
+   ef->Close();
+   
+   delete tofTree;
+   refFile->Close();
+
+   if (allgood!=0) cerr<<"\n\nEfficiency: "<<Float_t(allmatched)/allgood<<endl;
+   cout<<"Total number of good tracks: "<<allgood<<endl;
+   cout<<"Total number of matched tracks: "<<allmatched<<endl;
+   cout<<"Total number of mismatched tracks: "<<allmismatched<<endl;
+
+   TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
+   if (!c1) {
+      c1=new TCanvas("c1","",0,0,600,900);
+      c1->Divide(1,2);
+   }
+   hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
+   hgp->Divide(hfound,hgood,1,1.,"b");
+   hfp->Divide(hfake,hgood,1,1.,"b");
+   hgp->SetMaximum(1.4);
+   hgp->SetYTitle("Matching efficiency");
+   hgp->SetXTitle("Pt (GeV/c)");
+
+   hfoun->Sumw2(); hgoo->Sumw2(); hfak->Sumw2();
+   hgl->Divide(hfoun,hgoo,1,1.,"b");
+   hfl->Divide(hfak,hgoo,1,1.,"b");
+   hgl->SetMaximum(1.4);
+   hgl->SetYTitle("Matching efficiency");
+   hgl->SetXTitle("Tan(lambda)");
+
+   c1->cd(1);
+
+   hgp->Draw();
+   hfp->Draw("histsame");
+   TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
+   line1->Draw("same");
+   TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
+   line2->Draw("same");
+
+   c1->cd(2);
+
+   hgl->Draw();
+   hfl->Draw("histsame");
+   TLine *line3 = new TLine(-1,1.0,1,1.0); line3->SetLineStyle(4);
+   line3->Draw("same");
+   TLine *line4 = new TLine(-1,0.9,1,0.9); line4->SetLineStyle(4);
+   line4->Draw("same");
+
+   c1->Update();
+   
    TFile fc("AliTOFComparison.root","RECREATE");
    c1->Write();
    fc.Close();
 
+   gBenchmark->Stop("AliTOFComparison");
+   gBenchmark->Show("AliTOFComparison");
+
+   delete rl;
    return 0;
 }
 
-Int_t good_tracks_tof(GoodTrackTOF *gt, const Int_t max) {
-   ifstream in("good_tracks_its");
-   if (!in) {
-     cerr<<"Can't get good_tracks_its !\n"; exit(11);
-   }   
-
-   AliRunLoader *rl = AliRunLoader::Open("galice.root","COMPARISON");
-   if (!rl) {
-       cerr<<"Can't start session !\n";
-       exit(1);
+Int_t GoodTracksTOF(const Char_t *dir) {
+   if (gAlice) { 
+       delete gAlice->GetRunLoader();
+       delete gAlice;//if everything was OK here it is already NULL
+       gAlice = 0x0;
    }
 
-   rl->GetEvent(0);
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
 
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
+   if (!rl) {
+      ::Error("GoodTracksTOF","Can't start session !");
+      return 1;
+   }
 
    rl->LoadgAlice();
    rl->LoadHeader();
-   Int_t np = rl->GetHeader()->GetNtrack();
+   rl->LoadKinematics();
 
-   Int_t *good=new Int_t[np];
-   Int_t k;
-   for (k=0; k<np; k++) good[k]=0;
 
    AliLoader* tofl = rl->GetLoader("TOFLoader");
    if (tofl == 0x0) {
-      cerr<<"Can not get the TOF loader"<<endl;
-      exit(2);
+      ::Error("GoodTracksTOF","Can not get the TOF loader !");
+      delete rl;
+      return 2;
    }
    tofl->LoadDigits("read");
 
-   TTree *dTree=tofl->TreeD();
-   if (!dTree) {
-      cerr<<"Can't get the TOF cluster tree !\n";
-      exit(3);
-   } 
+   Int_t nev=rl->GetNumberOfEvents();
+   ::Info("GoodTracksTOF","Number of events : %d\n",nev);  
 
-   TBranch *branch=dTree->GetBranch("TOF");
-   if (!branch) { 
-     cerr<<"Can't get the branch with the TOF digits !\n";
-     exit(4);
+   sprintf(fname,"%s/GoodTracksITS.root",dir);
+   TFile *itsFile=TFile::Open(fname);
+   if ((!itsFile)||(!itsFile->IsOpen())) {
+       ::Error("GoodTracksTOF","Can't open the GoodTracksITS.root !");
+       delete rl;
+       return 5; 
    }
-   TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;
-   branch->SetAddress(&digits);
-   
-  dTree->GetEvent(0);
-  Int_t nd=digits->GetEntriesFast();
-  cerr<<"Number of digits: "<<nd<<endl;
-
-  for (Int_t i=0; i<nd; i++) {
-    AliTOFdigit *d=(AliTOFdigit*)digits->UncheckedAt(i);
-    Int_t l0=d->GetTrack(0);
-       if (l0>=np) {cerr<<"Wrong label: "<<l0<<endl; continue;}
-    Int_t l1=d->GetTrack(1);
-       if (l1>=np) {cerr<<"Wrong label: "<<l1<<endl; continue;}
-    Int_t l2=d->GetTrack(2);
-       if (l2>=np) {cerr<<"Wrong label: "<<l2<<endl; continue;}
-    if (l0>=0) good[l0]++; 
-    if (l1>=0) good[l1]++; 
-    if (l2>=0) good[l2]++;
-  }
+   TClonesArray dum("AliTrackReference",1000), *itsRefs=&dum;
+   TTree *itsTree=(TTree*)itsFile->Get("itsTree");
+   if (!itsTree) {
+       ::Error("GoodTracksTOF","Can't get the ITS reference tree !");
+       delete rl;
+       return 6;
+   }
+   TBranch *itsBranch=itsTree->GetBranch("ITS");
+   if (!itsBranch) {
+      ::Error("GoodTracksTOF","Can't get the ITS reference branch !");
+      delete rl;
+      return 7;
+   }
+   itsBranch->SetAddress(&itsRefs);
+
+
+   sprintf(fname,"%s/GoodTracksTOF.root",dir);
+   TFile *tofFile=TFile::Open(fname,"recreate");
+   TClonesArray dummy("AliTrackReference",1000), *tofRefs=&dummy;
+   TTree tofTree("tofTree","Tree with info about the reconstructable TOF tracks");
+   tofTree.Branch("TOF",&tofRefs);
+
+
+   //********  Loop over generated events 
+   for (Int_t e=0; e<nev; e++) {
 
+     rl->GetEvent(e); tofFile->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]; Int_t k; for (k=0; k<np; k++) good[k]=0;
+
+     TTree *dTree=tofl->TreeD();
+     if (!dTree) {
+        ::Error("GoodTracksTOF","Can't get the TOF cluster tree !");
+        delete rl;
+        return 8;
+     } 
+     TBranch *branch=dTree->GetBranch("TOF");
+     if (!branch) { 
+        ::Error("GoodTracksTOF","Can't get the branch with the TOF digits !");
+        return 9;
+     }
+     TClonesArray dummy("AliTOFdigit",10000), *digits=&dummy;
+     branch->SetAddress(&digits);
    
-  rl->LoadKinematics();
-  AliStack* stack = rl->Stack();
-  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] == 0) continue;
-      TParticle *p = (TParticle*)stack->Particle(lab);
-      if (p == 0x0) {
-         cerr<<"Can not get particle "<<lab<<endl;
-         exit(1);
-      }
-      if (TMath::Abs(p->Vx())>0.1) continue;
-      if (TMath::Abs(p->Vy())>0.1) continue;
-      //      if (TMath::Abs(p->Vz())>0.1) continue;
-
-      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=p->Vx(); gt[nt].y=p->Vy(); gt[nt].z=p->Vz();
-      nt++;
-      if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
-  }
-
-  delete[] good;
-
-  rl->UnloadKinematics();
-  rl->UnloadHeader();
-  rl->UnloadgAlice();
-  delete rl;
-
-  return nt;
+     dTree->GetEvent(0);
+     Int_t nd=digits->GetEntriesFast();
+
+     for (Int_t i=0; i<nd; i++) {
+       AliTOFdigit *d=(AliTOFdigit*)digits->UncheckedAt(i);
+       Int_t l0=d->GetTrack(0);
+          if (l0>=np) {cerr<<"Wrong label: "<<l0<<endl; continue;}
+       Int_t l1=d->GetTrack(1);
+          if (l1>=np) {cerr<<"Wrong label: "<<l1<<endl; continue;}
+       Int_t l2=d->GetTrack(2);
+          if (l2>=np) {cerr<<"Wrong label: "<<l2<<endl; continue;}
+       if (l0>=0) good[l0]++; 
+       if (l1>=0) good[l1]++; 
+       if (l2>=0) good[l2]++;
+     }
+     digits->Clear();
+
+
+     //****** select tracks which are "good" enough
+     AliStack* stack = rl->Stack();
+
+     itsTree->GetEvent(e);
+     Int_t nk=itsRefs->GetEntriesFast();
+
+     Int_t nt=0;
+     for (k=0; k<nk; k++) {
+        AliTrackReference *itsRef=(AliTrackReference *)itsRefs->UncheckedAt(k);
+        Int_t lab=itsRef->Label();
+        if (good[lab] == 0) continue;
+        TParticle *p = (TParticle*)stack->Particle(lab);
+        if (p == 0x0) {
+           cerr<<"Can not get particle "<<lab<<endl;
+           continue;
+        }
+
+        if (TMath::Abs(p->Vx())>0.1) continue;
+        if (TMath::Abs(p->Vy())>0.1) continue;
+        //if (TMath::Abs(p->Vz())>0.1) continue;
+
+        new((*tofRefs)[nt]) AliTrackReference(*itsRef);
+        nt++;
+     }
+     itsRefs->Clear();
+
+     tofTree.Fill();
+     tofRefs->Clear();
+
+     delete[] good;
+
+   } //*** end of the loop over generated events
+
+   tofTree.Write();
+   tofFile->Close();
+
+   delete itsTree;
+   itsFile->Close();
+
+   delete rl;
+   return 0;
 }
index c5552b1..41c708c 100644 (file)
@@ -17,7 +17,6 @@
   #include <TH2F.h>
   #include <TTree.h>
   #include <TParticle.h>
-  #include <TPad.h>
   #include <TCanvas.h>
   #include <TLine.h>
   #include <TText.h>
@@ -30,7 +29,6 @@
   #include "AliHeader.h"
   #include "AliTrackReference.h"
   #include "AliRunLoader.h"
-  #include "AliMagF.h"
   #include "AliRun.h"
   #include "AliESD.h"
 
   #include "AliTPCLoader.h"
 #endif
 
-struct GoodTrackTPC {
-  Int_t lab;
-  Int_t code;
-  Float_t px,py,pz;
-  Float_t x,y,z;
-};
-
-Int_t good_tracks_tpc(GoodTrackTPC *gt, const Int_t max, 
-const char* evfoldname=AliConfig::fgkDefaultEventFolderName);
+Int_t GoodTracksTPC(const Char_t *dir=".");
 
 extern AliRun *gAlice;
 extern TBenchmark *gBenchmark;
 extern TROOT *gROOT;
 
-Int_t AliTPCComparison(Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir=".") {
+static Int_t allgood=0;
+static Int_t allselected=0;
+static Int_t allfound=0;
+
+Int_t AliTPCComparison
+(Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir=".") {
    gBenchmark->Start("AliTPCComparison");
 
    ::Info("AliTPCComparison.C","Doing comparison...");
 
-   if (gAlice) { 
-       delete gAlice->GetRunLoader();
-       delete gAlice;//if everything was OK here it is already NULL
-       gAlice = 0x0;
-    }
 
-   Char_t fname[100];
-   sprintf(fname,"%s/galice.root",dir);
-
-   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
-   if (!rl) {
-       ::Fatal("AliTPCComparison.C","Can't start session !");
-   }
-
-   /* Generate a list of "good" tracks */
-   const Int_t MAX=20000;
-   GoodTrackTPC gt[MAX];
-   Int_t ngood=0;
-   sprintf(fname,"%s/good_tracks_tpc",dir);
-   ifstream in(fname);
-   if (in) {
-      ::Info("AliTPCComparison.C","Reading good tracks...");
-      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++;
-         if (ngood==MAX) {
-           ::Warning("AliTPCComparison.C","Too many good tracks !");
-           break;
-         }     
-      }
-      if (!in.eof()) 
-        ::Fatal("AliTPCComparison.C","Read error (good_tracks_tpc) !");
-   } else {
-      ::Info
-      ("AliTPCComparison","Marking good tracks (this will take a while)...");
-      ngood=good_tracks_tpc(gt,MAX,"COMPARISON");
-      ofstream out(fname);
-      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 
-         ::Fatal("AliTPCComparison.C","Can not open file (good_tracks_tpc) !");
-      out.close();
-   }
-
-   AliESD *event=0;
-   { /*Load tracks*/
-   sprintf(fname,"%s/AliESDs.root",dir);
-   TFile *ef=TFile::Open(fname);
-   if ((!ef)||(!ef->IsOpen())) {
-      sprintf(fname,"%s/AliESDtpc.root",dir);
-      ef=TFile::Open(fname);
-      if ((!ef)||(!ef->IsOpen()))
-         ::Fatal("AliTPCComparison.C","Can't open AliESDtpc.root !");
-   }
-   TKey *key=0;
-   TIter next(ef->GetListOfKeys());
-   if ((key=(TKey*)next())!=0) event=(AliESD*)key->ReadObj();
-   ef->Close();
-   }
-   Int_t nentr=event->GetNumberOfTracks();
 
    TH1F *hp=(TH1F*)gROOT->FindObject("hp");
    if (!hp) hp=new TH1F("hp","PHI resolution",50,-20.,20.); 
@@ -172,106 +104,187 @@ Int_t AliTPCComparison(Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir
    hep->SetMarkerStyle(8);
    hep->SetMarkerSize(0.4);
 
-   //MI change
-   Int_t mingood=ngood;
-   Int_t track_notfound[MAX], itrack_notfound=0;
-   Int_t track_fake[MAX], itrack_fake=0;
-   Int_t track_multifound[MAX], track_multifound_n[MAX], itrack_multifound=0;
-
-   Int_t i;
-   while (ngood--) {
-      Int_t lab=gt[ngood].lab,tlab=-1;
-      Float_t ptg=
-      TMath::Sqrt(gt[ngood].px*gt[ngood].px + gt[ngood].py*gt[ngood].py);
-
-      if (ptg<1e-33) continue; // for those not crossing 0 pad row
-      if (ptg<ptcutl) continue;
-      if (ptg>ptcuth) continue;
-      hgood->Fill(ptg);
-
-      AliESDtrack *track=0;
-      for (i=0; i<nentr; i++) {
+
+   Char_t fname[100];
+   sprintf(fname,"%s/GoodTracksTPC.root",dir);
+
+   TFile *refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Info("AliTPCComparison.C","Marking good tracks (will take a while)...");
+     if (GoodTracksTPC(dir)) {
+        ::Error("AliTPCComparison.C","Can't generate the reference file !");
+        return 1;
+     }
+   }
+   refFile=TFile::Open(fname,"old");
+   if (!refFile || !refFile->IsOpen()) {
+     ::Error("AliTPCComparison.C","Can't open the reference file !");
+     return 2;
+   }   
+  
+   TTree *tpcTree=(TTree*)refFile->Get("tpcTree");
+   if (!tpcTree) {
+     ::Error("AliTPCComparison.C","Can't get the reference tree !");
+     return 3;
+   }
+   TBranch *branch=tpcTree->GetBranch("TPC");
+   if (!branch) {
+     ::Error("AliTPCComparison.C","Can't get the TPC branch !");
+     return 4;
+   }
+   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/AliESDtpc.root",dir);
+      ef=TFile::Open(fname);
+      if ((!ef)||(!ef->IsOpen())) {
+         ::Error("AliTPCComparison.C","Can't open AliESDtpc.root !");
+         return 5;
+      }
+   }
+   TKey *key=0;
+   TIter next(ef->GetListOfKeys());
+
+
+   //******* Loop over events *********
+   Int_t e=0;
+   while ((key=(TKey*)next())!=0) {
+      cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
+
+      AliESD *event=(AliESD*)key->ReadObj();
+
+      Int_t nentr=event->GetNumberOfTracks();
+      allfound+=nentr;
+
+      if (tpcTree->GetEvent(e++)==0) {
+        cerr<<"No reconstructable tracks !\n";
+         continue;
+      }
+
+      Int_t ngood=refs->GetEntriesFast(); 
+      allgood+=ngood;
+
+      const Int_t MAX=15000;
+    //MI change
+      Int_t track_notfound[MAX], itrack_notfound=0;
+      Int_t track_fake[MAX], itrack_fake=0;
+      Int_t track_multifound[MAX],track_multifound_n[MAX],itrack_multifound=0;
+
+      Int_t i;
+      for (Int_t 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<1e-33) continue; // for those not crossing 0 pad row
+  
+        if (ptg<ptcutl) continue;
+        if (ptg>ptcuth) continue;
+
+        allselected++;
+
+        hgood->Fill(ptg);
+
+        AliESDtrack *track=0;
+        for (i=0; i<nentr; i++) {
           track=event->GetTrack(i);
           tlab=track->GetTPCLabel();
           if (lab==TMath::Abs(tlab)) break;
-      }
-      if (i==nentr) {
-       track_notfound[itrack_notfound++]=lab;
-        continue;
-      }
+        }
+        if (i==nentr) {
+          track_notfound[itrack_notfound++]=lab;
+           continue;
+        }
+      
+        //MI change  - addition
+        Int_t micount=0;
+        Int_t mi;
+        AliESDtrack * mitrack;
+        for (mi=0; mi<nentr; mi++) {
+         mitrack=event->GetTrack(mi);          
+         if (lab==TMath::Abs(mitrack->GetTPCLabel())) micount++;
+        }
+        if (micount>1) {
+         track_multifound[itrack_multifound]=lab;
+         track_multifound_n[itrack_multifound]=micount;
+         itrack_multifound++;
+        }
+        if ((track->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
+        if (lab==tlab) hfound->Fill(ptg);
+        else { 
+         track_fake[itrack_fake++]=lab;
+         hfake->Fill(ptg); 
+        }
       
-      //MI change  - addition
-      Int_t micount=0;
-      Int_t mi;
-      AliESDtrack * mitrack;
-      for (mi=0; mi<nentr; mi++) {
-       mitrack=event->GetTrack(mi);          
-       if (lab==TMath::Abs(mitrack->GetTPCLabel())) micount++;
+        Double_t pxpypz[3]; track->GetInnerPxPyPz(pxpypz);
+        Float_t phi=TMath::ATan2(pxpypz[1],pxpypz[0]);
+        if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
+        if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
+        Double_t pt=TMath::Sqrt(pxpypz[0]*pxpypz[0]+pxpypz[1]*pxpypz[1]);
+        Float_t lam=TMath::ATan2(pxpypz[2],pt); 
+        Float_t pt_1=1/pt;
+
+        Int_t pdg=(Int_t)ref->GetLength();  //this is particle's PDG !
+
+        if (TMath::Abs(pdg)==11 && ptg>4.) {//high momentum electrons
+           hmpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+        } else {
+           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.);
+
+           hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+        }
+
+        Float_t mom=pt/TMath::Cos(lam);
+        Float_t dedx=track->GetTPCsignal();
+        hep->Fill(mom,dedx,1.);
+        if (TMath::Abs(pdg)==211) //pions
+          if (mom>0.4 && mom<0.5) {
+              he->Fill(dedx,1.);
+           }
       }
-      if (micount>1) {
-       track_multifound[itrack_multifound]=lab;
-       track_multifound_n[itrack_multifound]=micount;
-       itrack_multifound++;
+
+      cout<<"\nList of Not found tracks :\n";
+      for ( i = 0; i< itrack_notfound; i++){
+        cout<<track_notfound[i]<<"\t";
+        if ((i%5)==4) cout<<"\n";
       }
-      if ((track->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
-      if (lab==tlab) hfound->Fill(ptg);
-      else { 
-       track_fake[itrack_fake++]=lab;
-       hfake->Fill(ptg); 
+      cout<<"\nList of fake  tracks :\n";
+      for ( i = 0; i< itrack_fake; i++){
+        cout<<track_fake[i]<<"\t";
+        if ((i%5)==4) cout<<"\n";
       }
-      
-      Double_t pxpypz[3]; track->GetInnerPxPyPz(pxpypz);
-      Float_t phi=TMath::ATan2(pxpypz[1],pxpypz[0]);
-      if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
-      if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
-      Double_t pt=TMath::Sqrt(pxpypz[0]*pxpypz[0]+pxpypz[1]*pxpypz[1]);
-      Float_t lam=TMath::ATan2(pxpypz[2],pt); 
-      Float_t pt_1=1/pt;
-
-      if (TMath::Abs(gt[ngood].code)==11 && ptg>4.) {
-         hmpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
-      } else {
-         Float_t phig=TMath::ATan2(gt[ngood].py,gt[ngood].px);
-         hp->Fill((phi - phig)*1000.);
-
-         Float_t lamg=TMath::ATan2(gt[ngood].pz,ptg);
-         hl->Fill((lam - lamg)*1000.);
-
-         hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
+      cout<<"\nList of multiple found tracks :\n";
+      for ( i=0; i<itrack_multifound; i++) {
+          cout<<"id.   "<<track_multifound[i]
+              <<"     found - "<<track_multifound_n[i]<<"times\n";
       }
 
-      Float_t mom=pt/TMath::Cos(lam);
-      Float_t dedx=track->GetTPCsignal();
-      hep->Fill(mom,dedx,1.);
-      if (TMath::Abs(gt[ngood].code)==211)
-        if (mom>0.4 && mom<0.5) {
-            he->Fill(dedx,1.);
-         }
+      cout<<"Number of found tracks ="<<nentr<<endl;
+      cout<<"Number of \"good\" tracks ="<<ngood<<endl;
 
-   }
+      refs->Clear();
+      delete event;
+  }// ***** End of the loop over events
 
-   cout<<"\nList of Not found tracks :\n";
-   for ( i = 0; i< itrack_notfound; i++){
-     cout<<track_notfound[i]<<"\t";
-     if ((i%5)==4) cout<<"\n";
-   }
-   cout<<"\nList of fake  tracks :\n";
-   for ( i = 0; i< itrack_fake; i++){
-     cout<<track_fake[i]<<"\t";
-     if ((i%5)==4) cout<<"\n";
-   }
-    cout<<"\nList of multiple found tracks :\n";
-   for ( i=0; i<itrack_multifound; i++) {
-       cout<<"id.   "<<track_multifound[i]
-           <<"     found - "<<track_multifound_n[i]<<"times\n";
-   }
+   ef->Close();
 
-   Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
-   if (ng!=0) cerr<<"\n\nIntegral efficiency is about "<<nf/ng*100.<<" %\n";
-   cout<<"Total number of found tracks ="<<nentr<<endl;
-   cout<<"Total number of \"good\" tracks ="
-       <<mingood<<"   (selected for comparison: "<<ng<<')'<<endl<<endl;
+   delete tpcTree;
+   refFile->Close();
 
+   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);
@@ -342,73 +355,106 @@ Int_t AliTPCComparison(Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir
    hep->SetXTitle("p (Gev/c)"); hep->SetYTitle("dE/dX (Arb. Units)"); 
    hep->Draw(); c1->cd();
 
-   gBenchmark->Stop("AliTPCComparison");
-   gBenchmark->Show("AliTPCComparison");
-
    TFile fc("AliTPCComparison.root","RECREATE");
    c1->Write();
    c2->Write();
    fc.Close();
 
-   delete rl;
+   gBenchmark->Stop("AliTPCComparison");
+   gBenchmark->Show("AliTPCComparison");
+
    return 0;
 }
 
 
-Int_t 
-good_tracks_tpc(GoodTrackTPC *gt, const Int_t max, const char* evfoldname) {
-   Int_t nt=0;
+Int_t GoodTracksTPC(const Char_t *dir) {
+   if (gAlice) { 
+       delete gAlice->GetRunLoader();
+       delete gAlice;//if everything was OK here it is already NULL
+       gAlice = 0x0;
+   }
+
+   Char_t fname[100];
+   sprintf(fname,"%s/galice.root",dir);
 
-   AliRunLoader *rl = AliRunLoader::GetRunLoader(evfoldname);
-   if (rl == 0x0) {
-      ::Fatal("AliTPCComparison.C::good_tracks_tpc",
-              "Can not find Run Loader in Folder Named %s",
-              evfoldname);
+   AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
+   if (!rl) {
+      ::Error("GoodTracksTPC","Can't start session !");
+      return 1;
    }
+
+   rl->LoadgAlice();
+   rl->LoadHeader();
+   rl->LoadKinematics();
+   rl->LoadTrackRefs();
+
    AliTPCLoader *tpcl = (AliTPCLoader *)rl->GetLoader("TPCLoader");
    if (tpcl == 0x0) {
-       ::Fatal("AliTPCHits2Digits.C","Can not find TPCLoader !");
+      ::Error("GoodTracksTPC","Can not find TPCLoader !");
+      delete rl;
+      return 2;
    }
-   
-   rl->LoadgAlice();
-   
+      
    AliTPC *TPC=(AliTPC*)rl->GetAliRun()->GetDetector("TPC");
    Int_t ver = TPC->IsVersion(); 
-   cerr<<"TPC version "<<ver<<" has been found !\n";
+   cout<<"TPC version "<<ver<<" has been found !\n";
+   if (ver==1) tpcl->LoadRecPoints();
+   else if (ver==2) tpcl->LoadDigits();
+   else {
+      ::Error("GoodTracksTPC","Wrong TPC version !");
+      delete rl;
+      return 3;
+   } 
 
    rl->CdGAFile();
    AliTPCParamSR *digp=(AliTPCParamSR*)gDirectory->Get("75x40_100x60_150x60");
    if (!digp) { 
-     ::Fatal("AliTPCHits2Digits.C","TPC parameters have not been found !"); 
+     ::Error("AliTPCHits2Digits.C","TPC parameters have not been found !");
+     delete rl;
+     return 4; 
    }
    TPC->SetParam(digp);
 
-   rl->LoadHeader();
-
-   Int_t np = rl->GetHeader()->GetNtrack();
-      
-
    Int_t nrow_up=digp->GetNRowUp();
    Int_t nrows=digp->GetNRowLow()+nrow_up;
    Int_t zero=digp->GetZeroSup();
    Int_t gap=Int_t(0.125*nrows), shift=Int_t(0.5*gap);
    Int_t good_number=Int_t(0.4*nrows);
 
-   Int_t *good=new Int_t[np];
-   Int_t i;
-   for (i=0; i<np; i++) good[i]=0;
-
-
-   switch (ver) {
-   case 1:
-     {
-      tpcl->LoadRecPoints();      
-      AliTPCClustersArray *pca=new AliTPCClustersArray, &ca=*pca;
-      ca.Setup(digp);
-      ca.SetClusterType("AliTPCcluster");
-      ca.ConnectTree(tpcl->TreeR());
-      Int_t nrows=Int_t(ca.GetTree()->GetEntries());
-      for (Int_t n=0; n<nrows; n++) {
+   Int_t nev=rl->GetNumberOfEvents();
+   ::Info("GoodTracksTPC","Number of events : %d\n",nev);  
+
+
+   sprintf(fname,"%s/GoodTracksTPC.root",dir);
+   TFile *file=TFile::Open(fname,"recreate");
+
+   TClonesArray dummy("AliTrackReference",1000), *refs=&dummy;
+   TTree tpcTree("tpcTree","Tree with info about the reconstructable TPC tracks");
+   tpcTree.Branch("TPC",&refs);
+
+   //********  Loop over generated events 
+   for (Int_t e=0; e<nev; e++) {
+     Int_t nt=0;
+     refs->Clear();
+
+     Int_t i;
+
+     rl->GetEvent(e);  file->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 (i=0; i<np; i++) good[i]=0;
+     switch (ver) {
+     case 1:
+        {
+        AliTPCClustersArray *pca=new AliTPCClustersArray, &ca=*pca;
+        ca.Setup(digp);
+        ca.SetClusterType("AliTPCcluster");
+        ca.ConnectTree(tpcl->TreeR());
+        Int_t nrows=Int_t(ca.GetTree()->GetEntries());
+        for (Int_t n=0; n<nrows; n++) {
           AliSegmentID *s=ca.LoadEntry(n);
           Int_t sec,row;
           digp->AdjustSectorRow(s->GetID(),sec,row);
@@ -433,29 +479,27 @@ good_tracks_tpc(GoodTrackTPC *gt, const Int_t max, const char* evfoldname) {
               good[lab]++;
           }
           ca.ClearRow(sec,row);
-      }
-      delete pca;
-      tpcl->UnloadRecPoints();
-     }
-     break;
-   case 2:
-     {
-      tpcl->LoadDigits();
-      TTree *TD=tpcl->TreeD();
+        }
+        delete pca;
+        }
+        break;
+     case 2:
+        {
+        TTree *TD=tpcl->TreeD();
       
-      AliSimDigits da, *digits=&da;
-      TD->GetBranch("Segment")->SetAddress(&digits);
+        AliSimDigits da, *digits=&da;
+        TD->GetBranch("Segment")->SetAddress(&digits);
 
-      Int_t *count = new Int_t[np];
-      Int_t i;
-      for (i=0; i<np; i++) count[i]=0;
+        Int_t *count = new Int_t[np];
+        Int_t i;
+        for (i=0; i<np; i++) count[i]=0;
 
-      Int_t sectors_by_rows=(Int_t)TD->GetEntries();
-      for (i=0; i<sectors_by_rows; i++) {
+        Int_t sectors_by_rows=(Int_t)TD->GetEntries();
+        for (i=0; i<sectors_by_rows; i++) {
           if (!TD->GetEvent(i)) continue;
           Int_t sec,row;
           digp->AdjustSectorRow(digits->GetID(),sec,row);
-         //PH          cerr<<sec<<' '<<row<<"                                     \r";
+         //cerr<<sec<<' '<<row<<'\r';
           digits->First();
           do { //Many thanks to J.Chudoba who noticed this
               Int_t it=digits->CurrentRow(), ip=digits->CurrentColumn();
@@ -482,102 +526,82 @@ good_tracks_tpc(GoodTrackTPC *gt, const Int_t max, const char* evfoldname) {
               }
               count[j]=0;
           }
-      }
-      delete[] count;
-      tpcl->UnloadDigits();
+        }
+        delete[] count;
+        }
+        break;
      }
-      break;
-   default:
-      ::Fatal("AliTPCComparison.C","Invalid TPC version !");
-   }
 
-   rl->LoadKinematics();
-   AliStack* stack = rl->Stack();
-   /** select tracks which are "good" enough **/
-   for (i=0; i<np; i++) {
-      if ((good[i]&0x5000) != 0x5000)
-      if ((good[i]&0x2800) != 0x2800) continue;
-      if ((good[i]&0x7FF ) < good_number) continue;
-
-      TParticle *p = (TParticle*)stack->Particle(i);
-      if (p == 0x0)
-       {
+
+     //****** select tracks which are "good" enough
+     AliStack* stack = rl->Stack();
+     for (i=0; i<np; i++) {
+        if ((good[i]&0x5000) != 0x5000)
+        if ((good[i]&0x2800) != 0x2800) continue;
+        if ((good[i]&0x7FF ) < good_number) continue;
+
+        TParticle *p = (TParticle*)stack->Particle(i);
+        if (p == 0x0) {
          cerr<<"Can not get particle "<<i<<endl;
          continue;
-       }
-      if (p->Pt()<0.100) continue;
-      if (TMath::Abs(p->Pz()/p->Pt())>0.999) continue;
-
-      Double_t vx=p->Vx(),vy=p->Vy();
-      if (TMath::Sqrt(vx*vx+vy*vy)>3.5) continue;
-
-      /*
-      Int_t j=p->GetFirstMother();
-      if (j>=0) {
-        TParticle *pp = (TParticle*)stack->Particle(j);
-        if (pp == 0x0)
-         {
-           cerr<<"Can not get particle "<<j<<endl;
-           continue;
-         }
-        //if (pp->GetFirstMother()>=0) continue;//only one decay is allowed
-       //  for cascade hyperons only
-        Int_t jj=pp->GetFirstMother();
-        if (jj>=0) {
-          TParticle *ppp = (TParticle*)stack->Particle(jj);
-          if (ppp->GetFirstMother()>=0) continue;//two decays are allowed
         }
+        if (p->Pt()<0.100) continue;
+        if (TMath::Abs(p->Pz()/p->Pt())>0.999) continue;
 
-      }
-      */
-
-      gt[nt].lab=i;
-      gt[nt].code=p->GetPdgCode();
-      gt[nt].px=0.; gt[nt].py=0.; gt[nt].pz=0.;
-      gt[nt].x=0.; gt[nt].y=0.; gt[nt].z=0.;
-      nt++;
-      if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
-      //      cerr<<np-i<<"                \r";
-   }
-   rl->UnloadKinematics();
+        Double_t vx=p->Vx(),vy=p->Vy();
+        if (TMath::Sqrt(vx*vx+vy*vy)>3.5) continue;
 
-   /** check if there is also information at the entrance of the TPC **/
-   
-   rl->LoadTrackRefs();
-   TTree *TR=rl->TreeTR();
-   TBranch *branch=TR->GetBranch("TPC");
-   if (branch==0) {
-      ::Fatal("AliTPCComparison.C::good_tracks_tpc","No track references !");
-   }
-   TClonesArray *references=new TClonesArray("AliTrackReference",1000);
-   branch->SetAddress(&references);
-   np=(Int_t)TR->GetEntries();
-   for (i=0; i<np; i++) {
-      references->Clear();
-      TR->GetEvent(i);
-      Int_t nref=references->GetEntriesFast();
-      if (nref==0) continue;
-      AliTrackReference *ref=(AliTrackReference*)references->UncheckedAt(0);
-
-      Int_t j, lab=ref->Label();
-      for (j=0; j<nt; j++) {if (gt[j].lab==lab) break;}
-      if (j==nt) continue;         
-
-      // (px,py,pz) - in global coordinate system, (x,y,z) - in local !
-      gt[j].px=ref->Px(); gt[j].py=ref->Py(); gt[j].pz=ref->Pz();
-      gt[j].x = ref->LocalX();
-      gt[j].y = ref->LocalY();
-      gt[j].z = ref->Z();
-   }
+        AliTrackReference *ref=new((*refs)[nt]) AliTrackReference();
+
+        ref->SetLabel(i);
+        Int_t pdg=p->GetPdgCode();
+        ref->SetLength(pdg);  //This will the particle's PDG !
+        ref->SetMomentum(0.,0.,0.);  
+        ref->SetPosition(0.,0.,0.);
+
+        nt++;
+     }
+
+     //**** check if there is also information at the entrance of the TPC
+     TTree *TR=rl->TreeTR();
+     TBranch *branch=TR->GetBranch("TPC");
+     if (branch==0) {
+        ::Error("GoodTracksTPC","No track references !");
+        delete rl;
+        return 5;
+     }
+     TClonesArray tpcdummy("AliTrackReference",1000), *tpcRefs=&tpcdummy;
+     branch->SetAddress(&tpcRefs);
+     Int_t nr=(Int_t)TR->GetEntries();
+     for (Int_t r=0; r<nr; r++) {
+         //cerr<<r<<' '<<nr<<'\r';
+         TR->GetEvent(r);
+         if (tpcRefs->GetEntriesFast()==0) continue;
+         AliTrackReference *tpcRef=(AliTrackReference*)tpcRefs->UncheckedAt(0);
+         Int_t j;
+        AliTrackReference *ref=0;
+         for (j=0; j<nt; j++) {
+          ref=(AliTrackReference *)refs->UncheckedAt(j);
+           if (ref->Label()==tpcRef->Label()) break;
+           ref=0;
+        }  
+         if (ref==0) continue;
+
+         ref->SetMomentum(tpcRef->Px(),tpcRef->Py(),tpcRef->Pz());
+         ref->SetPosition(tpcRef->LocalX(),tpcRef->LocalY(),tpcRef->Z());
+
+        tpcRefs->Clear();
+     }
+
+     tpcTree.Fill();
+
+     delete[] good;
+   } //****** end of the loop over generated events
 
-   delete references;
-   delete[] good;
    
-   tpcl->UnloadHits();
-   rl->UnloadTrackRefs();
-   rl->UnloadgAlice();
+   tpcTree.Write();
+   file->Close();
 
-   gBenchmark->Stop("AliTPCComparison");
-   gBenchmark->Show("AliTPCComparison");   
-   return nt;
+   delete rl;
+   return 0;
 }