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