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;
191 void AliMCEvent::FinishEvent()
193 // Clean-up after event
196 fMCParticles->Delete();
197 fMCParticleMap->Clear();
200 fTrackReferences->Delete();
204 // fSubsidiaryEvents->Clear();
205 fSubsidiaryEvents = 0;
210 void AliMCEvent::DrawCheck(Int_t i, Int_t search)
213 // Simple event display for debugging
215 AliWarning("No Track Reference information available");
219 if (i > -1 && i < fNparticles) {
220 fTreeTR->GetEntry(fStack->TreeKEntry(i));
222 AliWarning("AliMCEvent::GetEntry: Index out of range");
225 Int_t nh = fTRBuffer->GetEntries();
229 while(nh <= search && i < fNparticles - 1) {
231 fTreeTR->GetEntry(fStack->TreeKEntry(i));
232 nh = fTRBuffer->GetEntries();
234 printf("Found Hits at %5d\n", i);
236 TParticle* particle = fStack->Particle(i);
238 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
239 Float_t x0 = particle->Vx();
240 Float_t y0 = particle->Vy();
242 Float_t x1 = particle->Vx() + particle->Px() * 50.;
243 Float_t y1 = particle->Vy() + particle->Py() * 50.;
245 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
251 for (Int_t ih = 0; ih < nh; ih++) {
252 AliTrackReference* ref = (AliTrackReference*) fTRBuffer->At(ih);
253 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
255 m->SetMarkerSize(0.4);
261 void AliMCEvent::ReorderAndExpandTreeTR()
264 // Reorder and expand the track reference tree in order to match the kinematics tree.
265 // Copy the information from different branches into one
269 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
270 fTmpTreeTR = new TTree("TreeTR", "TrackReferences");
271 if (!fTRBuffer) fTRBuffer = new TClonesArray("AliTrackReference", 100);
272 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTRBuffer, 32000, 0);
276 // Activate the used branches only. Otherwisw we get a bad memory leak.
277 fTreeTR->SetBranchStatus("*", 0);
278 fTreeTR->SetBranchStatus("AliRun.*", 1);
279 fTreeTR->SetBranchStatus("ITS.*", 1);
280 fTreeTR->SetBranchStatus("TPC.*", 1);
281 fTreeTR->SetBranchStatus("TRD.*", 1);
282 fTreeTR->SetBranchStatus("TOF.*", 1);
283 fTreeTR->SetBranchStatus("FRAME.*", 1);
284 fTreeTR->SetBranchStatus("MUON.*", 1);
286 // Connect the active branches
287 TClonesArray* trefs[7];
288 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
290 // make branch for central track references
291 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
292 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
293 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
294 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
295 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
296 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
297 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
300 Int_t np = fStack->GetNprimary();
301 Int_t nt = fTreeTR->GetEntries();
304 // Loop over tracks and find the secondaries with the help of the kine tree
310 for (Int_t ip = np - 1; ip > -1; ip--) {
311 part = fStack->Particle(ip);
312 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
313 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
314 // part->GetLastDaughter(), part->TestBit(kTransportBit));
316 // Determine range of secondaries produced by this primary during transport
317 Int_t dau1 = part->GetFirstDaughter();
318 if (dau1 < np) continue; // This particle has no secondaries produced during transport
321 Int_t inext = ip - 1;
324 part = fStack->Particle(inext);
325 dau2 = part->GetFirstDaughter();
326 if (dau2 == -1 || dau2 < np) {
332 dau2 = fStack->GetNtrack() - 1;
335 } // find upper bound
339 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
341 // Loop over reference hits and find secondary label
342 // First the tricky part: find the entry in treeTR than contains the hits or
343 // make sure that no hits exist.
345 Bool_t hasHits = kFALSE;
346 Bool_t isOutside = kFALSE;
349 while (!hasHits && !isOutside && it < nt) {
350 fTreeTR->GetEntry(it++);
351 for (Int_t ib = 0; ib < 7; ib++) {
352 if (!trefs[ib]) continue;
353 Int_t nh = trefs[ib]->GetEntries();
354 for (Int_t ih = 0; ih < nh; ih++) {
355 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
356 Int_t label = tr->Label();
357 if (label >= dau1 && label <= dau2) {
362 if (label > dau2 || label < ip) {
368 if (hasHits || isOutside) break;
373 // Write empty entries
374 for (Int_t id = dau1; (id <= dau2); id++) {
380 fTreeTR->GetEntry(itlast);
381 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
382 for (Int_t ib = 0; ib < 7; ib++) {
383 if (!trefs[ib]) continue;
384 Int_t nh = trefs[ib]->GetEntries();
385 for (Int_t ih = 0; ih < nh; ih++) {
386 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
387 Int_t label = tr->Label();
389 if (label == ip) continue;
390 if (label > dau2 || label < dau1)
391 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
392 itlast, label, dau1, dau2);
395 tr->SetDetectorId(ib-1);
396 Int_t nref = fTRBuffer->GetEntriesFast();
397 TClonesArray &lref = *fTRBuffer;
398 new(lref[nref]) AliTrackReference(*tr);
410 // Now loop again and write the primaries
413 for (Int_t ip = 0; ip < np; ip++) {
415 while (labmax < ip && it > -1) {
416 fTreeTR->GetEntry(it--);
417 for (Int_t ib = 0; ib < 7; ib++) {
418 if (!trefs[ib]) continue;
419 Int_t nh = trefs[ib]->GetEntries();
421 // Loop over reference hits and find primary labels
422 for (Int_t ih = 0; ih < nh; ih++) {
423 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
424 Int_t label = tr->Label();
425 if (label < np && label > labmax) {
430 tr->SetDetectorId(ib-1);
431 Int_t nref = fTRBuffer->GetEntriesFast();
432 TClonesArray &lref = *fTRBuffer;
433 new(lref[nref]) AliTrackReference(*tr);
447 delete fTreeTR; fTreeTR = 0;
449 for (Int_t ib = 0; ib < 7; ib++) {
457 if (ifills != fStack->GetNtrack())
458 printf("AliMCEvent:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
459 ifills, fStack->GetNtrack());
462 fTreeTR = fTmpTreeTR;
465 AliVParticle* AliMCEvent::GetTrack(Int_t i) const
471 return ((AliVParticle*) (fMCParticles->At(i)));
475 // Check first if this explicitely accesses the subsidiary event
477 if (i >= BgLabelOffset()) {
478 if (fSubsidiaryEvents) {
479 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
480 return (bgEvent->GetTrack(i - BgLabelOffset()));
487 AliMCParticle *mcParticle = 0;
488 TParticle *particle = 0;
489 TClonesArray *trefs = 0;
491 TRefArray *rarray = 0;
495 // Out of range check
496 if (i < 0 || i >= fNparticles) {
497 AliWarning(Form("AliMCEvent::GetEntry: Index out of range"));
503 if (fSubsidiaryEvents) {
505 Int_t idx = FindIndexAndEvent(i, mc);
506 return (mc->GetTrack(idx));
510 // First check If the MC Particle has been already cached
511 if(!fMCParticleMap->At(i)) {
512 // Get particle from the stack
513 particle = fStack->Particle(i);
514 // Get track references from Tree TR
516 fTreeTR->GetEntry(fStack->TreeKEntry(i));
518 ntref = trefs->GetEntriesFast();
519 rarray = new TRefArray(ntref);
520 Int_t nen = fTrackReferences->GetEntriesFast();
521 for (Int_t j = 0; j < ntref; j++) {
522 // Save the track references in a TClonesArray
523 AliTrackReference* ref = dynamic_cast<AliTrackReference*>((*fTRBuffer)[j]);
524 // Save the pointer in a TRefArray
525 new ((*fTrackReferences)[nen]) AliTrackReference(*ref);
526 rarray->AddAt((*fTrackReferences)[nen], j);
528 } // loop over track references for entry i
529 } // if TreeTR available
530 Int_t nentries = fMCParticles->GetEntriesFast();
531 new ((*fMCParticles)[nentries]) AliMCParticle(particle, rarray, i);
532 mcParticle = dynamic_cast<AliMCParticle*>((*fMCParticles)[nentries]);
533 fMCParticleMap->AddAt(mcParticle, i);
535 TParticle* part = mcParticle->Particle();
536 Int_t imo = part->GetFirstMother();
537 Int_t id1 = part->GetFirstDaughter();
538 Int_t id2 = part->GetLastDaughter();
539 if (fPrimaryOffset > 0 || fSecondaryOffset > 0) {
540 // Remapping of the mother and daughter indices
541 if (imo < fNprimaries) {
542 mcParticle->SetMother(imo + fPrimaryOffset);
544 mcParticle->SetMother(imo + fSecondaryOffset - fNprimaries);
547 if (id1 < fNprimaries) {
548 mcParticle->SetFirstDaughter(id1 + fPrimaryOffset);
549 mcParticle->SetLastDaughter (id2 + fPrimaryOffset);
551 mcParticle->SetFirstDaughter(id1 + fSecondaryOffset - fNprimaries);
552 mcParticle->SetLastDaughter (id2 + fSecondaryOffset - fNprimaries);
556 if (i > fNprimaries) {
557 mcParticle->SetLabel(i + fPrimaryOffset);
559 mcParticle->SetLabel(i + fSecondaryOffset - fNprimaries);
564 mcParticle->SetFirstDaughter(id1);
565 mcParticle->SetLastDaughter (id2);
566 mcParticle->SetMother (imo);
570 mcParticle = dynamic_cast<AliMCParticle*>(fMCParticleMap->At(i));
577 AliGenEventHeader* AliMCEvent::GenEventHeader() const {return (fHeader->GenEventHeader());}
580 void AliMCEvent::AddSubsidiaryEvent(AliMCEvent* event)
582 // Add a subsidiary event to the list; for example merged background event.
583 if (!fSubsidiaryEvents) {
584 TList* events = new TList();
585 events->Add(new AliMCEvent(*this));
586 fSubsidiaryEvents = events;
589 fSubsidiaryEvents->Add(event);
592 Int_t AliMCEvent::FindIndexAndEvent(Int_t oldidx, AliMCEvent*& event) const
594 // Find the index and event in case of composed events like signal + background
595 TIter next(fSubsidiaryEvents);
597 if (oldidx < fNprimaries) {
598 while((event = (AliMCEvent*)next())) {
599 if (oldidx < (event->GetPrimaryOffset() + event->GetNumberOfPrimaries())) break;
601 return (oldidx - event->GetPrimaryOffset());
603 while((event = (AliMCEvent*)next())) {
604 if (oldidx < (event->GetSecondaryOffset() + (event->GetNumberOfTracks() - event->GetNumberOfPrimaries()))) break;
606 return (oldidx - event->GetSecondaryOffset() + event->GetNumberOfPrimaries());
610 Int_t AliMCEvent::BgLabelToIndex(Int_t label)
612 // Convert a background label to an absolute index
613 if (fSubsidiaryEvents) {
614 AliMCEvent* bgEvent = (AliMCEvent*) (fSubsidiaryEvents->At(1));
615 label -= BgLabelOffset();
616 if (label < bgEvent->GetNumberOfPrimaries()) {
617 label += bgEvent->GetPrimaryOffset();
619 label += (bgEvent->GetSecondaryOffset() - fNprimaries);
626 Bool_t AliMCEvent::IsPhysicalPrimary(Int_t i)
629 // Delegate to subevent if necesarry
632 if (!fSubsidiaryEvents) {
633 return fStack->IsPhysicalPrimary(i);
636 Int_t idx = FindIndexAndEvent(i, evt);
637 return (evt->IsPhysicalPrimary(idx));
641 void AliMCEvent::InitEvent()
644 // Initialize the subsidiary event structure
645 if (fSubsidiaryEvents) {
646 TIter next(fSubsidiaryEvents);
651 while((evt = (AliMCEvent*)next())) {
652 fNprimaries += evt->GetNumberOfPrimaries();
653 fNparticles += evt->GetNumberOfTracks();
657 Int_t ioffs = fNprimaries;
660 while((evt = (AliMCEvent*)next())) {
661 evt->SetPrimaryOffset(ioffp);
662 evt->SetSecondaryOffset(ioffs);
663 ioffp += evt->GetNumberOfPrimaries();
664 ioffs += (evt->GetNumberOfTracks() - evt->GetNumberOfPrimaries());
668 void AliMCEvent::PreReadAll()
670 // Preread the MC information
673 for (i = fStack->GetNprimary(); i < fStack->GetNtrack(); i++)
678 for (i = 0; i < fStack->GetNprimary(); i++)
687 const AliVVertex * AliMCEvent::GetPrimaryVertex() const
689 // Create a MCVertex object from the MCHeader information
692 GenEventHeader()->PrimaryVertex(v) ;
693 fVertex = new AliMCVertex(v[0], v[1], v[2]);