Bug corrected.
[u/mrichter/AliRoot.git] / STEER / AliESDHandler.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /*$Id$*/
18
19 //-------------------------------------------------------------------------
20 //
21 //     Implementation of the Virtual Event Handler Interface for ESD
22 //
23 //-------------------------------------------------------------------------
24
25
26 #include <TTree.h>
27 #include <TFile.h>
28 #include <TString.h>
29 #include <TROOT.h>
30
31 #include "AliLog.h"
32 #include "AliESDHandler.h"
33 #include "AliESDEvent.h"
34 #include "AliESDfriend.h"
35
36 ClassImp(AliESDHandler)
37
38 //______________________________________________________________________________
39 AliESDHandler::AliESDHandler() :
40         AliVEventHandler(),
41         fesdf(NULL),
42         fTreeEF(NULL),
43         fFileEF(NULL),
44         fFileName("AliESDfriends_v1.root"),
45         fIsEventSelectedForFriends(kFALSE)
46 {
47
48         // default constructor
49 }
50
51 //______________________________________________________________________________
52 AliESDHandler::AliESDHandler(const char* name, const char* title):
53         AliVEventHandler(name, title),
54         fesdf(NULL),
55         fTreeEF(NULL),
56         fFileEF(NULL),
57         fFileName("AliESDfriends_v1.root"),
58         fIsEventSelectedForFriends(kFALSE)
59 {
60
61         // constructor with name and title
62
63 }
64
65 //______________________________________________________________________________
66 AliESDHandler::~AliESDHandler() 
67 {
68         // Destructor.
69         delete fesdf;
70         if(fFileEF){
71                 // is already handled in TerminateIO
72                 fFileEF->Close();
73                 delete fFileEF;
74         }
75         delete fTreeEF;
76 }
77
78 //______________________________________________________________________________
79 Bool_t AliESDHandler::Init(Option_t* opt)
80 {
81         //
82         // Initialize IO
83         //
84         
85         // File opening according to execution mode
86         TString option(opt);
87         option.ToLower();
88         TDirectory *owd = gDirectory;
89
90         fesdf = new AliESDfriend();
91
92         // Open the file with friends
93         if (option.Contains("proof")) {
94                 // proof
95                 // Merging via files. Need to access analysis manager via interpreter.
96                 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(\"%s\", \"RECREATE\");", fFileName.Data()));
97                 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer()->SetFile((TFile*)0x%p);", gFile));
98                 fFileEF = gFile;
99         } else {
100                 // local and grid
101                 fFileEF = new TFile(fFileName.Data(), "RECREATE");
102         }
103
104         // Create the friends tree
105         fFileEF->cd();
106         fTreeEF = new TTree("esdFriendTree", "Tree with ESD friends");
107         fTreeEF->Branch("ESDfriend.","AliESDfriend", &fesdf);
108
109         owd->cd();
110         
111         return kTRUE;
112 }
113
114
115 //______________________________________________________________________________
116 Bool_t AliESDHandler::FinishEvent()
117 {
118         //
119         // Fill the tree 
120         //
121
122         FillTree();
123         
124         // resetting
125         if (fesdf) fesdf->~AliESDfriend();
126         new(fesdf) AliESDfriend();  
127         return kTRUE;
128 }
129
130 //______________________________________________________________________________
131 Bool_t AliESDHandler::Terminate()
132 {
133         //
134         // Terminate 
135         //
136
137         return kTRUE;
138 }
139
140 //______________________________________________________________________________
141 Bool_t AliESDHandler::TerminateIO()
142 {
143         //
144         // Terminate IO
145         //
146
147         if (fFileEF) {
148                 fFileEF->cd();
149                 fTreeEF->Write();
150                 fFileEF->Close();
151                 delete fFileEF;
152                 fFileEF = 0;
153         }
154
155         return kTRUE;
156 }
157
158 //______________________________________________________________________________
159 void AliESDHandler::FillTree()
160 {
161         //
162         // Fill the ESD Tree
163         //
164         if (fIsEventSelectedForFriends){
165                 AliDebug(2,Form("number of friend tracks = %d\n",fesdf->GetNumberOfTracks()));
166         }
167         else {
168                 fesdf->SetSkipBit(kTRUE);
169         }
170         AliDebug(2,Form("friend = %p",fesdf));
171         fFileEF->cd();
172         fTreeEF->Fill();
173 }