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"
40 #include <TParticle.h>
42 #include <TClonesArray.h>
43 #include <TDirectoryFile.h>
45 ClassImp(AliMCEventHandler)
47 AliMCEventHandler::AliMCEventHandler() :
49 fMCEvent(new AliMCEvent()),
62 fPathName(new TString("./")),
68 fSubsidiaryHandlers(0),
69 fEventsInContainer(0),
70 fPreReadMode(kNoPreRead)
73 // Default constructor
75 // Be sure to add all particles to the PDG database
76 AliPDG::AddParticlesToPdgDataBase();
79 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
80 AliVEventHandler(name, title),
81 fMCEvent(new AliMCEvent()),
94 fPathName(new TString("./")),
100 fSubsidiaryHandlers(0),
101 fEventsInContainer(0),
102 fPreReadMode(kNoPreRead)
107 // Be sure to add all particles to the PDG database
108 AliPDG::AddParticlesToPdgDataBase();
110 AliMCEventHandler::~AliMCEventHandler()
119 Bool_t AliMCEventHandler::Init(Option_t* opt)
123 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
125 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
127 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
134 fFileE->GetObject("TE", fTreeE);
135 // Connect Tree E to the MCEvent
136 fMCEvent->ConnectTreeE(fTreeE);
137 fNEvent = fTreeE->GetEntries();
140 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
142 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
147 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
151 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
153 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
159 // Reset the event number
162 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
166 if (fSubsidiaryHandlers) {
167 TIter next(fSubsidiaryHandlers);
168 AliMCEventHandler *handler;
169 while((handler = (AliMCEventHandler*)next())) {
171 handler->SetNumberOfEventsInContainer(fNEvent);
178 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
180 // Load the event number iev
182 // Calculate the file number
183 if (!fInitOk) return kFALSE;
185 Int_t inew = iev / fEventsPerFile;
186 if (inew != fFileNumber) {
188 if (!OpenFile(fFileNumber)){
194 sprintf(folder, "Event%d", iev);
196 fTreeE->GetEntry(iev);
198 fFileK->GetObject(folder, fDirK);
200 AliWarning(Form("AliMCEventHandler: Event #%5d - Cannot get kinematics\n", iev));
204 fDirK ->GetObject("TreeK", fTreeK);
206 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeK\n",iev));
209 // Connect TreeK to MCEvent
210 fMCEvent->ConnectTreeK(fTreeK);
213 // Check which format has been read
214 fFileTR->GetObject(folder, fDirTR);
216 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get track references\n",iev));
220 fDirTR->GetObject("TreeTR", fTreeTR);
223 AliError(Form("AliMCEventHandler: Event #%5d - Cannot get TreeTR\n",iev));
226 // Connect TR to MCEvent
227 fMCEvent->ConnectTreeTR(fTreeTR);
234 Bool_t AliMCEventHandler::OpenFile(Int_t i)
238 fExtension = Form("%d", i);
245 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
247 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
254 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
256 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
267 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
269 fParticleSelected.Delete();
271 // Read the next event
273 if (fEventsInContainer != 0) {
274 entry = (Long64_t) ( entry * Float_t(fNEvent) / Float_t (fEventsInContainer));
285 if (entry >= fNEvent) {
286 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry, fNEvent));
290 Bool_t result = GetEvent(entry);
292 if (fSubsidiaryHandlers) {
293 TIter next(fSubsidiaryHandlers);
294 AliMCEventHandler *handler;
295 while((handler = (AliMCEventHandler*)next())) {
296 handler->BeginEvent(entry);
299 while((handler = (AliMCEventHandler*)next())) {
300 fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
302 fMCEvent->InitEvent();
305 if (fPreReadMode == kLmPreRead) {
306 fMCEvent->PreReadAll();
313 void AliMCEventHandler::SelectParticle(Int_t i){
314 // taking the absolute values here, need to take care
315 // of negative daughter and mother
317 if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i = fMCEvent->BgLabelToIndex(TMath::Abs(i));
318 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
321 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
322 // taking the absolute values here, need to take
323 // care with negative daughter and mother
325 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
329 void AliMCEventHandler::CreateLabelMap(){
332 // this should be called once all selections where done
337 fParticleSelected.Delete();
341 VerifySelectedParticles();
344 for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
345 if(IsParticleSelected(i)){
346 fLabelMap.Add(i,iNew);
352 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
353 // Gets the label from the new created Map
354 // Call CreatLabelMap before
355 // otherwise only 0 returned
356 return fLabelMap.GetValue(TMath::Abs(i));
359 void AliMCEventHandler::VerifySelectedParticles(){
362 // Make sure that each particle has at least it's predecessors
363 // selected so that we have the complete ancestry tree
364 // Private, should be only called by CreateLabelMap
367 fParticleSelected.Delete();
371 Int_t nprim = fMCEvent->GetNumberOfPrimaries();
373 for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
375 SelectParticle(i);// take all primaries
379 if(!IsParticleSelected(i))continue;
381 AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(i);
382 Int_t imo = mcpart->GetMother();
383 while((imo >= nprim)&&!IsParticleSelected(imo)){
384 // Mother not yet selected
386 mcpart = (AliMCParticle*) fMCEvent->GetTrack(imo);
387 imo = mcpart->GetMother();
389 // after last step we may have an unselected primary
392 if(!IsParticleSelected(imo))
395 }// loop over all tracks
398 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
404 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
408 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
410 // Retrieve entry i and draw momentum vector and hits
411 fMCEvent->DrawCheck(i, search);
414 Bool_t AliMCEventHandler::Notify(const char *path)
416 // Notify about directory change
417 // The directory is taken from the 'path' argument
419 TString fileName(path);
420 if(fileName.Contains("AliESDs.root")){
421 fileName.ReplaceAll("AliESDs.root", "");
423 else if(fileName.Contains("AliAOD.root")){
424 fileName.ReplaceAll("AliAOD.root", "");
426 else if(fileName.Contains("galice.root")){
427 // for running with galice and kinematics alone...
428 fileName.ReplaceAll("galice.root", "");
430 else if (fileName.BeginsWith("root:")) {
431 fileName.Append("?ZIP=");
434 *fPathName = fileName;
435 AliInfo(Form("Path: -%s-\n", fPathName->Data()));
440 // Handle subsidiary handlers
441 if (fSubsidiaryHandlers) {
442 TIter next(fSubsidiaryHandlers);
443 AliMCEventHandler *handler;
444 while((handler = (AliMCEventHandler*) next())) {
445 TString* spath = handler->GetInputPath();
446 if (spath->Contains("merged")) {
447 if (! fPathName->IsNull()) {
448 handler->Notify(Form("%s/../.", fPathName->Data()));
450 handler->Notify("../");
459 void AliMCEventHandler::ResetIO()
461 // Clear header and stack
463 if (fInitOk) fMCEvent->Clean();
466 delete fTreeE; fTreeE = 0;
469 if (fFileE) {delete fFileE; fFileE = 0;}
470 if (fFileK) {delete fFileK; fFileK = 0;}
471 if (fFileTR) {delete fFileTR; fFileTR = 0;}
475 if (fSubsidiaryHandlers) {
476 TIter next(fSubsidiaryHandlers);
477 AliMCEventHandler *handler;
478 while((handler = (AliMCEventHandler*)next())) {
486 Bool_t AliMCEventHandler::FinishEvent()
488 // Clean-up after each event
489 delete fDirTR; fDirTR = 0;
490 delete fDirK; fDirK = 0;
491 if (fInitOk) fMCEvent->FinishEvent();
493 if (fSubsidiaryHandlers) {
494 TIter next(fSubsidiaryHandlers);
495 AliMCEventHandler *handler;
496 while((handler = (AliMCEventHandler*)next())) {
497 handler->FinishEvent();
504 Bool_t AliMCEventHandler::Terminate()
510 Bool_t AliMCEventHandler::TerminateIO()
517 void AliMCEventHandler::SetInputPath(const char* fname)
519 // Set the input path name
521 fPathName = new TString(fname);
524 void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
526 // Add a subsidiary handler. For example for background events
528 if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
529 fSubsidiaryHandlers->Add(handler);