19 Jobs::Jobs(TString name) : fArray(kFALSE), fDebugCommands(kFALSE), fDir(
"jobs"), fFile(name), fStartJob(1), fEndJob(1) {
21 XMLNode* root = file.GetRootNode();
23 XMLNode* settings = root->GetChild(
"settings");
24 fSubmitCommand = settings->GetAttrib(
"submit")->GetValue();
26 TString Shell = settings->GetAttrib(
"shell")->GetValue();
27 TString array =
"yes";
28 if (settings->GetAttrib(
"array")) array = settings->GetAttrib(
"array")->GetValue();
29 if (settings->GetAttrib(
"debug")) {
30 if (settings->GetAttrib(
"debug")->GetValue().EqualTo(
"yes")) fDebugCommands = kTRUE;
33 if (array.EqualTo(
"yes")) fArray = kTRUE;
34 if (settings->GetAttrib(
"start")) fStartJob = settings->GetAttrib(
"start")->GetValue().Atoi();
35 if (settings->GetAttrib(
"end")->GetValue().Atoi()) fEndJob = settings->GetAttrib(
"end")->GetValue().Atoi();
36 if (settings->GetAttrib(
"dir")->GetValue()) fDir = settings->GetAttrib(
"dir")->GetValue();
39 XMLNode* parameters = root->GetChild(
"parameters");
41 const Int_t nPar = parameters->GetNChildren(
"parameter");
42 for (
int i = 0; i < nPar; i++) {
43 XMLNode* param = parameters->GetChild(
"parameter", i);
44 TString parname = param->GetAttrib(
"name")->GetValue();
45 TString parvalue = param->GetAttrib(
"value")->GetValue();
46 fParameters.push_back(std::pair<TString, TString>(parname, parvalue));
49 fParameters.push_back(std::pair<TString, TString>(
"HAL::CONST::PWD", gSystem->pwd()));
50 fParameters.push_back(std::pair<TString, TString>(
"HAL::CONST::START", Form(
"%i", fStartJob)));
51 fParameters.push_back(std::pair<TString, TString>(
"HAL::CONST::END", Form(
"%i", fEndJob)));
53 fCommands.push_back(Shell);
54 XMLNode* commands = root->GetChild(
"commands");
55 const Int_t nCommands = commands->GetNChildren(
"command");
56 for (
int i = 0; i < nCommands; i++) {
57 XMLNode* command = commands->GetChild(
"command", i);
58 fCommands.push_back(command->GetValue());
62 for (
auto parameter : fParameters) {
63 TString patternToReplace = Form(
"${%s}", parameter.first.Data());
65 for (
auto& command : fCommands) {
66 command.ReplaceAll(patternToReplace, parameter.second);
68 fSubmitCommand.ReplaceAll(patternToReplace, parameter.second);
72 void Jobs::SubmitJobs() {
73 if (fSubmitCommand.Contains(
"${HAL::CONST::JOB_FILE}")) {
75 TString submit = fSubmitCommand;
76 submit.ReplaceAll(
"${HAL::CONST::JOB_FILE}", Form(
"%s/job_array", fDir.Data()));
78 Cout::Text(
"Sending array",
"M", kWhite);
81 gSystem->Exec(submit);
83 if (fDebugCommands) { Cout::Text(Form(
"Sending files %i - %i", fStartJob, fEndJob),
"M", kWhite); }
84 for (
int i = fStartJob; i <= fEndJob; i++) {
85 TString submit = fSubmitCommand;
86 submit.ReplaceAll(
"${HAL::CONST::JOB_FILE}", Form(
"%s/job_%i", fDir.Data(), i));
87 if (fDebugCommands) { Cout::Text(submit); }
88 gSystem->Exec(submit);
92 Cout::Text(
"NO ${HAL::CONST::JOB_FILE} trying to guess command pattern",
"L", kYellow);
94 gSystem->Exec(Form(
"%s %s/job_array", fDir.Data(), fSubmitCommand.Data()));
96 for (
int i = fStartJob; i <= fEndJob; i++) {
97 gSystem->Exec(Form(
"%s %s/job_%i", fDir.Data(), fSubmitCommand.Data(), i));
103 void Jobs::CreateJobs() {
105 std::ofstream job_array;
106 job_array.open(Form(
"%s/job_array", fDir.Data()));
107 for (
unsigned int i = 0; i < fCommands.size(); i++)
108 job_array << fCommands[i] << std::endl;
110 gSystem->Exec(Form(
"chmod a+x %s/job_array", fDir.Data()));
112 for (
int iJob = fStartJob; iJob <= fEndJob; iJob++) {
113 std::ofstream job_file;
114 job_file.open(Form(
"%s/job_%i", fDir.Data(), iJob));
115 for (
unsigned int i = 0; i < fCommands.size(); i++) {
116 TString commands = fCommands[i];
117 commands.ReplaceAll(
"${HAL::CONST::JOB_ID}", Form(
"%i", iJob));
118 job_file << commands << std::endl;
121 gSystem->Exec(Form(
"chmod a+x %s/job_%i", fDir.Data(), iJob));
128 Int_t Jobs::GetNVariables(TString textfile) {
129 if (textfile.EndsWith(
".xml")) {
return GetNVariablesXML(textfile); }
130 return GetNVariablesTxt(textfile);
133 TString Jobs::GetParameter(TString textfile, Int_t job_no, Int_t var_no) {
134 if (textfile.EndsWith(
".xml"))
return GetParameterXml(textfile, job_no, var_no);
135 return GetParameterTxt(textfile, job_no, var_no);
138 void Jobs::CreateDummyTxtFile(TString textfile, Int_t jobs, Int_t vars) {
141 file <<
"=======_HAL_JOBS_INPUT_FILE_=======" << std::endl;
142 file <<
"NVAR " << vars << std::endl;
143 file <<
"NJOBS " << jobs << std::endl;
144 for (
int i = 0; i < jobs; i++) {
145 for (
int j = 0; j < vars; j++) {
146 file << Form(
"job_%i_var_%i", i, j) <<
" ";
153 Int_t Jobs::GetNVariablesTxt(TString textfile) {
160 file >> dummy >> pars;
161 if (dummy !=
"NVAR") {
162 std::cout <<
"HalJobs::CreateJobsTxt failed to find Nvar: that specify "
163 "number of variables"
166 file >> dummy >> jobs;
167 if (dummy !=
"NJOBS") {
168 std::cout <<
"HalJobs::CreateJobsTxt failed to find Njobs that specify "
169 "number of variables"
175 std::cout <<
"HalJobs::GetNVariable failed to find txt file" << std::endl;
181 Int_t Jobs::GetNVariablesXML(TString xmlfile) {
182 XMLFile file(xmlfile);
183 XMLNode* root = file.GetRootNode();
184 return root->GetChild(0)->GetNAttributes();
187 TString Jobs::GetParameterTxt(TString textfile, Int_t job, Int_t var) {
188 Int_t vals = GetNVariables(textfile);
189 if (vals == 0)
return "";
195 file >> dummy >> pars;
196 file >> dummy >> jobs;
197 if (job > jobs)
return "";
198 if (var > pars)
return "";
199 for (
int i = 0; i < job; i++) {
200 for (
int j = 0; j < pars; j++) {
204 for (
int j = 0; j <= var; j++)
210 TString Jobs::GetParameterXml(TString xmlfile, Int_t job, Int_t var) {
212 XMLNode* root = p.GetRootNode();
213 XMLNode* nod = root->GetChild(job);
214 TString val = nod->GetAttrib(var)->GetValue();
218 void Jobs::CreateDummyXMLFile(TString xmlfile, Int_t jobs, Int_t vars) {
219 XMLFile file(xmlfile,
"write");
222 for (
int iJobs = 0; iJobs < jobs; iJobs++) {
224 for (
int iAttrib = 0; iAttrib < vars; iAttrib++) {
225 job->
AddAttrib(
new XMLAttrib(Form(
"par_%i", iAttrib), Form(
"job_%i_var_%i", iJobs, iAttrib)));
void CreateRootNode(TString name)
XMLNode * GetRootNode() const
void AddChild(XMLNode *node)
void AddAttrib(XMLAttrib *attrib)