#ifndef ATLAS_AtlTask
#include <AtlTask.h>
#endif
#include <TObjString.h>
#include <TString.h>
#include <TList.h>
#include <TSystem.h>
#include <iostream>
#include <TDirectory.h>
#include <TDirectoryFile.h>
#include <TKey.h>
#include <TH1F.h>
#include <TH1D.h>
#include <TH2F.h>
#include <TH2D.h>
using namespace std;
#ifndef __CINT__
ClassImp(AtlTask);
#endif
AtlTask::AtlTask(const char* name, const char* title) :
TTask(name, title) {
fInputFiles = new TList;
fInputEntryLists = new TList;
SetBatchJob(kTRUE);
fOutputFileName = 0;
fOutputTreeName = 0;
fNEvents = 0;
fFirstEntry = 0;
fIsTest = kFALSE;
fJobHome = 0;
fLogFilePath = 0;
fRunScript = 0;
fTempLogFilePath = 0;
fTempOutputPath = 0;
fTempOutputFileName = 0;
fBatchNodeAll = kFALSE;
fBatchNodeAutomatic = kTRUE;
fBatchNode = "ms01b";
fGridRootVersion = 0;
fGridCmtVersion = 0;
fGridUserName = 0;
fGridSuffix = 0;
fDebug = 0;
fDebugBuild = 0;
}
AtlTask::~AtlTask() {
fInputFiles->Delete(); delete fInputFiles;
if ( fOutputFileName != 0 ) delete fOutputFileName;
if ( fOutputTreeName != 0 ) delete fOutputTreeName;
if ( fJobHome != 0 ) delete fJobHome;
if ( fLogFilePath != 0 ) delete fLogFilePath;
if ( fRunScript != 0 ) delete fRunScript;
if ( fTempLogFilePath != 0 ) delete fTempLogFilePath;
if ( fTempOutputPath != 0 ) delete fTempOutputPath;
if ( fTempOutputFileName != 0 ) delete fTempOutputFileName;
if ( fGridRootVersion != 0 ) delete fGridRootVersion;
if ( fGridCmtVersion != 0 ) delete fGridCmtVersion;
if ( fGridUserName != 0 ) delete fGridUserName;
if ( fGridSuffix != 0 ) delete fGridSuffix;
}
void AtlTask::AddInputFiles(const char* InputFiles) {
TObjString *item = new TObjString(InputFiles);
fInputFiles->Add(item);
}
void AtlTask::AddInputEntryLists(const char* InputFile) {
TObjString *item = new TObjString(InputFile);
fInputEntryLists->Add(item);
}
void AtlTask::SetOutputFile(const char* OutputFile) {
fOutputFileName = new TString(OutputFile);
}
void AtlTask::SetTempOutputFile(const char* OutputFile) {
fTempOutputFileName = new TString(OutputFile);
}
void AtlTask::SetInteractiveJob(Bool_t InteractiveJob) {
fInteractiveJob = InteractiveJob;
fBatchJob = !InteractiveJob;
fNAFBatchJob = !InteractiveJob;
fGridJob = !InteractiveJob;
SetLogFile(kFALSE);
}
void AtlTask::SetBatchJob(Bool_t BatchJob) {
fBatchJob = BatchJob;
fNAFBatchJob = !BatchJob;
fInteractiveJob = !BatchJob;
fGridJob = !BatchJob;
SetLogFile(kTRUE);
}
void AtlTask::SetNAFBatchJob(Bool_t NAFBatchJob) {
fNAFBatchJob = NAFBatchJob;
fBatchJob = !NAFBatchJob;
fInteractiveJob = !NAFBatchJob;
fGridJob = !NAFBatchJob;
SetLogFile(kTRUE);
}
void AtlTask::SetGridJob(Bool_t GridJob) {
fGridJob = GridJob;
fInteractiveJob = !GridJob;
fBatchJob = !GridJob;
fNAFBatchJob = !GridJob;
SetLogFile(kTRUE);
}
void AtlTask::SetOutputTree(const char* name, const char* title) {
if ( fOutputTreeName == 0 ) {
fOutputTreeName = new TNamed(name, title);
} else{
fOutputTreeName->SetNameTitle(name, title);
}
}
void AtlTask::SetBatchNodeAutomatic(Bool_t BatchNodeAutomatic) {
fBatchNodeAutomatic = BatchNodeAutomatic;
fBatchNodeAll = !BatchNodeAutomatic;
}
void AtlTask::SetBatchNodeAll(Bool_t BatchNodeAll) {
fBatchNodeAll = BatchNodeAll;
fBatchNodeAutomatic = !BatchNodeAll;
}
void AtlTask::SetBatchNode(const char* BatchNode) {
if( strcmp(BatchNode,"ms01b") != 0 &&
strcmp(BatchNode,"ms02b") != 0 &&
strcmp(BatchNode,"ms03b") != 0 &&
strcmp(BatchNode,"ms04b") != 0 &&
strcmp(BatchNode,"ms05b") != 0 &&
strcmp(BatchNode,"ms06b") != 0 &&
strcmp(BatchNode,"ms07b") != 0 &&
strcmp(BatchNode,"ms08b") != 0 &&
strcmp(BatchNode,"as01b") != 0 &&
strcmp(BatchNode,"as02b") != 0 &&
strcmp(BatchNode,"as03b") != 0 &&
strcmp(BatchNode,"as04b") != 0 &&
strcmp(BatchNode,"as05b") != 0 &&
strcmp(BatchNode,"as06b") != 0 &&
strcmp(BatchNode,"as07b") != 0 &&
strcmp(BatchNode,"as08b") != 0 &&
strcmp(BatchNode,"as21b") != 0 &&
strcmp(BatchNode,"as22b") != 0 &&
strcmp(BatchNode,"as23b") != 0 &&
strcmp(BatchNode,"as24b") != 0 &&
strcmp(BatchNode,"as25b") != 0 &&
strcmp(BatchNode,"as26b") != 0 &&
strcmp(BatchNode,"as27b") != 0 &&
strcmp(BatchNode,"as28b") != 0 ) {
Error("SetBatchNode", "Invalid node given. Node not set.");
}
fBatchNodeAll = kFALSE;
fBatchNodeAutomatic = kFALSE;
fBatchNode = BatchNode;
}
Char_t AtlTask::GetBatchNodeFromPath(const char* path) {
TString path_str(path);
if ( (path_str.Index("/datm") == 0) ||
(path_str.Index("/dats") == 0) ) return path_str(5);
return '1';
}
void AtlTask::Exec(Option_t *option) {
cout << endl;
Info("Exec", "Task \"%s\" started", GetName());
Bool_t success = kTRUE;
TString opt = option;
opt.ToLower();
if ( opt.Contains("test") ) {
fIsTest = kTRUE;
fNEvents = 1000;
}
CreateJobHome();
if ( !fGridJob ) CreateLogFilePath();
if ( !fGridJob ) CreateOutFilePath();
if ( fInteractiveJob ) {
ExecInteractiveJob(option);
} else if ( fBatchJob ) {
CreateRunScriptPath();
CreateRunScript();
success = ExecBatchJob(option);
} else if ( fGridJob ) {
CreateRunScriptPath();
ExecGridJob(option);
} else if ( fNAFBatchJob ) {
CreateRunScriptPath();
CreateNAFBatchRunScript();
ExecNAFBatchJob(option);
}
if (success) Info("Exec", "Task \"%s\" finished", GetName());
else Error("Exec", "Task \"%s\" execution failed.", GetName());
cout << endl;
}
void AtlTask::SetJobHome(const char* JobHome) {
if ( fJobHome != 0 ) delete fJobHome;
fJobHome = new TString(JobHome);
}
void AtlTask::CreateJobHome() {
if ( fJobHome == 0 ) {
if ( fGridJob ) {
fJobHome = new TString(gSystem->ExpandPathName("$GRID_HOME/submitscripts"));
} else {
fJobHome = new TString(gSystem->pwd());
}
fJobHome->Append("/");
fJobHome->Append(GetName());
}
Info("CreateJobHome", "Create job working directory\n%s",
fJobHome->Data());
gSystem->Exec(Form("rm -fr %s", fJobHome->Data()));
gSystem->Exec(Form("mkdir -p %s", fJobHome->Data()));
}
void AtlTask::CreateRunScriptPath() {
if ( fRunScript != 0 ) {
delete fRunScript; fRunScript = 0;
}
fRunScript = new TString(fJobHome->Data());
fRunScript->Append("/");
fRunScript->Append(GetName());
fRunScript->Append(".run");
fRunScript->ReplaceAll("//", 2, "/", 1);
}
void AtlTask::CreateLogFilePath() {
if ( fOutputFileName == 0 ) {
Error("CreateLogFilePath", "No output filename given for this task \"%s\". Abort!",
GetName());
gSystem->Abort(0);
}
if ( fLogFilePath != 0 ) { delete fLogFilePath; fLogFilePath = 0; }
fLogFilePath = new TString(fJobHome->Data());
fLogFilePath->Append("/");
fLogFilePath->Append(gSystem->BaseName(fOutputFileName->Data()));
fLogFilePath->ReplaceAll(".root", 5, ".log", 4);
fLogFilePath->ReplaceAll("//", 2, "/", 1);
if ( fTempOutputPath != 0 && fTempOutputPath->BeginsWith("/") ) {
fTempLogFilePath = new TString(fTempOutputPath->Data());
fTempLogFilePath->Append("/");
fTempLogFilePath->Append(gSystem->BaseName(fOutputFileName->Data()));
fTempLogFilePath->ReplaceAll(".root", 5, ".log", 4);
fTempLogFilePath->ReplaceAll("//", 2, "/", 1);
}
}
void AtlTask::CreateOutFilePath() {
TString expd_name(gSystem->ExpandPathName(fOutputFileName->Data()));
if ( !expd_name.BeginsWith("/") ) {
fOutputFileName->Prepend("/");
fOutputFileName->Prepend(fJobHome->Data());
}
if ( fIsTest ) {
fOutputFileName->Append("_test");
}
Info("CreateOutFilePath", "Create outfile path:\n%s",
gSystem->DirName(fOutputFileName->Data()));
gSystem->Exec(Form("mkdir -p %s", gSystem->DirName(fOutputFileName->Data())));
}
Bool_t AtlTask::SubmitBatchJob() {
TString jobsub_cmd("qsub ");
TString expd_outfile(gSystem->ExpandPathName(fOutputFileName->Data()));
TString expd_infile;
if ( fInputFiles->GetEntries() > 0 )
expd_infile = TString(gSystem->ExpandPathName(((TObjString*)fInputFiles
->At(0))->GetString()));
TString host2(gSystem->HostName());
if ( host2.Contains("lx") ) {
jobsub_cmd.Append(" -o /dev/null -j y ");
jobsub_cmd.Append(fRunScript->Data());
} else {
if ( fBatchNodeAll ) {
jobsub_cmd.Append(" ");
} else if ( fBatchNodeAutomatic ) {
if ( expd_infile.Index("/rdsk/datm") == 0 ) {
jobsub_cmd.Append("-l nodes=ms0");
jobsub_cmd.Append(expd_infile(10));
jobsub_cmd.Append("b ");
}
if ( expd_infile.Index("/rdsk/dats") == 0 ) {
jobsub_cmd.Append("-l nodes=as0");
jobsub_cmd.Append(expd_infile(10));
jobsub_cmd.Append("b ");
}
} else {
jobsub_cmd.Append("-l nodes=");
jobsub_cmd.Append(fBatchNode);
jobsub_cmd.Append(" ");
}
jobsub_cmd.Append(" -o /dev/null -j oe ");
jobsub_cmd.Append(fRunScript->Data());
TString host(gSystem->HostName());
host.Remove(3);
host.Prepend("nodes=");
if ( !jobsub_cmd.Contains(host) && !fBatchNodeAll) {
Error("SubmitBatchJob", "Submission from this machine to desired queue impossible.\nNote that the batch systems on the ms0Xb and as0Xb machines are separated and you cannot submit jobs from one system to the other or vice versa. Either switch to the other system for submission (make sure everything is compiled and linked on that platform), or set a node of the system you are working on explicitly.");
return kFALSE;
}
}
Info("SubmitBatchJob", "Execute %s", jobsub_cmd.Data());
gSystem->Exec(jobsub_cmd.Data());
if ( ! host2.Contains("lx") ) {
gSystem->Sleep(2000);
}
return kTRUE;
}
void AtlTask::SubmitNAFBatchJob() {
TString jobsub_cmd("qsub ");
TString expd_outfile(gSystem->ExpandPathName(fOutputFileName->Data()));
TString expd_infile(gSystem->ExpandPathName(((TObjString*)fInputFiles
->At(0))->GetString()));
jobsub_cmd.Append(fRunScript->Data());
Info("SubmitBatchJob", "Execute %s", jobsub_cmd.Data());
gSystem->Exec(jobsub_cmd.Data());
}
void AtlTask::SubmitGridJob() {
TString jobsub_cmd("source ");
jobsub_cmd.Append(GetName());
jobsub_cmd.Append(".run");
Info("SubmitGridJob", "Execute %s", jobsub_cmd.Data());
gSystem->ChangeDirectory(gSystem->ExpandPathName("$GRID_HOME"));
gSystem->Exec(jobsub_cmd.Data());
}
void AtlTask::SetTempOutputPath(const char* OutputPath) {
fTempOutputPath = new TString(OutputPath);
}
void AtlTask::CopyFolder(TDirectory *SrcDir, TDirectory *DestDir,
Float_t scale) {
SrcDir->cd();
TIter next_key(SrcDir->GetListOfKeys());
TKey *key = 0;
while ( (key = (TKey*)next_key()) ) {
if ( strcmp(key->GetClassName(), "TDirectoryFile") == 0 ) {
TDirectory *NewSrcDir = (TDirectoryFile*)key->ReadObj();
DestDir->cd();
TDirectory *NewDestDir = DestDir->mkdir(key->GetName());
CopyFolder(NewSrcDir, NewDestDir, scale);
} else if ( (strcmp(key->GetClassName(), "TH1F") == 0) ) {
DestDir->cd();
TH1F *h1f = (TH1F*)key->ReadObj()->Clone();
h1f->SetDirectory(DestDir);
h1f->Scale(scale);
} else if ( (strcmp(key->GetClassName(), "TH1D") == 0) ) {
DestDir->cd();
TH1D *h1d = (TH1D*)key->ReadObj()->Clone();
h1d->SetDirectory(DestDir);
h1d->Scale(scale);
} else if ( (strcmp(key->GetClassName(), "TH2F") == 0) ) {
DestDir->cd();
TH2F *h2f = (TH2F*)key->ReadObj()->Clone();
h2f->SetDirectory(DestDir);
h2f->Scale(scale);
} else if ( (strcmp(key->GetClassName(), "TH2D") == 0) ) {
DestDir->cd();
TH2D *h2d = (TH2D*)key->ReadObj()->Clone();
h2d->SetDirectory(DestDir);
h2d->Scale(scale);
}
}
}
void AtlTask::SetGridRootVersion(const char* RootVersion) {
if ( fGridRootVersion != 0 ) delete fGridRootVersion;
fGridRootVersion = new TString(RootVersion);
}
void AtlTask::SetGridCmtVersion(const char* CmtVersion) {
if ( fGridCmtVersion != 0 ) delete fGridCmtVersion;
fGridCmtVersion = new TString(CmtVersion);
}
void AtlTask::SetGridUserName(const char* UserName) {
if ( fGridUserName != 0 ) delete fGridUserName;
fGridUserName = new TString(UserName);
}
void AtlTask::SetGridSuffix(const char* Suffix) {
if ( fGridSuffix != 0 ) delete fGridSuffix;
fGridSuffix = new TString(Suffix);
}
Bool_t AtlTask::WriteEnvSetup(std::ofstream & out) const {
char const * appSetupScriptPath = gSystem->Getenv("APLUSPLUS_SETUP");
if ( !appSetupScriptPath )
return kFALSE;
TString appSetupScriptPathEscaped(appSetupScriptPath);
appSetupScriptPathEscaped.ReplaceAll("'", "\'");
appSetupScriptPathEscaped.Insert(0, "'");
appSetupScriptPathEscaped.Append("'");
out << "source " << appSetupScriptPathEscaped << "\n";
return kTRUE;
}