Merge branch 'master' into TPCdev
[u/mrichter/AliRoot.git] / STEER / STEER / AliBaseLoader.cxx
CommitLineData
a9bbb414 1/////////////////////////////////////////////////////////////////////////////////////////////
2// //
3// class AliBaseLoader //
4// //
5// Container of all data needed for full //
6// description of each data type //
7// (Hits, Kine, ...) //
8// //
9// Each data loader has a basic standard setup of BaseLoaders //
10// which can be identuified by indexes (defined by EStdBasicLoaders) //
11// Data managed by these standard base loaders has fixed naming convention //
12// e.g. - tree with hits is always named TreeH //
13// (defined in AliLoader::fgkDefaultHitsContainerName) //
14// - task DtectorName+Name defined //
15// //
16// EStdBasicLoaders idx Object Type Description //
17// kData 0 TTree or TObject main data itself (hits,digits,...) //
18// kTask 1 TTask object producing main data //
19// kQA 2 TTree quality assurance tree //
20// kQATask 3 TTask task producing QA object //
21// //
22// //
23// User can define and add more basic loaders even Run Time. //
24// Caution: in order to save information about added base loader //
25// user must rewrite Run Loader to galice.file, overwriting old setup //
26// //
27/////////////////////////////////////////////////////////////////////////////////////////////
28
29/* $Id$ */
30
a9bbb414 31#include "AliBaseLoader.h"
a9bbb414 32#include "AliTreeLoader.h"
c93255fe 33#include "AliDataLoader.h"
34#include "AliLoader.h"
35#include "AliLog.h"
36
37#include <TFile.h>
38#include <TString.h>
a9bbb414 39
40ClassImp(AliBaseLoader)
41
42//______________________________________________________________________________
43AliBaseLoader::AliBaseLoader():
44 fIsLoaded(kFALSE),
45 fStoreInTopOfFile(kFALSE),
46 fDoNotReload(kFALSE),
47 fDataLoader(0x0)
48{
49 //
50 // default constructor
51 //
52}
53
54//______________________________________________________________________________
55AliBaseLoader::AliBaseLoader(const TString& name, AliDataLoader* dl, Bool_t storeontop):
56 TNamed(name,name+" Base Loader"),
57 fIsLoaded(kFALSE),
58 fStoreInTopOfFile(storeontop),
59 fDoNotReload(storeontop),//if stored on top of file - this object is loaded ones pe
60 fDataLoader(dl)
61{
62 //
63 // constructor
64 //
65}
66
67//______________________________________________________________________________
a9bbb414 68Int_t AliBaseLoader::Load(Option_t* opt)
69{
70 //
71 // Loads and posts the data
72 //
73 AliDebug(1, Form("data type = %s, option = %s",GetName(),opt));
74
75 if (Get())
76 {
36886a65 77 AliDebug(1,Form("Data <<%s>> are already loaded. Use ReloadData to force reload. Nothing done",GetName()));
a9bbb414 78 return 0;
79 }
80
81 Int_t retval;
82
83 if (GetDataLoader()->IsFileOpen() == kTRUE)
84 {
85 if (GetDataLoader()->IsOptionContrary(opt) == kTRUE)
86 {
87 AliError(Form("Data Type %s, Container Name %s", GetDataLoader()->GetName(),GetName()));
88 AliError("File was already opened in READ-ONLY mode, while now WRITEABLE access is requested.");
89 AliError("Use AliDataLoader::SetOption to enforce change of access mode OR");
90 AliError("Load previosly loaded data with coherent option.");
91 return 10;
92 }
93 }
94 else
95 {
96 retval = GetDataLoader()->OpenFile(opt);
97 if (retval)
98 {
99 AliError(Form("Error occured while opening <<%s>> file",GetName()));
100 return retval;
101 }
102 }
103 //if file is recreated there is no sense to search for data to post and get Error message
104 if (AliLoader::TestFileOption(opt) == kFALSE)
105 {
106 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(this);
107 if (tl) tl->MakeTree();
108 fIsLoaded = kTRUE;
109 return 0;
110 }
111
112 retval = Post();
113 if (retval)
114 {
115 AliError(Form("Error occured while posting %s from file to folder.",GetName()));
116 return retval;
117 }
118
119 fIsLoaded = kTRUE;
120 return 0;
121}
122
123//______________________________________________________________________________
124Int_t AliBaseLoader::Post()
125{
126 //
127 // Posts data container to proper folders
128 //
129
130 if ( GetDirectory() == 0x0)
131 {
132 AliError(Form("%s directory is NULL. Load before.",GetDataLoader()->GetName()));
133 return 2;
134 }
135
136 TObject* data = GetFromDirectory(fName);
137 if(data)
138 {
139 //if such an obejct already exists - remove it first
140 return Post(data);
141 }
142 else
143 {
144 //check if file is in update mode
145 Int_t fileupdate = GetDataLoader()->GetFileOption().CompareTo("update",TString::kIgnoreCase);
146 if ( fileupdate == 0)
147 { //if it is, it is normal that there is no data yet
148 AliDebug(1, Form("Can not find %s in file %s (file is opened in UPDATE mode).",
149 GetName(),GetDataLoader()->GetFile()->GetName()));
150 }
151 else
152 {
153 AliError(Form("Can not find %s in file %s", GetName(),GetDataLoader()->GetFile()->GetName()));
154 return 5;
155 }
156 }
157 return 0;
158}
159
160//______________________________________________________________________________
161Int_t AliBaseLoader::Post(TObject* data)
162{
163 //
164 // Posts data container to proper folders
165 //
166 if (data == 0x0)
167 {
168 AliError("Pointer to object is NULL");
169 return 1;
170 }
171
172 if ( fName.CompareTo(data->GetName()) != 0)
173 {
174 AliFatal(Form("Object name is <<%s>> while <<%s>> expected",data->GetName(),GetName()));
175 return -1;//pro forma
176 }
177
178 TObject* obj = Get();
179 if (data == obj)
180 {
181 AliWarning("This object was already posted.");
182 return 0;
183 }
184 if (obj)
185 {
186 AliWarning(Form("Object named %s already exitsts in data folder. Removing it",GetName()));
187 Clean();
188 }
189 return AddToBoard(data);
190}
191
192//______________________________________________________________________________
193Int_t AliBaseLoader::WriteData(Option_t* opt)
194{
195 //
196 // Writes data defined by di object
197 // opt might be "OVERWRITE" in case of forcing overwriting
198 //
199 AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
200 GetName(),GetDataLoader()->GetName(),opt));
201
202 TObject *data = Get();
203 if(data == 0x0)
204 {//did not get, nothing to write
205 AliWarning(Form("Tree named %s not found in folder. Nothing to write.",GetName()));
206 return 0;
207 }
208
209 //check if file is opened
210 if (GetDirectory() == 0x0)
211 {
212 //if not try to open
213 GetDataLoader()->SetFileOption("UPDATE");
214 if (GetDataLoader()->OpenFile("UPDATE"))
215 {
216 //oops, can not open the file, give an error message and return error code
217 AliError(Form("Can not open hits file. %s ARE NOT WRITTEN",GetName()));
218 return 1;
219 }
220 }
221
222 if (GetDataLoader()->IsFileWritable() == kFALSE)
223 {
224 AliError(Form("File %s is not writable",GetDataLoader()->GetFileName().Data()));
225 return 2;
226 }
227
228 GetDirectory()->cd(); //set the proper directory active
229
230 //see if hits container already exists in this (root) directory
231 TObject* obj = GetFromDirectory(GetName());
232 if (obj)
233 { //if they exist, see if option OVERWRITE is used
234 const char *oOverWrite = strstr(opt,"OVERWRITE");
235 if(!oOverWrite)
236 {//if it is not used - give an error message and return an error code
237 AliError("Tree already exisists. Use option \"OVERWRITE\" to overwrite previous data");
238 return 3;
239 }
240 }
241
242 AliDebug(1, Form("DataName = %s, opt = %s, data object name = %s",
243 GetName(),opt,data->GetName()));
244 AliDebug(1, Form("File Name = %s, Directory Name = %s Directory's File Name = %s",
245 GetDataLoader()->GetFile()->GetName(),GetDirectory()->GetName(),
246 GetDirectory()->GetFile()->GetName()));
247
248 AliDebug(1, "Writing data");
249 data->Write(0,TObject::kOverwrite);
250
251 fIsLoaded = kTRUE; // Just to ensure flag is on. Object can be posted manually to folder structure, not using loader.
252
253 return 0;
254
255}
256
257//______________________________________________________________________________
258Int_t AliBaseLoader::Reload()
259{
260 //
261 // Unloads and loads datat again - if loaded before
262 //
263 if (IsLoaded())
264 {
265 Unload();
266 return Load(GetDataLoader()->GetFileOption());
267 }
268 return 0;
269}
270
271//______________________________________________________________________________
272void AliBaseLoader::Clean()
273{
274 //
275 // Removes objects from folder/task
276 //
277 AliDebug(1, Form("%s %s",GetName(),GetDataLoader()->GetName()));
278 TObject* obj = Get();
279 if(obj)
280 {
281 AliDebug(1, Form("cleaning %s.",GetName()));
282 RemoveFromBoard(obj);
283 delete obj;
284 }
285}
286
287//______________________________________________________________________________
288void AliBaseLoader::Unload()
289{
290 // Unloads data and closes the files
291 Clean();
292 fIsLoaded = kFALSE;
293 GetDataLoader()->CloseFile();
294}
295
296//______________________________________________________________________________
297AliDataLoader* AliBaseLoader::GetDataLoader() const
298{
299 //
300 // Returns pointer to the data loader
301 //
302 if (fDataLoader == 0x0)
303 {
304 AliFatal("Pointer to Data Loader is NULL");
305 }
306 return fDataLoader;
307}
308
309//______________________________________________________________________________
310TDirectory* AliBaseLoader::GetDirectory() const
311{
312 //
313 // returnd TDirectory where data are to be saved
314 // if fStoreInTopOfFile flag is true - returns pointer to file
315 //
316 return (fStoreInTopOfFile)?GetDataLoader()->GetFile():GetDataLoader()->GetDirectory();
317}
318
319
320