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>
33 #include "AliMCEvent.h"
35 #include "AliTrackReference.h"
36 #include "AliHeader.h"
37 #include "AliGenEventHeader.h"
40 Int_t AliMCEvent::fgkBgLabelOffset(10000000);
43 AliMCEvent::AliMCEvent():
46 fMCParticles(new TClonesArray("AliMCParticle",1000)),
48 fHeader(new AliHeader()),
50 fTrackReferences(new TClonesArray("AliTrackReference", 1000)),
60 // Default constructor
63 AliMCEvent::AliMCEvent(const AliMCEvent& mcEvnt) :
65 fStack(mcEvnt.fStack),
66 fMCParticles(mcEvnt.fMCParticles),
67 fMCParticleMap(mcEvnt.fMCParticleMap),
68 fHeader(mcEvnt.fHeader),
69 fTRBuffer(mcEvnt.fTRBuffer),
70 fTrackReferences(mcEvnt.fTrackReferences),
71 fTreeTR(mcEvnt.fTreeTR),
72 fTmpTreeTR(mcEvnt.fTmpTreeTR),
73 fTmpFileTR(mcEvnt.fTmpFileTR),
74 fNprimaries(mcEvnt.fNprimaries),
75 fNparticles(mcEvnt.fNparticles),
84 AliMCEvent& AliMCEvent::operator=(const AliMCEvent& mcEvnt)
86 // assignment operator
88 AliVEvent::operator=(mcEvnt);
94 void AliMCEvent::ConnectTreeE (TTree* tree)
96 // Connect the event header tree
97 tree->SetBranchAddress("Header", &fHeader);
100 void AliMCEvent::ConnectTreeK (TTree* tree)
102 // Connect the kinematics tree to the stack
103 fStack = fHeader->Stack();
104 fStack->ConnectTree(tree);
108 fNparticles = fStack->GetNtrack();
109 fNprimaries = fStack->GetNprimary();
110 Int_t iev = fHeader->GetEvent();
111 Int_t ievr = fHeader->GetEventNrInRun();
113 AliInfo(Form("AliMCEvent# %5d %5d: Number of particles: %5d (all) %5d (primaries)\n",
114 iev, ievr, fNparticles, fNprimaries));
116 // This is a cache for the TParticles converted to MCParticles on user request
117 if (fMCParticleMap) {
118 fMCParticleMap->Clear();
119 fMCParticles->Delete();
120 if (fNparticles>0) fMCParticleMap->Expand(fNparticles);
123 fMCParticleMap = new TRefArray(fNparticles);
126 void AliMCEvent::ConnectTreeTR (TTree* tree)
128 // Connect the track reference tree
131 if (fTreeTR->GetBranch("AliRun")) {
136 // This is an old format with one branch per detector not in synch with TreeK
137 ReorderAndExpandTreeTR();
140 fTreeTR->SetBranchAddress("TrackReferences", &fTRBuffer);
144 Int_t AliMCEvent::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
147 if (i < 0 || i >= fNparticles) {
148 AliWarning(Form("AliMCEventHandler::GetEntry: Index out of range"));
153 particle = fStack->Particle(i);
155 fTreeTR->GetEntry(fStack->TreeKEntry(i));
157 return trefs->GetEntries();
165 void AliMCEvent::Clean()
167 // Clean-up before new trees are connected
173 delete fStack; fStack = 0;
183 void AliMCEvent::FinishEvent()
185 // Clean-up after event
188 fMCParticles->Delete();
189 fMCParticleMap->Clear();
192 fTrackReferences->Delete();
196 // fSubsidiaryEvents->Clear();
197 fSubsidiaryEvents = 0;
202 void AliMCEvent::DrawCheck(Int_t i, Int_t search)
205 // Simple event display for debugging
207 AliWarning("No Track Reference information available");
211 if (i > -1 && i < fNparticles) {
212 fTreeTR->GetEntry(fStack->TreeKEntry(i));
214 AliWarning("AliMCEvent::GetEntry: Index out of range");
217 Int_t nh = fTRBuffer->GetEntries();
221 while(nh <= search && i < fNparticles - 1) {
223 fTreeTR->GetEntry(fStack->TreeKEntry(i));
224 nh = fTRBuffer->GetEntries();
226 printf("Found Hits at %5d\n", i);
228 TParticle* particle = fStack->Particle(i);
230 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
231 Float_t x0 = particle->Vx();
232 Float_t y0 = particle->Vy();
234 Float_t x1 = particle->Vx() + particle->Px() * 50.;
235 Float_t y1 = particle->Vy() + particle->Py() * 50.;
237 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
243 for (Int_t ih = 0; ih < nh; ih++) {
244 AliTrackReference* ref = (AliTrackReference*) fTRBuffer->At(ih);
245 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
247 m->SetMarkerSize(0.4);
253 void AliMCEvent::ReorderAndExpandTreeTR()
256 // Reorder and expand the track reference tree in order to match the kinematics tree.
257 // Copy the information from different branches into one
261 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
262 fTmpTreeTR = new TTree("TreeTR", "TrackReferences");
263 if (!fTRBuffer) fTRBuffer = new TClonesArray("AliTrackReference", 100);
264 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTRBuffer, 32000, 0);
268 // Activate the used branches only. Otherwisw we get a bad memory leak.
269 fTreeTR->SetBranchStatus("*", 0);
270 fTreeTR->SetBranchStatus("AliRun.*", 1);
271 fTreeTR->SetBranchStatus("ITS.*", 1);
272 fTreeTR->SetBranchStatus("TPC.*", 1);
273 fTreeTR->SetBranchStatus("TRD.*", 1);
274 fTreeTR->SetBranchStatus("TOF.*", 1);
275 fTreeTR->SetBranchStatus("FRAME.*", 1);
276 fTreeTR->SetBranchStatus("MUON.*", 1);
278 // Connect the active branches
279 TClonesArray* trefs[7];
280 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
282 // make branch for central track references
283 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
284 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
285 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
286 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
287 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
288 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
289 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
292 Int_t np = fStack->GetNprimary();
293 Int_t nt = fTreeTR->GetEntries();
296 // Loop over tracks and find the secondaries with the help of the kine tree
302 for (Int_t ip = np - 1; ip > -1; ip--) {
303 part = fStack->Particle(ip);
304 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
305 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
306 // part->GetLastDaughter(), part->TestBit(kTransportBit));
308 // Determine range of secondaries produced by this primary during transport
309 Int_t dau1 = part->GetFirstDaughter();
310 if (dau1 < np) continue; // This particle has no secondaries produced during transport
313 Int_t inext = ip - 1;
316 part = fStack->Particle(inext);
317 dau2 = part->GetFirstDaughter();
318 if (dau2 == -1 || dau2 < np) {
324 dau2 = fStack->GetNtrack() - 1;
327 } // find upper bound
331 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
333 // Loop over reference hits and find secondary label
334 // First the tricky part: find the entry in treeTR than contains the hits or
335 // make sure that no hits exist.
337 Bool_t hasHits = kFALSE;
338 Bool_t isOutside = kFALSE;
341 while (!hasHits && !isOutside && it < nt) {
342 fTreeTR->GetEntry(it++);
343 for (Int_t ib = 0; ib < 7; ib++) {
344 if (!trefs[ib]) continue;
345 Int_t nh = trefs[ib]->GetEntries();
346 for (Int_t ih = 0; ih < nh; ih++) {
347 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
348 Int_t label = tr->Label();
349 if (label >= dau1 && label <= dau2) {
354 if (label > dau2 || label < ip) {
360 if (hasHits || isOutside) break;
365 // Write empty entries
366 for (Int_t id = dau1; (id <= dau2); id++) {
372 fTreeTR->GetEntry(itlast);
373 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
374 for (Int_t ib = 0; ib < 7; ib++) {
375 if (!trefs[ib]) continue;
376 Int_t nh = trefs[ib]->GetEntries();
377 for (Int_t ih = 0; ih < nh; ih++) {
378 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
379 Int_t label = tr->Label();
381 if (label == ip) continue;
382 if (label > dau2 || label < dau1)
383 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
384 itlast, label, dau1, dau2);
387 tr->SetDetectorId(ib-1);
388 Int_t nref = fTRBuffer->GetEntriesFast();
389 TClonesArray &lref = *fTRBuffer;
390 new(lref[nref]) AliTrackReference(*tr);
402 // Now loop again and write the primaries
405 for (Int_t ip = 0; ip < np; ip++) {
407 while (labmax < ip && it > -1) {
408 fTreeTR->GetEntry(it--);
409 for (Int_t ib = 0; ib < 7; ib++) {
410 if (!trefs[ib]) continue;
411 Int_t nh = trefs[ib]->GetEntries();
413 // Loop over reference hits and find primary labels
414 for (Int_t ih = 0; ih < nh; ih++) {
415 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
416 Int_t label = tr->Label();
417 if (label < np && label > labmax) {
422 tr->SetDetectorId(ib-1);
423 Int_t nref = fTRBuffer->GetEntriesFast();
424 TClonesArray &lref = *fTRBuffer;
425 new(lref[nref]) AliTrackReference(*tr);
439 delete fTreeTR; fTreeTR = 0;
441 for (Int_t ib = 0; ib < 7; ib++) {
449 if (ifills != fStack->GetNtrack())
450 printf("AliMCEvent:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
451 ifills, fStack->GetNtrack());
454 fTreeTR = fTmpTreeTR;
457 AliMCParticle* AliMCEvent::GetTrack(Int_t i) const
462 // Check first if this explicitely accesses the subsidiary event
463 if (i >= BgLabelOffset()) {
464 if (fSubsidiaryEvents) {
465 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
466 return (bgEvent->GetTrack(i - BgLabelOffset()));
473 AliMCParticle *mcParticle = 0;
474 TParticle *particle = 0;
475 TClonesArray *trefs = 0;
477 TRefArray *rarray = 0;
481 // Out of range check
482 if (i < 0 || i >= fNparticles) {
483 AliWarning(Form("AliMCEvent::GetEntry: Index out of range"));
489 if (fSubsidiaryEvents) {
491 Int_t idx = FindIndexAndEvent(i, mc);
492 return (mc->GetTrack(idx));
496 // First check If the MC Particle has been already cached
497 if(!fMCParticleMap->At(i)) {
498 // Get particle from the stack
499 particle = fStack->Particle(i);
500 // Get track references from Tree TR
502 fTreeTR->GetEntry(fStack->TreeKEntry(i));
504 ntref = trefs->GetEntriesFast();
505 rarray = new TRefArray(ntref);
506 Int_t nen = fTrackReferences->GetEntriesFast();
507 for (Int_t j = 0; j < ntref; j++) {
508 // Save the track references in a TClonesArray
509 AliTrackReference* ref = dynamic_cast<AliTrackReference*>((*fTRBuffer)[j]);
510 // Save the pointer in a TRefArray
511 new ((*fTrackReferences)[nen]) AliTrackReference(*ref);
512 rarray->AddAt((*fTrackReferences)[nen], j);
514 } // loop over track references for entry i
515 } // if TreeTR available
516 Int_t nentries = fMCParticles->GetEntriesFast();
517 new ((*fMCParticles)[nentries]) AliMCParticle(particle, rarray, i);
518 mcParticle = dynamic_cast<AliMCParticle*>((*fMCParticles)[nentries]);
519 fMCParticleMap->AddAt(mcParticle, i);
521 TParticle* part = mcParticle->Particle();
522 Int_t imo = part->GetFirstMother();
523 Int_t id1 = part->GetFirstDaughter();
524 Int_t id2 = part->GetLastDaughter();
525 if (fPrimaryOffset > 0 || fSecondaryOffset > 0) {
526 // Remapping of the mother and daughter indices
527 if (imo < fNprimaries) {
528 mcParticle->SetMother(imo + fPrimaryOffset);
530 mcParticle->SetMother(imo + fSecondaryOffset - fNprimaries);
533 if (id1 < fNprimaries) {
534 mcParticle->SetFirstDaughter(id1 + fPrimaryOffset);
535 mcParticle->SetLastDaughter (id2 + fPrimaryOffset);
537 mcParticle->SetFirstDaughter(id1 + fSecondaryOffset - fNprimaries);
538 mcParticle->SetLastDaughter (id2 + fSecondaryOffset - fNprimaries);
542 if (i > fNprimaries) {
543 mcParticle->SetLabel(i + fPrimaryOffset);
545 mcParticle->SetLabel(i + fSecondaryOffset - fNprimaries);
550 mcParticle->SetFirstDaughter(id1);
551 mcParticle->SetLastDaughter (id2);
552 mcParticle->SetMother (imo);
556 mcParticle = dynamic_cast<AliMCParticle*>(fMCParticleMap->At(i));
563 AliGenEventHeader* AliMCEvent::GenEventHeader() {return (fHeader->GenEventHeader());}
566 void AliMCEvent::AddSubsidiaryEvent(AliMCEvent* event)
568 // Add a subsidiary event to the list; for example merged background event.
569 if (!fSubsidiaryEvents) {
570 TList* events = new TList();
571 events->Add(new AliMCEvent(*this));
572 fSubsidiaryEvents = events;
575 fSubsidiaryEvents->Add(event);
578 Int_t AliMCEvent::FindIndexAndEvent(Int_t oldidx, AliMCEvent*& event) const
580 // Find the index and event in case of composed events like signal + background
581 TIter next(fSubsidiaryEvents);
583 if (oldidx < fNprimaries) {
584 while((event = (AliMCEvent*)next())) {
585 if (oldidx < (event->GetPrimaryOffset() + event->GetNumberOfPrimaries())) break;
587 return (oldidx - event->GetPrimaryOffset());
589 while((event = (AliMCEvent*)next())) {
590 if (oldidx < (event->GetSecondaryOffset() + (event->GetNumberOfTracks() - event->GetNumberOfPrimaries()))) break;
592 return (oldidx - event->GetSecondaryOffset() + event->GetNumberOfPrimaries());
596 Int_t AliMCEvent::BgLabelToIndex(Int_t label)
598 // Convert a background label to an absolute index
599 if (fSubsidiaryEvents) {
600 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
601 label -= BgLabelOffset();
602 if (label < bgEvent->GetNumberOfPrimaries()) {
603 label += bgEvent->GetPrimaryOffset();
605 label += (bgEvent->GetSecondaryOffset() - fNprimaries);
612 Bool_t AliMCEvent::IsPhysicalPrimary(Int_t i)
615 // Delegate to subevent if necesarry
618 if (!fSubsidiaryEvents) {
619 return fStack->IsPhysicalPrimary(i);
622 Int_t idx = FindIndexAndEvent(i, evt);
623 return (evt->IsPhysicalPrimary(idx));
627 void AliMCEvent::InitEvent()
629 if (fSubsidiaryEvents) {
630 TIter next(fSubsidiaryEvents);
635 while((evt = (AliMCEvent*)next())) {
636 fNprimaries += evt->GetNumberOfPrimaries();
637 fNparticles += evt->GetNumberOfTracks();
641 Int_t ioffs = fNprimaries;
644 while((evt = (AliMCEvent*)next())) {
645 evt->SetPrimaryOffset(ioffp);
646 evt->SetSecondaryOffset(ioffs);
647 ioffp += evt->GetNumberOfPrimaries();
648 ioffs += (evt->GetNumberOfTracks() - evt->GetNumberOfPrimaries());