dd386603f33f0df9f99145cad704bc7d41d63096
[u/mrichter/AliRoot.git] / STEER / AliESDTagCreator.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 //           AliESDTagCreator 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 <TList.h>
30 #include <TObjString.h>
31 #include <TLorentzVector.h>
32 #include <TMap.h>
33 #include <TTimeStamp.h>
34 #include <TRefArray.h>
35
36 //ROOT-AliEn
37 #include <TGrid.h>
38 #include <TGridResult.h>
39
40 //AliRoot
41 #include "AliRunTag.h"
42 #include "AliEventTag.h"
43 #include "AliESD.h"
44 #include "AliESDEvent.h"
45 #include "AliESDVertex.h"
46 #include "AliLog.h"
47 #include "AliGRPObject.h"
48
49 #include "AliESDTagCreator.h"
50
51
52 ClassImp(AliESDTagCreator)
53
54
55 //______________________________________________________________________________
56   AliESDTagCreator::AliESDTagCreator() :
57     AliTagCreator(),
58     fChain(new TChain("esdTree")), fGUIDList(new TList()), 
59     fMD5List(new TList()), fTURLList(new TList()), fBranches(""), 
60     meminfo(new MemInfo_t) {
61   //==============Default constructor for a AliESDTagCreator================
62 }
63
64 //______________________________________________________________________________
65 AliESDTagCreator::~AliESDTagCreator() {
66 //================Default destructor for a AliESDTagCreator===================
67   delete fChain;
68   delete fGUIDList;
69   delete fMD5List;
70   delete fTURLList;
71   delete meminfo;
72 }
73
74 //______________________________________________________________________________
75 Bool_t AliESDTagCreator::ReadGridCollection(TGridResult *fresult) {
76   // Reads the entry of the TGridResult and creates the tags
77   Int_t nEntries = fresult->GetEntries();
78
79   TString alienUrl;
80   const char* guid;
81   const char* md5;
82   const char* turl;
83   Long64_t size = -1;
84
85   Int_t counter = 0;
86   for(Int_t i = 0; i < nEntries; i++) {
87     alienUrl = fresult->GetKey(i,"turl");
88     guid = fresult->GetKey(i,"guid");
89     if(fresult->GetKey(i,"size")) size = atol (fresult->GetKey(i,"size"));
90     md5 = fresult->GetKey(i,"md5");
91     turl = fresult->GetKey(i,"turl");
92     if(md5 && !strlen(guid)) md5 = 0;
93     if(guid && !strlen(guid)) guid = 0;
94     
95     fChain->Add(alienUrl);
96     //fGUIDList->Add(new TObjString(guid));
97     //fMD5List->Add(new TObjString(md5));
98     //fTURLList->Add(new TObjString(turl));
99     
100     //TFile *f = TFile::Open(alienUrl,"READ");
101     //CreateTag(f,guid,md5,turl,size,counter);
102     //f->Close();
103     //delete f;  
104     counter += 1;
105   }//grid result loop
106   
107   if (fChain->GetEntries() > 0) {
108     AliInfo(Form("ESD chain created......."));  
109     AliInfo(Form("Chain entries: %l",fChain->GetEntries()));    
110   } else {
111     AliWarning(Form("No ESD files found !"));
112     return kFALSE;
113   }
114     
115   // Switch of branches on user request
116   SwitchOffBranches();
117   CreateTag(fChain,"grid");
118   
119   return kTRUE;
120 }
121
122 //______________________________________________________________________________
123 Bool_t AliESDTagCreator::ReadLocalCollection(const char *localpath) {
124   // Checks the different subdirs of the given local path and in the
125   // case where it finds an AliESDs.root file it creates the tags
126   
127   void *dira =  gSystem->OpenDirectory(localpath);
128   Char_t fPath[256];
129   const char * dirname = 0x0;
130   const char * filename = 0x0;
131   const char * pattern = "AliESDs.root"; 
132
133   Int_t counter = 0;
134   while((dirname = gSystem->GetDirEntry(dira))) {
135     sprintf(fPath,"%s/%s",localpath,dirname);
136     void *dirb =  gSystem->OpenDirectory(fPath);
137     while((filename = gSystem->GetDirEntry(dirb))) {
138       if(strstr(filename,pattern)) {
139         TString fESDFileName;
140         fESDFileName = fPath;
141         fESDFileName += "/";
142         fESDFileName += pattern;
143
144         fChain->Add(fESDFileName);
145
146         //TFile *f = TFile::Open(fESDFileName,"READ");
147         //CreateTag(f,fESDFileName,counter);
148         //f->Close();
149         //delete f;      
150         
151         counter += 1;
152       }//pattern check
153     }//child directory's entry loop
154   }//parent directory's entry loop
155
156   AliInfo(Form("ESD chain created......."));    
157   AliInfo(Form("Chain entries: %l",fChain->GetEntries()));      
158   // Switch of branches on user request
159   SwitchOffBranches();
160   CreateTag(fChain,"local");
161
162   return kTRUE;
163 }
164
165 //______________________________________________________________________________
166 Bool_t AliESDTagCreator::ReadCAFCollection(const char *filename) {
167   // Temporary solution for CAF: Takes as an input the ascii file that
168   // lists the ESDs stored in the SE of the CAF and creates the tags.
169
170   // Open the input stream
171   ifstream in;
172   in.open(filename);
173
174   Int_t counter = 0;
175   TString esdfile;
176   // Read the input list of files and add them to the chain
177   while(in.good()) {
178     in >> esdfile;
179     if (!esdfile.Contains("root")) continue; // protection
180
181     fChain->Add(esdfile);
182   
183     //TFile *f = TFile::Open(esdfile,"READ");
184     //CreateTag(f,esdfile,counter);
185     //f->Close();
186     //delete f;  
187     
188     counter += 1;
189   }
190
191   AliInfo(Form("ESD chain created......."));    
192   AliInfo(Form("Chain entries: %l",fChain->GetEntries()));      
193   // Switch of branches on user request
194   SwitchOffBranches();
195   CreateTag(fChain,"proof");
196
197   return kTRUE;
198 }
199
200 //_____________________________________________________________________________
201 void AliESDTagCreator::CreateTag(TChain* chain, const char *type) {
202   //private method that creates tag files
203   TString fSession = type;
204   TString fguid, fmd5, fturl;
205   TString fTempGuid;
206
207   Int_t iRunNumber = 0;
208   //gSystem->GetMemInfo(meminfo);
209   //AliInfo(Form("After the tag initialization - Memory used: %d MB",meminfo->fMemUsed));
210   //Int_t tempmem = meminfo->fMemUsed;
211   
212   AliInfo(Form("Creating the ESD tags......."));        
213   
214   Int_t firstEvent = 0,lastEvent = 0;
215   AliESDEvent *esd = new AliESDEvent();
216   esd->ReadFromTree(chain);
217   //  AliESD *esdold = 0x0;
218   
219   //gSystem->GetMemInfo(meminfo);
220   //AliInfo(Form("After the esd initialization - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
221   //tempmem = meminfo->fMemUsed;
222   
223   Int_t iInitRunNumber = -1;
224   chain->GetEntry(0);
225   TFile *f = chain->GetFile();
226   fTempGuid = f->GetUUID().AsString();
227
228   TString localFileName = "Run"; localFileName += esd->GetRunNumber(); 
229   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += chain->GetEntries(); //localFileName += "."; localFileName += Counter;
230   localFileName += ".ESD.tag.root";
231
232   TString fileName;
233   TRefArray tmp;
234  
235   if(fStorage == 0) {
236     fileName = localFileName.Data();      
237     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
238   }
239   else if(fStorage == 1) {
240     TString alienLocation = "/alien";
241     alienLocation += gGrid->Pwd();
242     alienLocation += fgridpath.Data();
243     alienLocation += "/";
244     alienLocation +=  localFileName;
245     alienLocation += "?se=";
246     alienLocation += fSE.Data();
247     fileName = alienLocation.Data();
248     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
249   }
250
251   TFile* ftag = TFile::Open(fileName, "recreate");
252
253   AliRunTag *tag = new AliRunTag();
254   AliEventTag *evTag = new AliEventTag();
255   TTree * ttag = new TTree("T","A Tree with event tags");
256   TBranch * btag = ttag->Branch("AliTAG", &tag);
257   btag->SetCompressionLevel(9);
258   // Run related information
259   tag->SetMagneticField(esd->GetMagneticField());
260   tag->SetBeamEnergy(esd->GetBeamEnergy());
261   tag->SetBeamType(TString(esd->GetBeamType()));
262
263   for(Int_t iEventNumber = 0; iEventNumber < chain->GetEntries(); iEventNumber++) {
264     FillEventTag(chain, evTag, iEventNumber, esd);
265 //     ntrack = 0; nPos = 0; nNeg = 0; nNeutr =0;
266 //     nK0s = 0; nNeutrons = 0; nPi0s = 0;
267 //     nGamas = 0; nProtons = 0; nKaons = 0;
268 //     nPions = 0; nMuons = 0; nElectrons = 0; nFWMuons = 0; nFWMatchedMuons = 0;         
269 //     nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
270 //     nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
271 //     nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
272 //     maxPt = .0; etamaxPt = -999.; phimaxPt = -999.; meanPt = .0; totalP = .0;
273 //     fVertexflag = 1;
274     
275 //     chain->GetEntry(iEventNumber);    
276 //     esdold = esd->GetAliESDOld();
277 //     if(esdold) esd->CopyFromOldESD();
278
279     TFile *file = chain->GetFile();
280     const TUrl *url = file->GetEndpointUrl();
281     fguid = file->GetUUID().AsString();
282     if(fSession == "grid") {
283       TString fturltemp = "alien://"; fturltemp += url->GetFile();
284       fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
285     }
286     else fturl = url->GetFile();
287
288     if(iEventNumber == 0) iInitRunNumber = esd->GetRunNumber();
289     iRunNumber = esd->GetRunNumber();
290     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD - You are trying to merge different runs!!!");
291
292 //     const AliESDVertex * vertexIn = esd->GetVertex();
293 //     fVertexName = vertexIn->GetName();
294 //     if(fVertexName == "default") fVertexflag = 0;
295
296 //     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
297 //       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
298 //       if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
299 //       else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
300 //       UInt_t status = esdTrack->GetStatus();
301       
302 //       //select only tracks with ITS refit
303 //       if ((status&AliESDtrack::kITSrefit)==0) continue;
304 //       //select only tracks with TPC refit
305 //       if ((status&AliESDtrack::kTPCrefit)==0) continue;
306       
307 //       //select only tracks with the "combined PID"
308 //       if ((status&AliESDtrack::kESDpid)==0) continue;
309 //       Double_t p[3];
310 //       esdTrack->GetPxPyPz(p);
311 //       Double_t pt2 = p[0]*p[0]+p[1]*p[1];
312 //       Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
313 //       Double_t fPt = TMath::Sqrt(pt2);
314 //       totalP += momentum;
315 //       meanPt += fPt;
316 //       if(fPt > maxPt) {
317 //        maxPt = fPt;
318 //        phimaxPt = esdTrack->Phi();
319 //        etamaxPt = esdTrack->Eta();
320 //       }
321       
322 //       if(esdTrack->GetSign() > 0) {
323 //      nPos++;
324 //      if(fPt > fLowPtCut) nCh1GeV++;
325 //      if(fPt > fHighPtCut) nCh3GeV++;
326 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
327 //       }
328 //       if(esdTrack->GetSign() < 0) {
329 //      nNeg++;
330 //      if(fPt > fLowPtCut) nCh1GeV++;
331 //      if(fPt > fHighPtCut) nCh3GeV++;
332 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
333 //       }
334 //       if(esdTrack->GetSign() == 0) nNeutr++;
335       
336 //       //PID
337 //       Double_t prob[5];
338 //       esdTrack->GetESDpid(prob);
339       
340 //       Double_t rcc = 0.0;
341 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
342 //       if(rcc == 0.0) continue;
343 //       //Bayes' formula
344 //       Double_t w[5];
345 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
346       
347 //       //protons
348 //       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
349 //       //kaons
350 //       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
351 //       //pions
352 //       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
353 //       //electrons
354 //       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
355 //      nElectrons++;
356 //      if(fPt > fLowPtCut) nEl1GeV++;
357 //      if(fPt > fHighPtCut) nEl3GeV++;
358 //      if(fPt > fVeryHighPtCut) nEl10GeV++;
359 //       }        
360 //       ntrack++;
361 //     }//esd track loop
362     
363 //     /////////////
364 //     //muon code//
365 //     ////////////
366 //     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
367 //     // loop over all reconstructed tracks (also first track of combination)
368 //     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
369 //       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
370 //       if (muonTrack == 0x0) continue;
371       
372 //       // Coordinates at vertex
373 //       fZ = muonTrack->GetZ(); 
374 //       fY = muonTrack->GetBendingCoor();
375 //       fX = muonTrack->GetNonBendingCoor(); 
376       
377 //       fThetaX = muonTrack->GetThetaX();
378 //       fThetaY = muonTrack->GetThetaY();
379       
380 //       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
381 //       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
382 //       fPxRec = fPzRec * TMath::Tan(fThetaX);
383 //       fPyRec = fPzRec * TMath::Tan(fThetaY);
384 //       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
385       
386 //       //ChiSquare of the track if needed
387 //       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
388 //       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
389 //       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
390       
391 //       if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
392       
393 //       nMuons++;
394 //       nFWMuons++;
395 //       if(fEPvector.Pt() > fLowPtCut) {
396 //         nMu1GeV++; 
397 //         if(fEPvector.Pt() > fHighPtCut) {
398 //           nMu3GeV++; 
399 //           if (fEPvector.Pt() > fVeryHighPtCut) {
400 //             nMu10GeV++;
401 //           }
402 //         }
403 //       }
404 //     }//muon track loop
405     
406 //     // Fill the event tags 
407 //     if(ntrack != 0) meanPt = meanPt/ntrack;
408     
409 //     //AliInfo(Form("====================================="));
410 //     //AliInfo(Form("URL: %s - GUID: %s",fturl.Data(),fguid.Data()));
411 //     //AliInfo(Form("====================================="));
412
413 //     //First physics data
414 //     const AliMultiplicity *spdMult = esd->GetMultiplicity();
415 //     evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
416 //     evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
417 //     evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
418
419 //     AliESDVZERO *vzeroData = esd->GetVZEROData();
420 //     evTag->SetMTotV0A(vzeroData->GetMTotV0A());
421 //     evTag->SetMTotV0C(vzeroData->GetMTotV0C());
422 //     evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
423 //     evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
424
425 //     //evTag->SetEventId(iEventNumber+1);
426 //     evTag->SetPeriodNumber(esd->GetPeriodNumber());
427 //     evTag->SetOrbitNumber(esd->GetOrbitNumber());
428 //     evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
429 //     evTag->SetGUID(fguid);
430     if(fSession == "grid") {
431       evTag->SetMD5("");
432       evTag->SetTURL(fturl);
433       evTag->SetSize(0);
434     }
435     else evTag->SetPath(fturl);
436     
437 //     evTag->SetVertexX(vertexIn->GetXv());
438 //     evTag->SetVertexY(vertexIn->GetYv());
439 //     evTag->SetVertexZ(vertexIn->GetZv());
440 //     evTag->SetVertexZError(vertexIn->GetZRes());
441 //     evTag->SetVertexFlag(fVertexflag);
442     
443 //     evTag->SetT0VertexZ(esd->GetT0zVertex());
444     
445 //     evTag->SetTriggerMask(esd->GetTriggerMask());
446 //     evTag->SetTriggerCluster(esd->GetTriggerCluster());
447     
448 //     evTag->SetEventType(esd->GetEventType());
449 //     evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
450
451 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
452 //     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
453 //     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
454 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
455 //     evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
456 //     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
457     
458     
459 //     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
460 //     evTag->SetNumOfPosTracks(nPos);
461 //     evTag->SetNumOfNegTracks(nNeg);
462 //     evTag->SetNumOfNeutrTracks(nNeutr);
463     
464 //     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
465 //     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
466 //     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
467 //     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
468     
469 //     evTag->SetNumOfProtons(nProtons);
470 //     evTag->SetNumOfKaons(nKaons);
471 //     evTag->SetNumOfPions(nPions);
472 //     evTag->SetNumOfMuons(nMuons);
473 //     evTag->SetNumOfFWMuons(nFWMuons);
474 //     evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
475 //     evTag->SetNumOfElectrons(nElectrons);
476 //     evTag->SetNumOfPhotons(nGamas);
477 //     evTag->SetNumOfPi0s(nPi0s);
478 //     evTag->SetNumOfNeutrons(nNeutrons);
479 //     evTag->SetNumOfKaon0s(nK0s);
480     
481 //     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
482 //     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
483 //     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
484 //     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
485 //     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
486 //     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
487 //     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
488 //     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
489 //     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
490     
491 //     tmp.Clear();
492 //     evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
493 //     tmp.Clear();
494 //     evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
495     
496 //     evTag->SetTotalMomentum(totalP);
497 //     evTag->SetMeanPt(meanPt);
498 //     evTag->SetMaxPt(maxPt);
499 //     evTag->SetEtaMaxPt(etamaxPt);
500 //     evTag->SetPhiMaxPt(phimaxPt);
501     
502     tag->SetRunId(iInitRunNumber);
503 //     if(fIsSim) tag->SetDataType(0);
504 //     else tag->SetDataType(1);
505
506     if(fguid != fTempGuid) {
507       fTempGuid = fguid;
508       ttag->Fill();
509       tag->Clear("");
510     }
511     tag->AddEventTag(*evTag);
512     if(iEventNumber+1 == chain->GetEntries()) {
513       //AliInfo(Form("File: %s",fturl.Data()));
514       ttag->Fill();
515       tag->Clear("");
516     }      
517   }//event loop
518   lastEvent = chain->GetEntries();
519   
520   //gSystem->GetMemInfo(meminfo);
521   //AliInfo(Form("After the event and track loop - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
522   //tempmem = meminfo->fMemUsed;
523
524   //chain->Delete("");
525   
526   //gSystem->GetMemInfo(meminfo);
527   //AliInfo(Form("After the t->Delete - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
528   //tempmem = meminfo->fMemUsed;
529
530   ftag->cd();
531   tag->Clear();
532   ttag->Write();
533   ftag->Close();
534
535   //gSystem->GetMemInfo(meminfo);
536   //AliInfo(Form("After the file closing - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
537   //tempmem = meminfo->fMemUsed;
538
539   delete esd;
540   delete tag;
541
542   //gSystem->GetMemInfo(meminfo);
543   //AliInfo(Form("After the delete objects - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
544 }
545
546 //_____________________________________________________________________________
547 void AliESDTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter) {
548   //private method that creates tag files
549   TString fguid = guid;
550   TString fmd5 = md5;
551   TString fturl = turl;
552   /////////////
553   //muon code//
554   ////////////
555     //  Double_t fMUONMASS = 0.105658369;
556   //Variables
557 //   Double_t fX,fY,fZ ;
558 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
559 //   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
560 //   Int_t fCharge;
561 //   TLorentzVector fEPvector;
562
563 //   Float_t fLowPtCut = 1.0;
564 //   Float_t fHighPtCut = 3.0;
565 //   Float_t fVeryHighPtCut = 10.0;
566   ////////////
567
568 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
569
570   // Creates the tags for all the events in a given ESD file
571   Bool_t fIsSim = kTRUE;
572 //   Int_t ntrack;
573 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
574 //   Int_t nPos, nNeg, nNeutr;
575 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
576 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
577 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
578 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
579 //   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
580 //   Int_t fVertexflag;
581   Int_t iRunNumber = 0;
582 //   TString fVertexName;
583 //   TRefArray tmp;
584
585   AliRunTag *tag = new AliRunTag();
586   AliEventTag *evTag = new AliEventTag();
587   TTree * ttag = new TTree("T","A Tree with event tags");
588   TBranch * btag = ttag->Branch("AliTAG", &tag);
589   btag->SetCompressionLevel(9);
590   gSystem->GetMemInfo(meminfo);
591   AliInfo(Form("After the tag initialization - Memory used: %d MB",meminfo->fMemUsed));
592   Int_t tempmem = meminfo->fMemUsed;
593   
594   AliInfo(Form("Creating the ESD tags......."));        
595   
596   Int_t firstEvent = 0,lastEvent = 0;
597   TTree *t = (TTree*) file->Get("esdTree");
598   AliESDEvent *esd = new AliESDEvent();
599   esd->ReadFromTree(t);
600   
601   gSystem->GetMemInfo(meminfo);
602   AliInfo(Form("After the esd initialization - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
603   tempmem = meminfo->fMemUsed;
604
605   t->GetEntry(0);
606   Int_t iInitRunNumber = esd->GetRunNumber();
607
608   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
609   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
610     FillEventTag(t, evTag, iEventNumber, esd);
611 //     ntrack = 0;
612 //     nPos = 0;
613 //     nNeg = 0;
614 //     nNeutr =0;
615 //     nK0s = 0;
616 //     nNeutrons = 0;
617 //     nPi0s = 0;
618 //     nGamas = 0;
619 //     nProtons = 0;
620 //     nKaons = 0;
621 //     nPions = 0;
622 //     nMuons = 0;
623 //     nFWMuons = 0;
624 //     nFWMatchedMuons = 0;
625 //     nElectrons = 0;    
626 //     nCh1GeV = 0;
627 //     nCh3GeV = 0;
628 //     nCh10GeV = 0;
629 //     nMu1GeV = 0;
630 //     nMu3GeV = 0;
631 //     nMu10GeV = 0;
632 //     nEl1GeV = 0;
633 //     nEl3GeV = 0;
634 //     nEl10GeV = 0;
635 //     maxPt = .0;
636 //     etamaxPt = -999.;
637 //     phimaxPt = -999.;
638 //     meanPt = .0;
639 //     totalP = .0;
640 //     fVertexflag = 1;
641     
642 //     t->GetEntry(iEventNumber);
643     iRunNumber = esd->GetRunNumber();
644     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
645 //     const AliESDVertex * vertexIn = esd->GetVertex();
646 //     fVertexName = vertexIn->GetName();
647 //     if(fVertexName == "default") fVertexflag = 0;
648
649 //     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
650 //       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
651 //       if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
652 //       else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
653 //       UInt_t status = esdTrack->GetStatus();
654       
655 //       //select only tracks with ITS refit
656 //       if ((status&AliESDtrack::kITSrefit)==0) continue;
657 //       //select only tracks with TPC refit
658 //       if ((status&AliESDtrack::kTPCrefit)==0) continue;
659       
660 //       //select only tracks with the "combined PID"
661 //       if ((status&AliESDtrack::kESDpid)==0) continue;
662 //       Double_t p[3];
663 //       esdTrack->GetPxPyPz(p);
664 //       Double_t pt2 = p[0]*p[0]+p[1]*p[1];
665 //       Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
666 //       Double_t fPt = TMath::Sqrt(pt2);
667 //       totalP += momentum;
668 //       meanPt += fPt;
669 //       if(fPt > maxPt) {
670 //        maxPt = fPt;
671 //        etamaxPt = esdTrack->Eta();
672 //        phimaxPt = esdTrack->Phi();
673 //       }
674       
675               
676 //       if(esdTrack->GetSign() > 0) {
677 //      nPos++;
678 //      if(fPt > fLowPtCut) nCh1GeV++;
679 //      if(fPt > fHighPtCut) nCh3GeV++;
680 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
681 //       }
682 //       if(esdTrack->GetSign() < 0) {
683 //      nNeg++;
684 //      if(fPt > fLowPtCut) nCh1GeV++;
685 //      if(fPt > fHighPtCut) nCh3GeV++;
686 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
687 //       }
688 //       if(esdTrack->GetSign() == 0) nNeutr++;
689       
690 //       //PID
691 //       Double_t prob[5];
692 //       esdTrack->GetESDpid(prob);
693       
694 //       Double_t rcc = 0.0;
695 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
696 //       if(rcc == 0.0) continue;
697 //       //Bayes' formula
698 //       Double_t w[5];
699 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
700       
701 //       //protons
702 //       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
703 //       //kaons
704 //       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
705 //       //pions
706 //       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
707 //       //electrons
708 //       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
709 //      nElectrons++;
710 //      if(fPt > fLowPtCut) nEl1GeV++;
711 //      if(fPt > fHighPtCut) nEl3GeV++;
712 //      if(fPt > fVeryHighPtCut) nEl10GeV++;
713 //       }        
714 //       ntrack++;
715 //     }//esd track loop
716     
717 //     /////////////
718 //     //muon code//
719 //     ////////////
720 //     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
721 //     // loop over all reconstructed tracks (also first track of combination)
722 //     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
723 //       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
724 //       if (muonTrack == 0x0) continue;
725       
726 //       // Coordinates at vertex
727 //       fZ = muonTrack->GetZ(); 
728 //       fY = muonTrack->GetBendingCoor();
729 //       fX = muonTrack->GetNonBendingCoor(); 
730       
731 //       fThetaX = muonTrack->GetThetaX();
732 //       fThetaY = muonTrack->GetThetaY();
733       
734 //       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
735 //       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
736 //       fPxRec = fPzRec * TMath::Tan(fThetaX);
737 //       fPyRec = fPzRec * TMath::Tan(fThetaY);
738 //       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
739       
740 //       //ChiSquare of the track if needed
741 //       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
742 //       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
743 //       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
744       
745 //       if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
746       
747 //       nMuons++;
748 //       nFWMuons++;
749 //       if(fEPvector.Pt() > fLowPtCut) {
750 //         nMu1GeV++; 
751 //         if(fEPvector.Pt() > fHighPtCut) {
752 //           nMu3GeV++; 
753 //           if (fEPvector.Pt() > fVeryHighPtCut) {
754 //             nMu10GeV++;
755 //           }
756 //         }
757 //       }
758 //     }//muon track loop
759     
760 //     // Fill the event tags 
761 //     if(ntrack != 0) meanPt = meanPt/ntrack;
762     
763 //     //First physics data
764 //     const AliMultiplicity *spdMult = esd->GetMultiplicity();
765 //     evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
766 //     evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
767 //     evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
768
769 //     AliESDVZERO *vzeroData = esd->GetVZEROData();
770 //     evTag->SetMTotV0A(vzeroData->GetMTotV0A());
771 //     evTag->SetMTotV0C(vzeroData->GetMTotV0C());
772 //     evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
773 //     evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
774
775 //     //evTag->SetEventId(iEventNumber+1);
776 //     evTag->SetPeriodNumber(esd->GetPeriodNumber());
777 //     evTag->SetOrbitNumber(esd->GetOrbitNumber());
778 //     evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
779
780 //     evTag->SetGUID(fguid);
781     evTag->SetMD5(fmd5);
782     evTag->SetTURL(fturl);
783     evTag->SetSize(size);
784 //     evTag->SetVertexX(vertexIn->GetXv());
785 //     evTag->SetVertexY(vertexIn->GetYv());
786 //     evTag->SetVertexZ(vertexIn->GetZv());
787 //     evTag->SetVertexZError(vertexIn->GetZRes());
788 //     evTag->SetVertexFlag(fVertexflag);
789     
790 //     evTag->SetT0VertexZ(esd->GetT0zVertex());
791     
792 //     evTag->SetTriggerMask(esd->GetTriggerMask());
793 //     evTag->SetTriggerCluster(esd->GetTriggerCluster());
794     
795 //     evTag->SetEventType(esd->GetEventType());
796 //     evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
797
798 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
799 //     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
800 //     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
801 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
802 //     evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
803 //     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
804     
805     
806 //     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
807 //     evTag->SetNumOfPosTracks(nPos);
808 //     evTag->SetNumOfNegTracks(nNeg);
809 //     evTag->SetNumOfNeutrTracks(nNeutr);
810     
811 //     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
812 //     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
813 //     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
814 //     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
815     
816 //     evTag->SetNumOfProtons(nProtons);
817 //     evTag->SetNumOfKaons(nKaons);
818 //     evTag->SetNumOfPions(nPions);
819 //     evTag->SetNumOfMuons(nMuons);
820 //     evTag->SetNumOfFWMuons(nFWMuons);
821 //     evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
822 //     evTag->SetNumOfElectrons(nElectrons);
823 //     evTag->SetNumOfPhotons(nGamas);
824 //     evTag->SetNumOfPi0s(nPi0s);
825 //     evTag->SetNumOfNeutrons(nNeutrons);
826 //     evTag->SetNumOfKaon0s(nK0s);
827     
828 //     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
829 //     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
830 //     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
831 //     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
832 //     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
833 //     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
834 //     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
835 //     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
836 //     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
837     
838 //     tmp.Clear();
839 //     evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
840 //     tmp.Clear();
841 //     evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
842     
843 //     evTag->SetTotalMomentum(totalP);
844 //     evTag->SetMeanPt(meanPt);
845 //     evTag->SetMaxPt(maxPt);
846 //     evTag->SetEtaMaxPt(etamaxPt);
847 //     evTag->SetPhiMaxPt(phimaxPt);
848     
849     tag->SetRunId(iInitRunNumber);
850     if(fIsSim) tag->SetDataType(0);
851     else tag->SetDataType(1);
852     tag->AddEventTag(*evTag);
853   }//event loop
854   lastEvent = iNumberOfEvents;
855   
856   gSystem->GetMemInfo(meminfo);
857   AliInfo(Form("After the event and track loop - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
858   tempmem = meminfo->fMemUsed;
859   t->Delete("");
860   
861   gSystem->GetMemInfo(meminfo);
862   AliInfo(Form("After the t->Delete - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
863   tempmem = meminfo->fMemUsed;
864
865   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
866   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
867   localFileName += ".ESD.tag.root";
868
869   TString fileName;
870   
871   if(fStorage == 0) {
872     fileName = localFileName.Data();      
873     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
874   }
875   else if(fStorage == 1) {
876     TString alienLocation = "/alien";
877     alienLocation += gGrid->Pwd();
878     alienLocation += fgridpath.Data();
879     alienLocation += "/";
880     alienLocation +=  localFileName;
881     alienLocation += "?se=";
882     alienLocation += fSE.Data();
883     fileName = alienLocation.Data();
884     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
885   }
886
887   TFile* ftag = TFile::Open(fileName, "recreate");
888   ftag->cd();
889   ttag->Fill();
890   tag->Clear();
891   ttag->Write();
892   ftag->Close();
893
894   gSystem->GetMemInfo(meminfo);
895   AliInfo(Form("After the file closing - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
896   tempmem = meminfo->fMemUsed;
897
898   delete ftag;
899   delete esd;
900
901   delete tag;
902   gSystem->GetMemInfo(meminfo);
903   AliInfo(Form("After the delete objects - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
904 }
905
906 //_____________________________________________________________________________
907 void AliESDTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter) {
908   //private method that creates tag files
909   /////////////
910   //muon code//
911   ////////////
912   //  Double_t fMUONMASS = 0.105658369;
913   //Variables
914 //   Double_t fX,fY,fZ ;
915 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
916 //   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
917 //   Int_t fCharge;
918 //   TLorentzVector fEPvector;
919
920 //   Float_t fLowPtCut = 1.0;
921 //   Float_t fHighPtCut = 3.0;
922 //   Float_t fVeryHighPtCut = 10.0;
923   ////////////
924
925 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
926
927   // Creates the tags for all the events in a given ESD file
928 //   Bool_t fIsSim = kTRUE;
929 //   Int_t ntrack;
930 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
931 //   Int_t nPos, nNeg, nNeutr;
932 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
933 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
934 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
935 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
936 //   Float_t maxPt = .0, etamaxPt = -999, phimaxPt = -999., meanPt = .0, totalP = .0;
937 //   Int_t fVertexflag;
938   Int_t iRunNumber = 0;
939 //   TString fVertexName;
940 //   TRefArray tmp;
941
942   AliRunTag *tag = new AliRunTag();
943   AliEventTag *evTag = new AliEventTag();
944   TTree * ttag = new TTree("T","A Tree with event tags");
945   TBranch * btag = ttag->Branch("AliTAG", &tag);
946   btag->SetCompressionLevel(9);
947   
948   AliInfo(Form("Creating the ESD tags......."));        
949   
950   Int_t firstEvent = 0,lastEvent = 0;
951   
952   TTree *t = (TTree*) file->Get("esdTree");
953   AliESDEvent *esd = new AliESDEvent();
954   esd->ReadFromTree(t);
955   
956   t->GetEntry(0);
957   Int_t iInitRunNumber = esd->GetRunNumber();
958
959   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
960   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
961     FillEventTag(t, evTag, iEventNumber, esd);
962 //     ntrack = 0;
963 //     nPos = 0;
964 //     nNeg = 0;
965 //     nNeutr =0;
966 //     nK0s = 0;
967 //     nNeutrons = 0;
968 //     nPi0s = 0;
969 //     nGamas = 0;
970 //     nProtons = 0;
971 //     nKaons = 0;
972 //     nPions = 0;
973 //     nMuons = 0;
974 //     nFWMuons = 0;
975 //     nFWMatchedMuons = 0;
976 //     nElectrons = 0;    
977 //     nCh1GeV = 0;
978 //     nCh3GeV = 0;
979 //     nCh10GeV = 0;
980 //     nMu1GeV = 0;
981 //     nMu3GeV = 0;
982 //     nMu10GeV = 0;
983 //     nEl1GeV = 0;
984 //     nEl3GeV = 0;
985 //     nEl10GeV = 0;
986 //     maxPt = .0;
987 //     etamaxPt = -999.;
988 //     phimaxPt = -999.;
989 //     meanPt = .0;
990 //     totalP = .0;
991 //     fVertexflag = 1;
992     
993 //     t->GetEntry(iEventNumber);
994     iRunNumber = esd->GetRunNumber();
995     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
996 //     const AliESDVertex * vertexIn = esd->GetVertex();
997 //     fVertexName = vertexIn->GetName();
998 //     if(fVertexName == "default") fVertexflag = 0;
999
1000 //     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
1001 //       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
1002 //       if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
1003 //       else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
1004 //       UInt_t status = esdTrack->GetStatus();
1005       
1006 //       //select only tracks with ITS refit
1007 //       if ((status&AliESDtrack::kITSrefit)==0) continue;
1008 //       //select only tracks with TPC refit
1009 //       if ((status&AliESDtrack::kTPCrefit)==0) continue;
1010       
1011 //       //select only tracks with the "combined PID"
1012 //       if ((status&AliESDtrack::kESDpid)==0) continue;
1013 //       Double_t p[3];
1014 //       esdTrack->GetPxPyPz(p);
1015 //       Double_t pt2 = p[0]*p[0]+p[1]*p[1];
1016 //       Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
1017 //       Double_t fPt = TMath::Sqrt(pt2);
1018 //       totalP += momentum;
1019 //       meanPt += fPt;
1020 //       if(fPt > maxPt) {
1021 //        maxPt = fPt;
1022 //        etamaxPt = esdTrack->Eta();
1023 //        phimaxPt = esdTrack->Phi();
1024 //       }
1025       
1026       
1027 //       if(esdTrack->GetSign() > 0) {
1028 //      nPos++;
1029 //      if(fPt > fLowPtCut) nCh1GeV++;
1030 //      if(fPt > fHighPtCut) nCh3GeV++;
1031 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
1032 //       }
1033 //       if(esdTrack->GetSign() < 0) {
1034 //      nNeg++;
1035 //      if(fPt > fLowPtCut) nCh1GeV++;
1036 //      if(fPt > fHighPtCut) nCh3GeV++;
1037 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
1038 //       }
1039 //       if(esdTrack->GetSign() == 0) nNeutr++;
1040       
1041 //       //PID
1042 //       Double_t prob[5];
1043 //       esdTrack->GetESDpid(prob);
1044       
1045 //       Double_t rcc = 0.0;
1046 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
1047 //       if(rcc == 0.0) continue;
1048 //       //Bayes' formula
1049 //       Double_t w[5];
1050 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
1051       
1052 //       //protons
1053 //       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
1054 //       //kaons
1055 //       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
1056 //       //pions
1057 //       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
1058 //       //electrons
1059 //       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
1060 //      nElectrons++;
1061 //      if(fPt > fLowPtCut) nEl1GeV++;
1062 //      if(fPt > fHighPtCut) nEl3GeV++;
1063 //      if(fPt > fVeryHighPtCut) nEl10GeV++;
1064 //       }        
1065 //       ntrack++;
1066 //     }//esd track loop
1067     
1068 //     /////////////
1069 //     //muon code//
1070 //     ////////////
1071 //     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
1072 //     // loop over all reconstructed tracks (also first track of combination)
1073 //     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
1074 //       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
1075 //       if (muonTrack == 0x0) continue;
1076       
1077 //       // Coordinates at vertex
1078 //       fZ = muonTrack->GetZ(); 
1079 //       fY = muonTrack->GetBendingCoor();
1080 //       fX = muonTrack->GetNonBendingCoor(); 
1081       
1082 //       fThetaX = muonTrack->GetThetaX();
1083 //       fThetaY = muonTrack->GetThetaY();
1084       
1085 //       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
1086 //       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
1087 //       fPxRec = fPzRec * TMath::Tan(fThetaX);
1088 //       fPyRec = fPzRec * TMath::Tan(fThetaY);
1089 //       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
1090       
1091 //       //ChiSquare of the track if needed
1092 //       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
1093 //       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
1094 //       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
1095       
1096 //       if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
1097
1098 //       nMuons++;
1099 //       nFWMuons++;
1100 //       if(fEPvector.Pt() > fLowPtCut) {
1101 //         nMu1GeV++; 
1102 //         if(fEPvector.Pt() > fHighPtCut) {
1103 //           nMu3GeV++; 
1104 //           if (fEPvector.Pt() > fVeryHighPtCut) {
1105 //             nMu10GeV++;
1106 //           }
1107 //         }
1108 //       }
1109 //     }//muon track loop
1110     
1111 //     // Fill the event tags 
1112 //     if(ntrack != 0) meanPt = meanPt/ntrack;
1113     
1114 //     //First physics data
1115 //     const AliMultiplicity *spdMult = esd->GetMultiplicity();
1116 //     evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
1117 //     evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
1118 //     evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
1119
1120 //     AliESDVZERO *vzeroData = esd->GetVZEROData();
1121 //     evTag->SetMTotV0A(vzeroData->GetMTotV0A());
1122 //     evTag->SetMTotV0C(vzeroData->GetMTotV0C());
1123 //     evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
1124 //     evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
1125
1126 //     //evTag->SetEventId(iEventNumber+1);
1127 //     evTag->SetPeriodNumber(esd->GetPeriodNumber());
1128 //     evTag->SetOrbitNumber(esd->GetOrbitNumber());
1129 //     evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
1130     evTag->SetPath(filepath);
1131  
1132 //     evTag->SetVertexX(vertexIn->GetXv());
1133 //     evTag->SetVertexY(vertexIn->GetYv());
1134 //     evTag->SetVertexZ(vertexIn->GetZv());
1135 //     evTag->SetVertexZError(vertexIn->GetZRes());
1136 //     evTag->SetVertexFlag(fVertexflag);
1137     
1138 //     evTag->SetT0VertexZ(esd->GetT0zVertex());
1139     
1140 //     evTag->SetTriggerMask(esd->GetTriggerMask());
1141 //     evTag->SetTriggerCluster(esd->GetTriggerCluster());
1142     
1143 //     evTag->SetEventType(esd->GetEventType());
1144 //     evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
1145
1146 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
1147 //     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
1148 //     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
1149 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN2Energy());
1150 //     evTag->SetZDCProton1Energy(esd->GetZDCP2Energy());
1151 //     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
1152     
1153     
1154 //     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
1155 //     evTag->SetNumOfPosTracks(nPos);
1156 //     evTag->SetNumOfNegTracks(nNeg);
1157 //     evTag->SetNumOfNeutrTracks(nNeutr);
1158     
1159 //     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
1160 //     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
1161 //     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
1162 //     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
1163     
1164 //     evTag->SetNumOfProtons(nProtons);
1165 //     evTag->SetNumOfKaons(nKaons);
1166 //     evTag->SetNumOfPions(nPions);
1167 //     evTag->SetNumOfMuons(nMuons);
1168 //     evTag->SetNumOfFWMuons(nFWMuons);
1169 //     evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
1170 //     evTag->SetNumOfElectrons(nElectrons);
1171 //     evTag->SetNumOfPhotons(nGamas);
1172 //     evTag->SetNumOfPi0s(nPi0s);
1173 //     evTag->SetNumOfNeutrons(nNeutrons);
1174 //     evTag->SetNumOfKaon0s(nK0s);
1175     
1176 //     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
1177 //     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
1178 //     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
1179 //     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
1180 //     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
1181 //     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
1182 //     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
1183 //     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
1184 //     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
1185     
1186 //     tmp.Clear();
1187 //     evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
1188 //     tmp.Clear();
1189 //     evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
1190     
1191 //     evTag->SetTotalMomentum(totalP);
1192 //     evTag->SetMeanPt(meanPt);
1193 //     evTag->SetMaxPt(maxPt);
1194 //     evTag->SetEtaMaxPt(etamaxPt);
1195 //     evTag->SetPhiMaxPt(phimaxPt);
1196     
1197     tag->SetRunId(iInitRunNumber);
1198 //     if(fIsSim) tag->SetDataType(0);
1199 //     else tag->SetDataType(1);
1200     tag->AddEventTag(*evTag);
1201   }//event loop
1202   lastEvent = iNumberOfEvents;
1203   
1204   t->Delete("");
1205   
1206   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
1207   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
1208   localFileName += ".ESD.tag.root";
1209
1210   TString fileName;
1211   
1212   if(fStorage == 0) {
1213     fileName = localFileName.Data();      
1214     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
1215   }
1216   else if(fStorage == 1) {
1217     TString alienLocation = "/alien";
1218     alienLocation += gGrid->Pwd();
1219     alienLocation += fgridpath.Data();
1220     alienLocation += "/";
1221     alienLocation +=  localFileName;
1222     alienLocation += "?se=";
1223     alienLocation += fSE.Data();
1224     fileName = alienLocation.Data();
1225     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
1226   }
1227
1228   TFile* ftag = TFile::Open(fileName, "recreate");
1229   ftag->cd();
1230   ttag->Fill();
1231   tag->Clear();
1232   ttag->Write();
1233   ftag->Close();
1234
1235   delete ftag;
1236   delete esd;
1237
1238   delete tag;
1239 }
1240
1241 //_____________________________________________________________________________
1242 void AliESDTagCreator::CreateESDTags(Int_t fFirstEvent, Int_t fLastEvent, AliGRPObject *grpData, ULong_t * qa, Bool_t * es, Int_t qalength, Int_t eslength) {
1243   //GRP
1244   Float_t lhcLuminosity = 0.0;
1245   TString lhcState = "test";
1246   //UInt_t detectorMask = 0;
1247   Int_t detectorMask = 0;
1248
1249   detectorMask = grpData->GetDetectorMask();
1250   time_t startTime = grpData->GetTimeStart();
1251   TTimeStamp t1(startTime);
1252   time_t endTime = grpData->GetTimeEnd();
1253   TTimeStamp t2(endTime);
1254   const char* beamtype = grpData->GetBeamType();
1255   Float_t beamenergy = grpData->GetBeamEnergy();
1256
1257
1258   /////////////
1259   //muon code//
1260   ////////////
1261 //   Double_t fMUONMASS = 0.105658369;
1262   //Variables
1263 //   Double_t fX,fY,fZ ;
1264 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
1265 //   Double_t fPxRec,fPyRec, fPzRec, fEnergy;
1266 //   Int_t fCharge;
1267 //   TLorentzVector fEPvector;
1268
1269 //   Float_t fLowPtCut = 1.0;
1270 //   Float_t fHighPtCut = 3.0;
1271 //   Float_t fVeryHighPtCut = 10.0;
1272   ////////////
1273
1274 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
1275
1276   // Creates the tags for all the events in a given ESD file
1277 //   Int_t ntrack;
1278 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
1279 //   Int_t nPos, nNeg, nNeutr;
1280 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
1281 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
1282 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
1283 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
1284 //   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
1285 //   Int_t fVertexflag;
1286   Int_t iRunNumber = 0;
1287   TString fguid, fmd5, fturl;
1288 //   TString fVertexName("default");
1289 //   TRefArray tmp;
1290   
1291   AliInfo(Form("Creating the ESD tags......."));        
1292
1293   TFile *esdfile = TFile::Open("AliESDs.root");
1294   if (!esdfile || !esdfile->IsOpen()) {
1295     AliError(Form("opening failed"));
1296     delete esdfile;
1297     return ;
1298   }  
1299   Int_t lastEvent = 0;
1300   TTree *b = (TTree*) esdfile->Get("esdTree");
1301   AliESDEvent *esd = new AliESDEvent();
1302   esd->ReadFromTree(b);
1303
1304   b->GetEntry(fFirstEvent);
1305   Int_t iInitRunNumber = esd->GetRunNumber();
1306   
1307   Int_t iNumberOfEvents = (Int_t)b->GetEntries();
1308   if ((fLastEvent == -1) || ((Int_t) b->GetEntries() < fLastEvent))
1309     lastEvent = (Int_t)b->GetEntries();
1310   else lastEvent = fLastEvent;
1311
1312   char fileName[256];
1313   sprintf(fileName, "Run%d.Event%d_%d.ESD.tag.root", 
1314           iInitRunNumber,fFirstEvent,lastEvent);
1315   AliInfo(Form("writing tags to file %s", fileName));
1316   AliDebug(1, Form("writing tags to file %s", fileName));
1317  
1318   TFile* ftag = TFile::Open(fileName, "recreate");
1319  
1320   AliRunTag *tag = new AliRunTag();
1321   AliEventTag *evTag = new AliEventTag();
1322   TTree * ttag = new TTree("T","A Tree with event tags");
1323   TBranch * btag = ttag->Branch("AliTAG", &tag);
1324   btag->SetCompressionLevel(9);
1325
1326   if ((fLastEvent != -1) && ((Int_t) b->GetEntries() > fLastEvent)) 
1327     iNumberOfEvents = fLastEvent + 1;
1328   for (Int_t iEventNumber = fFirstEvent; iEventNumber < iNumberOfEvents; iEventNumber++) {
1329     FillEventTag(b, evTag, iEventNumber, esd);
1330 //     ntrack = 0;
1331 //     nPos = 0;
1332 //     nNeg = 0;
1333 //     nNeutr =0;
1334 //     nK0s = 0;
1335 //     nNeutrons = 0;
1336 //     nPi0s = 0;
1337 //     nGamas = 0;
1338 //     nProtons = 0;
1339 //     nKaons = 0;
1340 //     nPions = 0;
1341 //     nMuons = 0;
1342 //     nFWMuons = 0;
1343 //     nFWMatchedMuons = 0;
1344 //     nElectrons = 0;    
1345 //     nCh1GeV = 0;
1346 //     nCh3GeV = 0;
1347 //     nCh10GeV = 0;
1348 //     nMu1GeV = 0;
1349 //     nMu3GeV = 0;
1350 //     nMu10GeV = 0;
1351 //     nEl1GeV = 0;
1352 //     nEl3GeV = 0;
1353 //     nEl10GeV = 0;
1354 //     maxPt = .0;
1355 //     etamaxPt = -999.;
1356 //     phimaxPt = -999.;
1357 //     meanPt = .0;
1358 //     totalP = .0;
1359 //     fVertexflag = 0;
1360
1361 //     b->GetEntry(iEventNumber);
1362     iRunNumber = esd->GetRunNumber();
1363     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
1364
1365     TFile *file = b->GetCurrentFile();
1366     const TUrl *url = file->GetEndpointUrl();
1367     fguid = file->GetUUID().AsString();
1368     if(fStorage == 1) {
1369       TString fturltemp = "alien://"; fturltemp += url->GetFile();
1370       fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
1371     }
1372     else fturl = url->GetFile();
1373
1374     evTag->SetGUID(fguid);
1375     if(fStorage == 1) {
1376       evTag->SetMD5("");
1377       evTag->SetTURL(fturl);
1378       evTag->SetSize(0);
1379     }
1380     else evTag->SetPath(fturl);
1381 //     const AliESDVertex * vertexIn = esd->GetVertex();
1382 //     if (!vertexIn) AliError("ESD has not defined vertex.");
1383 //     if (vertexIn) fVertexName = vertexIn->GetName();
1384 //     if(fVertexName != "default") fVertexflag = 1;
1385 //     for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
1386 //       AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
1387 //       UInt_t status = esdTrack->GetStatus();
1388       
1389 //       //select only tracks with ITS refit
1390 //       if ((status&AliESDtrack::kITSrefit)==0) continue;
1391 //       //select only tracks with TPC refit
1392 //       if ((status&AliESDtrack::kTPCrefit)==0) continue;
1393       
1394 //       //select only tracks with the "combined PID"
1395 //       if ((status&AliESDtrack::kESDpid)==0) continue;
1396 //       Double_t p[3];
1397 //       esdTrack->GetPxPyPz(p);
1398 //       Double_t pt2 = p[0]*p[0]+p[1]*p[1];
1399 //       Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
1400 //       Double_t fPt = TMath::Sqrt(pt2);
1401 //       totalP += momentum;
1402 //       meanPt += fPt;
1403 //       if(fPt > maxPt) {
1404 //        maxPt = fPt;
1405 //        phimaxPt = esdTrack->Eta();
1406 //        etamaxPt = esdTrack->Phi();
1407 //       }
1408       
1409 //       if(esdTrack->GetSign() > 0) {
1410 //      nPos++;
1411 //      if(fPt > fLowPtCut) nCh1GeV++;
1412 //      if(fPt > fHighPtCut) nCh3GeV++;
1413 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
1414 //       }
1415 //       if(esdTrack->GetSign() < 0) {
1416 //      nNeg++;
1417 //      if(fPt > fLowPtCut) nCh1GeV++;
1418 //      if(fPt > fHighPtCut) nCh3GeV++;
1419 //      if(fPt > fVeryHighPtCut) nCh10GeV++;
1420 //       }
1421 //       if(esdTrack->GetSign() == 0) nNeutr++;
1422       
1423 //       //PID
1424 //       Double_t prob[5];
1425 //       esdTrack->GetESDpid(prob);
1426       
1427 //       Double_t rcc = 0.0;
1428 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
1429 //       if(rcc == 0.0) continue;
1430 //       //Bayes' formula
1431 //       Double_t w[5];
1432 //       for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
1433       
1434 //       //protons
1435 //       if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
1436 //       //kaons
1437 //       if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
1438 //       //pions
1439 //       if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
1440 //       //electrons
1441 //       if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
1442 //      nElectrons++;
1443 //      if(fPt > fLowPtCut) nEl1GeV++;
1444 //      if(fPt > fHighPtCut) nEl3GeV++;
1445 //      if(fPt > fVeryHighPtCut) nEl10GeV++;
1446 //       }        
1447 //       ntrack++;
1448 //     }//track loop
1449     
1450 //     /////////////
1451 //     //muon code//
1452 //     ////////////
1453 //     Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
1454 //     // loop over all reconstructed tracks (also first track of combination)
1455 //     for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
1456 //       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
1457 //       if (muonTrack == 0x0) continue;
1458       
1459 //       // Coordinates at vertex
1460 //       fZ = muonTrack->GetZ(); 
1461 //       fY = muonTrack->GetBendingCoor();
1462 //       fX = muonTrack->GetNonBendingCoor(); 
1463       
1464 //       fThetaX = muonTrack->GetThetaX();
1465 //       fThetaY = muonTrack->GetThetaY();
1466       
1467 //       fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
1468 //       fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
1469 //       fPxRec = fPzRec * TMath::Tan(fThetaX);
1470 //       fPyRec = fPzRec * TMath::Tan(fThetaY);
1471 //       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
1472       
1473 //       //ChiSquare of the track if needed
1474 //       fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
1475 //       fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
1476 //       fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
1477       
1478 //       if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
1479
1480 //       nMuons++;
1481 //       nFWMuons++;
1482 //       if(fEPvector.Pt() > fLowPtCut) {
1483 //         nMu1GeV++; 
1484 //         if(fEPvector.Pt() > fHighPtCut) {
1485 //           nMu3GeV++; 
1486 //           if (fEPvector.Pt() > fVeryHighPtCut) {
1487 //             nMu10GeV++;
1488 //           }
1489 //         }
1490 //       }
1491 //     }//muon track loop
1492
1493     
1494 //     // Fill the event tags 
1495 //     if(ntrack != 0)
1496 //       meanPt = meanPt/ntrack;
1497
1498 //     //First physics data
1499 //     const AliMultiplicity *spdMult = esd->GetMultiplicity();
1500 //     evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
1501 //     evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
1502 //     evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
1503
1504 //     AliESDVZERO *vzeroData = esd->GetVZEROData();
1505 //     evTag->SetMTotV0A(vzeroData->GetMTotV0A());
1506 //     evTag->SetMTotV0C(vzeroData->GetMTotV0C());
1507 //     evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
1508 //     evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
1509
1510 //     //evTag->SetEventId(iEventNumber+1);
1511 //     evTag->SetPeriodNumber(esd->GetPeriodNumber());
1512 //     evTag->SetOrbitNumber(esd->GetOrbitNumber());
1513 //     evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
1514 //     if (vertexIn) {
1515 //       evTag->SetVertexX(vertexIn->GetXv());
1516 //       evTag->SetVertexY(vertexIn->GetYv());
1517 //       evTag->SetVertexZ(vertexIn->GetZv());
1518 //       evTag->SetVertexZError(vertexIn->GetZRes());
1519 //     }  
1520 //     evTag->SetVertexFlag(fVertexflag);
1521
1522 //     evTag->SetT0VertexZ(esd->GetT0zVertex());
1523     
1524 //     evTag->SetTriggerMask(esd->GetTriggerMask());
1525 //     evTag->SetTriggerCluster(esd->GetTriggerCluster());
1526     
1527 //     evTag->SetEventType(esd->GetEventType());
1528 //     evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
1529
1530 //     evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
1531 //     evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
1532 //     evTag->SetZDCNeutron2Energy(esd->GetZDCN2Energy());
1533 //     evTag->SetZDCProton2Energy(esd->GetZDCP2Energy());
1534 //     evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
1535 //     evTag->SetNumOfParticipants(esd->GetZDCParticipants());
1536     
1537     
1538 //     evTag->SetNumOfTracks(esd->GetNumberOfTracks());
1539 //     evTag->SetNumOfPosTracks(nPos);
1540 //     evTag->SetNumOfNegTracks(nNeg);
1541 //     evTag->SetNumOfNeutrTracks(nNeutr);
1542     
1543 //     evTag->SetNumOfV0s(esd->GetNumberOfV0s());
1544 //     evTag->SetNumOfCascades(esd->GetNumberOfCascades());
1545 //     evTag->SetNumOfKinks(esd->GetNumberOfKinks());
1546 //     evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
1547     
1548 //     evTag->SetNumOfProtons(nProtons);
1549 //     evTag->SetNumOfKaons(nKaons);
1550 //     evTag->SetNumOfPions(nPions);
1551 //     evTag->SetNumOfMuons(nMuons);
1552 //     evTag->SetNumOfFWMuons(nFWMuons);
1553 //     evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
1554 //     evTag->SetNumOfElectrons(nElectrons);
1555 //     evTag->SetNumOfPhotons(nGamas);
1556 //     evTag->SetNumOfPi0s(nPi0s);
1557 //     evTag->SetNumOfNeutrons(nNeutrons);
1558 //     evTag->SetNumOfKaon0s(nK0s);
1559     
1560 //     evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
1561 //     evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
1562 //     evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
1563 //     evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
1564 //     evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
1565 //     evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
1566 //     evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
1567 //     evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
1568 //     evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
1569     
1570 //     tmp.Clear();
1571 //     evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
1572 //     tmp.Clear();
1573 //     evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
1574     
1575 //     evTag->SetTotalMomentum(totalP);
1576 //     evTag->SetMeanPt(meanPt);
1577 //     evTag->SetMaxPt(maxPt);
1578 //     evTag->SetEtaMaxPt(etamaxPt);
1579 //     evTag->SetPhiMaxPt(phimaxPt);
1580     tag->AddEventTag(*evTag);
1581   }    
1582   
1583   tag->SetLHCTag(lhcLuminosity,lhcState);
1584   tag->SetDetectorTag(esd->GetESDRun()->GetDetectorsInDAQ(), esd->GetESDRun()->GetDetectorsInReco());
1585   
1586   // Get magnetic field info
1587   Bool_t ok = kTRUE;
1588   
1589   Float_t l3Current = grpData->GetL3Current((AliGRPObject::Stats)0);
1590   if (l3Current == AliGRPObject::GetInvalidFloat()) {
1591     AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
1592     ok = kFALSE;
1593   }
1594   
1595   Char_t l3Polarity = grpData->GetL3Polarity();
1596   if (l3Polarity == AliGRPObject::GetInvalidChar()) {
1597     AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
1598     ok = kFALSE;
1599   }
1600   
1601   // Dipole
1602   Float_t diCurrent = grpData->GetDipoleCurrent((AliGRPObject::Stats)0);
1603   if (diCurrent == AliGRPObject::GetInvalidFloat()) {
1604     AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
1605     ok = kFALSE;
1606   }
1607   
1608   Char_t diPolarity = grpData->GetDipolePolarity();
1609   if (diPolarity == AliGRPObject::GetInvalidChar()) {
1610     AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
1611     ok = kFALSE;
1612   }
1613   
1614   if (ok && grpData->IsPolarityConventionLHC()) {
1615     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 1))
1616       tag->SetMagneticField(-0.5);
1617     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 0)) 
1618       tag->SetMagneticField(0.5);
1619     if (TMath::Abs(l3Current) < 2000.0) 
1620       tag->SetMagneticField(0.0);
1621     
1622     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 1))
1623       tag->SetDipoleField(-0.2);
1624     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 0))
1625       tag->SetDipoleField(0.2);
1626     if (TMath::Abs(diCurrent) < 500.0)
1627       tag->SetDipoleField(0.0);
1628   }
1629   
1630   tag->SetRunId(iInitRunNumber);
1631   tag->SetRunStartTime(t1.GetDate());
1632   tag->SetRunStopTime(t2.GetDate());
1633   tag->SetBeamEnergy(beamenergy);
1634   tag->SetBeamType(beamtype);
1635   
1636   //QA setting 
1637   tag->SetQAArray(qa, qalength) ; 
1638   tag->SetEventSpecies(es, eslength) ;
1639         
1640   ftag->cd();
1641   ttag->Fill();
1642   tag->Clear();
1643   ttag->Write();
1644   ftag->Close();
1645   esdfile->cd();
1646   delete esdfile;
1647   delete ftag;
1648   delete esd;
1649   delete tag;
1650   delete evTag;
1651 }
1652
1653 void AliESDTagCreator::CreateESDTagsFullRun(TTree *chain, AliGRPObject *grpData, ULong_t * qa, Bool_t * es, Int_t qalength, Int_t eslength)
1654 {
1655   //GRP
1656   Float_t lhcLuminosity = 0.0;
1657   TString lhcState = "test";
1658   //UInt_t detectorMask = 0;
1659   Int_t detectorMask = 0;
1660
1661   detectorMask = grpData->GetDetectorMask();
1662   time_t startTime = grpData->GetTimeStart();
1663   TTimeStamp t1(startTime);
1664   time_t endTime = grpData->GetTimeEnd();
1665   TTimeStamp t2(endTime);
1666   const char* beamtype = grpData->GetBeamType();
1667   Float_t beamenergy = grpData->GetBeamEnergy();
1668
1669   Int_t iRunNumber = 0;
1670   TString fguid, fmd5, fturl;
1671
1672   AliInfo(Form("Creating the ESD tags......."));        
1673
1674   AliESDEvent *esd = new AliESDEvent();
1675   esd->ReadFromTree(chain);
1676
1677   chain->GetEntry(0);
1678   Int_t iInitRunNumber = esd->GetRunNumber();
1679   
1680   Int_t iNumberOfEvents = (Int_t)chain->GetEntries();
1681   Int_t iFirstEvent = 0;
1682
1683   char fileName[256];
1684   sprintf(fileName, "Run%d.Event%d_%d.ESD.tag.root", 
1685           iInitRunNumber,iFirstEvent,iNumberOfEvents);
1686   AliInfo(Form("writing tags to file %s", fileName));
1687   AliDebug(1, Form("writing tags to file %s", fileName));
1688  
1689   TFile* ftag = TFile::Open(fileName, "recreate");
1690  
1691   AliRunTag *tag = new AliRunTag();
1692   AliEventTag *evTag = new AliEventTag();
1693   TTree * ttag = new TTree("T","A Tree with event tags");
1694   TBranch * btag = ttag->Branch("AliTAG", &tag);
1695   btag->SetCompressionLevel(9);
1696
1697   for (Int_t iEventNumber = iFirstEvent; iEventNumber < iNumberOfEvents; iEventNumber++) {
1698     FillEventTag(chain, evTag, iEventNumber, esd);
1699
1700     iRunNumber = esd->GetRunNumber();
1701     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
1702     
1703     TFile *file = chain->GetCurrentFile();
1704     const TUrl *url = file->GetEndpointUrl();
1705     fguid = file->GetUUID().AsString();
1706     if(fStorage == 1) {
1707       TString fturltemp = "alien://"; fturltemp += url->GetFile();
1708       fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
1709     }
1710     else fturl = url->GetFile();
1711
1712     evTag->SetGUID(fguid);
1713     if(fStorage == 1) {
1714       evTag->SetMD5("");
1715       evTag->SetTURL(fturl);
1716       evTag->SetSize(0);
1717     }
1718     else {
1719       evTag->SetPath(fturl);
1720       evTag->SetTURL(fturl);
1721     }
1722
1723     tag->AddEventTag(*evTag);
1724   }
1725
1726   tag->SetLHCTag(lhcLuminosity,lhcState);
1727   tag->SetDetectorTag(esd->GetESDRun()->GetDetectorsInDAQ(), esd->GetESDRun()->GetDetectorsInReco());
1728   
1729   // Get magnetic field info
1730   Bool_t ok = kTRUE;
1731   
1732   Float_t l3Current = grpData->GetL3Current((AliGRPObject::Stats)0);
1733   if (l3Current == AliGRPObject::GetInvalidFloat()) {
1734     AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
1735     ok = kFALSE;
1736   }
1737   
1738   Char_t l3Polarity = grpData->GetL3Polarity();
1739   if (l3Polarity == AliGRPObject::GetInvalidChar()) {
1740     AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
1741     ok = kFALSE;
1742   }
1743   
1744   // Dipole
1745   Float_t diCurrent = grpData->GetDipoleCurrent((AliGRPObject::Stats)0);
1746   if (diCurrent == AliGRPObject::GetInvalidFloat()) {
1747     AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
1748     ok = kFALSE;
1749   }
1750   
1751   Char_t diPolarity = grpData->GetDipolePolarity();
1752   if (diPolarity == AliGRPObject::GetInvalidChar()) {
1753     AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
1754     ok = kFALSE;
1755   }
1756   
1757   if (ok && grpData->IsPolarityConventionLHC()) {
1758     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 1))
1759       tag->SetMagneticField(-0.5);
1760     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 0)) 
1761       tag->SetMagneticField(0.5);
1762     if (TMath::Abs(l3Current) < 2000.0) 
1763       tag->SetMagneticField(0.0);
1764     
1765     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 1))
1766       tag->SetDipoleField(-0.2);
1767     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 0))
1768       tag->SetDipoleField(0.2);
1769     if (TMath::Abs(diCurrent) < 500.0)
1770       tag->SetDipoleField(0.0);
1771   }
1772   
1773   tag->SetRunId(iInitRunNumber);
1774   tag->SetRunStartTime(t1.GetDate());
1775   tag->SetRunStopTime(t2.GetDate());
1776   tag->SetBeamEnergy(beamenergy);
1777   tag->SetBeamType(beamtype);
1778   
1779   //QA setting 
1780   tag->SetQAArray(qa, qalength) ; 
1781   tag->SetEventSpecies(es, eslength) ;
1782   
1783   ftag->cd();
1784   ttag->Fill();
1785   tag->Clear();
1786   ttag->Write();
1787   ftag->Close();
1788   delete ftag;
1789   delete esd;
1790   delete tag;
1791   delete evTag;
1792 }
1793
1794 //_____________________________________________________________________________
1795 void AliESDTagCreator::SwitchOffBranches() const {
1796   //
1797   // Switch of branches on user request
1798   TObjArray * tokens = fBranches.Tokenize(" ");
1799   Int_t ntok = tokens->GetEntries();
1800   for (Int_t i = 0; i < ntok; i++)  {
1801     TString str = ((TObjString*) tokens->At(i))->GetString();
1802     fChain->SetBranchStatus(Form("%s%s%s","*", str.Data(), "*"), 0);
1803     AliInfo(Form("Branch %s switched off \n", str.Data()));
1804   }
1805 }
1806
1807 //_____________________________________________________________________________
1808 void AliESDTagCreator::FillEventTag(TTree *chain, AliEventTag *evTag, Int_t iEventNumber, AliESDEvent *esd)
1809 {
1810   // Fill the event specific information in the EventTag
1811   AliESD *esdold = 0x0;
1812
1813   TString fTempGuid;
1814
1815   /////////////
1816   //muon code//
1817   ////////////
1818   Double_t fMUONMASS = 0.105658369;
1819   //Variables
1820   Double_t fX,fY,fZ ;
1821   Double_t fThetaX, fThetaY, fPyz, fChisquare;
1822   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
1823   Int_t fCharge;
1824   TLorentzVector fEPvector;
1825
1826   Float_t fLowPtCut      =  1.0;
1827   Float_t fHighPtCut     =  3.0;
1828   Float_t fVeryHighPtCut = 10.0;
1829   ////////////
1830
1831   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
1832
1833   // Creates the tags for all the events in a given ESD file
1834   Bool_t fIsSim = kTRUE;
1835   Int_t ntrack;
1836   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
1837   Int_t nPos, nNeg, nNeutr;
1838   Int_t nK0s, nLambdas, nNeutrons, nPi0s, nGamas;
1839   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
1840   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
1841   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
1842   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
1843   Int_t fVertexflag;
1844   TString fVertexName;
1845
1846   TRefArray tmp;
1847   
1848   ntrack = 0; nPos = 0; nNeg = 0; nNeutr =0;
1849   nK0s = 0; nLambdas = 0; nNeutrons = 0; nPi0s = 0;
1850   nGamas = 0; nProtons = 0; nKaons = 0;
1851   nPions = 0; nMuons = 0; nElectrons = 0; nFWMuons = 0; nFWMatchedMuons = 0;      
1852   nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
1853   nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
1854   nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
1855   maxPt = .0; etamaxPt = -999.; phimaxPt = -999.; meanPt = .0; totalP = .0;
1856   fVertexflag = 1;
1857   
1858   chain->GetEntry(iEventNumber);    
1859   esdold = esd->GetAliESDOld();
1860   if(esdold) esd->CopyFromOldESD();
1861   
1862 //   TFile *file = chain->GetFile();
1863 //   const TUrl *url = file->GetEndpointUrl();
1864 //   fguid = file->GetUUID().AsString();
1865 //   if(fSession == "grid") {
1866 //     TString fturltemp = "alien://"; fturltemp += url->GetFile();
1867 //     fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
1868 //   }
1869 //   else fturl = url->GetFile();
1870   
1871   const AliESDVertex * vertexIn = esd->GetVertex();
1872   fVertexName = vertexIn->GetName();
1873   if(fVertexName == "default") fVertexflag = 0;
1874   
1875   for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
1876     AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
1877     if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
1878     else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
1879     UInt_t status = esdTrack->GetStatus();
1880     
1881     //select only tracks with ITS refit
1882     if ((status&AliESDtrack::kITSrefit)==0) continue;
1883     //select only tracks with TPC refit
1884     if ((status&AliESDtrack::kTPCrefit)==0) continue;
1885     
1886     //select only tracks with the "combined PID"
1887     if ((status&AliESDtrack::kESDpid)==0) continue;
1888     Double_t p[3];
1889     esdTrack->GetPxPyPz(p);
1890     Double_t pt2 = p[0]*p[0]+p[1]*p[1];
1891     Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
1892     Double_t fPt = TMath::Sqrt(pt2);
1893     totalP += momentum;
1894     meanPt += fPt;
1895     if(fPt > maxPt) {
1896       maxPt = fPt;
1897       phimaxPt = esdTrack->Phi();
1898       etamaxPt = esdTrack->Eta();
1899     }
1900     
1901     if(esdTrack->GetSign() > 0) {
1902       nPos++;
1903       if(fPt > fLowPtCut) nCh1GeV++;
1904       if(fPt > fHighPtCut) nCh3GeV++;
1905       if(fPt > fVeryHighPtCut) nCh10GeV++;
1906     }
1907     if(esdTrack->GetSign() < 0) {
1908       nNeg++;
1909       if(fPt > fLowPtCut) nCh1GeV++;
1910       if(fPt > fHighPtCut) nCh3GeV++;
1911       if(fPt > fVeryHighPtCut) nCh10GeV++;
1912     }
1913     if(esdTrack->GetSign() == 0) nNeutr++;
1914     
1915     //PID
1916     Double_t prob[5];
1917     esdTrack->GetESDpid(prob);
1918     
1919     Double_t rcc = 0.0;
1920     for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
1921     if(rcc == 0.0) continue;
1922     //Bayes' formula
1923     Double_t w[5];
1924     for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
1925     
1926     //protons
1927     if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
1928     //kaons
1929     if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
1930     //pions
1931     if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
1932     //electrons
1933     if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
1934       nElectrons++;
1935       if(fPt > fLowPtCut) nEl1GeV++;
1936       if(fPt > fHighPtCut) nEl3GeV++;
1937       if(fPt > fVeryHighPtCut) nEl10GeV++;
1938     }     
1939     ntrack++;
1940   }
1941   //esd track loop
1942   
1943   /* muon code */
1944   Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
1945   // loop over all reconstructed tracks (also first track of combination)
1946   for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
1947     AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
1948     if (muonTrack == 0x0) continue;
1949     
1950     // Coordinates at vertex
1951     fZ = muonTrack->GetZ(); 
1952     fY = muonTrack->GetBendingCoor();
1953     fX = muonTrack->GetNonBendingCoor(); 
1954     
1955     fThetaX = muonTrack->GetThetaX();
1956     fThetaY = muonTrack->GetThetaY();
1957     
1958     fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
1959     fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
1960     fPxRec = fPzRec * TMath::Tan(fThetaX);
1961     fPyRec = fPzRec * TMath::Tan(fThetaY);
1962     fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
1963     
1964     //ChiSquare of the track if needed
1965     fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
1966     fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
1967     fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
1968     
1969     if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
1970     
1971     nMuons++;
1972     nFWMuons++;
1973     if(fEPvector.Pt() > fLowPtCut) {
1974       nMu1GeV++; 
1975       if(fEPvector.Pt() > fHighPtCut) {
1976         nMu3GeV++; 
1977         if (fEPvector.Pt() > fVeryHighPtCut) {
1978           nMu10GeV++;
1979         }
1980       }
1981     }
1982   }//muon track loop
1983   
1984   // Fill the event tags 
1985   if(ntrack != 0) meanPt = meanPt/ntrack;
1986   
1987   //AliInfo(Form("====================================="));
1988   //AliInfo(Form("URL: %s - GUID: %s",fturl.Data(),fguid.Data()));
1989   //AliInfo(Form("====================================="));
1990   
1991   //First physics data
1992   const AliMultiplicity *spdMult = esd->GetMultiplicity();
1993   evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
1994   evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
1995   evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
1996   
1997   AliESDVZERO *vzeroData = esd->GetVZEROData();
1998   evTag->SetMTotV0A(vzeroData->GetMTotV0A());
1999   evTag->SetMTotV0C(vzeroData->GetMTotV0C());
2000   evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
2001   evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
2002   
2003   //evTag->SetEventId(iEventNumber+1);
2004   evTag->SetPeriodNumber(esd->GetPeriodNumber());
2005   evTag->SetOrbitNumber(esd->GetOrbitNumber());
2006   evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
2007   //  evTag->SetGUID(fguid);
2008 //   if(fSession == "grid") {
2009 //     evTag->SetMD5("");
2010 //     evTag->SetTURL(fturl);
2011 //     evTag->SetSize(0);
2012 //   }
2013 //   else evTag->SetPath(fturl);
2014   
2015   evTag->SetVertexX(vertexIn->GetXv());
2016   evTag->SetVertexY(vertexIn->GetYv());
2017   evTag->SetVertexZ(vertexIn->GetZv());
2018   evTag->SetVertexZError(vertexIn->GetZRes());
2019   evTag->SetVertexFlag(fVertexflag);
2020   
2021   evTag->SetT0VertexZ(esd->GetT0zVertex());
2022   
2023   evTag->SetTriggerMask(esd->GetTriggerMask());
2024   evTag->SetTriggerCluster(esd->GetTriggerCluster());
2025   
2026   evTag->SetEventType(esd->GetEventType());
2027   evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
2028   
2029   evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
2030   evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
2031   evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
2032   evTag->SetZDCNeutron2Energy(esd->GetZDCN2Energy());
2033   evTag->SetZDCProton2Energy(esd->GetZDCP2Energy());
2034   evTag->SetNumOfParticipants(esd->GetZDCParticipants());
2035   
2036   evTag->SetNumOfTracks(esd->GetNumberOfTracks());
2037   evTag->SetNumOfPosTracks(nPos);
2038   evTag->SetNumOfNegTracks(nNeg);
2039   evTag->SetNumOfNeutrTracks(nNeutr);
2040   
2041   evTag->SetNumOfV0s(esd->GetNumberOfV0s());
2042   evTag->SetNumOfCascades(esd->GetNumberOfCascades());
2043   evTag->SetNumOfKinks(esd->GetNumberOfKinks());
2044   evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
2045   
2046   evTag->SetNumOfProtons(nProtons);
2047   evTag->SetNumOfKaons(nKaons);
2048   evTag->SetNumOfPions(nPions);
2049   evTag->SetNumOfMuons(nMuons);
2050   evTag->SetNumOfFWMuons(nFWMuons);
2051   evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
2052   evTag->SetNumOfElectrons(nElectrons);
2053   evTag->SetNumOfPhotons(nGamas);
2054   evTag->SetNumOfPi0s(nPi0s);
2055   evTag->SetNumOfNeutrons(nNeutrons);
2056   evTag->SetNumOfKaon0s(nK0s);
2057   evTag->SetNumOfLambdas(nLambdas);
2058   
2059   evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
2060   evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
2061   evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
2062   evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
2063   evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
2064   evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
2065   evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
2066   evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
2067   evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
2068
2069   tmp.Clear();
2070   evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
2071   tmp.Clear();
2072   evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
2073     
2074   evTag->SetTotalMomentum(totalP);
2075   evTag->SetMeanPt(meanPt);
2076   evTag->SetMaxPt(maxPt);
2077   evTag->SetEtaMaxPt(etamaxPt);
2078   evTag->SetPhiMaxPt(phimaxPt);
2079
2080   evTag->SetPhysicsFlag(kTRUE);
2081   evTag->SetBackgroungFlag(kFALSE);
2082 }
2083
2084 void AliESDTagCreator::CreateESDRunTagSummary(TTree *chain)
2085 {
2086   // Merge all tags from a run into a single RunTag
2087   // with as many EventTags as there is files
2088   AliRunTag *rtag;
2089   chain->SetBranchAddress("AliTAG", &rtag);
2090
2091   TFile* ftag = TFile::Open("RunTagSummary.tag.root", "recreate");
2092
2093   AliRunTag *tag = new AliRunTag();
2094   TTree * ttag = new TTree("T","A Tree with event tags");
2095   TBranch * btag = ttag->Branch("AliTAG", &tag);
2096   btag->SetCompressionLevel(9);
2097   
2098   for (int itag=0; itag<chain->GetEntries(); itag++) {
2099     chain->GetEntry(itag);
2100     tag->CopyStandardContent(rtag);
2101     
2102     TString curguid="";
2103
2104     for (int iev=0; iev<rtag->GetEventTags()->GetEntries(); iev++) {
2105       if (!curguid.Contains(((AliEventTag *) rtag->GetEventTags()->At(iev))->GetGUID())) {
2106         curguid = ((AliEventTag *) rtag->GetEventTags()->At(iev))->GetGUID();
2107         cout << "Adding " << curguid << endl;
2108         tag->AddEventTag(*((AliEventTag *) rtag->GetEventTags()->At(iev)));
2109       }
2110     }
2111
2112     ttag->Fill();
2113     tag->Clear();
2114   }
2115
2116   ftag->cd();
2117   ttag->Write();
2118   ftag->Close();
2119   
2120 }