]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliTagCreator.cxx
Replacing obsolete classes
[u/mrichter/AliRoot.git] / STEER / AliTagCreator.cxx
index a9d8c045a84a72efdc46ed33720709ce2d4a1a59..5525e5ceea94b7278c02ceb56fc29c7b6ddabfcc 100644 (file)
@@ -25,7 +25,6 @@
 #include <TString.h>
 #include <TTree.h>
 #include <TSystem.h>
-#include <TSystemDirectory.h>
 #include <TChain.h>
 #include <TLorentzVector.h>
 
 #include "AliRunTag.h"
 #include "AliEventTag.h"
 #include "AliESD.h"
+#include "AliESDEvent.h"
 #include "AliESDVertex.h"
 #include "AliLog.h"
 
+#include "AliAODEvent.h"
+#include "AliAODVertex.h"
+#include "AliAODTrack.h"
 
 #include "AliTagCreator.h"
 
@@ -48,11 +51,13 @@ ClassImp(AliTagCreator)
 
 
 //______________________________________________________________________________
-AliTagCreator::AliTagCreator() {
+  AliTagCreator::AliTagCreator() :
+    TObject(),
+    fSE("ALICE::CERN::se"),
+    fgridpath(""),
+    fStorage(0)
+{
   //==============Default constructor for a AliTagCreator==================
-  fgridpath = "";
-  fSE = "";   
-  fStorage = 0; 
 }
 
 //______________________________________________________________________________
@@ -119,10 +124,10 @@ Bool_t AliTagCreator::ReadLocalCollection(const char *localpath) {
   const char * pattern = "AliESDs.root"; 
 
   Int_t counter = 0;
-  while(dirname = gSystem->GetDirEntry(dira)) {
+  while((dirname = gSystem->GetDirEntry(dira))) {
     sprintf(fPath,"%s/%s",localpath,dirname);
     void *dirb =  gSystem->OpenDirectory(fPath);
-    while(filename = gSystem->GetDirEntry(dirb)) {
+    while((filename = gSystem->GetDirEntry(dirb))) {
       if(strstr(filename,pattern)) {
        TString fESDFileName;
        fESDFileName = fPath;
@@ -182,8 +187,7 @@ Bool_t AliTagCreator::MergeTags() {
     const char * name = 0x0;
     // Add all files matching *pattern* to the chain
     while((name = gSystem->GetDirEntry(dirp))) {
-      if (strstr(name,tagPattern))       
-       fgChain->Add(name);  
+      if (strstr(name,tagPattern)) fgChain->Add(name);  
     }//directory loop
     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
   }//local mode
@@ -203,26 +207,52 @@ Bool_t AliTagCreator::MergeTags() {
   }//grid mode
  
   AliRunTag *tag = new AliRunTag;
-  AliEventTag *evTag = new AliEventTag;
   fgChain->SetBranchAddress("AliTAG",&tag);
-   
-  //Defining new tag objects
-  AliRunTag *newTag = new AliRunTag();
-  TTree ttag("T","A Tree with event tags");
-  TBranch * btag = ttag.Branch("AliTAG", &newTag);
-  btag->SetCompressionLevel(9);
-  for(Int_t iTagFiles = 0; iTagFiles < fgChain->GetEntries(); iTagFiles++) {
-    fgChain->GetEntry(iTagFiles);
-    newTag->SetRunId(tag->GetRunId());
-    const TClonesArray *tagList = tag->GetEventTags();
-    for(Int_t j = 0; j < tagList->GetEntries(); j++) {
-      evTag = (AliEventTag *) tagList->At(j);
-      newTag->AddEventTag(*evTag);
-    }
-    ttag.Fill();
-    newTag->Clear();
-  }//tag file loop 
+  fgChain->GetEntry(0);
+  TString localFileName = "Run"; localFileName += tag->GetRunId(); 
+  localFileName += ".Merged"; localFileName += ".ESD.tag.root";
+     
+  TString filename = 0x0;
   
+  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);
+
+  return kTRUE;
+}
+
+//__________________________________________________________________________
+Bool_t AliTagCreator::MergeTags(TGridResult *result) {
+  //Merges the tags that are listed in the TGridResult 
+  AliInfo(Form("Merging tags....."));
+  TChain *fgChain = new TChain("T");
+
+  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: %d",fgChain->GetEntries()));
+  AliRunTag *tag = new AliRunTag;
+  fgChain->SetBranchAddress("AliTAG",&tag);
+  fgChain->GetEntry(0);
+    
   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
   localFileName += ".Merged"; localFileName += ".ESD.tag.root";
      
@@ -244,13 +274,7 @@ Bool_t AliTagCreator::MergeTags() {
     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
   }
   
-  TFile* ftag = TFile::Open(filename, "recreate");
-  ftag->cd();
-  ttag.Write();
-  ftag->Close();
-
-  delete tag;
-  delete newTag;
+  fgChain->Merge(filename);
 
   return kTRUE;
 }
@@ -306,14 +330,13 @@ void AliTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, co
   
   Int_t firstEvent = 0,lastEvent = 0;
   TTree *t = (TTree*) file->Get("esdTree");
-  TBranch * b = t->GetBranch("ESD");
-  AliESD *esd = 0;
-  b->SetAddress(&esd);
+  AliESDEvent *esd = new AliESDEvent();
+  esd->ReadFromTree(t);
   
-  b->GetEntry(0);
+  t->GetEntry(0);
   Int_t iInitRunNumber = esd->GetRunNumber();
 
-  Int_t iNumberOfEvents = b->GetEntries();
+  Int_t iNumberOfEvents = (Int_t)t->GetEntries();
   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
     ntrack = 0;
     nPos = 0;
@@ -342,7 +365,7 @@ void AliTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, co
     totalP = .0;
     fVertexflag = 1;
     
-    b->GetEntry(iEventNumber);
+    t->GetEntry(iEventNumber);
     iRunNumber = esd->GetRunNumber();
     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
     const AliESDVertex * vertexIn = esd->GetVertex();
@@ -612,14 +635,13 @@ void AliTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter)
   Int_t firstEvent = 0,lastEvent = 0;
   
   TTree *t = (TTree*) file->Get("esdTree");
-  TBranch * b = t->GetBranch("ESD");
-  AliESD *esd = 0;
-  b->SetAddress(&esd);
+  AliESDEvent *esd = new AliESDEvent();
+  esd->ReadFromTree(t);
   
-  b->GetEntry(0);
+  t->GetEntry(0);
   Int_t iInitRunNumber = esd->GetRunNumber();
 
-  Int_t iNumberOfEvents = b->GetEntries();
+  Int_t iNumberOfEvents = (Int_t)t->GetEntries();
   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
     ntrack = 0;
     nPos = 0;
@@ -648,7 +670,7 @@ void AliTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter)
     totalP = .0;
     fVertexflag = 1;
     
-    b->GetEntry(iEventNumber);
+    t->GetEntry(iEventNumber);
     iRunNumber = esd->GetRunNumber();
     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
     const AliESDVertex * vertexIn = esd->GetVertex();
@@ -866,3 +888,214 @@ void AliTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter)
 
   delete tag;
 }
+
+//__________________________________________________________________________
+void AliTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent) {
+  //creates tag files for AODs
+  
+  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 ESD 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();
+
+  cout<<"Creating the tags......."<<endl;      
+
+  TFile *file = TFile::Open("AliAOD.root");
+  if (!file || !file->IsOpen()) {
+    AliError(Form("opening failed"));
+    delete file;
+    return ;
+  }
+  TTree *aodTree = (TTree*)file->Get("AOD");
+  AliAODEvent *aod = (AliAODEvent*)aodTree->GetUserInfo()->FindObject("AliAODEvent");
+  TIter next(aod->GetList());
+  TObject *el;
+
+  Int_t lastEvent = 0;  
+  if(fLastEvent == -1) lastEvent = (Int_t)aodTree->GetEntries();
+  else lastEvent = fLastEvent;
+
+  while((el=(TNamed*)next())) 
+    aodTree->SetBranchAddress(el->GetName(),aod->GetList()->GetObjectRef(el));
+  
+  // 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();
+    cout << "Event: " << iEventNumber+1 << "/" << nEvents <<" - Tracks: "<<nTracks<< endl;
+    // 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);
+      
+      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;
+      
+      //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++;
+       }
+      //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;
+
+    evTag->SetEventId(iEventNumber+1);
+        
+    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
+  if(fLastEvent == -1) lastEvent = (Int_t)aodTree->GetEntries();
+  else lastEvent = fLastEvent;
+
+  ttag.Fill();
+  tag->Clear();
+
+  char fileName[256];
+  sprintf(fileName, "Run%d.Event%d_%d.AOD.tag.root", 
+         tag->GetRunId(),fFirstEvent,lastEvent );
+  cout<<"writing tags to file "<<fileName<<endl;
+  AliDebug(1, Form("writing tags to file %s", fileName));
+  TFile* ftag = TFile::Open(fileName, "recreate");
+  ftag->cd();
+  ttag.Write();
+  ftag->Close();
+  file->cd();
+  file->Close();
+}