Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:56

0001 #ifndef DataFormats_PatCandidates_TriggerObjectStandAlone_h
0002 #define DataFormats_PatCandidates_TriggerObjectStandAlone_h
0003 
0004 // -*- C++ -*-
0005 //
0006 // Package:    PatCandidates
0007 // Class:      pat::TriggerObjectStandAlone
0008 //
0009 //
0010 /**
0011   \class    pat::TriggerObjectStandAlone TriggerObjectStandAlone.h "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
0012   \brief    Analysis-level trigger object class (stand-alone)
0013 
0014    TriggerObjectStandAlone implements a container for trigger objects' information within the 'pat' namespace.
0015    These Trigger objects keep also information on filters and paths to be saved independently or embedded into PAT objects.
0016    The TriggerObjectStandAlone is also the data format used in the PAT trigger object matching.
0017    For detailed information, consult
0018    https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePATTrigger#TriggerObjectStandAlone
0019 
0020   \author   Volker Adler
0021 */
0022 
0023 #include "DataFormats/PatCandidates/interface/TriggerObject.h"
0024 #include "DataFormats/Common/interface/TriggerResults.h"
0025 #include "FWCore/Common/interface/EventBase.h"
0026 namespace edm {
0027   class TriggerNames;
0028 }
0029 
0030 namespace pat {
0031 
0032   class TriggerObjectStandAlone : public TriggerObject {
0033     /// Data Members
0034     /// Keeping the old names of the data members for backward compatibility,
0035     /// although they refer only to HLT objects.
0036 
0037     /// Vector of labels of all HLT filters or names of L1 conditions the trigger objects has been used in
0038     std::vector<std::string> filterLabels_;
0039     std::vector<uint16_t> filterLabelIndices_;
0040 
0041     /// Vector of names of all HLT paths or L1 algorithms the trigger objects has been used in
0042     std::vector<std::string> pathNames_;
0043     std::vector<uint16_t> pathIndices_;
0044 
0045     /// Vector alligned with 'pathNames_' of boolean indicating the usage of the trigger object
0046     /// An element is true, if the corresponding path succeeded and the trigger object was used in the last filter (HLT)
0047     /// or the corresponding algorithm succeeded as well as the corresponding condition (L1).
0048     /// The vector is empty for data (size 0), if the according information is not available.
0049     std::vector<bool> pathLastFilterAccepted_;
0050     /// Vector alligned with 'pathNames_' of boolean indicating the usage of the trigger object
0051     /// An element is true, if the corresponding path succeeded and the trigger object was used in an L3 filter (HLT only)
0052     /// The vector is empty for data (size 0), if the according information is not available.
0053     std::vector<bool> pathL3FilterAccepted_;
0054 
0055     edm::ParameterSetID psetId_;
0056 
0057     /// Constants
0058 
0059     /// Constant defining the wild-card used in 'hasAnyName()'
0060     static const char wildcard_ = '*';
0061 
0062     /// Private methods
0063 
0064     /// Checks a string vector for occurence of a certain string, incl. wild-card mechanism
0065     bool hasAnyName(const std::string &name, const std::vector<std::string> &nameVec) const;
0066     /// Adds a new HLT filter label or L1 condition name
0067     void addFilterOrCondition(const std::string &name) {
0068       if (!hasFilterOrCondition(name))
0069         filterLabels_.push_back(name);
0070     };
0071     /// Adds a new HLT path or L1 algorithm name
0072     void addPathOrAlgorithm(const std::string &name, bool pathLastFilterAccepted, bool pathL3FilterAccepted);
0073     /// Gets all HLT filter labels or L1 condition names
0074     const std::vector<std::string> &filtersOrConditions() const {
0075       checkIfFiltersAreUnpacked();
0076       return filterLabels_;
0077     };
0078     /// Gets all HLT path or L1 algorithm names
0079     std::vector<std::string> pathsOrAlgorithms(bool pathLastFilterAccepted, bool pathL3FilterAccepted) const;
0080     /// Checks, if a certain HLT filter label or L1 condition name is assigned
0081     bool hasFilterOrCondition(const std::string &name) const;
0082     /// Checks, if a certain HLT path or L1 algorithm name is assigned
0083     bool hasPathOrAlgorithm(const std::string &name, bool pathLastFilterAccepted, bool pathL3FilterAccepted) const;
0084     /// Check, if the usage indicator vectors have been filled
0085     bool hasLastFilter() const {
0086       return (!pathLastFilterAccepted_.empty() && pathLastFilterAccepted_.size() == pathNames_.size());
0087     };
0088     bool hasL3Filter() const {
0089       return (!pathL3FilterAccepted_.empty() && pathL3FilterAccepted_.size() == pathNames_.size());
0090     };
0091 
0092     /// Check if trigger names have been packed by calling packPathNames() and not yet unpacked
0093     bool checkIfPathsAreUnpacked(bool throwIfPacked = true) const;
0094     /// Check if trigger names have been packed by calling packFilterLabels() and not yet unpacked
0095     bool checkIfFiltersAreUnpacked(bool throwIfPacked = true) const;
0096 
0097   public:
0098     /// Constructors and Destructor
0099 
0100     /// Default constructor
0101     TriggerObjectStandAlone();
0102     /// Constructor from pat::TriggerObject
0103     TriggerObjectStandAlone(const TriggerObject &trigObj);
0104     /// Constructor from trigger::TriggerObject
0105     TriggerObjectStandAlone(const trigger::TriggerObject &trigObj);
0106     /// Constructor from reco::Candidate
0107     TriggerObjectStandAlone(const reco::LeafCandidate &leafCand);
0108     /// Constructors from Lorentz-vectors and (optional) PDG ID
0109     TriggerObjectStandAlone(const reco::Particle::LorentzVector &vec, int id = 0);
0110     TriggerObjectStandAlone(const reco::Particle::PolarLorentzVector &vec, int id = 0);
0111     TriggerObjectStandAlone(const TriggerObjectStandAlone &) = default;
0112 
0113     /// Destructor
0114     ~TriggerObjectStandAlone() override {}
0115 
0116     /// Methods
0117 
0118     TriggerObjectStandAlone copy() const { return *this; }
0119 
0120     /// Adds a new HLT filter label
0121     void addFilterLabel(const std::string &filterLabel) { addFilterOrCondition(filterLabel); };
0122     /// Adds a new L1 condition name
0123     void addConditionName(const std::string &conditionName) { addFilterOrCondition(conditionName); };
0124     /// Adds a new HLT path name
0125     void addPathName(const std::string &pathName,
0126                      bool pathLastFilterAccepted = true,
0127                      bool pathL3FilterAccepted = true) {
0128       addPathOrAlgorithm(pathName, pathLastFilterAccepted, pathL3FilterAccepted);
0129     };
0130     /// Adds a new L1 algorithm name
0131     void addAlgorithmName(const std::string &algorithmName, bool algoCondAccepted = true) {
0132       addPathOrAlgorithm(algorithmName, algoCondAccepted, false);
0133     };
0134     /// Gets all HLT filter labels
0135     const std::vector<std::string> &filterLabels() const { return filtersOrConditions(); };
0136     /// Gets all L1 condition names
0137     const std::vector<std::string> &conditionNames() const { return filtersOrConditions(); };
0138     /// Gets all HLT path names
0139     std::vector<std::string> pathNames(bool pathLastFilterAccepted = false, bool pathL3FilterAccepted = true) const {
0140       return pathsOrAlgorithms(pathLastFilterAccepted, pathL3FilterAccepted);
0141     };
0142     /// Gets all L1 algorithm names
0143     std::vector<std::string> algorithmNames(bool algoCondAccepted = true) const {
0144       return pathsOrAlgorithms(algoCondAccepted, false);
0145     };
0146     /// Gets the pat::TriggerObject (parent class)
0147     TriggerObject triggerObject();
0148     /// Checks, if a certain HLT filter label is assigned
0149     bool hasFilterLabel(const std::string &filterLabel) const { return hasFilterOrCondition(filterLabel); };
0150     /// Checks, if a certain L1 condition name is assigned
0151     bool hasConditionName(const std::string &conditionName) const { return hasFilterOrCondition(conditionName); };
0152     /// Checks, if a certain HLT path name is assigned
0153     bool hasPathName(const std::string &pathName,
0154                      bool pathLastFilterAccepted = false,
0155                      bool pathL3FilterAccepted = true) const {
0156       return hasPathOrAlgorithm(pathName, pathLastFilterAccepted, pathL3FilterAccepted);
0157     };
0158     /// Checks, if a certain L1 algorithm name is assigned
0159     bool hasAlgorithmName(const std::string &algorithmName, bool algoCondAccepted = true) const {
0160       return hasPathOrAlgorithm(algorithmName, algoCondAccepted, false);
0161     };
0162     /// Checks, if a certain label of original collection is assigned (method overrides)
0163     bool hasCollection(const std::string &collName) const override;
0164     bool hasCollection(const edm::InputTag &collName) const override { return hasCollection(collName.encode()); };
0165     /// Checks, if the usage indicator vector has been filled
0166     bool hasPathLastFilterAccepted() const { return hasLastFilter(); };
0167     bool hasAlgoCondAccepted() const { return hasLastFilter(); };
0168     bool hasPathL3FilterAccepted() const { return hasL3Filter(); };
0169 
0170     /// Special methods for the cut string parser
0171     /// - argument types usable in the cut string parser
0172     /// - short names for readable configuration files
0173 
0174     /// Calls 'hasFilterLabel(...)'
0175     bool filter(const std::string &filterLabel) const { return hasFilterLabel(filterLabel); };
0176     /// Calls 'hasConditionName(...)'
0177     bool cond(const std::string &conditionName) const { return hasConditionName(conditionName); };
0178     /// Calls 'hasPathName(...)'
0179     bool path(const std::string &pathName,
0180               unsigned pathLastFilterAccepted = 0,
0181               unsigned pathL3FilterAccepted = 1) const {
0182       return hasPathName(pathName, bool(pathLastFilterAccepted), bool(pathL3FilterAccepted));
0183     };
0184     /// Calls 'hasAlgorithmName(...)'
0185     bool algo(const std::string &algorithmName, unsigned algoCondAccepted = 1) const {
0186       return hasAlgorithmName(algorithmName, bool(algoCondAccepted));
0187     };
0188     /// Calls 'hasCollection(...)' (method override)
0189     bool coll(const std::string &collName) const override { return hasCollection(collName); };
0190 
0191     ///set the psetid of the trigger process
0192     void setPSetID(const edm::ParameterSetID &psetId) { psetId_ = psetId; }
0193 
0194     const edm::ParameterSetID &psetID() const { return psetId_; }
0195 
0196     void packFilterLabels(const edm::EventBase &event, const edm::TriggerResults &res);
0197     ///  pack trigger names into indices
0198     void packPathNames(const edm::TriggerNames &names);
0199     ///  unpack trigger names into indices
0200     void unpackPathNames(const edm::TriggerNames &names);
0201     ///  pack filter labels into indices; note that the labels must be sorted!
0202     void packFilterLabels(const std::vector<std::string> &labels);
0203     ///  unpack filter labels from indices
0204     void unpackFilterLabels(const std::vector<std::string> &labels);
0205     ///  unpack filter labels from indices
0206     void unpackFilterLabels(const edm::EventBase &event, const edm::TriggerResults &res);
0207     /// unpack both filter labels and trigger names
0208     void unpackNamesAndLabels(const edm::EventBase &event, const edm::TriggerResults &res);
0209 
0210     /// reduce the precision on the 4-vector
0211     void packP4();
0212 
0213     std::vector<std::string> const *allLabels(edm::ParameterSetID const &psetid,
0214                                               const edm::EventBase &event,
0215                                               const edm::TriggerResults &res) const;
0216   };
0217 
0218   /// Collection of TriggerObjectStandAlone
0219   typedef std::vector<TriggerObjectStandAlone> TriggerObjectStandAloneCollection;
0220   /// Persistent reference to an item in a TriggerObjectStandAloneCollection
0221   typedef edm::Ref<TriggerObjectStandAloneCollection> TriggerObjectStandAloneRef;
0222   /// Persistent reference to a TriggerObjectStandAloneCollection product
0223   typedef edm::RefProd<TriggerObjectStandAloneCollection> TriggerObjectStandAloneRefProd;
0224   /// Vector of persistent references to items in the same TriggerObjectStandAloneCollection
0225   typedef edm::RefVector<TriggerObjectStandAloneCollection> TriggerObjectStandAloneRefVector;
0226   /// Const iterator over vector of persistent references to items in the same TriggerObjectStandAloneCollection
0227   typedef edm::RefVectorIterator<TriggerObjectStandAloneCollection> TriggerObjectStandAloneRefVectorIterator;
0228   /// Association of TriggerObjectStandAlones to store matches to Candidates
0229   typedef edm::Association<TriggerObjectStandAloneCollection> TriggerObjectStandAloneMatch;
0230 
0231 }  // namespace pat
0232 
0233 #endif