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 "AliMCEvent.h"
30 #include "AliMCParticle.h"
32 #include "AliTrackReference.h"
33 #include "AliHeader.h"
39 #include <TTreeCache.h>
42 #include <TParticle.h>
44 #include <TClonesArray.h>
45 #include <TDirectoryFile.h>
47 ClassImp(AliMCEventHandler)
49 AliMCEventHandler::AliMCEventHandler() :
50 AliInputEventHandler(),
51 fMCEvent(new AliMCEvent()),
64 fPathName(new TString("./")),
70 fSubsidiaryHandlers(0),
71 fEventsInContainer(0),
72 fPreReadMode(kNoPreRead),
78 // Default constructor
80 // Be sure to add all particles to the PDG database
81 AliPDG::AddParticlesToPdgDataBase();
84 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
85 AliInputEventHandler(name, title),
86 fMCEvent(new AliMCEvent()),
99 fPathName(new TString("./")),
105 fSubsidiaryHandlers(0),
106 fEventsInContainer(0),
107 fPreReadMode(kNoPreRead),
115 // Be sure to add all particles to the PDG database
116 AliPDG::AddParticlesToPdgDataBase();
118 AliMCEventHandler::~AliMCEventHandler()
130 Bool_t AliMCEventHandler::Init(Option_t* opt)
134 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
136 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
138 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
145 fFileE->GetObject("TE", fTreeE);
146 // Connect Tree E to the MCEvent
147 fMCEvent->ConnectTreeE(fTreeE);
148 fNEvent = fTreeE->GetEntries();
151 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fkExtension));
153 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName->Data()));
158 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
162 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fkExtension));
164 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
170 // Reset the event number
173 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
177 if (fSubsidiaryHandlers) {
178 TIter next(fSubsidiaryHandlers);
179 AliMCEventHandler *handler;
180 while((handler = (AliMCEventHandler*)next())) {
182 handler->SetNumberOfEventsInContainer(fNEvent);
189 Bool_t AliMCEventHandler::LoadEvent(Int_t iev)
191 // Load the event number iev
193 // Calculate the file number
194 if (!fInitOk) return kFALSE;
196 Int_t inew = iev / fEventsPerFile;
197 Bool_t firsttree = (fTreeK==0) ? kTRUE : kFALSE;
198 // Bool_t newtree = firsttree;
199 if (inew != fFileNumber) {
202 if (!OpenFile(fFileNumber)){
208 snprintf(folder, 20, "Event%d", iev);
210 fTreeE->GetEntry(iev);
212 fFileK->GetObject(folder, fDirK);
214 AliWarning(Form("AliMCEventHandler: Event #%5d - Cannot get kinematics\n", iev));
218 fDirK ->GetObject("TreeK", fTreeK);
220 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeK\n",iev));
223 // Connect TreeK to MCEvent
224 fMCEvent->ConnectTreeK(fTreeK);
227 // Check which format has been read
228 fFileTR->GetObject(folder, fDirTR);
230 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get track references\n",iev));
234 fDirTR->GetObject("TreeTR", fTreeTR);
237 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeTR\n",iev));
240 // Connect TR to MCEvent
241 fMCEvent->ConnectTreeTR(fTreeTR);
243 // Now setup the caches if not yet done
245 fTreeK->SetCacheSize(fCacheSize);
246 fCacheTK = (TTreeCache*) fFileK->GetCacheRead(fTreeK);
247 TTreeCache::SetLearnEntries(1);
248 fTreeK->AddBranchToCache("*",kTRUE);
249 if (firsttree) Info("LoadEvent","Read cache enabled %lld bytes for TreeK",fCacheSize);
251 fTreeTR->SetCacheSize(fCacheSize);
252 fCacheTR = (TTreeCache*) fFileTR->GetCacheRead(fTreeTR);
253 TTreeCache::SetLearnEntries(1);
254 fTreeTR->AddBranchToCache("*",kTRUE);
255 if (firsttree) Info("LoadEvent","Read cache enabled %lld bytes for TreeTR",fCacheSize);
258 // We need to reuse the previous caches and every new event is a new tree
260 // fCacheTK->ResetCache();
261 // if (fFileK) fFileK->SetCacheRead(fCacheTK, fTreeK);
262 // fCacheTK->UpdateBranches(fTreeK);
265 // fCacheTR->ResetCache();
266 // if (fFileTR) fFileTR->SetCacheRead(fCacheTR, fTreeTR);
267 // fCacheTR->UpdateBranches(fTreeTR);
273 Bool_t AliMCEventHandler::OpenFile(Int_t i)
277 fkExtension = Form("%d", i);
282 if (fFileK && fCacheTK) fFileK->SetCacheRead(0, fTreeK);
284 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fkExtension));
286 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fkExtension, fPathName->Data()));
292 if (fFileTR && fCacheTR) fFileTR->SetCacheRead(0, fTreeTR);
294 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fkExtension));
296 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fkExtension, fPathName->Data()));
307 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
310 fParticleSelected.Delete();
312 // Read the next event
314 if (fEventsInContainer != 0) {
315 entry = (Long64_t) ( entry * Float_t(fNEvent) / Float_t (fEventsInContainer));
326 if (entry >= fNEvent) {
327 AliWarning(Form("AliMCEventHandler: Event number out of range %5lld %5d\n", entry, fNEvent));
331 Bool_t result = LoadEvent(entry);
333 if (fSubsidiaryHandlers) {
334 TIter next(fSubsidiaryHandlers);
335 AliMCEventHandler *handler;
336 while((handler = (AliMCEventHandler*)next())) {
337 handler->BeginEvent(entry);
340 while((handler = (AliMCEventHandler*)next())) {
341 fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
343 fMCEvent->InitEvent();
346 if (fPreReadMode == kLmPreRead) {
347 fMCEvent->PreReadAll();
354 void AliMCEventHandler::SelectParticle(Int_t i){
355 // taking the absolute values here, need to take care
356 // of negative daughter and mother
358 if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i = fMCEvent->BgLabelToIndex(TMath::Abs(i));
359 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
362 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
363 // taking the absolute values here, need to take
364 // care with negative daughter and mother
366 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
370 void AliMCEventHandler::CreateLabelMap(){
373 // this should be called once all selections where done
378 fParticleSelected.Delete();
382 VerifySelectedParticles();
385 for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
386 if(IsParticleSelected(i)){
387 fLabelMap.Add(i,iNew);
393 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
394 // Gets the label from the new created Map
395 // Call CreatLabelMap before
396 // otherwise only 0 returned
397 return fLabelMap.GetValue(TMath::Abs(i));
400 void AliMCEventHandler::VerifySelectedParticles(){
403 // Make sure that each particle has at least it's predecessors
404 // selected so that we have the complete ancestry tree
405 // Private, should be only called by CreateLabelMap
408 fParticleSelected.Delete();
412 Int_t nprim = fMCEvent->GetNumberOfPrimaries();
414 for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
416 SelectParticle(i);// take all primaries
420 if(!IsParticleSelected(i))continue;
422 AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(i);
423 Int_t imo = mcpart->GetMother();
424 while((imo >= nprim)&&!IsParticleSelected(imo)){
425 // Mother not yet selected
427 mcpart = (AliMCParticle*) fMCEvent->GetTrack(imo);
428 imo = mcpart->GetMother();
430 // after last step we may have an unselected primary
433 if(!IsParticleSelected(imo))
436 }// loop over all tracks
439 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
445 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
449 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
451 // Retrieve entry i and draw momentum vector and hits
452 fMCEvent->DrawCheck(i, search);
455 Bool_t AliMCEventHandler::Notify(const char *path)
457 // Notify about directory change
458 // The directory is taken from the 'path' argument
460 TString fileName(path);
461 TString dirname = gSystem->DirName(fileName);
462 TString basename = gSystem->BaseName(fileName);
463 Int_t index = basename.Index("#");
464 basename = basename(0, index+1);
467 fileName += basename;
468 if (fileName.BeginsWith("root:")) {
469 fileName.Append("?ZIP=");
472 *fPathName = fileName;
473 AliInfo(Form("Path: -%s-\n", fPathName->Data()));
478 // Handle subsidiary handlers
479 if (fSubsidiaryHandlers) {
480 TIter next(fSubsidiaryHandlers);
481 AliMCEventHandler *handler;
482 while((handler = (AliMCEventHandler*) next())) {
483 TString* spath = handler->GetInputPath();
484 if (spath->Contains("merged")) {
485 if (! fPathName->IsNull()) {
486 handler->Notify(Form("%s/../.", fPathName->Data()));
488 handler->Notify("../");
497 void AliMCEventHandler::ResetIO()
499 // Clear header and stack
501 if (fInitOk) fMCEvent->Clean();
504 delete fTreeE; fTreeE = 0;
507 if (fFileE) {delete fFileE; fFileE = 0;}
508 if (fFileK) {delete fFileK; fFileK = 0;}
509 if (fFileTR) {delete fFileTR; fFileTR = 0;}
513 if (fSubsidiaryHandlers) {
514 TIter next(fSubsidiaryHandlers);
515 AliMCEventHandler *handler;
516 while((handler = (AliMCEventHandler*)next())) {
524 Bool_t AliMCEventHandler::FinishEvent()
526 // Clean-up after each event
527 if (fFileK && fCacheTK) {
528 fTreeK->SetCacheSize(0);
530 fFileK->SetCacheRead(0, fTreeK);
532 if (fFileTR && fCacheTR) {
533 fTreeTR->SetCacheSize(0);
535 fFileTR->SetCacheRead(0, fTreeTR);
537 delete fDirTR; fDirTR = 0;
538 delete fDirK; fDirK = 0;
539 if (fInitOk) fMCEvent->FinishEvent();
541 if (fSubsidiaryHandlers) {
542 TIter next(fSubsidiaryHandlers);
543 AliMCEventHandler *handler;
544 while((handler = (AliMCEventHandler*)next())) {
545 handler->FinishEvent();
552 Bool_t AliMCEventHandler::Terminate()
558 Bool_t AliMCEventHandler::TerminateIO()
565 void AliMCEventHandler::SetInputPath(const char* fname)
567 // Set the input path name
569 fPathName = new TString(fname);
572 void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
574 // Add a subsidiary handler. For example for background events
576 if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
577 fSubsidiaryHandlers->Add(handler);