CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
8 
10 
15 
17 
18 
19 #include <vector>
20 
21 
22 namespace pat {
23 
24  class PATJetSelector : public edm::EDFilter {
25  public:
26 
27 
28  PATJetSelector( edm::ParameterSet const & params ) :
29  edm::EDFilter( ),
30  srcToken_(consumes<edm::View<pat::Jet> >( params.getParameter<edm::InputTag>("src") )),
31  cut_( params.getParameter<std::string>("cut") ),
32  filter_(false),
33  selector_( cut_ )
34  {
35  produces< std::vector<pat::Jet> >();
36  produces<reco::GenJetCollection> ("genJets");
37  produces<std::vector<CaloTower> > ("caloTowers");
38  produces<reco::PFCandidateCollection > ("pfCandidates");
39  produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
40 
41  if ( params.exists("filter") ) {
42  filter_ = params.getParameter<bool>("filter");
43  }
44  }
45 
46  virtual ~PATJetSelector() {}
47 
48  virtual void beginJob() {}
49  virtual void endJob() {}
50 
51  virtual bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
52 
53  std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() );
54 
55  std::auto_ptr<reco::GenJetCollection > genJetsOut ( new reco::GenJetCollection() );
56  std::auto_ptr<std::vector<CaloTower> > caloTowersOut( new std::vector<CaloTower> () );
57  std::auto_ptr<reco::PFCandidateCollection > pfCandidatesOut( new reco::PFCandidateCollection() );
58  std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );
59 
60 
62  edm::RefProd<std::vector<CaloTower> > h_caloTowersOut = iEvent.getRefBeforePut<std::vector<CaloTower> > ( "caloTowers" );
63  edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
65 
67  iEvent.getByToken( srcToken_, h_jets );
68 
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  // Check the selection
75  if ( selector_(*ijet) ) {
76  // Copy over the calo towers
77  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
78  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
79  itower != itowerEnd; ++itower ) {
80  // Add to global calo tower list
81  caloTowersOut->push_back( **itower );
82  }
83 
84 
85  // Copy over the pf candidates
86  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
87  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
88  icand != icandEnd; ++icand ) {
89  // Add to global pf candidate list
90  pfCandidatesOut->push_back( **icand );
91  }
92 
93  // Copy the tag infos
94  for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
95  iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
96  iinfo != iinfoEnd; ++iinfo ) {
97  // Add to global calo tower list
98  tagInfosOut->push_back( **iinfo );
99  }
100 
101  // Copy the gen jet
102  if ( ijet->genJet() != 0 ) {
103  genJetsOut->push_back( *(ijet->genJet()) );
104  }
105 
106  }
107  }
108 
109 
110  // Output the secondary collections.
111  edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put( genJetsOut, "genJets" );
112  edm::OrphanHandle<std::vector<CaloTower> > oh_caloTowersOut = iEvent.put( caloTowersOut, "caloTowers" );
113  edm::OrphanHandle<reco::PFCandidateCollection> oh_pfCandidatesOut = iEvent.put( pfCandidatesOut, "pfCandidates" );
114  edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo> > oh_tagInfosOut = iEvent.put( tagInfosOut, "tagInfos" );
115 
116 
117 
118 
119 
120  unsigned int caloTowerIndex = 0;
121  unsigned int pfCandidateIndex = 0;
122  unsigned int tagInfoIndex = 0;
123  unsigned int genJetIndex = 0;
124  // Now set the Ptrs with the orphan handles.
125  for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
126  iend = h_jets->end(), ijet = ibegin;
127  ijet != iend; ++ijet ) {
128 
129  // Check the selection
130  if ( selector_(*ijet) ) {
131  // Add the jets that pass to the output collection
132  patJets->push_back( *ijet );
133 
134  // Copy over the calo towers
135  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
136  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
137  itower != itowerEnd; ++itower ) {
138  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
139 
140  // ptr to "this" tower in the global list
141  edm::Ptr<CaloTower> outPtr( oh_caloTowersOut, caloTowerIndex);
142  patJets->back().updateFwdCaloTowerFwdPtr( itower - itowerBegin,// index of "this" tower in the jet
143  outPtr
144  );
145  ++caloTowerIndex;
146  }
147 
148 
149  // Copy over the pf candidates
150  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
151  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
152  icand != icandEnd; ++icand ) {
153  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
154 
155  // ptr to "this" cand in the global list
156  edm::Ptr<reco::PFCandidate> outPtr( oh_pfCandidatesOut, pfCandidateIndex );
157  patJets->back().updateFwdPFCandidateFwdPtr( icand - icandBegin,// index of "this" tower in the jet
158  outPtr
159  );
160  ++pfCandidateIndex;
161  }
162 
163  // Copy the tag infos
164  for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
165  iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
166  iinfo != iinfoEnd; ++iinfo ) {
167  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
168 
169  // ptr to "this" info in the global list
170  edm::Ptr<reco::BaseTagInfo > outPtr( oh_tagInfosOut, tagInfoIndex );
171  patJets->back().updateFwdTagInfoFwdPtr( iinfo - iinfoBegin,// index of "this" tower in the jet
172  outPtr
173  );
174  ++tagInfoIndex;
175  }
176 
177  // Copy the gen jet
178  if ( ijet->genJet() != 0 ) {
179  patJets->back().updateFwdGenJetFwdRef( edm::Ref<reco::GenJetCollection>( oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
180  );
181  ++genJetIndex;
182  }
183 
184  }
185  }
186 
187 
188  // put genEvt in Event
189  bool pass = patJets->size() > 0;
190  iEvent.put(patJets);
191 
192  if ( filter_ )
193  return pass;
194  else
195  return true;
196  }
197 
198  protected:
201  bool filter_;
203  };
204 
205 }
206 
207 
208 #endif
T getParameter(std::string const &) const
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
virtual void endJob()
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
std::vector< GenJet > GenJetCollection
collection of GenJet objects
bool exists(std::string const &parameterName) const
checks if a parameter exists
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
iterator begin()
Definition: OwnVector.h:227
virtual void beginJob()
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
StringCutObjectSelector< Jet > selector_
RefProd< PROD > getRefBeforePut()
Definition: Event.h:128
virtual ~PATJetSelector()
edm::EDGetTokenT< edm::View< pat::Jet > > srcToken_
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
iterator end()
Definition: OwnVector.h:232
Analysis-level calorimeter jet class.
Definition: Jet.h:73
volatile std::atomic< bool > shutdown_flag false
PATJetSelector(edm::ParameterSet const &params)