]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliDataLoader.cxx
Introducing hyperons in ESD (Yu.Belikov)
[u/mrichter/AliRoot.git] / STEER / AliDataLoader.cxx
1 #include <AliDataLoader.h>
2 //__________________________________________
3 /////////////////////////////////////////////////////////////////////////////////////////////
4 //                                                                                         //
5 //  class AliDataLoader                                                                    //
6 //                                                                                         //
7 //  Container of all data needed for full                                                  //
8 //  description of each data type                                                          //
9 //  (Hits, Kine, ...)                                                                      //
10 //                                                                                         //
11 //  Each data loader has a basic standard setup of BaseLoaders                             //
12 //  which can be identuified by indexes (defined by EStdBasicLoaders)                      //
13 //  Data managed by these standard base loaders has fixed naming convention                //
14 //  e.g. - tree with hits is always named TreeH                                            //
15 //                     (defined in AliLoader::fgkDefaultHitsContainerName)                 //
16 //       - task DtectorName+Name defined                                                   //
17 //                                                                                         //
18 //  EStdBasicLoaders   idx     Object Type        Description                              //
19 //      kData           0    TTree or TObject     main data itself (hits,digits,...)       //
20 //      kTask           1        TTask            object producing main data               //
21 //      kQA             2        TTree                quality assurance tree               //
22 //      kQATask         3        TTask            task producing QA object                 //
23 //                                                                                         //
24 //                                                                                         //
25 //  User can define and add more basic loaders even Run Time.                              //
26 //  Caution: in order to save information about added base loader                          //
27 //  user must rewrite Run Loader to galice.file, overwriting old setup                     //
28 //                                                                                         //
29 /////////////////////////////////////////////////////////////////////////////////////////////
30
31 #include <TROOT.h>
32 #include <TFile.h>
33 #include <TString.h>
34 #include <TBits.h>
35 #include <TList.h>
36
37 #include "AliRunLoader.h"
38
39 ClassImp(AliDataLoader)
40
41 AliDataLoader::AliDataLoader():
42  fFileName(0),
43  fFile(0x0),
44  fDirectory(0x0),
45  fFileOption(),
46  fCompressionLevel(2),
47  fBaseLoaders(0x0),
48  fHasTask(kFALSE),
49  fTaskName(),
50  fParentalTask(0x0),
51  fEventFolder(0x0),
52  fFolder(0x0)
53 {
54   
55 }
56 /*****************************************************************************/ 
57
58 AliDataLoader::AliDataLoader(const char* filename, const char* contname, const char* name, Option_t* opt):
59  TNamed(name,name),
60  fFileName(filename),
61  fFile(0x0),
62  fDirectory(0x0),
63  fFileOption(0),
64  fCompressionLevel(2),
65  fBaseLoaders(new TObjArray(4)),
66  fHasTask(kFALSE),
67  fTaskName(),
68  fParentalTask(0x0),
69  fEventFolder(0x0),
70  fFolder(0x0)
71 {
72 //constructor
73 // creates a 0 loader, depending on option, default "T" is specialized loader for trees
74 // else standard object loader
75 // trees needs special care, becouse we need to set dir before writing
76   if (GetDebug())
77    Info("AliDataLoader","File name is %s",fFileName.Data());
78    
79   TString option(opt);
80   AliBaseLoader* bl;
81   if (option.CompareTo("T",TString::kIgnoreCase) == 0)
82     bl = new AliTreeLoader(contname,this);
83   else 
84     bl = new AliObjectLoader(contname,this);
85   fBaseLoaders->AddAt(bl,kData);
86   
87 }
88 /*****************************************************************************/ 
89
90 AliDataLoader::~AliDataLoader()
91 {
92 //dtor
93  UnloadAll();
94 }
95 /*****************************************************************************/ 
96
97 Int_t  AliDataLoader::SetEvent()
98 {
99 //basically the same that GetEvent but do not post data to folders
100  AliRunLoader* rl = GetRunLoader();
101  if (rl == 0x0)
102   {
103     Error("SetEvent","Can not get RunGettr");
104     return 1;
105   }
106  
107  Int_t evno = rl->GetEventNumber();
108
109  TIter next(fBaseLoaders);
110  AliBaseLoader* bl;
111  while ((bl = (AliBaseLoader*)next()))
112   {
113     if (bl->DoNotReload() == kFALSE) bl->Clean();
114   }
115
116  if(fFile)
117   {
118     if (CheckReload())
119      {
120        delete fFile;
121        fFile = 0x0;
122        if (GetDebug()) Info("SetEvent","Reloading new file. File opt is %s",fFileOption.Data());
123        OpenFile(fFileOption);
124      }
125
126     fDirectory = AliLoader::ChangeDir(fFile,evno);
127     if (fDirectory == 0x0)
128       {
129         Error("SetEvent","Can not chage directory in file %s",fFile->GetName());
130         return 1;
131       }
132    }
133  return 0;
134 }
135 /*****************************************************************************/ 
136
137 Int_t  AliDataLoader::GetEvent()
138 {
139  // posts all loaded data from files to White Board
140  // event number is defined in RunLoader
141  // 
142  //returns:
143  //     0  - in case of no error
144  //     1  - event not found
145  //     
146  //for each base laoder post, if was loaded before GetEvent
147  
148  //call set event to switch to new directory in file
149
150
151  //post all data that were loaded before 
152  // ->SetEvent does not call Unload, but only cleans White Board
153  // such IsLoaded flag stays untached
154  
155  if ( AliLoader::TestFileOption(fFileOption) == kTRUE ) //if file is read or update mode try to post
156   {                                                     //in other case there is no sense to post: file is new
157    TIter nextbl(fBaseLoaders);
158    AliBaseLoader* bl;
159    while ((bl = (AliBaseLoader*)nextbl()))
160     {
161      if (bl->IsLoaded())
162       {
163         if (bl->DoNotReload() == kFALSE) bl->Post();
164       }
165     } 
166   }
167  return 0;
168 }
169 /*****************************************************************************/ 
170
171 Int_t AliDataLoader::OpenFile(Option_t* opt)
172 {
173 //Opens file named 'filename', and assigns pointer to it to 'file'
174 //jumps to fDirectoryectory corresponding to current event and stores the pointer to it in 'fDirectory'
175 //option 'opt' is passed to TFile::Open
176   if (fFile)
177    {
178      if(fFile->IsOpen() == kTRUE)
179        {
180          Warning("OpenFile"," File %s already opened. First close it.",fFile->GetName());
181          return 0;
182        }
183      else
184        {
185          Warning("OpenFile","Pointer to file %s is not null, but file is not opened",
186                 fFile->GetName());
187          delete fFile;
188          fFile = 0x0;
189        }
190    }
191   
192   TString fname(SetFileOffset(fFileName));
193   
194   fFile = (TFile *)(gROOT->GetListOfFiles()->FindObject(fname));
195   if (fFile)
196    {
197      if(fFile->IsOpen() == kTRUE)
198        {
199          Warning("OpenFile","File %s already opened by sombody else. First close it.",
200                  fFile->GetName());
201          return 0;
202        }
203    }
204   
205   fFileOption = opt;
206   fFile = TFile::Open(fname,fFileOption);//open the file
207   if (fFile == 0x0)
208    {//file is null
209      Error("OpenFile","Can not open file %s",fname.Data());
210      return 1;
211    }
212   if (fFile->IsOpen() == kFALSE)
213    {//file is null
214      Error("OpenFile","Can not open file %s",fname.Data());
215      return 1;
216    }
217
218   fFile->SetCompressionLevel(fCompressionLevel);
219   
220   AliRunLoader* rg = GetRunLoader();
221   if (rg == 0x0)
222    {
223      Error("OpenFile","Can not find Run-Loader in folder.");
224      return 2;
225    }
226   Int_t evno = rg->GetEventNumber();
227   
228   fDirectory = AliLoader::ChangeDir(fFile,evno);
229   if (fDirectory == 0x0)
230    {
231      Error("OpenFile","Can not chage fDirectory in file %s.",fFile->GetName());
232      return 3; 
233    }
234   return 0;
235 }
236 /*****************************************************************************/ 
237
238 void AliDataLoader::Unload()
239 {
240  //unloads main data -  shortcut method 
241   GetBaseLoader(0)->Unload();
242 }
243 /*****************************************************************************/ 
244
245 void AliDataLoader::UnloadAll()
246 {
247 //Unloads all data and tasks
248  TIter next(fBaseLoaders);
249  AliBaseLoader* bl;
250  while ((bl = (AliBaseLoader*)next()))
251   {
252     bl->Unload();
253   }
254 }
255 /*****************************************************************************/ 
256
257 Int_t AliDataLoader::Reload()
258 {
259  //Unloads and loads data again
260  if ( fFile == 0x0 ) return 0;
261    
262  TBits loaded(fBaseLoaders->GetEntries());  
263  TIter next(fBaseLoaders);
264  AliBaseLoader* bl;
265
266  Int_t i = 0;
267  while ((bl = (AliBaseLoader*)next()))
268   {
269     if (bl->IsLoaded())
270      {
271        loaded.SetBitNumber(i++,kTRUE);
272        bl->Unload();
273      }
274   }
275  
276  Int_t retval;
277  i = 0;  
278  next.Reset();
279  while ((bl = (AliBaseLoader*)next()))
280   {
281     if (loaded.TestBitNumber(i++))
282      {
283        retval = bl->Load(fFileOption);
284        if (retval) 
285         {
286          Error("Reload","Error occur while loading %s",bl->GetName());
287          return retval;
288         }
289      }
290   }
291  
292
293  return 0;
294  }
295 /*****************************************************************************/ 
296 Int_t AliDataLoader::WriteData(Option_t* opt)
297 {
298 //Writes primary data ==  first BaseLoader
299   if (GetDebug())
300    Info("WriteData","Writing %s container for %s data. Option is %s.",
301           GetBaseLoader(0)->GetName(),GetName(),opt);
302   return GetBaseLoader(0)->WriteData(opt);
303 }
304 /*****************************************************************************/ 
305
306 Int_t AliDataLoader::Load(Option_t* opt)
307 {
308 //Writes primary data ==  first BaseLoader
309   return GetBaseLoader(0)->Load(opt);
310 }
311 /*****************************************************************************/ 
312
313 Int_t  AliDataLoader::SetEventFolder(TFolder* eventfolder)
314 {
315  //sets the event folder
316  if (eventfolder == 0x0)
317   {
318     Error("SetEventFolder","Stupid joke. Argument is NULL");
319     return 1;
320   }
321  if (GetDebug()) 
322    Info("SetFolder","name = %s Setting Event Folder named %s.",
323          GetName(),eventfolder->GetName());
324
325  fEventFolder = eventfolder;
326  return 0;
327 }
328 /*****************************************************************************/ 
329
330 Int_t  AliDataLoader::SetFolder(TFolder* folder)
331 {
332
333  if (folder == 0x0)
334   {
335     Error("SetFolder","Stupid joke. Argument is NULL");
336     return 1;
337   }
338  
339  if (GetDebug()) Info("SetFolder","name = %s Setting folder named %s.",GetName(),folder->GetName());
340  
341  fFolder = folder;
342  TIter next(fBaseLoaders);
343  AliBaseLoader* bl;
344
345  while ((bl = (AliBaseLoader*)next()))
346   {
347    bl->SetDataLoader(this);
348   }  
349
350  return 0;
351 }
352 /******************************************************************/
353
354 TFolder* AliDataLoader::GetEventFolder()
355 {
356 //get EVENT folder (data that are changing from event to event, even in single run)
357   if (GetDebug()) Info("GetEventFolder","EF = %#x");
358   return fEventFolder;
359 }
360 /*****************************************************************************/ 
361
362 AliRunLoader* AliDataLoader::GetRunLoader()
363 {
364 //gets the run-loader from event folder
365   AliRunLoader* rg = 0x0;
366   TFolder* ef = GetEventFolder();
367   if (ef == 0x0)
368    {
369      Error("GetRunLoader","Can not get event folder.");
370      return 0;
371    }
372   rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::fgkRunLoaderName));
373   return rg;
374 }
375
376 /*****************************************************************************/ 
377 void AliDataLoader::CloseFile()
378 {
379   //closes file
380   TIter next(fBaseLoaders);
381   AliBaseLoader* bl;
382   while ((bl = (AliBaseLoader*)next()))
383    {
384      if (bl->IsLoaded()) return;
385    }
386   
387   if (GetDebug())
388     Info("CloseFile","Closing and deleting (object) file.");
389     
390   delete fFile;
391   fFile = 0x0;
392   fDirectory = 0x0;
393 }
394 /*****************************************************************************/ 
395
396 void AliDataLoader::Clean()
397 {
398   //Cleans main data
399   GetBaseLoader(0)->Clean();
400 }  
401 /*****************************************************************************/ 
402
403 void AliDataLoader::CleanAll()
404 {
405   //Cleans all folders and tasks
406   TIter next(fBaseLoaders);
407   AliBaseLoader* bl;
408   while ((bl = (AliBaseLoader*)next()))
409    {
410       bl->Clean();
411    }
412 }
413 /*****************************************************************************/ 
414
415 void AliDataLoader::SetFileNameSuffix(const TString& suffix)
416 {
417   //adds the suffix before ".root", 
418   //e.g. TPC.Digits.root -> TPC.DigitsMerged.root
419   //made on Jiri Chudoba demand
420   if (GetDebug()) 
421    {
422      Info("SetFileNameSuffix","suffix=%s",suffix.Data());
423      Info("SetFileNameSuffix","   Digits File Name before: %s",fFileName.Data());
424    }
425    
426   static TString dotroot(".root");
427   const TString& suffixdotroot = suffix + dotroot;
428   fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
429
430   if (GetDebug()) 
431     Info("SetDigitsFileNameSuffix","                    after : %s",fFileName.Data());
432 }
433 /*****************************************************************************/ 
434
435 Bool_t AliDataLoader::CheckReload()
436 {
437 //checks if we have to reload given file
438  if (fFile == 0x0) return kFALSE;
439  TString tmp = SetFileOffset(fFileName);
440  if (tmp.CompareTo(fFile->GetName())) return kTRUE;  //file must be reloaded
441  return  kFALSE;
442 }
443 /*****************************************************************************/ 
444
445 const TString AliDataLoader::SetFileOffset(const TString& fname)
446 {
447
448 //return fname;
449   Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
450   if (offset < 1) return fname;
451
452   TString soffset;
453   soffset += offset;//automatic conversion to string
454   TString dotroot(".root");
455   const TString& offfsetdotroot = offset + dotroot;
456   TString out = fname;
457   out = out.ReplaceAll(dotroot,offfsetdotroot);
458   if (GetDebug()) Info("SetFileOffset","in=%s  out=%s.",fname.Data(),out.Data());
459   return out;
460
461 }
462 /*****************************************************************************/ 
463
464 void AliDataLoader::SetFileOption(Option_t* newopt)
465 {
466   //sets file option
467   if (fFileOption.CompareTo(newopt) == 0) return;
468   fFileOption = newopt;
469   Reload();
470 }
471 /*****************************************************************************/ 
472
473 void AliDataLoader::SetCompressionLevel(Int_t cl)
474 {
475 //sets comression level for data defined by di
476   fCompressionLevel = cl;
477   if (fFile) fFile->SetCompressionLevel(cl);
478 }
479 /*****************************************************************************/ 
480
481 Int_t AliDataLoader::GetDebug() const 
482
483   //it is not inline bacause AliLoader.h includes AliDataLoaer.h 
484   //and there is circular depenedence
485  return AliLoader::GetDebug();
486 }
487 /*****************************************************************************/ 
488
489 void AliDataLoader::MakeTree()
490 {
491   AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
492   if (tl == 0x0)
493    {
494      Error("MakeTree","Can not make a tree because main base loader is not a tree loader");
495      return;
496    }
497   tl->MakeTree();
498 }
499 /*****************************************************************************/ 
500
501 Bool_t AliDataLoader::IsFileWritable() const
502 {
503 //returns true if file is writable
504  return (fFile)?fFile->IsWritable():kFALSE;
505 }
506 /*****************************************************************************/ 
507
508 Bool_t AliDataLoader::IsFileOpen() const
509 {
510 //returns true if file is writable
511  return (fFile)?fFile->IsOpen():kFALSE;
512 }
513 /*****************************************************************************/ 
514
515 Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
516 {
517 //Checks if passed option is contrary with file open option 
518 //which is passed option "writable" and existing option not wriable
519 //in reverse case it is no harm so it is NOT contrary
520   if (fFile == 0x0) return kFALSE; //file is not opened - no problem
521   
522   if ( ( AliLoader::IsOptionWritable(option)      == kTRUE  ) &&     // passed option is writable and 
523        ( AliLoader::IsOptionWritable(fFileOption) == kFALSE )    )   // existing one is not writable
524     {
525       return kTRUE;
526     }
527
528   return kFALSE;
529 }
530 /*****************************************************************************/ 
531 void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
532 {
533 //Adds a base loader to lits of base loaders managed by this data loader
534 //Managed data/task will be stored in proper root directory,
535 //and posted to 
536 // - in case of tree/object - data folder connected with detector associated with this data loader
537 // - in case of task - parental task which defined in this AliTaskLoader 
538
539  if (bl == 0x0)
540   {
541     Warning("AddBaseLoader","Pointer is null.");
542     return;
543   }
544  
545  TObject* obj = fBaseLoaders->FindObject(bl->GetName());
546  if (obj)
547   {
548     Error("AddBaseLoader","Can not add this base loader.");
549     Error("AddBaseLoader","There exists already base loader which manages data named %s for this detector.");
550     return;
551   }
552  
553  
554  fBaseLoaders->Add(bl);
555 }
556
557 /*****************************************************************************/ 
558
559 AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
560 {
561   return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
562 }
563 /*****************************************************************************/ 
564
565 AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
566 {
567  return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
568 }
569 /*****************************************************************************/ 
570
571 TTree* AliDataLoader::Tree() const
572 {
573 //returns tree from the main base loader
574 //it is just shortcut method for comfort of user
575 //main storage object does not have to be Tree  - 
576 //that is why first we need to check if it is a TreeLoader 
577  AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
578  if (tl == 0x0) return 0x0;
579  return tl->Tree();
580 }
581 /*****************************************************************************/ 
582
583 void  AliDataLoader::SetDirName(TString& dirname)
584 {
585   if (GetDebug()>9) Info("SetDirName","FileName before %s",fFileName.Data());
586
587   Int_t n = fFileName.Last('/');
588
589   if (GetDebug()>9) Info("SetDirName","Slash found on pos %d",n);
590
591   if (n > 0) fFileName = fFileName.Remove(0,n+1);
592
593   if (GetDebug()>9) Info("SetDirName","Core FileName %s",fFileName.Data());
594
595   fFileName = dirname + "/" + fFileName;
596
597   if (GetDebug()>9) Info("SetDirName","FileName after %s",fFileName.Data());
598 }
599 /*****************************************************************************/ 
600 AliObjectLoader* AliDataLoader::GetBaseDataLoader()
601 {
602  return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
603 }
604 /*****************************************************************************/ 
605 AliTaskLoader* AliDataLoader::GetBaseTaskLoader()
606 {
607  return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kTask));
608 }
609 /*****************************************************************************/ 
610 AliBaseLoader* AliDataLoader::GetBaseQALoader()
611 {
612   return GetBaseLoader(kQA);
613 }
614 /*****************************************************************************/ 
615 AliTaskLoader* AliDataLoader::GetBaseQATaskLoader()
616 {
617 //returns pointer to QA base loader
618  return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kQATask));
619 }
620 /*****************************************************************************/ 
621 void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
622 {
623 //sets data base loader
624   if (bl == 0x0)
625    {
626      Error("SetBaseDataLoader","Parameter is null");
627      return;
628    }
629   if (GetBaseDataLoader()) delete GetBaseDataLoader();
630   fBaseLoaders->AddAt(bl,kData);
631 }
632 /*****************************************************************************/ 
633 void AliDataLoader::SetBaseTaskLoader(AliTaskLoader* bl)
634 {
635 //sets Task base loader
636   if (bl == 0x0)
637    {
638      Error("SetBaseTaskLoader","Parameter is null");
639      return;
640    }
641   if (GetBaseTaskLoader()) delete GetBaseTaskLoader();
642   fBaseLoaders->AddAt(bl,kTask);
643 }
644 /*****************************************************************************/ 
645 void AliDataLoader::SetBaseQALoader(AliBaseLoader* bl)
646 {
647 //sets QA base loader
648   if (bl == 0x0)
649    {
650      Error("SetBaseQALoader","Parameter is null");
651      return;
652    }
653   if (GetBaseQALoader()) delete GetBaseQALoader();
654   fBaseLoaders->AddAt(bl,kQA);
655 }
656 /*****************************************************************************/ 
657 void AliDataLoader::SetBaseQATaskLoader(AliTaskLoader* bl)
658 {
659 //sets QA Task base loader
660   if (bl == 0x0)
661    {
662      Error("SetBaseQATaskLoader","Parameter is null");
663      return;
664    }
665   if (GetBaseQATaskLoader()) delete GetBaseQATaskLoader();
666   fBaseLoaders->AddAt(bl,kQATask);
667 }
668
669 /*****************************************************************************/ 
670 /*****************************************************************************/ 
671 /*****************************************************************************/ 
672 //__________________________________________
673 ///////////////////////////////////////////////////////////////////////////////
674 //                                                                           //
675 //  class AliBaseLoader                                                      //
676 //                                                                           //
677 //                                                                           //
678 ///////////////////////////////////////////////////////////////////////////////
679 ClassImp(AliBaseLoader)
680
681 AliBaseLoader::AliBaseLoader():
682  fIsLoaded(kFALSE),
683  fStoreInTopOfFile(kFALSE),
684  fDoNotReload(kFALSE),
685  fDataLoader(0x0)
686 {
687 }
688 /*****************************************************************************/ 
689
690 AliBaseLoader::AliBaseLoader(const TString& name,  AliDataLoader* dl, Bool_t storeontop):
691  TNamed(name,name+" Base Loader"),
692  fIsLoaded(kFALSE),
693  fStoreInTopOfFile(storeontop),
694  fDoNotReload(storeontop),//if stored on top of file - this object is loaded ones pe
695  fDataLoader(dl)
696 {
697 }
698
699 /*****************************************************************************/ 
700
701 Int_t AliBaseLoader::Load(Option_t* opt)
702 {
703   if (GetDebug())
704     Info("Load","data type = %s, option = %s",GetName(),opt);
705
706   if (Get())
707    {
708       Warning("Load","Data <<%s>> are already loaded. Use ReloadData to force reload. Nothing done",GetName());
709       return 0;
710    }
711   
712   Int_t retval;
713   
714   if (GetDataLoader()->IsFileOpen() == kTRUE)
715    {
716      if (GetDataLoader()->IsOptionContrary(opt) == kTRUE)
717        {
718          Error("Load","Data Type %s, Container Name %s", GetDataLoader()->GetName(),GetName());
719          Error("Load","File was already opened in READ-ONLY mode, while now WRITEABLE access is requested.");
720          Error("Load","Use AliDataLoader::SetOption to enforce change of access mode OR");
721          Error("Load","Load previosly loaded data with coherent option.");
722          return 10;
723        }
724    }
725   else
726    {
727      retval = GetDataLoader()->OpenFile(opt);
728      if (retval) 
729       {
730         Error("Load","Error occured while opening <<%s>> file",GetName());
731         return retval;
732       }
733    }
734   //if file is recreated there is no sense to search for data to post and get Error message
735   if (AliLoader::TestFileOption(opt) == kFALSE)
736    {
737     AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(this);
738     if (tl) tl->MakeTree();
739     fIsLoaded = kTRUE;
740     return 0;
741    }
742
743   retval = Post();
744   if (retval)
745    {
746     Error("Load","Error occured while posting %s from file to folder.",GetName());
747     return retval;
748    }
749   
750   fIsLoaded = kTRUE;
751   return 0;
752 }
753 /*****************************************************************************/ 
754
755 Int_t AliBaseLoader::Post()
756 {
757 //Posts data container to proper folders
758
759   if ( GetDirectory() == 0x0)
760    {
761      Error("Post","%s directory is NULL. Load before.",GetDataLoader()->GetName());
762      return 2; 
763    }
764   
765   TObject* data = GetFromDirectory(fName);
766   if(data)
767    {
768      //if such an obejct already exists - remove it first
769      return Post(data);
770    }
771   else
772    {
773     //check if file is in update mode
774     Int_t fileupdate = GetDataLoader()->GetFileOption().CompareTo("update",TString::kIgnoreCase);
775     if ( fileupdate == 0)
776      { //if it is, it is normal that there is no data yet
777        if (GetDebug())
778         {
779          Info("Post","Can not find %s in file %s (file is opened in UPDATE mode).",
780                GetName(),GetDataLoader()->GetFile()->GetName());
781         }
782      }
783     else
784      {
785         Warning("Post","Can not find %s in file %s", GetName(),GetDataLoader()->GetFile()->GetName());
786      }
787    }
788   return 0;
789 }
790 /*****************************************************************************/ 
791
792 Int_t AliBaseLoader::Post(TObject* data)
793 {
794 //Posts data container to proper folders
795  if (data == 0x0)
796   {
797     Error("Post","Pointer to object is NULL");
798     return 1;
799   }
800  TObject* obj = Get();
801  if (data == obj)
802   {
803     if (GetDebug()) Warning("Post","This object was already posted.");
804     return 0;
805   }
806  if (obj)
807   {
808     Warning("PostData","Object named %s already exitsts in data folder. Removing it",GetName());
809     Clean();
810   }
811  return AddToBoard(data);
812 }
813 /*****************************************************************************/ 
814
815 Int_t AliBaseLoader::WriteData(Option_t* opt)
816 {
817 //Writes data defined by di object
818 //opt might be "OVERWRITE" in case of forcing overwriting
819   if (GetDebug()) 
820     Info("WriteData","Writing %s container for %s data. Option is %s.",
821           GetName(),GetDataLoader()->GetName(),opt);
822   
823   TObject *data = Get();
824   if(data == 0x0)
825    {//did not get, nothing to write
826      Warning("WriteData","Tree named %s not found in folder. Nothing to write.",GetName());
827      return 0;
828    }
829   
830   //check if file is opened
831   if (GetDirectory() == 0x0)
832    { 
833      //if not try to open
834      GetDataLoader()->SetFileOption("UPDATE");
835      if (GetDataLoader()->OpenFile("UPDATE"))
836       {  
837         //oops, can not open the file, give an error message and return error code
838         Error("WriteData","Can not open hits file. %s ARE NOT WRITTEN",GetName());
839         return 1;
840       }
841    }
842
843   if (GetDataLoader()->IsFileWritable() == kFALSE)
844    {
845      Error("WriteData","File %s is not writable",GetDataLoader()->GetFileName().Data());
846      return 2;
847    }
848   
849   GetDirectory()->cd(); //set the proper directory active
850
851   //see if hits container already exists in this (root) directory
852   TObject* obj = GetFromDirectory(GetName());
853   if (obj)
854    { //if they exist, see if option OVERWRITE is used
855      const char *oOverWrite = strstr(opt,"OVERWRITE");
856      if(!oOverWrite)
857       {//if it is not used -  give an error message and return an error code
858         Error("WriteData","Tree already exisists. Use option \"OVERWRITE\" to overwrite previous data");
859         return 3;
860       }
861    }
862   
863   if (GetDebug()) Info("WriteData","DataName = %s, opt = %s, data object name = %s",
864                    GetName(),opt,data->GetName());
865   if (GetDebug()) Info("WriteData","File Name = %s, Directory Name = %s Directory's File Name = %s",
866                    GetDataLoader()->GetFile()->GetName(),GetDirectory()->GetName(),
867                    GetDirectory()->GetFile()->GetName());
868   
869   if (GetDebug()) Info("WriteData","Writing data");
870   data->Write(0,TObject::kOverwrite);
871
872   fIsLoaded = kTRUE;  // Just to ensure flag is on. Object can be posted manually to folder structure, not using loader.
873
874   return 0;
875  
876 }
877 /*****************************************************************************/ 
878
879 Int_t AliBaseLoader::Reload()
880 {
881 //Unloads and loads datat again - if loaded before
882  if (IsLoaded())
883   {
884     Unload();
885     return Load(GetDataLoader()->GetFileOption());
886   }
887   return 0;
888 }
889 /*****************************************************************************/ 
890
891 void AliBaseLoader::Clean()
892 {
893 //removes objects from folder/task
894   if (GetDebug()) Info("Clean","%s %s",GetName(),GetDataLoader()->GetName());
895   TObject* obj = Get();
896   if(obj)
897    { 
898      if (GetDebug()) 
899        Info("Clean","cleaning %s.",GetName());
900      RemoveFromBoard(obj);
901      delete obj;
902    }
903 }
904 /*****************************************************************************/ 
905
906 void AliBaseLoader::Unload()
907 {
908   Clean();
909   fIsLoaded = kFALSE;
910   GetDataLoader()->CloseFile();
911 }
912 /*****************************************************************************/ 
913 AliDataLoader* AliBaseLoader::GetDataLoader() const
914 {
915  if (fDataLoader == 0x0) 
916   {
917     Fatal("GetDataLoader","Pointer to Data Loader is NULL");
918   }
919  return fDataLoader;
920 }
921 /*****************************************************************************/ 
922
923 Int_t AliBaseLoader::GetDebug() const 
924
925  return (Int_t)AliLoader::fgDebug;
926 }
927
928 TDirectory* AliBaseLoader::GetDirectory()
929 {
930  // returnd TDirectory where data are to be saved
931  //if fStoreInTopOfFile flag is true - returns pointer to file
932   return (fStoreInTopOfFile)?GetDataLoader()->GetFile():GetDataLoader()->GetDirectory();
933 }
934 /*****************************************************************************/ 
935 /*****************************************************************************/ 
936 /*****************************************************************************/ 
937 //__________________________________________
938 ///////////////////////////////////////////////////////////////////////////////
939 //                                                                           //
940 //  class AliObjectLoader                                                      //
941 //                                                                           //
942 //                                                                           //
943 ///////////////////////////////////////////////////////////////////////////////
944
945 ClassImp(AliObjectLoader)
946
947 AliObjectLoader::AliObjectLoader(const TString& name, AliDataLoader* dl, Bool_t storeontop):
948  AliBaseLoader(name,dl,storeontop)
949 {
950 //constructor
951 }
952 /*****************************************************************************/ 
953
954 TFolder* AliObjectLoader::GetFolder() const
955 {
956   TFolder* df = GetDataLoader()->GetFolder();
957   if (df == 0x0)
958    {
959      Fatal("GetFolder","Data Folder is NULL");
960    }
961   return df;
962 }
963 /*****************************************************************************/ 
964
965 void AliObjectLoader::RemoveFromBoard(TObject* obj)
966 {
967   GetFolder()->Remove(obj);
968 }
969 /*****************************************************************************/ 
970 Int_t AliObjectLoader::AddToBoard(TObject* obj)
971 {
972   GetFolder()->Add(obj);
973   return 0;
974 }
975 /*****************************************************************************/ 
976
977 TObject* AliObjectLoader::Get() const
978 {
979   return (GetFolder()) ? GetFolder()->FindObject(GetName()) : 0x0;
980 }
981
982 /*****************************************************************************/ 
983 /*****************************************************************************/ 
984 /*****************************************************************************/ 
985 //__________________________________________
986 ///////////////////////////////////////////////////////////////////////////////
987 //                                                                           //
988 //  class AliTreeLoader                                                      //
989 //                                                                           //
990 //                                                                           //
991 ///////////////////////////////////////////////////////////////////////////////
992
993 ClassImp(AliTreeLoader)
994
995 AliTreeLoader::AliTreeLoader(const TString& name, AliDataLoader* dl,Bool_t storeontop):
996  AliObjectLoader(name,dl,storeontop)
997 {
998 //constructor
999 }
1000
1001 /*****************************************************************************/ 
1002
1003 Int_t AliTreeLoader::WriteData(Option_t* opt)
1004 {
1005 //Writes data defined by di object
1006 //opt might be "OVERWRITE" in case of forcing overwriting
1007
1008   if (GetDebug()) 
1009     Info("WriteData","Writing %s container for %s data. Option is %s.",
1010           GetName(),GetDataLoader()->GetName(),opt);
1011
1012   TObject *data = Get();
1013   if(data == 0x0)
1014    {//did not get, nothing to write
1015      Warning("WriteData","Tree named %s not found in folder. Nothing to write.",GetName());
1016      return 0;
1017    }
1018   
1019   //check if file is opened
1020   if (GetDirectory() == 0x0)
1021    { 
1022      //if not try to open
1023      GetDataLoader()->SetFileOption("UPDATE");
1024      if (GetDataLoader()->OpenFile("UPDATE"))
1025       {  
1026         //oops, can not open the file, give an error message and return error code
1027         Error("WriteData","Can not open hits file. %s ARE NOT WRITTEN",GetName());
1028         return 1;
1029       }
1030    }
1031
1032   if (GetDataLoader()->IsFileWritable() == kFALSE)
1033    {
1034      Error("WriteData","File %s is not writable",GetDataLoader()->GetFileName().Data());
1035      return 2;
1036    }
1037   
1038   GetDirectory()->cd(); //set the proper directory active
1039
1040   //see if hits container already exists in this (root) directory
1041   TObject* obj = GetFromDirectory(GetName());
1042   if (obj)
1043    { //if they exist, see if option OVERWRITE is used
1044      const char *oOverWrite = strstr(opt,"OVERWRITE");
1045      if(!oOverWrite)
1046       {//if it is not used -  give an error message and return an error code
1047         Error("WriteData","Tree already exisists. Use option \"OVERWRITE\" to overwrite previous data");
1048         return 3;
1049       }
1050    }
1051   
1052   if (GetDebug()) Info("WriteData","DataName = %s, opt = %s, data object name = %s",
1053                    GetName(),opt,data->GetName());
1054   if (GetDebug()) Info("WriteData","File Name = %s, Directory Name = %s Directory's File Name = %s",
1055                    GetDataLoader()->GetFile()->GetName(),GetDirectory()->GetName(),
1056                    GetDirectory()->GetFile()->GetName());
1057   
1058   //if a data object is a tree set the directory
1059   TTree* tree = dynamic_cast<TTree*>(data);
1060   if (tree) tree->SetDirectory(GetDirectory()); //forces setting the directory to this directory (we changed dir few lines above)
1061   
1062   if (GetDebug()) Info("WriteData","Writing tree");
1063   data->Write(0,TObject::kOverwrite);
1064
1065   fIsLoaded = kTRUE;  // Just to ensure flag is on. Object can be posted manually to folder structure, not using loader.
1066   
1067   return 0;
1068  
1069 }
1070 /*****************************************************************************/ 
1071
1072 void AliTreeLoader::MakeTree()
1073 {
1074 //this virtual method creates the tree for hits in the file
1075   if (Tree()) 
1076    {
1077     if (GetDebug()) 
1078       Info("MakeTree","name = %s, Data Name = %s Tree already exists.",
1079             GetName(),GetDataLoader()->GetName());
1080     return;//tree already made 
1081    }
1082   if (GetDebug()) 
1083     Info("MakeTree","Making Tree named %s.",GetName());
1084    
1085   TString dtypename(GetDataLoader()->GetName());
1086   TTree* tree = new TTree(GetName(), dtypename + " Container"); //make a tree
1087   if (tree == 0x0)
1088    {
1089      Error("MakeTree","Can not create %s tree.",GetName());
1090      return;
1091    }
1092   tree->SetAutoSave(1000000000); //no autosave
1093   GetFolder()->Add(tree);
1094   WriteData("OVERWRITE");//write tree to the file
1095 }
1096
1097
1098 /*****************************************************************************/ 
1099 /*****************************************************************************/ 
1100 /*****************************************************************************/ 
1101 //__________________________________________
1102 ///////////////////////////////////////////////////////////////////////////////
1103 //                                                                           //
1104 //  class AliTaskLoader                                                      //
1105 //                                                                           //
1106 //                                                                           //
1107 ///////////////////////////////////////////////////////////////////////////////
1108
1109 ClassImp(AliTaskLoader)
1110
1111 AliTaskLoader::AliTaskLoader(const TString& name, AliDataLoader* dl, TTask* parentaltask, Bool_t storeontop):
1112  AliBaseLoader(name,dl,storeontop),
1113  fParentalTask(parentaltask)
1114 {
1115 //constructor
1116 }
1117
1118 /*****************************************************************************/ 
1119
1120 void AliTaskLoader::RemoveFromBoard(TObject* obj)
1121 {
1122   GetParentalTask()->GetListOfTasks()->Remove(obj);
1123 }
1124 /*****************************************************************************/ 
1125
1126 Int_t AliTaskLoader::AddToBoard(TObject* obj)
1127 {
1128   TTask* task = dynamic_cast<TTask*>(obj);
1129   if (task == 0x0)
1130    {
1131      Error("AddToBoard","To TTask board can be added only tasks.");
1132      return 1;
1133    }
1134   GetParentalTask()->Add(task);
1135   return 0;
1136 }
1137 /*****************************************************************************/ 
1138
1139 TObject* AliTaskLoader::Get() const
1140 {
1141   return (GetParentalTask()) ? GetParentalTask()->GetListOfTasks()->FindObject(GetName()) : 0x0;
1142 }
1143 /*****************************************************************************/ 
1144
1145 TTask* AliTaskLoader::GetParentalTask() const
1146 {
1147 //returns parental tasks for this task
1148   return fParentalTask;
1149 }
1150
1151 /*****************************************************************************/ 
1152
1153 /*****************************************************************************/ 
1154 /*****************************************************************************/ 
1155 /*****************************************************************************/ 
1156
1157