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"
37 ClassImp(AliQAHistNavigator)
39 //_________________________________________________________________________
40 AliQAHistNavigator::AliQAHistNavigator(Int_t run):
43 fPQAResultFile( NULL ),
46 fPCurrDetector( NULL ),
49 fPListOfFiles( new AliQADirList() ),
51 fLoopAllDetectors(kTRUE),
52 fLoopAllLevels(kTRUE),
55 fExpertDirName("Expert"),
56 fPEmptyList(new TList())
59 fPEmptyList->AddLast(new AliQADirListItem(""));
63 //_________________________________________________________________________
64 AliQAHistNavigator::~AliQAHistNavigator()
69 //_________________________________________________________________________
70 Bool_t AliQAHistNavigator::GetHistogram(TH1*& hist)
72 //get histogram from file
73 TString file = GetFileName();
74 TString dir = GetDirName();
75 TString histname = GetItemName();
76 cout<<"GetHistogram: "<<file<<":"<<dir<<"/"<<histname<<endl;
77 if (file==""||dir==""||histname=="")
79 printf("GetItem: nothing to fetch...\n");
82 if (!OpenCurrentDirectory()) return kFALSE;
83 hist = dynamic_cast<TH1*>( gDirectory->FindKey(histname)->ReadObj() );
86 printf("GetItem: null pointer returned by gDirectory\n");
92 //_________________________________________________________________________
93 Bool_t AliQAHistNavigator::Next()
95 //move to the next histogram
96 if (!fPCurrFile||!fPCurrDetector||!fPCurrLevel) return kFALSE;
97 if (!(fPCurrItem=(AliQADirListItem*)GetItemList()->After(fPCurrItem)))
99 if (!(fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->After(fPCurrLevel)))
101 if (!(fPCurrDetector=(AliQADirList*)fPCurrFile->GetDirs()->After(fPCurrDetector)))
103 if (!(fPCurrFile=(AliQADirList*)fPListOfFiles->GetDirs()->After(fPCurrFile)))
105 //we're at the end of everything
108 //rewind to the beginning
109 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->First();
110 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
111 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->First();
112 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
114 OpenCurrentDirectory();
115 printf("----------------back at the beginning!\n");
116 } else return kFALSE; //no rewind, we finish
117 } else //if there is a next file
119 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
120 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->First();
121 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
123 OpenCurrentDirectory();
125 } else //if there is a next detector
127 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->First();
128 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
129 OpenCurrentDirectory();
131 } else //if there is a next level
133 fPCurrItem=(AliQADirListItem*)GetItemList()->First();
134 OpenCurrentDirectory();
136 } else //if there is a next histgram
142 //_________________________________________________________________________
143 Bool_t AliQAHistNavigator::Prev()
145 //move to the previous histrogram
146 if (!fPCurrLevel||!fPCurrDetector||!fPCurrFile) return kFALSE;
147 if (!(fPCurrItem=(AliQADirListItem*)GetItemList()->Before(fPCurrItem)))
149 if (!(fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Before(fPCurrLevel)))
151 if (!(fPCurrDetector=(AliQADirList*)fPCurrFile->GetDirs()->Before(fPCurrDetector)))
153 if (!(fPCurrFile=(AliQADirList*)fPListOfFiles->GetDirs()->Before(fPCurrFile)))
155 //we're at the end of everything
158 //rewind to the beginning
159 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->Last();
160 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->Last();
161 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->Last();
162 fPCurrItem = (AliQADirListItem*)GetItemList()->Last();
164 OpenCurrentDirectory();
165 printf("----------------back at the end!\n");
166 } else return kFALSE; //no rewind, we finish
167 } else //if there is a next file
169 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->Last();
170 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Last();
171 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
173 OpenCurrentDirectory();
175 } else //if there is a next detector
177 fPCurrLevel=(AliQADirList*)fPCurrDetector->GetDirs()->Last();
178 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
179 OpenCurrentDirectory();
181 } else //if there is a next level
183 fPCurrItem=(AliQADirListItem*)GetItemList()->Last();
184 OpenCurrentDirectory();
186 } else //if there is a next histgram
192 //_________________________________________________________________________
193 void AliQAHistNavigator::SetExpertMode(Bool_t mode)
195 //sets the expert mode
196 Bool_t oldmode = fExpertMode;
198 if (fExpertMode!=oldmode) fPCurrItem = (AliQADirListItem*)GetItemList()->First();
202 //_________________________________________________________________________
203 Bool_t AliQAHistNavigator::OpenCurrentFile()
206 if (fPFile) fPFile->Close();
207 if (!(fPFile->Open(GetFileName(),"READ")))
210 cout<<"There is no file: "<<GetFileName()<<endl;
215 //_________________________________________________________________________
216 Bool_t AliQAHistNavigator::OpenCurrentDirectory()
218 //Open current directory
219 if (!gDirectory->cd(GetDirName())) return kFALSE;
223 //_________________________________________________________________________
224 Bool_t AliQAHistNavigator::SetFile( TString file )
226 //Set a new file to read from
227 AliQADirList* tmp = (AliQADirList*)fPListOfFiles->GetDirs()->FindObject ( file.Data() );
228 if (!tmp) return kFALSE;
231 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
232 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
233 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
237 //_________________________________________________________________________
238 Bool_t AliQAHistNavigator::SetFile( Int_t file )
240 //Set a new file to read from
241 printf("AliQAHistNavigator::SetFile(%i)\n",file);
242 AliQADirList* tmp = (AliQADirList*)fPListOfFiles->GetDirs()->At(file);
243 if (!tmp) return kFALSE;
246 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
247 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
248 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
249 OpenCurrentDirectory();
253 //_________________________________________________________________________
254 Bool_t AliQAHistNavigator::SetDetector( TString det )
257 AliQADirList* tmp = (AliQADirList*)fPCurrFile->GetDirs()->FindObject( det.Data() );
258 if (!tmp) return kFALSE;
259 fPCurrDetector = tmp;
260 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
261 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
262 OpenCurrentDirectory();
266 //_________________________________________________________________________
267 Bool_t AliQAHistNavigator::SetDetector( Int_t det )
270 printf("AliQAHistNavigator::SetDetector(%i)\n",det);
271 AliQADirList* tmp = (AliQADirList*)fPCurrFile->GetDirs()->At( det );
272 if (!tmp) return kFALSE;
273 fPCurrDetector = tmp;
274 fPCurrLevel = (AliQADirList*)fPCurrDetector->GetDirs()->First();
275 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
276 OpenCurrentDirectory();
280 //_________________________________________________________________________
281 Bool_t AliQAHistNavigator::SetLevel( TString level )
284 AliQADirList* tmp = (AliQADirList*)fPCurrDetector->GetDirs()->FindObject( level.Data() );
285 if (!tmp) return kFALSE;
287 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
288 OpenCurrentDirectory();
292 //_________________________________________________________________________
293 Bool_t AliQAHistNavigator::SetLevel( Int_t level )
296 AliQADirList* tmp = (AliQADirList*)fPCurrDetector->GetDirs()->At( level );
297 if (!tmp) return kFALSE;
299 fPCurrItem = (AliQADirListItem*)GetItemList()->First();
300 OpenCurrentDirectory();
304 //_________________________________________________________________________
305 Bool_t AliQAHistNavigator::SetItem( TString hist )
308 AliQADirListItem* tmp = (AliQADirListItem*)GetItemList()->FindObject( hist.Data() );
309 if (!tmp) return kFALSE;
314 //_________________________________________________________________________
315 Bool_t AliQAHistNavigator::SetItem( Int_t hist )
318 AliQADirListItem* tmp = (AliQADirListItem*)GetItemList()->At( hist );
319 if (!tmp) return kFALSE;
324 //_________________________________________________________________________
325 TList* AliQAHistNavigator::GetItemList()
327 //returns the current list of histograms, if none, returns empty list
328 TList* itemlist=NULL;
331 AliQADirList* expertlist = (AliQADirList*)fPCurrLevel->GetDirs()->FindObject(fExpertDirName);
332 if (expertlist) itemlist = expertlist->GetItems();
335 //this is a bit of a hack, but it will always return something sensible
336 AliQADirListItem* it = (AliQADirListItem*)fPEmptyList->First();
337 it->SetParent(fPCurrLevel);
338 itemlist = fPEmptyList;
342 itemlist = fPCurrLevel->GetItems();
347 //_________________________________________________________________________
348 TString AliQAHistNavigator::GetDetectorName()
350 //Get name of current detector
351 if (!fPCurrDetector) return "";
352 TString name = fPCurrDetector->GetName();
356 //_________________________________________________________________________
357 TString AliQAHistNavigator::GetLevelName()
359 //Get name of current leve
360 if (!fPCurrLevel) return "";
361 TString name = fPCurrLevel->GetName();
365 //_________________________________________________________________________
366 TString AliQAHistNavigator::GetFileName()
368 //Get name of current file
369 if (!fPCurrFile) return "";
370 TString file = fPCurrFile->GetName();
374 //_________________________________________________________________________
375 TString AliQAHistNavigator::GetDirName()
377 //get the name of dir containing current item
378 if (!fPCurrItem) return "";
379 AliQADirList* d=(AliQADirList*)fPCurrItem->GetParent();
383 path = d->GetName() + path;
387 while (d->GetParent());
391 //_________________________________________________________________________
392 TString AliQAHistNavigator::GetPath(AliQADirListItem* const item)
394 //Get the full path to teh directory containing item
395 AliQADirList* d=item->GetParent();
396 TString path = "";//item->GetString();
399 TString sep = (d->GetParent()) ? "/" : ":/" ;
400 path = d->GetName() + sep + path;
402 while ((d=d->GetParent()));
406 //_________________________________________________________________________
407 TString AliQAHistNavigator::GetItemName()
409 //Get name of current item
410 if (!fPCurrItem) return "";
411 return fPCurrItem->GetString();
414 //_________________________________________________________________________
415 Bool_t AliQAHistNavigator::GetListOfFiles()
417 //scan directory for QA files
418 delete fPListOfFiles;
419 fPListOfFiles = new AliQADirList();
421 TString fileNameCORR;
422 TString fileNameQAResult;
425 gSystem->ExpandPathName(macdir);
427 void* dirhandle = gSystem->OpenDirectory(macdir.Data());
430 const char* filename;
436 reg+= (fRun==0) ? "[0-9].*" : revstr.Data();
438 TPRegexp reHist(reg);
439 TPRegexp reMerged("Merged.QA.Data.[0-9]*.root");
440 TPRegexp reCORR("CORR.QA.[0-9]*.root");
441 TPRegexp reQA("QA.root");
442 std::list<string> names;
443 while((filename = gSystem->GetDirEntry(dirhandle)) != 0)
445 if (reCORR.Match(filename))
447 fileNameCORR = filename;
450 if (reQA.Match(filename))
452 fileNameQAResult = filename;
455 if (reMerged.Match(filename))
458 names.push_back(filename);
461 if (reHist.Match(filename))
463 names.push_back(filename);
466 if (!fPCORRFile) fPCORRFile = new TFile(fileNameCORR,"READ");
467 if (!fPQAResultFile) fPQAResultFile = new TFile(fileNameQAResult,"READ");
470 printf("GetListOfFiles: no files matching...\n");
475 for (std::list<string>::iterator si=names.begin(); si!=names.end(); ++si)
477 sprintf(fullName,"%s", si->c_str());
478 AliQADirList* f = new AliQADirList();
479 f->SetName(fullName);
480 fPListOfFiles->GetDirs()->AddLast(f);
485 gSystem->FreeDirectory(dirhandle);
491 //_________________________________________________________________________
492 Bool_t AliQAHistNavigator::CloneDirStructure()
495 if (!GetListOfFiles()) return kFALSE;
496 if (fPListOfFiles->GetDirs()->GetEntries()==0) return kFALSE;
497 TIter fileiter(fPListOfFiles->GetDirs());
499 while ((f = (AliQADirList*)fileiter.Next()))
501 TString filename = f->GetName();
502 TFile file(filename);
503 if (!Crawl(f)) continue;
508 //_________________________________________________________________________
509 Bool_t AliQAHistNavigator::Crawl(AliQADirList* dir)
511 //scans the current directory and puts result in dir
512 //returns false if dir in file empty
515 TString pwd = gDirectory->GetPath();
516 TList* keys = gDirectory->GetListOfKeys();
517 if (!keys) return kFALSE;
518 if (keys->GetEntries()==0) return kFALSE;
521 while ((key = dynamic_cast <TKey* > (keyiter.Next()) ))
523 keyname = key->GetName();
524 if (keyname==oldkeyname) continue; //do not copy cycles
525 oldkeyname = keyname;
526 TString classname=key->GetClassName();
527 if (!classname) return kFALSE;
528 if (classname=="TDirectoryFile")
530 gDirectory->cd(key->GetName());
531 AliQADirList* newdir = new AliQADirList();
539 newdir->SetName(keyname);
540 newdir->SetParent(dir);
541 dir->GetDirs()->AddLast(newdir);
545 AliQADirListItem* item = new AliQADirListItem(keyname);
546 item->SetParent(dir);
547 dir->GetItems()->AddLast(item);
553 //_________________________________________________________________________
554 Bool_t AliQAHistNavigator::ReReadFiles()
556 //close, open and rescan the files
557 if (!CloneDirStructure())
559 printf("AliQAHistNavigator::AliQAHistNavigator(): error reading files\n");
562 fPCurrFile = (AliQADirList*)fPListOfFiles->GetDirs()->First();
565 fPCurrDetector = (AliQADirList*)fPCurrFile->GetDirs()->First();
568 fPCurrLevel = (AliQADirList*) fPCurrDetector->GetDirs()->First();
571 fPCurrItem = (AliQADirListItem*) GetItemList()->First();
576 OpenCurrentDirectory();
584 //_________________________________________________________________________
585 //_________________________________________________________________________
586 ClassImp(AliQADirList)
587 //_________________________________________________________________________
588 AliQADirList::AliQADirList():
591 fPItems(new TList()),
597 //_________________________________________________________________________
598 AliQADirList::~AliQADirList()
601 if (fPParent) delete fPParent;
606 //_________________________________________________________________________
607 ClassImp(AliQADirListItem)
608 //_________________________________________________________________________
609 AliQADirListItem::AliQADirListItem(const char* s):
616 //_________________________________________________________________________
617 AliQADirListItem::~AliQADirListItem()
620 if (fPParent) delete fPParent;