00001
00002
00004 #include "JobControl/JobCRootCommandModule.h"
00005
00006 #include "JobControl/JobCRootModule.h"
00007
00008 #include <cassert>
00009
00010
00011 #include "TROOT.h"
00012 #include "TSystem.h"
00013 #include "TObjectTable.h"
00014 #include "TRint.h"
00015 #include "TEnv.h"
00016 #include "TInterpreter.h"
00017
00018 #include "TString.h"
00019
00020 #include "JobControl/JobCResult.h"
00021 #include "JobControl/JobCModuleRegistry.h"
00022 #include "JobControl/JobCommand.h"
00023
00024 #include "MessageService/MsgService.h"
00025
00026 CVSID("$Id: JobCRootCommandModule.cxx,v 1.11 2005/06/25 20:03:11 rhatcher Exp $");
00027 JOBMODULE(JobCRootCommandModule,
00028 "RootCommand", "Handle lists of ROOT commands to run during path execution");
00029
00030
00031
00032 JobCRootCommandModule::JobCRootCommandModule()
00033 {
00034 MSG("Root",Msg::kVerbose) << "JobCRootCommandModule::ctor" << endl;
00035 }
00036
00037
00038
00039 JobCRootCommandModule::~JobCRootCommandModule()
00040 {
00041 MSG("Root",Msg::kVerbose) << "JobCRootCommandModule::dtor" << endl;
00042 }
00043
00044
00045
00046 void JobCRootCommandModule::Report()
00047 {
00048 MSG("Root",Msg::kWarning) << "JobCRootCommandModule::Report" << endl;
00049 }
00050
00051
00052
00053 void JobCRootCommandModule::Reset()
00054 {
00055 MSG("Root",Msg::kWarning) << "JobCRootCommandModule::Reset" << endl;
00056 }
00057
00058
00059
00060 void JobCRootCommandModule::Init()
00061 {
00062
00063
00064
00065 MSG("Root",Msg::kVerbose) << "JobCRootCommandModule::Init" << endl;
00066
00067 static bool first = true;
00068 if (!first) return;
00069
00070 first = false;
00071 JobCRootModule::InitRoot();
00072
00073 }
00074
00075
00076 JobCResult JobCRootCommandModule::Ana(const MomNavigator *mom)
00077 {
00078 MSG("Root",Msg::kDebug)
00079 << GetUniqueName() << " JobCRootCommandModule::Ana" << endl;
00080
00081 static JobCResult result;
00082 result = JobCResult(JobCResult::kAOK);
00083
00084 JobCRootModule::PassPtrToRoot("MomNavigator","mom",mom,true);
00085 JobCRootModule::PassPtrToRoot("JobCResult","jobcresultptr",&result,false);
00086 Init();
00087 TApplication* app = gROOT->GetApplication();
00088 app->ProcessLine("JobCResult& jobcresult = *jobcresultptr;");
00089
00090 ProcessLines("Ana");
00091 MSG("JobC",Msg::kDebug)
00092 << "RootCommand::Ana returned result " << result << endl;
00093 return result;
00094 }
00095
00096 JobCResult JobCRootCommandModule::Reco(MomNavigator *mom)
00097 {
00098 MSG("Root",Msg::kDebug)
00099 << GetUniqueName() << " JobCRootCommandModule::Reco" << endl;
00100
00101 static JobCResult result;
00102 result = JobCResult(JobCResult::kAOK);
00103 Init();
00104 JobCRootModule::PassPtrToRoot("MomNavigator","mom",mom,false);
00105 JobCRootModule::PassPtrToRoot("JobCResult","jobcresultptr",&result,false);
00106
00107 TApplication* app = gROOT->GetApplication();
00108 app->ProcessLine("JobCResult& jobcresult = *jobcresultptr;");
00109
00110 ProcessLines("Reco");
00111 MSG("JobC",Msg::kDebug)
00112 << "RootCommand::Reco returned result " << result << endl;
00113 return result;
00114 }
00115
00116
00117 void JobCRootCommandModule::BeginJob()
00118 {
00119 MSG("JobC",Msg::kDebug)
00120 << GetUniqueName() << " RootCommand::BeginJob" << endl;
00121 ProcessLines("BeginJob");
00122 }
00123 void JobCRootCommandModule::EndJob()
00124 {
00125 MSG("JobC",Msg::kDebug)
00126 << GetUniqueName() << " RootCommand::EndJob" << endl;
00127 ProcessLines("EndJob");
00128 }
00129 void JobCRootCommandModule::BeginFile()
00130 {
00131 MSG("JobC",Msg::kDebug)
00132 << GetUniqueName() << " RootCommand::BeginFile "
00133 << GetCurrentFile() << endl;
00134 ProcessLines("BeginFile");
00135 }
00136 void JobCRootCommandModule::EndFile()
00137 {
00138 MSG("JobC",Msg::kDebug)
00139 << GetUniqueName() << " RootCommand::EndFile "
00140 << GetLastFile() << endl;
00141 ProcessLines("EndFile");
00142 }
00143 void JobCRootCommandModule::BeginRun()
00144 {
00145 MSG("JobC",Msg::kDebug)
00146 << GetUniqueName() << " RootCommand::BeginRun "
00147 << GetCurrentRun() << endl;
00148 ProcessLines("BeginRun");
00149 }
00150 void JobCRootCommandModule::EndRun()
00151 {
00152 MSG("JobC",Msg::kDebug)
00153 << GetUniqueName() << " RootCommand::EndRun "
00154 << GetLastRun() << endl;
00155 ProcessLines("EndRun");
00156 }
00157
00158
00159 JobCResult JobCRootCommandModule::Get(MomNavigator *mom)
00160 {
00161 MSG("Root",Msg::kDebug) << "JobCRootCommandModule::Get" << endl;
00162
00163 JobCRootModule::PassPtrToRoot("MomNavigator","mom",mom,false);
00164 ProcessLines("Get");
00165 return JobCResult::kAOK;
00166 }
00167
00168 JobCResult JobCRootCommandModule::Put(const MomNavigator *mom)
00169 {
00170 MSG("Root",Msg::kDebug) << "JobCRootCommandModule::Put" << endl;
00171
00172 JobCRootModule::PassPtrToRoot("MomNavigator","mom",mom,true);
00173 ProcessLines("Put");
00174 return JobCResult::kAOK;
00175 }
00176
00177
00178 vector<string>* JobCRootCommandModule::WhichLinesList(string whichlist)
00179 {
00180 if (whichlist == "Ana" ) return &fAnaLines;
00181 if (whichlist == "Reco" ) return &fRecoLines;
00182
00183 if (whichlist == "BeginJob" ) return &fBeginJobLines;
00184 if (whichlist == "EndJob" ) return &fEndJobLines;
00185 if (whichlist == "BeginFile" ) return &fBeginFileLines;
00186 if (whichlist == "EndFile" ) return &fEndFileLines;
00187 if (whichlist == "BeginRun" ) return &fBeginRunLines;
00188 if (whichlist == "EndRun" ) return &fEndRunLines;
00189
00190 if (whichlist == "Get" ) return &fGetLines;
00191 if (whichlist == "Put" ) return &fPutLines;
00192
00193 MSG("Root",Msg::kInfo)
00194 << "JobCRootCommandModule::WhichLinesList \""
00195 << whichlist
00196 << "\" not a recoginized list"
00197 << endl;
00198
00199 return 0;
00200 }
00201
00202 void JobCRootCommandModule::ProcessLines(string whichlist)
00203 {
00204
00205
00206
00207 vector<string>* lines = WhichLinesList(whichlist);
00208 if (!lines) return;
00209
00210
00211 Init();
00212 TApplication* app = gROOT->GetApplication();
00213
00214 vector<string>::const_iterator itrend(lines->end());
00215 vector<string>::const_iterator itr;
00216
00217 for (itr = lines->begin(); itr != itrend; ++itr) {
00218 app->ProcessLine((*itr).c_str());
00219 }
00220
00221 }
00222
00223
00224
00225 void JobCRootCommandModule::HandleCommand(JobCommand* cmd)
00226 {
00227
00228 static int ncmd = 0;
00229 ncmd++;
00230
00231 class BadJobCRootCommandCmd { };
00232
00233
00234 Init();
00235
00236 try {
00237 TApplication* app = gROOT->GetApplication();
00238 if (!app) abort();
00239 const char* c = cmd->PopCmd();
00240 if (c) {
00241 string sc(c);
00242 const char* entry = cmd->PopCmd();
00243
00244 if (!entry) entry = "Ana";
00245 string sentry(entry);
00246
00247 vector<string>* lines = WhichLinesList(sentry);
00248 if (!lines) {
00249 MSG("Root",Msg::kWarning)
00250 << "\"" << sentry << "\":"
00251 << " Not a valid entry point for " << sc << " command" << endl;
00252 throw BadJobCRootCommandCmd();
00253 }
00254 if (sc == "AddLine" || sc == "Addline") {
00255 string wholeLine;
00256
00257 while (const char* opt = cmd->PopOpt()) {
00258 wholeLine += " ";
00259 wholeLine += opt;
00260 }
00261 lines->push_back(wholeLine);
00262 }
00263 else if (sc == "Clear") {
00264 lines->clear();
00265 }
00266 else if (sc == "Show") {
00267 vector<string>::const_iterator itrend(lines->end());
00268 vector<string>::const_iterator itr;
00269 int i = 0;
00270 for (itr = lines->begin(); itr != itrend; ++itr) {
00271 MSG("Root",Msg::kInfo)
00272 << " [" << i++ << "] " << (*itr) << endl;
00273 }
00274 }
00275 else {
00276 MSG("Root",Msg::kWarning)
00277 << "\"" << sc << "\":"
00278 << " Not a valid command" << endl;
00279 throw BadJobCRootCommandCmd();
00280 }
00281 }
00282 else {
00283 throw BadJobCRootCommandCmd();
00284 }
00285 }
00286 catch ( BadJobCRootCommandCmd ) {
00287 static bool firsthelp = true;
00288 if (firsthelp) Help();
00289 firsthelp = false;
00290 }
00291 }
00292
00293
00294
00295 void JobCRootCommandModule::Help()
00296 {
00297
00298
00299
00300
00301 const char *name = this->GetName();
00302
00303 MSG("JobC", Msg::kInfo)
00304 << "Help for '" << name << "': " << endl
00305 << name
00306 << " is a JobCModule allowing access to scripts from JobControl."
00307 << endl
00308 << " Lines of text are processed as if typed at the ROOT prompt at"
00309 << endl
00310 << " various stages of the job process (ala entry points)."
00311 << endl
00312 << " The legal entry points are: "
00313 << endl << " Analysis/Reconstruction:"
00314 << endl << " Ana - read-only analysis of data records"
00315 << endl << " Reco - read-write access to data records"
00316 << endl << " Status changes:"
00317 << endl << " BeginJob - Notice of begin job"
00318 << endl << " EndJob - Notice of end job"
00319 << endl << " BeginFile - Notice of begin of file"
00320 << endl << " EndFile - Notice of end of file"
00321 << endl << " BeginRun - Notice of begin of run"
00322 << endl << " EndRun - Notice of end of run"
00323 << endl << " I/O Methods (in general provided by framework):"
00324 << endl << " Get - get a header/event"
00325 << endl << " Put - write a header/event"
00326 << endl << endl
00327 << " As necessary the relevant MomNavigator* is made available to ROOT as \"mom\""
00328 << endl
00329 << " \"Ana\" is the default entry point"
00330 << endl << endl
00331 << "Commands implemented:"
00332 << endl << endl
00333
00334 << " /" << name << "/AddLine[/<entrypoint>] <arbitrary text>"
00335 << endl
00336 << " Add a line of text to end of the list -- text is interpeted when the "
00337 << endl
00338 << " entry point is processed in the path"
00339 << endl << endl
00340
00341 << " /" << name << "/Show[/<entrypoint>]"
00342 << endl
00343 << " Show the ordered list of lines. "
00344 << endl << endl
00345
00346 << " /" << name << "/Clear[/<entrypoint>]"
00347 << endl
00348 << " Clear the list of lines. "
00349 << endl << endl
00350
00351 << endl;
00352 }
00353