]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagCreator.cxx
Replacing TAlienCollection with TGridResult (P.Christakouglou)
[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 <TObject.h>
24 #include <Riostream.h>
25 #include <TSystem.h>
26 #include <TChain.h>
27 #include <TFile.h>
28 #include <TString.h>
29 #include <THashTable.h>
30
31 //ROOT-AliEn
32 #include <TGrid.h>
33 //#include <TAlienCollection.h>
34 #include <TGridResult.h>
35 #include <TFileMerger.h>
36 #include <TMap.h>
37 #include <TXMLParser.h>
38 #include <TThread.h>
39 #include <TTreePlayer.h>
40 #include <TProof.h>
41
42 //AliRoot
43 #include "AliRunTag.h"
44 #include "AliEventTag.h"
45 #include "AliESD.h"
46 #include "AliESDVertex.h"
47 #include "AliPID.h"
48 #include "AliESDpid.h"
49 #include "AliLog.h"
50
51
52 #include "AliTagCreator.h"
53
54
55 ClassImp(AliTagCreator)
56
57
58 //______________________________________________________________________________
59 AliTagCreator::AliTagCreator() //local mode
60 {
61   //==============Default constructor for a AliTagCreator==================
62   fUser = "";
63   fPasswd = "";  
64   fSE = "";   
65   fHost = "";
66   fPort = 0; 
67   fresult = 0;
68 }
69
70 //______________________________________________________________________________
71 AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username)
72 {
73   //==============Default constructor for a AliTagCreator==================
74   fresult = 0;
75   fHost = host;
76   fUser = username;
77   fPort = port;
78
79   TString grid = "alien://";
80   grid += fHost;
81   grid += ":";
82   grid += port;
83   
84   //connect to the grid
85   TGrid::Connect(grid.Data(),fUser.Data());
86   if(!gGrid)
87     {
88       AliError("Connection failed!!!");
89       return;
90     }
91 }
92
93
94 //______________________________________________________________________________
95 AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username, const char *passwd)
96 {
97   //==============Default constructor for a AliTagCreator==================
98   fresult = 0;
99   fHost = host;
100   fUser = username;
101   fPasswd = passwd;
102   fPort = port;
103
104   TString grid = "alien://";
105   grid += fHost;
106   grid += ":";
107   grid += port;
108   
109   //connect to the grid
110   TGrid::Connect(grid.Data(),fUser.Data(),fPasswd.Data());
111    if(!gGrid)
112     {
113       AliError("Connection failed!!!");
114       return;
115     }
116 }
117
118
119 //______________________________________________________________________________
120 AliTagCreator::~AliTagCreator()
121 {
122 //================Default destructor for a AliTagCreator=======================
123 }
124
125 //______________________________________________________________________________
126 void AliTagCreator::SetSE(const char *se)
127 {
128   fSE = se;
129 }
130
131 //______________________________________________________________________________
132 Bool_t AliTagCreator::ConnectToGrid(const char *host, Int_t port, const char *username)
133 {
134   fHost = host;
135   fUser = username;
136   fPort = port;
137
138   TString grid = "alien://";
139   grid += fHost;
140   grid += ":";
141   grid += port;
142   
143   //connect to the grid
144   TGrid::Connect(grid.Data(),fUser.Data());
145   if(!gGrid)
146     {
147       AliError("Connection failed!!!");
148       return kFALSE;
149     } //connect to the grid
150
151   return kTRUE;
152 }
153
154 //______________________________________________________________________________
155 Bool_t AliTagCreator::ReadESDCollection(TGridResult *res)
156 {
157   fresult = res;
158   Int_t NEntries = fresult->GetEntries();
159
160   TString gridname = "alien://";
161   TString AlienUrl;
162   const char *guid;
163  
164   Int_t counter = 0;
165   for(Int_t i = 0; i < NEntries; i++)
166     {
167       AlienUrl = gridname;
168       AlienUrl += fresult->GetKey(i,"lfn");
169       guid = fresult->GetKey(i,"guid");
170       TFile *f = TFile::Open(AlienUrl,"READ");
171       CreateTag(f,guid,counter);
172       f->Close();
173       delete f;  
174       counter += 1;
175     }//grid result loop
176
177   return kTRUE;
178 }
179
180
181 //_____________________________________________________________________________
182 void AliTagCreator::CreateTag(TFile* file, const char *guid, Int_t Counter)
183 {
184   Int_t ntrack;
185   Int_t NProtons, NKaons, NPions, NMuons, NElectrons;
186   Int_t Npos, Nneg, Nneutr;
187   Int_t NK0s, Nneutrons, Npi0s, Ngamas;
188   Int_t Nch1GeV, Nch3GeV, Nch10GeV;
189   Int_t Nmu1GeV, Nmu3GeV, Nmu10GeV;
190   Int_t Nel1GeV, Nel3GeV, Nel10GeV;
191   Float_t MaxPt = .0, MeanPt = .0, TotalP = .0;
192
193   AliRunTag *tag = new AliRunTag();
194   AliDetectorTag *detTag = new AliDetectorTag();
195   AliEventTag *evTag = new AliEventTag();
196   TTree ttag("T","A Tree with event tags");
197   TBranch * btag = ttag.Branch("AliTAG", "AliRunTag", &tag);
198   btag->SetCompressionLevel(9);
199   
200   AliInfo(Form("Creating the tags......."));    
201   
202   Int_t firstEvent = 0,lastEvent = 0;
203   TTree *t = (TTree*) file->Get("esdTree");
204   TBranch * b = t->GetBranch("ESD");
205   AliESD *esd = 0;
206   b->SetAddress(&esd);
207   
208   tag->SetRunId(esd->GetRunNumber());
209   
210   Int_t i_NumberOfEvents = b->GetEntries();
211   for (Int_t i_EventNumber = 0; i_EventNumber < i_NumberOfEvents; i_EventNumber++)
212     {
213       ntrack = 0;
214       Npos = 0;
215       Nneg = 0;
216       Nneutr =0;
217       NK0s = 0;
218       Nneutrons = 0;
219       Npi0s = 0;
220       Ngamas = 0;
221       NProtons = 0;
222       NKaons = 0;
223       NPions = 0;
224       NMuons = 0;
225       NElectrons = 0;     
226       Nch1GeV = 0;
227       Nch3GeV = 0;
228       Nch10GeV = 0;
229       Nmu1GeV = 0;
230       Nmu3GeV = 0;
231       Nmu10GeV = 0;
232       Nel1GeV = 0;
233       Nel3GeV = 0;
234       Nel10GeV = 0;
235       MaxPt = .0;
236       MeanPt = .0;
237       TotalP = .0;
238       
239       b->GetEntry(i_EventNumber);
240       const AliESDVertex * VertexIn = esd->GetVertex();
241       
242       for (Int_t i_TrackNumber = 0; i_TrackNumber < esd->GetNumberOfTracks(); i_TrackNumber++)
243         {
244           AliESDtrack * ESDTrack = esd->GetTrack(i_TrackNumber);
245           UInt_t status = ESDTrack->GetStatus();
246           
247           //select only tracks with ITS refit
248           if ((status&AliESDtrack::kITSrefit)==0) continue;
249           
250           //select only tracks with TPC refit-->remove extremely high Pt tracks
251           if ((status&AliESDtrack::kTPCrefit)==0) continue;
252           
253           //select only tracks with the "combined PID"
254           if ((status&AliESDtrack::kESDpid)==0) continue;
255           Double_t p[3];
256           ESDTrack->GetPxPyPz(p);
257           Double_t P = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
258           Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
259           TotalP += P;
260           MeanPt += fPt;
261           if(fPt > MaxPt)
262             MaxPt = fPt;
263           
264           if(ESDTrack->GetSign() > 0)
265             {
266               Npos++;
267               if(fPt > 1.0)
268                 Nch1GeV++;
269               if(fPt > 3.0)
270                 Nch3GeV++;
271               if(fPt > 10.0)
272                 Nch10GeV++;
273             }
274           if(ESDTrack->GetSign() < 0)
275             {
276               Nneg++;
277               if(fPt > 1.0)
278                 Nch1GeV++;
279               if(fPt > 3.0)
280                 Nch3GeV++;
281               if(fPt > 10.0)
282                 Nch10GeV++;
283             }
284           if(ESDTrack->GetSign() == 0)
285             Nneutr++;
286           
287           //PID
288           Double_t prob[10];
289           ESDTrack->GetESDpid(prob);
290           
291           //K0s
292           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]))
293             NK0s++;
294           //neutrons
295           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]))
296             Nneutrons++; 
297           //pi0s
298           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]))
299             Npi0s++;
300           //gamas
301           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]))
302             Ngamas++;
303           //protons
304           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]))
305             NProtons++;
306           //kaons
307           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]))
308             NKaons++;
309           //pions
310           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]))
311             NPions++; 
312           //muons
313           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]))
314             {
315               NMuons++;
316               if(fPt > 1.0)
317                 Nmu1GeV++;
318               if(fPt > 3.0)
319                 Nmu3GeV++;
320               if(fPt > 10.0)
321                 Nmu10GeV++;
322             }
323           //electrons
324           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]))
325             {
326               NElectrons++;
327               if(fPt > 1.0)
328                 Nel1GeV++;
329               if(fPt > 3.0)
330                 Nel3GeV++;
331               if(fPt > 10.0)
332                 Nel10GeV++;
333             }
334           
335           
336           
337           ntrack++;
338         }//track loop
339       // Fill the event tags 
340       if(ntrack != 0)
341         MeanPt = MeanPt/ntrack;
342       
343       evTag->SetEventId(i_EventNumber+1);
344       evTag->SetGUID(guid);
345       evTag->SetVertexX(VertexIn->GetXv());
346       evTag->SetVertexY(VertexIn->GetYv());
347       evTag->SetVertexZ(VertexIn->GetZv());
348       
349       evTag->SetT0VertexZ(esd->GetT0zVertex());
350       
351       evTag->SetTrigger(esd->GetTrigger());
352       
353       evTag->SetZDCNeutronEnergy(esd->GetZDCNEnergy());
354       evTag->SetZDCProtonEnergy(esd->GetZDCPEnergy());
355       evTag->SetZDCEMEnergy(esd->GetZDCEMEnergy());
356       evTag->SetNumOfParticipants(esd->GetZDCParticipants());
357       
358       
359       evTag->SetNumOfTracks(esd->GetNumberOfTracks());
360       evTag->SetNumOfPosTracks(Npos);
361       evTag->SetNumOfNegTracks(Nneg);
362       evTag->SetNumOfNeutrTracks(Nneutr);
363       
364       evTag->SetNumOfV0s(esd->GetNumberOfV0s());
365       evTag->SetNumOfCascades(esd->GetNumberOfCascades());
366       evTag->SetNumOfKinks(esd->GetNumberOfKinks());
367       evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
368       
369       evTag->SetNumOfProtons(NProtons);
370       evTag->SetNumOfKaons(NKaons);
371       evTag->SetNumOfPions(NPions);
372       evTag->SetNumOfMuons(NMuons);
373       evTag->SetNumOfElectrons(NElectrons);
374       evTag->SetNumOfPhotons(Ngamas);
375       evTag->SetNumOfPi0s(Npi0s);
376       evTag->SetNumOfNeutrons(Nneutrons);
377       evTag->SetNumOfKaon0s(NK0s);
378       
379       evTag->SetNumOfChargedAbove1GeV(Nch1GeV);
380       evTag->SetNumOfChargedAbove3GeV(Nch3GeV);
381       evTag->SetNumOfChargedAbove10GeV(Nch10GeV);
382       evTag->SetNumOfMuonsAbove1GeV(Nmu1GeV);
383       evTag->SetNumOfMuonsAbove3GeV(Nmu3GeV);
384       evTag->SetNumOfMuonsAbove10GeV(Nmu10GeV);
385       evTag->SetNumOfElectronsAbove1GeV(Nel1GeV);
386       evTag->SetNumOfElectronsAbove3GeV(Nel3GeV);
387       evTag->SetNumOfElectronsAbove10GeV(Nel10GeV);
388       
389       evTag->SetNumOfPHOSTracks(esd->GetNumberOfPHOSParticles());
390       evTag->SetNumOfEMCALTracks(esd->GetNumberOfEMCALParticles());
391       
392       evTag->SetTotalMomentum(TotalP);
393       evTag->SetMeanPt(MeanPt);
394       evTag->SetMaxPt(MaxPt);
395   
396       tag->AddEventTag(evTag);
397     }
398   lastEvent = i_NumberOfEvents;
399   
400   t->Delete("");
401         
402   ttag.Fill();
403   tag->Clear();
404
405   TString LocalfileName = "Run"; LocalfileName += tag->GetRunId(); 
406   LocalfileName += ".Event"; LocalfileName += firstEvent; LocalfileName += "_"; LocalfileName += lastEvent; LocalfileName += "."; LocalfileName += Counter;
407   LocalfileName += ".ESD.tag.root";
408
409   cout<<"Writing tags to local file: "<<LocalfileName<<endl;
410
411   TFile* ftag = TFile::Open(LocalfileName, "recreate");
412   ftag->cd();
413   ttag.Write();
414   ftag->Close();
415
416   delete ftag;
417   delete esd;
418
419   delete tag;
420   delete detTag;
421   delete evTag;
422 }
423
424 //_____________________________________________________________________________
425 Bool_t AliTagCreator::StoreGridTagFile(const char *localpath, const char *gridpath)
426 {
427   gSystem->Load("libThread.so");
428   gSystem->Load("libTreePlayer.so");
429   gSystem->Load("libProof.so");
430   cout<<"Storing tag file to alien's file catalog..."<<endl;
431   TFileMerger merger;
432   const char * pattern = "tag.root";
433   // Open the working directory
434   void * dirp = gSystem->OpenDirectory(localpath);
435   const char * name = 0x0;
436   TString AlienLocation;
437   Char_t LocalLocation[256];
438  
439   // Add all files matching *pattern* to the chain
440   while((name = gSystem->GetDirEntry(dirp)))
441     {
442       if (strstr(name,pattern))
443         {
444           sprintf(LocalLocation,"file:%s/%s",localpath,name);
445           AlienLocation = "/alien";
446           AlienLocation += gGrid->Pwd();
447           AlienLocation += gridpath;
448           AlienLocation += "/";
449           AlienLocation += name;
450           AlienLocation += "?se=";
451           AlienLocation += fSE.Data();
452           merger.Cp(LocalLocation,AlienLocation);
453             
454         }
455     }   
456   gSystem->FreeDirectory(dirp);
457    
458   return kTRUE;
459 }