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 not found\n", iev));
204 fDirK ->GetObject("TreeK", fTreeK);
205 // Connect TreeK to MCEvent
206 fMCEvent->ConnectTreeK(fTreeK);
209 // Check which format has been read
210 fFileTR->GetObject(folder, fDirTR);
211 fDirTR->GetObject("TreeTR", fTreeTR);
213 // Connect TR to MCEvent
214 fMCEvent->ConnectTreeTR(fTreeTR);
221 Bool_t AliMCEventHandler::OpenFile(Int_t i)
225 fExtension = Form("%d", i);
232 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
234 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
241 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
243 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
254 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
256 fParticleSelected.Delete();
258 // Read the next event
260 if (fEventsInContainer != 0) {
261 entry = (Long64_t) ( entry * Float_t(fNEvent) / Float_t (fEventsInContainer));
272 if (entry >= fNEvent) {
273 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry, fNEvent));
277 Bool_t result = GetEvent(entry);
279 if (fSubsidiaryHandlers) {
280 TIter next(fSubsidiaryHandlers);
281 AliMCEventHandler *handler;
282 while((handler = (AliMCEventHandler*)next())) {
283 handler->BeginEvent(entry);
286 while((handler = (AliMCEventHandler*)next())) {
287 fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
289 fMCEvent->InitEvent();
292 if (fPreReadMode == kLmPreRead) {
293 fMCEvent->PreReadAll();
300 void AliMCEventHandler::SelectParticle(Int_t i){
301 // taking the absolute values here, need to take care
302 // of negative daughter and mother
304 if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i = fMCEvent->BgLabelToIndex(TMath::Abs(i));
305 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
308 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
309 // taking the absolute values here, need to take
310 // care with negative daughter and mother
312 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
316 void AliMCEventHandler::CreateLabelMap(){
319 // this should be called once all selections where done
324 fParticleSelected.Delete();
328 VerifySelectedParticles();
331 for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
332 if(IsParticleSelected(i)){
333 fLabelMap.Add(i,iNew);
339 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
340 // Gets the label from the new created Map
341 // Call CreatLabelMap before
342 // otherwise only 0 returned
343 return fLabelMap.GetValue(TMath::Abs(i));
346 void AliMCEventHandler::VerifySelectedParticles(){
349 // Make sure that each particle has at least it's predecessors
350 // selected so that we have the complete ancestry tree
351 // Private, should be only called by CreateLabelMap
354 fParticleSelected.Delete();
358 Int_t nprim = fMCEvent->GetNumberOfPrimaries();
360 for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
362 SelectParticle(i);// take all primaries
366 if(!IsParticleSelected(i))continue;
368 AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(i);
369 Int_t imo = mcpart->GetMother();
370 while((imo >= nprim)&&!IsParticleSelected(imo)){
371 // Mother not yet selected
373 mcpart = (AliMCParticle*) fMCEvent->GetTrack(imo);
374 imo = mcpart->GetMother();
376 // after last step we may have an unselected primary
379 if(!IsParticleSelected(imo))
382 }// loop over all tracks
385 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
391 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
395 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
397 // Retrieve entry i and draw momentum vector and hits
398 fMCEvent->DrawCheck(i, search);
401 Bool_t AliMCEventHandler::Notify(const char *path)
403 // Notify about directory change
404 // The directory is taken from the 'path' argument
406 TString fileName(path);
407 if(fileName.Contains("AliESDs.root")){
408 fileName.ReplaceAll("AliESDs.root", "");
410 else if(fileName.Contains("AliAOD.root")){
411 fileName.ReplaceAll("AliAOD.root", "");
413 else if(fileName.Contains("galice.root")){
414 // for running with galice and kinematics alone...
415 fileName.ReplaceAll("galice.root", "");
417 else if (fileName.BeginsWith("root:")) {
418 fileName.Append("?ZIP=");
421 *fPathName = fileName;
422 AliInfo(Form("Path: -%s-\n", fPathName->Data()));
427 // Handle subsidiary handlers
428 if (fSubsidiaryHandlers) {
429 TIter next(fSubsidiaryHandlers);
430 AliMCEventHandler *handler;
431 while((handler = (AliMCEventHandler*) next())) {
432 TString* spath = handler->GetInputPath();
433 if (spath->Contains("merged")) {
434 if (! fPathName->IsNull()) {
435 handler->Notify(Form("%s/../.", fPathName->Data()));
437 handler->Notify("../");
446 void AliMCEventHandler::ResetIO()
448 // Clear header and stack
450 if (fInitOk) fMCEvent->Clean();
453 delete fTreeE; fTreeE = 0;
456 if (fFileE) {delete fFileE; fFileE = 0;}
457 if (fFileK) {delete fFileK; fFileK = 0;}
458 if (fFileTR) {delete fFileTR; fFileTR = 0;}
462 if (fSubsidiaryHandlers) {
463 TIter next(fSubsidiaryHandlers);
464 AliMCEventHandler *handler;
465 while((handler = (AliMCEventHandler*)next())) {
473 Bool_t AliMCEventHandler::FinishEvent()
475 // Clean-up after each event
476 delete fDirTR; fDirTR = 0;
477 delete fDirK; fDirK = 0;
478 if (fInitOk) fMCEvent->FinishEvent();
480 if (fSubsidiaryHandlers) {
481 TIter next(fSubsidiaryHandlers);
482 AliMCEventHandler *handler;
483 while((handler = (AliMCEventHandler*)next())) {
484 handler->FinishEvent();
491 Bool_t AliMCEventHandler::Terminate()
497 Bool_t AliMCEventHandler::TerminateIO()
504 void AliMCEventHandler::SetInputPath(const char* fname)
506 // Set the input path name
508 fPathName = new TString(fname);
511 void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
513 // Add a subsidiary handler. For example for background events
515 if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
516 fSubsidiaryHandlers->Add(handler);