28 #include "TRestTask.h"
30 #include "TFunction.h"
31 #include "TMethodArg.h"
32 #include "TRestManager.h"
33 #include "TRestReflector.h"
34 #include "TRestStringHelper.h"
43 #define PTR_ADDR_PREFIX "0x"
45 #define PTR_ADDR_PREFIX ""
53 fNRequiredArgument = 0;
54 if (this->ClassName() != (
string)
"TRestTask") {
67 fNRequiredArgument = 0;
70 if (mode == TASK_MACRO) {
73 int n = filename.find_last_of(
'.');
76 funcName = filename.substr(0, n);
78 funcName = TaskString;
80 TFunction* f = gROOT->GetGlobalFunction(funcName.c_str());
84 fNRequiredArgument = f->GetNargs() - f->GetNargsOpt();
85 fInvokeMethod = funcName;
89 TList* list = f->GetListOfMethodArgs();
90 for (
int i = 0; i < list->GetSize(); i++) {
91 TMethodArg* arg = (TMethodArg*)list->At(i);
92 string type = arg->GetTypeName();
93 fArgumentTypes.push_back(type ==
"string" || type ==
"char" || type ==
"TString");
94 fArgumentNames.push_back(arg->GetName());
96 string fConstructedCommand =
"";
99 }
else if (mode == TASK_CPPCMD) {
102 string cmd = (string)TaskString;
106 if (
Split(cmd,
"->").size() != 2) {
107 RESTWarning <<
"command"
108 <<
" \"" << cmd <<
"\" "
109 <<
"is illegal!" << RESTendl;
113 name =
Split(cmd,
"->")[0];
114 call =
Split(cmd,
"->")[1];
117 int p1 = call.find_first_of(
"(");
118 int p2 = call.find_last_of(
")");
119 if (p1 == -1 || p2 == -1 || p1 >= p2) {
120 RESTWarning <<
"command"
121 <<
" \"" << cmd <<
"\" "
122 <<
"is illegal!" << RESTendl;
126 fInvokeObject = name;
127 fInvokeMethod = call.substr(0, p1);
129 string args = call.substr(p1 + 1, p2 - p1 - 1);
130 fArgumentValues =
Split(args,
",");
131 fConstructedCommand = cmd;
132 }
else if (mode == TASK_CLASS) {
134 }
else if (mode == TASK_SHELLCMD) {
135 fConstructedCommand = TaskString;
136 fInvokeMethod =
Split(fConstructedCommand,
" ")[0];
152 if (arg.size() < fNRequiredArgument) {
156 fArgumentValues = arg;
163 if (fInvokeMethod ==
"") {
164 RESTError <<
"no task specified for TRestTask!!!" << RESTendl;
167 if (fMode == TASK_MACRO) {
169 for (
unsigned int i = 0; i < fArgumentValues.size(); i++) {
170 if (fArgumentValues[i] ==
"NOT SET") {
171 RESTError <<
"TRestTask : argument " << i <<
" not set! Task will not run!" << RESTendl;
175 fConstructedCommand = fInvokeMethod +
"(";
176 for (
unsigned int i = 0; i < fArgumentValues.size(); i++) {
177 if (fArgumentTypes[i] == 1) {
178 fConstructedCommand +=
"\"" +
Replace(fArgumentValues[i],
"\\",
"\\\\", 0) +
"\"";
180 fConstructedCommand += fArgumentValues[i];
183 if (i < fArgumentValues.size() - 1) fConstructedCommand +=
",";
185 fConstructedCommand +=
")";
187 cout <<
"Command : " << fConstructedCommand << endl;
189 gInterpreter->ProcessLine(fConstructedCommand.c_str());
191 }
else if (fMode == TASK_CPPCMD) {
193 if (mgr ==
nullptr) {
194 RESTError <<
"no target specified for the command:" << RESTendl;
195 RESTError << fConstructedCommand << RESTendl;
199 if (meta ==
nullptr) {
200 RESTError <<
"cannot file metadata: " << fInvokeObject <<
" in TRestManager" << RESTendl;
201 RESTError <<
"command: " << fConstructedCommand << RESTendl;
204 string type = meta->ClassName();
205 string cmd = Form(
"%s* %s = (%s*)%s;", type.c_str(), fInvokeObject.c_str(), type.c_str(),
206 (PTR_ADDR_PREFIX + ToString(meta)).c_str());
208 TInterpreter::EErrorCode err;
209 gInterpreter->ProcessLine(cmd.c_str(), &err);
210 if (err != TInterpreter::kNoError) {
211 RESTError <<
"TRestTask::RunTask(): unknown error" << RESTendl;
212 RESTError <<
"code: " << err << RESTendl;
215 gInterpreter->ProcessLine(fConstructedCommand.c_str(), &err);
216 if (err != TInterpreter::kNoError) {
217 RESTError <<
"TRestTask: failed to execute cpp command, error code: " << err
219 RESTError << fConstructedCommand << RESTendl;
220 RESTError <<
"Check your <AddTask section!" << RESTendl;
225 }
else if (fMode == TASK_SHELLCMD) {
226 int z = system(fConstructedCommand.c_str());
227 if (z != 0) RESTError <<
"Problem launching : " << fConstructedCommand << RESTendl;
238 RESTError << fInvokeMethod <<
"() Gets invalided input!" << RESTendl;
239 cout <<
"You should give the following arguments (* is mandatory input):" << endl;
240 unsigned int n = fArgumentNames.size();
241 for (
unsigned int i = 0; i < n; i++) {
242 cout << (i < fNRequiredArgument ?
"*" :
"") << fArgumentNames[i] << endl;
244 }
else if (fMode == 1) {
245 }
else if (fMode == 2) {
246 RESTError <<
"Macro class \"" << this->ClassName() <<
"\" gets invalided input!" << RESTendl;
247 RESTError <<
"You should give the following arguments ( * : necessary input):" << RESTendl;
249 for (
unsigned int i = 1; i < n; i++) {
250 if (i < fNRequiredArgument + 1) RESTError <<
"*";
274 string macfilelists =
276 auto macfiles =
Split(macfilelists,
"\n");
279 if (macfiles.size() != 0 && macfiles[0] !=
"") {
280 RESTInfo <<
"Found MacroFile " << macfiles[0] << TRestStringOutput::RESTendl;
283 if (gInterpreter->LoadFile(macfiles[0].c_str()) == 0) {
285 if (tsk->GetMode() == TASK_ERROR) {
287 <<
"Task file: " << macfiles[0]
288 <<
" loaded but method not found. Make sure it contains the method with same name as "
290 << TRestStringOutput::RESTendl;
295 RESTError <<
"Task file: " << macfiles[0] <<
" contains error" << TRestStringOutput::RESTendl;
302 if (tsk !=
nullptr && tsk->InheritsFrom(
"TRestTask")) {
303 tsk->SetMode(TASK_CLASS);
307 RESTError <<
"REST ERROR. Task : " << taskName <<
" not found!!" << TRestStringOutput::RESTendl;
311 TRestTask* TRestTask::GetTaskFromCommand(TString cmd) {
312 REST_TASKMODE mode = TASK_CPPCMD;
313 if (((
string)cmd).find(
"->") == string::npos) mode = TASK_SHELLCMD;
316 if (tsk->GetMode() == TASK_ERROR) {
int GetNumberOfDataMembers() const
Get the number of data members of a class.
std::string name
Name field.
TRestReflector GetDataMember(const std::string &name)
Find the class's datamember as TRestReflector object, including those from base class.
Managing applications and executing tasks.
TRestMetadata * GetMetadata(std::string name)
Get the application metadata class, according to the name.
Wrapping REST macros into tasks.
void SetArgumentValue(std::vector< std::string > arg)
Set argument directly with a list of string.
virtual void RunTask(TRestManager *)
Run the task with command line.
virtual void PrintArgumentHelp()
Default helper method both for TRestTask and any TRestTask-inherited class.
TRestTask()
TRestTask default constructor.
static TRestTask * GetTaskFromMacro(TString Name)
Static method to instantiate a TRestTask object with "MacroName".
void InitFromConfigFile() override
Starter method. Looks through the rml sections and set argument/datamenber value.
TRestReflector Assembly(const std::string &typeName)
Assembly an object of type: typeName, returning the allocated memory address and size.
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.