]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTagCreator.cxx
Creation of tags after the reconstruction (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 <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   fCollectionFile = "";
66   fHost = "";
67   fPort = 0; 
68 }
69
70 //______________________________________________________________________________
71 AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username)
72 {
73   //==============Default constructor for a AliTagCreator==================
74   fHost = host;
75   fUser = username;
76   fPort = port;
77
78   TString grid = "alien://";
79   grid += fHost;
80   grid += ":";
81   grid += port;
82   
83   //connect to the grid
84   TGrid::Connect(grid.Data(),fUser.Data());
85   if(!gGrid)
86     {
87       AliError("Connection failed!!!");
88       return;
89     }
90 }
91
92
93 //______________________________________________________________________________
94 AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username, const char *passwd)
95 {
96   //==============Default constructor for a AliTagCreator==================
97   fHost = host;
98   fUser = username;
99   fPasswd = passwd;
100   fPort = port;
101
102   TString grid = "alien://";
103   grid += fHost;
104   grid += ":";
105   grid += port;
106   
107   //connect to the grid
108   TGrid::Connect(grid.Data(),fUser.Data(),fPasswd.Data());
109    if(!gGrid)
110     {
111       AliError("Connection failed!!!");
112       return;
113     }
114 }
115
116
117 //______________________________________________________________________________
118 AliTagCreator::~AliTagCreator()
119 {
120 //================Default destructor for a AliTagCreator=======================
121 }
122
123 //______________________________________________________________________________
124 void AliTagCreator::SetSE(const char *se)
125 {
126   fSE = se;
127 }
128
129 //______________________________________________________________________________
130 Bool_t AliTagCreator::ConnectToGrid(const char *host, Int_t port, const char *username)
131 {
132   fHost = host;
133   fUser = username;
134   fPort = port;
135
136   TString grid = "alien://";
137   grid += fHost;
138   grid += ":";
139   grid += port;
140   
141   //connect to the grid
142   TGrid::Connect(grid.Data(),fUser.Data());
143   if(!gGrid)
144     {
145       AliError("Connection failed!!!");
146       return kFALSE;
147     } //connect to the grid
148
149   return kTRUE;
150 }
151
152 //______________________________________________________________________________
153 Bool_t AliTagCreator::ReadESDCollection(const char *CollectionFile)
154 {
155   fCollectionFile = CollectionFile;
156
157   gSystem->Load("libXMLIO.so");
158   gSystem->Load("libXMLParser.so");
159   //define the tag collection
160   TAlienCollection *collection = new TAlienCollection(fCollectionFile.Data());
161   collection->Reset();
162
163   TMap *map;
164   TPair *pair;
165    
166   TString gridname = "alien://";
167   Int_t gridnameSize = gridname.Sizeof();
168   TString Esd = "AliEsds.root";
169   
170   TString EsdFilePath;
171   TString AlienUrl;
172   TString lfn;
173   const char *guid;
174   TString EsdPattern = "/alien";
175  
176   Int_t counter = 0;
177   while((map = collection->Next()))
178     { 
179       TIter next(map->GetTable());
180       counter += 1;
181       while ((pair = (TPair*) next()))
182         {
183           EsdFilePath = pair->Key()->GetName();
184           AlienUrl = collection->GetTURL(EsdFilePath);
185           EsdFilePath = pair->Value()->GetName();
186           
187           lfn = EsdFilePath.Replace(0,gridnameSize-1,EsdFilePath,0);
188           //TAlienFile *f = new TAlienFile(AlienUrl,"READ");
189           //TFile *f = new TFile(AlienUrl,"READ");
190           TFile *f = TFile::Open(AlienUrl,"READ");
191          
192           TGridResult* result = gGrid->Ls(lfn,"-b");
193           guid = result->GetKey(0,"guid");
194           
195           CreateTag(f,guid,counter);
196           f->Close();
197           delete f;
198         }//key loop
199     }//collection loop
200
201   return kTRUE;
202 }
203
204
205 //_____________________________________________________________________________
206 void AliTagCreator::CreateTag(TFile* file, const char *guid, Int_t Counter)
207 {
208   Int_t ntrack;
209   Int_t NProtons, NKaons, NPions, NMuons, NElectrons;
210   Int_t Npos, Nneg, Nneutr;
211   Int_t NK0s, Nneutrons, Npi0s, Ngamas;
212   Int_t Nch1GeV, Nch3GeV, Nch10GeV;
213   Int_t Nmu1GeV, Nmu3GeV, Nmu10GeV;
214   Int_t Nel1GeV, Nel3GeV, Nel10GeV;
215   Float_t MaxPt = .0, MeanPt = .0, TotalP = .0;
216
217   AliRunTag *tag = new AliRunTag();
218   AliDetectorTag *detTag = new AliDetectorTag();
219   AliEventTag *evTag = new AliEventTag();
220   TTree ttag("T","A Tree with event tags");
221   TBranch * btag = ttag.Branch("AliTAG", "AliRunTag", &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 i_NumberOfEvents = b->GetEntries();
235   for (Int_t i_EventNumber = 0; i_EventNumber < i_NumberOfEvents; i_EventNumber++)
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(i_EventNumber);
264       const AliESDVertex * VertexIn = esd->GetVertex();
265       
266       for (Int_t i_TrackNumber = 0; i_TrackNumber < esd->GetNumberOfTracks(); i_TrackNumber++)
267         {
268           AliESDtrack * ESDTrack = esd->GetTrack(i_TrackNumber);
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 P = 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 += P;
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           
359           
360           
361           ntrack++;
362         }//track loop
363       // Fill the event tags 
364       if(ntrack != 0)
365         MeanPt = MeanPt/ntrack;
366       
367       evTag->SetEventId(i_EventNumber+1);
368       evTag->SetGUID(guid);
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 = i_NumberOfEvents;
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   cout<<"Writing tags to local file: "<<LocalfileName<<endl;
434
435   TFile* ftag = TFile::Open(LocalfileName, "recreate");
436   ftag->cd();
437   ttag.Write();
438   ftag->Close();
439
440   delete ftag;
441   delete esd;
442
443   delete tag;
444   delete detTag;
445   delete evTag;
446 }
447
448 //_____________________________________________________________________________
449 Bool_t AliTagCreator::StoreGridTagFile(const char *localpath, const char *gridpath)
450 {
451   gSystem->Load("libThread.so");
452   gSystem->Load("libTreePlayer.so");
453   gSystem->Load("libProof.so");
454   cout<<"Storing tag file to alien's file catalog..."<<endl;
455   TFileMerger merger;
456   const char * pattern = "tag.root";
457   // Open the working directory
458   void * dirp = gSystem->OpenDirectory(localpath);
459   const char * name = 0x0;
460   TString AlienLocation;
461   Char_t LocalLocation[256];
462  
463   // Add all files matching *pattern* to the chain
464   while((name = gSystem->GetDirEntry(dirp)))
465     {
466       if (strstr(name,pattern))
467         {
468           sprintf(LocalLocation,"file:%s/%s",localpath,name);
469           AlienLocation = "/alien";
470           AlienLocation += gGrid->Pwd();
471           AlienLocation += gridpath;
472           AlienLocation += "/";
473           AlienLocation += name;
474           AlienLocation += "?se=";
475           AlienLocation += fSE.Data();
476           merger.Cp(LocalLocation,AlienLocation);
477             
478         }
479     }   
480   gSystem->FreeDirectory(dirp);
481    
482   return kTRUE;
483 }