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