Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DemoSnarlList.cxx

Go to the documentation of this file.
00001 
00002 // $Id: DemoSnarlList.cxx,v 1.2 2009/07/21 22:10:17 gmieg Exp $
00003 //
00004 // Given a list of run/snarl numbers, this module passes only those
00005 // records whose run/snarl number is in the list
00006 //
00007 // messier@huhepl.harvard.edu
00009 #include "Demo/DemoSnarlList.h"
00010 #include <algorithm>
00011 #include <fstream>
00012 #include "JobControl/JobCommand.h"
00013 #include "JobControl/JobCModuleRegistry.h" // JOBMODULE registration macro
00014 #include "MessageService/MsgService.h"
00015 #include "MinosObjectMap/MomNavigator.h"
00016 #include "RawData/RawRecord.h"
00017 #include "RawData/RawDaqSnarlHeader.h"
00018 #include "Util/UtilString.h"
00019 
00020 CVSID("$Id: DemoSnarlList.cxx,v 1.2 2009/07/21 22:10:17 gmieg Exp $");
00021 JOBMODULE(DemoSnarlList,
00022           "SnarlList","Filter records based on run/snarl numbers.");
00023 
00024 //......................................................................
00025 
00026 DemoSnarlList::DemoSnarlList() : fIsSorted(false), fVetoMode(false) { }
00027 
00028 //......................................................................
00029 
00030 JobCResult DemoSnarlList::Ana(const MomNavigator* mom) 
00031 {
00032 //======================================================================
00033 // Are there records in mom whose run/snarl number matchs one in the
00034 // list?
00035 //======================================================================
00036   TObject* obj;
00037   for (int i=0; (obj=mom->At(i)); ++i) {
00038     const RawRecord* rr = dynamic_cast<RawRecord*>(obj);
00039     if (rr) {
00040       const RawDaqSnarlHeader* 
00041         rdsh = dynamic_cast<const RawDaqSnarlHeader*>(rr->GetRawHeader());
00042       if (rdsh) {
00043         int run   = rdsh->GetRun();
00044         int snarl = rdsh->GetSnarl();
00045         std::string k(this->Key(run,snarl));
00046         if (fIsSorted==false) { // Make sure we're sorted prior to search...
00047           fSnarlList.sort();
00048           fIsSorted = true;
00049         }
00050         if (std::binary_search(fSnarlList.begin(),fSnarlList.end(),k)) {
00051           if (fVetoMode) {
00052 
00053             int nrun, nsnarl;
00054             MsgService::Instance()->GetCurrentRunSnarl(nrun, nsnarl);
00055             MSG("Demo",Msg::kWarning)
00056                                 << "DemoSnarlList reject at nsnarl = "
00057                                 <<  nsnarl << endl;
00058 
00059             return JobCResult::kFailed;
00060           }
00061           else return JobCResult::kPassed;
00062         }
00063       }
00064     }
00065   }
00066   if (fVetoMode) return JobCResult::kPassed;
00067   else {
00068 
00069     int nrun, nsnarl;
00070     MsgService::Instance()->GetCurrentRunSnarl(nrun, nsnarl);
00071     MSG("Demo",Msg::kWarning) << "DemoSnarlList reject at nsnarl = "
00072                               <<  nsnarl << endl;
00073     return JobCResult::kFailed;
00074 
00075   }
00076 }
00077 
00078 //......................................................................
00079 
00080 void DemoSnarlList::Report() 
00081 {
00082 //======================================================================
00083 // Print the snarl numbers in the list
00084 //======================================================================
00085   std::list<std::string>::iterator itr(fSnarlList.begin());
00086   std::list<std::string>::iterator itrEnd(fSnarlList.end());
00087   MSG("Demo",Msg::kInfo) << "Snarls in list:\n";
00088   for (; itr!=itrEnd; ++itr) {
00089     MSG("Demo",Msg::kInfo) << (*itr) << "\n";
00090   }
00091 }
00092 
00093 //......................................................................
00094 
00095 void DemoSnarlList::HandleCommand(JobCommand *cmd) 
00096 {
00097   std::string c = cmd->PopCmd();
00098   if (!(c=="Add"||c=="Remove"||c=="VetoMode")) {
00099     MSG("Demo",Msg::kWarning) << 
00100       "Only: Add [run#] [snarl#] [filename] and \n" <<
00101       "      Remove [run#] [snarl#] [filename] and \n" <<
00102       "      VetoMode <on|off> are supported.\n";
00103     return;
00104   }
00105 
00106   int run   = -1;
00107   int snarl = -1;
00108   while (cmd->HaveOpt()) {
00109     std::string opt = cmd->PopOpt();
00110     if (UtilString::IsInt(opt.c_str())) {
00111       if (run!=-1) {
00112         snarl = atoi(opt.c_str());
00113         if (c=="Add") {
00114           this->AddRunSnarl(run,snarl);
00115         }
00116         else if (c=="Remove") {
00117           this->RemoveRunSnarl(run,snarl);
00118         }
00119         run   = -1;
00120         snarl = -1;
00121       }
00122       else {
00123         run = atoi(opt.c_str());
00124       }
00125     }
00126     else if (c=="VetoMode"&&opt.c_str()!="off") {
00127       fVetoMode = true;
00128     }
00129     else { // Assume all strings are file names
00130       if (c=="Add") {
00131         this->AddUsingFile(opt.c_str());
00132       }
00133       else if (c=="Remove") {
00134         this->RemoveUsingFile(opt.c_str());
00135       }
00136     }
00137   }
00138 }
00139 
00140 //......................................................................
00141 
00142 void DemoSnarlList::AddRunSnarl(int r, int s) 
00143 {
00144 //======================================================================
00145 // Add the run/snarl (r,s) to the list of records to pass
00146 //======================================================================
00147   std::string k(this->Key(r,s));
00148   fSnarlList.push_back(k);
00149   fIsSorted = false;
00150 }
00151 
00152 //......................................................................
00153 
00154 void DemoSnarlList::RemoveRunSnarl(int r, int s) 
00155 {
00156 //======================================================================
00157 // Remove the run/snarl (r,s) to the list of records to pass
00158 //======================================================================
00159   std::string k(this->Key(r,s));
00160   std::list<std::string>::iterator itr;
00161   itr = std::find(fSnarlList.begin(), fSnarlList.end(), k);
00162   if (itr!=fSnarlList.end()) {
00163     fSnarlList.erase(itr);
00164   }
00165 }
00166 
00167 //......................................................................
00168 
00169 void DemoSnarlList::AddUsingFile(const char* f) 
00170 {
00171 //======================================================================
00172 // Add all the run/snarls from the file f to the list. Assumes format
00173 // is run<space>snarl
00174 //======================================================================
00175   std::ifstream ifs(f);
00176   int run, snarl;
00177   while (ifs) { 
00178     ifs >> run >> snarl;
00179     this->AddRunSnarl(run,snarl);
00180   }
00181 }
00182 
00183 //......................................................................
00184 
00185 void DemoSnarlList::RemoveUsingFile(const char* f) 
00186 {
00187 //======================================================================
00188 // Remove all the run/snarls from the file f to the list. Assumes format
00189 // is run<space>snarl
00190 //======================================================================
00191   std::ifstream ifs(f);
00192   int run, snarl;
00193   while (ifs) { 
00194     ifs >> run >> snarl;
00195     this->RemoveRunSnarl(run,snarl);
00196   }
00197 }
00198 
00199 //......................................................................
00200 
00201 std::string DemoSnarlList::Key(int run, int snarl) 
00202 {
00203 //======================================================================
00204 // Construct a sortable key using run and snarl numbers
00205 //======================================================================
00206   char k[1024];
00207   sprintf(k,"%12d.%12.12d",run,snarl);
00208   return k;
00209 }
00210 
00212 

Generated on Mon Feb 15 11:06:37 2010 for loon by  doxygen 1.3.9.1