CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/EventFilter/StorageManager/src/InitMsgCollection.cc

Go to the documentation of this file.
00001 // $Id: InitMsgCollection.cc,v 1.16 2011/03/22 16:27:00 mommsen Exp $
00003 
00004 #include "DataFormats/Streamer/interface/StreamedProducts.h"
00005 #include "EventFilter/StorageManager/interface/InitMsgCollection.h"
00006 #include "FWCore/Framework/interface/EventSelector.h"
00007 #include "FWCore/Utilities/interface/DebugMacros.h"
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 #include "IOPool/Streamer/interface/DumpTools.h"
00010 #include "IOPool/Streamer/interface/OtherMessage.h"
00011 #include "IOPool/Streamer/interface/StreamerInputSource.h"
00012 #include "IOPool/Streamer/interface/Utilities.h"
00013 
00014 #include "boost/algorithm/string/trim.hpp"
00015 #include <iostream>
00016 
00017 using namespace stor;
00018 using namespace edm;
00019 
00020 InitMsgCollection::InitMsgCollection()
00021 {
00022   clear();
00023 }
00024 
00025 
00026 InitMsgCollection::~InitMsgCollection()
00027 {
00028 }
00029 
00030 
00031 bool InitMsgCollection::addIfUnique(InitMsgView const& initMsgView)
00032 {
00033   boost::mutex::scoped_lock sl(listLock_);
00034 
00035   // test the output module label for validity
00036   std::string inputOMLabel = initMsgView.outputModuleLabel();
00037   std::string trimmedOMLabel = boost::algorithm::trim_copy(inputOMLabel);
00038   if (trimmedOMLabel.empty()) {
00039     throw cms::Exception("InitMsgCollection", "addIfUnique:")
00040       << "Invalid INIT message: the HLT output module label is empty!"
00041       << std::endl;
00042   }
00043 
00044   // initially, assume that we will want to include the new message
00045   bool addToList = true;
00046 
00047   // if this is the first INIT message that we've seen, we just add it
00048   if (initMsgList_.empty()) {
00049     this->add(initMsgView);
00050   }
00051 
00052   // if this is a subsequent INIT message, check if it is unique
00053   else {
00054 
00055     // loop over the existing messages
00056     for (InitMsgList::iterator msgIter = initMsgList_.begin(),
00057            msgIterEnd = initMsgList_.end();
00058          msgIter != msgIterEnd;
00059          msgIter++)
00060     {
00061       InitMsgSharedPtr serializedProds = msgIter->first;
00062       InitMsgView existingInitMsg(&(*serializedProds)[0]);
00063       std::string existingOMLabel = existingInitMsg.outputModuleLabel();
00064 
00065       // check if the output module labels match
00066       if (inputOMLabel == existingOMLabel) {
00067         // we already have a copy of the INIT message
00068         addToList = false;
00069         ++msgIter->second;
00070         break;
00071       }
00072     }
00073 
00074     // if we've found no problems, add the new message to the collection
00075     if (addToList) {
00076       this->add(initMsgView);
00077     }
00078   }
00079 
00080   // indicate whether the message was added or not
00081   return addToList;
00082 }
00083 
00084 
00085 InitMsgSharedPtr
00086 InitMsgCollection::getElementForOutputModule(const std::string& requestedOMLabel) const
00087 {
00088   boost::mutex::scoped_lock sl(listLock_);
00089   InitMsgSharedPtr serializedProds;
00090 
00091   // handle the special case of an empty request
00092   // (If we want to use class methods to check the collection size and
00093   // fetch the last element in the collection, then we would need to 
00094   // switch to recursive mutexes...)
00095   if (requestedOMLabel.empty()) {
00096     if (initMsgList_.size() == 1) {
00097       serializedProds = initMsgList_.back().first;
00098     }
00099     else if (initMsgList_.size() > 1) {
00100       std::string msg = "Invalid INIT message lookup: the requested ";
00101       msg.append("HLT output module label is empty but there are multiple ");
00102       msg.append("HLT output modules to choose from.");
00103       throw cms::Exception("InitMsgCollection", "getElementForOutputModule:")
00104         << msg << std::endl;
00105     }
00106   }
00107 
00108   else {
00109     // loop over the existing messages
00110     for (InitMsgList::const_iterator msgIter = initMsgList_.begin(),
00111            msgIterEnd = initMsgList_.end();
00112          msgIter != msgIterEnd;
00113          msgIter++)
00114     {
00115       InitMsgSharedPtr workingMessage = msgIter->first;
00116       InitMsgView existingInitMsg(&(*workingMessage)[0]);
00117       std::string existingOMLabel = existingInitMsg.outputModuleLabel();
00118       
00119       // check if the output module labels match
00120       if (requestedOMLabel == existingOMLabel) {
00121         serializedProds = workingMessage;
00122         break;
00123       }
00124     }
00125   }
00126 
00127   return serializedProds;
00128 }
00129 
00130 
00131 InitMsgSharedPtr InitMsgCollection::getLastElement() const
00132 {
00133   boost::mutex::scoped_lock sl(listLock_);
00134 
00135   InitMsgSharedPtr ptrToLast;
00136   if (!initMsgList_.empty()) {
00137     ptrToLast = initMsgList_.back().first;
00138   }
00139   return ptrToLast;
00140 }
00141 
00142 
00143 InitMsgSharedPtr InitMsgCollection::getElementAt(const unsigned int index) const
00144 {
00145   boost::mutex::scoped_lock sl(listLock_);
00146 
00147   InitMsgSharedPtr ptrToElement;
00148   try
00149   {
00150     ptrToElement = initMsgList_.at(index).first;
00151   }
00152   catch (std::out_of_range& e)
00153   { }
00154 
00155   return ptrToElement;
00156 }
00157 
00158 
00159 void InitMsgCollection::clear()
00160 {
00161   boost::mutex::scoped_lock sl(listLock_);
00162   initMsgList_.clear();
00163   outModNameTable_.clear();
00164 }
00165 
00166 
00167 size_t InitMsgCollection::size() const
00168 {
00169   boost::mutex::scoped_lock sl(listLock_);
00170   return initMsgList_.size();
00171 }
00172 
00173 
00174 size_t InitMsgCollection::initMsgCount(const std::string& outputModuleLabel) const
00175 {
00176   boost::mutex::scoped_lock sl(listLock_);
00177 
00178   for (InitMsgList::const_iterator msgIter = initMsgList_.begin(),
00179          msgIterEnd = initMsgList_.end();
00180        msgIter != msgIterEnd;
00181        msgIter++)
00182   {
00183     InitMsgSharedPtr workingMessage = msgIter->first;
00184     InitMsgView existingInitMsg(&(*workingMessage)[0]);
00185     std::string existingOMLabel = existingInitMsg.outputModuleLabel();
00186       
00187     // check if the output module labels match
00188     if (outputModuleLabel == existingOMLabel) {
00189       return msgIter->second;
00190     }
00191   }
00192   return 0;
00193 }
00194 
00195 
00196 size_t InitMsgCollection::maxMsgCount() const
00197 {
00198   boost::mutex::scoped_lock sl(listLock_);
00199 
00200   size_t maxCount = 0;
00201 
00202   for (InitMsgList::const_iterator msgIter = initMsgList_.begin(),
00203          msgIterEnd = initMsgList_.end();
00204        msgIter != msgIterEnd;
00205        msgIter++)
00206   {
00207     if (msgIter->second > maxCount)
00208       maxCount = msgIter->second;
00209   }
00210   return maxCount;
00211 }
00212 
00213 
00214 std::string InitMsgCollection::getSelectionHelpString() const
00215 {
00216   boost::mutex::scoped_lock sl(listLock_);
00217 
00218   // nothing much we can say if the collection is empty
00219   if (initMsgList_.empty()) {
00220     return "No information is available about the available triggers.";
00221   }
00222 
00223   // list the full set of available triggers
00224   std::string helpString;
00225   helpString.append("The full list of trigger paths is the following:");
00226 
00227   // we can just use the list from the first entry since all
00228   // subsequent entries are forced to be the same
00229   InitMsgSharedPtr serializedProds = initMsgList_[0].first;
00230   InitMsgView existingInitMsg(&(*serializedProds)[0]);
00231   Strings existingTriggerList;
00232   existingInitMsg.hltTriggerNames(existingTriggerList);
00233   for (unsigned int idx = 0; idx < existingTriggerList.size(); idx++) {
00234     helpString.append("\n    " + existingTriggerList[idx]);
00235   }
00236 
00237   // list the output modules (INIT messages)
00238   helpString.append("\nThe registered HLT output modules and their ");
00239   helpString.append("trigger selections are the following:");
00240 
00241   // loop over the existing messages
00242     for (InitMsgList::const_iterator msgIter = initMsgList_.begin(),
00243            msgIterEnd = initMsgList_.end();
00244          msgIter != msgIterEnd;
00245          msgIter++)
00246     {
00247     serializedProds = msgIter->first;
00248     InitMsgView workingInitMsg(&(*serializedProds)[0]);
00249     helpString.append("\n  *** Output module \"");
00250     helpString.append(workingInitMsg.outputModuleLabel());
00251     helpString.append("\" ***");
00252     Strings workingSelectionList;
00253     workingInitMsg.hltTriggerSelections(workingSelectionList);
00254     for (unsigned int idx = 0; idx < workingSelectionList.size(); idx++) {
00255       helpString.append("\n    " + workingSelectionList[idx]);
00256     }
00257   }
00258 
00259   // return the result
00260   return helpString;
00261 }
00262 
00263 
00264 std::string InitMsgCollection::getOutputModuleName(const uint32_t outputModuleId) const
00265 {
00266   boost::mutex::scoped_lock sl(listLock_);
00267 
00268   OutModTable::const_iterator it = outModNameTable_.find(outputModuleId);
00269 
00270   if (it == outModNameTable_.end())
00271   {
00272     return "";
00273   }
00274   else {
00275     return it->second;
00276   }
00277 }
00278 
00279 
00280 std::string InitMsgCollection::stringsToText(Strings const& list,
00281                                              unsigned int maxCount)
00282 {
00283   std::string resultString = "";
00284   unsigned int elementCount = list.size();
00285   if (maxCount > 0 && maxCount < elementCount) {elementCount = maxCount;}
00286   for (unsigned int idx = 0; idx < elementCount; idx++)
00287   {
00288     resultString.append(list[idx]);
00289     if (idx < (elementCount-1)) {
00290       resultString.append(", ");
00291     }
00292   }
00293   if (elementCount < list.size())
00294   {
00295     resultString.append(", ...");
00296   }
00297   return resultString;
00298 }
00299 
00300 
00301 void InitMsgCollection::add(InitMsgView const& initMsgView)
00302 {
00303   // add the message to the internal list
00304   InitMsgSharedPtr serializedProds(new InitMsgBuffer(initMsgView.size()));
00305   initMsgList_.push_back( std::make_pair(serializedProds,1) );
00306   std::copy(initMsgView.startAddress(),
00307             initMsgView.startAddress()+initMsgView.size(),
00308             &(*serializedProds)[0]);
00309 
00310   // add the module ID name to the name map
00311   outModNameTable_[initMsgView.outputModuleId()] =
00312     initMsgView.outputModuleLabel();
00313 }
00314 
00315 
00316