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