CMS 3D CMS Logo

ProbeTreeProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ProbeTreeProducer
4 // Class: ProbeTreeProducer
5 //
14 #include <memory>
15 #include <cctype>
25 #include <set>
26 
28 public:
29  explicit ProbeTreeProducer(const edm::ParameterSet&);
30  ~ProbeTreeProducer() override;
31 
32 private:
33  bool filter(edm::Event&, const edm::EventSetup&) override;
34  void endJob() override;
35 
38 
41 
43  bool filter_;
44 
47 
50 
52  int32_t maxProbes_;
53 
55  std::unique_ptr<tnp::BaseTreeFiller> probeFiller_;
56 };
57 
59  : probesToken_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("src"))),
60  cut_(iConfig.existsAs<std::string>("cut") ? iConfig.getParameter<std::string>("cut") : ""),
61  filter_(iConfig.existsAs<bool>("filter") ? iConfig.getParameter<bool>("filter") : false),
62  sortDescendingBy_(iConfig.existsAs<std::string>("sortDescendingBy")
63  ? iConfig.getParameter<std::string>("sortDescendingBy")
64  : ""),
65  sortFunction_(!sortDescendingBy_.empty() ? sortDescendingBy_ : "pt"), //need to pass a valid default
66  maxProbes_(iConfig.existsAs<int32_t>("maxProbes") ? iConfig.getParameter<int32_t>("maxProbes") : -1),
67  probeFiller_(new tnp::BaseTreeFiller("probe_tree", iConfig, consumesCollector())) {}
68 
70 
72  bool result = !filter_;
74  iEvent.getByToken(probesToken_, probes);
75  if (!probes.isValid())
76  return result;
77  probeFiller_->init(iEvent);
78  // select probes and calculate the sorting value
79  typedef std::pair<reco::CandidateBaseRef, double> Pair;
80  std::vector<Pair> selectedProbes;
81  for (size_t i = 0; i < probes->size(); ++i) {
82  const reco::CandidateBaseRef& probe = probes->refAt(i);
83  if (cut_(*probe)) {
84  selectedProbes.push_back(Pair(probe, sortFunction_(*probe)));
85  }
86  }
87  // sort only if a function was provided
88  if (!sortDescendingBy_.empty())
89  sort(selectedProbes.begin(), selectedProbes.end(), [&](auto& arg1, auto& arg2) {
90  return arg1.second > arg2.second;
91  });
92  // fill the first maxProbes_ into the tree
93  for (size_t i = 0; i < (maxProbes_ < 0 ? selectedProbes.size() : std::min((size_t)maxProbes_, selectedProbes.size()));
94  ++i) {
95  probeFiller_->fill(selectedProbes[i].first);
96  result = true;
97  }
98  return result;
99 }
100 
102  // ask to write the current PSet info into the TTree header
104 }
105 
106 //define this as a plug-in
RefToBase< value_type > refAt(size_type i) const
StringObjectFunction< reco::Candidate, true > sortFunction_
The StringObjectFunction itself.
ProbeTreeProducer(const edm::ParameterSet &)
int32_t maxProbes_
The number of first probes used to fill the tree.
size_type size() const
void endJob() override
~ProbeTreeProducer() override
int iEvent
Definition: GenABIO.cc:224
bool filter(edm::Event &, const edm::EventSetup &) override
std::unique_ptr< tnp::BaseTreeFiller > probeFiller_
The object that actually computes variables and fills the tree for the probe.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ParameterSet const & getProcessParameterSetContainingModule(ModuleDescription const &moduleDescription)
StringCutObjectSelector< reco::Candidate, true > cut_
The selector object.
bool isValid() const
Definition: HandleBase.h:70
ModuleDescription const & moduleDescription() const
Definition: EDFilterBase.h:65
fixed size matrix
HLT enums.
std::string sortDescendingBy_
Name of the reco::Candidate function used for sorting.
bool filter_
Specifies whether this module should filter.
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23
edm::EDGetTokenT< reco::CandidateView > probesToken_
InputTag to the collection of all probes.