]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliTagCreator.cxx
LOG muondep:
[u/mrichter/AliRoot.git] / STEER / AliTagCreator.cxx
index 9112448171ffd447aa95d050dd376f5f3aa032ed..0e87a52028857945ca864ffea1918574ec3ad883 100644 (file)
 //-----------------------------------------------------------------
 
 //ROOT
-#include <TObject.h>
 #include <Riostream.h>
-#include <TSystem.h>
-#include <TChain.h>
 #include <TFile.h>
 #include <TString.h>
-#include <THashTable.h>
+#include <TTree.h>
+#include <TSystem.h>
+#include <TChain.h>
+#include <TLorentzVector.h>
 
 //ROOT-AliEn
 #include <TGrid.h>
-//#include <TAlienCollection.h>
 #include <TGridResult.h>
-#include <TFileMerger.h>
-#include <TMap.h>
-#include <TXMLParser.h>
-#include <TThread.h>
-#include <TTreePlayer.h>
-#include <TProof.h>
 
 //AliRoot
 #include "AliRunTag.h"
 #include "AliEventTag.h"
-#include "AliESD.h"
-#include "AliESDVertex.h"
-#include "AliPID.h"
-#include "AliESDpid.h"
 #include "AliLog.h"
 
-
 #include "AliTagCreator.h"
 
 
@@ -56,404 +44,450 @@ ClassImp(AliTagCreator)
 
 
 //______________________________________________________________________________
-AliTagCreator::AliTagCreator() //local mode
+  AliTagCreator::AliTagCreator() :
+    TObject(),
+    fSE("ALICE::CERN::se"),
+    fgridpath(""),
+    fStorage(0)
 {
   //==============Default constructor for a AliTagCreator==================
-  fUser = "";
-  fPasswd = "";  
-  fSE = "";   
-  fHost = "";
-  fPort = 0; 
-  fresult = 0;
 }
 
 //______________________________________________________________________________
-AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username)
-{
-  //==============Default constructor for a AliTagCreator==================
-  fresult = 0;
-  fHost = host;
-  fUser = username;
-  fPort = port;
-
-  TString grid = "alien://";
-  grid += fHost;
-  grid += ":";
-  grid += port;
-  
-  //connect to the grid
-  TGrid::Connect(grid.Data(),fUser.Data());
-  if(!gGrid)
-    {
-      AliError("Connection failed!!!");
-      return;
-    }
+AliTagCreator::~AliTagCreator() {
+//================Default destructor for a AliTagCreator=======================
 }
 
-
 //______________________________________________________________________________
-AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username, const char *passwd)
-{
-  //==============Default constructor for a AliTagCreator==================
-  fresult = 0;
-  fHost = host;
-  fUser = username;
-  fPasswd = passwd;
-  fPort = port;
-
-  TString grid = "alien://";
-  grid += fHost;
-  grid += ":";
-  grid += port;
-  
-  //connect to the grid
-  TGrid::Connect(grid.Data(),fUser.Data(),fPasswd.Data());
-   if(!gGrid)
+void AliTagCreator::SetStorage(Int_t storage) {
+  // Sets correctly the storage: 0 for local, 1 for GRID
+  fStorage = storage;
+  if(fStorage == 0)
+    AliInfo(Form("Tags will be stored locally...."));
+  if(fStorage == 1)
+    AliInfo(Form("Tags will be stored in the grid...."));
+  if((fStorage != 0)&&(fStorage != 1))
     {
-      AliError("Connection failed!!!");
-      return;
-    }
+      AliInfo(Form("Storage was not properly set!!!"));
+      abort();
+    }  
 }
 
+//__________________________________________________________________________
+Bool_t AliTagCreator::MergeTags(const char *type) {
+  //Merges the tags and stores the merged tag file 
+  //locally if fStorage=0 or in the grid if fStorage=1
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = type; tagPattern += ".tag.root"; 
+
+  if(fStorage == 0) {
+    // 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)) fgChain->Add(name);  
+    }//directory loop
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }//local mode
+
+  else if(fStorage == 1) {
+    TString alienLocation = gGrid->Pwd();
+    alienLocation += fgridpath.Data();
+    alienLocation += "/";
+    TString queryPattern = "*."; queryPattern += tagPattern;
+    TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
+    Int_t nEntries = tagresult->GetEntries();
+    for(Int_t i = 0; i < nEntries; i++) {
+      TString alienUrl = tagresult->GetKey(i,"turl");
+      fgChain->Add(alienUrl);
+    }//grid result loop      
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }//grid mode
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged."; localFileName += tagPattern.Data();
+     
+  TString filename;
+  
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
+
+  fgChain->Merge(filename);
+  gSystem->Exec("rm Run*.Event*");
 
-//______________________________________________________________________________
-AliTagCreator::~AliTagCreator()
-{
-//================Default destructor for a AliTagCreator=======================
+  return kTRUE;
 }
 
-//______________________________________________________________________________
-void AliTagCreator::SetSE(const char *se)
-{
-  fSE = se;
-}
 
-//______________________________________________________________________________
-Bool_t AliTagCreator::ConnectToGrid(const char *host, Int_t port, const char *username)
-{
-  fHost = host;
-  fUser = username;
-  fPort = port;
-
-  TString grid = "alien://";
-  grid += fHost;
-  grid += ":";
-  grid += port;
+//__________________________________________________________________________
+Bool_t AliTagCreator::MergeTags(const char *type, const char *inflist) {
+  //Merges the tags and stores the merged tag file 
+  //locally if fStorage=0 or in the grid if fStorage=1
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = type; tagPattern += ".tag.root"; 
+
+//   if(fStorage == 0) {
+//     // 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)) fgChain->Add(name);  
+//     }//directory loop
+//     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
+//   }//local mode
+
+//   else if(fStorage == 1) {
+//     TString alienLocation = gGrid->Pwd();
+//     alienLocation += fgridpath.Data();
+//     alienLocation += "/";
+//     TString queryPattern = "*."; queryPattern += tagPattern;
+//     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
+//     Int_t nEntries = tagresult->GetEntries();
+//     for(Int_t i = 0; i < nEntries; i++) {
+//       TString alienUrl = tagresult->GetKey(i,"turl");
+//       fgChain->Add(alienUrl);
+//     }//grid result loop      
+//     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
+//   }//grid mode
+
+  if (fStorage == 0) {
+    ifstream *istr = new ifstream(inflist);
+    char fname[200];
+    while (!(istr->eof())) {
+      (*istr) >> fname;
+      if (strstr(fname, tagPattern)) fgChain->Add(fname);
+    }
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged."; localFileName += tagPattern.Data();
+     
+  TString filename;
   
-  //connect to the grid
-  TGrid::Connect(grid.Data(),fUser.Data());
-  if(!gGrid)
-    {
-      AliError("Connection failed!!!");
-      return kFALSE;
-    } //connect to the grid
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
+
+  fgChain->Merge(filename);
+  gSystem->Exec("rm Run*.Event*");
 
   return kTRUE;
 }
 
-//______________________________________________________________________________
-Bool_t AliTagCreator::ReadESDCollection(TGridResult *res)
-{
-  fresult = res;
-  Int_t NEntries = fresult->GetEntries();
+//__________________________________________________________________________
+Bool_t AliTagCreator::MergeTags(const char *type, TGridResult *result) {
+  //Merges the tags that are listed in the TGridResult 
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = "."; tagPattern += type; tagPattern += ".tag.root";
+
+  Int_t nEntries = result->GetEntries();
+
+  TString alienUrl;
+  for(Int_t i = 0; i < nEntries; i++) {
+    alienUrl = result->GetKey(i,"turl");
+    fgChain->Add(alienUrl);  
+  }
+  AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+    
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged"; localFileName += tagPattern.Data();
+     
+  TString filename;
+  
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
+  
+  fgChain->Merge(filename);
 
-  TString gridname = "alien://";
-  TString AlienUrl;
-  const char *guid;
+  return kTRUE;
+}
+
+Bool_t AliTagCreator::MergeTagsForRun(const char* type) {
+  //Merges the tags and stores the merged tag file 
+  //locally if fStorage=0 or in the grid if fStorage=1
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = type; tagPattern += ".tag.root"; 
+
+  if(fStorage == 0) {
+    // 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)) fgChain->Add(name);  
+    }//directory loop
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }//local mode
+
+  else if(fStorage == 1) {
+    TString alienLocation = gGrid->Pwd();
+    alienLocation += fgridpath.Data();
+    alienLocation += "/";
+    TString queryPattern = "*."; queryPattern += tagPattern;
+    TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
+    Int_t nEntries = tagresult->GetEntries();
+    for(Int_t i = 0; i < nEntries; i++) {
+      TString alienUrl = tagresult->GetKey(i,"turl");
+      fgChain->Add(alienUrl);
+    }//grid result loop      
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }//grid mode
  
-  Int_t counter = 0;
-  for(Int_t i = 0; i < NEntries; i++)
-    {
-      AlienUrl = gridname;
-      AlienUrl += fresult->GetKey(i,"lfn");
-      guid = fresult->GetKey(i,"guid");
-      TFile *f = TFile::Open(AlienUrl,"READ");
-      CreateTag(f,guid,counter);
-      f->Close();
-      delete f;         
-      counter += 1;
-    }//grid result loop
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged."; localFileName += tagPattern.Data();
+     
+  TString filename;
+  
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
+
+  //  fgChain->Merge(filename);
+  MergeToSingleRunTag(fgChain, filename);
+
+  gSystem->Exec("rm Run*.Event*");
 
   return kTRUE;
 }
 
-
-//_____________________________________________________________________________
-void AliTagCreator::CreateTag(TFile* file, const char *guid, Int_t Counter)
-{
-  Int_t ntrack;
-  Int_t NProtons, NKaons, NPions, NMuons, NElectrons;
-  Int_t Npos, Nneg, Nneutr;
-  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();
-  AliDetectorTag *detTag = new AliDetectorTag();
-  AliEventTag *evTag = new AliEventTag();
-  TTree ttag("T","A Tree with event tags");
-  TBranch * btag = ttag.Branch("AliTAG", "AliRunTag", &tag);
-  btag->SetCompressionLevel(9);
-  
-  AliInfo(Form("Creating the tags......."));   
+Bool_t AliTagCreator::MergeTagsForRun(const char* type, TGridResult *result) {
+  //Merges the tags that are listed in the TGridResult 
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = "."; tagPattern += type; tagPattern += ".tag.root";
+
+  Int_t nEntries = result->GetEntries();
+
+  TString alienUrl;
+  for(Int_t i = 0; i < nEntries; i++) {
+    alienUrl = result->GetKey(i,"turl");
+    fgChain->Add(alienUrl);  
+  }
+  AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+    
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged"; localFileName += tagPattern.Data();
+     
+  TString filename;
   
-  Int_t firstEvent = 0,lastEvent = 0;
-  TTree *t = (TTree*) file->Get("esdTree");
-  TBranch * b = t->GetBranch("ESD");
-  AliESD *esd = 0;
-  b->SetAddress(&esd);
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
   
-  tag->SetRunId(esd->GetRunNumber());
-  
-  Int_t i_NumberOfEvents = b->GetEntries();
-  for (Int_t i_EventNumber = 0; i_EventNumber < i_NumberOfEvents; i_EventNumber++)
-    {
-      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;
-      
-      b->GetEntry(i_EventNumber);
-      const AliESDVertex * VertexIn = esd->GetVertex();
-      
-      for (Int_t i_TrackNumber = 0; i_TrackNumber < esd->GetNumberOfTracks(); i_TrackNumber++)
-       {
-         AliESDtrack * ESDTrack = esd->GetTrack(i_TrackNumber);
-         UInt_t status = ESDTrack->GetStatus();
-         
-         //select only tracks with ITS refit
-         if ((status&AliESDtrack::kITSrefit)==0) continue;
-         
-         //select only tracks with TPC refit-->remove extremely high Pt tracks
-         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 P = 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 += P;
-         MeanPt += fPt;
-         if(fPt > MaxPt)
-           MaxPt = fPt;
-         
-         if(ESDTrack->GetSign() > 0)
-           {
-             Npos++;
-             if(fPt > 1.0)
-               Nch1GeV++;
-             if(fPt > 3.0)
-               Nch3GeV++;
-             if(fPt > 10.0)
-               Nch10GeV++;
-           }
-         if(ESDTrack->GetSign() < 0)
-           {
-             Nneg++;
-             if(fPt > 1.0)
-               Nch1GeV++;
-             if(fPt > 3.0)
-               Nch3GeV++;
-             if(fPt > 10.0)
-               Nch10GeV++;
-           }
-         if(ESDTrack->GetSign() == 0)
-           Nneutr++;
-         
-         //PID
-         Double_t prob[10];
-         ESDTrack->GetESDpid(prob);
-         
-         //K0s
-         if ((prob[8]>prob[7])&&(prob[8]>prob[6])&&(prob[8]>prob[5])&&(prob[8]>prob[4])&&(prob[8]>prob[3])&&(prob[8]>prob[2])&&(prob[8]>prob[1])&&(prob[8]>prob[0]))
-           NK0s++;
-         //neutrons
-         if ((prob[7]>prob[8])&&(prob[7]>prob[6])&&(prob[7]>prob[5])&&(prob[7]>prob[4])&&(prob[7]>prob[3])&&(prob[7]>prob[2])&&(prob[7]>prob[1])&&(prob[7]>prob[0]))
-           Nneutrons++; 
-         //pi0s
-         if ((prob[6]>prob[8])&&(prob[6]>prob[7])&&(prob[6]>prob[5])&&(prob[6]>prob[4])&&(prob[6]>prob[3])&&(prob[6]>prob[2])&&(prob[6]>prob[1])&&(prob[6]>prob[0]))
-           Npi0s++;
-         //gamas
-         if ((prob[5]>prob[8])&&(prob[5]>prob[7])&&(prob[5]>prob[6])&&(prob[5]>prob[4])&&(prob[5]>prob[3])&&(prob[5]>prob[2])&&(prob[5]>prob[1])&&(prob[5]>prob[0]))
-           Ngamas++;
-         //protons
-         if ((prob[4]>prob[8])&&(prob[4]>prob[7])&&(prob[4]>prob[6])&&(prob[4]>prob[5])&&(prob[4]>prob[3])&&(prob[4]>prob[2])&&(prob[4]>prob[1])&&(prob[4]>prob[0]))
-           NProtons++;
-         //kaons
-         if ((prob[3]>prob[8])&&(prob[3]>prob[7])&&(prob[3]>prob[6])&&(prob[3]>prob[5])&&(prob[3]>prob[4])&&(prob[3]>prob[2])&&(prob[3]>prob[1])&&(prob[3]>prob[0]))
-           NKaons++;
-         //pions
-         if ((prob[2]>prob[8])&&(prob[2]>prob[7])&&(prob[2]>prob[6])&&(prob[2]>prob[5])&&(prob[2]>prob[4])&&(prob[2]>prob[3])&&(prob[2]>prob[1])&&(prob[2]>prob[0]))
-           NPions++; 
-         //muons
-         if ((prob[1]>prob[8])&&(prob[1]>prob[7])&&(prob[1]>prob[6])&&(prob[1]>prob[5])&&(prob[1]>prob[4])&&(prob[1]>prob[3])&&(prob[1]>prob[2])&&(prob[1]>prob[0]))
-           {
-             NMuons++;
-             if(fPt > 1.0)
-               Nmu1GeV++;
-             if(fPt > 3.0)
-               Nmu3GeV++;
-             if(fPt > 10.0)
-               Nmu10GeV++;
-           }
-         //electrons
-         if ((prob[0]>prob[8])&&(prob[0]>prob[7])&&(prob[0]>prob[6])&&(prob[0]>prob[5])&&(prob[0]>prob[4])&&(prob[0]>prob[3])&&(prob[0]>prob[2])&&(prob[0]>prob[1]))
-           {
-             NElectrons++;
-             if(fPt > 1.0)
-               Nel1GeV++;
-             if(fPt > 3.0)
-               Nel3GeV++;
-             if(fPt > 10.0)
-               Nel10GeV++;
-           }
-         
-         
-         
-         ntrack++;
-       }//track loop
-      // Fill the event tags 
-      if(ntrack != 0)
-       MeanPt = MeanPt/ntrack;
-      
-      evTag->SetEventId(i_EventNumber+1);
-      evTag->SetGUID(guid);
-      evTag->SetVertexX(VertexIn->GetXv());
-      evTag->SetVertexY(VertexIn->GetYv());
-      evTag->SetVertexZ(VertexIn->GetZv());
-      
-      evTag->SetT0VertexZ(esd->GetT0zVertex());
-      
-      evTag->SetTrigger(esd->GetTrigger());
-      
-      evTag->SetZDCNeutronEnergy(esd->GetZDCNEnergy());
-      evTag->SetZDCProtonEnergy(esd->GetZDCPEnergy());
-      evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
-      evTag->SetNumOfParticipants(esd->GetZDCParticipants());
-      
-      
-      evTag->SetNumOfTracks(esd->GetNumberOfTracks());
-      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->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->SetNumOfPHOSTracks(esd->GetNumberOfPHOSParticles());
-      evTag->SetNumOfEMCALTracks(esd->GetNumberOfEMCALParticles());
-      
-      evTag->SetTotalMomentum(TotalP);
-      evTag->SetMeanPt(MeanPt);
-      evTag->SetMaxPt(MaxPt);
-  
-      tag->AddEventTag(evTag);
+  //  fgChain->Merge(filename);
+  MergeToSingleRunTag(fgChain, filename);
+
+  return kTRUE;
+}
+
+Bool_t AliTagCreator::MergeTagsForRun(const char* type, const char *inflist) {
+  //Merges the tags and stores the merged tag file 
+  //locally if fStorage=0 or in the grid if fStorage=1
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+  TString tagPattern = type; tagPattern += ".tag.root"; 
+
+//   if(fStorage == 0) {
+//     // 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)) fgChain->Add(name);  
+//     }//directory loop
+//     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
+//   }//local mode
+
+//   else if(fStorage == 1) {
+//     TString alienLocation = gGrid->Pwd();
+//     alienLocation += fgridpath.Data();
+//     alienLocation += "/";
+//     TString queryPattern = "*."; queryPattern += tagPattern;
+//     TGridResult *tagresult = gGrid->Query(alienLocation,queryPattern.Data(),"","");
+//     Int_t nEntries = tagresult->GetEntries();
+//     for(Int_t i = 0; i < nEntries; i++) {
+//       TString alienUrl = tagresult->GetKey(i,"turl");
+//       fgChain->Add(alienUrl);
+//     }//grid result loop      
+//     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
+//   }//grid mode
+
+  if (fStorage == 0) {
+    ifstream *istr = new ifstream(inflist);
+    char fname[200];
+    while (!(istr->eof())) {
+      (*istr) >> fname;
+      if (strstr(fname, tagPattern)) fgChain->Add(fname);
     }
-  lastEvent = i_NumberOfEvents;
+    AliInfo(Form("Chained tag files: %lld",fgChain->GetEntries()));
+  }
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged."; localFileName += tagPattern.Data();
+     
+  TString filename;
   
-  t->Delete("");
-       
-  ttag.Fill();
-  tag->Clear();
+  if(fStorage == 0) {
+    filename = localFileName.Data();      
+    AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
+  } 
+  else if(fStorage == 1) {
+    TString alienFileName = "/alien";
+    alienFileName += gGrid->Pwd();
+    alienFileName += fgridpath.Data();
+    alienFileName += "/";
+    alienFileName +=  localFileName;
+    alienFileName += "?se=";
+    alienFileName += fSE.Data();
+    filename = alienFileName.Data();
+    AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
+  }
+
+  //  fgChain->Merge(filename);
+  MergeToSingleRunTag(fgChain, filename);
+
+  gSystem->Exec("rm Run*.Event*");
 
-  TString LocalfileName = "Run"; LocalfileName += tag->GetRunId(); 
-  LocalfileName += ".Event"; LocalfileName += firstEvent; LocalfileName += "_"; LocalfileName += lastEvent; LocalfileName += "."; LocalfileName += Counter;
-  LocalfileName += ".ESD.tag.root";
+  return kTRUE;
+}
 
-  cout<<"Writing tags to local file: "<<LocalfileName<<endl;
 
-  TFile* ftag = TFile::Open(LocalfileName, "recreate");
-  ftag->cd();
-  ttag.Write();
-  ftag->Close();
+Bool_t AliTagCreator::MergeToSingleRunTag(TChain *chain, const char *filename)
+{
+  // Merge all tags for a given run into a single RunTag
+  TFile* ftag = TFile::Open(filename, "recreate");
 
-  delete ftag;
-  delete esd;
+  AliRunTag *tag = new AliRunTag;
+  TTree * ttag = new TTree("T","A Tree with event tags");
+  TBranch * btag = ttag->Branch("AliTAG", &tag, 1000000);
+  btag->SetCompressionLevel(9);
+  ttag->AutoSave("10000");
 
-  delete tag;
-  delete detTag;
-  delete evTag;
-}
+  AliRunTag *rtag = new AliRunTag();
+  chain->SetBranchAddress("AliTAG", &rtag);
 
-//_____________________________________________________________________________
-Bool_t AliTagCreator::StoreGridTagFile(const char *localpath, const char *gridpath)
-{
-  gSystem->Load("libThread.so");
-  gSystem->Load("libTreePlayer.so");
-  gSystem->Load("libProof.so");
-  cout<<"Storing tag file to alien's file catalog..."<<endl;
-  TFileMerger merger;
-  const char * pattern = "tag.root";
-  // Open the working directory
-  void * dirp = gSystem->OpenDirectory(localpath);
-  const char * name = 0x0;
-  TString AlienLocation;
-  Char_t LocalLocation[256];
-  // Add all files matching *pattern* to the chain
-  while((name = gSystem->GetDirEntry(dirp)))
-    {
-      if (strstr(name,pattern))
-       {
-         sprintf(LocalLocation,"file:%s/%s",localpath,name);
-         AlienLocation = "/alien";
-         AlienLocation += gGrid->Pwd();
-         AlienLocation += gridpath;
-         AlienLocation += "/";
-         AlienLocation += name;
-         AlienLocation += "?se=";
-         AlienLocation += fSE.Data();
-         merger.Cp(LocalLocation,AlienLocation);
-           
-       }
-    }  
-  gSystem->FreeDirectory(dirp);
-   
+  AliFileTag *evt;
+
+  if (chain->GetEntries()) {
+    chain->GetEntry(0);
+    tag->CopyStandardContent(rtag);
+    tag->Clear();
+
+    int runno = rtag->GetRunId();
+
+    for (int iter=0; iter<chain->GetEntries(); iter++) {
+      chain->GetEntry(iter);
+      if (runno != rtag->GetRunId()) {
+       AliInfo(Form("Run tag ID %i is different from the Run ID for the merged run: %i\n", rtag->GetRunId(), runno));
+       continue;
+      }
+
+      for (int iev=0; iev<rtag->GetNFiles(); iev++) {
+       evt = (AliFileTag *) rtag->GetFileTag(iev);
+       tag->AddFileTag(new AliFileTag(*evt));
+      }
+    }
+  }
+  else {
+    AliInfo("Found no tag files to merge.");
+    return kFALSE;
+  }
+
+  ttag->Fill();
+
+  ftag->cd();
+  tag->Clear();
+  ttag->Write();
+  ftag->Close();
+  
   return kTRUE;
 }
+
+