Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Jobs.cxx
1/*
2 * HalJobs.cxx
3 *
4 * Created on: 6 maj 2020
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9
10#include "Jobs.h"
11
12#include "Cout.h"
13#include "XMLNode.h"
14#include <TSystem.h>
15#include <fstream>
16#include <iostream>
17
18namespace Hal {
19 Jobs::Jobs(TString name) : fArray(kFALSE), fDebugCommands(kFALSE), fDir("jobs"), fFile(name), fStartJob(1), fEndJob(1) {
20 XMLFile file(fFile);
21 XMLNode* root = file.GetRootNode();
22
23 XMLNode* settings = root->GetChild("settings");
24 fSubmitCommand = settings->GetAttrib("submit")->GetValue();
25
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;
31 }
32 array.ToLower();
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();
37 gSystem->mkdir(fDir);
38
39 XMLNode* parameters = root->GetChild("parameters");
40 if (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));
47 }
48 }
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)));
52
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());
59 }
60
61
62 for (auto parameter : fParameters) {
63 TString patternToReplace = Form("${%s}", parameter.first.Data());
64
65 for (auto& command : fCommands) {
66 command.ReplaceAll(patternToReplace, parameter.second);
67 }
68 fSubmitCommand.ReplaceAll(patternToReplace, parameter.second);
69 }
70 }
71
72 void Jobs::SubmitJobs() {
73 if (fSubmitCommand.Contains("${HAL::CONST::JOB_FILE}")) {
74 if (fArray) {
75 TString submit = fSubmitCommand;
76 submit.ReplaceAll("${HAL::CONST::JOB_FILE}", Form("%s/job_array", fDir.Data()));
77 if (fDebugCommands) {
78 Cout::Text("Sending array", "M", kWhite);
79 Cout::Text(submit);
80 }
81 gSystem->Exec(submit);
82 } else {
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);
89 }
90 }
91 } else {
92 Cout::Text("NO ${HAL::CONST::JOB_FILE} trying to guess command pattern", "L", kYellow);
93 if (fArray) {
94 gSystem->Exec(Form("%s %s/job_array", fDir.Data(), fSubmitCommand.Data()));
95 } else {
96 for (int i = fStartJob; i <= fEndJob; i++) {
97 gSystem->Exec(Form("%s %s/job_%i", fDir.Data(), fSubmitCommand.Data(), i));
98 }
99 }
100 }
101 }
102
103 void Jobs::CreateJobs() {
104 if (fArray) {
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;
109 job_array.close();
110 gSystem->Exec(Form("chmod a+x %s/job_array", fDir.Data()));
111 } else {
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;
119 }
120 job_file.close();
121 gSystem->Exec(Form("chmod a+x %s/job_%i", fDir.Data(), iJob));
122 }
123 }
124 }
125
126 Jobs::~Jobs() {}
127
128 Int_t Jobs::GetNVariables(TString textfile) {
129 if (textfile.EndsWith(".xml")) { return GetNVariablesXML(textfile); }
130 return GetNVariablesTxt(textfile);
131 }
132
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);
136 }
137
138 void Jobs::CreateDummyTxtFile(TString textfile, Int_t jobs, Int_t vars) {
139 std::ofstream file;
140 file.open(textfile);
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) << " ";
147 }
148 file << std::endl;
149 }
150 file.close();
151 }
152
153 Int_t Jobs::GetNVariablesTxt(TString textfile) {
154 std::ifstream file;
155 file.open(textfile);
156 if (file.good()) {
157 int pars, jobs;
158 TString dummy;
159 file >> dummy;
160 file >> dummy >> pars;
161 if (dummy != "NVAR") {
162 std::cout << "HalJobs::CreateJobsTxt failed to find Nvar: that specify "
163 "number of variables"
164 << std::endl;
165 }
166 file >> dummy >> jobs;
167 if (dummy != "NJOBS") {
168 std::cout << "HalJobs::CreateJobsTxt failed to find Njobs that specify "
169 "number of variables"
170 << std::endl;
171 }
172 file.close();
173 return pars;
174 } else {
175 std::cout << "HalJobs::GetNVariable failed to find txt file" << std::endl;
176 file.close();
177 return 0;
178 }
179 }
180
181 Int_t Jobs::GetNVariablesXML(TString xmlfile) {
182 XMLFile file(xmlfile);
183 XMLNode* root = file.GetRootNode();
184 return root->GetChild(0)->GetNAttributes();
185 }
186
187 TString Jobs::GetParameterTxt(TString textfile, Int_t job, Int_t var) {
188 Int_t vals = GetNVariables(textfile);
189 if (vals == 0) return "";
190 std::ifstream file;
191 file.open(textfile);
192 int pars, jobs;
193 TString dummy;
194 file >> dummy;
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++) {
201 file >> dummy;
202 }
203 }
204 for (int j = 0; j <= var; j++)
205 file >> dummy;
206 file.close();
207 return dummy;
208 }
209
210 TString Jobs::GetParameterXml(TString xmlfile, Int_t job, Int_t var) {
211 XMLFile p(xmlfile);
212 XMLNode* root = p.GetRootNode();
213 XMLNode* nod = root->GetChild(job);
214 TString val = nod->GetAttrib(var)->GetValue();
215 return val;
216 }
217
218 void Jobs::CreateDummyXMLFile(TString xmlfile, Int_t jobs, Int_t vars) {
219 XMLFile file(xmlfile, "write");
220 file.CreateRootNode("jobs");
221 XMLNode* node = file.GetRootNode();
222 for (int iJobs = 0; iJobs < jobs; iJobs++) {
223 XMLNode* job = new XMLNode("job", Form("job%iJobs", 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)));
226 }
227 node->AddChild(job);
228 }
229 file.Close();
230 }
231} // namespace Hal
void CreateRootNode(TString name)
Definition XMLNode.cxx:128
void Close()
Definition XMLNode.cxx:132
XMLNode * GetRootNode() const
Definition XMLNode.h:180
void AddChild(XMLNode *node)
Definition XMLNode.h:94
void AddAttrib(XMLAttrib *attrib)
Definition XMLNode.cxx:75