]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDTagCreator.cxx
Remove loading FMDanalysis library
[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: %lld",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: %lld",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: %lld",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, foldguid;
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  
234   if(fStorage == 0) {
235     fileName = localFileName.Data();      
236     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
237   }
238   else if(fStorage == 1) {
239     TString alienLocation = "/alien";
240     alienLocation += gGrid->Pwd();
241     alienLocation += fgridpath.Data();
242     alienLocation += "/";
243     alienLocation +=  localFileName;
244     alienLocation += "?se=";
245     alienLocation += fSE.Data();
246     fileName = alienLocation.Data();
247     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
248   }
249
250   TFile* ftag = TFile::Open(fileName, "recreate");
251
252   AliRunTag *tag = new AliRunTag();
253   AliEventTag *evTag = new AliEventTag();
254   TTree * ttag = new TTree("T","A Tree with event tags");
255   TBranch * btag = ttag->Branch("AliTAG", &tag);
256   btag->SetCompressionLevel(9);
257   // Run related information
258   tag->SetMagneticField(esd->GetMagneticField());
259   tag->SetBeamEnergy(esd->GetBeamEnergy());
260   tag->SetBeamType(TString(esd->GetBeamType()));
261   tag->SetActiveTriggerClasses(esd->GetESDRun()->GetActiveTriggerClasses());
262   
263   foldguid = "";
264
265   for(Int_t iEventNumber = 0; iEventNumber < chain->GetEntries(); iEventNumber++) {
266     FillEventTag(chain, evTag, iEventNumber, esd);
267
268     if(iEventNumber == 0) iInitRunNumber = esd->GetRunNumber();
269     iRunNumber = esd->GetRunNumber();
270     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD - You are trying to merge different runs!!!");
271
272     TFile *file = chain->GetFile();
273     //    const TUrl *url = file->GetEndpointUrl();
274     fguid = file->GetUUID().AsString();
275
276     if (foldguid == fguid) {
277       tag->AddEventTag(*evTag);
278     }
279     else {
280       AliFileTag *nftag = new AliFileTag();
281
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       fturl = file->GetName();
288
289       if(fSession == "grid") {
290         nftag->SetMD5("");
291         nftag->SetTURL(fturl);
292         nftag->SetSize(0);
293       }
294       else {
295         nftag->SetPath(fturl);
296         nftag->SetSize(0);
297         nftag->SetMD5("");
298         nftag->SetTURL(fturl);
299       }
300       foldguid = fguid;
301       
302       if (tag->GetFileId(fguid) > -1)
303         AliFatal("Adding a file which is already in the RunTag.");
304
305       tag->AddFileTag(nftag);
306     }
307
308     tag->SetRunId(iInitRunNumber);
309 //     if(fIsSim) tag->SetDataType(0);
310 //     else tag->SetDataType(1);
311
312 //     if(fguid != fTempGuid) {
313 //       fTempGuid = fguid;
314 //       ttag->Fill();
315 //       tag->Clear("");
316 //     }
317     if(iEventNumber+1 == chain->GetEntries()) {
318       //AliInfo(Form("File: %s",fturl.Data()));
319
320       ttag->Fill();
321       tag->Clear("");
322     }      
323   }//event loop
324   lastEvent = chain->GetEntries();
325   
326   //gSystem->GetMemInfo(meminfo);
327   //AliInfo(Form("After the event and track loop - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
328   //tempmem = meminfo->fMemUsed;
329
330   //chain->Delete("");
331   
332   //gSystem->GetMemInfo(meminfo);
333   //AliInfo(Form("After the t->Delete - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
334   //tempmem = meminfo->fMemUsed;
335
336   ftag->cd();
337   tag->Clear();
338   ttag->Write();
339   ftag->Close();
340
341   //gSystem->GetMemInfo(meminfo);
342   //AliInfo(Form("After the file closing - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
343   //tempmem = meminfo->fMemUsed;
344
345   delete esd;
346   delete tag;
347
348   //gSystem->GetMemInfo(meminfo);
349   //AliInfo(Form("After the delete objects - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
350 }
351
352 //_____________________________________________________________________________
353 void AliESDTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter) {
354   //private method that creates tag files
355   TString fguid = guid;
356   TString fmd5 = md5;
357   TString fturl = turl;
358   /////////////
359   //muon code//
360   ////////////
361     //  Double_t fMUONMASS = 0.105658369;
362   //Variables
363 //   Double_t fX,fY,fZ ;
364 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
365 //   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
366 //   Int_t fCharge;
367 //   TLorentzVector fEPvector;
368
369 //   Float_t fLowPtCut = 1.0;
370 //   Float_t fHighPtCut = 3.0;
371 //   Float_t fVeryHighPtCut = 10.0;
372   ////////////
373
374 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
375
376   // Creates the tags for all the events in a given ESD file
377   Bool_t fIsSim = kTRUE;
378 //   Int_t ntrack;
379 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
380 //   Int_t nPos, nNeg, nNeutr;
381 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
382 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
383 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
384 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
385 //   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
386 //   Int_t fVertexflag;
387   Int_t iRunNumber = 0;
388 //   TString fVertexName;
389 //   TRefArray tmp;
390
391   AliRunTag *tag = new AliRunTag();
392   AliEventTag *evTag = new AliEventTag();
393   TTree * ttag = new TTree("T","A Tree with event tags");
394   TBranch * btag = ttag->Branch("AliTAG", &tag);
395   btag->SetCompressionLevel(9);
396   gSystem->GetMemInfo(meminfo);
397   AliInfo(Form("After the tag initialization - Memory used: %d MB",meminfo->fMemUsed));
398   Int_t tempmem = meminfo->fMemUsed;
399   
400   AliInfo(Form("Creating the ESD tags......."));        
401   
402   Int_t firstEvent = 0,lastEvent = 0;
403   TTree *t = (TTree*) file->Get("esdTree");
404   AliESDEvent *esd = new AliESDEvent();
405   esd->ReadFromTree(t);
406   
407   gSystem->GetMemInfo(meminfo);
408   AliInfo(Form("After the esd initialization - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
409   tempmem = meminfo->fMemUsed;
410
411   t->GetEntry(0);
412   Int_t iInitRunNumber = esd->GetRunNumber();
413   tag->SetMagneticField(esd->GetMagneticField());
414   tag->SetBeamEnergy(esd->GetBeamEnergy());
415   tag->SetBeamType(TString(esd->GetBeamType()));
416   tag->SetActiveTriggerClasses(esd->GetESDRun()->GetActiveTriggerClasses());
417
418   AliFileTag *eftag = new AliFileTag();
419   eftag->SetMD5(md5);
420   eftag->SetTURL(fturl);
421   eftag->SetSize(size);
422   tag->AddFileTag(eftag);
423
424   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
425   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
426     FillEventTag(t, evTag, iEventNumber, esd);
427     iRunNumber = esd->GetRunNumber();
428     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
429     
430     tag->SetRunId(iInitRunNumber);
431     if(fIsSim) tag->SetDataType(0);
432     else tag->SetDataType(1);
433     tag->AddEventTag(*evTag);
434   }//event loop
435   lastEvent = iNumberOfEvents;
436   
437   gSystem->GetMemInfo(meminfo);
438   AliInfo(Form("After the event and track loop - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
439   tempmem = meminfo->fMemUsed;
440   t->Delete("");
441   
442   gSystem->GetMemInfo(meminfo);
443   AliInfo(Form("After the t->Delete - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
444   tempmem = meminfo->fMemUsed;
445
446   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
447   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
448   localFileName += ".ESD.tag.root";
449
450   TString fileName;
451   
452   if(fStorage == 0) {
453     fileName = localFileName.Data();      
454     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
455   }
456   else if(fStorage == 1) {
457     TString alienLocation = "/alien";
458     alienLocation += gGrid->Pwd();
459     alienLocation += fgridpath.Data();
460     alienLocation += "/";
461     alienLocation +=  localFileName;
462     alienLocation += "?se=";
463     alienLocation += fSE.Data();
464     fileName = alienLocation.Data();
465     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
466   }
467
468   TFile* ftag = TFile::Open(fileName, "recreate");
469   ftag->cd();
470   ttag->Fill();
471   tag->Clear();
472   ttag->Write();
473   ftag->Close();
474
475   gSystem->GetMemInfo(meminfo);
476   AliInfo(Form("After the file closing - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
477   tempmem = meminfo->fMemUsed;
478
479   delete ftag;
480   delete esd;
481
482   delete tag;
483   gSystem->GetMemInfo(meminfo);
484   AliInfo(Form("After the delete objects - Memory used: %d MB - Increase: %d MB",meminfo->fMemUsed,meminfo->fMemUsed - tempmem));
485 }
486
487 //_____________________________________________________________________________
488 void AliESDTagCreator::CreateTag(TFile* file, const char *filepath, Int_t Counter) {
489   //private method that creates tag files
490   /////////////
491   //muon code//
492   ////////////
493   //  Double_t fMUONMASS = 0.105658369;
494   //Variables
495 //   Double_t fX,fY,fZ ;
496 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
497 //   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
498 //   Int_t fCharge;
499 //   TLorentzVector fEPvector;
500
501 //   Float_t fLowPtCut = 1.0;
502 //   Float_t fHighPtCut = 3.0;
503 //   Float_t fVeryHighPtCut = 10.0;
504   ////////////
505
506 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
507
508   // Creates the tags for all the events in a given ESD file
509 //   Bool_t fIsSim = kTRUE;
510 //   Int_t ntrack;
511 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
512 //   Int_t nPos, nNeg, nNeutr;
513 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
514 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
515 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
516 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
517 //   Float_t maxPt = .0, etamaxPt = -999, phimaxPt = -999., meanPt = .0, totalP = .0;
518 //   Int_t fVertexflag;
519   Int_t iRunNumber = 0;
520 //   TString fVertexName;
521 //   TRefArray tmp;
522
523   AliRunTag *tag = new AliRunTag();
524   AliEventTag *evTag = new AliEventTag();
525   TTree * ttag = new TTree("T","A Tree with event tags");
526   TBranch * btag = ttag->Branch("AliTAG", &tag);
527   btag->SetCompressionLevel(9);
528   
529   AliInfo(Form("Creating the ESD tags......."));        
530   
531   Int_t firstEvent = 0,lastEvent = 0;
532   
533   TTree *t = (TTree*) file->Get("esdTree");
534   AliESDEvent *esd = new AliESDEvent();
535   esd->ReadFromTree(t);
536   
537   t->GetEntry(0);
538   Int_t iInitRunNumber = esd->GetRunNumber();
539   tag->SetMagneticField(esd->GetMagneticField());
540   tag->SetBeamEnergy(esd->GetBeamEnergy());
541   tag->SetBeamType(TString(esd->GetBeamType()));
542   tag->SetActiveTriggerClasses(esd->GetESDRun()->GetActiveTriggerClasses());
543
544   AliFileTag *eftag = new AliFileTag();
545   eftag->SetPath(filepath);
546   eftag->SetTURL(Form("local://%s", filepath));
547   eftag->SetSize(0);
548   eftag->SetMD5("");
549   tag->AddFileTag(eftag);
550
551   Int_t iNumberOfEvents = (Int_t)t->GetEntries();
552   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++) {
553     FillEventTag(t, evTag, iEventNumber, esd);
554     iRunNumber = esd->GetRunNumber();
555     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
556     //    evTag->SetPath(filepath);
557     
558     tag->SetRunId(iInitRunNumber);
559 //     if(fIsSim) tag->SetDataType(0);
560 //     else tag->SetDataType(1);
561     tag->AddEventTag(*evTag);
562   }//event loop
563   lastEvent = iNumberOfEvents;
564   
565   t->Delete("");
566   
567   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
568   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
569   localFileName += ".ESD.tag.root";
570
571   TString fileName;
572   
573   if(fStorage == 0) {
574     fileName = localFileName.Data();      
575     AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
576   }
577   else if(fStorage == 1) {
578     TString alienLocation = "/alien";
579     alienLocation += gGrid->Pwd();
580     alienLocation += fgridpath.Data();
581     alienLocation += "/";
582     alienLocation +=  localFileName;
583     alienLocation += "?se=";
584     alienLocation += fSE.Data();
585     fileName = alienLocation.Data();
586     AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
587   }
588
589   TFile* ftag = TFile::Open(fileName, "recreate");
590   ftag->cd();
591   ttag->Fill();
592   tag->Clear();
593   ttag->Write();
594   ftag->Close();
595
596   delete ftag;
597   delete esd;
598
599   delete tag;
600 }
601
602 //_____________________________________________________________________________
603 void AliESDTagCreator::CreateESDTags(Int_t fFirstEvent, Int_t fLastEvent, AliGRPObject *grpData, ULong_t * qa, Bool_t * es, Int_t qalength, Int_t eslength) {
604   //GRP
605   Float_t lhcLuminosity = 0.0;
606   TString lhcState = "test";
607   //UInt_t detectorMask = 0;
608   Int_t detectorMask = 0;
609
610   detectorMask = grpData->GetDetectorMask();
611   time_t startTime = grpData->GetTimeStart();
612   TTimeStamp t1(startTime);
613   time_t endTime = grpData->GetTimeEnd();
614   TTimeStamp t2(endTime);
615   const char* beamtype = grpData->GetBeamType();
616   Float_t beamenergy = grpData->GetBeamEnergy();
617
618
619   /////////////
620   //muon code//
621   ////////////
622 //   Double_t fMUONMASS = 0.105658369;
623   //Variables
624 //   Double_t fX,fY,fZ ;
625 //   Double_t fThetaX, fThetaY, fPyz, fChisquare;
626 //   Double_t fPxRec,fPyRec, fPzRec, fEnergy;
627 //   Int_t fCharge;
628 //   TLorentzVector fEPvector;
629
630 //   Float_t fLowPtCut = 1.0;
631 //   Float_t fHighPtCut = 3.0;
632 //   Float_t fVeryHighPtCut = 10.0;
633   ////////////
634
635 //   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
636
637   // Creates the tags for all the events in a given ESD file
638 //   Int_t ntrack;
639 //   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
640 //   Int_t nPos, nNeg, nNeutr;
641 //   Int_t nK0s, nNeutrons, nPi0s, nGamas;
642 //   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
643 //   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
644 //   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
645 //   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
646 //   Int_t fVertexflag;
647   Int_t iRunNumber = 0;
648   TString fguid, fmd5, fturl;
649 //   TString fVertexName("default");
650 //   TRefArray tmp;
651   
652   AliInfo(Form("Creating the ESD tags......."));        
653
654   TFile *esdfile = TFile::Open("AliESDs.root");
655   if (!esdfile || !esdfile->IsOpen()) {
656     AliError(Form("opening failed"));
657     delete esdfile;
658     return ;
659   }  
660   Int_t lastEvent = 0;
661   TTree *b = (TTree*) esdfile->Get("esdTree");
662   AliESDEvent *esd = new AliESDEvent();
663   esd->ReadFromTree(b);
664
665   b->GetEntry(fFirstEvent);
666   Int_t iInitRunNumber = esd->GetRunNumber();
667   
668   Int_t iNumberOfEvents = (Int_t)b->GetEntries();
669   if ((fLastEvent == -1) || ((Int_t) b->GetEntries() < fLastEvent))
670     lastEvent = (Int_t)b->GetEntries();
671   else lastEvent = fLastEvent;
672
673   char fileName[256];
674   sprintf(fileName, "Run%d.Event%d_%d.ESD.tag.root", 
675           iInitRunNumber,fFirstEvent,lastEvent);
676   AliInfo(Form("writing tags to file %s", fileName));
677   AliDebug(1, Form("writing tags to file %s", fileName));
678  
679   TFile* ftag = TFile::Open(fileName, "recreate");
680  
681   AliRunTag *tag = new AliRunTag();
682   AliEventTag *evTag = new AliEventTag();
683   TTree * ttag = new TTree("T","A Tree with event tags");
684   TBranch * btag = ttag->Branch("AliTAG", &tag);
685   btag->SetCompressionLevel(9);
686
687   if ((fLastEvent != -1) && ((Int_t) b->GetEntries() > fLastEvent)) 
688     iNumberOfEvents = fLastEvent + 1;
689
690   AliFileTag *eftag = new AliFileTag();
691   tag->AddFileTag(eftag);
692
693   for (Int_t iEventNumber = fFirstEvent; iEventNumber < iNumberOfEvents; iEventNumber++) {
694     FillEventTag(b, evTag, iEventNumber, esd);
695     iRunNumber = esd->GetRunNumber();
696     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
697
698     if (iEventNumber == fFirstEvent) {
699       TFile *file = b->GetCurrentFile();
700 //      const TUrl *url = file->GetEndpointUrl();
701       fguid = file->GetUUID().AsString();
702
703 //       if(fStorage == 1) {
704 //      TString fturltemp = "alien://"; fturltemp += url->GetFile();
705 //      fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
706 //       }
707 //       else fturl = url->GetFile(); 
708       fturl = file->GetName();
709       
710       //    evTag->SetGUID(fguid);
711       ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetGUID(fguid);
712       if(fStorage == 1) {
713         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetMD5("");
714         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetTURL(fturl);
715         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetSize(0);
716       }
717       else {
718         //      evTag->SetPath(fturl);
719         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetPath(fturl);
720         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetMD5("");
721         ((AliFileTag *) tag->GetFileTag(tag->GetNFiles()-1))->SetSize(0);
722       }
723
724     }
725
726     tag->AddEventTag(*evTag);
727   }    
728   
729   tag->SetLHCTag(lhcLuminosity,lhcState);
730   tag->SetDetectorTag(esd->GetESDRun()->GetDetectorsInDAQ(), esd->GetESDRun()->GetDetectorsInReco());
731   tag->SetActiveTriggerClasses(esd->GetESDRun()->GetActiveTriggerClasses());
732
733   // Get magnetic field info
734   Bool_t ok = kTRUE;
735   
736   Float_t l3Current = grpData->GetL3Current((AliGRPObject::Stats)0);
737   if (l3Current == AliGRPObject::GetInvalidFloat()) {
738     AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
739     ok = kFALSE;
740   }
741   
742   Char_t l3Polarity = grpData->GetL3Polarity();
743   if (l3Polarity == AliGRPObject::GetInvalidChar()) {
744     AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
745     ok = kFALSE;
746   }
747   
748   // Dipole
749   Float_t diCurrent = grpData->GetDipoleCurrent((AliGRPObject::Stats)0);
750   if (diCurrent == AliGRPObject::GetInvalidFloat()) {
751     AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
752     ok = kFALSE;
753   }
754   
755   Char_t diPolarity = grpData->GetDipolePolarity();
756   if (diPolarity == AliGRPObject::GetInvalidChar()) {
757     AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
758     ok = kFALSE;
759   }
760   
761   if (ok && grpData->IsPolarityConventionLHC()) {
762     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 1))
763       tag->SetMagneticField(-0.5);
764     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 0)) 
765       tag->SetMagneticField(0.5);
766     if (TMath::Abs(l3Current) < 2000.0) 
767       tag->SetMagneticField(0.0);
768     
769     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 1))
770       tag->SetDipoleField(-0.2);
771     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 0))
772       tag->SetDipoleField(0.2);
773     if (TMath::Abs(diCurrent) < 500.0)
774       tag->SetDipoleField(0.0);
775   }
776   
777   tag->SetRunId(iInitRunNumber);
778   tag->SetRunStartTime(t1.GetDate());
779   tag->SetRunStopTime(t2.GetDate());
780   tag->SetBeamEnergy(beamenergy);
781   tag->SetBeamType(beamtype);
782   
783   //QA setting 
784   tag->SetQAArray(qa, qalength) ; 
785   tag->SetEventSpecies(es, eslength) ;
786
787   ftag->cd();
788   ttag->Fill();
789   tag->Clear();
790   ttag->Write();
791   ftag->Close();
792   esdfile->cd();
793   delete esdfile;
794   delete ftag;
795   delete esd;
796   delete tag;
797   delete evTag;
798 }
799
800 void AliESDTagCreator::CreateESDTagsFullRun(TTree *chain, AliGRPObject *grpData, ULong_t * qa, Bool_t * es, Int_t qalength, Int_t eslength)
801 {
802   //GRP
803   Float_t lhcLuminosity = 0.0;
804   TString lhcState = "test";
805   //UInt_t detectorMask = 0;
806   Int_t detectorMask = 0;
807
808   detectorMask = grpData->GetDetectorMask();
809   time_t startTime = grpData->GetTimeStart();
810   TTimeStamp t1(startTime);
811   time_t endTime = grpData->GetTimeEnd();
812   TTimeStamp t2(endTime);
813   const char* beamtype = grpData->GetBeamType();
814   Float_t beamenergy = grpData->GetBeamEnergy();
815
816   Int_t iRunNumber = 0;
817   TString fguid, fmd5, fturl;
818   TString fturlold;
819
820   AliInfo(Form("Creating the ESD tags......."));        
821
822   AliESDEvent *esd = new AliESDEvent();
823   esd->ReadFromTree(chain);
824
825   chain->GetEntry(0);
826   Int_t iInitRunNumber = esd->GetRunNumber();
827   
828   Int_t iNumberOfEvents = (Int_t)chain->GetEntries();
829   Int_t iFirstEvent = 0;
830
831   char fileName[256];
832   sprintf(fileName, "Run%d.Event%d_%d.ESD.tag.root", 
833           iInitRunNumber,iFirstEvent,iNumberOfEvents);
834   AliInfo(Form("writing tags to file %s", fileName));
835   AliDebug(1, Form("writing tags to file %s", fileName));
836  
837   TFile* ftag = TFile::Open(fileName, "recreate");
838  
839   AliRunTag *tag = new AliRunTag();
840   AliEventTag *evTag = new AliEventTag();
841   TTree * ttag = new TTree("T","A Tree with event tags");
842   TBranch * btag = ttag->Branch("AliTAG", &tag);
843   btag->SetCompressionLevel(9);
844
845 //   AliFileTag *eftag = new AliFileTag();
846 //   tag->AddFileTag(*eftag);
847
848   for (Int_t iEventNumber = iFirstEvent; iEventNumber < iNumberOfEvents; iEventNumber++) {
849     FillEventTag(chain, evTag, iEventNumber, esd);
850
851     iRunNumber = esd->GetRunNumber();
852     if(iRunNumber != iInitRunNumber) AliFatal("Inconsistency of run numbers in the AliESD!!!");
853     
854     TFile *file = chain->GetCurrentFile();
855     //    const TUrl *url = file->GetName();
856     fguid = file->GetUUID().AsString();
857     fturl = file->GetName();
858 //     if(fStorage == 1) {
859 //       TString fturltemp = "alien://"; fturltemp += url->GetFile();
860 //       fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
861 //     }
862 //     else fturl = url->GetFile();
863
864     if (fturl.CompareTo(fturlold)) {
865
866       AliFileTag *eftag = new AliFileTag();
867       
868       //evTag->SetGUID(fguid);
869       eftag->SetGUID(fguid);
870       if(fStorage == 1) {
871         //       evTag->SetMD5("");
872         //       evTag->SetTURL(fturl);
873         //       evTag->SetSize(0);
874         eftag->SetPath("");
875         eftag->SetMD5("");
876         eftag->SetTURL(fturl);
877         eftag->SetSize(0);
878       }
879       else {
880         //       evTag->SetPath(fturl);
881         //       evTag->SetTURL(fturl);
882         eftag->SetPath(fturl);
883         eftag->SetTURL(fturl);
884         eftag->SetMD5("");
885         eftag->SetSize(0);
886       }
887
888       tag->AddFileTag(eftag);
889     
890       fturlold = fturl;
891       
892     }
893     else {
894       //      cout << "FileTag found " << fturl.Data() << " " << fturlold.Data() << endl;
895     }
896
897     tag->AddEventTag(*evTag);
898   }
899
900   tag->SetLHCTag(lhcLuminosity,lhcState);
901   tag->SetDetectorTag(esd->GetESDRun()->GetDetectorsInDAQ(), esd->GetESDRun()->GetDetectorsInReco());
902   tag->SetActiveTriggerClasses(esd->GetESDRun()->GetActiveTriggerClasses());
903   
904   // Get magnetic field info
905   Bool_t ok = kTRUE;
906   
907   Float_t l3Current = grpData->GetL3Current((AliGRPObject::Stats)0);
908   if (l3Current == AliGRPObject::GetInvalidFloat()) {
909     AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
910     ok = kFALSE;
911   }
912   
913   Char_t l3Polarity = grpData->GetL3Polarity();
914   if (l3Polarity == AliGRPObject::GetInvalidChar()) {
915     AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
916     ok = kFALSE;
917   }
918   
919   // Dipole
920   Float_t diCurrent = grpData->GetDipoleCurrent((AliGRPObject::Stats)0);
921   if (diCurrent == AliGRPObject::GetInvalidFloat()) {
922     AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
923     ok = kFALSE;
924   }
925   
926   Char_t diPolarity = grpData->GetDipolePolarity();
927   if (diPolarity == AliGRPObject::GetInvalidChar()) {
928     AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
929     ok = kFALSE;
930   }
931   
932   if (ok && grpData->IsPolarityConventionLHC()) {
933     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 1))
934       tag->SetMagneticField(-0.5);
935     if ((TMath::Abs(l3Current) > 29000.0) && (l3Polarity == 0)) 
936       tag->SetMagneticField(0.5);
937     if (TMath::Abs(l3Current) < 2000.0) 
938       tag->SetMagneticField(0.0);
939     
940     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 1))
941       tag->SetDipoleField(-0.2);
942     if ((TMath::Abs(diCurrent) > 5900.0) && (diPolarity == 0))
943       tag->SetDipoleField(0.2);
944     if (TMath::Abs(diCurrent) < 500.0)
945       tag->SetDipoleField(0.0);
946   }
947   
948   tag->SetRunId(iInitRunNumber);
949   tag->SetRunStartTime(t1.GetDate());
950   tag->SetRunStopTime(t2.GetDate());
951   tag->SetBeamEnergy(beamenergy);
952   tag->SetBeamType(beamtype);
953   
954   //QA setting 
955   tag->SetQAArray(qa, qalength) ; 
956   tag->SetEventSpecies(es, eslength) ;
957
958   ftag->cd();
959   ttag->Fill();
960   tag->Clear();
961   ttag->Write();
962   ftag->Close();
963   delete ftag;
964   delete esd;
965   delete tag;
966   delete evTag;
967 }
968
969 //_____________________________________________________________________________
970 void AliESDTagCreator::SwitchOffBranches() const {
971   //
972   // Switch of branches on user request
973   TObjArray * tokens = fBranches.Tokenize(" ");
974   Int_t ntok = tokens->GetEntries();
975   for (Int_t i = 0; i < ntok; i++)  {
976     TString str = ((TObjString*) tokens->At(i))->GetString();
977     fChain->SetBranchStatus(Form("%s%s%s","*", str.Data(), "*"), 0);
978     AliInfo(Form("Branch %s switched off \n", str.Data()));
979   }
980 }
981
982 //_____________________________________________________________________________
983 void AliESDTagCreator::FillEventTag(TTree *chain, AliEventTag *evTag, Int_t iEventNumber, AliESDEvent *esd)
984 {
985   // Fill the event specific information in the EventTag
986   AliESD *esdold = 0x0;
987
988   TString fTempGuid;
989
990   /////////////
991   //muon code//
992   ////////////
993   Double_t fMUONMASS = 0.105658369;
994   //Variables
995   Double_t fX,fY,fZ ;
996   Double_t fThetaX, fThetaY, fPyz, fChisquare;
997   Double_t fPxRec, fPyRec, fPzRec, fEnergy;
998   Int_t fCharge;
999   TLorentzVector fEPvector;
1000
1001   Float_t fLowPtCut      =  1.0;
1002   Float_t fHighPtCut     =  3.0;
1003   Float_t fVeryHighPtCut = 10.0;
1004   ////////////
1005
1006   Double_t partFrac[5] = {0.01, 0.01, 0.85, 0.10, 0.05};
1007
1008   // Creates the tags for all the events in a given ESD file
1009   Bool_t fIsSim = kTRUE;
1010   Int_t ntrack;
1011   Int_t nProtons, nKaons, nPions, nMuons, nElectrons, nFWMuons, nFWMatchedMuons;
1012   Int_t nPos, nNeg, nNeutr;
1013   Int_t nK0s, nLambdas, nNeutrons, nPi0s, nGamas;
1014   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
1015   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
1016   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
1017   Float_t maxPt = .0, etamaxPt = -999., phimaxPt = -999., meanPt = .0, totalP = .0;
1018   Int_t fVertexflag;
1019   TString fVertexName;
1020
1021   TRefArray tmp;
1022   
1023   ntrack = 0; nPos = 0; nNeg = 0; nNeutr =0;
1024   nK0s = 0; nLambdas = 0; nNeutrons = 0; nPi0s = 0;
1025   nGamas = 0; nProtons = 0; nKaons = 0;
1026   nPions = 0; nMuons = 0; nElectrons = 0; nFWMuons = 0; nFWMatchedMuons = 0;      
1027   nCh1GeV = 0; nCh3GeV = 0; nCh10GeV = 0;
1028   nMu1GeV = 0; nMu3GeV = 0; nMu10GeV = 0;
1029   nEl1GeV = 0; nEl3GeV = 0; nEl10GeV = 0;
1030   maxPt = .0; etamaxPt = -999.; phimaxPt = -999.; meanPt = .0; totalP = .0;
1031   fVertexflag = 1;
1032   
1033   chain->GetEntry(iEventNumber);    
1034   esdold = esd->GetAliESDOld();
1035   if(esdold) esd->CopyFromOldESD();
1036   
1037 //   TFile *file = chain->GetFile();
1038 //   const TUrl *url = file->GetEndpointUrl();
1039 //   fguid = file->GetUUID().AsString();
1040 //   if(fSession == "grid") {
1041 //     TString fturltemp = "alien://"; fturltemp += url->GetFile();
1042 //     fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
1043 //   }
1044 //   else fturl = url->GetFile();
1045   
1046   const AliESDVertex * vertexIn = esd->GetVertex();
1047   fVertexName = vertexIn->GetName();
1048   if(fVertexName == "default") fVertexflag = 0;
1049   
1050   for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++) {
1051     AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
1052     if(esdTrack->GetLabel() != 0) fIsSim = kTRUE;
1053     else if(esdTrack->GetLabel() == 0) fIsSim = kFALSE;
1054     UInt_t status = esdTrack->GetStatus();
1055     
1056     //select only tracks with ITS refit
1057     if ((status&AliESDtrack::kITSrefit)==0) continue;
1058     //select only tracks with TPC refit
1059     if ((status&AliESDtrack::kTPCrefit)==0) continue;
1060     
1061     //select only tracks with the "combined PID"
1062     if ((status&AliESDtrack::kESDpid)==0) continue;
1063     Double_t p[3];
1064     esdTrack->GetPxPyPz(p);
1065     Double_t pt2 = p[0]*p[0]+p[1]*p[1];
1066     Double_t momentum = TMath::Sqrt(pt2+p[2]*p[2]);
1067     Double_t fPt = TMath::Sqrt(pt2);
1068     totalP += momentum;
1069     meanPt += fPt;
1070     if(fPt > maxPt) {
1071       maxPt = fPt;
1072       phimaxPt = esdTrack->Phi();
1073       etamaxPt = esdTrack->Eta();
1074     }
1075     
1076     if(esdTrack->GetSign() > 0) {
1077       nPos++;
1078       if(fPt > fLowPtCut) nCh1GeV++;
1079       if(fPt > fHighPtCut) nCh3GeV++;
1080       if(fPt > fVeryHighPtCut) nCh10GeV++;
1081     }
1082     if(esdTrack->GetSign() < 0) {
1083       nNeg++;
1084       if(fPt > fLowPtCut) nCh1GeV++;
1085       if(fPt > fHighPtCut) nCh3GeV++;
1086       if(fPt > fVeryHighPtCut) nCh10GeV++;
1087     }
1088     if(esdTrack->GetSign() == 0) nNeutr++;
1089     
1090     //PID
1091     Double_t prob[5];
1092     esdTrack->GetESDpid(prob);
1093     
1094     Double_t rcc = 0.0;
1095     for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
1096     if(rcc == 0.0) continue;
1097     //Bayes' formula
1098     Double_t w[5];
1099     for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
1100     
1101     //protons
1102     if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
1103     //kaons
1104     if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
1105     //pions
1106     if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++; 
1107     //electrons
1108     if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
1109       nElectrons++;
1110       if(fPt > fLowPtCut) nEl1GeV++;
1111       if(fPt > fHighPtCut) nEl3GeV++;
1112       if(fPt > fVeryHighPtCut) nEl10GeV++;
1113     }     
1114     ntrack++;
1115   }
1116   //esd track loop
1117   
1118   /* muon code */
1119   Int_t nMuonTracks = esd->GetNumberOfMuonTracks();
1120   // loop over all reconstructed tracks (also first track of combination)
1121   for (Int_t iTrack = 0; iTrack <  nMuonTracks;  iTrack++) {
1122     AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
1123     if (muonTrack == 0x0) continue;
1124     
1125     // Coordinates at vertex
1126     fZ = muonTrack->GetZ(); 
1127     fY = muonTrack->GetBendingCoor();
1128     fX = muonTrack->GetNonBendingCoor(); 
1129     
1130     fThetaX = muonTrack->GetThetaX();
1131     fThetaY = muonTrack->GetThetaY();
1132     
1133     fPyz = 1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
1134     fPzRec = - fPyz / TMath::Sqrt(1.0 + TMath::Tan(fThetaY)*TMath::Tan(fThetaY));
1135     fPxRec = fPzRec * TMath::Tan(fThetaX);
1136     fPyRec = fPzRec * TMath::Tan(fThetaY);
1137     fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
1138     
1139     //ChiSquare of the track if needed
1140     fChisquare = muonTrack->GetChi2()/(2.0 * muonTrack->GetNHit() - 5);
1141     fEnergy = TMath::Sqrt(fMUONMASS * fMUONMASS + fPxRec * fPxRec + fPyRec * fPyRec + fPzRec * fPzRec);
1142     fEPvector.SetPxPyPzE(fPxRec, fPyRec, fPzRec, fEnergy);
1143     
1144     if (muonTrack->GetMatchTrigger()>0) nFWMatchedMuons++;
1145     
1146     nMuons++;
1147     nFWMuons++;
1148     if(fEPvector.Pt() > fLowPtCut) {
1149       nMu1GeV++; 
1150       if(fEPvector.Pt() > fHighPtCut) {
1151         nMu3GeV++; 
1152         if (fEPvector.Pt() > fVeryHighPtCut) {
1153           nMu10GeV++;
1154         }
1155       }
1156     }
1157   }//muon track loop
1158   
1159   // Fill the event tags 
1160   if(ntrack != 0) meanPt = meanPt/ntrack;
1161   
1162   //AliInfo(Form("====================================="));
1163   //AliInfo(Form("URL: %s - GUID: %s",fturl.Data(),fguid.Data()));
1164   //AliInfo(Form("====================================="));
1165   
1166   //First physics data
1167   const AliMultiplicity *spdMult = esd->GetMultiplicity();
1168   evTag->SetNumberOfFiredChipsLayer1(spdMult->GetNumberOfFiredChips(0));
1169   evTag->SetNumberOfFiredChipsLayer2(spdMult->GetNumberOfFiredChips(1));
1170   evTag->SetNumberOfSPDTracklets(spdMult->GetNumberOfTracklets());
1171   
1172   AliESDVZERO *vzeroData = esd->GetVZEROData();
1173   evTag->SetMTotV0A(vzeroData->GetMTotV0A());
1174   evTag->SetMTotV0C(vzeroData->GetMTotV0C());
1175   evTag->SetNbPMV0A(vzeroData->GetNbPMV0A());
1176   evTag->SetNbPMV0C(vzeroData->GetNbPMV0C());
1177   
1178   //evTag->SetEventId(iEventNumber+1);
1179   evTag->SetPeriodNumber(esd->GetPeriodNumber());
1180   evTag->SetOrbitNumber(esd->GetOrbitNumber());
1181   evTag->SetBunchCrossNumber(esd->GetBunchCrossNumber());
1182   //  evTag->SetGUID(fguid);
1183 //   if(fSession == "grid") {
1184 //     evTag->SetMD5("");
1185 //     evTag->SetTURL(fturl);
1186 //     evTag->SetSize(0);
1187 //   }
1188 //   else evTag->SetPath(fturl);
1189   
1190   evTag->SetVertexX(vertexIn->GetXv());
1191   evTag->SetVertexY(vertexIn->GetYv());
1192   evTag->SetVertexZ(vertexIn->GetZv());
1193   evTag->SetVertexZError(vertexIn->GetZRes());
1194   evTag->SetVertexFlag(fVertexflag);
1195   
1196   evTag->SetT0VertexZ(esd->GetT0zVertex());
1197   
1198   evTag->SetTriggerMask(esd->GetTriggerMask());
1199   evTag->SetTriggerCluster(esd->GetTriggerCluster());
1200   
1201   evTag->SetEventType(esd->GetEventType());
1202   //*T*  evTag->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());
1203   
1204   evTag->SetZDCNeutron1Energy(esd->GetZDCN1Energy());
1205   evTag->SetZDCProton1Energy(esd->GetZDCP1Energy());
1206   evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
1207   evTag->SetZDCNeutron2Energy(esd->GetZDCN2Energy());
1208   evTag->SetZDCProton2Energy(esd->GetZDCP2Energy());
1209   evTag->SetNumOfParticipants(esd->GetZDCParticipants());
1210   
1211   evTag->SetNumOfTracks(esd->GetNumberOfTracks());
1212   evTag->SetNumOfPosTracks(nPos);
1213   evTag->SetNumOfNegTracks(nNeg);
1214   evTag->SetNumOfNeutrTracks(nNeutr);
1215   
1216   evTag->SetNumOfV0s(esd->GetNumberOfV0s());
1217   evTag->SetNumOfCascades(esd->GetNumberOfCascades());
1218   evTag->SetNumOfKinks(esd->GetNumberOfKinks());
1219   evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
1220   
1221   evTag->SetNumOfProtons(nProtons);
1222   evTag->SetNumOfKaons(nKaons);
1223   evTag->SetNumOfPions(nPions);
1224   evTag->SetNumOfMuons(nMuons);
1225   evTag->SetNumOfFWMuons(nFWMuons);
1226   evTag->SetNumOfFWMatchedMuons(nFWMatchedMuons);
1227   evTag->SetNumOfElectrons(nElectrons);
1228   evTag->SetNumOfPhotons(nGamas);
1229   evTag->SetNumOfPi0s(nPi0s);
1230   evTag->SetNumOfNeutrons(nNeutrons);
1231   evTag->SetNumOfKaon0s(nK0s);
1232   evTag->SetNumOfLambdas(nLambdas);
1233   
1234   evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
1235   evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
1236   evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
1237   evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
1238   evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
1239   evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
1240   evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
1241   evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
1242   evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
1243
1244   tmp.Clear();
1245   evTag->SetNumOfPHOSClusters(esd->GetPHOSClusters(&tmp));
1246   tmp.Clear();
1247   evTag->SetNumOfEMCALClusters(esd->GetEMCALClusters(&tmp));
1248     
1249   evTag->SetTotalMomentum(totalP);
1250   evTag->SetMeanPt(meanPt);
1251   evTag->SetMaxPt(maxPt);
1252   evTag->SetEtaMaxPt(etamaxPt);
1253   evTag->SetPhiMaxPt(phimaxPt);
1254
1255   evTag->SetPhysicsFlag(kTRUE);
1256   evTag->SetBackgroungFlag(kFALSE);
1257 }
1258
1259 void AliESDTagCreator::CreateESDRunTagSummary(TTree *chain)
1260 {
1261   // Merge all tags from a run into a single RunTag
1262   // with only the File tags
1263   AliRunTag *rtag;
1264   chain->SetBranchAddress("AliTAG", &rtag);
1265
1266   TFile* ftag = TFile::Open("RunTagSummary.tag.root", "recreate");
1267
1268   AliRunTag *tag = new AliRunTag();
1269   TTree * ttag = new TTree("T","A Tree with event tags");
1270   TBranch * btag = ttag->Branch("AliTAG", &tag);
1271   btag->SetCompressionLevel(9);
1272   
1273   for (int itag=0; itag<chain->GetEntries(); itag++) {
1274     chain->GetEntry(itag);
1275     tag->CopyStandardContent(rtag);
1276     
1277     ttag->Fill();
1278     tag->Clear();
1279   }
1280
1281   ftag->cd();
1282   ttag->Write();
1283   ftag->Close();
1284   
1285 }