ROOT logo
#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TSystem.h"
#include "Riostream.h"
#include "TEnv.h"
#include "TROOT.h"
#include "TGrid.h"
#include "TSystem.h"
#include "TFile.h"
#include "TError.h"
#endif

// copy a QAresult.root file from alien to a local directory and check it. 

//_____________________________________________________________________________
Int_t GetRunNumber(TString filePath)
{
  TObjArray* array = filePath.Tokenize("/");
  array->SetOwner();
  TString auxString = "";
  Int_t runNum = -1;
  for ( Int_t ientry=0; ientry<array->GetEntries(); ientry++ ) {
    auxString = array->At(ientry)->GetName();
    if ( auxString.Length() == 9 && auxString.IsDigit() ) {
      runNum = auxString.Atoi();
      break;
    }
  }
  delete array;

  if ( runNum < 0 ) {
    array = auxString.Tokenize("_");
    array->SetOwner();
    auxString = array->Last()->GetName();
    auxString.ReplaceAll(".root","");
    if ( auxString.IsDigit() )
      runNum = auxString.Atoi();
    delete array;
  }

  return runNum;
}

//_____________________________________________________________________________

Int_t CopyQAFile(TString inFilename, TString baseOutDir=".", Bool_t makeRunDir=kTRUE, TString changeFilename="", Int_t timeOut = 10)
{

  gSystem->Setenv("XRDCLIENTMAXWAIT",Form("%d",timeOut));
  gEnv->SetValue("XNet.RequestTimeout", timeOut);
  gEnv->SetValue("XNet.ConnectTimeout", timeOut);
  gEnv->SetValue("XNet.TransactionTimeout", timeOut);
  TFile::SetOpenTimeout(timeOut);


  if ( inFilename.Contains("alien://") && ! gGrid )

    if (! TGrid::Connect("alien://")) {
      Error(__FUNCTION__,"Error connecting to alien");
      return -1;
    }

  TObjArray* array = inFilename.Tokenize("/");
  array->SetOwner();
  TString outFilename = changeFilename.IsNull() ? array->Last()->GetName() : changeFilename.Data();
  delete array;

  if ( makeRunDir ) {
    Int_t runNumber = GetRunNumber(inFilename);
    if ( runNumber >= 0 ) {
      baseOutDir = Form("%s/%i", baseOutDir.Data(), runNumber);
      if ( gSystem->AccessPathName(baseOutDir.Data()) )
        gSystem->mkdir(baseOutDir.Data());
    }
    else Warning(__FUNCTION__,"run number not found!");
  }
  outFilename.Prepend(Form("%s/", baseOutDir.Data()));
  Bool_t showProgressBar = ! gROOT->IsBatch();
 
  if ( gSystem->AccessPathName(outFilename.Data())) {
    if (! TFile::Cp(inFilename.Data(), outFilename.Data(), showProgressBar)) {
      Error(__FUNCTION__,"Error copying the file from alien");
      return -2;
    }
  }

  printf("file: %s\n", inFilename.Data());
  printf("outDir: %s\n", baseOutDir.Data());
  printf("outFile: %s\n", outFilename.Data());

  gErrorIgnoreLevel = kWarning +1; 
  TFile f(outFilename.Data());
  gErrorIgnoreLevel = -1;
   
  if (f.IsZombie()) {
    Error(__FUNCTION__,"Error opening outFile");
    return -3;
  }

  if (f.TestBit(TFile::kRecovered)) {
    Info(__FUNCTION__,"The file is likely to be corrupted");
    return -4;
  }

  return 0; 
}

 CopyQAFile.C:1
 CopyQAFile.C:2
 CopyQAFile.C:3
 CopyQAFile.C:4
 CopyQAFile.C:5
 CopyQAFile.C:6
 CopyQAFile.C:7
 CopyQAFile.C:8
 CopyQAFile.C:9
 CopyQAFile.C:10
 CopyQAFile.C:11
 CopyQAFile.C:12
 CopyQAFile.C:13
 CopyQAFile.C:14
 CopyQAFile.C:15
 CopyQAFile.C:16
 CopyQAFile.C:17
 CopyQAFile.C:18
 CopyQAFile.C:19
 CopyQAFile.C:20
 CopyQAFile.C:21
 CopyQAFile.C:22
 CopyQAFile.C:23
 CopyQAFile.C:24
 CopyQAFile.C:25
 CopyQAFile.C:26
 CopyQAFile.C:27
 CopyQAFile.C:28
 CopyQAFile.C:29
 CopyQAFile.C:30
 CopyQAFile.C:31
 CopyQAFile.C:32
 CopyQAFile.C:33
 CopyQAFile.C:34
 CopyQAFile.C:35
 CopyQAFile.C:36
 CopyQAFile.C:37
 CopyQAFile.C:38
 CopyQAFile.C:39
 CopyQAFile.C:40
 CopyQAFile.C:41
 CopyQAFile.C:42
 CopyQAFile.C:43
 CopyQAFile.C:44
 CopyQAFile.C:45
 CopyQAFile.C:46
 CopyQAFile.C:47
 CopyQAFile.C:48
 CopyQAFile.C:49
 CopyQAFile.C:50
 CopyQAFile.C:51
 CopyQAFile.C:52
 CopyQAFile.C:53
 CopyQAFile.C:54
 CopyQAFile.C:55
 CopyQAFile.C:56
 CopyQAFile.C:57
 CopyQAFile.C:58
 CopyQAFile.C:59
 CopyQAFile.C:60
 CopyQAFile.C:61
 CopyQAFile.C:62
 CopyQAFile.C:63
 CopyQAFile.C:64
 CopyQAFile.C:65
 CopyQAFile.C:66
 CopyQAFile.C:67
 CopyQAFile.C:68
 CopyQAFile.C:69
 CopyQAFile.C:70
 CopyQAFile.C:71
 CopyQAFile.C:72
 CopyQAFile.C:73
 CopyQAFile.C:74
 CopyQAFile.C:75
 CopyQAFile.C:76
 CopyQAFile.C:77
 CopyQAFile.C:78
 CopyQAFile.C:79
 CopyQAFile.C:80
 CopyQAFile.C:81
 CopyQAFile.C:82
 CopyQAFile.C:83
 CopyQAFile.C:84
 CopyQAFile.C:85
 CopyQAFile.C:86
 CopyQAFile.C:87
 CopyQAFile.C:88
 CopyQAFile.C:89
 CopyQAFile.C:90
 CopyQAFile.C:91
 CopyQAFile.C:92
 CopyQAFile.C:93
 CopyQAFile.C:94
 CopyQAFile.C:95
 CopyQAFile.C:96
 CopyQAFile.C:97
 CopyQAFile.C:98
 CopyQAFile.C:99
 CopyQAFile.C:100
 CopyQAFile.C:101
 CopyQAFile.C:102
 CopyQAFile.C:103
 CopyQAFile.C:104
 CopyQAFile.C:105
 CopyQAFile.C:106
 CopyQAFile.C:107