]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEER/AliDataLoader.cxx
When creating the snapshot, get alignment objects.
[u/mrichter/AliRoot.git] / STEER / STEER / AliDataLoader.cxx
CommitLineData
88cb7938 1/////////////////////////////////////////////////////////////////////////////////////////////
2// //
3// class AliDataLoader //
4// //
5// Container of all data needed for full //
6// description of each data type //
7// (Hits, Kine, ...) //
8// //
9// Each data loader has a basic standard setup of BaseLoaders //
10// which can be identuified by indexes (defined by EStdBasicLoaders) //
11// Data managed by these standard base loaders has fixed naming convention //
12// e.g. - tree with hits is always named TreeH //
13// (defined in AliLoader::fgkDefaultHitsContainerName) //
88cb7938 14// //
15// EStdBasicLoaders idx Object Type Description //
16// kData 0 TTree or TObject main data itself (hits,digits,...) //
88cb7938 17// //
18// //
19// User can define and add more basic loaders even Run Time. //
20// Caution: in order to save information about added base loader //
21// user must rewrite Run Loader to galice.file, overwriting old setup //
22// //
23/////////////////////////////////////////////////////////////////////////////////////////////
24
a9bbb414 25/* $Id$ */
26
a9bbb414 27#include "AliDataLoader.h"
88cb7938 28#include "AliRunLoader.h"
c93255fe 29#include "AliLoader.h"
30#include "AliObjectLoader.h"
a9bbb414 31#include "AliTreeLoader.h"
c93255fe 32#include "AliLog.h"
33
34#include <TFile.h>
35#include <TROOT.h>
36#include <TString.h>
37#include <TBits.h>
88cb7938 38
39ClassImp(AliDataLoader)
40
a9bbb414 41//______________________________________________________________________________
88cb7938 42AliDataLoader::AliDataLoader():
43 fFileName(0),
44 fFile(0x0),
45 fDirectory(0x0),
46 fFileOption(),
47 fCompressionLevel(2),
18b43626 48 fNEventsPerFile(0),
88cb7938 49 fBaseLoaders(0x0),
88cb7938 50 fEventFolder(0x0),
51 fFolder(0x0)
52{
53
54}
88cb7938 55
a9bbb414 56//______________________________________________________________________________
57AliDataLoader::AliDataLoader(const char* filename, const char* contname,
58 const char* name, Option_t* opt):
88cb7938 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)),
88cb7938 67 fEventFolder(0x0),
68 fFolder(0x0)
69{
a9bbb414 70 //constructor
71 // creates a 0 loader, depending on option, default "T" is specialized loader for trees
72 // else standard object loader
73 // trees needs special care, becouse we need to set dir before writing
21bf7095 74 AliDebug(1, Form("File name is %s",fFileName.Data()));
a9bbb414 75
88cb7938 76 TString option(opt);
77 AliBaseLoader* bl;
78 if (option.CompareTo("T",TString::kIgnoreCase) == 0)
79 bl = new AliTreeLoader(contname,this);
80 else
81 bl = new AliObjectLoader(contname,this);
82 fBaseLoaders->AddAt(bl,kData);
83
84}
a9bbb414 85
a9bbb414 86//______________________________________________________________________________
88cb7938 87AliDataLoader::~AliDataLoader()
88{
a9bbb414 89 //
90 //dtor
91 //
92 UnloadAll();
88cb7938 93}
88cb7938 94
a9bbb414 95//______________________________________________________________________________
88cb7938 96Int_t AliDataLoader::SetEvent()
97{
a9bbb414 98 //
99 // The same that GetEvent but do not post data to folders
100 //
101 AliRunLoader* rl = GetRunLoader();
102 if (rl == 0x0)
103 {
104 AliError("Can not get RunGettr");
105 return 1;
106 }
107
108 Int_t evno = rl->GetEventNumber();
109
110 TIter next(fBaseLoaders);
111 AliBaseLoader* bl;
112 while ((bl = (AliBaseLoader*)next()))
113 {
114 if (bl->DoNotReload() == kFALSE) bl->Clean();
115 }
116
117 if(fFile)
118 {
119 if (CheckReload())
120 {
121 delete fFile;
122 fFile = 0x0;
123 AliDebug(1, Form("Reloading new file. File opt is %s",fFileOption.Data()));
124 OpenFile(fFileOption);
125 }
126
127 fDirectory = AliLoader::ChangeDir(fFile,evno);
128 if (fDirectory == 0x0)
129 {
130 AliError(Form("Can not chage directory in file %s",fFile->GetName()));
131 return 1;
132 }
133 }
134 return 0;
88cb7938 135}
88cb7938 136
a9bbb414 137//______________________________________________________________________________
88cb7938 138Int_t AliDataLoader::GetEvent()
139{
a9bbb414 140 // posts all loaded data from files to White Board
141 // event number is defined in RunLoader
142 //
143 //returns:
144 // 0 - in case of no error
145 // 1 - event not found
146 //
147 //for each base laoder post, if was loaded before GetEvent
148
149 //call set event to switch to new directory in file
150
151
152 //post all data that were loaded before
153 // ->SetEvent does not call Unload, but only cleans White Board
154 // such IsLoaded flag stays untached
155
156 if ( AliLoader::TestFileOption(fFileOption) == kTRUE ) //if file is read or update mode try to post
157 { //in other case there is no sense to post: file is new
158 TIter nextbl(fBaseLoaders);
159 AliBaseLoader* bl;
160 while ((bl = (AliBaseLoader*)nextbl()))
161 {
162 if (bl->IsLoaded())
163 {
164 if (bl->DoNotReload() == kFALSE) bl->Post();
165 }
166 }
167 }
168 return 0;
88cb7938 169}
88cb7938 170
a9bbb414 171//______________________________________________________________________________
88cb7938 172Int_t AliDataLoader::OpenFile(Option_t* opt)
173{
a9bbb414 174 //Opens file named 'filename', and assigns pointer to it to 'file'
175 //jumps to fDirectoryectory corresponding to current event and stores the pointer to it in 'fDirectory'
176 //option 'opt' is passed to TFile::Open
88cb7938 177 if (fFile)
a9bbb414 178 {
179 if(fFile->IsOpen() == kTRUE)
180 {
181 AliWarning(Form(" File %s already opened. First close it.",fFile->GetName()));
182 return 0;
183 }
184 else
185 {
186 AliWarning(Form("Pointer to file %s is not null, but file is not opened",
187 fFile->GetName()));
188 delete fFile;
189 fFile = 0x0;
190 }
191 }
88cb7938 192
193 TString fname(SetFileOffset(fFileName));
194
195 fFile = (TFile *)(gROOT->GetListOfFiles()->FindObject(fname));
196 if (fFile)
a9bbb414 197 {
198 if(fFile->IsOpen() == kTRUE)
199 {
eeb1fb79 200 TString option1 = fFile->GetOption();
201 if (option1.CompareTo("read",TString::kIgnoreCase) == 0)
202 {
203 AliInfo(Form("File %s already opened in read mode.",fFile->GetName()));
204 }
205 else
206 {
207 TString option2 = opt;
208 if (option2.CompareTo("read",TString::kIgnoreCase) == 0)
209 {
210 AliInfo(Form("Open already opened file %s in read mode.",fFile->GetName()));
211 }
212 else {
213 AliWarning(Form("File %s already opened by sombody else. First close it.",
214 fFile->GetName()));
215 return 0;
216 }
217 }
a9bbb414 218 }
219 }
88cb7938 220
221 fFileOption = opt;
222 fFile = TFile::Open(fname,fFileOption);//open the file
223 if (fFile == 0x0)
a9bbb414 224 {//file is null
225 AliError(Form("Can not open file %s",fname.Data()));
226 return 1;
227 }
88cb7938 228 if (fFile->IsOpen() == kFALSE)
a9bbb414 229 {//file is null
230 AliError(Form("Can not open file %s",fname.Data()));
231 return 1;
232 }
233
9d072410 234 fFile->SetBit(TFile::kDevNull);
88cb7938 235 fFile->SetCompressionLevel(fCompressionLevel);
236
237 AliRunLoader* rg = GetRunLoader();
238 if (rg == 0x0)
a9bbb414 239 {
240 AliError("Can not find Run-Loader in folder.");
241 return 2;
242 }
88cb7938 243 Int_t evno = rg->GetEventNumber();
244
245 fDirectory = AliLoader::ChangeDir(fFile,evno);
246 if (fDirectory == 0x0)
a9bbb414 247 {
248 AliError(Form("Can not chage fDirectory in file %s.",fFile->GetName()));
249 return 3;
250 }
88cb7938 251 return 0;
252}
88cb7938 253
a9bbb414 254//______________________________________________________________________________
88cb7938 255void AliDataLoader::Unload()
256{
a9bbb414 257 //
258 //unloads main data - shortcut method
259 //
88cb7938 260 GetBaseLoader(0)->Unload();
261}
88cb7938 262
a9bbb414 263//______________________________________________________________________________
88cb7938 264void AliDataLoader::UnloadAll()
265{
a9bbb414 266 //
f21fc003 267 // Unloads all data
a9bbb414 268 //
269 if ( fFile == 0x0 ) return; //nothing loaded
270
271 TIter next(fBaseLoaders);
272 AliBaseLoader* bl;
273 while ((bl = (AliBaseLoader*)next()))
274 {
275 bl->Unload();
276 }
88cb7938 277}
88cb7938 278
a9bbb414 279
280//______________________________________________________________________________
88cb7938 281Int_t AliDataLoader::Reload()
282{
a9bbb414 283 //
284 // Unloads and loads data again
285 //
286 if ( fFile == 0x0 ) return 0;
287
288 TBits loaded(fBaseLoaders->GetEntries());
289 TIter next(fBaseLoaders);
290 AliBaseLoader* bl;
291
292 Int_t i = 0;
293 while ((bl = (AliBaseLoader*)next()))
294 {
295 if (bl->IsLoaded())
296 {
297 loaded.SetBitNumber(i++,kTRUE);
298 bl->Unload();
299 }
300 }
301
302 Int_t retval;
303 i = 0;
304 next.Reset();
305 while ((bl = (AliBaseLoader*)next()))
306 {
307 if (loaded.TestBitNumber(i++))
308 {
309 retval = bl->Load(fFileOption);
310 if (retval)
311 {
312 AliError(Form("Error occur while loading %s",bl->GetName()));
313 return retval;
314 }
315 }
316 }
317 return 0;
318}
88cb7938 319
88cb7938 320
a9bbb414 321//______________________________________________________________________________
88cb7938 322Int_t AliDataLoader::WriteData(Option_t* opt)
323{
a9bbb414 324 //
325 // Writes primary data == first BaseLoader
326 //
21bf7095 327 AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
328 GetBaseLoader(0)->GetName(),GetName(),opt));
88cb7938 329 return GetBaseLoader(0)->WriteData(opt);
330}
88cb7938 331
a9bbb414 332//______________________________________________________________________________
88cb7938 333Int_t AliDataLoader::Load(Option_t* opt)
334{
a9bbb414 335 //
336 // Writes primary data == first BaseLoader
337 //
88cb7938 338 return GetBaseLoader(0)->Load(opt);
339}
88cb7938 340
a9bbb414 341//______________________________________________________________________________
88cb7938 342Int_t AliDataLoader::SetEventFolder(TFolder* eventfolder)
343{
a9bbb414 344 //
345 // Sets the event folder
346 //
347 if (eventfolder == 0x0)
348 {
349 AliError("Stupid joke. Argument is NULL");
350 return 1;
351 }
352 AliDebug(1, Form("name = %s Setting Event Folder named %s.",
353 GetName(),eventfolder->GetName()));
354
355 fEventFolder = eventfolder;
356 return 0;
88cb7938 357}
88cb7938 358
a9bbb414 359
360//______________________________________________________________________________
88cb7938 361Int_t AliDataLoader::SetFolder(TFolder* folder)
362{
d0d4a6b3 363 // Sets the folder and the data loaders
a9bbb414 364 if (folder == 0x0)
365 {
366 AliError("Stupid joke. Argument is NULL");
88cb7938 367 return 1;
a9bbb414 368 }
369
370 AliDebug(1, Form("name = %s Setting folder named %s.",GetName(),folder->GetName()));
371
372 fFolder = folder;
373 TIter next(fBaseLoaders);
374 AliBaseLoader* bl;
375
376 while ((bl = (AliBaseLoader*)next()))
377 {
378 bl->SetDataLoader(this);
379 }
380
381 return 0;
88cb7938 382}
88cb7938 383
a9bbb414 384//______________________________________________________________________________
88cb7938 385TFolder* AliDataLoader::GetEventFolder()
386{
a9bbb414 387 //
388 // Get EVENT folder
389 // Data that are changing from event to event, even in single run
390 //
21bf7095 391 AliDebug(1, "EF = %#x");
88cb7938 392 return fEventFolder;
393}
88cb7938 394
a9bbb414 395//______________________________________________________________________________
88cb7938 396AliRunLoader* AliDataLoader::GetRunLoader()
397{
a9bbb414 398 //
399 // Gets the run-loader from event folder
400 //
88cb7938 401 AliRunLoader* rg = 0x0;
402 TFolder* ef = GetEventFolder();
403 if (ef == 0x0)
404 {
21bf7095 405 AliError("Can not get event folder.");
88cb7938 406 return 0;
407 }
024a7e64 408 rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::GetRunLoaderName()));
88cb7938 409 return rg;
410}
411
a9bbb414 412//______________________________________________________________________________
88cb7938 413void AliDataLoader::CloseFile()
414{
a9bbb414 415 //
416 // Closes file
417 //
88cb7938 418 TIter next(fBaseLoaders);
419 AliBaseLoader* bl;
420 while ((bl = (AliBaseLoader*)next()))
a9bbb414 421 {
422 if (bl->IsLoaded()) return;
423 }
88cb7938 424
1b62c7c8 425 AliDebug(1, "Closing (object) file.");
a9bbb414 426
1b62c7c8 427 if (fFile) {
428 fFile->Close("R");
48b634ff 429 delete fFile;
1b62c7c8 430 fFile = 0x0;
431 }
88cb7938 432 fDirectory = 0x0;
433}
88cb7938 434
a9bbb414 435
436//______________________________________________________________________________
88cb7938 437void AliDataLoader::Clean()
438{
a9bbb414 439 //
440 // Cleans main data
441 //
88cb7938 442 GetBaseLoader(0)->Clean();
a9bbb414 443}
88cb7938 444
a9bbb414 445//______________________________________________________________________________
88cb7938 446void AliDataLoader::CleanAll()
447{
a9bbb414 448 //
f21fc003 449 // Cleans all folders
a9bbb414 450 //
88cb7938 451 TIter next(fBaseLoaders);
452 AliBaseLoader* bl;
453 while ((bl = (AliBaseLoader*)next()))
a9bbb414 454 {
88cb7938 455 bl->Clean();
a9bbb414 456 }
88cb7938 457}
88cb7938 458
a9bbb414 459//______________________________________________________________________________
88cb7938 460void AliDataLoader::SetFileNameSuffix(const TString& suffix)
461{
a9bbb414 462 //
463 // adds the suffix before ".root",
464 // e.g. TPC.Digits.root -> TPC.DigitsMerged.root
465 // made on Jiri Chudoba demand
466 //
21bf7095 467 AliDebug(1, Form("suffix=%s",suffix.Data()));
468 AliDebug(1, Form(" Digits File Name before: %s",fFileName.Data()));
a9bbb414 469
eeb1fb79 470 static const TString dotroot(".root");
88cb7938 471 const TString& suffixdotroot = suffix + dotroot;
472 fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
a9bbb414 473
21bf7095 474 AliDebug(1, Form(" after : %s",fFileName.Data()));
88cb7938 475}
88cb7938 476
a9bbb414 477//______________________________________________________________________________
88cb7938 478Bool_t AliDataLoader::CheckReload()
479{
a9bbb414 480 //
481 // Checks if we have to reload given file
482 //
483 if (fFile == 0x0) return kFALSE;
484 TString tmp = SetFileOffset(fFileName);
485 if (tmp.CompareTo(fFile->GetName())) return kTRUE; //file must be reloaded
486 return kFALSE;
88cb7938 487}
88cb7938 488
a9bbb414 489//______________________________________________________________________________
88cb7938 490const TString AliDataLoader::SetFileOffset(const TString& fname)
491{
a9bbb414 492 //
493 // Return fname
494 //
88cb7938 495 Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
18b43626 496 if (fNEventsPerFile > 0) {
497 offset = GetRunLoader()->GetEventNumber()/fNEventsPerFile;
498 }
88cb7938 499 if (offset < 1) return fname;
a9bbb414 500
88cb7938 501 TString soffset;
502 soffset += offset;//automatic conversion to string
503 TString dotroot(".root");
504 const TString& offfsetdotroot = offset + dotroot;
505 TString out = fname;
506 out = out.ReplaceAll(dotroot,offfsetdotroot);
21bf7095 507 AliDebug(1, Form("in=%s out=%s.",fname.Data(),out.Data()));
88cb7938 508 return out;
88cb7938 509}
88cb7938 510
a9bbb414 511//______________________________________________________________________________
88cb7938 512void AliDataLoader::SetFileOption(Option_t* newopt)
513{
a9bbb414 514 //
515 // Sets file option
516 //
88cb7938 517 if (fFileOption.CompareTo(newopt) == 0) return;
518 fFileOption = newopt;
519 Reload();
520}
88cb7938 521
a9bbb414 522//______________________________________________________________________________
88cb7938 523void AliDataLoader::SetCompressionLevel(Int_t cl)
524{
a9bbb414 525 //
526 // Sets comression level for data defined by di
527 //
88cb7938 528 fCompressionLevel = cl;
529 if (fFile) fFile->SetCompressionLevel(cl);
530}
88cb7938 531
a9bbb414 532//______________________________________________________________________________
88cb7938 533void AliDataLoader::MakeTree()
534{
a9bbb414 535 //
d0d4a6b3 536 // Makes tree for the current data loader
a9bbb414 537 //
88cb7938 538 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
539 if (tl == 0x0)
540 {
21bf7095 541 AliError("Can not make a tree because main base loader is not a tree loader");
88cb7938 542 return;
543 }
544 tl->MakeTree();
545}
88cb7938 546
a9bbb414 547//______________________________________________________________________________
88cb7938 548Bool_t AliDataLoader::IsFileWritable() const
549{
a9bbb414 550 //
551 // Returns true if file is writable
552 //
553 return (fFile)?fFile->IsWritable():kFALSE;
88cb7938 554}
88cb7938 555
a9bbb414 556//______________________________________________________________________________
88cb7938 557Bool_t AliDataLoader::IsFileOpen() const
558{
a9bbb414 559 //
560 // Returns true if file is writable
561 //
562 return (fFile)?fFile->IsOpen():kFALSE;
88cb7938 563}
88cb7938 564
a9bbb414 565//______________________________________________________________________________
88cb7938 566Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
567{
a9bbb414 568 // Checks if passed option is contrary with file open option
569 // which is passed option "writable" and existing option not wriable
570 // in reverse case it is no harm so it is NOT contrary
88cb7938 571 if (fFile == 0x0) return kFALSE; //file is not opened - no problem
572
573 if ( ( AliLoader::IsOptionWritable(option) == kTRUE ) && // passed option is writable and
574 ( AliLoader::IsOptionWritable(fFileOption) == kFALSE ) ) // existing one is not writable
575 {
576 return kTRUE;
577 }
a9bbb414 578
88cb7938 579 return kFALSE;
580}
a9bbb414 581
582
583//______________________________________________________________________________
88cb7938 584void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
585{
a9bbb414 586 //Adds a base loader to lits of base loaders managed by this data loader
f21fc003 587 //Managed data will be stored in proper root directory,
a9bbb414 588 //and posted to
589 // - in case of tree/object - data folder connected with detector associated with this data loader
a9bbb414 590
591 if (bl == 0x0)
592 {
593 AliWarning("Pointer is null.");
594 return;
595 }
596
597 TObject* obj = fBaseLoaders->FindObject(bl->GetName());
598 if (obj)
599 {
600 AliError("Can not add this base loader.");
601 AliError(Form("There exists already base loader which manages data named %s for this detector.",obj->GetName()));
602 return;
603 }
88cb7938 604
a9bbb414 605 fBaseLoaders->Add(bl);
88cb7938 606}
607
a9bbb414 608//______________________________________________________________________________
88cb7938 609AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
610{
a9bbb414 611 //
612 // Return pointer to base loader
613 //
88cb7938 614 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
615}
88cb7938 616
a9bbb414 617//______________________________________________________________________________
88cb7938 618AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
619{
a9bbb414 620 //
d0d4a6b3 621 // Gets the n-th base loader (what is n?)
a9bbb414 622 //
623 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
88cb7938 624}
88cb7938 625
a9bbb414 626//______________________________________________________________________________
88cb7938 627TTree* AliDataLoader::Tree() const
628{
a9bbb414 629 // Returns tree from the main base loader
630 // it is just shortcut method for comfort of user
631 // main storage object does not have to be Tree -
632 // that is why first we need to check if it is a TreeLoader
633 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
634 if (tl == 0x0) return 0x0;
635 return tl->Tree();
88cb7938 636}
88cb7938 637
a9bbb414 638//______________________________________________________________________________
88cb7938 639void AliDataLoader::SetDirName(TString& dirname)
640{
a9bbb414 641 //
d0d4a6b3 642 // Sets the directory name where the files will be stored
a9bbb414 643 //
21bf7095 644 AliDebug(10, Form("FileName before %s",fFileName.Data()));
88cb7938 645 Int_t n = fFileName.Last('/');
21bf7095 646 AliDebug(10, Form("Slash found on pos %d",n));
88cb7938 647 if (n > 0) fFileName = fFileName.Remove(0,n+1);
21bf7095 648 AliDebug(10, Form("Core FileName %s",fFileName.Data()));
fca6cd9f 649 fFileName = dirname + fFileName;
21bf7095 650 AliDebug(10, Form("FileName after %s",fFileName.Data()));
88cb7938 651}
a9bbb414 652
653//______________________________________________________________________________
88cb7938 654AliObjectLoader* AliDataLoader::GetBaseDataLoader()
655{
a9bbb414 656 //
d0d4a6b3 657 // Gets the base data loader
a9bbb414 658 //
659 return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
88cb7938 660}
a9bbb414 661
a9bbb414 662//______________________________________________________________________________
88cb7938 663void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
664{
a9bbb414 665 //
666 // Sets data base loader
667 //
88cb7938 668 if (bl == 0x0)
a9bbb414 669 {
670 AliError("Parameter is null");
671 return;
672 }
88cb7938 673 if (GetBaseDataLoader()) delete GetBaseDataLoader();
674 fBaseLoaders->AddAt(bl,kData);
675}
a9bbb414 676
a9bbb414 677//______________________________________________________________________________
f0f6f856 678void AliDataLoader::Synchronize()
679{
a9bbb414 680 //
681 // Synchronizes all writable files
682 //
504b172d 683 if ( fFile ) fFile->Flush();
f0f6f856 684}
88cb7938 685
88cb7938 686
687