Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:46

0001 #ifndef PhysicsTools_PatAlgos_interface_MultiIsolator_h
0002 #define PhysicsTools_PatAlgos_interface_MultiIsolator_h
0003 
0004 #include "DataFormats/Common/interface/View.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "PhysicsTools/PatAlgos/interface/BaseIsolator.h"
0008 #include "DataFormats/PatCandidates/interface/Isolation.h"
0009 
0010 #include <memory>
0011 #include <vector>
0012 
0013 namespace pat {
0014   namespace helper {
0015     class MultiIsolator {
0016     public:
0017       typedef std::vector<std::pair<pat::IsolationKeys, float>> IsolationValuePairs;
0018       MultiIsolator() {}
0019       MultiIsolator(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC, bool cuts = true);
0020       ~MultiIsolator() {}
0021 
0022       // adds an isolator (and takes onwership of the pointer)
0023       void addIsolator(BaseIsolator *iso, uint32_t mask, pat::IsolationKeys key);
0024 
0025       // parses an isolator and adds it to the list
0026       void addIsolator(const edm::ParameterSet &conf,
0027                        edm::ConsumesCollector &iC,
0028                        bool withCut,
0029                        uint32_t mask,
0030                        pat::IsolationKeys key);
0031 
0032       // Parses out an isolator, and returns a pointer to it.
0033       // For an empty PSet, it returns a null pointer.
0034       // You own the returned pointer!
0035       static BaseIsolator *make(const edm::ParameterSet &conf, edm::ConsumesCollector &iC, bool withCut);
0036 
0037       void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup);
0038       void endEvent();
0039 
0040       template <typename T>
0041       uint32_t test(const edm::View<T> &coll, int idx) const;
0042 
0043       template <typename T>
0044       void fill(const edm::View<T> &coll, int idx, IsolationValuePairs &isolations) const;
0045 
0046       /// Fill Isolation from a Ref, Ptr or RefToBase to the object
0047       template <typename RefType>
0048       void fill(const RefType &ref, IsolationValuePairs &isolations) const;
0049 
0050       void print(std::ostream &out) const;
0051 
0052       std::string printSummary() const;
0053 
0054       /// True if it has a non null configuration
0055       bool enabled() const { return !isolators_.empty(); }
0056 
0057     private:
0058       std::vector<std::unique_ptr<BaseIsolator>> isolators_;
0059       std::vector<uint32_t> masks_;
0060       std::vector<pat::IsolationKeys> keys_;
0061     };
0062 
0063     template <typename T>
0064     uint32_t MultiIsolator::test(const edm::View<T> &coll, int idx) const {
0065       uint32_t retval = 0;
0066       edm::RefToBase<T> rb = coll.refAt(idx);  // edm::Ptr<T> in a shiny new future to come one remote day ;-)
0067       for (size_t i = 0, n = isolators_.size(); i < n; ++i) {
0068         if (!isolators_[i]->test(rb))
0069           retval |= masks_[i];
0070       }
0071       return retval;
0072     }
0073 
0074     template <typename RefType>
0075     void MultiIsolator::fill(const RefType &rb, IsolationValuePairs &isolations) const {
0076       isolations.resize(isolators_.size());
0077       for (size_t i = 0, n = isolators_.size(); i < n; ++i) {
0078         isolations[i].first = keys_[i];
0079         isolations[i].second = isolators_[i]->getValue(rb);
0080       }
0081     }
0082 
0083     template <typename T>
0084     void MultiIsolator::fill(const edm::View<T> &coll, int idx, IsolationValuePairs &isolations) const {
0085       edm::RefToBase<T> rb = coll.refAt(idx);
0086       fill(rb, isolations);
0087     }
0088 
0089   }  // namespace helper
0090 }  // namespace pat
0091 
0092 #endif