CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CommonTools/TriggerUtils/src/PrescaleWeightProvider.cc

Go to the documentation of this file.
00001 //
00002 // See header file for description
00003 // $Id: PrescaleWeightProvider.cc,v 1.2 2010/08/25 16:20:29 vadler Exp $
00004 //
00005 
00006 
00007 #include "CommonTools/TriggerUtils/interface/PrescaleWeightProvider.h"
00008 
00009 #include <sstream>
00010 
00011 #include "L1Trigger/GlobalTriggerAnalyzer/interface/L1GtUtils.h"
00012 #include "DataFormats/Common/interface/TriggerResults.h"
00013 
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 
00017 PrescaleWeightProvider::PrescaleWeightProvider( const edm::ParameterSet & config )
00018 // default values
00019 : verbosity_( 0 )
00020 , triggerResults_( "TriggerResults::HLT" )
00021 , l1GtTriggerMenuLite_( "l1GtTriggerMenuLite" )
00022 {
00023 
00024   hltPaths_.clear();
00025   if ( config.exists( "prescaleWeightVerbosityLevel" ) )      verbosity_           = config.getParameter< unsigned >( "prescaleWeightVerbosityLevel" );
00026   if ( config.exists( "prescaleWeightTriggerResults" ) )      triggerResults_      = config.getParameter< edm::InputTag >( "prescaleWeightTriggerResults" );
00027   if ( config.exists( "prescaleWeightL1GtTriggerMenuLite" ) ) l1GtTriggerMenuLite_ = config.getParameter< edm::InputTag >( "prescaleWeightL1GtTriggerMenuLite" );
00028   if ( config.exists( "prescaleWeightHltPaths" ) )            hltPaths_            = config.getParameter< std::vector< std::string > >( "prescaleWeightHltPaths" );
00029 
00030   configured_ = true;
00031   if ( triggerResults_.process().empty() ) {
00032     configured_ = false;
00033     if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "Process name not configured via TriggerResults InputTag";
00034   } else if ( triggerResults_.label().empty() ) {
00035     configured_ = false;
00036     if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "TriggerResults label not configured";
00037   } else if ( l1GtTriggerMenuLite_.label().empty() ) {
00038     configured_ = false;
00039     if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "L1GtTriggerMenuLite label not configured";
00040   } else if ( hltPaths_.empty() ) {
00041     configured_ = false;
00042     if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT paths of interest not configured";
00043   }
00044 
00045 }
00046 
00047 void PrescaleWeightProvider::initRun( const edm::Run & run, const edm::EventSetup & setup )
00048 {
00049 
00050   init_ = true;
00051 
00052   if ( ! configured_ ) {
00053     init_ = false;
00054     if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "Run initialisation failed due to failing configuration";
00055     return;
00056   }
00057 
00058   bool hltChanged( false );
00059   if ( ! hltConfig_.init( run, setup, triggerResults_.process(), hltChanged ) ) {
00060     if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT config initialization error with process name \"" << triggerResults_.process() << "\"";
00061     init_ = false;
00062   } else if ( hltConfig_.size() <= 0 ) {
00063     if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT config size error";
00064     init_ = false;
00065   } else if ( hltChanged ) {
00066     if ( verbosity_ > 0 ) edm::LogInfo( "PrescaleWeightProvider" ) << "HLT configuration changed";
00067   }
00068   if ( ! init_ ) return;
00069 
00070   run.getByLabel( l1GtTriggerMenuLite_.label(), triggerMenuLite_ );
00071   if ( ! triggerMenuLite_.isValid() ) {
00072     if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "L1GtTriggerMenuLite with label \"" << l1GtTriggerMenuLite_.label() << "\" not found";
00073     init_ = false;
00074   }
00075 
00076 }
00077 
00078 
00079 int PrescaleWeightProvider::prescaleWeight( const edm::Event & event, const edm::EventSetup & setup )
00080 {
00081   if ( ! init_ ) return 1;
00082 
00083   // L1
00084   L1GtUtils l1GtUtils;
00085   l1GtUtils.retrieveL1EventSetup( setup );
00086 
00087   // HLT
00088   edm::Handle< edm::TriggerResults > triggerResults;
00089   event.getByLabel( triggerResults_, triggerResults);
00090   if( ! triggerResults.isValid() ) {
00091     if ( verbosity_ > 0 ) edm::LogError("PrescaleWeightProvider::prescaleWeight") << "TriggerResults product not found for InputTag \"" << triggerResults_.encode() << "\"";
00092     return 1;
00093   }
00094 
00095   const int SENTINEL( -1 );
00096   int weight( SENTINEL );
00097 
00098   for ( unsigned ui = 0; ui < hltPaths_.size(); ui++ ) {
00099     const std::string hltPath( hltPaths_.at( ui ) );
00100     unsigned hltIndex( hltConfig_.triggerIndex( hltPath ) );
00101     if ( hltIndex == hltConfig_.size() ) {
00102       if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "HLT path \"" << hltPath << "\" does not exist";
00103       continue;
00104     }
00105     if ( ! triggerResults->accept( hltIndex ) ) continue;
00106 
00107     const std::vector< std::pair < bool, std::string > > level1Seeds = hltConfig_.hltL1GTSeeds( hltPath );
00108     if ( level1Seeds.size() != 1 ) {
00109       if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "HLT path \"" << hltPath << "\" provides too many L1 seeds";
00110       return 1;
00111     }
00112     parseL1Seeds( level1Seeds.at( 0 ).second );
00113     if ( l1SeedPaths_.empty() ){
00114       if ( verbosity_ > 0 ) edm::LogWarning("PrescaleWeightProvider::prescaleWeight") << "Failed to parse L1 seeds for HLT path \"" << hltPath << "\"";
00115       continue;
00116     }
00117 
00118     int l1Prescale( SENTINEL );
00119     for ( unsigned uj = 0; uj < l1SeedPaths_.size(); uj++ ) {
00120       int l1TempPrescale( SENTINEL );
00121       int errorCode( 0 );
00122       if ( level1Seeds.at( 0 ).first ) { // technical triggers
00123         unsigned          techBit( atoi( l1SeedPaths_.at( uj ).c_str() ) );
00124         const std::string techName( *( triggerMenuLite_->gtTechTrigName( techBit, errorCode ) ) );
00125         if ( errorCode != 0 ) continue;
00126         if ( ! l1GtUtils.decision( event, techName, errorCode ) ) continue;
00127         if ( errorCode != 0 ) continue;
00128         l1TempPrescale = l1GtUtils.prescaleFactor( event, techName, errorCode );
00129         if ( errorCode != 0 ) continue;
00130       }
00131       else { // algorithmic triggers
00132         if ( ! l1GtUtils.decision( event, l1SeedPaths_.at( uj ), errorCode ) ) continue;
00133         if ( errorCode != 0 ) continue;
00134         l1TempPrescale = l1GtUtils.prescaleFactor( event, l1SeedPaths_.at( uj ), errorCode );
00135         if ( errorCode != 0 ) continue;
00136       }
00137       if ( l1TempPrescale > 0 ){
00138         if ( l1Prescale == SENTINEL || l1Prescale > l1TempPrescale ) l1Prescale = l1TempPrescale;
00139       }
00140     }
00141     if ( l1Prescale == SENTINEL ){
00142       if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "Unable to find the L1 prescale for HLT path \"" << hltPath << "\"";
00143       continue;
00144     }
00145 
00146     int hltPrescale( hltConfig_.prescaleValue( event, setup, hltPath ) );
00147 
00148     if ( hltPrescale * l1Prescale > 0 ) {
00149       if ( weight == SENTINEL || weight > hltPrescale * l1Prescale ) {
00150         weight = hltPrescale * l1Prescale;
00151       }
00152     }
00153   }
00154 
00155   if ( weight == SENTINEL ){
00156     if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider::prescaleWeight" ) << "No valid weight for any requested HLT path, returning default weight of 1";
00157     return 1;
00158   }
00159   return weight;
00160 
00161 }
00162 
00163 
00164 void PrescaleWeightProvider::parseL1Seeds( const std::string & l1Seeds )
00165 {
00166   l1SeedPaths_.clear();
00167   std::stringstream ss( l1Seeds );
00168   std::string       buf;
00169 
00170   while ( ss.good() && ! ss.eof() ){
00171     ss >> buf;
00172     if ( buf[0] == '('  || buf[ buf.size() - 1 ] == ')' || buf == "AND" || buf == "NOT" ){
00173       l1SeedPaths_.clear();
00174       if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider::parseL1Seeds" ) << "Only supported logical expression is OR";
00175       return;
00176     }
00177     else if (buf == "OR") continue;
00178     else                  l1SeedPaths_.push_back( buf );
00179   }
00180 
00181 }