some more bug fixes
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
CommitLineData
5fe09262 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/* $Id$ */
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.
22//
23// Origin: Andreas Morsch, CERN, andreas.morsch@cern.ch
24//---------------------------------------------------------------------------------
25
26
27
28#include "AliMCEventHandler.h"
415d9f5c 29#include "AliMCEvent.h"
5fe09262 30#include "AliTrackReference.h"
31#include "AliHeader.h"
32#include "AliStack.h"
2c36081e 33#include "AliLog.h"
5fe09262 34
35#include <TTree.h>
36#include <TFile.h>
37#include <TParticle.h>
0a05cd41 38#include <TString.h>
5fe09262 39#include <TClonesArray.h>
40#include <TDirectoryFile.h>
5fe09262 41
42ClassImp(AliMCEventHandler)
43
44AliMCEventHandler::AliMCEventHandler() :
d2f1d9ef 45 AliVEventHandler(),
415d9f5c 46 fMCEvent(new AliMCEvent()),
5fe09262 47 fFileE(0),
48 fFileK(0),
49 fFileTR(0),
5fe09262 50 fTreeE(0),
51 fTreeK(0),
52 fTreeTR(0),
5efedd31 53 fDirK(0),
54 fDirTR(0),
5fe09262 55 fNEvent(-1),
56 fEvent(-1),
0a05cd41 57 fPathName(new TString("./")),
9aea8469 58 fExtension(""),
59 fFileNumber(0),
969c7896 60 fEventsPerFile(0),
61 fReadTR(kTRUE)
5fe09262 62{
63 // Default constructor
64}
65
66AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
d2f1d9ef 67 AliVEventHandler(name, title),
415d9f5c 68 fMCEvent(new AliMCEvent()),
5fe09262 69 fFileE(0),
70 fFileK(0),
71 fFileTR(0),
5fe09262 72 fTreeE(0),
73 fTreeK(0),
74 fTreeTR(0),
5efedd31 75 fDirK(0),
76 fDirTR(0),
5fe09262 77 fNEvent(-1),
78 fEvent(-1),
0a05cd41 79 fPathName(new TString("./")),
9aea8469 80 fExtension(""),
81 fFileNumber(0),
82 fEventsPerFile(0)
5fe09262 83{
84 // Constructor
85}
86AliMCEventHandler::~AliMCEventHandler()
87{
88 // Destructor
415d9f5c 89 delete fMCEvent;
5fe09262 90 delete fFileE;
91 delete fFileK;
92 delete fFileTR;
93}
94
969c7896 95Bool_t AliMCEventHandler::InitIO(Option_t* opt)
5fe09262 96{
97 // Initialize input
98 //
6073f8c9 99 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
100 //
36e82a52 101 ResetIO();
6073f8c9 102
0a05cd41 103 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
104 if (!fFileE) AliFatal(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
5fe09262 105
415d9f5c 106 //
107 // Tree E
5fe09262 108 fFileE->GetObject("TE", fTreeE);
415d9f5c 109 // Connect Tree E to the MCEvent
110 fMCEvent->ConnectTreeE(fTreeE);
5fe09262 111 fNEvent = fTreeE->GetEntries();
112 //
113 // Tree K
0a05cd41 114 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
2c36081e 115 if (!fFileK) AliFatal(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
9aea8469 116 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
5fe09262 117 //
118 // Tree TR
969c7896 119 if (fReadTR) {
120 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
121 if (!fFileTR) AliWarning(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
122 }
5fe09262 123 //
124 // Reset the event number
2c36081e 125 fEvent = -1;
126 fFileNumber = 0;
9aea8469 127
2c36081e 128 AliInfo(Form("AliMCEventHandler:Number of events in this directory %5d \n", fNEvent));
5fe09262 129 return kTRUE;
5fe09262 130}
131
9aea8469 132Bool_t AliMCEventHandler::GetEvent(Int_t iev)
133{
134 // Load the event number iev
2c36081e 135 //
136 // Calculate the file number
415d9f5c 137 Int_t inew = iev / fEventsPerFile;
9aea8469 138 if (inew != fFileNumber) {
139 fFileNumber = inew;
140 if (!OpenFile(fFileNumber)){
141 return kFALSE;
142 }
143 }
2c36081e 144 // Folder name
5fe09262 145 char folder[20];
9aea8469 146 sprintf(folder, "Event%d", iev);
5fe09262 147 // TreeE
9aea8469 148 fTreeE->GetEntry(iev);
5fe09262 149 // Tree K
5efedd31 150 fFileK->GetObject(folder, fDirK);
151 if (!fDirK) {
2c36081e 152 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
9aea8469 153 return kFALSE;
154 }
5efedd31 155 fDirK ->GetObject("TreeK", fTreeK);
415d9f5c 156 // Connect TreeK to MCEvent
157 fMCEvent->ConnectTreeK(fTreeK);
5fe09262 158 //Tree TR
07bd4750 159 if (fFileTR) {
5efedd31 160 // Check which format has been read
161 fFileTR->GetObject(folder, fDirTR);
162 fDirTR->GetObject("TreeTR", fTreeTR);
415d9f5c 163 //
164 // Connect TR to MCEvent
165 fMCEvent->ConnectTreeTR(fTreeTR);
5fe09262 166 }
5fe09262 167 //
5fe09262 168 return kTRUE;
9aea8469 169}
170
171Bool_t AliMCEventHandler::OpenFile(Int_t i)
172{
173 // Open file i
174 Bool_t ok = kTRUE;
175 if (i > 0) {
176 fExtension = Form("%d", i);
177 } else {
178 fExtension = "";
179 }
180
181
182 delete fFileK;
0a05cd41 183 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
9aea8469 184 if (!fFileK) {
0a05cd41 185 AliFatal(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
9aea8469 186 ok = kFALSE;
187 }
188
189 delete fFileTR;
0a05cd41 190 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
9aea8469 191 if (!fFileTR) {
0a05cd41 192 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
9aea8469 193 ok = kFALSE;
194 }
195
196 return ok;
197}
198
ed97dc98 199Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
9aea8469 200{
201 // Read the next event
ed97dc98 202 if (entry == -1) {
203 fEvent++;
204 entry = fEvent;
205 } else {
206 fEvent = entry;
207 }
208
209 if (entry >= fNEvent) {
210 AliWarning(Form("AliMCEventHandler: Event number out of range %5d\n", entry));
9aea8469 211 return kFALSE;
212 }
ed97dc98 213 return GetEvent(entry);
5fe09262 214}
215
216Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
217{
218 // Retrieve entry i
415d9f5c 219 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
5fe09262 220}
221
2d8f26f6 222void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
5fe09262 223{
224 // Retrieve entry i and draw momentum vector and hits
415d9f5c 225 fMCEvent->DrawCheck(i, search);
5fe09262 226}
227
890126ab 228Bool_t AliMCEventHandler::Notify(const char *path)
5fe09262 229{
53faeca4 230 // Notify about directory change
231 // The directory is taken from the 'path' argument
232 // Reconnect trees
233 TString fileName(path);
234 if(fileName.Contains("AliESDs.root")){
235 fileName.ReplaceAll("AliESDs.root", "");
236 }
237 else if(fileName.Contains("galice.root")){
238 // for running with galice and kinematics alone...
239 fileName.ReplaceAll("galice.root", "");
240 }
241
242 *fPathName = fileName;
243 printf("AliMCEventHandler::Notify() Path: %s\n", fPathName->Data());
244
5fe09262 245 return kTRUE;
246}
36e82a52 247
5fe09262 248void AliMCEventHandler::ResetIO()
249{
5efedd31 250// Clear header and stack
415d9f5c 251 fMCEvent->Clean();
5efedd31 252
36e82a52 253// Delete Tree E
415d9f5c 254 delete fTreeE; fTreeE = 0;
36e82a52 255
5efedd31 256// Reset files
36e82a52 257 if (fFileE) delete fFileE; fFileE = 0;
258 if (fFileK) delete fFileK; fFileK = 0;
259 if (fFileTR) delete fFileTR; fFileTR = 0;
5fe09262 260}
261
262
263Bool_t AliMCEventHandler::FinishEvent()
264{
5efedd31 265 // Clean-up after each event
266 delete fDirTR; fDirTR = 0;
267 delete fDirK; fDirK = 0;
415d9f5c 268 fMCEvent->FinishEvent();
5fe09262 269 return kTRUE;
270}
271
272Bool_t AliMCEventHandler::Terminate()
273{
274 // Dummy
275 return kTRUE;
276}
277
278Bool_t AliMCEventHandler::TerminateIO()
279{
280 // Dummy
281 return kTRUE;
282}
283
0a05cd41 284
285void AliMCEventHandler::SetInputPath(char* fname)
286{
287 // Set the input path name
288 delete fPathName;
289 fPathName = new TString(fname);
290}