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