1 /**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 //-------------------------------------------------------------------------
19 // Class for Kinematic Events
20 // Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
27 #include <TParticle.h>
28 #include <TClonesArray.h>
29 #include <TRefArray.h>
32 #include "AliMCEvent.h"
34 #include "AliTrackReference.h"
35 #include "AliHeader.h"
36 #include "AliGenEventHeader.h"
39 AliMCEvent::AliMCEvent():
42 fMCParticles(new TClonesArray("AliMCParticle",1000)),
46 fTrackReferences(new TClonesArray("AliTrackReference", 1000)),
53 // Default constructor
56 AliMCEvent::AliMCEvent(const AliMCEvent& mcEvnt) :
74 AliMCEvent& AliMCEvent::operator=(const AliMCEvent& mcEvnt)
76 // assignment operator
78 AliVEvent::operator=(mcEvnt);
84 void AliMCEvent::ConnectTreeE (TTree* tree)
86 // Connect the event header tree
87 tree->SetBranchAddress("Header", &fHeader);
90 void AliMCEvent::ConnectTreeK (TTree* tree)
92 // Connect the kinematics tree to the stack
93 fStack = fHeader->Stack();
94 fStack->ConnectTree(tree);
98 fNparticles = fStack->GetNtrack();
99 fNprimaries = fStack->GetNprimary();
100 AliInfo(Form("AliMCEvent: Number of particles: %5d (all) %5d (primaries)\n",
101 fNparticles, fNprimaries));
103 // This is a cache for the TParticles converted to MCParticles on user request
104 if (fMCParticleMap) {
105 fMCParticleMap->Clear();
106 if (fNparticles>0) fMCParticleMap->Expand(fNparticles);}
108 fMCParticleMap = new TRefArray(fNparticles);
112 void AliMCEvent::ConnectTreeTR (TTree* tree)
114 // Connect the track reference tree
117 if (fTreeTR->GetBranch("AliRun")) {
122 // This is an old format with one branch per detector not in synch with TreeK
123 ReorderAndExpandTreeTR();
126 fTreeTR->SetBranchAddress("TrackReferences", &fTRBuffer);
130 Int_t AliMCEvent::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
133 if (i < 0 || i >= fNparticles) {
134 AliWarning(Form("AliMCEventHandler::GetEntry: Index out of range"));
139 particle = fStack->Particle(i);
141 fTreeTR->GetEntry(fStack->TreeKEntry(i));
143 return trefs->GetEntries();
151 void AliMCEvent::Clean()
153 // Clean-up before new trees are connected
170 void AliMCEvent::FinishEvent()
172 // Clean-up after event
174 fMCParticles->Clear();
175 fTrackReferences->Clear();
180 void AliMCEvent::DrawCheck(Int_t i, Int_t search)
183 // Simple event display for debugging
185 AliWarning("No Track Reference information available");
189 if (i > -1 && i < fNparticles) {
190 fTreeTR->GetEntry(fStack->TreeKEntry(i));
192 AliWarning("AliMCEvent::GetEntry: Index out of range");
195 Int_t nh = fTRBuffer->GetEntries();
199 while(nh <= search && i < fNparticles - 1) {
201 fTreeTR->GetEntry(fStack->TreeKEntry(i));
202 nh = fTRBuffer->GetEntries();
204 printf("Found Hits at %5d\n", i);
206 TParticle* particle = fStack->Particle(i);
208 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
209 Float_t x0 = particle->Vx();
210 Float_t y0 = particle->Vy();
212 Float_t x1 = particle->Vx() + particle->Px() * 50.;
213 Float_t y1 = particle->Vy() + particle->Py() * 50.;
215 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
221 for (Int_t ih = 0; ih < nh; ih++) {
222 AliTrackReference* ref = (AliTrackReference*) fTRBuffer->At(ih);
223 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
225 m->SetMarkerSize(0.4);
231 void AliMCEvent::ReorderAndExpandTreeTR()
234 // Reorder and expand the track reference tree in order to match the kinematics tree.
235 // Copy the information from different branches into one
239 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
240 fTmpTreeTR = new TTree("TreeTR", "TrackReferences");
241 if (!fTRBuffer) fTRBuffer = new TClonesArray("AliTrackReference", 100);
242 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTRBuffer, 32000, 0);
246 // Activate the used branches only. Otherwisw we get a bad memory leak.
247 fTreeTR->SetBranchStatus("*", 0);
248 fTreeTR->SetBranchStatus("AliRun.*", 1);
249 fTreeTR->SetBranchStatus("ITS.*", 1);
250 fTreeTR->SetBranchStatus("TPC.*", 1);
251 fTreeTR->SetBranchStatus("TRD.*", 1);
252 fTreeTR->SetBranchStatus("TOF.*", 1);
253 fTreeTR->SetBranchStatus("FRAME.*", 1);
254 fTreeTR->SetBranchStatus("MUON.*", 1);
256 // Connect the active branches
257 TClonesArray* trefs[7];
258 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
260 // make branch for central track references
261 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
262 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
263 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
264 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
265 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
266 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
267 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
270 Int_t np = fStack->GetNprimary();
271 Int_t nt = fTreeTR->GetEntries();
274 // Loop over tracks and find the secondaries with the help of the kine tree
280 for (Int_t ip = np - 1; ip > -1; ip--) {
281 part = fStack->Particle(ip);
282 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
283 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
284 // part->GetLastDaughter(), part->TestBit(kTransportBit));
286 // Determine range of secondaries produced by this primary during transport
287 Int_t dau1 = part->GetFirstDaughter();
288 if (dau1 < np) continue; // This particle has no secondaries produced during transport
291 Int_t inext = ip - 1;
294 part = fStack->Particle(inext);
295 dau2 = part->GetFirstDaughter();
296 if (dau2 == -1 || dau2 < np) {
302 dau2 = fStack->GetNtrack() - 1;
305 } // find upper bound
309 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
311 // Loop over reference hits and find secondary label
312 // First the tricky part: find the entry in treeTR than contains the hits or
313 // make sure that no hits exist.
315 Bool_t hasHits = kFALSE;
316 Bool_t isOutside = kFALSE;
319 while (!hasHits && !isOutside && it < nt) {
320 fTreeTR->GetEntry(it++);
321 for (Int_t ib = 0; ib < 7; ib++) {
322 if (!trefs[ib]) continue;
323 Int_t nh = trefs[ib]->GetEntries();
324 for (Int_t ih = 0; ih < nh; ih++) {
325 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
326 Int_t label = tr->Label();
327 if (label >= dau1 && label <= dau2) {
332 if (label > dau2 || label < ip) {
338 if (hasHits || isOutside) break;
343 // Write empty entries
344 for (Int_t id = dau1; (id <= dau2); id++) {
350 fTreeTR->GetEntry(itlast);
351 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
352 for (Int_t ib = 0; ib < 7; ib++) {
353 if (!trefs[ib]) continue;
354 Int_t nh = trefs[ib]->GetEntries();
355 for (Int_t ih = 0; ih < nh; ih++) {
356 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
357 Int_t label = tr->Label();
359 if (label == ip) continue;
360 if (label > dau2 || label < dau1)
361 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
362 itlast, label, dau1, dau2);
365 tr->SetDetectorId(ib-1);
366 Int_t nref = fTRBuffer->GetEntriesFast();
367 TClonesArray &lref = *fTRBuffer;
368 new(lref[nref]) AliTrackReference(*tr);
380 // Now loop again and write the primaries
383 for (Int_t ip = 0; ip < np; ip++) {
385 while (labmax < ip && it > -1) {
386 fTreeTR->GetEntry(it--);
387 for (Int_t ib = 0; ib < 7; ib++) {
388 if (!trefs[ib]) continue;
389 Int_t nh = trefs[ib]->GetEntries();
391 // Loop over reference hits and find primary labels
392 for (Int_t ih = 0; ih < nh; ih++) {
393 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
394 Int_t label = tr->Label();
395 if (label < np && label > labmax) {
400 tr->SetDetectorId(ib-1);
401 Int_t nref = fTRBuffer->GetEntriesFast();
402 TClonesArray &lref = *fTRBuffer;
403 new(lref[nref]) AliTrackReference(*tr);
417 delete fTreeTR; fTreeTR = 0;
419 for (Int_t ib = 0; ib < 7; ib++) {
427 if (ifills != fStack->GetNtrack())
428 printf("AliMCEvent:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
429 ifills, fStack->GetNtrack());
432 fTreeTR = fTmpTreeTR;
435 AliMCParticle* AliMCEvent::GetTrack(Int_t i) const
438 AliMCParticle *mcParticle = 0;
439 TParticle *particle = 0;
440 TClonesArray *trefs = 0;
442 TRefArray *rarray = 0;
444 // Out of range check
445 if (i < 0 || i >= fNparticles) {
446 AliWarning(Form("AliMCEvent::GetEntry: Index out of range"));
454 // First check of the MC Particle has been already cached
455 if(!fMCParticleMap->At(i)) {
456 // Get particle from the stack
457 particle = fStack->Particle(i);
458 // Get track references from Tree TR
460 fTreeTR->GetEntry(fStack->TreeKEntry(i));
462 ntref = trefs->GetEntriesFast();
463 rarray = new TRefArray(ntref);
464 Int_t nen = fTrackReferences->GetEntriesFast();
465 for (Int_t j = 0; j < ntref; j++) {
466 // Save the track references in a TClonesArray
467 AliTrackReference* ref = dynamic_cast<AliTrackReference*>((*fTRBuffer)[j]);
468 // Save the pointer in a TRefArray
469 new ((*fTrackReferences)[nen]) AliTrackReference(*ref);
470 rarray->AddAt((*fTrackReferences)[nen], j);
472 } // loop over track references for entry i
473 } // if TreeTR available
475 Int_t nentries = fMCParticles->GetEntriesFast();
476 new ((*fMCParticles)[nentries]) AliMCParticle(particle, rarray);
477 mcParticle = dynamic_cast<AliMCParticle*>((*fMCParticles)[nentries]);
478 fMCParticleMap->AddAt(mcParticle, i);
480 mcParticle = dynamic_cast<AliMCParticle*>(fMCParticleMap->At(i));
486 AliGenEventHeader* AliMCEvent::GenEventHeader() {return (fHeader->GenEventHeader());}