CMS 3D CMS Logo

PATGenericParticleProducer.cc
Go to the documentation of this file.
1 
30 
31 #include <memory>
32 
33 namespace pat {
34 
36  public:
37  explicit PATGenericParticleProducer(const edm::ParameterSet& iConfig);
38  ~PATGenericParticleProducer() override;
39 
40  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
41 
42  private:
43  // configurables
45 
46  // embed RECo objects
49 
52 
55  std::vector<edm::EDGetTokenT<edm::Association<reco::GenParticleCollection> > > genMatchTokens_;
56 
57  // tools
59 
61  pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_; // better here than recreate at each event
62  std::vector<std::pair<pat::IsolationKeys, edm::InputTag> > isoDepositLabels_;
63  std::vector<edm::EDGetTokenT<edm::ValueMap<IsoDeposit> > > isoDepositTokens_;
64 
67 
70 
72 
75  };
76 
77 } // namespace pat
78 
79 using namespace pat;
80 
82  : isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation")
83  : edm::ParameterSet(),
84  consumesCollector(),
85  false),
86  userDataHelper_(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector()) {
87  // initialize the configurables
88  srcToken_ = consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("src"));
89 
90  // RECO embedding
91  embedTrack_ = iConfig.getParameter<bool>("embedTrack");
92  embedGsfTrack_ = iConfig.getParameter<bool>("embedGsfTrack");
93  embedStandalone_ = iConfig.getParameter<bool>("embedStandAloneMuon");
94  embedCombined_ = iConfig.getParameter<bool>("embedCombinedMuon");
95  embedSuperCluster_ = iConfig.getParameter<bool>("embedSuperCluster");
96  embedTracks_ = iConfig.getParameter<bool>("embedMultipleTracks");
97  embedCaloTower_ = iConfig.getParameter<bool>("embedCaloTower");
98 
99  // MC matching configurables
100  addGenMatch_ = iConfig.getParameter<bool>("addGenMatch");
101  if (addGenMatch_) {
102  embedGenMatch_ = iConfig.getParameter<bool>("embedGenMatch");
103  if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
105  iConfig.getParameter<edm::InputTag>("genParticleMatch")));
106  } else {
108  iConfig.getParameter<std::vector<edm::InputTag> >("genParticleMatch"),
109  [this](edm::InputTag const& tag) { return consumes<edm::Association<reco::GenParticleCollection> >(tag); });
110  }
111  }
112 
113  // quality
114  addQuality_ = iConfig.getParameter<bool>("addQuality");
115  qualitySrcToken_ = mayConsume<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("qualitySource"));
116 
117  // produces vector of particles
118  produces<std::vector<GenericParticle> >();
119 
120  if (iConfig.exists("isoDeposits")) {
121  edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
122  if (depconf.exists("tracker"))
123  isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
124  if (depconf.exists("ecal"))
125  isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
126  if (depconf.exists("hcal"))
127  isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
128  if (depconf.exists("user")) {
129  std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
130  std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
131  int key = UserBaseIso;
132  for (; it != ed; ++it, ++key) {
133  isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
134  }
135  }
136  }
138  edm::vector_transform(isoDepositLabels_, [this](std::pair<IsolationKeys, edm::InputTag> const& label) {
139  return consumes<edm::ValueMap<IsoDeposit> >(label.second);
140  });
141 
142  // Efficiency configurables
143  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
144  if (addEfficiencies_) {
146  pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
147  }
148 
149  // Resolution configurables
150  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
151  if (addResolutions_) {
153  pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"), consumesCollector());
154  }
155 
156  if (iConfig.exists("vertexing")) {
158  pat::helper::VertexingHelper(iConfig.getParameter<edm::ParameterSet>("vertexing"), consumesCollector());
159  }
160 
161  // Check to see if the user wants to add user data
162  useUserData_ = false;
163  if (iConfig.exists("userData")) {
164  useUserData_ = true;
165  }
166 }
167 
169 
171  // Get the vector of GenericParticle's from the event
173  iEvent.getByToken(srcToken_, cands);
174 
175  // prepare isolation
176  if (isolator_.enabled())
177  isolator_.beginEvent(iEvent, iSetup);
178 
185 
186  // prepare IsoDeposits
187  std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositTokens_.size());
188  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
189  iEvent.getByToken(isoDepositTokens_[j], deposits[j]);
190  }
191 
192  // prepare the MC matching
193  std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchTokens_.size());
194  if (addGenMatch_) {
195  for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
196  iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
197  }
198  }
199 
200  // prepare the quality
202  if (addQuality_)
203  iEvent.getByToken(qualitySrcToken_, qualities);
204 
205  // loop over cands
206  std::vector<GenericParticle>* PATGenericParticles = new std::vector<GenericParticle>();
207  for (edm::View<reco::Candidate>::const_iterator itGenericParticle = cands->begin(); itGenericParticle != cands->end();
208  itGenericParticle++) {
209  // construct the GenericParticle from the ref -> save ref to original object
210  unsigned int idx = itGenericParticle - cands->begin();
211  edm::RefToBase<reco::Candidate> candRef = cands->refAt(idx);
212 
213  PATGenericParticles->push_back(GenericParticle(candRef));
214  GenericParticle& aGenericParticle = PATGenericParticles->back();
215 
216  // embed RECO
217  if (embedTrack_)
218  aGenericParticle.embedTrack();
219  if (embedGsfTrack_)
220  aGenericParticle.embedGsfTrack();
221  if (embedTracks_)
222  aGenericParticle.embedTracks();
223  if (embedStandalone_)
224  aGenericParticle.embedStandalone();
225  if (embedCombined_)
226  aGenericParticle.embedCombined();
227  if (embedSuperCluster_)
228  aGenericParticle.embedSuperCluster();
229  if (embedCaloTower_)
230  aGenericParticle.embedCaloTower();
231 
232  // isolation
233  if (isolator_.enabled()) {
235  typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
236  // better to loop backwards, so the vector is resized less times
237  for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(),
238  ed = isolatorTmpStorage_.rend();
239  it != ed;
240  ++it) {
241  aGenericParticle.setIsolation(it->first, it->second);
242  }
243  }
244 
245  // isodeposit
246  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
247  aGenericParticle.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[candRef]);
248  }
249 
250  // store the match to the generated final state muons
251  if (addGenMatch_) {
252  for (size_t i = 0, n = genMatches.size(); i < n; ++i) {
253  reco::GenParticleRef genGenericParticle = (*genMatches[i])[candRef];
254  aGenericParticle.addGenParticleRef(genGenericParticle);
255  }
256  if (embedGenMatch_)
257  aGenericParticle.embedGenParticle();
258  }
259 
260  if (addQuality_) {
261  aGenericParticle.setQuality((*qualities)[candRef]);
262  }
263 
264  if (efficiencyLoader_.enabled()) {
265  efficiencyLoader_.setEfficiencies(aGenericParticle, candRef);
266  }
267 
268  if (resolutionLoader_.enabled()) {
269  resolutionLoader_.setResolutions(aGenericParticle);
270  }
271 
272  if (vertexingHelper_.enabled()) {
273  aGenericParticle.setVertexAssociation(vertexingHelper_(candRef));
274  }
275 
276  if (useUserData_) {
277  userDataHelper_.add(aGenericParticle, iEvent, iSetup);
278  }
279 
280  // PATGenericParticles->push_back(aGenericParticle); // NOOOOO!!!!
281  // We have already pushed_back this generic particle in the collection
282  // (we first push an empty particle and then fill it, to avoid useless copies)
283  }
284 
285  // sort GenericParticles in ET
286  std::sort(PATGenericParticles->begin(), PATGenericParticles->end(), eTComparator_);
287 
288  // put genEvt object in Event
289  std::unique_ptr<std::vector<GenericParticle> > myGenericParticles(PATGenericParticles);
290  iEvent.put(std::move(myGenericParticles));
291  if (isolator_.enabled())
293 }
294 
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
void newEvent(const edm::Event &event)
To be called for each new event, reads in the ValueMaps for efficiencies.
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void setIsolation(IsolationKeys key, float value)
bool enabled() const
True if it has a non null configuration.
Definition: MultiIsolator.h:55
void setEfficiencies(pat::PATObject< T > &obj, const R &originalRef) const
Sets the efficiencies for this object, using the reference to the original objects.
bool exists(std::string const &parameterName) const
checks if a parameter exists
void embedTrack()
embeds the master track instead of keeping a reference to it
void setResolutions(pat::PATObject< T > &obj) const
Sets the efficiencies for this object, using the reference to the original objects.
IsolationKeys
Enum defining isolation keys.
Definition: Isolation.h:9
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:171
std::vector< edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > > genMatchTokens_
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void fill(const edm::View< T > &coll, int idx, IsolationValuePairs &isolations) const
Definition: MultiIsolator.h:84
Definition: HeavyIon.h:7
char const * label
Analysis-level Generic Particle class (e.g. for hadron or muon not fully reconstructed) ...
pat::PATUserDataHelper< pat::GenericParticle > userDataHelper_
int iEvent
Definition: GenABIO.cc:224
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup)
void embedStandalone()
embeds the stand-alone track instead of keeping a reference to it
void embedGenParticle()
Definition: PATObject.h:768
pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_
edm::EDGetTokenT< edm::View< reco::Candidate > > srcToken_
void newEvent(const edm::Event &event, const edm::EventSetup &setup)
To be called for each new event, reads in the EventSetup object.
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
PATGenericParticleProducer(const edm::ParameterSet &iConfig)
GreaterByEt< GenericParticle > eTComparator_
Produces the pat::GenericParticle.
bool enabled() const
returns true if this was given a non dummy configuration
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void add(ObjectType &patObject, edm::Event const &iEvent, edm::EventSetup const &iSetup)
const char * qualities[3]
std::vector< edm::EDGetTokenT< edm::ValueMap< IsoDeposit > > > isoDepositTokens_
void embedGsfTrack()
embeds the gsf track instead of keeping a reference to it
void embedSuperCluster()
embeds the supercluster instead of keeping a reference to it
std::vector< std::pair< pat::IsolationKeys, float > > IsolationValuePairs
Definition: MultiIsolator.h:17
void addGenParticleRef(const reco::GenParticleRef &ref)
Definition: PATObject.h:751
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void embedTracks()
embeds the other tracks instead of keeping references
void newEvent(const edm::Event &event)
To be called for each new event, reads in the vertex collection.
std::vector< std::pair< pat::IsolationKeys, edm::InputTag > > isoDepositLabels_
pat::helper::EfficiencyLoader efficiencyLoader_
pat::helper::VertexingHelper vertexingHelper_
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep)
Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
void embedCaloTower()
embeds the calotower instead of keeping a reference to it
void setQuality(float quality)
sets a user defined quality value
void embedCombined()
embeds the combined track instead of keeping a reference to it
pat::helper::KinResolutionsLoader resolutionLoader_
edm::EDGetTokenT< edm::ValueMap< float > > qualitySrcToken_
def move(src, dest)
Definition: eostools.py:511
Produces and/or checks pat::VertexAssociation&#39;s.
void setVertexAssociation(const pat::VertexAssociation &assoc)
Set a single vertex association.