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