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*/)
107 fFileE = TFile::Open(Form("%sgalice.root", fPathName));
108 if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName));
110 fFileE->GetObject("TE", fTreeE);
111 fTreeE->SetBranchAddress("Header", &fHeader);
112 fNEvent = fTreeE->GetEntries();
115 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName, fExtension));
116 if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
117 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
120 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName, fExtension));
121 if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName));
123 // Reset the event number
127 AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
132 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
134 // Load the event number iev
136 // Calculate the file number
137 Int_t inew = iev/fEventsPerFile;
138 if (inew != fFileNumber) {
140 if (!OpenFile(fFileNumber)){
146 sprintf(folder, "Event%d", iev);
149 fTreeE->GetEntry(iev);
150 fStack = fHeader->Stack();
152 TDirectoryFile* dirK = 0;
153 fFileK->GetObject(folder, dirK);
155 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
158 dirK->GetObject("TreeK", fTreeK);
159 fStack->ConnectTree(fTreeK);
165 TDirectoryFile* dirTR = 0;
166 fFileTR->GetObject(folder, dirTR);
167 dirTR->GetObject("TreeTR", fTreeTR);
168 if (fTreeTR->GetBranch("AliRun")) {
169 // This is an old format with one branch per detector not in synch with TreeK
170 ReorderAndExpandTreeTR();
174 fTreeTR->SetBranchAddress("TrackReferences", &fTrackReferences);
179 fNparticles = fStack->GetNtrack();
180 fNprimaries = fStack->GetNprimary();
181 AliInfo(Form("AliMCEventHandler: Event#: %5d Number of particles: %5d (all) %5d (primaries)\n",
182 fEvent, fNparticles, fNprimaries));
187 Bool_t AliMCEventHandler::OpenFile(Int_t i)
192 fExtension = Form("%d", i);
199 fFileK = new TFile(Form("%sKinematics%s.root", fPathName, fExtension));
201 AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName));
206 fFileTR = new TFile(Form("%sTrackRefs%s.root", fPathName, fExtension));
208 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName));
215 Bool_t AliMCEventHandler::BeginEvent()
217 // Read the next event
219 if (fEvent >= fNEvent) {
220 AliWarning(Form("AliMCEventHandler: Event number out of range %5d\n", fEvent));
223 return GetEvent(fEvent);
226 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
229 if (i < 0 || i >= fNparticles) {
230 AliWarning(Form("AliMCEventHandler::GetEntry: Index out of range"));
235 particle = fStack->Particle(i);
237 fTreeTR->GetEntry(fStack->TreeKEntry(i));
238 trefs = fTrackReferences;
239 return trefs->GetEntries();
246 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
248 // Retrieve entry i and draw momentum vector and hits
250 AliWarning("No Track Reference information available");
254 if (i > -1 && i < fNparticles) {
255 fTreeTR->GetEntry(fStack->TreeKEntry(i));
257 AliWarning("AliMCEventHandler::GetEntry: Index out of range");
260 Int_t nh = fTrackReferences->GetEntries();
264 while(nh <= search && i < fNparticles - 1) {
266 fTreeTR->GetEntry(fStack->TreeKEntry(i));
267 nh = fTrackReferences->GetEntries();
269 printf("Found Hits at %5d\n", i);
271 TParticle* particle = fStack->Particle(i);
273 TH2F* h = new TH2F("", "", 100, -500, 500, 100, -500, 500);
274 Float_t x0 = particle->Vx();
275 Float_t y0 = particle->Vy();
277 Float_t x1 = particle->Vx() + particle->Px() * 50.;
278 Float_t y1 = particle->Vy() + particle->Py() * 50.;
280 TArrow* a = new TArrow(x0, y0, x1, y1, 0.01);
286 for (Int_t ih = 0; ih < nh; ih++) {
287 AliTrackReference* ref = (AliTrackReference*) fTrackReferences->At(ih);
288 TMarker* m = new TMarker(ref->X(), ref->Y(), 20);
290 m->SetMarkerSize(0.4);
295 Bool_t AliMCEventHandler::Notify(const char *path)
297 // Notify about directory change
298 // The directory is taken from the 'path' argument
301 printf("AliMCEventHandler::Notify() file: %s\n", path);
302 fPathName = Form("%s", path);
308 void AliMCEventHandler::ResetIO()
311 if (fFileE) delete fFileE;
312 if (fFileK) delete fFileK;
313 if (fFileTR) delete fFileTR;
317 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
345 fTmpTreeTR->Delete("all");
353 fTmpFileTR = new TFile("TrackRefsTmp.root", "recreate");
354 fTmpTreeTR = new TTree("TreeTR", "Track References");
355 if (!fTrackReferences) fTrackReferences = new TClonesArray("AliTrackReference", 100);
356 fTmpTreeTR->Branch("TrackReferences", "TClonesArray", &fTrackReferences, 4000);
359 fTreeTR->SetBranchStatus("*", 0);
360 fTreeTR->SetBranchStatus("AliRun", 1);
361 fTreeTR->SetBranchStatus("ITS", 1);
362 fTreeTR->SetBranchStatus("TPC", 1);
363 fTreeTR->SetBranchStatus("TRD", 1);
364 fTreeTR->SetBranchStatus("TOF", 1);
365 fTreeTR->SetBranchStatus("FRAME", 1);
366 fTreeTR->SetBranchStatus("MUON", 1);
368 TClonesArray* trefs[7];
369 for (Int_t i = 0; i < 7; i++) trefs[i] = 0;
371 // make branch for central track references
372 if (fTreeTR->GetBranch("AliRun")) fTreeTR->SetBranchAddress("AliRun", &trefs[0]);
373 if (fTreeTR->GetBranch("ITS")) fTreeTR->SetBranchAddress("ITS", &trefs[1]);
374 if (fTreeTR->GetBranch("TPC")) fTreeTR->SetBranchAddress("TPC", &trefs[2]);
375 if (fTreeTR->GetBranch("TRD")) fTreeTR->SetBranchAddress("TRD", &trefs[3]);
376 if (fTreeTR->GetBranch("TOF")) fTreeTR->SetBranchAddress("TOF", &trefs[4]);
377 if (fTreeTR->GetBranch("FRAME")) fTreeTR->SetBranchAddress("FRAME", &trefs[5]);
378 if (fTreeTR->GetBranch("MUON")) fTreeTR->SetBranchAddress("MUON", &trefs[6]);
381 Int_t np = fStack->GetNprimary();
382 Int_t nt = fTreeTR->GetEntries();
384 // Loop over tracks and find the secondaries with the help of the kine tree
389 for (Int_t ip = np - 1; ip > -1; ip--) {
390 TParticle *part = fStack->Particle(ip);
391 // printf("Particle %5d %5d %5d %5d %5d %5d \n",
392 // ip, part->GetPdgCode(), part->GetFirstMother(), part->GetFirstDaughter(),
393 // part->GetLastDaughter(), part->TestBit(kTransportBit));
395 // Determine range of secondaries produced by this primary during transport
396 Int_t dau1 = part->GetFirstDaughter();
397 if (dau1 < np) continue; // This particle has no secondaries produced during transport
400 Int_t inext = ip - 1;
403 part = fStack->Particle(inext);
404 dau2 = part->GetFirstDaughter();
405 if (dau2 == -1 || dau2 < np) {
411 dau2 = fStack->GetNtrack() - 1;
414 } // find upper bound
418 // printf("Check (1) %5d %5d %5d %5d %5d \n", ip, np, it, dau1, dau2);
420 // Loop over reference hits and find secondary label
421 // First the tricky part: find the entry in treeTR than contains the hits or
422 // make sure that no hits exist.
424 Bool_t hasHits = kFALSE;
425 Bool_t isOutside = kFALSE;
428 while (!hasHits && !isOutside && it < nt) {
429 fTreeTR->GetEntry(it++);
430 for (Int_t ib = 0; ib < 7; ib++) {
431 if (!trefs[ib]) continue;
432 Int_t nh = trefs[ib]->GetEntries();
433 for (Int_t ih = 0; ih < nh; ih++) {
434 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
435 Int_t label = tr->Label();
436 if (label >= dau1 && label <= dau2) {
441 if (label > dau2 || label < ip) {
447 if (hasHits || isOutside) break;
452 // Write empty entries
453 for (Int_t id = dau1; (id <= dau2); id++) {
459 fTreeTR->GetEntry(itlast);
460 for (Int_t id = dau1; (id <= dau2) && (dau1 > -1); id++) {
461 for (Int_t ib = 0; ib < 7; ib++) {
462 if (!trefs[ib]) continue;
463 Int_t nh = trefs[ib]->GetEntries();
464 for (Int_t ih = 0; ih < nh; ih++) {
465 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
466 Int_t label = tr->Label();
468 if (label == ip) continue;
469 if (label > dau2 || label < dau1)
470 printf("AliMCEventHandler::Track Reference Label out of range !: %5d %5d %5d %5d \n",
471 itlast, label, dau1, dau2);
474 tr->SetDetectorId(ib-1);
475 Int_t nref = fTrackReferences->GetEntriesFast();
476 TClonesArray &lref = *fTrackReferences;
477 new(lref[nref]) AliTrackReference(*tr);
482 fTrackReferences->Clear();
488 // Now loop again and write the primaries
491 for (Int_t ip = 0; ip < np; ip++) {
493 while (labmax < ip && it > -1) {
494 fTreeTR->GetEntry(it--);
495 for (Int_t ib = 0; ib < 7; ib++) {
496 if (!trefs[ib]) continue;
497 Int_t nh = trefs[ib]->GetEntries();
499 // Loop over reference hits and find primary labels
500 for (Int_t ih = 0; ih < nh; ih++) {
501 AliTrackReference* tr = (AliTrackReference*) trefs[ib]->At(ih);
502 Int_t label = tr->Label();
503 if (label < np && label > labmax) {
508 tr->SetDetectorId(ib-1);
509 Int_t nref = fTrackReferences->GetEntriesFast();
510 TClonesArray &lref = *fTrackReferences;
511 new(lref[nref]) AliTrackReference(*tr);
518 fTrackReferences->Clear();
523 for (Int_t ib = 0; ib < 7; ib++) {
524 if (trefs[ib]) delete trefs[ib];
527 if (ifills != fStack->GetNtrack())
528 printf("AliMCEventHandler:Number of entries in TreeTR (%5d) unequal to TreeK (%5d) \n",
529 ifills, fStack->GetNtrack());
530 fTreeTR = fTmpTreeTR;