New classes for selection of events using their tags (P.Christakoglou)
[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;
b45e5084 56}
57
58//______________________________________________________________________________
59AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username)
60{
61 //==============Default constructor for a AliTagCreator==================
28afeb2e 62 fStorage = 0;
28afeb2e 63 fgridpath = "";
b45e5084 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//______________________________________________________________________________
84AliTagCreator::AliTagCreator(const char *host, Int_t port, const char *username, const char *passwd)
85{
86 //==============Default constructor for a AliTagCreator==================
28afeb2e 87 fStorage = 0;
28afeb2e 88 fgridpath = "";
b45e5084 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//______________________________________________________________________________
110AliTagCreator::~AliTagCreator()
111{
112//================Default destructor for a AliTagCreator=======================
113}
114
115//______________________________________________________________________________
28afeb2e 116void AliTagCreator::SetStorage(Int_t storage)
117{
cb1645b7 118 // Sets correctly the storage: 0 for local, 1 for GRID
28afeb2e 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//______________________________________________________________________________
b45e5084 132Bool_t AliTagCreator::ConnectToGrid(const char *host, Int_t port, const char *username)
133{
cb1645b7 134 // Connects to a given AliEn API service
b45e5084 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//______________________________________________________________________________
cb1645b7 156Bool_t AliTagCreator::ReadESDCollection(TGridResult *fresult)
b45e5084 157{
cb1645b7 158 // Reads the entry of the TGridResult and creates the tags
159 Int_t nEntries = fresult->GetEntries();
b45e5084 160
cb1645b7 161 TString alienUrl;
b45e5084 162 const char *guid;
e16601cf 163 const char *md5;
164 const char *turl;
165 Long64_t size = -1;
166
b45e5084 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");
e16601cf 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
cb1645b7 181 TFile *f = TFile::Open(alienUrl,"READ");
e16601cf 182 //CreateTag(f,guid,counter);
183 CreateTag(f,guid,md5,turl,size,counter);
09c52d4a 184 f->Close();
185 delete f;
b45e5084 186 counter += 1;
09c52d4a 187 }//grid result loop
b45e5084 188
189 return kTRUE;
190}
191
192
193//_____________________________________________________________________________
e16601cf 194//void AliTagCreator::CreateTag(TFile* file, const char *guid, Int_t Counter)
195void AliTagCreator::CreateTag(TFile* file, const char *guid, const char *md5, const char *turl, Long64_t size, Int_t Counter)
b45e5084 196{
cb1645b7 197 // Creates the tags for all the events in a given ESD file
b45e5084 198 Int_t ntrack;
cb1645b7 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;
b45e5084 206
207 AliRunTag *tag = new AliRunTag();
b45e5084 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
cb1645b7 223 Int_t iNumberOfEvents = b->GetEntries();
224 for (Int_t iEventNumber = 0; iEventNumber < iNumberOfEvents; iEventNumber++)
b45e5084 225 {
226 ntrack = 0;
cb1645b7 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;
b45e5084 251
cb1645b7 252 b->GetEntry(iEventNumber);
253 const AliESDVertex * vertexIn = esd->GetVertex();
b45e5084 254
cb1645b7 255 for (Int_t iTrackNumber = 0; iTrackNumber < esd->GetNumberOfTracks(); iTrackNumber++)
b45e5084 256 {
cb1645b7 257 AliESDtrack * esdTrack = esd->GetTrack(iTrackNumber);
258 UInt_t status = esdTrack->GetStatus();
b45e5084 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];
cb1645b7 269 esdTrack->GetPxPyPz(p);
270 Double_t momentum = sqrt(pow(p[0],2) + pow(p[1],2) + pow(p[2],2));
b45e5084 271 Double_t fPt = sqrt(pow(p[0],2) + pow(p[1],2));
cb1645b7 272 totalP += momentum;
273 meanPt += fPt;
274 if(fPt > maxPt)
275 maxPt = fPt;
b45e5084 276
cb1645b7 277 if(esdTrack->GetSign() > 0)
b45e5084 278 {
cb1645b7 279 nPos++;
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)
b45e5084 288 {
cb1645b7 289 nNeg++;
b45e5084 290 if(fPt > 1.0)
cb1645b7 291 nCh1GeV++;
b45e5084 292 if(fPt > 3.0)
cb1645b7 293 nCh3GeV++;
b45e5084 294 if(fPt > 10.0)
cb1645b7 295 nCh10GeV++;
b45e5084 296 }
cb1645b7 297 if(esdTrack->GetSign() == 0)
298 nNeutr++;
b45e5084 299
300 //PID
301 Double_t prob[10];
cb1645b7 302 esdTrack->GetESDpid(prob);
b45e5084 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]))
cb1645b7 306 nK0s++;
b45e5084 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]))
cb1645b7 309 nNeutrons++;
b45e5084 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]))
cb1645b7 312 nPi0s++;
b45e5084 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]))
cb1645b7 315 nGamas++;
b45e5084 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]))
cb1645b7 318 nProtons++;
b45e5084 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]))
cb1645b7 321 nKaons++;
b45e5084 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]))
cb1645b7 324 nPions++;
b45e5084 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 {
cb1645b7 328 nMuons++;
b45e5084 329 if(fPt > 1.0)
cb1645b7 330 nMu1GeV++;
b45e5084 331 if(fPt > 3.0)
cb1645b7 332 nMu3GeV++;
b45e5084 333 if(fPt > 10.0)
cb1645b7 334 nMu10GeV++;
b45e5084 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 {
cb1645b7 339 nElectrons++;
b45e5084 340 if(fPt > 1.0)
cb1645b7 341 nEl1GeV++;
b45e5084 342 if(fPt > 3.0)
cb1645b7 343 nEl3GeV++;
b45e5084 344 if(fPt > 10.0)
cb1645b7 345 nEl10GeV++;
b45e5084 346 }
347
348
349
350 ntrack++;
351 }//track loop
352 // Fill the event tags
353 if(ntrack != 0)
cb1645b7 354 meanPt = meanPt/ntrack;
b45e5084 355
cb1645b7 356 evTag->SetEventId(iEventNumber+1);
b45e5084 357 evTag->SetGUID(guid);
e16601cf 358 evTag->SetMD5(md5);
359 evTag->SetTURL(turl);
360 evTag->SetSize(size);
cb1645b7 361 evTag->SetVertexX(vertexIn->GetXv());
362 evTag->SetVertexY(vertexIn->GetYv());
363 evTag->SetVertexZ(vertexIn->GetZv());
b45e5084 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());
cb1645b7 376 evTag->SetNumOfPosTracks(nPos);
377 evTag->SetNumOfNegTracks(nNeg);
378 evTag->SetNumOfNeutrTracks(nNeutr);
b45e5084 379
380 evTag->SetNumOfV0s(esd->GetNumberOfV0s());
381 evTag->SetNumOfCascades(esd->GetNumberOfCascades());
382 evTag->SetNumOfKinks(esd->GetNumberOfKinks());
383 evTag->SetNumOfPMDTracks(esd->GetNumberOfPmdTracks());
384
cb1645b7 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);
b45e5084 394
cb1645b7 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);
b45e5084 404
405 evTag->SetNumOfPHOSTracks(esd->GetNumberOfPHOSParticles());
406 evTag->SetNumOfEMCALTracks(esd->GetNumberOfEMCALParticles());
407
cb1645b7 408 evTag->SetTotalMomentum(totalP);
409 evTag->SetMeanPt(meanPt);
410 evTag->SetMaxPt(maxPt);
b45e5084 411
cb1645b7 412 tag->AddEventTag(*evTag);
b45e5084 413 }
cb1645b7 414 lastEvent = iNumberOfEvents;
b45e5084 415
416 t->Delete("");
417
418 ttag.Fill();
419 tag->Clear();
420
cb1645b7 421 TString localFileName = "Run"; localFileName += tag->GetRunId();
422 localFileName += ".Event"; localFileName += firstEvent; localFileName += "_"; localFileName += lastEvent; localFileName += "."; localFileName += Counter;
423 localFileName += ".ESD.tag.root";
b45e5084 424
cb1645b7 425 TString alienLocation = "/alien";
426 alienLocation += gGrid->Pwd();
427 alienLocation += fgridpath.Data();
428 alienLocation += "/";
429 alienLocation += localFileName;
430 alienLocation += "?se=";
431 alienLocation += fSE.Data();
28afeb2e 432
cb1645b7 433 TString fileName;
28afeb2e 434
435 if(fStorage == 0)
436 {
cb1645b7 437 fileName = localFileName.Data();
438 AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
28afeb2e 439 }
440 if(fStorage == 1)
441 {
cb1645b7 442 fileName = alienLocation.Data();
443 AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
28afeb2e 444 }
b45e5084 445
cb1645b7 446 TFile* ftag = TFile::Open(fileName, "recreate");
b45e5084 447 ftag->cd();
448 ttag.Write();
449 ftag->Close();
450
451 delete ftag;
452 delete esd;
453
454 delete tag;
b45e5084 455 delete evTag;
456}
457