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 **************************************************************************/
16 ////////////////////////////////////////////////////////////////////////////
18 // support class for the QA histogram viewer
20 // origin: Mikolaj Krzewicki, Nikhef, Mikolaj.Krzewicki@cern.ch
22 ///////////////////////////////////////////////////////////////////////////
24 #include "AliQAHistNavigator.h"
26 ClassImp(AliQAHistNavigator)
28 //_________________________________________________________________________
29 AliQAHistNavigator::AliQAHistNavigator(Int_t run, Int_t rev):
34 fPCurrDetector( NULL ),
36 fPCurrHistName( NULL ),
37 fPListOfFiles( new TList() ),
39 fLoopAllDetectors(kTRUE),
40 fLoopAllLevels(kTRUE),
46 //_________________________________________________________________________
47 Bool_t AliQAHistNavigator::GetNextHistogram(TH1*& hist)
49 //moves to the next histogram from the list and tries to get it.
55 if (GetHistName()=="")
61 return GetHistogram(hist);
64 //_________________________________________________________________________
65 Bool_t AliQAHistNavigator::GetHistogram(TH1*& hist)
67 TString file = GetFileName();
68 TString dir = GetDirName();
69 TString histname = GetHistName();
70 if (file==""||dir==""||histname=="")
72 printf("GetHistogram: nothing to fetch...\n");
75 gDirectory->GetObject(file+":"+dir+"/"+histname,hist);
78 printf("GetHistogram: null pointer returned by gDirectory\n");
84 //_________________________________________________________________________
85 Bool_t AliQAHistNavigator::GetPrevHistogram(TH1*& hist)
87 //moves to the prev histogram from the list and tries to get it.
93 if (GetHistName()=="")
99 return GetHistogram(hist);
102 //_________________________________________________________________________
103 Bool_t AliQAHistNavigator::Next()
105 if (!fPCurrHistName||!fPCurrFile||!fPCurrDetector||!fPCurrLevel) return kFALSE;
106 if (!(fPCurrHistName=(TObjString*)fPCurrLevel->After(fPCurrHistName)))
108 if (!(fPCurrLevel=(TList*)fPCurrDetector->After(fPCurrLevel)))
110 if (!(fPCurrDetector=(TList*)fPCurrFile->After(fPCurrDetector)))
112 if (!(fPCurrFile=(TList*)fPListOfFiles->After(fPCurrFile)))
114 //we're at the end of everything
117 //rewind to the beginning
118 fPCurrFile = (TList*)fPListOfFiles->First();
119 fPCurrDetector = (TList*)fPCurrFile->First();
120 fPCurrLevel = (TList*) fPCurrDetector->First();
121 fPCurrHistName = (TObjString*) fPCurrLevel->First();
123 OpenCurrentDirectory();
124 printf("----------------back at the beginning!\n");
125 } else return kFALSE; //no rewind, we finish
126 } else //if there is a next file
128 fPCurrDetector = (TList*)fPCurrFile->First();
129 fPCurrLevel=(TList*)fPCurrDetector->First();
130 fPCurrHistName=(TObjString*)fPCurrLevel->First();
131 cout<<GetFileName()<<":"<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
133 OpenCurrentDirectory();
135 } else //if there is a next detector
137 fPCurrLevel=(TList*)fPCurrDetector->First();
138 fPCurrHistName=(TObjString*)fPCurrLevel->First();
139 cout<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
140 OpenCurrentDirectory();
142 } else //if there is a next level
144 fPCurrHistName=(TObjString*)fPCurrLevel->First();
145 cout<<GetLevelName()<<"/"<<GetHistName()<<endl;
146 OpenCurrentDirectory();
147 cout<<GetHistName()<<endl;
149 } else //if there is a next histgram
151 cout<<GetHistName()<<endl;
156 //_________________________________________________________________________
157 Bool_t AliQAHistNavigator::Prev()
159 if (!fPCurrHistName) return kFALSE;
160 if (!(fPCurrHistName=(TObjString*)fPCurrLevel->Before(fPCurrHistName)))
162 if (!(fPCurrLevel=(TList*)fPCurrDetector->Before(fPCurrLevel)))
164 if (!(fPCurrDetector=(TList*)fPCurrFile->Before(fPCurrDetector)))
166 if (!(fPCurrFile=(TList*)fPListOfFiles->Before(fPCurrFile)))
168 //we're at the end of everything
171 //rewind to the beginning
172 fPCurrFile = (TList*)fPListOfFiles->Last();
173 fPCurrDetector = (TList*)fPCurrFile->Last();
174 fPCurrLevel = (TList*) fPCurrDetector->Last();
175 fPCurrHistName = (TObjString*) fPCurrLevel->Last();
177 OpenCurrentDirectory();
178 printf("----------------back at the end!\n");
179 } else return kFALSE; //no rewind, we finish
180 } else //if there is a next file
182 fPCurrDetector = (TList*)fPCurrFile->Last();
183 fPCurrLevel=(TList*)fPCurrDetector->Last();
184 fPCurrHistName=(TObjString*)fPCurrLevel->Last();
185 cout<<GetFileName()<<":"<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
187 OpenCurrentDirectory();
189 } else //if there is a next detector
191 fPCurrLevel=(TList*)fPCurrDetector->Last();
192 fPCurrHistName=(TObjString*)fPCurrLevel->Last();
193 cout<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
194 OpenCurrentDirectory();
196 } else //if there is a next level
198 fPCurrHistName=(TObjString*)fPCurrLevel->Last();
199 cout<<GetLevelName()<<"/"<<GetHistName()<<endl;
200 OpenCurrentDirectory();
201 cout<<GetHistName()<<endl;
203 } else //if there is a next histgram
205 cout<<GetHistName()<<endl;
210 //_________________________________________________________________________
211 Bool_t AliQAHistNavigator::OpenCurrentFile()
213 if (fPFile) fPFile->Close();
214 if (!(fPFile->Open(GetFileName(),"READ")))
217 cout<<"There is no file: "<<GetFileName()<<endl;
222 //_________________________________________________________________________
223 Bool_t AliQAHistNavigator::OpenCurrentDirectory()
225 if (!gDirectory->cd(GetDirName())) return kFALSE;
229 //_________________________________________________________________________
230 Bool_t AliQAHistNavigator::SetFile( TString file )
232 TList* tmp = (TList*)fPListOfFiles->FindObject ( file.Data() );
233 if (!tmp) return kFALSE;
236 fPCurrDetector = (TList*)fPCurrFile->First();
237 fPCurrLevel = (TList*)fPCurrDetector->First();
238 fPCurrHistName = (TObjString*)fPCurrLevel->First();
242 //_________________________________________________________________________
243 Bool_t AliQAHistNavigator::SetFile( Int_t file )
245 printf("AliQAHistNavigator::SetFile(%i)\n",file);
246 TList* tmp = (TList*)fPListOfFiles->At(file);
247 if (!tmp) return kFALSE;
250 fPCurrDetector = (TList*)fPCurrFile->First();
251 fPCurrLevel = (TList*)fPCurrDetector->First();
252 fPCurrHistName = (TObjString*)fPCurrLevel->First();
253 OpenCurrentDirectory();
257 //_________________________________________________________________________
258 Bool_t AliQAHistNavigator::SetDetector( TString det )
260 TList* tmp = (TList*)fPCurrFile->FindObject( det.Data() );
261 if (!tmp) return kFALSE;
262 fPCurrDetector = tmp;
263 fPCurrLevel = (TList*)fPCurrDetector->First();
264 fPCurrHistName = (TObjString*)fPCurrLevel->First();
265 OpenCurrentDirectory();
269 //_________________________________________________________________________
270 Bool_t AliQAHistNavigator::SetDetector( Int_t det )
272 printf("AliQAHistNavigator::SetDetector(%i)\n",det);
273 TList* tmp = (TList*)fPCurrFile->At( det );
274 if (!tmp) return kFALSE;
275 fPCurrDetector = tmp;
276 fPCurrLevel = (TList*)fPCurrDetector->First();
277 fPCurrHistName = (TObjString*)fPCurrLevel->First();
278 OpenCurrentDirectory();
282 //_________________________________________________________________________
283 Bool_t AliQAHistNavigator::SetLevel( TString level )
285 TList* tmp = (TList*)fPCurrDetector->FindObject( level.Data() );
286 if (!tmp) return kFALSE;
288 fPCurrHistName = (TObjString*)fPCurrLevel->First();
289 OpenCurrentDirectory();
293 //_________________________________________________________________________
294 Bool_t AliQAHistNavigator::SetLevel( Int_t level )
296 TList* tmp = (TList*)fPCurrDetector->At( level );
297 if (!tmp) return kFALSE;
299 fPCurrHistName = (TObjString*)fPCurrLevel->First();
300 OpenCurrentDirectory();
304 //_________________________________________________________________________
305 Bool_t AliQAHistNavigator::SetHist( TString hist )
307 TObjString* tmp = (TObjString*)fPCurrLevel->FindObject( hist.Data() );
308 if (!tmp) return kFALSE;
309 fPCurrHistName = tmp;
313 //_________________________________________________________________________
314 Bool_t AliQAHistNavigator::SetHist( Int_t hist )
316 TObjString* tmp = (TObjString*)fPCurrLevel->At( hist );
317 if (!tmp) return kFALSE;
318 fPCurrHistName = tmp;
322 //_________________________________________________________________________
323 void AliQAHistNavigator::PrintDebugInfo()
325 if (!fPCurrHistName) {cout<<"no more histograms"<<endl;return;};
326 if (!fPCurrLevel) {cout<<"no more levels"<<endl;return;};
327 if (!fPCurrDetector) {cout<<"no more detectors"<<endl;return;};
328 cout<<"AliQAHistNavigator state information:"<<endl;
329 cout<<"hist: "<<GetHistName()<<endl;
330 cout<<"dir: "<<GetDirName()<<endl;
331 cout<<"$PWD: ";gDirectory->pwd();cout<<endl;
334 //_________________________________________________________________________
335 TString AliQAHistNavigator::GetDetectorName()
337 if (!fPCurrDetector) return "";
338 TString name = fPCurrDetector->GetName();
342 //_________________________________________________________________________
343 TString AliQAHistNavigator::GetLevelName()
345 if (!fPCurrLevel) return "";
346 TString name = fPCurrLevel->GetName();
350 //_________________________________________________________________________
351 TString AliQAHistNavigator::GetFileName()
353 if (!fPCurrFile) return "";
354 TString file = fPCurrFile->GetName();
358 //_________________________________________________________________________
359 TString AliQAHistNavigator::GetDirName()
361 TString detector = GetDetectorName();
362 TString level = GetLevelName();
363 if (detector==""||level=="") return "";
364 TString dir = "/"+ detector +"/"+ level;
368 //_________________________________________________________________________
369 TString AliQAHistNavigator::GetHistName()
371 if (!fPCurrHistName) return "";
372 return fPCurrHistName->GetString();
375 //_________________________________________________________________________
376 Bool_t AliQAHistNavigator::DumpList( TString filename )
378 ofstream fout(filename);
379 if (fout.bad()) return kFALSE;
380 TString lastlevel="";
382 if (!Next()) return kTRUE;
385 if (GetLevelName()!=lastlevel)
387 fout<<GetDetectorName()<<"/"<<GetLevelName()<<":"<<endl;
388 lastlevel=GetLevelName();
390 fout << GetHistName() << endl;
396 //_________________________________________________________________________
397 Bool_t AliQAHistNavigator::ReadList( TString filename )
399 TString line = filename;
401 //TString lastlevel="";
403 //TString lastdet="";
404 //TRegexp detRegexp("^[a-zA-Z0-9]*");
405 //TRegexp typRegexp("[a-zA-Z0-9]*");
407 //ifstream fin(filename);
408 //if (fin.bad()) return kFALSE;
409 //delete fPListOfFiles;
410 //fPListOfFiles = new TList();
411 //TList* pDetector=new TList();
412 //TList* pLevel=new TList();
414 //line.ReadLine(fin,kFALSE);
415 //if (line=="") return kFALSE;
416 //cout<<"read line: "<<line<<endl;
417 //det = line(detRegexp);
418 //level = line(typRegexp, line.Index("/")+1);
419 //pDetector->SetName(det);
421 //pLevel->SetName(level);
423 //pLevel->AddLast(new TObjString("DO NOT REMOVE THIS LINE"));
427 // line.ReadLine(fin,kFALSE);
428 // cout<<"read line: "<<line<<endl;
429 // if (line.EndsWith(":"))
431 // det = line(detRegexp);
432 // level = line(typRegexp, line.Index("/")+1);
435 // pDetector->AddLast(pLevel);
436 // fPListOfFiles->AddLast(pDetector);
437 // pDetector = new TList();
438 // pDetector->SetName(det);
439 // cout<<"new detector: "<<det<<endl;
441 // pLevel = new TList();
442 // pLevel->SetName(level);
443 // cout<<"new level: "<<level<<endl;
444 // lastlevel = level;
447 // if (level!=lastlevel)
449 // pDetector->AddLast(pLevel);
450 // pLevel = new TList();
451 // pLevel->SetName(level);
452 // cout<<"new level: "<<level<<endl;
453 // lastlevel = level;
457 // if (line.BeginsWith("//")) continue;
458 // if (line.BeginsWith("#")) continue;
459 // pLevel->AddLast(new TObjString(line));
460 // cout<<"added line: "<<line<<endl;
463 //fPCurrDetector = (TList*)fPListOfFiles->First();
464 //fPCurrLevel = (TList*) fPCurrDetector->First();
465 //fPCurrHistName = (TObjString*) fPCurrLevel->First();
467 //OpenCurrentDirectory();
468 //fPListOfFiles->Print();
472 //_________________________________________________________________________
473 Bool_t AliQAHistNavigator::GetListOfFiles()
475 delete fPListOfFiles;
476 fPListOfFiles = new TList();
479 gSystem->ExpandPathName(macdir);
481 void* dirhandle = gSystem->OpenDirectory(macdir.Data());
484 const char* filename;
491 reg+= (fRun==0) ? "[0-9].*" : runstr.Data();
493 reg+= (fRun==0) ? "[0-9].*" : revstr.Data();
497 std::list<string> names;
498 while((filename = gSystem->GetDirEntry(dirhandle)) != 0)
500 if(re.Match(filename))
502 names.push_back(filename);
507 printf("GetListOfFiles: no files matching...\n");
512 for (std::list<string>::iterator si=names.begin(); si!=names.end(); ++si)
514 sprintf(fullName,"%s", si->c_str());
515 TList* f = new TList();
516 f->SetName(fullName);
517 fPListOfFiles->AddLast(f);
522 gSystem->FreeDirectory(dirhandle);
528 //_________________________________________________________________________
529 Bool_t AliQAHistNavigator::CloneDirStructure()
531 if (!GetListOfFiles()) return kFALSE;
532 if (fPListOfFiles->GetEntries()==0) return kFALSE;
533 TIter fileiter(fPListOfFiles);
535 while ((f = (TList*)fileiter.Next()))
537 TString filename = f->GetName();
538 cout<<filename<<endl;
539 TFile file(filename);
540 if (!Crawl(f)) continue;
545 //_________________________________________________________________________
546 Bool_t AliQAHistNavigator::Crawl(TList* dir)
548 TString pwd = gDirectory->GetPath();
549 //returns false if dir in file empty
550 TList* keys = gDirectory->GetListOfKeys();
551 if (!keys) return kFALSE;
552 if (keys->GetEntries()==0) return kFALSE;
555 while ((key = dynamic_cast <TKey* > (keyiter.Next()) ))
557 TString classname=key->GetClassName();
558 if (!classname) return kFALSE;
559 if (classname=="TDirectoryFile")
561 gDirectory->cd(key->GetName());
563 TList* newdir = new TList();
571 newdir->SetName(key->GetName());
572 dir->AddLast(newdir);
576 cout<<key->GetName()<<endl;
577 dir->AddLast(new TObjString(key->GetName()));
583 Bool_t AliQAHistNavigator::ReReadFiles()
585 if (CloneDirStructure())
587 fPCurrFile = (TList*)fPListOfFiles->First();
590 fPCurrDetector = (TList*)fPCurrFile->First();
593 fPCurrLevel = (TList*) fPCurrDetector->First();
596 fPCurrHistName = (TObjString*) fPCurrLevel->First();
601 OpenCurrentDirectory();
608 printf("AliQAHistNavigator::AliQAHistNavigator(): error reading files\n");