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