// dialcom.C // David Adams // April 2005 // Root macros for DIAL 1.30. //********************************************************************** // Submit the job defined by papp, ptsk and pdst to // the scheduler msch. Job ID is put in jid. int submit() { // Invalidate the job ID. jid = JobId(); // Check the application. if ( papp == 0 ) { cout << "Application is not defined" << endl; return 1; } if ( ! papp->is_valid() ) { cout << "Application is invalid" << endl; return 2; } cout << "Application " << papp->id() << endl; // Check the task. if ( ptsk == 0 ) { cout << "Task is not defined" << endl; return 3; } if ( ! ptsk->is_valid() ) { cout << "Task is invalid" << endl; return 4; } cout << "Task " << ptsk->id() << endl; // Check the dataset. if ( pdst == 0 ) { cout << "Dataset is not defined" << endl; return 5; } if ( ! pdst->is_valid() ) { cout << "Dataset is invalid" << endl; return 6; } cout << "Dataset " << pdst->id() << endl; // Check the preferences. if ( pprf == 0 ) { cout << "Preferences are not defined" << endl; } else if ( ! pprf->is_locked() ) { cout << "Preferences are invalid or unlocked" << endl; return 8; } else { cout << "Preferences " << pprf->id() << endl; } // Check the scheduler. if ( ! msch.is_valid() ) { cout << "Scheduler is invalid" << endl; return 9; } // Submit job. cout << "*** Submitting job" << endl; if ( pprf == 0 ) { jid = msch.submit(*papp, *ptsk, *pdst); } else { jid = msch.submit(*papp, *ptsk, *pdst, *pprf); } // Check job status. cout << "*** Submitted job status:" << endl; if ( jid.is_valid() ) { cout << msch.job(jid) << endl; } else { //cout << "Job submission failed" << endl; //cout << msch.log() << endl; return 10; } // Record job ID. ssystem("rm -f jid"); Text tjid("jid"); tjid.append(jid.to_string()); tjid.write(); return 0; } //********************************************************************** // Give the user some pointers. void help() { system("cat welcome.txt"); } //********************************************************************** // Fetch results for job jid. // For now, assume results are in root files. void get_result() { open_root(); } //********************************************************************** // For backward compatibility. void get_results() { get_result(); } //********************************************************************** // Show the catalogs. void show_catalogs() { cout << endl; cout << " dr - " << dr << endl; cout << " dfc - " << dfc << endl; cout << " dsc - " << dsc << endl; //cout << " drc - " << drc << endl; cout << " ar - " << ar << endl; cout << " asc - " << asc << endl; cout << " tr - " << tr << endl; cout << " tsc - " << tsc << endl; cout << " jr - " << jr << endl; cout << " csc - " << csc << endl; cout << endl; } //********************************************************************** // Template to display an object. template void print(const T& obj) { std::cout << obj << std::endl; } //********************************************************************** // Template to display an object referenced by a pointer. template void pprint(const T* pobj) { if ( pobj == 0 ) { std::cout << "Null pointer" << std::endl; } else { std::cout << *pobj << endl; } } //********************************************************************** // Template to create an object from an XML file. // Won't work with datasets; use DatasetCreator. template int xmlread(string file, T*& pobj) { const XmlElement* px = parser.parse(file); if ( px == 0 ) return 1; pobj = new T(*px); return 0; } //********************************************************************** // Extract a text dataset from a job. // Useful after using the scriptrunner application. const dset::TextDataset* get_result_text(size_t isdst=0, size_t itxt =0, bool show =true) { const dial::Job& job = msch.job(jid); const dset::Dataset* pres = job.result(); if ( pres != 0 ) { if ( ! job.result_id().is_valid() ) { cout << "Unable to access result dataset with ID " << job.result_id() << endl; return 0; } } else { cout << "Job does not have a result" << endl; } const dset::DatasetList& sdsts = pres->constituents(); if ( isdst >= sdsts.size() ) { cout << "There are only " << sdsts.size() << " subdatasets" << endl; return 0; } const dset::Dataset* psdst = sdsts[isdst]; const dset::TextDataset* ptdst = dynamic_cast(psdst); if ( ptdst == 0 ) { cout << "Subdataset is not a text dataset" << endl; cout << " Type is " << psdst->fulltype() << endl; return 0; } if ( show ) { const dset::TextDataset::TextList texts = ptdst->texts(); if ( itxt >= texts.size() ) { cout << "Text dataset has " << texts.size() << " "; if ( texts.size() == 1 ) { cout << "entry" << endl; } else { cout << "entries" << endl; } cout << "." << endl; } else { cout << texts[itxt]; } } return ptdst; } //********************************************************************** // Fetch and open a job archive. int open_job_archive(dial::JobId jid) { if ( ! jid.is_valid() ) { cout << "Invalid job ID" << endl; return 1; } dset::Url lurl = msch.job(jid).archive(); dset::FileManagementSystem fms = dset::FileManagementSystem::default_instance(); dset::Url durl("file:" + getcwd()); dset::Url purl = fms.copy(lurl, durl); string fname = purl.fullpath(); if ( fname == "" ) { cout << "Unable fetch file" << endl; cout << " LURL: " << lurl << endl; cout << " PURL: " << purl << endl; return 3; } string dirname = "job-" + jid.to_string(); if ( ssystem("rm -rf " + dirname) ) { cout << "Error removing old directory" << endl; return 4; } if ( ssystem("mkdir " + dirname) ) { cout << "Unable to create directory" << endl; return 5; } if ( ssystem("cd " + dirname + "; tar -zxf " + fname) ) { cout << "Unable to open archive" << endl; return 6; } ssystem("rm " + fname); string fulldirname = FileName(dirname).fullpath_name(); cout << "Job archive is at " << endl; cout << " " << fulldirname << endl; return 0; } //********************************************************************** // Fetch a job with string job reference. int open_job_archive(string sjid) { dial::JobId jid(sjid); return open_job_archive(jid); } //********************************************************************** // Create a global job definition. // // Usage is described by define_job_help(). int define_job(string aname, string tname, string dname) { int rstat = 0; // Find application. if ( FileStatus(aname).is_directory() ) { cout << "Creating new application from " << aname << endl; papp = new dial::Application("*", aname); aid = papp->id(); } else { cout << "Extracting application " << aname << endl; aid = asc.id(aname); if ( ! aid.is_valid() ) { aid = ApplicationId(aname); } papp = ar.extract(aid); } // Find task. if ( FileStatus(tname).is_directory() ) { cout << "Creating new task from directory " << tname << endl; ptsk = new dial::Task("*", tname); tid = ptsk->id(); } else if ( FileStatus(tname).is_readable() ) { cout << "Creating task from XML file " << tname << endl; const XmlElement* pxtsk = parser.parse(tname); if ( pxtsk != 0 ) { ptsk = new dial::Task(*pxtsk); tid = ptsk->id(); } } else { cout << "Extracting task " << tname << endl; tid = tsc.id(tname); if ( ! tid.is_valid() ) { tid = TaskId(tname); } ptsk = tr.extract(tid); } // Find dataset. cout << "Extracting dataset " << dname << endl; if ( FileStatus(dname).is_readable() ) { cout << "Creating dataset from XML file " << dname << endl; const XmlElement* pxdst = parser.parse(dname); if ( pxdst != 0 ) { pdst = dset::DatasetCreator::create(*pxdst, &dr); } } else { did = dsc.id(dname); if ( ! did.is_valid() ) { did = DatasetId(dname); } pdst = dr.extract(did); } // Check definitions. if ( papp==0 || !papp->is_valid() ) { cout << "Application is invalid!!!" << endl; rstat = 1; } if ( ptsk==0 || !ptsk->is_valid() ) { cout << "Task is invalid!!!" << endl; rstat = 2; } if ( pdst==0 || !pdst->is_valid() ) { cout << "Dataset is invalid!!!" << endl; rstat = 3; } return rstat; } // Show help for the above. void define_job_help() { cout << "\nUsage: define_job(aname, tname, dname)\n"; cout << "\nArguments are strings specifying the application, task and dataset.\n"; cout << "\nThese strings are interpreted as one of the following:\n"; cout << " id: Object identifier.\n"; cout << " name: Object name in the default selection catalog.\n"; cout << " dir: Name of a directory holding files to be used to\n"; cout << " construct the object.\n"; cout << " xml: Name of file holding XML description of the object.\n"; cout << "\nAvailable options (in order) for each type:\n"; cout << " application: dir, name, id\n"; cout << " task: dir, xml, name, id\n"; cout << " dataset: xml, name, id\n"; cout << "\nThe function returns 0 for success.\n"; } //**********************************************************************