Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-08 01:45:51

0001 #ifndef HeavyFlavorAnalysis_RecoDecay_BPHAnalyzerTokenWrapper_h
0002 #define HeavyFlavorAnalysis_RecoDecay_BPHAnalyzerTokenWrapper_h
0003 /** \classes BPHModuleWrapper, BPHTokenWrapper, BPHESTokenWrapper,
0004  *           BPHEventSetupWrapper and BPHAnalyzerWrapper
0005  *
0006  *  Description: 
0007  *    Common interfaces to define modules and get objects
0008  *    from "old" and "new" CMSSW version in an uniform way
0009  *
0010  *  \author Paolo Ronchese INFN Padova
0011  *
0012  */
0013 
0014 //----------------------
0015 // Base Class Headers --
0016 //----------------------
0017 
0018 //------------------------------------
0019 // Collaborating Class Declarations --
0020 //------------------------------------
0021 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoCandidate.h"
0022 #include "FWCore/Framework/interface/Event.h"
0023 #include "FWCore/Framework/interface/EventSetup.h"
0024 #include "FWCore/Framework/interface/EDConsumerBase.h"
0025 #include "FWCore/Framework/interface/ESConsumesCollector.h"
0026 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0027 #include "FWCore/Framework/interface/one/EDProducer.h"
0028 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0029 #include "FWCore/Framework/interface/stream/EDProducer.h"
0030 
0031 //---------------
0032 // C++ Headers --
0033 //---------------
0034 #include <string>
0035 #include <map>
0036 #include <memory>
0037 
0038 //              ---------------------
0039 //              -- Class Interface --
0040 //              ---------------------
0041 
0042 class BPHModuleWrapper {
0043 public:
0044   typedef edm::one::EDAnalyzer<> one_analyzer;
0045   typedef edm::one::EDProducer<> one_producer;
0046   typedef edm::stream::EDAnalyzer<> stream_analyzer;
0047   typedef edm::stream::EDProducer<> stream_producer;
0048 };
0049 
0050 template <class Obj>
0051 class BPHTokenWrapper {
0052 public:
0053   typedef edm::EDGetTokenT<Obj> type;
0054   bool get(const edm::Event& ev, edm::Handle<Obj>& obj) { return ev.getByToken(token, obj); }
0055   type token;
0056 };
0057 
0058 template <class Obj, class Rec>
0059 class BPHESTokenWrapper {
0060 public:
0061   typedef edm::ESGetToken<Obj, Rec> type;
0062   bool get(const edm::EventSetup& es, edm::ESHandle<Obj>& obj) {
0063     obj = es.get<Rec>().getHandle(token);
0064     return obj.isValid();
0065   }
0066   type token;
0067 };
0068 
0069 template <class T>
0070 class BPHAnalyzerWrapper : public T {
0071 protected:
0072   template <class Obj>
0073   void consume(BPHTokenWrapper<Obj>& tw, const std::string& label) {
0074     edm::InputTag tag(label);
0075     tw.token = this->template consumes<Obj>(tag);
0076     return;
0077   }
0078   template <class Obj>
0079   void consume(BPHTokenWrapper<Obj>& tw, const edm::InputTag& tag) {
0080     tw.token = this->template consumes<Obj>(tag);
0081     return;
0082   }
0083   template <class Obj, class Rec>
0084   void esConsume(BPHESTokenWrapper<Obj, Rec>& tw) {
0085     tw.token = this->template esConsumes<Obj, Rec>();
0086     return;
0087   }
0088   template <class Obj, class Rec>
0089   void esConsume(BPHESTokenWrapper<Obj, Rec>& tw, const std::string& label) {
0090     tw.token = this->template esConsumes<Obj, Rec>(edm::ESInputTag("", label));
0091     return;
0092   }
0093   template <class Obj, class Rec>
0094   void esConsume(BPHESTokenWrapper<Obj, Rec>& tw, const edm::ESInputTag& tag) {
0095     tw.token = this->template esConsumes<Obj>(tag);
0096     return;
0097   }
0098 };
0099 
0100 class BPHEventSetupWrapper {
0101 public:
0102   explicit BPHEventSetupWrapper(const edm::EventSetup& es)
0103       : ep(&es), twMap(new std::map<BPHRecoCandidate::esType, void*>) {}
0104   BPHEventSetupWrapper(const edm::EventSetup& es, BPHRecoCandidate::esType type, void* token)
0105       : BPHEventSetupWrapper(es) {
0106     (*twMap)[type] = token;
0107   }
0108   BPHEventSetupWrapper(const edm::EventSetup& es, std::map<BPHRecoCandidate::esType, void*> tokenMap)
0109       : BPHEventSetupWrapper(es) {
0110     twMap->insert(tokenMap.begin(), tokenMap.end());
0111   }
0112   BPHEventSetupWrapper(const BPHEventSetupWrapper& es) = default;
0113   BPHEventSetupWrapper(const BPHEventSetupWrapper* es) : BPHEventSetupWrapper(*es) {}
0114   BPHEventSetupWrapper(const BPHEventSetupWrapper& es, BPHRecoCandidate::esType type, void* token)
0115       : BPHEventSetupWrapper(es) {
0116     (*twMap)[type] = token;
0117   }
0118   BPHEventSetupWrapper(BPHEventSetupWrapper& es, std::map<BPHRecoCandidate::esType, void*> tokenMap)
0119       : BPHEventSetupWrapper(es) {
0120     twMap->insert(tokenMap.begin(), tokenMap.end());
0121   }
0122   const edm::EventSetup* get() const { return ep; }
0123   operator const edm::EventSetup&() const { return *ep; }
0124   template <class Obj, class Rec>
0125   BPHESTokenWrapper<Obj, Rec>* get(BPHRecoCandidate::esType type) const {
0126     const auto& iter = twMap->find(type);
0127     return (iter == twMap->end() ? nullptr : static_cast<BPHESTokenWrapper<Obj, Rec>*>(iter->second));
0128   }
0129 
0130 private:
0131   const edm::EventSetup* ep;
0132   std::shared_ptr<std::map<BPHRecoCandidate::esType, void*>> twMap;
0133 };
0134 
0135 #endif