1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15 ////////////////////////////////////////////////////////////////////////////
17 // support classes for the QA histogram viewer
19 // origin: Mikolaj Krzewicki, Nikhef, Mikolaj.Krzewicki@cern.ch
21 ///////////////////////////////////////////////////////////////////////////
25 #include <Riostream.h>
30 #include <TObjString.h>
35 #include "AliQAHistNavigator.h"
40 ClassImp(AliQAHistNavigator)
42 //_________________________________________________________________________
43 AliQAHistNavigator::AliQAHistNavigator(Int_t run):
46 fPQAResultFile( NULL ),
49 fPCurrDetector( NULL ),
52 fPListOfFiles( new AliQADirList() ),
54 fLoopAllDetectors(kTRUE),
55 fLoopAllLevels(kTRUE),
58 fExpertDirName("Expert"),
59 fPEmptyList(new TList())
62 fPEmptyList->AddLast(new AliQADirListItem(""));
66 //_________________________________________________________________________
67 AliQAHistNavigator::~AliQAHistNavigator()
72 //_________________________________________________________________________
73 Bool_t AliQAHistNavigator::GetHistogram(TH1*& hist)
75 //get histogram from file
76 TString file = GetFileName();
77 TString dir = GetDirName();
78 TString histname = GetItemName();
79 cout<<"GetHistogram: "<<file<<":"<<dir<<"/"<<histname<<endl;
80 if (file==""||dir==""||histname=="")
82 printf("GetItem: nothing to fetch...\n");
85 if (!OpenCurrentDirectory()) return kFALSE;
86 hist = dynamic_cast<TH1*>( gDirectory->FindKey(histname)->ReadObj() );
89 printf("GetItem: null pointer returned by gDirectory\n");
95 //_________________________________________________________________________
96 Bool_t AliQAHistNavigator::Next()
98 //move to the next histogram
99 if (!fPCurrFile||!fPCurrDetector||!fPCurrLevel) return kFALSE;
100 if (!(fPCurrItem=(AliQADirListItem*)GetItemList()->After(fPCurrItem)))
102 if (!(fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->After(fPCurrLevel)))
104 if (!(fPCurrDetector=(AliQADirList*)fPCurrFile->GetDirs()->After(fPCurrDetector)))
106 if (!(fPCurrFile=(AliQADirList*)fPListOfFiles->GetDirs()->After(fPCurrFile)))
108 //we're at the end of everything
111 //rewind to the beginning
112 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->First();
113 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
114 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->First();
115 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
117 OpenCurrentDirectory();
118 printf("----------------back at the beginning!\n");
119 } else return kFALSE; //no rewind, we finish
120 } else //if there is a next file
122 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
123 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->First();
124 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
126 OpenCurrentDirectory();
128 } else //if there is a next detector
130 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->First();
131 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
132 OpenCurrentDirectory();
134 } else //if there is a next level
136 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
137 OpenCurrentDirectory();
139 } else //if there is a next histgram
145 //_________________________________________________________________________
146 Bool_t AliQAHistNavigator::Prev()
148 //move to the previous histrogram
149 if (!fPCurrLevel||!fPCurrDetector||!fPCurrFile) return kFALSE;
150 if (!(fPCurrItem=(AliQADirListItem*)GetItemList()->Before(fPCurrItem)))
152 if (!(fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Before(fPCurrLevel)))
154 if (!(fPCurrDetector=(AliQADirList*)fPCurrFile->GetDirs()->Before(fPCurrDetector)))
156 if (!(fPCurrFile=(AliQADirList*)fPListOfFiles->GetDirs()->Before(fPCurrFile)))
158 //we're at the end of everything
161 //rewind to the beginning
162 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->Last();
163 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->Last();
164 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->Last();
165 fPCurrItem = (AliQADirListItem*)GetItemList()->Last();
167 OpenCurrentDirectory();
168 printf("----------------back at the end!\n");
169 } else return kFALSE; //no rewind, we finish
170 } else //if there is a next file
172 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->Last();
173 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Last();
174 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
176 OpenCurrentDirectory();
178 } else //if there is a next detector
180 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Last();
181 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
182 OpenCurrentDirectory();
184 } else //if there is a next level
186 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
187 OpenCurrentDirectory();
189 } else //if there is a next histgram
195 //_________________________________________________________________________
196 void AliQAHistNavigator::SetExpertMode(Bool_t mode)
198 //sets the expert mode
199 Bool_t oldmode = fExpertMode;
201 TList* itemList = GetItemList();
203 if (fExpertMode!=oldmode && itemList) fPCurrItem = (AliQADirListItem*)itemList->First();
207 //_________________________________________________________________________
208 Bool_t AliQAHistNavigator::OpenCurrentFile()
211 if (fPFile) fPFile->Close();
212 if (!(fPFile->Open(GetFileName(),"READ")))
214 cout<<"There is no file: "<<GetFileName()<<endl;
220 //_________________________________________________________________________
221 Bool_t AliQAHistNavigator::OpenCurrentDirectory()
223 //Open current directory
224 if (!gDirectory->cd(GetDirName())) return kFALSE;
228 //_________________________________________________________________________
229 Bool_t AliQAHistNavigator::SetFile( TString file )
231 //Set a new file to read from
232 AliQADirList* tmp = (AliQADirList*)fPListOfFiles->GetDirs()->FindObject ( file.Data() );
233 if (!tmp) return kFALSE;
236 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
237 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
238 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
242 //_________________________________________________________________________
243 Bool_t AliQAHistNavigator::SetFile( Int_t file )
245 //Set a new file to read from
246 printf("AliQAHistNavigator::SetFile(%i)\n",file);
247 AliQADirList* tmp = (AliQADirList*)fPListOfFiles->GetDirs()->At(file);
248 if (!tmp) return kFALSE;
251 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
252 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
253 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
254 OpenCurrentDirectory();
258 //_________________________________________________________________________
259 Bool_t AliQAHistNavigator::SetDetector( TString det )
262 AliQADirList* tmp = (AliQADirList*)fPCurrFile->GetDirs()->FindObject( det.Data() );
263 if (!tmp) return kFALSE;
264 fPCurrDetector = tmp;
265 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
266 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
267 OpenCurrentDirectory();
271 //_________________________________________________________________________
272 Bool_t AliQAHistNavigator::SetDetector( Int_t det )
275 printf("AliQAHistNavigator::SetDetector(%i)\n",det);
276 AliQADirList* tmp = (AliQADirList*)fPCurrFile->GetDirs()->At( det );
277 if (!tmp) return kFALSE;
278 fPCurrDetector = tmp;
279 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
280 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
281 OpenCurrentDirectory();
285 //_________________________________________________________________________
286 Bool_t AliQAHistNavigator::SetLevel( TString level )
289 AliQADirList* tmp = (AliQADirList*)fPCurrDetector->GetDirs()->FindObject( level.Data() );
290 if (!tmp) return kFALSE;
292 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
293 OpenCurrentDirectory();
297 //_________________________________________________________________________
298 Bool_t AliQAHistNavigator::SetLevel( Int_t level )
301 AliQADirList* tmp = (AliQADirList*)fPCurrDetector->GetDirs()->At( level );
302 if (!tmp) return kFALSE;
304 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
305 OpenCurrentDirectory();
309 //_________________________________________________________________________
310 Bool_t AliQAHistNavigator::SetItem( TString hist )
313 AliQADirListItem* tmp = (AliQADirListItem*)GetItemList()->FindObject( hist.Data() );
314 if (!tmp) return kFALSE;
319 //_________________________________________________________________________
320 Bool_t AliQAHistNavigator::SetItem( Int_t hist )
323 AliQADirListItem* tmp = (AliQADirListItem*)GetItemList()->At( hist );
324 if (!tmp) return kFALSE;
329 //_________________________________________________________________________
330 TList* AliQAHistNavigator::GetItemList()
332 //returns the current list of histograms, if none, returns empty list
336 AliQADirList* expertlist = 0;
337 if(fPCurrLevel) expertlist = (AliQADirList*)fPCurrLevel->GetDirs()->FindObject(fExpertDirName);
338 if (expertlist) itemlist = expertlist->GetItems();
341 //this is a bit of a hack, but it will always return something sensible
342 AliQADirListItem* it = (AliQADirListItem*)fPEmptyList->First();
343 it->SetParent(fPCurrLevel);
344 itemlist = fPEmptyList;
348 if(fPCurrLevel) itemlist = fPCurrLevel->GetItems();
353 //_________________________________________________________________________
354 TString AliQAHistNavigator::GetDetectorName()
356 //Get name of current detector
357 if (!fPCurrDetector) return "";
358 TString name = fPCurrDetector->GetName();
362 //_________________________________________________________________________
363 TString AliQAHistNavigator::GetLevelName()
365 //Get name of current leve
366 if (!fPCurrLevel) return "";
367 TString name = fPCurrLevel->GetName();
371 //_________________________________________________________________________
372 TString AliQAHistNavigator::GetFileName()
374 //Get name of current file
375 if (!fPCurrFile) return "";
376 TString file = fPCurrFile->GetName();
380 //_________________________________________________________________________
381 TString AliQAHistNavigator::GetDirName()
383 //get the name of dir containing current item
384 if (!fPCurrItem || !fPCurrItem->GetParent()) return "";
385 AliQADirList* d=(AliQADirList*)fPCurrItem->GetParent();
389 path = d->GetName() + path;
393 while (d->GetParent());
397 //_________________________________________________________________________
398 TString AliQAHistNavigator::GetPath(AliQADirListItem* const item)
400 //Get the full path to teh directory containing item
401 AliQADirList* d=item->GetParent();
402 TString path = "";//item->GetString();
405 TString sep = (d->GetParent()) ? "/" : ":/" ;
406 path = d->GetName() + sep + path;
408 while ((d=d->GetParent()));
412 //_________________________________________________________________________
413 TString AliQAHistNavigator::GetItemName()
415 //Get name of current item
416 if (!fPCurrItem) return "";
417 return fPCurrItem->GetString();
420 //_________________________________________________________________________
421 Bool_t AliQAHistNavigator::GetListOfFiles()
423 //scan directory for QA files
424 delete fPListOfFiles;
425 fPListOfFiles = new AliQADirList();
427 TString fileNameCORR;
428 TString fileNameQAResult;
431 gSystem->ExpandPathName(macdir);
433 void* dirhandle = gSystem->OpenDirectory(macdir.Data());
436 const char* filename;
442 reg+= (fRun==0) ? "[0-9].*" : revstr.Data();
444 TPRegexp reHist(reg);
445 TPRegexp reMerged("Merged.QA.Data.[0-9]*.root");
446 TPRegexp reCORR("CORR.QA.[0-9]*.root");
447 TPRegexp reQA("QA.root");
448 std::list<string> names;
449 while((filename = gSystem->GetDirEntry(dirhandle)) != 0)
451 if (reCORR.Match(filename))
453 fileNameCORR = filename;
456 if (reQA.Match(filename))
458 fileNameQAResult = filename;
461 if (reMerged.Match(filename))
464 names.push_back(filename);
467 if (reHist.Match(filename))
469 names.push_back(filename);
472 if (!fPCORRFile) fPCORRFile = new TFile(fileNameCORR,"READ");
473 if (!fPQAResultFile) fPQAResultFile = new TFile(fileNameQAResult,"READ");
476 printf("GetListOfFiles: no files matching...\n");
481 for (std::list<string>::iterator si=names.begin(); si!=names.end(); ++si)
483 snprintf(fullName,sizeof(fullName),"%s", si->c_str());
484 AliQADirList* f = new AliQADirList();
485 f->SetName(fullName);
486 fPListOfFiles->GetDirs()->AddLast(f);
491 gSystem->FreeDirectory(dirhandle);
497 //_________________________________________________________________________
498 Bool_t AliQAHistNavigator::CloneDirStructure()
501 if (!GetListOfFiles()) return kFALSE;
502 if (fPListOfFiles->GetDirs()->GetEntries()==0) return kFALSE;
503 TIter fileiter(fPListOfFiles->GetDirs());
505 while ((f = (AliQADirList*)fileiter.Next()))
507 TString filename = f->GetName();
508 TFile file(filename);
509 if (!Crawl(f)) continue;
514 //_________________________________________________________________________
515 Bool_t AliQAHistNavigator::Crawl(AliQADirList* dir)
517 //scans the current directory and puts result in dir
518 //returns false if dir in file empty
521 TString pwd = gDirectory->GetPath();
522 TList* keys = gDirectory->GetListOfKeys();
523 if (!keys) return kFALSE;
524 if (keys->GetEntries()==0) return kFALSE;
527 while ((key = dynamic_cast <TKey* > (keyiter.Next()) ))
529 keyname = key->GetName();
530 if (keyname==oldkeyname) continue; //do not copy cycles
531 oldkeyname = keyname;
532 TString classname=key->GetClassName();
533 if (!classname) return kFALSE;
534 if (classname=="TDirectoryFile")
536 gDirectory->cd(key->GetName());
537 AliQADirList* newdir = new AliQADirList();
545 newdir->SetName(keyname);
546 newdir->SetParent(dir);
547 dir->GetDirs()->AddLast(newdir);
551 AliQADirListItem* item = new AliQADirListItem(keyname);
552 item->SetParent(dir);
553 dir->GetItems()->AddLast(item);
559 //_________________________________________________________________________
560 Bool_t AliQAHistNavigator::ReReadFiles()
562 //close, open and rescan the files
563 if (!CloneDirStructure())
565 printf("AliQAHistNavigator::AliQAHistNavigator(): error reading files\n");
568 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->First();
571 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
574 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->First();
577 fPCurrItem = (AliQADirListItem*) GetItemList()->First();
582 OpenCurrentDirectory();
590 //_________________________________________________________________________
591 //_________________________________________________________________________
592 ClassImp(AliQADirList)
593 //_________________________________________________________________________
594 AliQADirList::AliQADirList():
597 fPItems(new TList()),
603 //_________________________________________________________________________
604 AliQADirList::~AliQADirList()
607 if (fPParent) delete fPParent;
612 //_________________________________________________________________________
613 ClassImp(AliQADirListItem)
614 //_________________________________________________________________________
615 AliQADirListItem::AliQADirListItem(const char* s):
622 //_________________________________________________________________________
623 AliQADirListItem::~AliQADirListItem()
626 if (fPParent) delete fPParent;