CMS 3D CMS Logo

PrescaleWeightProvider.cc
Go to the documentation of this file.
1 //
2 // See header file for description
3 //
4 
5 
7 
8 #include <sstream>
9 
14 
18 
20 // default values
21 : init_(false)
22 , verbosity_( 0 )
23 , triggerResultsTag_( "TriggerResults::HLT" )
24 , triggerResultsToken_( iC.mayConsume< edm::TriggerResults >( triggerResultsTag_ ) )
25 , l1GtTriggerMenuLiteTag_( "l1GtTriggerMenuLite" )
26 , l1GtTriggerMenuLiteToken_( iC.mayConsume< L1GtTriggerMenuLite, edm::InRun >( l1GtTriggerMenuLiteTag_ ) )
27 {
28 
29  hltPaths_.clear();
30  if ( config.exists( "prescaleWeightVerbosityLevel" ) ) verbosity_ = config.getParameter< unsigned >( "prescaleWeightVerbosityLevel" );
31  if ( config.exists( "prescaleWeightTriggerResults" ) ) triggerResultsTag_ = config.getParameter< edm::InputTag >( "prescaleWeightTriggerResults" );
32  if ( config.exists( "prescaleWeightL1GtTriggerMenuLite" ) ) l1GtTriggerMenuLiteTag_ = config.getParameter< edm::InputTag >( "prescaleWeightL1GtTriggerMenuLite" );
33  if ( config.exists( "prescaleWeightHltPaths" ) ) hltPaths_ = config.getParameter< std::vector< std::string > >( "prescaleWeightHltPaths" );
34 
35  configured_ = true;
36  if ( triggerResultsTag_.process().empty() ) {
37  configured_ = false;
38  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "Process name not configured via TriggerResults InputTag";
39  } else if ( triggerResultsTag_.label().empty() ) {
40  configured_ = false;
41  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "TriggerResults label not configured";
42  } else if ( l1GtTriggerMenuLiteTag_.label().empty() ) {
43  configured_ = false;
44  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "L1GtTriggerMenuLite label not configured";
45  } else if ( hltPaths_.empty() ) {
46  configured_ = false;
47  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT paths of interest not configured";
48  }
49  if ( configured_ ) {
52  }
53 
54 }
55 
57 {
58 
59  init_ = true;
60 
61  if ( ! configured_ ) {
62  init_ = false;
63  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider" ) << "Run initialisation failed due to failing configuration";
64  return;
65  }
66 
67  HLTConfigProvider const& hltConfig = hltPrescaleProvider_->hltConfigProvider();
68  bool hltChanged( false );
69  if ( ! hltPrescaleProvider_->init( run, setup, triggerResultsTag_.process(), hltChanged ) ) {
70  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT config initialization error with process name \"" << triggerResultsTag_.process() << "\"";
71  init_ = false;
72  } else if ( hltConfig.size() <= 0 ) {
73  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "HLT config size error";
74  init_ = false;
75  } else if ( hltChanged ) {
76  if ( verbosity_ > 0 ) edm::LogInfo( "PrescaleWeightProvider" ) << "HLT configuration changed";
77  }
78  if ( ! init_ ) return;
79 
81  if ( ! triggerMenuLite_.isValid() ) {
82  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider" ) << "L1GtTriggerMenuLite with label \"" << l1GtTriggerMenuLiteTag_.label() << "\" not found";
83  init_ = false;
84  }
85 
86 }
87 
88 
90 {
91  if ( ! init_ ) return 1;
92 
93  // L1
94  L1GtUtils const& l1GtUtils = hltPrescaleProvider_->l1GtUtils();
95 
96  // HLT
97  HLTConfigProvider const& hltConfig = hltPrescaleProvider_->hltConfigProvider();
98 
100  event.getByToken( triggerResultsToken_, triggerResults);
101  if( ! triggerResults.isValid() ) {
102  if ( verbosity_ > 0 ) edm::LogError("PrescaleWeightProvider::prescaleWeight") << "TriggerResults product not found for InputTag \"" << triggerResultsTag_.encode() << "\"";
103  return 1;
104  }
105 
106  const int SENTINEL( -1 );
107  int weight( SENTINEL );
108 
109  for ( unsigned ui = 0; ui < hltPaths_.size(); ui++ ) {
110  const std::string hltPath( hltPaths_.at( ui ) );
111  unsigned hltIndex( hltConfig.triggerIndex( hltPath ) );
112  if ( hltIndex == hltConfig.size() ) {
113  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "HLT path \"" << hltPath << "\" does not exist";
114  continue;
115  }
116  if ( ! triggerResults->accept( hltIndex ) ) continue;
117 
118  const std::vector< std::pair < bool, std::string > >& level1Seeds = hltConfig.hltL1GTSeeds( hltPath );
119  if ( level1Seeds.size() != 1 ) {
120  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "HLT path \"" << hltPath << "\" provides too many L1 seeds";
121  return 1;
122  }
123  parseL1Seeds( level1Seeds.at( 0 ).second );
124  if ( l1SeedPaths_.empty() ){
125  if ( verbosity_ > 0 ) edm::LogWarning("PrescaleWeightProvider::prescaleWeight") << "Failed to parse L1 seeds for HLT path \"" << hltPath << "\"";
126  continue;
127  }
128 
129  int l1Prescale( SENTINEL );
130  for ( unsigned uj = 0; uj < l1SeedPaths_.size(); uj++ ) {
131  int l1TempPrescale( SENTINEL );
132  int errorCode( 0 );
133  if ( level1Seeds.at( 0 ).first ) { // technical triggers
134  unsigned techBit( atoi( l1SeedPaths_.at( uj ).c_str() ) );
135  const std::string techName( *( triggerMenuLite_->gtTechTrigName( techBit, errorCode ) ) );
136  if ( errorCode != 0 ) continue;
137  if ( ! l1GtUtils.decision( event, techName, errorCode ) ) continue;
138  if ( errorCode != 0 ) continue;
139  l1TempPrescale = l1GtUtils.prescaleFactor( event, techName, errorCode );
140  if ( errorCode != 0 ) continue;
141  }
142  else { // algorithmic triggers
143  if ( ! l1GtUtils.decision( event, l1SeedPaths_.at( uj ), errorCode ) ) continue;
144  if ( errorCode != 0 ) continue;
145  l1TempPrescale = l1GtUtils.prescaleFactor( event, l1SeedPaths_.at( uj ), errorCode );
146  if ( errorCode != 0 ) continue;
147  }
148  if ( l1TempPrescale > 0 ){
149  if ( l1Prescale == SENTINEL || l1Prescale > l1TempPrescale ) l1Prescale = l1TempPrescale;
150  }
151  }
152  if ( l1Prescale == SENTINEL ){
153  if ( verbosity_ > 0 ) edm::LogError( "PrescaleWeightProvider::prescaleWeight" ) << "Unable to find the L1 prescale for HLT path \"" << hltPath << "\"";
154  continue;
155  }
156 
157  int hltPrescale( hltPrescaleProvider_->prescaleValue( event, setup, hltPath ) );
158 
159  if ( hltPrescale * l1Prescale > 0 ) {
160  if ( weight == SENTINEL || weight > hltPrescale * l1Prescale ) {
161  weight = hltPrescale * l1Prescale;
162  }
163  }
164  }
165 
166  if ( weight == SENTINEL ){
167  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider::prescaleWeight" ) << "No valid weight for any requested HLT path, returning default weight of 1";
168  return 1;
169  }
170  return weight;
171 
172 }
173 
174 
176 {
177  l1SeedPaths_.clear();
178  std::stringstream ss( l1Seeds );
179  std::string buf;
180 
181  while ( ss.good() && ! ss.eof() ){
182  ss >> buf;
183  if ( buf[0] == '(' || buf[ buf.size() - 1 ] == ')' || buf == "AND" || buf == "NOT" ){
184  l1SeedPaths_.clear();
185  if ( verbosity_ > 0 ) edm::LogWarning( "PrescaleWeightProvider::parseL1Seeds" ) << "Only supported logical expression is OR";
186  return;
187  }
188  else if (buf == "OR") continue;
189  else l1SeedPaths_.push_back( buf );
190  }
191 
192 }
unsigned int size() const
number of trigger paths in trigger table
edm::EDGetTokenT< L1GtTriggerMenuLite > l1GtTriggerMenuLiteToken_
T getParameter(std::string const &) const
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
PrescaleWeightProvider(const edm::ParameterSet &config, edm::ConsumesCollector &&iC, T &module)
int prescaleWeight(const edm::Event &event, const edm::EventSetup &setup)
edm::Handle< L1GtTriggerMenuLite > triggerMenuLite_
std::vector< std::string > l1SeedPaths_
const std::vector< std::vector< std::pair< bool, std::string > > > & hltL1GTSeeds() const
bool accept() const
Has at least one path accepted the event?
bool exists(std::string const &parameterName) const
checks if a parameter exists
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
const bool decision(const edm::Event &iEvent, const std::string &nameAlgoTechTrig, int &errorCode) const
Definition: L1GtUtils.cc:1208
Definition: config.py:1
std::string encode() const
Definition: InputTag.cc:166
unsigned int triggerIndex(const std::string &triggerName) const
slot position of trigger path in trigger table (0 to size-1)
std::vector< std::string > hltPaths_
const std::string * gtTechTrigName(const unsigned int bitNumber, int &errorCode) const
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:342
std::unique_ptr< HLTPrescaleProvider > hltPrescaleProvider_
void initRun(const edm::Run &run, const edm::EventSetup &setup)
static std::string const triggerResults
Definition: EdmProvDump.cc:41
bool isValid() const
Definition: HandleBase.h:74
std::string const & label() const
Definition: InputTag.h:36
std::string const & process() const
Definition: InputTag.h:40
HLT enums.
void parseL1Seeds(const std::string &l1Seeds)
Definition: event.py:1
Definition: Run.h:44
const int prescaleFactor(const edm::Event &iEvent, const std::string &nameAlgoTechTrig, int &errorCode) const
return prescale factor for a given algorithm or technical trigger
Definition: L1GtUtils.cc:1223