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>
16 #include "boost/bind.hpp"
26 #include <set>
27 
29  public:
30  explicit ProbeTreeProducer(const edm::ParameterSet&);
31  ~ProbeTreeProducer() override;
32 
33  private:
34  bool filter(edm::Event&, const edm::EventSetup&) override;
35  void endJob() override;
36 
39 
42 
44  bool filter_;
45 
48 
51 
53  int32_t maxProbes_;
54 
56  std::unique_ptr<tnp::BaseTreeFiller> probeFiller_;
57 };
58 
60  probesToken_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("src"))),
61  cut_(iConfig.existsAs<std::string>("cut") ? iConfig.getParameter<std::string>("cut") : ""),
62  filter_(iConfig.existsAs<bool>("filter") ? iConfig.getParameter<bool>("filter") : false),
63  sortDescendingBy_(iConfig.existsAs<std::string>("sortDescendingBy") ? iConfig.getParameter<std::string>("sortDescendingBy") : ""),
64  sortFunction_(!sortDescendingBy_.empty() ? sortDescendingBy_ : "pt"), //need to pass a valid default
65  maxProbes_(iConfig.existsAs<int32_t>("maxProbes") ? iConfig.getParameter<int32_t>("maxProbes") : -1),
66  probeFiller_(new tnp::BaseTreeFiller("probe_tree", iConfig, consumesCollector()))
67 {
68 }
69 
71 }
72 
74  bool result = !filter_;
76  iEvent.getByToken(probesToken_, probes);
77  if(!probes.isValid()) return result;
78  probeFiller_->init(iEvent);
79  // select probes and calculate the sorting value
80  typedef std::pair<reco::CandidateBaseRef, double> Pair;
81  std::vector<Pair> selectedProbes;
82  for (size_t i = 0; i < probes->size(); ++i){
83  const reco::CandidateBaseRef &probe = probes->refAt(i);
84  if(cut_(*probe)){
85  selectedProbes.push_back(Pair(probe, sortFunction_(*probe)));
86  }
87  }
88  // sort only if a function was provided
89  if(!sortDescendingBy_.empty()) sort(selectedProbes.begin(), selectedProbes.end(), boost::bind(&Pair::second, _1) > boost::bind(&Pair::second, _2));
90  // fill the first maxProbes_ into the tree
91  for (size_t i = 0; i < (maxProbes_<0 ? selectedProbes.size() : std::min((size_t)maxProbes_, selectedProbes.size())); ++i){
92  probeFiller_->fill(selectedProbes[i].first);
93  result = true;
94  }
95  return result;
96 }
97 
99  // ask to write the current PSet info into the TTree header
101 }
102 
103 //define this as a plug-in
105 
StringObjectFunction< reco::Candidate, true > sortFunction_
The StringObjectFunction itself.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
ProbeTreeProducer(const edm::ParameterSet &)
int32_t maxProbes_
The number of first probes used to fill the tree.
size_type size() const
ParameterSet const & getProcessParameterSetContainingModule(ModuleDescription const &moduleDescription)
void endJob() override
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
RefToBase< value_type > refAt(size_type i) const
~ProbeTreeProducer() override
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
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.
T min(T a, T b)
Definition: MathUtil.h:58
bool isValid() const
Definition: HandleBase.h:74
StringCutObjectSelector< reco::Candidate, true > cut_
The selector object.
fixed size matrix
HLT enums.
ModuleDescription const & moduleDescription() const
Definition: EDFilter.h:56
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.