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