Effective C++ changes (Andrei)
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisRLContainer.cxx
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 // Author: Andrei Gheata, 31/05/2006
18
19 //==============================================================================
20 //   AliAnalysysRLContainer - 
21 //==============================================================================
22
23 #include "AliAnalysisRLContainer.h"
24 #include "TTree.h"
25 #include "TFile.h"
26
27 #include "AliLog.h"
28 #include "AliRunLoader.h"
29 #include "AliESD.h"
30 #include "AliHeader.h"
31 #include "AliStack.h"
32 #include "AliAnalysisDataContainer.h"
33 #include "AliAnalysisDataSlot.h"
34 #include "AliAnalysisTask.h"
35
36 ClassImp(AliAnalysisRLContainer)
37
38 //______________________________________________________________________________
39 AliAnalysisRLContainer::AliAnalysisRLContainer()
40                        :AliAnalysisDataContainer(),
41                         fRunLoader(NULL),
42                         fESD(NULL),
43                         fKineFile(NULL),
44                         fKinematicsLoaded(kFALSE),
45                         fHeaderLoaded(kFALSE)
46 {
47 // Dummy ctor.
48 }
49
50 //______________________________________________________________________________
51 AliAnalysisRLContainer::AliAnalysisRLContainer(const char *name)
52                        :AliAnalysisDataContainer(name, TTree::Class()),
53                         fRunLoader(NULL),
54                         fESD(NULL),
55                         fKineFile(NULL),
56                         fKinematicsLoaded(kFALSE),
57                         fHeaderLoaded(kFALSE)
58 {
59 // Normal constructor.
60 }
61
62 //______________________________________________________________________________
63 AliAnalysisRLContainer::AliAnalysisRLContainer(const AliAnalysisRLContainer &rlc)
64                        :AliAnalysisDataContainer(rlc),
65                         fRunLoader(rlc.fRunLoader),
66                         fESD(rlc.fESD),
67                         fKineFile(rlc.fKineFile),
68                         fKinematicsLoaded(rlc.fKinematicsLoaded),
69                         fHeaderLoaded(rlc.fHeaderLoaded)
70 {
71 // Copy constructor.
72 }   
73
74 //______________________________________________________________________________
75 AliAnalysisRLContainer::~AliAnalysisRLContainer()
76 {
77 // Destructor. Deletes data ! (What happens if data is a container ???)
78 }
79
80 //______________________________________________________________________________
81 AliAnalysisRLContainer &AliAnalysisRLContainer::operator=(const AliAnalysisRLContainer &rlc)
82 {
83 // Assignment.
84    if (&rlc != this) {
85       AliAnalysisDataContainer::operator=(rlc);
86       fRunLoader = rlc.fRunLoader;
87       fESD = rlc.fESD;
88       fKineFile = rlc.fKineFile;
89       fKinematicsLoaded = rlc.fKinematicsLoaded;
90       fHeaderLoaded = rlc.fHeaderLoaded;
91    }
92    return *this;   
93 }
94
95 //______________________________________________________________________________
96 Bool_t AliAnalysisRLContainer::SetData(TObject *data, Option_t */*option*/)
97 {
98 // Data must be a tree here.
99    fData = data;
100    TTree *tree = (TTree *)data;
101    // Set branch address
102    tree->SetBranchAddress("ESD", &fESD);
103    fDataReady = kTRUE;
104    return kTRUE;
105 }
106
107 //______________________________________________________________________________
108 void AliAnalysisRLContainer::GetEntry(Long64_t ientry)
109 {
110 // If data is ready and derives from TTree or from TBranch, this will get the
111 // requested entry in memory if not already loaded.
112    if (!fDataReady) return;
113    TTree *tree = (TTree*)fData;
114    tree->GetTree()->GetEntry(ientry);
115    if (fRunLoader) fRunLoader->GetEvent(ientry);
116 }   
117
118 //______________________________________________________________________________
119 void AliAnalysisRLContainer::NotifyChange(ENotifyMessage type)
120 {
121 // Notify container that file has changed.
122    AliAnalysisDataContainer::NotifyChange(type);
123    if (!type==kFileChange) return;
124    DeleteKinematicsFile();
125    DeleteRunLoader();
126 }
127
128 //______________________________________________________________________________
129 void AliAnalysisRLContainer::DeleteRunLoader() 
130 {
131 // Deletes the runloader.
132    if (fRunLoader) {
133       fRunLoader->Delete();
134       fRunLoader = 0;
135    }
136    fKinematicsLoaded = kFALSE;
137    fHeaderLoaded = kFALSE;
138 }
139
140 //______________________________________________________________________________
141 void AliAnalysisRLContainer::DeleteKinematicsFile() 
142 {
143 // Closes the kinematics file and deletes the pointer.
144    if (fKineFile) {
145       fKineFile->Close();
146       delete fKineFile;
147       fKineFile = 0;
148    }
149 }   
150
151 //______________________________________________________________________________
152 AliRunLoader* AliAnalysisRLContainer::GetRunLoader()
153 {
154 // Returns AliRun instance corresponding to current ESD active in fTree
155 // Loads galice.root, the file is identified by replacing "AliESDs" to
156 // "galice" in the file path of the ESD file. This is a hack, to be changed!
157
158    if (!fDataReady) return 0;
159    TTree *tree = (TTree*)fData;
160    if (!fRunLoader) {
161       if (!tree->GetCurrentFile()) return 0;
162       TString fileName(tree->GetCurrentFile()->GetName());
163       fileName.ReplaceAll("AliESDs", "galice");
164       fRunLoader = AliRunLoader::Open(fileName);
165       if (!fRunLoader) return 0;
166       if (fRunLoader->LoadgAlice() != 0) {
167          delete fRunLoader;
168          fRunLoader = 0;
169          return 0;
170       }
171       fRunLoader->GetEvent(tree->GetTree()->GetReadEntry());
172   }
173   return fRunLoader;
174 }
175
176 //______________________________________________________________________________
177 AliHeader* AliAnalysisRLContainer::GetHeader()
178 {
179 // Returns header retrieved from RunLoader
180    AliRunLoader* runLoader = GetRunLoader();
181    if (!runLoader) return 0;
182    if (!fHeaderLoaded) 
183       if (runLoader->LoadHeader() != 0) return 0;
184    fHeaderLoaded = kTRUE;
185    return runLoader->GetHeader();
186 }
187
188 //______________________________________________________________________________
189 TTree* AliAnalysisRLContainer::GetKinematics()
190 {
191 // Returns kinematics tree corresponding to current ESD active in fTree
192 // Loads the kinematics from the kinematics file, the file is identified by replacing "AliESDs" to
193 // "Kinematics" in the file path of the ESD file. This is a hack, to be changed!
194
195    if (!fDataReady) return 0;
196    TTree *tree = (TTree*)fData;
197    if (!fKineFile) {
198       if (!tree->GetCurrentFile()) return 0;
199       TString fileName(tree->GetCurrentFile()->GetName());
200       fileName.ReplaceAll("AliESDs", "Kinematics");
201
202       AliDebug(AliLog::kInfo, Form("Opening %s", fileName.Data()));
203
204       fKineFile = TFile::Open(fileName);
205       if (!fKineFile) return 0;
206    }
207    return dynamic_cast<TTree*> (fKineFile->Get(Form("Event%d/TreeK", tree->GetTree()->GetReadEntry())));
208 }
209
210 //______________________________________________________________________________
211 AliStack* AliAnalysisRLContainer::GetStack()
212 {
213 // Returns stack retrieved from RunLoader
214
215    AliRunLoader* runLoader = GetRunLoader();
216    if (!runLoader) return 0;
217    if (!fKinematicsLoaded)
218       if (runLoader->LoadKinematics() != 0) return 0;
219    fKinematicsLoaded = kTRUE;
220    return runLoader->Stack();
221 }