1 /************************************************************************* * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved *
3 * Author: The ALICE Off-line Project. *
4 * Contributors are mentioned in the code where appropriate. *
6 * Permission to use, copy, modify and distribute this software and its *
7 * documentation strictly for non-commercial purposes is hereby granted *
8 * without fee, provided that the above copyright notice appears in all *
9 * copies and that both the copyright notice and this permission notice *
10 * appear in the supporting documentation. The authors make no claims *
11 * about the suitability of this software for any purpose. It is *
12 * provided "as is" without express or implied warranty. *
13 **************************************************************************/
16 //---------------------------------------------------------------------------------
17 // Class AliMCEventHandler
18 // This class gives access to MC truth during the analysis.
19 // Monte Carlo truth is containe in the kinematics tree (produced particles) and
20 // the tree of reference hits.
22 // Origin: Andreas Morsch, CERN, andreas.morsch@cern.ch
23 //---------------------------------------------------------------------------------
27 #include "AliMCEventHandler.h"
28 #include "AliMCEvent.h"
30 #include "AliTrackReference.h"
31 #include "AliHeader.h"
37 #include <TParticle.h>
39 #include <TClonesArray.h>
40 #include <TDirectoryFile.h>
42 ClassImp(AliMCEventHandler)
44 AliMCEventHandler::AliMCEventHandler() :
46 fMCEvent(new AliMCEvent()),
59 fPathName(new TString("./")),
67 // Default constructor
69 // Be sure to add all particles to the PDG database
70 AliPDG::AddParticlesToPdgDataBase();
73 AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
74 AliVEventHandler(name, title),
75 fMCEvent(new AliMCEvent()),
88 fPathName(new TString("./")),
98 // Be sure to add all particles to the PDG database
99 AliPDG::AddParticlesToPdgDataBase();
101 AliMCEventHandler::~AliMCEventHandler()
110 Bool_t AliMCEventHandler::Init(Option_t* opt)
114 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
116 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
118 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
125 fFileE->GetObject("TE", fTreeE);
126 // Connect Tree E to the MCEvent
127 fMCEvent->ConnectTreeE(fTreeE);
128 fNEvent = fTreeE->GetEntries();
131 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
133 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
138 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
142 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
144 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
150 // Reset the event number
153 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
158 Bool_t AliMCEventHandler::GetEvent(Int_t iev)
160 // Load the event number iev
162 // Calculate the file number
163 if (!fInitOk) return kFALSE;
165 Int_t inew = iev / fEventsPerFile;
166 if (inew != fFileNumber) {
168 if (!OpenFile(fFileNumber)){
174 sprintf(folder, "Event%d", iev);
176 fTreeE->GetEntry(iev);
178 fFileK->GetObject(folder, fDirK);
180 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
184 fDirK ->GetObject("TreeK", fTreeK);
185 // Connect TreeK to MCEvent
186 fMCEvent->ConnectTreeK(fTreeK);
189 // Check which format has been read
190 fFileTR->GetObject(folder, fDirTR);
191 fDirTR->GetObject("TreeTR", fTreeTR);
193 // Connect TR to MCEvent
194 fMCEvent->ConnectTreeTR(fTreeTR);
200 Bool_t AliMCEventHandler::OpenFile(Int_t i)
204 fExtension = Form("%d", i);
211 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
213 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
220 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
222 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
232 Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
234 fParticleSelected.Delete();
236 // Read the next event
244 if (entry >= fNEvent) {
245 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
248 return GetEvent(entry);
251 void AliMCEventHandler::SelectParticle(Int_t i){
252 // taking the absolute values here, need to take care
253 // of negative daughter and mother
255 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
258 Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
259 // taking the absolute values here, need to take
260 // care with negative daughter and mother
262 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
266 void AliMCEventHandler::CreateLabelMap(){
269 // this should be called once all selections where done
274 fParticleSelected.Delete();
278 VerifySelectedParticles();
279 AliStack *pStack = fMCEvent->Stack();
282 for(int i = 0;i < pStack->GetNtrack();++i){
283 if(IsParticleSelected(i)){
284 fLabelMap.Add(i,iNew);
290 Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
291 // Gets the labe from the new created Map
292 // Call CreatLabelMap before
293 // otherwise only 0 returned
294 return fLabelMap.GetValue(TMath::Abs(i));
297 void AliMCEventHandler::VerifySelectedParticles(){
300 // Make sure that each particle has at least it's predecessors
301 // selected so that we have the complete ancestry tree
302 // Private, should be only called by CreateLabelMap
305 fParticleSelected.Delete();
308 AliStack *pStack = fMCEvent->Stack();
310 Int_t nprim = pStack->GetNprimary();
312 for(int i = 0;i < pStack->GetNtrack();++i){
314 SelectParticle(i);// take all primaries
317 if(!IsParticleSelected(i))continue;
318 TParticle *part = pStack->Particle(i);
319 Int_t imo = part->GetFirstMother();
320 while((imo >= nprim)&&!IsParticleSelected(imo)){
321 // Mother not yet selected
323 TParticle *mother = pStack->Particle(imo);
324 imo = mother->GetFirstMother();
326 // after last step we may have a unselected primary
329 if(!IsParticleSelected(imo))
332 }// loop over all tracks
335 Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
341 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
345 void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
347 // Retrieve entry i and draw momentum vector and hits
348 fMCEvent->DrawCheck(i, search);
351 Bool_t AliMCEventHandler::Notify(const char *path)
353 // Notify about directory change
354 // The directory is taken from the 'path' argument
356 TString fileName(path);
357 if(fileName.Contains("AliESDs.root")){
358 fileName.ReplaceAll("AliESDs.root", "");
360 else if(fileName.Contains("AliAOD.root")){
361 fileName.ReplaceAll("AliAOD.root", "");
363 else if(fileName.Contains("galice.root")){
364 // for running with galice and kinematics alone...
365 fileName.ReplaceAll("galice.root", "");
368 *fPathName = fileName;
369 AliInfo(Form("Notify() Path: %s\n", fPathName->Data()));
377 void AliMCEventHandler::ResetIO()
379 // Clear header and stack
381 if (fInitOk) fMCEvent->Clean();
384 delete fTreeE; fTreeE = 0;
387 if (fFileE) {delete fFileE; fFileE = 0;}
388 if (fFileK) {delete fFileK; fFileK = 0;}
389 if (fFileTR) {delete fFileTR; fFileTR = 0;}
395 Bool_t AliMCEventHandler::FinishEvent()
397 // Clean-up after each event
398 delete fDirTR; fDirTR = 0;
399 delete fDirK; fDirK = 0;
400 if (fInitOk) fMCEvent->FinishEvent();
404 Bool_t AliMCEventHandler::Terminate()
410 Bool_t AliMCEventHandler::TerminateIO()
417 void AliMCEventHandler::SetInputPath(const char* fname)
419 // Set the input path name
421 fPathName = new TString(fname);