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