Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

DemoSnarlList.cxx

Go to the documentation of this file.
00001 
00002 // $Id: DemoSnarlList.cxx,v 1.1 2002/05/14 22:50:18 messier 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.1 2002/05/14 22:50:18 messier Exp $");
00021 JOBMODULE(DemoSnarlList,
00022           "SnarlList","Filter records based on run/snarl numbers.");
00023 
00024 //......................................................................
00025 
00026 DemoSnarlList::DemoSnarlList() : fIsSorted(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           return JobCResult::kPassed;
00052         }
00053       }
00054     }
00055   }
00056   return JobCResult::kFailed;
00057 }
00058 
00059 //......................................................................
00060 
00061 void DemoSnarlList::Report() 
00062 {
00063 //======================================================================
00064 // Print the snarl numbers in the list
00065 //======================================================================
00066   std::list<std::string>::iterator itr(fSnarlList.begin());
00067   std::list<std::string>::iterator itrEnd(fSnarlList.end());
00068   MSG("Demo",Msg::kInfo) << "Snarls in list:\n";
00069   for (; itr!=itrEnd; ++itr) {
00070     MSG("Demo",Msg::kInfo) << (*itr) << "\n";
00071   }
00072 }
00073 
00074 //......................................................................
00075 
00076 void DemoSnarlList::HandleCommand(JobCommand *cmd) 
00077 {
00078   std::string c = cmd->PopCmd();
00079   if (!(c=="Add"||c=="Remove")) {
00080     MSG("Demo",Msg::kWarning) << 
00081       "Only: Add [run#] [snarl#] [filename] and \n" <<
00082       "      Remove [run#] [snarl#] [filename] are supported.\n";
00083     return;
00084   }
00085 
00086   int run   = -1;
00087   int snarl = -1;
00088   while (cmd->HaveOpt()) {
00089     std::string opt = cmd->PopOpt();
00090     if (UtilString::IsInt(opt.c_str())) {
00091       if (run!=-1) {
00092         snarl = atoi(opt.c_str());
00093         if (c=="Add") {
00094           this->AddRunSnarl(run,snarl);
00095         }
00096         else {
00097           this->RemoveRunSnarl(run,snarl);
00098         }
00099         run   = -1;
00100         snarl = -1;
00101       }
00102       else {
00103         run = atoi(opt.c_str());
00104       }
00105     }
00106     else { // Assume all strings are file names
00107       if (c=="Add") {
00108         this->AddUsingFile(opt.c_str());
00109       }
00110       else {
00111         this->RemoveUsingFile(opt.c_str());
00112       }
00113     }
00114   }
00115 }
00116 
00117 //......................................................................
00118 
00119 void DemoSnarlList::AddRunSnarl(int r, int s) 
00120 {
00121 //======================================================================
00122 // Add the run/snarl (r,s) to the list of records to pass
00123 //======================================================================
00124   std::string k(this->Key(r,s));
00125   fSnarlList.push_back(k);
00126   fIsSorted = false;
00127 }
00128 
00129 //......................................................................
00130 
00131 void DemoSnarlList::RemoveRunSnarl(int r, int s) 
00132 {
00133 //======================================================================
00134 // Remove the run/snarl (r,s) to the list of records to pass
00135 //======================================================================
00136   std::string k(this->Key(r,s));
00137   std::list<std::string>::iterator itr;
00138   itr = std::find(fSnarlList.begin(), fSnarlList.end(), k);
00139   if (itr!=fSnarlList.end()) {
00140     fSnarlList.erase(itr);
00141   }
00142 }
00143 
00144 //......................................................................
00145 
00146 void DemoSnarlList::AddUsingFile(const char* f) 
00147 {
00148 //======================================================================
00149 // Add all the run/snarls from the file f to the list. Assumes format
00150 // is run<space>snarl
00151 //======================================================================
00152   std::ifstream ifs(f);
00153   int run, snarl;
00154   while (ifs) { 
00155     ifs >> run >> snarl;
00156     this->AddRunSnarl(run,snarl);
00157   }
00158 }
00159 
00160 //......................................................................
00161 
00162 void DemoSnarlList::RemoveUsingFile(const char* f) 
00163 {
00164 //======================================================================
00165 // Remove all the run/snarls from the file f to the list. Assumes format
00166 // is run<space>snarl
00167 //======================================================================
00168   std::ifstream ifs(f);
00169   int run, snarl;
00170   while (ifs) { 
00171     ifs >> run >> snarl;
00172     this->RemoveRunSnarl(run,snarl);
00173   }
00174 }
00175 
00176 //......................................................................
00177 
00178 std::string DemoSnarlList::Key(int run, int snarl) 
00179 {
00180 //======================================================================
00181 // Construct a sortable key using run and snarl numbers
00182 //======================================================================
00183   char k[1024];
00184   sprintf(k,"%12d.%12.12d",run,snarl);
00185   return k;
00186 }
00187 
00189 

Generated on Wed Sep 4 19:01:02 2002 for loon by doxygen1.2.16