]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliMCEventHandler.cxx
Memory leaks in TOF visualization code
[u/mrichter/AliRoot.git] / STEER / AliMCEventHandler.cxx
CommitLineData
93836e1b 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved *
5fe09262 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"
93836e1b 30#include "AliMCParticle.h"
82418625 31#include "AliPDG.h"
5fe09262 32#include "AliTrackReference.h"
33#include "AliHeader.h"
34#include "AliStack.h"
2c36081e 35#include "AliLog.h"
5fe09262 36
37#include <TTree.h>
38#include <TFile.h>
93836e1b 39#include <TList.h>
5fe09262 40#include <TParticle.h>
0a05cd41 41#include <TString.h>
5fe09262 42#include <TClonesArray.h>
43#include <TDirectoryFile.h>
5fe09262 44
45ClassImp(AliMCEventHandler)
46
47AliMCEventHandler::AliMCEventHandler() :
d2f1d9ef 48 AliVEventHandler(),
415d9f5c 49 fMCEvent(new AliMCEvent()),
5fe09262 50 fFileE(0),
51 fFileK(0),
52 fFileTR(0),
5fe09262 53 fTreeE(0),
54 fTreeK(0),
55 fTreeTR(0),
5efedd31 56 fDirK(0),
57 fDirTR(0),
da97a08a 58 fParticleSelected(0),
59 fLabelMap(0),
5fe09262 60 fNEvent(-1),
61 fEvent(-1),
0a05cd41 62 fPathName(new TString("./")),
9aea8469 63 fExtension(""),
64 fFileNumber(0),
969c7896 65 fEventsPerFile(0),
c8b7b5d3 66 fReadTR(kTRUE),
93836e1b 67 fInitOk(kFALSE),
68 fSubsidiaryHandlers(0),
69 fEventsInContainer(0)
5fe09262 70{
82418625 71 //
72 // Default constructor
73 //
74 // Be sure to add all particles to the PDG database
75 AliPDG::AddParticlesToPdgDataBase();
5fe09262 76}
77
78AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
d2f1d9ef 79 AliVEventHandler(name, title),
415d9f5c 80 fMCEvent(new AliMCEvent()),
5fe09262 81 fFileE(0),
82 fFileK(0),
83 fFileTR(0),
5fe09262 84 fTreeE(0),
85 fTreeK(0),
86 fTreeTR(0),
5efedd31 87 fDirK(0),
88 fDirTR(0),
da97a08a 89 fParticleSelected(0),
90 fLabelMap(0),
5fe09262 91 fNEvent(-1),
92 fEvent(-1),
0a05cd41 93 fPathName(new TString("./")),
9aea8469 94 fExtension(""),
95 fFileNumber(0),
0cd61c1d 96 fEventsPerFile(0),
c8b7b5d3 97 fReadTR(kTRUE),
93836e1b 98 fInitOk(kFALSE),
99 fSubsidiaryHandlers(0),
100 fEventsInContainer(0)
5fe09262 101{
82418625 102 //
103 // Constructor
104 //
105 // Be sure to add all particles to the PDG database
106 AliPDG::AddParticlesToPdgDataBase();
5fe09262 107}
108AliMCEventHandler::~AliMCEventHandler()
109{
110 // Destructor
415d9f5c 111 delete fMCEvent;
5fe09262 112 delete fFileE;
113 delete fFileK;
114 delete fFileTR;
115}
116
300d5701 117Bool_t AliMCEventHandler::Init(Option_t* opt)
5fe09262 118{
119 // Initialize input
120 //
6073f8c9 121 if (!(strcmp(opt, "proof")) || !(strcmp(opt, "local"))) return kTRUE;
122 //
0a05cd41 123 fFileE = TFile::Open(Form("%sgalice.root", fPathName->Data()));
c8b7b5d3 124 if (!fFileE) {
125 AliError(Form("AliMCEventHandler:galice.root not found in directory %s ! \n", fPathName->Data()));
126 fInitOk = kFALSE;
127 return kFALSE;
128 }
129
415d9f5c 130 //
131 // Tree E
5fe09262 132 fFileE->GetObject("TE", fTreeE);
415d9f5c 133 // Connect Tree E to the MCEvent
134 fMCEvent->ConnectTreeE(fTreeE);
5fe09262 135 fNEvent = fTreeE->GetEntries();
136 //
137 // Tree K
0a05cd41 138 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
c8b7b5d3 139 if (!fFileK) {
140 AliError(Form("AliMCEventHandler:Kinematics.root not found in directory %s ! \n", fPathName));
141 fInitOk = kFALSE;
b72029a3 142 return kTRUE;
c8b7b5d3 143 }
144
9aea8469 145 fEventsPerFile = fFileK->GetNkeys() - fFileK->GetNProcessIDs();
5fe09262 146 //
147 // Tree TR
969c7896 148 if (fReadTR) {
c8b7b5d3 149 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
150 if (!fFileTR) {
151 AliError(Form("AliMCEventHandler:TrackRefs.root not found in directory %s ! \n", fPathName->Data()));
152 fInitOk = kFALSE;
b72029a3 153 return kTRUE;
c8b7b5d3 154 }
969c7896 155 }
5fe09262 156 //
157 // Reset the event number
2c36081e 158 fEvent = -1;
159 fFileNumber = 0;
b544c64d 160 AliInfo(Form("Number of events in this directory %5d \n", fNEvent));
c8b7b5d3 161 fInitOk = kTRUE;
93836e1b 162
163
164 if (fSubsidiaryHandlers) {
165 TIter next(fSubsidiaryHandlers);
166 AliMCEventHandler *handler;
167 while((handler = (AliMCEventHandler*)next())) {
168 handler->Init(opt);
169 handler->SetNumberOfEventsInContainer(fNEvent);
170 }
171 }
172
5fe09262 173 return kTRUE;
5fe09262 174}
175
9aea8469 176Bool_t AliMCEventHandler::GetEvent(Int_t iev)
177{
178 // Load the event number iev
2c36081e 179 //
180 // Calculate the file number
c8b7b5d3 181 if (!fInitOk) return kFALSE;
182
415d9f5c 183 Int_t inew = iev / fEventsPerFile;
9aea8469 184 if (inew != fFileNumber) {
185 fFileNumber = inew;
186 if (!OpenFile(fFileNumber)){
187 return kFALSE;
188 }
189 }
2c36081e 190 // Folder name
5fe09262 191 char folder[20];
9aea8469 192 sprintf(folder, "Event%d", iev);
5fe09262 193 // TreeE
9aea8469 194 fTreeE->GetEntry(iev);
5fe09262 195 // Tree K
5efedd31 196 fFileK->GetObject(folder, fDirK);
197 if (!fDirK) {
2c36081e 198 AliWarning(Form("AliMCEventHandler: Event #%5d not found\n", iev));
9aea8469 199 return kFALSE;
200 }
b72029a3 201
5efedd31 202 fDirK ->GetObject("TreeK", fTreeK);
415d9f5c 203 // Connect TreeK to MCEvent
204 fMCEvent->ConnectTreeK(fTreeK);
5fe09262 205 //Tree TR
07bd4750 206 if (fFileTR) {
5efedd31 207 // Check which format has been read
208 fFileTR->GetObject(folder, fDirTR);
209 fDirTR->GetObject("TreeTR", fTreeTR);
415d9f5c 210 //
211 // Connect TR to MCEvent
212 fMCEvent->ConnectTreeTR(fTreeTR);
5fe09262 213 }
93836e1b 214
5fe09262 215 //
5fe09262 216 return kTRUE;
9aea8469 217}
218
219Bool_t AliMCEventHandler::OpenFile(Int_t i)
220{
221 // Open file i
9aea8469 222 if (i > 0) {
223 fExtension = Form("%d", i);
224 } else {
225 fExtension = "";
226 }
227
228
229 delete fFileK;
0a05cd41 230 fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fExtension));
9aea8469 231 if (!fFileK) {
c8b7b5d3 232 AliError(Form("AliMCEventHandler:Kinematics%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
233 fInitOk = kFALSE;
234 return kFALSE;
9aea8469 235 }
236
c8b7b5d3 237 if (fReadTR) {
238 delete fFileTR;
239 fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fExtension));
240 if (!fFileTR) {
241 AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fExtension, fPathName->Data()));
242 fInitOk = kFALSE;
243 return kFALSE;
244 }
9aea8469 245 }
246
c8b7b5d3 247 fInitOk = kTRUE;
93836e1b 248
c8b7b5d3 249 return kTRUE;
9aea8469 250}
251
ed97dc98 252Bool_t AliMCEventHandler::BeginEvent(Long64_t entry)
9aea8469 253{
da97a08a 254 fParticleSelected.Delete();
255 fLabelMap.Delete();
9aea8469 256 // Read the next event
93836e1b 257
258 if (fEventsInContainer != 0) {
11e4d716 259 entry = (Long64_t) ( entry * Float_t(fNEvent) / Float_t (fEventsInContainer));
93836e1b 260 }
261
262
ed97dc98 263 if (entry == -1) {
264 fEvent++;
265 entry = fEvent;
266 } else {
267 fEvent = entry;
268 }
269
270 if (entry >= fNEvent) {
93836e1b 271 AliWarning(Form("AliMCEventHandler: Event number out of range %5d %5d\n", entry, fNEvent));
9aea8469 272 return kFALSE;
273 }
93836e1b 274
275 Bool_t result = GetEvent(entry);
276
277 if (fSubsidiaryHandlers) {
278 TIter next(fSubsidiaryHandlers);
279 AliMCEventHandler *handler;
280 while((handler = (AliMCEventHandler*)next())) {
281 handler->BeginEvent(entry);
282 }
283 next.Reset();
284 while((handler = (AliMCEventHandler*)next())) {
285 fMCEvent->AddSubsidiaryEvent(handler->MCEvent());
286 }
287 fMCEvent->InitEvent();
288 }
289 return result;
290
5fe09262 291}
292
da97a08a 293void AliMCEventHandler::SelectParticle(Int_t i){
294 // taking the absolute values here, need to take care
295 // of negative daughter and mother
296 // IDs when setting!
93836e1b 297 if (TMath::Abs(i) >= AliMCEvent::BgLabelOffset()) i = fMCEvent->BgLabelToIndex(TMath::Abs(i));
298 if(!IsParticleSelected(TMath::Abs(i)))fParticleSelected.Add(TMath::Abs(i),1);
da97a08a 299}
300
301Bool_t AliMCEventHandler::IsParticleSelected(Int_t i) {
302 // taking the absolute values here, need to take
303 // care with negative daughter and mother
304 // IDs when setting!
305 return (fParticleSelected.GetValue(TMath::Abs(i))==1);
306}
307
308
309void AliMCEventHandler::CreateLabelMap(){
310
311 //
312 // this should be called once all selections where done
313 //
314
315 fLabelMap.Delete();
316 if(!fMCEvent){
317 fParticleSelected.Delete();
318 return;
319 }
320
321 VerifySelectedParticles();
da97a08a 322
323 Int_t iNew = 0;
93836e1b 324 for(int i = 0;i < fMCEvent->GetNumberOfTracks();++i){
da97a08a 325 if(IsParticleSelected(i)){
326 fLabelMap.Add(i,iNew);
327 iNew++;
328 }
329 }
330}
331
332Int_t AliMCEventHandler::GetNewLabel(Int_t i) {
93836e1b 333 // Gets the label from the new created Map
da97a08a 334 // Call CreatLabelMap before
335 // otherwise only 0 returned
336 return fLabelMap.GetValue(TMath::Abs(i));
337}
338
339void AliMCEventHandler::VerifySelectedParticles(){
340
341 //
342 // Make sure that each particle has at least it's predecessors
343 // selected so that we have the complete ancestry tree
344 // Private, should be only called by CreateLabelMap
345
346 if(!fMCEvent){
93836e1b 347 fParticleSelected.Delete();
348 return;
da97a08a 349 }
da97a08a 350
93836e1b 351 Int_t nprim = fMCEvent->GetNumberOfPrimaries();
352
353 for(int i = 0;i < fMCEvent->GetNumberOfTracks(); ++i){
354 if(i < nprim){
355 SelectParticle(i);// take all primaries
356 continue;
357 }
358
359 if(!IsParticleSelected(i))continue;
360
63a1afff 361 AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(i);
93836e1b 362 Int_t imo = mcpart->GetMother();
363 while((imo >= nprim)&&!IsParticleSelected(imo)){
364 // Mother not yet selected
365 SelectParticle(imo);
63a1afff 366 AliMCParticle* mcpart = (AliMCParticle*) fMCEvent->GetTrack(imo);
93836e1b 367 imo = mcpart->GetMother();
368 }
369 // after last step we may have an unselected primary
da97a08a 370 // mother
371 if(imo>=0){
372 if(!IsParticleSelected(imo))
373 SelectParticle(imo);
374 }
375 }// loop over all tracks
376}
377
5fe09262 378Int_t AliMCEventHandler::GetParticleAndTR(Int_t i, TParticle*& particle, TClonesArray*& trefs)
379{
380 // Retrieve entry i
c8b7b5d3 381 if (!fInitOk) {
382 return 0;
383 } else {
384 return (fMCEvent->GetParticleAndTR(i, particle, trefs));
385 }
5fe09262 386}
387
2d8f26f6 388void AliMCEventHandler::DrawCheck(Int_t i, Int_t search)
5fe09262 389{
390 // Retrieve entry i and draw momentum vector and hits
415d9f5c 391 fMCEvent->DrawCheck(i, search);
5fe09262 392}
393
890126ab 394Bool_t AliMCEventHandler::Notify(const char *path)
5fe09262 395{
53faeca4 396 // Notify about directory change
397 // The directory is taken from the 'path' argument
398 // Reconnect trees
399 TString fileName(path);
400 if(fileName.Contains("AliESDs.root")){
401 fileName.ReplaceAll("AliESDs.root", "");
402 }
c8b5ce3a 403 else if(fileName.Contains("AliAOD.root")){
404 fileName.ReplaceAll("AliAOD.root", "");
405 }
53faeca4 406 else if(fileName.Contains("galice.root")){
407 // for running with galice and kinematics alone...
408 fileName.ReplaceAll("galice.root", "");
409 }
39734d21 410 else if (fileName.BeginsWith("root:")) {
411 fileName.Append("?ZIP=");
412 }
93836e1b 413
53faeca4 414 *fPathName = fileName;
93836e1b 415 AliInfo(Form("Path: -%s-\n", fPathName->Data()));
53faeca4 416
f6065654 417 ResetIO();
418 InitIO("");
419
93836e1b 420// Handle subsidiary handlers
421 if (fSubsidiaryHandlers) {
422 TIter next(fSubsidiaryHandlers);
423 AliMCEventHandler *handler;
424 while((handler = (AliMCEventHandler*) next())) {
425 TString* spath = handler->GetInputPath();
426 if (spath->Contains("merged")) {
427 if (! fPathName->IsNull()) {
428 handler->Notify(Form("%s/../.", fPathName->Data()));
429 } else {
430 handler->Notify("../");
431 }
432 }
433 }
434 }
435
5fe09262 436 return kTRUE;
437}
36e82a52 438
5fe09262 439void AliMCEventHandler::ResetIO()
440{
5efedd31 441// Clear header and stack
b72029a3 442
aff49450 443 if (fInitOk) fMCEvent->Clean();
5efedd31 444
36e82a52 445// Delete Tree E
415d9f5c 446 delete fTreeE; fTreeE = 0;
b72029a3 447
5efedd31 448// Reset files
89f249fc 449 if (fFileE) {delete fFileE; fFileE = 0;}
450 if (fFileK) {delete fFileK; fFileK = 0;}
451 if (fFileTR) {delete fFileTR; fFileTR = 0;}
19cfde04 452 fExtension="";
c8b7b5d3 453 fInitOk = kFALSE;
93836e1b 454
455 if (fSubsidiaryHandlers) {
456 TIter next(fSubsidiaryHandlers);
457 AliMCEventHandler *handler;
458 while((handler = (AliMCEventHandler*)next())) {
459 handler->ResetIO();
460 }
461 }
462
5fe09262 463}
464
465
466Bool_t AliMCEventHandler::FinishEvent()
467{
5efedd31 468 // Clean-up after each event
469 delete fDirTR; fDirTR = 0;
470 delete fDirK; fDirK = 0;
aff49450 471 if (fInitOk) fMCEvent->FinishEvent();
93836e1b 472
473 if (fSubsidiaryHandlers) {
474 TIter next(fSubsidiaryHandlers);
475 AliMCEventHandler *handler;
476 while((handler = (AliMCEventHandler*)next())) {
477 handler->FinishEvent();
478 }
479 }
480
5fe09262 481 return kTRUE;
482}
483
484Bool_t AliMCEventHandler::Terminate()
485{
486 // Dummy
487 return kTRUE;
488}
489
490Bool_t AliMCEventHandler::TerminateIO()
491{
492 // Dummy
493 return kTRUE;
494}
495
0a05cd41 496
0931e76a 497void AliMCEventHandler::SetInputPath(const char* fname)
0a05cd41 498{
499 // Set the input path name
500 delete fPathName;
501 fPathName = new TString(fname);
502}
93836e1b 503
504void AliMCEventHandler::AddSubsidiaryHandler(AliMCEventHandler* handler)
505{
506 // Add a subsidiary handler. For example for background events
507
508 if (!fSubsidiaryHandlers) fSubsidiaryHandlers = new TList();
509 fSubsidiaryHandlers->Add(handler);
510}