]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliAODTagCreator.cxx
Possibility to fix some of the parameters. New method to get the number of free param...
[u/mrichter/AliRoot.git] / STEER / AliAODTagCreator.cxx
index 7034d8898e6838310005b115647251ec58c7b2b9..c7f4a57aba4f1077d929c914c762159ce68dbcd3 100644 (file)
@@ -48,13 +48,14 @@ ClassImp(AliAODTagCreator)
 
 //______________________________________________________________________________
   AliAODTagCreator::AliAODTagCreator() :
-    AliTagCreator() {
+    AliTagCreator(), fChain(new TChain("aodTree")) {
   //==============Default constructor for a AliAODTagCreator================
 }
 
 //______________________________________________________________________________
 AliAODTagCreator::~AliAODTagCreator() {
 //================Default destructor for a AliAODTagCreator===================
+  delete fChain;
 }
 
 //______________________________________________________________________________
@@ -63,9 +64,9 @@ Bool_t AliAODTagCreator::ReadGridCollection(TGridResult *fresult) {
   Int_t nEntries = fresult->GetEntries();
 
   TString alienUrl;
-  const char *guid;
-  const char *md5;
-  const char *turl;
+  const charguid;
+  const charmd5;
+  const charturl;
   Long64_t size = -1;
 
   Int_t counter = 0;
@@ -77,21 +78,30 @@ Bool_t AliAODTagCreator::ReadGridCollection(TGridResult *fresult) {
     turl = fresult->GetKey(i,"turl");
     if(md5 && !strlen(guid)) md5 = 0;
     if(guid && !strlen(guid)) guid = 0;
-
-    TFile *f = TFile::Open(alienUrl,"READ");
+    
+    fChain->Add(alienUrl);
+    //fGUIDList->Add(new TObjString(guid));
+    //fMD5List->Add(new TObjString(md5));
+    //fTURLList->Add(new TObjString(turl));
+    
+    //TFile *f = TFile::Open(alienUrl,"READ");
     //CreateTag(f,guid,md5,turl,size,counter);
-    f->Close();
-    delete f;   
+    //f->Close();
+    //delete f;         
     counter += 1;
   }//grid result loop
-
+  
+  AliInfo(Form("AOD chain created......."));   
+  AliInfo(Form("Chain entries: %d",fChain->GetEntries()));     
+  CreateTag(fChain,"grid");
+  
   return kTRUE;
 }
 
 //______________________________________________________________________________
 Bool_t AliAODTagCreator::ReadLocalCollection(const char *localpath) {
   // Checks the different subdirs of the given local path and in the
-  // case where it finds an AliAODs.root file it creates the tags
+  // case where it finds an AliESDs.root file it creates the tags
   
   void *dira =  gSystem->OpenDirectory(localpath);
   Char_t fPath[256];
@@ -105,27 +115,34 @@ Bool_t AliAODTagCreator::ReadLocalCollection(const char *localpath) {
     void *dirb =  gSystem->OpenDirectory(fPath);
     while((filename = gSystem->GetDirEntry(dirb))) {
       if(strstr(filename,pattern)) {
-       TString fAODFileName;
-       fAODFileName = fPath;
-       fAODFileName += "/";
-       fAODFileName += pattern;
-       TFile *f = TFile::Open(fAODFileName,"READ");
-       //CreateTag(f,fAODFileName,counter);
-       f->Close();
-       delete f;        
+       TString fESDFileName;
+       fESDFileName = fPath;
+       fESDFileName += "/";
+       fESDFileName += pattern;
+
+       fChain->Add(fESDFileName);
+
+       //TFile *f = TFile::Open(fESDFileName,"READ");
+       //CreateTag(f,fESDFileName,counter);
+       //f->Close();
+       //delete f;      
        
        counter += 1;
       }//pattern check
     }//child directory's entry loop
   }//parent directory's entry loop
 
+  AliInfo(Form("AOD chain created......."));   
+  AliInfo(Form("Chain entries: %d",fChain->GetEntries()));     
+  CreateTag(fChain,"local");
+
   return kTRUE;
 }
 
 //______________________________________________________________________________
 Bool_t AliAODTagCreator::ReadCAFCollection(const char *filename) {
   // Temporary solution for CAF: Takes as an input the ascii file that
-  // lists the AODs stored in the SE of the CAF and creates the tags.
+  // lists the ESDs stored in the SE of the CAF and creates the tags.
 
   // Open the input stream
   ifstream in;
@@ -137,19 +154,26 @@ Bool_t AliAODTagCreator::ReadCAFCollection(const char *filename) {
   while(in.good()) {
     in >> esdfile;
     if (!esdfile.Contains("root")) continue; // protection
-    TFile *f = TFile::Open(esdfile,"READ");
+
+    fChain->Add(esdfile);
+  
+    //TFile *f = TFile::Open(esdfile,"READ");
     //CreateTag(f,esdfile,counter);
-    f->Close();
-    delete f;   
+    //f->Close();
+    //delete f;         
     
     counter += 1;
   }
 
+  AliInfo(Form("AOD chain created......."));   
+  AliInfo(Form("Chain entries: %d",fChain->GetEntries()));     
+  CreateTag(fChain,"proof");
+
   return kTRUE;
 }
 
 //__________________________________________________________________________
-void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
+void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent, TList */*grpList*/) {
   //creates tag files for AODs
   
   Float_t fLowPtCut = 1.0;
@@ -169,11 +193,6 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
   Float_t maxPt = .0, meanPt = .0, totalP = .0;
-
-  AliRunTag *tag = new AliRunTag();
-  TTree ttag("T","A Tree with event tags");
-  TBranch * btag = ttag.Branch("AliTAG", &tag);
-  btag->SetCompressionLevel(9);
   
   //reading the esd tag file
   TChain *oldTagTree = new TChain("T");
@@ -187,14 +206,6 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
   }//directory loop
   AliInfo(Form("Chained tag files: %d",oldTagTree->GetEntries()));
 
-  //reading the esd tag file
-  AliRunTag *oldtag = new AliRunTag();
-  TString tagFilename;
-  oldTagTree->SetBranchAddress("AliTAG",&oldtag);
-  oldTagTree->GetEntry(0);
-  tag->CopyStandardContent(oldtag);
-  const TClonesArray *evTagList = oldtag->GetEventTags();
-
   AliInfo(Form("Creating the AOD tags......."));       
 
   TFile *file = TFile::Open("AliAOD.root");
@@ -211,6 +222,30 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
   if(fLastEvent == -1) lastEvent = (Int_t)aodTree->GetEntries();
   else lastEvent = fLastEvent;
 
+  if(fLastEvent == -1) lastEvent = (Int_t)aodTree->GetEntries();
+  else lastEvent = fLastEvent;
+
+  char fileName[256];
+  sprintf(fileName, "Run%d.Event%d_%d.AOD.tag.root", 
+         aod->GetRunNumber(),fFirstEvent,lastEvent );
+  AliInfo(Form("writing tags to file %s", fileName));
+  AliDebug(1, Form("writing tags to file %s", fileName));
+  TFile* ftag = TFile::Open(fileName, "recreate");
+
+  AliRunTag *tag = new AliRunTag();
+  TTree ttag("T","A Tree with event tags");
+  TBranch * btag = ttag.Branch("AliTAG", &tag);
+  btag->SetCompressionLevel(9);
+
+  //reading the esd tag file
+  AliRunTag *oldtag = new AliRunTag();
+  TString tagFilename;
+  oldTagTree->SetBranchAddress("AliTAG",&oldtag);
+  oldTagTree->GetEntry(0);
+  tag->CopyStandardContent(oldtag);
+  const TClonesArray *evTagList = oldtag->GetEventTags();
+
   // loop over events
   Int_t nEvents = aodTree->GetEntries();
   for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
@@ -334,20 +369,10 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
     evTag->SetMaxPt(maxPt);
     tag->AddEventTag(*evTag);
   }//event loop
-  if(fLastEvent == -1) lastEvent = (Int_t)aodTree->GetEntries();
-  else lastEvent = fLastEvent;
 
+  ftag->cd();
   ttag.Fill();
   tag->Clear();
-
-  char fileName[256];
-  sprintf(fileName, "Run%d.Event%d_%d.AOD.tag.root", 
-         tag->GetRunId(),fFirstEvent,lastEvent );
-  AliInfo(Form("writing tags to file %s", fileName));
-  AliDebug(1, Form("writing tags to file %s", fileName));
-  TFile* ftag = TFile::Open(fileName, "recreate");
-  ftag->cd();
   ttag.Write();
   ftag->Close();
   file->cd();
@@ -355,240 +380,206 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
 }
 
 //_____________________________________________________________________________
-/*void AliAODTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter) {
+void AliAODTagCreator::CreateTag(TChain* fChain, const char *type) {
   //private method that creates tag files
-  TString fguid = guid;
-  TString fmd5 = md5;
-  TString fturl = turl;
-  /////////////
-  //muon code//
-  ////////////
-  Double_t fMUONMASS = 0.105658369;
-  //Variables
-  Double_t fX,fY,fZ ;
-  Double_t fThetaX, fThetaY, fPyz, fChisquare;
-  Double_t fPxRec, fPyRec, fPzRec, fEnergy;
-  Int_t fCharge;
-  TLorentzVector fEPvector;
-
-  Float_t fZVertexCut = 10.0; 
-  Float_t fRhoVertexCut = 2.0; 
+  TString fSession = type;
+  TString fguid, fmd5, fturl;
+  TString fTempGuid = 0;
 
+  //private method that creates tag files                                            
   Float_t fLowPtCut = 1.0;
   Float_t fHighPtCut = 3.0;
   Float_t fVeryHighPtCut = 10.0;
-  ////////////
-
-  Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
+  ////////////                            
+  Double_t partFrac[10] = {0.01, 0.01, 0.85, 0.10, 0.05, 0., 0., 0., 0., 0.};
 
   // Creates the tags for all the events in a given AOD file
-  Bool_t fIsSim = kTRUE;
   Int_t ntrack;
   Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
   Int_t nPos, nNeg, nNeutr;
+  Int_t nKinks, nV0s, nCascades;
   Int_t nK0s, nNeutrons, nPi0s, nGamas;
   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
   Float_t maxPt = .0, meanPt = .0, totalP = .0;
-  Int_t fVertexflag;
-  Int_t iRunNumber = 0;
-  TString fVertexName;
+  
+  //reading the esd tag file                                                  
+  TChain *oldTagTree = new TChain("T");
+  const char * tagPattern = "ESD.tag";
+  // Open the working directory
+  void * dirp = gSystem->OpenDirectory(gSystem->pwd());
+  const char * name = 0x0;
+  // Add all files matching *pattern* to the chain
+  while((name = gSystem->GetDirEntry(dirp))) {
+    if (strstr(name,tagPattern)) oldTagTree->Add(name);
+  }//directory loop
+  AliInfo(Form("Chained tag files: %d",oldTagTree->GetEntries()));
+  
+  /*if (!file || !file->IsOpen()) {
+    AliError(Form("opening failed"));
+    delete file;
+    return ;
+  }
+  TTree *aodTree = (TTree*)file->Get("aodTree");*/
+  AliAODEvent *aod = new AliAODEvent();
+  aod->ReadFromTree(fChain);
+  fChain->GetEntry(0);
+  TFile *f = fChain->GetFile();
+  fTempGuid = f->GetUUID().AsString();
+  Int_t firstEvent = 0, lastEvent = 0;
+  //lastEvent = (Int_t)aodTree->GetEntries();
+
+  TString localFileName = "Run"; localFileName += aod->GetRunNumber(); 
+  localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += fChain->GetEntries(); //localFileName += "."; localFileName += Counter;
+  localFileName += ".AOD.tag.root";
+
+  TString fileName;
+  
+  if(fStorage == 0) {
+    fileName = localFileName.Data();      
+    AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
+  }
+  else if(fStorage == 1) {
+    TString alienLocation = "/alien";
+    alienLocation += gGrid->Pwd();
+    alienLocation += fgridpath.Data();
+    alienLocation += "/";
+    alienLocation +=  localFileName;
+    alienLocation += "?se=";
+    alienLocation += fSE.Data();
+    fileName = alienLocation.Data();
+    AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
+  }
+
+  TFile* ftag = TFile::Open(fileName, "recreate");
 
   AliRunTag *tag = new AliRunTag();
-  AliEventTag *evTag = new AliEventTag();
   TTree ttag("T","A Tree with event tags");
   TBranch * btag = ttag.Branch("AliTAG", &tag);
   btag->SetCompressionLevel(9);
-  
-  AliInfo(Form("Creating the tags......."));   
-  
-  Int_t firstEvent = 0,lastEvent = 0;
-  TTree *t = (TTree*) file->Get("esdTree");
-  AliESDEvent *esd = new AliESDEvent();
-  esd->ReadFromTree(t);
-  
-  t->GetEntry(0);
-  Int_t iInitRunNumber = esd->GetRunNumber();
 
-  Int_t iNumberOfEvents = (Int_t)t->GetEntries();
-  for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
+  //reading the esd tag file 
+  AliRunTag *oldtag = new AliRunTag();
+  TString tagFilename;
+  oldTagTree->SetBranchAddress("AliTAG",&oldtag);
+  oldTagTree->GetEntry(0);
+  tag->CopyStandardContent(oldtag);
+  const TClonesArray *evTagList = oldtag->GetEventTags();
+
+  AliInfo(Form("Creating the AOD tags......."));
+
+  // loop over events 
+  Int_t nEvents = fChain->GetEntries();
+  for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
+    AliEventTag *evTag = (AliEventTag *)evTagList->At(iEventNumber);
     ntrack = 0;
-    nPos = 0;
-    nNeg = 0;
-    nNeutr =0;
-    nK0s = 0;
-    nNeutrons = 0;
-    nPi0s = 0;
-    nGamas = 0;
-    nProtons = 0;
-    nKaons = 0;
-    nPions = 0;
-    nMuons = 0;
-    nElectrons = 0;      
-    nCh1GeV = 0;
-    nCh3GeV = 0;
-    nCh10GeV = 0;
-    nMu1GeV = 0;
-    nMu3GeV = 0;
-    nMu10GeV = 0;
-    nEl1GeV = 0;
-    nEl3GeV = 0;
-    nEl10GeV = 0;
-    maxPt = .0;
-    meanPt = .0;
-    totalP = .0;
-    fVertexflag = 1;
-    
-    t->GetEntry(iEventNumber);
-    iRunNumber = esd->GetRunNumber();
-    if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
-    const AliESDVertex * vertexIn = esd->GetVertex();
-    fVertexName = vertexIn->GetName();
-    if(fVertexName == "default") fVertexflag = 0;
-
-    for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
-      AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
-      if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
-      else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
-      UInt_t status = esdTrack->GetStatus();
-      
-      //select only tracks with ITS refit
-      if ((status&AliESDtrack::kITSrefit)==0) continue;
-      //select only tracks with TPC refit
-      if ((status&AliESDtrack::kTPCrefit)==0) continue;
-      
-      //select only tracks with the "combined PID"
-      if ((status&AliESDtrack::kESDpid)==0) continue;
-      Double_t p[3];
-      esdTrack->GetPxPyPz(p);
-      Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
-      Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
-      totalP += momentum;
-      meanPt += fPt;
+    nPos = 0; nNeg = 0; nNeutr =0;
+    nKinks = 0; nV0s = 0; nCascades = 0;
+    nK0s = 0; nNeutrons = 0; nPi0s = 0; nGamas = 0;
+    nProtons = 0;  nKaons = 0; nPions = 0; nMuons = 0; nElectrons = 0;
+    nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
+    nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
+    nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
+    maxPt = .0; meanPt = .0; totalP = .0;
+
+    // read events
+    fChain->GetEntry(iEventNumber);
+    TFile *f = fChain->GetFile();
+    const TUrl *url = f->GetEndpointUrl();
+    fguid = f->GetUUID().AsString();
+    if(fSession == "grid") {
+      TString fturltemp = "alien://"; fturltemp += url->GetFile();
+      fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
+    }
+    else fturl = url->GetFile();
+
+    // set pointers
+    aod->GetStdContent();
+
+    Int_t nTracks = aod->GetNTracks();
+    // loop over vertices 
+    Int_t nVtxs = aod->GetNVertices();
+    for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
+      AliAODVertex *vertex = aod->GetVertex(nVtx);
+      if(vertex->GetType() == 1) nKinks += 1;
+      if(vertex->GetType() == 2) nV0s += 1;
+      if(vertex->GetType() == 3) nCascades += 1;
+    }
+    for (Int_t nTr = 0; nTr < nTracks; nTr++) {
+      AliAODTrack *track = aod->GetTrack(nTr);
+
+      Double_t fPt = track->Pt();
       if(fPt > maxPt) maxPt = fPt;
-      
-      if(esdTrack->GetSign() > 0) {
+      if(track->Charge() > 0) {
        nPos++;
        if(fPt > fLowPtCut) nCh1GeV++;
        if(fPt > fHighPtCut) nCh3GeV++;
        if(fPt > fVeryHighPtCut) nCh10GeV++;
       }
-      if(esdTrack->GetSign() < 0) {
+      if(track->Charge() < 0) {
        nNeg++;
        if(fPt > fLowPtCut) nCh1GeV++;
        if(fPt > fHighPtCut) nCh3GeV++;
        if(fPt > fVeryHighPtCut) nCh10GeV++;
       }
-      if(esdTrack->GetSign() == 0) nNeutr++;
-      
+      if(track->Charge() == 0) nNeutr++;
       //PID
-      Double_t prob[5];
-      esdTrack->GetESDpid(prob);
-      
+      const Double32_t *prob = track->PID();
       Double_t rcc = 0.0;
       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
       if(rcc == 0.0) continue;
-      //Bayes' formula
-      Double_t w[5];
+      //Bayes' formula  
+      Double_t w[10];
       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
-      
+
       //protons
       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
       //kaons
       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
       //pions
-      if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
-      //electrons
+      if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++;
+      //muons 
+      if ((w[1]>w[4])&&(w[1]>w[3])&&(w[1]>w[2])&&(w[1]>w[0])) {
+       nMuons++;
+       if(fPt > fLowPtCut) nMu1GeV++;
+       if(fPt > fHighPtCut) nMu3GeV++;
+       if(fPt > fVeryHighPtCut) nMu10GeV++;
+      }
+      //electrons  
       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
        nElectrons++;
        if(fPt > fLowPtCut) nEl1GeV++;
        if(fPt > fHighPtCut) nEl3GeV++;
        if(fPt > fVeryHighPtCut) nEl10GeV++;
-      }          
-      ntrack++;
-    }//esd track loop
-    
-    /////////////
-    //muon code//
-    ////////////
-    Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
-    // loop over all reconstructed tracks (also first track of combination)
-    for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
-      AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
-      if (muonTrack == 0x0) continue;
-      
-      // Coordinates at vertex
-      fZ = muonTrack->GetZ(); 
-      fY = muonTrack->GetBendingCoor();
-      fX = muonTrack->GetNonBendingCoor(); 
-      
-      fThetaX = muonTrack->GetThetaX();
-      fThetaY = muonTrack->GetThetaY();
-      
-      fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
-      fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
-      fPxRec = fPzRec * TMath::Tan(fThetaX);
-      fPyRec = fPzRec * TMath::Tan(fThetaY);
-      fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
-      
-      //ChiSquare of the track if needed
-      fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
-      fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
-      fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
-      
-      // total number of muons inside a vertex cut 
-      if((TMath::Abs(fZ)<fZVertexCut) && (TMath::Sqrt(fY*fY+fX*fX)<fRhoVertexCut)) {
-       nMuons++;
-       if(fEPvector.Pt() > fLowPtCut) {
-         nMu1GeV++; 
-         if(fEPvector.Pt() > fHighPtCut) {
-           nMu3GeV++; 
-           if (fEPvector.Pt() > fVeryHighPtCut) {
-             nMu10GeV++;
-           }
-         }
-       }
       }
-    }//muon track loop
-    
-    // Fill the event tags 
-    if(ntrack != 0) meanPt = meanPt/ntrack;
+
+      totalP += track->P();
+      meanPt += fPt;
+      ntrack++;
+    }//track loop
+    // Fill the event tags  
+    if(ntrack != 0)
+      meanPt = meanPt/ntrack;
     
     evTag->SetEventId(iEventNumber+1);
     evTag->SetGUID(fguid);
-    evTag->SetMD5(fmd5);
-    evTag->SetTURL(fturl);
-    evTag->SetSize(size);
-    evTag->SetVertexX(vertexIn->GetXv());
-    evTag->SetVertexY(vertexIn->GetYv());
-    evTag->SetVertexZ(vertexIn->GetZv());
-    evTag->SetVertexZError(vertexIn->GetZRes());
-    evTag->SetVertexFlag(fVertexflag);
-    
-    evTag->SetT0VertexZ(esd->GetT0zVertex());
-    
-    evTag->SetTriggerMask(esd->GetTriggerMask());
-    evTag->SetTriggerCluster(esd->GetTriggerCluster());
-    
-    evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
-    evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
-    evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
-    evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
-    evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
-    evTag->SetNumOfParticipants(esd->GetZDCParticipants());
-    
-    
-    evTag->SetNumOfTracks(esd->GetNumberOfTracks());
+    if(fSession == "grid") {
+      evTag->SetMD5(0);
+      evTag->SetTURL(fturl);
+      evTag->SetSize(0);
+    }
+    else evTag->SetPath(fturl);
+
+    evTag->SetNumOfTracks(nTracks);
     evTag->SetNumOfPosTracks(nPos);
     evTag->SetNumOfNegTracks(nNeg);
     evTag->SetNumOfNeutrTracks(nNeutr);
-    
-    evTag->SetNumOfV0s(esd->GetNumberOfV0s());
-    evTag->SetNumOfCascades(esd->GetNumberOfCascades());
-    evTag->SetNumOfKinks(esd->GetNumberOfKinks());
-    evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
-    
+
+    evTag->SetNumOfV0s(nV0s);
+    evTag->SetNumOfCascades(nCascades);
+    evTag->SetNumOfKinks(nKinks);
+
     evTag->SetNumOfProtons(nProtons);
     evTag->SetNumOfKaons(nKaons);
     evTag->SetNumOfPions(nPions);
@@ -598,7 +589,7 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
     evTag->SetNumOfPi0s(nPi0s);
     evTag->SetNumOfNeutrons(nNeutrons);
     evTag->SetNumOfKaon0s(nK0s);
-    
+
     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
@@ -608,289 +599,410 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
-    
-    evTag->SetNumOfPHOSClusters(esd->GetNumberOfPHOSClusters());
-    evTag->SetNumOfEMCALClusters(esd->GetNumberOfEMCALClusters());
-    
+
     evTag->SetTotalMomentum(totalP);
     evTag->SetMeanPt(meanPt);
     evTag->SetMaxPt(maxPt);
-    
-    tag->SetRunId(iInitRunNumber);
-    if(fIsSim) tag->SetDataType(0);
-    else tag->SetDataType(1);
+
+    if(fguid != fTempGuid) {
+      fTempGuid = fguid;
+      ttag.Fill();
+      tag->Clear("");
+    }
     tag->AddEventTag(*evTag);
+    if(iEventNumber+1 == fChain->GetEntries()) {
+      //AliInfo(Form("File: %s",fturl.Data()));
+      ttag.Fill();
+      tag->Clear("");
+    }      
   }//event loop
-  lastEvent = iNumberOfEvents;
-  
-  t->Delete("");
-  
-  ttag.Fill();
-  tag->Clear();
-
-  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
-  localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
-  localFileName += ".ESD.tag.root";
-
-  TString fileName;
-  
-  if(fStorage == 0) {
-    fileName = localFileName.Data();      
-    AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
-  }
-  else if(fStorage == 1) {
-    TString alienLocation = "/alien";
-    alienLocation += gGrid->Pwd();
-    alienLocation += fgridpath.Data();
-    alienLocation += "/";
-    alienLocation +=  localFileName;
-    alienLocation += "?se=";
-    alienLocation += fSE.Data();
-    fileName = alienLocation.Data();
-    AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
-  }
-
-  TFile* ftag = TFile::Open(fileName, "recreate");
+  lastEvent = fChain->GetEntries();
+    
   ftag->cd();
+  //ttag.Fill();
+  tag->Clear();
   ttag.Write();
   ftag->Close();
-
-  delete ftag;
-  delete esd;
-
-  delete tag;
-  }*/
+}
 
 //_____________________________________________________________________________
-/*void AliESDTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter) {
+void AliAODTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter) {
   //private method that creates tag files
-  /////////////
-  //muon code//
-  ////////////
-  Double_t fMUONMASS = 0.105658369;
-  //Variables
-  Double_t fX,fY,fZ ;
-  Double_t fThetaX, fThetaY, fPyz, fChisquare;
-  Double_t fPxRec, fPyRec, fPzRec, fEnergy;
-  Int_t fCharge;
-  TLorentzVector fEPvector;
-
-  Float_t fZVertexCut = 10.0; 
-  Float_t fRhoVertexCut = 2.0; 
+  TString fguid = guid;
+  TString fmd5 = md5;
+  TString fturl = turl;
 
+  //private method that creates tag files                                            
   Float_t fLowPtCut = 1.0;
   Float_t fHighPtCut = 3.0;
   Float_t fVeryHighPtCut = 10.0;
-  ////////////
-
-  Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
+  ////////////                            
+  Double_t partFrac[10] = {0.01, 0.01, 0.85, 0.10, 0.05, 0., 0., 0., 0., 0.};
 
-  // Creates the tags for all the events in a given ESD file
-  Bool_t fIsSim = kTRUE;
+  // Creates the tags for all the events in a given AOD file
   Int_t ntrack;
   Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
   Int_t nPos, nNeg, nNeutr;
+  Int_t nKinks, nV0s, nCascades;
   Int_t nK0s, nNeutrons, nPi0s, nGamas;
   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
   Float_t maxPt = .0, meanPt = .0, totalP = .0;
-  Int_t fVertexflag;
-  Int_t iRunNumber = 0;
-  TString fVertexName;
 
   AliRunTag *tag = new AliRunTag();
-  AliEventTag *evTag = new AliEventTag();
   TTree ttag("T","A Tree with event tags");
   TBranch * btag = ttag.Branch("AliTAG", &tag);
   btag->SetCompressionLevel(9);
   
-  AliInfo(Form("Creating the tags......."));   
-  
-  Int_t firstEvent = 0,lastEvent = 0;
-  
-  TTree *t = (TTree*) file->Get("esdTree");
-  AliESDEvent *esd = new AliESDEvent();
-  esd->ReadFromTree(t);
-  
-  t->GetEntry(0);
-  Int_t iInitRunNumber = esd->GetRunNumber();
-
-  Int_t iNumberOfEvents = (Int_t)t->GetEntries();
-  for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
-    ntrack = 0;
-    nPos = 0;
-    nNeg = 0;
-    nNeutr =0;
-    nK0s = 0;
-    nNeutrons = 0;
-    nPi0s = 0;
-    nGamas = 0;
-    nProtons = 0;
-    nKaons = 0;
-    nPions = 0;
-    nMuons = 0;
-    nElectrons = 0;      
-    nCh1GeV = 0;
-    nCh3GeV = 0;
-    nCh10GeV = 0;
-    nMu1GeV = 0;
-    nMu3GeV = 0;
-    nMu10GeV = 0;
-    nEl1GeV = 0;
-    nEl3GeV = 0;
-    nEl10GeV = 0;
-    maxPt = .0;
-    meanPt = .0;
-    totalP = .0;
-    fVertexflag = 1;
-    
-    t->GetEntry(iEventNumber);
-    iRunNumber = esd->GetRunNumber();
-    if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
-    const AliESDVertex * vertexIn = esd->GetVertex();
-    fVertexName = vertexIn->GetName();
-    if(fVertexName == "default") fVertexflag = 0;
-
-    for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
-      AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
-      if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
-      else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
-      UInt_t status = esdTrack->GetStatus();
-      
-      //select only tracks with ITS refit
-      if ((status&AliESDtrack::kITSrefit)==0) continue;
-      //select only tracks with TPC refit
-      if ((status&AliESDtrack::kTPCrefit)==0) continue;
-      
-      //select only tracks with the "combined PID"
-      if ((status&AliESDtrack::kESDpid)==0) continue;
-      Double_t p[3];
-      esdTrack->GetPxPyPz(p);
-      Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
-      Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
-      totalP += momentum;
-      meanPt += fPt;
+  //reading the esd tag file                    
+                              
+  TChain *oldTagTree = new TChain("T");
+  const char * tagPattern = "ESD.tag";
+  // Open the working directory
+  void * dirp = gSystem->OpenDirectory(gSystem->pwd());
+  const char * name = 0x0;
+  // Add all files matching *pattern* to the chain
+  while((name = gSystem->GetDirEntry(dirp))) {
+    if (strstr(name,tagPattern)) oldTagTree->Add(name);
+  }//directory loop
+  AliInfo(Form("Chained tag files: %d",oldTagTree->GetEntries()));
+  
+  //reading the esd tag file 
+  AliRunTag *oldtag = new AliRunTag();
+  TString tagFilename;
+  oldTagTree->SetBranchAddress("AliTAG",&oldtag);
+  oldTagTree->GetEntry(0);
+  tag->CopyStandardContent(oldtag);
+  const TClonesArray *evTagList = oldtag->GetEventTags();
+
+  AliInfo(Form("Creating the AOD tags......."));
+
+  if (!file || !file->IsOpen()) {
+    AliError(Form("opening failed"));
+    delete file;
+    return ;
+  }
+  TTree *aodTree = (TTree*)file->Get("aodTree");
+  AliAODEvent *aod = new AliAODEvent();
+  aod->ReadFromTree(aodTree);
+  Int_t firstEvent = 0, lastEvent = 0;
+  lastEvent = (Int_t)aodTree->GetEntries();
+
+  // loop over events 
+  Int_t nEvents = aodTree->GetEntries();
+  for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
+    AliEventTag *evTag = (AliEventTag *)evTagList->At(iEventNumber);
+    ntrack = 0;
+    nPos = 0; nNeg = 0; nNeutr =0;
+    nKinks = 0; nV0s = 0; nCascades = 0;
+    nK0s = 0; nNeutrons = 0; nPi0s = 0; nGamas = 0;
+    nProtons = 0;  nKaons = 0; nPions = 0; nMuons = 0; nElectrons = 0;
+    nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
+    nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
+    nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
+    maxPt = .0; meanPt = .0; totalP = .0;
+
+    // read events
+    aodTree->GetEvent(iEventNumber);
+    // set pointers
+    aod->GetStdContent();
+
+    Int_t nTracks = aod->GetNTracks();
+    // loop over vertices 
+    Int_t nVtxs = aod->GetNVertices();
+    for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
+      AliAODVertex *vertex = aod->GetVertex(nVtx);
+      if(vertex->GetType() == 1) nKinks += 1;
+      if(vertex->GetType() == 2) nV0s += 1;
+      if(vertex->GetType() == 3) nCascades += 1;
+    }
+    for (Int_t nTr = 0; nTr < nTracks; nTr++) {
+      AliAODTrack *track = aod->GetTrack(nTr);
+
+      Double_t fPt = track->Pt();
       if(fPt > maxPt) maxPt = fPt;
-      
-      if(esdTrack->GetSign() > 0) {
+      if(track->Charge() > 0) {
        nPos++;
        if(fPt > fLowPtCut) nCh1GeV++;
        if(fPt > fHighPtCut) nCh3GeV++;
        if(fPt > fVeryHighPtCut) nCh10GeV++;
       }
-      if(esdTrack->GetSign() < 0) {
+      if(track->Charge() < 0) {
        nNeg++;
        if(fPt > fLowPtCut) nCh1GeV++;
        if(fPt > fHighPtCut) nCh3GeV++;
        if(fPt > fVeryHighPtCut) nCh10GeV++;
       }
-      if(esdTrack->GetSign() == 0) nNeutr++;
-      
+      if(track->Charge() == 0) nNeutr++;
       //PID
-      Double_t prob[5];
-      esdTrack->GetESDpid(prob);
-      
+      const Double32_t *prob = track->PID();
       Double_t rcc = 0.0;
       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
       if(rcc == 0.0) continue;
-      //Bayes' formula
-      Double_t w[5];
+      //Bayes' formula  
+      Double_t w[10];
       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
-      
+
       //protons
       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
       //kaons
       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
       //pions
-      if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
-      //electrons
+      if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++;
+      //muons 
+      if ((w[1]>w[4])&&(w[1]>w[3])&&(w[1]>w[2])&&(w[1]>w[0])) {
+       nMuons++;
+       if(fPt > fLowPtCut) nMu1GeV++;
+       if(fPt > fHighPtCut) nMu3GeV++;
+       if(fPt > fVeryHighPtCut) nMu10GeV++;
+      }
+      //electrons  
       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
        nElectrons++;
        if(fPt > fLowPtCut) nEl1GeV++;
        if(fPt > fHighPtCut) nEl3GeV++;
        if(fPt > fVeryHighPtCut) nEl10GeV++;
-      }          
+      }
+
+      totalP += track->P();
+      meanPt += fPt;
       ntrack++;
-    }//esd track loop
+    }//track loop
+    // Fill the event tags  
+    if(ntrack != 0)
+      meanPt = meanPt/ntrack;
     
-    /////////////
-    //muon code//
-    ////////////
-    Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
-    // loop over all reconstructed tracks (also first track of combination)
-    for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
-      AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
-      if (muonTrack == 0x0) continue;
-      
-      // Coordinates at vertex
-      fZ = muonTrack->GetZ(); 
-      fY = muonTrack->GetBendingCoor();
-      fX = muonTrack->GetNonBendingCoor(); 
-      
-      fThetaX = muonTrack->GetThetaX();
-      fThetaY = muonTrack->GetThetaY();
-      
-      fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
-      fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
-      fPxRec = fPzRec * TMath::Tan(fThetaX);
-      fPyRec = fPzRec * TMath::Tan(fThetaY);
-      fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
+    evTag->SetEventId(iEventNumber+1);
+    evTag->SetGUID(fguid);
+    evTag->SetMD5(fmd5);
+    evTag->SetTURL(fturl);
+    evTag->SetSize(size);
+
+    evTag->SetNumOfTracks(nTracks);
+    evTag->SetNumOfPosTracks(nPos);
+    evTag->SetNumOfNegTracks(nNeg);
+    evTag->SetNumOfNeutrTracks(nNeutr);
+
+    evTag->SetNumOfV0s(nV0s);
+    evTag->SetNumOfCascades(nCascades);
+    evTag->SetNumOfKinks(nKinks);
+
+    evTag->SetNumOfProtons(nProtons);
+    evTag->SetNumOfKaons(nKaons);
+    evTag->SetNumOfPions(nPions);
+    evTag->SetNumOfMuons(nMuons);
+    evTag->SetNumOfElectrons(nElectrons);
+    evTag->SetNumOfPhotons(nGamas);
+    evTag->SetNumOfPi0s(nPi0s);
+    evTag->SetNumOfNeutrons(nNeutrons);
+    evTag->SetNumOfKaon0s(nK0s);
+
+    evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
+    evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
+    evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
+    evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
+    evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
+    evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
+    evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
+    evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
+    evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
+
+    evTag->SetTotalMomentum(totalP);
+    evTag->SetMeanPt(meanPt);
+    evTag->SetMaxPt(maxPt);
+    tag->AddEventTag(*evTag);
+  }//event loop
+    
+  TString localFileName = "Run"; localFileName += tag->GetRunId();
+  localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; 
+  localFileName += lastEvent; localFileName += "."; localFileName += Counter;
+  localFileName += ".AOD.tag.root";
+
+  TString fileName;
+
+  if(fStorage == 0) {
+    fileName = localFileName.Data();
+    AliInfo(Form("Writing AOD tags to local file: %s",fileName.Data()));
+  }
+  else if(fStorage == 1) {
+    TString alienLocation = "/alien";
+    alienLocation += gGrid->Pwd();
+    alienLocation += fgridpath.Data();
+    alienLocation += "/";
+    alienLocation +=  localFileName;
+    alienLocation += "?se=";
+    alienLocation += fSE.Data();
+    fileName = alienLocation.Data();
+    AliInfo(Form("Writing AOD tags to grid file: %s",fileName.Data()));
+  }
+
+  TFile* ftag = TFile::Open(fileName, "recreate");
+  ftag->cd();
+  ttag.Fill();
+  tag->Clear();
+  ttag.Write();
+  ftag->Close();
+}
+  
+
+//_____________________________________________________________________________
+void AliAODTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter) {
+  //private method that creates tag files
+  Float_t fLowPtCut = 1.0;
+  Float_t fHighPtCut = 3.0;
+  Float_t fVeryHighPtCut = 10.0;
+  ////////////
+  
+  Double_t partFrac[10] = {0.01, 0.01, 0.85, 0.10, 0.05, 0., 0., 0., 0., 0.};
+  
+  // Creates the tags for all the events in a given AOD file
+  Int_t ntrack;
+  Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
+  Int_t nPos, nNeg, nNeutr;
+  Int_t nKinks, nV0s, nCascades;
+  Int_t nK0s, nNeutrons, nPi0s, nGamas;
+  Int_t nCh1GeV, nCh3GeV, nCh10GeV;
+  Int_t nMu1GeV, nMu3GeV, nMu10GeV;
+  Int_t nEl1GeV, nEl3GeV, nEl10GeV;
+  Float_t maxPt = .0, meanPt = .0, totalP = .0;
+
+  AliRunTag *tag = new AliRunTag();
+  TTree ttag("T","A Tree with event tags");
+  TBranch * btag = ttag.Branch("AliTAG", &tag);
+  btag->SetCompressionLevel(9);
+
+  //reading the esd tag file
+  TChain *oldTagTree = new TChain("T");
+  const char * tagPattern = "ESD.tag";
+  // Open the working directory
+  void * dirp = gSystem->OpenDirectory(gSystem->pwd());
+  const char * name = 0x0;
+  // Add all files matching *pattern* to the chain
+  while((name = gSystem->GetDirEntry(dirp))) {
+    if (strstr(name,tagPattern)) oldTagTree->Add(name);
+  }//directory loop
+  AliInfo(Form("Chained tag files: %d",oldTagTree->GetEntries()));
+
+  //reading the esd tag file
+  AliRunTag *oldtag = new AliRunTag();
+  TString tagFilename;
+  oldTagTree->SetBranchAddress("AliTAG",&oldtag);
+  oldTagTree->GetEntry(0);
+  tag->CopyStandardContent(oldtag);
+  const TClonesArray *evTagList = oldtag->GetEventTags();
+
+  AliInfo(Form("Creating the AOD tags......."));
+
+  if (!file || !file->IsOpen()) {
+    AliError(Form("opening failed"));
+    delete file;
+    return ;
+  }
+  TTree *aodTree = (TTree*)file->Get("aodTree");
+  AliAODEvent *aod = new AliAODEvent();
+  aod->ReadFromTree(aodTree);
+  Int_t firstEvent = 0, lastEvent = 0;  
+  lastEvent = (Int_t)aodTree->GetEntries();
+  
+  // loop over events
+  Int_t nEvents = aodTree->GetEntries();
+  for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
+    AliEventTag *evTag = (AliEventTag *)evTagList->At(iEventNumber);    
+    ntrack = 0;
+    nPos = 0; nNeg = 0; nNeutr =0;
+    nKinks = 0; nV0s = 0; nCascades = 0;
+    nK0s = 0; nNeutrons = 0; nPi0s = 0; nGamas = 0;
+    nProtons = 0;  nKaons = 0; nPions = 0; nMuons = 0; nElectrons = 0;  
+    nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
+    nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
+    nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
+    maxPt = .0; meanPt = .0; totalP = .0;
+
+    // read events
+    aodTree->GetEvent(iEventNumber);
+    
+    // set pointers
+    aod->GetStdContent();
+    
+    Int_t nTracks = aod->GetNTracks();
+    // loop over vertices
+    Int_t nVtxs = aod->GetNVertices();
+    for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
+      // print track info
+      AliAODVertex *vertex = aod->GetVertex(nVtx);
+      if(vertex->GetType() == 1) nKinks += 1;
+      if(vertex->GetType() == 2) nV0s += 1;
+      if(vertex->GetType() == 3) nCascades += 1;
+    }
+    for (Int_t nTr = 0; nTr < nTracks; nTr++) {
+      AliAODTrack *track = aod->GetTrack(nTr);
       
-      //ChiSquare of the track if needed
-      fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
-      fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
-      fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
+      Double_t fPt = track->Pt();
+      if(fPt > maxPt) maxPt = fPt;
+      if(track->Charge() > 0) {
+      nPos++;
+      if(fPt > fLowPtCut) nCh1GeV++;
+      if(fPt > fHighPtCut) nCh3GeV++;
+      if(fPt > fVeryHighPtCut) nCh10GeV++;
+      }
+      if(track->Charge() < 0) {
+      nNeg++;
+      if(fPt > fLowPtCut) nCh1GeV++;
+      if(fPt > fHighPtCut) nCh3GeV++;
+      if(fPt > fVeryHighPtCut) nCh10GeV++;
+      }
+      if(track->Charge() == 0) nNeutr++;
+
+      //PID
+      const Double32_t *prob = track->PID();
+      Double_t rcc = 0.0;
+      for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
+      if(rcc == 0.0) continue;
+      //Bayes' formula
+      Double_t w[10];
+      for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
       
-      // total number of muons inside a vertex cut 
-      if((TMath::Abs(fZ)<fZVertexCut) && (TMath::Sqrt(fY*fY+fX*fX)<fRhoVertexCut)) {
-       nMuons++;
-       if(fEPvector.Pt() > fLowPtCut) {
-         nMu1GeV++; 
-         if(fEPvector.Pt() > fHighPtCut) {
-           nMu3GeV++; 
-           if (fEPvector.Pt() > fVeryHighPtCut) {
-             nMu10GeV++;
-           }
-         }
-       }
+      //protons
+      if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
+      //kaons
+      if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
+      //pions
+      if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
+      //muons
+      if ((w[1]>w[4])&&(w[1]>w[3])&&(w[1]>w[2])&&(w[1]>w[0])) {
+      nMuons++;
+      if(fPt > fLowPtCut) nMu1GeV++;
+      if(fPt > fHighPtCut) nMu3GeV++;
+      if(fPt > fVeryHighPtCut) nMu10GeV++;
       }
-    }//muon track loop
-    
+      //electrons
+      if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
+      nElectrons++;
+      if(fPt > fLowPtCut) nEl1GeV++;
+      if(fPt > fHighPtCut) nEl3GeV++;
+      if(fPt > fVeryHighPtCut) nEl10GeV++;
+      }
+
+      totalP += track->P();
+      meanPt += fPt;
+      ntrack++;
+    }//track loop    
     // Fill the event tags 
-    if(ntrack != 0) meanPt = meanPt/ntrack;
-    
+    if(ntrack != 0)
+      meanPt = meanPt/ntrack;
+
     evTag->SetEventId(iEventNumber+1);
     evTag->SetPath(filepath);
-    evTag->SetVertexX(vertexIn->GetXv());
-    evTag->SetVertexY(vertexIn->GetYv());
-    evTag->SetVertexZ(vertexIn->GetZv());
-    evTag->SetVertexZError(vertexIn->GetZRes());
-    evTag->SetVertexFlag(fVertexflag);
-    
-    evTag->SetT0VertexZ(esd->GetT0zVertex());
-    
-    evTag->SetTriggerMask(esd->GetTriggerMask());
-    evTag->SetTriggerCluster(esd->GetTriggerCluster());
-    
-    evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
-    evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
-    evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
-    evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
-    evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
-    evTag->SetNumOfParticipants(esd->GetZDCParticipants());
-    
-    
-    evTag->SetNumOfTracks(esd->GetNumberOfTracks());
+        
+    evTag->SetNumOfTracks(nTracks);
     evTag->SetNumOfPosTracks(nPos);
     evTag->SetNumOfNegTracks(nNeg);
     evTag->SetNumOfNeutrTracks(nNeutr);
     
-    evTag->SetNumOfV0s(esd->GetNumberOfV0s());
-    evTag->SetNumOfCascades(esd->GetNumberOfCascades());
-    evTag->SetNumOfKinks(esd->GetNumberOfKinks());
-    evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
+    evTag->SetNumOfV0s(nV0s);
+    evTag->SetNumOfCascades(nCascades);
+    evTag->SetNumOfKinks(nKinks);
     
     evTag->SetNumOfProtons(nProtons);
     evTag->SetNumOfKaons(nKaons);
@@ -912,34 +1024,22 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
     
-    evTag->SetNumOfPHOSClusters(esd->GetNumberOfPHOSClusters());
-    evTag->SetNumOfEMCALClusters(esd->GetNumberOfEMCALClusters());
-    
     evTag->SetTotalMomentum(totalP);
     evTag->SetMeanPt(meanPt);
     evTag->SetMaxPt(maxPt);
-    
-    tag->SetRunId(iInitRunNumber);
-    if(fIsSim) tag->SetDataType(0);
-    else tag->SetDataType(1);
     tag->AddEventTag(*evTag);
-  }//event loop
-  lastEvent = iNumberOfEvents;
-  
-  t->Delete("");
-  
-  ttag.Fill();
-  tag->Clear();
+  }//event loop  
 
   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
-  localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
-  localFileName += ".ESD.tag.root";
+  localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; 
+  localFileName += lastEvent; localFileName += "."; localFileName += Counter;
+  localFileName += ".AOD.tag.root";
 
   TString fileName;
   
   if(fStorage == 0) {
     fileName = localFileName.Data();      
-    AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
+    AliInfo(Form("Writing AOD tags to local file: %s",fileName.Data()));
   }
   else if(fStorage == 1) {
     TString alienLocation = "/alien";
@@ -950,17 +1050,14 @@ void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
     alienLocation += "?se=";
     alienLocation += fSE.Data();
     fileName = alienLocation.Data();
-    AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
+    AliInfo(Form("Writing AOD tags to grid file: %s",fileName.Data()));
   }
 
   TFile* ftag = TFile::Open(fileName, "recreate");
   ftag->cd();
+  ttag.Fill();
+  tag->Clear();
   ttag.Write();
   ftag->Close();
-
-  delete ftag;
-  delete esd;
-
-  delete tag;
-  }*/
+}