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