New code for visualization of QA histos. Will be embedded in AliEve (Mikolaj)
[u/mrichter/AliRoot.git] / MONITOR / AliQAHistNavigator.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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
16 ////////////////////////////////////////////////////////////////////////////
17 //
18 //  support class for the QA histogram viewer
19 //
20 //  origin: Mikolaj Krzewicki, Nikhef, Mikolaj.Krzewicki@cern.ch
21 //
22 ///////////////////////////////////////////////////////////////////////////
23
24 #include "AliQAHistNavigator.h"
25
26 ClassImp(AliQAHistNavigator)
27
28 //_________________________________________________________________________
29 AliQAHistNavigator::AliQAHistNavigator(Int_t run, Int_t rev):
30     fPFile( NULL ),
31     fRun( run ),
32     fCyc( rev ),
33     fPCurrFile( NULL ),
34     fPCurrDetector( NULL ),
35     fPCurrLevel( NULL ),
36     fPCurrHistName( NULL ),
37     fPListOfFiles( new TList() ),
38     fLoopAllFiles(kTRUE),
39     fLoopAllDetectors(kTRUE),
40     fLoopAllLevels(kTRUE)
41 {
42     if (CloneDirStructure())
43     {
44         fPCurrFile = (TList*)fPListOfFiles->First();
45         if (fPCurrFile)
46         {
47             fPCurrDetector = (TList*)fPCurrFile->First();
48             if (fPCurrDetector)
49             {
50                 fPCurrLevel = (TList*) fPCurrDetector->First();
51                 if (fPCurrLevel)
52                 {
53                     fPCurrHistName = (TObjString*) fPCurrLevel->First();
54                     if (fPCurrHistName)
55                     {
56                         OpenCurrentFile();
57                         OpenCurrentDirectory();
58                     }
59                 }
60             }
61         }
62     } else
63     {
64         printf("AliQAHistNavigator::AliQAHistNavigator(): error reading files\n");
65     }
66     
67 }
68
69 //_________________________________________________________________________
70 Bool_t AliQAHistNavigator::GetNextHistogram(TH1*& hist)
71 {
72     //moves to the next histogram from the list and tries to get it.
73     if (!Next()) 
74     {
75         hist = NULL;
76         return kFALSE;
77     }
78     if (GetHistName()=="")
79         if (!Next())
80         {
81             hist = NULL;
82             return kTRUE;
83         }
84     return GetHistogram(hist);
85 }
86
87 //_________________________________________________________________________
88 Bool_t AliQAHistNavigator::GetHistogram(TH1*& hist)
89 {
90     gDirectory->GetObject(GetFileName()+":"+GetDirName()+"/"+GetHistName(),hist);
91     if (!hist)
92     {
93         printf("GetHistogram: null pointer returned by gDirectory\n");
94         return kFALSE;
95     }
96     return kTRUE;
97 }
98
99 //_________________________________________________________________________
100 Bool_t AliQAHistNavigator::GetPrevHistogram(TH1*& hist)
101 {
102     //moves to the prev histogram from the list and tries to get it.
103     if (!Prev()) 
104     {
105         hist = NULL;
106         return kFALSE;
107     }
108     if (GetHistName()=="")
109         if (!Prev())
110         {
111             hist = NULL;
112             return kTRUE;
113         }
114     return GetHistogram(hist);
115 }
116
117 //_________________________________________________________________________
118 Bool_t AliQAHistNavigator::Next()
119 {
120     if (!fPCurrHistName) return kFALSE;
121     if (!(fPCurrHistName=(TObjString*)fPCurrLevel->After(fPCurrHistName)))
122     {
123         if (!(fPCurrLevel=(TList*)fPCurrDetector->After(fPCurrLevel)))
124         {
125             if (!(fPCurrDetector=(TList*)fPCurrFile->After(fPCurrDetector)))
126             {
127                 if (!(fPCurrFile=(TList*)fPListOfFiles->After(fPCurrFile)))
128                 {
129                     //we're at the end of everything
130                     if (fLoopAllFiles)
131                     {
132                         //rewind to the beginning
133                         fPCurrFile = (TList*)fPListOfFiles->First();
134                         fPCurrDetector = (TList*)fPCurrFile->First();
135                         fPCurrLevel = (TList*) fPCurrDetector->First();
136                         fPCurrHistName = (TObjString*) fPCurrLevel->First();
137                         OpenCurrentFile();
138                         OpenCurrentDirectory();
139                         printf("----------------back at the beginning!\n");
140                     } else return kFALSE; //no rewind, we finish
141                 } else //if there is a next file
142                 {
143                     fPCurrDetector = (TList*)fPCurrFile->First();
144                     fPCurrLevel=(TList*)fPCurrDetector->First();
145                     fPCurrHistName=(TObjString*)fPCurrLevel->First();
146                     cout<<GetFileName()<<":"<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
147                     OpenCurrentFile();
148                     OpenCurrentDirectory();
149                 }
150             } else //if there is a next detector
151             {
152                 fPCurrLevel=(TList*)fPCurrDetector->First();
153                 fPCurrHistName=(TObjString*)fPCurrLevel->First();
154                 cout<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
155                 OpenCurrentDirectory();
156             }
157         } else //if there is a next level
158         {
159             fPCurrHistName=(TObjString*)fPCurrLevel->First();
160             cout<<GetLevelName()<<"/"<<GetHistName()<<endl;
161             OpenCurrentDirectory();
162             cout<<GetHistName()<<endl;
163         }
164     } else //if there is a next histgram
165     {
166         cout<<GetHistName()<<endl;
167     }
168     return kTRUE;
169 }
170
171 //_________________________________________________________________________
172 Bool_t AliQAHistNavigator::Prev()
173 {
174     if (!fPCurrHistName) return kFALSE;
175     if (!(fPCurrHistName=(TObjString*)fPCurrLevel->Before(fPCurrHistName)))
176     {
177         if (!(fPCurrLevel=(TList*)fPCurrDetector->Before(fPCurrLevel)))
178         {
179             if (!(fPCurrDetector=(TList*)fPCurrFile->Before(fPCurrDetector)))
180             {
181                 if (!(fPCurrFile=(TList*)fPListOfFiles->Before(fPCurrFile)))
182                 {
183                     //we're at the end of everything
184                     if (fLoopAllFiles)
185                     {
186                         //rewind to the beginning
187                         fPCurrFile = (TList*)fPListOfFiles->Last();
188                         fPCurrDetector = (TList*)fPCurrFile->Last();
189                         fPCurrLevel = (TList*) fPCurrDetector->Last();
190                         fPCurrHistName = (TObjString*) fPCurrLevel->Last();
191                         OpenCurrentFile();
192                         OpenCurrentDirectory();
193                         printf("----------------back at the end!\n");
194                     } else return kFALSE; //no rewind, we finish
195                 } else //if there is a next file
196                 {
197                     fPCurrDetector = (TList*)fPCurrFile->Last();
198                     fPCurrLevel=(TList*)fPCurrDetector->Last();
199                     fPCurrHistName=(TObjString*)fPCurrLevel->Last();
200                     cout<<GetFileName()<<":"<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
201                     OpenCurrentFile();
202                     OpenCurrentDirectory();
203                 }
204             } else //if there is a next detector
205             {
206                 fPCurrLevel=(TList*)fPCurrDetector->Last();
207                 fPCurrHistName=(TObjString*)fPCurrLevel->Last();
208                 cout<<GetDetectorName()<<"/"<<GetLevelName()<<"/"<<GetHistName()<<endl;
209                 OpenCurrentDirectory();
210             }
211         } else //if there is a next level
212         {
213             fPCurrHistName=(TObjString*)fPCurrLevel->Last();
214             cout<<GetLevelName()<<"/"<<GetHistName()<<endl;
215             OpenCurrentDirectory();
216             cout<<GetHistName()<<endl;
217         }
218     } else //if there is a next histgram
219     {
220         cout<<GetHistName()<<endl;
221     }
222     return kTRUE;
223 }
224
225 //_________________________________________________________________________
226 Bool_t AliQAHistNavigator::OpenCurrentFile()
227 {
228     if (fPFile) fPFile->Close();
229     if (!(fPFile->Open(GetFileName(),"READ")))
230     {
231         return kFALSE;
232         cout<<"There is no file: "<<GetFileName()<<endl;
233     }
234     return kTRUE;
235 }
236
237 //_________________________________________________________________________
238 Bool_t AliQAHistNavigator::OpenCurrentDirectory()
239 {
240     if (!gDirectory->cd(GetDirName())) return kFALSE;
241     return kTRUE;
242 }
243
244 //_________________________________________________________________________
245 Bool_t AliQAHistNavigator::SetFile( TString file )
246 {
247     TList* tmp = (TList*)fPListOfFiles->FindObject ( file.Data() );
248     if (!tmp) return kFALSE;
249     fPCurrFile = tmp;
250     OpenCurrentFile();
251     fPCurrDetector = (TList*)fPCurrFile->First();
252     fPCurrLevel = (TList*)fPCurrDetector->First();
253     fPCurrHistName = (TObjString*)fPCurrLevel->First();
254     return kTRUE;
255 }
256
257 //_________________________________________________________________________
258 Bool_t AliQAHistNavigator::SetFile( Int_t file )
259 {
260     printf("AliQAHistNavigator::SetFile(%i)\n",file);
261     TList* tmp = (TList*)fPListOfFiles->At(file);
262     if (!tmp) return kFALSE;
263     fPCurrFile = tmp;
264     OpenCurrentFile();
265     fPCurrDetector = (TList*)fPCurrFile->First();
266     fPCurrLevel = (TList*)fPCurrDetector->First();
267     fPCurrHistName = (TObjString*)fPCurrLevel->First();
268     OpenCurrentDirectory();
269     return kTRUE;
270 }
271
272 //_________________________________________________________________________
273 Bool_t AliQAHistNavigator::SetDetector( TString det )
274 {
275     TList* tmp = (TList*)fPCurrFile->FindObject( det.Data() );
276     if (!tmp) return kFALSE;
277     fPCurrDetector = tmp;
278     fPCurrLevel = (TList*)fPCurrDetector->First();
279     fPCurrHistName = (TObjString*)fPCurrLevel->First();
280     OpenCurrentDirectory();
281     return kTRUE;
282 }
283
284 //_________________________________________________________________________
285 Bool_t AliQAHistNavigator::SetDetector( Int_t det )
286 {
287     printf("AliQAHistNavigator::SetDetector(%i)\n",det);
288     TList* tmp = (TList*)fPCurrFile->At( det );
289     if (!tmp) return kFALSE;
290     fPCurrDetector = tmp;
291     fPCurrLevel = (TList*)fPCurrDetector->First();
292     fPCurrHistName = (TObjString*)fPCurrLevel->First();
293     OpenCurrentDirectory();
294     return kTRUE;
295 }
296
297 //_________________________________________________________________________
298 Bool_t AliQAHistNavigator::SetLevel( TString level )
299 {
300     TList* tmp = (TList*)fPCurrDetector->FindObject( level.Data() );
301     if (!tmp) return kFALSE;
302     fPCurrLevel = tmp;
303     fPCurrHistName = (TObjString*)fPCurrLevel->First();
304     OpenCurrentDirectory();
305     return kTRUE;
306 }
307
308 //_________________________________________________________________________
309 Bool_t AliQAHistNavigator::SetLevel( Int_t level )
310 {
311     TList* tmp = (TList*)fPCurrDetector->At( level );
312     if (!tmp) return kFALSE;
313     fPCurrLevel = tmp;
314     fPCurrHistName = (TObjString*)fPCurrLevel->First();
315     OpenCurrentDirectory();
316     return kTRUE;
317 }
318
319 //_________________________________________________________________________
320 Bool_t AliQAHistNavigator::SetHist( TString hist )
321 {
322     TObjString* tmp = (TObjString*)fPCurrLevel->FindObject( hist.Data() );
323     if (!tmp) return kFALSE;
324     fPCurrHistName = tmp;
325     return kTRUE;
326 }
327
328 //_________________________________________________________________________
329 Bool_t AliQAHistNavigator::SetHist( Int_t hist )
330 {
331     TObjString* tmp = (TObjString*)fPCurrLevel->At( hist );
332     if (!tmp) return kFALSE;
333     fPCurrHistName = tmp;
334     return kTRUE;
335 }
336
337 //_________________________________________________________________________
338 void AliQAHistNavigator::PrintDebugInfo()
339 {
340     if (!fPCurrHistName) {cout<<"no more histograms"<<endl;return;};
341     if (!fPCurrLevel) {cout<<"no more levels"<<endl;return;};
342     if (!fPCurrDetector) {cout<<"no more detectors"<<endl;return;};
343     cout<<"AliQAHistNavigator state information:"<<endl;
344     cout<<"hist: "<<GetHistName()<<endl;
345     cout<<"dir:  "<<GetDirName()<<endl;
346     cout<<"$PWD: ";gDirectory->pwd();cout<<endl;
347 }
348
349 //_________________________________________________________________________
350 TString AliQAHistNavigator::GetDetectorName()
351 {
352     if (!fPCurrDetector) return "";
353     TString name = fPCurrDetector->GetName();
354     return name;
355 }
356
357 //_________________________________________________________________________
358 TString AliQAHistNavigator::GetLevelName()
359 {
360     if (!fPCurrLevel) return "";
361     TString name = fPCurrLevel->GetName();
362     return name;
363 }
364
365 //_________________________________________________________________________
366 TString AliQAHistNavigator::GetFileName()
367 {
368     if (!fPCurrFile) return "";
369     TString file = fPCurrFile->GetName();
370     return file;
371 }
372
373 //_________________________________________________________________________
374 TString AliQAHistNavigator::GetDirName()
375 {
376     TString dir = "/"+ GetDetectorName()+"/"+GetLevelName();
377     return dir;
378 }
379
380 //_________________________________________________________________________
381 TString AliQAHistNavigator::GetHistName()
382 {
383     if (!fPCurrHistName) return "";
384     return fPCurrHistName->GetString();
385 }
386
387 //_________________________________________________________________________
388 Bool_t AliQAHistNavigator::DumpList( TString filename )
389 {
390     ofstream fout(filename);
391     if (fout.bad()) return kFALSE;
392     TString lastlevel="";
393     TString lastdet="";
394     if (!Next()) return kTRUE;
395     do
396     {
397         if (GetLevelName()!=lastlevel)
398         {
399             fout<<GetDetectorName()<<"/"<<GetLevelName()<<":"<<endl;
400             lastlevel=GetLevelName();
401         }
402         fout << GetHistName() << endl;
403     } while (Next());
404     fout.close();
405     return kTRUE;
406 }
407
408 //_________________________________________________________________________
409 Bool_t AliQAHistNavigator::ReadList( TString filename )
410 {
411     TString line = filename;
412     //TString level="";
413     //TString lastlevel="";
414     //TString det="";
415     //TString lastdet="";
416     //TRegexp detRegexp("^[a-zA-Z0-9]*");
417     //TRegexp typRegexp("[a-zA-Z0-9]*");
418
419     //ifstream fin(filename);
420     //if (fin.bad()) return kFALSE;
421     //delete fPListOfFiles;
422     //fPListOfFiles = new TList();
423     //TList* pDetector=new TList();
424     //TList* pLevel=new TList();
425     //
426     //line.ReadLine(fin,kFALSE);
427     //if (line=="") return kFALSE;
428     //cout<<"read line: "<<line<<endl;
429     //det = line(detRegexp);
430     //level = line(typRegexp, line.Index("/")+1);
431     //pDetector->SetName(det);
432     //lastdet = det;
433     //pLevel->SetName(level);
434     //lastlevel = level;
435     //pLevel->AddLast(new TObjString("DO NOT REMOVE THIS LINE"));
436     //
437     //while (!fin.eof())
438     //{
439     //    line.ReadLine(fin,kFALSE);
440     //    cout<<"read line: "<<line<<endl;
441     //    if (line.EndsWith(":"))
442     //    {
443     //        det = line(detRegexp);
444     //        level = line(typRegexp, line.Index("/")+1);
445     //        if (det!=lastdet)
446     //        {
447     //            pDetector->AddLast(pLevel);
448     //            fPListOfFiles->AddLast(pDetector);
449     //            pDetector = new TList();
450     //            pDetector->SetName(det);
451     //            cout<<"new detector: "<<det<<endl;
452     //            lastdet = det;
453     //            pLevel = new TList();
454     //            pLevel->SetName(level);
455     //            cout<<"new level: "<<level<<endl;
456     //            lastlevel = level;
457     //            continue;
458     //        }
459     //        if (level!=lastlevel)
460     //        {
461     //            pDetector->AddLast(pLevel);
462     //            pLevel = new TList();
463     //            pLevel->SetName(level);
464     //            cout<<"new level: "<<level<<endl;
465     //            lastlevel = level;
466     //            continue;
467     //        }
468     //    }
469     //    if (line.BeginsWith("//")) continue;
470     //    if (line.BeginsWith("#")) continue;
471     //    pLevel->AddLast(new TObjString(line));        
472     //    cout<<"added line: "<<line<<endl;
473     //}
474     //
475     //fPCurrDetector = (TList*)fPListOfFiles->First();
476     //fPCurrLevel = (TList*) fPCurrDetector->First();
477     //fPCurrHistName = (TObjString*) fPCurrLevel->First();
478     //OpenCurrentFile();
479     //OpenCurrentDirectory();
480     //fPListOfFiles->Print();
481     return kFALSE;
482 }
483
484 //_________________________________________________________________________
485 Bool_t AliQAHistNavigator::GetListOfFiles()
486 {
487     delete fPListOfFiles;
488     fPListOfFiles = new TList();
489
490     TString macdir(".");
491     gSystem->ExpandPathName(macdir);
492
493     void* dirhandle = gSystem->OpenDirectory(macdir.Data());
494     if(dirhandle != 0)
495     {
496         const char* filename;
497         TString runstr = "";
498         TString revstr = "";
499         runstr += fRun;
500         revstr += fCyc;
501         TString reg;
502         reg+= ".*QA\\.";
503         reg+= (fRun==0) ? "[0-9].*" : runstr.Data();
504         reg+= "\\.";
505         reg+= (fRun==0) ? "[0-9].*" : revstr.Data();
506         reg+= "\\.root$";
507         cout<<reg<<endl;
508         TPRegexp re(reg);
509         std::list<string> names;
510         while((filename = gSystem->GetDirEntry(dirhandle)) != 0)
511         {
512             if(re.Match(filename))
513             {
514                 names.push_back(filename);
515             }
516         }
517         if (names.empty())
518         {
519             printf("GetListOfFiles: no files matching...\n");
520             return kFALSE;
521         }
522         names.sort();
523         char fullName[1000];
524         for (std::list<string>::iterator si=names.begin(); si!=names.end(); ++si)
525         {
526           sprintf(fullName,"%s", si->c_str());
527           TList* f = new TList();
528           f->SetName(fullName);
529           fPListOfFiles->AddLast(f);
530         }
531     }
532     else
533     {
534         gSystem->FreeDirectory(dirhandle);
535         return kFALSE;
536     }
537     return kTRUE;
538 }
539
540 //_________________________________________________________________________
541 Bool_t AliQAHistNavigator::CloneDirStructure()
542 {
543     if (!GetListOfFiles()) return kFALSE;
544     if (fPListOfFiles->GetEntries()==0) return kFALSE;
545     TIter fileiter(fPListOfFiles);
546     TList* f;
547     while ((f = (TList*)fileiter.Next()))
548     {
549         TString filename = f->GetName();
550         cout<<filename<<endl;
551         TFile file(filename);
552         if (!Crawl(f)) continue;
553     }
554     return kTRUE;
555 }
556
557 //_________________________________________________________________________
558 Bool_t AliQAHistNavigator::Crawl(TList* dir)
559 {
560     TString pwd = gDirectory->GetPath();
561     //returns false if dir in file empty
562     TList* keys = gDirectory->GetListOfKeys();
563     if (!keys) return kFALSE;
564     if (keys->GetEntries()==0) return kFALSE;
565     TIter keyiter(keys);
566     TKey* key;
567     while ((key = dynamic_cast <TKey* > (keyiter.Next()) ))
568     {
569         TString classname=key->GetClassName();
570         if (!classname) return kFALSE;
571         if (classname=="TDirectoryFile")
572         {
573             gDirectory->cd(key->GetName());
574             gDirectory->pwd();
575             TList* newdir = new TList();
576             if (!Crawl(newdir))
577             {
578                 gDirectory->cd(pwd);
579                 continue;
580             }
581             gDirectory->cd(pwd);
582
583             newdir->SetName(key->GetName());
584             dir->AddLast(newdir);
585         }
586         else
587         {
588             cout<<key->GetName()<<endl;
589             dir->AddLast(new TObjString(key->GetName()));
590         }
591     }
592     return kTRUE;
593 }