CMS 3D CMS Logo

PATTauIDEmbedder.cc
Go to the documentation of this file.
10 
12 public:
13  explicit PATTauIDEmbedder(const edm::ParameterSet&);
14  ~PATTauIDEmbedder() override{};
15 
16  void produce(edm::Event&, const edm::EventSetup&) override;
17 
18 private:
19  //--- configuration parameters
21  typedef std::pair<std::string, edm::InputTag> NameTag;
22  typedef std::pair<std::string, int> NameWPIdx;
23  //to save input module tag and corresponding pairs <working point name for the output tree, WP index in the input ID container>
24  typedef std::pair<edm::InputTag, std::vector<NameWPIdx> > IDContainerData;
25  std::vector<NameTag> tauIDSrcs_;
26  std::vector<std::vector<NameWPIdx> > tauIDSrcContainers_;
27  std::vector<edm::EDGetTokenT<pat::PATTauDiscriminator> > patTauIDTokens_;
28  std::vector<edm::EDGetTokenT<reco::TauDiscriminatorContainer> > patTauIDContainerTokens_;
30  size_t nNewTauIds_;
31 };
32 
34  src_ = consumes<pat::TauCollection>(cfg.getParameter<edm::InputTag>("src"));
35  // read the different tau ID names
36  edm::ParameterSet idps = cfg.getParameter<edm::ParameterSet>("tauIDSources");
37  std::vector<std::string> names = idps.getParameterNamesForType<edm::ParameterSet>();
38  std::map<std::string, IDContainerData> idContainerMap;
39  for (auto const& name : names) {
41  int wpidx = idp.getParameter<int>("workingPointIndex");
42  edm::InputTag tag = idp.getParameter<edm::InputTag>("inputTag");
43  if (wpidx == -99) {
44  tauIDSrcs_.push_back(NameTag(name, tag));
45  } else {
46  std::map<std::string, IDContainerData>::iterator it;
47  it = idContainerMap.insert({tag.label() + tag.instance(), {tag, std::vector<NameWPIdx>()}}).first;
48  it->second.second.push_back(NameWPIdx(name, wpidx));
49  }
50  }
51  // but in any case at least once
52  if (tauIDSrcs_.empty() && idContainerMap.empty())
53  throw cms::Exception("Configuration") << "PATTauProducer: id addTauID is true, you must specify:\n"
54  << "\tPSet tauIDSources = { \n"
55  << "\t\tInputTag <someName> = <someTag> // as many as you want \n "
56  << "\t}\n";
57  for (auto const& mapEntry : idContainerMap) {
58  tauIDSrcContainers_.push_back(mapEntry.second.second);
59  patTauIDContainerTokens_.push_back(mayConsume<reco::TauDiscriminatorContainer>(mapEntry.second.first));
60  }
62  tauIDSrcs_, [this](NameTag const& tag) { return mayConsume<pat::PATTauDiscriminator>(tag.second); });
65  for (auto const& cont : tauIDSrcContainers_) {
66  nNewTauIds_ += cont.size();
67  }
68 
69  produces<std::vector<pat::Tau> >();
70 }
71 
74  evt.getByToken(src_, inputTaus);
75 
76  auto outputTaus = std::make_unique<std::vector<pat::Tau> >();
77  outputTaus->reserve(inputTaus->size());
78 
79  int tau_idx = 0;
80  for (pat::TauCollection::const_iterator inputTau = inputTaus->begin(); inputTau != inputTaus->end();
81  ++inputTau, ++tau_idx) {
82  pat::Tau outputTau(*inputTau);
83  pat::TauRef inputTauRef(inputTaus, tau_idx);
84  size_t nTauIds = inputTau->tauIDs().size();
85  std::vector<pat::Tau::IdPair> tauIds(nTauIds + nNewTauIds_);
86 
87  // copy IDs that are already stored in PAT taus
88  for (size_t i = 0; i < nTauIds; ++i) {
89  tauIds[i] = inputTau->tauIDs().at(i);
90  }
91 
92  // store IDs that were produced in PATTauDiscriminator format
94  for (size_t i = 0; i < nNewPlainTauIds_; ++i) {
95  evt.getByToken(patTauIDTokens_[i], tauDiscr);
96  tauIds[nTauIds + i].first = tauIDSrcs_[i].first;
97  tauIds[nTauIds + i].second = (*tauDiscr)[inputTauRef];
98  }
99 
100  // store IDs that were produced in PATTauDiscriminatorContainer format
101  size_t nEmbeddedIDs = nTauIds + nNewPlainTauIds_;
102  for (size_t i = 0; i < tauIDSrcContainers_.size(); ++i) {
103  auto const& tauDiscrCont = evt.get(patTauIDContainerTokens_[i]);
104  for (size_t j = 0; j < tauIDSrcContainers_[i].size(); ++j) {
105  tauIds[nEmbeddedIDs + j].first = tauIDSrcContainers_[i][j].first;
106  int wpIdx = tauIDSrcContainers_[i][j].second;
107  if (wpIdx < 0) {
108  if (tauDiscrCont[inputTauRef].rawValues.size() == 1)
109  //Only 0th component filled with default value if prediscriminor in PatTauDiscriminator failed.
110  tauIds[nEmbeddedIDs + j].second = tauDiscrCont[inputTauRef].rawValues.at(0);
111  else
112  //uses negative indices to access rawValues. In most cases only one rawValue at WPIdx=-1 exists.
113  tauIds[nEmbeddedIDs + j].second = tauDiscrCont[inputTauRef].rawValues.at(-1 - wpIdx);
114  } else {
115  if (tauDiscrCont[inputTauRef].workingPoints.empty())
116  //WP vector not filled if prediscriminor in PatTauDiscriminator failed. Set PAT output to false in this case
117  tauIds[nEmbeddedIDs + j].second = 0.0;
118  else
119  tauIds[nEmbeddedIDs + j].second = tauDiscrCont[inputTauRef].workingPoints.at(wpIdx);
120  }
121  }
122  nEmbeddedIDs += tauIDSrcContainers_[i].size();
123  }
124 
125  outputTau.setTauIDs(tauIds);
126  outputTaus->push_back(outputTau);
127  }
128 
129  evt.put(std::move(outputTaus));
130 }
131 
133 
std::vector< edm::EDGetTokenT< pat::PATTauDiscriminator > > patTauIDTokens_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
std::pair< edm::InputTag, std::vector< NameWPIdx > > IDContainerData
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:344
std::vector< NameTag > tauIDSrcs_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
void setTauIDs(const std::vector< IdPair > &ids)
Definition: Tau.h:352
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
const std::string names[nVars_]
~PATTauIDEmbedder() override
U second(std::pair< T, U > const &p)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Analysis-level tau class.
Definition: Tau.h:53
PATTauIDEmbedder(const edm::ParameterSet &)
edm::EDGetTokenT< pat::TauCollection > src_
std::pair< std::string, int > NameWPIdx
void produce(edm::Event &, const edm::EventSetup &) override
std::vector< std::vector< NameWPIdx > > tauIDSrcContainers_
std::pair< std::string, edm::InputTag > NameTag
def move(src, dest)
Definition: eostools.py:511
cont
load Luminosity info ##
Definition: generateEDF.py:628
std::vector< edm::EDGetTokenT< reco::TauDiscriminatorContainer > > patTauIDContainerTokens_