ROOT logo
/**
 * @file   RunQA.C
 * @author Christian Holm Christensen <cholm@nbi.dk>
 * @date   Thu Nov 17 11:35:08 2011
 * 
 * @brief  Script to run the QATrender and QAPlotter in one go
 * 
 * @ingroup pwglf_forward_qa_scripts
 */
/** 
 * Scan directory (and possibly sub-directories) for trending files
 * 
 * @param dir        Start directory
 * @param list       List to add file names to
 * @param recursive  Whether to scan recursively
 * 
 * @return true on success
 * 
 * @deprecated Use the RunFileQA and RunFinalQA instead. 
 * @ingroup pwglf_forward_qa_scripts
 */
Bool_t ScanDirectory(TSystemDirectory* dir, TList* list, 
		     bool recursive=false)
{
  TString fnPattern = "trending_";

  // Assume failure 
  Bool_t ret = false;

  // Get list of files, and go back to old working directory
  TString oldDir(gSystem->WorkingDirectory());
  TList*  files = dir->GetListOfFiles();
  if (!gSystem->ChangeDirectory(oldDir)) { 
    Error("ScanDirectory", "Failed to go back to %s", oldDir.Data());
    return false;
  }
  if (!files) {
    Warning("ScanDirectory", "No files found in %s", dir->GetName());
    return false;
  }
  // files->ls();

  TList toAdd;
    
  // Sort list of files and check if we should add it 
  // files->Sort();
  TIter next(files);
  TSystemFile* file = 0;
  while ((file = static_cast<TSystemFile*>(next()))) {
    TString name(file->GetName());
    TString title(file->GetTitle());
    TString full(gSystem->ConcatFileName(file->GetTitle(), name.Data()));
    if (file->IsA() == TSystemDirectory::Class()) full = title;
    // Ignore special links 
    // Info("ScanDirectory", "name=%s title=%s full=%s", 
    //      name.Data(), title.Data(), full.Data());
    if (name == "." || name == "..") { 
      // Info("ScanDirectory", "Ignoring %s", name.Data());
      continue;
    }

    FileStat_t fs;
    if (gSystem->GetPathInfo(full.Data(), fs)) {
      Warning("ScanDirectory", "Cannot stat %s (%s)", full.Data(),
	      gSystem->WorkingDirectory());
      continue;
    }
    // Check if this is a directory 
    if (file->IsDirectory(full)) { 
      if (recursive) {
	TSystemDirectory* d = new TSystemDirectory(file->GetName(),
						   full.Data());
	if (ScanDirectory(d,chain,type,recursive,mc))
	  ret = true;
	delete d;
      }
      continue;
    }
    
    // If this is not a root file, ignore 
    if (!name.EndsWith(".root")) {
      // Info("ScanDirectory", "Ignoring non-ROOT file %s", name.Data());
      continue;
    }

    // If this file does not contain AliESDs, ignore 
    if (!name.Contains(fnPattern)) { 
      // Info("ScanDirectory", "%s does not match pattern %s", 
      //      name.Data(), fnPattern.Data());
      continue;
    }
    
    // Add 
    // Info("ScanDirectory", "Adding %s", full.Data());
    toAdd.Add(new TObjString(full));
  }

  TIter nextAdd(&toAdd);
  TObjString* s = 0;
  while ((s = static_cast<TObjString*>(nextAdd()))) {
    // Info("ScanDirectory", "Adding %s", s->GetString().Data());
    list->Add(s);
  }
  if (toAdd.GetEntries() > 0) ret = true;

  gSystem->ChangeDirectory(oldDir);
  return ret;
}
/** 
 * Get the list of trending files
 * 
 * @param input Start directory 
 * 
 * @return List of files 
 * 
 * @ingroup pwglf_forward_qa_scripts
 */
TList*
GetListOfFiles(const char* input=".")
{
  TList* ret = new TList;
  TString dir(input);
  // if (dir == ".") dir = "";

  TString savdir(gSystem->WorkingDirectory());
  TSystemDirectory d(gSystem->BaseName(dir.Data()), dir.Data());
  if (!ScanDirectory(&d, ret, false)) { 
    delete ret;
    ret = 0;
  }
  gSystem->ChangeDirectory(savdir);  
  if (ret) ret->Sort();
  return ret;
}

/** 
 * Run the QATrender and QAPlotter. 
 * 
 * The QATrender is run over the list of files (runs) to produce the
 * file <tt>forward_trending.root</tt> which contains a TTree of QA
 * information - one entry per run.  
 * 
 * The QATrender will also produce two files per run: 
 * 
 * - <tt>qa_<i>runNo</i>.root</tt> which contains TCanvas objects of
 *   the finished plots.
 *
 * - <tt>qa_<i>runNo</i>.pdf</tt> which is a PDF of the TCanvases
 *   mentioned above.
 *
 * The QAPlotter is then run over the  <tt>forward_trending.root</tt>
 * file and produces two files 
 * 
 * - <tt>qa_<i>first-run</i>-<i>last-run</i>.root</tt> which contains
 *   TCanvas objects of the finished plots.  It also contains the
 *   TMultiGraph objects painted in the canvases.
 *
 * - <tt>qa_<i>first-run</i>-<i>last-run</i>.pdf</tt> which is a PDF
 *   of the TCanvases mentioned above.
 * 
 * The QAPlotter will also produce PNGs of each canvas. 
 *
 * if @a runNo is larger than zero, given, then only the that run will
 * be processed and only by QATrender.  In addition, PNGs of each
 * canvas is produced. 
 * 
 * @param runNo (optional) Run number. If greater than 0, only this
 *              run will be processed 
 * @param input Input directory 
 * @param what  (expert) Flag of what to do 
 * @param keep  
 * 
 * @ingroup pwglf_forward_qa_scripts
 */
void
RunQAOld(const char* input=".", Bool_t keep=true, Int_t runNo=-1,
	 UShort_t what=0x3)
{
  gROOT->SetMacroPath(Form(".:$(ALICE_ROOT)/PWGLF/FORWARD/analysis2/qa:"
			   "$(ALICE_ROOT)/PWGLF/FORWARD/analysis2/corrs:%s",
			   gROOT->GetMacroPath()));
  gSystem->AddIncludePath("-I${ALICE_ROOT}/PWGLF/FORWARD/analysis2/qa");
  gSystem->Load("libGpad");
  gSystem->Load("libTree");

  if (runNo > 0) what = 0x1; // Only do first pass

  gROOT->LoadMacro("QATrender.C+g");
  gROOT->LoadMacro("QAPlotter.C+g");
  if (what & 0x1) {
    QATrender t(keep, runNo > 0);

    TList* l = 0;
    if (runNo < 0) l = GetListOfFiles(input);
    else {
      TObjString* s = new TObjString("");
      s->String() = gSystem->ConcatFileName(input, Form("trending_%09d.root", 
							runNo));
      l = new TList;
      l->Add(s);
    }
    if (!l) {
      Error("RunQA", "No trending files found");
      return;
    }
    // l->ls();
    TIter next(l);
    TObjString* s = 0;
    while ((s = static_cast<TObjString*>(next()))) {
      Info("Run", "Adding  file %s", s->GetName());
      t.AddFile(s->GetName());
    }
    t.Run();
  }
  if (what & 0x2) {
    QAPlotter p;
    p.Run();
  }
}
//
// EOF
//
 RunQAOld.C:1
 RunQAOld.C:2
 RunQAOld.C:3
 RunQAOld.C:4
 RunQAOld.C:5
 RunQAOld.C:6
 RunQAOld.C:7
 RunQAOld.C:8
 RunQAOld.C:9
 RunQAOld.C:10
 RunQAOld.C:11
 RunQAOld.C:12
 RunQAOld.C:13
 RunQAOld.C:14
 RunQAOld.C:15
 RunQAOld.C:16
 RunQAOld.C:17
 RunQAOld.C:18
 RunQAOld.C:19
 RunQAOld.C:20
 RunQAOld.C:21
 RunQAOld.C:22
 RunQAOld.C:23
 RunQAOld.C:24
 RunQAOld.C:25
 RunQAOld.C:26
 RunQAOld.C:27
 RunQAOld.C:28
 RunQAOld.C:29
 RunQAOld.C:30
 RunQAOld.C:31
 RunQAOld.C:32
 RunQAOld.C:33
 RunQAOld.C:34
 RunQAOld.C:35
 RunQAOld.C:36
 RunQAOld.C:37
 RunQAOld.C:38
 RunQAOld.C:39
 RunQAOld.C:40
 RunQAOld.C:41
 RunQAOld.C:42
 RunQAOld.C:43
 RunQAOld.C:44
 RunQAOld.C:45
 RunQAOld.C:46
 RunQAOld.C:47
 RunQAOld.C:48
 RunQAOld.C:49
 RunQAOld.C:50
 RunQAOld.C:51
 RunQAOld.C:52
 RunQAOld.C:53
 RunQAOld.C:54
 RunQAOld.C:55
 RunQAOld.C:56
 RunQAOld.C:57
 RunQAOld.C:58
 RunQAOld.C:59
 RunQAOld.C:60
 RunQAOld.C:61
 RunQAOld.C:62
 RunQAOld.C:63
 RunQAOld.C:64
 RunQAOld.C:65
 RunQAOld.C:66
 RunQAOld.C:67
 RunQAOld.C:68
 RunQAOld.C:69
 RunQAOld.C:70
 RunQAOld.C:71
 RunQAOld.C:72
 RunQAOld.C:73
 RunQAOld.C:74
 RunQAOld.C:75
 RunQAOld.C:76
 RunQAOld.C:77
 RunQAOld.C:78
 RunQAOld.C:79
 RunQAOld.C:80
 RunQAOld.C:81
 RunQAOld.C:82
 RunQAOld.C:83
 RunQAOld.C:84
 RunQAOld.C:85
 RunQAOld.C:86
 RunQAOld.C:87
 RunQAOld.C:88
 RunQAOld.C:89
 RunQAOld.C:90
 RunQAOld.C:91
 RunQAOld.C:92
 RunQAOld.C:93
 RunQAOld.C:94
 RunQAOld.C:95
 RunQAOld.C:96
 RunQAOld.C:97
 RunQAOld.C:98
 RunQAOld.C:99
 RunQAOld.C:100
 RunQAOld.C:101
 RunQAOld.C:102
 RunQAOld.C:103
 RunQAOld.C:104
 RunQAOld.C:105
 RunQAOld.C:106
 RunQAOld.C:107
 RunQAOld.C:108
 RunQAOld.C:109
 RunQAOld.C:110
 RunQAOld.C:111
 RunQAOld.C:112
 RunQAOld.C:113
 RunQAOld.C:114
 RunQAOld.C:115
 RunQAOld.C:116
 RunQAOld.C:117
 RunQAOld.C:118
 RunQAOld.C:119
 RunQAOld.C:120
 RunQAOld.C:121
 RunQAOld.C:122
 RunQAOld.C:123
 RunQAOld.C:124
 RunQAOld.C:125
 RunQAOld.C:126
 RunQAOld.C:127
 RunQAOld.C:128
 RunQAOld.C:129
 RunQAOld.C:130
 RunQAOld.C:131
 RunQAOld.C:132
 RunQAOld.C:133
 RunQAOld.C:134
 RunQAOld.C:135
 RunQAOld.C:136
 RunQAOld.C:137
 RunQAOld.C:138
 RunQAOld.C:139
 RunQAOld.C:140
 RunQAOld.C:141
 RunQAOld.C:142
 RunQAOld.C:143
 RunQAOld.C:144
 RunQAOld.C:145
 RunQAOld.C:146
 RunQAOld.C:147
 RunQAOld.C:148
 RunQAOld.C:149
 RunQAOld.C:150
 RunQAOld.C:151
 RunQAOld.C:152
 RunQAOld.C:153
 RunQAOld.C:154
 RunQAOld.C:155
 RunQAOld.C:156
 RunQAOld.C:157
 RunQAOld.C:158
 RunQAOld.C:159
 RunQAOld.C:160
 RunQAOld.C:161
 RunQAOld.C:162
 RunQAOld.C:163
 RunQAOld.C:164
 RunQAOld.C:165
 RunQAOld.C:166
 RunQAOld.C:167
 RunQAOld.C:168
 RunQAOld.C:169
 RunQAOld.C:170
 RunQAOld.C:171
 RunQAOld.C:172
 RunQAOld.C:173
 RunQAOld.C:174
 RunQAOld.C:175
 RunQAOld.C:176
 RunQAOld.C:177
 RunQAOld.C:178
 RunQAOld.C:179
 RunQAOld.C:180
 RunQAOld.C:181
 RunQAOld.C:182
 RunQAOld.C:183
 RunQAOld.C:184
 RunQAOld.C:185
 RunQAOld.C:186
 RunQAOld.C:187
 RunQAOld.C:188
 RunQAOld.C:189
 RunQAOld.C:190
 RunQAOld.C:191
 RunQAOld.C:192
 RunQAOld.C:193
 RunQAOld.C:194
 RunQAOld.C:195
 RunQAOld.C:196
 RunQAOld.C:197
 RunQAOld.C:198
 RunQAOld.C:199
 RunQAOld.C:200
 RunQAOld.C:201
 RunQAOld.C:202
 RunQAOld.C:203
 RunQAOld.C:204
 RunQAOld.C:205
 RunQAOld.C:206
 RunQAOld.C:207
 RunQAOld.C:208
 RunQAOld.C:209
 RunQAOld.C:210
 RunQAOld.C:211
 RunQAOld.C:212
 RunQAOld.C:213
 RunQAOld.C:214
 RunQAOld.C:215
 RunQAOld.C:216
 RunQAOld.C:217
 RunQAOld.C:218
 RunQAOld.C:219
 RunQAOld.C:220
 RunQAOld.C:221
 RunQAOld.C:222
 RunQAOld.C:223