Corrected enums
[u/mrichter/AliRoot.git] / STEER / CreateAODfromESD.C
CommitLineData
df9db588 1#include "TFile.h"
2#include "TTree.h"
3
4#include "AliAODEvent.h"
5#include "AliESD.h"
6#include "AliESDtrack.h"
7#include "AliESDVertex.h"
8#include "AliESDv0.h"
9
10#include <iostream>
11
12void CreateAODfromESD(const char *inFileName = "AliESD.root", const char *outFileName = "AliAOD.root") {
13
14 // create an AliAOD object
15 AliAODEvent *aod = new AliAODEvent();
16 aod->CreateStdContent();
17
18 // open the file
19 TFile *outFile = TFile::Open(outFileName, "RECREATE");
20
21 // create the tree
22 TTree *aodTree = new TTree("AOD", "AliAOD tree");
23 aodTree->Branch(aod->GetList());
24
25 // connect to ESD
26 TFile *inFile = TFile::Open(inFileName, "READ");
27 TTree *t = (TTree*) inFile->Get("esdTree");
28 TBranch *b = t->GetBranch("ESD");
29 AliESD *esd = 0;
30 b->SetAddress(&esd);
31
32 Int_t nEvents = b->GetEntries();
33
34 // loop over events and fill them
35 for (Int_t iEvent = 0; iEvent < nEvents; ++iEvent) {
36 b->GetEntry(iEvent);
37
38 // Multiplicity information needed by the header (to be revised!)
39 Int_t nTracks = esd->GetNumberOfTracks();
40 Int_t nPosTracks = 0;
41 for (Int_t iTrack=0; iTrack<nTracks; ++iTrack)
42 if (esd->GetTrack(iTrack)->GetSign()> 0) nPosTracks++;
43
44 // create the header
45 aod->AddHeader(new AliAODHeader(esd ->GetEventNumber(),
46 esd->GetRunNumber(),
47 nTracks,
48 nPosTracks,
49 nTracks-nPosTracks,
50 esd->GetMagneticField(),
51 -999., // centrality; to be filled, still
52 esd->GetTriggerMask(),
53 esd->GetTriggerCluster(),
54 esd->GetEventType()));
55
56 Int_t nV0s = esd->GetNumberOfV0s();
57 Int_t nCascades = esd->GetNumberOfCascades();
58 Int_t nKinks = esd->GetNumberOfKinks();
59 Int_t nVertices = nV0s + nCascades + nKinks;
60
61 aod->ResetStd(nTracks, nVertices);
62
63
64 // Array to take into account the tracks already added to the AOD
65 Bool_t * usedTrack = NULL;
66 if (nTracks>0) {
67 usedTrack = new Bool_t[nTracks];
68 for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) usedTrack[iTrack]=kFALSE;
69 }
70 // Array to take into account the V0s already added to the AOD
71 Bool_t * usedV0 = NULL;
72 if (nV0s>0) {
73 usedV0 = new Bool_t[nV0s];
74 for (Int_t iV0=0; iV0<nV0s; ++iV0) usedV0[iV0]=kFALSE;
75 }
76
77 // Access to the AOD container of vertices
78 TClonesArray &vertices = *(aod->GetVertices());
79 Int_t jVertices=0;
80
81 // Access to the AOD container of tracks
82 TClonesArray &tracks = *(aod->GetTracks());
83 Int_t jTracks=0;
84
85 // Add primary vertex. The primary tracks will be defined
86 // after the loops on the composite objects (V0, cascades, kinks)
87 const AliESDVertex *vtx = esd->GetPrimaryVertex();
88
89 Double_t pos[3];
90 vtx->GetXYZ(pos); // position
91 Double_t cov[6];
92 vtx->GetCovMatrix(cov); //covariance matrix
93
94 AliAODVertex * primary = new(vertices[jVertices++])
95 AliAODVertex(pos, cov, vtx->GetChi2(), NULL, AliAODVertex::kPrimary);
96
97 // Create vertices starting from the most complex objects
98
99 // Cascades
100 for (Int_t nCascade = 0; nCascade < nCascades; ++nCascade) {
101 AliESDcascade *cascade = esd->GetCascade(nCascade);
102
103 Double_t posXi[3];
104 cascade->GetXYZ(posXi[0], posXi[1], posXi[2]);
105 Double_t covXi[6];
106 cascade->GetPosCovXi(covXi);
107
108 // Add the cascade vertex
109 AliAODVertex * vcascade = new(vertices[jVertices++]) AliAODVertex(posXi,
110 covXi,
111 cascade->GetChi2Xi(),
112 primary,
113 AliAODVertex::kCascade);
114
115 primary->AddDaughter(vcascade);
116
117 // Add the V0 from the cascade. The ESD class have to be optimized...
118 // Now we have to search for the corresponding Vo in the list of V0s
119 // using the indeces of the positive and negative tracks
120
121 Int_t posFromV0 = cascade->GetPindex();
122 Int_t negFromV0 = cascade->GetNindex();
123
124
125 AliESDv0 * v0 = 0x0;
126 Int_t indV0 = -1;
127
128 for (Int_t iV0=0; iV0<nV0s; ++iV0) {
129
130 v0 = esd->GetV0(iV0);
131 Int_t pos = v0->GetPindex();
132 Int_t neg = v0->GetNindex();
133
134 if (pos==posFromV0 && neg==negFromV0) {
135 indV0 = iV0;
136 break;
137 }
138 }
139
140 AliAODVertex * vV0FromCascade = 0x0;
141
142 if (indV0>-1) {
143
144 usedV0[indV0] = kTRUE;
145
146 Double_t posV0[3];
147 v0->GetXYZ(posV0[0], posV0[1], posV0[2]);
148 Double_t covV0[6];
149 v0->GetPosCov(covV0);
150
151 vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(posV0,
152 covV0,
153 v0->GetChi2V0(),
154 vcascade,
155 AliAODVertex::kV0);
156 } else {
157
158 Double_t posV0[3];
159 cascade->GetXYZ(posV0[0], posV0[1], posV0[2]);
160 Double_t covV0[6];
161 cascade->GetPosCov(covV0);
162
163 vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(posV0,
164 covV0,
165 v0->GetChi2V0(),
166 vcascade,
167 AliAODVertex::kV0);
168 vcascade->AddDaughter(vV0FromCascade);
169 }
170
171 // Add the positive tracks from the V0
172
173 usedTrack[posFromV0] = kTRUE;
174 {
175
176 AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
177
178 Double_t p[3];
179 esdTrack->GetPxPyPz(p);
180
181 Double_t x[3];
182 esdTrack->GetXYZ(x);
183
184 Double_t cov[21];
185 esdTrack->GetCovarianceXYZPxPyPz(cov);
186
187 Double_t pid[10];
188 esdTrack->GetESDpid(pid);
189
190 vV0FromCascade->AddDaughter(
191 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
192 esdTrack->GetLabel(),
193 p, kTRUE,
194 x,
195 kFALSE,
196 cov,
197 (Short_t)esdTrack->GetSign(),
198 esdTrack->GetITSClusterMap(),
199 pid,
200 vV0FromCascade,
201 AliAODTrack::kSecondary)
202 );
203 }
204
205 // Add the negative tracks from the V0
206
207 usedTrack[negFromV0] = kTRUE;
208 {
209
210 AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
211
212 Double_t p[3];
213 esdTrack->GetPxPyPz(p);
214
215 Double_t x[3];
216 esdTrack->GetXYZ(x);
217
218 Double_t cov[21];
219 esdTrack->GetCovarianceXYZPxPyPz(cov);
220
221 Double_t pid[10];
222 esdTrack->GetESDpid(pid);
223
224 vV0FromCascade->AddDaughter(
225 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
226 esdTrack->GetLabel(),
227 p,
228 kTRUE,
229 x,
230 kFALSE,
231 cov,
232 (Short_t)esdTrack->GetSign(),
233 esdTrack->GetITSClusterMap(),
234 pid,
235 vV0FromCascade,
236 AliAODTrack::kSecondary)
237 );
238 }
239
240 // Add the bachelor track from the cascade
241
242 Int_t bachelor = cascade->GetBindex();
243
244 usedTrack[bachelor] = kTRUE;
245 {
246
247 AliESDtrack *esdTrack = esd->GetTrack(bachelor);
248
249 Double_t p[3];
250 esdTrack->GetPxPyPz(p);
251
252 Double_t x[3];
253 esdTrack->GetXYZ(x);
254
255 Double_t cov[21];
256 esdTrack->GetCovarianceXYZPxPyPz(cov);
257
258 Double_t pid[10];
259 esdTrack->GetESDpid(pid);
260
261 vcascade->AddDaughter(
262 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
263 esdTrack->GetLabel(),
264 p,
265 kTRUE,
266 x,
267 kFALSE,
268 cov,
269 (Short_t)esdTrack->GetSign(),
270 esdTrack->GetITSClusterMap(),
271 pid,
272 vcascade,
273 AliAODTrack::kSecondary)
274 );
275 }
276
277 // Add the primary track of the cascade (if any)
278
279 }
280
281 // V0s
282
283 for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {
284
285 if (usedV0[nV0]) continue; // skip if aready added to the AOD
286
287 AliESDv0 *v0 = esd->GetV0(nV0);
288
289 Double_t posV0[3];
290 v0->GetXYZ(posV0[0], posV0[1], posV0[2]);
291 Double_t covV0[6];
292 v0->GetPosCov(covV0);
293
294 AliAODVertex * vV0 =
295 new(vertices[jVertices++]) AliAODVertex(posV0,
296 covV0,
297 v0->GetChi2V0(),
298 primary,
299 AliAODVertex::kV0);
300 primary->AddDaughter(vV0);
301
302 Int_t posFromV0 = v0->GetPindex();
303 Int_t negFromV0 = v0->GetNindex();
304
305 // Add the positive tracks from the V0
306
307 usedTrack[posFromV0] = kTRUE;
308 {
309
310 AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
311
312 Double_t p[3];
313 esdTrack->GetPxPyPz(p);
314
315 Double_t x[3];
316 esdTrack->GetXYZ(x);
317
318 Double_t cov[21];
319 esdTrack->GetCovarianceXYZPxPyPz(cov);
320
321 Double_t pid[10];
322 esdTrack->GetESDpid(pid);
323
324 vV0->AddDaughter(
325 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
326 esdTrack->GetLabel(),
327 p, kTRUE,
328 x,
329 kFALSE,
330 cov,
331 (Short_t)esdTrack->GetSign(),
332 esdTrack->GetITSClusterMap(),
333 pid,
334 vV0,
335 AliAODTrack::kSecondary)
336 );
337 }
338
339 // Add the negative tracks from the V0
340
341 usedTrack[negFromV0] = kTRUE;
342 {
343
344 AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
345
346 Double_t p[3];
347 esdTrack->GetPxPyPz(p);
348
349 Double_t x[3];
350 esdTrack->GetXYZ(x);
351
352 Double_t cov[21];
353 esdTrack->GetCovarianceXYZPxPyPz(cov);
354
355 Double_t pid[10];
356 esdTrack->GetESDpid(pid);
357
358 vV0->AddDaughter(
359 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
360 esdTrack->GetLabel(),
361 p,
362 kTRUE,
363 x,
364 kFALSE,
365 cov,
366 (Short_t)esdTrack->GetSign(),
367 esdTrack->GetITSClusterMap(),
368 pid,
369 vV0,
370 AliAODTrack::kSecondary)
371 );
372 }
373
374
375 }
376
377 // Kinks
378 for (Int_t nKink = 0; nKink < nKinks; ++nKink) {
379
380 AliESDkink *kink = esd->GetKink(nKink);
381
382 new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),
383 NULL,
384 0.,
385 NULL,
386 AliAODVertex::kKink); // create a vtx
387 }
388
389 // create tracks
390
391 for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {
392
393
394 if (usedTrack[nTrack]) continue;
395
396 AliESDtrack *esdTrack = esd->GetTrack(nTrack);
397
398 Double_t p[3];
399 esdTrack->GetPxPyPz(p);
400
401 Double_t x[3];
402 esdTrack->GetXYZ(x);
403
404 Double_t cov[21];
405 esdTrack->GetCovarianceXYZPxPyPz(cov);
406
407 Double_t pid[10];
408 esdTrack->GetESDpid(pid);
409
410 primary->AddDaughter(
411 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
412 esdTrack->GetLabel(),
413 p,
414 kTRUE,
415 x,
416 kFALSE,
417 cov,
418 (Short_t)esdTrack->GetSign(),
419 esdTrack->GetITSClusterMap(),
420 pid,
421 primary,
422 AliAODTrack::kPrimary)
423 );
424
425 }
426
427 // fill the tree for this event
428 aodTree->Fill();
429 }
430
431 aodTree->GetUserInfo()->Add(aod);
432
433 // close ESD file
434 inFile->Close();
435
436 // write the tree to the specified file
437 outFile = aodTree->GetCurrentFile();
438 outFile->cd();
439 aodTree->Write();
440 outFile->Close();
441
442}