CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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.19 2010/02/16 21:59:24 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 {
00054    //now do what ever initialization is needed
00055    typedef std::vector<edm::ParameterSet> ToSave;
00056    ToSave toSave = iConfig.getParameter<ToSave> ("toPut");
00057    for (ToSave::const_iterator it = toSave.begin (); it != toSave.end (); it++)
00058    {
00059       std::string record = it->getParameter<std::string> ("record");
00060       std::string type = it->getParameter<std::string> ("type");
00061       m_recordTypes.push_back( record  + "@" + type ) ;
00062    }
00063 }
00064 
00065 
00066 L1CondDBIOVWriter::~L1CondDBIOVWriter()
00067 {
00068  
00069    // do anything here that needs to be done at desctruction time
00070    // (e.g. close files, deallocate resources etc.)
00071 
00072 }
00073 
00074 
00075 //
00076 // member functions
00077 //
00078 
00079 // ------------ method called to for each event  ------------
00080 void
00081 L1CondDBIOVWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00082 {
00083    using namespace edm;
00084 
00085    // Get L1TriggerKeyList
00086    L1TriggerKeyList keyList ;
00087    l1t::DataWriter dataWriter ;
00088    if( !dataWriter.fillLastTriggerKeyList( keyList ) )
00089      {
00090        edm::LogError( "L1-O2O" )
00091          << "Problem getting last L1TriggerKeyList" ;
00092      }
00093 
00094    unsigned long long run = iEvent.id().run() ;
00095 
00096    L1TriggerKey::RecordToKey recordTypeToKeyMap ;
00097 
00098    bool triggerKeyIOVUpdated = true ;
00099 
00100    // Start log string, convert run number into string
00101    std::stringstream ss ;
00102    ss << run ;
00103    std::string log = "KEYLOG runNumber=" + ss.str() ;
00104    bool logRecords = true ;
00105 
00106    if( !m_ignoreTriggerKey )
00107      {
00108        if( !m_tscKey.empty() )
00109          {
00110            edm::LogVerbatim( "L1-O2O" )
00111              << "Object key for L1TriggerKey@L1TriggerKeyRcd: "
00112              << m_tscKey ;
00113 
00114            // Use TSC key and L1TriggerKeyList to find next run's
00115            // L1TriggerKey token
00116            std::string keyToken = keyList.token( m_tscKey ) ;
00117 
00118            // Update IOV sequence for this token with since-time = new run 
00119            triggerKeyIOVUpdated =
00120              m_writer.updateIOV( "L1TriggerKeyRcd", keyToken, run, m_logTransactions ) ;
00121 
00122            // Read current L1TriggerKey directly from ORCON using token
00123            L1TriggerKey key ;
00124            m_writer.readObject( keyToken, key ) ;
00125 
00126            recordTypeToKeyMap = key.recordToKeyMap() ;
00127 
00128            // Replace spaces in key with ?s.  Do reverse substitution when
00129            // making L1TriggerKey.
00130            std::string tmpKey = m_tscKey ;
00131            replace( tmpKey.begin(), tmpKey.end(), ' ', '?' ) ;
00132            log += " tscKey=" + tmpKey ;
00133            logRecords = false ;
00134          }
00135        else
00136          {
00137            // For use with Run Settings, no corresponding L1TrigerKey in
00138            // ORCON.
00139 
00140            // Get L1TriggerKey from EventSetup
00141            ESHandle< L1TriggerKey > esKey ;
00142            iSetup.get< L1TriggerKeyRcd >().get( esKey ) ;
00143 
00144            recordTypeToKeyMap = esKey->recordToKeyMap() ;
00145          }
00146      }
00147    else
00148      {
00149        std::vector<std::string >::const_iterator
00150          recordTypeItr = m_recordTypes.begin() ;
00151        std::vector<std::string >::const_iterator
00152          recordTypeEnd = m_recordTypes.end() ;
00153 
00154        for( ; recordTypeItr != recordTypeEnd ; ++recordTypeItr )
00155          {
00156            recordTypeToKeyMap.insert(
00157              std::make_pair( *recordTypeItr, m_tscKey ) ) ;
00158          }
00159      }
00160 
00161    // If L1TriggerKey IOV was already up to date, then so are all its
00162    // sub-records.
00163    bool throwException = false ;
00164 
00165    if( triggerKeyIOVUpdated )
00166      {
00167        // Loop over record@type in L1TriggerKey
00168        L1TriggerKey::RecordToKey::const_iterator itr =
00169          recordTypeToKeyMap.begin() ;
00170        L1TriggerKey::RecordToKey::const_iterator end =
00171          recordTypeToKeyMap.end() ;
00172 
00173        for( ; itr != end ; ++itr )
00174          {
00175            std::string recordType = itr->first ;
00176            std::string objectKey = itr->second ;
00177 
00178            std::string recordName( recordType,
00179                                    0, recordType.find_first_of("@") ) ;
00180 
00181            if( logRecords )
00182              {
00183                // Replace spaces in key with ?s.  Do reverse substitution when
00184                // making L1TriggerKey.
00185                std::string tmpKey = objectKey ;
00186                replace( tmpKey.begin(), tmpKey.end(), ' ', '?' ) ;
00187                log += " " + recordName + "Key=" + tmpKey ;
00188              }
00189 
00190            // Do nothing if object key is null.
00191            if( objectKey == L1TriggerKey::kNullKey )
00192              {
00193                edm::LogVerbatim( "L1-O2O" )
00194                  << "L1CondDBIOVWriter: null object key for "
00195                  << recordType << "; skipping this record." ;
00196              }
00197            else
00198              {
00199                // Find payload token
00200                edm::LogVerbatim( "L1-O2O" )
00201                  << "Object key for "
00202                  << recordType << ": " << objectKey ;
00203 
00204                std::string payloadToken = keyList.token( recordType,
00205                                                          objectKey ) ;
00206                if( payloadToken.empty() )
00207                  {
00208                    edm::LogVerbatim( "L1-O2O" )
00209                      << "L1CondDBIOVWriter: empty payload token for " +
00210                      recordType + ", key " + objectKey ;
00211 
00212                    throwException = true ;
00213                  }
00214                else
00215                  {
00216                    m_writer.updateIOV( recordName,
00217                                        payloadToken,
00218                                        run,
00219                                        m_logTransactions ) ;
00220                  }
00221              }
00222          }
00223      }
00224 
00225    if( m_logKeys )
00226      {
00227        edm::LogVerbatim( "L1-O2O" ) << log ;
00228      }
00229 
00230    if( throwException )
00231      {
00232        throw cond::Exception( "L1CondDBIOVWriter: empty payload tokens" ) ;
00233      }
00234 }
00235 
00236 
00237 // ------------ method called once each job just before starting event loop  ------------
00238 void 
00239 L1CondDBIOVWriter::beginJob()
00240 {
00241 }
00242 
00243 // ------------ method called once each job just after ending the event loop  ------------
00244 void 
00245 L1CondDBIOVWriter::endJob() {
00246 }
00247 
00248 //define this as a plug-in
00249 //DEFINE_FWK_MODULE(L1CondDBIOVWriter);