CMS 3D CMS Logo

JetConstituentSelector.cc
Go to the documentation of this file.
1 /* \class PFJetSelector
2  *
3  * Selects jets with a configurable string-based cut,
4  * and also writes out the constituents of the jet
5  * into a separate collection.
6  *
7  * \author: Sal Rappoccio
8  *
9  *
10  * for more details about the cut syntax, see the documentation
11  * page below:
12  *
13  * https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsCutParser
14  *
15  */
16 
30 
31 
32 
33 
34 template <class T, typename C = std::vector<typename T::ConstituentTypeFwdPtr>>
36 public:
37 
38  using JetsOutput = std::vector<T>;
40  using ValueType = typename C::value_type;
41 
43  srcToken_{consumes<edm::View<T>>(params.getParameter<edm::InputTag>("src"))},
44  selector_{params.getParameter<std::string>("cut")},
45  unpackAK8_{params.getParameter<bool>("unpackAK8")}
46  {
47  produces<JetsOutput>();
48  produces<ConstituentsOutput>("constituents");
49  }
50 
52  {
54  desc.add<edm::InputTag>("src")->setComment("InputTag used for retrieving jets in event.");
55  desc.add<std::string>("cut")->setComment("Cut used by which to select jets. For example:\n"
56  " \"pt > 100.0 && abs(rapidity()) < 2.4\".");
57  desc.add<bool>("unpackAK8", false)->setComment("Assume the jets are AK8 jets from miniAOD and need to be unpacked.");
58 
59  // addDefault must be used here instead of add unless this function is specialized
60  // for different sets of template parameter types. Each specialization would need
61  // a different module label. Otherwise the generated cfi filenames will conflict
62  // for the different plugins.
63  descriptions.addDefault(desc);
64  }
65 
66  // Default initialization is for edm::FwdPtr. Specialization (below) is for edm::Ptr.
68  return typename ConstituentsOutput::value_type( dau, dau );
69  }
70 
71  void produce(edm::Event& iEvent, edm::EventSetup const& iSetup) override
72  {
73  auto jets = std::make_unique<JetsOutput>();
74  auto candsOut = std::make_unique<ConstituentsOutput>();
75 
77  iEvent.getByToken(srcToken_, h_jets);
78 
79 
80  // Now set the Ptrs with the orphan handles.
81  for (auto const& jet : *h_jets) {
82  // Check the selection
83  if (selector_(jet)) {
84  // Add the jets that pass to the output collection
85  jets->push_back(jet);
86 
87  if ( !unpackAK8_) {
88  for (unsigned int ida {}; ida < jet.numberOfDaughters(); ++ida) {
89  candsOut->emplace_back( initptr(jet.daughterPtr(ida)) );
90  }
91  }
92  else {
93  // Special case for AK8 pat::Jets from miniAOD, which store the subjets in the first two
94  // daughter links, and then the rest of the daughters that do not satisfy the jet groomer.
95  for (unsigned int ida {}; ida < jet.numberOfDaughters(); ++ida) {
96  auto const & dau = jet.daughterPtr(ida);
97  if ( dau->numberOfDaughters() == 0 ) {
98  candsOut->emplace_back( initptr(dau) );
99  } else {
100  auto const * dauJet = dynamic_cast<pat::Jet const *>(dau.get());
101  for ( unsigned int jda {}; jda < dauJet->numberOfDaughters(); ++jda ) {
102  candsOut->emplace_back( initptr(dauJet->daughterPtr(jda)) );
103  }
104  }
105  }
106  }
107  }
108  }
109 
110  iEvent.put(std::move(jets));
111  iEvent.put(std::move(candsOut), "constituents");
112  }
113 
114 private:
118 };
119 
120 template<>
123  edm::Ptr<pat::PackedCandidate> retval( dau );
124  return retval;
125 }
126 
127 template<>
130  edm::Ptr<pat::PackedGenParticle> retval( dau );
131  return retval;
132 }
133 
142 
T getParameter(std::string const &) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
StringCutObjectSelector< T > const selector_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
JetConstituentSelector(edm::ParameterSet const &params)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
vector< PseudoJet > jets
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::EDGetTokenT< edm::View< T > > const srcToken_
Analysis-level calorimeter jet class.
Definition: Jet.h:80
void produce(edm::Event &iEvent, edm::EventSetup const &iSetup) override
typename C::value_type ValueType
def move(src, dest)
Definition: eostools.py:510
ConstituentsOutput::value_type const initptr(edm::Ptr< reco::Candidate > const &dau) const