]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliAODTagCreator.cxx
coding conventions
[u/mrichter/AliRoot.git] / STEER / AliAODTagCreator.cxx
CommitLineData
a1069ee1 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// AliAODTagCreator 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 <Riostream.h>
24#include <TFile.h>
25#include <TString.h>
26#include <TTree.h>
27#include <TSystem.h>
28#include <TChain.h>
29#include <TLorentzVector.h>
a69c1246 30#include <TRefArray.h>
a1069ee1 31
32//ROOT-AliEn
33#include <TGrid.h>
34#include <TGridResult.h>
35
36//AliRoot
37#include "AliRunTag.h"
38#include "AliEventTag.h"
39#include "AliPID.h"
a1069ee1 40#include "AliAODEvent.h"
41#include "AliAODVertex.h"
42#include "AliLog.h"
43
44#include "AliAODTagCreator.h"
45
46
47ClassImp(AliAODTagCreator)
48
49
50//______________________________________________________________________________
51 AliAODTagCreator::AliAODTagCreator() :
6e557e6d 52 AliTagCreator(),
53 fChain(0),
54 fAODEvent(0),
55 fTreeT(0),
56 fRunTag(0),
57 fTreeTEsd(0),
58 fRunTagEsd(0)
59{
a1069ee1 60 //==============Default constructor for a AliAODTagCreator================
61}
62
63//______________________________________________________________________________
64AliAODTagCreator::~AliAODTagCreator() {
65//================Default destructor for a AliAODTagCreator===================
103d3fba 66 delete fChain;
6e557e6d 67 delete fAODEvent;
a1069ee1 68}
69
70//______________________________________________________________________________
71Bool_t AliAODTagCreator::ReadGridCollection(TGridResult *fresult) {
72 // Reads the entry of the TGridResult and creates the tags
73 Int_t nEntries = fresult->GetEntries();
74
103d3fba 75 TString alienUrl;
76 const char* guid;
77 const char* md5;
78 const char* turl;
79 Long64_t size = -1;
80
6e557e6d 81 fChain = new TChain("aodTree");
82
103d3fba 83 for(Int_t i = 0; i < nEntries; i++) {
84 alienUrl = fresult->GetKey(i,"turl");
85 guid = fresult->GetKey(i,"guid");
86 if(fresult->GetKey(i,"size")) size = atol (fresult->GetKey(i,"size"));
87 md5 = fresult->GetKey(i,"md5");
88 turl = fresult->GetKey(i,"turl");
89 if(md5 && !strlen(guid)) md5 = 0;
90 if(guid && !strlen(guid)) guid = 0;
91
92 fChain->Add(alienUrl);
103d3fba 93 }//grid result loop
94
95 AliInfo(Form("AOD chain created......."));
96 AliInfo(Form("Chain entries: %d",fChain->GetEntries()));
6e557e6d 97
98 CreateTag(fChain, "grid");
103d3fba 99
100 return kTRUE;
101}
102
a1069ee1 103//______________________________________________________________________________
104Bool_t AliAODTagCreator::ReadLocalCollection(const char *localpath) {
103d3fba 105 // Checks the different subdirs of the given local path and in the
6e557e6d 106 // case where it finds an AliAODs.root file it creates the tags
103d3fba 107
108 void *dira = gSystem->OpenDirectory(localpath);
109 Char_t fPath[256];
110 const char * dirname = 0x0;
111 const char * filename = 0x0;
112 const char * pattern = "AliAODs.root";
113
6e557e6d 114 fChain = new TChain("aodTree");
115
103d3fba 116 while((dirname = gSystem->GetDirEntry(dira))) {
117 sprintf(fPath,"%s/%s",localpath,dirname);
118 void *dirb = gSystem->OpenDirectory(fPath);
119 while((filename = gSystem->GetDirEntry(dirb))) {
6d81c8d4 120 TString bstr = dirname;
121 if(bstr.Contains("..")) continue;
122 if(strstr(filename,pattern)) {
123 TString aodFileName;
124 aodFileName = fPath;
125 aodFileName += "/";
126 aodFileName += pattern;
127 fChain->Add(aodFileName);
128 } //pattern check
129 } //child directory's entry loop
130 } //parent directory's entry loop
131
103d3fba 132 AliInfo(Form("AOD chain created......."));
133 AliInfo(Form("Chain entries: %d",fChain->GetEntries()));
6e557e6d 134
135 CreateTag(fChain, "local");
103d3fba 136
137 return kTRUE;
138}
139
a1069ee1 140//______________________________________________________________________________
141Bool_t AliAODTagCreator::ReadCAFCollection(const char *filename) {
103d3fba 142 // Temporary solution for CAF: Takes as an input the ascii file that
6e557e6d 143 // lists the AODs stored in the SE of the CAF and creates the tags.
103d3fba 144
145 // Open the input stream
146 ifstream in;
147 in.open(filename);
148
6e557e6d 149 TString aodfile;
150
151 fChain = new TChain("aodTree");
152
103d3fba 153 // Read the input list of files and add them to the chain
154 while(in.good()) {
6e557e6d 155 in >> aodfile;
156 if (!aodfile.Contains("root")) continue; // protection
157 fChain->Add(aodfile);
103d3fba 158 }
159
160 AliInfo(Form("AOD chain created......."));
161 AliInfo(Form("Chain entries: %d",fChain->GetEntries()));
6e557e6d 162
163 CreateTag(fChain, "proof");
103d3fba 164
165 return kTRUE;
166}
167
a1069ee1 168//__________________________________________________________________________
786172af 169void AliAODTagCreator::CreateAODTags(Int_t fFirstEvent, Int_t fLastEvent, TList */*grpList*/) {
6e557e6d 170 // Creates tag files for AODs
a1069ee1 171
6e557e6d 172 AliInfo(Form("Creating the AOD tags......."));
a1069ee1 173
6e557e6d 174 TFile *file = TFile::Open("AliAOD.root");
175 if (!file || !file->IsOpen()) {
176 AliError(Form("opening failed"));
177 delete file;
178 return ;
a1069ee1 179 }
a1069ee1 180
6e557e6d 181 fChain = new TChain("aodTree");
182 fChain->Add("AliAOD.root");
a1069ee1 183
6e557e6d 184 fAODEvent = new AliAODEvent();
185 fAODEvent->ReadFromTree(fChain);
a1069ee1 186
6e557e6d 187 Int_t lastEvent = 0;
188 if(fLastEvent == -1) lastEvent = (Int_t)fChain->GetEntries();
189 else lastEvent = fLastEvent;
190
6e557e6d 191 char fileName[256];
192 sprintf(fileName, "Run%d.Event%d_%d.AOD.tag.root",
193 fAODEvent->GetRunNumber(), fFirstEvent, lastEvent );
194 AliInfo(Form("writing tags to file %s", fileName));
195 AliDebug(1, Form("writing tags to file %s", fileName));
196
197 TFile* ftag = TFile::Open(fileName, "recreate");
198
24ae0874 199 fRunTag = new AliRunTag();
6e557e6d 200 fTreeT = new TTree("T","A Tree with event tags");
201 TBranch * btag = fTreeT->Branch("AliTAG", &fRunTag);
202 btag->SetCompressionLevel(9);
203
204 CreateTags();
205
206 ftag->cd();
207 fTreeT->Fill();
208 fRunTag->Clear();
209 fTreeT->Write();
210 ftag->Close();
211 file->cd();
212 file->Close();
a1069ee1 213}
214
103d3fba 215//_____________________________________________________________________________
5222362f 216void AliAODTagCreator::CreateTag(TChain* chain, const char *type) {
6e557e6d 217
218 // Private method that creates tag files
219 //
220
a69c1246 221 //reading the esd tag file
6e557e6d 222 fTreeTEsd = new TChain("T");
223 const char * tagPattern = "ESD.tag";
224 // Open the working directory
225 void * dirp = gSystem->OpenDirectory(gSystem->pwd());
226 const char * name = 0x0;
227 // Add all files matching *pattern* to the chain
228 while((name = gSystem->GetDirEntry(dirp))) {
229 if (strstr(name,tagPattern)) fTreeTEsd->Add(name);
230 }//directory loop
231 AliInfo(Form("Chained tag files: %d", fTreeTEsd->GetEntries()));
a69c1246 232
6e557e6d 233 fChain = chain;
234
235 TString fSession = type;
236 TString fguid, fmd5, fturl;
237 fAODEvent = new AliAODEvent();
238 fAODEvent->ReadFromTree(fChain);
103d3fba 239
6e557e6d 240 Int_t firstEvent = 0;
241
242 TString localFileName = "Run";
243 localFileName += fAODEvent->GetRunNumber();
244 localFileName += ".Event";
245 localFileName += firstEvent;
246 localFileName += "_";
247 localFileName += chain->GetEntries(); //localFileName += "."; localFileName += Counter;
248 localFileName += ".AOD.tag.root";
249
250 TString fileName;
103d3fba 251
6e557e6d 252 if(fStorage == 0) {
253 fileName = localFileName.Data();
254 AliInfo(Form("Writing tags to local file: %s",fileName.Data()));
103d3fba 255 }
6e557e6d 256 else if(fStorage == 1) {
257 TString alienLocation = "/alien";
258 alienLocation += gGrid->Pwd();
259 alienLocation += fgridpath.Data();
260 alienLocation += "/";
261 alienLocation += localFileName;
262 alienLocation += "?se=";
263 alienLocation += fSE.Data();
264 fileName = alienLocation.Data();
265 AliInfo(Form("Writing tags to grid file: %s",fileName.Data()));
103d3fba 266 }
103d3fba 267
6e557e6d 268 TFile* ftag = TFile::Open(fileName, "recreate");
269
270 fRunTag = new AliRunTag();
271 fTreeT = new TTree("T", "A Tree with event tags");
272 TBranch * btag = fTreeT->Branch("AliTAG", &fRunTag);
273 btag->SetCompressionLevel(9);
274
275 // Access information from esd tag
276 fRunTagEsd = new AliRunTag();
277 fTreeTEsd->SetBranchAddress("AliTAG",&fRunTagEsd);
278 // Creating new information of aod
279 AliInfo(Form("Creating the AOD tags......."));
280 CreateTags(type);
281 ftag->cd();
282 fRunTag->Clear();
283 fTreeT->Write();
284 ftag->Close();
103d3fba 285}
286
6e557e6d 287void AliAODTagCreator::CreateTags(const char* type)
288{
289 // Event loop for tag creation
290 TString fturl;
291 TString fguid;
292 Int_t oldRun = -1;
293 fChain->GetEntry(0);
294 TFile *f = fChain->GetFile();
295 TString ftempGuid = f->GetUUID().AsString();
296 // Loop over events
297 Int_t nEvents = fChain->GetEntries();
298 Int_t ntags = 0;
299 Int_t tagentry = 0;
300 const TClonesArray *evTagList = 0;
a69c1246 301
6e557e6d 302 for (Int_t iEventNumber = 0; iEventNumber < nEvents; iEventNumber++) {
303 // Copy old tag information
304 if (iEventNumber >= ntags) {
305 fTreeTEsd->GetEntry(tagentry++);
306 fRunTag->CopyStandardContent(fRunTagEsd);
307 evTagList = fRunTagEsd->GetEventTags();
308 ntags += evTagList->GetEntries();
309 }
5b8d5d69 310
6e557e6d 311 // Create a new Tag
312 AliEventTag* evTag = new AliEventTag();
313 // Read event
314 fChain->GetEntry(iEventNumber);
315 if (iEventNumber == 0) oldRun = fAODEvent->GetRunNumber();
316 // Reference to the input file
317 TFile *file = fChain->GetFile();
318 const TUrl *url = file->GetEndpointUrl();
319 fguid = file->GetUUID().AsString();
320
f12d42ce 321 if (!strcmp(type,"grid")) {
6e557e6d 322 TString fturltemp = "alien://"; fturltemp += url->GetFile();
323 fturl = fturltemp(0,fturltemp.Index(".root",5,0,TString::kExact)+5);
324 } else {
325 fturl = url->GetFile();
326 }
327
328 fAODEvent->GetStdContent();
329
330 // Fill the event tag from the aod informatiom
331 FillEventTag(fAODEvent, evTag);
332 // Set the event and input file references
7e3c2e04 333 //evTag->SetEventId(iEventNumber+1);
6e557e6d 334 evTag->SetGUID(fguid);
f12d42ce 335 if(!strcmp(type,"grid")) {
6e557e6d 336 evTag->SetMD5(0);
337 evTag->SetTURL(fturl);
338 evTag->SetSize(0);
339 }
340 else evTag->SetPath(fturl);
341
342 // Check if a new run has to be created
343 // File has changed
344 if(fguid != ftempGuid) {
345 ftempGuid = fguid;
346 fTreeT->Fill();
347 fRunTag->Clear("");
348 }
349 // Run# has changed
350 if (oldRun != (fAODEvent->GetRunNumber()))
351 {
352 oldRun = fAODEvent->GetRunNumber();
353 fTreeT->Fill();
354 fRunTag->Clear("");
355 }
356
357 // Add the event tag
358 fRunTag->AddEventTag(*evTag);
359 delete evTag;
360 // Last event
361 if(iEventNumber+1 == fChain->GetEntries()) {
362 fTreeT->Fill();
363 fRunTag->Clear("");
364 }
365 }//event loop
366}
5b8d5d69 367
5b8d5d69 368
5b8d5d69 369
6e557e6d 370void AliAODTagCreator::FillEventTag(AliAODEvent* aod, AliEventTag* evTag)
371{
372//
373// Fill the event tag information
374 //
375 fAODEvent = aod;
376
377 //
378 Float_t fLowPtCut = 1.0;
379 Float_t fHighPtCut = 3.0;
380 Float_t fVeryHighPtCut = 10.0;
381 ////////////
382 Double_t partFrac[10] = {0.01, 0.01, 0.85, 0.10, 0.05, 0., 0., 0., 0., 0.};
383
384 // Creates the tags for all the events in a given AOD file
385 Int_t ntrack = 0;
386 Int_t nPos = 0, nNeg = 0, nNeutr =0;
387 Int_t nKinks = 0, nV0s = 0, nCascades = 0;
388 Int_t nK0s = 0, nNeutrons = 0, nPi0s = 0, nGamas = 0;
d3893274 389 Int_t nProtons = 0, nKaons = 0, nPions = 0, nMuons = 0, nElectrons = 0, nFWMuons = 0;
6e557e6d 390 Int_t nCh1GeV = 0, nCh3GeV = 0, nCh10GeV = 0;
391 Int_t nMu1GeV = 0, nMu3GeV = 0, nMu10GeV = 0;
392 Int_t nEl1GeV = 0, nEl3GeV = 0, nEl10GeV = 0;
393 Float_t maxPt = .0, meanPt = .0, totalP = .0;
394
a69c1246 395 TRefArray tmp;
396
6e557e6d 397 Int_t nTracks = fAODEvent->GetNTracks();
5b8d5d69 398 // loop over vertices
6e557e6d 399 Int_t nVtxs = fAODEvent->GetNumberOfVertices();
5b8d5d69 400 for (Int_t nVtx = 0; nVtx < nVtxs; nVtx++) {
6e557e6d 401 AliAODVertex *vertex = fAODEvent->GetVertex(nVtx);
402 if(vertex->GetType() == 1) nKinks += 1;
403 if(vertex->GetType() == 2) nV0s += 1;
404 if(vertex->GetType() == 3) nCascades += 1;
5b8d5d69 405 }
406 for (Int_t nTr = 0; nTr < nTracks; nTr++) {
6e557e6d 407 AliAODTrack *track = fAODEvent->GetTrack(nTr);
408
409 Double_t fPt = track->Pt();
410 if(fPt > maxPt) maxPt = fPt;
411 if(track->Charge() > 0) {
412 nPos++;
413 if(fPt > fLowPtCut) nCh1GeV++;
414 if(fPt > fHighPtCut) nCh3GeV++;
415 if(fPt > fVeryHighPtCut) nCh10GeV++;
416 }
417 if(track->Charge() < 0) {
418 nNeg++;
419 if(fPt > fLowPtCut) nCh1GeV++;
420 if(fPt > fHighPtCut) nCh3GeV++;
421 if(fPt > fVeryHighPtCut) nCh10GeV++;
422 }
423 if(track->Charge() == 0) nNeutr++;
424 //PID
425 const Double32_t *prob = track->PID();
426 Double_t rcc = 0.0;
427 for(Int_t i = 0; i < AliPID::kSPECIES; i++) rcc += prob[i]*partFrac[i];
428 if(rcc == 0.0) continue;
429 //Bayes' formula
430 Double_t w[10];
431 for(Int_t i = 0; i < AliPID::kSPECIES; i++) w[i] = prob[i]*partFrac[i]/rcc;
432
433 //protons
434 if ((w[4]>w[3])&&(w[4]>w[2])&&(w[4]>w[1])&&(w[4]>w[0])) nProtons++;
435 //kaons
436 if ((w[3]>w[4])&&(w[3]>w[2])&&(w[3]>w[1])&&(w[3]>w[0])) nKaons++;
437 //pions
438 if ((w[2]>w[4])&&(w[2]>w[3])&&(w[2]>w[1])&&(w[2]>w[0])) nPions++;
439 //muons
440 if ((w[1]>w[4])&&(w[1]>w[3])&&(w[1]>w[2])&&(w[1]>w[0])) {
441 nMuons++;
442 if(fPt > fLowPtCut) nMu1GeV++;
443 if(fPt > fHighPtCut) nMu3GeV++;
444 if(fPt > fVeryHighPtCut) nMu10GeV++;
445 }
446 //electrons
447 if ((w[0]>w[4])&&(w[0]>w[3])&&(w[0]>w[2])&&(w[0]>w[1])) {
448 nElectrons++;
449 if(fPt > fLowPtCut) nEl1GeV++;
450 if(fPt > fHighPtCut) nEl3GeV++;
451 if(fPt > fVeryHighPtCut) nEl10GeV++;
452 }
6e557e6d 453 totalP += track->P();
454 meanPt += fPt;
455 ntrack++;
d3893274 456 // forward muons (in the dimuon spectrometer)
457 if(track->IsMuonTrack()) nFWMuons++;
458
5b8d5d69 459 }//track loop
6e557e6d 460 //
5b8d5d69 461 // Fill the event tags
462 if(ntrack != 0)
6e557e6d 463 meanPt = meanPt/ntrack;
a1069ee1 464
7ee67d2a 465
5b8d5d69 466 evTag->SetNumOfTracks(nTracks);
a1069ee1 467 evTag->SetNumOfPosTracks(nPos);
468 evTag->SetNumOfNegTracks(nNeg);
469 evTag->SetNumOfNeutrTracks(nNeutr);
470
5b8d5d69 471 evTag->SetNumOfV0s(nV0s);
472 evTag->SetNumOfCascades(nCascades);
473 evTag->SetNumOfKinks(nKinks);
a1069ee1 474
475 evTag->SetNumOfProtons(nProtons);
476 evTag->SetNumOfKaons(nKaons);
477 evTag->SetNumOfPions(nPions);
478 evTag->SetNumOfMuons(nMuons);
d3893274 479 evTag->SetNumOfFWMuons(nFWMuons);
a1069ee1 480 evTag->SetNumOfElectrons(nElectrons);
481 evTag->SetNumOfPhotons(nGamas);
482 evTag->SetNumOfPi0s(nPi0s);
483 evTag->SetNumOfNeutrons(nNeutrons);
484 evTag->SetNumOfKaon0s(nK0s);
485
486 evTag->SetNumOfChargedAbove1GeV(nCh1GeV);
487 evTag->SetNumOfChargedAbove3GeV(nCh3GeV);
488 evTag->SetNumOfChargedAbove10GeV(nCh10GeV);
489 evTag->SetNumOfMuonsAbove1GeV(nMu1GeV);
490 evTag->SetNumOfMuonsAbove3GeV(nMu3GeV);
491 evTag->SetNumOfMuonsAbove10GeV(nMu10GeV);
492 evTag->SetNumOfElectronsAbove1GeV(nEl1GeV);
493 evTag->SetNumOfElectronsAbove3GeV(nEl3GeV);
494 evTag->SetNumOfElectronsAbove10GeV(nEl10GeV);
a69c1246 495
496 tmp.Clear();
497 evTag->SetNumOfPHOSClusters(fAODEvent->GetPHOSClusters(&tmp));
498 tmp.Clear();
499 evTag->SetNumOfEMCALClusters(fAODEvent->GetEMCALClusters(&tmp));
6e557e6d 500
a1069ee1 501 evTag->SetTotalMomentum(totalP);
502 evTag->SetMeanPt(meanPt);
503 evTag->SetMaxPt(maxPt);
5b8d5d69 504}