Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:51

0001 #ifndef RecoBTau_BTauComputer_GenericMVAComputer_h
0002 #define RecoBTau_BTauComputer_GenericMVAComputer_h
0003 
0004 #include <iterator>
0005 #include <vector>
0006 
0007 #include "DataFormats/BTauReco/interface/TaggingVariable.h"
0008 #include "PhysicsTools/MVAComputer/interface/AtomicId.h"
0009 #include "PhysicsTools/MVAComputer/interface/Calibration.h"
0010 #include "PhysicsTools/MVAComputer/interface/MVAComputer.h"
0011 
0012 // overload MVAComputer and replace eval() methods to work on TaggingVariable
0013 class GenericMVAComputer : public PhysicsTools::MVAComputer {
0014 public:
0015   // forward declarations;
0016   template <typename Iter_t>
0017   class TaggingVariableIterator;
0018   class TaggingVariableMapping;
0019 
0020   GenericMVAComputer(const PhysicsTools::Calibration::MVAComputer *calib) : PhysicsTools::MVAComputer(calib) {}
0021 
0022   // create wrapping iterator
0023   template <typename Iter_t>
0024   inline TaggingVariableIterator<Iter_t> iterator(Iter_t iter) const {
0025     return TaggingVariableIterator<Iter_t>(&mapping, iter);
0026   }
0027 
0028   // overload eval method to work on containers of TaggingVariable
0029   template <typename Iter_t>
0030   inline double eval(Iter_t first, Iter_t last) const {
0031     typedef TaggingVariableIterator<Iter_t> Wrapped_t;
0032     return PhysicsTools::MVAComputer::template eval<Wrapped_t>(iterator<Iter_t>(first), iterator<Iter_t>(last));
0033   }
0034 
0035   template <typename Container_t>
0036   inline double eval(const Container_t &values) const {
0037     typedef typename Container_t::const_iterator Iter_t;
0038     return this->template eval<Iter_t>(values.begin(), values.end());
0039   }
0040 
0041   // iterator wrapper with on-the-fly TaggingVariableName -> AtomicId mapping
0042   //
0043   // Notice that this tells the computer to completely inline it
0044   // this is reasonable since inlining it means that the optimizer
0045   // will not produce any additional code for the wrapper
0046   // (it is entirely optimized away), except for the actual mapping
0047   // which is no more than a simple array lookup.
0048   //
0049   // The result will be inlined into the eval() template of MVAComputer
0050   // which will be instantiated as one single tightly-integrated
0051   // function.
0052   template <typename Iter_t>
0053   class TaggingVariableIterator {
0054   public:
0055     // class implementing MVAComputer::Variable::Value interface
0056     struct Value {
0057     public:
0058       // the actual operator doing the mapping
0059       inline PhysicsTools::AtomicId getName() const { return mapping->getAtomicId(iter->first); }
0060 
0061       inline double getValue() const { return iter->second; }
0062 
0063       operator PhysicsTools::Variable::Value() const { return PhysicsTools::Variable::Value(getName(), getValue()); }
0064 
0065     protected:
0066       friend class TaggingVariableIterator;
0067 
0068       inline Value(TaggingVariableMapping const *mapping, const Iter_t &iter) : mapping(mapping), iter(iter) {}
0069 
0070     private:
0071       // pointer to the current mapping
0072       TaggingVariableMapping const *mapping;
0073       // iterator to reco::TaggingVariable in orig. container
0074       Iter_t iter;
0075     };
0076 
0077     typedef std::forward_iterator_tag iterator_category;
0078     typedef Value value_type;
0079     typedef typename std::iterator_traits<Iter_t>::difference_type difference_type;
0080     typedef const Value *pointer;
0081     typedef const Value &reference;
0082 
0083     inline ~TaggingVariableIterator() {}
0084 
0085     // methods to make class a standard forward iterator
0086 
0087     inline const Value &operator*() const { return value; }
0088     inline Value &operator*() { return value; }
0089 
0090     inline const Value *operator->() const { return &value; }
0091     inline Value *operator->() { return &value; }
0092 
0093     inline bool operator==(const TaggingVariableIterator &other) const { return value.iter == other.value.iter; }
0094     inline bool operator!=(const TaggingVariableIterator &other) const { return value.iter != other.value.iter; }
0095     inline bool operator<(const TaggingVariableIterator &other) const { return value.iter < other.value.iter; }
0096 
0097     inline TaggingVariableIterator &operator++() {
0098       ++value.iter;
0099       return *this;
0100     }
0101 
0102     inline TaggingVariableIterator operator++(int dummy) {
0103       TaggingVariableIterator orig = *this;
0104       ++value.iter;
0105       return orig;
0106     }
0107 
0108   protected:
0109     friend class GenericMVAComputer;
0110     inline TaggingVariableIterator(TaggingVariableMapping const *mapping, const Iter_t &iter) : value(mapping, iter) {}
0111 
0112   private:
0113     // holds the current "value"
0114     // it's really only an iterator that points the original
0115     // current TaggingVariable plus required methods
0116     Value value;
0117   };
0118 
0119   // TaggingVariableName -> PhysicsTools::AtomicId mapping
0120   class TaggingVariableMapping {
0121   public:
0122     typedef PhysicsTools::AtomicId AtomicId;
0123     typedef reco::TaggingVariableName TaggingName;
0124 
0125     TaggingVariableMapping();
0126     ~TaggingVariableMapping() {}
0127 
0128     inline AtomicId getAtomicId(TaggingName taggingName) const { return taggingVarToAtomicId[taggingName]; }
0129 
0130   private:
0131     std::vector<AtomicId> taggingVarToAtomicId;
0132   };
0133 
0134 private:
0135   static const TaggingVariableMapping mapping;
0136 };
0137 
0138 #endif  // RecoBTau_BTauComputer_GenericMVAComputer_h