Coding convention
[u/mrichter/AliRoot.git] / STEER / AliTagCreator.cxx
CommitLineData
b45e5084 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
b45e5084 23#include <TFile.h>
24#include <TString.h>
cb1645b7 25#include <TTree.h>
b45e5084 26
27//ROOT-AliEn
28#include <TGrid.h>
b45e5084 29#include <TGridResult.h>
b45e5084 30
31//AliRoot
32#include "AliRunTag.h"
33#include "AliEventTag.h"
34#include "AliESD.h"
35#include "AliESDVertex.h"
b45e5084 36#include "AliLog.h"
37
38
39#include "AliTagCreator.h"
40
41
42ClassImp(AliTagCreator)
43
44
45//______________________________________________________________________________
46AliTagCreator::AliTagCreator() //local mode
47{
48 //==============Default constructor for a AliTagCreator==================
28afeb2e 49 fgridpath = "";
b45e5084 50 fUser = "";
51 fPasswd = "";
52 fSE = "";
b45e5084 53 fHost = "";
54 fPort = 0;
28afeb2e 55 fStorage = 0;
cb1645b7 56 // fresult = 0;
b45e5084 57}
58
59//______________________________________________________________________________
60AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username)
61{
62 //==============Default constructor for a AliTagCreator==================
28afeb2e 63 fStorage = 0;
cb1645b7 64 // fresult = 0;
28afeb2e 65 fgridpath = "";
b45e5084 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//______________________________________________________________________________
86AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username, const char *passwd)
87{
88 //==============Default constructor for a AliTagCreator==================
28afeb2e 89 fStorage = 0;
cb1645b7 90 // fresult = 0;
28afeb2e 91 fgridpath = "";
b45e5084 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//______________________________________________________________________________
113AliTagCreator::~AliTagCreator()
114{
115//================Default destructor for a AliTagCreator=======================
116}
117
28afeb2e 118//______________________________________________________________________________
119void AliTagCreator::SetStorage(Int_t storage)
120{
cb1645b7 121 // Sets correctly the storage: 0 for local, 1 for GRID
28afeb2e 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
b45e5084 134//______________________________________________________________________________
135Bool_t AliTagCreator::ConnectToGrid(const char *host, Int_t port, const char *username)
136{
cb1645b7 137 // Connects to a given AliEn API service
b45e5084 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//______________________________________________________________________________
cb1645b7 159Bool_t AliTagCreator::ReadESDCollection(TGridResult *fresult)
b45e5084 160{
cb1645b7 161 // Reads the entry of the TGridResult and creates the tags
162 Int_t nEntries = fresult->GetEntries();
b45e5084 163
cb1645b7 164 TString alienUrl;
b45e5084 165 const char *guid;
b45e5084 166
167 Int_t counter = 0;
cb1645b7 168 for(Int_t i = 0; i < nEntries; i++)
09c52d4a 169 {
cb1645b7 170 alienUrl = fresult->GetKey(i,"turl");
09c52d4a 171 guid = fresult->GetKey(i,"guid");
cb1645b7 172 TFile *f = TFile::Open(alienUrl,"READ");
09c52d4a 173 CreateTag(f,guid,counter);
174 f->Close();
175 delete f;
b45e5084 176 counter += 1;
09c52d4a 177 }//grid result loop
b45e5084 178
179 return kTRUE;
180}
181
182
183//_____________________________________________________________________________
184void AliTagCreator::CreateTag(TFile* file, const char *guid, Int_t Counter)
185{
cb1645b7 186 // Creates the tags for all the events in a given ESD file
b45e5084 187 Int_t ntrack;
cb1645b7 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;
b45e5084 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
cb1645b7 213 Int_t iNumberOfEvents = b->GetEntries();
214 for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++)
b45e5084 215 {
216 ntrack = 0;
cb1645b7 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;
b45e5084 241
cb1645b7 242 b->GetEntry(iEventNumber);
243 const AliESDVertex * vertexIn = esd->GetVertex();
b45e5084 244
cb1645b7 245 for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++)
b45e5084 246 {
cb1645b7 247 AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
248 UInt_t status = esdTrack->GetStatus();
b45e5084 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];
cb1645b7 259 esdTrack->GetPxPyPz(p);
260 Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
b45e5084 261 Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
cb1645b7 262 totalP += momentum;
263 meanPt += fPt;
264 if(fPt > maxPt)
265 maxPt = fPt;
b45e5084 266
cb1645b7 267 if(esdTrack->GetSign() > 0)
b45e5084 268 {
cb1645b7 269 nPos++;
b45e5084 270 if(fPt > 1.0)
cb1645b7 271 nCh1GeV++;
b45e5084 272 if(fPt > 3.0)
cb1645b7 273 nCh3GeV++;
b45e5084 274 if(fPt > 10.0)
cb1645b7 275 nCh10GeV++;
b45e5084 276 }
cb1645b7 277 if(esdTrack->GetSign() < 0)
b45e5084 278 {
cb1645b7 279 nNeg++;
b45e5084 280 if(fPt > 1.0)
cb1645b7 281 nCh1GeV++;
b45e5084 282 if(fPt > 3.0)
cb1645b7 283 nCh3GeV++;
b45e5084 284 if(fPt > 10.0)
cb1645b7 285 nCh10GeV++;
b45e5084 286 }
cb1645b7 287 if(esdTrack->GetSign() == 0)
288 nNeutr++;
b45e5084 289
290 //PID
291 Double_t prob[10];
cb1645b7 292 esdTrack->GetESDpid(prob);
b45e5084 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]))
cb1645b7 296 nK0s++;
b45e5084 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]))
cb1645b7 299 nNeutrons++;
b45e5084 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]))
cb1645b7 302 nPi0s++;
b45e5084 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]))
cb1645b7 305 nGamas++;
b45e5084 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]))
cb1645b7 308 nProtons++;
b45e5084 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]))
cb1645b7 311 nKaons++;
b45e5084 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]))
cb1645b7 314 nPions++;
b45e5084 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 {
cb1645b7 318 nMuons++;
b45e5084 319 if(fPt > 1.0)
cb1645b7 320 nMu1GeV++;
b45e5084 321 if(fPt > 3.0)
cb1645b7 322 nMu3GeV++;
b45e5084 323 if(fPt > 10.0)
cb1645b7 324 nMu10GeV++;
b45e5084 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 {
cb1645b7 329 nElectrons++;
b45e5084 330 if(fPt > 1.0)
cb1645b7 331 nEl1GeV++;
b45e5084 332 if(fPt > 3.0)
cb1645b7 333 nEl3GeV++;
b45e5084 334 if(fPt > 10.0)
cb1645b7 335 nEl10GeV++;
b45e5084 336 }
337
338
339
340 ntrack++;
341 }//track loop
342 // Fill the event tags
343 if(ntrack != 0)
cb1645b7 344 meanPt = meanPt/ntrack;
b45e5084 345
cb1645b7 346 evTag->SetEventId(iEventNumber+1);
b45e5084 347 evTag->SetGUID(guid);
cb1645b7 348 evTag->SetVertexX(vertexIn->GetXv());
349 evTag->SetVertexY(vertexIn->GetYv());
350 evTag->SetVertexZ(vertexIn->GetZv());
b45e5084 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());
cb1645b7 363 evTag->SetNumOfPosTracks(nPos);
364 evTag->SetNumOfNegTracks(nNeg);
365 evTag->SetNumOfNeutrTracks(nNeutr);
b45e5084 366
367 evTag->SetNumOfV0s(esd->GetNumberOfV0s());
368 evTag->SetNumOfCascades(esd->GetNumberOfCascades());
369 evTag->SetNumOfKinks(esd->GetNumberOfKinks());
370 evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
371
cb1645b7 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);
b45e5084 381
cb1645b7 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);
b45e5084 391
392 evTag->SetNumOfPHOSTracks(esd->GetNumberOfPHOSParticles());
393 evTag->SetNumOfEMCALTracks(esd->GetNumberOfEMCALParticles());
394
cb1645b7 395 evTag->SetTotalMomentum(totalP);
396 evTag->SetMeanPt(meanPt);
397 evTag->SetMaxPt(maxPt);
b45e5084 398
cb1645b7 399 tag->AddEventTag(*evTag);
b45e5084 400 }
cb1645b7 401 lastEvent = iNumberOfEvents;
b45e5084 402
403 t->Delete("");
404
405 ttag.Fill();
406 tag->Clear();
407
cb1645b7 408 TString localFileName = "Run"; localFileName += tag->GetRunId();
409 localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
410 localFileName += ".ESD.tag.root";
b45e5084 411
cb1645b7 412 TString alienLocation = "/alien";
413 alienLocation += gGrid->Pwd();
414 alienLocation += fgridpath.Data();
415 alienLocation += "/";
416 alienLocation += localFileName;
417 alienLocation += "?se=";
418 alienLocation += fSE.Data();
28afeb2e 419
cb1645b7 420 TString fileName;
28afeb2e 421
422 if(fStorage == 0)
423 {
cb1645b7 424 fileName = localFileName.Data();
425 AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
28afeb2e 426 }
427 if(fStorage == 1)
428 {
cb1645b7 429 fileName = alienLocation.Data();
430 AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
28afeb2e 431 }
b45e5084 432
cb1645b7 433 TFile* ftag = TFile::Open(fileName, "recreate");
b45e5084 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