]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagCreator.cxx
Moving to the new ESD structure
[u/mrichter/AliRoot.git] / STEER / AliTagCreator.cxx
1 /**************************************************************************
2  * Author: Panos Christakoglou.                                           *
3  * Contributors are mentioned in the code where appropriate.              *
4  *                                                                        *
5  * Permission to use, copy, modify and distribute this software and its   *
6  * documentation strictly for non-commercial purposes is hereby granted   *
7  * without fee, provided that the above copyright notice appears in all   *
8  * copies and that both the copyright notice and this permission notice   *
9  * appear in the supporting documentation. The authors make no claims     *
10  * about the suitability of this software for any purpose. It is          *
11  * provided "as is" without express or implied warranty.                  *
12  **************************************************************************/
13
14 /* $Id$ */
15
16 //-----------------------------------------------------------------
17 //           AliTagCreator class
18 //   This is the class to deal with the tag creation (post process)
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <Riostream.h>
24 #include <TFile.h>
25 #include <TString.h>
26 #include <TTree.h>
27 #include <TSystem.h>
28 #include <TChain.h>
29 #include <TLorentzVector.h>
30
31 //ROOT-AliEn
32 #include <TGrid.h>
33 #include <TGridResult.h>
34
35 //AliRoot
36 #include "AliRunTag.h"
37 #include "AliEventTag.h"
38 #include "AliESD.h"
39 #include "AliESDEvent.h"
40 #include "AliESDVertex.h"
41 #include "AliLog.h"
42
43
44 #include "AliTagCreator.h"
45
46
47 ClassImp(AliTagCreator)
48
49
50 //______________________________________________________________________________
51   AliTagCreator::AliTagCreator() :
52     TObject(),
53     fSE("ALICE::CERN::se"),
54     fgridpath(""),
55     fStorage(0)
56 {
57   //==============Default constructor for a AliTagCreator==================
58 }
59
60 //______________________________________________________________________________
61 AliTagCreator::~AliTagCreator() {
62 //================Default destructor for a AliTagCreator=======================
63 }
64
65 //______________________________________________________________________________
66 void AliTagCreator::SetStorage(Int_t storage) {
67   // Sets correctly the storage: 0 for local, 1 for GRID
68   fStorage = storage;
69   if(fStorage == 0)
70     AliInfo(Form("Tags will be stored locally...."));
71   if(fStorage == 1)
72     AliInfo(Form("Tags will be stored in the grid...."));
73   if((fStorage != 0)&&(fStorage != 1))
74     {
75       AliInfo(Form("Storage was not properly set!!!"));
76       abort();
77     }  
78 }
79
80
81 //______________________________________________________________________________
82 Bool_t AliTagCreator::ReadGridCollection(TGridResult *fresult) {
83   // Reads the entry of the TGridResult and creates the tags
84   Int_t nEntries = fresult->GetEntries();
85
86   TString alienUrl;
87   const char *guid;
88   const char *md5;
89   const char *turl;
90   Long64_t size = -1;
91
92   Int_t counter = 0;
93   for(Int_t i = 0; i < nEntries; i++) {
94     alienUrl = fresult->GetKey(i,"turl");
95     guid = fresult->GetKey(i,"guid");
96     if(fresult->GetKey(i,"size")) size = atol (fresult->GetKey(i,"size"));
97     md5 = fresult->GetKey(i,"md5");
98     turl = fresult->GetKey(i,"turl");
99     if(md5 && !strlen(guid)) md5 = 0;
100     if(guid && !strlen(guid)) guid = 0;
101
102     TFile *f = TFile::Open(alienUrl,"READ");
103     CreateTag(f,guid,md5,turl,size,counter);
104     f->Close();
105     delete f;    
106     counter += 1;
107   }//grid result loop
108
109   return kTRUE;
110 }
111
112 //______________________________________________________________________________
113 Bool_t AliTagCreator::ReadLocalCollection(const char *localpath) {
114   // Checks the different subdirs of the given local path and in the
115   // case where it finds an AliESDs.root file it creates the tags
116   
117   void *dira =  gSystem->OpenDirectory(localpath);
118   Char_t fPath[256];
119   const char * dirname = 0x0;
120   const char * filename = 0x0;
121   const char * pattern = "AliESDs.root"; 
122
123   Int_t counter = 0;
124   while((dirname = gSystem->GetDirEntry(dira))) {
125     sprintf(fPath,"%s/%s",localpath,dirname);
126     void *dirb =  gSystem->OpenDirectory(fPath);
127     while((filename = gSystem->GetDirEntry(dirb))) {
128       if(strstr(filename,pattern)) {
129         TString fESDFileName;
130         fESDFileName = fPath;
131         fESDFileName += "/";
132         fESDFileName += pattern;
133         TFile *f = TFile::Open(fESDFileName,"READ");
134         CreateTag(f,fESDFileName,counter);
135         f->Close();
136         delete f;        
137         
138         counter += 1;
139       }//pattern check
140     }//child directory's entry loop
141   }//parent directory's entry loop
142
143   return kTRUE;
144 }
145
146 //______________________________________________________________________________
147 Bool_t AliTagCreator::ReadCAFCollection(const char *filename) {
148   // Temporary solution for CAF: Takes as an input the ascii file that
149   // lists the ESDs stored in the SE of the CAF and creates the tags.
150
151   // Open the input stream
152   ifstream in;
153   in.open(filename);
154
155   Int_t counter = 0;
156   TString esdfile;
157   // Read the input list of files and add them to the chain
158   while(in.good()) {
159     in >> esdfile;
160     if (!esdfile.Contains("root")) continue; // protection
161     TFile *f = TFile::Open(esdfile,"READ");
162     CreateTag(f,esdfile,counter);
163     f->Close();
164     delete f;    
165     
166     counter += 1;
167   }
168
169   return kTRUE;
170 }
171
172
173 //__________________________________________________________________________
174 Bool_t AliTagCreator::MergeTags() {
175   //Merges the tags and stores the merged tag file 
176   //locally if fStorage=0 or in the grid if fStorage=1
177   AliInfo(Form("Merging tags....."));
178   TChain *fgChain = new TChain("T");
179
180   if(fStorage == 0) {
181     const char * tagPattern = "tag";
182     // Open the working directory
183     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
184     const char * name = 0x0;
185     // Add all files matching *pattern* to the chain
186     while((name = gSystem->GetDirEntry(dirp))) {
187       if (strstr(name,tagPattern)) fgChain->Add(name);  
188     }//directory loop
189     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
190   }//local mode
191
192   else if(fStorage == 1) {
193     TString alienLocation = gGrid->Pwd();
194     alienLocation += fgridpath.Data();
195     alienLocation += "/";
196
197     TGridResult *tagresult = gGrid->Query(alienLocation,"*tag.root","","");
198     Int_t nEntries = tagresult->GetEntries();
199     for(Int_t i = 0; i < nEntries; i++) {
200       TString alienUrl = tagresult->GetKey(i,"turl");
201       fgChain->Add(alienUrl);
202     }//grid result loop      
203     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
204   }//grid mode
205  
206   AliRunTag *tag = new AliRunTag;
207   fgChain->SetBranchAddress("AliTAG",&tag);
208   fgChain->GetEntry(0);
209   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
210   localFileName += ".Merged"; localFileName += ".ESD.tag.root";
211      
212   TString filename = 0x0;
213   
214   if(fStorage == 0) {
215     filename = localFileName.Data();      
216     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
217   } 
218   else if(fStorage == 1) {
219     TString alienFileName = "/alien";
220     alienFileName += gGrid->Pwd();
221     alienFileName += fgridpath.Data();
222     alienFileName += "/";
223     alienFileName +=  localFileName;
224     alienFileName += "?se=";
225     alienFileName += fSE.Data();
226     filename = alienFileName.Data();
227     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
228   }
229
230   fgChain->Merge(filename);
231
232   return kTRUE;
233 }
234
235 //__________________________________________________________________________
236 Bool_t AliTagCreator::MergeTags(TGridResult *result) {
237   //Merges the tags that are listed in the TGridResult 
238   AliInfo(Form("Merging tags....."));
239   TChain *fgChain = new TChain("T");
240
241   Int_t nEntries = result->GetEntries();
242
243   TString alienUrl;
244   for(Int_t i = 0; i < nEntries; i++) {
245     alienUrl = result->GetKey(i,"turl");
246     fgChain->Add(alienUrl);  
247   }
248   AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
249   AliRunTag *tag = new AliRunTag;
250   fgChain->SetBranchAddress("AliTAG",&tag);
251   fgChain->GetEntry(0);
252     
253   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
254   localFileName += ".Merged"; localFileName += ".ESD.tag.root";
255      
256   TString filename = 0x0;
257   
258   if(fStorage == 0) {
259     filename = localFileName.Data();      
260     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
261   } 
262   else if(fStorage == 1) {
263     TString alienFileName = "/alien";
264     alienFileName += gGrid->Pwd();
265     alienFileName += fgridpath.Data();
266     alienFileName += "/";
267     alienFileName +=  localFileName;
268     alienFileName += "?se=";
269     alienFileName += fSE.Data();
270     filename = alienFileName.Data();
271     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
272   }
273   
274   fgChain->Merge(filename);
275
276   return kTRUE;
277 }
278
279 //_____________________________________________________________________________
280 void AliTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter) {
281   //private method that creates tag files
282   TString fguid = guid;
283   TString fmd5 = md5;
284   TString fturl = turl;
285   /////////////
286   //muon code//
287   ////////////
288   Double_t fMUONMASS = 0.105658369;
289   //Variables
290   Double_t fX,fY,fZ ;
291   Double_t fThetaX, fThetaY, fPyz, fChisquare;
292   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
293   Int_t fCharge;
294   TLorentzVector fEPvector;
295
296   Float_t fZVertexCut = 10.0; 
297   Float_t fRhoVertexCut = 2.0; 
298
299   Float_t fLowPtCut = 1.0;
300   Float_t fHighPtCut = 3.0;
301   Float_t fVeryHighPtCut = 10.0;
302   ////////////
303
304   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
305
306   // Creates the tags for all the events in a given ESD file
307   Bool_t fIsSim = kTRUE;
308   Int_t ntrack;
309   Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
310   Int_t nPos, nNeg, nNeutr;
311   Int_t nK0s, nNeutrons, nPi0s, nGamas;
312   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
313   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
314   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
315   Float_t maxPt = .0, meanPt = .0, totalP = .0;
316   Int_t fVertexflag;
317   Int_t iRunNumber = 0;
318   TString fVertexName;
319
320   AliRunTag *tag = new AliRunTag();
321   AliEventTag *evTag = new AliEventTag();
322   TTree ttag("T","A Tree with event tags");
323   TBranch * btag = ttag.Branch("AliTAG", &tag);
324   btag->SetCompressionLevel(9);
325   
326   AliInfo(Form("Creating the tags......."));    
327   
328   Int_t firstEvent = 0,lastEvent = 0;
329   TTree *t = (TTree*) file->Get("esdTree");
330   AliESDEvent *esd = new AliESDEvent();
331   esd->ReadFromTree(t);
332   
333   t->GetEntry(0);
334   Int_t iInitRunNumber = esd->GetRunNumber();
335
336   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
337   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
338     ntrack = 0;
339     nPos = 0;
340     nNeg = 0;
341     nNeutr =0;
342     nK0s = 0;
343     nNeutrons = 0;
344     nPi0s = 0;
345     nGamas = 0;
346     nProtons = 0;
347     nKaons = 0;
348     nPions = 0;
349     nMuons = 0;
350     nElectrons = 0;       
351     nCh1GeV = 0;
352     nCh3GeV = 0;
353     nCh10GeV = 0;
354     nMu1GeV = 0;
355     nMu3GeV = 0;
356     nMu10GeV = 0;
357     nEl1GeV = 0;
358     nEl3GeV = 0;
359     nEl10GeV = 0;
360     maxPt = .0;
361     meanPt = .0;
362     totalP = .0;
363     fVertexflag = 1;
364     
365     t->GetEntry(iEventNumber);
366     iRunNumber = esd->GetRunNumber();
367     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
368     const AliESDVertex * vertexIn = esd->GetVertex();
369     fVertexName = vertexIn->GetName();
370     if(fVertexName == "default") fVertexflag = 0;
371
372     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
373       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
374       if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
375       else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
376       UInt_t status = esdTrack->GetStatus();
377       
378       //select only tracks with ITS refit
379       if ((status&AliESDtrack::kITSrefit)==0) continue;
380       //select only tracks with TPC refit
381       if ((status&AliESDtrack::kTPCrefit)==0) continue;
382       
383       //select only tracks with the "combined PID"
384       if ((status&AliESDtrack::kESDpid)==0) continue;
385       Double_t p[3];
386       esdTrack->GetPxPyPz(p);
387       Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
388       Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
389       totalP += momentum;
390       meanPt += fPt;
391       if(fPt > maxPt) maxPt = fPt;
392       
393       if(esdTrack->GetSign() > 0) {
394         nPos++;
395         if(fPt > fLowPtCut) nCh1GeV++;
396         if(fPt > fHighPtCut) nCh3GeV++;
397         if(fPt > fVeryHighPtCut) nCh10GeV++;
398       }
399       if(esdTrack->GetSign() < 0) {
400         nNeg++;
401         if(fPt > fLowPtCut) nCh1GeV++;
402         if(fPt > fHighPtCut) nCh3GeV++;
403         if(fPt > fVeryHighPtCut) nCh10GeV++;
404       }
405       if(esdTrack->GetSign() == 0) nNeutr++;
406       
407       //PID
408       Double_t prob[5];
409       esdTrack->GetESDpid(prob);
410       
411       Double_t rcc = 0.0;
412       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
413       if(rcc == 0.0) continue;
414       //Bayes' formula
415       Double_t w[5];
416       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
417       
418       //protons
419       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
420       //kaons
421       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
422       //pions
423       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
424       //electrons
425       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
426         nElectrons++;
427         if(fPt > fLowPtCut) nEl1GeV++;
428         if(fPt > fHighPtCut) nEl3GeV++;
429         if(fPt > fVeryHighPtCut) nEl10GeV++;
430       }   
431       ntrack++;
432     }//esd track loop
433     
434     /////////////
435     //muon code//
436     ////////////
437     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
438     // loop over all reconstructed tracks (also first track of combination)
439     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
440       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
441       if (muonTrack == 0x0) continue;
442       
443       // Coordinates at vertex
444       fZ = muonTrack->GetZ(); 
445       fY = muonTrack->GetBendingCoor();
446       fX = muonTrack->GetNonBendingCoor(); 
447       
448       fThetaX = muonTrack->GetThetaX();
449       fThetaY = muonTrack->GetThetaY();
450       
451       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
452       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
453       fPxRec = fPzRec * TMath::Tan(fThetaX);
454       fPyRec = fPzRec * TMath::Tan(fThetaY);
455       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
456       
457       //ChiSquare of the track if needed
458       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
459       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
460       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
461       
462       // total number of muons inside a vertex cut 
463       if((TMath::Abs(fZ)<fZVertexCut) && (TMath::Sqrt(fY*fY+fX*fX)<fRhoVertexCut)) {
464         nMuons++;
465         if(fEPvector.Pt() > fLowPtCut) {
466           nMu1GeV++; 
467           if(fEPvector.Pt() > fHighPtCut) {
468             nMu3GeV++; 
469             if (fEPvector.Pt() > fVeryHighPtCut) {
470               nMu10GeV++;
471             }
472           }
473         }
474       }
475     }//muon track loop
476     
477     // Fill the event tags 
478     if(ntrack != 0) meanPt = meanPt/ntrack;
479     
480     evTag->SetEventId(iEventNumber+1);
481     evTag->SetGUID(fguid);
482     evTag->SetMD5(fmd5);
483     evTag->SetTURL(fturl);
484     evTag->SetSize(size);
485     evTag->SetVertexX(vertexIn->GetXv());
486     evTag->SetVertexY(vertexIn->GetYv());
487     evTag->SetVertexZ(vertexIn->GetZv());
488     evTag->SetVertexZError(vertexIn->GetZRes());
489     evTag->SetVertexFlag(fVertexflag);
490     
491     evTag->SetT0VertexZ(esd->GetT0zVertex());
492     
493     evTag->SetTriggerMask(esd->GetTriggerMask());
494     evTag->SetTriggerCluster(esd->GetTriggerCluster());
495     
496     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
497     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
498     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
499     evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
500     evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
501     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
502     
503     
504     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
505     evTag->SetNumOfPosTracks(nPos);
506     evTag->SetNumOfNegTracks(nNeg);
507     evTag->SetNumOfNeutrTracks(nNeutr);
508     
509     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
510     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
511     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
512     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
513     
514     evTag->SetNumOfProtons(nProtons);
515     evTag->SetNumOfKaons(nKaons);
516     evTag->SetNumOfPions(nPions);
517     evTag->SetNumOfMuons(nMuons);
518     evTag->SetNumOfElectrons(nElectrons);
519     evTag->SetNumOfPhotons(nGamas);
520     evTag->SetNumOfPi0s(nPi0s);
521     evTag->SetNumOfNeutrons(nNeutrons);
522     evTag->SetNumOfKaon0s(nK0s);
523     
524     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
525     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
526     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
527     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
528     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
529     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
530     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
531     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
532     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
533     
534     evTag->SetNumOfPHOSClusters(esd->GetNumberOfPHOSClusters());
535     evTag->SetNumOfEMCALClusters(esd->GetNumberOfEMCALClusters());
536     
537     evTag->SetTotalMomentum(totalP);
538     evTag->SetMeanPt(meanPt);
539     evTag->SetMaxPt(maxPt);
540     
541     tag->SetRunId(iInitRunNumber);
542     if(fIsSim) tag->SetDataType(0);
543     else tag->SetDataType(1);
544     tag->AddEventTag(*evTag);
545   }//event loop
546   lastEvent = iNumberOfEvents;
547   
548   t->Delete("");
549   
550   ttag.Fill();
551   tag->Clear();
552
553   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
554   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
555   localFileName += ".ESD.tag.root";
556
557   TString fileName;
558   
559   if(fStorage == 0) {
560     fileName = localFileName.Data();      
561     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
562   }
563   else if(fStorage == 1) {
564     TString alienLocation = "/alien";
565     alienLocation += gGrid->Pwd();
566     alienLocation += fgridpath.Data();
567     alienLocation += "/";
568     alienLocation +=  localFileName;
569     alienLocation += "?se=";
570     alienLocation += fSE.Data();
571     fileName = alienLocation.Data();
572     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
573   }
574
575   TFile* ftag = TFile::Open(fileName, "recreate");
576   ftag->cd();
577   ttag.Write();
578   ftag->Close();
579
580   delete ftag;
581   delete esd;
582
583   delete tag;
584 }
585
586 //_____________________________________________________________________________
587 void AliTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter) {
588   //private method that creates tag files
589   /////////////
590   //muon code//
591   ////////////
592   Double_t fMUONMASS = 0.105658369;
593   //Variables
594   Double_t fX,fY,fZ ;
595   Double_t fThetaX, fThetaY, fPyz, fChisquare;
596   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
597   Int_t fCharge;
598   TLorentzVector fEPvector;
599
600   Float_t fZVertexCut = 10.0; 
601   Float_t fRhoVertexCut = 2.0; 
602
603   Float_t fLowPtCut = 1.0;
604   Float_t fHighPtCut = 3.0;
605   Float_t fVeryHighPtCut = 10.0;
606   ////////////
607
608   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
609
610   // Creates the tags for all the events in a given ESD file
611   Bool_t fIsSim = kTRUE;
612   Int_t ntrack;
613   Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
614   Int_t nPos, nNeg, nNeutr;
615   Int_t nK0s, nNeutrons, nPi0s, nGamas;
616   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
617   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
618   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
619   Float_t maxPt = .0, meanPt = .0, totalP = .0;
620   Int_t fVertexflag;
621   Int_t iRunNumber = 0;
622   TString fVertexName;
623
624   AliRunTag *tag = new AliRunTag();
625   AliEventTag *evTag = new AliEventTag();
626   TTree ttag("T","A Tree with event tags");
627   TBranch * btag = ttag.Branch("AliTAG", &tag);
628   btag->SetCompressionLevel(9);
629   
630   AliInfo(Form("Creating the tags......."));    
631   
632   Int_t firstEvent = 0,lastEvent = 0;
633   
634   TTree *t = (TTree*) file->Get("esdTree");
635   AliESDEvent *esd = new AliESDEvent();
636   esd->ReadFromTree(t);
637   
638   t->GetEntry(0);
639   Int_t iInitRunNumber = esd->GetRunNumber();
640
641   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
642   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
643     ntrack = 0;
644     nPos = 0;
645     nNeg = 0;
646     nNeutr =0;
647     nK0s = 0;
648     nNeutrons = 0;
649     nPi0s = 0;
650     nGamas = 0;
651     nProtons = 0;
652     nKaons = 0;
653     nPions = 0;
654     nMuons = 0;
655     nElectrons = 0;       
656     nCh1GeV = 0;
657     nCh3GeV = 0;
658     nCh10GeV = 0;
659     nMu1GeV = 0;
660     nMu3GeV = 0;
661     nMu10GeV = 0;
662     nEl1GeV = 0;
663     nEl3GeV = 0;
664     nEl10GeV = 0;
665     maxPt = .0;
666     meanPt = .0;
667     totalP = .0;
668     fVertexflag = 1;
669     
670     t->GetEntry(iEventNumber);
671     iRunNumber = esd->GetRunNumber();
672     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
673     const AliESDVertex * vertexIn = esd->GetVertex();
674     fVertexName = vertexIn->GetName();
675     if(fVertexName == "default") fVertexflag = 0;
676
677     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
678       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
679       if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
680       else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
681       UInt_t status = esdTrack->GetStatus();
682       
683       //select only tracks with ITS refit
684       if ((status&AliESDtrack::kITSrefit)==0) continue;
685       //select only tracks with TPC refit
686       if ((status&AliESDtrack::kTPCrefit)==0) continue;
687       
688       //select only tracks with the "combined PID"
689       if ((status&AliESDtrack::kESDpid)==0) continue;
690       Double_t p[3];
691       esdTrack->GetPxPyPz(p);
692       Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
693       Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
694       totalP += momentum;
695       meanPt += fPt;
696       if(fPt > maxPt) maxPt = fPt;
697       
698       if(esdTrack->GetSign() > 0) {
699         nPos++;
700         if(fPt > fLowPtCut) nCh1GeV++;
701         if(fPt > fHighPtCut) nCh3GeV++;
702         if(fPt > fVeryHighPtCut) nCh10GeV++;
703       }
704       if(esdTrack->GetSign() < 0) {
705         nNeg++;
706         if(fPt > fLowPtCut) nCh1GeV++;
707         if(fPt > fHighPtCut) nCh3GeV++;
708         if(fPt > fVeryHighPtCut) nCh10GeV++;
709       }
710       if(esdTrack->GetSign() == 0) nNeutr++;
711       
712       //PID
713       Double_t prob[5];
714       esdTrack->GetESDpid(prob);
715       
716       Double_t rcc = 0.0;
717       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
718       if(rcc == 0.0) continue;
719       //Bayes' formula
720       Double_t w[5];
721       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
722       
723       //protons
724       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
725       //kaons
726       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
727       //pions
728       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
729       //electrons
730       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
731         nElectrons++;
732         if(fPt > fLowPtCut) nEl1GeV++;
733         if(fPt > fHighPtCut) nEl3GeV++;
734         if(fPt > fVeryHighPtCut) nEl10GeV++;
735       }   
736       ntrack++;
737     }//esd track loop
738     
739     /////////////
740     //muon code//
741     ////////////
742     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
743     // loop over all reconstructed tracks (also first track of combination)
744     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
745       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
746       if (muonTrack == 0x0) continue;
747       
748       // Coordinates at vertex
749       fZ = muonTrack->GetZ(); 
750       fY = muonTrack->GetBendingCoor();
751       fX = muonTrack->GetNonBendingCoor(); 
752       
753       fThetaX = muonTrack->GetThetaX();
754       fThetaY = muonTrack->GetThetaY();
755       
756       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
757       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
758       fPxRec = fPzRec * TMath::Tan(fThetaX);
759       fPyRec = fPzRec * TMath::Tan(fThetaY);
760       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
761       
762       //ChiSquare of the track if needed
763       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
764       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
765       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
766       
767       // total number of muons inside a vertex cut 
768       if((TMath::Abs(fZ)<fZVertexCut) && (TMath::Sqrt(fY*fY+fX*fX)<fRhoVertexCut)) {
769         nMuons++;
770         if(fEPvector.Pt() > fLowPtCut) {
771           nMu1GeV++; 
772           if(fEPvector.Pt() > fHighPtCut) {
773             nMu3GeV++; 
774             if (fEPvector.Pt() > fVeryHighPtCut) {
775               nMu10GeV++;
776             }
777           }
778         }
779       }
780     }//muon track loop
781     
782     // Fill the event tags 
783     if(ntrack != 0) meanPt = meanPt/ntrack;
784     
785     evTag->SetEventId(iEventNumber+1);
786     evTag->SetPath(filepath);
787  
788     evTag->SetVertexX(vertexIn->GetXv());
789     evTag->SetVertexY(vertexIn->GetYv());
790     evTag->SetVertexZ(vertexIn->GetZv());
791     evTag->SetVertexZError(vertexIn->GetZRes());
792     evTag->SetVertexFlag(fVertexflag);
793     
794     evTag->SetT0VertexZ(esd->GetT0zVertex());
795     
796     evTag->SetTriggerMask(esd->GetTriggerMask());
797     evTag->SetTriggerCluster(esd->GetTriggerCluster());
798     
799     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
800     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
801     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
802     evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
803     evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
804     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
805     
806     
807     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
808     evTag->SetNumOfPosTracks(nPos);
809     evTag->SetNumOfNegTracks(nNeg);
810     evTag->SetNumOfNeutrTracks(nNeutr);
811     
812     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
813     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
814     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
815     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
816     
817     evTag->SetNumOfProtons(nProtons);
818     evTag->SetNumOfKaons(nKaons);
819     evTag->SetNumOfPions(nPions);
820     evTag->SetNumOfMuons(nMuons);
821     evTag->SetNumOfElectrons(nElectrons);
822     evTag->SetNumOfPhotons(nGamas);
823     evTag->SetNumOfPi0s(nPi0s);
824     evTag->SetNumOfNeutrons(nNeutrons);
825     evTag->SetNumOfKaon0s(nK0s);
826     
827     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
828     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
829     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
830     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
831     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
832     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
833     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
834     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
835     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
836     
837     evTag->SetNumOfPHOSClusters(esd->GetNumberOfPHOSClusters());
838     evTag->SetNumOfEMCALClusters(esd->GetNumberOfEMCALClusters());
839     
840     evTag->SetTotalMomentum(totalP);
841     evTag->SetMeanPt(meanPt);
842     evTag->SetMaxPt(maxPt);
843     
844     tag->SetRunId(iInitRunNumber);
845     if(fIsSim) tag->SetDataType(0);
846     else tag->SetDataType(1);
847     tag->AddEventTag(*evTag);
848   }//event loop
849   lastEvent = iNumberOfEvents;
850   
851   t->Delete("");
852   
853   ttag.Fill();
854   tag->Clear();
855
856   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
857   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
858   localFileName += ".ESD.tag.root";
859
860   TString fileName;
861   
862   if(fStorage == 0) {
863     fileName = localFileName.Data();      
864     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
865   }
866   else if(fStorage == 1) {
867     TString alienLocation = "/alien";
868     alienLocation += gGrid->Pwd();
869     alienLocation += fgridpath.Data();
870     alienLocation += "/";
871     alienLocation +=  localFileName;
872     alienLocation += "?se=";
873     alienLocation += fSE.Data();
874     fileName = alienLocation.Data();
875     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
876   }
877
878   TFile* ftag = TFile::Open(fileName, "recreate");
879   ftag->cd();
880   ttag.Write();
881   ftag->Close();
882
883   delete ftag;
884   delete esd;
885
886   delete tag;
887 }