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),
72 // Default constructor
74 // Be sure to add all particles to the PDG database
75 AliPDG::AddParticlesToPdgDataBase();
78 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
79 AliVEventHandler(name, title),
80 fMCEvent(new AliMCEvent()),
93 fPathName(new TString("./")),
99 fSubsidiaryHandlers(0),
100 fEventsInContainer(0)
105 // Be sure to add all particles to the PDG database
106 AliPDG::AddParticlesToPdgDataBase();
108 AliMCEventHandler::~AliMCEventHandler()
117 Bool_t AliMCEventHandler::Init(Option_t* opt)
121 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
123 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
125 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
132 fFileE->GetObject("TE", fTreeE);
133 // Connect Tree E to the MCEvent
134 fMCEvent->ConnectTreeE(fTreeE);
135 fNEvent = fTreeE->GetEntries();
138 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
140 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
145 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
149 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
151 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
157 // Reset the event number
160 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
164 if (fSubsidiaryHandlers) {
165 TIter next(fSubsidiaryHandlers);
166 AliMCEventHandler *handler;
167 while((handler = (AliMCEventHandler*)next())) {
169 handler->SetNumberOfEventsInContainer(fNEvent);
176 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
178 // Load the event number iev
180 // Calculate the file number
181 if (!fInitOk) return kFALSE;
183 Int_t inew = iev / fEventsPerFile;
184 if (inew != fFileNumber) {
186 if (!OpenFile(fFileNumber)){
192 sprintf(folder, "Event%d", iev);
194 fTreeE->GetEntry(iev);
196 fFileK->GetObject(folder, fDirK);
198 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
202 fDirK ->GetObject("TreeK", fTreeK);
203 // Connect TreeK to MCEvent
204 fMCEvent->ConnectTreeK(fTreeK);
207 // Check which format has been read
208 fFileTR->GetObject(folder, fDirTR);
209 fDirTR->GetObject("TreeTR", fTreeTR);
211 // Connect TR to MCEvent
212 fMCEvent->ConnectTreeTR(fTreeTR);
219 Bool_t AliMCEventHandler::OpenFile(Int_t i)
223 fExtension = Form("%d", i);
230 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
232 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
239 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
241 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
252 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
254 fParticleSelected.Delete();
256 // Read the next event
258 if (fEventsInContainer != 0) {
259 entry = (Long64_t) entry * Float_t(fNEvent) / Float_t (fEventsInContainer);
270 if (entry >= fNEvent) {
271 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry, fNEvent));
275 Bool_t result = GetEvent(entry);
277 if (fSubsidiaryHandlers) {
278 TIter next(fSubsidiaryHandlers);
279 AliMCEventHandler *handler;
280 while((handler = (AliMCEventHandler*)next())) {
281 handler->BeginEvent(entry);
284 while((handler = (AliMCEventHandler*)next())) {
285 fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
287 fMCEvent->InitEvent();
293 void AliMCEventHandler::SelectParticle(Int_t i){
294 // taking the absolute values here, need to take care
295 // of negative daughter and mother
297 if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i = fMCEvent->BgLabelToIndex(TMath::Abs(i));
298 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
301 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
302 // taking the absolute values here, need to take
303 // care with negative daughter and mother
305 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
309 void AliMCEventHandler::CreateLabelMap(){
312 // this should be called once all selections where done
317 fParticleSelected.Delete();
321 VerifySelectedParticles();
324 for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
325 if(IsParticleSelected(i)){
326 fLabelMap.Add(i,iNew);
332 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
333 // Gets the label from the new created Map
334 // Call CreatLabelMap before
335 // otherwise only 0 returned
336 return fLabelMap.GetValue(TMath::Abs(i));
339 void AliMCEventHandler::VerifySelectedParticles(){
342 // Make sure that each particle has at least it's predecessors
343 // selected so that we have the complete ancestry tree
344 // Private, should be only called by CreateLabelMap
347 fParticleSelected.Delete();
351 Int_t nprim = fMCEvent->GetNumberOfPrimaries();
353 for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
355 SelectParticle(i);// take all primaries
359 if(!IsParticleSelected(i))continue;
361 AliMCParticle* mcpart = fMCEvent->GetTrack(i);
362 Int_t imo = mcpart->GetMother();
363 while((imo >= nprim)&&!IsParticleSelected(imo)){
364 // Mother not yet selected
366 AliMCParticle* mcpart = fMCEvent->GetTrack(imo);
367 imo = mcpart->GetMother();
369 // after last step we may have an unselected primary
372 if(!IsParticleSelected(imo))
375 }// loop over all tracks
378 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
384 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
388 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
390 // Retrieve entry i and draw momentum vector and hits
391 fMCEvent->DrawCheck(i, search);
394 Bool_t AliMCEventHandler::Notify(const char *path)
396 // Notify about directory change
397 // The directory is taken from the 'path' argument
399 TString fileName(path);
400 if(fileName.Contains("AliESDs.root")){
401 fileName.ReplaceAll("AliESDs.root", "");
403 else if(fileName.Contains("AliAOD.root")){
404 fileName.ReplaceAll("AliAOD.root", "");
406 else if(fileName.Contains("galice.root")){
407 // for running with galice and kinematics alone...
408 fileName.ReplaceAll("galice.root", "");
410 else if (fileName.BeginsWith("root:")) {
411 fileName.Append("?ZIP=");
414 *fPathName = fileName;
415 AliInfo(Form("Path: -%s-\n", fPathName->Data()));
420 // Handle subsidiary handlers
421 if (fSubsidiaryHandlers) {
422 TIter next(fSubsidiaryHandlers);
423 AliMCEventHandler *handler;
424 while((handler = (AliMCEventHandler*) next())) {
425 TString* spath = handler->GetInputPath();
426 if (spath->Contains("merged")) {
427 if (! fPathName->IsNull()) {
428 handler->Notify(Form("%s/../.", fPathName->Data()));
430 handler->Notify("../");
439 void AliMCEventHandler::ResetIO()
441 // Clear header and stack
443 if (fInitOk) fMCEvent->Clean();
446 delete fTreeE; fTreeE = 0;
449 if (fFileE) {delete fFileE; fFileE = 0;}
450 if (fFileK) {delete fFileK; fFileK = 0;}
451 if (fFileTR) {delete fFileTR; fFileTR = 0;}
455 if (fSubsidiaryHandlers) {
456 TIter next(fSubsidiaryHandlers);
457 AliMCEventHandler *handler;
458 while((handler = (AliMCEventHandler*)next())) {
466 Bool_t AliMCEventHandler::FinishEvent()
468 // Clean-up after each event
469 delete fDirTR; fDirTR = 0;
470 delete fDirK; fDirK = 0;
471 if (fInitOk) fMCEvent->FinishEvent();
473 if (fSubsidiaryHandlers) {
474 TIter next(fSubsidiaryHandlers);
475 AliMCEventHandler *handler;
476 while((handler = (AliMCEventHandler*)next())) {
477 handler->FinishEvent();
484 Bool_t AliMCEventHandler::Terminate()
490 Bool_t AliMCEventHandler::TerminateIO()
497 void AliMCEventHandler::SetInputPath(const char* fname)
499 // Set the input path name
501 fPathName = new TString(fname);
504 void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
506 // Add a subsidiary handler. For example for background events
508 if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
509 fSubsidiaryHandlers->Add(handler);