]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagCreator.cxx
Removing the autentication since it is external to the tag processing (P.Christakoglou)
[u/mrichter/AliRoot.git] / STEER / AliTagCreator.cxx
1 /**************************************************************************
2  * Author: Panos Christakoglou.                                           *
3  * Contributors are mentioned in the code where appropriate.              *
4  *                                                                        *
5  * Permission to use, copy, modify and distribute this software and its   *
6  * documentation strictly for non-commercial purposes is hereby granted   *
7  * without fee, provided that the above copyright notice appears in all   *
8  * copies and that both the copyright notice and this permission notice   *
9  * appear in the supporting documentation. The authors make no claims     *
10  * about the suitability of this software for any purpose. It is          *
11  * provided "as is" without express or implied warranty.                  *
12  **************************************************************************/
13
14 /* $Id$ */
15
16 //-----------------------------------------------------------------
17 //           AliTagCreator class
18 //   This is the class to deal with the tag creation (post process)
19 //   Origin: Panos Christakoglou, UOA-CERN, Panos.Christakoglou@cern.ch
20 //-----------------------------------------------------------------
21
22 //ROOT
23 #include <TFile.h>
24 #include <TString.h>
25 #include <TTree.h>
26 #include <TSystem.h>
27 #include <TChain.h>
28
29 //ROOT-AliEn
30 #include <TGrid.h>
31 #include <TGridResult.h>
32
33 //AliRoot
34 #include "AliRunTag.h"
35 #include "AliEventTag.h"
36 #include "AliESD.h"
37 #include "AliESDVertex.h"
38 #include "AliLog.h"
39
40
41 #include "AliTagCreator.h"
42
43
44 ClassImp(AliTagCreator)
45
46
47 //______________________________________________________________________________
48 AliTagCreator::AliTagCreator() //local mode
49 {
50   //==============Default constructor for a AliTagCreator==================
51   fgridpath = "";
52   fSE = "";   
53   fStorage = 0; 
54 }
55
56 //______________________________________________________________________________
57 AliTagCreator::~AliTagCreator()
58 {
59 //================Default destructor for a AliTagCreator=======================
60 }
61
62 //______________________________________________________________________________
63 void AliTagCreator::SetStorage(Int_t storage)
64 {
65   // Sets correctly the storage: 0 for local, 1 for GRID
66   fStorage = storage;
67   if(fStorage == 0)
68     AliInfo(Form("Tags will be stored locally...."));
69   if(fStorage == 1)
70     AliInfo(Form("Tags will be stored in the grid...."));
71   if((fStorage != 0)&&(fStorage != 1))
72     {
73       AliInfo(Form("Storage was not properly set!!!"));
74       abort();
75     }  
76 }
77
78
79 //______________________________________________________________________________
80 Bool_t AliTagCreator::ReadESDCollection(TGridResult *fresult)
81 {
82   // Reads the entry of the TGridResult and creates the tags
83   Int_t nEntries = fresult->GetEntries();
84
85   TString alienUrl;
86   const char *guid;
87   const char *md5;
88   const char *turl;
89   Long64_t size = -1;
90
91   Int_t counter = 0;
92   for(Int_t i = 0; i < nEntries; i++)
93     {
94       alienUrl = fresult->GetKey(i,"turl");
95       guid = fresult->GetKey(i,"guid");
96       if(fresult->GetKey(i,"size"))
97         size = atol (fresult->GetKey(i,"size"));
98       md5 = fresult->GetKey(i,"md5");
99       turl = fresult->GetKey(i,"turl");
100       if(md5 && !strlen(guid))
101         md5 = 0;
102       if(guid && !strlen(guid))
103         guid = 0;
104
105       TFile *f = TFile::Open(alienUrl,"READ");
106       CreateTag(f,guid,md5,turl,size,counter);
107       f->Close();
108       delete f;  
109       counter += 1;
110     }//grid result loop
111
112   return kTRUE;
113 }
114
115 //__________________________________________________________________________
116 Bool_t AliTagCreator::MergeTags()
117 {
118   //Merges the tags and stores the merged tag file 
119   //locally if fStorage=0 or in the grid if fStorage=1
120   AliInfo(Form("Merging tags....."));
121   TChain *fgChain = new TChain("T");
122
123   if(fStorage == 0) {
124     const char * tagPattern = "tag";
125     // Open the working directory
126     void * dirp = gSystem->OpenDirectory(gSystem->pwd());
127     const char * name = 0x0;
128     // Add all files matching *pattern* to the chain
129     while((name = gSystem->GetDirEntry(dirp))) {
130       if (strstr(name,tagPattern))       
131         fgChain->Add(name);  
132     }//directory loop
133     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
134   }//local mode
135
136   else if(fStorage == 1) {
137     TString alienLocation = gGrid->Pwd();
138     alienLocation += fgridpath.Data();
139     alienLocation += "/";
140
141     TGridResult *tagresult = gGrid->Query(alienLocation,"*tag.root","","");
142     Int_t nEntries = tagresult->GetEntries();
143     for(Int_t i = 0; i < nEntries; i++) {
144       TString alienUrl = tagresult->GetKey(i,"turl");
145       fgChain->Add(alienUrl);
146     }//grid result loop      
147     AliInfo(Form("Chained tag files: %d",fgChain->GetEntries()));
148   }//grid mode
149  
150   AliRunTag *tag = new AliRunTag;
151   AliEventTag *evTag = new AliEventTag;
152   fgChain->SetBranchAddress("AliTAG",&tag);
153    
154   //Defining new tag objects
155   AliRunTag *newTag = new AliRunTag();
156   TTree ttag("T","A Tree with event tags");
157   TBranch * btag = ttag.Branch("AliTAG", &newTag);
158   btag->SetCompressionLevel(9);
159   for(Int_t iTagFiles = 0; iTagFiles < fgChain->GetEntries(); iTagFiles++) {
160     fgChain->GetEntry(iTagFiles);
161     newTag->SetRunId(tag->GetRunId());
162     const TClonesArray *tagList = tag->GetEventTags();
163     for(Int_t j = 0; j < tagList->GetEntries(); j++) {
164       evTag = (AliEventTag *) tagList->At(j);
165       newTag->AddEventTag(*evTag);
166     }
167     ttag.Fill();
168     newTag->Clear();
169   }//tag file loop 
170   
171   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
172   localFileName += ".Merged"; localFileName += ".ESD.tag.root";
173      
174   TString alienFileName = "/alien";
175   alienFileName += gGrid->Pwd();
176   alienFileName += fgridpath.Data();
177   alienFileName += "/";
178   alienFileName +=  localFileName;
179   alienFileName += "?se=";
180   alienFileName += fSE.Data();
181
182   TString filename = 0x0;
183   
184   if(fStorage == 0) {
185     filename = localFileName.Data();      
186     AliInfo(Form("Writing merged tags to local file: %s",filename.Data()));
187   } 
188   if(fStorage == 1) {
189     filename = alienFileName.Data();
190     AliInfo(Form("Writing merged tags to grid file: %s",filename.Data()));     
191   }
192   
193   TFile* ftag = TFile::Open(filename, "recreate");
194   ftag->cd();
195   ttag.Write();
196   ftag->Close();
197
198   delete tag;
199   delete evTag;
200   delete newTag;
201
202   return kTRUE;
203 }
204
205 //_____________________________________________________________________________
206 void AliTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter)
207 {
208   // Creates the tags for all the events in a given ESD file
209   Int_t ntrack;
210   Int_t nProtons, nKaons, nPions, nMuons, nElectrons;
211   Int_t nPos, nNeg, nNeutr;
212   Int_t nK0s, nNeutrons, nPi0s, nGamas;
213   Int_t nCh1GeV, nCh3GeV, nCh10GeV;
214   Int_t nMu1GeV, nMu3GeV, nMu10GeV;
215   Int_t nEl1GeV, nEl3GeV, nEl10GeV;
216   Float_t maxPt = .0, meanPt = .0, totalP = .0;
217
218   AliRunTag *tag = new AliRunTag();
219   AliEventTag *evTag = new AliEventTag();
220   TTree ttag("T","A Tree with event tags");
221   TBranch * btag = ttag.Branch("AliTAG", &tag);
222   btag->SetCompressionLevel(9);
223   
224   AliInfo(Form("Creating the tags......."));    
225   
226   Int_t firstEvent = 0,lastEvent = 0;
227   TTree *t = (TTree*) file->Get("esdTree");
228   TBranch * b = t->GetBranch("ESD");
229   AliESD *esd = 0;
230   b->SetAddress(&esd);
231   
232   tag->SetRunId(esd->GetRunNumber());
233   
234   Int_t iNumberOfEvents = b->GetEntries();
235   for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++)
236     {
237       ntrack = 0;
238       nPos = 0;
239       nNeg = 0;
240       nNeutr =0;
241       nK0s = 0;
242       nNeutrons = 0;
243       nPi0s = 0;
244       nGamas = 0;
245       nProtons = 0;
246       nKaons = 0;
247       nPions = 0;
248       nMuons = 0;
249       nElectrons = 0;     
250       nCh1GeV = 0;
251       nCh3GeV = 0;
252       nCh10GeV = 0;
253       nMu1GeV = 0;
254       nMu3GeV = 0;
255       nMu10GeV = 0;
256       nEl1GeV = 0;
257       nEl3GeV = 0;
258       nEl10GeV = 0;
259       maxPt = .0;
260       meanPt = .0;
261       totalP = .0;
262       
263       b->GetEntry(iEventNumber);
264       const AliESDVertex * vertexIn = esd->GetVertex();
265       
266       for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++)
267         {
268           AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
269           UInt_t status = esdTrack->GetStatus();
270           
271           //select only tracks with ITS refit
272           if ((status&AliESDtrack::kITSrefit)==0) continue;
273           
274           //select only tracks with TPC refit-->remove extremely high Pt tracks
275           if ((status&AliESDtrack::kTPCrefit)==0) continue;
276           
277           //select only tracks with the "combined PID"
278           if ((status&AliESDtrack::kESDpid)==0) continue;
279           Double_t p[3];
280           esdTrack->GetPxPyPz(p);
281           Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
282           Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
283           totalP += momentum;
284           meanPt += fPt;
285           if(fPt > maxPt)
286             maxPt = fPt;
287           
288           if(esdTrack->GetSign() > 0)
289             {
290               nPos++;
291               if(fPt > 1.0)
292                 nCh1GeV++;
293               if(fPt > 3.0)
294                 nCh3GeV++;
295               if(fPt > 10.0)
296                 nCh10GeV++;
297             }
298           if(esdTrack->GetSign() < 0)
299             {
300               nNeg++;
301               if(fPt > 1.0)
302                 nCh1GeV++;
303               if(fPt > 3.0)
304                 nCh3GeV++;
305               if(fPt > 10.0)
306                 nCh10GeV++;
307             }
308           if(esdTrack->GetSign() == 0)
309             nNeutr++;
310           
311           //PID
312           Double_t prob[10];
313           esdTrack->GetESDpid(prob);
314           
315           //K0s
316           if ((prob[8]>prob[7])&&(prob[8]>prob[6])&&(prob[8]>prob[5])&&(prob[8]>prob[4])&&(prob[8]>prob[3])&&(prob[8]>prob[2])&&(prob[8]>prob[1])&&(prob[8]>prob[0]))
317             nK0s++;
318           //neutrons
319           if ((prob[7]>prob[8])&&(prob[7]>prob[6])&&(prob[7]>prob[5])&&(prob[7]>prob[4])&&(prob[7]>prob[3])&&(prob[7]>prob[2])&&(prob[7]>prob[1])&&(prob[7]>prob[0]))
320             nNeutrons++; 
321           //pi0s
322           if ((prob[6]>prob[8])&&(prob[6]>prob[7])&&(prob[6]>prob[5])&&(prob[6]>prob[4])&&(prob[6]>prob[3])&&(prob[6]>prob[2])&&(prob[6]>prob[1])&&(prob[6]>prob[0]))
323             nPi0s++;
324           //gamas
325           if ((prob[5]>prob[8])&&(prob[5]>prob[7])&&(prob[5]>prob[6])&&(prob[5]>prob[4])&&(prob[5]>prob[3])&&(prob[5]>prob[2])&&(prob[5]>prob[1])&&(prob[5]>prob[0]))
326             nGamas++;
327           //protons
328           if ((prob[4]>prob[8])&&(prob[4]>prob[7])&&(prob[4]>prob[6])&&(prob[4]>prob[5])&&(prob[4]>prob[3])&&(prob[4]>prob[2])&&(prob[4]>prob[1])&&(prob[4]>prob[0]))
329             nProtons++;
330           //kaons
331           if ((prob[3]>prob[8])&&(prob[3]>prob[7])&&(prob[3]>prob[6])&&(prob[3]>prob[5])&&(prob[3]>prob[4])&&(prob[3]>prob[2])&&(prob[3]>prob[1])&&(prob[3]>prob[0]))
332             nKaons++;
333           //pions
334           if ((prob[2]>prob[8])&&(prob[2]>prob[7])&&(prob[2]>prob[6])&&(prob[2]>prob[5])&&(prob[2]>prob[4])&&(prob[2]>prob[3])&&(prob[2]>prob[1])&&(prob[2]>prob[0]))
335             nPions++; 
336           //muons
337           if ((prob[1]>prob[8])&&(prob[1]>prob[7])&&(prob[1]>prob[6])&&(prob[1]>prob[5])&&(prob[1]>prob[4])&&(prob[1]>prob[3])&&(prob[1]>prob[2])&&(prob[1]>prob[0]))
338             {
339               nMuons++;
340               if(fPt > 1.0)
341                 nMu1GeV++;
342               if(fPt > 3.0)
343                 nMu3GeV++;
344               if(fPt > 10.0)
345                 nMu10GeV++;
346             }
347           //electrons
348           if ((prob[0]>prob[8])&&(prob[0]>prob[7])&&(prob[0]>prob[6])&&(prob[0]>prob[5])&&(prob[0]>prob[4])&&(prob[0]>prob[3])&&(prob[0]>prob[2])&&(prob[0]>prob[1]))
349             {
350               nElectrons++;
351               if(fPt > 1.0)
352                 nEl1GeV++;
353               if(fPt > 3.0)
354                 nEl3GeV++;
355               if(fPt > 10.0)
356                 nEl10GeV++;
357             }     
358           ntrack++;
359         }//track loop
360       // Fill the event tags 
361       if(ntrack != 0)
362         meanPt = meanPt/ntrack;
363       
364       evTag->SetEventId(iEventNumber+1);
365       evTag->SetGUID(guid);
366       evTag->SetMD5(md5);
367       evTag->SetTURL(turl);
368       evTag->SetSize(size);
369       evTag->SetVertexX(vertexIn->GetXv());
370       evTag->SetVertexY(vertexIn->GetYv());
371       evTag->SetVertexZ(vertexIn->GetZv());
372       
373       evTag->SetT0VertexZ(esd->GetT0zVertex());
374       
375       evTag->SetTrigger(esd->GetTrigger());
376       
377       evTag->SetZDCNeutronEnergy(esd->GetZDCNEnergy());
378       evTag->SetZDCProtonEnergy(esd->GetZDCPEnergy());
379       evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
380       evTag->SetNumOfParticipants(esd->GetZDCParticipants());
381       
382       
383       evTag->SetNumOfTracks(esd->GetNumberOfTracks());
384       evTag->SetNumOfPosTracks(nPos);
385       evTag->SetNumOfNegTracks(nNeg);
386       evTag->SetNumOfNeutrTracks(nNeutr);
387       
388       evTag->SetNumOfV0s(esd->GetNumberOfV0s());
389       evTag->SetNumOfCascades(esd->GetNumberOfCascades());
390       evTag->SetNumOfKinks(esd->GetNumberOfKinks());
391       evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
392       
393       evTag->SetNumOfProtons(nProtons);
394       evTag->SetNumOfKaons(nKaons);
395       evTag->SetNumOfPions(nPions);
396       evTag->SetNumOfMuons(nMuons);
397       evTag->SetNumOfElectrons(nElectrons);
398       evTag->SetNumOfPhotons(nGamas);
399       evTag->SetNumOfPi0s(nPi0s);
400       evTag->SetNumOfNeutrons(nNeutrons);
401       evTag->SetNumOfKaon0s(nK0s);
402       
403       evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
404       evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
405       evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
406       evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
407       evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
408       evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
409       evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
410       evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
411       evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
412       
413       evTag->SetNumOfPHOSTracks(esd->GetNumberOfPHOSParticles());
414       evTag->SetNumOfEMCALTracks(esd->GetNumberOfEMCALParticles());
415       
416       evTag->SetTotalMomentum(totalP);
417       evTag->SetMeanPt(meanPt);
418       evTag->SetMaxPt(maxPt);
419   
420       tag->AddEventTag(*evTag);
421     }
422   lastEvent = iNumberOfEvents;
423   
424   t->Delete("");
425         
426   ttag.Fill();
427   tag->Clear();
428
429   TString localFileName = "Run"; localFileName += tag->GetRunId(); 
430   localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
431   localFileName += ".ESD.tag.root";
432
433   TString alienLocation = "/alien";
434   alienLocation += gGrid->Pwd();
435   alienLocation += fgridpath.Data();
436   alienLocation += "/";
437   alienLocation +=  localFileName;
438   alienLocation += "?se=";
439   alienLocation += fSE.Data();
440
441   TString fileName;
442   
443   if(fStorage == 0)
444     {
445       fileName = localFileName.Data();      
446       AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
447     }
448   if(fStorage == 1)
449     {
450       fileName = alienLocation.Data();
451       AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
452     }
453
454   TFile* ftag = TFile::Open(fileName, "recreate");
455   ftag->cd();
456   ttag.Write();
457   ftag->Close();
458
459   delete ftag;
460   delete esd;
461
462   delete tag;
463   delete evTag;
464 }
465