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