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>
34 #include "AliMCEvent.h"
35 #include "AliMCVertex.h"
37 #include "AliTrackReference.h"
38 #include "AliHeader.h"
39 #include "AliGenEventHeader.h"
42 Int_t AliMCEvent::fgkBgLabelOffset(10000000);
45 AliMCEvent::AliMCEvent():
50 fHeader(new AliHeader()),
52 fTrackReferences(new TClonesArray("AliTrackReference", 1000)),
64 // Default constructor
67 AliMCEvent::AliMCEvent(const AliMCEvent& mcEvnt) :
69 fStack(mcEvnt.fStack),
70 fMCParticles(mcEvnt.fMCParticles),
71 fMCParticleMap(mcEvnt.fMCParticleMap),
72 fHeader(mcEvnt.fHeader),
73 fTRBuffer(mcEvnt.fTRBuffer),
74 fTrackReferences(mcEvnt.fTrackReferences),
75 fTreeTR(mcEvnt.fTreeTR),
76 fTmpTreeTR(mcEvnt.fTmpTreeTR),
77 fTmpFileTR(mcEvnt.fTmpFileTR),
78 fNprimaries(mcEvnt.fNprimaries),
79 fNparticles(mcEvnt.fNparticles),
84 fVertex(mcEvnt.fVertex)
90 AliMCEvent& AliMCEvent::operator=(const AliMCEvent& mcEvnt)
92 // assignment operator
94 AliVEvent::operator=(mcEvnt);
100 void AliMCEvent::ConnectTreeE (TTree* tree)
102 // Connect the event header tree
103 tree->SetBranchAddress("Header", &fHeader);
106 void AliMCEvent::ConnectTreeK (TTree* tree)
108 // Connect the kinematics tree to the stack
109 if (!fMCParticles) fMCParticles = new TClonesArray("AliMCParticle",1000);
111 fStack = fHeader->Stack();
112 fStack->ConnectTree(tree);
116 fNparticles = fStack->GetNtrack();
117 fNprimaries = fStack->GetNprimary();
118 Int_t iev = fHeader->GetEvent();
119 Int_t ievr = fHeader->GetEventNrInRun();
121 AliInfo(Form("AliMCEvent# %5d %5d: Number of particles: %5d (all) %5d (primaries)\n",
122 iev, ievr, fNparticles, fNprimaries));
124 // This is a cache for the TParticles converted to MCParticles on user request
125 if (fMCParticleMap) {
126 fMCParticleMap->Clear();
127 fMCParticles->Delete();
128 if (fNparticles>0) fMCParticleMap->Expand(fNparticles);
131 fMCParticleMap = new TRefArray(fNparticles);
134 void AliMCEvent::ConnectTreeTR (TTree* tree)
136 // Connect the track reference tree
139 if (fTreeTR->GetBranch("AliRun")) {
144 // This is an old format with one branch per detector not in synch with TreeK
145 ReorderAndExpandTreeTR();
148 fTreeTR->SetBranchAddress("TrackReferences", &fTRBuffer);
152 Int_t AliMCEvent::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
155 if (i < 0 || i >= fNparticles) {
156 AliWarning(Form("AliMCEventHandler::GetEntry: Index out of range"));
161 particle = fStack->Particle(i);
163 fTreeTR->GetEntry(fStack->TreeKEntry(i));
165 return trefs->GetEntries();
173 void AliMCEvent::Clean()
175 // Clean-up before new trees are connected
176 delete fStack; fStack = 0;
186 void AliMCEvent::FinishEvent()
188 // Clean-up after event
191 fMCParticles->Delete();
192 fMCParticleMap->Clear();
195 fTrackReferences->Delete();
199 // fSubsidiaryEvents->Clear();
200 fSubsidiaryEvents = 0;
205 void AliMCEvent::DrawCheck(Int_t i, Int_t search)
208 // Simple event display for debugging
210 AliWarning("No Track Reference information available");
214 if (i > -1 && i < fNparticles) {
215 fTreeTR->GetEntry(fStack->TreeKEntry(i));
217 AliWarning("AliMCEvent::GetEntry: Index out of range");
220 Int_t nh = fTRBuffer->GetEntries();
224 while(nh <= search && i < fNparticles - 1) {
226 fTreeTR->GetEntry(fStack->TreeKEntry(i));
227 nh = fTRBuffer->GetEntries();
229 printf("Found Hits at %5d\n", i);
231 TParticle* particle = fStack->Particle(i);
233 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
234 Float_t x0 = particle->Vx();
235 Float_t y0 = particle->Vy();
237 Float_t x1 = particle->Vx() + particle->Px() * 50.;
238 Float_t y1 = particle->Vy() + particle->Py() * 50.;
240 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
246 for (Int_t ih = 0; ih < nh; ih++) {
247 AliTrackReference* ref = (AliTrackReference*) fTRBuffer->At(ih);
248 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
250 m->SetMarkerSize(0.4);
256 void AliMCEvent::ReorderAndExpandTreeTR()
259 // Reorder and expand the track reference tree in order to match the kinematics tree.
260 // Copy the information from different branches into one
264 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
265 fTmpTreeTR = new TTree("TreeTR", "TrackReferences");
266 if (!fTRBuffer) fTRBuffer = new TClonesArray("AliTrackReference", 100);
267 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTRBuffer, 32000, 0);
271 // Activate the used branches only. Otherwisw we get a bad memory leak.
272 fTreeTR->SetBranchStatus("*", 0);
273 fTreeTR->SetBranchStatus("AliRun.*", 1);
274 fTreeTR->SetBranchStatus("ITS.*", 1);
275 fTreeTR->SetBranchStatus("TPC.*", 1);
276 fTreeTR->SetBranchStatus("TRD.*", 1);
277 fTreeTR->SetBranchStatus("TOF.*", 1);
278 fTreeTR->SetBranchStatus("FRAME.*", 1);
279 fTreeTR->SetBranchStatus("MUON.*", 1);
281 // Connect the active branches
282 TClonesArray* trefs[7];
283 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
285 // make branch for central track references
286 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
287 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
288 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
289 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
290 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
291 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
292 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
295 Int_t np = fStack->GetNprimary();
296 Int_t nt = fTreeTR->GetEntries();
299 // Loop over tracks and find the secondaries with the help of the kine tree
305 for (Int_t ip = np - 1; ip > -1; ip--) {
306 part = fStack->Particle(ip);
307 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
308 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
309 // part->GetLastDaughter(), part->TestBit(kTransportBit));
311 // Determine range of secondaries produced by this primary during transport
312 Int_t dau1 = part->GetFirstDaughter();
313 if (dau1 < np) continue; // This particle has no secondaries produced during transport
316 Int_t inext = ip - 1;
319 part = fStack->Particle(inext);
320 dau2 = part->GetFirstDaughter();
321 if (dau2 == -1 || dau2 < np) {
327 dau2 = fStack->GetNtrack() - 1;
330 } // find upper bound
334 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
336 // Loop over reference hits and find secondary label
337 // First the tricky part: find the entry in treeTR than contains the hits or
338 // make sure that no hits exist.
340 Bool_t hasHits = kFALSE;
341 Bool_t isOutside = kFALSE;
344 while (!hasHits && !isOutside && it < nt) {
345 fTreeTR->GetEntry(it++);
346 for (Int_t ib = 0; ib < 7; ib++) {
347 if (!trefs[ib]) continue;
348 Int_t nh = trefs[ib]->GetEntries();
349 for (Int_t ih = 0; ih < nh; ih++) {
350 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
351 Int_t label = tr->Label();
352 if (label >= dau1 && label <= dau2) {
357 if (label > dau2 || label < ip) {
363 if (hasHits || isOutside) break;
368 // Write empty entries
369 for (Int_t id = dau1; (id <= dau2); id++) {
375 fTreeTR->GetEntry(itlast);
376 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
377 for (Int_t ib = 0; ib < 7; ib++) {
378 if (!trefs[ib]) continue;
379 Int_t nh = trefs[ib]->GetEntries();
380 for (Int_t ih = 0; ih < nh; ih++) {
381 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
382 Int_t label = tr->Label();
384 if (label == ip) continue;
385 if (label > dau2 || label < dau1)
386 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
387 itlast, label, dau1, dau2);
390 tr->SetDetectorId(ib-1);
391 Int_t nref = fTRBuffer->GetEntriesFast();
392 TClonesArray &lref = *fTRBuffer;
393 new(lref[nref]) AliTrackReference(*tr);
405 // Now loop again and write the primaries
408 for (Int_t ip = 0; ip < np; ip++) {
410 while (labmax < ip && it > -1) {
411 fTreeTR->GetEntry(it--);
412 for (Int_t ib = 0; ib < 7; ib++) {
413 if (!trefs[ib]) continue;
414 Int_t nh = trefs[ib]->GetEntries();
416 // Loop over reference hits and find primary labels
417 for (Int_t ih = 0; ih < nh; ih++) {
418 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
419 Int_t label = tr->Label();
420 if (label < np && label > labmax) {
425 tr->SetDetectorId(ib-1);
426 Int_t nref = fTRBuffer->GetEntriesFast();
427 TClonesArray &lref = *fTRBuffer;
428 new(lref[nref]) AliTrackReference(*tr);
442 delete fTreeTR; fTreeTR = 0;
444 for (Int_t ib = 0; ib < 7; ib++) {
452 if (ifills != fStack->GetNtrack())
453 printf("AliMCEvent:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
454 ifills, fStack->GetNtrack());
457 fTreeTR = fTmpTreeTR;
460 AliVParticle* AliMCEvent::GetTrack(Int_t i) const
466 return ((AliVParticle*) (fMCParticles->At(i)));
470 // Check first if this explicitely accesses the subsidiary event
472 if (i >= BgLabelOffset()) {
473 if (fSubsidiaryEvents) {
474 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
475 return (bgEvent->GetTrack(i - BgLabelOffset()));
482 AliMCParticle *mcParticle = 0;
483 TParticle *particle = 0;
484 TClonesArray *trefs = 0;
486 TRefArray *rarray = 0;
490 // Out of range check
491 if (i < 0 || i >= fNparticles) {
492 AliWarning(Form("AliMCEvent::GetEntry: Index out of range"));
498 if (fSubsidiaryEvents) {
500 Int_t idx = FindIndexAndEvent(i, mc);
501 return (mc->GetTrack(idx));
505 // First check If the MC Particle has been already cached
506 if(!fMCParticleMap->At(i)) {
507 // Get particle from the stack
508 particle = fStack->Particle(i);
509 // Get track references from Tree TR
511 fTreeTR->GetEntry(fStack->TreeKEntry(i));
513 ntref = trefs->GetEntriesFast();
514 rarray = new TRefArray(ntref);
515 Int_t nen = fTrackReferences->GetEntriesFast();
516 for (Int_t j = 0; j < ntref; j++) {
517 // Save the track references in a TClonesArray
518 AliTrackReference* ref = dynamic_cast<AliTrackReference*>((*fTRBuffer)[j]);
519 // Save the pointer in a TRefArray
520 new ((*fTrackReferences)[nen]) AliTrackReference(*ref);
521 rarray->AddAt((*fTrackReferences)[nen], j);
523 } // loop over track references for entry i
524 } // if TreeTR available
525 Int_t nentries = fMCParticles->GetEntriesFast();
526 new ((*fMCParticles)[nentries]) AliMCParticle(particle, rarray, i);
527 mcParticle = dynamic_cast<AliMCParticle*>((*fMCParticles)[nentries]);
528 fMCParticleMap->AddAt(mcParticle, i);
530 TParticle* part = mcParticle->Particle();
531 Int_t imo = part->GetFirstMother();
532 Int_t id1 = part->GetFirstDaughter();
533 Int_t id2 = part->GetLastDaughter();
534 if (fPrimaryOffset > 0 || fSecondaryOffset > 0) {
535 // Remapping of the mother and daughter indices
536 if (imo < fNprimaries) {
537 mcParticle->SetMother(imo + fPrimaryOffset);
539 mcParticle->SetMother(imo + fSecondaryOffset - fNprimaries);
542 if (id1 < fNprimaries) {
543 mcParticle->SetFirstDaughter(id1 + fPrimaryOffset);
544 mcParticle->SetLastDaughter (id2 + fPrimaryOffset);
546 mcParticle->SetFirstDaughter(id1 + fSecondaryOffset - fNprimaries);
547 mcParticle->SetLastDaughter (id2 + fSecondaryOffset - fNprimaries);
551 if (i > fNprimaries) {
552 mcParticle->SetLabel(i + fPrimaryOffset);
554 mcParticle->SetLabel(i + fSecondaryOffset - fNprimaries);
559 mcParticle->SetFirstDaughter(id1);
560 mcParticle->SetLastDaughter (id2);
561 mcParticle->SetMother (imo);
565 mcParticle = dynamic_cast<AliMCParticle*>(fMCParticleMap->At(i));
572 AliGenEventHeader* AliMCEvent::GenEventHeader() const {return (fHeader->GenEventHeader());}
575 void AliMCEvent::AddSubsidiaryEvent(AliMCEvent* event)
577 // Add a subsidiary event to the list; for example merged background event.
578 if (!fSubsidiaryEvents) {
579 TList* events = new TList();
580 events->Add(new AliMCEvent(*this));
581 fSubsidiaryEvents = events;
584 fSubsidiaryEvents->Add(event);
587 Int_t AliMCEvent::FindIndexAndEvent(Int_t oldidx, AliMCEvent*& event) const
589 // Find the index and event in case of composed events like signal + background
590 TIter next(fSubsidiaryEvents);
592 if (oldidx < fNprimaries) {
593 while((event = (AliMCEvent*)next())) {
594 if (oldidx < (event->GetPrimaryOffset() + event->GetNumberOfPrimaries())) break;
596 return (oldidx - event->GetPrimaryOffset());
598 while((event = (AliMCEvent*)next())) {
599 if (oldidx < (event->GetSecondaryOffset() + (event->GetNumberOfTracks() - event->GetNumberOfPrimaries()))) break;
601 return (oldidx - event->GetSecondaryOffset() + event->GetNumberOfPrimaries());
605 Int_t AliMCEvent::BgLabelToIndex(Int_t label)
607 // Convert a background label to an absolute index
608 if (fSubsidiaryEvents) {
609 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
610 label -= BgLabelOffset();
611 if (label < bgEvent->GetNumberOfPrimaries()) {
612 label += bgEvent->GetPrimaryOffset();
614 label += (bgEvent->GetSecondaryOffset() - fNprimaries);
621 Bool_t AliMCEvent::IsPhysicalPrimary(Int_t i)
624 // Delegate to subevent if necesarry
627 if (!fSubsidiaryEvents) {
628 return fStack->IsPhysicalPrimary(i);
631 Int_t idx = FindIndexAndEvent(i, evt);
632 return (evt->IsPhysicalPrimary(idx));
636 void AliMCEvent::InitEvent()
639 // Initialize the subsidiary event structure
640 if (fSubsidiaryEvents) {
641 TIter next(fSubsidiaryEvents);
646 while((evt = (AliMCEvent*)next())) {
647 fNprimaries += evt->GetNumberOfPrimaries();
648 fNparticles += evt->GetNumberOfTracks();
652 Int_t ioffs = fNprimaries;
655 while((evt = (AliMCEvent*)next())) {
656 evt->SetPrimaryOffset(ioffp);
657 evt->SetSecondaryOffset(ioffs);
658 ioffp += evt->GetNumberOfPrimaries();
659 ioffs += (evt->GetNumberOfTracks() - evt->GetNumberOfPrimaries());
663 void AliMCEvent::PreReadAll()
665 // Preread the MC information
668 for (i = fStack->GetNprimary(); i < fStack->GetNtrack(); i++)
673 for (i = 0; i < fStack->GetNprimary(); i++)
682 const AliVVertex * AliMCEvent::GetPrimaryVertex() const
684 // Create a MCVertex object from the MCHeader information
686 GenEventHeader()->PrimaryVertex(v) ;
688 fVertex = new AliMCVertex(v[0], v[1], v[2]);
690 ((AliMCVertex*) fVertex)->SetPosition(v[0], v[1], v[2]);