CandCombiner

CandCombinerBase

RoleNames

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#ifndef CandAlgos_CandCombiner_h
#define CandAlgos_CandCombiner_h
/** \class CandCombiner
 *
 * performs all possible and selected combinations
 * of particle pairs using the CandCombiner utility
 *
 * \author Luca Lista, INFN
 *
 * \version $Revision: 1.2 $
 *
 * $Id: CandCombiner.h,v 1.2 2009/04/22 17:51:05 kaulmer Exp $
 *
 */
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "CommonTools/CandUtils/interface/CandCombiner.h"
#include "CommonTools/CandAlgos/interface/decayParser.h"
#include "CommonTools/Utils/interface/StringCutObjectSelector.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
#include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
#include "CommonTools/Utils/interface/cutParser.h"
#include "DataFormats/Candidate/interface/Candidate.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/transform.h"
#include "CommonTools/CandUtils/interface/AddFourMomenta.h"
#include <string>
#include <vector>
#include <algorithm>

namespace edm {
  class ParameterSet;
}

namespace reco {
  namespace modules {

    struct RoleNames {
      explicit RoleNames(const edm::ParameterSet& cfg) {
        if (cfg.exists("name"))
          name_ = cfg.getParameter<std::string>("name");
        else
          name_ = "";
        if (cfg.exists("roles"))
          roles_ = cfg.getParameter<std::vector<std::string> >("roles");
        else
          roles_ = std::vector<std::string>();
      }
      const std::vector<std::string> roles() const { return roles_; }
      void set(reco::CompositeCandidate& c) const {
        c.setName(name_);
        c.setRoles(roles_);
        c.applyRoles();
      }

    private:
      /// Name of this candidate
      std::string name_;
      // Name of the roles
      std::vector<std::string> roles_;
    };

    struct CandCombinerBase : public edm::stream::EDProducer<> {
      CandCombinerBase(const edm::ParameterSet& cfg)
          : setLongLived_(false), setMassConstraint_(false), setPdgId_(false) {
        using namespace cand::parser;
        using namespace std;
        string decay(cfg.getParameter<string>("decay"));
        if (decayParser(decay, labels_))
          for (vector<ConjInfo>::iterator label = labels_.begin(); label != labels_.end(); ++label)
            if (label->mode_ == ConjInfo::kPlus)
              dauCharge_.push_back(1);
            else if (label->mode_ == ConjInfo::kMinus)
              dauCharge_.push_back(-1);
            else
              dauCharge_.push_back(0);
        else
          throw edm::Exception(edm::errors::Configuration, "failed to parse \"" + decay + "\"");

        int lists = labels_.size();
        if (lists != 2 && lists != 3 && lists != 4)
          throw edm::Exception(edm::errors::LogicError, "invalid number of collections");
        bool found;
        const string setLongLived("setLongLived");
        vector<string> vBoolParams = cfg.getParameterNamesForType<bool>();
        found = find(vBoolParams.begin(), vBoolParams.end(), setLongLived) != vBoolParams.end();
        if (found)
          setLongLived_ = cfg.getParameter<bool>("setLongLived");
        const string setMassConstraint("setMassConstraint");
        found = find(vBoolParams.begin(), vBoolParams.end(), setMassConstraint) != vBoolParams.end();
        if (found)
          setMassConstraint_ = cfg.getParameter<bool>("setMassConstraint");
        const string setPdgId("setPdgId");
        vector<string> vIntParams = cfg.getParameterNamesForType<int>();
        found = find(vIntParams.begin(), vIntParams.end(), setPdgId) != vIntParams.end();
        if (found) {
          setPdgId_ = true;
          pdgId_ = cfg.getParameter<int>("setPdgId");
        }
        tokens_ =
            edm::vector_transform(labels_, [this](ConjInfo const& cI) { return consumes<CandidateView>(cI.tag_); });
      }

    protected:
      /// label vector
      std::vector<cand::parser::ConjInfo> labels_;
      std::vector<edm::EDGetTokenT<CandidateView> > tokens_;
      /// daughter charges
      std::vector<int> dauCharge_;
      /// set long lived flag
      bool setLongLived_;
      /// set mass constraint flag
      bool setMassConstraint_;
      /// set pdgId flag
      bool setPdgId_;
      /// which pdgId to set
      int pdgId_;
    };

    template <typename Selector,
              typename PairSelector = AnyPairSelector,
              typename Cloner = ::combiner::helpers::NormalClone,
              typename OutputCollection = reco::CompositeCandidateCollection,
              typename Setup = AddFourMomenta,
              typename Init = typename ::reco::modules::EventSetupInit<Setup>::type>
    class CandCombiner : public CandCombinerBase {
    public:
      /// constructor from parameter settypedef
      explicit CandCombiner(const edm::ParameterSet& cfg)
          : CandCombinerBase(cfg),
            combinerInit_(consumesCollector()),
            combiner_(reco::modules::make<Selector>(cfg, consumesCollector()),
                      reco::modules::make<PairSelector>(cfg),
                      Setup(cfg),
                      cfg.existsAs<bool>("checkCharge") ? cfg.getParameter<bool>("checkCharge") : true,
                      cfg.existsAs<bool>("checkOverlap") ? cfg.getParameter<bool>("checkOverlap") : true,
                      dauCharge_),
            names_(cfg) {
        produces<OutputCollection>();
      }
      /// destructor
      ~CandCombiner() override {}

    private:
      /// process an event
      void produce(edm::Event& evt, const edm::EventSetup& es) override {
        combinerInit_.init(combiner_.setup(), evt, es);
        int n = labels_.size();
        std::vector<edm::Handle<CandidateView> > colls(n);
        for (int i = 0; i < n; ++i)
          evt.getByToken(tokens_[i], colls[i]);

        std::unique_ptr<OutputCollection> out = combiner_.combine(colls, names_.roles());
        if (setLongLived_ || setMassConstraint_ || setPdgId_) {
          typename OutputCollection::iterator i = out->begin(), e = out->end();
          for (; i != e; ++i) {
            names_.set(*i);
            if (setLongLived_)
              i->setLongLived();
            if (setMassConstraint_)
              i->setMassConstraint();
            if (setPdgId_)
              i->setPdgId(pdgId_);
          }
        }
        evt.put(std::move(out));
      }
      /// combiner utility
      Init combinerInit_;
      ::CandCombiner<Selector, PairSelector, Cloner, OutputCollection, Setup> combiner_;

      RoleNames names_;
    };

  }  // namespace modules
}  // namespace reco

#endif