CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/EventFilter/StorageManager/src/EventMsgData.cc

Go to the documentation of this file.
00001 // $Id: EventMsgData.cc,v 1.11 2011/03/08 18:34:11 mommsen Exp $
00003 
00004 #include "EventFilter/StorageManager/src/ChainData.h"
00005 
00006 #include "IOPool/Streamer/interface/EventMessage.h"
00007 
00008 #include <stdlib.h>
00009 
00010 namespace stor
00011 {
00012 
00013   namespace detail
00014   {
00015 
00016     EventMsgData::EventMsgData(toolbox::mem::Reference* pRef) :
00017       ChainData(I2O_SM_DATA, Header::EVENT),
00018       headerFieldsCached_(false)
00019     {
00020       addFirstFragment(pRef);
00021       parseI2OHeader();
00022     }
00023 
00024     inline size_t EventMsgData::do_i2oFrameSize() const
00025     {
00026       return sizeof(I2O_SM_DATA_MESSAGE_FRAME);
00027     }
00028 
00029     unsigned long EventMsgData::do_headerSize() const
00030     {
00031       if ( !headerOkay() )
00032       {
00033         return 0;
00034       }
00035 
00036       if (! headerFieldsCached_) {cacheHeaderFields();}
00037       return headerSize_;
00038     }
00039 
00040     unsigned char* EventMsgData::do_headerLocation() const
00041     {
00042       if ( !headerOkay() )
00043       {
00044         return 0;
00045       }
00046 
00047       if (! headerFieldsCached_) {cacheHeaderFields();}
00048       return headerLocation_;
00049     }
00050 
00051     inline unsigned char*
00052     EventMsgData::do_fragmentLocation(unsigned char* dataLoc) const
00053     {
00054       if ( parsable() )
00055       {
00056         I2O_SM_DATA_MESSAGE_FRAME *smMsg =
00057           (I2O_SM_DATA_MESSAGE_FRAME*) dataLoc;
00058         return (unsigned char*) smMsg->dataPtr();
00059       }
00060       else
00061       {
00062         return dataLoc;
00063       }
00064     }
00065 
00066     uint32_t EventMsgData::do_outputModuleId() const
00067     {
00068       if ( !headerOkay() )
00069       {
00070         std::stringstream msg;
00071         msg << "An output module ID can not be determined from a ";
00072         msg << "faulty or incomplete Event message.";
00073         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00074       }
00075 
00076       if (! headerFieldsCached_) {cacheHeaderFields();}
00077       return outputModuleId_;
00078     }
00079 
00080     uint32_t EventMsgData::do_hltTriggerCount() const
00081     {
00082       if ( !headerOkay() )
00083       {
00084         std::stringstream msg;
00085         msg << "The number of HLT trigger bits can not be determined ";
00086         msg << "from a faulty or incomplete Event message.";
00087         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00088       }
00089 
00090       if (! headerFieldsCached_) {cacheHeaderFields();}
00091       return hltTriggerCount_;
00092     }
00093 
00094     void
00095     EventMsgData::do_hltTriggerBits(std::vector<unsigned char>& bitList) const
00096     {
00097       if ( !headerOkay() )
00098       {
00099         std::stringstream msg;
00100         msg << "The HLT trigger bits can not be determined from a ";
00101         msg << "faulty or incomplete Event message.";
00102         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00103       }
00104 
00105       if (! headerFieldsCached_) {cacheHeaderFields();}
00106       bitList = hltTriggerBits_;
00107     }
00108 
00109     unsigned int
00110     EventMsgData::do_droppedEventsCount() const
00111     {
00112       if ( !headerOkay() )
00113       {
00114         std::stringstream msg;
00115         msg << "The dropped events count cannot be determined from a ";
00116         msg << "faulty or incomplete Event message.";
00117         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00118       }
00119 
00120       if (! headerFieldsCached_) {cacheHeaderFields();}
00121       return droppedEventsCount_;
00122      }
00123 
00124     void
00125     EventMsgData::do_setDroppedEventsCount(unsigned int count)
00126     {
00127       if ( headerOkay() )
00128       {
00129         const unsigned long firstFragSize = dataSize(0);
00130         
00131         // This should always be the case:
00132         assert( firstFragSize > sizeof(EventHeader) );
00133 
00134         EventHeader* header = (EventHeader*)dataLocation(0);
00135         convert(count,header->droppedEventsCount_);
00136       }
00137     }
00138 
00139     void 
00140     EventMsgData::do_assertRunNumber(uint32_t runNumber)
00141     {
00142       if ( headerOkay() && do_runNumber() != runNumber )
00143       {
00144         std::ostringstream errorMsg;
00145         errorMsg << "Run number " << do_runNumber() 
00146           << " of event " << do_eventNumber() <<
00147           " received from " << hltURL() <<
00148           " (FU process id " << fuProcessId() << ")" <<
00149           " does not match the run number " << runNumber << 
00150           " used to configure the StorageManager.";
00151         XCEPT_RAISE(stor::exception::RunNumberMismatch, errorMsg.str());
00152       }
00153     }
00154 
00155     uint32_t EventMsgData::do_runNumber() const
00156     {
00157       if ( !headerOkay() )
00158       {
00159         std::stringstream msg;
00160         msg << "A run number can not be determined from a ";
00161         msg << "faulty or incomplete Event message.";
00162         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00163       }
00164 
00165       if (! headerFieldsCached_) {cacheHeaderFields();}
00166       return runNumber_;
00167     }
00168 
00169     uint32_t EventMsgData::do_lumiSection() const
00170     {
00171       if ( !headerOkay() )
00172       {
00173         std::stringstream msg;
00174         msg << "A luminosity section can not be determined from a ";
00175         msg << "faulty or incomplete Event message.";
00176         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00177       }
00178 
00179       if (! headerFieldsCached_) {cacheHeaderFields();}
00180       return lumiSection_;
00181     }
00182 
00183     uint32_t EventMsgData::do_eventNumber() const
00184     {
00185       if ( !headerOkay() )
00186       {
00187         std::stringstream msg;
00188         msg << "An event number can not be determined from a ";
00189         msg << "faulty or incomplete Event message.";
00190         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00191       }
00192 
00193       if (! headerFieldsCached_) {cacheHeaderFields();}
00194       return eventNumber_;
00195     }
00196 
00197     uint32_t EventMsgData::do_adler32Checksum() const
00198     {
00199       if ( !headerOkay() )
00200       {
00201         std::stringstream msg;
00202         msg << "An adler32 checksum can not be determined from a ";
00203         msg << "faulty or incomplete Event message.";
00204         XCEPT_RAISE(stor::exception::IncompleteEventMessage, msg.str());
00205       }
00206 
00207       if (! headerFieldsCached_) {cacheHeaderFields();}
00208       return adler32_;
00209     }
00210 
00211     inline void EventMsgData::parseI2OHeader()
00212     {
00213       if ( parsable() )
00214       {
00215         I2O_SM_DATA_MESSAGE_FRAME *smMsg =
00216           (I2O_SM_DATA_MESSAGE_FRAME*) ref_->getDataLocation();
00217         fragKey_.code_ = messageCode_;
00218         fragKey_.run_ = smMsg->runID;
00219         fragKey_.event_ = smMsg->eventID;
00220         fragKey_.secondaryId_ = smMsg->outModID;
00221         fragKey_.originatorPid_ = smMsg->fuProcID;
00222         fragKey_.originatorGuid_ = smMsg->fuGUID;
00223         rbBufferId_ = smMsg->rbBufferID;
00224         hltLocalId_ = smMsg->hltLocalId;
00225         hltInstance_ = smMsg->hltInstance;
00226         hltTid_ = smMsg->hltTid;
00227         fuProcessId_ = smMsg->fuProcID;
00228         fuGuid_ = smMsg->fuGUID;
00229       }
00230     }
00231 
00232     void EventMsgData::cacheHeaderFields() const
00233     {
00234       unsigned char* firstFragLoc = dataLocation(0);
00235       unsigned long firstFragSize = dataSize(0);
00236       bool useFirstFrag = false;
00237 
00238       // if there is only one fragment, use it
00239       if (fragmentCount_ == 1)
00240       {
00241         useFirstFrag = true;
00242       }
00243       // otherwise, check if the first fragment is large enough to hold
00244       // the full Event message header  (we require some minimal fixed
00245       // size in the hope that we don't parse garbage when we overlay
00246       // the EventMsgView on the buffer)
00247       else if (firstFragSize > (sizeof(EventHeader) + 4096))
00248       {
00249         EventMsgView view(firstFragLoc);
00250         if (view.headerSize() <= firstFragSize)
00251         {
00252           useFirstFrag = true;
00253         }
00254       }
00255 
00256       boost::shared_ptr<EventMsgView> msgView;
00257       if (useFirstFrag)
00258       {
00259         msgView.reset(new EventMsgView(firstFragLoc));
00260       }
00261       else
00262       {
00263         copyFragmentsIntoBuffer(headerCopy_);
00264         msgView.reset(new EventMsgView(&headerCopy_[0]));
00265       }
00266 
00267       headerSize_ = msgView->headerSize();
00268       headerLocation_ = msgView->startAddress();
00269       outputModuleId_ = msgView->outModId();
00270       hltTriggerCount_ = msgView->hltCount();
00271       if (hltTriggerCount_ > 0)
00272         {
00273           hltTriggerBits_.resize(1 + (hltTriggerCount_-1)/4);
00274         }
00275       msgView->hltTriggerBits(&hltTriggerBits_[0]);
00276 
00277       runNumber_ = msgView->run();
00278       lumiSection_ = msgView->lumi();
00279       eventNumber_ = msgView->event();
00280       adler32_ = msgView->adler32_chksum();
00281       droppedEventsCount_ = msgView->droppedEventsCount();
00282 
00283       headerFieldsCached_ = true;
00284 
00285       #ifdef STOR_DEBUG_WRONG_ADLER
00286       double r = rand()/static_cast<double>(RAND_MAX);
00287       if (r < 0.01)
00288       {
00289         std::cout << "Simulating corrupt Adler calculation" << std::endl;
00290         headerSize_ += 3;
00291       }
00292       else if (r < 0.02)
00293       {
00294         std::cout << "Simulating corrupt Adler entry" << std::endl;
00295         adler32_ += r*10000;
00296       }
00297       #endif // STOR_DEBUG_WRONG_ADLER
00298     }
00299 
00300   } // namespace detail
00301 
00302 } // namespace stor
00303 
00304