CMS 3D CMS Logo

PATJetSelector.h
Go to the documentation of this file.
1 //
2 //
3 
4 #ifndef PhysicsTools_PatAlgos_PATJetSelector_h
5 #define PhysicsTools_PatAlgos_PATJetSelector_h
6 
9 
11 
16 
18 
19 #include <vector>
20 
21 namespace pat {
22 
24  public:
26  : srcToken_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("src"))),
27  cut_(params.getParameter<std::string>("cut")),
28  cutLoose_(params.getParameter<std::string>("cutLoose")),
29  filter_(params.exists("filter") ? params.getParameter<bool>("filter") : false),
30  nLoose_(params.getParameter<unsigned>("nLoose")),
31  selector_(cut_),
33  produces<std::vector<pat::Jet>>();
34  produces<reco::GenJetCollection>("genJets");
35  produces<std::vector<CaloTower>>("caloTowers");
36  produces<reco::PFCandidateCollection>("pfCandidates");
37  produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
38  }
39 
40  ~PATJetSelector() override {}
41 
42  virtual void beginJob() {}
43  virtual void endJob() {}
44 
45  bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
46  auto patJets = std::make_unique<std::vector<Jet>>();
47 
48  auto genJetsOut = std::make_unique<reco::GenJetCollection>();
49  auto caloTowersOut = std::make_unique<std::vector<CaloTower>>();
50  auto pfCandidatesOut = std::make_unique<reco::PFCandidateCollection>();
51  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
52 
53  edm::RefProd<reco::GenJetCollection> h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection>("genJets");
54  edm::RefProd<std::vector<CaloTower>> h_caloTowersOut =
55  iEvent.getRefBeforePut<std::vector<CaloTower>>("caloTowers");
57  iEvent.getRefBeforePut<reco::PFCandidateCollection>("pfCandidates");
59  iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
60 
62  iEvent.getByToken(srcToken_, h_jets);
63 
64  unsigned nl = 0; // number of loose jets
65  // First loop over the products and make the secondary output collections
66  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
67  ijet != iend;
68  ++ijet) {
69  bool selectedLoose = false;
70  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
71  selectedLoose = true;
72  ++nl;
73  }
74 
75  if (selector_(*ijet) || selectedLoose) {
76  // Copy over the calo towers
77  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
78  itowerEnd = ijet->caloTowersFwdPtr().end(),
79  itower = itowerBegin;
80  itower != itowerEnd;
81  ++itower) {
82  // Add to global calo tower list
83  caloTowersOut->push_back(**itower);
84  }
85 
86  // Copy over the pf candidates
87  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
88  icandEnd = ijet->pfCandidatesFwdPtr().end(),
89  icand = icandBegin;
90  icand != icandEnd;
91  ++icand) {
92  // Add to global pf candidate list
93  pfCandidatesOut->push_back(**icand);
94  }
95 
96  // Copy the tag infos
97  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
98  iinfoEnd = ijet->tagInfosFwdPtr().end(),
99  iinfo = iinfoBegin;
100  iinfo != iinfoEnd;
101  ++iinfo) {
102  // Add to global calo tower list
103  tagInfosOut->push_back(**iinfo);
104  }
105 
106  // Copy the gen jet
107  if (ijet->genJet() != nullptr) {
108  genJetsOut->push_back(*(ijet->genJet()));
109  }
110  }
111  }
112 
113  // Output the secondary collections.
114  edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put(std::move(genJetsOut), "genJets");
115  edm::OrphanHandle<std::vector<CaloTower>> oh_caloTowersOut = iEvent.put(std::move(caloTowersOut), "caloTowers");
117  iEvent.put(std::move(pfCandidatesOut), "pfCandidates");
119  iEvent.put(std::move(tagInfosOut), "tagInfos");
120 
121  unsigned int caloTowerIndex = 0;
122  unsigned int pfCandidateIndex = 0;
123  unsigned int tagInfoIndex = 0;
124  unsigned int genJetIndex = 0;
125  // Now set the Ptrs with the orphan handles.
126  nl = 0; // Reset number of loose jets
127  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
128  ijet != iend;
129  ++ijet) {
130  bool selectedLoose = false;
131  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
132  selectedLoose = true;
133  ++nl;
134  }
135 
136  if (selector_(*ijet) || selectedLoose) {
137  // Add the jets that pass to the output collection
138  patJets->push_back(*ijet);
139 
140  // Copy over the calo towers
141  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
142  itowerEnd = ijet->caloTowersFwdPtr().end(),
143  itower = itowerBegin;
144  itower != itowerEnd;
145  ++itower) {
146  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
147 
148  // ptr to "this" tower in the global list
149  edm::Ptr<CaloTower> outPtr(oh_caloTowersOut, caloTowerIndex);
150  patJets->back().updateFwdCaloTowerFwdPtr(itower - itowerBegin, // index of "this" tower in the jet
151  outPtr);
152  ++caloTowerIndex;
153  }
154 
155  // Copy over the pf candidates
156  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
157  icandEnd = ijet->pfCandidatesFwdPtr().end(),
158  icand = icandBegin;
159  icand != icandEnd;
160  ++icand) {
161  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
162 
163  // ptr to "this" cand in the global list
164  edm::Ptr<reco::PFCandidate> outPtr(oh_pfCandidatesOut, pfCandidateIndex);
165  patJets->back().updateFwdPFCandidateFwdPtr(icand - icandBegin, // index of "this" tower in the jet
166  outPtr);
167  ++pfCandidateIndex;
168  }
169 
170  // Copy the tag infos
171  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
172  iinfoEnd = ijet->tagInfosFwdPtr().end(),
173  iinfo = iinfoBegin;
174  iinfo != iinfoEnd;
175  ++iinfo) {
176  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
177 
178  // ptr to "this" info in the global list
179  edm::Ptr<reco::BaseTagInfo> outPtr(oh_tagInfosOut, tagInfoIndex);
180  patJets->back().updateFwdTagInfoFwdPtr(iinfo - iinfoBegin, // index of "this" tower in the jet
181  outPtr);
182  ++tagInfoIndex;
183  }
184 
185  // Copy the gen jet
186  if (ijet->genJet() != nullptr) {
187  patJets->back().updateFwdGenJetFwdRef(
188  edm::Ref<reco::GenJetCollection>(oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
189  );
190  ++genJetIndex;
191  }
192  }
193  }
194 
195  // put genEvt in Event
196  bool pass = !patJets->empty();
197  iEvent.put(std::move(patJets));
198 
199  if (filter_)
200  return pass;
201  else
202  return true;
203  }
204 
207  iDesc.setComment("Energy Correlation Functions adder");
208  iDesc.add<edm::InputTag>("src", edm::InputTag("no default"))->setComment("input collection");
209  iDesc.add<std::string>("cut", "")->setComment("Jet selection.");
210  iDesc.add<std::string>("cutLoose", "")->setComment("Loose jet selection. Will keep nLoose loose jets.");
211  iDesc.add<bool>("filter", false)->setComment("Filter selection?");
212  iDesc.add<unsigned>("nLoose", 0)->setComment("Keep nLoose loose jets that satisfy cutLoose");
213  descriptions.add("PATJetSelector", iDesc);
214  }
215 
216  protected:
219  const std::string cutLoose_; // Cut to define loose jets.
220  const bool filter_;
221  const unsigned nLoose_; // If desired, keep nLoose loose jets.
223  const StringCutObjectSelector<Jet> selectorLoose_; // Selector for loose jets.
224  };
225 
226 } // namespace pat
227 
228 #endif
ConfigurationDescriptions.h
edm::RefProd
Definition: EDProductfwd.h:25
ObjectSelector.h
electrons_cff.bool
bool
Definition: electrons_cff.py:372
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
funct::false
false
Definition: Factorize.h:34
pat::PATJetSelector::selector_
const StringCutObjectSelector< Jet > selector_
Definition: PATJetSelector.h:222
StringCutObjectSelector.h
edm::OwnVector::end
iterator end()
Definition: OwnVector.h:285
sistrip::View
View
Definition: ConstantsForView.h:26
reco::GenJetCollection
std::vector< GenJet > GenJetCollection
collection of GenJet objects
Definition: GenJetCollection.h:14
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
pat::PATJetSelector::filter_
const bool filter_
Definition: PATJetSelector.h:220
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
SingleObjectSelector.h
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
pat::PATJetSelector::endJob
virtual void endJob()
Definition: PATJetSelector.h:43
pat::PATJetSelector::nLoose_
const unsigned nLoose_
Definition: PATJetSelector.h:221
edm::Handle
Definition: AssociativeIterator.h:50
edm::Ref
Definition: AssociativeIterator.h:58
Jet
Definition: Jet.py:1
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
SingleElementCollectionSelector.h
pat::PATJetSelector::beginJob
virtual void beginJob()
Definition: PATJetSelector.h:42
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
RefVector.h
pat::PATJetSelector::cutLoose_
const std::string cutLoose_
Definition: PATJetSelector.h:219
pat::PATJetSelector
Definition: PATJetSelector.h:23
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup
Definition: EventSetup.h:57
pat
Definition: HeavyIon.h:7
pat::PATJetSelector::filter
bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
Definition: PATJetSelector.h:45
hemisphereProducer_cfi.patJets
patJets
Definition: hemisphereProducer_cfi.py:5
Jet.h
edm::Ptr
Definition: AssociationVector.h:31
pat::PATJetSelector::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PATJetSelector.h:205
pat::PATJetSelector::~PATJetSelector
~PATJetSelector() override
Definition: PATJetSelector.h:40
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::OrphanHandle
Definition: EDProductfwd.h:39
StringCutObjectSelector
Definition: StringCutObjectSelector.h:16
reco::PFCandidateCollection
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
Definition: PFCandidateFwd.h:12
pat::PATJetSelector::srcToken_
const edm::EDGetTokenT< edm::View< pat::Jet > > srcToken_
Definition: PATJetSelector.h:217
edm::View::const_iterator
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
pat::PATJetSelector::PATJetSelector
PATJetSelector(edm::ParameterSet const &params)
Definition: PATJetSelector.h:25
EDFilter.h
pat::PATJetSelector::cut_
const std::string cut_
Definition: PATJetSelector.h:218
edm::EDConsumerBase::consumes
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: EDConsumerBase.h:126
edm::OwnVector::begin
iterator begin()
Definition: OwnVector.h:280
edm::stream::EDFilter
Definition: EDFilter.h:38
edm::Event
Definition: Event.h:73
pat::PATJetSelector::selectorLoose_
const StringCutObjectSelector< Jet > selectorLoose_
Definition: PATJetSelector.h:223
edm::InputTag
Definition: InputTag.h:15
edm::OwnVector< reco::BaseTagInfo >