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