1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
17 //---------------------------------------------------------------------------------
18 // Class AliMCEventHandler
19 // This class gives access to MC truth during the analysis.
20 // Monte Carlo truth is containe in the kinematics tree (produced particles) and
21 // the tree of reference hits.
23 // Origin: Andreas Morsch, CERN, andreas.morsch@cern.ch
24 //---------------------------------------------------------------------------------
28 #include "AliMCEventHandler.h"
29 #include "AliTrackReference.h"
30 #include "AliHeader.h"
36 #include <TParticle.h>
37 #include <TClonesArray.h>
38 #include <TDirectoryFile.h>
44 ClassImp(AliMCEventHandler)
46 AliMCEventHandler::AliMCEventHandler() :
68 // Default constructor
71 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
72 AliVEventHandler(name, title),
82 fHeader(new AliHeader()),
83 fTrackReferences(new TClonesArray("AliTrackReference", 200)),
95 AliMCEventHandler::~AliMCEventHandler()
103 Bool_t AliMCEventHandler::InitIO(Option_t* /*opt*/)
109 name = Form("%sgalice.root", fPathName);
110 printf("Here (1) %s \n", name);
111 fFileE = new TFile(name);
112 if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName));
114 fFileE->GetObject("TE", fTreeE);
115 fTreeE->SetBranchAddress("Header", &fHeader);
116 fNEvent = fTreeE->GetEntries();
119 printf("Here (2) \n");
120 fFileK = new TFile(Form("%sKinematics%s.root", fPathName, fExtension));
121 if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
122 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
125 printf("Here (3) \n");
126 fFileTR = new TFile(Form("%sTrackRefs%s.root", fPathName, fExtension));
127 if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName));
129 // Reset the event number
133 AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
138 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
140 // Load the event number iev
142 // Calculate the file number
143 Int_t inew = iev/fEventsPerFile;
144 if (inew != fFileNumber) {
146 if (!OpenFile(fFileNumber)){
152 sprintf(folder, "Event%d", iev);
155 fTreeE->GetEntry(iev);
156 fStack = fHeader->Stack();
158 TDirectoryFile* dirK = 0;
159 fFileK->GetObject(folder, dirK);
161 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
164 dirK->GetObject("TreeK", fTreeK);
165 fStack->ConnectTree(fTreeK);
169 TDirectoryFile* dirTR = 0;
170 fFileTR->GetObject(folder, dirTR);
171 dirTR->GetObject("TreeTR", fTreeTR);
172 if (fTreeTR->GetBranch("AliRun")) {
173 // This is an old format with one branch per detector not in synch with TreeK
174 ReorderAndExpandTreeTR();
177 fTreeTR->SetBranchAddress("TrackReferences", &fTrackReferences);
182 fNparticles = fStack->GetNtrack();
183 fNprimaries = fStack->GetNprimary();
184 AliInfo(Form("AliMCEventHandler: Event#: %5d Number of particles %5d \n", fEvent, fNparticles));
189 Bool_t AliMCEventHandler::OpenFile(Int_t i)
194 fExtension = Form("%d", i);
201 fFileK = new TFile(Form("%sKinematics%s.root", fPathName, fExtension));
203 AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName));
208 fFileTR = new TFile(Form("%sTrackRefs%s.root", fPathName, fExtension));
210 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName));
217 Bool_t AliMCEventHandler::BeginEvent()
219 // Read the next event
221 if (fEvent >= fNEvent) {
222 AliWarning(Form("AliMCEventHandler: Event number out of range %5d\n", fEvent));
225 return GetEvent(fEvent);
228 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
231 if (i > -1 && i < fNparticles) {
232 AliWarning(Form("AliMCEventHandler::GetEntry: Index out of range"));
237 particle = fStack->Particle(i);
239 fTreeTR->GetEntry(fStack->TreeKEntry(i));
240 trefs = fTrackReferences;
241 return trefs->GetEntries();
248 void AliMCEventHandler::DrawCheck(Int_t i, Bool_t search)
250 // Retrieve entry i and draw momentum vector and hits
252 AliWarning("No Track Reference information available");
256 if (i > -1 && i < fNparticles) {
257 fTreeTR->GetEntry(fStack->TreeKEntry(i));
259 AliWarning("AliMCEventHandler::GetEntry: Index out of range");
262 Int_t nh = fTrackReferences->GetEntries();
266 while(nh == 0 && i < fNparticles - 1) {
268 fTreeTR->GetEntry(fStack->TreeKEntry(i));
269 nh = fTrackReferences->GetEntries();
271 printf("Found Hits at %5d\n", i);
273 TParticle* particle = fStack->Particle(i);
275 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
276 Float_t x0 = particle->Vx();
277 Float_t y0 = particle->Vy();
279 Float_t x1 = particle->Vx() + particle->Px() * 50.;
280 Float_t y1 = particle->Vy() + particle->Py() * 50.;
282 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
288 for (Int_t ih = 0; ih < nh; ih++) {
289 AliTrackReference* ref = (AliTrackReference*) fTrackReferences->At(ih);
290 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
292 m->SetMarkerSize(0.4);
297 Bool_t AliMCEventHandler::Notify(const char *path)
299 // Notify about directory change
300 // The directory is taken from the 'path' argument
303 printf("AliMCEventHandler::Notify() file: %s\n", path);
304 fPathName = Form("%s", path);
310 void AliMCEventHandler::ResetIO()
313 if (fFileE) delete fFileE;
314 if (fFileK) delete fFileK;
315 if (fFileTR) delete fFileTR;
319 Bool_t AliMCEventHandler::FinishEvent()
325 Bool_t AliMCEventHandler::Terminate()
331 Bool_t AliMCEventHandler::TerminateIO()
337 void AliMCEventHandler::ReorderAndExpandTreeTR()
340 // Reorder and expand the track reference tree in order to match the kinematics tree.
341 // Copy the information from different branches into one
344 if (fTmpTreeTR) delete fTmpTreeTR;
350 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
351 fTmpTreeTR = new TTree("TreeTR", "Track References");
352 if (!fTrackReferences) fTrackReferences = new TClonesArray("AliTrackReference", 100);
353 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTrackReferences, 4000);
355 TClonesArray* trefs[7];
356 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
358 // make branch for central track references
359 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
360 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
361 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
362 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
363 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
364 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
365 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
368 Int_t np = fStack->GetNprimary();
369 Int_t nt = fTreeTR->GetEntries();
371 // Loop over tracks and find the secondaries with the help of the kine tree
376 for (Int_t ip = np - 1; ip > -1; ip--) {
377 TParticle *part = fStack->Particle(ip);
378 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
379 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
380 // part->GetLastDaughter(), part->TestBit(kTransportBit));
382 // Determine range of secondaries produced by this primary during transport
383 Int_t dau1 = part->GetFirstDaughter();
384 if (dau1 < np) continue; // This particle has no secondaries produced during transport
387 Int_t inext = ip - 1;
390 part = fStack->Particle(inext);
391 dau2 = part->GetFirstDaughter();
392 if (dau2 == -1 || dau2 < np) {
398 dau2 = fStack->GetNtrack() - 1;
401 } // find upper bound
405 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
407 // Loop over reference hits and find secondary label
408 // First the tricky part: find the entry in treeTR than contains the hits or
409 // make sure that no hits exist.
411 Bool_t hasHits = kFALSE;
412 Bool_t isOutside = kFALSE;
415 while (!hasHits && !isOutside && it < nt) {
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();
420 for (Int_t ih = 0; ih < nh; ih++) {
421 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
422 Int_t label = tr->Label();
423 if (label >= dau1 && label <= dau2) {
428 if (label > dau2 || label < ip) {
434 if (hasHits || isOutside) break;
439 // Write empty entries
440 for (Int_t id = dau1; (id <= dau2); id++) {
446 fTreeTR->GetEntry(itlast);
447 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
448 for (Int_t ib = 0; ib < 7; ib++) {
449 if (!trefs[ib]) continue;
450 Int_t nh = trefs[ib]->GetEntries();
451 for (Int_t ih = 0; ih < nh; ih++) {
452 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
453 Int_t label = tr->Label();
455 if (label == ip) continue;
456 if (label > dau2 || label < dau1)
457 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
458 itlast, label, dau1, dau2);
461 tr->SetDetectorId(ib-1);
462 Int_t nref = fTrackReferences->GetEntriesFast();
463 TClonesArray &lref = *fTrackReferences;
464 new(lref[nref]) AliTrackReference(*tr);
469 fTrackReferences->Clear();
475 // Now loop again and write the primaries
478 for (Int_t ip = 0; ip < np; ip++) {
480 while (labmax < ip && it > -1) {
481 fTreeTR->GetEntry(it--);
482 for (Int_t ib = 0; ib < 7; ib++) {
483 if (!trefs[ib]) continue;
484 Int_t nh = trefs[ib]->GetEntries();
486 // Loop over reference hits and find primary labels
487 for (Int_t ih = 0; ih < nh; ih++) {
488 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
489 Int_t label = tr->Label();
490 if (label < np && label > labmax) {
495 tr->SetDetectorId(ib-1);
496 Int_t nref = fTrackReferences->GetEntriesFast();
497 TClonesArray &lref = *fTrackReferences;
498 new(lref[nref]) AliTrackReference(*tr);
505 fTrackReferences->Clear();
509 if (ifills != fStack->GetNtrack())
510 printf("AliMCEventHandler:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
511 ifills, fStack->GetNtrack());
512 fTreeTR = fTmpTreeTR;