Fixes for coverity.
[u/mrichter/AliRoot.git] / STEER / STEER / AliLoader.cxx
CommitLineData
88cb7938 1//
2//Class (base) responsible for management of data:
3// - opening and closing proper files
4// - posting data to folders
5// - writing data from folders to proper files
6//
7//Author: Alice Offline Group http://alisoft.cern.ch
8//Responsible: Piotr.Skowronski@cern.ch
9//
d65adc48 10#include "AliLoader.h"
88cb7938 11
12//Root includes
13#include <TROOT.h>
88cb7938 14#include <TFile.h>
a9bbb414 15#include <TFolder.h>
88cb7938 16#include <TString.h>
88cb7938 17
18//AliRoot includes
d0d4a6b3 19#include "AliConfig.h"
a9bbb414 20#include "AliDetector.h"
a9bbb414 21#include "AliLog.h"
22#include "AliRun.h"
23#include "AliRunLoader.h"
88cb7938 24
88cb7938 25const TString AliLoader::fgkDefaultHitsContainerName("TreeH");
26const TString AliLoader::fgkDefaultDigitsContainerName = "TreeD";
27const TString AliLoader::fgkDefaultSDigitsContainerName = "TreeS";
28const TString AliLoader::fgkDefaultRecPointsContainerName = "TreeR";
29const TString AliLoader::fgkDefaultTracksContainerName = "TreeT";
30const TString AliLoader::fgkDefaultRecParticlesContainerName = "TreeP";
31const TString AliLoader::fgkLoaderBaseName("Loader");
32
33ClassImp(AliLoader)
34//___________________________________________________________________
35/////////////////////////////////////////////////////////////////////
36// //
37// class AliLoader //
38// //
39// Base class for Loaders. //
40// Loader provides base I/O fascilities for "standard" data. //
41// Each detector has a laoder data member //
42// loader is accessible via folder structure as well //
43// //
44/////////////////////////////////////////////////////////////////////
45
46/*****************************************************************************/
47
48AliLoader::AliLoader():
49 fDataLoaders(0x0),
50 fDetectorName(""),
51 fEventFolder(0x0),
52 fDataFolder(0x0),
53 fDetectorDataFolder(0x0),
f21fc003 54 fModuleFolder(0x0)
90e48c0c 55{
f21fc003 56 //default constructor
57
58}
88cb7938 59/******************************************************************/
60
61AliLoader::AliLoader(const Char_t* detname,const Char_t* eventfoldername):
62 fDataLoaders(new TObjArray(kNDataTypes)),
63 fDetectorName(""),
64 fEventFolder(0x0),
65 fDataFolder(0x0),
66 fDetectorDataFolder(0x0),
f21fc003 67 fModuleFolder(0x0)
88cb7938 68{
69 //ctor
21bf7095 70 AliDebug(1, Form("detname = %s eventfoldername = %s",detname,eventfoldername));
88cb7938 71
72 //try to find folder eventfoldername in top alice folder
73 //safe because GetTopFolder will abort in case of failure
74
75 fDetectorName = detname;
76 fName = fDetectorName+"Loader";
77 InitDefaults();
78
79 TObject* fobj = GetTopFolder()->FindObject(eventfoldername);
80 fEventFolder = (fobj)?dynamic_cast<TFolder*>(fobj):0x0;//in case FindObject returns NULL dynamic cast cause seg. fault
81 SetEventFolder(fEventFolder);
82
83 }
84/*****************************************************************************/
85
86AliLoader::AliLoader(const Char_t * detname,TFolder* eventfolder):
87 fDataLoaders(new TObjArray(kNDataTypes)),
88 fDetectorName(detname),
89 fEventFolder(0x0),
90 fDataFolder(0x0),
91 fDetectorDataFolder(0x0),
f21fc003 92 fModuleFolder(0x0)
88cb7938 93{
94//constructor
95 fDetectorName = detname;
96 fName = fDetectorName+"Loader";
97 InitDefaults();
98 SetEventFolder(eventfolder);
99 //fileoption's don't need to initialized because default TString ctor does it correctly
100}
d0d4a6b3 101
102/*****************************************************************************/
88cb7938 103AliLoader::~AliLoader()
104{
105//detructor
106 if (fDataLoaders) fDataLoaders->SetOwner();
107 delete fDataLoaders;
108}
109/*****************************************************************************/
110
111void AliLoader::InitDefaults()
112{
113 // H I T S
114 AliDataLoader* dl;
115 dl = new AliDataLoader(fDetectorName + ".Hits.root",fgkDefaultHitsContainerName, "Hits" );
116 fDataLoaders->AddAt(dl,kHits);
117
118
119 // S U M M A B L E D I G I T S
120 dl = new AliDataLoader(fDetectorName + ".SDigits.root",fgkDefaultSDigitsContainerName, "Summable Digits");
88cb7938 121 fDataLoaders->AddAt(dl,kSDigits);
122
123 // D I G I T S
124 dl = new AliDataLoader(fDetectorName + ".Digits.root",fgkDefaultDigitsContainerName, "Digits");
88cb7938 125 fDataLoaders->AddAt(dl,kDigits);
126
127 // R E C O N S T R U C T E D P O I N T S aka C L U S T E R S
128 dl = new AliDataLoader(fDetectorName + ".RecPoints.root",fgkDefaultRecPointsContainerName, "Reconstructed Points");
88cb7938 129 fDataLoaders->AddAt(dl,kRecPoints);
130
131 // T R A C K S
132 dl = new AliDataLoader(fDetectorName + ".Tracks.root",fgkDefaultTracksContainerName, "Tracks");
88cb7938 133 fDataLoaders->AddAt(dl,kTracks);
134
135 // R E C O N S T R U C T E D P O I N T S aka C L U S T E R S
136 dl = new AliDataLoader(fDetectorName + ".RecParticles.root",fgkDefaultRecParticlesContainerName, "Reconstructed Particles");
88cb7938 137 fDataLoaders->AddAt(dl,kRecParticles);
138
139 }
140/*****************************************************************************/
141
142AliDataLoader* AliLoader::GetDataLoader(const char* name)
143{
144//returns Data Loader with specified name
145 return dynamic_cast<AliDataLoader*>(fDataLoaders->FindObject(name));
146}
147/*****************************************************************************/
deee9e3b 148void AliLoader::AddDataLoader(AliDataLoader* dl)
149{
8364b0ef 150 //
151 // Adds a data loader
152 //
deee9e3b 153 if (dl == 0x0)
154 {
21bf7095 155 AliError("Pointer is NULL");
deee9e3b 156 return;
157 }
158 if (fDataLoaders->FindObject(dl->GetName()))
159 {
21bf7095 160 AliError("Such a loader exists");
deee9e3b 161 return;
162 }
163 fDataLoaders->AddLast(dl);
164 dl->SetEventFolder(fEventFolder);
165 dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
166}
167/*****************************************************************************/
88cb7938 168
169Int_t AliLoader::SetEvent()
170{
171 //basically the same that GetEvent but do not post data to folders
172 TIter next(fDataLoaders);
173 AliDataLoader* dl;
174 while ((dl = (AliDataLoader*)next()))
175 {
176 dl->SetEvent();
177 }
178 return 0;
179}
180/******************************************************************/
181
1bb20a37 182void AliLoader::UnloadAll()
183{
184 //calls UnloadAll for all base laoders
185 //Unloads everything
186 TIter next(fDataLoaders);
187 AliDataLoader* dl;
188 while ((dl = (AliDataLoader*)next()))
189 {
190 dl->UnloadAll();
191 }
192}
193/******************************************************************/
194
88cb7938 195Int_t AliLoader::GetEvent()
196{
197 //changes to proper root directory and tries to load data from files to folders
198 // event number is defined in RunLoader
199 //
200 //returns:
201 // 0 - in case of no error
202 // 1 - event not found
203 //
204
205 Int_t retval;
206 TIter next(fDataLoaders);
207 AliDataLoader* dl;
208 while ((dl = (AliDataLoader*)next()))
209 {
210 retval = dl->GetEvent();
211 if (retval)
212 {
21bf7095 213 AliError(Form("Error occured while GetEvent for %s",dl->GetName()));
88cb7938 214 return retval;
215 }
216 }
217
218 return 0;
219}
220
221/******************************************************************/
222
223TFolder* AliLoader::GetTopFolder()
224{
225//returns TOP aliroot folder, just a simple interface to AliConfig (gives shorter notation)
226 return AliConfig::Instance()->GetTopFolder();
227}
228
229/******************************************************************/
230
231TFolder* AliLoader::GetEventFolder()
232{
233//get EVENT folder (data that are changing from event to event, even in single run)
234 return fEventFolder;
235}
236/******************************************************************/
237TFolder* AliLoader::GetDataFolder()
238{
239//returns the folder speciofic to given detector e.g. /Folders/Event/Data/
240 if (!fDataFolder)
241 {
e191bb57 242 fDataFolder = dynamic_cast<TFolder*>(GetEventFolder()->FindObject(AliConfig::Instance()->GetDataFolderName()));
88cb7938 243
244 if (!fDataFolder)
245 {
21bf7095 246 AliFatal("Can not find AliRoot data folder. Aborting");
88cb7938 247 return 0x0;
248 }
249 }
250 return fDataFolder;
251}
252
253/*****************************************************************************/
254
88cb7938 255TFolder* AliLoader::GetModulesFolder()
256{
d0d4a6b3 257 //returns pointer to the folder containing modules
88cb7938 258 if (!fModuleFolder)
259 {
260 fModuleFolder = dynamic_cast<TFolder*>(GetEventFolder()->FindObjectAny(AliConfig::GetModulesFolderName()));
261
262 if (!fModuleFolder)
263 {
21bf7095 264 AliFatal("Can not find modules folder. Aborting");
88cb7938 265 return 0x0;
266 }
267 }
268 return fModuleFolder;
269
270}
88cb7938 271
88cb7938 272/*****************************************************************************/
273
274TDirectory* AliLoader::ChangeDir(TFile* file, Int_t eventno)
275{
276//changes the root directory in "file" to "dirname" which corresponds to event 'eventno'
277//in case of success returns the pointer to directory
278//else NULL
279
280 if (file == 0x0)
281 {
21bf7095 282 AliErrorClass("File is null");
88cb7938 283 return 0x0;
284 }
285 if (file->IsOpen() == kFALSE)
286 {
21bf7095 287 AliErrorClass("File is not opened");
88cb7938 288 return 0x0;
289 }
290
291 TString dirname("Event");
292 dirname+=eventno;
21bf7095 293 AliDebugClass(1, Form("Changing Dir to %s in file %s.",dirname.Data(),file->GetName()));
88cb7938 294
295 Bool_t result;
296
297 TDirectory* dir = dynamic_cast<TDirectory*>(file->Get(dirname));
298
299 if (dir == 0x0)
300 {
21bf7095 301 AliDebugClass(1, Form("Can not find directory %s in file %s, creating...",
302 dirname.Data(),file->GetName()));
88cb7938 303
304 if (file->IsWritable() == kFALSE)
305 {
21bf7095 306 AliErrorClass(Form("Can not create directory. File %s in not writable.",
307 file->GetName()));
88cb7938 308 return 0x0;
309 }
310
311 TDirectory* newdir = file->mkdir(dirname);
312 if (newdir == 0x0)
313 {
21bf7095 314 AliErrorClass(Form("Failed to create new directory in file %s.",
315 file->GetName()));
88cb7938 316 return 0x0;
317 }
318 result = file->cd(dirname);
319 if (result == kFALSE)
320 {
321 return 0x0;
322 }
323 }
324 else
325 {
326 file->cd();//make a file active
327 file->cd(dirname);//cd event dir
328 }
329
330 return gDirectory;
331}
332/*****************************************************************************/
333
d0d4a6b3 334TString AliLoader::GetUnixDir() const
88cb7938 335 {
336 //This Method will manage jumping through unix directories in case of
337 //run with more events per run than defined in gAlice
338
339 TString dir;
340
341 return dir;
342 }
343/*****************************************************************************/
344/************************************************************/
345
346void AliLoader::MakeTree(Option_t *option)
347 {
348//Makes a tree depending on option
349// H: - Hits
350// D: - Digits
351// S: - Summable Digits
352// R: - Reconstructed Points (clusters)
353// T: - Tracks (tracklets)
354
355 const char *oH = strstr(option,"H");
356 const char *oD = strstr(option,"D");
357 const char *oS = strstr(option,"S");
358 const char *oR = strstr(option,"R");
359 const char *oT = strstr(option,"T");
360 const char *oP = strstr(option,"P");
361
362 if (oH) MakeHitsContainer();
363 if (oD) MakeDigitsContainer();
364 if (oS) MakeSDigitsContainer();
365 if (oR) MakeRecPointsContainer();
366 if (oT) MakeTracksContainer();
367 if (oP) MakeRecParticlesContainer();
368 }
369
370/*****************************************************************************/
9bb30bcb 371Int_t AliLoader::WriteHits(Option_t* opt) const
88cb7938 372 {
d0d4a6b3 373 // Writes hits
88cb7938 374 AliDataLoader* dl = GetHitsDataLoader();
375 Int_t ret = dl->WriteData(opt);
376 return ret;
377 }
378/*****************************************************************************/
379
9bb30bcb 380Int_t AliLoader::WriteSDigits(Option_t* opt) const
88cb7938 381 {
d0d4a6b3 382 // Writes summable digits
88cb7938 383 AliDataLoader* dl = GetSDigitsDataLoader();
384 Int_t ret = dl->WriteData(opt);
385 return ret;
386 }
387
388/*****************************************************************************/
389
88cb7938 390TObject** AliLoader::GetDetectorDataRef(TObject *obj)
391{
d0d4a6b3 392 // Returns pointer to an entry in the list of folders pointing to "obj"
88cb7938 393 if (obj == 0x0)
394 {
395 return 0x0;
396 }
397 return GetDetectorDataFolder()->GetListOfFolders()->GetObjectRef(obj) ;
398}
88cb7938 399
400/*****************************************************************************/
401void AliLoader::CleanFolders()
402 {
403 //Cleans all posted objects == removes from folders and deletes them
404 TIter next(fDataLoaders);
405 AliDataLoader* dl;
406 while ((dl = (AliDataLoader*)next()))
407 {
21bf7095 408 AliDebug(1, Form("name = %s cleaning",dl->GetName()));
88cb7938 409 dl->Clean();
410 }
411 }
88cb7938 412
88cb7938 413/*****************************************************************************/
414
415Int_t AliLoader::ReloadAll()
416{
d0d4a6b3 417 // Calling Reload function for all the data loaders
88cb7938 418 TIter next(fDataLoaders);
419 AliDataLoader* dl;
420
421 while((dl = (AliDataLoader*)next()))
422 {
423 Int_t err = dl->Reload();
424 if (err)
425 {
21bf7095 426 AliError(Form("Reload returned error for %s",dl->GetName()));
88cb7938 427 return err;
428 }
429 }
430 return 0;
431}
432/*****************************************************************************/
433
434void AliLoader::CloseFiles()
435{
436//close files for data loaders
437 TIter next(fDataLoaders);
438 AliDataLoader* dl;
439 while((dl = (AliDataLoader*)next()))
440 {
441 dl->CloseFile();
442 }
443}
444/*****************************************************************************/
445
446Int_t AliLoader::SetEventFolder(TFolder* eventfolder)
447{
d0d4a6b3 448 //sets the event folder
88cb7938 449 if (eventfolder == 0x0)
450 {
21bf7095 451 AliError("Stupid joke. Argument is NULL");
88cb7938 452 return 1;
453 }
454
455 fEventFolder = eventfolder;
456 TIter next(fDataLoaders);
457 AliDataLoader* dl;
458
459 while((dl = (AliDataLoader*)next()))
460 {
461 dl->SetEventFolder(fEventFolder);
462 dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
463 }
464
465 return 0;
466}//sets the event folder
467/*****************************************************************************/
468
469Int_t AliLoader::Register(TFolder* eventFolder)
470{
471//triggers creation of subfolders for a given detector
472//this method is called when session is read from disk
473//
474//warning: AliDetector in constructor (not default) calls
475//creation of folder structure as well (some detectors needs folders
476//alrady in constructors)
477
21bf7095 478 AliDebug(1, Form("Name is %s.",GetName()));
88cb7938 479 if (eventFolder == 0x0)
480 {
21bf7095 481 AliError("Event folder is not set.");
88cb7938 482 return 1;
483 }
484 Int_t retval = AliConfig::Instance()->AddDetector(eventFolder,fDetectorName,fDetectorName);
485 if(retval)
486 {
f21fc003 487 AliError(Form("Can not create folders for %s. Event folder name is %s",
21bf7095 488 fDetectorName.Data(),eventFolder->GetName()));
88cb7938 489 return retval;
490 }
491 SetEventFolder(eventFolder);
492 return 0;
493}
494/*****************************************************************************/
495AliRunLoader* AliLoader::GetRunLoader()
496{
497//gets the run-loader from event folder
498 AliRunLoader* rg = 0x0;
024a7e64 499 TObject * obj = GetEventFolder()->FindObject(AliRunLoader::GetRunLoaderName());
88cb7938 500 if (obj) rg = dynamic_cast<AliRunLoader*>(obj);
501 return rg;
502}
503/*****************************************************************************/
504Bool_t AliLoader::TestFileOption(Option_t* opt)
505{
506//tests the TFile::Option
507//if file is truncated at opening moment ("recreate", "new" or "create") returns kFALSE;
508//else kTRUE (means opened with "read" or "update" mode)
509 TString option(opt);
510 if (option.CompareTo("recreate",TString::kIgnoreCase) == 0) return kFALSE;
511 if (option.CompareTo("new",TString::kIgnoreCase) == 0) return kFALSE;
512 if (option.CompareTo("create",TString::kIgnoreCase) == 0) return kFALSE;
513 return kTRUE;
514}
515/*****************************************************************************/
516void AliLoader::SetDirName(TString& dirname)
517{
fca6cd9f 518//adds "dirname" to each file. Dirname should end with "#","/", or ":"
88cb7938 519 TIter next(fDataLoaders);
520 AliDataLoader* dl;
521 while((dl = (AliDataLoader*)next()))
522 {
523 dl->SetDirName(dirname);
524 }
525}
526
527/*****************************************************************************/
528
9bb30bcb 529void AliLoader::SetDigitsFileNameSuffix(const TString& suffix) const
88cb7938 530{
531 //adds the suffix before ".root",
532 //e.g. TPC.Digits.root -> TPC.DigitsMerged.root
533 //made on Jiri Chudoba demand
534 GetDigitsDataLoader()->SetFileNameSuffix(suffix);
535}
536/*****************************************************************************/
537
538void AliLoader::SetCompressionLevel(Int_t cl)
539{
540//sets comression level for data defined by di
541 TIter next(fDataLoaders);
542 AliDataLoader* dl;
543 while((dl = (AliDataLoader*)next()))
544 {
545 dl->SetCompressionLevel(cl);
546 }
547}
548/*****************************************************************************/
549
550void AliLoader::Clean()
551{
552//Cleans all data loaders
553 TIter next(fDataLoaders);
554 AliDataLoader* dl;
555 while((dl = (AliDataLoader*)next()))
556 {
557 dl->Clean();
558 }
559}
560/*****************************************************************************/
561
562void AliLoader::Clean(const TString& name)
563{
d0d4a6b3 564 // Removes object with "name" from the detector's data folder
565 // and from the memory
88cb7938 566 TObject* obj = GetDetectorDataFolder()->FindObject(name);
567 if(obj)
568 {
21bf7095 569 AliDebug(1, Form("name=%s, cleaning %s.",GetName(),name.Data()));
88cb7938 570 GetDetectorDataFolder()->Remove(obj);
571 delete obj;
572 }
573}
574
575/*****************************************************************************/
576
577Bool_t AliLoader::IsOptionWritable(const TString& opt)
578{
d0d4a6b3 579 // Returns "true" if the option means also "writable"
88cb7938 580 if (opt.CompareTo("recreate",TString::kIgnoreCase)) return kTRUE;
581 if (opt.CompareTo("new",TString::kIgnoreCase)) return kTRUE;
582 if (opt.CompareTo("create",TString::kIgnoreCase)) return kTRUE;
583 if (opt.CompareTo("update",TString::kIgnoreCase)) return kTRUE;
584 return kFALSE;
585}
88cb7938 586
21bf7095 587/*****************************************************************************/
588
589void AliLoader::SetDebug(Int_t deb)
590{
591 // Sets debug level
592 AliLog::SetClassDebugLevel("AliRunLoader", deb);
593 AliLog::SetClassDebugLevel("AliLoader", deb);
594 AliLog::SetClassDebugLevel("AliDataLoader", deb);
595 AliLog::SetClassDebugLevel("AliBaseLoader", deb);
596 AliLog::SetClassDebugLevel("AliObjectLoader", deb);
597 AliLog::SetClassDebugLevel("AliTreeLoader", deb);
21bf7095 598 AliLog::SetClassDebugLevel("AliConfig", deb);
599}
600/*****************************************************************************/
601
88cb7938 602void AliLoader::SetTAddrInDet()
603{
604 //calls SetTreeAddress for corresponding detector
605 AliRunLoader* rl = GetRunLoader();
606 if (rl == 0x0) return;
607 AliRun* ar = rl->GetAliRun();
608 if (ar == 0x0) return;
609 AliDetector* det = ar->GetDetector(fDetectorName);
610 if (det == 0x0) return;
611 det->SetTreeAddress();
612}
613/*****************************************************************************/
f0f6f856 614
615void AliLoader::Synchronize()
616{
617 //synchrinizes all writtable files
618 TIter next(fDataLoaders);
619 AliDataLoader* dl;
620 while ((dl = (AliDataLoader*)next()))
621 {
622 dl->Synchronize();
623 }
624
625}
626/*****************************************************************************/
88cb7938 627/*****************************************************************************/
628/*****************************************************************************/
629