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