Fix: Added config file for MC
[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"
82418625 30#include "AliPDG.h"
5fe09262 31#include "AliTrackReference.h"
32#include "AliHeader.h"
33#include "AliStack.h"
2c36081e 34#include "AliLog.h"
5fe09262 35
36#include <TTree.h>
37#include <TFile.h>
38#include <TParticle.h>
0a05cd41 39#include <TString.h>
5fe09262 40#include <TClonesArray.h>
41#include <TDirectoryFile.h>
5fe09262 42
43ClassImp(AliMCEventHandler)
44
45AliMCEventHandler::AliMCEventHandler() :
d2f1d9ef 46 AliVEventHandler(),
415d9f5c 47 fMCEvent(new AliMCEvent()),
5fe09262 48 fFileE(0),
49 fFileK(0),
50 fFileTR(0),
5fe09262 51 fTreeE(0),
52 fTreeK(0),
53 fTreeTR(0),
5efedd31 54 fDirK(0),
55 fDirTR(0),
5fe09262 56 fNEvent(-1),
57 fEvent(-1),
0a05cd41 58 fPathName(new TString("./")),
9aea8469 59 fExtension(""),
60 fFileNumber(0),
969c7896 61 fEventsPerFile(0),
c8b7b5d3 62 fReadTR(kTRUE),
63 fInitOk(kFALSE)
5fe09262 64{
82418625 65 //
66 // Default constructor
67 //
68 // Be sure to add all particles to the PDG database
69 AliPDG::AddParticlesToPdgDataBase();
5fe09262 70}
71
72AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
d2f1d9ef 73 AliVEventHandler(name, title),
415d9f5c 74 fMCEvent(new AliMCEvent()),
5fe09262 75 fFileE(0),
76 fFileK(0),
77 fFileTR(0),
5fe09262 78 fTreeE(0),
79 fTreeK(0),
80 fTreeTR(0),
5efedd31 81 fDirK(0),
82 fDirTR(0),
5fe09262 83 fNEvent(-1),
84 fEvent(-1),
0a05cd41 85 fPathName(new TString("./")),
9aea8469 86 fExtension(""),
87 fFileNumber(0),
0cd61c1d 88 fEventsPerFile(0),
c8b7b5d3 89 fReadTR(kTRUE),
90 fInitOk(kFALSE)
5fe09262 91{
82418625 92 //
93 // Constructor
94 //
95 // Be sure to add all particles to the PDG database
96 AliPDG::AddParticlesToPdgDataBase();
5fe09262 97}
98AliMCEventHandler::~AliMCEventHandler()
99{
100 // Destructor
415d9f5c 101 delete fMCEvent;
5fe09262 102 delete fFileE;
103 delete fFileK;
104 delete fFileTR;
105}
106
300d5701 107Bool_t AliMCEventHandler::Init(Option_t* opt)
5fe09262 108{
109 // Initialize input
110 //
6073f8c9 111 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
112 //
0a05cd41 113 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
c8b7b5d3 114 if (!fFileE) {
115 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
116 fInitOk = kFALSE;
117 return kFALSE;
118 }
119
415d9f5c 120 //
121 // Tree E
5fe09262 122 fFileE->GetObject("TE", fTreeE);
415d9f5c 123 // Connect Tree E to the MCEvent
124 fMCEvent->ConnectTreeE(fTreeE);
5fe09262 125 fNEvent = fTreeE->GetEntries();
126 //
127 // Tree K
0a05cd41 128 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
c8b7b5d3 129 if (!fFileK) {
130 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
131 fInitOk = kFALSE;
132 return kFALSE;
133 }
134
9aea8469 135 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
5fe09262 136 //
137 // Tree TR
969c7896 138 if (fReadTR) {
c8b7b5d3 139 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
140 if (!fFileTR) {
141 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
142 fInitOk = kFALSE;
143 return kFALSE;
144 }
969c7896 145 }
5fe09262 146 //
147 // Reset the event number
2c36081e 148 fEvent = -1;
149 fFileNumber = 0;
b544c64d 150 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
c8b7b5d3 151 fInitOk = kTRUE;
5fe09262 152 return kTRUE;
5fe09262 153}
154
9aea8469 155Bool_t AliMCEventHandler::GetEvent(Int_t iev)
156{
157 // Load the event number iev
2c36081e 158 //
159 // Calculate the file number
c8b7b5d3 160 if (!fInitOk) return kFALSE;
161
415d9f5c 162 Int_t inew = iev / fEventsPerFile;
9aea8469 163 if (inew != fFileNumber) {
164 fFileNumber = inew;
165 if (!OpenFile(fFileNumber)){
166 return kFALSE;
167 }
168 }
2c36081e 169 // Folder name
5fe09262 170 char folder[20];
9aea8469 171 sprintf(folder, "Event%d", iev);
5fe09262 172 // TreeE
9aea8469 173 fTreeE->GetEntry(iev);
5fe09262 174 // Tree K
5efedd31 175 fFileK->GetObject(folder, fDirK);
176 if (!fDirK) {
2c36081e 177 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
9aea8469 178 return kFALSE;
179 }
5efedd31 180 fDirK ->GetObject("TreeK", fTreeK);
415d9f5c 181 // Connect TreeK to MCEvent
182 fMCEvent->ConnectTreeK(fTreeK);
5fe09262 183 //Tree TR
07bd4750 184 if (fFileTR) {
5efedd31 185 // Check which format has been read
186 fFileTR->GetObject(folder, fDirTR);
187 fDirTR->GetObject("TreeTR", fTreeTR);
415d9f5c 188 //
189 // Connect TR to MCEvent
190 fMCEvent->ConnectTreeTR(fTreeTR);
5fe09262 191 }
5fe09262 192 //
5fe09262 193 return kTRUE;
9aea8469 194}
195
196Bool_t AliMCEventHandler::OpenFile(Int_t i)
197{
198 // Open file i
9aea8469 199 if (i > 0) {
200 fExtension = Form("%d", i);
201 } else {
202 fExtension = "";
203 }
204
205
206 delete fFileK;
0a05cd41 207 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
9aea8469 208 if (!fFileK) {
c8b7b5d3 209 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
210 fInitOk = kFALSE;
211 return kFALSE;
9aea8469 212 }
213
c8b7b5d3 214 if (fReadTR) {
215 delete fFileTR;
216 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
217 if (!fFileTR) {
218 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
219 fInitOk = kFALSE;
220 return kFALSE;
221 }
9aea8469 222 }
223
c8b7b5d3 224 fInitOk = kTRUE;
225 return kTRUE;
9aea8469 226}
227
ed97dc98 228Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
9aea8469 229{
230 // Read the next event
ed97dc98 231 if (entry == -1) {
232 fEvent++;
233 entry = fEvent;
234 } else {
235 fEvent = entry;
236 }
237
238 if (entry >= fNEvent) {
f6065654 239 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry,fNEvent));
9aea8469 240 return kFALSE;
241 }
ed97dc98 242 return GetEvent(entry);
5fe09262 243}
244
245Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
246{
247 // Retrieve entry i
c8b7b5d3 248 if (!fInitOk) {
249 return 0;
250 } else {
251 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
252 }
5fe09262 253}
254
2d8f26f6 255void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
5fe09262 256{
257 // Retrieve entry i and draw momentum vector and hits
415d9f5c 258 fMCEvent->DrawCheck(i, search);
5fe09262 259}
260
890126ab 261Bool_t AliMCEventHandler::Notify(const char *path)
5fe09262 262{
53faeca4 263 // Notify about directory change
264 // The directory is taken from the 'path' argument
265 // Reconnect trees
266 TString fileName(path);
267 if(fileName.Contains("AliESDs.root")){
268 fileName.ReplaceAll("AliESDs.root", "");
269 }
c8b5ce3a 270 else if(fileName.Contains("AliAOD.root")){
271 fileName.ReplaceAll("AliAOD.root", "");
272 }
53faeca4 273 else if(fileName.Contains("galice.root")){
274 // for running with galice and kinematics alone...
275 fileName.ReplaceAll("galice.root", "");
276 }
277
278 *fPathName = fileName;
b544c64d 279 AliInfo(Form("Notify() Path: %s\n", fPathName->Data()));
53faeca4 280
f6065654 281 ResetIO();
282 InitIO("");
283
5fe09262 284 return kTRUE;
285}
36e82a52 286
5fe09262 287void AliMCEventHandler::ResetIO()
288{
5efedd31 289// Clear header and stack
aff49450 290 if (fInitOk) fMCEvent->Clean();
5efedd31 291
36e82a52 292// Delete Tree E
415d9f5c 293 delete fTreeE; fTreeE = 0;
36e82a52 294
5efedd31 295// Reset files
89f249fc 296 if (fFileE) {delete fFileE; fFileE = 0;}
297 if (fFileK) {delete fFileK; fFileK = 0;}
298 if (fFileTR) {delete fFileTR; fFileTR = 0;}
19cfde04 299 fExtension="";
c8b7b5d3 300 fInitOk = kFALSE;
5fe09262 301}
302
303
304Bool_t AliMCEventHandler::FinishEvent()
305{
5efedd31 306 // Clean-up after each event
307 delete fDirTR; fDirTR = 0;
308 delete fDirK; fDirK = 0;
aff49450 309 if (fInitOk) fMCEvent->FinishEvent();
5fe09262 310 return kTRUE;
311}
312
313Bool_t AliMCEventHandler::Terminate()
314{
315 // Dummy
316 return kTRUE;
317}
318
319Bool_t AliMCEventHandler::TerminateIO()
320{
321 // Dummy
322 return kTRUE;
323}
324
0a05cd41 325
0931e76a 326void AliMCEventHandler::SetInputPath(const char* fname)
0a05cd41 327{
328 // Set the input path name
329 delete fPathName;
330 fPathName = new TString(fname);
331}