CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondTools/L1Trigger/plugins/L1CondDBIOVWriter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    L1CondDBIOVWriter
00004 // Class:      L1CondDBIOVWriter
00005 // 
00013 //
00014 // Original Author:  Werner Man-Li Sun
00015 //         Created:  Sun Mar  2 20:09:46 CET 2008
00016 // $Id: L1CondDBIOVWriter.cc,v 1.20 2011/05/10 19:16:56 wsun Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <sstream>
00023 
00024 // user include files
00025 #include "CondTools/L1Trigger/plugins/L1CondDBIOVWriter.h"
00026 #include "CondTools/L1Trigger/interface/DataWriter.h"
00027 
00028 #include "CondFormats/L1TObjects/interface/L1TriggerKey.h"
00029 #include "CondFormats/DataRecord/interface/L1TriggerKeyRcd.h"
00030 #include "CondFormats/L1TObjects/interface/L1TriggerKeyList.h"
00031 #include "CondFormats/DataRecord/interface/L1TriggerKeyListRcd.h"
00032 
00033 //
00034 // class declaration
00035 //
00036 
00037 //
00038 // constants, enums and typedefs
00039 //
00040 
00041 //
00042 // static data member definitions
00043 //
00044 
00045 //
00046 // constructors and destructor
00047 //
00048 L1CondDBIOVWriter::L1CondDBIOVWriter(const edm::ParameterSet& iConfig)
00049    : m_tscKey( iConfig.getParameter<std::string> ("tscKey") ),
00050      m_ignoreTriggerKey( iConfig.getParameter<bool> ("ignoreTriggerKey") ),
00051      m_logKeys( iConfig.getParameter<bool>( "logKeys" ) ),
00052      m_logTransactions( iConfig.getParameter<bool>( "logTransactions" ) ),
00053      m_forceUpdate( iConfig.getParameter<bool>( "forceUpdate" ) )
00054 {
00055    //now do what ever initialization is needed
00056    typedef std::vector<edm::ParameterSet> ToSave;
00057    ToSave toSave = iConfig.getParameter<ToSave> ("toPut");
00058    for (ToSave::const_iterator it = toSave.begin (); it != toSave.end (); it++)
00059    {
00060       std::string record = it->getParameter<std::string> ("record");
00061       std::string type = it->getParameter<std::string> ("type");
00062       m_recordTypes.push_back( record  + "@" + type ) ;
00063    }
00064 }
00065 
00066 
00067 L1CondDBIOVWriter::~L1CondDBIOVWriter()
00068 {
00069  
00070    // do anything here that needs to be done at desctruction time
00071    // (e.g. close files, deallocate resources etc.)
00072 
00073 }
00074 
00075 
00076 //
00077 // member functions
00078 //
00079 
00080 // ------------ method called to for each event  ------------
00081 void
00082 L1CondDBIOVWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00083 {
00084    using namespace edm;
00085 
00086    // Get L1TriggerKeyList
00087    L1TriggerKeyList keyList ;
00088    l1t::DataWriter dataWriter ;
00089    if( !dataWriter.fillLastTriggerKeyList( keyList ) )
00090      {
00091        edm::LogError( "L1-O2O" )
00092          << "Problem getting last L1TriggerKeyList" ;
00093      }
00094 
00095    unsigned long long run = iEvent.id().run() ;
00096 
00097    L1TriggerKey::RecordToKey recordTypeToKeyMap ;
00098 
00099    bool triggerKeyIOVUpdated = true ;
00100 
00101    // Start log string, convert run number into string
00102    std::stringstream ss ;
00103    ss << run ;
00104    std::string log = "KEYLOG runNumber=" + ss.str() ;
00105    bool logRecords = true ;
00106 
00107    if( !m_ignoreTriggerKey )
00108      {
00109        if( !m_tscKey.empty() )
00110          {
00111            edm::LogVerbatim( "L1-O2O" )
00112              << "Object key for L1TriggerKey@L1TriggerKeyRcd: "
00113              << m_tscKey ;
00114 
00115            // Use TSC key and L1TriggerKeyList to find next run's
00116            // L1TriggerKey token
00117            std::string keyToken = keyList.token( m_tscKey ) ;
00118 
00119            // Update IOV sequence for this token with since-time = new run 
00120            triggerKeyIOVUpdated =
00121              m_writer.updateIOV( "L1TriggerKeyRcd", keyToken, run, m_logTransactions ) ;
00122 
00123            // Read current L1TriggerKey directly from ORCON using token
00124            L1TriggerKey key ;
00125            m_writer.readObject( keyToken, key ) ;
00126 
00127            recordTypeToKeyMap = key.recordToKeyMap() ;
00128 
00129            // Replace spaces in key with ?s.  Do reverse substitution when
00130            // making L1TriggerKey.
00131            std::string tmpKey = m_tscKey ;
00132            replace( tmpKey.begin(), tmpKey.end(), ' ', '?' ) ;
00133            log += " tscKey=" + tmpKey ;
00134            logRecords = false ;
00135          }
00136        else
00137          {
00138            // For use with Run Settings, no corresponding L1TrigerKey in
00139            // ORCON.
00140 
00141            // Get L1TriggerKey from EventSetup
00142            ESHandle< L1TriggerKey > esKey ;
00143            iSetup.get< L1TriggerKeyRcd >().get( esKey ) ;
00144 
00145            recordTypeToKeyMap = esKey->recordToKeyMap() ;
00146          }
00147      }
00148    else
00149      {
00150        std::vector<std::string >::const_iterator
00151          recordTypeItr = m_recordTypes.begin() ;
00152        std::vector<std::string >::const_iterator
00153          recordTypeEnd = m_recordTypes.end() ;
00154 
00155        for( ; recordTypeItr != recordTypeEnd ; ++recordTypeItr )
00156          {
00157            recordTypeToKeyMap.insert(
00158              std::make_pair( *recordTypeItr, m_tscKey ) ) ;
00159          }
00160      }
00161 
00162    // If L1TriggerKey IOV was already up to date, then so are all its
00163    // sub-records.
00164    bool throwException = false ;
00165 
00166    if( triggerKeyIOVUpdated || m_forceUpdate )
00167      {
00168        // Loop over record@type in L1TriggerKey
00169        L1TriggerKey::RecordToKey::const_iterator itr =
00170          recordTypeToKeyMap.begin() ;
00171        L1TriggerKey::RecordToKey::const_iterator end =
00172          recordTypeToKeyMap.end() ;
00173 
00174        for( ; itr != end ; ++itr )
00175          {
00176            std::string recordType = itr->first ;
00177            std::string objectKey = itr->second ;
00178 
00179            std::string recordName( recordType,
00180                                    0, recordType.find_first_of("@") ) ;
00181 
00182            if( logRecords )
00183              {
00184                // Replace spaces in key with ?s.  Do reverse substitution when
00185                // making L1TriggerKey.
00186                std::string tmpKey = objectKey ;
00187                replace( tmpKey.begin(), tmpKey.end(), ' ', '?' ) ;
00188                log += " " + recordName + "Key=" + tmpKey ;
00189              }
00190 
00191            // Do nothing if object key is null.
00192            if( objectKey == L1TriggerKey::kNullKey )
00193              {
00194                edm::LogVerbatim( "L1-O2O" )
00195                  << "L1CondDBIOVWriter: null object key for "
00196                  << recordType << "; skipping this record." ;
00197              }
00198            else
00199              {
00200                // Find payload token
00201                edm::LogVerbatim( "L1-O2O" )
00202                  << "Object key for "
00203                  << recordType << ": " << objectKey ;
00204 
00205                std::string payloadToken = keyList.token( recordType,
00206                                                          objectKey ) ;
00207                if( payloadToken.empty() )
00208                  {
00209                    edm::LogVerbatim( "L1-O2O" )
00210                      << "L1CondDBIOVWriter: empty payload token for " +
00211                      recordType + ", key " + objectKey ;
00212 
00213                    throwException = true ;
00214                  }
00215                else
00216                  {
00217                    m_writer.updateIOV( recordName,
00218                                        payloadToken,
00219                                        run,
00220                                        m_logTransactions ) ;
00221                  }
00222              }
00223          }
00224      }
00225 
00226    if( m_logKeys )
00227      {
00228        edm::LogVerbatim( "L1-O2O" ) << log ;
00229      }
00230 
00231    if( throwException )
00232      {
00233        throw cond::Exception( "L1CondDBIOVWriter: empty payload tokens" ) ;
00234      }
00235 }
00236 
00237 
00238 // ------------ method called once each job just before starting event loop  ------------
00239 void 
00240 L1CondDBIOVWriter::beginJob()
00241 {
00242 }
00243 
00244 // ------------ method called once each job just after ending the event loop  ------------
00245 void 
00246 L1CondDBIOVWriter::endJob() {
00247 }
00248 
00249 //define this as a plug-in
00250 //DEFINE_FWK_MODULE(L1CondDBIOVWriter);