CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TriggerObjectStandAlone.cc
Go to the documentation of this file.
1 //
2 //
3 
6 
7 #include <boost/algorithm/string.hpp>
9 
10 
11 using namespace pat;
12 
13 
14 // Const data members' definitions
15 
16 
18 
19 
20 // Constructors and Destructor
21 
22 
23 // Default constructor
26 {
27  filterLabels_.clear();
28  pathNames_.clear();
30  pathL3FilterAccepted_.clear();
31 }
32 
33 
34 // Constructor from pat::TriggerObject
36  TriggerObject( trigObj )
37 {
38  filterLabels_.clear();
39  pathNames_.clear();
41  pathL3FilterAccepted_.clear();
42 }
43 
44 
45 // Constructor from trigger::TriggerObject
47  TriggerObject( trigObj )
48 {
49  filterLabels_.clear();
50  pathNames_.clear();
52  pathL3FilterAccepted_.clear();
53 }
54 
55 
56 // Constructor from reco::Candidate
58  TriggerObject( leafCand )
59 {
60  filterLabels_.clear();
61  pathNames_.clear();
63  pathL3FilterAccepted_.clear();
64 }
65 
66 
67 // Constructors from Lorentz-vectors and (optional) PDG ID
69  TriggerObject( vec, id )
70 {
71  filterLabels_.clear();
72  pathNames_.clear();
74  pathL3FilterAccepted_.clear();
75 }
77  TriggerObject( vec, id )
78 {
79  filterLabels_.clear();
80  pathNames_.clear();
82  pathL3FilterAccepted_.clear();
83 }
84 
85 
86 // Private methods
87 
88 
89 // Checks a string vector for occurence of a certain string, incl. wild-card mechanism
90 bool TriggerObjectStandAlone::hasAnyName( const std::string & name, const std::vector< std::string > & nameVec ) const
91 {
92  // Special cases first
93  // Always false for empty vector to check
94  if ( nameVec.empty() ) return false;
95  // Always true for general wild-card(s)
96  if ( name.find_first_not_of( wildcard_ ) == std::string::npos ) return true;
97  // Split name to evaluate in parts, seperated by wild-cards
98  std::vector< std::string > namePartsVec;
99  boost::split( namePartsVec, name, boost::is_any_of( std::string( 1, wildcard_ ) ), boost::token_compress_on );
100  // Iterate over vector of names to search
101  for ( std::vector< std::string >::const_iterator iVec = nameVec.begin(); iVec != nameVec.end(); ++iVec ) {
102  // Not failed yet
103  bool failed( false );
104  // Start searching at the first character
105  size_type index( 0 );
106  // Iterate over evaluation name parts
107  for ( std::vector< std::string >::const_iterator iName = namePartsVec.begin(); iName != namePartsVec.end(); ++iName ) {
108  // Empty parts due to
109  // - wild-card at beginning/end or
110  // - multiple wild-cards (should be supressed by 'boost::token_compress_on')
111  if ( iName->length() == 0 ) continue;
112  // Search from current index and
113  // set index to found occurence
114  index = iVec->find( *iName, index );
115  // Failed and exit loop, if
116  // - part not found
117  // - part at beginning not found there
118  if ( index == std::string::npos || ( iName == namePartsVec.begin() && index > 0 ) ) {
119  failed = true;
120  break;
121  }
122  // Increase index by length of found part
123  index += iName->length();
124  }
125  // Failed, if end of name not reached
126  if ( index < iVec->length() && namePartsVec.back().length() != 0 ) failed = true;
127  // Match found!
128  if ( ! failed ) return true;
129  }
130  // No match found!
131  return false;
132 }
133 
134 
135 // Adds a new HLT path or L1 algorithm name
136 void TriggerObjectStandAlone::addPathOrAlgorithm( const std::string & name, bool pathLastFilterAccepted, bool pathL3FilterAccepted )
137 {
139 
140  // Check, if path is already assigned
141  if ( ! hasPathOrAlgorithm( name, false, false ) ) {
142  // The path itself
143  pathNames_.push_back( name );
144  // The corresponding usage of the trigger objects
145  pathLastFilterAccepted_.push_back( pathLastFilterAccepted );
146  pathL3FilterAccepted_.push_back( pathL3FilterAccepted );
147  // Enable status updates
148  } else if ( pathLastFilterAccepted || pathL3FilterAccepted ) {
149  // Search for path
150  unsigned index( 0 );
151  while ( index < pathNames_.size() ) {
152  if ( pathNames_.at( index ) == name ) break;
153  ++index;
154  }
155  // Status update
156  if ( index < pathNames_.size() ) {
157  pathLastFilterAccepted_.at( index ) = pathLastFilterAccepted_.at( index ) || pathLastFilterAccepted;
158  pathL3FilterAccepted_.at( index ) = pathL3FilterAccepted_.at( index ) || pathL3FilterAccepted;
159  }
160  }
161 }
162 
163 
164 // Gets all HLT path or L1 algorithm names
165 std::vector< std::string > TriggerObjectStandAlone::pathsOrAlgorithms( bool pathLastFilterAccepted, bool pathL3FilterAccepted ) const
166 {
168 
169  // Deal with older PAT-tuples, where trigger object usage is not available
170  if ( ! hasLastFilter() ) pathLastFilterAccepted = false;
171  if ( ! hasL3Filter() ) pathL3FilterAccepted = false;
172  // All path names, if usage not restricted (not required or not available)
173  if ( ! pathLastFilterAccepted && ! pathL3FilterAccepted ) return pathNames_;
174  // Temp vector of path names
175  std::vector< std::string > paths;
176  // Loop over usage vector and fill corresponding paths into temp vector
177  for ( unsigned iPath = 0; iPath < pathNames_.size(); ++iPath ) {
178  if ( ( ! pathLastFilterAccepted || pathLastFilterAccepted_.at( iPath ) ) && ( ! pathL3FilterAccepted || pathL3FilterAccepted_.at( iPath ) ) ) paths.push_back( pathNames_.at( iPath ) ); // order matters in order to protect from empty vectors in old data
179  }
180  // Return temp vector
181  return paths;
182 }
183 
184 
185 // Checks, if a certain HLT filter label or L1 condition name is assigned
187 {
188  // Move to wild-card parser, if needed
189  if ( name.find( wildcard_ ) != std::string::npos ) return hasAnyName( name, filterLabels_ );
190  // Return, if filter label is assigned
191  return ( std::find( filterLabels_.begin(), filterLabels_.end(), name ) != filterLabels_.end() );
192 }
193 
194 
195 // Checks, if a certain path name is assigned
196 bool TriggerObjectStandAlone::hasPathOrAlgorithm( const std::string & name, bool pathLastFilterAccepted, bool pathL3FilterAccepted ) const
197 {
199 
200  // Move to wild-card parser, if needed
201  if ( name.find( wildcard_ ) != std::string::npos ) return hasAnyName( name, pathsOrAlgorithms( pathLastFilterAccepted, pathL3FilterAccepted ) );
202  // Deal with older PAT-tuples, where trigger object usage is not available
203  if ( ! hasLastFilter() ) pathLastFilterAccepted = false;
204  if ( ! hasL3Filter() ) pathL3FilterAccepted = false;
205  // Check, if path name is assigned at all
206  std::vector< std::string >::const_iterator match( std::find( pathNames_.begin(), pathNames_.end(), name ) );
207  // False, if path name not assigned
208  if ( match == pathNames_.end() ) return false;
209  if ( ! pathLastFilterAccepted && ! pathL3FilterAccepted ) return true;
210  bool foundLastFilter( pathLastFilterAccepted ? pathLastFilterAccepted_.at( match - pathNames_.begin() ) : true );
211  bool foundL3Filter( pathL3FilterAccepted ? pathL3FilterAccepted_.at( match - pathNames_.begin() ) : true );
212  // Return for assigned path name, if trigger object usage meets requirement
213  return ( foundLastFilter && foundL3Filter );
214 }
215 
216 
217 // Methods
218 
219 
220 // Gets the pat::TriggerObject (parent class)
222 {
223  // Create a TriggerObjects
224  TriggerObject theObj( p4(), pdgId() );
225  // Set its collection and trigger objects types (no c'tor for that)
226  theObj.setCollection( collection() );
227  for ( size_t i = 0; i < triggerObjectTypes().size(); ++i ) theObj.addTriggerObjectType( triggerObjectTypes().at( i ) );
228  // Return TriggerObject
229  return theObj;
230 }
231 
232 
233 // Checks, if a certain label of original collection is assigned (method overrides)
235 {
236  // Move to wild-card parser, if needed only
237  if ( collName.find( wildcard_ ) != std::string::npos ) {
238  // True, if collection name is simply fine
239  if ( hasAnyName( collName, std::vector< std::string >( 1, collection() ) ) ) return true;
240  // Check, if collection name possibly fits in an edm::InputTag approach
241  const edm::InputTag collectionTag( collection() );
242  const edm::InputTag collTag( collName );
243  // If evaluated collection tag contains a process name, it must have been found already by identity check
244  if ( collTag.process().empty() ) {
245  // Check instance ...
246  if ( ( collTag.instance().empty() && collectionTag.instance().empty() ) || hasAnyName( collTag.instance(), std::vector< std::string >( 1, collectionTag.instance() ) ) ) {
247  // ... and label
248  return hasAnyName( collTag.label(), std::vector< std::string >( 1, collectionTag.label() ) );
249  }
250  }
251  return false;
252  }
253  // Use parent class's method otherwise
254  return TriggerObject::hasCollection( collName );
255 }
256 
257 
258 bool TriggerObjectStandAlone::checkIfPathsAreUnpacked(bool throwIfPacked) const {
259  bool unpacked = (!pathNames_.empty() || pathIndices_.empty());
260  if (!unpacked && throwIfPacked) throw cms::Exception("RuntimeError", "This TriggerObjectStandAlone object has packed trigger path names. Before accessing path names you must call unpackPathNames with an edm::TriggerNames object. You can get the latter from the edm::Event or fwlite::Event and the TriggerResults\n");
261  return unpacked;
262 }
263 
265  if (!pathIndices_.empty()) {
266  if (!pathNames_.empty()) {
267  throw cms::Exception("RuntimeError", "Error, trying to pack a partially packed TriggerObjectStandAlone");
268  } else {
269  return;
270  }
271  }
272  bool ok = true;
273  unsigned int n = pathNames_.size(), end = names.size();
274  std::vector<uint16_t> indices(n);
275  for (unsigned int i = 0; i < n; ++i) {
276  uint16_t id = names.triggerIndex(pathNames_[i]);
277  if (id >= end) {
278  static int _warn = 0;
279  if (++_warn < 5) std::cerr << "Warning: can't resolve '" << pathNames_[i] << "' to a path index" << std::endl;
280  ok = false; break;
281  } else {
282  indices[i] = id;
283  }
284  }
285  if (ok) {
286  pathIndices_.swap(indices);
287  pathNames_.clear();
288  }
289 }
290 
292  if (!pathNames_.empty()) {
293  if (!pathIndices_.empty()) {
294  throw cms::Exception("RuntimeError", "Error, trying to unpack a partially unpacked TriggerObjectStandAlone");
295  } else {
296  return;
297  }
298  }
299  unsigned int n = pathIndices_.size(), end = names.size();
300  std::vector<std::string> paths(n);
301  for (unsigned int i = 0; i < n; ++i) {
302  if (pathIndices_[i] >= end) throw cms::Exception("RuntimeError", "Error, path index out of bounds");
303  paths[i] = names.triggerName(pathIndices_[i]);
304  }
305  pathIndices_.clear();
306  pathNames_.swap(paths);
307 }
308 
int i
Definition: DBlmapReader.cc:9
virtual int pdgId() const
PDG identifier.
std::vector< std::string > pathsOrAlgorithms(bool pathLastFilterAccepted, bool pathL3FilterAccepted) const
Gets all HLT path or L1 algorithm names.
bool checkIfPathsAreUnpacked(bool throwIfPacked=true) const
Check if trigger names have been packed by calling packPathNames() and not yet unpacked.
static const HistoName names[]
size_t size_type
Definition: Candidate.h:30
bool id(trigger::TriggerObjectType triggerObjectType) const
virtual bool hasCollection(const std::string &collName) const
Checks, if a certain label of original collection is assigned (method overrides)
static const char wildcard_
Constants.
std::vector< std::string > pathNames_
Vector of names of all HLT paths or L1 algorithms the trigger objects has been used in...
std::vector< std::string > filterLabels_
Vector of labels of all HLT filters or names od L1 conditions the trigger objects has been used in...
Strings::size_type size() const
Definition: TriggerNames.cc:39
void unpackPathNames(const edm::TriggerNames &names)
unpack trigger names into indices
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
Analysis-level trigger object class.
Definition: TriggerObject.h:47
bool hasAnyName(const std::string &name, const std::vector< std::string > &nameVec) const
Private methods.
void addPathOrAlgorithm(const std::string &name, bool pathLastFilterAccepted, bool pathL3FilterAccepted)
Adds a new HLT path or L1 algorithm name.
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:22
TriggerObject triggerObject()
Gets the pat::TriggerObject (parent class)
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:32
void setCollection(const std::string &collName)
Methods.
Definition: TriggerObject.h:84
const_iterator end() const
last daughter const_iterator
Definition: Candidate.h:146
const std::string & collection() const
Get the label of the collection the trigger object originates from.
Definition: TriggerObject.h:92
TriggerObjectStandAlone()
Constructors and Destructor.
unsigned int index
Definition: LeafCandidate.h:31
std::vector< int > triggerObjectTypes() const
Get all trigger object type identifiers.
bool hasLastFilter() const
Check, if the usage indicator vectors have been filled.
unsigned int index
index type
Definition: Candidate.h:51
bool hasPathOrAlgorithm(const std::string &name, bool pathLastFilterAccepted, bool pathL3FilterAccepted) const
Checks, if a certain HLT path or L1 algorithm name is assigned.
std::vector< bool > pathL3FilterAccepted_
std::string const & triggerName(unsigned int index) const
Definition: TriggerNames.cc:27
void packPathNames(const edm::TriggerNames &names)
pack trigger names into indices
virtual bool hasCollection(const std::string &collName) const
Checks, if a certain label of original collection is assigned.
std::string const & label() const
Definition: InputTag.h:42
std::string const & process() const
Definition: InputTag.h:46
std::vector< uint16_t > pathIndices_
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:23
bool hasFilterOrCondition(const std::string &name) const
Checks, if a certain HLT filter label or L1 condition name is assigned.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
void addTriggerObjectType(trigger::TriggerObjectType triggerObjectType)
Add a new trigger object type identifier.
Definition: TriggerObject.h:87
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
std::string const & instance() const
Definition: InputTag.h:43
std::vector< bool > pathLastFilterAccepted_
double split
Definition: MVATrainer.cc:139
list at
Definition: asciidump.py:428